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