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