#include <assert.h>
#include "glxclient.h"
#include <X11/extensions/Xext.h>
#include <X11/extensions/extutil.h>
#include "glxextensions.h"
#include "glcontextmodes.h"
#include "apple_glx.h"
#include "apple_visual.h"
#ifdef USE_XCB
#include <X11/Xlib-xcb.h>
#include <xcb/xcb.h>
#include <xcb/glx.h>
#endif
#ifdef DEBUG
void __glXDumpDrawBuffer(__GLXcontext *ctx);
#endif
#ifdef USE_SPARC_ASM
static void _glx_mesa_init_sparc_glapi_relocs(void);
static int _mesa_sparc_needs_init = 1;
#define INIT_MESA_SPARC { \
if(_mesa_sparc_needs_init) { \
_glx_mesa_init_sparc_glapi_relocs(); \
_mesa_sparc_needs_init = 0; \
} \
}
#else
#define INIT_MESA_SPARC
#endif
_X_HIDDEN int __glXDebug = 0;
static char *__glXExtensionName = GLX_EXTENSION_NAME;
static XExtensionInfo glxext_info_data;
static XExtensionInfo *glXExtensionInfo = &glxext_info_data;
static char *error_list[] = {
"GLXBadContext",
"GLXBadContextState",
"GLXBadDrawable",
"GLXBadPixmap",
"GLXBadContextTag",
"GLXBadCurrentWindow",
"GLXBadRenderRequest",
"GLXBadLargeRequest",
"GLXUnsupportedPrivateRequest",
"GLXBadFBConfig",
"GLXBadPbuffer",
"GLXBadCurrentDrawable",
"GLXBadWindow",
};
static int __glXCloseDisplay(Display *dpy, XExtCodes *codes)
{
GLXContext gc;
gc = __glXGetCurrentContext();
if (dpy == gc->currentDpy) {
__glXSetCurrentContextNull();
__glXFreeContext(gc);
}
return XextRemoveDisplay(glXExtensionInfo, dpy);
}
static char *__glXErrorString(Display *dpy, int code, XExtCodes *codes,
char *buf, int n);
static XExtensionHooks __glXExtensionHooks = {
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
__glXCloseDisplay,
NULL,
NULL,
NULL,
__glXErrorString,
};
XEXT_GENERATE_FIND_DISPLAY(__glXFindDisplay, glXExtensionInfo,
__glXExtensionName, &__glXExtensionHooks,
__GLX_NUMBER_EVENTS, NULL)
static XEXT_GENERATE_ERROR_STRING(__glXErrorString, __glXExtensionName,
__GLX_NUMBER_ERRORS, error_list)
static void FreeScreenConfigs(__GLXdisplayPrivate *priv)
{
__GLXscreenConfigs *psc;
GLint i, screens;
psc = priv->screenConfigs;
screens = ScreenCount(priv->dpy);
for (i = 0; i < screens; i++, psc++) {
if (psc->configs) {
_gl_context_modes_destroy( psc->configs );
if (psc->effectiveGLXexts)
Xfree(psc->effectiveGLXexts);
psc->configs = NULL;
}
if (psc->visuals) {
_gl_context_modes_destroy( psc->visuals );
psc->visuals = NULL;
}
Xfree((char*) psc->serverGLXexts);
#ifdef GLX_DIRECT_RENDERING
if (psc->driScreen) {
psc->driScreen->destroyScreen(psc);
__glxHashDestroy(psc->drawHash);
XFree(psc->driScreen);
psc->driScreen = NULL;
}
#endif
}
XFree((char*) priv->screenConfigs);
priv->screenConfigs = NULL;
}
static int __glXFreeDisplayPrivate(XExtData *extension)
{
__GLXdisplayPrivate *priv;
priv = (__GLXdisplayPrivate*) extension->private_data;
FreeScreenConfigs(priv);
if(priv->serverGLXvendor) {
Xfree((char*)priv->serverGLXvendor);
priv->serverGLXvendor = 0x0;
}
if(priv->serverGLXversion) {
Xfree((char*)priv->serverGLXversion);
priv->serverGLXversion = 0x0;
}
#ifdef GLX_DIRECT_RENDERING
if (priv->driswDisplay)
(*priv->driswDisplay->destroyDisplay)(priv->driswDisplay);
priv->driswDisplay = NULL;
if (priv->driDisplay)
(*priv->driDisplay->destroyDisplay)(priv->driDisplay);
priv->driDisplay = NULL;
#endif
Xfree((char*) priv);
return 0;
}
static Bool QueryVersion(Display *dpy, int opcode, int *major, int *minor)
{
xGLXQueryVersionReq *req;
xGLXQueryVersionReply reply;
LockDisplay(dpy);
GetReq(GLXQueryVersion,req);
req->reqType = opcode;
req->glxCode = X_GLXQueryVersion;
req->majorVersion = GLX_MAJOR_VERSION;
req->minorVersion = GLX_MINOR_VERSION;
_XReply(dpy, (xReply*) &reply, 0, False);
UnlockDisplay(dpy);
SyncHandle();
if (reply.majorVersion != GLX_MAJOR_VERSION) {
return GL_FALSE;
}
*major = reply.majorVersion;
*minor = GLX_MINOR_VERSION;
return GL_TRUE;
}
enum {
IGNORE_GLX_SWAP_METHOD_OML = 0x8060
};
_X_HIDDEN void
__glXInitializeVisualConfigFromTags( __GLcontextModes *config, int count,
const INT32 *bp, Bool tagged_only,
Bool fbconfig_style_tags )
{
int i;
long int tag, tagvalue;
if (!tagged_only) {
config->visualID = *bp++;
config->visualType = _gl_convert_from_x_visual_type( *bp++ );
config->rgbMode = *bp++;
config->redBits = *bp++;
config->greenBits = *bp++;
config->blueBits = *bp++;
config->alphaBits = *bp++;
config->accumRedBits = *bp++;
config->accumGreenBits = *bp++;
config->accumBlueBits = *bp++;
config->accumAlphaBits = *bp++;
config->doubleBufferMode = *bp++;
config->stereoMode = *bp++;
config->rgbBits = *bp++;
config->depthBits = *bp++;
config->stencilBits = *bp++;
config->numAuxBuffers = *bp++;
config->level = *bp++;
config->drawableType = GLX_WINDOW_BIT | GLX_PIXMAP_BIT | GLX_PBUFFER_BIT;
count -= 18;
}
#define FETCH_OR_SET(tag) \
config-> tag = ( fbconfig_style_tags ) ? *bp++ : 1
for (i = 0; i < count; i += 2 ) {
tag = *bp++;
switch(tag) {
case GLX_RGBA:
FETCH_OR_SET( rgbMode );
break;
case GLX_BUFFER_SIZE:
config->rgbBits = *bp++;
break;
case GLX_LEVEL:
config->level = *bp++;
break;
case GLX_DOUBLEBUFFER:
FETCH_OR_SET( doubleBufferMode );
break;
case GLX_STEREO:
FETCH_OR_SET( stereoMode );
break;
case GLX_AUX_BUFFERS:
config->numAuxBuffers = *bp++;
break;
case GLX_RED_SIZE:
config->redBits = *bp++;
break;
case GLX_GREEN_SIZE:
config->greenBits = *bp++;
break;
case GLX_BLUE_SIZE:
config->blueBits = *bp++;
break;
case GLX_ALPHA_SIZE:
config->alphaBits = *bp++;
break;
case GLX_DEPTH_SIZE:
config->depthBits = *bp++;
break;
case GLX_STENCIL_SIZE:
config->stencilBits = *bp++;
break;
case GLX_ACCUM_RED_SIZE:
config->accumRedBits = *bp++;
break;
case GLX_ACCUM_GREEN_SIZE:
config->accumGreenBits = *bp++;
break;
case GLX_ACCUM_BLUE_SIZE:
config->accumBlueBits = *bp++;
break;
case GLX_ACCUM_ALPHA_SIZE:
config->accumAlphaBits = *bp++;
break;
case GLX_VISUAL_CAVEAT_EXT:
config->visualRating = *bp++;
break;
case GLX_X_VISUAL_TYPE:
config->visualType = *bp++;
break;
case GLX_TRANSPARENT_TYPE:
config->transparentPixel = *bp++;
break;
case GLX_TRANSPARENT_INDEX_VALUE:
config->transparentIndex = *bp++;
break;
case GLX_TRANSPARENT_RED_VALUE:
config->transparentRed = *bp++;
break;
case GLX_TRANSPARENT_GREEN_VALUE:
config->transparentGreen = *bp++;
break;
case GLX_TRANSPARENT_BLUE_VALUE:
config->transparentBlue = *bp++;
break;
case GLX_TRANSPARENT_ALPHA_VALUE:
config->transparentAlpha = *bp++;
break;
case GLX_VISUAL_ID:
config->visualID = *bp++;
break;
case GLX_DRAWABLE_TYPE:
config->drawableType = *bp++;
config->drawableType |= GLX_WINDOW_BIT | GLX_PIXMAP_BIT
| GLX_PBUFFER_BIT;
break;
case GLX_RENDER_TYPE:
config->renderType = *bp++;
break;
case GLX_X_RENDERABLE:
config->xRenderable = *bp++;
break;
case GLX_FBCONFIG_ID:
config->fbconfigID = *bp++;
break;
case GLX_MAX_PBUFFER_WIDTH:
config->maxPbufferWidth = *bp++;
break;
case GLX_MAX_PBUFFER_HEIGHT:
config->maxPbufferHeight = *bp++;
break;
case GLX_MAX_PBUFFER_PIXELS:
config->maxPbufferPixels = *bp++;
break;
#if 0
case GLX_OPTIMAL_PBUFFER_WIDTH_SGIX:
config->optimalPbufferWidth = *bp++;
break;
case GLX_OPTIMAL_PBUFFER_HEIGHT_SGIX:
config->optimalPbufferHeight = *bp++;
break;
#endif
#if 0
case GLX_VISUAL_SELECT_GROUP_SGIX:
config->visualSelectGroup = *bp++;
break;
#endif
#if 0
case GLX_SWAP_METHOD_OML:
config->swapMethod = *bp++;
break;
#endif
case GLX_SAMPLE_BUFFERS:
config->sampleBuffers = *bp++;
break;
case GLX_SAMPLES:
config->samples = *bp++;
break;
#if 0
case GLX_BIND_TO_TEXTURE_RGB_EXT:
config->bindToTextureRgb = *bp++;
break;
case GLX_BIND_TO_TEXTURE_RGBA_EXT:
config->bindToTextureRgba = *bp++;
break;
case GLX_BIND_TO_MIPMAP_TEXTURE_EXT:
config->bindToMipmapTexture = *bp++;
break;
case GLX_BIND_TO_TEXTURE_TARGETS_EXT:
config->bindToTextureTargets = *bp++;
break;
case GLX_Y_INVERTED_EXT:
config->yInverted = *bp++;
break;
#endif
case IGNORE_GLX_SWAP_METHOD_OML:
++bp;
break;
case None:
i = count;
break;
default:
if(getenv("LIBGL_DIAGNOSTIC")) {
tagvalue = *bp++;
fprintf(stderr, "WARNING: unknown GLX tag from server: "
"tag 0x%lx value 0x%lx\n",
tag, tagvalue);
}
break;
}
}
config->renderType = (config->rgbMode) ? GLX_RGBA_BIT : GLX_COLOR_INDEX_BIT;
config->haveAccumBuffer = ((config->accumRedBits +
config->accumGreenBits +
config->accumBlueBits +
config->accumAlphaBits) > 0);
config->haveDepthBuffer = (config->depthBits > 0);
config->haveStencilBuffer = (config->stencilBits > 0);
}
static __GLcontextModes *
createConfigsFromProperties(Display *dpy, int nvisuals, int nprops,
int screen, GLboolean tagged_only)
{
INT32 buf[__GLX_TOTAL_CONFIG], *props;
unsigned prop_size;
__GLcontextModes *modes, *m;
int i;
if (nprops == 0)
return NULL;
if (nprops < __GLX_MIN_CONFIG_PROPS || nprops > __GLX_MAX_CONFIG_PROPS)
return NULL;
modes = _gl_context_modes_create(nvisuals, sizeof(__GLcontextModes));
if (!modes)
return NULL;
prop_size = nprops * __GLX_SIZE_INT32;
if (prop_size <= sizeof(buf))
props = buf;
else
props = Xmalloc(prop_size);
m = modes;
for (i = 0; i < nvisuals; i++) {
_XRead(dpy, (char *)props, prop_size);
m->drawableType = GLX_WINDOW_BIT | GLX_PIXMAP_BIT | GLX_PBUFFER_BIT;
__glXInitializeVisualConfigFromTags(m, nprops, props,
tagged_only, GL_TRUE);
m->screen = screen;
m = m->next;
}
if (props != buf)
Xfree(props);
return modes;
}
static GLboolean
getVisualConfigs(Display *dpy, __GLXdisplayPrivate *priv, int screen)
{
xGLXGetVisualConfigsReq *req;
__GLXscreenConfigs *psc;
xGLXGetVisualConfigsReply reply;
LockDisplay(dpy);
psc = priv->screenConfigs + screen;
psc->visuals = NULL;
GetReq(GLXGetVisualConfigs, req);
req->reqType = priv->majorOpcode;
req->glxCode = X_GLXGetVisualConfigs;
req->screen = screen;
if (!_XReply(dpy, (xReply*) &reply, 0, False))
goto out;
psc->visuals = createConfigsFromProperties(dpy,
reply.numVisuals,
reply.numProps,
screen, GL_FALSE);
out:
UnlockDisplay(dpy);
return psc->visuals != NULL;
}
static GLboolean
getFBConfigs(Display *dpy, __GLXdisplayPrivate *priv, int screen)
{
xGLXGetFBConfigsReq *fb_req;
xGLXGetFBConfigsSGIXReq *sgi_req;
xGLXVendorPrivateWithReplyReq *vpreq;
xGLXGetFBConfigsReply reply;
__GLXscreenConfigs *psc;
psc = priv->screenConfigs + screen;
psc->serverGLXexts = __glXGetStringFromServer(dpy, priv->majorOpcode,
X_GLXQueryServerString,
screen, GLX_EXTENSIONS);
LockDisplay(dpy);
psc->configs = NULL;
if (atof(priv->serverGLXversion) >= 1.3) {
GetReq(GLXGetFBConfigs, fb_req);
fb_req->reqType = priv->majorOpcode;
fb_req->glxCode = X_GLXGetFBConfigs;
fb_req->screen = screen;
} else if (strstr(psc->serverGLXexts, "GLX_SGIX_fbconfig") != NULL) {
GetReqExtra(GLXVendorPrivateWithReply,
sz_xGLXGetFBConfigsSGIXReq +
sz_xGLXVendorPrivateWithReplyReq, vpreq);
sgi_req = (xGLXGetFBConfigsSGIXReq *) vpreq;
sgi_req->reqType = priv->majorOpcode;
sgi_req->glxCode = X_GLXVendorPrivateWithReply;
sgi_req->vendorCode = X_GLXvop_GetFBConfigsSGIX;
sgi_req->screen = screen;
} else
goto out;
if (!_XReply(dpy, (xReply*) &reply, 0, False))
goto out;
psc->configs = createConfigsFromProperties(dpy,
reply.numFBConfigs,
reply.numAttribs * 2,
screen, GL_TRUE);
out:
UnlockDisplay(dpy);
return psc->configs != NULL;
}
static Bool AllocAndFetchScreenConfigs(Display *dpy, __GLXdisplayPrivate *priv)
{
__GLXscreenConfigs *psc;
GLint i, screens;
screens = ScreenCount(dpy);
psc = (__GLXscreenConfigs*) Xmalloc(screens * sizeof(__GLXscreenConfigs));
if (!psc) {
return GL_FALSE;
}
memset(psc, 0, screens * sizeof(__GLXscreenConfigs));
priv->screenConfigs = psc;
priv->serverGLXversion = __glXGetStringFromServer(dpy, priv->majorOpcode,
X_GLXQueryServerString,
0, GLX_VERSION);
if ( priv->serverGLXversion == NULL ) {
FreeScreenConfigs(priv);
return GL_FALSE;
}
for (i = 0; i < screens; i++, psc++) {
getVisualConfigs(dpy, priv, i);
getFBConfigs(dpy, priv, i);
}
SyncHandle();
return GL_TRUE;
}
_X_HIDDEN __GLXdisplayPrivate *__glXInitialize(Display* dpy)
{
XExtDisplayInfo *info = __glXFindDisplay(dpy);
XExtData **privList, *private, *found;
__GLXdisplayPrivate *dpyPriv;
XEDataObject dataObj;
int major, minor;
#ifdef GLX_DIRECT_RENDERING
Bool glx_direct, glx_accel;
#endif
#if defined(USE_XTHREADS)
{
static int firstCall = 1;
if (firstCall) {
xmutex_init(&__glXmutex);
firstCall = 0;
}
}
#endif
INIT_MESA_SPARC
__glXLock();
if (!XextHasExtension(info)) {
__glXUnlock();
XMissingExtension(dpy, __glXExtensionName);
return 0;
}
dataObj.display = dpy;
privList = XEHeadOfExtensionList(dataObj);
found = XFindOnExtensionList(privList, info->codes->extension);
if (found) {
__glXUnlock();
return (__GLXdisplayPrivate *) found->private_data;
}
if (!QueryVersion(dpy, info->codes->major_opcode, &major, &minor)) {
__glXUnlock();
return 0;
}
private = (XExtData *) Xmalloc(sizeof(XExtData));
if (!private) {
__glXUnlock();
return 0;
}
dpyPriv = (__GLXdisplayPrivate *) Xcalloc(1, sizeof(__GLXdisplayPrivate));
if (!dpyPriv) {
__glXUnlock();
Xfree((char*) private);
return 0;
}
dpyPriv->majorOpcode = info->codes->major_opcode;
dpyPriv->majorVersion = major;
dpyPriv->minorVersion = minor;
dpyPriv->dpy = dpy;
dpyPriv->serverGLXvendor = 0x0;
dpyPriv->serverGLXversion = 0x0;
#ifdef GLX_DIRECT_RENDERING
glx_direct = (getenv("LIBGL_ALWAYS_INDIRECT") == NULL);
glx_accel = (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL);
if (glx_direct && glx_accel) {
dpyPriv->driDisplay = driCreateDisplay(dpy);
}
if (glx_direct)
dpyPriv->driswDisplay = driswCreateDisplay(dpy);
#endif
apple_init_glx(dpy);
if (!AllocAndFetchScreenConfigs(dpy, dpyPriv)) {
__glXUnlock();
Xfree((char*) dpyPriv);
Xfree((char*) private);
return 0;
}
private->number = info->codes->extension;
private->next = 0;
private->free_private = __glXFreeDisplayPrivate;
private->private_data = (char *) dpyPriv;
XAddToExtensionList(privList, private);
if (dpyPriv->majorVersion == 1 && dpyPriv->minorVersion >= 1) {
__glXClientInfo(dpy, dpyPriv->majorOpcode);
}
__glXUnlock();
return dpyPriv;
}
_X_HIDDEN CARD8 __glXSetupForCommand(Display *dpy)
{
GLXContext gc;
__GLXdisplayPrivate *priv;
gc = __glXGetCurrentContext();
if (gc->currentDpy) {
(void) __glXFlushRenderBuffer(gc, gc->pc);
if (gc->currentDpy == dpy) {
INIT_MESA_SPARC
return gc->majorOpcode;
} else {
}
}
priv = __glXInitialize(dpy);
if (!priv) {
return 0;
}
return priv->majorOpcode;
}
_X_HIDDEN GLubyte *__glXFlushRenderBuffer(__GLXcontext *ctx, GLubyte *pc)
{
Display * const dpy = ctx->currentDpy;
#ifdef USE_XCB
xcb_connection_t *c = XGetXCBConnection(dpy);
#else
xGLXRenderReq *req;
#endif
const GLint size = pc - ctx->buf;
if ( (dpy != NULL) && (size > 0) ) {
#ifdef USE_XCB
xcb_glx_render(c, ctx->currentContextTag, size,
(const uint8_t *)ctx->buf);
#else
LockDisplay(dpy);
GetReq(GLXRender,req);
req->reqType = ctx->majorOpcode;
req->glxCode = X_GLXRender;
req->contextTag = ctx->currentContextTag;
req->length += (size + 3) >> 2;
_XSend(dpy, (char *)ctx->buf, size);
UnlockDisplay(dpy);
SyncHandle();
#endif
}
ctx->pc = ctx->buf;
return ctx->pc;
}
_X_HIDDEN void __glXSendLargeChunk(__GLXcontext *gc, GLint requestNumber,
GLint totalRequests,
const GLvoid * data, GLint dataLen)
{
Display *dpy = gc->currentDpy;
#ifdef USE_XCB
xcb_connection_t *c = XGetXCBConnection(dpy);
xcb_glx_render_large(c, gc->currentContextTag, requestNumber, totalRequests, dataLen, data);
#else
xGLXRenderLargeReq *req;
if ( requestNumber == 1 ) {
LockDisplay(dpy);
}
GetReq(GLXRenderLarge,req);
req->reqType = gc->majorOpcode;
req->glxCode = X_GLXRenderLarge;
req->contextTag = gc->currentContextTag;
req->length += (dataLen + 3) >> 2;
req->requestNumber = requestNumber;
req->requestTotal = totalRequests;
req->dataBytes = dataLen;
Data(dpy, data, dataLen);
if ( requestNumber == totalRequests ) {
UnlockDisplay(dpy);
SyncHandle();
}
#endif
}
_X_HIDDEN void __glXSendLargeCommand(__GLXcontext *ctx,
const GLvoid *header, GLint headerLen,
const GLvoid *data, GLint dataLen)
{
GLint maxSize;
GLint totalRequests, requestNumber;
maxSize = (ctx->bufSize + sz_xGLXRenderReq) - sz_xGLXRenderLargeReq;
totalRequests = 1 + (dataLen / maxSize);
if (dataLen % maxSize) totalRequests++;
assert( headerLen <= maxSize );
__glXSendLargeChunk(ctx, 1, totalRequests, header, headerLen);
for ( requestNumber = 2 ; requestNumber <= (totalRequests - 1) ; requestNumber++ ) {
__glXSendLargeChunk(ctx, requestNumber, totalRequests, data, maxSize);
data = (const GLvoid *) (((const GLubyte *) data) + maxSize);
dataLen -= maxSize;
assert( dataLen > 0 );
}
assert( dataLen <= maxSize );
__glXSendLargeChunk(ctx, requestNumber, totalRequests, data, dataLen);
}
#ifdef DEBUG
_X_HIDDEN void __glXDumpDrawBuffer(__GLXcontext *ctx)
{
GLubyte *p = ctx->buf;
GLubyte *end = ctx->pc;
GLushort opcode, length;
while (p < end) {
opcode = *((GLushort*) p);
length = *((GLushort*) (p + 2));
printf("%2x: %5d: ", opcode, length);
length -= 4;
p += 4;
while (length > 0) {
printf("%08x ", *((unsigned *) p));
p += 4;
length -= 4;
}
printf("\n");
}
}
#endif
#ifdef USE_SPARC_ASM
extern unsigned int _mesa_sparc_glapi_begin;
extern unsigned int _mesa_sparc_glapi_end;
extern void __glapi_sparc_icache_flush(unsigned int *);
static void
_glx_mesa_init_sparc_glapi_relocs(void)
{
unsigned int *insn_ptr, *end_ptr;
unsigned long disp_addr;
insn_ptr = &_mesa_sparc_glapi_begin;
end_ptr = &_mesa_sparc_glapi_end;
disp_addr = (unsigned long) &_glapi_Dispatch;
while (insn_ptr < end_ptr) {
#if ( defined(__sparc_v9__) && ( !defined(__linux__) || defined(__linux_64__) ) )
insn_ptr[0] |= (disp_addr >> (32 + 10));
insn_ptr[1] |= ((disp_addr & 0xffffffff) >> 10);
__glapi_sparc_icache_flush(&insn_ptr[0]);
insn_ptr[2] |= ((disp_addr >> 32) & ((1 << 10) - 1));
insn_ptr[3] |= (disp_addr & ((1 << 10) - 1));
__glapi_sparc_icache_flush(&insn_ptr[2]);
insn_ptr += 11;
#else
insn_ptr[0] |= (disp_addr >> 10);
insn_ptr[1] |= (disp_addr & ((1 << 10) - 1));
__glapi_sparc_icache_flush(&insn_ptr[0]);
insn_ptr += 5;
#endif
}
}
#endif