2 Copyright (c) 2008, 2009 Apple Inc.
4 Permission is hereby granted, free of charge, to any person
5 obtaining a copy of this software and associated documentation files
6 (the "Software"), to deal in the Software without restriction,
7 including without limitation the rights to use, copy, modify, merge,
8 publish, distribute, sublicense, and/or sell copies of the Software,
9 and to permit persons to whom the Software is furnished to do so,
10 subject to the following conditions:
12 The above copyright notice and this permission notice shall be
13 included in all copies or substantial portions of the Software.
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 NONINFRINGEMENT. IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT
19 HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
24 Except as contained in this notice, the name(s) of the above
25 copyright holders shall not be used in advertising or otherwise to
26 promote the sale, use or other dealings in this Software without
27 prior written authorization.
36 #include "apple_glx.h"
37 #include "apple_glx_context.h"
38 #include "apple_glx_drawable.h"
41 static pthread_mutex_t drawables_lock
= PTHREAD_MUTEX_INITIALIZER
;
42 static struct apple_glx_drawable
*drawables_list
= NULL
;
45 lock_drawables_list(void)
49 err
= pthread_mutex_lock(&drawables_lock
);
52 fprintf(stderr
, "pthread_mutex_lock failure in %s: %s\n",
53 __func__
, strerror(err
));
59 unlock_drawables_list(void)
63 err
= pthread_mutex_unlock(&drawables_lock
);
66 fprintf(stderr
, "pthread_mutex_unlock failure in %s: %s\n",
67 __func__
, strerror(err
));
72 struct apple_glx_drawable
*
73 apple_glx_find_drawable(Display
* dpy
, GLXDrawable drawable
)
75 struct apple_glx_drawable
*i
, *agd
= NULL
;
77 lock_drawables_list();
79 for (i
= drawables_list
; i
; i
= i
->next
) {
80 if (i
->drawable
== drawable
) {
86 unlock_drawables_list();
92 drawable_lock(struct apple_glx_drawable
*agd
)
96 err
= pthread_mutex_lock(&agd
->mutex
);
99 fprintf(stderr
, "pthread_mutex_lock error: %s\n", strerror(err
));
105 drawable_unlock(struct apple_glx_drawable
*d
)
109 err
= pthread_mutex_unlock(&d
->mutex
);
112 fprintf(stderr
, "pthread_mutex_unlock error: %s\n", strerror(err
));
119 reference_drawable(struct apple_glx_drawable
*d
)
122 d
->reference_count
++;
127 release_drawable(struct apple_glx_drawable
*d
)
130 d
->reference_count
--;
134 /* The drawables list must be locked prior to calling this. */
135 /* Return true if the drawable was destroyed. */
137 destroy_drawable(struct apple_glx_drawable
*d
)
143 if (d
->reference_count
> 0) {
151 d
->previous
->next
= d
->next
;
155 * The item must be at the head of the list, if it
156 * has no previous pointer.
158 drawables_list
= d
->next
;
162 d
->next
->previous
= d
->previous
;
164 unlock_drawables_list();
166 if (d
->callbacks
.destroy
) {
168 * Warning: this causes other routines to be called (potentially)
169 * from surface_notify_handler. It's probably best to not have
170 * any locks at this point locked.
172 d
->callbacks
.destroy(d
->display
, d
);
175 apple_glx_diagnostic("%s: freeing %p\n", __func__
, (void *) d
);
177 err
= pthread_mutex_destroy(&d
->mutex
);
179 fprintf(stderr
, "pthread_mutex_destroy error: %s\n", strerror(err
));
185 /* So that the locks are balanced and the caller correctly unlocks. */
186 lock_drawables_list();
192 * This is typically called when a context is destroyed or the current
193 * drawable is made None.
196 destroy_drawable_callback(struct apple_glx_drawable
*d
)
202 apple_glx_diagnostic("%s: %p ->reference_count before -- %d\n", __func__
,
203 (void *) d
, d
->reference_count
);
205 d
->reference_count
--;
207 if (d
->reference_count
> 0) {
214 lock_drawables_list();
216 result
= destroy_drawable(d
);
218 unlock_drawables_list();
224 is_pbuffer(struct apple_glx_drawable
*d
)
226 return APPLE_GLX_DRAWABLE_PBUFFER
== d
->type
;
230 is_pixmap(struct apple_glx_drawable
*d
)
232 return APPLE_GLX_DRAWABLE_PIXMAP
== d
->type
;
236 common_init(Display
* dpy
, GLXDrawable drawable
, struct apple_glx_drawable
*d
)
239 pthread_mutexattr_t attr
;
242 d
->reference_count
= 0;
243 d
->drawable
= drawable
;
246 err
= pthread_mutexattr_init(&attr
);
249 fprintf(stderr
, "pthread_mutexattr_init error: %s\n", strerror(err
));
254 * There are some patterns that require a recursive mutex,
255 * when working with locks that protect the apple_glx_drawable,
256 * and reference functions like ->reference, and ->release.
258 err
= pthread_mutexattr_settype(&attr
, PTHREAD_MUTEX_RECURSIVE
);
261 fprintf(stderr
, "error: setting pthread mutex type: %s\n", strerror(err
));
265 err
= pthread_mutex_init(&d
->mutex
, &attr
);
268 fprintf(stderr
, "pthread_mutex_init error: %s\n", strerror(err
));
272 (void) pthread_mutexattr_destroy(&attr
);
274 d
->lock
= drawable_lock
;
275 d
->unlock
= drawable_unlock
;
277 d
->reference
= reference_drawable
;
278 d
->release
= release_drawable
;
280 d
->destroy
= destroy_drawable_callback
;
282 d
->is_pbuffer
= is_pbuffer
;
283 d
->is_pixmap
= is_pixmap
;
291 d
->buffer_length
= 0;
298 link_tail(struct apple_glx_drawable
*agd
)
300 lock_drawables_list();
302 /* Link the new drawable into the global list. */
303 agd
->next
= drawables_list
;
306 drawables_list
->previous
= agd
;
308 drawables_list
= agd
;
310 unlock_drawables_list();
313 /*WARNING: this returns a locked and referenced object. */
315 apple_glx_drawable_create(Display
* dpy
,
317 GLXDrawable drawable
,
318 struct apple_glx_drawable
**agdResult
,
319 struct apple_glx_drawable_callbacks
*callbacks
)
321 struct apple_glx_drawable
*d
;
323 d
= calloc(1, sizeof *d
);
330 common_init(dpy
, drawable
, d
);
331 d
->type
= callbacks
->type
;
332 d
->callbacks
= *callbacks
;
339 apple_glx_diagnostic("%s: new drawable %p\n", __func__
, (void *) d
);
346 static int error_count
= 0;
349 error_handler(Display
* dpy
, XErrorEvent
* err
)
351 if (err
->error_code
== BadWindow
) {
359 apple_glx_garbage_collect_drawables(Display
* dpy
)
361 struct apple_glx_drawable
*d
, *dnext
;
364 unsigned int width
, height
, bd
, depth
;
365 int (*old_handler
) (Display
*, XErrorEvent
*);
368 if (NULL
== drawables_list
)
371 old_handler
= XSetErrorHandler(error_handler
);
375 lock_drawables_list();
377 for (d
= drawables_list
; d
;) {
382 if (d
->reference_count
> 0) {
384 * Skip this, because some context still retains a reference
397 * Mesa uses XGetWindowAttributes, but some of these things are
398 * most definitely not Windows, and that's against the rules.
399 * XGetGeometry on the other hand is legal with a Pixmap and Window.
401 XGetGeometry(dpy
, d
->drawable
, &root
, &x
, &y
, &width
, &height
, &bd
,
404 if (error_count
> 0) {
406 * Note: this may not actually destroy the drawable.
407 * If another context retains a reference to the drawable
408 * after the reference count test above.
410 (void) destroy_drawable(d
);
417 XSetErrorHandler(old_handler
);
419 unlock_drawables_list();
423 apple_glx_get_drawable_count(void)
425 unsigned int result
= 0;
426 struct apple_glx_drawable
*d
;
428 lock_drawables_list();
430 for (d
= drawables_list
; d
; d
= d
->next
)
433 unlock_drawables_list();
438 struct apple_glx_drawable
*
439 apple_glx_drawable_find_by_type(GLXDrawable drawable
, int type
, int flags
)
441 struct apple_glx_drawable
*d
;
443 lock_drawables_list();
445 for (d
= drawables_list
; d
; d
= d
->next
) {
446 if (d
->type
== type
&& d
->drawable
== drawable
) {
447 if (flags
& APPLE_GLX_DRAWABLE_REFERENCE
)
450 if (flags
& APPLE_GLX_DRAWABLE_LOCK
)
453 unlock_drawables_list();
459 unlock_drawables_list();
464 struct apple_glx_drawable
*
465 apple_glx_drawable_find(GLXDrawable drawable
, int flags
)
467 struct apple_glx_drawable
*d
;
469 lock_drawables_list();
471 for (d
= drawables_list
; d
; d
= d
->next
) {
472 if (d
->drawable
== drawable
) {
473 if (flags
& APPLE_GLX_DRAWABLE_REFERENCE
)
476 if (flags
& APPLE_GLX_DRAWABLE_LOCK
)
479 unlock_drawables_list();
485 unlock_drawables_list();
490 /* Return true if the type is valid for the drawable. */
492 apple_glx_drawable_destroy_by_type(Display
* dpy
,
493 GLXDrawable drawable
, int type
)
495 struct apple_glx_drawable
*d
;
497 lock_drawables_list();
499 for (d
= drawables_list
; d
; d
= d
->next
) {
500 if (drawable
== d
->drawable
&& type
== d
->type
) {
502 * The user has requested that we destroy this resource.
503 * However, there may be references in the contexts to it, so
504 * release it, and call destroy_drawable which doesn't destroy
505 * if the reference_count is > 0.
509 apple_glx_diagnostic("%s d->reference_count %d\n",
510 __func__
, d
->reference_count
);
513 unlock_drawables_list();
518 unlock_drawables_list();
523 struct apple_glx_drawable
*
524 apple_glx_drawable_find_by_uid(unsigned int uid
, int flags
)
526 struct apple_glx_drawable
*d
;
528 lock_drawables_list();
530 for (d
= drawables_list
; d
; d
= d
->next
) {
531 /* Only surfaces have a uid. */
532 if (APPLE_GLX_DRAWABLE_SURFACE
== d
->type
) {
533 if (d
->types
.surface
.uid
== uid
) {
534 if (flags
& APPLE_GLX_DRAWABLE_REFERENCE
)
537 if (flags
& APPLE_GLX_DRAWABLE_LOCK
)
540 unlock_drawables_list();
547 unlock_drawables_list();