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