OSDN Git Service

mesa: Fix null buffer object reference counting.
[android-x86/external-mesa.git] / src / mesa / main / shared.c
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
31
32 #include "imports.h"
33 #include "mtypes.h"
34 #include "hash.h"
35 #include "arrayobj.h"
36 #include "bufferobj.h"
37 #include "shared.h"
38 #include "shader/program.h"
39 #include "shader/shader_api.h"
40 #include "dlist.h"
41 #if FEATURE_ATI_fragment_shader
42 #include "shader/atifragshader.h"
43 #endif
44 #if FEATURE_ARB_sync
45 #include "syncobj.h"
46 #endif
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  */
57 struct gl_shared_state *
58 _mesa_alloc_shared_state(GLcontext *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    /* Allocate the default buffer object */
97    shared->NullBufferObj = ctx->Driver.NewBufferObject(ctx, 0, 0);
98 #ifndef DEBUG
99    /* Set refcount so high that it never gets deleted.
100     * XXX with recent/improved refcounting this should be no longer be needed.
101     */
102    shared->NullBufferObj->RefCount = 1000 * 1000 * 1000;
103 #endif
104
105    /* Create default texture objects */
106    for (i = 0; i < NUM_TEXTURE_TARGETS; i++) {
107       /* NOTE: the order of these enums matches the TEXTURE_x_INDEX values */
108       static const GLenum targets[NUM_TEXTURE_TARGETS] = {
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       shared->DefaultTex[i] = ctx->Driver.NewTextureObject(ctx, 0, targets[i]);
118    }
119
120    /* sanity check */
121    assert(shared->DefaultTex[TEXTURE_1D_INDEX]->RefCount == 1);
122
123    /* Mutex and timestamp for texobj state validation */
124    _glthread_INIT_MUTEX(shared->TexMutex);
125    shared->TextureStateStamp = 0;
126
127 #if FEATURE_EXT_framebuffer_object
128    shared->FrameBuffers = _mesa_NewHashTable();
129    shared->RenderBuffers = _mesa_NewHashTable();
130 #endif
131
132 #if FEATURE_ARB_sync
133    make_empty_list(& shared->SyncObjects);
134 #endif
135
136    return shared;
137 }
138
139
140 /**
141  * Callback for deleting a display list.  Called by _mesa_HashDeleteAll().
142  */
143 static void
144 delete_displaylist_cb(GLuint id, void *data, void *userData)
145 {
146    struct gl_display_list *list = (struct gl_display_list *) data;
147    GLcontext *ctx = (GLcontext *) userData;
148    _mesa_delete_list(ctx, list);
149 }
150
151
152 /**
153  * Callback for deleting a texture object.  Called by _mesa_HashDeleteAll().
154  */
155 static void
156 delete_texture_cb(GLuint id, void *data, void *userData)
157 {
158    struct gl_texture_object *texObj = (struct gl_texture_object *) data;
159    GLcontext *ctx = (GLcontext *) userData;
160    ctx->Driver.DeleteTexture(ctx, texObj);
161 }
162
163
164 /**
165  * Callback for deleting a program object.  Called by _mesa_HashDeleteAll().
166  */
167 static void
168 delete_program_cb(GLuint id, void *data, void *userData)
169 {
170    struct gl_program *prog = (struct gl_program *) data;
171    GLcontext *ctx = (GLcontext *) userData;
172    if(prog != &_mesa_DummyProgram) {
173       ASSERT(prog->RefCount == 1); /* should only be referenced by hash table */
174       prog->RefCount = 0;  /* now going away */
175       ctx->Driver.DeleteProgram(ctx, prog);
176    }
177 }
178
179
180 #if FEATURE_ATI_fragment_shader
181 /**
182  * Callback for deleting an ATI fragment shader object.
183  * Called by _mesa_HashDeleteAll().
184  */
185 static void
186 delete_fragshader_cb(GLuint id, void *data, void *userData)
187 {
188    struct ati_fragment_shader *shader = (struct ati_fragment_shader *) data;
189    GLcontext *ctx = (GLcontext *) userData;
190    _mesa_delete_ati_fragment_shader(ctx, shader);
191 }
192 #endif
193
194
195 /**
196  * Callback for deleting a buffer object.  Called by _mesa_HashDeleteAll().
197  */
198 static void
199 delete_bufferobj_cb(GLuint id, void *data, void *userData)
200 {
201    struct gl_buffer_object *bufObj = (struct gl_buffer_object *) data;
202    GLcontext *ctx = (GLcontext *) userData;
203    if (_mesa_bufferobj_mapped(bufObj)) {
204       ctx->Driver.UnmapBuffer(ctx, 0, bufObj);
205       bufObj->Pointer = NULL;
206    }
207    _mesa_reference_buffer_object(ctx, &bufObj, NULL);
208 }
209
210
211 /**
212  * Callback for freeing shader program data. Call it before delete_shader_cb
213  * to avoid memory access error.
214  */
215 static void
216 free_shader_program_data_cb(GLuint id, void *data, void *userData)
217 {
218    GLcontext *ctx = (GLcontext *) userData;
219    struct gl_shader_program *shProg = (struct gl_shader_program *) data;
220
221    if (shProg->Type == GL_SHADER_PROGRAM_MESA) {
222        _mesa_free_shader_program_data(ctx, shProg);
223    }
224 }
225
226
227 /**
228  * Callback for deleting shader and shader programs objects.
229  * Called by _mesa_HashDeleteAll().
230  */
231 static void
232 delete_shader_cb(GLuint id, void *data, void *userData)
233 {
234    GLcontext *ctx = (GLcontext *) userData;
235    struct gl_shader *sh = (struct gl_shader *) data;
236    if (sh->Type == GL_FRAGMENT_SHADER || sh->Type == GL_VERTEX_SHADER) {
237       _mesa_free_shader(ctx, sh);
238    }
239    else {
240       struct gl_shader_program *shProg = (struct gl_shader_program *) data;
241       ASSERT(shProg->Type == GL_SHADER_PROGRAM_MESA);
242       _mesa_free_shader_program(ctx, shProg);
243    }
244 }
245
246
247 /**
248  * Callback for deleting a framebuffer object.  Called by _mesa_HashDeleteAll()
249  */
250 static void
251 delete_framebuffer_cb(GLuint id, void *data, void *userData)
252 {
253    struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
254    /* The fact that the framebuffer is in the hashtable means its refcount
255     * is one, but we're removing from the hashtable now.  So clear refcount.
256     */
257    /*assert(fb->RefCount == 1);*/
258    fb->RefCount = 0;
259
260    /* NOTE: Delete should always be defined but there are two reports
261     * of it being NULL (bugs 13507, 14293).  Work-around for now.
262     */
263    if (fb->Delete)
264       fb->Delete(fb);
265 }
266
267
268 /**
269  * Callback for deleting a renderbuffer object. Called by _mesa_HashDeleteAll()
270  */
271 static void
272 delete_renderbuffer_cb(GLuint id, void *data, void *userData)
273 {
274    struct gl_renderbuffer *rb = (struct gl_renderbuffer *) data;
275    rb->RefCount = 0;  /* see comment for FBOs above */
276    if (rb->Delete)
277       rb->Delete(rb);
278 }
279
280
281 /**
282  * Deallocate a shared state object and all children structures.
283  *
284  * \param ctx GL context.
285  * \param shared shared state pointer.
286  * 
287  * Frees the display lists, the texture objects (calling the driver texture
288  * deletion callback to free its private data) and the vertex programs, as well
289  * as their hash tables.
290  *
291  * \sa alloc_shared_state().
292  */
293 static void
294 free_shared_state(GLcontext *ctx, struct gl_shared_state *shared)
295 {
296    GLuint i;
297
298    /*
299     * Free display lists
300     */
301    _mesa_HashDeleteAll(shared->DisplayList, delete_displaylist_cb, ctx);
302    _mesa_DeleteHashTable(shared->DisplayList);
303
304 #if FEATURE_ARB_shader_objects
305    _mesa_HashWalk(shared->ShaderObjects, free_shader_program_data_cb, ctx);
306    _mesa_HashDeleteAll(shared->ShaderObjects, delete_shader_cb, ctx);
307    _mesa_DeleteHashTable(shared->ShaderObjects);
308 #endif
309
310    _mesa_HashDeleteAll(shared->Programs, delete_program_cb, ctx);
311    _mesa_DeleteHashTable(shared->Programs);
312
313 #if FEATURE_ARB_vertex_program
314    _mesa_reference_vertprog(ctx, &shared->DefaultVertexProgram, NULL);
315 #endif
316
317 #if FEATURE_ARB_fragment_program
318    _mesa_reference_fragprog(ctx, &shared->DefaultFragmentProgram, NULL);
319 #endif
320
321 #if FEATURE_ATI_fragment_shader
322    _mesa_HashDeleteAll(shared->ATIShaders, delete_fragshader_cb, ctx);
323    _mesa_DeleteHashTable(shared->ATIShaders);
324    _mesa_delete_ati_fragment_shader(ctx, shared->DefaultFragmentShader);
325 #endif
326
327 #if FEATURE_ARB_vertex_buffer_object || FEATURE_ARB_pixel_buffer_object
328    _mesa_HashDeleteAll(shared->BufferObjects, delete_bufferobj_cb, ctx);
329    _mesa_DeleteHashTable(shared->BufferObjects);
330 #endif
331
332 #if FEATURE_EXT_framebuffer_object
333    _mesa_HashDeleteAll(shared->FrameBuffers, delete_framebuffer_cb, ctx);
334    _mesa_DeleteHashTable(shared->FrameBuffers);
335    _mesa_HashDeleteAll(shared->RenderBuffers, delete_renderbuffer_cb, ctx);
336    _mesa_DeleteHashTable(shared->RenderBuffers);
337 #endif
338
339 #if FEATURE_ARB_vertex_buffer_object
340    _mesa_reference_buffer_object(ctx, &shared->NullBufferObj, NULL);
341 #endif
342
343 #if FEATURE_ARB_sync
344    {
345       struct simple_node *node;
346       struct simple_node *temp;
347
348       foreach_s(node, temp, & shared->SyncObjects) {
349          _mesa_unref_sync_object(ctx, (struct gl_sync_object *) node);
350       }
351    }
352 #endif
353
354    /*
355     * Free texture objects (after FBOs since some textures might have
356     * been bound to FBOs).
357     */
358    ASSERT(ctx->Driver.DeleteTexture);
359    /* the default textures */
360    for (i = 0; i < NUM_TEXTURE_TARGETS; i++) {
361       ctx->Driver.DeleteTexture(ctx, shared->DefaultTex[i]);
362    }
363
364    /* all other textures */
365    _mesa_HashDeleteAll(shared->TexObjects, delete_texture_cb, ctx);
366    _mesa_DeleteHashTable(shared->TexObjects);
367
368    _glthread_DESTROY_MUTEX(shared->Mutex);
369    _glthread_DESTROY_MUTEX(shared->TexMutex);
370
371    _mesa_free(shared);
372 }
373
374
375 /**
376  * Decrement shared state object reference count and potentially free it
377  * and all children structures.
378  *
379  * \param ctx GL context.
380  * \param shared shared state pointer.
381  *
382  * \sa free_shared_state().
383  */
384 void
385 _mesa_release_shared_state(GLcontext *ctx, struct gl_shared_state *shared)
386 {
387    GLint RefCount;
388
389    _glthread_LOCK_MUTEX(shared->Mutex);
390    RefCount = --shared->RefCount;
391    _glthread_UNLOCK_MUTEX(shared->Mutex);
392
393    assert(RefCount >= 0);
394
395    if (RefCount == 0) {
396       /* free shared state */
397       free_shared_state( ctx, shared );
398    }
399 }