#include <X11/Xlib-xcb.h>
#include <xcb/xcb.h>
#include <xcb/dri2.h>
-#include "glapi.h"
#include "glxclient.h"
#include <X11/extensions/dri2proto.h>
-#include "xf86dri.h"
#include <dlfcn.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/time.h>
-#include "xf86drm.h"
#include "dri2.h"
#include "dri_common.h"
+#include "dri2_priv.h"
+#include "loader.h"
/* From xmlpool/options.h, user exposed so should be stable */
#define DRI_CONF_VBLANK_NEVER 0
const __DRIextension *loader_extensions[4];
};
-struct dri2_screen {
- struct glx_screen base;
-
- __DRIscreen *driScreen;
- __GLXDRIscreen vtable;
- const __DRIdri2Extension *dri2;
- const __DRIcoreExtension *core;
-
- const __DRI2flushExtension *f;
- const __DRI2configQueryExtension *config;
- const __DRItexBufferExtension *texBuffer;
- const __DRI2throttleExtension *throttle;
- const __DRIconfig **driver_configs;
-
- void *driver;
- int fd;
-
- Bool show_fps;
-};
-
struct dri2_context
{
struct glx_context base;
struct dri2_context *pcp = (struct dri2_context *) context;
struct dri2_screen *psc = (struct dri2_screen *) pcp->base.psc;
struct dri2_drawable *pdraw, *pread;
+ __DRIdrawable *dri_draw = NULL, *dri_read = NULL;
+ struct glx_display *dpyPriv = psc->base.display;
struct dri2_display *pdp;
pdraw = (struct dri2_drawable *) driFetchDrawable(context, draw);
driReleaseDrawables(&pcp->base);
- if (pdraw == NULL || pread == NULL)
+ if (pdraw)
+ dri_draw = pdraw->driDrawable;
+ else if (draw != None)
+ return GLXBadDrawable;
+
+ if (pread)
+ dri_read = pread->driDrawable;
+ else if (read != None)
return GLXBadDrawable;
- if (!(*psc->core->bindContext) (pcp->driContext,
- pdraw->driDrawable, pread->driDrawable))
+ if (!(*psc->core->bindContext) (pcp->driContext, dri_draw, dri_read))
return GLXBadContext;
/* If the server doesn't send invalidate events, we may miss a
* resize before the rendering starts. Invalidate the buffers now
* so the driver will recheck before rendering starts. */
- pdp = (struct dri2_display *) psc->base.display;
- if (!pdp->invalidateAvailable) {
+ pdp = (struct dri2_display *) dpyPriv->dri2Display;
+ if (!pdp->invalidateAvailable && pdraw) {
dri2InvalidateBuffers(psc->base.dpy, pdraw->base.xDrawable);
- if (pread != pdraw)
+ if (pread != pdraw && pread)
dri2InvalidateBuffers(psc->base.dpy, pread->base.xDrawable);
}
__GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base;
__DRIcontext *shared = NULL;
+ /* Check the renderType value */
+ if (!validate_renderType_against_config(config_base, renderType))
+ return NULL;
+
if (shareList) {
/* If the shareList context is not a DRI2 context, we cannot possibly
* create a DRI2 context that shares it.
return NULL;
}
+ pcp->base.renderType = renderType;
+
pcp->driContext =
(*psc->dri2->createNewContext) (psc->driScreen,
config->driConfig, shared, pcp);
__GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base;
__DRIcontext *shared = NULL;
- uint32_t minor_ver = 1;
- uint32_t major_ver = 2;
- uint32_t flags = 0;
+ uint32_t minor_ver;
+ uint32_t major_ver;
+ uint32_t renderType;
+ uint32_t flags;
unsigned api;
- int reset = __DRI_CTX_RESET_NO_NOTIFICATION;
+ int reset;
uint32_t ctx_attribs[2 * 5];
unsigned num_ctx_attribs = 0;
/* Remap the GLX tokens to DRI2 tokens.
*/
if (!dri2_convert_glx_attribs(num_attribs, attribs,
- &major_ver, &minor_ver, &flags, &api, &reset,
- error))
+ &major_ver, &minor_ver, &renderType, &flags,
+ &api, &reset, error))
goto error_exit;
+ /* Check the renderType value */
+ if (!validate_renderType_against_config(config_base, renderType))
+ goto error_exit;
+
if (shareList) {
pcp_shared = (struct dri2_context *) shareList;
shared = pcp_shared->driContext;
ctx_attribs[num_ctx_attribs++] = flags;
}
+ /* The renderType is retrieved from attribs, or set to default
+ * of GLX_RGBA_TYPE.
+ */
+ pcp->base.renderType = renderType;
+
pcp->driContext =
(*psc->dri2->createContextAttribs) (psc->driScreen,
api,
struct dri2_display *pdp;
GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
+ dpyPriv = __glXInitialize(psc->base.dpy);
+ if (dpyPriv == NULL)
+ return NULL;
+
pdraw = calloc(1, sizeof(*pdraw));
if (!pdraw)
return NULL;
}
DRI2CreateDrawable(psc->base.dpy, xDrawable);
-
- dpyPriv = __glXInitialize(psc->base.dpy);
pdp = (struct dri2_display *)dpyPriv->dri2Display;;
/* Create a new drawable */
pdraw->driDrawable =
}
}
+/**
+ * Asks the driver to flush any queued work necessary for serializing with the
+ * X command stream, and optionally the slightly more strict requirement of
+ * glFlush() equivalence (which would require flushing even if nothing had
+ * been drawn to a window system framebuffer, for example).
+ */
+static void
+dri2Flush(struct dri2_screen *psc,
+ __DRIcontext *ctx,
+ struct dri2_drawable *draw,
+ unsigned flags,
+ enum __DRI2throttleReason throttle_reason)
+{
+ if (ctx && psc->f && psc->f->base.version >= 4) {
+ psc->f->flush_with_flags(ctx, draw->driDrawable, flags, throttle_reason);
+ } else {
+ if (flags & __DRI2_FLUSH_CONTEXT)
+ glFlush();
+
+ if (psc->f)
+ psc->f->flush(draw->driDrawable);
+
+ dri2Throttle(psc, draw, throttle_reason);
+ }
+}
+
static void
__dri2CopySubBuffer(__GLXDRIdrawable *pdraw, int x, int y,
int width, int height,
struct dri2_screen *psc = (struct dri2_screen *) pdraw->psc;
XRectangle xrect;
XserverRegion region;
+ __DRIcontext *ctx = dri2GetCurrentContext();
+ unsigned flags;
/* Check we have the right attachments */
if (!priv->have_back)
xrect.width = width;
xrect.height = height;
- if (psc->f && psc->f->base.version >= 4) {
- unsigned flags = (flush ? __DRI2_FLUSH_CONTEXT : 0) |
- __DRI2_FLUSH_DRAWABLE;
- __DRIcontext *ctx = dri2GetCurrentContext();
-
- if (ctx) {
- (*psc->f->flush_with_flags)(ctx, priv->driDrawable, flags, reason);
- }
- }
- else {
- if (flush) {
- glFlush();
- }
-
- if (psc->f) {
- (*psc->f->flush) (priv->driDrawable);
- }
-
- dri2Throttle(psc, priv, reason);
- }
+ flags = __DRI2_FLUSH_DRAWABLE;
+ if (flush)
+ flags |= __DRI2_FLUSH_CONTEXT;
+ dri2Flush(psc, ctx, priv, flags, __DRI2_THROTTLE_SWAPBUFFER);
region = XFixesCreateRegion(psc->base.dpy, &xrect, 1);
DRI2CopyRegion(psc->base.dpy, pdraw->xDrawable, region,
psc = (struct dri2_screen *) pdraw->base.psc;
priv = __glXInitialize(psc->base.dpy);
+
+ if (priv == NULL)
+ return;
+
pdp = (struct dri2_display *) priv->dri2Display;
gc = __glXGetCurrentContext();
static void show_fps(struct dri2_drawable *draw)
{
+ const int interval =
+ ((struct dri2_screen *) draw->base.psc)->show_fps_interval;
struct timeval tv;
uint64_t current_time;
draw->frames++;
- if (draw->previous_time + 1000000 <= current_time) {
+ if (draw->previous_time + interval * 1000000 <= current_time) {
if (draw->previous_time) {
fprintf(stderr, "libGL: FPS = %.1f\n",
((uint64_t)draw->frames * 1000000) /
__dri2CopySubBuffer(pdraw, 0, 0, priv->width, priv->height,
__DRI2_THROTTLE_SWAPBUFFER, flush);
} else {
- if (psc->f && psc->f->base.version >= 4) {
- unsigned flags = (flush ? __DRI2_FLUSH_CONTEXT : 0) |
- __DRI2_FLUSH_DRAWABLE;
- __DRIcontext *ctx = dri2GetCurrentContext();
-
- if (ctx) {
- (*psc->f->flush_with_flags)(ctx, priv->driDrawable, flags,
- __DRI2_THROTTLE_SWAPBUFFER);
- }
- }
- else {
- if (flush) {
- glFlush();
- }
-
- if (psc->f) {
- struct glx_context *gc = __glXGetCurrentContext();
-
- if (gc) {
- (*psc->f->flush)(priv->driDrawable);
- }
- }
-
- dri2Throttle(psc, priv, __DRI2_THROTTLE_SWAPBUFFER);
- }
+ __DRIcontext *ctx = dri2GetCurrentContext();
+ unsigned flags = __DRI2_FLUSH_DRAWABLE;
+ if (flush)
+ flags |= __DRI2_FLUSH_CONTEXT;
+ dri2Flush(psc, ctx, priv, flags, __DRI2_THROTTLE_SWAPBUFFER);
ret = dri2XcbSwapBuffers(pdraw->psc->dpy, pdraw,
target_msc, divisor, remainder);
}
- if (psc->show_fps) {
+ if (psc->show_fps_interval) {
show_fps(priv);
}
}
static const __DRIdri2LoaderExtension dri2LoaderExtension = {
- {__DRI_DRI2_LOADER, __DRI_DRI2_LOADER_VERSION},
- dri2GetBuffers,
- dri2FlushFrontBuffer,
- dri2GetBuffersWithFormat,
+ .base = { __DRI_DRI2_LOADER, 3 },
+
+ .getBuffers = dri2GetBuffers,
+ .flushFrontBuffer = dri2FlushFrontBuffer,
+ .getBuffersWithFormat = dri2GetBuffersWithFormat,
};
static const __DRIdri2LoaderExtension dri2LoaderExtension_old = {
- {__DRI_DRI2_LOADER, __DRI_DRI2_LOADER_VERSION},
- dri2GetBuffers,
- dri2FlushFrontBuffer,
- NULL,
+ .base = { __DRI_DRI2_LOADER, 3 },
+
+ .getBuffers = dri2GetBuffers,
+ .flushFrontBuffer = dri2FlushFrontBuffer,
+ .getBuffersWithFormat = NULL,
};
static const __DRIuseInvalidateExtension dri2UseInvalidate = {
- { __DRI_USE_INVALIDATE, __DRI_USE_INVALIDATE_VERSION }
+ .base = { __DRI_USE_INVALIDATE, 1 }
};
_X_HIDDEN void
__GLXDRIdrawable *base = GetGLXDRIDrawable(dpy, drawable);
struct glx_display *dpyPriv = __glXInitialize(dpy);
struct dri2_drawable *pdraw = (struct dri2_drawable *) base;
- struct dri2_display *pdp =
- (struct dri2_display *) dpyPriv->dri2Display;
+ struct dri2_display *pdp;
struct dri2_screen *psc;
+ if (dpyPriv == NULL)
+ return;
+
+ pdp = (struct dri2_display *) dpyPriv->dri2Display;
+
if (pdraw != NULL) {
psc = (struct dri2_screen *) base->psc;
static void
dri2_release_tex_image(Display * dpy, GLXDrawable drawable, int buffer)
{
-#if __DRI_TEX_BUFFER_VERSION >= 3
struct glx_context *gc = __glXGetCurrentContext();
struct dri2_context *pcp = (struct dri2_context *) gc;
__GLXDRIdrawable *base = GetGLXDRIDrawable(dpy, drawable);
struct glx_display *dpyPriv = __glXInitialize(dpy);
struct dri2_drawable *pdraw = (struct dri2_drawable *) base;
- struct dri2_display *pdp =
- (struct dri2_display *) dpyPriv->dri2Display;
struct dri2_screen *psc;
- if (pdraw != NULL) {
+ if (dpyPriv != NULL && pdraw != NULL) {
psc = (struct dri2_screen *) base->psc;
if (psc->texBuffer->base.version >= 3 &&
pdraw->driDrawable);
}
}
-#endif
}
static const struct glx_context_vtable dri2_context_vtable = {
- dri2_destroy_context,
- dri2_bind_context,
- dri2_unbind_context,
- dri2_wait_gl,
- dri2_wait_x,
- DRI_glXUseXFont,
- dri2_bind_tex_image,
- dri2_release_tex_image,
- NULL, /* get_proc_address */
+ .destroy = dri2_destroy_context,
+ .bind = dri2_bind_context,
+ .unbind = dri2_unbind_context,
+ .wait_gl = dri2_wait_gl,
+ .wait_x = dri2_wait_x,
+ .use_x_font = DRI_glXUseXFont,
+ .bind_tex_image = dri2_bind_tex_image,
+ .release_tex_image = dri2_release_tex_image,
+ .get_proc_address = NULL,
};
static void
-dri2BindExtensions(struct dri2_screen *psc, const __DRIextension **extensions)
+dri2BindExtensions(struct dri2_screen *psc, struct glx_display * priv,
+ const char *driverName)
{
+ const struct dri2_display *const pdp = (struct dri2_display *)
+ priv->dri2Display;
+ const __DRIextension **extensions;
int i;
+ extensions = psc->core->getExtensions(psc->driScreen);
+
__glXEnableDirectExtension(&psc->base, "GLX_SGI_video_sync");
__glXEnableDirectExtension(&psc->base, "GLX_SGI_swap_control");
__glXEnableDirectExtension(&psc->base, "GLX_MESA_swap_control");
__glXEnableDirectExtension(&psc->base, "GLX_SGI_make_current_read");
- if (psc->dri2->base.version >= 4) {
+ /*
+ * GLX_INTEL_swap_event is broken on the server side, where it's
+ * currently unconditionally enabled. This completely breaks
+ * systems running on drivers which don't support that extension.
+ * There's no way to test for its presence on this side, so instead
+ * of disabling it unconditionally, just disable it for drivers
+ * which are known to not support it, or for DDX drivers supporting
+ * only an older (pre-ScheduleSwap) version of DRI2.
+ *
+ * This is a hack which is required until:
+ * http://lists.x.org/archives/xorg-devel/2013-February/035449.html
+ * is merged and updated xserver makes it's way into distros:
+ */
+ if (pdp->swapAvailable && strcmp(driverName, "vmwgfx") != 0) {
__glXEnableDirectExtension(&psc->base, "GLX_INTEL_swap_event");
}
&& strcmp(extensions[i]->name, __DRI2_ROBUSTNESS) == 0)
__glXEnableDirectExtension(&psc->base,
"GLX_ARB_create_context_robustness");
+
+ /* DRI2 version 3 is also required because GLX_MESA_query_renderer
+ * requires GLX_ARB_create_context_profile.
+ */
+ if (psc->dri2->base.version >= 3
+ && strcmp(extensions[i]->name, __DRI2_RENDERER_QUERY) == 0) {
+ psc->rendererQuery = (__DRI2rendererQueryExtension *) extensions[i];
+ __glXEnableDirectExtension(&psc->base, "GLX_MESA_query_renderer");
+ }
}
}
static const struct glx_screen_vtable dri2_screen_vtable = {
- dri2_create_context,
- dri2_create_context_attribs
+ .create_context = dri2_create_context,
+ .create_context_attribs = dri2_create_context_attribs,
+ .query_renderer_integer = dri2_query_renderer_integer,
+ .query_renderer_string = dri2_query_renderer_string,
};
static struct glx_screen *
struct dri2_screen *psc;
__GLXDRIscreen *psp;
struct glx_config *configs = NULL, *visuals = NULL;
- char *driverName, *deviceName, *tmp;
+ char *driverName = NULL, *loader_driverName, *deviceName, *tmp;
drm_magic_t magic;
int i;
return NULL;
}
- psc->driver = driOpenDriver(driverName);
- if (psc->driver == NULL) {
- ErrorMessageF("driver pointer missing\n");
- goto handle_error;
- }
-
- extensions = dlsym(psc->driver, __DRI_DRIVER_EXTENSIONS);
- if (extensions == NULL) {
- ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
- goto handle_error;
- }
-
- for (i = 0; extensions[i]; i++) {
- if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
- psc->core = (__DRIcoreExtension *) extensions[i];
- if (strcmp(extensions[i]->name, __DRI_DRI2) == 0)
- psc->dri2 = (__DRIdri2Extension *) extensions[i];
- }
-
- if (psc->core == NULL || psc->dri2 == NULL) {
- ErrorMessageF("core dri or dri2 extension not found\n");
- goto handle_error;
- }
-
#ifdef O_CLOEXEC
psc->fd = open(deviceName, O_RDWR | O_CLOEXEC);
if (psc->fd == -1 && errno == EINVAL)
goto handle_error;
}
-
- /* If the server does not support the protocol for
- * DRI2GetBuffersWithFormat, don't supply that interface to the driver.
+ /* If Mesa knows about the appropriate driver for this fd, then trust it.
+ * Otherwise, default to the server's value.
*/
- psc->driScreen =
- psc->dri2->createNewScreen(screen, psc->fd,
- (const __DRIextension **)
- &pdp->loader_extensions[0],
- &driver_configs, psc);
+ loader_driverName = loader_get_driver_for_fd(psc->fd, 0);
+ if (loader_driverName) {
+ free(driverName);
+ driverName = loader_driverName;
+ }
+
+ psc->driver = driOpenDriver(driverName);
+ if (psc->driver == NULL) {
+ ErrorMessageF("driver pointer missing\n");
+ goto handle_error;
+ }
+
+ extensions = driGetDriverExtensions(psc->driver, driverName);
+ if (extensions == NULL)
+ goto handle_error;
+
+ for (i = 0; extensions[i]; i++) {
+ if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
+ psc->core = (__DRIcoreExtension *) extensions[i];
+ if (strcmp(extensions[i]->name, __DRI_DRI2) == 0)
+ psc->dri2 = (__DRIdri2Extension *) extensions[i];
+ }
+
+ if (psc->core == NULL || psc->dri2 == NULL) {
+ ErrorMessageF("core dri or dri2 extension not found\n");
+ goto handle_error;
+ }
+
+ if (psc->dri2->base.version >= 4) {
+ psc->driScreen =
+ psc->dri2->createNewScreen2(screen, psc->fd,
+ (const __DRIextension **)
+ &pdp->loader_extensions[0],
+ extensions,
+ &driver_configs, psc);
+ } else {
+ psc->driScreen =
+ psc->dri2->createNewScreen(screen, psc->fd,
+ (const __DRIextension **)
+ &pdp->loader_extensions[0],
+ &driver_configs, psc);
+ }
if (psc->driScreen == NULL) {
ErrorMessageF("failed to create dri screen\n");
goto handle_error;
}
- extensions = psc->core->getExtensions(psc->driScreen);
- dri2BindExtensions(psc, extensions);
+ dri2BindExtensions(psc, priv, driverName);
configs = driConvertConfigs(psc->core, psc->base.configs, driver_configs);
visuals = driConvertConfigs(psc->core, psc->base.visuals, driver_configs);
psp->waitForSBC = NULL;
psp->setSwapInterval = NULL;
psp->getSwapInterval = NULL;
+ psp->getBufferAge = NULL;
if (pdp->driMinor >= 2) {
psp->getDrawableMSC = dri2DrawableGetMSC;
free(deviceName);
tmp = getenv("LIBGL_SHOW_FPS");
- psc->show_fps = tmp && strcmp(tmp, "1") == 0;
+ psc->show_fps_interval = (tmp) ? atoi(tmp) : 0;
+ if (psc->show_fps_interval < 0)
+ psc->show_fps_interval = 0;
return &psc->base;