2 * Mesa 3-D graphics library
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
27 * Shared-context state
31 #include "mfeatures.h"
34 #include "bufferobj.h"
36 #include "program/program.h"
38 #include "shaderobj.h"
42 * Allocate and initialize a shared context state structure.
43 * Initializes the display list, texture objects and vertex programs hash
44 * tables, allocates the texture objects. If it runs out of memory, frees
45 * everything already allocated before returning NULL.
47 * \return pointer to a gl_shared_state structure on success, or NULL on
50 struct gl_shared_state
*
51 _mesa_alloc_shared_state(struct gl_context
*ctx
)
53 struct gl_shared_state
*shared
;
56 shared
= CALLOC_STRUCT(gl_shared_state
);
60 _glthread_INIT_MUTEX(shared
->Mutex
);
62 shared
->DisplayList
= _mesa_NewHashTable();
63 shared
->TexObjects
= _mesa_NewHashTable();
64 shared
->Programs
= _mesa_NewHashTable();
66 #if FEATURE_ARB_vertex_program
67 shared
->DefaultVertexProgram
= (struct gl_vertex_program
*)
68 ctx
->Driver
.NewProgram(ctx
, GL_VERTEX_PROGRAM_ARB
, 0);
71 #if FEATURE_ARB_fragment_program
72 shared
->DefaultFragmentProgram
= (struct gl_fragment_program
*)
73 ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
76 #if FEATURE_ARB_shader_objects
77 shared
->ShaderObjects
= _mesa_NewHashTable();
80 shared
->BufferObjects
= _mesa_NewHashTable();
82 /* Allocate the default buffer object */
83 shared
->NullBufferObj
= ctx
->Driver
.NewBufferObject(ctx
, 0, 0);
85 /* Create default texture objects */
86 for (i
= 0; i
< NUM_TEXTURE_TARGETS
; i
++) {
87 /* NOTE: the order of these enums matches the TEXTURE_x_INDEX values */
88 static const GLenum targets
[NUM_TEXTURE_TARGETS
] = {
90 GL_TEXTURE_2D_ARRAY_EXT
,
91 GL_TEXTURE_1D_ARRAY_EXT
,
92 GL_TEXTURE_EXTERNAL_OES
,
95 GL_TEXTURE_RECTANGLE_NV
,
99 STATIC_ASSERT(Elements(targets
) == NUM_TEXTURE_TARGETS
);
100 shared
->DefaultTex
[i
] = ctx
->Driver
.NewTextureObject(ctx
, 0, targets
[i
]);
104 assert(shared
->DefaultTex
[TEXTURE_1D_INDEX
]->RefCount
== 1);
106 /* Mutex and timestamp for texobj state validation */
107 _glthread_INIT_MUTEX(shared
->TexMutex
);
108 shared
->TextureStateStamp
= 0;
110 #if FEATURE_EXT_framebuffer_object
111 shared
->FrameBuffers
= _mesa_NewHashTable();
112 shared
->RenderBuffers
= _mesa_NewHashTable();
120 * Callback for deleting a display list. Called by _mesa_HashDeleteAll().
123 delete_displaylist_cb(GLuint id
, void *data
, void *userData
)
125 struct gl_display_list
*list
= (struct gl_display_list
*) data
;
126 struct gl_context
*ctx
= (struct gl_context
*) userData
;
127 _mesa_delete_list(ctx
, list
);
132 * Callback for deleting a texture object. Called by _mesa_HashDeleteAll().
135 delete_texture_cb(GLuint id
, void *data
, void *userData
)
137 struct gl_texture_object
*texObj
= (struct gl_texture_object
*) data
;
138 struct gl_context
*ctx
= (struct gl_context
*) userData
;
139 ctx
->Driver
.DeleteTexture(ctx
, texObj
);
144 * Callback for deleting a program object. Called by _mesa_HashDeleteAll().
147 delete_program_cb(GLuint id
, void *data
, void *userData
)
149 struct gl_program
*prog
= (struct gl_program
*) data
;
150 struct gl_context
*ctx
= (struct gl_context
*) userData
;
151 if(prog
!= &_mesa_DummyProgram
) {
152 ASSERT(prog
->RefCount
== 1); /* should only be referenced by hash table */
153 prog
->RefCount
= 0; /* now going away */
154 ctx
->Driver
.DeleteProgram(ctx
, prog
);
160 * Callback for deleting a buffer object. Called by _mesa_HashDeleteAll().
163 delete_bufferobj_cb(GLuint id
, void *data
, void *userData
)
165 struct gl_buffer_object
*bufObj
= (struct gl_buffer_object
*) data
;
166 struct gl_context
*ctx
= (struct gl_context
*) userData
;
167 if (_mesa_bufferobj_mapped(bufObj
)) {
168 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
);
169 bufObj
->Pointer
= NULL
;
171 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
176 * Callback for freeing shader program data. Call it before delete_shader_cb
177 * to avoid memory access error.
180 free_shader_program_data_cb(GLuint id
, void *data
, void *userData
)
182 struct gl_context
*ctx
= (struct gl_context
*) userData
;
183 struct gl_shader_program
*shProg
= (struct gl_shader_program
*) data
;
185 if (shProg
->Type
== GL_SHADER_PROGRAM_MESA
) {
186 _mesa_free_shader_program_data(ctx
, shProg
);
192 * Callback for deleting shader and shader programs objects.
193 * Called by _mesa_HashDeleteAll().
196 delete_shader_cb(GLuint id
, void *data
, void *userData
)
198 struct gl_context
*ctx
= (struct gl_context
*) userData
;
199 struct gl_shader
*sh
= (struct gl_shader
*) data
;
200 if (sh
->Type
== GL_FRAGMENT_SHADER
|| sh
->Type
== GL_VERTEX_SHADER
) {
201 ctx
->Driver
.DeleteShader(ctx
, sh
);
204 struct gl_shader_program
*shProg
= (struct gl_shader_program
*) data
;
205 ASSERT(shProg
->Type
== GL_SHADER_PROGRAM_MESA
);
206 ctx
->Driver
.DeleteShaderProgram(ctx
, shProg
);
212 * Callback for deleting a framebuffer object. Called by _mesa_HashDeleteAll()
215 delete_framebuffer_cb(GLuint id
, void *data
, void *userData
)
217 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) data
;
218 /* The fact that the framebuffer is in the hashtable means its refcount
219 * is one, but we're removing from the hashtable now. So clear refcount.
221 /*assert(fb->RefCount == 1);*/
224 /* NOTE: Delete should always be defined but there are two reports
225 * of it being NULL (bugs 13507, 14293). Work-around for now.
233 * Callback for deleting a renderbuffer object. Called by _mesa_HashDeleteAll()
236 delete_renderbuffer_cb(GLuint id
, void *data
, void *userData
)
238 struct gl_renderbuffer
*rb
= (struct gl_renderbuffer
*) data
;
239 rb
->RefCount
= 0; /* see comment for FBOs above */
246 * Deallocate a shared state object and all children structures.
248 * \param ctx GL context.
249 * \param shared shared state pointer.
251 * Frees the display lists, the texture objects (calling the driver texture
252 * deletion callback to free its private data) and the vertex programs, as well
253 * as their hash tables.
255 * \sa alloc_shared_state().
258 free_shared_state(struct gl_context
*ctx
, struct gl_shared_state
*shared
)
262 /* Free the dummy/fallback texture object */
263 if (shared
->FallbackTex
)
264 ctx
->Driver
.DeleteTexture(ctx
, shared
->FallbackTex
);
269 _mesa_HashDeleteAll(shared
->DisplayList
, delete_displaylist_cb
, ctx
);
270 _mesa_DeleteHashTable(shared
->DisplayList
);
272 #if FEATURE_ARB_shader_objects
273 _mesa_HashWalk(shared
->ShaderObjects
, free_shader_program_data_cb
, ctx
);
274 _mesa_HashDeleteAll(shared
->ShaderObjects
, delete_shader_cb
, ctx
);
275 _mesa_DeleteHashTable(shared
->ShaderObjects
);
278 _mesa_HashDeleteAll(shared
->Programs
, delete_program_cb
, ctx
);
279 _mesa_DeleteHashTable(shared
->Programs
);
281 #if FEATURE_ARB_vertex_program
282 _mesa_reference_vertprog(ctx
, &shared
->DefaultVertexProgram
, NULL
);
285 #if FEATURE_ARB_fragment_program
286 _mesa_reference_fragprog(ctx
, &shared
->DefaultFragmentProgram
, NULL
);
289 _mesa_HashDeleteAll(shared
->BufferObjects
, delete_bufferobj_cb
, ctx
);
290 _mesa_DeleteHashTable(shared
->BufferObjects
);
292 #if FEATURE_EXT_framebuffer_object
293 _mesa_HashDeleteAll(shared
->FrameBuffers
, delete_framebuffer_cb
, ctx
);
294 _mesa_DeleteHashTable(shared
->FrameBuffers
);
295 _mesa_HashDeleteAll(shared
->RenderBuffers
, delete_renderbuffer_cb
, ctx
);
296 _mesa_DeleteHashTable(shared
->RenderBuffers
);
299 _mesa_reference_buffer_object(ctx
, &shared
->NullBufferObj
, NULL
);
302 * Free texture objects (after FBOs since some textures might have
303 * been bound to FBOs).
305 ASSERT(ctx
->Driver
.DeleteTexture
);
306 /* the default textures */
307 for (i
= 0; i
< NUM_TEXTURE_TARGETS
; i
++) {
308 ctx
->Driver
.DeleteTexture(ctx
, shared
->DefaultTex
[i
]);
311 /* all other textures */
312 _mesa_HashDeleteAll(shared
->TexObjects
, delete_texture_cb
, ctx
);
313 _mesa_DeleteHashTable(shared
->TexObjects
);
315 _glthread_DESTROY_MUTEX(shared
->Mutex
);
316 _glthread_DESTROY_MUTEX(shared
->TexMutex
);
323 * gl_shared_state objects are ref counted.
324 * If ptr's refcount goes to zero, free the shared state.
327 _mesa_reference_shared_state(struct gl_context
*ctx
,
328 struct gl_shared_state
**ptr
,
329 struct gl_shared_state
*state
)
335 /* unref old state */
336 struct gl_shared_state
*old
= *ptr
;
339 _glthread_LOCK_MUTEX(old
->Mutex
);
340 assert(old
->RefCount
>= 1);
342 delete = (old
->RefCount
== 0);
343 _glthread_UNLOCK_MUTEX(old
->Mutex
);
346 free_shared_state(ctx
, old
);
353 /* reference new state */
354 _glthread_LOCK_MUTEX(state
->Mutex
);
357 _glthread_UNLOCK_MUTEX(state
->Mutex
);