meson: Add tests to suites
[mesa.git] / src / glx / glxcmds.c
1 /*
2 * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
3 * Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice including the dates of first publication and
13 * either this permission notice or a reference to
14 * http://oss.sgi.com/projects/FreeB/
15 * shall be included 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 * SILICON GRAPHICS, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
22 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * Except as contained in this notice, the name of Silicon Graphics, Inc.
26 * shall not be used in advertising or otherwise to promote the sale, use or
27 * other dealings in this Software without prior written authorization from
28 * Silicon Graphics, Inc.
29 */
30
31 /**
32 * \file glxcmds.c
33 * Client-side GLX interface.
34 */
35
36 #include "glxclient.h"
37 #include "glapi.h"
38 #include "glxextensions.h"
39 #include "indirect.h"
40 #include "glx_error.h"
41
42 #ifdef GLX_DIRECT_RENDERING
43 #ifdef GLX_USE_APPLEGL
44 #include "apple/apple_glx_context.h"
45 #include "apple/apple_glx.h"
46 #include "util/debug.h"
47 #else
48 #include <sys/time.h>
49 #include <X11/extensions/xf86vmode.h>
50 #endif
51 #endif
52
53 #include <X11/Xlib-xcb.h>
54 #include <xcb/xcb.h>
55 #include <xcb/glx.h>
56 #include "GL/mesa_glinterop.h"
57
58 static const char __glXGLXClientVendorName[] = "Mesa Project and SGI";
59 static const char __glXGLXClientVersion[] = "1.4";
60
61 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
62
63 /**
64 * Get the __DRIdrawable for the drawable associated with a GLXContext
65 *
66 * \param dpy The display associated with \c drawable.
67 * \param drawable GLXDrawable whose __DRIdrawable part is to be retrieved.
68 * \param scrn_num If non-NULL, the drawables screen is stored there
69 * \returns A pointer to the context's __DRIdrawable on success, or NULL if
70 * the drawable is not associated with a direct-rendering context.
71 */
72 _X_HIDDEN __GLXDRIdrawable *
73 GetGLXDRIDrawable(Display * dpy, GLXDrawable drawable)
74 {
75 struct glx_display *priv = __glXInitialize(dpy);
76 __GLXDRIdrawable *pdraw;
77
78 if (priv == NULL)
79 return NULL;
80
81 if (__glxHashLookup(priv->drawHash, drawable, (void *) &pdraw) == 0)
82 return pdraw;
83
84 return NULL;
85 }
86
87 #endif
88
89 _X_HIDDEN struct glx_drawable *
90 GetGLXDrawable(Display *dpy, GLXDrawable drawable)
91 {
92 struct glx_display *priv = __glXInitialize(dpy);
93 struct glx_drawable *glxDraw;
94
95 if (priv == NULL)
96 return NULL;
97
98 if (__glxHashLookup(priv->glXDrawHash, drawable, (void *) &glxDraw) == 0)
99 return glxDraw;
100
101 return NULL;
102 }
103
104 _X_HIDDEN int
105 InitGLXDrawable(Display *dpy, struct glx_drawable *glxDraw, XID xDrawable,
106 GLXDrawable drawable)
107 {
108 struct glx_display *priv = __glXInitialize(dpy);
109
110 if (!priv)
111 return -1;
112
113 glxDraw->xDrawable = xDrawable;
114 glxDraw->drawable = drawable;
115 glxDraw->lastEventSbc = 0;
116 glxDraw->eventSbcWrap = 0;
117
118 return __glxHashInsert(priv->glXDrawHash, drawable, glxDraw);
119 }
120
121 _X_HIDDEN void
122 DestroyGLXDrawable(Display *dpy, GLXDrawable drawable)
123 {
124 struct glx_display *priv = __glXInitialize(dpy);
125 struct glx_drawable *glxDraw;
126
127 if (!priv)
128 return;
129
130 glxDraw = GetGLXDrawable(dpy, drawable);
131 __glxHashDelete(priv->glXDrawHash, drawable);
132 free(glxDraw);
133 }
134
135 /**
136 * Get the GLX per-screen data structure associated with a GLX context.
137 *
138 * \param dpy Display for which the GLX per-screen information is to be
139 * retrieved.
140 * \param scrn Screen on \c dpy for which the GLX per-screen information is
141 * to be retrieved.
142 * \returns A pointer to the GLX per-screen data if \c dpy and \c scrn
143 * specify a valid GLX screen, or NULL otherwise.
144 *
145 * \todo Should this function validate that \c scrn is within the screen
146 * number range for \c dpy?
147 */
148
149 _X_HIDDEN struct glx_screen *
150 GetGLXScreenConfigs(Display * dpy, int scrn)
151 {
152 struct glx_display *const priv = __glXInitialize(dpy);
153
154 return (priv
155 && priv->screens !=
156 NULL) ? priv->screens[scrn] : NULL;
157 }
158
159
160 static int
161 GetGLXPrivScreenConfig(Display * dpy, int scrn, struct glx_display ** ppriv,
162 struct glx_screen ** ppsc)
163 {
164 /* Initialize the extension, if needed . This has the added value
165 * of initializing/allocating the display private
166 */
167
168 if (dpy == NULL) {
169 return GLX_NO_EXTENSION;
170 }
171
172 *ppriv = __glXInitialize(dpy);
173 if (*ppriv == NULL) {
174 return GLX_NO_EXTENSION;
175 }
176
177 /* Check screen number to see if its valid */
178 if ((scrn < 0) || (scrn >= ScreenCount(dpy))) {
179 return GLX_BAD_SCREEN;
180 }
181
182 /* Check to see if the GL is supported on this screen */
183 *ppsc = (*ppriv)->screens[scrn];
184 if ((*ppsc)->configs == NULL && (*ppsc)->visuals == NULL) {
185 /* No support for GL on this screen regardless of visual */
186 return GLX_BAD_VISUAL;
187 }
188
189 return Success;
190 }
191
192
193 /**
194 * Determine if a \c GLXFBConfig supplied by the application is valid.
195 *
196 * \param dpy Application supplied \c Display pointer.
197 * \param config Application supplied \c GLXFBConfig.
198 *
199 * \returns If the \c GLXFBConfig is valid, the a pointer to the matching
200 * \c struct glx_config structure is returned. Otherwise, \c NULL
201 * is returned.
202 */
203 static struct glx_config *
204 ValidateGLXFBConfig(Display * dpy, GLXFBConfig fbconfig)
205 {
206 struct glx_display *const priv = __glXInitialize(dpy);
207 int num_screens = ScreenCount(dpy);
208 unsigned i;
209 struct glx_config *config;
210
211 if (priv != NULL) {
212 for (i = 0; i < num_screens; i++) {
213 for (config = priv->screens[i]->configs; config != NULL;
214 config = config->next) {
215 if (config == (struct glx_config *) fbconfig) {
216 return config;
217 }
218 }
219 }
220 }
221
222 return NULL;
223 }
224
225 /**
226 * Verifies context's GLX_RENDER_TYPE value with config.
227 *
228 * \param config GLX FBConfig which will support the returned renderType.
229 * \param renderType The context render type to be verified.
230 * \return True if the value of context renderType was approved, or 0 if no
231 * valid value was found.
232 */
233 Bool
234 validate_renderType_against_config(const struct glx_config *config,
235 int renderType)
236 {
237 /* GLX_EXT_no_config_context supports any render type */
238 if (!config)
239 return True;
240
241 switch (renderType) {
242 case GLX_RGBA_TYPE:
243 return (config->renderType & GLX_RGBA_BIT) != 0;
244 case GLX_COLOR_INDEX_TYPE:
245 return (config->renderType & GLX_COLOR_INDEX_BIT) != 0;
246 case GLX_RGBA_FLOAT_TYPE_ARB:
247 return (config->renderType & GLX_RGBA_FLOAT_BIT_ARB) != 0;
248 case GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT:
249 return (config->renderType & GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT) != 0;
250 default:
251 break;
252 }
253 return 0;
254 }
255
256 _X_HIDDEN Bool
257 glx_context_init(struct glx_context *gc,
258 struct glx_screen *psc, struct glx_config *config)
259 {
260 gc->majorOpcode = __glXSetupForCommand(psc->display->dpy);
261 if (!gc->majorOpcode)
262 return False;
263
264 gc->screen = psc->scr;
265 gc->psc = psc;
266 gc->config = config;
267 gc->isDirect = GL_TRUE;
268 gc->currentContextTag = -1;
269
270 return True;
271 }
272
273 /**
274 * Determine if a context uses direct rendering.
275 *
276 * \param dpy Display where the context was created.
277 * \param contextID ID of the context to be tested.
278 * \param error Out parameter, set to True on error if not NULL
279 *
280 * \returns \c True if the context is direct rendering or not.
281 */
282 static Bool
283 __glXIsDirect(Display * dpy, GLXContextID contextID, Bool *error)
284 {
285 CARD8 opcode;
286 xcb_connection_t *c;
287 xcb_generic_error_t *err;
288 xcb_glx_is_direct_reply_t *reply;
289 Bool is_direct;
290
291 opcode = __glXSetupForCommand(dpy);
292 if (!opcode) {
293 return False;
294 }
295
296 c = XGetXCBConnection(dpy);
297 reply = xcb_glx_is_direct_reply(c, xcb_glx_is_direct(c, contextID), &err);
298 is_direct = (reply != NULL && reply->is_direct) ? True : False;
299
300 if (err != NULL) {
301 if (error)
302 *error = True;
303 __glXSendErrorForXcb(dpy, err);
304 free(err);
305 }
306
307 free(reply);
308
309 return is_direct;
310 }
311
312 /**
313 * Create a new context.
314 *
315 * \param renderType For FBConfigs, what is the rendering type?
316 */
317
318 static GLXContext
319 CreateContext(Display *dpy, int generic_id, struct glx_config *config,
320 GLXContext shareList_user, Bool allowDirect,
321 unsigned code, int renderType, int screen)
322 {
323 struct glx_context *gc;
324 struct glx_screen *psc;
325 struct glx_context *shareList = (struct glx_context *) shareList_user;
326 if (dpy == NULL)
327 return NULL;
328
329 psc = GetGLXScreenConfigs(dpy, screen);
330 if (psc == NULL)
331 return NULL;
332
333 if (generic_id == None)
334 return NULL;
335
336 gc = NULL;
337 #ifdef GLX_USE_APPLEGL
338 gc = applegl_create_context(psc, config, shareList, renderType);
339 #else
340 if (allowDirect && psc->vtable->create_context)
341 gc = psc->vtable->create_context(psc, config, shareList, renderType);
342 if (!gc)
343 gc = indirect_create_context(psc, config, shareList, renderType);
344 #endif
345 if (!gc)
346 return NULL;
347
348 LockDisplay(dpy);
349 switch (code) {
350 case X_GLXCreateContext: {
351 xGLXCreateContextReq *req;
352
353 /* Send the glXCreateContext request */
354 GetReq(GLXCreateContext, req);
355 req->reqType = gc->majorOpcode;
356 req->glxCode = X_GLXCreateContext;
357 req->context = gc->xid = XAllocID(dpy);
358 req->visual = generic_id;
359 req->screen = screen;
360 req->shareList = shareList ? shareList->xid : None;
361 req->isDirect = gc->isDirect;
362 break;
363 }
364
365 case X_GLXCreateNewContext: {
366 xGLXCreateNewContextReq *req;
367
368 /* Send the glXCreateNewContext request */
369 GetReq(GLXCreateNewContext, req);
370 req->reqType = gc->majorOpcode;
371 req->glxCode = X_GLXCreateNewContext;
372 req->context = gc->xid = XAllocID(dpy);
373 req->fbconfig = generic_id;
374 req->screen = screen;
375 req->renderType = renderType;
376 req->shareList = shareList ? shareList->xid : None;
377 req->isDirect = gc->isDirect;
378 break;
379 }
380
381 case X_GLXvop_CreateContextWithConfigSGIX: {
382 xGLXVendorPrivateWithReplyReq *vpreq;
383 xGLXCreateContextWithConfigSGIXReq *req;
384
385 /* Send the glXCreateNewContext request */
386 GetReqExtra(GLXVendorPrivateWithReply,
387 sz_xGLXCreateContextWithConfigSGIXReq -
388 sz_xGLXVendorPrivateWithReplyReq, vpreq);
389 req = (xGLXCreateContextWithConfigSGIXReq *) vpreq;
390 req->reqType = gc->majorOpcode;
391 req->glxCode = X_GLXVendorPrivateWithReply;
392 req->vendorCode = X_GLXvop_CreateContextWithConfigSGIX;
393 req->context = gc->xid = XAllocID(dpy);
394 req->fbconfig = generic_id;
395 req->screen = screen;
396 req->renderType = renderType;
397 req->shareList = shareList ? shareList->xid : None;
398 req->isDirect = gc->isDirect;
399 break;
400 }
401
402 default:
403 /* What to do here? This case is the sign of an internal error. It
404 * should never be reachable.
405 */
406 break;
407 }
408
409 UnlockDisplay(dpy);
410 SyncHandle();
411
412 gc->share_xid = shareList ? shareList->xid : None;
413 gc->imported = GL_FALSE;
414
415 /* Unlike most X resource creation requests, we're about to return a handle
416 * with client-side state, not just an XID. To simplify error handling
417 * elsewhere in libGL, force a round-trip here to ensure the CreateContext
418 * request above succeeded.
419 */
420 {
421 Bool error = False;
422 int isDirect = __glXIsDirect(dpy, gc->xid, &error);
423
424 if (error != False || isDirect != gc->isDirect) {
425 gc->vtable->destroy(gc);
426 gc = NULL;
427 }
428 }
429
430 return (GLXContext) gc;
431 }
432
433 _GLX_PUBLIC GLXContext
434 glXCreateContext(Display * dpy, XVisualInfo * vis,
435 GLXContext shareList, Bool allowDirect)
436 {
437 struct glx_config *config = NULL;
438 int renderType = GLX_RGBA_TYPE;
439
440 #if defined(GLX_DIRECT_RENDERING) || defined(GLX_USE_APPLEGL)
441 struct glx_screen *const psc = GetGLXScreenConfigs(dpy, vis->screen);
442
443 if (psc)
444 config = glx_config_find_visual(psc->visuals, vis->visualid);
445
446 if (config == NULL) {
447 __glXSendError(dpy, BadValue, vis->visualid, X_GLXCreateContext, True);
448 return None;
449 }
450
451 /* Choose the context render type based on DRI config values. It is
452 * unusual to set this type from config, but we have no other choice, as
453 * this old API does not provide renderType parameter.
454 */
455 if (config->renderType & GLX_RGBA_FLOAT_BIT_ARB) {
456 renderType = GLX_RGBA_FLOAT_TYPE_ARB;
457 } else if (config->renderType & GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT) {
458 renderType = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT;
459 } else if (config->renderType & GLX_RGBA_BIT) {
460 renderType = GLX_RGBA_TYPE;
461 } else if (config->renderType & GLX_COLOR_INDEX_BIT) {
462 renderType = GLX_COLOR_INDEX_TYPE;
463 } else if (config->rgbMode) {
464 /* If we're here, then renderType is not set correctly. Let's use a
465 * safeguard - any TrueColor or DirectColor mode is RGB mode. Such
466 * default value is needed by old DRI drivers, which didn't set
467 * renderType correctly as the value was just ignored.
468 */
469 renderType = GLX_RGBA_TYPE;
470 } else {
471 /* Safeguard - only one option left, all non-RGB modes are indexed
472 * modes. Again, this allows drivers with invalid renderType to work
473 * properly.
474 */
475 renderType = GLX_COLOR_INDEX_TYPE;
476 }
477 #endif
478
479 return CreateContext(dpy, vis->visualid, config, shareList, allowDirect,
480 X_GLXCreateContext, renderType, vis->screen);
481 }
482
483 static void
484 glx_send_destroy_context(Display *dpy, XID xid)
485 {
486 CARD8 opcode = __glXSetupForCommand(dpy);
487 xGLXDestroyContextReq *req;
488
489 LockDisplay(dpy);
490 GetReq(GLXDestroyContext, req);
491 req->reqType = opcode;
492 req->glxCode = X_GLXDestroyContext;
493 req->context = xid;
494 UnlockDisplay(dpy);
495 SyncHandle();
496 }
497
498 /*
499 ** Destroy the named context
500 */
501
502 _GLX_PUBLIC void
503 glXDestroyContext(Display * dpy, GLXContext ctx)
504 {
505 struct glx_context *gc = (struct glx_context *) ctx;
506
507 if (gc == NULL || gc->xid == None)
508 return;
509
510 __glXLock();
511 if (!gc->imported)
512 glx_send_destroy_context(dpy, gc->xid);
513
514 if (gc->currentDpy) {
515 /* This context is bound to some thread. According to the man page,
516 * we should not actually delete the context until it's unbound.
517 * Note that we set gc->xid = None above. In MakeContextCurrent()
518 * we check for that and delete the context there.
519 */
520 gc->xid = None;
521 } else {
522 gc->vtable->destroy(gc);
523 }
524 __glXUnlock();
525 }
526
527 /*
528 ** Return the major and minor version #s for the GLX extension
529 */
530 _GLX_PUBLIC Bool
531 glXQueryVersion(Display * dpy, int *major, int *minor)
532 {
533 struct glx_display *priv;
534
535 /* Init the extension. This fetches the major and minor version. */
536 priv = __glXInitialize(dpy);
537 if (!priv)
538 return False;
539
540 if (major)
541 *major = priv->majorVersion;
542 if (minor)
543 *minor = priv->minorVersion;
544 return True;
545 }
546
547 /*
548 ** Query the existence of the GLX extension
549 */
550 _GLX_PUBLIC Bool
551 glXQueryExtension(Display * dpy, int *errorBase, int *eventBase)
552 {
553 int major_op, erb, evb;
554 Bool rv;
555
556 rv = XQueryExtension(dpy, GLX_EXTENSION_NAME, &major_op, &evb, &erb);
557 if (rv) {
558 if (errorBase)
559 *errorBase = erb;
560 if (eventBase)
561 *eventBase = evb;
562 }
563 return rv;
564 }
565
566 /*
567 ** Put a barrier in the token stream that forces the GL to finish its
568 ** work before X can proceed.
569 */
570 _GLX_PUBLIC void
571 glXWaitGL(void)
572 {
573 struct glx_context *gc = __glXGetCurrentContext();
574
575 if (gc->vtable->wait_gl)
576 gc->vtable->wait_gl(gc);
577 }
578
579 /*
580 ** Put a barrier in the token stream that forces X to finish its
581 ** work before GL can proceed.
582 */
583 _GLX_PUBLIC void
584 glXWaitX(void)
585 {
586 struct glx_context *gc = __glXGetCurrentContext();
587
588 if (gc->vtable->wait_x)
589 gc->vtable->wait_x(gc);
590 }
591
592 _GLX_PUBLIC void
593 glXUseXFont(Font font, int first, int count, int listBase)
594 {
595 struct glx_context *gc = __glXGetCurrentContext();
596
597 if (gc->vtable->use_x_font)
598 gc->vtable->use_x_font(gc, font, first, count, listBase);
599 }
600
601 /************************************************************************/
602
603 /*
604 ** Copy the source context to the destination context using the
605 ** attribute "mask".
606 */
607 _GLX_PUBLIC void
608 glXCopyContext(Display * dpy, GLXContext source_user,
609 GLXContext dest_user, unsigned long mask)
610 {
611 struct glx_context *source = (struct glx_context *) source_user;
612 struct glx_context *dest = (struct glx_context *) dest_user;
613 #ifdef GLX_USE_APPLEGL
614 struct glx_context *gc = __glXGetCurrentContext();
615 int errorcode;
616 bool x11error;
617
618 if(apple_glx_copy_context(gc->driContext, source->driContext, dest->driContext,
619 mask, &errorcode, &x11error)) {
620 __glXSendError(dpy, errorcode, 0, X_GLXCopyContext, x11error);
621 }
622
623 #else
624 xGLXCopyContextReq *req;
625 struct glx_context *gc = __glXGetCurrentContext();
626 GLXContextTag tag;
627 CARD8 opcode;
628
629 opcode = __glXSetupForCommand(dpy);
630 if (!opcode) {
631 return;
632 }
633
634 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
635 if (gc->isDirect) {
636 /* NOT_DONE: This does not work yet */
637 }
638 #endif
639
640 /*
641 ** If the source is the current context, send its tag so that the context
642 ** can be flushed before the copy.
643 */
644 if (source == gc && dpy == gc->currentDpy) {
645 tag = gc->currentContextTag;
646 }
647 else {
648 tag = 0;
649 }
650
651 /* Send the glXCopyContext request */
652 LockDisplay(dpy);
653 GetReq(GLXCopyContext, req);
654 req->reqType = opcode;
655 req->glxCode = X_GLXCopyContext;
656 req->source = source ? source->xid : None;
657 req->dest = dest ? dest->xid : None;
658 req->mask = mask;
659 req->contextTag = tag;
660 UnlockDisplay(dpy);
661 SyncHandle();
662 #endif /* GLX_USE_APPLEGL */
663 }
664
665
666 /**
667 * \todo
668 * Shouldn't this function \b always return \c False when
669 * \c GLX_DIRECT_RENDERING is not defined? Do we really need to bother with
670 * the GLX protocol here at all?
671 */
672 _GLX_PUBLIC Bool
673 glXIsDirect(Display * dpy, GLXContext gc_user)
674 {
675 struct glx_context *gc = (struct glx_context *) gc_user;
676
677 if (!gc) {
678 return False;
679 }
680 else if (gc->isDirect) {
681 return True;
682 }
683 #ifdef GLX_USE_APPLEGL /* TODO: indirect on darwin */
684 return False;
685 #else
686 return __glXIsDirect(dpy, gc->xid, NULL);
687 #endif
688 }
689
690 _GLX_PUBLIC GLXPixmap
691 glXCreateGLXPixmap(Display * dpy, XVisualInfo * vis, Pixmap pixmap)
692 {
693 #ifdef GLX_USE_APPLEGL
694 int screen = vis->screen;
695 struct glx_screen *const psc = GetGLXScreenConfigs(dpy, screen);
696 const struct glx_config *config;
697
698 config = glx_config_find_visual(psc->visuals, vis->visualid);
699
700 if(apple_glx_pixmap_create(dpy, vis->screen, pixmap, config))
701 return None;
702
703 return pixmap;
704 #else
705 xGLXCreateGLXPixmapReq *req;
706 struct glx_drawable *glxDraw;
707 GLXPixmap xid;
708 CARD8 opcode;
709
710 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
711 struct glx_display *const priv = __glXInitialize(dpy);
712
713 if (priv == NULL)
714 return None;
715 #endif
716
717 opcode = __glXSetupForCommand(dpy);
718 if (!opcode) {
719 return None;
720 }
721
722 glxDraw = malloc(sizeof(*glxDraw));
723 if (!glxDraw)
724 return None;
725
726 /* Send the glXCreateGLXPixmap request */
727 LockDisplay(dpy);
728 GetReq(GLXCreateGLXPixmap, req);
729 req->reqType = opcode;
730 req->glxCode = X_GLXCreateGLXPixmap;
731 req->screen = vis->screen;
732 req->visual = vis->visualid;
733 req->pixmap = pixmap;
734 req->glxpixmap = xid = XAllocID(dpy);
735 UnlockDisplay(dpy);
736 SyncHandle();
737
738 if (InitGLXDrawable(dpy, glxDraw, pixmap, req->glxpixmap)) {
739 free(glxDraw);
740 return None;
741 }
742
743 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
744 do {
745 /* FIXME: Maybe delay __DRIdrawable creation until the drawable
746 * is actually bound to a context... */
747
748 __GLXDRIdrawable *pdraw;
749 struct glx_screen *psc;
750 struct glx_config *config;
751
752 psc = priv->screens[vis->screen];
753 if (psc->driScreen == NULL)
754 return xid;
755
756 config = glx_config_find_visual(psc->visuals, vis->visualid);
757 pdraw = psc->driScreen->createDrawable(psc, pixmap, xid, config);
758 if (pdraw == NULL) {
759 fprintf(stderr, "failed to create pixmap\n");
760 xid = None;
761 break;
762 }
763
764 if (__glxHashInsert(priv->drawHash, xid, pdraw)) {
765 (*pdraw->destroyDrawable) (pdraw);
766 xid = None;
767 break;
768 }
769 } while (0);
770
771 if (xid == None) {
772 xGLXDestroyGLXPixmapReq *dreq;
773 LockDisplay(dpy);
774 GetReq(GLXDestroyGLXPixmap, dreq);
775 dreq->reqType = opcode;
776 dreq->glxCode = X_GLXDestroyGLXPixmap;
777 dreq->glxpixmap = xid;
778 UnlockDisplay(dpy);
779 SyncHandle();
780 }
781 #endif
782
783 return xid;
784 #endif
785 }
786
787 /*
788 ** Destroy the named pixmap
789 */
790 _GLX_PUBLIC void
791 glXDestroyGLXPixmap(Display * dpy, GLXPixmap glxpixmap)
792 {
793 #ifdef GLX_USE_APPLEGL
794 if(apple_glx_pixmap_destroy(dpy, glxpixmap))
795 __glXSendError(dpy, GLXBadPixmap, glxpixmap, X_GLXDestroyPixmap, false);
796 #else
797 xGLXDestroyGLXPixmapReq *req;
798 CARD8 opcode;
799
800 opcode = __glXSetupForCommand(dpy);
801 if (!opcode) {
802 return;
803 }
804
805 /* Send the glXDestroyGLXPixmap request */
806 LockDisplay(dpy);
807 GetReq(GLXDestroyGLXPixmap, req);
808 req->reqType = opcode;
809 req->glxCode = X_GLXDestroyGLXPixmap;
810 req->glxpixmap = glxpixmap;
811 UnlockDisplay(dpy);
812 SyncHandle();
813
814 DestroyGLXDrawable(dpy, glxpixmap);
815
816 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
817 {
818 struct glx_display *const priv = __glXInitialize(dpy);
819 __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, glxpixmap);
820
821 if (priv != NULL && pdraw != NULL) {
822 (*pdraw->destroyDrawable) (pdraw);
823 __glxHashDelete(priv->drawHash, glxpixmap);
824 }
825 }
826 #endif
827 #endif /* GLX_USE_APPLEGL */
828 }
829
830 _GLX_PUBLIC void
831 glXSwapBuffers(Display * dpy, GLXDrawable drawable)
832 {
833 #ifdef GLX_USE_APPLEGL
834 struct glx_context * gc = __glXGetCurrentContext();
835 if(gc != &dummyContext && apple_glx_is_current_drawable(dpy, gc->driContext, drawable)) {
836 apple_glx_swap_buffers(gc->driContext);
837 } else {
838 __glXSendError(dpy, GLXBadCurrentWindow, 0, X_GLXSwapBuffers, false);
839 }
840 #else
841 struct glx_context *gc;
842 GLXContextTag tag;
843 CARD8 opcode;
844 xcb_connection_t *c;
845
846 gc = __glXGetCurrentContext();
847
848 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
849 {
850 __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable);
851
852 if (pdraw != NULL) {
853 Bool flush = gc != &dummyContext && drawable == gc->currentDrawable;
854
855 (*pdraw->psc->driScreen->swapBuffers)(pdraw, 0, 0, 0, flush);
856 return;
857 }
858 }
859 #endif
860
861 opcode = __glXSetupForCommand(dpy);
862 if (!opcode) {
863 return;
864 }
865
866 /*
867 ** The calling thread may or may not have a current context. If it
868 ** does, send the context tag so the server can do a flush.
869 */
870 if ((gc != &dummyContext) && (dpy == gc->currentDpy) &&
871 ((drawable == gc->currentDrawable)
872 || (drawable == gc->currentReadable))) {
873 tag = gc->currentContextTag;
874 }
875 else {
876 tag = 0;
877 }
878
879 c = XGetXCBConnection(dpy);
880 xcb_glx_swap_buffers(c, tag, drawable);
881 xcb_flush(c);
882 #endif /* GLX_USE_APPLEGL */
883 }
884
885
886 /*
887 ** Return configuration information for the given display, screen and
888 ** visual combination.
889 */
890 _GLX_PUBLIC int
891 glXGetConfig(Display * dpy, XVisualInfo * vis, int attribute,
892 int *value_return)
893 {
894 struct glx_display *priv;
895 struct glx_screen *psc;
896 struct glx_config *config;
897 int status;
898
899 status = GetGLXPrivScreenConfig(dpy, vis->screen, &priv, &psc);
900 if (status == Success) {
901 config = glx_config_find_visual(psc->visuals, vis->visualid);
902
903 /* Lookup attribute after first finding a match on the visual */
904 if (config != NULL) {
905 return glx_config_get(config, attribute, value_return);
906 }
907
908 status = GLX_BAD_VISUAL;
909 }
910
911 /*
912 ** If we can't find the config for this visual, this visual is not
913 ** supported by the OpenGL implementation on the server.
914 */
915 if ((status == GLX_BAD_VISUAL) && (attribute == GLX_USE_GL)) {
916 *value_return = False;
917 status = Success;
918 }
919
920 return status;
921 }
922
923 /************************************************************************/
924
925 static void
926 init_fbconfig_for_chooser(struct glx_config * config,
927 GLboolean fbconfig_style_tags)
928 {
929 memset(config, 0, sizeof(struct glx_config));
930 config->visualID = (XID) GLX_DONT_CARE;
931 config->visualType = GLX_DONT_CARE;
932
933 /* glXChooseFBConfig specifies different defaults for these properties than
934 * glXChooseVisual.
935 */
936 if (fbconfig_style_tags) {
937 config->rgbMode = GL_TRUE;
938 config->doubleBufferMode = GLX_DONT_CARE;
939 config->renderType = GLX_RGBA_BIT;
940 }
941
942 config->drawableType = GLX_WINDOW_BIT;
943 config->visualRating = GLX_DONT_CARE;
944 config->transparentPixel = GLX_NONE;
945 config->transparentRed = GLX_DONT_CARE;
946 config->transparentGreen = GLX_DONT_CARE;
947 config->transparentBlue = GLX_DONT_CARE;
948 config->transparentAlpha = GLX_DONT_CARE;
949 config->transparentIndex = GLX_DONT_CARE;
950
951 config->xRenderable = GLX_DONT_CARE;
952 config->fbconfigID = (GLXFBConfigID) (GLX_DONT_CARE);
953
954 config->swapMethod = GLX_DONT_CARE;
955 config->sRGBCapable = GLX_DONT_CARE;
956 }
957
958 #define MATCH_DONT_CARE( param ) \
959 do { \
960 if ( ((int) a-> param != (int) GLX_DONT_CARE) \
961 && (a-> param != b-> param) ) { \
962 return False; \
963 } \
964 } while ( 0 )
965
966 #define MATCH_MINIMUM( param ) \
967 do { \
968 if ( ((int) a-> param != (int) GLX_DONT_CARE) \
969 && (a-> param > b-> param) ) { \
970 return False; \
971 } \
972 } while ( 0 )
973
974 #define MATCH_EXACT( param ) \
975 do { \
976 if ( a-> param != b-> param) { \
977 return False; \
978 } \
979 } while ( 0 )
980
981 /* Test that all bits from a are contained in b */
982 #define MATCH_MASK(param) \
983 do { \
984 if ( ((int) a-> param != (int) GLX_DONT_CARE) \
985 && ((a->param & ~b->param) != 0) ) { \
986 return False; \
987 } \
988 } while (0);
989
990 /**
991 * Determine if two GLXFBConfigs are compatible.
992 *
993 * \param a Application specified config to test.
994 * \param b Server specified config to test against \c a.
995 */
996 static Bool
997 fbconfigs_compatible(const struct glx_config * const a,
998 const struct glx_config * const b)
999 {
1000 MATCH_DONT_CARE(doubleBufferMode);
1001 MATCH_DONT_CARE(visualType);
1002 MATCH_DONT_CARE(visualRating);
1003 MATCH_DONT_CARE(xRenderable);
1004 MATCH_DONT_CARE(fbconfigID);
1005 MATCH_DONT_CARE(swapMethod);
1006
1007 MATCH_MINIMUM(rgbBits);
1008 MATCH_MINIMUM(numAuxBuffers);
1009 MATCH_MINIMUM(redBits);
1010 MATCH_MINIMUM(greenBits);
1011 MATCH_MINIMUM(blueBits);
1012 MATCH_MINIMUM(alphaBits);
1013 MATCH_MINIMUM(depthBits);
1014 MATCH_MINIMUM(stencilBits);
1015 MATCH_MINIMUM(accumRedBits);
1016 MATCH_MINIMUM(accumGreenBits);
1017 MATCH_MINIMUM(accumBlueBits);
1018 MATCH_MINIMUM(accumAlphaBits);
1019 MATCH_MINIMUM(sampleBuffers);
1020 MATCH_MINIMUM(maxPbufferWidth);
1021 MATCH_MINIMUM(maxPbufferHeight);
1022 MATCH_MINIMUM(maxPbufferPixels);
1023 MATCH_MINIMUM(samples);
1024
1025 MATCH_DONT_CARE(stereoMode);
1026 MATCH_EXACT(level);
1027
1028 MATCH_MASK(drawableType);
1029 MATCH_MASK(renderType);
1030 MATCH_DONT_CARE(sRGBCapable);
1031
1032 /* There is a bug in a few of the XFree86 DDX drivers. They contain
1033 * visuals with a "transparent type" of 0 when they really mean GLX_NONE.
1034 * Technically speaking, it is a bug in the DDX driver, but there is
1035 * enough of an installed base to work around the problem here. In any
1036 * case, 0 is not a valid value of the transparent type, so we'll treat 0
1037 * from the app as GLX_DONT_CARE. We'll consider GLX_NONE from the app and
1038 * 0 from the server to be a match to maintain backward compatibility with
1039 * the (broken) drivers.
1040 */
1041
1042 if (a->transparentPixel != (int) GLX_DONT_CARE && a->transparentPixel != 0) {
1043 if (a->transparentPixel == GLX_NONE) {
1044 if (b->transparentPixel != GLX_NONE && b->transparentPixel != 0)
1045 return False;
1046 }
1047 else {
1048 MATCH_EXACT(transparentPixel);
1049 }
1050
1051 switch (a->transparentPixel) {
1052 case GLX_TRANSPARENT_RGB:
1053 MATCH_DONT_CARE(transparentRed);
1054 MATCH_DONT_CARE(transparentGreen);
1055 MATCH_DONT_CARE(transparentBlue);
1056 MATCH_DONT_CARE(transparentAlpha);
1057 break;
1058
1059 case GLX_TRANSPARENT_INDEX:
1060 MATCH_DONT_CARE(transparentIndex);
1061 break;
1062
1063 default:
1064 break;
1065 }
1066 }
1067
1068 return True;
1069 }
1070
1071
1072 /* There's some trickly language in the GLX spec about how this is supposed
1073 * to work. Basically, if a given component size is either not specified
1074 * or the requested size is zero, it is supposed to act like PERFER_SMALLER.
1075 * Well, that's really hard to do with the code as-is. This behavior is
1076 * closer to correct, but still not technically right.
1077 */
1078 #define PREFER_LARGER_OR_ZERO(comp) \
1079 do { \
1080 if ( ((*a)-> comp) != ((*b)-> comp) ) { \
1081 if ( ((*a)-> comp) == 0 ) { \
1082 return -1; \
1083 } \
1084 else if ( ((*b)-> comp) == 0 ) { \
1085 return 1; \
1086 } \
1087 else { \
1088 return ((*b)-> comp) - ((*a)-> comp) ; \
1089 } \
1090 } \
1091 } while( 0 )
1092
1093 #define PREFER_LARGER(comp) \
1094 do { \
1095 if ( ((*a)-> comp) != ((*b)-> comp) ) { \
1096 return ((*b)-> comp) - ((*a)-> comp) ; \
1097 } \
1098 } while( 0 )
1099
1100 #define PREFER_SMALLER(comp) \
1101 do { \
1102 if ( ((*a)-> comp) != ((*b)-> comp) ) { \
1103 return ((*a)-> comp) - ((*b)-> comp) ; \
1104 } \
1105 } while( 0 )
1106
1107 /**
1108 * Compare two GLXFBConfigs. This function is intended to be used as the
1109 * compare function passed in to qsort.
1110 *
1111 * \returns If \c a is a "better" config, according to the specification of
1112 * SGIX_fbconfig, a number less than zero is returned. If \c b is
1113 * better, then a number greater than zero is return. If both are
1114 * equal, zero is returned.
1115 * \sa qsort, glXChooseVisual, glXChooseFBConfig, glXChooseFBConfigSGIX
1116 */
1117 static int
1118 fbconfig_compare(struct glx_config **a, struct glx_config **b)
1119 {
1120 /* The order of these comparisons must NOT change. It is defined by
1121 * the GLX 1.4 specification.
1122 */
1123
1124 PREFER_SMALLER(visualSelectGroup);
1125
1126 /* The sort order for the visualRating is GLX_NONE, GLX_SLOW, and
1127 * GLX_NON_CONFORMANT_CONFIG. It just so happens that this is the
1128 * numerical sort order of the enums (0x8000, 0x8001, and 0x800D).
1129 */
1130 PREFER_SMALLER(visualRating);
1131
1132 /* This isn't quite right. It is supposed to compare the sum of the
1133 * components the user specifically set minimums for.
1134 */
1135 PREFER_LARGER_OR_ZERO(redBits);
1136 PREFER_LARGER_OR_ZERO(greenBits);
1137 PREFER_LARGER_OR_ZERO(blueBits);
1138 PREFER_LARGER_OR_ZERO(alphaBits);
1139
1140 PREFER_SMALLER(rgbBits);
1141
1142 if (((*a)->doubleBufferMode != (*b)->doubleBufferMode)) {
1143 /* Prefer single-buffer.
1144 */
1145 return (!(*a)->doubleBufferMode) ? -1 : 1;
1146 }
1147
1148 PREFER_SMALLER(numAuxBuffers);
1149
1150 PREFER_SMALLER(sampleBuffers);
1151 PREFER_SMALLER(samples);
1152
1153 PREFER_LARGER_OR_ZERO(depthBits);
1154 PREFER_SMALLER(stencilBits);
1155
1156 /* This isn't quite right. It is supposed to compare the sum of the
1157 * components the user specifically set minimums for.
1158 */
1159 PREFER_LARGER_OR_ZERO(accumRedBits);
1160 PREFER_LARGER_OR_ZERO(accumGreenBits);
1161 PREFER_LARGER_OR_ZERO(accumBlueBits);
1162 PREFER_LARGER_OR_ZERO(accumAlphaBits);
1163
1164 PREFER_SMALLER(visualType);
1165
1166 /* None of the pbuffer or fbconfig specs say that this comparison needs
1167 * to happen at all, but it seems like it should.
1168 */
1169 PREFER_LARGER(maxPbufferWidth);
1170 PREFER_LARGER(maxPbufferHeight);
1171 PREFER_LARGER(maxPbufferPixels);
1172
1173 return 0;
1174 }
1175
1176
1177 /**
1178 * Selects and sorts a subset of the supplied configs based on the attributes.
1179 * This function forms to basis of \c glXChooseFBConfig and
1180 * \c glXChooseFBConfigSGIX.
1181 *
1182 * \param configs Array of pointers to possible configs. The elements of
1183 * this array that do not meet the criteria will be set to
1184 * NULL. The remaining elements will be sorted according to
1185 * the various visual / FBConfig selection rules.
1186 * \param num_configs Number of elements in the \c configs array.
1187 * \param attribList Attributes used select from \c configs. This array is
1188 * terminated by a \c None tag. The array is of the form
1189 * expected by \c glXChooseFBConfig (where every tag has a
1190 * value).
1191 * \returns The number of valid elements left in \c configs.
1192 *
1193 * \sa glXChooseFBConfig, glXChooseFBConfigSGIX
1194 */
1195 static int
1196 choose_fbconfig(struct glx_config ** configs, int num_configs,
1197 const int *attribList)
1198 {
1199 struct glx_config test_config;
1200 int base;
1201 int i;
1202
1203 /* This is a fairly direct implementation of the selection method
1204 * described by GLX_SGIX_fbconfig. Start by culling out all the
1205 * configs that are not compatible with the selected parameter
1206 * list.
1207 */
1208
1209 init_fbconfig_for_chooser(&test_config, GL_TRUE);
1210 __glXInitializeVisualConfigFromTags(&test_config, 512,
1211 (const INT32 *) attribList,
1212 GL_TRUE, GL_TRUE);
1213
1214 base = 0;
1215 for (i = 0; i < num_configs; i++) {
1216 if (fbconfigs_compatible(&test_config, configs[i])) {
1217 configs[base] = configs[i];
1218 base++;
1219 }
1220 }
1221
1222 if (base == 0) {
1223 return 0;
1224 }
1225
1226 if (base < num_configs) {
1227 (void) memset(&configs[base], 0, sizeof(void *) * (num_configs - base));
1228 }
1229
1230 /* After the incompatible configs are removed, the resulting
1231 * list is sorted according to the rules set out in the various
1232 * specifications.
1233 */
1234
1235 qsort(configs, base, sizeof(struct glx_config *),
1236 (int (*)(const void *, const void *)) fbconfig_compare);
1237 return base;
1238 }
1239
1240
1241
1242
1243 /*
1244 ** Return the visual that best matches the template. Return None if no
1245 ** visual matches the template.
1246 */
1247 _GLX_PUBLIC XVisualInfo *
1248 glXChooseVisual(Display * dpy, int screen, int *attribList)
1249 {
1250 XVisualInfo *visualList = NULL;
1251 struct glx_display *priv;
1252 struct glx_screen *psc;
1253 struct glx_config test_config;
1254 struct glx_config *config;
1255 struct glx_config *best_config = NULL;
1256
1257 /*
1258 ** Get a list of all visuals, return if list is empty
1259 */
1260 if (GetGLXPrivScreenConfig(dpy, screen, &priv, &psc) != Success) {
1261 return None;
1262 }
1263
1264
1265 /*
1266 ** Build a template from the defaults and the attribute list
1267 ** Free visual list and return if an unexpected token is encountered
1268 */
1269 init_fbconfig_for_chooser(&test_config, GL_FALSE);
1270 __glXInitializeVisualConfigFromTags(&test_config, 512,
1271 (const INT32 *) attribList,
1272 GL_TRUE, GL_FALSE);
1273
1274 /*
1275 ** Eliminate visuals that don't meet minimum requirements
1276 ** Compute a score for those that do
1277 ** Remember which visual, if any, got the highest score
1278 ** If no visual is acceptable, return None
1279 ** Otherwise, create an XVisualInfo list with just the selected X visual
1280 ** and return this.
1281 */
1282 for (config = psc->visuals; config != NULL; config = config->next) {
1283 if (fbconfigs_compatible(&test_config, config)
1284 && ((best_config == NULL) ||
1285 (fbconfig_compare (&config, &best_config) < 0))) {
1286 XVisualInfo visualTemplate;
1287 XVisualInfo *newList;
1288 int i;
1289
1290 visualTemplate.screen = screen;
1291 visualTemplate.visualid = config->visualID;
1292 newList = XGetVisualInfo(dpy, VisualScreenMask | VisualIDMask,
1293 &visualTemplate, &i);
1294
1295 if (newList) {
1296 free(visualList);
1297 visualList = newList;
1298 best_config = config;
1299 }
1300 }
1301 }
1302
1303 #ifdef GLX_USE_APPLEGL
1304 if(visualList && env_var_as_boolean("LIBGL_DUMP_VISUALID", false)) {
1305 printf("visualid 0x%lx\n", visualList[0].visualid);
1306 }
1307 #endif
1308
1309 return visualList;
1310 }
1311
1312
1313 _GLX_PUBLIC const char *
1314 glXQueryExtensionsString(Display * dpy, int screen)
1315 {
1316 struct glx_screen *psc;
1317 struct glx_display *priv;
1318
1319 if (GetGLXPrivScreenConfig(dpy, screen, &priv, &psc) != Success) {
1320 return NULL;
1321 }
1322
1323 if (!psc->effectiveGLXexts) {
1324 if (!psc->serverGLXexts) {
1325 psc->serverGLXexts =
1326 __glXQueryServerString(dpy, priv->majorOpcode, screen,
1327 GLX_EXTENSIONS);
1328 }
1329
1330 __glXCalculateUsableExtensions(psc,
1331 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
1332 (psc->driScreen != NULL),
1333 #else
1334 GL_FALSE,
1335 #endif
1336 priv->minorVersion);
1337 }
1338
1339 return psc->effectiveGLXexts;
1340 }
1341
1342 _GLX_PUBLIC const char *
1343 glXGetClientString(Display * dpy, int name)
1344 {
1345 (void) dpy;
1346
1347 switch (name) {
1348 case GLX_VENDOR:
1349 return (__glXGLXClientVendorName);
1350 case GLX_VERSION:
1351 return (__glXGLXClientVersion);
1352 case GLX_EXTENSIONS:
1353 return (__glXGetClientExtensions());
1354 default:
1355 return NULL;
1356 }
1357 }
1358
1359 _GLX_PUBLIC const char *
1360 glXQueryServerString(Display * dpy, int screen, int name)
1361 {
1362 struct glx_screen *psc;
1363 struct glx_display *priv;
1364 const char **str;
1365
1366
1367 if (GetGLXPrivScreenConfig(dpy, screen, &priv, &psc) != Success) {
1368 return NULL;
1369 }
1370
1371 switch (name) {
1372 case GLX_VENDOR:
1373 str = &priv->serverGLXvendor;
1374 break;
1375 case GLX_VERSION:
1376 str = &priv->serverGLXversion;
1377 break;
1378 case GLX_EXTENSIONS:
1379 str = &psc->serverGLXexts;
1380 break;
1381 default:
1382 return NULL;
1383 }
1384
1385 if (*str == NULL) {
1386 *str = __glXQueryServerString(dpy, priv->majorOpcode, screen, name);
1387 }
1388
1389 return *str;
1390 }
1391
1392
1393 /*
1394 ** EXT_import_context
1395 */
1396
1397 _GLX_PUBLIC Display *
1398 glXGetCurrentDisplay(void)
1399 {
1400 struct glx_context *gc = __glXGetCurrentContext();
1401 if (gc == &dummyContext)
1402 return NULL;
1403 return gc->currentDpy;
1404 }
1405
1406 _GLX_PUBLIC
1407 GLX_ALIAS(Display *, glXGetCurrentDisplayEXT, (void), (),
1408 glXGetCurrentDisplay)
1409
1410 #ifndef GLX_USE_APPLEGL
1411 _GLX_PUBLIC GLXContext
1412 glXImportContextEXT(Display *dpy, GLXContextID contextID)
1413 {
1414 struct glx_display *priv = __glXInitialize(dpy);
1415 struct glx_screen *psc = NULL;
1416 xGLXQueryContextReply reply;
1417 CARD8 opcode;
1418 struct glx_context *ctx;
1419 int i, renderType = GLX_RGBA_TYPE; /* By default, assume RGBA context */
1420 XID share = None;
1421 struct glx_config *mode = NULL;
1422 uint32_t fbconfigID = 0;
1423 uint32_t visualID = 0;
1424 uint32_t screen = 0;
1425 Bool got_screen = False;
1426
1427 if (priv == NULL)
1428 return NULL;
1429
1430 /* The GLX_EXT_import_context spec says:
1431 *
1432 * "If <contextID> does not refer to a valid context, then a BadContext
1433 * error is generated; if <contextID> refers to direct rendering
1434 * context then no error is generated but glXImportContextEXT returns
1435 * NULL."
1436 *
1437 * If contextID is None, generate BadContext on the client-side. Other
1438 * sorts of invalid contexts will be detected by the server in the
1439 * __glXIsDirect call.
1440 */
1441 if (contextID == None) {
1442 __glXSendError(dpy, GLXBadContext, contextID, X_GLXIsDirect, false);
1443 return NULL;
1444 }
1445
1446 if (__glXIsDirect(dpy, contextID, NULL))
1447 return NULL;
1448
1449 opcode = __glXSetupForCommand(dpy);
1450 if (!opcode)
1451 return 0;
1452
1453 /* Send the glXQueryContextInfoEXT request */
1454 LockDisplay(dpy);
1455
1456 if (priv->majorVersion > 1 || priv->minorVersion >= 3) {
1457 xGLXQueryContextReq *req;
1458
1459 GetReq(GLXQueryContext, req);
1460
1461 req->reqType = opcode;
1462 req->glxCode = X_GLXQueryContext;
1463 req->context = contextID;
1464 }
1465 else {
1466 xGLXVendorPrivateReq *vpreq;
1467 xGLXQueryContextInfoEXTReq *req;
1468
1469 GetReqExtra(GLXVendorPrivate,
1470 sz_xGLXQueryContextInfoEXTReq - sz_xGLXVendorPrivateReq,
1471 vpreq);
1472 req = (xGLXQueryContextInfoEXTReq *) vpreq;
1473 req->reqType = opcode;
1474 req->glxCode = X_GLXVendorPrivateWithReply;
1475 req->vendorCode = X_GLXvop_QueryContextInfoEXT;
1476 req->context = contextID;
1477 }
1478
1479 if (_XReply(dpy, (xReply *) & reply, 0, False) &&
1480 reply.n < (INT32_MAX / 2)) {
1481
1482 for (i = 0; i < reply.n; i++) {
1483 int prop[2];
1484
1485 _XRead(dpy, (char *)prop, sizeof(prop));
1486 switch (prop[0]) {
1487 case GLX_SCREEN:
1488 screen = prop[1];
1489 got_screen = True;
1490 break;
1491 case GLX_SHARE_CONTEXT_EXT:
1492 share = prop[1];
1493 break;
1494 case GLX_VISUAL_ID_EXT:
1495 visualID = prop[1];
1496 break;
1497 case GLX_FBCONFIG_ID:
1498 fbconfigID = prop[1];
1499 break;
1500 case GLX_RENDER_TYPE:
1501 renderType = prop[1];
1502 break;
1503 }
1504 }
1505 }
1506 UnlockDisplay(dpy);
1507 SyncHandle();
1508
1509 if (!got_screen)
1510 return NULL;
1511
1512 psc = GetGLXScreenConfigs(dpy, screen);
1513 if (psc == NULL)
1514 return NULL;
1515
1516 if (fbconfigID != 0) {
1517 mode = glx_config_find_fbconfig(psc->configs, fbconfigID);
1518 } else if (visualID != 0) {
1519 mode = glx_config_find_visual(psc->visuals, visualID);
1520 }
1521
1522 if (mode == NULL)
1523 return NULL;
1524
1525 ctx = indirect_create_context(psc, mode, NULL, renderType);
1526 if (ctx == NULL)
1527 return NULL;
1528
1529 ctx->xid = contextID;
1530 ctx->imported = GL_TRUE;
1531 ctx->share_xid = share;
1532
1533 return (GLXContext) ctx;
1534 }
1535
1536 #endif
1537
1538 _GLX_PUBLIC int
1539 glXQueryContext(Display * dpy, GLXContext ctx_user, int attribute, int *value)
1540 {
1541 struct glx_context *ctx = (struct glx_context *) ctx_user;
1542
1543 switch (attribute) {
1544 case GLX_SHARE_CONTEXT_EXT:
1545 *value = ctx->share_xid;
1546 break;
1547 case GLX_VISUAL_ID_EXT:
1548 *value = ctx->config ? ctx->config->visualID : None;
1549 break;
1550 case GLX_SCREEN:
1551 *value = ctx->screen;
1552 break;
1553 case GLX_FBCONFIG_ID:
1554 *value = ctx->config ? ctx->config->fbconfigID : None;
1555 break;
1556 case GLX_RENDER_TYPE:
1557 *value = ctx->renderType;
1558 break;
1559 default:
1560 return GLX_BAD_ATTRIBUTE;
1561 }
1562 return Success;
1563 }
1564
1565 _GLX_PUBLIC
1566 GLX_ALIAS(int, glXQueryContextInfoEXT,
1567 (Display * dpy, GLXContext ctx, int attribute, int *value),
1568 (dpy, ctx, attribute, value), glXQueryContext)
1569
1570 _GLX_PUBLIC GLXContextID glXGetContextIDEXT(const GLXContext ctx_user)
1571 {
1572 struct glx_context *ctx = (struct glx_context *) ctx_user;
1573
1574 return (ctx == NULL) ? None : ctx->xid;
1575 }
1576
1577 _GLX_PUBLIC void
1578 glXFreeContextEXT(Display *dpy, GLXContext ctx)
1579 {
1580 struct glx_context *gc = (struct glx_context *) ctx;
1581
1582 if (gc == NULL || gc->xid == None)
1583 return;
1584
1585 /* The GLX_EXT_import_context spec says:
1586 *
1587 * "glXFreeContext does not free the server-side context information or
1588 * the XID associated with the server-side context."
1589 *
1590 * Don't send any protocol. Just destroy the client-side tracking of the
1591 * context. Also, only release the context structure if it's not current.
1592 */
1593 __glXLock();
1594 if (gc->currentDpy) {
1595 gc->xid = None;
1596 } else {
1597 gc->vtable->destroy(gc);
1598 }
1599 __glXUnlock();
1600 }
1601
1602 _GLX_PUBLIC GLXFBConfig *
1603 glXChooseFBConfig(Display * dpy, int screen,
1604 const int *attribList, int *nitems)
1605 {
1606 struct glx_config **config_list;
1607 int list_size;
1608
1609
1610 config_list = (struct glx_config **)
1611 glXGetFBConfigs(dpy, screen, &list_size);
1612
1613 if ((config_list != NULL) && (list_size > 0) && (attribList != NULL)) {
1614 list_size = choose_fbconfig(config_list, list_size, attribList);
1615 if (list_size == 0) {
1616 free(config_list);
1617 config_list = NULL;
1618 }
1619 }
1620
1621 *nitems = list_size;
1622 return (GLXFBConfig *) config_list;
1623 }
1624
1625
1626 _GLX_PUBLIC GLXContext
1627 glXCreateNewContext(Display * dpy, GLXFBConfig fbconfig,
1628 int renderType, GLXContext shareList, Bool allowDirect)
1629 {
1630 struct glx_config *config = (struct glx_config *) fbconfig;
1631 struct glx_config **config_list;
1632 int list_size;
1633 unsigned i;
1634
1635 if (!config) {
1636 __glXSendError(dpy, GLXBadFBConfig, 0, X_GLXCreateNewContext, false);
1637 return NULL;
1638 }
1639
1640 config_list = (struct glx_config **)
1641 glXGetFBConfigs(dpy, config->screen, &list_size);
1642
1643 for (i = 0; i < list_size; i++) {
1644 if (config_list[i] == config)
1645 break;
1646 }
1647 free(config_list);
1648
1649 if (i == list_size) {
1650 __glXSendError(dpy, GLXBadFBConfig, 0, X_GLXCreateNewContext, false);
1651 return NULL;
1652 }
1653
1654 return CreateContext(dpy, config->fbconfigID, config, shareList,
1655 allowDirect, X_GLXCreateNewContext, renderType,
1656 config->screen);
1657 }
1658
1659
1660 _GLX_PUBLIC GLXDrawable
1661 glXGetCurrentReadDrawable(void)
1662 {
1663 struct glx_context *gc = __glXGetCurrentContext();
1664
1665 return gc->currentReadable;
1666 }
1667
1668
1669 _GLX_PUBLIC GLXFBConfig *
1670 glXGetFBConfigs(Display * dpy, int screen, int *nelements)
1671 {
1672 struct glx_display *priv = __glXInitialize(dpy);
1673 struct glx_config **config_list = NULL;
1674 struct glx_config *config;
1675 unsigned num_configs = 0;
1676 int i;
1677
1678 *nelements = 0;
1679 if (priv && (priv->screens != NULL)
1680 && (screen >= 0) && (screen < ScreenCount(dpy))
1681 && (priv->screens[screen]->configs != NULL)
1682 && (priv->screens[screen]->configs->fbconfigID
1683 != (int) GLX_DONT_CARE)) {
1684
1685 for (config = priv->screens[screen]->configs; config != NULL;
1686 config = config->next) {
1687 if (config->fbconfigID != (int) GLX_DONT_CARE) {
1688 num_configs++;
1689 }
1690 }
1691
1692 config_list = malloc(num_configs * sizeof *config_list);
1693 if (config_list != NULL) {
1694 *nelements = num_configs;
1695 i = 0;
1696 for (config = priv->screens[screen]->configs; config != NULL;
1697 config = config->next) {
1698 if (config->fbconfigID != (int) GLX_DONT_CARE) {
1699 config_list[i] = config;
1700 i++;
1701 }
1702 }
1703 }
1704 }
1705
1706 return (GLXFBConfig *) config_list;
1707 }
1708
1709
1710 _GLX_PUBLIC int
1711 glXGetFBConfigAttrib(Display * dpy, GLXFBConfig fbconfig,
1712 int attribute, int *value)
1713 {
1714 struct glx_config *config = ValidateGLXFBConfig(dpy, fbconfig);
1715
1716 if (config == NULL)
1717 return GLXBadFBConfig;
1718
1719 return glx_config_get(config, attribute, value);
1720 }
1721
1722
1723 _GLX_PUBLIC XVisualInfo *
1724 glXGetVisualFromFBConfig(Display * dpy, GLXFBConfig fbconfig)
1725 {
1726 XVisualInfo visualTemplate;
1727 struct glx_config *config = (struct glx_config *) fbconfig;
1728 int count;
1729
1730 /*
1731 ** Get a list of all visuals, return if list is empty
1732 */
1733 visualTemplate.visualid = config->visualID;
1734 return XGetVisualInfo(dpy, VisualIDMask, &visualTemplate, &count);
1735 }
1736
1737 #ifndef GLX_USE_APPLEGL
1738 /*
1739 ** GLX_SGI_swap_control
1740 */
1741 static int
1742 __glXSwapIntervalSGI(int interval)
1743 {
1744 xGLXVendorPrivateReq *req;
1745 struct glx_context *gc = __glXGetCurrentContext();
1746 struct glx_screen *psc;
1747 Display *dpy;
1748 CARD32 *interval_ptr;
1749 CARD8 opcode;
1750
1751 if (gc == &dummyContext) {
1752 return GLX_BAD_CONTEXT;
1753 }
1754
1755 if (interval <= 0) {
1756 return GLX_BAD_VALUE;
1757 }
1758
1759 psc = GetGLXScreenConfigs( gc->currentDpy, gc->screen);
1760
1761 #ifdef GLX_DIRECT_RENDERING
1762 if (gc->isDirect && psc && psc->driScreen &&
1763 psc->driScreen->setSwapInterval) {
1764 __GLXDRIdrawable *pdraw =
1765 GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
1766 /* Simply ignore the command if the GLX drawable has been destroyed but
1767 * the context is still bound.
1768 */
1769 if (pdraw)
1770 psc->driScreen->setSwapInterval(pdraw, interval);
1771 return 0;
1772 }
1773 #endif
1774
1775 dpy = gc->currentDpy;
1776 opcode = __glXSetupForCommand(dpy);
1777 if (!opcode) {
1778 return 0;
1779 }
1780
1781 /* Send the glXSwapIntervalSGI request */
1782 LockDisplay(dpy);
1783 GetReqExtra(GLXVendorPrivate, sizeof(CARD32), req);
1784 req->reqType = opcode;
1785 req->glxCode = X_GLXVendorPrivate;
1786 req->vendorCode = X_GLXvop_SwapIntervalSGI;
1787 req->contextTag = gc->currentContextTag;
1788
1789 interval_ptr = (CARD32 *) (req + 1);
1790 *interval_ptr = interval;
1791
1792 UnlockDisplay(dpy);
1793 SyncHandle();
1794 XFlush(dpy);
1795
1796 return 0;
1797 }
1798
1799
1800 /*
1801 ** GLX_MESA_swap_control
1802 */
1803 static int
1804 __glXSwapIntervalMESA(unsigned int interval)
1805 {
1806 #ifdef GLX_DIRECT_RENDERING
1807 struct glx_context *gc = __glXGetCurrentContext();
1808
1809 if (gc != &dummyContext && gc->isDirect) {
1810 struct glx_screen *psc;
1811
1812 psc = GetGLXScreenConfigs( gc->currentDpy, gc->screen);
1813 if (psc && psc->driScreen && psc->driScreen->setSwapInterval) {
1814 __GLXDRIdrawable *pdraw =
1815 GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
1816
1817 /* Simply ignore the command if the GLX drawable has been destroyed but
1818 * the context is still bound.
1819 */
1820 if (!pdraw)
1821 return 0;
1822
1823 return psc->driScreen->setSwapInterval(pdraw, interval);
1824 }
1825 }
1826 #endif
1827
1828 return GLX_BAD_CONTEXT;
1829 }
1830
1831
1832 static int
1833 __glXGetSwapIntervalMESA(void)
1834 {
1835 #ifdef GLX_DIRECT_RENDERING
1836 struct glx_context *gc = __glXGetCurrentContext();
1837
1838 if (gc != &dummyContext && gc->isDirect) {
1839 struct glx_screen *psc;
1840
1841 psc = GetGLXScreenConfigs( gc->currentDpy, gc->screen);
1842 if (psc && psc->driScreen && psc->driScreen->getSwapInterval) {
1843 __GLXDRIdrawable *pdraw =
1844 GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
1845 if (pdraw)
1846 return psc->driScreen->getSwapInterval(pdraw);
1847 }
1848 }
1849 #endif
1850
1851 return 0;
1852 }
1853
1854
1855 /*
1856 ** GLX_SGI_video_sync
1857 */
1858 static int
1859 __glXGetVideoSyncSGI(unsigned int *count)
1860 {
1861 int64_t ust, msc, sbc;
1862 int ret;
1863 struct glx_context *gc = __glXGetCurrentContext();
1864 struct glx_screen *psc;
1865 #ifdef GLX_DIRECT_RENDERING
1866 __GLXDRIdrawable *pdraw;
1867 #endif
1868
1869 if (gc == &dummyContext)
1870 return GLX_BAD_CONTEXT;
1871
1872 #ifdef GLX_DIRECT_RENDERING
1873 if (!gc->isDirect)
1874 return GLX_BAD_CONTEXT;
1875 #endif
1876
1877 psc = GetGLXScreenConfigs(gc->currentDpy, gc->screen);
1878 #ifdef GLX_DIRECT_RENDERING
1879 pdraw = GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
1880 #endif
1881
1882 /* FIXME: Looking at the GLX_SGI_video_sync spec in the extension registry,
1883 * FIXME: there should be a GLX encoding for this call. I can find no
1884 * FIXME: documentation for the GLX encoding.
1885 */
1886 #ifdef GLX_DIRECT_RENDERING
1887 if (psc && psc->driScreen && psc->driScreen->getDrawableMSC) {
1888 ret = psc->driScreen->getDrawableMSC(psc, pdraw, &ust, &msc, &sbc);
1889 *count = (unsigned) msc;
1890 return (ret == True) ? 0 : GLX_BAD_CONTEXT;
1891 }
1892 #endif
1893
1894 return GLX_BAD_CONTEXT;
1895 }
1896
1897 static int
1898 __glXWaitVideoSyncSGI(int divisor, int remainder, unsigned int *count)
1899 {
1900 struct glx_context *gc = __glXGetCurrentContext();
1901 struct glx_screen *psc;
1902 #ifdef GLX_DIRECT_RENDERING
1903 __GLXDRIdrawable *pdraw;
1904 #endif
1905 int64_t ust, msc, sbc;
1906 int ret;
1907
1908 if (divisor <= 0 || remainder < 0)
1909 return GLX_BAD_VALUE;
1910
1911 if (gc == &dummyContext)
1912 return GLX_BAD_CONTEXT;
1913
1914 #ifdef GLX_DIRECT_RENDERING
1915 if (!gc->isDirect)
1916 return GLX_BAD_CONTEXT;
1917 #endif
1918
1919 psc = GetGLXScreenConfigs( gc->currentDpy, gc->screen);
1920 #ifdef GLX_DIRECT_RENDERING
1921 pdraw = GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
1922 #endif
1923
1924 #ifdef GLX_DIRECT_RENDERING
1925 if (psc && psc->driScreen && psc->driScreen->waitForMSC) {
1926 ret = psc->driScreen->waitForMSC(pdraw, 0, divisor, remainder, &ust, &msc,
1927 &sbc);
1928 *count = (unsigned) msc;
1929 return (ret == True) ? 0 : GLX_BAD_CONTEXT;
1930 }
1931 #endif
1932
1933 return GLX_BAD_CONTEXT;
1934 }
1935
1936 #endif /* GLX_USE_APPLEGL */
1937
1938 /*
1939 ** GLX_SGIX_fbconfig
1940 ** Many of these functions are aliased to GLX 1.3 entry points in the
1941 ** GLX_functions table.
1942 */
1943
1944 _GLX_PUBLIC
1945 GLX_ALIAS(int, glXGetFBConfigAttribSGIX,
1946 (Display * dpy, GLXFBConfigSGIX config, int attribute, int *value),
1947 (dpy, config, attribute, value), glXGetFBConfigAttrib)
1948
1949 _GLX_PUBLIC GLX_ALIAS(GLXFBConfigSGIX *, glXChooseFBConfigSGIX,
1950 (Display * dpy, int screen, int *attrib_list,
1951 int *nelements), (dpy, screen, attrib_list, nelements),
1952 glXChooseFBConfig)
1953
1954 _GLX_PUBLIC GLX_ALIAS(XVisualInfo *, glXGetVisualFromFBConfigSGIX,
1955 (Display * dpy, GLXFBConfigSGIX config),
1956 (dpy, config), glXGetVisualFromFBConfig)
1957
1958 _GLX_PUBLIC GLXPixmap
1959 glXCreateGLXPixmapWithConfigSGIX(Display * dpy,
1960 GLXFBConfigSGIX fbconfig,
1961 Pixmap pixmap)
1962 {
1963 #ifndef GLX_USE_APPLEGL
1964 xGLXVendorPrivateWithReplyReq *vpreq;
1965 xGLXCreateGLXPixmapWithConfigSGIXReq *req;
1966 GLXPixmap xid = None;
1967 CARD8 opcode;
1968 struct glx_screen *psc;
1969 #endif
1970 struct glx_config *config = (struct glx_config *) fbconfig;
1971
1972
1973 if ((dpy == NULL) || (config == NULL)) {
1974 return None;
1975 }
1976 #ifdef GLX_USE_APPLEGL
1977 if(apple_glx_pixmap_create(dpy, config->screen, pixmap, config))
1978 return None;
1979 return pixmap;
1980 #else
1981
1982 psc = GetGLXScreenConfigs(dpy, config->screen);
1983 if ((psc != NULL)
1984 && __glXExtensionBitIsEnabled(psc, SGIX_fbconfig_bit)) {
1985 opcode = __glXSetupForCommand(dpy);
1986 if (!opcode) {
1987 return None;
1988 }
1989
1990 /* Send the glXCreateGLXPixmapWithConfigSGIX request */
1991 LockDisplay(dpy);
1992 GetReqExtra(GLXVendorPrivateWithReply,
1993 sz_xGLXCreateGLXPixmapWithConfigSGIXReq -
1994 sz_xGLXVendorPrivateWithReplyReq, vpreq);
1995 req = (xGLXCreateGLXPixmapWithConfigSGIXReq *) vpreq;
1996 req->reqType = opcode;
1997 req->glxCode = X_GLXVendorPrivateWithReply;
1998 req->vendorCode = X_GLXvop_CreateGLXPixmapWithConfigSGIX;
1999 req->screen = config->screen;
2000 req->fbconfig = config->fbconfigID;
2001 req->pixmap = pixmap;
2002 req->glxpixmap = xid = XAllocID(dpy);
2003 UnlockDisplay(dpy);
2004 SyncHandle();
2005 }
2006
2007 return xid;
2008 #endif
2009 }
2010
2011 _GLX_PUBLIC GLXContext
2012 glXCreateContextWithConfigSGIX(Display * dpy,
2013 GLXFBConfigSGIX fbconfig, int renderType,
2014 GLXContext shareList, Bool allowDirect)
2015 {
2016 GLXContext gc = NULL;
2017 struct glx_config *config = (struct glx_config *) fbconfig;
2018 struct glx_screen *psc;
2019
2020
2021 if ((dpy == NULL) || (config == NULL)) {
2022 return None;
2023 }
2024
2025 psc = GetGLXScreenConfigs(dpy, config->screen);
2026 if ((psc != NULL)
2027 && __glXExtensionBitIsEnabled(psc, SGIX_fbconfig_bit)) {
2028 gc = CreateContext(dpy, config->fbconfigID, config, shareList,
2029 allowDirect,
2030 X_GLXvop_CreateContextWithConfigSGIX, renderType,
2031 config->screen);
2032 }
2033
2034 return gc;
2035 }
2036
2037
2038 _GLX_PUBLIC GLXFBConfigSGIX
2039 glXGetFBConfigFromVisualSGIX(Display * dpy, XVisualInfo * vis)
2040 {
2041 struct glx_display *priv;
2042 struct glx_screen *psc = NULL;
2043
2044 if ((GetGLXPrivScreenConfig(dpy, vis->screen, &priv, &psc) == Success)
2045 && __glXExtensionBitIsEnabled(psc, SGIX_fbconfig_bit)
2046 && (psc->configs->fbconfigID != (int) GLX_DONT_CARE)) {
2047 return (GLXFBConfigSGIX) glx_config_find_visual(psc->configs,
2048 vis->visualid);
2049 }
2050
2051 return NULL;
2052 }
2053
2054 #ifndef GLX_USE_APPLEGL
2055 /*
2056 ** GLX_OML_sync_control
2057 */
2058 static Bool
2059 __glXGetSyncValuesOML(Display * dpy, GLXDrawable drawable,
2060 int64_t * ust, int64_t * msc, int64_t * sbc)
2061 {
2062 struct glx_display * const priv = __glXInitialize(dpy);
2063 int ret;
2064 #ifdef GLX_DIRECT_RENDERING
2065 __GLXDRIdrawable *pdraw;
2066 #endif
2067 struct glx_screen *psc;
2068
2069 if (!priv)
2070 return False;
2071
2072 #ifdef GLX_DIRECT_RENDERING
2073 pdraw = GetGLXDRIDrawable(dpy, drawable);
2074 psc = pdraw ? pdraw->psc : NULL;
2075 if (pdraw && psc->driScreen->getDrawableMSC) {
2076 ret = psc->driScreen->getDrawableMSC(psc, pdraw, ust, msc, sbc);
2077 return ret;
2078 }
2079 #endif
2080
2081 return False;
2082 }
2083
2084 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
2085 _X_HIDDEN GLboolean
2086 __glxGetMscRate(struct glx_screen *psc,
2087 int32_t * numerator, int32_t * denominator)
2088 {
2089 XF86VidModeModeLine mode_line;
2090 int dot_clock;
2091 int i;
2092
2093 if (XF86VidModeQueryVersion(psc->dpy, &i, &i) &&
2094 XF86VidModeGetModeLine(psc->dpy, psc->scr, &dot_clock, &mode_line)) {
2095 unsigned n = dot_clock * 1000;
2096 unsigned d = mode_line.vtotal * mode_line.htotal;
2097
2098 # define V_INTERLACE 0x010
2099 # define V_DBLSCAN 0x020
2100
2101 if (mode_line.flags & V_INTERLACE)
2102 n *= 2;
2103 else if (mode_line.flags & V_DBLSCAN)
2104 d *= 2;
2105
2106 /* The OML_sync_control spec requires that if the refresh rate is a
2107 * whole number, that the returned numerator be equal to the refresh
2108 * rate and the denominator be 1.
2109 */
2110
2111 if (n % d == 0) {
2112 n /= d;
2113 d = 1;
2114 }
2115 else {
2116 static const unsigned f[] = { 13, 11, 7, 5, 3, 2, 0 };
2117
2118 /* This is a poor man's way to reduce a fraction. It's far from
2119 * perfect, but it will work well enough for this situation.
2120 */
2121
2122 for (i = 0; f[i] != 0; i++) {
2123 while (n % f[i] == 0 && d % f[i] == 0) {
2124 d /= f[i];
2125 n /= f[i];
2126 }
2127 }
2128 }
2129
2130 *numerator = n;
2131 *denominator = d;
2132
2133 return True;
2134 }
2135
2136 return False;
2137 }
2138 #endif
2139
2140 /**
2141 * Determine the refresh rate of the specified drawable and display.
2142 *
2143 * \param dpy Display whose refresh rate is to be determined.
2144 * \param drawable Drawable whose refresh rate is to be determined.
2145 * \param numerator Numerator of the refresh rate.
2146 * \param demoninator Denominator of the refresh rate.
2147 * \return If the refresh rate for the specified display and drawable could
2148 * be calculated, True is returned. Otherwise False is returned.
2149 *
2150 * \note This function is implemented entirely client-side. A lot of other
2151 * functionality is required to export GLX_OML_sync_control, so on
2152 * XFree86 this function can be called for direct-rendering contexts
2153 * when GLX_OML_sync_control appears in the client extension string.
2154 */
2155
2156 _X_HIDDEN GLboolean
2157 __glXGetMscRateOML(Display * dpy, GLXDrawable drawable,
2158 int32_t * numerator, int32_t * denominator)
2159 {
2160 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
2161 __GLXDRIdrawable *draw = GetGLXDRIDrawable(dpy, drawable);
2162
2163 if (draw == NULL)
2164 return False;
2165
2166 return __glxGetMscRate(draw->psc, numerator, denominator);
2167 #else
2168 (void) dpy;
2169 (void) drawable;
2170 (void) numerator;
2171 (void) denominator;
2172 #endif
2173 return False;
2174 }
2175
2176
2177 static int64_t
2178 __glXSwapBuffersMscOML(Display * dpy, GLXDrawable drawable,
2179 int64_t target_msc, int64_t divisor, int64_t remainder)
2180 {
2181 struct glx_context *gc = __glXGetCurrentContext();
2182 #ifdef GLX_DIRECT_RENDERING
2183 __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable);
2184 struct glx_screen *psc = pdraw ? pdraw->psc : NULL;
2185 #endif
2186
2187 if (gc == &dummyContext) /* no GLX for this */
2188 return -1;
2189
2190 #ifdef GLX_DIRECT_RENDERING
2191 if (!pdraw || !gc->isDirect)
2192 return -1;
2193 #endif
2194
2195 /* The OML_sync_control spec says these should "generate a GLX_BAD_VALUE
2196 * error", but it also says "It [glXSwapBuffersMscOML] will return a value
2197 * of -1 if the function failed because of errors detected in the input
2198 * parameters"
2199 */
2200 if (divisor < 0 || remainder < 0 || target_msc < 0)
2201 return -1;
2202 if (divisor > 0 && remainder >= divisor)
2203 return -1;
2204
2205 if (target_msc == 0 && divisor == 0 && remainder == 0)
2206 remainder = 1;
2207
2208 #ifdef GLX_DIRECT_RENDERING
2209 if (psc->driScreen && psc->driScreen->swapBuffers)
2210 return (*psc->driScreen->swapBuffers)(pdraw, target_msc, divisor,
2211 remainder, False);
2212 #endif
2213
2214 return -1;
2215 }
2216
2217
2218 static Bool
2219 __glXWaitForMscOML(Display * dpy, GLXDrawable drawable,
2220 int64_t target_msc, int64_t divisor,
2221 int64_t remainder, int64_t * ust,
2222 int64_t * msc, int64_t * sbc)
2223 {
2224 #ifdef GLX_DIRECT_RENDERING
2225 __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable);
2226 struct glx_screen *psc = pdraw ? pdraw->psc : NULL;
2227 int ret;
2228 #endif
2229
2230
2231 /* The OML_sync_control spec says these should "generate a GLX_BAD_VALUE
2232 * error", but the return type in the spec is Bool.
2233 */
2234 if (divisor < 0 || remainder < 0 || target_msc < 0)
2235 return False;
2236 if (divisor > 0 && remainder >= divisor)
2237 return False;
2238
2239 #ifdef GLX_DIRECT_RENDERING
2240 if (pdraw && psc->driScreen && psc->driScreen->waitForMSC) {
2241 ret = psc->driScreen->waitForMSC(pdraw, target_msc, divisor, remainder,
2242 ust, msc, sbc);
2243 return ret;
2244 }
2245 #endif
2246
2247 return False;
2248 }
2249
2250
2251 static Bool
2252 __glXWaitForSbcOML(Display * dpy, GLXDrawable drawable,
2253 int64_t target_sbc, int64_t * ust,
2254 int64_t * msc, int64_t * sbc)
2255 {
2256 #ifdef GLX_DIRECT_RENDERING
2257 __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable);
2258 struct glx_screen *psc = pdraw ? pdraw->psc : NULL;
2259 int ret;
2260 #endif
2261
2262 /* The OML_sync_control spec says this should "generate a GLX_BAD_VALUE
2263 * error", but the return type in the spec is Bool.
2264 */
2265 if (target_sbc < 0)
2266 return False;
2267
2268 #ifdef GLX_DIRECT_RENDERING
2269 if (pdraw && psc->driScreen && psc->driScreen->waitForSBC) {
2270 ret = psc->driScreen->waitForSBC(pdraw, target_sbc, ust, msc, sbc);
2271 return ret;
2272 }
2273 #endif
2274
2275 return False;
2276 }
2277
2278 /*@}*/
2279
2280
2281 /**
2282 * Mesa extension stubs. These will help reduce portability problems.
2283 */
2284 /*@{*/
2285
2286 /**
2287 * Release all buffers associated with the specified GLX drawable.
2288 *
2289 * \todo
2290 * This function was intended for stand-alone Mesa. The issue there is that
2291 * the library doesn't get any notification when a window is closed. In
2292 * DRI there is a similar but slightly different issue. When GLX 1.3 is
2293 * supported, there are 3 different functions to destroy a drawable. It
2294 * should be possible to create GLX protocol (or have it determine which
2295 * protocol to use based on the type of the drawable) to have one function
2296 * do the work of 3. For the direct-rendering case, this function could
2297 * just call the driver's \c __DRIdrawableRec::destroyDrawable function.
2298 * This would reduce the frequency with which \c __driGarbageCollectDrawables
2299 * would need to be used. This really should be done as part of the new DRI
2300 * interface work.
2301 *
2302 * \sa http://oss.sgi.com/projects/ogl-sample/registry/MESA/release_buffers.txt
2303 * __driGarbageCollectDrawables
2304 * glXDestroyGLXPixmap
2305 * glXDestroyPbuffer glXDestroyPixmap glXDestroyWindow
2306 * glXDestroyGLXPbufferSGIX glXDestroyGLXVideoSourceSGIX
2307 */
2308 static Bool
2309 __glXReleaseBuffersMESA(Display * dpy, GLXDrawable d)
2310 {
2311 (void) dpy;
2312 (void) d;
2313 return False;
2314 }
2315
2316
2317 _GLX_PUBLIC GLXPixmap
2318 glXCreateGLXPixmapMESA(Display * dpy, XVisualInfo * visual,
2319 Pixmap pixmap, Colormap cmap)
2320 {
2321 (void) dpy;
2322 (void) visual;
2323 (void) pixmap;
2324 (void) cmap;
2325 return 0;
2326 }
2327
2328 /*@}*/
2329
2330
2331 /**
2332 * GLX_MESA_copy_sub_buffer
2333 */
2334 #define X_GLXvop_CopySubBufferMESA 5154 /* temporary */
2335 static void
2336 __glXCopySubBufferMESA(Display * dpy, GLXDrawable drawable,
2337 int x, int y, int width, int height)
2338 {
2339 xGLXVendorPrivateReq *req;
2340 struct glx_context *gc;
2341 GLXContextTag tag;
2342 CARD32 *drawable_ptr;
2343 INT32 *x_ptr, *y_ptr, *w_ptr, *h_ptr;
2344 CARD8 opcode;
2345
2346 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
2347 __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable);
2348 if (pdraw != NULL) {
2349 struct glx_screen *psc = pdraw->psc;
2350 if (psc->driScreen->copySubBuffer != NULL) {
2351 (*psc->driScreen->copySubBuffer) (pdraw, x, y, width, height, True);
2352 }
2353
2354 return;
2355 }
2356 #endif
2357
2358 opcode = __glXSetupForCommand(dpy);
2359 if (!opcode)
2360 return;
2361
2362 /*
2363 ** The calling thread may or may not have a current context. If it
2364 ** does, send the context tag so the server can do a flush.
2365 */
2366 gc = __glXGetCurrentContext();
2367 if ((gc != &dummyContext) && (dpy == gc->currentDpy) &&
2368 ((drawable == gc->currentDrawable) ||
2369 (drawable == gc->currentReadable))) {
2370 tag = gc->currentContextTag;
2371 }
2372 else {
2373 tag = 0;
2374 }
2375
2376 LockDisplay(dpy);
2377 GetReqExtra(GLXVendorPrivate, sizeof(CARD32) + sizeof(INT32) * 4, req);
2378 req->reqType = opcode;
2379 req->glxCode = X_GLXVendorPrivate;
2380 req->vendorCode = X_GLXvop_CopySubBufferMESA;
2381 req->contextTag = tag;
2382
2383 drawable_ptr = (CARD32 *) (req + 1);
2384 x_ptr = (INT32 *) (drawable_ptr + 1);
2385 y_ptr = (INT32 *) (drawable_ptr + 2);
2386 w_ptr = (INT32 *) (drawable_ptr + 3);
2387 h_ptr = (INT32 *) (drawable_ptr + 4);
2388
2389 *drawable_ptr = drawable;
2390 *x_ptr = x;
2391 *y_ptr = y;
2392 *w_ptr = width;
2393 *h_ptr = height;
2394
2395 UnlockDisplay(dpy);
2396 SyncHandle();
2397 }
2398
2399 /*@{*/
2400 static void
2401 __glXBindTexImageEXT(Display * dpy,
2402 GLXDrawable drawable, int buffer, const int *attrib_list)
2403 {
2404 struct glx_context *gc = __glXGetCurrentContext();
2405
2406 if (gc->vtable->bind_tex_image == NULL)
2407 return;
2408
2409 gc->vtable->bind_tex_image(dpy, drawable, buffer, attrib_list);
2410 }
2411
2412 static void
2413 __glXReleaseTexImageEXT(Display * dpy, GLXDrawable drawable, int buffer)
2414 {
2415 struct glx_context *gc = __glXGetCurrentContext();
2416
2417 if (gc->vtable->release_tex_image == NULL)
2418 return;
2419
2420 gc->vtable->release_tex_image(dpy, drawable, buffer);
2421 }
2422
2423 /*@}*/
2424
2425 #endif /* GLX_USE_APPLEGL */
2426
2427 /*
2428 ** glXGetProcAddress support
2429 */
2430
2431 struct name_address_pair
2432 {
2433 const char *Name;
2434 GLvoid *Address;
2435 };
2436
2437 #define GLX_FUNCTION(f) { # f, (GLvoid *) f }
2438 #define GLX_FUNCTION2(n,f) { # n, (GLvoid *) f }
2439
2440 static const struct name_address_pair GLX_functions[] = {
2441 /*** GLX_VERSION_1_0 ***/
2442 GLX_FUNCTION(glXChooseVisual),
2443 GLX_FUNCTION(glXCopyContext),
2444 GLX_FUNCTION(glXCreateContext),
2445 GLX_FUNCTION(glXCreateGLXPixmap),
2446 GLX_FUNCTION(glXDestroyContext),
2447 GLX_FUNCTION(glXDestroyGLXPixmap),
2448 GLX_FUNCTION(glXGetConfig),
2449 GLX_FUNCTION(glXGetCurrentContext),
2450 GLX_FUNCTION(glXGetCurrentDrawable),
2451 GLX_FUNCTION(glXIsDirect),
2452 GLX_FUNCTION(glXMakeCurrent),
2453 GLX_FUNCTION(glXQueryExtension),
2454 GLX_FUNCTION(glXQueryVersion),
2455 GLX_FUNCTION(glXSwapBuffers),
2456 GLX_FUNCTION(glXUseXFont),
2457 GLX_FUNCTION(glXWaitGL),
2458 GLX_FUNCTION(glXWaitX),
2459
2460 /*** GLX_VERSION_1_1 ***/
2461 GLX_FUNCTION(glXGetClientString),
2462 GLX_FUNCTION(glXQueryExtensionsString),
2463 GLX_FUNCTION(glXQueryServerString),
2464
2465 /*** GLX_VERSION_1_2 ***/
2466 GLX_FUNCTION(glXGetCurrentDisplay),
2467
2468 /*** GLX_VERSION_1_3 ***/
2469 GLX_FUNCTION(glXChooseFBConfig),
2470 GLX_FUNCTION(glXCreateNewContext),
2471 GLX_FUNCTION(glXCreatePbuffer),
2472 GLX_FUNCTION(glXCreatePixmap),
2473 GLX_FUNCTION(glXCreateWindow),
2474 GLX_FUNCTION(glXDestroyPbuffer),
2475 GLX_FUNCTION(glXDestroyPixmap),
2476 GLX_FUNCTION(glXDestroyWindow),
2477 GLX_FUNCTION(glXGetCurrentReadDrawable),
2478 GLX_FUNCTION(glXGetFBConfigAttrib),
2479 GLX_FUNCTION(glXGetFBConfigs),
2480 GLX_FUNCTION(glXGetSelectedEvent),
2481 GLX_FUNCTION(glXGetVisualFromFBConfig),
2482 GLX_FUNCTION(glXMakeContextCurrent),
2483 GLX_FUNCTION(glXQueryContext),
2484 GLX_FUNCTION(glXQueryDrawable),
2485 GLX_FUNCTION(glXSelectEvent),
2486
2487 #ifndef GLX_USE_APPLEGL
2488 /*** GLX_SGI_swap_control ***/
2489 GLX_FUNCTION2(glXSwapIntervalSGI, __glXSwapIntervalSGI),
2490
2491 /*** GLX_SGI_video_sync ***/
2492 GLX_FUNCTION2(glXGetVideoSyncSGI, __glXGetVideoSyncSGI),
2493 GLX_FUNCTION2(glXWaitVideoSyncSGI, __glXWaitVideoSyncSGI),
2494
2495 /*** GLX_SGI_make_current_read ***/
2496 GLX_FUNCTION2(glXMakeCurrentReadSGI, glXMakeContextCurrent),
2497 GLX_FUNCTION2(glXGetCurrentReadDrawableSGI, glXGetCurrentReadDrawable),
2498
2499 /*** GLX_EXT_import_context ***/
2500 GLX_FUNCTION(glXFreeContextEXT),
2501 GLX_FUNCTION(glXGetContextIDEXT),
2502 GLX_FUNCTION2(glXGetCurrentDisplayEXT, glXGetCurrentDisplay),
2503 GLX_FUNCTION(glXImportContextEXT),
2504 GLX_FUNCTION2(glXQueryContextInfoEXT, glXQueryContext),
2505 #endif
2506
2507 /*** GLX_SGIX_fbconfig ***/
2508 GLX_FUNCTION2(glXGetFBConfigAttribSGIX, glXGetFBConfigAttrib),
2509 GLX_FUNCTION2(glXChooseFBConfigSGIX, glXChooseFBConfig),
2510 GLX_FUNCTION(glXCreateGLXPixmapWithConfigSGIX),
2511 GLX_FUNCTION(glXCreateContextWithConfigSGIX),
2512 GLX_FUNCTION2(glXGetVisualFromFBConfigSGIX, glXGetVisualFromFBConfig),
2513 GLX_FUNCTION(glXGetFBConfigFromVisualSGIX),
2514
2515 #ifndef GLX_USE_APPLEGL
2516 /*** GLX_SGIX_pbuffer ***/
2517 GLX_FUNCTION(glXCreateGLXPbufferSGIX),
2518 GLX_FUNCTION(glXDestroyGLXPbufferSGIX),
2519 GLX_FUNCTION(glXQueryGLXPbufferSGIX),
2520 GLX_FUNCTION(glXSelectEventSGIX),
2521 GLX_FUNCTION(glXGetSelectedEventSGIX),
2522
2523 /*** GLX_MESA_copy_sub_buffer ***/
2524 GLX_FUNCTION2(glXCopySubBufferMESA, __glXCopySubBufferMESA),
2525
2526 /*** GLX_MESA_pixmap_colormap ***/
2527 GLX_FUNCTION(glXCreateGLXPixmapMESA),
2528
2529 /*** GLX_MESA_release_buffers ***/
2530 GLX_FUNCTION2(glXReleaseBuffersMESA, __glXReleaseBuffersMESA),
2531
2532 /*** GLX_MESA_swap_control ***/
2533 GLX_FUNCTION2(glXSwapIntervalMESA, __glXSwapIntervalMESA),
2534 GLX_FUNCTION2(glXGetSwapIntervalMESA, __glXGetSwapIntervalMESA),
2535 #endif
2536
2537 /*** GLX_ARB_get_proc_address ***/
2538 GLX_FUNCTION(glXGetProcAddressARB),
2539
2540 /*** GLX 1.4 ***/
2541 GLX_FUNCTION2(glXGetProcAddress, glXGetProcAddressARB),
2542
2543 #ifndef GLX_USE_APPLEGL
2544 /*** GLX_OML_sync_control ***/
2545 GLX_FUNCTION2(glXWaitForSbcOML, __glXWaitForSbcOML),
2546 GLX_FUNCTION2(glXWaitForMscOML, __glXWaitForMscOML),
2547 GLX_FUNCTION2(glXSwapBuffersMscOML, __glXSwapBuffersMscOML),
2548 GLX_FUNCTION2(glXGetMscRateOML, __glXGetMscRateOML),
2549 GLX_FUNCTION2(glXGetSyncValuesOML, __glXGetSyncValuesOML),
2550
2551 /*** GLX_EXT_texture_from_pixmap ***/
2552 GLX_FUNCTION2(glXBindTexImageEXT, __glXBindTexImageEXT),
2553 GLX_FUNCTION2(glXReleaseTexImageEXT, __glXReleaseTexImageEXT),
2554 #endif
2555
2556 #if defined(GLX_DIRECT_RENDERING) && defined(GLX_USE_DRM)
2557 /*** DRI configuration ***/
2558 GLX_FUNCTION(glXGetScreenDriver),
2559 GLX_FUNCTION(glXGetDriverConfig),
2560 #endif
2561
2562 /*** GLX_ARB_create_context and GLX_ARB_create_context_profile ***/
2563 GLX_FUNCTION(glXCreateContextAttribsARB),
2564
2565 /*** GLX_MESA_query_renderer ***/
2566 GLX_FUNCTION(glXQueryRendererIntegerMESA),
2567 GLX_FUNCTION(glXQueryRendererStringMESA),
2568 GLX_FUNCTION(glXQueryCurrentRendererIntegerMESA),
2569 GLX_FUNCTION(glXQueryCurrentRendererStringMESA),
2570
2571 {NULL, NULL} /* end of list */
2572 };
2573
2574 static const GLvoid *
2575 get_glx_proc_address(const char *funcName)
2576 {
2577 GLuint i;
2578
2579 /* try static functions */
2580 for (i = 0; GLX_functions[i].Name; i++) {
2581 if (strcmp(GLX_functions[i].Name, funcName) == 0)
2582 return GLX_functions[i].Address;
2583 }
2584
2585 return NULL;
2586 }
2587
2588 /**
2589 * Get the address of a named GL function. This is the pre-GLX 1.4 name for
2590 * \c glXGetProcAddress.
2591 *
2592 * \param procName Name of a GL or GLX function.
2593 * \returns A pointer to the named function
2594 *
2595 * \sa glXGetProcAddress
2596 */
2597 _GLX_PUBLIC void (*glXGetProcAddressARB(const GLubyte * procName)) (void)
2598 {
2599 typedef void (*gl_function) (void);
2600 gl_function f;
2601
2602
2603 /* Search the table of GLX and internal functions first. If that
2604 * fails and the supplied name could be a valid core GL name, try
2605 * searching the core GL function table. This check is done to prevent
2606 * DRI based drivers from searching the core GL function table for
2607 * internal API functions.
2608 */
2609 f = (gl_function) get_glx_proc_address((const char *) procName);
2610 if ((f == NULL) && (procName[0] == 'g') && (procName[1] == 'l')
2611 && (procName[2] != 'X')) {
2612 #ifdef GLX_INDIRECT_RENDERING
2613 f = (gl_function) __indirect_get_proc_address((const char *) procName);
2614 #endif
2615 if (!f)
2616 f = (gl_function) _glapi_get_proc_address((const char *) procName);
2617 if (!f) {
2618 struct glx_context *gc = __glXGetCurrentContext();
2619
2620 if (gc != NULL && gc->vtable->get_proc_address != NULL)
2621 f = gc->vtable->get_proc_address((const char *) procName);
2622 }
2623 }
2624 return f;
2625 }
2626
2627 /**
2628 * Get the address of a named GL function. This is the GLX 1.4 name for
2629 * \c glXGetProcAddressARB.
2630 *
2631 * \param procName Name of a GL or GLX function.
2632 * \returns A pointer to the named function
2633 *
2634 * \sa glXGetProcAddressARB
2635 */
2636 _GLX_PUBLIC
2637 GLX_ALIAS(__GLXextFuncPtr, glXGetProcAddress,
2638 (const GLubyte * procName),
2639 (procName), glXGetProcAddressARB)
2640
2641 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
2642 /**
2643 * Get the unadjusted system time (UST). Currently, the UST is measured in
2644 * microseconds since Epoc. The actual resolution of the UST may vary from
2645 * system to system, and the units may vary from release to release.
2646 * Drivers should not call this function directly. They should instead use
2647 * \c glXGetProcAddress to obtain a pointer to the function.
2648 *
2649 * \param ust Location to store the 64-bit UST
2650 * \returns Zero on success or a negative errno value on failure.
2651 *
2652 * \sa glXGetProcAddress, PFNGLXGETUSTPROC
2653 *
2654 * \since Internal API version 20030317.
2655 */
2656 _X_HIDDEN int
2657 __glXGetUST(int64_t * ust)
2658 {
2659 struct timeval tv;
2660
2661 if (ust == NULL) {
2662 return -EFAULT;
2663 }
2664
2665 if (gettimeofday(&tv, NULL) == 0) {
2666 ust[0] = (tv.tv_sec * 1000000) + tv.tv_usec;
2667 return 0;
2668 }
2669 else {
2670 return -errno;
2671 }
2672 }
2673 #endif /* GLX_DIRECT_RENDERING */
2674
2675 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
2676
2677 PUBLIC int
2678 MesaGLInteropGLXQueryDeviceInfo(Display *dpy, GLXContext context,
2679 struct mesa_glinterop_device_info *out)
2680 {
2681 struct glx_context *gc = (struct glx_context*)context;
2682 int ret;
2683
2684 __glXLock();
2685
2686 if (!gc || gc->xid == None || !gc->isDirect) {
2687 __glXUnlock();
2688 return MESA_GLINTEROP_INVALID_CONTEXT;
2689 }
2690
2691 if (!gc->vtable->interop_query_device_info) {
2692 __glXUnlock();
2693 return MESA_GLINTEROP_UNSUPPORTED;
2694 }
2695
2696 ret = gc->vtable->interop_query_device_info(gc, out);
2697 __glXUnlock();
2698 return ret;
2699 }
2700
2701 PUBLIC int
2702 MesaGLInteropGLXExportObject(Display *dpy, GLXContext context,
2703 struct mesa_glinterop_export_in *in,
2704 struct mesa_glinterop_export_out *out)
2705 {
2706 struct glx_context *gc = (struct glx_context*)context;
2707 int ret;
2708
2709 __glXLock();
2710
2711 if (!gc || gc->xid == None || !gc->isDirect) {
2712 __glXUnlock();
2713 return MESA_GLINTEROP_INVALID_CONTEXT;
2714 }
2715
2716 if (!gc->vtable->interop_export_object) {
2717 __glXUnlock();
2718 return MESA_GLINTEROP_UNSUPPORTED;
2719 }
2720
2721 ret = gc->vtable->interop_export_object(gc, in, out);
2722 __glXUnlock();
2723 return ret;
2724 }
2725
2726 #endif /* defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL) */