xref: /aosp_15_r20/external/mesa3d/src/mesa/main/shared.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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 "externalobjects.h"
39 #include "samplerobj.h"
40 #include "shaderapi.h"
41 #include "shaderobj.h"
42 #include "syncobj.h"
43 #include "texobj.h"
44 #include "texturebindless.h"
45 
46 #include "util/hash_table.h"
47 #include "util/set.h"
48 #include "util/u_memory.h"
49 
50 static void
51 free_shared_state(struct gl_context *ctx, struct gl_shared_state *shared);
52 
53 /**
54  * Allocate and initialize a shared context state structure.
55  * Initializes the display list, texture objects and vertex programs hash
56  * tables, allocates the texture objects. If it runs out of memory, frees
57  * everything already allocated before returning NULL.
58  *
59  * \return pointer to a gl_shared_state structure on success, or NULL on
60  * failure.
61  */
62 struct gl_shared_state *
_mesa_alloc_shared_state(struct gl_context * ctx)63 _mesa_alloc_shared_state(struct gl_context *ctx)
64 {
65    struct gl_shared_state *shared;
66    GLuint i;
67 
68    shared = CALLOC_STRUCT(gl_shared_state);
69    if (!shared)
70       return NULL;
71 
72    simple_mtx_init(&shared->Mutex, mtx_plain);
73 
74    _mesa_InitHashTable(&shared->DisplayList);
75    _mesa_InitHashTable(&shared->TexObjects);
76    _mesa_InitHashTable(&shared->Programs);
77 
78    shared->DefaultVertexProgram =
79       ctx->Driver.NewProgram(ctx, MESA_SHADER_VERTEX, 0, true);
80    shared->DefaultFragmentProgram =
81       ctx->Driver.NewProgram(ctx, MESA_SHADER_FRAGMENT, 0, true);
82 
83    _mesa_InitHashTable(&shared->ATIShaders);
84    shared->DefaultFragmentShader = _mesa_new_ati_fragment_shader(ctx, 0);
85 
86    _mesa_InitHashTable(&shared->ShaderObjects);
87 
88    _mesa_InitHashTable(&shared->BufferObjects);
89    shared->ZombieBufferObjects = _mesa_set_create(NULL, _mesa_hash_pointer,
90                                                   _mesa_key_pointer_equal);
91 
92    /* GL_ARB_sampler_objects */
93    _mesa_InitHashTable(&shared->SamplerObjects);
94 
95    /* GL_ARB_bindless_texture */
96    _mesa_init_shared_handles(shared);
97 
98    /* ARB_shading_language_include */
99    _mesa_init_shader_includes(shared);
100    simple_mtx_init(&shared->ShaderIncludeMutex, mtx_plain);
101 
102    /* Create default texture objects */
103    for (i = 0; i < NUM_TEXTURE_TARGETS; i++) {
104       /* NOTE: the order of these enums matches the TEXTURE_x_INDEX values */
105       static const GLenum targets[] = {
106          GL_TEXTURE_2D_MULTISAMPLE,
107          GL_TEXTURE_2D_MULTISAMPLE_ARRAY,
108          GL_TEXTURE_CUBE_MAP_ARRAY,
109          GL_TEXTURE_BUFFER,
110          GL_TEXTURE_2D_ARRAY_EXT,
111          GL_TEXTURE_1D_ARRAY_EXT,
112          GL_TEXTURE_EXTERNAL_OES,
113          GL_TEXTURE_CUBE_MAP,
114          GL_TEXTURE_3D,
115          GL_TEXTURE_RECTANGLE_NV,
116          GL_TEXTURE_2D,
117          GL_TEXTURE_1D
118       };
119       STATIC_ASSERT(ARRAY_SIZE(targets) == NUM_TEXTURE_TARGETS);
120       shared->DefaultTex[i] = _mesa_new_texture_object(ctx, 0, targets[i]);
121       /* Need to explicitly set/overwrite the TargetIndex field here since
122        * the call to _mesa_tex_target_to_index() in NewTextureObject() may
123        * fail if the texture target is not supported.
124        */
125       shared->DefaultTex[i]->TargetIndex = i;
126    }
127 
128    /* sanity check */
129    assert(shared->DefaultTex[TEXTURE_1D_INDEX]->RefCount == 1);
130 
131    /* Mutex and timestamp for texobj state validation */
132    simple_mtx_init(&shared->TexMutex, mtx_plain);
133    shared->TextureStateStamp = 0;
134 
135    _mesa_InitHashTable(&shared->FrameBuffers);
136    _mesa_InitHashTable(&shared->RenderBuffers);
137 
138    shared->SyncObjects = _mesa_set_create(NULL, _mesa_hash_pointer,
139                                           _mesa_key_pointer_equal);
140 
141    _mesa_InitHashTable(&shared->MemoryObjects);
142    _mesa_InitHashTable(&shared->SemaphoreObjects);
143 
144    shared->GLThread.NoLockDuration = ONE_SECOND_IN_NS;
145 
146    return shared;
147 }
148 
149 
150 /**
151  * Callback for deleting a display list.  Called by _mesa_DeleteHashTable().
152  */
153 static void
delete_displaylist_cb(void * data,void * userData)154 delete_displaylist_cb(void *data, void *userData)
155 {
156    struct gl_display_list *list = (struct gl_display_list *) data;
157    struct gl_context *ctx = (struct gl_context *) userData;
158    _mesa_delete_list(ctx, list);
159 }
160 
161 
162 /**
163  * Callback for deleting a texture object.  Called by _mesa_DeleteHashTable().
164  */
165 static void
delete_texture_cb(void * data,void * userData)166 delete_texture_cb(void *data, void *userData)
167 {
168    struct gl_texture_object *texObj = (struct gl_texture_object *) data;
169    struct gl_context *ctx = (struct gl_context *) userData;
170    _mesa_delete_texture_object(ctx, texObj);
171 }
172 
173 
174 /**
175  * Callback for deleting a program object.  Called by _mesa_DeleteHashTable().
176  */
177 static void
delete_program_cb(void * data,void * userData)178 delete_program_cb(void *data, void *userData)
179 {
180    struct gl_program *prog = (struct gl_program *) data;
181    struct gl_context *ctx = (struct gl_context *) userData;
182    if(prog != &_mesa_DummyProgram) {
183       assert(prog->RefCount == 1); /* should only be referenced by hash table */
184       prog->RefCount = 0;  /* now going away */
185       _mesa_delete_program(ctx, prog);
186    }
187 }
188 
189 
190 /**
191  * Callback for deleting an ATI fragment shader object.
192  * Called by _mesa_DeleteHashTable().
193  */
194 static void
delete_fragshader_cb(void * data,void * userData)195 delete_fragshader_cb(void *data, void *userData)
196 {
197    struct ati_fragment_shader *shader = (struct ati_fragment_shader *) data;
198    struct gl_context *ctx = (struct gl_context *) userData;
199    _mesa_delete_ati_fragment_shader(ctx, shader);
200 }
201 
202 
203 /**
204  * Callback for deleting a buffer object.  Called by _mesa_DeleteHashTable().
205  */
206 static void
delete_bufferobj_cb(void * data,void * userData)207 delete_bufferobj_cb(void *data, void *userData)
208 {
209    struct gl_buffer_object *bufObj = (struct gl_buffer_object *) data;
210    struct gl_context *ctx = (struct gl_context *) userData;
211 
212    _mesa_buffer_unmap_all_mappings(ctx, bufObj);
213    _mesa_reference_buffer_object(ctx, &bufObj, NULL);
214 }
215 
216 
217 /**
218  * Callback for freeing shader program data. Call it before delete_shader_cb
219  * to avoid memory access error.
220  */
221 static void
free_shader_program_data_cb(void * data,void * userData)222 free_shader_program_data_cb(void *data, void *userData)
223 {
224    struct gl_context *ctx = (struct gl_context *) userData;
225    struct gl_shader_program *shProg = (struct gl_shader_program *) data;
226 
227    if (shProg->Type == GL_SHADER_PROGRAM_MESA) {
228        _mesa_free_shader_program_data(ctx, shProg);
229    }
230 }
231 
232 
233 /**
234  * Callback for deleting shader and shader programs objects.
235  * Called by _mesa_DeleteHashTable().
236  */
237 static void
delete_shader_cb(void * data,void * userData)238 delete_shader_cb(void *data, void *userData)
239 {
240    struct gl_context *ctx = (struct gl_context *) userData;
241    struct gl_shader *sh = (struct gl_shader *) data;
242    if (_mesa_validate_shader_target(ctx, sh->Type)) {
243       _mesa_delete_shader(ctx, sh);
244    }
245    else {
246       struct gl_shader_program *shProg = (struct gl_shader_program *) data;
247       assert(shProg->Type == GL_SHADER_PROGRAM_MESA);
248       _mesa_delete_shader_program(ctx, shProg);
249    }
250 }
251 
252 
253 /**
254  * Callback for deleting a framebuffer object.  Called by _mesa_DeleteHashTable()
255  */
256 static void
delete_framebuffer_cb(void * data,UNUSED void * userData)257 delete_framebuffer_cb(void *data, UNUSED void *userData)
258 {
259    struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
260    /* The fact that the framebuffer is in the hashtable means its refcount
261     * is one, but we're removing from the hashtable now.  So clear refcount.
262     */
263    /*assert(fb->RefCount == 1);*/
264    fb->RefCount = 0;
265 
266    /* NOTE: Delete should always be defined but there are two reports
267     * of it being NULL (bugs 13507, 14293).  Work-around for now.
268     */
269    if (fb->Delete)
270       fb->Delete(fb);
271 }
272 
273 
274 /**
275  * Callback for deleting a renderbuffer object. Called by _mesa_DeleteHashTable()
276  */
277 static void
delete_renderbuffer_cb(void * data,void * userData)278 delete_renderbuffer_cb(void *data, void *userData)
279 {
280    struct gl_context *ctx = (struct gl_context *) userData;
281    struct gl_renderbuffer *rb = (struct gl_renderbuffer *) data;
282    rb->RefCount = 0;  /* see comment for FBOs above */
283    if (rb->Delete)
284       rb->Delete(ctx, rb);
285 }
286 
287 
288 /**
289  * Callback for deleting a sampler object. Called by _mesa_DeleteHashTable()
290  */
291 static void
delete_sampler_object_cb(void * data,void * userData)292 delete_sampler_object_cb(void *data, void *userData)
293 {
294    struct gl_context *ctx = (struct gl_context *) userData;
295    struct gl_sampler_object *sampObj = (struct gl_sampler_object *) data;
296    _mesa_reference_sampler_object(ctx, &sampObj, NULL);
297 }
298 
299 /**
300  * Callback for deleting a memory object.  Called by _mesa_DeleteHashTable().
301  */
302 static void
delete_memory_object_cb(void * data,void * userData)303 delete_memory_object_cb(void *data, void *userData)
304 {
305    struct gl_memory_object *memObj = (struct gl_memory_object *) data;
306    struct gl_context *ctx = (struct gl_context *) userData;
307    _mesa_delete_memory_object(ctx, memObj);
308 }
309 
310 /**
311  * Callback for deleting a memory object.  Called by _mesa_DeleteHashTable().
312  */
313 static void
delete_semaphore_object_cb(void * data,void * userData)314 delete_semaphore_object_cb(void *data, void *userData)
315 {
316    struct gl_semaphore_object *semObj = (struct gl_semaphore_object *) data;
317    struct gl_context *ctx = (struct gl_context *) userData;
318    _mesa_delete_semaphore_object(ctx, semObj);
319 }
320 
321 /**
322  * Deallocate a shared state object and all children structures.
323  *
324  * \param ctx GL context.
325  * \param shared shared state pointer.
326  *
327  * Frees the display lists, the texture objects (calling the driver texture
328  * deletion callback to free its private data) and the vertex programs, as well
329  * as their hash tables.
330  *
331  * \sa alloc_shared_state().
332  */
333 static void
free_shared_state(struct gl_context * ctx,struct gl_shared_state * shared)334 free_shared_state(struct gl_context *ctx, struct gl_shared_state *shared)
335 {
336    GLuint i;
337 
338    /* Free the dummy/fallback texture objects */
339    for (i = 0; i < NUM_TEXTURE_TARGETS; i++) {
340       for (unsigned j = 0; j < ARRAY_SIZE(shared->FallbackTex[0]); j++) {
341          if (shared->FallbackTex[i][j])
342             _mesa_delete_texture_object(ctx, shared->FallbackTex[i][j]);
343       }
344    }
345 
346    /*
347     * Free display lists
348     */
349    _mesa_DeinitHashTable(&shared->DisplayList, delete_displaylist_cb, ctx);
350    free(shared->small_dlist_store.ptr);
351    util_idalloc_fini(&shared->small_dlist_store.free_idx);
352 
353    _mesa_HashWalk(&shared->ShaderObjects, free_shader_program_data_cb, ctx);
354    _mesa_DeinitHashTable(&shared->ShaderObjects, delete_shader_cb, ctx);
355    _mesa_DeinitHashTable(&shared->Programs, delete_program_cb, ctx);
356 
357    if (shared->DefaultVertexProgram)
358       _mesa_reference_program(ctx, &shared->DefaultVertexProgram, NULL);
359 
360    if (shared->DefaultFragmentProgram)
361       _mesa_reference_program(ctx, &shared->DefaultFragmentProgram, NULL);
362 
363    if (shared->DefaultFragmentShader)
364       _mesa_delete_ati_fragment_shader(ctx, shared->DefaultFragmentShader);
365 
366    _mesa_DeinitHashTable(&shared->ATIShaders, delete_fragshader_cb, ctx);
367    _mesa_DeinitHashTable(&shared->BufferObjects, delete_bufferobj_cb, ctx);
368 
369    if (shared->ZombieBufferObjects) {
370       set_foreach(shared->ZombieBufferObjects, entry) {
371          assert(!"ZombieBufferObjects should be empty");
372       }
373       _mesa_set_destroy(shared->ZombieBufferObjects, NULL);
374    }
375 
376    _mesa_DeinitHashTable(&shared->FrameBuffers, delete_framebuffer_cb, ctx);
377    _mesa_DeinitHashTable(&shared->RenderBuffers, delete_renderbuffer_cb, ctx);
378 
379    if (shared->SyncObjects) {
380       set_foreach(shared->SyncObjects, entry) {
381          _mesa_unref_sync_object(ctx, (struct gl_sync_object *) entry->key, 1);
382       }
383 
384       _mesa_set_destroy(shared->SyncObjects, NULL);
385    }
386 
387    _mesa_DeinitHashTable(&shared->SamplerObjects, delete_sampler_object_cb,
388                             ctx);
389 
390    /*
391     * Free texture objects (after FBOs since some textures might have
392     * been bound to FBOs).
393     */
394    /* the default textures */
395    for (i = 0; i < NUM_TEXTURE_TARGETS; i++) {
396       if (shared->DefaultTex[i])
397          _mesa_delete_texture_object(ctx, shared->DefaultTex[i]);
398    }
399 
400    /* all other textures */
401    _mesa_DeinitHashTable(&shared->TexObjects, delete_texture_cb, ctx);
402 
403    _mesa_free_shared_handles(shared);
404 
405    /* ARB_shading_language_include */
406    _mesa_destroy_shader_includes(shared);
407    simple_mtx_destroy(&shared->ShaderIncludeMutex);
408 
409    _mesa_DeinitHashTable(&shared->MemoryObjects, delete_memory_object_cb,
410                          ctx);
411    _mesa_DeinitHashTable(&shared->SemaphoreObjects,
412                          delete_semaphore_object_cb, ctx);
413 
414    simple_mtx_destroy(&shared->Mutex);
415    simple_mtx_destroy(&shared->TexMutex);
416 
417    FREE(shared);
418 }
419 
420 
421 /**
422  * gl_shared_state objects are ref counted.
423  * If ptr's refcount goes to zero, free the shared state.
424  */
425 void
_mesa_reference_shared_state(struct gl_context * ctx,struct gl_shared_state ** ptr,struct gl_shared_state * state)426 _mesa_reference_shared_state(struct gl_context *ctx,
427                              struct gl_shared_state **ptr,
428                              struct gl_shared_state *state)
429 {
430    if (*ptr == state)
431       return;
432 
433    if (*ptr) {
434       /* unref old state */
435       struct gl_shared_state *old = *ptr;
436       GLboolean delete;
437 
438       simple_mtx_lock(&old->Mutex);
439       assert(old->RefCount >= 1);
440       old->RefCount--;
441       delete = (old->RefCount == 0);
442       simple_mtx_unlock(&old->Mutex);
443 
444       if (delete) {
445          free_shared_state(ctx, old);
446       }
447 
448       *ptr = NULL;
449    }
450 
451    if (state) {
452       /* reference new state */
453       simple_mtx_lock(&state->Mutex);
454       state->RefCount++;
455       *ptr = state;
456       simple_mtx_unlock(&state->Mutex);
457    }
458 }
459