#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"
void *driver;
int fd;
+
+ Bool show_fps;
};
struct dri2_context
int have_back;
int have_fake_front;
int swap_interval;
+
+ double previous_time;
+ unsigned frames;
};
static const struct glx_context_vtable dri2_context_vtable;
driReleaseDrawables(&pcp->base);
- if (context->xid)
- glx_send_destroy_context(psc->base.dpy, context->xid);
-
if (context->extensions)
XFree((char *) context->extensions);
__DRIcontext *shared = NULL;
if (shareList) {
+ /* If the shareList context is not a DRI2 context, we cannot possibly
+ * create a DRI2 context that shares it.
+ */
+ if (shareList->vtable->destroy != dri2_destroy_context) {
+ return NULL;
+ }
+
pcp_shared = (struct dri2_context *) shareList;
shared = pcp_shared->driContext;
}
return &pcp->base;
}
+static struct glx_context *
+dri2_create_context_attribs(struct glx_screen *base,
+ struct glx_config *config_base,
+ struct glx_context *shareList,
+ unsigned num_attribs,
+ const uint32_t *attribs,
+ unsigned *error)
+{
+ struct dri2_context *pcp = NULL;
+ struct dri2_context *pcp_shared = NULL;
+ struct dri2_screen *psc = (struct dri2_screen *) base;
+ __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base;
+ __DRIcontext *shared = NULL;
+
+ uint32_t minor_ver = 1;
+ uint32_t major_ver = 2;
+ uint32_t flags = 0;
+ unsigned api;
+ int reset;
+ uint32_t ctx_attribs[2 * 5];
+ unsigned num_ctx_attribs = 0;
+
+ if (psc->dri2->base.version < 3) {
+ *error = __DRI_CTX_ERROR_NO_MEMORY;
+ goto error_exit;
+ }
+
+ /* Remap the GLX tokens to DRI2 tokens.
+ */
+ if (!dri2_convert_glx_attribs(num_attribs, attribs,
+ &major_ver, &minor_ver, &flags, &api, &reset,
+ error))
+ goto error_exit;
+
+ if (shareList) {
+ pcp_shared = (struct dri2_context *) shareList;
+ shared = pcp_shared->driContext;
+ }
+
+ pcp = Xmalloc(sizeof *pcp);
+ if (pcp == NULL) {
+ *error = __DRI_CTX_ERROR_NO_MEMORY;
+ goto error_exit;
+ }
+
+ memset(pcp, 0, sizeof *pcp);
+ if (!glx_context_init(&pcp->base, &psc->base, &config->base))
+ goto error_exit;
+
+ ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_MAJOR_VERSION;
+ ctx_attribs[num_ctx_attribs++] = major_ver;
+ ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_MINOR_VERSION;
+ ctx_attribs[num_ctx_attribs++] = minor_ver;
+
+ /* Only send a value when the non-default value is requested. By doing
+ * this we don't have to check the driver's DRI2 version before sending the
+ * default value.
+ */
+ if (reset != __DRI_CTX_RESET_NO_NOTIFICATION) {
+ ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_RESET_STRATEGY;
+ ctx_attribs[num_ctx_attribs++] = reset;
+ }
+
+ if (flags != 0) {
+ ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_FLAGS;
+
+ /* The current __DRI_CTX_FLAG_* values are identical to the
+ * GLX_CONTEXT_*_BIT values.
+ */
+ ctx_attribs[num_ctx_attribs++] = flags;
+ }
+
+ pcp->driContext =
+ (*psc->dri2->createContextAttribs) (psc->driScreen,
+ api,
+ config->driConfig,
+ shared,
+ num_ctx_attribs / 2,
+ ctx_attribs,
+ error,
+ pcp);
+
+ if (pcp->driContext == NULL)
+ goto error_exit;
+
+ pcp->base.vtable = &dri2_context_vtable;
+
+ return &pcp->base;
+
+error_exit:
+ if (pcp != NULL)
+ Xfree(pcp);
+
+ return NULL;
+}
+
static void
dri2DestroyDrawable(__GLXDRIdrawable *base)
{
return glx_dpy->codes->first_event + GLX_BufferSwapComplete;
}
+static void show_fps(struct dri2_drawable *draw)
+{
+ struct timeval tv;
+ double current_time;
+
+ gettimeofday(&tv, 0);
+ current_time = (double)tv.tv_sec + (double)tv.tv_usec * 0.000001;
+
+ draw->frames++;
+
+ if (draw->previous_time + 1 < current_time) {
+ if (draw->previous_time) {
+ fprintf(stderr, "libGL: FPS = %.1f\n",
+ draw->frames / (current_time - draw->previous_time));
+ }
+ draw->frames = 0;
+ draw->previous_time = current_time;
+ }
+}
+
static int64_t
dri2SwapBuffers(__GLXDRIdrawable *pdraw, int64_t target_msc, int64_t divisor,
int64_t remainder)
#endif
}
+ if (psc->show_fps) {
+ show_fps(priv);
+ }
+
/* Old servers don't send invalidate events */
if (!pdp->invalidateAvailable)
dri2InvalidateBuffers(dpyPriv->dpy, pdraw->xDrawable);
/* FIXME: if DRI2 version supports it... */
__glXEnableDirectExtension(&psc->base, "INTEL_swap_event");
+ if (psc->dri2->base.version >= 3) {
+ const unsigned mask = psc->dri2->getAPIMask(psc->driScreen);
+
+ __glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context");
+ __glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context_profile");
+
+ if ((mask & (1 << __DRI_API_GLES2)) != 0)
+ __glXEnableDirectExtension(&psc->base,
+ "GLX_EXT_create_context_es2_profile");
+ }
+
for (i = 0; extensions[i]; i++) {
if ((strcmp(extensions[i]->name, __DRI_TEX_BUFFER) == 0)) {
psc->texBuffer = (__DRItexBufferExtension *) extensions[i];
if (((strcmp(extensions[i]->name, __DRI2_THROTTLE) == 0)))
psc->throttle = (__DRI2throttleExtension *) extensions[i];
+
+ /* DRI2 version 3 is also required because
+ * GLX_ARB_create_context_robustness requires GLX_ARB_create_context.
+ */
+ if (psc->dri2->base.version >= 3
+ && strcmp(extensions[i]->name, __DRI2_ROBUSTNESS) == 0)
+ __glXEnableDirectExtension(&psc->base,
+ "GLX_ARB_create_context_robustness");
}
}
static const struct glx_screen_vtable dri2_screen_vtable = {
- dri2_create_context
+ dri2_create_context,
+ dri2_create_context_attribs
};
static struct glx_screen *
struct dri2_screen *psc;
__GLXDRIscreen *psp;
struct glx_config *configs = NULL, *visuals = NULL;
- char *driverName, *deviceName;
+ char *driverName, *deviceName, *tmp;
drm_magic_t magic;
int i;
goto handle_error;
}
- psc->fd = open(deviceName, O_RDWR);
+#ifdef O_CLOEXEC
+ psc->fd = open(deviceName, O_RDWR | O_CLOEXEC);
+ if (psc->fd == -1 && errno == EINVAL)
+#endif
+ {
+ psc->fd = open(deviceName, O_RDWR);
+ if (psc->fd != -1)
+ fcntl(psc->fd, F_SETFD, fcntl(psc->fd, F_GETFD) | FD_CLOEXEC);
+ }
if (psc->fd < 0) {
ErrorMessageF("failed to open drm device: %s\n", strerror(errno));
goto handle_error;
Xfree(driverName);
Xfree(deviceName);
+ tmp = getenv("LIBGL_SHOW_FPS");
+ psc->show_fps = tmp && strcmp(tmp, "1") == 0;
+
return &psc->base;
handle_error:
+ CriticalErrorMessageF("failed to load driver: %s\n", driverName);
+
if (configs)
glx_config_destroy_list(configs);
if (visuals)