Added few more stubs so that control reaches to DestroyDevice().
[mesa.git] / src / glx / glxext.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 glxext.c
33 * GLX protocol interface boot-strap code.
34 *
35 * Direct rendering support added by Precision Insight, Inc.
36 *
37 * \author Kevin E. Martin <kevin@precisioninsight.com>
38 */
39
40 #include <assert.h>
41 #include <stdbool.h>
42
43 #include "glxclient.h"
44 #include <X11/extensions/Xext.h>
45 #include <X11/extensions/extutil.h>
46 #ifdef GLX_USE_APPLEGL
47 #include "apple/apple_glx.h"
48 #include "apple/apple_visual.h"
49 #endif
50 #include "glxextensions.h"
51
52 #include "util/debug.h"
53 #ifndef GLX_USE_APPLEGL
54 #include "dri_common.h"
55 #endif
56
57 #include <X11/Xlib-xcb.h>
58 #include <xcb/xcb.h>
59 #include <xcb/glx.h>
60
61
62 #ifdef DEBUG
63 void __glXDumpDrawBuffer(struct glx_context * ctx);
64 #endif
65
66 /*
67 ** You can set this cell to 1 to force the gl drawing stuff to be
68 ** one command per packet
69 */
70 _X_HIDDEN int __glXDebug = 0;
71
72 /* Extension required boiler plate */
73
74 static const char __glXExtensionName[] = GLX_EXTENSION_NAME;
75 static struct glx_display *glx_displays;
76
77 static /* const */ char *error_list[] = {
78 "GLXBadContext",
79 "GLXBadContextState",
80 "GLXBadDrawable",
81 "GLXBadPixmap",
82 "GLXBadContextTag",
83 "GLXBadCurrentWindow",
84 "GLXBadRenderRequest",
85 "GLXBadLargeRequest",
86 "GLXUnsupportedPrivateRequest",
87 "GLXBadFBConfig",
88 "GLXBadPbuffer",
89 "GLXBadCurrentDrawable",
90 "GLXBadWindow",
91 "GLXBadProfileARB",
92 };
93
94 #ifdef GLX_USE_APPLEGL
95 static char *__glXErrorString(Display *dpy, int code, XExtCodes *codes,
96 char *buf, int n);
97 #endif
98
99 static
100 XEXT_GENERATE_ERROR_STRING(__glXErrorString, __glXExtensionName,
101 __GLX_NUMBER_ERRORS, error_list)
102
103 /*
104 * GLX events are a bit funky. We don't stuff the X event code into
105 * our user exposed (via XNextEvent) structure. Instead we use the GLX
106 * private event code namespace (and hope it doesn't conflict). Clients
107 * have to know that bit 15 in the event type field means they're getting
108 * a GLX event, and then handle the various sub-event types there, rather
109 * than simply checking the event code and handling it directly.
110 */
111
112 static Bool
113 __glXWireToEvent(Display *dpy, XEvent *event, xEvent *wire)
114 {
115 struct glx_display *glx_dpy = __glXInitialize(dpy);
116
117 if (glx_dpy == NULL)
118 return False;
119
120 switch ((wire->u.u.type & 0x7f) - glx_dpy->codes->first_event) {
121 case GLX_PbufferClobber:
122 {
123 GLXPbufferClobberEvent *aevent = (GLXPbufferClobberEvent *)event;
124 xGLXPbufferClobberEvent *awire = (xGLXPbufferClobberEvent *)wire;
125 aevent->event_type = awire->type;
126 aevent->serial = awire->sequenceNumber;
127 aevent->event_type = awire->event_type;
128 aevent->draw_type = awire->draw_type;
129 aevent->drawable = awire->drawable;
130 aevent->buffer_mask = awire->buffer_mask;
131 aevent->aux_buffer = awire->aux_buffer;
132 aevent->x = awire->x;
133 aevent->y = awire->y;
134 aevent->width = awire->width;
135 aevent->height = awire->height;
136 aevent->count = awire->count;
137 return True;
138 }
139 case GLX_BufferSwapComplete:
140 {
141 GLXBufferSwapComplete *aevent = (GLXBufferSwapComplete *)event;
142 xGLXBufferSwapComplete2 *awire = (xGLXBufferSwapComplete2 *)wire;
143 struct glx_drawable *glxDraw = GetGLXDrawable(dpy, awire->drawable);
144
145 if (!glxDraw)
146 return False;
147
148 aevent->serial = _XSetLastRequestRead(dpy, (xGenericReply *) wire);
149 aevent->send_event = (awire->type & 0x80) != 0;
150 aevent->display = dpy;
151 aevent->event_type = awire->event_type;
152 aevent->drawable = glxDraw->xDrawable;
153 aevent->ust = ((CARD64)awire->ust_hi << 32) | awire->ust_lo;
154 aevent->msc = ((CARD64)awire->msc_hi << 32) | awire->msc_lo;
155
156 /* Handle 32-Bit wire sbc wraparound in both directions to cope with out
157 * of sequence 64-Bit sbc's
158 */
159 if ((int64_t) awire->sbc < ((int64_t) glxDraw->lastEventSbc - 0x40000000))
160 glxDraw->eventSbcWrap += 0x100000000;
161 if ((int64_t) awire->sbc > ((int64_t) glxDraw->lastEventSbc + 0x40000000))
162 glxDraw->eventSbcWrap -= 0x100000000;
163 glxDraw->lastEventSbc = awire->sbc;
164 aevent->sbc = awire->sbc + glxDraw->eventSbcWrap;
165 return True;
166 }
167 default:
168 /* client doesn't support server event */
169 break;
170 }
171
172 return False;
173 }
174
175 /* We don't actually support this. It doesn't make sense for clients to
176 * send each other GLX events.
177 */
178 static Status
179 __glXEventToWire(Display *dpy, XEvent *event, xEvent *wire)
180 {
181 struct glx_display *glx_dpy = __glXInitialize(dpy);
182
183 if (glx_dpy == NULL)
184 return False;
185
186 switch (event->type) {
187 case GLX_DAMAGED:
188 break;
189 case GLX_SAVED:
190 break;
191 case GLX_EXCHANGE_COMPLETE_INTEL:
192 break;
193 case GLX_COPY_COMPLETE_INTEL:
194 break;
195 case GLX_FLIP_COMPLETE_INTEL:
196 break;
197 default:
198 /* client doesn't support server event */
199 break;
200 }
201
202 return Success;
203 }
204
205 /************************************************************************/
206 /*
207 ** Free the per screen configs data as well as the array of
208 ** __glXScreenConfigs.
209 */
210 static void
211 FreeScreenConfigs(struct glx_display * priv)
212 {
213 struct glx_screen *psc;
214 GLint i, screens;
215
216 /* Free screen configuration information */
217 screens = ScreenCount(priv->dpy);
218 for (i = 0; i < screens; i++) {
219 psc = priv->screens[i];
220 if (!psc)
221 continue;
222 glx_screen_cleanup(psc);
223
224 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
225 if (psc->driScreen) {
226 psc->driScreen->destroyScreen(psc);
227 } else {
228 free(psc);
229 }
230 #else
231 free(psc);
232 #endif
233 }
234 free((char *) priv->screens);
235 priv->screens = NULL;
236 }
237
238 static void
239 glx_display_free(struct glx_display *priv)
240 {
241 struct glx_context *gc;
242
243 gc = __glXGetCurrentContext();
244 if (priv->dpy == gc->currentDpy) {
245 gc->vtable->destroy(gc);
246 __glXSetCurrentContextNull();
247 }
248
249 FreeScreenConfigs(priv);
250 free((char *) priv->serverGLXvendor);
251 free((char *) priv->serverGLXversion);
252
253 __glxHashDestroy(priv->glXDrawHash);
254
255 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
256 __glxHashDestroy(priv->drawHash);
257
258 /* Free the direct rendering per display data */
259 if (priv->driswDisplay)
260 (*priv->driswDisplay->destroyDisplay) (priv->driswDisplay);
261 priv->driswDisplay = NULL;
262
263 #if defined (GLX_USE_DRM)
264 if (priv->driDisplay)
265 (*priv->driDisplay->destroyDisplay) (priv->driDisplay);
266 priv->driDisplay = NULL;
267
268 if (priv->dri2Display)
269 (*priv->dri2Display->destroyDisplay) (priv->dri2Display);
270 priv->dri2Display = NULL;
271
272 if (priv->dri3Display)
273 (*priv->dri3Display->destroyDisplay) (priv->dri3Display);
274 priv->dri3Display = NULL;
275 #endif /* GLX_USE_DRM */
276
277 #if defined(GLX_USE_WINDOWSGL)
278 if (priv->windowsdriDisplay)
279 (*priv->windowsdriDisplay->destroyDisplay) (priv->windowsdriDisplay);
280 priv->windowsdriDisplay = NULL;
281 #endif /* GLX_USE_WINDOWSGL */
282
283 #endif /* GLX_DIRECT_RENDERING && !GLX_USE_APPLEGL */
284
285 free((char *) priv);
286 }
287
288 static int
289 __glXCloseDisplay(Display * dpy, XExtCodes * codes)
290 {
291 struct glx_display *priv, **prev;
292
293 _XLockMutex(_Xglobal_lock);
294 prev = &glx_displays;
295 for (priv = glx_displays; priv; prev = &priv->next, priv = priv->next) {
296 if (priv->dpy == dpy) {
297 *prev = priv->next;
298 break;
299 }
300 }
301 _XUnlockMutex(_Xglobal_lock);
302
303 if (priv != NULL)
304 glx_display_free(priv);
305
306 return 1;
307 }
308
309 /*
310 ** Query the version of the GLX extension. This procedure works even if
311 ** the client extension is not completely set up.
312 */
313 static Bool
314 QueryVersion(Display * dpy, int opcode, int *major, int *minor)
315 {
316 xcb_connection_t *c = XGetXCBConnection(dpy);
317 xcb_glx_query_version_reply_t *reply = xcb_glx_query_version_reply(c,
318 xcb_glx_query_version
319 (c,
320 GLX_MAJOR_VERSION,
321 GLX_MINOR_VERSION),
322 NULL);
323
324 if (!reply)
325 return GL_FALSE;
326
327 if (reply->major_version != GLX_MAJOR_VERSION) {
328 free(reply);
329 return GL_FALSE;
330 }
331 *major = reply->major_version;
332 *minor = min(reply->minor_version, GLX_MINOR_VERSION);
333 free(reply);
334 return GL_TRUE;
335 }
336
337 /*
338 * We don't want to enable this GLX_OML_swap_method in glxext.h,
339 * because we can't support it. The X server writes it out though,
340 * so we should handle it somehow, to avoid false warnings.
341 */
342 enum {
343 IGNORE_GLX_SWAP_METHOD_OML = 0x8060
344 };
345
346
347 static GLint
348 convert_from_x_visual_type(int visualType)
349 {
350 static const int glx_visual_types[] = {
351 [StaticGray] = GLX_STATIC_GRAY,
352 [GrayScale] = GLX_GRAY_SCALE,
353 [StaticColor] = GLX_STATIC_COLOR,
354 [PseudoColor] = GLX_PSEUDO_COLOR,
355 [TrueColor] = GLX_TRUE_COLOR,
356 [DirectColor] = GLX_DIRECT_COLOR,
357 };
358
359 if (visualType < ARRAY_SIZE(glx_visual_types))
360 return glx_visual_types[visualType];
361
362 return GLX_NONE;
363 }
364
365 /*
366 * getVisualConfigs uses the !tagged_only path.
367 * getFBConfigs uses the tagged_only path.
368 */
369 _X_HIDDEN void
370 __glXInitializeVisualConfigFromTags(struct glx_config * config, int count,
371 const INT32 * bp, Bool tagged_only,
372 Bool fbconfig_style_tags)
373 {
374 int i;
375
376 if (!tagged_only) {
377 /* Copy in the first set of properties */
378 config->visualID = *bp++;
379
380 config->visualType = convert_from_x_visual_type(*bp++);
381
382 config->renderType = *bp++ ? GLX_RGBA_BIT : GLX_COLOR_INDEX_BIT;
383
384 config->redBits = *bp++;
385 config->greenBits = *bp++;
386 config->blueBits = *bp++;
387 config->alphaBits = *bp++;
388 config->accumRedBits = *bp++;
389 config->accumGreenBits = *bp++;
390 config->accumBlueBits = *bp++;
391 config->accumAlphaBits = *bp++;
392
393 config->doubleBufferMode = *bp++;
394 config->stereoMode = *bp++;
395
396 config->rgbBits = *bp++;
397 config->depthBits = *bp++;
398 config->stencilBits = *bp++;
399 config->numAuxBuffers = *bp++;
400 config->level = *bp++;
401
402 #ifdef GLX_USE_APPLEGL
403 /* AppleSGLX supports pixmap and pbuffers with all config. */
404 config->drawableType = GLX_WINDOW_BIT | GLX_PIXMAP_BIT | GLX_PBUFFER_BIT;
405 /* Unfortunately this can create an ABI compatibility problem. */
406 count -= 18;
407 #else
408 count -= __GLX_MIN_CONFIG_PROPS;
409 #endif
410 }
411
412 /*
413 ** Additional properties may be in a list at the end
414 ** of the reply. They are in pairs of property type
415 ** and property value.
416 */
417
418 #define FETCH_OR_SET(tag) \
419 config-> tag = ( fbconfig_style_tags ) ? *bp++ : 1
420
421 for (i = 0; i < count; i += 2) {
422 long int tag = *bp++;
423
424 switch (tag) {
425 case GLX_RGBA:
426 if (fbconfig_style_tags)
427 config->renderType = *bp++ ? GLX_RGBA_BIT : GLX_COLOR_INDEX_BIT;
428 else
429 config->renderType = GLX_RGBA_BIT;
430 break;
431 case GLX_BUFFER_SIZE:
432 config->rgbBits = *bp++;
433 break;
434 case GLX_LEVEL:
435 config->level = *bp++;
436 break;
437 case GLX_DOUBLEBUFFER:
438 FETCH_OR_SET(doubleBufferMode);
439 break;
440 case GLX_STEREO:
441 FETCH_OR_SET(stereoMode);
442 break;
443 case GLX_AUX_BUFFERS:
444 config->numAuxBuffers = *bp++;
445 break;
446 case GLX_RED_SIZE:
447 config->redBits = *bp++;
448 break;
449 case GLX_GREEN_SIZE:
450 config->greenBits = *bp++;
451 break;
452 case GLX_BLUE_SIZE:
453 config->blueBits = *bp++;
454 break;
455 case GLX_ALPHA_SIZE:
456 config->alphaBits = *bp++;
457 break;
458 case GLX_DEPTH_SIZE:
459 config->depthBits = *bp++;
460 break;
461 case GLX_STENCIL_SIZE:
462 config->stencilBits = *bp++;
463 break;
464 case GLX_ACCUM_RED_SIZE:
465 config->accumRedBits = *bp++;
466 break;
467 case GLX_ACCUM_GREEN_SIZE:
468 config->accumGreenBits = *bp++;
469 break;
470 case GLX_ACCUM_BLUE_SIZE:
471 config->accumBlueBits = *bp++;
472 break;
473 case GLX_ACCUM_ALPHA_SIZE:
474 config->accumAlphaBits = *bp++;
475 break;
476 case GLX_VISUAL_CAVEAT_EXT:
477 config->visualRating = *bp++;
478 break;
479 case GLX_X_VISUAL_TYPE:
480 config->visualType = *bp++;
481 break;
482 case GLX_TRANSPARENT_TYPE:
483 config->transparentPixel = *bp++;
484 break;
485 case GLX_TRANSPARENT_INDEX_VALUE:
486 config->transparentIndex = *bp++;
487 break;
488 case GLX_TRANSPARENT_RED_VALUE:
489 config->transparentRed = *bp++;
490 break;
491 case GLX_TRANSPARENT_GREEN_VALUE:
492 config->transparentGreen = *bp++;
493 break;
494 case GLX_TRANSPARENT_BLUE_VALUE:
495 config->transparentBlue = *bp++;
496 break;
497 case GLX_TRANSPARENT_ALPHA_VALUE:
498 config->transparentAlpha = *bp++;
499 break;
500 case GLX_VISUAL_ID:
501 config->visualID = *bp++;
502 break;
503 case GLX_DRAWABLE_TYPE:
504 config->drawableType = *bp++;
505 #ifdef GLX_USE_APPLEGL
506 /* AppleSGLX supports pixmap and pbuffers with all config. */
507 config->drawableType |= GLX_WINDOW_BIT | GLX_PIXMAP_BIT | GLX_PBUFFER_BIT;
508 #endif
509 break;
510 case GLX_RENDER_TYPE: /* fbconfig render type bits */
511 config->renderType = *bp++;
512 break;
513 case GLX_X_RENDERABLE:
514 config->xRenderable = *bp++;
515 break;
516 case GLX_FBCONFIG_ID:
517 config->fbconfigID = *bp++;
518 break;
519 case GLX_MAX_PBUFFER_WIDTH:
520 config->maxPbufferWidth = *bp++;
521 break;
522 case GLX_MAX_PBUFFER_HEIGHT:
523 config->maxPbufferHeight = *bp++;
524 break;
525 case GLX_MAX_PBUFFER_PIXELS:
526 config->maxPbufferPixels = *bp++;
527 break;
528 #ifndef GLX_USE_APPLEGL
529 case GLX_OPTIMAL_PBUFFER_WIDTH_SGIX:
530 config->optimalPbufferWidth = *bp++;
531 break;
532 case GLX_OPTIMAL_PBUFFER_HEIGHT_SGIX:
533 config->optimalPbufferHeight = *bp++;
534 break;
535 case GLX_VISUAL_SELECT_GROUP_SGIX:
536 config->visualSelectGroup = *bp++;
537 break;
538 case GLX_SWAP_METHOD_OML:
539 if (*bp == GLX_SWAP_UNDEFINED_OML ||
540 *bp == GLX_SWAP_COPY_OML ||
541 *bp == GLX_SWAP_EXCHANGE_OML) {
542 config->swapMethod = *bp++;
543 } else {
544 /* X servers with old HW drivers may return any value here, so
545 * assume GLX_SWAP_METHOD_UNDEFINED.
546 */
547 config->swapMethod = GLX_SWAP_UNDEFINED_OML;
548 bp++;
549 }
550 break;
551 #endif
552 case GLX_SAMPLE_BUFFERS_SGIS:
553 config->sampleBuffers = *bp++;
554 break;
555 case GLX_SAMPLES_SGIS:
556 config->samples = *bp++;
557 break;
558 #ifdef GLX_USE_APPLEGL
559 case IGNORE_GLX_SWAP_METHOD_OML:
560 /* We ignore this tag. See the comment above this function. */
561 ++bp;
562 break;
563 #else
564 case GLX_BIND_TO_TEXTURE_RGB_EXT:
565 config->bindToTextureRgb = *bp++;
566 break;
567 case GLX_BIND_TO_TEXTURE_RGBA_EXT:
568 config->bindToTextureRgba = *bp++;
569 break;
570 case GLX_BIND_TO_MIPMAP_TEXTURE_EXT:
571 config->bindToMipmapTexture = *bp++;
572 break;
573 case GLX_BIND_TO_TEXTURE_TARGETS_EXT:
574 config->bindToTextureTargets = *bp++;
575 break;
576 case GLX_Y_INVERTED_EXT:
577 config->yInverted = *bp++;
578 break;
579 #endif
580 case GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT:
581 config->sRGBCapable = *bp++;
582 break;
583
584 case GLX_USE_GL:
585 if (fbconfig_style_tags)
586 bp++;
587 break;
588 case None:
589 i = count;
590 break;
591 default:
592 if(env_var_as_boolean("LIBGL_DIAGNOSTIC", false)) {
593 long int tagvalue = *bp++;
594 fprintf(stderr, "WARNING: unknown GLX tag from server: "
595 "tag 0x%lx value 0x%lx\n", tag, tagvalue);
596 } else {
597 /* Ignore the unrecognized tag's value */
598 bp++;
599 }
600 break;
601 }
602 }
603
604 /* The GLX_ARB_fbconfig_float spec says:
605 *
606 * "Note that floating point rendering is only supported for
607 * GLXPbuffer drawables."
608 */
609 if (config->renderType &
610 (GLX_RGBA_FLOAT_BIT_ARB|GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT))
611 config->drawableType &= GLX_PBUFFER_BIT;
612 }
613
614 static struct glx_config *
615 createConfigsFromProperties(Display * dpy, int nvisuals, int nprops,
616 int screen, GLboolean tagged_only)
617 {
618 INT32 buf[__GLX_TOTAL_CONFIG], *props;
619 unsigned prop_size;
620 struct glx_config *modes, *m;
621 int i;
622
623 if (nprops == 0)
624 return NULL;
625
626 /* FIXME: Is the __GLX_MIN_CONFIG_PROPS test correct for FBconfigs? */
627
628 /* Check number of properties */
629 if (nprops < __GLX_MIN_CONFIG_PROPS || nprops > __GLX_MAX_CONFIG_PROPS)
630 return NULL;
631
632 /* Allocate memory for our config structure */
633 modes = glx_config_create_list(nvisuals);
634 if (!modes)
635 return NULL;
636
637 prop_size = nprops * __GLX_SIZE_INT32;
638 if (prop_size <= sizeof(buf))
639 props = buf;
640 else
641 props = malloc(prop_size);
642
643 /* Read each config structure and convert it into our format */
644 m = modes;
645 for (i = 0; i < nvisuals; i++) {
646 _XRead(dpy, (char *) props, prop_size);
647 #ifdef GLX_USE_APPLEGL
648 /* Older X servers don't send this so we default it here. */
649 m->drawableType = GLX_WINDOW_BIT;
650 #else
651 /*
652 * The XQuartz 2.3.2.1 X server doesn't set this properly, so
653 * set the proper bits here.
654 * AppleSGLX supports windows, pixmaps, and pbuffers with all config.
655 */
656 m->drawableType = GLX_WINDOW_BIT | GLX_PIXMAP_BIT | GLX_PBUFFER_BIT;
657 #endif
658 /* Older X servers don't send this so we default it here. */
659 m->sRGBCapable = GL_FALSE;
660 __glXInitializeVisualConfigFromTags(m, nprops, props,
661 tagged_only, GL_TRUE);
662 m->screen = screen;
663 m = m->next;
664 }
665
666 if (props != buf)
667 free(props);
668
669 return modes;
670 }
671
672 static GLboolean
673 getVisualConfigs(struct glx_screen *psc,
674 struct glx_display *priv, int screen)
675 {
676 xGLXGetVisualConfigsReq *req;
677 xGLXGetVisualConfigsReply reply;
678 Display *dpy = priv->dpy;
679
680 LockDisplay(dpy);
681
682 psc->visuals = NULL;
683 GetReq(GLXGetVisualConfigs, req);
684 req->reqType = priv->majorOpcode;
685 req->glxCode = X_GLXGetVisualConfigs;
686 req->screen = screen;
687
688 if (!_XReply(dpy, (xReply *) & reply, 0, False))
689 goto out;
690
691 psc->visuals = createConfigsFromProperties(dpy,
692 reply.numVisuals,
693 reply.numProps,
694 screen, GL_FALSE);
695
696 out:
697 UnlockDisplay(dpy);
698 return psc->visuals != NULL;
699 }
700
701 static GLboolean
702 getFBConfigs(struct glx_screen *psc, struct glx_display *priv, int screen)
703 {
704 xGLXGetFBConfigsReq *fb_req;
705 xGLXGetFBConfigsSGIXReq *sgi_req;
706 xGLXVendorPrivateWithReplyReq *vpreq;
707 xGLXGetFBConfigsReply reply;
708 Display *dpy = priv->dpy;
709
710 psc->serverGLXexts =
711 __glXQueryServerString(dpy, priv->majorOpcode, screen, GLX_EXTENSIONS);
712
713 if (psc->serverGLXexts == NULL) {
714 return GL_FALSE;
715 }
716
717 LockDisplay(dpy);
718
719 psc->configs = NULL;
720 if (priv->majorVersion > 1 ||
721 (priv->majorVersion == 1 && priv->minorVersion >= 3)) {
722 GetReq(GLXGetFBConfigs, fb_req);
723 fb_req->reqType = priv->majorOpcode;
724 fb_req->glxCode = X_GLXGetFBConfigs;
725 fb_req->screen = screen;
726 }
727 else if (strstr(psc->serverGLXexts, "GLX_SGIX_fbconfig") != NULL) {
728 GetReqExtra(GLXVendorPrivateWithReply,
729 sz_xGLXGetFBConfigsSGIXReq -
730 sz_xGLXVendorPrivateWithReplyReq, vpreq);
731 sgi_req = (xGLXGetFBConfigsSGIXReq *) vpreq;
732 sgi_req->reqType = priv->majorOpcode;
733 sgi_req->glxCode = X_GLXVendorPrivateWithReply;
734 sgi_req->vendorCode = X_GLXvop_GetFBConfigsSGIX;
735 sgi_req->screen = screen;
736 }
737 else
738 goto out;
739
740 if (!_XReply(dpy, (xReply *) & reply, 0, False))
741 goto out;
742
743 psc->configs = createConfigsFromProperties(dpy,
744 reply.numFBConfigs,
745 reply.numAttribs * 2,
746 screen, GL_TRUE);
747
748 out:
749 UnlockDisplay(dpy);
750 return psc->configs != NULL;
751 }
752
753 _X_HIDDEN Bool
754 glx_screen_init(struct glx_screen *psc,
755 int screen, struct glx_display * priv)
756 {
757 /* Initialize per screen dynamic client GLX extensions */
758 psc->ext_list_first_time = GL_TRUE;
759 psc->scr = screen;
760 psc->dpy = priv->dpy;
761 psc->display = priv;
762
763 if (!getVisualConfigs(psc, priv, screen))
764 return GL_FALSE;
765
766 if (!getFBConfigs(psc, priv, screen))
767 return GL_FALSE;
768
769 return GL_TRUE;
770 }
771
772 _X_HIDDEN void
773 glx_screen_cleanup(struct glx_screen *psc)
774 {
775 if (psc->configs) {
776 glx_config_destroy_list(psc->configs);
777 free(psc->effectiveGLXexts);
778 psc->configs = NULL; /* NOTE: just for paranoia */
779 }
780 if (psc->visuals) {
781 glx_config_destroy_list(psc->visuals);
782 psc->visuals = NULL; /* NOTE: just for paranoia */
783 }
784 free((char *) psc->serverGLXexts);
785 }
786
787 /*
788 ** Allocate the memory for the per screen configs for each screen.
789 ** If that works then fetch the per screen configs data.
790 */
791 static Bool
792 AllocAndFetchScreenConfigs(Display * dpy, struct glx_display * priv)
793 {
794 struct glx_screen *psc;
795 GLint i, screens;
796
797 /*
798 ** First allocate memory for the array of per screen configs.
799 */
800 screens = ScreenCount(dpy);
801 priv->screens = calloc(screens, sizeof *priv->screens);
802 if (!priv->screens)
803 return GL_FALSE;
804
805 priv->serverGLXversion =
806 __glXQueryServerString(dpy, priv->majorOpcode, 0, GLX_VERSION);
807 if (priv->serverGLXversion == NULL) {
808 FreeScreenConfigs(priv);
809 return GL_FALSE;
810 }
811
812 for (i = 0; i < screens; i++, psc++) {
813 psc = NULL;
814 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
815 #if defined(GLX_USE_DRM)
816 #if defined(HAVE_DRI3)
817 if (priv->dri3Display)
818 psc = (*priv->dri3Display->createScreen) (i, priv);
819 #endif /* HAVE_DRI3 */
820 if (psc == NULL && priv->dri2Display)
821 psc = (*priv->dri2Display->createScreen) (i, priv);
822 if (psc == NULL && priv->driDisplay)
823 psc = (*priv->driDisplay->createScreen) (i, priv);
824 #endif /* GLX_USE_DRM */
825
826 #ifdef GLX_USE_WINDOWSGL
827 if (psc == NULL && priv->windowsdriDisplay)
828 psc = (*priv->windowsdriDisplay->createScreen) (i, priv);
829 #endif
830
831 if (psc == NULL && priv->driswDisplay)
832 psc = (*priv->driswDisplay->createScreen) (i, priv);
833 #endif /* GLX_DIRECT_RENDERING && !GLX_USE_APPLEGL */
834
835 #if defined(GLX_USE_APPLEGL)
836 if (psc == NULL)
837 psc = applegl_create_screen(i, priv);
838 #else
839 if (psc == NULL)
840 psc = indirect_create_screen(i, priv);
841 #endif
842 priv->screens[i] = psc;
843 }
844 SyncHandle();
845 return GL_TRUE;
846 }
847
848 /*
849 ** Initialize the client side extension code.
850 */
851 _X_HIDDEN struct glx_display *
852 __glXInitialize(Display * dpy)
853 {
854 struct glx_display *dpyPriv, *d;
855 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
856 Bool glx_direct, glx_accel;
857 #endif
858 int i;
859
860 _XLockMutex(_Xglobal_lock);
861
862 for (dpyPriv = glx_displays; dpyPriv; dpyPriv = dpyPriv->next) {
863 if (dpyPriv->dpy == dpy) {
864 _XUnlockMutex(_Xglobal_lock);
865 return dpyPriv;
866 }
867 }
868
869 /* Drop the lock while we create the display private. */
870 _XUnlockMutex(_Xglobal_lock);
871
872 dpyPriv = calloc(1, sizeof *dpyPriv);
873 if (!dpyPriv)
874 return NULL;
875
876 dpyPriv->codes = XInitExtension(dpy, __glXExtensionName);
877 if (!dpyPriv->codes) {
878 free(dpyPriv);
879 return NULL;
880 }
881
882 dpyPriv->dpy = dpy;
883 dpyPriv->majorOpcode = dpyPriv->codes->major_opcode;
884 dpyPriv->serverGLXvendor = 0x0;
885 dpyPriv->serverGLXversion = 0x0;
886
887 /* See if the versions are compatible. This GLX implementation does not
888 * work with servers that only support GLX 1.0.
889 */
890 if (!QueryVersion(dpy, dpyPriv->majorOpcode,
891 &dpyPriv->majorVersion, &dpyPriv->minorVersion)
892 || (dpyPriv->majorVersion == 1 && dpyPriv->minorVersion < 1)) {
893 free(dpyPriv);
894 return NULL;
895 }
896
897 for (i = 0; i < __GLX_NUMBER_EVENTS; i++) {
898 XESetWireToEvent(dpy, dpyPriv->codes->first_event + i, __glXWireToEvent);
899 XESetEventToWire(dpy, dpyPriv->codes->first_event + i, __glXEventToWire);
900 }
901
902 XESetCloseDisplay(dpy, dpyPriv->codes->extension, __glXCloseDisplay);
903 XESetErrorString (dpy, dpyPriv->codes->extension, __glXErrorString);
904
905 dpyPriv->glXDrawHash = __glxHashCreate();
906
907 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
908 glx_direct = !env_var_as_boolean("LIBGL_ALWAYS_INDIRECT", false);
909 glx_accel = !env_var_as_boolean("LIBGL_ALWAYS_SOFTWARE", false);
910
911 dpyPriv->drawHash = __glxHashCreate();
912
913 #ifndef GLX_USE_APPLEGL
914 /* Set the logger before the *CreateDisplay functions. */
915 loader_set_logger(dri_message);
916 #endif
917
918 /*
919 ** Initialize the direct rendering per display data and functions.
920 ** Note: This _must_ be done before calling any other DRI routines
921 ** (e.g., those called in AllocAndFetchScreenConfigs).
922 */
923 #if defined(GLX_USE_DRM)
924 if (glx_direct && glx_accel) {
925 #if defined(HAVE_DRI3)
926 if (!env_var_as_boolean("LIBGL_DRI3_DISABLE", false))
927 dpyPriv->dri3Display = dri3_create_display(dpy);
928 #endif /* HAVE_DRI3 */
929 dpyPriv->dri2Display = dri2CreateDisplay(dpy);
930 dpyPriv->driDisplay = driCreateDisplay(dpy);
931 }
932 #endif /* GLX_USE_DRM */
933 if (glx_direct)
934 dpyPriv->driswDisplay = driswCreateDisplay(dpy);
935 #endif /* GLX_DIRECT_RENDERING && !GLX_USE_APPLEGL */
936
937 #ifdef GLX_USE_APPLEGL
938 if (!applegl_create_display(dpyPriv)) {
939 free(dpyPriv);
940 return NULL;
941 }
942 #endif
943
944 #ifdef GLX_USE_WINDOWSGL
945 if (glx_direct && glx_accel)
946 dpyPriv->windowsdriDisplay = driwindowsCreateDisplay(dpy);
947 #endif
948
949 if (!AllocAndFetchScreenConfigs(dpy, dpyPriv)) {
950 free(dpyPriv);
951 return NULL;
952 }
953
954 __glX_send_client_info(dpyPriv);
955
956 /* Grab the lock again and add the dispay private, unless somebody
957 * beat us to initializing on this display in the meantime. */
958 _XLockMutex(_Xglobal_lock);
959
960 for (d = glx_displays; d; d = d->next) {
961 if (d->dpy == dpy) {
962 _XUnlockMutex(_Xglobal_lock);
963 glx_display_free(dpyPriv);
964 return d;
965 }
966 }
967
968 dpyPriv->next = glx_displays;
969 glx_displays = dpyPriv;
970
971 _XUnlockMutex(_Xglobal_lock);
972
973 return dpyPriv;
974 }
975
976 /*
977 ** Setup for sending a GLX command on dpy. Make sure the extension is
978 ** initialized. Try to avoid calling __glXInitialize as its kinda slow.
979 */
980 _X_HIDDEN CARD8
981 __glXSetupForCommand(Display * dpy)
982 {
983 struct glx_context *gc;
984 struct glx_display *priv;
985
986 /* If this thread has a current context, flush its rendering commands */
987 gc = __glXGetCurrentContext();
988 if (gc->currentDpy) {
989 /* Flush rendering buffer of the current context, if any */
990 (void) __glXFlushRenderBuffer(gc, gc->pc);
991
992 if (gc->currentDpy == dpy) {
993 /* Use opcode from gc because its right */
994 return gc->majorOpcode;
995 }
996 else {
997 /*
998 ** Have to get info about argument dpy because it might be to
999 ** a different server
1000 */
1001 }
1002 }
1003
1004 /* Forced to lookup extension via the slow initialize route */
1005 priv = __glXInitialize(dpy);
1006 if (!priv) {
1007 return 0;
1008 }
1009 return priv->majorOpcode;
1010 }
1011
1012 /**
1013 * Flush the drawing command transport buffer.
1014 *
1015 * \param ctx Context whose transport buffer is to be flushed.
1016 * \param pc Pointer to first unused buffer location.
1017 *
1018 * \todo
1019 * Modify this function to use \c ctx->pc instead of the explicit
1020 * \c pc parameter.
1021 */
1022 _X_HIDDEN GLubyte *
1023 __glXFlushRenderBuffer(struct glx_context * ctx, GLubyte * pc)
1024 {
1025 Display *const dpy = ctx->currentDpy;
1026 xcb_connection_t *c = XGetXCBConnection(dpy);
1027 const GLint size = pc - ctx->buf;
1028
1029 if ((dpy != NULL) && (size > 0)) {
1030 xcb_glx_render(c, ctx->currentContextTag, size,
1031 (const uint8_t *) ctx->buf);
1032 }
1033
1034 /* Reset pointer and return it */
1035 ctx->pc = ctx->buf;
1036 return ctx->pc;
1037 }
1038
1039
1040 /**
1041 * Send a portion of a GLXRenderLarge command to the server. The advantage of
1042 * this function over \c __glXSendLargeCommand is that callers can use the
1043 * data buffer in the GLX context and may be able to avoid allocating an
1044 * extra buffer. The disadvantage is the clients will have to do more
1045 * GLX protocol work (i.e., calculating \c totalRequests, etc.).
1046 *
1047 * \sa __glXSendLargeCommand
1048 *
1049 * \param gc GLX context
1050 * \param requestNumber Which part of the whole command is this? The first
1051 * request is 1.
1052 * \param totalRequests How many requests will there be?
1053 * \param data Command data.
1054 * \param dataLen Size, in bytes, of the command data.
1055 */
1056 _X_HIDDEN void
1057 __glXSendLargeChunk(struct glx_context * gc, GLint requestNumber,
1058 GLint totalRequests, const GLvoid * data, GLint dataLen)
1059 {
1060 Display *dpy = gc->currentDpy;
1061 xcb_connection_t *c = XGetXCBConnection(dpy);
1062 xcb_glx_render_large(c, gc->currentContextTag, requestNumber,
1063 totalRequests, dataLen, data);
1064 }
1065
1066
1067 /**
1068 * Send a command that is too large for the GLXRender protocol request.
1069 *
1070 * Send a large command, one that is too large for some reason to
1071 * send using the GLXRender protocol request. One reason to send
1072 * a large command is to avoid copying the data.
1073 *
1074 * \param ctx GLX context
1075 * \param header Header data.
1076 * \param headerLen Size, in bytes, of the header data. It is assumed that
1077 * the header data will always be small enough to fit in
1078 * a single X protocol packet.
1079 * \param data Command data.
1080 * \param dataLen Size, in bytes, of the command data.
1081 */
1082 _X_HIDDEN void
1083 __glXSendLargeCommand(struct glx_context * ctx,
1084 const GLvoid * header, GLint headerLen,
1085 const GLvoid * data, GLint dataLen)
1086 {
1087 GLint maxSize;
1088 GLint totalRequests, requestNumber;
1089
1090 /*
1091 ** Calculate the maximum amount of data can be stuffed into a single
1092 ** packet. sz_xGLXRenderReq is added because bufSize is the maximum
1093 ** packet size minus sz_xGLXRenderReq.
1094 */
1095 maxSize = (ctx->bufSize + sz_xGLXRenderReq) - sz_xGLXRenderLargeReq;
1096 totalRequests = 1 + (dataLen / maxSize);
1097 if (dataLen % maxSize)
1098 totalRequests++;
1099
1100 /*
1101 ** Send all of the command, except the large array, as one request.
1102 */
1103 assert(headerLen <= maxSize);
1104 __glXSendLargeChunk(ctx, 1, totalRequests, header, headerLen);
1105
1106 /*
1107 ** Send enough requests until the whole array is sent.
1108 */
1109 for (requestNumber = 2; requestNumber <= (totalRequests - 1);
1110 requestNumber++) {
1111 __glXSendLargeChunk(ctx, requestNumber, totalRequests, data, maxSize);
1112 data = (const GLvoid *) (((const GLubyte *) data) + maxSize);
1113 dataLen -= maxSize;
1114 assert(dataLen > 0);
1115 }
1116
1117 assert(dataLen <= maxSize);
1118 __glXSendLargeChunk(ctx, requestNumber, totalRequests, data, dataLen);
1119 }
1120
1121 /************************************************************************/
1122
1123 #ifdef DEBUG
1124 _X_HIDDEN void
1125 __glXDumpDrawBuffer(struct glx_context * ctx)
1126 {
1127 GLubyte *p = ctx->buf;
1128 GLubyte *end = ctx->pc;
1129 GLushort opcode, length;
1130
1131 while (p < end) {
1132 /* Fetch opcode */
1133 opcode = *((GLushort *) p);
1134 length = *((GLushort *) (p + 2));
1135 printf("%2x: %5d: ", opcode, length);
1136 length -= 4;
1137 p += 4;
1138 while (length > 0) {
1139 printf("%08x ", *((unsigned *) p));
1140 p += 4;
1141 length -= 4;
1142 }
1143 printf("\n");
1144 }
1145 }
1146 #endif