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