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