b6bdbd52bb5b599d9f7a44d3f9f5647b6a983652
[mesa.git] / src / glx / x11 / 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 "glapi.h"
45 #include "glxextensions.h"
46 #include "glcontextmodes.h"
47
48 #ifdef USE_XCB
49 #include <X11/Xlib-xcb.h>
50 #include <xcb/xcb.h>
51 #include <xcb/glx.h>
52 #endif
53
54
55 #ifdef DEBUG
56 void __glXDumpDrawBuffer(__GLXcontext * ctx);
57 #endif
58
59 #ifdef USE_SPARC_ASM
60 static void _glx_mesa_init_sparc_glapi_relocs(void);
61 static int _mesa_sparc_needs_init = 1;
62 #define INIT_MESA_SPARC do { \
63 if (_mesa_sparc_needs_init) { \
64 _glx_mesa_init_sparc_glapi_relocs(); \
65 _mesa_sparc_needs_init = 0; \
66 } \
67 } while(0)
68 #else
69 #define INIT_MESA_SPARC do { } while(0)
70 #endif
71
72 /*
73 ** You can set this cell to 1 to force the gl drawing stuff to be
74 ** one command per packet
75 */
76 _X_HIDDEN int __glXDebug = 0;
77
78 /* Extension required boiler plate */
79
80 static char *__glXExtensionName = GLX_EXTENSION_NAME;
81 XExtensionInfo *__glXExtensionInfo = NULL;
82
83 static /* const */ char *error_list[] = {
84 "GLXBadContext",
85 "GLXBadContextState",
86 "GLXBadDrawable",
87 "GLXBadPixmap",
88 "GLXBadContextTag",
89 "GLXBadCurrentWindow",
90 "GLXBadRenderRequest",
91 "GLXBadLargeRequest",
92 "GLXUnsupportedPrivateRequest",
93 "GLXBadFBConfig",
94 "GLXBadPbuffer",
95 "GLXBadCurrentDrawable",
96 "GLXBadWindow",
97 };
98
99 static int
100 __glXCloseDisplay(Display * dpy, XExtCodes * codes)
101 {
102 GLXContext gc;
103
104 gc = __glXGetCurrentContext();
105 if (dpy == gc->currentDpy) {
106 __glXSetCurrentContextNull();
107 __glXFreeContext(gc);
108 }
109
110 return XextRemoveDisplay(__glXExtensionInfo, dpy);
111 }
112
113
114 static
115 XEXT_GENERATE_ERROR_STRING(__glXErrorString, __glXExtensionName,
116 __GLX_NUMBER_ERRORS, error_list)
117
118 static /* const */ XExtensionHooks __glXExtensionHooks = {
119 NULL, /* create_gc */
120 NULL, /* copy_gc */
121 NULL, /* flush_gc */
122 NULL, /* free_gc */
123 NULL, /* create_font */
124 NULL, /* free_font */
125 __glXCloseDisplay, /* close_display */
126 NULL, /* wire_to_event */
127 NULL, /* event_to_wire */
128 NULL, /* error */
129 __glXErrorString, /* error_string */
130 };
131
132 static
133 XEXT_GENERATE_FIND_DISPLAY(__glXFindDisplay, __glXExtensionInfo,
134 __glXExtensionName, &__glXExtensionHooks,
135 __GLX_NUMBER_EVENTS, NULL)
136
137 /************************************************************************/
138 /*
139 ** Free the per screen configs data as well as the array of
140 ** __glXScreenConfigs.
141 */
142 static void FreeScreenConfigs(__GLXdisplayPrivate * priv)
143 {
144 __GLXscreenConfigs *psc;
145 GLint i, screens;
146
147 /* Free screen configuration information */
148 psc = priv->screenConfigs;
149 screens = ScreenCount(priv->dpy);
150 for (i = 0; i < screens; i++, psc++) {
151 if (psc->configs) {
152 _gl_context_modes_destroy(psc->configs);
153 if (psc->effectiveGLXexts)
154 Xfree(psc->effectiveGLXexts);
155 psc->configs = NULL; /* NOTE: just for paranoia */
156 }
157 if (psc->visuals) {
158 _gl_context_modes_destroy(psc->visuals);
159 psc->visuals = NULL; /* NOTE: just for paranoia */
160 }
161 Xfree((char *) psc->serverGLXexts);
162
163 #ifdef GLX_DIRECT_RENDERING
164 if (psc->driScreen) {
165 psc->driScreen->destroyScreen(psc);
166 __glxHashDestroy(psc->drawHash);
167 XFree(psc->driScreen);
168 psc->driScreen = NULL;
169 }
170 #endif
171 }
172 XFree((char *) priv->screenConfigs);
173 priv->screenConfigs = NULL;
174 }
175
176 /*
177 ** Release the private memory referred to in a display private
178 ** structure. The caller will free the extension structure.
179 */
180 static int
181 __glXFreeDisplayPrivate(XExtData * extension)
182 {
183 __GLXdisplayPrivate *priv;
184
185 priv = (__GLXdisplayPrivate *) extension->private_data;
186 FreeScreenConfigs(priv);
187 if (priv->serverGLXvendor) {
188 Xfree((char *) priv->serverGLXvendor);
189 priv->serverGLXvendor = 0x0; /* to protect against double free's */
190 }
191 if (priv->serverGLXversion) {
192 Xfree((char *) priv->serverGLXversion);
193 priv->serverGLXversion = 0x0; /* to protect against double free's */
194 }
195
196 #ifdef GLX_DIRECT_RENDERING
197 /* Free the direct rendering per display data */
198 if (priv->driswDisplay)
199 (*priv->driswDisplay->destroyDisplay) (priv->driswDisplay);
200 priv->driswDisplay = NULL;
201
202 if (priv->driDisplay)
203 (*priv->driDisplay->destroyDisplay) (priv->driDisplay);
204 priv->driDisplay = NULL;
205
206 if (priv->dri2Display)
207 (*priv->dri2Display->destroyDisplay) (priv->dri2Display);
208 priv->dri2Display = NULL;
209 #endif
210
211 Xfree((char *) priv);
212 return 0;
213 }
214
215 /************************************************************************/
216
217 /*
218 ** Query the version of the GLX extension. This procedure works even if
219 ** the client extension is not completely set up.
220 */
221 static Bool
222 QueryVersion(Display * dpy, int opcode, int *major, int *minor)
223 {
224 xGLXQueryVersionReq *req;
225 xGLXQueryVersionReply reply;
226
227 /* Send the glXQueryVersion request */
228 LockDisplay(dpy);
229 GetReq(GLXQueryVersion, req);
230 req->reqType = opcode;
231 req->glxCode = X_GLXQueryVersion;
232 req->majorVersion = GLX_MAJOR_VERSION;
233 req->minorVersion = GLX_MINOR_VERSION;
234 _XReply(dpy, (xReply *) & reply, 0, False);
235 UnlockDisplay(dpy);
236 SyncHandle();
237
238 if (reply.majorVersion != GLX_MAJOR_VERSION) {
239 /*
240 ** The server does not support the same major release as this
241 ** client.
242 */
243 return GL_FALSE;
244 }
245 *major = reply.majorVersion;
246 *minor = min(reply.minorVersion, GLX_MINOR_VERSION);
247 return GL_TRUE;
248 }
249
250
251 _X_HIDDEN void
252 __glXInitializeVisualConfigFromTags(__GLcontextModes * config, int count,
253 const INT32 * bp, Bool tagged_only,
254 Bool fbconfig_style_tags)
255 {
256 int i;
257
258 if (!tagged_only) {
259 /* Copy in the first set of properties */
260 config->visualID = *bp++;
261
262 config->visualType = _gl_convert_from_x_visual_type(*bp++);
263
264 config->rgbMode = *bp++;
265
266 config->redBits = *bp++;
267 config->greenBits = *bp++;
268 config->blueBits = *bp++;
269 config->alphaBits = *bp++;
270 config->accumRedBits = *bp++;
271 config->accumGreenBits = *bp++;
272 config->accumBlueBits = *bp++;
273 config->accumAlphaBits = *bp++;
274
275 config->doubleBufferMode = *bp++;
276 config->stereoMode = *bp++;
277
278 config->rgbBits = *bp++;
279 config->depthBits = *bp++;
280 config->stencilBits = *bp++;
281 config->numAuxBuffers = *bp++;
282 config->level = *bp++;
283
284 count -= __GLX_MIN_CONFIG_PROPS;
285 }
286
287 /*
288 ** Additional properties may be in a list at the end
289 ** of the reply. They are in pairs of property type
290 ** and property value.
291 */
292
293 #define FETCH_OR_SET(tag) \
294 config-> tag = ( fbconfig_style_tags ) ? *bp++ : 1
295
296 for (i = 0; i < count; i += 2) {
297 switch (*bp++) {
298 case GLX_RGBA:
299 FETCH_OR_SET(rgbMode);
300 break;
301 case GLX_BUFFER_SIZE:
302 config->rgbBits = *bp++;
303 break;
304 case GLX_LEVEL:
305 config->level = *bp++;
306 break;
307 case GLX_DOUBLEBUFFER:
308 FETCH_OR_SET(doubleBufferMode);
309 break;
310 case GLX_STEREO:
311 FETCH_OR_SET(stereoMode);
312 break;
313 case GLX_AUX_BUFFERS:
314 config->numAuxBuffers = *bp++;
315 break;
316 case GLX_RED_SIZE:
317 config->redBits = *bp++;
318 break;
319 case GLX_GREEN_SIZE:
320 config->greenBits = *bp++;
321 break;
322 case GLX_BLUE_SIZE:
323 config->blueBits = *bp++;
324 break;
325 case GLX_ALPHA_SIZE:
326 config->alphaBits = *bp++;
327 break;
328 case GLX_DEPTH_SIZE:
329 config->depthBits = *bp++;
330 break;
331 case GLX_STENCIL_SIZE:
332 config->stencilBits = *bp++;
333 break;
334 case GLX_ACCUM_RED_SIZE:
335 config->accumRedBits = *bp++;
336 break;
337 case GLX_ACCUM_GREEN_SIZE:
338 config->accumGreenBits = *bp++;
339 break;
340 case GLX_ACCUM_BLUE_SIZE:
341 config->accumBlueBits = *bp++;
342 break;
343 case GLX_ACCUM_ALPHA_SIZE:
344 config->accumAlphaBits = *bp++;
345 break;
346 case GLX_VISUAL_CAVEAT_EXT:
347 config->visualRating = *bp++;
348 break;
349 case GLX_X_VISUAL_TYPE:
350 config->visualType = *bp++;
351 break;
352 case GLX_TRANSPARENT_TYPE:
353 config->transparentPixel = *bp++;
354 break;
355 case GLX_TRANSPARENT_INDEX_VALUE:
356 config->transparentIndex = *bp++;
357 break;
358 case GLX_TRANSPARENT_RED_VALUE:
359 config->transparentRed = *bp++;
360 break;
361 case GLX_TRANSPARENT_GREEN_VALUE:
362 config->transparentGreen = *bp++;
363 break;
364 case GLX_TRANSPARENT_BLUE_VALUE:
365 config->transparentBlue = *bp++;
366 break;
367 case GLX_TRANSPARENT_ALPHA_VALUE:
368 config->transparentAlpha = *bp++;
369 break;
370 case GLX_VISUAL_ID:
371 config->visualID = *bp++;
372 break;
373 case GLX_DRAWABLE_TYPE:
374 config->drawableType = *bp++;
375 break;
376 case GLX_RENDER_TYPE:
377 config->renderType = *bp++;
378 break;
379 case GLX_X_RENDERABLE:
380 config->xRenderable = *bp++;
381 break;
382 case GLX_FBCONFIG_ID:
383 config->fbconfigID = *bp++;
384 break;
385 case GLX_MAX_PBUFFER_WIDTH:
386 config->maxPbufferWidth = *bp++;
387 break;
388 case GLX_MAX_PBUFFER_HEIGHT:
389 config->maxPbufferHeight = *bp++;
390 break;
391 case GLX_MAX_PBUFFER_PIXELS:
392 config->maxPbufferPixels = *bp++;
393 break;
394 case GLX_OPTIMAL_PBUFFER_WIDTH_SGIX:
395 config->optimalPbufferWidth = *bp++;
396 break;
397 case GLX_OPTIMAL_PBUFFER_HEIGHT_SGIX:
398 config->optimalPbufferHeight = *bp++;
399 break;
400 case GLX_VISUAL_SELECT_GROUP_SGIX:
401 config->visualSelectGroup = *bp++;
402 break;
403 case GLX_SWAP_METHOD_OML:
404 config->swapMethod = *bp++;
405 break;
406 case GLX_SAMPLE_BUFFERS_SGIS:
407 config->sampleBuffers = *bp++;
408 break;
409 case GLX_SAMPLES_SGIS:
410 config->samples = *bp++;
411 break;
412 case GLX_BIND_TO_TEXTURE_RGB_EXT:
413 config->bindToTextureRgb = *bp++;
414 break;
415 case GLX_BIND_TO_TEXTURE_RGBA_EXT:
416 config->bindToTextureRgba = *bp++;
417 break;
418 case GLX_BIND_TO_MIPMAP_TEXTURE_EXT:
419 config->bindToMipmapTexture = *bp++;
420 break;
421 case GLX_BIND_TO_TEXTURE_TARGETS_EXT:
422 config->bindToTextureTargets = *bp++;
423 break;
424 case GLX_Y_INVERTED_EXT:
425 config->yInverted = *bp++;
426 break;
427 case None:
428 i = count;
429 break;
430 default:
431 break;
432 }
433 }
434
435 config->renderType =
436 (config->rgbMode) ? GLX_RGBA_BIT : GLX_COLOR_INDEX_BIT;
437
438 config->haveAccumBuffer = ((config->accumRedBits +
439 config->accumGreenBits +
440 config->accumBlueBits +
441 config->accumAlphaBits) > 0);
442 config->haveDepthBuffer = (config->depthBits > 0);
443 config->haveStencilBuffer = (config->stencilBits > 0);
444 }
445
446 static __GLcontextModes *
447 createConfigsFromProperties(Display * dpy, int nvisuals, int nprops,
448 int screen, GLboolean tagged_only)
449 {
450 INT32 buf[__GLX_TOTAL_CONFIG], *props;
451 unsigned prop_size;
452 __GLcontextModes *modes, *m;
453 int i;
454
455 if (nprops == 0)
456 return NULL;
457
458 /* FIXME: Is the __GLX_MIN_CONFIG_PROPS test correct for FBconfigs? */
459
460 /* Check number of properties */
461 if (nprops < __GLX_MIN_CONFIG_PROPS || nprops > __GLX_MAX_CONFIG_PROPS)
462 return NULL;
463
464 /* Allocate memory for our config structure */
465 modes = _gl_context_modes_create(nvisuals, sizeof(__GLcontextModes));
466 if (!modes)
467 return NULL;
468
469 prop_size = nprops * __GLX_SIZE_INT32;
470 if (prop_size <= sizeof(buf))
471 props = buf;
472 else
473 props = Xmalloc(prop_size);
474
475 /* Read each config structure and convert it into our format */
476 m = modes;
477 for (i = 0; i < nvisuals; i++) {
478 _XRead(dpy, (char *) props, prop_size);
479 /* Older X servers don't send this so we default it here. */
480 m->drawableType = GLX_WINDOW_BIT;
481 __glXInitializeVisualConfigFromTags(m, nprops, props,
482 tagged_only, GL_TRUE);
483 m->screen = screen;
484 m = m->next;
485 }
486
487 if (props != buf)
488 Xfree(props);
489
490 return modes;
491 }
492
493 static GLboolean
494 getVisualConfigs(Display * dpy, __GLXdisplayPrivate * priv, int screen)
495 {
496 xGLXGetVisualConfigsReq *req;
497 __GLXscreenConfigs *psc;
498 xGLXGetVisualConfigsReply reply;
499
500 LockDisplay(dpy);
501
502 psc = priv->screenConfigs + screen;
503 psc->visuals = NULL;
504 GetReq(GLXGetVisualConfigs, req);
505 req->reqType = priv->majorOpcode;
506 req->glxCode = X_GLXGetVisualConfigs;
507 req->screen = screen;
508
509 if (!_XReply(dpy, (xReply *) & reply, 0, False))
510 goto out;
511
512 psc->visuals = createConfigsFromProperties(dpy,
513 reply.numVisuals,
514 reply.numProps,
515 screen, GL_FALSE);
516
517 out:
518 UnlockDisplay(dpy);
519 return psc->visuals != NULL;
520 }
521
522 static GLboolean
523 getFBConfigs(Display * dpy, __GLXdisplayPrivate * priv, int screen)
524 {
525 xGLXGetFBConfigsReq *fb_req;
526 xGLXGetFBConfigsSGIXReq *sgi_req;
527 xGLXVendorPrivateWithReplyReq *vpreq;
528 xGLXGetFBConfigsReply reply;
529 __GLXscreenConfigs *psc;
530
531 psc = priv->screenConfigs + screen;
532 psc->serverGLXexts = __glXGetStringFromServer(dpy, priv->majorOpcode,
533 X_GLXQueryServerString,
534 screen, GLX_EXTENSIONS);
535
536 LockDisplay(dpy);
537
538 psc->configs = NULL;
539 if (atof(priv->serverGLXversion) >= 1.3) {
540 GetReq(GLXGetFBConfigs, fb_req);
541 fb_req->reqType = priv->majorOpcode;
542 fb_req->glxCode = X_GLXGetFBConfigs;
543 fb_req->screen = screen;
544 }
545 else if (strstr(psc->serverGLXexts, "GLX_SGIX_fbconfig") != NULL) {
546 GetReqExtra(GLXVendorPrivateWithReply,
547 sz_xGLXGetFBConfigsSGIXReq +
548 sz_xGLXVendorPrivateWithReplyReq, vpreq);
549 sgi_req = (xGLXGetFBConfigsSGIXReq *) vpreq;
550 sgi_req->reqType = priv->majorOpcode;
551 sgi_req->glxCode = X_GLXVendorPrivateWithReply;
552 sgi_req->vendorCode = X_GLXvop_GetFBConfigsSGIX;
553 sgi_req->screen = screen;
554 }
555 else
556 goto out;
557
558 if (!_XReply(dpy, (xReply *) & reply, 0, False))
559 goto out;
560
561 psc->configs = createConfigsFromProperties(dpy,
562 reply.numFBConfigs,
563 reply.numAttribs * 2,
564 screen, GL_TRUE);
565
566 out:
567 UnlockDisplay(dpy);
568 return psc->configs != NULL;
569 }
570
571 /*
572 ** Allocate the memory for the per screen configs for each screen.
573 ** If that works then fetch the per screen configs data.
574 */
575 static Bool
576 AllocAndFetchScreenConfigs(Display * dpy, __GLXdisplayPrivate * priv)
577 {
578 __GLXscreenConfigs *psc;
579 GLint i, screens;
580
581 /*
582 ** First allocate memory for the array of per screen configs.
583 */
584 screens = ScreenCount(dpy);
585 psc = (__GLXscreenConfigs *) Xmalloc(screens * sizeof(__GLXscreenConfigs));
586 if (!psc) {
587 return GL_FALSE;
588 }
589 memset(psc, 0, screens * sizeof(__GLXscreenConfigs));
590 priv->screenConfigs = psc;
591
592 priv->serverGLXversion = __glXGetStringFromServer(dpy, priv->majorOpcode,
593 X_GLXQueryServerString,
594 0, GLX_VERSION);
595 if (priv->serverGLXversion == NULL) {
596 FreeScreenConfigs(priv);
597 return GL_FALSE;
598 }
599
600 for (i = 0; i < screens; i++, psc++) {
601 getVisualConfigs(dpy, priv, i);
602 getFBConfigs(dpy, priv, i);
603
604 #ifdef GLX_DIRECT_RENDERING
605 psc->scr = i;
606 psc->dpy = dpy;
607 psc->drawHash = __glxHashCreate();
608 if (psc->drawHash == NULL)
609 continue;
610
611 if (priv->dri2Display)
612 psc->driScreen = (*priv->dri2Display->createScreen) (psc, i, priv);
613
614 if (psc->driScreen == NULL && priv->driDisplay)
615 psc->driScreen = (*priv->driDisplay->createScreen) (psc, i, priv);
616
617 if (psc->driScreen == NULL && priv->driswDisplay)
618 psc->driScreen = (*priv->driswDisplay->createScreen) (psc, i, priv);
619
620 if (psc->driScreen == NULL) {
621 __glxHashDestroy(psc->drawHash);
622 psc->drawHash = NULL;
623 }
624 #endif
625 }
626 SyncHandle();
627 return GL_TRUE;
628 }
629
630 /*
631 ** Initialize the client side extension code.
632 */
633 _X_HIDDEN __GLXdisplayPrivate *
634 __glXInitialize(Display * dpy)
635 {
636 XExtDisplayInfo *info = __glXFindDisplay(dpy);
637 XExtData **privList, *private, *found;
638 __GLXdisplayPrivate *dpyPriv;
639 XEDataObject dataObj;
640 int major, minor;
641 #ifdef GLX_DIRECT_RENDERING
642 Bool glx_direct, glx_accel;
643 #endif
644
645 #if defined(USE_XTHREADS)
646 {
647 static int firstCall = 1;
648 if (firstCall) {
649 /* initialize the GLX mutexes */
650 xmutex_init(&__glXmutex);
651 firstCall = 0;
652 }
653 }
654 #endif
655
656 INIT_MESA_SPARC;
657 /* The one and only long long lock */
658 __glXLock();
659
660 if (!XextHasExtension(info)) {
661 /* No GLX extension supported by this server. Oh well. */
662 __glXUnlock();
663 XMissingExtension(dpy, __glXExtensionName);
664 return 0;
665 }
666
667 /* See if a display private already exists. If so, return it */
668 dataObj.display = dpy;
669 privList = XEHeadOfExtensionList(dataObj);
670 found = XFindOnExtensionList(privList, info->codes->extension);
671 if (found) {
672 __glXUnlock();
673 return (__GLXdisplayPrivate *) found->private_data;
674 }
675
676 /* See if the versions are compatible */
677 if (!QueryVersion(dpy, info->codes->major_opcode, &major, &minor)) {
678 /* The client and server do not agree on versions. Punt. */
679 __glXUnlock();
680 return 0;
681 }
682
683 /*
684 ** Allocate memory for all the pieces needed for this buffer.
685 */
686 private = (XExtData *) Xmalloc(sizeof(XExtData));
687 if (!private) {
688 __glXUnlock();
689 return 0;
690 }
691 dpyPriv = (__GLXdisplayPrivate *) Xcalloc(1, sizeof(__GLXdisplayPrivate));
692 if (!dpyPriv) {
693 __glXUnlock();
694 Xfree((char *) private);
695 return 0;
696 }
697
698 /*
699 ** Init the display private and then read in the screen config
700 ** structures from the server.
701 */
702 dpyPriv->majorOpcode = info->codes->major_opcode;
703 dpyPriv->majorVersion = major;
704 dpyPriv->minorVersion = minor;
705 dpyPriv->dpy = dpy;
706
707 dpyPriv->serverGLXvendor = 0x0;
708 dpyPriv->serverGLXversion = 0x0;
709
710 #ifdef GLX_DIRECT_RENDERING
711 glx_direct = (getenv("LIBGL_ALWAYS_INDIRECT") == NULL);
712 glx_accel = (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL);
713
714 /*
715 ** Initialize the direct rendering per display data and functions.
716 ** Note: This _must_ be done before calling any other DRI routines
717 ** (e.g., those called in AllocAndFetchScreenConfigs).
718 */
719 if (glx_direct && glx_accel) {
720 dpyPriv->dri2Display = dri2CreateDisplay(dpy);
721 dpyPriv->driDisplay = driCreateDisplay(dpy);
722 }
723 if (glx_direct)
724 dpyPriv->driswDisplay = driswCreateDisplay(dpy);
725 #endif
726
727 if (!AllocAndFetchScreenConfigs(dpy, dpyPriv)) {
728 __glXUnlock();
729 Xfree((char *) dpyPriv);
730 Xfree((char *) private);
731 return 0;
732 }
733
734 /*
735 ** Fill in the private structure. This is the actual structure that
736 ** hangs off of the Display structure. Our private structure is
737 ** referred to by this structure. Got that?
738 */
739 private->number = info->codes->extension;
740 private->next = 0;
741 private->free_private = __glXFreeDisplayPrivate;
742 private->private_data = (char *) dpyPriv;
743 XAddToExtensionList(privList, private);
744
745 if (dpyPriv->majorVersion == 1 && dpyPriv->minorVersion >= 1) {
746 __glXClientInfo(dpy, dpyPriv->majorOpcode);
747 }
748 __glXUnlock();
749
750 return dpyPriv;
751 }
752
753 /*
754 ** Setup for sending a GLX command on dpy. Make sure the extension is
755 ** initialized. Try to avoid calling __glXInitialize as its kinda slow.
756 */
757 _X_HIDDEN CARD8
758 __glXSetupForCommand(Display * dpy)
759 {
760 GLXContext gc;
761 __GLXdisplayPrivate *priv;
762
763 /* If this thread has a current context, flush its rendering commands */
764 gc = __glXGetCurrentContext();
765 if (gc->currentDpy) {
766 /* Flush rendering buffer of the current context, if any */
767 (void) __glXFlushRenderBuffer(gc, gc->pc);
768
769 if (gc->currentDpy == dpy) {
770 /* Use opcode from gc because its right */
771 INIT_MESA_SPARC;
772 return gc->majorOpcode;
773 }
774 else {
775 /*
776 ** Have to get info about argument dpy because it might be to
777 ** a different server
778 */
779 }
780 }
781
782 /* Forced to lookup extension via the slow initialize route */
783 priv = __glXInitialize(dpy);
784 if (!priv) {
785 return 0;
786 }
787 return priv->majorOpcode;
788 }
789
790 /**
791 * Flush the drawing command transport buffer.
792 *
793 * \param ctx Context whose transport buffer is to be flushed.
794 * \param pc Pointer to first unused buffer location.
795 *
796 * \todo
797 * Modify this function to use \c ctx->pc instead of the explicit
798 * \c pc parameter.
799 */
800 _X_HIDDEN GLubyte *
801 __glXFlushRenderBuffer(__GLXcontext * ctx, GLubyte * pc)
802 {
803 Display *const dpy = ctx->currentDpy;
804 #ifdef USE_XCB
805 xcb_connection_t *c = XGetXCBConnection(dpy);
806 #else
807 xGLXRenderReq *req;
808 #endif /* USE_XCB */
809 const GLint size = pc - ctx->buf;
810
811 if ((dpy != NULL) && (size > 0)) {
812 #ifdef USE_XCB
813 xcb_glx_render(c, ctx->currentContextTag, size,
814 (const uint8_t *) ctx->buf);
815 #else
816 /* Send the entire buffer as an X request */
817 LockDisplay(dpy);
818 GetReq(GLXRender, req);
819 req->reqType = ctx->majorOpcode;
820 req->glxCode = X_GLXRender;
821 req->contextTag = ctx->currentContextTag;
822 req->length += (size + 3) >> 2;
823 _XSend(dpy, (char *) ctx->buf, size);
824 UnlockDisplay(dpy);
825 SyncHandle();
826 #endif
827 }
828
829 /* Reset pointer and return it */
830 ctx->pc = ctx->buf;
831 return ctx->pc;
832 }
833
834
835 /**
836 * Send a portion of a GLXRenderLarge command to the server. The advantage of
837 * this function over \c __glXSendLargeCommand is that callers can use the
838 * data buffer in the GLX context and may be able to avoid allocating an
839 * extra buffer. The disadvantage is the clients will have to do more
840 * GLX protocol work (i.e., calculating \c totalRequests, etc.).
841 *
842 * \sa __glXSendLargeCommand
843 *
844 * \param gc GLX context
845 * \param requestNumber Which part of the whole command is this? The first
846 * request is 1.
847 * \param totalRequests How many requests will there be?
848 * \param data Command data.
849 * \param dataLen Size, in bytes, of the command data.
850 */
851 _X_HIDDEN void
852 __glXSendLargeChunk(__GLXcontext * gc, GLint requestNumber,
853 GLint totalRequests, const GLvoid * data, GLint dataLen)
854 {
855 Display *dpy = gc->currentDpy;
856 #ifdef USE_XCB
857 xcb_connection_t *c = XGetXCBConnection(dpy);
858 xcb_glx_render_large(c, gc->currentContextTag, requestNumber,
859 totalRequests, dataLen, data);
860 #else
861 xGLXRenderLargeReq *req;
862
863 if (requestNumber == 1) {
864 LockDisplay(dpy);
865 }
866
867 GetReq(GLXRenderLarge, req);
868 req->reqType = gc->majorOpcode;
869 req->glxCode = X_GLXRenderLarge;
870 req->contextTag = gc->currentContextTag;
871 req->length += (dataLen + 3) >> 2;
872 req->requestNumber = requestNumber;
873 req->requestTotal = totalRequests;
874 req->dataBytes = dataLen;
875 Data(dpy, data, dataLen);
876
877 if (requestNumber == totalRequests) {
878 UnlockDisplay(dpy);
879 SyncHandle();
880 }
881 #endif /* USE_XCB */
882 }
883
884
885 /**
886 * Send a command that is too large for the GLXRender protocol request.
887 *
888 * Send a large command, one that is too large for some reason to
889 * send using the GLXRender protocol request. One reason to send
890 * a large command is to avoid copying the data.
891 *
892 * \param ctx GLX context
893 * \param header Header data.
894 * \param headerLen Size, in bytes, of the header data. It is assumed that
895 * the header data will always be small enough to fit in
896 * a single X protocol packet.
897 * \param data Command data.
898 * \param dataLen Size, in bytes, of the command data.
899 */
900 _X_HIDDEN void
901 __glXSendLargeCommand(__GLXcontext * ctx,
902 const GLvoid * header, GLint headerLen,
903 const GLvoid * data, GLint dataLen)
904 {
905 GLint maxSize;
906 GLint totalRequests, requestNumber;
907
908 /*
909 ** Calculate the maximum amount of data can be stuffed into a single
910 ** packet. sz_xGLXRenderReq is added because bufSize is the maximum
911 ** packet size minus sz_xGLXRenderReq.
912 */
913 maxSize = (ctx->bufSize + sz_xGLXRenderReq) - sz_xGLXRenderLargeReq;
914 totalRequests = 1 + (dataLen / maxSize);
915 if (dataLen % maxSize)
916 totalRequests++;
917
918 /*
919 ** Send all of the command, except the large array, as one request.
920 */
921 assert(headerLen <= maxSize);
922 __glXSendLargeChunk(ctx, 1, totalRequests, header, headerLen);
923
924 /*
925 ** Send enough requests until the whole array is sent.
926 */
927 for (requestNumber = 2; requestNumber <= (totalRequests - 1);
928 requestNumber++) {
929 __glXSendLargeChunk(ctx, requestNumber, totalRequests, data, maxSize);
930 data = (const GLvoid *) (((const GLubyte *) data) + maxSize);
931 dataLen -= maxSize;
932 assert(dataLen > 0);
933 }
934
935 assert(dataLen <= maxSize);
936 __glXSendLargeChunk(ctx, requestNumber, totalRequests, data, dataLen);
937 }
938
939 /************************************************************************/
940
941 #ifdef DEBUG
942 _X_HIDDEN void
943 __glXDumpDrawBuffer(__GLXcontext * ctx)
944 {
945 GLubyte *p = ctx->buf;
946 GLubyte *end = ctx->pc;
947 GLushort opcode, length;
948
949 while (p < end) {
950 /* Fetch opcode */
951 opcode = *((GLushort *) p);
952 length = *((GLushort *) (p + 2));
953 printf("%2x: %5d: ", opcode, length);
954 length -= 4;
955 p += 4;
956 while (length > 0) {
957 printf("%08x ", *((unsigned *) p));
958 p += 4;
959 length -= 4;
960 }
961 printf("\n");
962 }
963 }
964 #endif
965
966 #ifdef USE_SPARC_ASM
967 /*
968 * This is where our dispatch table's bounds are.
969 * And the static mesa_init is taken directly from
970 * Mesa's 'sparc.c' initializer.
971 *
972 * We need something like this here, because this version
973 * of openGL/glx never initializes a Mesa context, and so
974 * the address of the dispatch table pointer never gets stuffed
975 * into the dispatch jump table otherwise.
976 *
977 * It matters only on SPARC, and only if you are using assembler
978 * code instead of C-code indirect dispatch.
979 *
980 * -- FEM, 04.xii.03
981 */
982 extern unsigned int _mesa_sparc_glapi_begin;
983 extern unsigned int _mesa_sparc_glapi_end;
984 extern void __glapi_sparc_icache_flush(unsigned int *);
985
986 static void
987 _glx_mesa_init_sparc_glapi_relocs(void)
988 {
989 unsigned int *insn_ptr, *end_ptr;
990 unsigned long disp_addr;
991
992 insn_ptr = &_mesa_sparc_glapi_begin;
993 end_ptr = &_mesa_sparc_glapi_end;
994 disp_addr = (unsigned long) &_glapi_Dispatch;
995
996 /*
997 * Verbatim from Mesa sparc.c. It's needed because there doesn't
998 * seem to be a better way to do this:
999 *
1000 * UNCONDITIONAL_JUMP ( (*_glapi_Dispatch) + entry_offset )
1001 *
1002 * This code is patching in the ADDRESS of the pointer to the
1003 * dispatch table. Hence, it must be called exactly once, because
1004 * that address is not going to change.
1005 *
1006 * What it points to can change, but Mesa (and hence, we) assume
1007 * that there is only one pointer.
1008 *
1009 */
1010 while (insn_ptr < end_ptr) {
1011 #if ( defined(__sparc_v9__) && ( !defined(__linux__) || defined(__linux_64__) ) )
1012 /*
1013 This code patches for 64-bit addresses. This had better
1014 not happen for Sparc/Linux, no matter what architecture we
1015 are building for. So, don't do this.
1016
1017 The 'defined(__linux_64__)' is used here as a placeholder for
1018 when we do do 64-bit usermode on sparc linux.
1019 */
1020 insn_ptr[0] |= (disp_addr >> (32 + 10));
1021 insn_ptr[1] |= ((disp_addr & 0xffffffff) >> 10);
1022 __glapi_sparc_icache_flush(&insn_ptr[0]);
1023 insn_ptr[2] |= ((disp_addr >> 32) & ((1 << 10) - 1));
1024 insn_ptr[3] |= (disp_addr & ((1 << 10) - 1));
1025 __glapi_sparc_icache_flush(&insn_ptr[2]);
1026 insn_ptr += 11;
1027 #else
1028 insn_ptr[0] |= (disp_addr >> 10);
1029 insn_ptr[1] |= (disp_addr & ((1 << 10) - 1));
1030 __glapi_sparc_icache_flush(&insn_ptr[0]);
1031 insn_ptr += 5;
1032 #endif
1033 }
1034 }
1035 #endif /* sparc ASM in use */