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