mesa: reference counting for gl_array_object
[mesa.git] / src / mesa / main / arrayobj.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.2
4 *
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * (C) Copyright IBM Corporation 2006
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL OR IBM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
23 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26
27
28 /**
29 * \file arrayobj.c
30 * Functions for the GL_APPLE_vertex_array_object extension.
31 *
32 * \todo
33 * The code in this file borrows a lot from bufferobj.c. There's a certain
34 * amount of cruft left over from that origin that may be unnecessary.
35 *
36 * \author Ian Romanick <idr@us.ibm.com>
37 * \author Brian Paul
38 */
39
40
41 #include "glheader.h"
42 #include "hash.h"
43 #include "imports.h"
44 #include "context.h"
45 #if FEATURE_ARB_vertex_buffer_object
46 #include "bufferobj.h"
47 #endif
48 #include "arrayobj.h"
49 #include "glapi/dispatch.h"
50
51
52 /**
53 * Look up the array object for the given ID.
54 *
55 * \returns
56 * Either a pointer to the array object with the specified ID or \c NULL for
57 * a non-existent ID. The spec defines ID 0 as being technically
58 * non-existent.
59 */
60
61 static INLINE struct gl_array_object *
62 lookup_arrayobj(GLcontext *ctx, GLuint id)
63 {
64 return (id == 0)
65 ? NULL
66 : (struct gl_array_object *) _mesa_HashLookup(ctx->Shared->ArrayObjects,
67 id);
68 }
69
70
71 /**
72 * Allocate and initialize a new vertex array object.
73 *
74 * This function is intended to be called via
75 * \c dd_function_table::NewArrayObject.
76 */
77 struct gl_array_object *
78 _mesa_new_array_object( GLcontext *ctx, GLuint name )
79 {
80 struct gl_array_object *obj = CALLOC_STRUCT(gl_array_object);
81 if (obj)
82 _mesa_initialize_array_object(ctx, obj, name);
83 return obj;
84 }
85
86
87 /**
88 * Delete an array object.
89 *
90 * This function is intended to be called via
91 * \c dd_function_table::DeleteArrayObject.
92 */
93 void
94 _mesa_delete_array_object( GLcontext *ctx, struct gl_array_object *obj )
95 {
96 (void) ctx;
97 _glthread_DESTROY_MUTEX(obj->Mutex);
98 _mesa_free(obj);
99 }
100
101
102 /**
103 * Set ptr to arrayObj w/ reference counting.
104 */
105 void
106 _mesa_reference_array_object(GLcontext *ctx,
107 struct gl_array_object **ptr,
108 struct gl_array_object *arrayObj)
109 {
110 if (*ptr == arrayObj)
111 return;
112
113 if (*ptr) {
114 /* Unreference the old array object */
115 GLboolean deleteFlag = GL_FALSE;
116 struct gl_array_object *oldObj = *ptr;
117
118 _glthread_LOCK_MUTEX(oldObj->Mutex);
119 ASSERT(oldObj->RefCount > 0);
120 oldObj->RefCount--;
121 #if 0
122 printf("ArrayObj %p %d DECR to %d\n",
123 (void *) oldObj, oldObj->Name, oldObj->RefCount);
124 #endif
125 deleteFlag = (oldObj->RefCount == 0);
126 _glthread_UNLOCK_MUTEX(oldObj->Mutex);
127
128 if (deleteFlag) {
129 ASSERT(ctx->Driver.DeleteArrayObject);
130 ctx->Driver.DeleteArrayObject(ctx, oldObj);
131 }
132
133 *ptr = NULL;
134 }
135 ASSERT(!*ptr);
136
137 if (arrayObj) {
138 /* reference new array object */
139 _glthread_LOCK_MUTEX(arrayObj->Mutex);
140 if (arrayObj->RefCount == 0) {
141 /* this array's being deleted (look just above) */
142 /* Not sure this can every really happen. Warn if it does. */
143 _mesa_problem(NULL, "referencing deleted array object");
144 *ptr = NULL;
145 }
146 else {
147 arrayObj->RefCount++;
148 #if 0
149 printf("ArrayObj %p %d INCR to %d\n",
150 (void *) arrayObj, arrayObj->Name, arrayObj->RefCount);
151 #endif
152 *ptr = arrayObj;
153 }
154 _glthread_UNLOCK_MUTEX(arrayObj->Mutex);
155 }
156 }
157
158
159
160 static void
161 init_array(GLcontext *ctx,
162 struct gl_client_array *array, GLint size, GLint type)
163 {
164 array->Size = size;
165 array->Type = type;
166 array->Format = GL_RGBA; /* only significant for GL_EXT_vertex_array_bgra */
167 array->Stride = 0;
168 array->StrideB = 0;
169 array->Ptr = NULL;
170 array->Enabled = GL_FALSE;
171 array->Normalized = GL_FALSE;
172 #if FEATURE_ARB_vertex_buffer_object
173 /* Vertex array buffers */
174 array->BufferObj = ctx->Array.NullBufferObj;
175 #endif
176 }
177
178
179 /**
180 * Initialize a gl_array_object's arrays.
181 */
182 void
183 _mesa_initialize_array_object( GLcontext *ctx,
184 struct gl_array_object *obj,
185 GLuint name )
186 {
187 GLuint i;
188
189 obj->Name = name;
190
191 _glthread_INIT_MUTEX(obj->Mutex);
192 obj->RefCount = 1;
193
194 /* Init the individual arrays */
195 init_array(ctx, &obj->Vertex, 4, GL_FLOAT);
196 init_array(ctx, &obj->Normal, 3, GL_FLOAT);
197 init_array(ctx, &obj->Color, 4, GL_FLOAT);
198 init_array(ctx, &obj->SecondaryColor, 4, GL_FLOAT);
199 init_array(ctx, &obj->FogCoord, 1, GL_FLOAT);
200 init_array(ctx, &obj->Index, 1, GL_FLOAT);
201 for (i = 0; i < MAX_TEXTURE_COORD_UNITS; i++) {
202 init_array(ctx, &obj->TexCoord[i], 4, GL_FLOAT);
203 }
204 init_array(ctx, &obj->EdgeFlag, 1, GL_BOOL);
205 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
206 init_array(ctx, &obj->VertexAttrib[i], 4, GL_FLOAT);
207 }
208
209 #if FEATURE_point_size_array
210 init_array(ctx, &obj->PointSize, 1, GL_FLOAT);
211 #endif
212 }
213
214
215 /**
216 * Add the given array object to the array object pool.
217 */
218 void
219 _mesa_save_array_object( GLcontext *ctx, struct gl_array_object *obj )
220 {
221 if (obj->Name > 0) {
222 /* insert into hash table */
223 _mesa_HashInsert(ctx->Shared->ArrayObjects, obj->Name, obj);
224 }
225 }
226
227
228 /**
229 * Remove the given array object from the array object pool.
230 * Do not deallocate the array object though.
231 */
232 void
233 _mesa_remove_array_object( GLcontext *ctx, struct gl_array_object *obj )
234 {
235 if (obj->Name > 0) {
236 /* remove from hash table */
237 _mesa_HashRemove(ctx->Shared->ArrayObjects, obj->Name);
238 }
239 }
240
241
242 static void
243 unbind_buffer_object( GLcontext *ctx, struct gl_buffer_object *bufObj )
244 {
245 if (bufObj != ctx->Array.NullBufferObj) {
246 _mesa_reference_buffer_object(ctx, &bufObj, NULL);
247 }
248 }
249
250
251 /**********************************************************************/
252 /* API Functions */
253 /**********************************************************************/
254
255 /**
256 * Bind a new array.
257 *
258 * \todo
259 * The binding could be done more efficiently by comparing the non-NULL
260 * pointers in the old and new objects. The only arrays that are "dirty" are
261 * the ones that are non-NULL in either object.
262 */
263 void GLAPIENTRY
264 _mesa_BindVertexArrayAPPLE( GLuint id )
265 {
266 GET_CURRENT_CONTEXT(ctx);
267 struct gl_array_object * const oldObj = ctx->Array.ArrayObj;
268 struct gl_array_object *newObj = NULL;
269 ASSERT_OUTSIDE_BEGIN_END(ctx);
270
271 ASSERT(oldObj != NULL);
272
273 if ( oldObj->Name == id )
274 return; /* rebinding the same array object- no change */
275
276 /*
277 * Get pointer to new array object (newBufObj)
278 */
279 if (id == 0) {
280 /* The spec says there is no array object named 0, but we use
281 * one internally because it simplifies things.
282 */
283 newObj = ctx->Array.DefaultArrayObj;
284 }
285 else {
286 /* non-default array object */
287 newObj = lookup_arrayobj(ctx, id);
288 if (!newObj) {
289 /* If this is a new array object id, allocate an array object now.
290 */
291 newObj = (*ctx->Driver.NewArrayObject)(ctx, id);
292 if (!newObj) {
293 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindVertexArrayAPPLE");
294 return;
295 }
296 _mesa_save_array_object(ctx, newObj);
297 }
298 }
299
300 ctx->NewState |= _NEW_ARRAY;
301 ctx->Array.NewState |= _NEW_ARRAY_ALL;
302 _mesa_reference_array_object(ctx, &ctx->Array.ArrayObj, newObj);
303
304 /* Pass BindVertexArray call to device driver */
305 if (ctx->Driver.BindArrayObject && newObj)
306 (*ctx->Driver.BindArrayObject)( ctx, newObj );
307 }
308
309
310 /**
311 * Delete a set of array objects.
312 *
313 * \param n Number of array objects to delete.
314 * \param ids Array of \c n array object IDs.
315 */
316 void GLAPIENTRY
317 _mesa_DeleteVertexArraysAPPLE(GLsizei n, const GLuint *ids)
318 {
319 GET_CURRENT_CONTEXT(ctx);
320 GLsizei i;
321 ASSERT_OUTSIDE_BEGIN_END(ctx);
322
323 if (n < 0) {
324 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteVertexArrayAPPLE(n)");
325 return;
326 }
327
328 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
329
330 for (i = 0; i < n; i++) {
331 struct gl_array_object *obj = lookup_arrayobj(ctx, ids[i]);
332
333 if ( obj != NULL ) {
334 ASSERT( obj->Name == ids[i] );
335
336
337 /* If the array object is currently bound, the spec says "the binding
338 * for that object reverts to zero and the default vertex array
339 * becomes current."
340 */
341 if ( obj == ctx->Array.ArrayObj ) {
342 CALL_BindVertexArrayAPPLE( ctx->Exec, (0) );
343 }
344
345 #if FEATURE_ARB_vertex_buffer_object
346 /* Unbind any buffer objects that might be bound to arrays in
347 * this array object.
348 */
349 unbind_buffer_object( ctx, obj->Vertex.BufferObj );
350 unbind_buffer_object( ctx, obj->Normal.BufferObj );
351 unbind_buffer_object( ctx, obj->Color.BufferObj );
352 unbind_buffer_object( ctx, obj->SecondaryColor.BufferObj );
353 unbind_buffer_object( ctx, obj->FogCoord.BufferObj );
354 unbind_buffer_object( ctx, obj->Index.BufferObj );
355 for (i = 0; i < MAX_TEXTURE_COORD_UNITS; i++) {
356 unbind_buffer_object( ctx, obj->TexCoord[i].BufferObj );
357 }
358 unbind_buffer_object( ctx, obj->EdgeFlag.BufferObj );
359 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
360 unbind_buffer_object( ctx, obj->VertexAttrib[i].BufferObj );
361 }
362 #endif
363
364 /* The ID is immediately freed for re-use */
365 _mesa_remove_array_object(ctx, obj);
366 ctx->Driver.DeleteArrayObject(ctx, obj);
367 }
368 }
369
370 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
371 }
372
373
374 /**
375 * Generate a set of unique array object IDs and store them in \c arrays.
376 *
377 * \param n Number of IDs to generate.
378 * \param arrays Array of \c n locations to store the IDs.
379 */
380 void GLAPIENTRY
381 _mesa_GenVertexArraysAPPLE(GLsizei n, GLuint *arrays)
382 {
383 GET_CURRENT_CONTEXT(ctx);
384 GLuint first;
385 GLint i;
386 ASSERT_OUTSIDE_BEGIN_END(ctx);
387
388 if (n < 0) {
389 _mesa_error(ctx, GL_INVALID_VALUE, "glGenVertexArraysAPPLE");
390 return;
391 }
392
393 if (!arrays) {
394 return;
395 }
396
397 /*
398 * This must be atomic (generation and allocation of array object IDs)
399 */
400 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
401
402 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->ArrayObjects, n);
403
404 /* Allocate new, empty array objects and return identifiers */
405 for (i = 0; i < n; i++) {
406 struct gl_array_object *obj;
407 GLuint name = first + i;
408
409 obj = (*ctx->Driver.NewArrayObject)( ctx, name );
410 if (!obj) {
411 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
412 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenVertexArraysAPPLE");
413 return;
414 }
415 _mesa_save_array_object(ctx, obj);
416 arrays[i] = first + i;
417 }
418
419 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
420 }
421
422
423 /**
424 * Determine if ID is the name of an array object.
425 *
426 * \param id ID of the potential array object.
427 * \return \c GL_TRUE if \c id is the name of a array object,
428 * \c GL_FALSE otherwise.
429 */
430 GLboolean GLAPIENTRY
431 _mesa_IsVertexArrayAPPLE( GLuint id )
432 {
433 struct gl_array_object * obj;
434 GET_CURRENT_CONTEXT(ctx);
435 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
436
437 if (id == 0)
438 return GL_FALSE;
439
440 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
441 obj = lookup_arrayobj(ctx, id);
442 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
443
444 return (obj != NULL) ? GL_TRUE : GL_FALSE;
445 }