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