1 /*
2  * Mesa 3-D graphics library
3  *
4  * Copyright (C) 2009  VMware, Inc.  All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 /**
26  * \file shared.c
27  * Shared-context state
28  */
29 
30 
31 #include "mtypes.h"
32 #include "hash.h"
33 #include "atifragshader.h"
34 #include "bufferobj.h"
35 #include "shared.h"
36 #include "program/program.h"
37 #include "dlist.h"
38 #include "samplerobj.h"
39 #include "shaderapi.h"
40 #include "shaderobj.h"
41 #include "syncobj.h"
42 #include "texturebindless.h"
43 
44 #include "util/hash_table.h"
45 #include "util/set.h"
46 #include "util/u_memory.h"
47 
48 static void
49 free_shared_state(struct gl_context *ctx, struct gl_shared_state *shared);
50 
51 /**
52  * Allocate and initialize a shared context state structure.
53  * Initializes the display list, texture objects and vertex programs hash
54  * tables, allocates the texture objects. If it runs out of memory, frees
55  * everything already allocated before returning NULL.
56  *
57  * \return pointer to a gl_shared_state structure on success, or NULL on
58  * failure.
59  */
60 struct gl_shared_state *
_mesa_alloc_shared_state(struct gl_context * ctx)61 _mesa_alloc_shared_state(struct gl_context *ctx)
62 {
63    struct gl_shared_state *shared;
64    GLuint i;
65 
66    shared = CALLOC_STRUCT(gl_shared_state);
67    if (!shared)
68       return NULL;
69 
70    simple_mtx_init(&shared->Mutex, mtx_plain);
71 
72    shared->DisplayList = _mesa_NewHashTable();
73    shared->BitmapAtlas = _mesa_NewHashTable();
74    shared->TexObjects = _mesa_NewHashTable();
75    shared->Programs = _mesa_NewHashTable();
76 
77    shared->DefaultVertexProgram =
78       ctx->Driver.NewProgram(ctx, MESA_SHADER_VERTEX, 0, true);
79    shared->DefaultFragmentProgram =
80       ctx->Driver.NewProgram(ctx, MESA_SHADER_FRAGMENT, 0, true);
81 
82    shared->ATIShaders = _mesa_NewHashTable();
83    shared->DefaultFragmentShader = _mesa_new_ati_fragment_shader(ctx, 0);
84 
85    shared->ShaderObjects = _mesa_NewHashTable();
86 
87    shared->BufferObjects = _mesa_NewHashTable();
88 
89    /* GL_ARB_sampler_objects */
90    shared->SamplerObjects = _mesa_NewHashTable();
91 
92    /* GL_ARB_bindless_texture */
93    _mesa_init_shared_handles(shared);
94 
95    /* ARB_shading_language_include */
96    _mesa_init_shader_includes(shared);
97    mtx_init(&shared->ShaderIncludeMutex, mtx_plain);
98 
99    /* Create default texture objects */
100    for (i = 0; i < NUM_TEXTURE_TARGETS; i++) {
101       /* NOTE: the order of these enums matches the TEXTURE_x_INDEX values */
102       static const GLenum targets[] = {
103          GL_TEXTURE_2D_MULTISAMPLE,
104          GL_TEXTURE_2D_MULTISAMPLE_ARRAY,
105          GL_TEXTURE_CUBE_MAP_ARRAY,
106          GL_TEXTURE_BUFFER,
107          GL_TEXTURE_2D_ARRAY_EXT,
108          GL_TEXTURE_1D_ARRAY_EXT,
109          GL_TEXTURE_EXTERNAL_OES,
110          GL_TEXTURE_CUBE_MAP,
111          GL_TEXTURE_3D,
112          GL_TEXTURE_RECTANGLE_NV,
113          GL_TEXTURE_2D,
114          GL_TEXTURE_1D
115       };
116       STATIC_ASSERT(ARRAY_SIZE(targets) == NUM_TEXTURE_TARGETS);
117       shared->DefaultTex[i] = ctx->Driver.NewTextureObject(ctx, 0, targets[i]);
118       /* Need to explicitly set/overwrite the TargetIndex field here since
119        * the call to _mesa_tex_target_to_index() in NewTextureObject() may
120        * fail if the texture target is not supported.
121        */
122       shared->DefaultTex[i]->TargetIndex = i;
123    }
124 
125    /* sanity check */
126    assert(shared->DefaultTex[TEXTURE_1D_INDEX]->RefCount == 1);
127 
128    /* Mutex and timestamp for texobj state validation */
129    mtx_init(&shared->TexMutex, mtx_recursive);
130    shared->TextureStateStamp = 0;
131 
132    shared->FrameBuffers = _mesa_NewHashTable();
133    shared->RenderBuffers = _mesa_NewHashTable();
134 
135    shared->SyncObjects = _mesa_set_create(NULL, _mesa_hash_pointer,
136                                           _mesa_key_pointer_equal);
137 
138    shared->MemoryObjects = _mesa_NewHashTable();
139    shared->SemaphoreObjects = _mesa_NewHashTable();
140 
141    return shared;
142 }
143 
144 
145 /**
146  * Callback for deleting a display list.  Called by _mesa_HashDeleteAll().
147  */
148 static void
delete_displaylist_cb(GLuint id,void * data,void * userData)149 delete_displaylist_cb(GLuint id, void *data, void *userData)
150 {
151    struct gl_display_list *list = (struct gl_display_list *) data;
152    struct gl_context *ctx = (struct gl_context *) userData;
153    _mesa_delete_list(ctx, list);
154 }
155 
156 
157 /**
158  * Callback for deleting a bitmap atlas.  Called by _mesa_HashDeleteAll().
159  */
160 static void
delete_bitmap_atlas_cb(GLuint id,void * data,void * userData)161 delete_bitmap_atlas_cb(GLuint id, void *data, void *userData)
162 {
163    struct gl_bitmap_atlas *atlas = (struct gl_bitmap_atlas *) data;
164    struct gl_context *ctx = (struct gl_context *) userData;
165    _mesa_delete_bitmap_atlas(ctx, atlas);
166 }
167 
168 
169 /**
170  * Callback for deleting a texture object.  Called by _mesa_HashDeleteAll().
171  */
172 static void
delete_texture_cb(GLuint id,void * data,void * userData)173 delete_texture_cb(GLuint id, void *data, void *userData)
174 {
175    struct gl_texture_object *texObj = (struct gl_texture_object *) data;
176    struct gl_context *ctx = (struct gl_context *) userData;
177    ctx->Driver.DeleteTexture(ctx, texObj);
178 }
179 
180 
181 /**
182  * Callback for deleting a program object.  Called by _mesa_HashDeleteAll().
183  */
184 static void
delete_program_cb(GLuint id,void * data,void * userData)185 delete_program_cb(GLuint id, void *data, void *userData)
186 {
187    struct gl_program *prog = (struct gl_program *) data;
188    struct gl_context *ctx = (struct gl_context *) userData;
189    if(prog != &_mesa_DummyProgram) {
190       assert(prog->RefCount == 1); /* should only be referenced by hash table */
191       prog->RefCount = 0;  /* now going away */
192       ctx->Driver.DeleteProgram(ctx, prog);
193    }
194 }
195 
196 
197 /**
198  * Callback for deleting an ATI fragment shader object.
199  * Called by _mesa_HashDeleteAll().
200  */
201 static void
delete_fragshader_cb(GLuint id,void * data,void * userData)202 delete_fragshader_cb(GLuint id, void *data, void *userData)
203 {
204    struct ati_fragment_shader *shader = (struct ati_fragment_shader *) data;
205    struct gl_context *ctx = (struct gl_context *) userData;
206    _mesa_delete_ati_fragment_shader(ctx, shader);
207 }
208 
209 
210 /**
211  * Callback for deleting a buffer object.  Called by _mesa_HashDeleteAll().
212  */
213 static void
delete_bufferobj_cb(GLuint id,void * data,void * userData)214 delete_bufferobj_cb(GLuint id, void *data, void *userData)
215 {
216    struct gl_buffer_object *bufObj = (struct gl_buffer_object *) data;
217    struct gl_context *ctx = (struct gl_context *) userData;
218 
219    _mesa_buffer_unmap_all_mappings(ctx, bufObj);
220    _mesa_reference_buffer_object(ctx, &bufObj, NULL);
221 }
222 
223 
224 /**
225  * Callback for freeing shader program data. Call it before delete_shader_cb
226  * to avoid memory access error.
227  */
228 static void
free_shader_program_data_cb(GLuint id,void * data,void * userData)229 free_shader_program_data_cb(GLuint id, void *data, void *userData)
230 {
231    struct gl_context *ctx = (struct gl_context *) userData;
232    struct gl_shader_program *shProg = (struct gl_shader_program *) data;
233 
234    if (shProg->Type == GL_SHADER_PROGRAM_MESA) {
235        _mesa_free_shader_program_data(ctx, shProg);
236    }
237 }
238 
239 
240 /**
241  * Callback for deleting shader and shader programs objects.
242  * Called by _mesa_HashDeleteAll().
243  */
244 static void
delete_shader_cb(GLuint id,void * data,void * userData)245 delete_shader_cb(GLuint id, void *data, void *userData)
246 {
247    struct gl_context *ctx = (struct gl_context *) userData;
248    struct gl_shader *sh = (struct gl_shader *) data;
249    if (_mesa_validate_shader_target(ctx, sh->Type)) {
250       _mesa_delete_shader(ctx, sh);
251    }
252    else {
253       struct gl_shader_program *shProg = (struct gl_shader_program *) data;
254       assert(shProg->Type == GL_SHADER_PROGRAM_MESA);
255       _mesa_delete_shader_program(ctx, shProg);
256    }
257 }
258 
259 
260 /**
261  * Callback for deleting a framebuffer object.  Called by _mesa_HashDeleteAll()
262  */
263 static void
delete_framebuffer_cb(GLuint id,void * data,void * userData)264 delete_framebuffer_cb(GLuint id, void *data, void *userData)
265 {
266    struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
267    /* The fact that the framebuffer is in the hashtable means its refcount
268     * is one, but we're removing from the hashtable now.  So clear refcount.
269     */
270    /*assert(fb->RefCount == 1);*/
271    fb->RefCount = 0;
272 
273    /* NOTE: Delete should always be defined but there are two reports
274     * of it being NULL (bugs 13507, 14293).  Work-around for now.
275     */
276    if (fb->Delete)
277       fb->Delete(fb);
278 }
279 
280 
281 /**
282  * Callback for deleting a renderbuffer object. Called by _mesa_HashDeleteAll()
283  */
284 static void
delete_renderbuffer_cb(GLuint id,void * data,void * userData)285 delete_renderbuffer_cb(GLuint id, void *data, void *userData)
286 {
287    struct gl_context *ctx = (struct gl_context *) userData;
288    struct gl_renderbuffer *rb = (struct gl_renderbuffer *) data;
289    rb->RefCount = 0;  /* see comment for FBOs above */
290    if (rb->Delete)
291       rb->Delete(ctx, rb);
292 }
293 
294 
295 /**
296  * Callback for deleting a sampler object. Called by _mesa_HashDeleteAll()
297  */
298 static void
delete_sampler_object_cb(GLuint id,void * data,void * userData)299 delete_sampler_object_cb(GLuint id, void *data, void *userData)
300 {
301    struct gl_context *ctx = (struct gl_context *) userData;
302    struct gl_sampler_object *sampObj = (struct gl_sampler_object *) data;
303    _mesa_reference_sampler_object(ctx, &sampObj, NULL);
304 }
305 
306 /**
307  * Callback for deleting a memory object.  Called by _mesa_HashDeleteAll().
308  */
309 static void
delete_memory_object_cb(GLuint id,void * data,void * userData)310 delete_memory_object_cb(GLuint id, void *data, void *userData)
311 {
312    struct gl_memory_object *memObj = (struct gl_memory_object *) data;
313    struct gl_context *ctx = (struct gl_context *) userData;
314    ctx->Driver.DeleteMemoryObject(ctx, memObj);
315 }
316 
317 /**
318  * Callback for deleting a memory object.  Called by _mesa_HashDeleteAll().
319  */
320 static void
delete_semaphore_object_cb(GLuint id,void * data,void * userData)321 delete_semaphore_object_cb(GLuint id, void *data, void *userData)
322 {
323    struct gl_semaphore_object *semObj = (struct gl_semaphore_object *) data;
324    struct gl_context *ctx = (struct gl_context *) userData;
325    ctx->Driver.DeleteSemaphoreObject(ctx, semObj);
326 }
327 
328 /**
329  * Deallocate a shared state object and all children structures.
330  *
331  * \param ctx GL context.
332  * \param shared shared state pointer.
333  *
334  * Frees the display lists, the texture objects (calling the driver texture
335  * deletion callback to free its private data) and the vertex programs, as well
336  * as their hash tables.
337  *
338  * \sa alloc_shared_state().
339  */
340 static void
free_shared_state(struct gl_context * ctx,struct gl_shared_state * shared)341 free_shared_state(struct gl_context *ctx, struct gl_shared_state *shared)
342 {
343    GLuint i;
344 
345    /* Free the dummy/fallback texture objects */
346    for (i = 0; i < NUM_TEXTURE_TARGETS; i++) {
347       if (shared->FallbackTex[i])
348          ctx->Driver.DeleteTexture(ctx, shared->FallbackTex[i]);
349    }
350 
351    /*
352     * Free display lists
353     */
354    if (shared->DisplayList) {
355       _mesa_HashDeleteAll(shared->DisplayList, delete_displaylist_cb, ctx);
356       _mesa_DeleteHashTable(shared->DisplayList);
357    }
358 
359    if (shared->BitmapAtlas) {
360       _mesa_HashDeleteAll(shared->BitmapAtlas, delete_bitmap_atlas_cb, ctx);
361       _mesa_DeleteHashTable(shared->BitmapAtlas);
362    }
363 
364    if (shared->ShaderObjects) {
365       _mesa_HashWalk(shared->ShaderObjects, free_shader_program_data_cb, ctx);
366       _mesa_HashDeleteAll(shared->ShaderObjects, delete_shader_cb, ctx);
367       _mesa_DeleteHashTable(shared->ShaderObjects);
368    }
369 
370    if (shared->Programs) {
371       _mesa_HashDeleteAll(shared->Programs, delete_program_cb, ctx);
372       _mesa_DeleteHashTable(shared->Programs);
373    }
374 
375    if (shared->DefaultVertexProgram)
376       _mesa_reference_program(ctx, &shared->DefaultVertexProgram, NULL);
377 
378    if (shared->DefaultFragmentProgram)
379       _mesa_reference_program(ctx, &shared->DefaultFragmentProgram, NULL);
380 
381    if (shared->DefaultFragmentShader)
382       _mesa_delete_ati_fragment_shader(ctx, shared->DefaultFragmentShader);
383 
384    if (shared->ATIShaders) {
385       _mesa_HashDeleteAll(shared->ATIShaders, delete_fragshader_cb, ctx);
386       _mesa_DeleteHashTable(shared->ATIShaders);
387    }
388 
389    if (shared->BufferObjects) {
390       _mesa_HashDeleteAll(shared->BufferObjects, delete_bufferobj_cb, ctx);
391       _mesa_DeleteHashTable(shared->BufferObjects);
392    }
393 
394    if (shared->FrameBuffers) {
395       _mesa_HashDeleteAll(shared->FrameBuffers, delete_framebuffer_cb, ctx);
396       _mesa_DeleteHashTable(shared->FrameBuffers);
397    }
398 
399    if (shared->RenderBuffers) {
400       _mesa_HashDeleteAll(shared->RenderBuffers, delete_renderbuffer_cb, ctx);
401       _mesa_DeleteHashTable(shared->RenderBuffers);
402    }
403 
404    if (shared->SyncObjects) {
405       set_foreach(shared->SyncObjects, entry) {
406          _mesa_unref_sync_object(ctx, (struct gl_sync_object *) entry->key, 1);
407       }
408 
409       _mesa_set_destroy(shared->SyncObjects, NULL);
410    }
411 
412    if (shared->SamplerObjects) {
413       _mesa_HashDeleteAll(shared->SamplerObjects, delete_sampler_object_cb,
414                           ctx);
415       _mesa_DeleteHashTable(shared->SamplerObjects);
416    }
417 
418    /*
419     * Free texture objects (after FBOs since some textures might have
420     * been bound to FBOs).
421     */
422    assert(ctx->Driver.DeleteTexture);
423    /* the default textures */
424    for (i = 0; i < NUM_TEXTURE_TARGETS; i++) {
425       if (shared->DefaultTex[i])
426          ctx->Driver.DeleteTexture(ctx, shared->DefaultTex[i]);
427    }
428 
429    /* all other textures */
430    if (shared->TexObjects) {
431       _mesa_HashDeleteAll(shared->TexObjects, delete_texture_cb, ctx);
432       _mesa_DeleteHashTable(shared->TexObjects);
433    }
434 
435    _mesa_free_shared_handles(shared);
436 
437    /* ARB_shading_language_include */
438    _mesa_destroy_shader_includes(shared);
439    mtx_destroy(&shared->ShaderIncludeMutex);
440 
441    if (shared->MemoryObjects) {
442       _mesa_HashDeleteAll(shared->MemoryObjects, delete_memory_object_cb, ctx);
443       _mesa_DeleteHashTable(shared->MemoryObjects);
444    }
445 
446    if (shared->SemaphoreObjects) {
447       _mesa_HashDeleteAll(shared->SemaphoreObjects, delete_semaphore_object_cb, ctx);
448       _mesa_DeleteHashTable(shared->SemaphoreObjects);
449    }
450 
451    simple_mtx_destroy(&shared->Mutex);
452    mtx_destroy(&shared->TexMutex);
453 
454    free(shared);
455 }
456 
457 
458 /**
459  * gl_shared_state objects are ref counted.
460  * If ptr's refcount goes to zero, free the shared state.
461  */
462 void
_mesa_reference_shared_state(struct gl_context * ctx,struct gl_shared_state ** ptr,struct gl_shared_state * state)463 _mesa_reference_shared_state(struct gl_context *ctx,
464                              struct gl_shared_state **ptr,
465                              struct gl_shared_state *state)
466 {
467    if (*ptr == state)
468       return;
469 
470    if (*ptr) {
471       /* unref old state */
472       struct gl_shared_state *old = *ptr;
473       GLboolean delete;
474 
475       simple_mtx_lock(&old->Mutex);
476       assert(old->RefCount >= 1);
477       old->RefCount--;
478       delete = (old->RefCount == 0);
479       simple_mtx_unlock(&old->Mutex);
480 
481       if (delete) {
482          free_shared_state(ctx, old);
483       }
484 
485       *ptr = NULL;
486    }
487 
488    if (state) {
489       /* reference new state */
490       simple_mtx_lock(&state->Mutex);
491       state->RefCount++;
492       *ptr = state;
493       simple_mtx_unlock(&state->Mutex);
494    }
495 }
496