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