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