glx: Move drawHash to display private
[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 psc->driScreen->destroyScreen(psc);
272 psc->driScreen = NULL;
273 } else {
274 Xfree(psc);
275 }
276 #else
277 Xfree(psc);
278 #endif
279 }
280 XFree((char *) priv->screenConfigs);
281 priv->screenConfigs = NULL;
282 }
283
284 /*
285 ** Release the private memory referred to in a display private
286 ** structure. The caller will free the extension structure.
287 */
288 static int
289 __glXFreeDisplayPrivate(XExtData * extension)
290 {
291 __GLXdisplayPrivate *priv;
292
293 priv = (__GLXdisplayPrivate *) extension->private_data;
294 FreeScreenConfigs(priv);
295 if (priv->serverGLXvendor) {
296 Xfree((char *) priv->serverGLXvendor);
297 priv->serverGLXvendor = 0x0; /* to protect against double free's */
298 }
299 if (priv->serverGLXversion) {
300 Xfree((char *) priv->serverGLXversion);
301 priv->serverGLXversion = 0x0; /* to protect against double free's */
302 }
303
304 __glxHashDestroy(priv->drawHash);
305
306 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
307 /* Free the direct rendering per display data */
308 if (priv->driswDisplay)
309 (*priv->driswDisplay->destroyDisplay) (priv->driswDisplay);
310 priv->driswDisplay = NULL;
311
312 if (priv->driDisplay)
313 (*priv->driDisplay->destroyDisplay) (priv->driDisplay);
314 priv->driDisplay = NULL;
315
316 if (priv->dri2Display)
317 (*priv->dri2Display->destroyDisplay) (priv->dri2Display);
318 priv->dri2Display = NULL;
319 #endif
320
321 Xfree((char *) priv);
322 return 0;
323 }
324
325 /************************************************************************/
326
327 /*
328 ** Query the version of the GLX extension. This procedure works even if
329 ** the client extension is not completely set up.
330 */
331 static Bool
332 QueryVersion(Display * dpy, int opcode, int *major, int *minor)
333 {
334 #ifdef USE_XCB
335 xcb_connection_t *c = XGetXCBConnection(dpy);
336 xcb_glx_query_version_reply_t *reply = xcb_glx_query_version_reply(c,
337 xcb_glx_query_version
338 (c,
339 GLX_MAJOR_VERSION,
340 GLX_MINOR_VERSION),
341 NULL);
342
343 if (reply->major_version != GLX_MAJOR_VERSION) {
344 free(reply);
345 return GL_FALSE;
346 }
347 *major = reply->major_version;
348 *minor = min(reply->minor_version, GLX_MINOR_VERSION);
349 free(reply);
350 return GL_TRUE;
351 #else
352 xGLXQueryVersionReq *req;
353 xGLXQueryVersionReply reply;
354
355 /* Send the glXQueryVersion request */
356 LockDisplay(dpy);
357 GetReq(GLXQueryVersion, req);
358 req->reqType = opcode;
359 req->glxCode = X_GLXQueryVersion;
360 req->majorVersion = GLX_MAJOR_VERSION;
361 req->minorVersion = GLX_MINOR_VERSION;
362 _XReply(dpy, (xReply *) & reply, 0, False);
363 UnlockDisplay(dpy);
364 SyncHandle();
365
366 if (reply.majorVersion != GLX_MAJOR_VERSION) {
367 /*
368 ** The server does not support the same major release as this
369 ** client.
370 */
371 return GL_FALSE;
372 }
373 *major = reply.majorVersion;
374 *minor = min(reply.minorVersion, GLX_MINOR_VERSION);
375 return GL_TRUE;
376 #endif /* USE_XCB */
377 }
378
379 /*
380 * We don't want to enable this GLX_OML_swap_method in glxext.h,
381 * because we can't support it. The X server writes it out though,
382 * so we should handle it somehow, to avoid false warnings.
383 */
384 enum {
385 IGNORE_GLX_SWAP_METHOD_OML = 0x8060
386 };
387
388
389 /*
390 * getVisualConfigs uses the !tagged_only path.
391 * getFBConfigs uses the tagged_only path.
392 */
393 _X_HIDDEN void
394 __glXInitializeVisualConfigFromTags(__GLcontextModes * config, int count,
395 const INT32 * bp, Bool tagged_only,
396 Bool fbconfig_style_tags)
397 {
398 int i;
399
400 if (!tagged_only) {
401 /* Copy in the first set of properties */
402 config->visualID = *bp++;
403
404 config->visualType = _gl_convert_from_x_visual_type(*bp++);
405
406 config->rgbMode = *bp++;
407
408 config->redBits = *bp++;
409 config->greenBits = *bp++;
410 config->blueBits = *bp++;
411 config->alphaBits = *bp++;
412 config->accumRedBits = *bp++;
413 config->accumGreenBits = *bp++;
414 config->accumBlueBits = *bp++;
415 config->accumAlphaBits = *bp++;
416
417 config->doubleBufferMode = *bp++;
418 config->stereoMode = *bp++;
419
420 config->rgbBits = *bp++;
421 config->depthBits = *bp++;
422 config->stencilBits = *bp++;
423 config->numAuxBuffers = *bp++;
424 config->level = *bp++;
425
426 #ifdef GLX_USE_APPLEGL
427 /* AppleSGLX supports pixmap and pbuffers with all config. */
428 config->drawableType = GLX_WINDOW_BIT | GLX_PIXMAP_BIT | GLX_PBUFFER_BIT;
429 /* Unfortunately this can create an ABI compatibility problem. */
430 count -= 18;
431 #else
432 count -= __GLX_MIN_CONFIG_PROPS;
433 #endif
434 }
435
436 /*
437 ** Additional properties may be in a list at the end
438 ** of the reply. They are in pairs of property type
439 ** and property value.
440 */
441
442 #define FETCH_OR_SET(tag) \
443 config-> tag = ( fbconfig_style_tags ) ? *bp++ : 1
444
445 for (i = 0; i < count; i += 2) {
446 long int tag = *bp++;
447
448 switch (tag) {
449 case GLX_RGBA:
450 FETCH_OR_SET(rgbMode);
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:
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 case GLX_SWAP_METHOD_OML:
560 config->swapMethod = *bp++;
561 break;
562 #endif
563 case GLX_SAMPLE_BUFFERS_SGIS:
564 config->sampleBuffers = *bp++;
565 break;
566 case GLX_SAMPLES_SGIS:
567 config->samples = *bp++;
568 break;
569 #ifdef GLX_USE_APPLEGL
570 case IGNORE_GLX_SWAP_METHOD_OML:
571 /* We ignore this tag. See the comment above this function. */
572 ++bp;
573 break;
574 #else
575 case GLX_BIND_TO_TEXTURE_RGB_EXT:
576 config->bindToTextureRgb = *bp++;
577 break;
578 case GLX_BIND_TO_TEXTURE_RGBA_EXT:
579 config->bindToTextureRgba = *bp++;
580 break;
581 case GLX_BIND_TO_MIPMAP_TEXTURE_EXT:
582 config->bindToMipmapTexture = *bp++;
583 break;
584 case GLX_BIND_TO_TEXTURE_TARGETS_EXT:
585 config->bindToTextureTargets = *bp++;
586 break;
587 case GLX_Y_INVERTED_EXT:
588 config->yInverted = *bp++;
589 break;
590 #endif
591 case GLX_USE_GL:
592 if (fbconfig_style_tags)
593 bp++;
594 break;
595 case None:
596 i = count;
597 break;
598 default:
599 if(getenv("LIBGL_DIAGNOSTIC")) {
600 long int tagvalue = *bp++;
601 fprintf(stderr, "WARNING: unknown GLX tag from server: "
602 "tag 0x%lx value 0x%lx\n", tag, tagvalue);
603 } else {
604 /* Ignore the unrecognized tag's value */
605 bp++;
606 }
607 break;
608 }
609 }
610
611 config->renderType =
612 (config->rgbMode) ? GLX_RGBA_BIT : GLX_COLOR_INDEX_BIT;
613
614 config->haveAccumBuffer = ((config->accumRedBits +
615 config->accumGreenBits +
616 config->accumBlueBits +
617 config->accumAlphaBits) > 0);
618 config->haveDepthBuffer = (config->depthBits > 0);
619 config->haveStencilBuffer = (config->stencilBits > 0);
620 }
621
622 static __GLcontextModes *
623 createConfigsFromProperties(Display * dpy, int nvisuals, int nprops,
624 int screen, GLboolean tagged_only)
625 {
626 INT32 buf[__GLX_TOTAL_CONFIG], *props;
627 unsigned prop_size;
628 __GLcontextModes *modes, *m;
629 int i;
630
631 if (nprops == 0)
632 return NULL;
633
634 /* FIXME: Is the __GLX_MIN_CONFIG_PROPS test correct for FBconfigs? */
635
636 /* Check number of properties */
637 if (nprops < __GLX_MIN_CONFIG_PROPS || nprops > __GLX_MAX_CONFIG_PROPS)
638 return NULL;
639
640 /* Allocate memory for our config structure */
641 modes = _gl_context_modes_create(nvisuals, sizeof(__GLcontextModes));
642 if (!modes)
643 return NULL;
644
645 prop_size = nprops * __GLX_SIZE_INT32;
646 if (prop_size <= sizeof(buf))
647 props = buf;
648 else
649 props = Xmalloc(prop_size);
650
651 /* Read each config structure and convert it into our format */
652 m = modes;
653 for (i = 0; i < nvisuals; i++) {
654 _XRead(dpy, (char *) props, prop_size);
655 #ifdef GLX_USE_APPLEGL
656 /* Older X servers don't send this so we default it here. */
657 m->drawableType = GLX_WINDOW_BIT;
658 #else
659 /*
660 * The XQuartz 2.3.2.1 X server doesn't set this properly, so
661 * set the proper bits here.
662 * AppleSGLX supports windows, pixmaps, and pbuffers with all config.
663 */
664 m->drawableType = GLX_WINDOW_BIT | GLX_PIXMAP_BIT | GLX_PBUFFER_BIT;
665 #endif
666 __glXInitializeVisualConfigFromTags(m, nprops, props,
667 tagged_only, GL_TRUE);
668 m->screen = screen;
669 m = m->next;
670 }
671
672 if (props != buf)
673 Xfree(props);
674
675 return modes;
676 }
677
678 static GLboolean
679 getVisualConfigs(__GLXscreenConfigs *psc,
680 __GLXdisplayPrivate *priv, int screen)
681 {
682 xGLXGetVisualConfigsReq *req;
683 xGLXGetVisualConfigsReply reply;
684 Display *dpy = priv->dpy;
685
686 LockDisplay(dpy);
687
688 psc->visuals = NULL;
689 GetReq(GLXGetVisualConfigs, req);
690 req->reqType = priv->majorOpcode;
691 req->glxCode = X_GLXGetVisualConfigs;
692 req->screen = screen;
693
694 if (!_XReply(dpy, (xReply *) & reply, 0, False))
695 goto out;
696
697 psc->visuals = createConfigsFromProperties(dpy,
698 reply.numVisuals,
699 reply.numProps,
700 screen, GL_FALSE);
701
702 out:
703 UnlockDisplay(dpy);
704 return psc->visuals != NULL;
705 }
706
707 static GLboolean
708 getFBConfigs(__GLXscreenConfigs *psc, __GLXdisplayPrivate *priv, int screen)
709 {
710 xGLXGetFBConfigsReq *fb_req;
711 xGLXGetFBConfigsSGIXReq *sgi_req;
712 xGLXVendorPrivateWithReplyReq *vpreq;
713 xGLXGetFBConfigsReply reply;
714 Display *dpy = priv->dpy;
715
716 psc->serverGLXexts =
717 __glXQueryServerString(dpy, priv->majorOpcode, screen, GLX_EXTENSIONS);
718
719 LockDisplay(dpy);
720
721 psc->configs = NULL;
722 if (atof(priv->serverGLXversion) >= 1.3) {
723 GetReq(GLXGetFBConfigs, fb_req);
724 fb_req->reqType = priv->majorOpcode;
725 fb_req->glxCode = X_GLXGetFBConfigs;
726 fb_req->screen = screen;
727 }
728 else if (strstr(psc->serverGLXexts, "GLX_SGIX_fbconfig") != NULL) {
729 GetReqExtra(GLXVendorPrivateWithReply,
730 sz_xGLXGetFBConfigsSGIXReq +
731 sz_xGLXVendorPrivateWithReplyReq, vpreq);
732 sgi_req = (xGLXGetFBConfigsSGIXReq *) vpreq;
733 sgi_req->reqType = priv->majorOpcode;
734 sgi_req->glxCode = X_GLXVendorPrivateWithReply;
735 sgi_req->vendorCode = X_GLXvop_GetFBConfigsSGIX;
736 sgi_req->screen = screen;
737 }
738 else
739 goto out;
740
741 if (!_XReply(dpy, (xReply *) & reply, 0, False))
742 goto out;
743
744 psc->configs = createConfigsFromProperties(dpy,
745 reply.numFBConfigs,
746 reply.numAttribs * 2,
747 screen, GL_TRUE);
748
749 out:
750 UnlockDisplay(dpy);
751 return psc->configs != NULL;
752 }
753
754 _X_HIDDEN Bool
755 glx_screen_init(__GLXscreenConfigs *psc,
756 int screen, __GLXdisplayPrivate * priv)
757 {
758 /* Initialize per screen dynamic client GLX extensions */
759 psc->ext_list_first_time = GL_TRUE;
760 psc->scr = screen;
761 psc->dpy = priv->dpy;
762 psc->display = priv;
763
764 getVisualConfigs(psc, priv, screen);
765 getFBConfigs(psc, priv, screen);
766
767 return GL_TRUE;
768 }
769
770 static __GLXscreenConfigs *
771 createIndirectScreen()
772 {
773 __GLXscreenConfigs *psc;
774
775 psc = Xmalloc(sizeof *psc);
776 memset(psc, 0, sizeof *psc);
777
778 return psc;
779 }
780
781 /*
782 ** Allocate the memory for the per screen configs for each screen.
783 ** If that works then fetch the per screen configs data.
784 */
785 static Bool
786 AllocAndFetchScreenConfigs(Display * dpy, __GLXdisplayPrivate * priv)
787 {
788 __GLXscreenConfigs *psc;
789 GLint i, screens;
790
791 /*
792 ** First allocate memory for the array of per screen configs.
793 */
794 screens = ScreenCount(dpy);
795 priv->screenConfigs = Xmalloc(screens * sizeof *priv->screenConfigs);
796 if (!priv->screenConfigs)
797 return GL_FALSE;
798
799 priv->serverGLXversion =
800 __glXQueryServerString(dpy, priv->majorOpcode, 0, GLX_VERSION);
801 if (priv->serverGLXversion == NULL) {
802 FreeScreenConfigs(priv);
803 return GL_FALSE;
804 }
805
806 for (i = 0; i < screens; i++, psc++) {
807 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
808 if (priv->dri2Display)
809 psc = (*priv->dri2Display->createScreen) (i, priv);
810 if (psc == NULL && priv->driDisplay)
811 psc = (*priv->driDisplay->createScreen) (i, priv);
812 if (psc == NULL && priv->driswDisplay)
813 psc = (*priv->driswDisplay->createScreen) (i, priv);
814 if (psc == NULL)
815 psc = createIndirectScreen (i, priv);
816 #endif
817 priv->screenConfigs[i] = psc;
818 }
819 SyncHandle();
820 return GL_TRUE;
821 }
822
823 /*
824 ** Initialize the client side extension code.
825 */
826 _X_HIDDEN __GLXdisplayPrivate *
827 __glXInitialize(Display * dpy)
828 {
829 XExtDisplayInfo *info = __glXFindDisplay(dpy);
830 XExtData **privList, *private, *found;
831 __GLXdisplayPrivate *dpyPriv;
832 XEDataObject dataObj;
833 int major, minor;
834 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
835 Bool glx_direct, glx_accel;
836 #endif
837
838 /* The one and only long long lock */
839 __glXLock();
840
841 if (!XextHasExtension(info)) {
842 /* No GLX extension supported by this server. Oh well. */
843 __glXUnlock();
844 XMissingExtension(dpy, __glXExtensionName);
845 return 0;
846 }
847
848 /* See if a display private already exists. If so, return it */
849 dataObj.display = dpy;
850 privList = XEHeadOfExtensionList(dataObj);
851 found = XFindOnExtensionList(privList, info->codes->extension);
852 if (found) {
853 __glXUnlock();
854 return (__GLXdisplayPrivate *) found->private_data;
855 }
856
857 /* See if the versions are compatible */
858 if (!QueryVersion(dpy, info->codes->major_opcode, &major, &minor)) {
859 /* The client and server do not agree on versions. Punt. */
860 __glXUnlock();
861 return 0;
862 }
863
864 /*
865 ** Allocate memory for all the pieces needed for this buffer.
866 */
867 private = (XExtData *) Xmalloc(sizeof(XExtData));
868 if (!private) {
869 __glXUnlock();
870 return 0;
871 }
872 dpyPriv = (__GLXdisplayPrivate *) Xcalloc(1, sizeof(__GLXdisplayPrivate));
873 if (!dpyPriv) {
874 __glXUnlock();
875 Xfree((char *) private);
876 return 0;
877 }
878
879 /*
880 ** Init the display private and then read in the screen config
881 ** structures from the server.
882 */
883 dpyPriv->majorOpcode = info->codes->major_opcode;
884 dpyPriv->majorVersion = major;
885 dpyPriv->minorVersion = minor;
886 dpyPriv->dpy = dpy;
887
888 dpyPriv->serverGLXvendor = 0x0;
889 dpyPriv->serverGLXversion = 0x0;
890
891 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
892 glx_direct = (getenv("LIBGL_ALWAYS_INDIRECT") == NULL);
893 glx_accel = (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL);
894
895 dpyPriv->drawHash = __glxHashCreate();
896
897 /*
898 ** Initialize the direct rendering per display data and functions.
899 ** Note: This _must_ be done before calling any other DRI routines
900 ** (e.g., those called in AllocAndFetchScreenConfigs).
901 */
902 if (glx_direct && glx_accel) {
903 dpyPriv->dri2Display = dri2CreateDisplay(dpy);
904 dpyPriv->driDisplay = driCreateDisplay(dpy);
905 }
906 if (glx_direct)
907 dpyPriv->driswDisplay = driswCreateDisplay(dpy);
908 #endif
909 #ifdef GLX_USE_APPLEGL
910 if (apple_init_glx(dpy) || !AllocAndFetchScreenConfigs(dpy, dpyPriv)) {
911 #else
912 if (!AllocAndFetchScreenConfigs(dpy, dpyPriv)) {
913 #endif
914 __glXUnlock();
915 Xfree((char *) dpyPriv);
916 Xfree((char *) private);
917 return 0;
918 }
919
920 /*
921 ** Fill in the private structure. This is the actual structure that
922 ** hangs off of the Display structure. Our private structure is
923 ** referred to by this structure. Got that?
924 */
925 private->number = info->codes->extension;
926 private->next = 0;
927 private->free_private = __glXFreeDisplayPrivate;
928 private->private_data = (char *) dpyPriv;
929 XAddToExtensionList(privList, private);
930
931 if (dpyPriv->majorVersion == 1 && dpyPriv->minorVersion >= 1) {
932 __glXClientInfo(dpy, dpyPriv->majorOpcode);
933 }
934 __glXUnlock();
935
936 return dpyPriv;
937 }
938
939 /*
940 ** Setup for sending a GLX command on dpy. Make sure the extension is
941 ** initialized. Try to avoid calling __glXInitialize as its kinda slow.
942 */
943 _X_HIDDEN CARD8
944 __glXSetupForCommand(Display * dpy)
945 {
946 GLXContext gc;
947 __GLXdisplayPrivate *priv;
948
949 /* If this thread has a current context, flush its rendering commands */
950 gc = __glXGetCurrentContext();
951 if (gc->currentDpy) {
952 /* Flush rendering buffer of the current context, if any */
953 (void) __glXFlushRenderBuffer(gc, gc->pc);
954
955 if (gc->currentDpy == dpy) {
956 /* Use opcode from gc because its right */
957 return gc->majorOpcode;
958 }
959 else {
960 /*
961 ** Have to get info about argument dpy because it might be to
962 ** a different server
963 */
964 }
965 }
966
967 /* Forced to lookup extension via the slow initialize route */
968 priv = __glXInitialize(dpy);
969 if (!priv) {
970 return 0;
971 }
972 return priv->majorOpcode;
973 }
974
975 /**
976 * Flush the drawing command transport buffer.
977 *
978 * \param ctx Context whose transport buffer is to be flushed.
979 * \param pc Pointer to first unused buffer location.
980 *
981 * \todo
982 * Modify this function to use \c ctx->pc instead of the explicit
983 * \c pc parameter.
984 */
985 _X_HIDDEN GLubyte *
986 __glXFlushRenderBuffer(__GLXcontext * ctx, GLubyte * pc)
987 {
988 Display *const dpy = ctx->currentDpy;
989 #ifdef USE_XCB
990 xcb_connection_t *c = XGetXCBConnection(dpy);
991 #else
992 xGLXRenderReq *req;
993 #endif /* USE_XCB */
994 const GLint size = pc - ctx->buf;
995
996 if ((dpy != NULL) && (size > 0)) {
997 #ifdef USE_XCB
998 xcb_glx_render(c, ctx->currentContextTag, size,
999 (const uint8_t *) ctx->buf);
1000 #else
1001 /* Send the entire buffer as an X request */
1002 LockDisplay(dpy);
1003 GetReq(GLXRender, req);
1004 req->reqType = ctx->majorOpcode;
1005 req->glxCode = X_GLXRender;
1006 req->contextTag = ctx->currentContextTag;
1007 req->length += (size + 3) >> 2;
1008 _XSend(dpy, (char *) ctx->buf, size);
1009 UnlockDisplay(dpy);
1010 SyncHandle();
1011 #endif
1012 }
1013
1014 /* Reset pointer and return it */
1015 ctx->pc = ctx->buf;
1016 return ctx->pc;
1017 }
1018
1019
1020 /**
1021 * Send a portion of a GLXRenderLarge command to the server. The advantage of
1022 * this function over \c __glXSendLargeCommand is that callers can use the
1023 * data buffer in the GLX context and may be able to avoid allocating an
1024 * extra buffer. The disadvantage is the clients will have to do more
1025 * GLX protocol work (i.e., calculating \c totalRequests, etc.).
1026 *
1027 * \sa __glXSendLargeCommand
1028 *
1029 * \param gc GLX context
1030 * \param requestNumber Which part of the whole command is this? The first
1031 * request is 1.
1032 * \param totalRequests How many requests will there be?
1033 * \param data Command data.
1034 * \param dataLen Size, in bytes, of the command data.
1035 */
1036 _X_HIDDEN void
1037 __glXSendLargeChunk(__GLXcontext * gc, GLint requestNumber,
1038 GLint totalRequests, const GLvoid * data, GLint dataLen)
1039 {
1040 Display *dpy = gc->currentDpy;
1041 #ifdef USE_XCB
1042 xcb_connection_t *c = XGetXCBConnection(dpy);
1043 xcb_glx_render_large(c, gc->currentContextTag, requestNumber,
1044 totalRequests, dataLen, data);
1045 #else
1046 xGLXRenderLargeReq *req;
1047
1048 if (requestNumber == 1) {
1049 LockDisplay(dpy);
1050 }
1051
1052 GetReq(GLXRenderLarge, req);
1053 req->reqType = gc->majorOpcode;
1054 req->glxCode = X_GLXRenderLarge;
1055 req->contextTag = gc->currentContextTag;
1056 req->length += (dataLen + 3) >> 2;
1057 req->requestNumber = requestNumber;
1058 req->requestTotal = totalRequests;
1059 req->dataBytes = dataLen;
1060 Data(dpy, data, dataLen);
1061
1062 if (requestNumber == totalRequests) {
1063 UnlockDisplay(dpy);
1064 SyncHandle();
1065 }
1066 #endif /* USE_XCB */
1067 }
1068
1069
1070 /**
1071 * Send a command that is too large for the GLXRender protocol request.
1072 *
1073 * Send a large command, one that is too large for some reason to
1074 * send using the GLXRender protocol request. One reason to send
1075 * a large command is to avoid copying the data.
1076 *
1077 * \param ctx GLX context
1078 * \param header Header data.
1079 * \param headerLen Size, in bytes, of the header data. It is assumed that
1080 * the header data will always be small enough to fit in
1081 * a single X protocol packet.
1082 * \param data Command data.
1083 * \param dataLen Size, in bytes, of the command data.
1084 */
1085 _X_HIDDEN void
1086 __glXSendLargeCommand(__GLXcontext * ctx,
1087 const GLvoid * header, GLint headerLen,
1088 const GLvoid * data, GLint dataLen)
1089 {
1090 GLint maxSize;
1091 GLint totalRequests, requestNumber;
1092
1093 /*
1094 ** Calculate the maximum amount of data can be stuffed into a single
1095 ** packet. sz_xGLXRenderReq is added because bufSize is the maximum
1096 ** packet size minus sz_xGLXRenderReq.
1097 */
1098 maxSize = (ctx->bufSize + sz_xGLXRenderReq) - sz_xGLXRenderLargeReq;
1099 totalRequests = 1 + (dataLen / maxSize);
1100 if (dataLen % maxSize)
1101 totalRequests++;
1102
1103 /*
1104 ** Send all of the command, except the large array, as one request.
1105 */
1106 assert(headerLen <= maxSize);
1107 __glXSendLargeChunk(ctx, 1, totalRequests, header, headerLen);
1108
1109 /*
1110 ** Send enough requests until the whole array is sent.
1111 */
1112 for (requestNumber = 2; requestNumber <= (totalRequests - 1);
1113 requestNumber++) {
1114 __glXSendLargeChunk(ctx, requestNumber, totalRequests, data, maxSize);
1115 data = (const GLvoid *) (((const GLubyte *) data) + maxSize);
1116 dataLen -= maxSize;
1117 assert(dataLen > 0);
1118 }
1119
1120 assert(dataLen <= maxSize);
1121 __glXSendLargeChunk(ctx, requestNumber, totalRequests, data, dataLen);
1122 }
1123
1124 /************************************************************************/
1125
1126 #ifdef DEBUG
1127 _X_HIDDEN void
1128 __glXDumpDrawBuffer(__GLXcontext * ctx)
1129 {
1130 GLubyte *p = ctx->buf;
1131 GLubyte *end = ctx->pc;
1132 GLushort opcode, length;
1133
1134 while (p < end) {
1135 /* Fetch opcode */
1136 opcode = *((GLushort *) p);
1137 length = *((GLushort *) (p + 2));
1138 printf("%2x: %5d: ", opcode, length);
1139 length -= 4;
1140 p += 4;
1141 while (length > 0) {
1142 printf("%08x ", *((unsigned *) p));
1143 p += 4;
1144 length -= 4;
1145 }
1146 printf("\n");
1147 }
1148 }
1149 #endif