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