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