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