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