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.
41 // Get the newer glext.h first
45 #include <OpenGL/CGLTypes.h>
46 #include <OpenGL/CGLCurrent.h>
47 #include <OpenGL/OpenGL.h>
49 #include "glxclient.h"
51 #include "apple_glx.h"
52 #include "apple_glx_context.h"
54 #include "apple_visual.h"
55 #include "apple_cgl.h"
56 #include "apple_glx_drawable.h"
58 static pthread_mutex_t context_lock
= PTHREAD_MUTEX_INITIALIZER
;
61 * This should be locked on creation and destruction of the
64 * It's also locked when the surface_notify_handler is searching
65 * for a uid associated with a surface.
67 static struct apple_glx_context
*context_list
= NULL
;
69 /* This guards the context_list above. */
71 lock_context_list(void)
75 err
= pthread_mutex_lock(&context_lock
);
78 fprintf(stderr
, "pthread_mutex_lock failure in %s: %d\n",
85 unlock_context_list(void)
89 err
= pthread_mutex_unlock(&context_lock
);
92 fprintf(stderr
, "pthread_mutex_unlock failure in %s: %d\n",
99 is_context_valid(struct apple_glx_context
*ac
)
101 struct apple_glx_context
*i
;
105 for (i
= context_list
; i
; i
= i
->next
) {
107 unlock_context_list();
112 unlock_context_list();
117 /* This creates an apple_private_context struct.
119 * It's typically called to save the struct in a GLXContext.
121 * This is also where the CGLContextObj is created, and the CGLPixelFormatObj.
124 apple_glx_create_context(void **ptr
, Display
* dpy
, int screen
,
125 const void *mode
, void *sharedContext
,
126 int *errorptr
, bool * x11errorptr
)
128 struct apple_glx_context
*ac
;
129 struct apple_glx_context
*sharedac
= sharedContext
;
134 ac
= malloc(sizeof *ac
);
137 *errorptr
= BadAlloc
;
142 if (sharedac
&& !is_context_valid(sharedac
)) {
143 *errorptr
= GLXBadContext
;
144 *x11errorptr
= false;
148 ac
->context_obj
= NULL
;
149 ac
->pixel_format_obj
= NULL
;
151 ac
->thread_id
= pthread_self();
153 ac
->double_buffered
= false;
154 ac
->uses_stereo
= false;
155 ac
->need_update
= false;
156 ac
->is_current
= false;
157 ac
->made_current
= false;
158 ac
->last_surface_window
= None
;
160 apple_visual_create_pfobj(&ac
->pixel_format_obj
, mode
,
161 &ac
->double_buffered
, &ac
->uses_stereo
,
162 /*offscreen */ false);
164 error
= apple_cgl
.create_context(ac
->pixel_format_obj
,
165 sharedac
? sharedac
->context_obj
: NULL
,
170 (void) apple_cgl
.destroy_pixel_format(ac
->pixel_format_obj
);
174 if (kCGLBadMatch
== error
) {
175 *errorptr
= BadMatch
;
179 *errorptr
= GLXBadContext
;
180 *x11errorptr
= false;
183 if (getenv("LIBGL_DIAGNOSTIC"))
184 fprintf(stderr
, "error: %s\n", apple_cgl
.error_string(error
));
189 /* The context creation succeeded, so we can link in the new context. */
193 context_list
->previous
= ac
;
196 ac
->next
= context_list
;
201 apple_glx_diagnostic("%s: ac %p ac->context_obj %p\n",
202 __func__
, (void *) ac
, (void *) ac
->context_obj
);
204 unlock_context_list();
210 apple_glx_destroy_context(void **ptr
, Display
* dpy
)
212 struct apple_glx_context
*ac
= *ptr
;
217 apple_glx_diagnostic("%s: ac %p ac->context_obj %p\n",
218 __func__
, (void *) ac
, (void *) ac
->context_obj
);
220 if (apple_cgl
.get_current_context() == ac
->context_obj
) {
221 apple_glx_diagnostic("%s: context ac->context_obj %p "
222 "is still current!\n", __func__
,
223 (void *) ac
->context_obj
);
224 if (apple_cgl
.set_current_context(NULL
)) {
229 /* Remove ac from the context_list as soon as possible. */
233 ac
->previous
->next
= ac
->next
;
236 context_list
= ac
->next
;
240 ac
->next
->previous
= ac
->previous
;
243 unlock_context_list();
246 if (apple_cgl
.clear_drawable(ac
->context_obj
)) {
247 fprintf(stderr
, "error: while clearing drawable!\n");
252 * This potentially causes surface_notify_handler to be called in
254 * We can NOT have a lock held at this point. It would result in
255 * an abort due to an attempted deadlock. This is why we earlier
256 * removed the ac pointer from the double-linked list.
259 ac
->drawable
->destroy(ac
->drawable
);
262 if (apple_cgl
.destroy_pixel_format(ac
->pixel_format_obj
)) {
263 fprintf(stderr
, "error: destroying pixel format in %s\n", __func__
);
267 if (apple_cgl
.destroy_context(ac
->context_obj
)) {
268 fprintf(stderr
, "error: destroying context_obj in %s\n", __func__
);
276 apple_glx_garbage_collect_drawables(dpy
);
280 /* Return true if an error occured. */
282 apple_glx_make_current_context(Display
* dpy
, void *oldptr
, void *ptr
,
283 GLXDrawable drawable
)
285 struct apple_glx_context
*oldac
= oldptr
;
286 struct apple_glx_context
*ac
= ptr
;
287 struct apple_glx_drawable
*newagd
= NULL
;
289 bool same_drawable
= false;
292 apple_glx_diagnostic("%s: oldac %p ac %p drawable 0x%lx\n",
293 __func__
, (void *) oldac
, (void *) ac
, drawable
);
295 apple_glx_diagnostic("%s: oldac->context_obj %p ac->context_obj %p\n",
297 (void *) (oldac
? oldac
->context_obj
: NULL
),
298 (void *) (ac
? ac
->context_obj
: NULL
));
301 /* This a common path for GLUT and other apps, so special case it. */
302 if (ac
&& ac
->drawable
&& ac
->drawable
->drawable
== drawable
) {
303 same_drawable
= true;
309 /* Reset the is_current state of the old context, if non-NULL. */
310 if (oldac
&& (ac
!= oldac
))
311 oldac
->is_current
= false;
314 /*Clear the current context for this thread. */
315 apple_cgl
.set_current_context(NULL
);
318 oldac
->is_current
= false;
320 if (oldac
->drawable
) {
321 oldac
->drawable
->destroy(oldac
->drawable
);
322 oldac
->drawable
= NULL
;
325 /* Invalidate this to prevent surface recreation. */
326 oldac
->last_surface_window
= None
;
332 if (None
== drawable
) {
335 /* Clear the current drawable for this context_obj. */
337 if (apple_cgl
.set_current_context(ac
->context_obj
))
340 if (apple_cgl
.clear_drawable(ac
->context_obj
))
344 ac
->drawable
->destroy(ac
->drawable
);
348 /* Invalidate this to prevent surface recreation. */
349 ac
->last_surface_window
= None
;
351 apple_glx_diagnostic("%s: drawable is None, error is: %d\n",
357 /* This is an optimisation to avoid searching for the current drawable. */
358 if (ac
->drawable
&& ac
->drawable
->drawable
== drawable
) {
359 newagd
= ac
->drawable
;
362 /* Find the drawable if possible, and retain a reference to it. */
364 apple_glx_drawable_find(drawable
, APPLE_GLX_DRAWABLE_REFERENCE
);
368 * Try to destroy the old drawable, so long as the new one
371 if (ac
->drawable
&& !same_drawable
) {
372 ac
->drawable
->destroy(ac
->drawable
);
376 if (NULL
== newagd
) {
377 if (apple_glx_surface_create(dpy
, ac
->screen
, drawable
, &newagd
))
380 /* The drawable is referenced once by apple_glx_surface_create. */
383 * FIXME: We actually need 2 references to prevent premature surface
384 * destruction. The problem is that the surface gets destroyed in
385 * the case of the context being reused for another window, and
386 * we then lose the surface contents. Wait for destruction of a
387 * window to destroy a surface.
389 * Note: this may leave around surfaces we don't want around, if
390 * say we are using X for raster drawing after OpenGL rendering,
391 * but it will be compatible with the old libGL's behavior.
393 * Someday the X11 and OpenGL rendering must be unified at some
394 * layer. I suspect we can do that via shared memory and
395 * multiple threads in the X server (1 for each context created
396 * by a client). This would also allow users to render from
397 * multiple clients to the same OpenGL surface. In fact it could
401 newagd
->reference(newagd
);
403 /* Save the new drawable with the context structure. */
404 ac
->drawable
= newagd
;
407 /* We are reusing an existing drawable structure. */
410 assert(ac
->drawable
== newagd
);
411 /* The drawable_find above retained a reference for us. */
414 ac
->drawable
= newagd
;
419 * Avoid this costly path if this is the same drawable and the
420 * context is already current.
423 if (same_drawable
&& ac
->is_current
) {
424 apple_glx_diagnostic("same_drawable and ac->is_current\n");
428 cglerr
= apple_cgl
.set_current_context(ac
->context_obj
);
430 if (kCGLNoError
!= cglerr
) {
431 fprintf(stderr
, "set current error: %s\n",
432 apple_cgl
.error_string(cglerr
));
436 ac
->is_current
= true;
438 assert(NULL
!= ac
->context_obj
);
439 assert(NULL
!= ac
->drawable
);
441 ac
->thread_id
= pthread_self();
443 /* This will be set if the pending_destroy code indicates it should be: */
444 ac
->last_surface_window
= None
;
446 switch (ac
->drawable
->type
) {
447 case APPLE_GLX_DRAWABLE_PBUFFER
:
448 case APPLE_GLX_DRAWABLE_SURFACE
:
449 case APPLE_GLX_DRAWABLE_PIXMAP
:
450 if (ac
->drawable
->callbacks
.make_current
) {
451 if (ac
->drawable
->callbacks
.make_current(ac
, ac
->drawable
))
457 fprintf(stderr
, "internal error: invalid drawable type: %d\n",
466 apple_glx_is_current_drawable(Display
* dpy
, void *ptr
, GLXDrawable drawable
)
468 struct apple_glx_context
*ac
= ptr
;
470 if (ac
->drawable
&& ac
->drawable
->drawable
== drawable
) {
473 else if (NULL
== ac
->drawable
&& None
!= ac
->last_surface_window
) {
474 apple_glx_context_update(dpy
, ac
);
476 return (ac
->drawable
&& ac
->drawable
->drawable
== drawable
);
483 apple_glx_copy_context(void *currentptr
, void *srcptr
, void *destptr
,
484 unsigned long mask
, int *errorptr
, bool * x11errorptr
)
486 struct apple_glx_context
*src
, *dest
;
492 if (src
->screen
!= dest
->screen
) {
493 *errorptr
= BadMatch
;
498 if (dest
== currentptr
|| dest
->is_current
) {
499 *errorptr
= BadAccess
;
505 * If srcptr is the current context then we should do an implicit glFlush.
507 if (currentptr
== srcptr
)
510 err
= apple_cgl
.copy_context(src
->context_obj
, dest
->context_obj
,
513 if (kCGLNoError
!= err
) {
514 *errorptr
= GLXBadContext
;
515 *x11errorptr
= false;
523 * The value returned is the total number of contexts set to update.
524 * It's meant for debugging/introspection.
527 apple_glx_context_surface_changed(unsigned int uid
, pthread_t caller
)
529 struct apple_glx_context
*ac
;
534 for (ac
= context_list
; ac
; ac
= ac
->next
) {
535 if (ac
->drawable
&& APPLE_GLX_DRAWABLE_SURFACE
== ac
->drawable
->type
536 && ac
->drawable
->types
.surface
.uid
== uid
) {
538 if (caller
== ac
->thread_id
) {
539 apple_glx_diagnostic("caller is the same thread for uid %u\n",
542 xp_update_gl_context(ac
->context_obj
);
545 ac
->need_update
= true;
551 unlock_context_list();
557 apple_glx_context_update(Display
* dpy
, void *ptr
)
559 struct apple_glx_context
*ac
= ptr
;
561 if (NULL
== ac
->drawable
&& None
!= ac
->last_surface_window
) {
564 /* Attempt to recreate the surface for a destroyed drawable. */
566 apple_glx_make_current_context(dpy
, ac
, ac
, ac
->last_surface_window
);
568 apple_glx_diagnostic("%s: surface recreation failed? %s\n", __func__
,
569 failed
? "YES" : "NO");
572 if (ac
->need_update
) {
573 xp_update_gl_context(ac
->context_obj
);
574 ac
->need_update
= false;
576 apple_glx_diagnostic("%s: updating context %p\n", __func__
, ptr
);
579 if (ac
->drawable
&& APPLE_GLX_DRAWABLE_SURFACE
== ac
->drawable
->type
580 && ac
->drawable
->types
.surface
.pending_destroy
) {
581 apple_glx_diagnostic("%s: clearing drawable %p\n", __func__
, ptr
);
582 apple_cgl
.clear_drawable(ac
->context_obj
);
585 struct apple_glx_drawable
*d
;
587 apple_glx_diagnostic("%s: attempting to destroy drawable %p\n",
589 apple_glx_diagnostic("%s: ac->drawable->drawable is 0x%lx\n",
590 __func__
, ac
->drawable
->drawable
);
594 ac
->last_surface_window
= d
->drawable
;
599 * This will destroy the surface drawable if there are
600 * no references to it.
601 * It also subtracts 1 from the reference_count.
602 * If there are references to it, then it's probably made
603 * current in another context.
611 apple_glx_context_uses_stereo(void *ptr
)
613 struct apple_glx_context
*ac
= ptr
;
615 return ac
->uses_stereo
;