Commit 8753218b authored by Kyle Brenneman's avatar Kyle Brenneman Committed by Adam Jackson

glx: Import glxvnd server module (v2)

This is based on an out-of-tree module written by Kyle:

https://github.com/kbrenneman/libglvnd/tree/server-libglx

I (ajax) did a bunch of cosmetic fixes, ported it off xfree86 API,
added request length checks, and fixed a minor bug or two.

v2: Use separate functions to set/get a context tag's private data, and
call the backend's MakeCurrent when a client disconnects to unbind the
context. (Kyle Brenneman)
Signed-off-by: Adam Jackson's avatarAdam Jackson <ajax@redhat.com>
parent d1fdddeb
......@@ -2,7 +2,7 @@ if DRI2
GLXDRI_LIBRARY = libglxdri.la
endif
noinst_LTLIBRARIES = libglx.la $(GLXDRI_LIBRARY)
noinst_LTLIBRARIES = libglx.la $(GLXDRI_LIBRARY) libglxvnd.la
AM_CFLAGS = \
@DIX_CFLAGS@ \
......@@ -83,3 +83,9 @@ libglx_la_SOURCES = \
xfont.c
libglx_la_LIBADD = $(DLOPEN_LIBS)
libglxvnd_la_SOURCES = \
vndcmds.c \
vndext.c \
vndservermapping.c \
vndservervendor.c
......@@ -53,3 +53,24 @@ srcs_glxdri2 = []
if build_dri2 or build_dri3
srcs_glxdri2 = files('glxdri2.c')
endif
srcs_vnd = [
'vndcmds.c',
'vndext.c',
'vndservermapping.c',
'vndservervendor.c',
]
libglxvnd = ''
if build_glx
libglxvnd = static_library('libglxvnd',
srcs_vnd,
include_directories: inc,
dependencies: [
common_dep,
dl_dep,
dependency('glproto', version: '>= 1.4.17'),
dependency('gl', version: '>= 9.2.0'),
],
)
endif
This diff is collapsed.
This diff is collapsed.
/*
* Copyright (c) 2016, NVIDIA CORPORATION.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and/or associated documentation files (the
* "Materials"), to deal in the Materials without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Materials, and to
* permit persons to whom the Materials are furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* unaltered in all copies or substantial portions of the Materials.
* Any additions, deletions, or changes to the original source files
* must be clearly indicated in accompanying documentation.
*
* If only executable code is distributed, then the accompanying
* documentation must state that "this software is based in part on the
* work of the Khronos Group."
*
* THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
*/
#include "vndserver.h"
#include <string.h>
#include <scrnintstr.h>
#include <windowstr.h>
#include <dixstruct.h>
#include <extnsionst.h>
#include <glx_extinit.h>
#include <GL/glxproto.h>
#include "vndservervendor.h"
int GlxErrorBase = 0;
static CallbackListPtr vndInitCallbackList;
static DevPrivateKeyRec glvXGLVScreenPrivKey;
static DevPrivateKeyRec glvXGLVClientPrivKey;
// The resource type used to keep track of the vendor library for XID's.
RESTYPE idResource;
static int
idResourceDeleteCallback(void *value, XID id)
{
return 0;
}
static GlxScreenPriv *
xglvGetScreenPrivate(ScreenPtr pScreen)
{
return dixLookupPrivate(&pScreen->devPrivates, &glvXGLVScreenPrivKey);
}
static void
xglvSetScreenPrivate(ScreenPtr pScreen, void *priv)
{
dixSetPrivate(&pScreen->devPrivates, &glvXGLVScreenPrivKey, priv);
}
GlxScreenPriv *
GlxGetScreen(ScreenPtr pScreen)
{
if (pScreen != NULL) {
GlxScreenPriv *priv = xglvGetScreenPrivate(pScreen);
if (priv == NULL) {
priv = calloc(1, sizeof(GlxScreenPriv));
if (priv == NULL) {
return NULL;
}
xglvSetScreenPrivate(pScreen, priv);
}
return priv;
} else {
return NULL;
}
}
static void
GlxMappingReset(void)
{
int i;
for (i=0; i<screenInfo.numScreens; i++) {
GlxScreenPriv *priv = xglvGetScreenPrivate(screenInfo.screens[i]);
if (priv != NULL) {
xglvSetScreenPrivate(screenInfo.screens[i], NULL);
free(priv);
}
}
}
static Bool
GlxMappingInit(void)
{
int i;
for (i=0; i<screenInfo.numScreens; i++) {
if (GlxGetScreen(screenInfo.screens[i]) == NULL) {
GlxMappingReset();
return FALSE;
}
}
idResource = CreateNewResourceType(idResourceDeleteCallback,
"GLXServerIDRes");
if (idResource == RT_NONE)
{
GlxMappingReset();
return FALSE;
}
return TRUE;
}
static GlxClientPriv *
xglvGetClientPrivate(ClientPtr pClient)
{
return dixLookupPrivate(&pClient->devPrivates, &glvXGLVClientPrivKey);
}
static void
xglvSetClientPrivate(ClientPtr pClient, void *priv)
{
dixSetPrivate(&pClient->devPrivates, &glvXGLVClientPrivKey, priv);
}
GlxClientPriv *
GlxGetClientData(ClientPtr client)
{
GlxClientPriv *cl = xglvGetClientPrivate(client);
if (cl == NULL) {
cl = calloc(1, sizeof(GlxClientPriv));
if (cl != NULL) {
xglvSetClientPrivate(client, cl);
}
}
return cl;
}
void
GlxFreeClientData(ClientPtr client)
{
GlxClientPriv *cl = xglvGetClientPrivate(client);
if (cl != NULL) {
unsigned int i;
for (i = 0; i < cl->contextTagCount; i++) {
GlxContextTagInfo *tag = &cl->contextTags[i];
if (tag->vendor != NULL) {
tag->vendor->glxvc.makeCurrent(client, tag->tag,
None, None, None, 0);
}
}
xglvSetClientPrivate(client, NULL);
free(cl->contextTags);
free(cl);
}
}
static void
GLXClientCallback(CallbackListPtr *list, void *closure, void *data)
{
NewClientInfoRec *clientinfo = (NewClientInfoRec *) data;
ClientPtr client = clientinfo->client;
switch (client->clientState)
{
case ClientStateRetained:
case ClientStateGone:
GlxFreeClientData(client);
break;
}
}
static void
GLXReset(ExtensionEntry *extEntry)
{
// xf86Msg(X_INFO, "GLX: GLXReset\n");
GlxVendorExtensionReset(extEntry);
GlxDispatchReset();
GlxMappingReset();
}
void
GlxExtensionInit(void)
{
ExtensionEntry *extEntry;
// Init private keys, per-screen data
if (!dixRegisterPrivateKey(&glvXGLVScreenPrivKey, PRIVATE_SCREEN, 0))
return;
if (!dixRegisterPrivateKey(&glvXGLVClientPrivKey, PRIVATE_CLIENT, 0))
return;
if (!GlxMappingInit()) {
return;
}
if (!GlxDispatchInit()) {
return;
}
if (!AddCallback(&ClientStateCallback, GLXClientCallback, NULL)) {
return;
}
extEntry = AddExtension(GLX_EXTENSION_NAME, __GLX_NUMBER_EVENTS,
__GLX_NUMBER_ERRORS, GlxDispatchRequest,
GlxDispatchRequest, GLXReset, StandardMinorOpcode);
if (!extEntry) {
return;
}
GlxErrorBase = extEntry->errorBase;
CallCallbacks(&vndInitCallbackList, extEntry);
}
static int
GlxForwardRequest(GlxServerVendor *vendor, ClientPtr client)
{
return vendor->glxvc.handleRequest(client);
}
static GlxServerVendor *
GlxGetContextTag(ClientPtr client, GLXContextTag tag)
{
GlxContextTagInfo *tagInfo = GlxLookupContextTag(client, tag);
if (tagInfo != NULL) {
return tagInfo->vendor;
} else {
return NULL;
}
}
static Bool
GlxSetContextTagPrivate(ClientPtr client, GLXContextTag tag, void *data)
{
GlxContextTagInfo *tagInfo = GlxLookupContextTag(client, tag);
if (tagInfo != NULL) {
tagInfo->data = data;
return TRUE;
} else {
return FALSE;
}
}
static void *
GlxGetContextTagPrivate(ClientPtr client, GLXContextTag tag)
{
GlxContextTagInfo *tagInfo = GlxLookupContextTag(client, tag);
if (tagInfo != NULL) {
return tagInfo->data;
} else {
return NULL;
}
}
static GlxServerImports *
GlxAllocateServerImports(void)
{
return calloc(1, sizeof(GlxServerImports));
}
static void
GlxFreeServerImports(GlxServerImports *imports)
{
free(imports);
}
_X_EXPORT const GlxServerExports glxServer = {
GLXSERVER_VENDOR_ABI_MAJOR_VERSION, // majorVersion
GLXSERVER_VENDOR_ABI_MINOR_VERSION, // minorVersion
&vndInitCallbackList,
GlxAllocateServerImports, // allocateServerImports
GlxFreeServerImports, // freeServerImports
GlxCreateVendor, // createVendor
GlxDestroyVendor, // destroyVendor
GlxSetScreenVendor, // setScreenVendor
GlxAddXIDMap, // addXIDMap
GlxGetXIDMap, // getXIDMap
GlxRemoveXIDMap, // removeXIDMap
GlxGetContextTag, // getContextTag
GlxSetContextTagPrivate, // setContextTagPrivate
GlxGetContextTagPrivate, // getContextTagPrivate
GlxGetVendorForScreen, // getVendorForScreen
GlxForwardRequest, // forwardRequest
};
const GlxServerExports *
glvndGetExports(void)
{
return &glxServer;
}
/*
* Copyright (c) 2016, NVIDIA CORPORATION.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and/or associated documentation files (the
* "Materials"), to deal in the Materials without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Materials, and to
* permit persons to whom the Materials are furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* unaltered in all copies or substantial portions of the Materials.
* Any additions, deletions, or changes to the original source files
* must be clearly indicated in accompanying documentation.
*
* If only executable code is distributed, then the accompanying
* documentation must state that "this software is based in part on the
* work of the Khronos Group."
*
* THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
*/
#ifndef VNDSERVER_H
#define VNDSERVER_H
#include <dix-config.h>
#include "glxvndabi.h"
#define GLXContextID CARD32
#define GLXDrawable CARD32
#if defined(__cplusplus)
extern "C" {
#endif
typedef struct GlxScreenPrivRec {
GlxServerVendor *vendor;
} GlxScreenPriv;
typedef struct GlxContextTagInfoRec {
GLXContextTag tag;
ClientPtr client;
GlxServerVendor *vendor;
void *data;
GLXContextID context;
GLXDrawable drawable;
GLXDrawable readdrawable;
} GlxContextTagInfo;
typedef struct GlxClientPrivRec {
GlxContextTagInfo *contextTags;
unsigned int contextTagCount;
} GlxClientPriv;
extern int GlxErrorBase;
extern RESTYPE idResource;
// Defined in glxext.c.
const ExtensionEntry *GlxGetExtensionEntry(void);
Bool GlxDispatchInit(void);
void GlxDispatchReset(void);
/**
* Handles a request from the client.
*
* This function will look up the correct handler function and forward the
* request to it.
*/
int GlxDispatchRequest(ClientPtr client);
/**
* Looks up the GlxClientPriv struct for a client. If we don't have a
* GlxClientPriv struct yet, then allocate one.
*/
GlxClientPriv *GlxGetClientData(ClientPtr client);
/**
* Frees any data that's specific to a client. This should be called when a
* client disconnects.
*/
void GlxFreeClientData(ClientPtr client);
Bool GlxAddXIDMap(XID id, GlxServerVendor *vendor);
GlxServerVendor * GlxGetXIDMap(XID id);
void GlxRemoveXIDMap(XID id);
GlxContextTagInfo *GlxAllocContextTag(ClientPtr client, GlxServerVendor *vendor);
GlxContextTagInfo *GlxLookupContextTag(ClientPtr client, GLXContextTag tag);
void GlxFreeContextTag(GlxContextTagInfo *tagInfo);
Bool GlxSetScreenVendor(ScreenPtr screen, GlxServerVendor *vendor);
GlxScreenPriv *GlxGetScreen(ScreenPtr pScreen);
GlxServerVendor *GlxGetVendorForScreen(ClientPtr client, ScreenPtr screen);
static inline CARD32 GlxCheckSwap(ClientPtr client, CARD32 value)
{
if (client->swapped)
{
value = ((value & 0XFF000000) >> 24) | ((value & 0X00FF0000) >> 8)
| ((value & 0X0000FF00) << 8) | ((value & 0X000000FF) << 24);
}
return value;
}
#if defined(__cplusplus)
}
#endif
_X_EXPORT const GlxServerExports *glvndGetExports(void);
#endif // VNDSERVER_H
/*
* Copyright (c) 2016, NVIDIA CORPORATION.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and/or associated documentation files (the
* "Materials"), to deal in the Materials without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Materials, and to
* permit persons to whom the Materials are furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* unaltered in all copies or substantial portions of the Materials.
* Any additions, deletions, or changes to the original source files
* must be clearly indicated in accompanying documentation.
*
* If only executable code is distributed, then the accompanying
* documentation must state that "this software is based in part on the
* work of the Khronos Group."
*
* THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
*/
#include "vndserver.h"
#include <pixmapstr.h>
#include "vndservervendor.h"
static GlxServerVendor *LookupXIDMapResource(XID id)
{
void *ptr = NULL;
int rv;
rv = dixLookupResourceByType(&ptr, id, idResource, NULL, DixReadAccess);
if (rv == Success) {
return (GlxServerVendor *) ptr;
} else {
return NULL;
}
}
GlxServerVendor *GlxGetXIDMap(XID id)
{
GlxServerVendor *vendor = LookupXIDMapResource(id);
if (vendor == NULL) {
// If we haven't seen this XID before, then it may be a drawable that
// wasn't created through GLX, like a regular X window or pixmap. Try
// to look up a matching drawable to find a screen number for it.
void *ptr = NULL;
int rv = dixLookupResourceByClass(&ptr, id, RC_DRAWABLE, NULL,
DixGetAttrAccess);
if (rv == Success && ptr != NULL) {
DrawablePtr draw = (DrawablePtr) ptr;
GlxScreenPriv *screenPriv = GlxGetScreen(draw->pScreen);
if (screenPriv != NULL) {
vendor = screenPriv->vendor;
}
}
}
return vendor;
}
Bool GlxAddXIDMap(XID id, GlxServerVendor *vendor)
{
if (id == 0 || vendor == NULL) {
return FALSE;
}
if (LookupXIDMapResource(id) != NULL) {
return FALSE;
}
return AddResource(id, idResource, vendor);
}
void GlxRemoveXIDMap(XID id)
{
FreeResourceByType(id, idResource, FALSE);
}
GlxContextTagInfo *GlxAllocContextTag(ClientPtr client, GlxServerVendor *vendor)
{
GlxClientPriv *cl;
unsigned int index;
if (vendor == NULL) {
return NULL;
}
cl = GlxGetClientData(client);
if (cl == NULL) {
return NULL;
}
// Look for a free tag index.
for (index=0; index<cl->contextTagCount; index++) {
if (cl->contextTags[index].vendor == NULL) {
break;
}
}
if (index >= cl->contextTagCount) {
// We didn't find a free entry, so grow the array.
GlxContextTagInfo *newTags;
unsigned int newSize = cl->contextTagCount * 2;
if (newSize == 0) {
// TODO: What's a good starting size for this?
newSize = 16;
}
newTags = (GlxContextTagInfo *)
realloc(cl->contextTags, newSize * sizeof(GlxContextTagInfo));
if (newTags == NULL) {
return NULL;
}
memset(&newTags[cl->contextTagCount], 0,
(newSize - cl->contextTagCount) * sizeof(GlxContextTagInfo));
index = cl->contextTagCount;
cl->contextTags = newTags;
cl->contextTagCount = newSize;
}
assert(index >= 0);
assert(index < cl->contextTagCount);
memset(&cl->contextTags[index], 0, sizeof(GlxContextTagInfo));
cl->contextTags[index].tag = (GLXContextTag) (index + 1);
cl->contextTags[index].client = client;
cl->contextTags[index].vendor = vendor;
return &cl->contextTags[index];
}
GlxContextTagInfo *GlxLookupContextTag(ClientPtr client, GLXContextTag tag)
{
GlxClientPriv *cl = GlxGetClientData(client);
if (cl == NULL) {
return NULL;
}
if (tag > 0 && (tag - 1) < cl->contextTagCount) {
if (cl->contextTags[tag - 1].vendor != NULL) {
assert(cl->contextTags[tag - 1].client == client);
return &cl->contextTags[tag - 1];
}
}
return NULL;
}
void GlxFreeContextTag(GlxContextTagInfo *tagInfo)
{
if (tagInfo != NULL) {
tagInfo->vendor = NULL;
tagInfo->vendor = NULL;
tagInfo->data = NULL;
tagInfo->context = None;
tagInfo->drawable = None;
tagInfo->readdrawable = None;
}
}
Bool GlxSetScreenVendor(ScreenPtr screen, GlxServerVendor *vendor)
{
GlxScreenPriv *priv;
if (vendor == NULL) {
return FALSE;
}
priv = GlxGetScreen(screen);
if (priv == NULL) {
return FALSE;
}
if (priv->vendor != NULL) {
return FALSE;
}
priv->vendor = vendor;
return TRUE;
}
GlxServerVendor *GlxGetVendorForScreen(ClientPtr client, ScreenPtr screen)
{
GlxScreenPriv *priv = GlxGetScreen(screen);
if (priv != NULL) {
return priv->vendor;
} else {
return NULL;
}
}
/*
* Copyright (c) 2016, NVIDIA CORPORATION.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and/or associated documentation files (the
* "Materials"), to deal in the Materials without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Materials, and to
* permit persons to whom the Materials are furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* unaltered in all copies or substantial portions of the Materials.
* Any additions, deletions, or changes to the original source files
* must be clearly indicated in accompanying documentation.
*
* If only executable code is distributed, then the accompanying
* documentation must state that "this software is based in part on the
* work of the Khronos Group."
*
* THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
*/
#include "vndservervendor.h"
struct xorg_list GlxVendorList = { &GlxVendorList, &GlxVendorList };
GlxServerVendor *GlxCreateVendor(const GlxServerImports