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