shared.c revision 3464ebd5
1/* 2 * Mesa 3-D graphics library 3 * Version: 7.5 4 * 5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved. 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a 8 * copy of this software and associated documentation files (the "Software"), 9 * to deal in the Software without restriction, including without limitation 10 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 11 * and/or sell copies of the Software, and to permit persons to whom the 12 * Software is furnished to do so, subject to the following conditions: 13 * 14 * The above copyright notice and this permission notice shall be included 15 * in all copies or substantial portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 */ 24 25/** 26 * \file shared.c 27 * Shared-context state 28 */ 29 30#include "imports.h" 31#include "mfeatures.h" 32#include "mtypes.h" 33#include "hash.h" 34#if FEATURE_ATI_fragment_shader 35#include "atifragshader.h" 36#endif 37#include "bufferobj.h" 38#include "shared.h" 39#include "program/program.h" 40#include "dlist.h" 41#if FEATURE_ARB_sampler_objects 42#include "samplerobj.h" 43#endif 44#include "shaderobj.h" 45#include "syncobj.h" 46 47 48/** 49 * Allocate and initialize a shared context state structure. 50 * Initializes the display list, texture objects and vertex programs hash 51 * tables, allocates the texture objects. If it runs out of memory, frees 52 * everything already allocated before returning NULL. 53 * 54 * \return pointer to a gl_shared_state structure on success, or NULL on 55 * failure. 56 */ 57struct gl_shared_state * 58_mesa_alloc_shared_state(struct gl_context *ctx) 59{ 60 struct gl_shared_state *shared; 61 GLuint i; 62 63 shared = CALLOC_STRUCT(gl_shared_state); 64 if (!shared) 65 return NULL; 66 67 _glthread_INIT_MUTEX(shared->Mutex); 68 69 shared->DisplayList = _mesa_NewHashTable(); 70 shared->TexObjects = _mesa_NewHashTable(); 71 shared->Programs = _mesa_NewHashTable(); 72 73#if FEATURE_ARB_vertex_program 74 shared->DefaultVertexProgram = (struct gl_vertex_program *) 75 ctx->Driver.NewProgram(ctx, GL_VERTEX_PROGRAM_ARB, 0); 76#endif 77 78#if FEATURE_ARB_fragment_program 79 shared->DefaultFragmentProgram = (struct gl_fragment_program *) 80 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0); 81#endif 82 83#if FEATURE_ATI_fragment_shader 84 shared->ATIShaders = _mesa_NewHashTable(); 85 shared->DefaultFragmentShader = _mesa_new_ati_fragment_shader(ctx, 0); 86#endif 87 88#if FEATURE_ARB_shader_objects 89 shared->ShaderObjects = _mesa_NewHashTable(); 90#endif 91 92#if FEATURE_ARB_vertex_buffer_object || FEATURE_ARB_pixel_buffer_object 93 shared->BufferObjects = _mesa_NewHashTable(); 94#endif 95 96#if FEATURE_ARB_sampler_objects 97 /* GL_ARB_sampler_objects */ 98 shared->SamplerObjects = _mesa_NewHashTable(); 99#endif 100 101 /* Allocate the default buffer object */ 102 shared->NullBufferObj = ctx->Driver.NewBufferObject(ctx, 0, 0); 103 104 /* Create default texture objects */ 105 for (i = 0; i < NUM_TEXTURE_TARGETS; i++) { 106 /* NOTE: the order of these enums matches the TEXTURE_x_INDEX values */ 107 static const GLenum targets[NUM_TEXTURE_TARGETS] = { 108 GL_TEXTURE_BUFFER, 109 GL_TEXTURE_2D_ARRAY_EXT, 110 GL_TEXTURE_1D_ARRAY_EXT, 111 GL_TEXTURE_CUBE_MAP, 112 GL_TEXTURE_3D, 113 GL_TEXTURE_RECTANGLE_NV, 114 GL_TEXTURE_2D, 115 GL_TEXTURE_1D 116 }; 117 assert(Elements(targets) == NUM_TEXTURE_TARGETS); 118 shared->DefaultTex[i] = ctx->Driver.NewTextureObject(ctx, 0, targets[i]); 119 } 120 121 /* sanity check */ 122 assert(shared->DefaultTex[TEXTURE_1D_INDEX]->RefCount == 1); 123 124 /* Mutex and timestamp for texobj state validation */ 125 _glthread_INIT_MUTEX(shared->TexMutex); 126 shared->TextureStateStamp = 0; 127 128#if FEATURE_EXT_framebuffer_object 129 shared->FrameBuffers = _mesa_NewHashTable(); 130 shared->RenderBuffers = _mesa_NewHashTable(); 131#endif 132 133 make_empty_list(& shared->SyncObjects); 134 135 return shared; 136} 137 138 139/** 140 * Callback for deleting a display list. Called by _mesa_HashDeleteAll(). 141 */ 142static void 143delete_displaylist_cb(GLuint id, void *data, void *userData) 144{ 145 struct gl_display_list *list = (struct gl_display_list *) data; 146 struct gl_context *ctx = (struct gl_context *) userData; 147 _mesa_delete_list(ctx, list); 148} 149 150 151/** 152 * Callback for deleting a texture object. Called by _mesa_HashDeleteAll(). 153 */ 154static void 155delete_texture_cb(GLuint id, void *data, void *userData) 156{ 157 struct gl_texture_object *texObj = (struct gl_texture_object *) data; 158 struct gl_context *ctx = (struct gl_context *) userData; 159 ctx->Driver.DeleteTexture(ctx, texObj); 160} 161 162 163/** 164 * Callback for deleting a program object. Called by _mesa_HashDeleteAll(). 165 */ 166static void 167delete_program_cb(GLuint id, void *data, void *userData) 168{ 169 struct gl_program *prog = (struct gl_program *) data; 170 struct gl_context *ctx = (struct gl_context *) userData; 171 if(prog != &_mesa_DummyProgram) { 172 ASSERT(prog->RefCount == 1); /* should only be referenced by hash table */ 173 prog->RefCount = 0; /* now going away */ 174 ctx->Driver.DeleteProgram(ctx, prog); 175 } 176} 177 178 179#if FEATURE_ATI_fragment_shader 180/** 181 * Callback for deleting an ATI fragment shader object. 182 * Called by _mesa_HashDeleteAll(). 183 */ 184static void 185delete_fragshader_cb(GLuint id, void *data, void *userData) 186{ 187 struct ati_fragment_shader *shader = (struct ati_fragment_shader *) data; 188 struct gl_context *ctx = (struct gl_context *) userData; 189 _mesa_delete_ati_fragment_shader(ctx, shader); 190} 191#endif 192 193 194/** 195 * Callback for deleting a buffer object. Called by _mesa_HashDeleteAll(). 196 */ 197static void 198delete_bufferobj_cb(GLuint id, void *data, void *userData) 199{ 200 struct gl_buffer_object *bufObj = (struct gl_buffer_object *) data; 201 struct gl_context *ctx = (struct gl_context *) userData; 202 if (_mesa_bufferobj_mapped(bufObj)) { 203 ctx->Driver.UnmapBuffer(ctx, 0, bufObj); 204 bufObj->Pointer = NULL; 205 } 206 _mesa_reference_buffer_object(ctx, &bufObj, NULL); 207} 208 209 210/** 211 * Callback for freeing shader program data. Call it before delete_shader_cb 212 * to avoid memory access error. 213 */ 214static void 215free_shader_program_data_cb(GLuint id, void *data, void *userData) 216{ 217 struct gl_context *ctx = (struct gl_context *) userData; 218 struct gl_shader_program *shProg = (struct gl_shader_program *) data; 219 220 if (shProg->Type == GL_SHADER_PROGRAM_MESA) { 221 _mesa_free_shader_program_data(ctx, shProg); 222 } 223} 224 225 226/** 227 * Callback for deleting shader and shader programs objects. 228 * Called by _mesa_HashDeleteAll(). 229 */ 230static void 231delete_shader_cb(GLuint id, void *data, void *userData) 232{ 233 struct gl_context *ctx = (struct gl_context *) userData; 234 struct gl_shader *sh = (struct gl_shader *) data; 235 if (sh->Type == GL_FRAGMENT_SHADER || sh->Type == GL_VERTEX_SHADER) { 236 ctx->Driver.DeleteShader(ctx, sh); 237 } 238 else { 239 struct gl_shader_program *shProg = (struct gl_shader_program *) data; 240 ASSERT(shProg->Type == GL_SHADER_PROGRAM_MESA); 241 ctx->Driver.DeleteShaderProgram(ctx, shProg); 242 } 243} 244 245 246/** 247 * Callback for deleting a framebuffer object. Called by _mesa_HashDeleteAll() 248 */ 249static void 250delete_framebuffer_cb(GLuint id, void *data, void *userData) 251{ 252 struct gl_framebuffer *fb = (struct gl_framebuffer *) data; 253 /* The fact that the framebuffer is in the hashtable means its refcount 254 * is one, but we're removing from the hashtable now. So clear refcount. 255 */ 256 /*assert(fb->RefCount == 1);*/ 257 fb->RefCount = 0; 258 259 /* NOTE: Delete should always be defined but there are two reports 260 * of it being NULL (bugs 13507, 14293). Work-around for now. 261 */ 262 if (fb->Delete) 263 fb->Delete(fb); 264} 265 266 267/** 268 * Callback for deleting a renderbuffer object. Called by _mesa_HashDeleteAll() 269 */ 270static void 271delete_renderbuffer_cb(GLuint id, void *data, void *userData) 272{ 273 struct gl_renderbuffer *rb = (struct gl_renderbuffer *) data; 274 rb->RefCount = 0; /* see comment for FBOs above */ 275 if (rb->Delete) 276 rb->Delete(rb); 277} 278 279 280#if FEATURE_ARB_sampler_objects 281/** 282 * Callback for deleting a sampler object. Called by _mesa_HashDeleteAll() 283 */ 284static void 285delete_sampler_object_cb(GLuint id, void *data, void *userData) 286{ 287 struct gl_context *ctx = (struct gl_context *) userData; 288 struct gl_sampler_object *sampObj = (struct gl_sampler_object *) data; 289 _mesa_reference_sampler_object(ctx, &sampObj, NULL); 290} 291#endif 292 293 294/** 295 * Deallocate a shared state object and all children structures. 296 * 297 * \param ctx GL context. 298 * \param shared shared state pointer. 299 * 300 * Frees the display lists, the texture objects (calling the driver texture 301 * deletion callback to free its private data) and the vertex programs, as well 302 * as their hash tables. 303 * 304 * \sa alloc_shared_state(). 305 */ 306static void 307free_shared_state(struct gl_context *ctx, struct gl_shared_state *shared) 308{ 309 GLuint i; 310 311 /* Free the dummy/fallback texture object */ 312 if (shared->FallbackTex) 313 ctx->Driver.DeleteTexture(ctx, shared->FallbackTex); 314 315 /* 316 * Free display lists 317 */ 318 _mesa_HashDeleteAll(shared->DisplayList, delete_displaylist_cb, ctx); 319 _mesa_DeleteHashTable(shared->DisplayList); 320 321#if FEATURE_ARB_shader_objects 322 _mesa_HashWalk(shared->ShaderObjects, free_shader_program_data_cb, ctx); 323 _mesa_HashDeleteAll(shared->ShaderObjects, delete_shader_cb, ctx); 324 _mesa_DeleteHashTable(shared->ShaderObjects); 325#endif 326 327 _mesa_HashDeleteAll(shared->Programs, delete_program_cb, ctx); 328 _mesa_DeleteHashTable(shared->Programs); 329 330#if FEATURE_ARB_vertex_program 331 _mesa_reference_vertprog(ctx, &shared->DefaultVertexProgram, NULL); 332#endif 333 334#if FEATURE_ARB_fragment_program 335 _mesa_reference_fragprog(ctx, &shared->DefaultFragmentProgram, NULL); 336#endif 337 338#if FEATURE_ATI_fragment_shader 339 _mesa_HashDeleteAll(shared->ATIShaders, delete_fragshader_cb, ctx); 340 _mesa_DeleteHashTable(shared->ATIShaders); 341 _mesa_delete_ati_fragment_shader(ctx, shared->DefaultFragmentShader); 342#endif 343 344#if FEATURE_ARB_vertex_buffer_object || FEATURE_ARB_pixel_buffer_object 345 _mesa_HashDeleteAll(shared->BufferObjects, delete_bufferobj_cb, ctx); 346 _mesa_DeleteHashTable(shared->BufferObjects); 347#endif 348 349#if FEATURE_EXT_framebuffer_object 350 _mesa_HashDeleteAll(shared->FrameBuffers, delete_framebuffer_cb, ctx); 351 _mesa_DeleteHashTable(shared->FrameBuffers); 352 _mesa_HashDeleteAll(shared->RenderBuffers, delete_renderbuffer_cb, ctx); 353 _mesa_DeleteHashTable(shared->RenderBuffers); 354#endif 355 356#if FEATURE_ARB_vertex_buffer_object 357 _mesa_reference_buffer_object(ctx, &shared->NullBufferObj, NULL); 358#endif 359 360 { 361 struct simple_node *node; 362 struct simple_node *temp; 363 364 foreach_s(node, temp, & shared->SyncObjects) { 365 _mesa_unref_sync_object(ctx, (struct gl_sync_object *) node); 366 } 367 } 368 369#if FEATURE_ARB_sampler_objects 370 _mesa_HashDeleteAll(shared->SamplerObjects, delete_sampler_object_cb, ctx); 371 _mesa_DeleteHashTable(shared->SamplerObjects); 372#endif 373 374 /* 375 * Free texture objects (after FBOs since some textures might have 376 * been bound to FBOs). 377 */ 378 ASSERT(ctx->Driver.DeleteTexture); 379 /* the default textures */ 380 for (i = 0; i < NUM_TEXTURE_TARGETS; i++) { 381 ctx->Driver.DeleteTexture(ctx, shared->DefaultTex[i]); 382 } 383 384 /* all other textures */ 385 _mesa_HashDeleteAll(shared->TexObjects, delete_texture_cb, ctx); 386 _mesa_DeleteHashTable(shared->TexObjects); 387 388 _glthread_DESTROY_MUTEX(shared->Mutex); 389 _glthread_DESTROY_MUTEX(shared->TexMutex); 390 391 free(shared); 392} 393 394 395/** 396 * Decrement shared state object reference count and potentially free it 397 * and all children structures. 398 * 399 * \param ctx GL context. 400 * \param shared shared state pointer. 401 * 402 * \sa free_shared_state(). 403 */ 404void 405_mesa_release_shared_state(struct gl_context *ctx, 406 struct gl_shared_state *shared) 407{ 408 GLint RefCount; 409 410 _glthread_LOCK_MUTEX(shared->Mutex); 411 RefCount = --shared->RefCount; 412 _glthread_UNLOCK_MUTEX(shared->Mutex); 413 414 assert(RefCount >= 0); 415 416 if (RefCount == 0) { 417 /* free shared state */ 418 free_shared_state( ctx, shared ); 419 } 420} 421