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