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