mesa: Restore 78-column wrapping of license text in C-style comments.
[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 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 /**
27 * \file shared.c
28 * Shared-context state
29 */
30
31 #include "imports.h"
32 #include "mtypes.h"
33 #include "hash.h"
34 #include "hash_table.h"
35 #include "atifragshader.h"
36 #include "bufferobj.h"
37 #include "shared.h"
38 #include "program/program.h"
39 #include "dlist.h"
40 #include "samplerobj.h"
41 #include "set.h"
42 #include "shaderobj.h"
43 #include "syncobj.h"
44
45
46 /**
47 * Allocate and initialize a shared context state structure.
48 * Initializes the display list, texture objects and vertex programs hash
49 * tables, allocates the texture objects. If it runs out of memory, frees
50 * everything already allocated before returning NULL.
51 *
52 * \return pointer to a gl_shared_state structure on success, or NULL on
53 * failure.
54 */
55 struct gl_shared_state *
56 _mesa_alloc_shared_state(struct gl_context *ctx)
57 {
58 struct gl_shared_state *shared;
59 GLuint i;
60
61 shared = CALLOC_STRUCT(gl_shared_state);
62 if (!shared)
63 return NULL;
64
65 _glthread_INIT_MUTEX(shared->Mutex);
66
67 shared->DisplayList = _mesa_NewHashTable();
68 shared->TexObjects = _mesa_NewHashTable();
69 shared->Programs = _mesa_NewHashTable();
70
71 shared->DefaultVertexProgram =
72 gl_vertex_program(ctx->Driver.NewProgram(ctx,
73 GL_VERTEX_PROGRAM_ARB, 0));
74 shared->DefaultFragmentProgram =
75 gl_fragment_program(ctx->Driver.NewProgram(ctx,
76 GL_FRAGMENT_PROGRAM_ARB, 0));
77
78 shared->ATIShaders = _mesa_NewHashTable();
79 shared->DefaultFragmentShader = _mesa_new_ati_fragment_shader(ctx, 0);
80
81 shared->ShaderObjects = _mesa_NewHashTable();
82
83 shared->BufferObjects = _mesa_NewHashTable();
84
85 /* GL_ARB_sampler_objects */
86 shared->SamplerObjects = _mesa_NewHashTable();
87
88 /* Allocate the default buffer object */
89 shared->NullBufferObj = ctx->Driver.NewBufferObject(ctx, 0, 0);
90
91 /* Create default texture objects */
92 for (i = 0; i < NUM_TEXTURE_TARGETS; i++) {
93 /* NOTE: the order of these enums matches the TEXTURE_x_INDEX values */
94 static const GLenum targets[] = {
95 GL_TEXTURE_2D_MULTISAMPLE,
96 GL_TEXTURE_2D_MULTISAMPLE_ARRAY,
97 GL_TEXTURE_CUBE_MAP_ARRAY,
98 GL_TEXTURE_BUFFER,
99 GL_TEXTURE_2D_ARRAY_EXT,
100 GL_TEXTURE_1D_ARRAY_EXT,
101 GL_TEXTURE_EXTERNAL_OES,
102 GL_TEXTURE_CUBE_MAP,
103 GL_TEXTURE_3D,
104 GL_TEXTURE_RECTANGLE_NV,
105 GL_TEXTURE_2D,
106 GL_TEXTURE_1D
107 };
108 STATIC_ASSERT(Elements(targets) == NUM_TEXTURE_TARGETS);
109 shared->DefaultTex[i] = ctx->Driver.NewTextureObject(ctx, 0, targets[i]);
110 }
111
112 /* sanity check */
113 assert(shared->DefaultTex[TEXTURE_1D_INDEX]->RefCount == 1);
114
115 /* Mutex and timestamp for texobj state validation */
116 _glthread_INIT_MUTEX(shared->TexMutex);
117 shared->TextureStateStamp = 0;
118
119 shared->FrameBuffers = _mesa_NewHashTable();
120 shared->RenderBuffers = _mesa_NewHashTable();
121
122 shared->SyncObjects = _mesa_set_create(NULL, _mesa_key_pointer_equal);
123
124 return shared;
125 }
126
127
128 /**
129 * Callback for deleting a display list. Called by _mesa_HashDeleteAll().
130 */
131 static void
132 delete_displaylist_cb(GLuint id, void *data, void *userData)
133 {
134 struct gl_display_list *list = (struct gl_display_list *) data;
135 struct gl_context *ctx = (struct gl_context *) userData;
136 _mesa_delete_list(ctx, list);
137 }
138
139
140 /**
141 * Callback for deleting a texture object. Called by _mesa_HashDeleteAll().
142 */
143 static void
144 delete_texture_cb(GLuint id, void *data, void *userData)
145 {
146 struct gl_texture_object *texObj = (struct gl_texture_object *) data;
147 struct gl_context *ctx = (struct gl_context *) userData;
148 ctx->Driver.DeleteTexture(ctx, texObj);
149 }
150
151
152 /**
153 * Callback for deleting a program object. Called by _mesa_HashDeleteAll().
154 */
155 static void
156 delete_program_cb(GLuint id, void *data, void *userData)
157 {
158 struct gl_program *prog = (struct gl_program *) data;
159 struct gl_context *ctx = (struct gl_context *) userData;
160 if(prog != &_mesa_DummyProgram) {
161 ASSERT(prog->RefCount == 1); /* should only be referenced by hash table */
162 prog->RefCount = 0; /* now going away */
163 ctx->Driver.DeleteProgram(ctx, prog);
164 }
165 }
166
167
168 /**
169 * Callback for deleting an ATI fragment shader object.
170 * Called by _mesa_HashDeleteAll().
171 */
172 static void
173 delete_fragshader_cb(GLuint id, void *data, void *userData)
174 {
175 struct ati_fragment_shader *shader = (struct ati_fragment_shader *) data;
176 struct gl_context *ctx = (struct gl_context *) userData;
177 _mesa_delete_ati_fragment_shader(ctx, shader);
178 }
179
180
181 /**
182 * Callback for deleting a buffer object. Called by _mesa_HashDeleteAll().
183 */
184 static void
185 delete_bufferobj_cb(GLuint id, void *data, void *userData)
186 {
187 struct gl_buffer_object *bufObj = (struct gl_buffer_object *) data;
188 struct gl_context *ctx = (struct gl_context *) userData;
189 if (_mesa_bufferobj_mapped(bufObj)) {
190 ctx->Driver.UnmapBuffer(ctx, bufObj);
191 bufObj->Pointer = NULL;
192 }
193 _mesa_reference_buffer_object(ctx, &bufObj, NULL);
194 }
195
196
197 /**
198 * Callback for freeing shader program data. Call it before delete_shader_cb
199 * to avoid memory access error.
200 */
201 static void
202 free_shader_program_data_cb(GLuint id, void *data, void *userData)
203 {
204 struct gl_context *ctx = (struct gl_context *) userData;
205 struct gl_shader_program *shProg = (struct gl_shader_program *) data;
206
207 if (shProg->Type == GL_SHADER_PROGRAM_MESA) {
208 _mesa_free_shader_program_data(ctx, shProg);
209 }
210 }
211
212
213 /**
214 * Callback for deleting shader and shader programs objects.
215 * Called by _mesa_HashDeleteAll().
216 */
217 static void
218 delete_shader_cb(GLuint id, void *data, void *userData)
219 {
220 struct gl_context *ctx = (struct gl_context *) userData;
221 struct gl_shader *sh = (struct gl_shader *) data;
222 if (sh->Type == GL_FRAGMENT_SHADER || sh->Type == GL_VERTEX_SHADER) {
223 ctx->Driver.DeleteShader(ctx, sh);
224 }
225 else {
226 struct gl_shader_program *shProg = (struct gl_shader_program *) data;
227 ASSERT(shProg->Type == GL_SHADER_PROGRAM_MESA);
228 ctx->Driver.DeleteShaderProgram(ctx, shProg);
229 }
230 }
231
232
233 /**
234 * Callback for deleting a framebuffer object. Called by _mesa_HashDeleteAll()
235 */
236 static void
237 delete_framebuffer_cb(GLuint id, void *data, void *userData)
238 {
239 struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
240 /* The fact that the framebuffer is in the hashtable means its refcount
241 * is one, but we're removing from the hashtable now. So clear refcount.
242 */
243 /*assert(fb->RefCount == 1);*/
244 fb->RefCount = 0;
245
246 /* NOTE: Delete should always be defined but there are two reports
247 * of it being NULL (bugs 13507, 14293). Work-around for now.
248 */
249 if (fb->Delete)
250 fb->Delete(fb);
251 }
252
253
254 /**
255 * Callback for deleting a renderbuffer object. Called by _mesa_HashDeleteAll()
256 */
257 static void
258 delete_renderbuffer_cb(GLuint id, void *data, void *userData)
259 {
260 struct gl_context *ctx = (struct gl_context *) userData;
261 struct gl_renderbuffer *rb = (struct gl_renderbuffer *) data;
262 rb->RefCount = 0; /* see comment for FBOs above */
263 if (rb->Delete)
264 rb->Delete(ctx, rb);
265 }
266
267
268 /**
269 * Callback for deleting a sampler object. Called by _mesa_HashDeleteAll()
270 */
271 static void
272 delete_sampler_object_cb(GLuint id, void *data, void *userData)
273 {
274 struct gl_context *ctx = (struct gl_context *) userData;
275 struct gl_sampler_object *sampObj = (struct gl_sampler_object *) data;
276 _mesa_reference_sampler_object(ctx, &sampObj, NULL);
277 }
278
279
280 /**
281 * Deallocate a shared state object and all children structures.
282 *
283 * \param ctx GL context.
284 * \param shared shared state pointer.
285 *
286 * Frees the display lists, the texture objects (calling the driver texture
287 * deletion callback to free its private data) and the vertex programs, as well
288 * as their hash tables.
289 *
290 * \sa alloc_shared_state().
291 */
292 static void
293 free_shared_state(struct gl_context *ctx, struct gl_shared_state *shared)
294 {
295 GLuint i;
296
297 /* Free the dummy/fallback texture objects */
298 for (i = 0; i < NUM_TEXTURE_TARGETS; i++) {
299 if (shared->FallbackTex[i])
300 ctx->Driver.DeleteTexture(ctx, shared->FallbackTex[i]);
301 }
302
303 /*
304 * Free display lists
305 */
306 _mesa_HashDeleteAll(shared->DisplayList, delete_displaylist_cb, ctx);
307 _mesa_DeleteHashTable(shared->DisplayList);
308
309 _mesa_HashWalk(shared->ShaderObjects, free_shader_program_data_cb, ctx);
310 _mesa_HashDeleteAll(shared->ShaderObjects, delete_shader_cb, ctx);
311 _mesa_DeleteHashTable(shared->ShaderObjects);
312
313 _mesa_HashDeleteAll(shared->Programs, delete_program_cb, ctx);
314 _mesa_DeleteHashTable(shared->Programs);
315
316 _mesa_reference_vertprog(ctx, &shared->DefaultVertexProgram, NULL);
317 _mesa_reference_fragprog(ctx, &shared->DefaultFragmentProgram, NULL);
318
319 _mesa_HashDeleteAll(shared->ATIShaders, delete_fragshader_cb, ctx);
320 _mesa_DeleteHashTable(shared->ATIShaders);
321 _mesa_delete_ati_fragment_shader(ctx, shared->DefaultFragmentShader);
322
323 _mesa_HashDeleteAll(shared->BufferObjects, delete_bufferobj_cb, ctx);
324 _mesa_DeleteHashTable(shared->BufferObjects);
325
326 _mesa_HashDeleteAll(shared->FrameBuffers, delete_framebuffer_cb, ctx);
327 _mesa_DeleteHashTable(shared->FrameBuffers);
328 _mesa_HashDeleteAll(shared->RenderBuffers, delete_renderbuffer_cb, ctx);
329 _mesa_DeleteHashTable(shared->RenderBuffers);
330
331 _mesa_reference_buffer_object(ctx, &shared->NullBufferObj, NULL);
332
333 {
334 struct set_entry *entry;
335
336 set_foreach(shared->SyncObjects, entry) {
337 _mesa_unref_sync_object(ctx, (struct gl_sync_object *) entry->key);
338 }
339 }
340 _mesa_set_destroy(shared->SyncObjects, NULL);
341
342 _mesa_HashDeleteAll(shared->SamplerObjects, delete_sampler_object_cb, ctx);
343 _mesa_DeleteHashTable(shared->SamplerObjects);
344
345 /*
346 * Free texture objects (after FBOs since some textures might have
347 * been bound to FBOs).
348 */
349 ASSERT(ctx->Driver.DeleteTexture);
350 /* the default textures */
351 for (i = 0; i < NUM_TEXTURE_TARGETS; i++) {
352 ctx->Driver.DeleteTexture(ctx, shared->DefaultTex[i]);
353 }
354
355 /* all other textures */
356 _mesa_HashDeleteAll(shared->TexObjects, delete_texture_cb, ctx);
357 _mesa_DeleteHashTable(shared->TexObjects);
358
359 _glthread_DESTROY_MUTEX(shared->Mutex);
360 _glthread_DESTROY_MUTEX(shared->TexMutex);
361
362 free(shared);
363 }
364
365
366 /**
367 * gl_shared_state objects are ref counted.
368 * If ptr's refcount goes to zero, free the shared state.
369 */
370 void
371 _mesa_reference_shared_state(struct gl_context *ctx,
372 struct gl_shared_state **ptr,
373 struct gl_shared_state *state)
374 {
375 if (*ptr == state)
376 return;
377
378 if (*ptr) {
379 /* unref old state */
380 struct gl_shared_state *old = *ptr;
381 GLboolean delete;
382
383 _glthread_LOCK_MUTEX(old->Mutex);
384 assert(old->RefCount >= 1);
385 old->RefCount--;
386 delete = (old->RefCount == 0);
387 _glthread_UNLOCK_MUTEX(old->Mutex);
388
389 if (delete) {
390 free_shared_state(ctx, old);
391 }
392
393 *ptr = NULL;
394 }
395
396 if (state) {
397 /* reference new state */
398 _glthread_LOCK_MUTEX(state->Mutex);
399 state->RefCount++;
400 *ptr = state;
401 _glthread_UNLOCK_MUTEX(state->Mutex);
402 }
403 }