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