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