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