summaryrefslogtreecommitdiff
path: root/dri3
diff options
context:
space:
mode:
authorKeith Packard <keithp@keithp.com>2013-04-09 19:59:39 -0700
committerKeith Packard <keithp@keithp.com>2013-10-31 16:58:30 -0700
commit563138298868f62501875d3016f03469dcffaad0 (patch)
treea63491ea60c8a0360351b0e163a8195ce22f697d /dri3
parentfdec793cdc2ef9a6ea66b311cb1068a7bd4a3be3 (diff)
dri3: Add DRI3 extension
Adds DRM compatible fences using futexes. Uses FD passing to get pixmaps from DRM applications. Signed-off-by: Keith Packard <keithp@keithp.com> Reviewed-by: Adam Jackson <ajax@redhat.com>
Diffstat (limited to 'dri3')
-rw-r--r--dri3/Makefile.am13
-rw-r--r--dri3/dri3.c87
-rw-r--r--dri3/dri3.h59
-rw-r--r--dri3/dri3_event.c163
-rw-r--r--dri3/dri3_priv.h80
-rw-r--r--dri3/dri3_request.c394
-rw-r--r--dri3/dri3_screen.c80
-rw-r--r--dri3/dri3int.h26
8 files changed, 902 insertions, 0 deletions
diff --git a/dri3/Makefile.am b/dri3/Makefile.am
new file mode 100644
index 000000000..e47a734e0
--- /dev/null
+++ b/dri3/Makefile.am
@@ -0,0 +1,13 @@
+noinst_LTLIBRARIES = libdri3.la
+AM_CFLAGS = \
+ -DHAVE_XORG_CONFIG_H \
+ @DIX_CFLAGS@ @XORG_CFLAGS@
+
+libdri3_la_SOURCES = \
+ dri3.h \
+ dri3_priv.h \
+ dri3.c \
+ dri3_request.c \
+ dri3_screen.c
+
+sdk_HEADERS = dri3.h
diff --git a/dri3/dri3.c b/dri3/dri3.c
new file mode 100644
index 000000000..2bca7ae9b
--- /dev/null
+++ b/dri3/dri3.c
@@ -0,0 +1,87 @@
+/*
+ * Copyright © 2013 Keith Packard
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and its
+ * documentation for any purpose is hereby granted without fee, provided that
+ * the above copyright notice appear in all copies and that both that copyright
+ * notice and this permission notice appear in supporting documentation, and
+ * that the name of the copyright holders not be used in advertising or
+ * publicity pertaining to distribution of the software without specific,
+ * written prior permission. The copyright holders make no representations
+ * about the suitability of this software for any purpose. It is provided "as
+ * is" without express or implied warranty.
+ *
+ * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
+ * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
+ * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
+ * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
+ * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
+ * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+#ifdef HAVE_XORG_CONFIG_H
+#include <xorg-config.h>
+#endif
+
+#include "dri3_priv.h"
+
+int dri3_request;
+DevPrivateKeyRec dri3_screen_private_key;
+DevPrivateKeyRec dri3_window_private_key;
+
+static Bool
+dri3_close_screen(ScreenPtr screen)
+{
+ dri3_screen_priv_ptr screen_priv = dri3_screen_priv(screen);
+
+ unwrap(screen_priv, screen, CloseScreen);
+
+ free(screen_priv);
+ return (*screen->CloseScreen) (screen);
+}
+
+Bool
+dri3_screen_init(ScreenPtr screen, dri3_screen_info_ptr info)
+{
+ if (!dixRegisterPrivateKey(&dri3_screen_private_key, PRIVATE_SCREEN, 0))
+ return FALSE;
+
+ if (!dri3_screen_priv(screen)) {
+ dri3_screen_priv_ptr screen_priv = calloc(1, sizeof (dri3_screen_priv_rec));
+ if (!screen_priv)
+ return FALSE;
+
+ wrap(screen_priv, screen, CloseScreen, dri3_close_screen);
+
+ screen_priv->info = info;
+
+ dixSetPrivate(&screen->devPrivates, &dri3_screen_private_key, screen_priv);
+ }
+
+ return TRUE;
+}
+
+void
+dri3_extension_init(void)
+{
+ ExtensionEntry *extension;
+ int i;
+
+ extension = AddExtension(DRI3_NAME, DRI3NumberEvents, DRI3NumberErrors,
+ proc_dri3_dispatch, sproc_dri3_dispatch,
+ NULL, StandardMinorOpcode);
+ if (!extension)
+ goto bail;
+
+ dri3_request = extension->base;
+
+ for (i = 0; i < screenInfo.numScreens; i++) {
+ if (!dri3_screen_init(screenInfo.screens[i], NULL))
+ goto bail;
+ }
+ return;
+
+bail:
+ FatalError("Cannot initialize DRI3 extension");
+}
diff --git a/dri3/dri3.h b/dri3/dri3.h
new file mode 100644
index 000000000..7774c8757
--- /dev/null
+++ b/dri3/dri3.h
@@ -0,0 +1,59 @@
+/*
+ * Copyright © 2013 Keith Packard
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and its
+ * documentation for any purpose is hereby granted without fee, provided that
+ * the above copyright notice appear in all copies and that both that copyright
+ * notice and this permission notice appear in supporting documentation, and
+ * that the name of the copyright holders not be used in advertising or
+ * publicity pertaining to distribution of the software without specific,
+ * written prior permission. The copyright holders make no representations
+ * about the suitability of this software for any purpose. It is provided "as
+ * is" without express or implied warranty.
+ *
+ * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
+ * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
+ * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
+ * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
+ * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
+ * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+#ifndef _DRI3_H_
+#define _DRI3_H_
+
+#include <X11/extensions/dri3proto.h>
+#include <randrstr.h>
+
+#define DRI3_SCREEN_INFO_VERSION 0
+
+typedef int (*dri3_open_proc)(ScreenPtr screen,
+ RRProviderPtr provider,
+ int *fd);
+
+typedef PixmapPtr (*dri3_pixmap_from_fd_proc) (ScreenPtr screen,
+ int fd,
+ CARD16 width,
+ CARD16 height,
+ CARD16 stride,
+ CARD8 depth,
+ CARD8 bpp);
+
+typedef int (*dri3_fd_from_pixmap_proc) (ScreenPtr screen,
+ PixmapPtr pixmap,
+ CARD16 *stride,
+ CARD32 *size);
+
+typedef struct dri3_screen_info {
+ uint32_t version;
+
+ dri3_open_proc open;
+ dri3_pixmap_from_fd_proc pixmap_from_fd;
+ dri3_fd_from_pixmap_proc fd_from_pixmap;
+} dri3_screen_info_rec, *dri3_screen_info_ptr;
+
+extern _X_EXPORT Bool
+dri3_screen_init(ScreenPtr screen, dri3_screen_info_ptr info);
+
+#endif /* _DRI3_H_ */
diff --git a/dri3/dri3_event.c b/dri3/dri3_event.c
new file mode 100644
index 000000000..02f0f6579
--- /dev/null
+++ b/dri3/dri3_event.c
@@ -0,0 +1,163 @@
+/*
+ * Copyright © 2013 Keith Packard
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and its
+ * documentation for any purpose is hereby granted without fee, provided that
+ * the above copyright notice appear in all copies and that both that copyright
+ * notice and this permission notice appear in supporting documentation, and
+ * that the name of the copyright holders not be used in advertising or
+ * publicity pertaining to distribution of the software without specific,
+ * written prior permission. The copyright holders make no representations
+ * about the suitability of this software for any purpose. It is provided "as
+ * is" without express or implied warranty.
+ *
+ * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
+ * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
+ * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
+ * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
+ * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
+ * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+#ifdef HAVE_XORG_CONFIG_H
+#include <xorg-config.h>
+#endif
+
+#include "dri3_priv.h"
+
+RESTYPE dri3_event_type;
+
+static int
+dri3_free_event(pointer data, XID id)
+{
+ dri3_event_ptr dri3_event = (dri3_event_ptr) data;
+ dri3_window_priv_ptr window_priv = dri3_window_priv(dri3_event->window);
+ dri3_event_ptr *previous, current;
+
+ for (previous = &window_priv->events; (current = *previous); previous = &current->next) {
+ if (current == dri3_event) {
+ *previous = dri3_event->next;
+ break;
+ }
+ }
+ free((pointer) dri3_event);
+ return 1;
+
+}
+
+void
+dri3_free_events(WindowPtr window)
+{
+ dri3_window_priv_ptr window_priv = dri3_window_priv(window);
+ dri3_event_ptr event;
+
+ if (!window_priv)
+ return;
+
+ while ((event = window_priv->events))
+ FreeResource(event->id, RT_NONE);
+}
+
+static void
+dri3_event_swap(xGenericEvent *from, xGenericEvent *to)
+{
+ *to = *from;
+ swaps(&to->sequenceNumber);
+ swapl(&to->length);
+ swaps(&to->evtype);
+ switch (from->evtype) {
+ case DRI3_ConfigureNotify: {
+ xDRI3ConfigureNotify *c = (xDRI3ConfigureNotify *) to;
+
+ swapl(&c->eid);
+ swapl(&c->window);
+ swaps(&c->x);
+ swaps(&c->y);
+ swaps(&c->width);
+ swaps(&c->height);
+ swaps(&c->off_x);
+ swaps(&c->off_y);
+ swaps(&c->pixmap_width);
+ swaps(&c->pixmap_height);
+ swapl(&c->pixmap_flags);
+ break;
+ }
+ }
+}
+
+void
+dri3_send_config_notify(WindowPtr window, int x, int y, int w, int h, int bw, WindowPtr sibling)
+{
+ dri3_window_priv_ptr window_priv = dri3_window_priv(window);
+
+ if (window_priv) {
+ xDRI3ConfigureNotify cn = {
+ .type = GenericEvent,
+ .extension = dri3_request,
+ .length = (sizeof(xDRI3ConfigureNotify) - 32) >> 2,
+ .evtype = DRI3_ConfigureNotify,
+ .eid = 0,
+ .window = window->drawable.id,
+ .x = x,
+ .y = y,
+ .width = w,
+ .height = h,
+ .off_x = 0,
+ .off_y = 0,
+ .pixmap_width = w,
+ .pixmap_height = h,
+ .pixmap_flags = 0
+ };
+ dri3_event_ptr event;
+ dri3_screen_priv_ptr screen_priv = dri3_screen_priv(window->drawable.pScreen);
+
+ if (screen_priv->info && screen_priv->info->driver_config)
+ screen_priv->info->driver_config(window, &cn);
+
+ for (event = window_priv->events; event; event = event->next) {
+ if (event->mask & (1 << DRI3ConfigureNotify)) {
+ cn.eid = event->id;
+ WriteEventsToClient(event->client, 1, (xEvent *) &cn);
+ }
+ }
+ }
+}
+
+int
+dri3_select_input(ClientPtr client, XID eid, WindowPtr window, CARD32 mask)
+{
+ dri3_window_priv_ptr window_priv = dri3_window_priv(window);
+ dri3_event_ptr event;
+
+ if (!window_priv)
+ return BadAlloc;
+
+ event = calloc (1, sizeof (dri3_event_rec));
+ if (!event)
+ return BadAlloc;
+
+ event->client = client;
+ event->window = window;
+ event->id = eid;
+ event->mask = mask;
+
+ event->next = window_priv->events;
+ window_priv->events = event;
+
+ if (!AddResource(event->id, dri3_event_type, (pointer) event))
+ return BadAlloc;
+
+ return Success;
+}
+
+Bool
+dri3_event_init(void)
+{
+ dri3_event_type = CreateNewResourceType(dri3_free_event, "DRI3Event");
+ if (!dri3_event_type)
+ return FALSE;
+
+ GERegisterExtension(dri3_request, dri3_event_swap);
+ return TRUE;
+}
diff --git a/dri3/dri3_priv.h b/dri3/dri3_priv.h
new file mode 100644
index 000000000..e2fed839b
--- /dev/null
+++ b/dri3/dri3_priv.h
@@ -0,0 +1,80 @@
+/*
+ * Copyright © 2013 Keith Packard
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and its
+ * documentation for any purpose is hereby granted without fee, provided that
+ * the above copyright notice appear in all copies and that both that copyright
+ * notice and this permission notice appear in supporting documentation, and
+ * that the name of the copyright holders not be used in advertising or
+ * publicity pertaining to distribution of the software without specific,
+ * written prior permission. The copyright holders make no representations
+ * about the suitability of this software for any purpose. It is provided "as
+ * is" without express or implied warranty.
+ *
+ * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
+ * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
+ * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
+ * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
+ * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
+ * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+#ifndef _DRI3PRIV_H_
+#define _DRI3PRIV_H_
+
+#include <X11/X.h>
+#include "scrnintstr.h"
+#include "misc.h"
+#include "list.h"
+#include "windowstr.h"
+#include "dixstruct.h"
+#include <randrstr.h>
+#include "dri3.h"
+
+extern int dri3_request;
+
+extern DevPrivateKeyRec dri3_screen_private_key;
+
+typedef struct dri3_screen_priv {
+ CloseScreenProcPtr CloseScreen;
+ ConfigNotifyProcPtr ConfigNotify;
+ DestroyWindowProcPtr DestroyWindow;
+
+ dri3_screen_info_ptr info;
+} dri3_screen_priv_rec, *dri3_screen_priv_ptr;
+
+#define wrap(priv,real,mem,func) {\
+ priv->mem = real->mem; \
+ real->mem = func; \
+}
+
+#define unwrap(priv,real,mem) {\
+ real->mem = priv->mem; \
+}
+
+static inline dri3_screen_priv_ptr
+dri3_screen_priv(ScreenPtr screen)
+{
+ return (dri3_screen_priv_ptr)dixLookupPrivate(&(screen)->devPrivates, &dri3_screen_private_key);
+}
+
+int
+proc_dri3_dispatch(ClientPtr client);
+
+int
+sproc_dri3_dispatch(ClientPtr client);
+
+/* DDX interface */
+
+int
+dri3_open(ClientPtr client, ScreenPtr screen, RRProviderPtr provider, int *fd);
+
+int
+dri3_pixmap_from_fd(PixmapPtr *ppixmap, ScreenPtr screen, int fd,
+ CARD16 width, CARD16 height, CARD16 stride, CARD8 depth, CARD8 bpp);
+
+int
+dri3_fd_from_pixmap(int *pfd, PixmapPtr pixmap, CARD16 *stride, CARD32 *size);
+
+#endif /* _DRI3PRIV_H_ */
diff --git a/dri3/dri3_request.c b/dri3/dri3_request.c
new file mode 100644
index 000000000..3ebb9d509
--- /dev/null
+++ b/dri3/dri3_request.c
@@ -0,0 +1,394 @@
+/*
+ * Copyright © 2013 Keith Packard
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and its
+ * documentation for any purpose is hereby granted without fee, provided that
+ * the above copyright notice appear in all copies and that both that copyright
+ * notice and this permission notice appear in supporting documentation, and
+ * that the name of the copyright holders not be used in advertising or
+ * publicity pertaining to distribution of the software without specific,
+ * written prior permission. The copyright holders make no representations
+ * about the suitability of this software for any purpose. It is provided "as
+ * is" without express or implied warranty.
+ *
+ * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
+ * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
+ * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
+ * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
+ * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
+ * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+#ifdef HAVE_XORG_CONFIG_H
+#include <xorg-config.h>
+#endif
+
+#include "dri3_priv.h"
+#include <syncsrv.h>
+#include <unistd.h>
+#include <xace.h>
+#include "../Xext/syncsdk.h"
+
+static int
+proc_dri3_query_version(ClientPtr client)
+{
+ REQUEST(xDRI3QueryVersionReq);
+ xDRI3QueryVersionReply rep = {
+ .type = X_Reply,
+ .sequenceNumber = client->sequence,
+ .length = 0,
+ .majorVersion = DRI3_MAJOR,
+ .minorVersion = DRI3_MINOR
+ };
+
+ REQUEST_SIZE_MATCH(xDRI3QueryVersionReq);
+ (void) stuff;
+ if (client->swapped) {
+ swaps(&rep.sequenceNumber);
+ swapl(&rep.length);
+ swapl(&rep.majorVersion);
+ swapl(&rep.minorVersion);
+ }
+ WriteToClient(client, sizeof(rep), &rep);
+ return Success;
+}
+
+static int
+proc_dri3_open(ClientPtr client)
+{
+ REQUEST(xDRI3OpenReq);
+ xDRI3OpenReply rep = {
+ .type = X_Reply,
+ .nfd = 1,
+ .sequenceNumber = client->sequence,
+ .length = 0,
+ };
+ RRProviderPtr provider;
+ DrawablePtr drawable;
+ ScreenPtr screen;
+ int fd;
+ int status;
+
+ REQUEST_SIZE_MATCH(xDRI3OpenReq);
+
+ status = dixLookupDrawable(&drawable, stuff->drawable, client, 0, DixReadAccess);
+ if (status != Success)
+ return status;
+
+ if (stuff->provider == None)
+ provider = NULL;
+ else if (!RRProviderType) {
+ return BadMatch;
+ } else {
+ VERIFY_RR_PROVIDER(stuff->provider, provider, DixReadAccess);
+ if (drawable->pScreen != provider->pScreen)
+ return BadMatch;
+ }
+ screen = drawable->pScreen;
+
+ status = dri3_open(client, screen, provider, &fd);
+ if (status != Success)
+ return status;
+
+ if (client->swapped) {
+ swaps(&rep.sequenceNumber);
+ swapl(&rep.length);
+ }
+
+ if (WriteFdToClient(client, fd, TRUE) < 0) {
+ close(fd);
+ return BadAlloc;
+ }
+
+ WriteToClient(client, sizeof (rep), &rep);
+
+ return Success;
+}
+
+static int
+proc_dri3_pixmap_from_buffer(ClientPtr client)
+{
+ REQUEST(xDRI3PixmapFromBufferReq);
+ int fd;
+ DrawablePtr drawable;
+ PixmapPtr pixmap;
+ int rc;
+
+ SetReqFds(client, 1);
+ REQUEST_SIZE_MATCH(xDRI3PixmapFromBufferReq);
+ LEGAL_NEW_RESOURCE(stuff->pixmap, client);
+ rc = dixLookupDrawable(&drawable, stuff->drawable, client, M_ANY, DixGetAttrAccess);
+ if (rc != Success) {
+ client->errorValue = stuff->drawable;
+ return rc;
+ }
+
+ if (!stuff->width || !stuff->height) {
+ client->errorValue = 0;
+ return BadValue;
+ }
+
+ if (stuff->width > 32767 || stuff->height > 32767)
+ return BadAlloc;
+
+ if (stuff->depth != 1) {
+ DepthPtr depth = drawable->pScreen->allowedDepths;
+ int i;
+ for (i = 0; i < drawable->pScreen->numDepths; i++, depth++)
+ if (depth->depth == stuff->depth)
+ break;
+ if (i == drawable->pScreen->numDepths) {
+ client->errorValue = stuff->depth;
+ return BadValue;
+ }
+ }
+
+ fd = ReadFdFromClient(client);
+ if (fd < 0)
+ return BadValue;
+
+ rc = dri3_pixmap_from_fd(&pixmap,
+ drawable->pScreen, fd,
+ stuff->width, stuff->height,
+ stuff->stride, stuff->depth,
+ stuff->bpp);
+ close (fd);
+ if (rc != Success)
+ return rc;
+
+ pixmap->drawable.id = stuff->pixmap;
+
+ /* security creation/labeling check */
+ rc = XaceHook(XACE_RESOURCE_ACCESS, client, stuff->pixmap, RT_PIXMAP,
+ pixmap, RT_NONE, NULL, DixCreateAccess);
+
+ if (rc != Success) {
+ (*drawable->pScreen->DestroyPixmap) (pixmap);
+ return rc;
+ }
+ if (AddResource(stuff->pixmap, RT_PIXMAP, (pointer) pixmap))
+ return Success;
+
+ return Success;
+}
+
+static int
+proc_dri3_buffer_from_pixmap(ClientPtr client)
+{
+ REQUEST(xDRI3BufferFromPixmapReq);
+ xDRI3BufferFromPixmapReply rep = {
+ .type = X_Reply,
+ .nfd = 1,
+ .sequenceNumber = client->sequence,
+ .length = 0,
+ };
+ int rc;
+ int fd;
+ PixmapPtr pixmap;
+
+ REQUEST_SIZE_MATCH(xDRI3BufferFromPixmapReq);
+ rc = dixLookupResourceByType((pointer *) &pixmap, stuff->pixmap, RT_PIXMAP,
+ client, DixWriteAccess);
+ if (rc != Success) {
+ client->errorValue = stuff->pixmap;
+ return rc;
+ }
+
+ rep.width = pixmap->drawable.width;
+ rep.height = pixmap->drawable.height;
+ rep.depth = pixmap->drawable.depth;
+ rep.bpp = pixmap->drawable.bitsPerPixel;
+
+ rc = dri3_fd_from_pixmap(&fd, pixmap, &rep.stride, &rep.size);
+ if (rc != Success)
+ return rc;
+
+ if (client->swapped) {
+ swaps(&rep.sequenceNumber);
+ swapl(&rep.length);
+ swapl(&rep.size);
+ swaps(&rep.width);
+ swaps(&rep.height);
+ swaps(&rep.stride);
+ }
+ if (WriteFdToClient(client, fd, TRUE) < 0) {
+ close(fd);
+ return BadAlloc;
+ }
+
+ WriteToClient(client, sizeof(rep), &rep);
+
+ return client->noClientException;
+}
+
+static int
+proc_dri3_fence_from_fd(ClientPtr client)
+{
+ REQUEST(xDRI3FenceFromFDReq);
+ DrawablePtr drawable;
+ int fd;
+ int status;
+
+ SetReqFds(client, 1);
+ REQUEST_SIZE_MATCH(xDRI3FenceFromFDReq);
+ LEGAL_NEW_RESOURCE(stuff->fence, client);
+
+ status = dixLookupDrawable(&drawable, stuff->drawable, client, M_ANY, DixGetAttrAccess);
+ if (status != Success)
+ return status;
+
+ fd = ReadFdFromClient(client);
+ if (fd < 0)
+ return BadValue;
+
+ status = SyncCreateFenceFromFD(client, drawable, stuff->fence,
+ fd, stuff->initially_triggered);
+
+ return status;
+}
+
+static int
+proc_dri3_fd_from_fence(ClientPtr client)
+{
+ REQUEST(xDRI3FDFromFenceReq);
+ xDRI3FDFromFenceReply rep = {
+ .type = X_Reply,
+ .nfd = 1,
+ .sequenceNumber = client->sequence,
+ .length = 0,
+ };
+ DrawablePtr drawable;
+ int fd;
+ int status;
+ SyncFence *fence;
+
+ REQUEST_SIZE_MATCH(xDRI3FDFromFenceReq);
+
+ status = dixLookupDrawable(&drawable, stuff->drawable, client, M_ANY, DixGetAttrAccess);
+ if (status != Success)
+ return status;
+ status = SyncVerifyFence(&fence, stuff->fence, client, DixWriteAccess);
+ if (status != Success)
+ return status;
+
+ fd = SyncFDFromFence(client, drawable, fence);
+ if (fd < 0)
+ return BadMatch;
+
+ if (client->swapped) {
+ swaps(&rep.sequenceNumber);
+ swapl(&rep.length);
+ }
+ if (WriteFdToClient(client, fd, FALSE) < 0)
+ return BadAlloc;
+
+ WriteToClient(client, sizeof(rep), &rep);
+
+ return client->noClientException;
+}
+
+int (*proc_dri3_vector[DRI3NumberRequests]) (ClientPtr) = {
+ proc_dri3_query_version, /* 0 */
+ proc_dri3_open, /* 1 */
+ proc_dri3_pixmap_from_buffer, /* 2 */
+ proc_dri3_buffer_from_pixmap, /* 3 */
+ proc_dri3_fence_from_fd, /* 4 */
+ proc_dri3_fd_from_fence, /* 5 */
+};
+
+int
+proc_dri3_dispatch(ClientPtr client)
+{
+ REQUEST(xReq);
+ if (stuff->data >= DRI3NumberRequests || !proc_dri3_vector[stuff->data])
+ return BadRequest;
+ return (*proc_dri3_vector[stuff->data]) (client);
+}
+
+static int
+sproc_dri3_query_version(ClientPtr client)
+{
+ REQUEST(xDRI3QueryVersionReq);
+
+ swaps(&stuff->length);
+ swapl(&stuff->majorVersion);
+ swapl(&stuff->minorVersion);
+ return (*proc_dri3_vector[stuff->dri3ReqType]) (client);
+}
+
+static int
+sproc_dri3_open(ClientPtr client)
+{
+ REQUEST(xDRI3OpenReq);
+
+ swaps(&stuff->length);
+ swapl(&stuff->drawable);
+ swapl(&stuff->provider);
+ return (*proc_dri3_vector[stuff->dri3ReqType]) (client);
+}
+
+static int
+sproc_dri3_pixmap_from_buffer(ClientPtr client)
+{
+ REQUEST(xDRI3PixmapFromBufferReq);
+
+ swaps(&stuff->length);
+ swapl(&stuff->pixmap);
+ swapl(&stuff->drawable);
+ swapl(&stuff->size);
+ swaps(&stuff->width);
+ swaps(&stuff->height);
+ swaps(&stuff->stride);
+ return (*proc_dri3_vector[stuff->dri3ReqType]) (client);
+}
+
+static int
+sproc_dri3_buffer_from_pixmap(ClientPtr client)
+{
+ REQUEST(xDRI3BufferFromPixmapReq);
+
+ swaps(&stuff->length);
+ swapl(&stuff->pixmap);
+ return (*proc_dri3_vector[stuff->dri3ReqType]) (client);
+}
+
+static int
+sproc_dri3_fence_from_fd(ClientPtr client)
+{
+ REQUEST(xDRI3FenceFromFDReq);
+
+ swaps(&stuff->length);
+ swapl(&stuff->drawable);
+ swapl(&stuff->fence);
+ return (*proc_dri3_vector[stuff->dri3ReqType]) (client);
+}
+
+static int
+sproc_dri3_fd_from_fence(ClientPtr client)
+{
+ REQUEST(xDRI3FDFromFenceReq);
+
+ swaps(&stuff->length);
+ swapl(&stuff->drawable);
+ swapl(&stuff->fence);
+ return (*proc_dri3_vector[stuff->dri3ReqType]) (client);
+}
+
+int (*sproc_dri3_vector[DRI3NumberRequests]) (ClientPtr) = {
+ sproc_dri3_query_version, /* 0 */
+ sproc_dri3_open, /* 1 */
+ sproc_dri3_pixmap_from_buffer, /* 2 */
+ sproc_dri3_buffer_from_pixmap, /* 3 */
+ sproc_dri3_fence_from_fd, /* 4 */
+ sproc_dri3_fd_from_fence, /* 5 */
+};
+
+int
+sproc_dri3_dispatch(ClientPtr client)
+{
+ REQUEST(xReq);
+ if (stuff->data >= DRI3NumberRequests || !sproc_dri3_vector[stuff->data])
+ return BadRequest;
+ return (*sproc_dri3_vector[stuff->data]) (client);
+}
diff --git a/dri3/dri3_screen.c b/dri3/dri3_screen.c
new file mode 100644
index 000000000..cf2735b8d
--- /dev/null
+++ b/dri3/dri3_screen.c
@@ -0,0 +1,80 @@
+/*
+ * Copyright © 2013 Keith Packard
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and its
+ * documentation for any purpose is hereby granted without fee, provided that
+ * the above copyright notice appear in all copies and that both that copyright
+ * notice and this permission notice appear in supporting documentation, and
+ * that the name of the copyright holders not be used in advertising or
+ * publicity pertaining to distribution of the software without specific,
+ * written prior permission. The copyright holders make no representations
+ * about the suitability of this software for any purpose. It is provided "as
+ * is" without express or implied warranty.
+ *
+ * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
+ * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
+ * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
+ * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
+ * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
+ * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+#ifdef HAVE_XORG_CONFIG_H
+#include <xorg-config.h>
+#endif
+
+#include "dri3_priv.h"
+#include <syncsdk.h>
+#include <misync.h>
+#include <misyncshm.h>
+#include <randrstr.h>
+
+int
+dri3_open(ClientPtr client, ScreenPtr screen, RRProviderPtr provider, int *fd)
+{
+ dri3_screen_priv_ptr ds = dri3_screen_priv(screen);
+ dri3_screen_info_ptr info = ds->info;
+ int rc;
+
+ if (!info || !info->open)
+ return BadMatch;
+
+ rc = (*info->open) (screen, provider, fd);
+ if (rc != Success)
+ return rc;
+
+ return Success;
+}
+
+int
+dri3_pixmap_from_fd(PixmapPtr *ppixmap, ScreenPtr screen, int fd,
+ CARD16 width, CARD16 height, CARD16 stride, CARD8 depth, CARD8 bpp)
+{
+ dri3_screen_priv_ptr ds = dri3_screen_priv(screen);
+ dri3_screen_info_ptr info = ds->info;
+ PixmapPtr pixmap;
+
+ pixmap = (*info->pixmap_from_fd) (screen, fd, width, height, stride, depth, bpp);
+ if (!pixmap)
+ return BadAlloc;
+
+ *ppixmap = pixmap;
+ return Success;
+}
+
+int
+dri3_fd_from_pixmap(int *pfd, PixmapPtr pixmap, CARD16 *stride, CARD32 *size)
+{
+ ScreenPtr screen = pixmap->drawable.pScreen;
+ dri3_screen_priv_ptr ds = dri3_screen_priv(screen);
+ dri3_screen_info_ptr info = ds->info;
+ int fd;
+
+ fd = (*info->fd_from_pixmap)(screen, pixmap, stride, size);
+ if (fd < 0)
+ return BadAlloc;
+ *pfd = fd;
+ return Success;
+}
+
diff --git a/dri3/dri3int.h b/dri3/dri3int.h
new file mode 100644
index 000000000..7f53eba45
--- /dev/null
+++ b/dri3/dri3int.h
@@ -0,0 +1,26 @@
+/*
+ * Copyright © 2011 Daniel Stone
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS 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 SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Author: Daniel Stone <daniel@fooishbar.org>
+ */
+
+extern Bool DRI2ModuleSetup(void);