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