Merge branch 'mesa_7_5_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 return xid;
884 }
885
886 /*
887 ** Destroy the named pixmap
888 */
889 PUBLIC void
890 glXDestroyGLXPixmap(Display * dpy, GLXPixmap glxpixmap)
891 {
892 xGLXDestroyGLXPixmapReq *req;
893 CARD8 opcode;
894
895 opcode = __glXSetupForCommand(dpy);
896 if (!opcode) {
897 return;
898 }
899
900 /* Send the glXDestroyGLXPixmap request */
901 LockDisplay(dpy);
902 GetReq(GLXDestroyGLXPixmap, req);
903 req->reqType = opcode;
904 req->glxCode = X_GLXDestroyGLXPixmap;
905 req->glxpixmap = glxpixmap;
906 UnlockDisplay(dpy);
907 SyncHandle();
908
909 #ifdef GLX_DIRECT_RENDERING
910 {
911 int screen;
912 __GLXdisplayPrivate *const priv = __glXInitialize(dpy);
913 __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, glxpixmap, &screen);
914 __GLXscreenConfigs *psc = &priv->screenConfigs[screen];
915
916 if (pdraw != NULL) {
917 (*pdraw->destroyDrawable) (pdraw);
918 __glxHashDelete(psc->drawHash, glxpixmap);
919 }
920 }
921 #endif
922 }
923
924 PUBLIC void
925 glXSwapBuffers(Display * dpy, GLXDrawable drawable)
926 {
927 GLXContext gc;
928 GLXContextTag tag;
929 CARD8 opcode;
930 #ifdef USE_XCB
931 xcb_connection_t *c;
932 #else
933 xGLXSwapBuffersReq *req;
934 #endif
935
936 #ifdef GLX_DIRECT_RENDERING
937 __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable, NULL);
938
939 if (pdraw != NULL) {
940 glFlush();
941 (*pdraw->psc->driScreen->swapBuffers) (pdraw);
942 return;
943 }
944 #endif
945
946 opcode = __glXSetupForCommand(dpy);
947 if (!opcode) {
948 return;
949 }
950
951 /*
952 ** The calling thread may or may not have a current context. If it
953 ** does, send the context tag so the server can do a flush.
954 */
955 gc = __glXGetCurrentContext();
956 if ((gc != NULL) && (dpy == gc->currentDpy) &&
957 ((drawable == gc->currentDrawable)
958 || (drawable == gc->currentReadable))) {
959 tag = gc->currentContextTag;
960 }
961 else {
962 tag = 0;
963 }
964
965 #ifdef USE_XCB
966 c = XGetXCBConnection(dpy);
967 xcb_glx_swap_buffers(c, tag, drawable);
968 xcb_flush(c);
969 #else
970 /* Send the glXSwapBuffers request */
971 LockDisplay(dpy);
972 GetReq(GLXSwapBuffers, req);
973 req->reqType = opcode;
974 req->glxCode = X_GLXSwapBuffers;
975 req->drawable = drawable;
976 req->contextTag = tag;
977 UnlockDisplay(dpy);
978 SyncHandle();
979 XFlush(dpy);
980 #endif /* USE_XCB */
981 }
982
983
984 /*
985 ** Return configuration information for the given display, screen and
986 ** visual combination.
987 */
988 PUBLIC int
989 glXGetConfig(Display * dpy, XVisualInfo * vis, int attribute,
990 int *value_return)
991 {
992 __GLXdisplayPrivate *priv;
993 __GLXscreenConfigs *psc;
994 __GLcontextModes *modes;
995 int status;
996
997 status = GetGLXPrivScreenConfig(dpy, vis->screen, &priv, &psc);
998 if (status == Success) {
999 modes = _gl_context_modes_find_visual(psc->visuals, vis->visualid);
1000
1001 /* Lookup attribute after first finding a match on the visual */
1002 if (modes != NULL) {
1003 return _gl_get_context_mode_data(modes, attribute, value_return);
1004 }
1005
1006 status = GLX_BAD_VISUAL;
1007 }
1008
1009 /*
1010 ** If we can't find the config for this visual, this visual is not
1011 ** supported by the OpenGL implementation on the server.
1012 */
1013 if ((status == GLX_BAD_VISUAL) && (attribute == GLX_USE_GL)) {
1014 *value_return = GL_FALSE;
1015 status = Success;
1016 }
1017
1018 return status;
1019 }
1020
1021 /************************************************************************/
1022
1023 static void
1024 init_fbconfig_for_chooser(__GLcontextModes * config,
1025 GLboolean fbconfig_style_tags)
1026 {
1027 memset(config, 0, sizeof(__GLcontextModes));
1028 config->visualID = (XID) GLX_DONT_CARE;
1029 config->visualType = GLX_DONT_CARE;
1030
1031 /* glXChooseFBConfig specifies different defaults for these two than
1032 * glXChooseVisual.
1033 */
1034 if (fbconfig_style_tags) {
1035 config->rgbMode = GL_TRUE;
1036 config->doubleBufferMode = GLX_DONT_CARE;
1037 }
1038
1039 config->visualRating = GLX_DONT_CARE;
1040 config->transparentPixel = GLX_NONE;
1041 config->transparentRed = GLX_DONT_CARE;
1042 config->transparentGreen = GLX_DONT_CARE;
1043 config->transparentBlue = GLX_DONT_CARE;
1044 config->transparentAlpha = GLX_DONT_CARE;
1045 config->transparentIndex = GLX_DONT_CARE;
1046
1047 config->drawableType = GLX_WINDOW_BIT;
1048 config->renderType =
1049 (config->rgbMode) ? GLX_RGBA_BIT : GLX_COLOR_INDEX_BIT;
1050 config->xRenderable = GLX_DONT_CARE;
1051 config->fbconfigID = (GLXFBConfigID) (GLX_DONT_CARE);
1052
1053 config->swapMethod = GLX_DONT_CARE;
1054 }
1055
1056 #define MATCH_DONT_CARE( param ) \
1057 do { \
1058 if ( (a-> param != GLX_DONT_CARE) \
1059 && (a-> param != b-> param) ) { \
1060 return False; \
1061 } \
1062 } while ( 0 )
1063
1064 #define MATCH_MINIMUM( param ) \
1065 do { \
1066 if ( (a-> param != GLX_DONT_CARE) \
1067 && (a-> param > b-> param) ) { \
1068 return False; \
1069 } \
1070 } while ( 0 )
1071
1072 #define MATCH_EXACT( param ) \
1073 do { \
1074 if ( a-> param != b-> param) { \
1075 return False; \
1076 } \
1077 } while ( 0 )
1078
1079 /**
1080 * Determine if two GLXFBConfigs are compatible.
1081 *
1082 * \param a Application specified config to test.
1083 * \param b Server specified config to test against \c a.
1084 */
1085 static Bool
1086 fbconfigs_compatible(const __GLcontextModes * const a,
1087 const __GLcontextModes * const b)
1088 {
1089 MATCH_DONT_CARE(doubleBufferMode);
1090 MATCH_DONT_CARE(visualType);
1091 MATCH_DONT_CARE(visualRating);
1092 MATCH_DONT_CARE(xRenderable);
1093 MATCH_DONT_CARE(fbconfigID);
1094 MATCH_DONT_CARE(swapMethod);
1095
1096 MATCH_MINIMUM(rgbBits);
1097 MATCH_MINIMUM(numAuxBuffers);
1098 MATCH_MINIMUM(redBits);
1099 MATCH_MINIMUM(greenBits);
1100 MATCH_MINIMUM(blueBits);
1101 MATCH_MINIMUM(alphaBits);
1102 MATCH_MINIMUM(depthBits);
1103 MATCH_MINIMUM(stencilBits);
1104 MATCH_MINIMUM(accumRedBits);
1105 MATCH_MINIMUM(accumGreenBits);
1106 MATCH_MINIMUM(accumBlueBits);
1107 MATCH_MINIMUM(accumAlphaBits);
1108 MATCH_MINIMUM(sampleBuffers);
1109 MATCH_MINIMUM(maxPbufferWidth);
1110 MATCH_MINIMUM(maxPbufferHeight);
1111 MATCH_MINIMUM(maxPbufferPixels);
1112 MATCH_MINIMUM(samples);
1113
1114 MATCH_DONT_CARE(stereoMode);
1115 MATCH_EXACT(level);
1116
1117 if (((a->drawableType & b->drawableType) == 0)
1118 || ((a->renderType & b->renderType) == 0)) {
1119 return False;
1120 }
1121
1122
1123 /* There is a bug in a few of the XFree86 DDX drivers. They contain
1124 * visuals with a "transparent type" of 0 when they really mean GLX_NONE.
1125 * Technically speaking, it is a bug in the DDX driver, but there is
1126 * enough of an installed base to work around the problem here. In any
1127 * case, 0 is not a valid value of the transparent type, so we'll treat 0
1128 * from the app as GLX_DONT_CARE. We'll consider GLX_NONE from the app and
1129 * 0 from the server to be a match to maintain backward compatibility with
1130 * the (broken) drivers.
1131 */
1132
1133 if (a->transparentPixel != GLX_DONT_CARE && a->transparentPixel != 0) {
1134 if (a->transparentPixel == GLX_NONE) {
1135 if (b->transparentPixel != GLX_NONE && b->transparentPixel != 0)
1136 return False;
1137 }
1138 else {
1139 MATCH_EXACT(transparentPixel);
1140 }
1141
1142 switch (a->transparentPixel) {
1143 case GLX_TRANSPARENT_RGB:
1144 MATCH_DONT_CARE(transparentRed);
1145 MATCH_DONT_CARE(transparentGreen);
1146 MATCH_DONT_CARE(transparentBlue);
1147 MATCH_DONT_CARE(transparentAlpha);
1148 break;
1149
1150 case GLX_TRANSPARENT_INDEX:
1151 MATCH_DONT_CARE(transparentIndex);
1152 break;
1153
1154 default:
1155 break;
1156 }
1157 }
1158
1159 return True;
1160 }
1161
1162
1163 /* There's some trickly language in the GLX spec about how this is supposed
1164 * to work. Basically, if a given component size is either not specified
1165 * or the requested size is zero, it is supposed to act like PERFER_SMALLER.
1166 * Well, that's really hard to do with the code as-is. This behavior is
1167 * closer to correct, but still not technically right.
1168 */
1169 #define PREFER_LARGER_OR_ZERO(comp) \
1170 do { \
1171 if ( ((*a)-> comp) != ((*b)-> comp) ) { \
1172 if ( ((*a)-> comp) == 0 ) { \
1173 return -1; \
1174 } \
1175 else if ( ((*b)-> comp) == 0 ) { \
1176 return 1; \
1177 } \
1178 else { \
1179 return ((*b)-> comp) - ((*a)-> comp) ; \
1180 } \
1181 } \
1182 } while( 0 )
1183
1184 #define PREFER_LARGER(comp) \
1185 do { \
1186 if ( ((*a)-> comp) != ((*b)-> comp) ) { \
1187 return ((*b)-> comp) - ((*a)-> comp) ; \
1188 } \
1189 } while( 0 )
1190
1191 #define PREFER_SMALLER(comp) \
1192 do { \
1193 if ( ((*a)-> comp) != ((*b)-> comp) ) { \
1194 return ((*a)-> comp) - ((*b)-> comp) ; \
1195 } \
1196 } while( 0 )
1197
1198 /**
1199 * Compare two GLXFBConfigs. This function is intended to be used as the
1200 * compare function passed in to qsort.
1201 *
1202 * \returns If \c a is a "better" config, according to the specification of
1203 * SGIX_fbconfig, a number less than zero is returned. If \c b is
1204 * better, then a number greater than zero is return. If both are
1205 * equal, zero is returned.
1206 * \sa qsort, glXChooseVisual, glXChooseFBConfig, glXChooseFBConfigSGIX
1207 */
1208 static int
1209 fbconfig_compare(const __GLcontextModes * const *const a,
1210 const __GLcontextModes * const *const b)
1211 {
1212 /* The order of these comparisons must NOT change. It is defined by
1213 * the GLX 1.3 spec and ARB_multisample.
1214 */
1215
1216 PREFER_SMALLER(visualSelectGroup);
1217
1218 /* The sort order for the visualRating is GLX_NONE, GLX_SLOW, and
1219 * GLX_NON_CONFORMANT_CONFIG. It just so happens that this is the
1220 * numerical sort order of the enums (0x8000, 0x8001, and 0x800D).
1221 */
1222 PREFER_SMALLER(visualRating);
1223
1224 /* This isn't quite right. It is supposed to compare the sum of the
1225 * components the user specifically set minimums for.
1226 */
1227 PREFER_LARGER_OR_ZERO(redBits);
1228 PREFER_LARGER_OR_ZERO(greenBits);
1229 PREFER_LARGER_OR_ZERO(blueBits);
1230 PREFER_LARGER_OR_ZERO(alphaBits);
1231
1232 PREFER_SMALLER(rgbBits);
1233
1234 if (((*a)->doubleBufferMode != (*b)->doubleBufferMode)) {
1235 /* Prefer single-buffer.
1236 */
1237 return (!(*a)->doubleBufferMode) ? -1 : 1;
1238 }
1239
1240 PREFER_SMALLER(numAuxBuffers);
1241
1242 PREFER_LARGER_OR_ZERO(depthBits);
1243 PREFER_SMALLER(stencilBits);
1244
1245 /* This isn't quite right. It is supposed to compare the sum of the
1246 * components the user specifically set minimums for.
1247 */
1248 PREFER_LARGER_OR_ZERO(accumRedBits);
1249 PREFER_LARGER_OR_ZERO(accumGreenBits);
1250 PREFER_LARGER_OR_ZERO(accumBlueBits);
1251 PREFER_LARGER_OR_ZERO(accumAlphaBits);
1252
1253 PREFER_SMALLER(visualType);
1254
1255 /* None of the multisample specs say where this comparison should happen,
1256 * so I put it near the end.
1257 */
1258 PREFER_SMALLER(sampleBuffers);
1259 PREFER_SMALLER(samples);
1260
1261 /* None of the pbuffer or fbconfig specs say that this comparison needs
1262 * to happen at all, but it seems like it should.
1263 */
1264 PREFER_LARGER(maxPbufferWidth);
1265 PREFER_LARGER(maxPbufferHeight);
1266 PREFER_LARGER(maxPbufferPixels);
1267
1268 return 0;
1269 }
1270
1271
1272 /**
1273 * Selects and sorts a subset of the supplied configs based on the attributes.
1274 * This function forms to basis of \c glXChooseVisual, \c glXChooseFBConfig,
1275 * and \c glXChooseFBConfigSGIX.
1276 *
1277 * \param configs Array of pointers to possible configs. The elements of
1278 * this array that do not meet the criteria will be set to
1279 * NULL. The remaining elements will be sorted according to
1280 * the various visual / FBConfig selection rules.
1281 * \param num_configs Number of elements in the \c configs array.
1282 * \param attribList Attributes used select from \c configs. This array is
1283 * terminated by a \c None tag. The array can either take
1284 * the form expected by \c glXChooseVisual (where boolean
1285 * tags do not have a value) or by \c glXChooseFBConfig
1286 * (where every tag has a value).
1287 * \param fbconfig_style_tags Selects whether \c attribList is in
1288 * \c glXChooseVisual style or
1289 * \c glXChooseFBConfig style.
1290 * \returns The number of valid elements left in \c configs.
1291 *
1292 * \sa glXChooseVisual, glXChooseFBConfig, glXChooseFBConfigSGIX
1293 */
1294 static int
1295 choose_visual(__GLcontextModes ** configs, int num_configs,
1296 const int *attribList, GLboolean fbconfig_style_tags)
1297 {
1298 __GLcontextModes test_config;
1299 int base;
1300 int i;
1301
1302 /* This is a fairly direct implementation of the selection method
1303 * described by GLX_SGIX_fbconfig. Start by culling out all the
1304 * configs that are not compatible with the selected parameter
1305 * list.
1306 */
1307
1308 init_fbconfig_for_chooser(&test_config, fbconfig_style_tags);
1309 __glXInitializeVisualConfigFromTags(&test_config, 512,
1310 (const INT32 *) attribList,
1311 GL_TRUE, fbconfig_style_tags);
1312
1313 base = 0;
1314 for (i = 0; i < num_configs; i++) {
1315 if (fbconfigs_compatible(&test_config, configs[i])) {
1316 configs[base] = configs[i];
1317 base++;
1318 }
1319 }
1320
1321 if (base == 0) {
1322 return 0;
1323 }
1324
1325 if (base < num_configs) {
1326 (void) memset(&configs[base], 0, sizeof(void *) * (num_configs - base));
1327 }
1328
1329 /* After the incompatible configs are removed, the resulting
1330 * list is sorted according to the rules set out in the various
1331 * specifications.
1332 */
1333
1334 qsort(configs, base, sizeof(__GLcontextModes *),
1335 (int (*)(const void *, const void *)) fbconfig_compare);
1336 return base;
1337 }
1338
1339
1340
1341
1342 /*
1343 ** Return the visual that best matches the template. Return None if no
1344 ** visual matches the template.
1345 */
1346 PUBLIC XVisualInfo *
1347 glXChooseVisual(Display * dpy, int screen, int *attribList)
1348 {
1349 XVisualInfo *visualList = NULL;
1350 __GLXdisplayPrivate *priv;
1351 __GLXscreenConfigs *psc;
1352 __GLcontextModes test_config;
1353 __GLcontextModes *modes;
1354 const __GLcontextModes *best_config = NULL;
1355
1356 /*
1357 ** Get a list of all visuals, return if list is empty
1358 */
1359 if (GetGLXPrivScreenConfig(dpy, screen, &priv, &psc) != Success) {
1360 return None;
1361 }
1362
1363
1364 /*
1365 ** Build a template from the defaults and the attribute list
1366 ** Free visual list and return if an unexpected token is encountered
1367 */
1368 init_fbconfig_for_chooser(&test_config, GL_FALSE);
1369 __glXInitializeVisualConfigFromTags(&test_config, 512,
1370 (const INT32 *) attribList,
1371 GL_TRUE, GL_FALSE);
1372
1373 /*
1374 ** Eliminate visuals that don't meet minimum requirements
1375 ** Compute a score for those that do
1376 ** Remember which visual, if any, got the highest score
1377 ** If no visual is acceptable, return None
1378 ** Otherwise, create an XVisualInfo list with just the selected X visual
1379 ** and return this.
1380 */
1381 for (modes = psc->visuals; modes != NULL; modes = modes->next) {
1382 if (fbconfigs_compatible(&test_config, modes)
1383 && ((best_config == NULL)
1384 ||
1385 (fbconfig_compare
1386 ((const __GLcontextModes * const *const) &modes,
1387 &best_config) < 0))) {
1388 XVisualInfo visualTemplate;
1389 XVisualInfo *newList;
1390 int i;
1391
1392 visualTemplate.screen = screen;
1393 visualTemplate.visualid = modes->visualID;
1394 newList = XGetVisualInfo(dpy, VisualScreenMask | VisualIDMask,
1395 &visualTemplate, &i);
1396
1397 if (newList) {
1398 Xfree(visualList);
1399 visualList = newList;
1400 best_config = modes;
1401 }
1402 }
1403 }
1404
1405 return visualList;
1406 }
1407
1408
1409 PUBLIC const char *
1410 glXQueryExtensionsString(Display * dpy, int screen)
1411 {
1412 __GLXscreenConfigs *psc;
1413 __GLXdisplayPrivate *priv;
1414
1415 if (GetGLXPrivScreenConfig(dpy, screen, &priv, &psc) != Success) {
1416 return NULL;
1417 }
1418
1419 if (!psc->effectiveGLXexts) {
1420 if (!psc->serverGLXexts) {
1421 psc->serverGLXexts =
1422 __glXQueryServerString(dpy, priv->majorOpcode, screen,
1423 GLX_EXTENSIONS);
1424 }
1425
1426 __glXCalculateUsableExtensions(psc,
1427 #ifdef GLX_DIRECT_RENDERING
1428 (psc->driScreen != NULL),
1429 #else
1430 GL_FALSE,
1431 #endif
1432 priv->minorVersion);
1433 }
1434
1435 return psc->effectiveGLXexts;
1436 }
1437
1438 PUBLIC const char *
1439 glXGetClientString(Display * dpy, int name)
1440 {
1441 switch (name) {
1442 case GLX_VENDOR:
1443 return (__glXGLXClientVendorName);
1444 case GLX_VERSION:
1445 return (__glXGLXClientVersion);
1446 case GLX_EXTENSIONS:
1447 return (__glXGetClientExtensions());
1448 default:
1449 return NULL;
1450 }
1451 }
1452
1453 PUBLIC const char *
1454 glXQueryServerString(Display * dpy, int screen, int name)
1455 {
1456 __GLXscreenConfigs *psc;
1457 __GLXdisplayPrivate *priv;
1458 const char **str;
1459
1460
1461 if (GetGLXPrivScreenConfig(dpy, screen, &priv, &psc) != Success) {
1462 return NULL;
1463 }
1464
1465 switch (name) {
1466 case GLX_VENDOR:
1467 str = &priv->serverGLXvendor;
1468 break;
1469 case GLX_VERSION:
1470 str = &priv->serverGLXversion;
1471 break;
1472 case GLX_EXTENSIONS:
1473 str = &psc->serverGLXexts;
1474 break;
1475 default:
1476 return NULL;
1477 }
1478
1479 if (*str == NULL) {
1480 *str = __glXQueryServerString(dpy, priv->majorOpcode, screen, name);
1481 }
1482
1483 return *str;
1484 }
1485
1486 void
1487 __glXClientInfo(Display * dpy, int opcode)
1488 {
1489 char *ext_str = __glXGetClientGLExtensionString();
1490 int size = strlen(ext_str) + 1;
1491
1492 #ifdef USE_XCB
1493 xcb_connection_t *c = XGetXCBConnection(dpy);
1494 xcb_glx_client_info(c,
1495 GLX_MAJOR_VERSION, GLX_MINOR_VERSION, size, ext_str);
1496 #else
1497 xGLXClientInfoReq *req;
1498
1499 /* Send the glXClientInfo request */
1500 LockDisplay(dpy);
1501 GetReq(GLXClientInfo, req);
1502 req->reqType = opcode;
1503 req->glxCode = X_GLXClientInfo;
1504 req->major = GLX_MAJOR_VERSION;
1505 req->minor = GLX_MINOR_VERSION;
1506
1507 req->length += (size + 3) >> 2;
1508 req->numbytes = size;
1509 Data(dpy, ext_str, size);
1510
1511 UnlockDisplay(dpy);
1512 SyncHandle();
1513 #endif /* USE_XCB */
1514
1515 Xfree(ext_str);
1516 }
1517
1518
1519 /*
1520 ** EXT_import_context
1521 */
1522
1523 PUBLIC Display *
1524 glXGetCurrentDisplay(void)
1525 {
1526 GLXContext gc = __glXGetCurrentContext();
1527 if (NULL == gc)
1528 return NULL;
1529 return gc->currentDpy;
1530 }
1531
1532 PUBLIC
1533 GLX_ALIAS(Display *, glXGetCurrentDisplayEXT, (void), (),
1534 glXGetCurrentDisplay)
1535
1536 /**
1537 * Used internally by libGL to send \c xGLXQueryContextinfoExtReq requests
1538 * to the X-server.
1539 *
1540 * \param dpy Display where \c ctx was created.
1541 * \param ctx Context to query.
1542 * \returns \c Success on success. \c GLX_BAD_CONTEXT if \c ctx is invalid,
1543 * or zero if the request failed due to internal problems (i.e.,
1544 * unable to allocate temporary memory, etc.)
1545 *
1546 * \note
1547 * This function dynamically determines whether to use the EXT_import_context
1548 * version of the protocol or the GLX 1.3 version of the protocol.
1549 */
1550 static int __glXQueryContextInfo(Display * dpy, GLXContext ctx)
1551 {
1552 __GLXdisplayPrivate *priv = __glXInitialize(dpy);
1553 xGLXQueryContextReply reply;
1554 CARD8 opcode;
1555 GLuint numValues;
1556 int retval;
1557
1558 if (ctx == NULL) {
1559 return GLX_BAD_CONTEXT;
1560 }
1561 opcode = __glXSetupForCommand(dpy);
1562 if (!opcode) {
1563 return 0;
1564 }
1565
1566 /* Send the glXQueryContextInfoEXT request */
1567 LockDisplay(dpy);
1568
1569 if ((priv->majorVersion > 1) || (priv->minorVersion >= 3)) {
1570 xGLXQueryContextReq *req;
1571
1572 GetReq(GLXQueryContext, req);
1573
1574 req->reqType = opcode;
1575 req->glxCode = X_GLXQueryContext;
1576 req->context = (unsigned int) (ctx->xid);
1577 }
1578 else {
1579 xGLXVendorPrivateReq *vpreq;
1580 xGLXQueryContextInfoEXTReq *req;
1581
1582 GetReqExtra(GLXVendorPrivate,
1583 sz_xGLXQueryContextInfoEXTReq - sz_xGLXVendorPrivateReq,
1584 vpreq);
1585 req = (xGLXQueryContextInfoEXTReq *) vpreq;
1586 req->reqType = opcode;
1587 req->glxCode = X_GLXVendorPrivateWithReply;
1588 req->vendorCode = X_GLXvop_QueryContextInfoEXT;
1589 req->context = (unsigned int) (ctx->xid);
1590 }
1591
1592 _XReply(dpy, (xReply *) & reply, 0, False);
1593
1594 numValues = reply.n;
1595 if (numValues == 0)
1596 retval = Success;
1597 else if (numValues > __GLX_MAX_CONTEXT_PROPS)
1598 retval = 0;
1599 else {
1600 int *propList, *pProp;
1601 int nPropListBytes;
1602 int i;
1603
1604 nPropListBytes = numValues << 3;
1605 propList = (int *) Xmalloc(nPropListBytes);
1606 if (NULL == propList) {
1607 retval = 0;
1608 }
1609 else {
1610 _XRead(dpy, (char *) propList, nPropListBytes);
1611 pProp = propList;
1612 for (i = 0; i < numValues; i++) {
1613 switch (*pProp++) {
1614 case GLX_SHARE_CONTEXT_EXT:
1615 ctx->share_xid = *pProp++;
1616 break;
1617 case GLX_VISUAL_ID_EXT:
1618 ctx->mode =
1619 _gl_context_modes_find_visual(ctx->psc->visuals, *pProp++);
1620 break;
1621 case GLX_SCREEN:
1622 ctx->screen = *pProp++;
1623 break;
1624 case GLX_FBCONFIG_ID:
1625 ctx->mode =
1626 _gl_context_modes_find_fbconfig(ctx->psc->configs,
1627 *pProp++);
1628 break;
1629 case GLX_RENDER_TYPE:
1630 ctx->renderType = *pProp++;
1631 break;
1632 default:
1633 pProp++;
1634 continue;
1635 }
1636 }
1637 Xfree((char *) propList);
1638 retval = Success;
1639 }
1640 }
1641 UnlockDisplay(dpy);
1642 SyncHandle();
1643 return retval;
1644 }
1645
1646 PUBLIC int
1647 glXQueryContext(Display * dpy, GLXContext ctx, int attribute, int *value)
1648 {
1649 int retVal;
1650
1651 /* get the information from the server if we don't have it already */
1652 #ifdef GLX_DIRECT_RENDERING
1653 if (!ctx->driContext && (ctx->mode == NULL)) {
1654 #else
1655 if (ctx->mode == NULL) {
1656 #endif
1657 retVal = __glXQueryContextInfo(dpy, ctx);
1658 if (Success != retVal)
1659 return retVal;
1660 }
1661 switch (attribute) {
1662 case GLX_SHARE_CONTEXT_EXT:
1663 *value = (int) (ctx->share_xid);
1664 break;
1665 case GLX_VISUAL_ID_EXT:
1666 *value = ctx->mode ? ctx->mode->visualID : None;
1667 break;
1668 case GLX_SCREEN:
1669 *value = (int) (ctx->screen);
1670 break;
1671 case GLX_FBCONFIG_ID:
1672 *value = ctx->mode ? ctx->mode->fbconfigID : None;
1673 break;
1674 case GLX_RENDER_TYPE:
1675 *value = (int) (ctx->renderType);
1676 break;
1677 default:
1678 return GLX_BAD_ATTRIBUTE;
1679 }
1680 return Success;
1681 }
1682
1683 PUBLIC
1684 GLX_ALIAS(int, glXQueryContextInfoEXT,
1685 (Display * dpy, GLXContext ctx, int attribute, int *value),
1686 (dpy, ctx, attribute, value), glXQueryContext)
1687
1688 PUBLIC GLXContextID glXGetContextIDEXT(const GLXContext ctx)
1689 {
1690 return ctx->xid;
1691 }
1692
1693 PUBLIC GLXContext
1694 glXImportContextEXT(Display * dpy, GLXContextID contextID)
1695 {
1696 GLXContext ctx;
1697
1698 if (contextID == None) {
1699 return NULL;
1700 }
1701 if (__glXIsDirect(dpy, contextID)) {
1702 return NULL;
1703 }
1704
1705 ctx = CreateContext(dpy, NULL, NULL, NULL, False, contextID, False, 0);
1706 if (NULL != ctx) {
1707 if (Success != __glXQueryContextInfo(dpy, ctx)) {
1708 return NULL;
1709 }
1710 }
1711 return ctx;
1712 }
1713
1714 PUBLIC void
1715 glXFreeContextEXT(Display * dpy, GLXContext ctx)
1716 {
1717 DestroyContext(dpy, ctx);
1718 }
1719
1720
1721
1722 /*
1723 * GLX 1.3 functions - these are just stubs for now!
1724 */
1725
1726 PUBLIC GLXFBConfig *
1727 glXChooseFBConfig(Display * dpy, int screen,
1728 const int *attribList, int *nitems)
1729 {
1730 __GLcontextModes **config_list;
1731 int list_size;
1732
1733
1734 config_list = (__GLcontextModes **)
1735 glXGetFBConfigs(dpy, screen, &list_size);
1736
1737 if ((config_list != NULL) && (list_size > 0) && (attribList != NULL)) {
1738 list_size = choose_visual(config_list, list_size, attribList, GL_TRUE);
1739 if (list_size == 0) {
1740 XFree(config_list);
1741 config_list = NULL;
1742 }
1743 }
1744
1745 *nitems = list_size;
1746 return (GLXFBConfig *) config_list;
1747 }
1748
1749
1750 PUBLIC GLXContext
1751 glXCreateNewContext(Display * dpy, GLXFBConfig config,
1752 int renderType, GLXContext shareList, Bool allowDirect)
1753 {
1754 return CreateContext(dpy, NULL, (__GLcontextModes *) config, shareList,
1755 allowDirect, None, True, renderType);
1756 }
1757
1758
1759 PUBLIC GLXDrawable
1760 glXGetCurrentReadDrawable(void)
1761 {
1762 GLXContext gc = __glXGetCurrentContext();
1763 return gc->currentReadable;
1764 }
1765
1766
1767 PUBLIC GLXFBConfig *
1768 glXGetFBConfigs(Display * dpy, int screen, int *nelements)
1769 {
1770 __GLXdisplayPrivate *priv = __glXInitialize(dpy);
1771 __GLcontextModes **config = NULL;
1772 int i;
1773
1774 *nelements = 0;
1775 if (priv && (priv->screenConfigs != NULL)
1776 && (screen >= 0) && (screen <= ScreenCount(dpy))
1777 && (priv->screenConfigs[screen].configs != NULL)
1778 && (priv->screenConfigs[screen].configs->fbconfigID != GLX_DONT_CARE)) {
1779 unsigned num_configs = 0;
1780 __GLcontextModes *modes;
1781
1782
1783 for (modes = priv->screenConfigs[screen].configs; modes != NULL;
1784 modes = modes->next) {
1785 if (modes->fbconfigID != GLX_DONT_CARE) {
1786 num_configs++;
1787 }
1788 }
1789
1790 config = (__GLcontextModes **) Xmalloc(sizeof(__GLcontextModes *)
1791 * num_configs);
1792 if (config != NULL) {
1793 *nelements = num_configs;
1794 i = 0;
1795 for (modes = priv->screenConfigs[screen].configs; modes != NULL;
1796 modes = modes->next) {
1797 if (modes->fbconfigID != GLX_DONT_CARE) {
1798 config[i] = modes;
1799 i++;
1800 }
1801 }
1802 }
1803 }
1804 return (GLXFBConfig *) config;
1805 }
1806
1807
1808 PUBLIC int
1809 glXGetFBConfigAttrib(Display * dpy, GLXFBConfig config,
1810 int attribute, int *value)
1811 {
1812 __GLcontextModes *const modes = ValidateGLXFBConfig(dpy, config);
1813
1814 return (modes != NULL)
1815 ? _gl_get_context_mode_data(modes, attribute, value)
1816 : GLXBadFBConfig;
1817 }
1818
1819
1820 PUBLIC XVisualInfo *
1821 glXGetVisualFromFBConfig(Display * dpy, GLXFBConfig config)
1822 {
1823 XVisualInfo visualTemplate;
1824 __GLcontextModes *fbconfig = (__GLcontextModes *) config;
1825 int count;
1826
1827 /*
1828 ** Get a list of all visuals, return if list is empty
1829 */
1830 visualTemplate.visualid = fbconfig->visualID;
1831 return XGetVisualInfo(dpy, VisualIDMask, &visualTemplate, &count);
1832 }
1833
1834
1835 /*
1836 ** GLX_SGI_swap_control
1837 */
1838 static int
1839 __glXSwapIntervalSGI(int interval)
1840 {
1841 xGLXVendorPrivateReq *req;
1842 GLXContext gc = __glXGetCurrentContext();
1843 Display *dpy;
1844 CARD32 *interval_ptr;
1845 CARD8 opcode;
1846
1847 if (gc == NULL) {
1848 return GLX_BAD_CONTEXT;
1849 }
1850
1851 if (interval <= 0) {
1852 return GLX_BAD_VALUE;
1853 }
1854
1855 #ifdef __DRI_SWAP_CONTROL
1856 if (gc->driContext) {
1857 __GLXscreenConfigs *const psc = GetGLXScreenConfigs(gc->currentDpy,
1858 gc->screen);
1859 __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(gc->currentDpy,
1860 gc->currentDrawable,
1861 NULL);
1862 if (psc->swapControl != NULL && pdraw != NULL) {
1863 psc->swapControl->setSwapInterval(pdraw->driDrawable, interval);
1864 return 0;
1865 }
1866 else {
1867 return GLX_BAD_CONTEXT;
1868 }
1869 }
1870 #endif
1871 dpy = gc->currentDpy;
1872 opcode = __glXSetupForCommand(dpy);
1873 if (!opcode) {
1874 return 0;
1875 }
1876
1877 /* Send the glXSwapIntervalSGI request */
1878 LockDisplay(dpy);
1879 GetReqExtra(GLXVendorPrivate, sizeof(CARD32), req);
1880 req->reqType = opcode;
1881 req->glxCode = X_GLXVendorPrivate;
1882 req->vendorCode = X_GLXvop_SwapIntervalSGI;
1883 req->contextTag = gc->currentContextTag;
1884
1885 interval_ptr = (CARD32 *) (req + 1);
1886 *interval_ptr = interval;
1887
1888 UnlockDisplay(dpy);
1889 SyncHandle();
1890 XFlush(dpy);
1891
1892 return 0;
1893 }
1894
1895
1896 /*
1897 ** GLX_MESA_swap_control
1898 */
1899 static int
1900 __glXSwapIntervalMESA(unsigned int interval)
1901 {
1902 #ifdef __DRI_SWAP_CONTROL
1903 GLXContext gc = __glXGetCurrentContext();
1904
1905 if (interval < 0) {
1906 return GLX_BAD_VALUE;
1907 }
1908
1909 if (gc != NULL && gc->driContext) {
1910 __GLXscreenConfigs *const psc = GetGLXScreenConfigs(gc->currentDpy,
1911 gc->screen);
1912
1913 if ((psc != NULL) && (psc->driScreen != NULL)) {
1914 __GLXDRIdrawable *pdraw =
1915 GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable, NULL);
1916 if (psc->swapControl != NULL && pdraw != NULL) {
1917 psc->swapControl->setSwapInterval(pdraw->driDrawable, interval);
1918 return 0;
1919 }
1920 }
1921 }
1922 #else
1923 (void) interval;
1924 #endif
1925
1926 return GLX_BAD_CONTEXT;
1927 }
1928
1929
1930 static int
1931 __glXGetSwapIntervalMESA(void)
1932 {
1933 #ifdef __DRI_SWAP_CONTROL
1934 GLXContext gc = __glXGetCurrentContext();
1935
1936 if (gc != NULL && gc->driContext) {
1937 __GLXscreenConfigs *const psc = GetGLXScreenConfigs(gc->currentDpy,
1938 gc->screen);
1939
1940 if ((psc != NULL) && (psc->driScreen != NULL)) {
1941 __GLXDRIdrawable *pdraw =
1942 GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable, NULL);
1943 if (psc->swapControl != NULL && pdraw != NULL) {
1944 return psc->swapControl->getSwapInterval(pdraw->driDrawable);
1945 }
1946 }
1947 }
1948 #endif
1949
1950 return 0;
1951 }
1952
1953
1954 /*
1955 ** GLX_MESA_swap_frame_usage
1956 */
1957
1958 static GLint
1959 __glXBeginFrameTrackingMESA(Display * dpy, GLXDrawable drawable)
1960 {
1961 int status = GLX_BAD_CONTEXT;
1962 #ifdef __DRI_FRAME_TRACKING
1963 int screen;
1964 __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable, &screen);
1965 __GLXscreenConfigs *const psc = GetGLXScreenConfigs(dpy, screen);
1966
1967 if (pdraw != NULL && psc->frameTracking != NULL)
1968 status = psc->frameTracking->frameTracking(pdraw->driDrawable, GL_TRUE);
1969 #else
1970 (void) dpy;
1971 (void) drawable;
1972 #endif
1973 return status;
1974 }
1975
1976
1977 static GLint
1978 __glXEndFrameTrackingMESA(Display * dpy, GLXDrawable drawable)
1979 {
1980 int status = GLX_BAD_CONTEXT;
1981 #ifdef __DRI_FRAME_TRACKING
1982 int screen;
1983 __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable, &screen);
1984 __GLXscreenConfigs *psc = GetGLXScreenConfigs(dpy, screen);
1985
1986 if (pdraw != NULL && psc->frameTracking != NULL)
1987 status = psc->frameTracking->frameTracking(pdraw->driDrawable,
1988 GL_FALSE);
1989 #else
1990 (void) dpy;
1991 (void) drawable;
1992 #endif
1993 return status;
1994 }
1995
1996
1997 static GLint
1998 __glXGetFrameUsageMESA(Display * dpy, GLXDrawable drawable, GLfloat * usage)
1999 {
2000 int status = GLX_BAD_CONTEXT;
2001 #ifdef __DRI_FRAME_TRACKING
2002 int screen;
2003 __GLXDRIdrawable *const pdraw = GetGLXDRIDrawable(dpy, drawable, &screen);
2004 __GLXscreenConfigs *const psc = GetGLXScreenConfigs(dpy, screen);
2005
2006 if (pdraw != NULL && psc->frameTracking != NULL) {
2007 int64_t sbc, missedFrames;
2008 float lastMissedUsage;
2009
2010 status = psc->frameTracking->queryFrameTracking(pdraw->driDrawable,
2011 &sbc,
2012 &missedFrames,
2013 &lastMissedUsage,
2014 usage);
2015 }
2016 #else
2017 (void) dpy;
2018 (void) drawable;
2019 (void) usage;
2020 #endif
2021 return status;
2022 }
2023
2024
2025 static GLint
2026 __glXQueryFrameTrackingMESA(Display * dpy, GLXDrawable drawable,
2027 int64_t * sbc, int64_t * missedFrames,
2028 GLfloat * lastMissedUsage)
2029 {
2030 int status = GLX_BAD_CONTEXT;
2031 #ifdef __DRI_FRAME_TRACKING
2032 int screen;
2033 __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable, &screen);
2034 __GLXscreenConfigs *const psc = GetGLXScreenConfigs(dpy, screen);
2035
2036 if (pdraw != NULL && psc->frameTracking != NULL) {
2037 float usage;
2038
2039 status = psc->frameTracking->queryFrameTracking(pdraw->driDrawable,
2040 sbc, missedFrames,
2041 lastMissedUsage,
2042 &usage);
2043 }
2044 #else
2045 (void) dpy;
2046 (void) drawable;
2047 (void) sbc;
2048 (void) missedFrames;
2049 (void) lastMissedUsage;
2050 #endif
2051 return status;
2052 }
2053
2054
2055 /*
2056 ** GLX_SGI_video_sync
2057 */
2058 static int
2059 __glXGetVideoSyncSGI(unsigned int *count)
2060 {
2061 /* FIXME: Looking at the GLX_SGI_video_sync spec in the extension registry,
2062 * FIXME: there should be a GLX encoding for this call. I can find no
2063 * FIXME: documentation for the GLX encoding.
2064 */
2065 #ifdef __DRI_MEDIA_STREAM_COUNTER
2066 GLXContext gc = __glXGetCurrentContext();
2067
2068
2069 if (gc != NULL && gc->driContext) {
2070 __GLXscreenConfigs *const psc = GetGLXScreenConfigs(gc->currentDpy,
2071 gc->screen);
2072 if (psc->msc && psc->driScreen) {
2073 __GLXDRIdrawable *pdraw =
2074 GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable, NULL);
2075 int64_t temp;
2076 int ret;
2077
2078 ret = (*psc->msc->getDrawableMSC) (psc->__driScreen,
2079 pdraw->driDrawable, &temp);
2080 *count = (unsigned) temp;
2081
2082 return (ret == 0) ? 0 : GLX_BAD_CONTEXT;
2083 }
2084 }
2085 #else
2086 (void) count;
2087 #endif
2088 return GLX_BAD_CONTEXT;
2089 }
2090
2091 static int
2092 __glXWaitVideoSyncSGI(int divisor, int remainder, unsigned int *count)
2093 {
2094 #ifdef __DRI_MEDIA_STREAM_COUNTER
2095 GLXContext gc = __glXGetCurrentContext();
2096
2097 if (divisor <= 0 || remainder < 0)
2098 return GLX_BAD_VALUE;
2099
2100 if (gc != NULL && gc->driContext) {
2101 __GLXscreenConfigs *const psc = GetGLXScreenConfigs(gc->currentDpy,
2102 gc->screen);
2103 if (psc->msc != NULL && psc->driScreen) {
2104 __GLXDRIdrawable *pdraw =
2105 GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable, NULL);
2106 int ret;
2107 int64_t msc;
2108 int64_t sbc;
2109
2110 ret = (*psc->msc->waitForMSC) (pdraw->driDrawable, 0,
2111 divisor, remainder, &msc, &sbc);
2112 *count = (unsigned) msc;
2113 return (ret == 0) ? 0 : GLX_BAD_CONTEXT;
2114 }
2115 }
2116 #else
2117 (void) count;
2118 #endif
2119 return GLX_BAD_CONTEXT;
2120 }
2121
2122
2123 /*
2124 ** GLX_SGIX_fbconfig
2125 ** Many of these functions are aliased to GLX 1.3 entry points in the
2126 ** GLX_functions table.
2127 */
2128
2129 PUBLIC
2130 GLX_ALIAS(int, glXGetFBConfigAttribSGIX,
2131 (Display * dpy, GLXFBConfigSGIX config, int attribute, int *value),
2132 (dpy, config, attribute, value), glXGetFBConfigAttrib)
2133
2134 PUBLIC GLX_ALIAS(GLXFBConfigSGIX *, glXChooseFBConfigSGIX,
2135 (Display * dpy, int screen, int *attrib_list,
2136 int *nelements), (dpy, screen, attrib_list, nelements),
2137 glXChooseFBConfig)
2138
2139 PUBLIC GLX_ALIAS(XVisualInfo *, glXGetVisualFromFBConfigSGIX,
2140 (Display * dpy, GLXFBConfigSGIX config),
2141 (dpy, config), glXGetVisualFromFBConfig)
2142
2143 PUBLIC GLXPixmap glXCreateGLXPixmapWithConfigSGIX(Display * dpy,
2144 GLXFBConfigSGIX config,
2145 Pixmap pixmap)
2146 {
2147 xGLXVendorPrivateWithReplyReq *vpreq;
2148 xGLXCreateGLXPixmapWithConfigSGIXReq *req;
2149 GLXPixmap xid = None;
2150 CARD8 opcode;
2151 const __GLcontextModes *const fbconfig = (__GLcontextModes *) config;
2152 __GLXscreenConfigs *psc;
2153
2154
2155 if ((dpy == NULL) || (config == NULL)) {
2156 return None;
2157 }
2158
2159 psc = GetGLXScreenConfigs(dpy, fbconfig->screen);
2160 if ((psc != NULL)
2161 && __glXExtensionBitIsEnabled(psc, SGIX_fbconfig_bit)) {
2162 opcode = __glXSetupForCommand(dpy);
2163 if (!opcode) {
2164 return None;
2165 }
2166
2167 /* Send the glXCreateGLXPixmapWithConfigSGIX request */
2168 LockDisplay(dpy);
2169 GetReqExtra(GLXVendorPrivateWithReply,
2170 sz_xGLXCreateGLXPixmapWithConfigSGIXReq -
2171 sz_xGLXVendorPrivateWithReplyReq, vpreq);
2172 req = (xGLXCreateGLXPixmapWithConfigSGIXReq *) vpreq;
2173 req->reqType = opcode;
2174 req->glxCode = X_GLXVendorPrivateWithReply;
2175 req->vendorCode = X_GLXvop_CreateGLXPixmapWithConfigSGIX;
2176 req->screen = fbconfig->screen;
2177 req->fbconfig = fbconfig->fbconfigID;
2178 req->pixmap = pixmap;
2179 req->glxpixmap = xid = XAllocID(dpy);
2180 UnlockDisplay(dpy);
2181 SyncHandle();
2182 }
2183
2184 return xid;
2185 }
2186
2187 PUBLIC GLXContext
2188 glXCreateContextWithConfigSGIX(Display * dpy,
2189 GLXFBConfigSGIX config, int renderType,
2190 GLXContext shareList, Bool allowDirect)
2191 {
2192 GLXContext gc = NULL;
2193 const __GLcontextModes *const fbconfig = (__GLcontextModes *) config;
2194 __GLXscreenConfigs *psc;
2195
2196
2197 if ((dpy == NULL) || (config == NULL)) {
2198 return None;
2199 }
2200
2201 psc = GetGLXScreenConfigs(dpy, fbconfig->screen);
2202 if ((psc != NULL)
2203 && __glXExtensionBitIsEnabled(psc, SGIX_fbconfig_bit)) {
2204 gc = CreateContext(dpy, NULL, (__GLcontextModes *) config, shareList,
2205 allowDirect, None, False, renderType);
2206 }
2207
2208 return gc;
2209 }
2210
2211
2212 PUBLIC GLXFBConfigSGIX
2213 glXGetFBConfigFromVisualSGIX(Display * dpy, XVisualInfo * vis)
2214 {
2215 __GLXdisplayPrivate *priv;
2216 __GLXscreenConfigs *psc;
2217
2218 if ((GetGLXPrivScreenConfig(dpy, vis->screen, &priv, &psc) != Success)
2219 && __glXExtensionBitIsEnabled(psc, SGIX_fbconfig_bit)
2220 && (psc->configs->fbconfigID != GLX_DONT_CARE)) {
2221 return (GLXFBConfigSGIX) _gl_context_modes_find_visual(psc->configs,
2222 vis->visualid);
2223 }
2224
2225 return NULL;
2226 }
2227
2228
2229 /*
2230 ** GLX_SGIX_swap_group
2231 */
2232 static void
2233 __glXJoinSwapGroupSGIX(Display * dpy, GLXDrawable drawable,
2234 GLXDrawable member)
2235 {
2236 (void) dpy;
2237 (void) drawable;
2238 (void) member;
2239 }
2240
2241
2242 /*
2243 ** GLX_SGIX_swap_barrier
2244 */
2245 static void
2246 __glXBindSwapBarrierSGIX(Display * dpy, GLXDrawable drawable, int barrier)
2247 {
2248 (void) dpy;
2249 (void) drawable;
2250 (void) barrier;
2251 }
2252
2253 static Bool
2254 __glXQueryMaxSwapBarriersSGIX(Display * dpy, int screen, int *max)
2255 {
2256 (void) dpy;
2257 (void) screen;
2258 (void) max;
2259 return False;
2260 }
2261
2262
2263 /*
2264 ** GLX_OML_sync_control
2265 */
2266 static Bool
2267 __glXGetSyncValuesOML(Display * dpy, GLXDrawable drawable,
2268 int64_t * ust, int64_t * msc, int64_t * sbc)
2269 {
2270 #if defined(__DRI_SWAP_BUFFER_COUNTER) && defined(__DRI_MEDIA_STREAM_COUNTER)
2271 __GLXdisplayPrivate *const priv = __glXInitialize(dpy);
2272
2273 if (priv != NULL) {
2274 int i;
2275 __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable, &i);
2276 __GLXscreenConfigs *const psc = &priv->screenConfigs[i];
2277
2278 assert((pdraw == NULL) || (i != -1));
2279 return ((pdraw && psc->sbc && psc->msc)
2280 && ((*psc->msc->getMSC) (psc->driScreen, msc) == 0)
2281 && ((*psc->sbc->getSBC) (pdraw->driDrawable, sbc) == 0)
2282 && (__glXGetUST(ust) == 0));
2283 }
2284 #else
2285 (void) dpy;
2286 (void) drawable;
2287 (void) ust;
2288 (void) msc;
2289 (void) sbc;
2290 #endif
2291 return False;
2292 }
2293
2294 #ifdef GLX_DIRECT_RENDERING
2295 _X_HIDDEN GLboolean
2296 __driGetMscRateOML(__DRIdrawable * draw,
2297 int32_t * numerator, int32_t * denominator, void *private)
2298 {
2299 #ifdef XF86VIDMODE
2300 __GLXscreenConfigs *psc;
2301 XF86VidModeModeLine mode_line;
2302 int dot_clock;
2303 int i;
2304 __GLXDRIdrawable *glxDraw = private;
2305
2306 psc = glxDraw->psc;
2307 if (XF86VidModeQueryVersion(psc->dpy, &i, &i) &&
2308 XF86VidModeGetModeLine(psc->dpy, psc->scr, &dot_clock, &mode_line)) {
2309 unsigned n = dot_clock * 1000;
2310 unsigned d = mode_line.vtotal * mode_line.htotal;
2311
2312 # define V_INTERLACE 0x010
2313 # define V_DBLSCAN 0x020
2314
2315 if (mode_line.flags & V_INTERLACE)
2316 n *= 2;
2317 else if (mode_line.flags & V_DBLSCAN)
2318 d *= 2;
2319
2320 /* The OML_sync_control spec requires that if the refresh rate is a
2321 * whole number, that the returned numerator be equal to the refresh
2322 * rate and the denominator be 1.
2323 */
2324
2325 if (n % d == 0) {
2326 n /= d;
2327 d = 1;
2328 }
2329 else {
2330 static const unsigned f[] = { 13, 11, 7, 5, 3, 2, 0 };
2331
2332 /* This is a poor man's way to reduce a fraction. It's far from
2333 * perfect, but it will work well enough for this situation.
2334 */
2335
2336 for (i = 0; f[i] != 0; i++) {
2337 while (n % f[i] == 0 && d % f[i] == 0) {
2338 d /= f[i];
2339 n /= f[i];
2340 }
2341 }
2342 }
2343
2344 *numerator = n;
2345 *denominator = d;
2346
2347 return True;
2348 }
2349 else
2350 return False;
2351 #else
2352 return False;
2353 #endif
2354 }
2355 #endif
2356
2357 /**
2358 * Determine the refresh rate of the specified drawable and display.
2359 *
2360 * \param dpy Display whose refresh rate is to be determined.
2361 * \param drawable Drawable whose refresh rate is to be determined.
2362 * \param numerator Numerator of the refresh rate.
2363 * \param demoninator Denominator of the refresh rate.
2364 * \return If the refresh rate for the specified display and drawable could
2365 * be calculated, True is returned. Otherwise False is returned.
2366 *
2367 * \note This function is implemented entirely client-side. A lot of other
2368 * functionality is required to export GLX_OML_sync_control, so on
2369 * XFree86 this function can be called for direct-rendering contexts
2370 * when GLX_OML_sync_control appears in the client extension string.
2371 */
2372
2373 _X_HIDDEN GLboolean
2374 __glXGetMscRateOML(Display * dpy, GLXDrawable drawable,
2375 int32_t * numerator, int32_t * denominator)
2376 {
2377 #if defined( GLX_DIRECT_RENDERING ) && defined( XF86VIDMODE )
2378 __GLXDRIdrawable *draw = GetGLXDRIDrawable(dpy, drawable, NULL);
2379
2380 if (draw == NULL)
2381 return False;
2382
2383 return __driGetMscRateOML(draw->driDrawable, numerator, denominator, draw);
2384 #else
2385 (void) dpy;
2386 (void) drawable;
2387 (void) numerator;
2388 (void) denominator;
2389 #endif
2390 return False;
2391 }
2392
2393
2394 static int64_t
2395 __glXSwapBuffersMscOML(Display * dpy, GLXDrawable drawable,
2396 int64_t target_msc, int64_t divisor, int64_t remainder)
2397 {
2398 #ifdef __DRI_SWAP_BUFFER_COUNTER
2399 int screen;
2400 __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable, &screen);
2401 __GLXscreenConfigs *const psc = GetGLXScreenConfigs(dpy, screen);
2402
2403 /* The OML_sync_control spec says these should "generate a GLX_BAD_VALUE
2404 * error", but it also says "It [glXSwapBuffersMscOML] will return a value
2405 * of -1 if the function failed because of errors detected in the input
2406 * parameters"
2407 */
2408 if (divisor < 0 || remainder < 0 || target_msc < 0)
2409 return -1;
2410 if (divisor > 0 && remainder >= divisor)
2411 return -1;
2412
2413 if (pdraw != NULL && psc->counters != NULL)
2414 return (*psc->sbc->swapBuffersMSC) (pdraw->driDrawable, target_msc,
2415 divisor, remainder);
2416
2417 #else
2418 (void) dpy;
2419 (void) drawable;
2420 (void) target_msc;
2421 (void) divisor;
2422 (void) remainder;
2423 #endif
2424 return 0;
2425 }
2426
2427
2428 static Bool
2429 __glXWaitForMscOML(Display * dpy, GLXDrawable drawable,
2430 int64_t target_msc, int64_t divisor,
2431 int64_t remainder, int64_t * ust,
2432 int64_t * msc, int64_t * sbc)
2433 {
2434 #ifdef __DRI_MEDIA_STREAM_COUNTER
2435 int screen;
2436 __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable, &screen);
2437 __GLXscreenConfigs *const psc = GetGLXScreenConfigs(dpy, screen);
2438 int ret;
2439
2440 /* The OML_sync_control spec says these should "generate a GLX_BAD_VALUE
2441 * error", but the return type in the spec is Bool.
2442 */
2443 if (divisor < 0 || remainder < 0 || target_msc < 0)
2444 return False;
2445 if (divisor > 0 && remainder >= divisor)
2446 return False;
2447
2448 if (pdraw != NULL && psc->msc != NULL) {
2449 ret = (*psc->msc->waitForMSC) (pdraw->driDrawable, target_msc,
2450 divisor, remainder, msc, sbc);
2451
2452 /* __glXGetUST returns zero on success and non-zero on failure.
2453 * This function returns True on success and False on failure.
2454 */
2455 return ((ret == 0) && (__glXGetUST(ust) == 0));
2456 }
2457 #else
2458 (void) dpy;
2459 (void) drawable;
2460 (void) target_msc;
2461 (void) divisor;
2462 (void) remainder;
2463 (void) ust;
2464 (void) msc;
2465 (void) sbc;
2466 #endif
2467 return False;
2468 }
2469
2470
2471 static Bool
2472 __glXWaitForSbcOML(Display * dpy, GLXDrawable drawable,
2473 int64_t target_sbc, int64_t * ust,
2474 int64_t * msc, int64_t * sbc)
2475 {
2476 #ifdef __DRI_SWAP_BUFFER_COUNTER
2477 int screen;
2478 __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable, &screen);
2479 __GLXscreenConfigs *const psc = GetGLXScreenConfigs(dpy, screen);
2480 int ret;
2481
2482 /* The OML_sync_control spec says this should "generate a GLX_BAD_VALUE
2483 * error", but the return type in the spec is Bool.
2484 */
2485 if (target_sbc < 0)
2486 return False;
2487
2488 if (pdraw != NULL && psc->sbc != NULL) {
2489 ret =
2490 (*psc->sbc->waitForSBC) (pdraw->driDrawable, target_sbc, msc, sbc);
2491
2492 /* __glXGetUST returns zero on success and non-zero on failure.
2493 * This function returns True on success and False on failure.
2494 */
2495 return ((ret == 0) && (__glXGetUST(ust) == 0));
2496 }
2497 #else
2498 (void) dpy;
2499 (void) drawable;
2500 (void) target_sbc;
2501 (void) ust;
2502 (void) msc;
2503 (void) sbc;
2504 #endif
2505 return False;
2506 }
2507
2508
2509 /**
2510 * GLX_MESA_allocate_memory
2511 */
2512 /*@{*/
2513
2514 PUBLIC void *
2515 glXAllocateMemoryMESA(Display * dpy, int scrn,
2516 size_t size, float readFreq,
2517 float writeFreq, float priority)
2518 {
2519 #ifdef __DRI_ALLOCATE
2520 __GLXscreenConfigs *const psc = GetGLXScreenConfigs(dpy, scrn);
2521
2522 if (psc && psc->allocate)
2523 return (*psc->allocate->allocateMemory) (psc->__driScreen, size,
2524 readFreq, writeFreq, priority);
2525
2526 #else
2527 (void) dpy;
2528 (void) scrn;
2529 (void) size;
2530 (void) readFreq;
2531 (void) writeFreq;
2532 (void) priority;
2533 #endif /* GLX_DIRECT_RENDERING */
2534
2535 return NULL;
2536 }
2537
2538
2539 PUBLIC void
2540 glXFreeMemoryMESA(Display * dpy, int scrn, void *pointer)
2541 {
2542 #ifdef __DRI_ALLOCATE
2543 __GLXscreenConfigs *const psc = GetGLXScreenConfigs(dpy, scrn);
2544
2545 if (psc && psc->allocate)
2546 (*psc->allocate->freeMemory) (psc->__driScreen, pointer);
2547
2548 #else
2549 (void) dpy;
2550 (void) scrn;
2551 (void) pointer;
2552 #endif /* GLX_DIRECT_RENDERING */
2553 }
2554
2555
2556 PUBLIC GLuint
2557 glXGetMemoryOffsetMESA(Display * dpy, int scrn, const void *pointer)
2558 {
2559 #ifdef __DRI_ALLOCATE
2560 __GLXscreenConfigs *const psc = GetGLXScreenConfigs(dpy, scrn);
2561
2562 if (psc && psc->allocate)
2563 return (*psc->allocate->memoryOffset) (psc->__driScreen, pointer);
2564
2565 #else
2566 (void) dpy;
2567 (void) scrn;
2568 (void) pointer;
2569 #endif /* GLX_DIRECT_RENDERING */
2570
2571 return ~0L;
2572 }
2573
2574 /*@}*/
2575
2576
2577 /**
2578 * Mesa extension stubs. These will help reduce portability problems.
2579 */
2580 /*@{*/
2581
2582 /**
2583 * Release all buffers associated with the specified GLX drawable.
2584 *
2585 * \todo
2586 * This function was intended for stand-alone Mesa. The issue there is that
2587 * the library doesn't get any notification when a window is closed. In
2588 * DRI there is a similar but slightly different issue. When GLX 1.3 is
2589 * supported, there are 3 different functions to destroy a drawable. It
2590 * should be possible to create GLX protocol (or have it determine which
2591 * protocol to use based on the type of the drawable) to have one function
2592 * do the work of 3. For the direct-rendering case, this function could
2593 * just call the driver's \c __DRIdrawableRec::destroyDrawable function.
2594 * This would reduce the frequency with which \c __driGarbageCollectDrawables
2595 * would need to be used. This really should be done as part of the new DRI
2596 * interface work.
2597 *
2598 * \sa http://oss.sgi.com/projects/ogl-sample/registry/MESA/release_buffers.txt
2599 * __driGarbageCollectDrawables
2600 * glXDestroyGLXPixmap
2601 * glXDestroyPbuffer glXDestroyPixmap glXDestroyWindow
2602 * glXDestroyGLXPbufferSGIX glXDestroyGLXVideoSourceSGIX
2603 */
2604 static Bool
2605 __glXReleaseBuffersMESA(Display * dpy, GLXDrawable d)
2606 {
2607 (void) dpy;
2608 (void) d;
2609 return False;
2610 }
2611
2612
2613 PUBLIC GLXPixmap
2614 glXCreateGLXPixmapMESA(Display * dpy, XVisualInfo * visual,
2615 Pixmap pixmap, Colormap cmap)
2616 {
2617 (void) dpy;
2618 (void) visual;
2619 (void) pixmap;
2620 (void) cmap;
2621 return 0;
2622 }
2623
2624 /*@}*/
2625
2626
2627 /**
2628 * GLX_MESA_copy_sub_buffer
2629 */
2630 #define X_GLXvop_CopySubBufferMESA 5154 /* temporary */
2631 static void
2632 __glXCopySubBufferMESA(Display * dpy, GLXDrawable drawable,
2633 int x, int y, int width, int height)
2634 {
2635 xGLXVendorPrivateReq *req;
2636 GLXContext gc;
2637 GLXContextTag tag;
2638 CARD32 *drawable_ptr;
2639 INT32 *x_ptr, *y_ptr, *w_ptr, *h_ptr;
2640 CARD8 opcode;
2641
2642 #ifdef __DRI_COPY_SUB_BUFFER
2643 int screen;
2644 __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable, &screen);
2645 if (pdraw != NULL) {
2646 __GLXscreenConfigs *const psc = GetGLXScreenConfigs(dpy, screen);
2647 if (psc->driScreen->copySubBuffer != NULL) {
2648 glFlush();
2649 (*psc->driScreen->copySubBuffer) (pdraw, x, y, width, height);
2650 }
2651
2652 return;
2653 }
2654 #endif
2655
2656 opcode = __glXSetupForCommand(dpy);
2657 if (!opcode)
2658 return;
2659
2660 /*
2661 ** The calling thread may or may not have a current context. If it
2662 ** does, send the context tag so the server can do a flush.
2663 */
2664 gc = __glXGetCurrentContext();
2665 if ((gc != NULL) && (dpy == gc->currentDpy) &&
2666 ((drawable == gc->currentDrawable) ||
2667 (drawable == gc->currentReadable))) {
2668 tag = gc->currentContextTag;
2669 }
2670 else {
2671 tag = 0;
2672 }
2673
2674 LockDisplay(dpy);
2675 GetReqExtra(GLXVendorPrivate, sizeof(CARD32) + sizeof(INT32) * 4, req);
2676 req->reqType = opcode;
2677 req->glxCode = X_GLXVendorPrivate;
2678 req->vendorCode = X_GLXvop_CopySubBufferMESA;
2679 req->contextTag = tag;
2680
2681 drawable_ptr = (CARD32 *) (req + 1);
2682 x_ptr = (INT32 *) (drawable_ptr + 1);
2683 y_ptr = (INT32 *) (drawable_ptr + 2);
2684 w_ptr = (INT32 *) (drawable_ptr + 3);
2685 h_ptr = (INT32 *) (drawable_ptr + 4);
2686
2687 *drawable_ptr = drawable;
2688 *x_ptr = x;
2689 *y_ptr = y;
2690 *w_ptr = width;
2691 *h_ptr = height;
2692
2693 UnlockDisplay(dpy);
2694 SyncHandle();
2695 }
2696
2697
2698 /**
2699 * GLX_EXT_texture_from_pixmap
2700 */
2701 /*@{*/
2702 static void
2703 __glXBindTexImageEXT(Display * dpy,
2704 GLXDrawable drawable, int buffer, const int *attrib_list)
2705 {
2706 xGLXVendorPrivateReq *req;
2707 GLXContext gc = __glXGetCurrentContext();
2708 CARD32 *drawable_ptr;
2709 INT32 *buffer_ptr;
2710 CARD32 *num_attrib_ptr;
2711 CARD32 *attrib_ptr;
2712 CARD8 opcode;
2713 unsigned int i;
2714
2715 if (gc == NULL)
2716 return;
2717
2718 i = 0;
2719 if (attrib_list) {
2720 while (attrib_list[i * 2] != None)
2721 i++;
2722 }
2723
2724 #ifdef GLX_DIRECT_RENDERING
2725 if (gc->driContext) {
2726 __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable, NULL);
2727
2728 if (pdraw != NULL) {
2729 if (pdraw->psc->texBuffer->base.version >= 2 &&
2730 pdraw->psc->texBuffer->setTexBuffer2 != NULL) {
2731 (*pdraw->psc->texBuffer->setTexBuffer2) (gc->__driContext,
2732 pdraw->textureTarget,
2733 pdraw->textureFormat,
2734 pdraw->driDrawable);
2735 }
2736 else {
2737 (*pdraw->psc->texBuffer->setTexBuffer) (gc->__driContext,
2738 pdraw->textureTarget,
2739 pdraw->driDrawable);
2740 }
2741 }
2742 return;
2743 }
2744 #endif
2745
2746 opcode = __glXSetupForCommand(dpy);
2747 if (!opcode)
2748 return;
2749
2750 LockDisplay(dpy);
2751 GetReqExtra(GLXVendorPrivate, 12 + 8 * i, req);
2752 req->reqType = opcode;
2753 req->glxCode = X_GLXVendorPrivate;
2754 req->vendorCode = X_GLXvop_BindTexImageEXT;
2755 req->contextTag = gc->currentContextTag;
2756
2757 drawable_ptr = (CARD32 *) (req + 1);
2758 buffer_ptr = (INT32 *) (drawable_ptr + 1);
2759 num_attrib_ptr = (CARD32 *) (buffer_ptr + 1);
2760 attrib_ptr = (CARD32 *) (num_attrib_ptr + 1);
2761
2762 *drawable_ptr = drawable;
2763 *buffer_ptr = buffer;
2764 *num_attrib_ptr = (CARD32) i;
2765
2766 i = 0;
2767 if (attrib_list) {
2768 while (attrib_list[i * 2] != None) {
2769 *attrib_ptr++ = (CARD32) attrib_list[i * 2 + 0];
2770 *attrib_ptr++ = (CARD32) attrib_list[i * 2 + 1];
2771 i++;
2772 }
2773 }
2774
2775 UnlockDisplay(dpy);
2776 SyncHandle();
2777 }
2778
2779 static void
2780 __glXReleaseTexImageEXT(Display * dpy, GLXDrawable drawable, int buffer)
2781 {
2782 xGLXVendorPrivateReq *req;
2783 GLXContext gc = __glXGetCurrentContext();
2784 CARD32 *drawable_ptr;
2785 INT32 *buffer_ptr;
2786 CARD8 opcode;
2787
2788 if (gc == NULL)
2789 return;
2790
2791 #ifdef GLX_DIRECT_RENDERING
2792 if (gc->driContext)
2793 return;
2794 #endif
2795
2796 opcode = __glXSetupForCommand(dpy);
2797 if (!opcode)
2798 return;
2799
2800 LockDisplay(dpy);
2801 GetReqExtra(GLXVendorPrivate, sizeof(CARD32) + sizeof(INT32), req);
2802 req->reqType = opcode;
2803 req->glxCode = X_GLXVendorPrivate;
2804 req->vendorCode = X_GLXvop_ReleaseTexImageEXT;
2805 req->contextTag = gc->currentContextTag;
2806
2807 drawable_ptr = (CARD32 *) (req + 1);
2808 buffer_ptr = (INT32 *) (drawable_ptr + 1);
2809
2810 *drawable_ptr = drawable;
2811 *buffer_ptr = buffer;
2812
2813 UnlockDisplay(dpy);
2814 SyncHandle();
2815 }
2816
2817 /*@}*/
2818
2819 /**
2820 * \c strdup is actually not a standard ANSI C or POSIX routine.
2821 * Irix will not define it if ANSI mode is in effect.
2822 *
2823 * \sa strdup
2824 */
2825 _X_HIDDEN char *
2826 __glXstrdup(const char *str)
2827 {
2828 char *copy;
2829 copy = (char *) Xmalloc(strlen(str) + 1);
2830 if (!copy)
2831 return NULL;
2832 strcpy(copy, str);
2833 return copy;
2834 }
2835
2836 /*
2837 ** glXGetProcAddress support
2838 */
2839
2840 struct name_address_pair
2841 {
2842 const char *Name;
2843 GLvoid *Address;
2844 };
2845
2846 #define GLX_FUNCTION(f) { # f, (GLvoid *) f }
2847 #define GLX_FUNCTION2(n,f) { # n, (GLvoid *) f }
2848
2849 static const struct name_address_pair GLX_functions[] = {
2850 /*** GLX_VERSION_1_0 ***/
2851 GLX_FUNCTION(glXChooseVisual),
2852 GLX_FUNCTION(glXCopyContext),
2853 GLX_FUNCTION(glXCreateContext),
2854 GLX_FUNCTION(glXCreateGLXPixmap),
2855 GLX_FUNCTION(glXDestroyContext),
2856 GLX_FUNCTION(glXDestroyGLXPixmap),
2857 GLX_FUNCTION(glXGetConfig),
2858 GLX_FUNCTION(glXGetCurrentContext),
2859 GLX_FUNCTION(glXGetCurrentDrawable),
2860 GLX_FUNCTION(glXIsDirect),
2861 GLX_FUNCTION(glXMakeCurrent),
2862 GLX_FUNCTION(glXQueryExtension),
2863 GLX_FUNCTION(glXQueryVersion),
2864 GLX_FUNCTION(glXSwapBuffers),
2865 GLX_FUNCTION(glXUseXFont),
2866 GLX_FUNCTION(glXWaitGL),
2867 GLX_FUNCTION(glXWaitX),
2868
2869 /*** GLX_VERSION_1_1 ***/
2870 GLX_FUNCTION(glXGetClientString),
2871 GLX_FUNCTION(glXQueryExtensionsString),
2872 GLX_FUNCTION(glXQueryServerString),
2873
2874 /*** GLX_VERSION_1_2 ***/
2875 GLX_FUNCTION(glXGetCurrentDisplay),
2876
2877 /*** GLX_VERSION_1_3 ***/
2878 GLX_FUNCTION(glXChooseFBConfig),
2879 GLX_FUNCTION(glXCreateNewContext),
2880 GLX_FUNCTION(glXCreatePbuffer),
2881 GLX_FUNCTION(glXCreatePixmap),
2882 GLX_FUNCTION(glXCreateWindow),
2883 GLX_FUNCTION(glXDestroyPbuffer),
2884 GLX_FUNCTION(glXDestroyPixmap),
2885 GLX_FUNCTION(glXDestroyWindow),
2886 GLX_FUNCTION(glXGetCurrentReadDrawable),
2887 GLX_FUNCTION(glXGetFBConfigAttrib),
2888 GLX_FUNCTION(glXGetFBConfigs),
2889 GLX_FUNCTION(glXGetSelectedEvent),
2890 GLX_FUNCTION(glXGetVisualFromFBConfig),
2891 GLX_FUNCTION(glXMakeContextCurrent),
2892 GLX_FUNCTION(glXQueryContext),
2893 GLX_FUNCTION(glXQueryDrawable),
2894 GLX_FUNCTION(glXSelectEvent),
2895
2896 /*** GLX_SGI_swap_control ***/
2897 GLX_FUNCTION2(glXSwapIntervalSGI, __glXSwapIntervalSGI),
2898
2899 /*** GLX_SGI_video_sync ***/
2900 GLX_FUNCTION2(glXGetVideoSyncSGI, __glXGetVideoSyncSGI),
2901 GLX_FUNCTION2(glXWaitVideoSyncSGI, __glXWaitVideoSyncSGI),
2902
2903 /*** GLX_SGI_make_current_read ***/
2904 GLX_FUNCTION2(glXMakeCurrentReadSGI, glXMakeContextCurrent),
2905 GLX_FUNCTION2(glXGetCurrentReadDrawableSGI, glXGetCurrentReadDrawable),
2906
2907 /*** GLX_EXT_import_context ***/
2908 GLX_FUNCTION(glXFreeContextEXT),
2909 GLX_FUNCTION(glXGetContextIDEXT),
2910 GLX_FUNCTION2(glXGetCurrentDisplayEXT, glXGetCurrentDisplay),
2911 GLX_FUNCTION(glXImportContextEXT),
2912 GLX_FUNCTION2(glXQueryContextInfoEXT, glXQueryContext),
2913
2914 /*** GLX_SGIX_fbconfig ***/
2915 GLX_FUNCTION2(glXGetFBConfigAttribSGIX, glXGetFBConfigAttrib),
2916 GLX_FUNCTION2(glXChooseFBConfigSGIX, glXChooseFBConfig),
2917 GLX_FUNCTION(glXCreateGLXPixmapWithConfigSGIX),
2918 GLX_FUNCTION(glXCreateContextWithConfigSGIX),
2919 GLX_FUNCTION2(glXGetVisualFromFBConfigSGIX, glXGetVisualFromFBConfig),
2920 GLX_FUNCTION(glXGetFBConfigFromVisualSGIX),
2921
2922 /*** GLX_SGIX_pbuffer ***/
2923 GLX_FUNCTION(glXCreateGLXPbufferSGIX),
2924 GLX_FUNCTION(glXDestroyGLXPbufferSGIX),
2925 GLX_FUNCTION(glXQueryGLXPbufferSGIX),
2926 GLX_FUNCTION(glXSelectEventSGIX),
2927 GLX_FUNCTION(glXGetSelectedEventSGIX),
2928
2929 /*** GLX_SGIX_swap_group ***/
2930 GLX_FUNCTION2(glXJoinSwapGroupSGIX, __glXJoinSwapGroupSGIX),
2931
2932 /*** GLX_SGIX_swap_barrier ***/
2933 GLX_FUNCTION2(glXBindSwapBarrierSGIX, __glXBindSwapBarrierSGIX),
2934 GLX_FUNCTION2(glXQueryMaxSwapBarriersSGIX, __glXQueryMaxSwapBarriersSGIX),
2935
2936 /*** GLX_MESA_allocate_memory ***/
2937 GLX_FUNCTION(glXAllocateMemoryMESA),
2938 GLX_FUNCTION(glXFreeMemoryMESA),
2939 GLX_FUNCTION(glXGetMemoryOffsetMESA),
2940
2941 /*** GLX_MESA_copy_sub_buffer ***/
2942 GLX_FUNCTION2(glXCopySubBufferMESA, __glXCopySubBufferMESA),
2943
2944 /*** GLX_MESA_pixmap_colormap ***/
2945 GLX_FUNCTION(glXCreateGLXPixmapMESA),
2946
2947 /*** GLX_MESA_release_buffers ***/
2948 GLX_FUNCTION2(glXReleaseBuffersMESA, __glXReleaseBuffersMESA),
2949
2950 /*** GLX_MESA_swap_control ***/
2951 GLX_FUNCTION2(glXSwapIntervalMESA, __glXSwapIntervalMESA),
2952 GLX_FUNCTION2(glXGetSwapIntervalMESA, __glXGetSwapIntervalMESA),
2953
2954 /*** GLX_MESA_swap_frame_usage ***/
2955 GLX_FUNCTION2(glXBeginFrameTrackingMESA, __glXBeginFrameTrackingMESA),
2956 GLX_FUNCTION2(glXEndFrameTrackingMESA, __glXEndFrameTrackingMESA),
2957 GLX_FUNCTION2(glXGetFrameUsageMESA, __glXGetFrameUsageMESA),
2958 GLX_FUNCTION2(glXQueryFrameTrackingMESA, __glXQueryFrameTrackingMESA),
2959
2960 /*** GLX_ARB_get_proc_address ***/
2961 GLX_FUNCTION(glXGetProcAddressARB),
2962
2963 /*** GLX 1.4 ***/
2964 GLX_FUNCTION2(glXGetProcAddress, glXGetProcAddressARB),
2965
2966 /*** GLX_OML_sync_control ***/
2967 GLX_FUNCTION2(glXWaitForSbcOML, __glXWaitForSbcOML),
2968 GLX_FUNCTION2(glXWaitForMscOML, __glXWaitForMscOML),
2969 GLX_FUNCTION2(glXSwapBuffersMscOML, __glXSwapBuffersMscOML),
2970 GLX_FUNCTION2(glXGetMscRateOML, __glXGetMscRateOML),
2971 GLX_FUNCTION2(glXGetSyncValuesOML, __glXGetSyncValuesOML),
2972
2973 /*** GLX_EXT_texture_from_pixmap ***/
2974 GLX_FUNCTION2(glXBindTexImageEXT, __glXBindTexImageEXT),
2975 GLX_FUNCTION2(glXReleaseTexImageEXT, __glXReleaseTexImageEXT),
2976
2977 #ifdef GLX_DIRECT_RENDERING
2978 /*** DRI configuration ***/
2979 GLX_FUNCTION(glXGetScreenDriver),
2980 GLX_FUNCTION(glXGetDriverConfig),
2981 #endif
2982
2983 {NULL, NULL} /* end of list */
2984 };
2985
2986
2987 static const GLvoid *
2988 get_glx_proc_address(const char *funcName)
2989 {
2990 GLuint i;
2991
2992 /* try static functions */
2993 for (i = 0; GLX_functions[i].Name; i++) {
2994 if (strcmp(GLX_functions[i].Name, funcName) == 0)
2995 return GLX_functions[i].Address;
2996 }
2997
2998 return NULL;
2999 }
3000
3001
3002 /**
3003 * Get the address of a named GL function. This is the pre-GLX 1.4 name for
3004 * \c glXGetProcAddress.
3005 *
3006 * \param procName Name of a GL or GLX function.
3007 * \returns A pointer to the named function
3008 *
3009 * \sa glXGetProcAddress
3010 */
3011 PUBLIC void (*glXGetProcAddressARB(const GLubyte * procName)) (void)
3012 {
3013 typedef void (*gl_function) (void);
3014 gl_function f;
3015
3016
3017 /* Search the table of GLX and internal functions first. If that
3018 * fails and the supplied name could be a valid core GL name, try
3019 * searching the core GL function table. This check is done to prevent
3020 * DRI based drivers from searching the core GL function table for
3021 * internal API functions.
3022 */
3023
3024 f = (gl_function) get_glx_proc_address((const char *) procName);
3025 if ((f == NULL) && (procName[0] == 'g') && (procName[1] == 'l')
3026 && (procName[2] != 'X')) {
3027 f = (gl_function) _glapi_get_proc_address((const char *) procName);
3028 }
3029
3030 return f;
3031 }
3032
3033 /**
3034 * Get the address of a named GL function. This is the GLX 1.4 name for
3035 * \c glXGetProcAddressARB.
3036 *
3037 * \param procName Name of a GL or GLX function.
3038 * \returns A pointer to the named function
3039 *
3040 * \sa glXGetProcAddressARB
3041 */
3042 PUBLIC void (*glXGetProcAddress(const GLubyte * procName)) (void)
3043 #if defined(__GNUC__) && !defined(GLX_ALIAS_UNSUPPORTED)
3044 __attribute__ ((alias("glXGetProcAddressARB")));
3045 #else
3046 {
3047 return glXGetProcAddressARB(procName);
3048 }
3049 #endif /* __GNUC__ */
3050
3051
3052 #ifdef GLX_DIRECT_RENDERING
3053 /**
3054 * Get the unadjusted system time (UST). Currently, the UST is measured in
3055 * microseconds since Epoc. The actual resolution of the UST may vary from
3056 * system to system, and the units may vary from release to release.
3057 * Drivers should not call this function directly. They should instead use
3058 * \c glXGetProcAddress to obtain a pointer to the function.
3059 *
3060 * \param ust Location to store the 64-bit UST
3061 * \returns Zero on success or a negative errno value on failure.
3062 *
3063 * \sa glXGetProcAddress, PFNGLXGETUSTPROC
3064 *
3065 * \since Internal API version 20030317.
3066 */
3067 _X_HIDDEN int
3068 __glXGetUST(int64_t * ust)
3069 {
3070 struct timeval tv;
3071
3072 if (ust == NULL) {
3073 return -EFAULT;
3074 }
3075
3076 if (gettimeofday(&tv, NULL) == 0) {
3077 ust[0] = (tv.tv_sec * 1000000) + tv.tv_usec;
3078 return 0;
3079 }
3080 else {
3081 return -errno;
3082 }
3083 }
3084 #endif /* GLX_DIRECT_RENDERING */