summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Clark <robdclark@gmail.com>2017-02-22 11:36:12 -0500
committerRob Clark <robdclark@gmail.com>2017-02-25 10:48:22 -0500
commit87e3ff5683ee54228b3e6e75f7d4de83901fadb0 (patch)
tree0aa767fb89bbf7c64670fb117f5a2b65cda9db4a
parent1f1158fe1279e845420d3cb838eff3bbf2e5701a (diff)
add atomic kms support
Based mostly on original atomic conversion from Gustavo Padovan.
-rw-r--r--Makefile.am1
-rw-r--r--common.c4
-rw-r--r--common.h4
-rw-r--r--drm-atomic.c388
-rw-r--r--drm.c7
-rw-r--r--drm.h28
-rw-r--r--kmscube.c17
7 files changed, 445 insertions, 4 deletions
diff --git a/Makefile.am b/Makefile.am
index 24cc49c..eca8c7d 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -46,6 +46,7 @@ kmscube_SOURCES = \
cube-tex.c \
drm.c \
drm.h \
+ drm-atomic.c \
drm-legacy.c \
frame-512x512-NV12.c \
frame-512x512-RGBA.c \
diff --git a/common.c b/common.c
index f3443ac..4bf3c5a 100644
--- a/common.c
+++ b/common.c
@@ -77,6 +77,10 @@ int init_egl(struct egl *egl, const struct gbm *gbm)
get_proc(eglCreateImageKHR);
get_proc(eglDestroyImageKHR);
get_proc(glEGLImageTargetTexture2DOES);
+ get_proc(eglCreateSyncKHR);
+ get_proc(eglDestroySyncKHR);
+ get_proc(eglWaitSyncKHR);
+ get_proc(eglDupNativeFenceFDANDROID);
if (egl->eglGetPlatformDisplayEXT) {
egl->display = egl->eglGetPlatformDisplayEXT(EGL_PLATFORM_GBM_KHR,
diff --git a/common.h b/common.h
index 6a7d7f3..b7e0c5b 100644
--- a/common.h
+++ b/common.h
@@ -52,6 +52,10 @@ struct egl {
PFNEGLCREATEIMAGEKHRPROC eglCreateImageKHR;
PFNEGLDESTROYIMAGEKHRPROC eglDestroyImageKHR;
PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES;
+ PFNEGLCREATESYNCKHRPROC eglCreateSyncKHR;
+ PFNEGLDESTROYSYNCKHRPROC eglDestroySyncKHR;
+ PFNEGLWAITSYNCKHRPROC eglWaitSyncKHR;
+ PFNEGLDUPNATIVEFENCEFDANDROIDPROC eglDupNativeFenceFDANDROID;
void (*draw)(unsigned i);
};
diff --git a/drm-atomic.c b/drm-atomic.c
new file mode 100644
index 0000000..b4755e1
--- /dev/null
+++ b/drm-atomic.c
@@ -0,0 +1,388 @@
+/*
+ * Copyright (c) 2017 Rob Clark <rclark@redhat.com>
+ *
+ * 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, sub license,
+ * 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 NON-INFRINGEMENT. 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.
+ */
+
+#include <assert.h>
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "common.h"
+#include "drm.h"
+
+#define VOID2U64(x) ((uint64_t)(unsigned long)(x))
+
+static struct drm drm;
+
+
+static int add_connector_property(drmModeAtomicReq *req, uint32_t obj_id,
+ const char *name, uint64_t value)
+{
+ struct connector *obj = drm.connector;
+ unsigned int i;
+ int prop_id = 0;
+
+ for (i = 0 ; i < obj->props->count_props ; i++) {
+ if (strcmp(obj->props_info[i]->name, name) == 0) {
+ prop_id = obj->props_info[i]->prop_id;
+ break;
+ }
+ }
+
+ if (prop_id < 0) {
+ printf("no connector property: %s\n", name);
+ return -EINVAL;
+ }
+
+ return drmModeAtomicAddProperty(req, obj_id, prop_id, value);
+}
+
+static int add_crtc_property(drmModeAtomicReq *req, uint32_t obj_id,
+ const char *name, uint64_t value)
+{
+ struct crtc *obj = drm.crtc;
+ unsigned int i;
+ int prop_id = -1;
+
+ for (i = 0 ; i < obj->props->count_props ; i++) {
+ if (strcmp(obj->props_info[i]->name, name) == 0) {
+ prop_id = obj->props_info[i]->prop_id;
+ break;
+ }
+ }
+
+ if (prop_id < 0) {
+ printf("no crtc property: %s\n", name);
+ return -EINVAL;
+ }
+
+ return drmModeAtomicAddProperty(req, obj_id, prop_id, value);
+}
+
+static int add_plane_property(drmModeAtomicReq *req, uint32_t obj_id,
+ const char *name, uint64_t value)
+{
+ struct plane *obj = drm.plane;
+ unsigned int i;
+ int prop_id = -1;
+
+ for (i = 0 ; i < obj->props->count_props ; i++) {
+ if (strcmp(obj->props_info[i]->name, name) == 0) {
+ prop_id = obj->props_info[i]->prop_id;
+ break;
+ }
+ }
+
+
+ if (prop_id < 0) {
+ printf("no plane property: %s\n", name);
+ return -EINVAL;
+ }
+
+ return drmModeAtomicAddProperty(req, obj_id, prop_id, value);
+}
+
+static int drm_atomic_commit(uint32_t fb_id, uint32_t flags)
+{
+ drmModeAtomicReq *req;
+ uint32_t plane_id = drm.plane->plane->plane_id;
+ uint32_t blob_id;
+ unsigned int i;
+ int ret;
+
+ req = drmModeAtomicAlloc();
+
+ if (flags & DRM_MODE_ATOMIC_ALLOW_MODESET) {
+ if (add_connector_property(req, drm.connector_id, "CRTC_ID",
+ drm.crtc_id) < 0)
+ return -1;
+
+ if (drmModeCreatePropertyBlob(drm.fd, drm.mode, sizeof(*drm.mode),
+ &blob_id) != 0)
+ return -1;
+
+ if (add_crtc_property(req, drm.crtc_id, "MODE_ID", blob_id) < 0)
+ return -1;
+
+ if (add_crtc_property(req, drm.crtc_id, "ACTIVE", 1) < 0)
+ return -1;
+ }
+
+ add_plane_property(req, plane_id, "FB_ID", fb_id);
+ add_plane_property(req, plane_id, "CRTC_ID", drm.crtc_id);
+ add_plane_property(req, plane_id, "SRC_X", 0);
+ add_plane_property(req, plane_id, "SRC_Y", 0);
+ add_plane_property(req, plane_id, "SRC_W", drm.mode->hdisplay << 16);
+ add_plane_property(req, plane_id, "SRC_H", drm.mode->vdisplay << 16);
+ add_plane_property(req, plane_id, "CRTC_X", 0);
+ add_plane_property(req, plane_id, "CRTC_Y", 0);
+ add_plane_property(req, plane_id, "CRTC_W", drm.mode->hdisplay);
+ add_plane_property(req, plane_id, "CRTC_H", drm.mode->vdisplay);
+
+ if (drm.kms_in_fence_fd != -1) {
+ add_crtc_property(req, drm.crtc_id, "OUT_FENCE_PTR",
+ VOID2U64(&drm.kms_out_fence_fd));
+ add_plane_property(req, plane_id, "IN_FENCE_FD", drm.kms_in_fence_fd);
+ }
+
+ ret = drmModeAtomicCommit(drm.fd, req, flags, NULL);
+ if (ret)
+ goto out;
+
+ if (drm.kms_in_fence_fd != -1) {
+ close(drm.kms_in_fence_fd);
+ drm.kms_in_fence_fd = -1;
+ }
+
+out:
+ drmModeAtomicFree(req);
+
+ return ret;
+}
+
+static EGLSyncKHR create_fence(const struct egl *egl, int fd)
+{
+ EGLint attrib_list[] = {
+ EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fd,
+ EGL_NONE,
+ };
+ EGLSyncKHR fence = egl->eglCreateSyncKHR(egl->display,
+ EGL_SYNC_NATIVE_FENCE_ANDROID, attrib_list);
+ assert(fence);
+ return fence;
+}
+
+static int atomic_run(const struct gbm *gbm, const struct egl *egl)
+{
+ struct gbm_bo *bo;
+ struct drm_fb *fb;
+ uint32_t i = 0;
+ int ret;
+
+ if (!egl->eglDupNativeFenceFDANDROID) {
+ printf("no eglDupNativeFenceFDANDROID\n");
+ return -1;
+ }
+
+ eglSwapBuffers(egl->display, egl->surface);
+ bo = gbm_surface_lock_front_buffer(gbm->surface);
+ fb = drm_fb_get_from_bo(bo);
+
+ drm.kms_in_fence_fd = -1;
+
+ /* set mode: */
+ ret = drm_atomic_commit(fb->fb_id, DRM_MODE_ATOMIC_ALLOW_MODESET);
+ if (ret) {
+ printf("failed to commit modeset: %s\n", strerror(errno));
+ return ret;
+ }
+
+ while (1) {
+ struct gbm_bo *next_bo;
+ EGLSyncKHR gpu_fence = NULL; /* out-fence from gpu, in-fence to kms */
+ EGLSyncKHR kms_fence = NULL; /* in-fence to gpu, out-fence from kms */
+ int gpu_fence_fd, kms_fence_fd; /* just for debugging */
+
+ kms_fence_fd = drm.kms_out_fence_fd;
+
+ if (drm.kms_out_fence_fd != -1) {
+ kms_fence = create_fence(egl, drm.kms_out_fence_fd);
+
+ /* driver now has ownership of the fence fd: */
+ drm.kms_out_fence_fd = -1;
+
+ /* wait "on the gpu" (ie. this won't necessarily block, but
+ * will block the rendering until fence is signaled), until
+ * the previous pageflip completes so we don't render into
+ * the buffer that is still on screen.
+ */
+ egl->eglWaitSyncKHR(egl->display, kms_fence, 0);
+ egl->eglDestroySyncKHR(egl->display, kms_fence);
+ }
+
+ egl->draw(i++);
+
+ /* insert fence to be singled in cmdstream.. this fence will be
+ * signaled when gpu rendering done
+ */
+ gpu_fence = create_fence(egl, EGL_NO_NATIVE_FENCE_FD_ANDROID);
+
+ eglSwapBuffers(egl->display, egl->surface);
+
+ /* after swapbuffers, gpu_fence should be flushed, so safe
+ * to get fd:
+ */
+ drm.kms_in_fence_fd = egl->eglDupNativeFenceFDANDROID(egl->display, gpu_fence);
+ gpu_fence_fd = drm.kms_in_fence_fd;
+ egl->eglDestroySyncKHR(egl->display, gpu_fence);
+ assert(drm.kms_in_fence_fd != -1);
+
+ next_bo = gbm_surface_lock_front_buffer(gbm->surface);
+ fb = drm_fb_get_from_bo(next_bo);
+
+ /*
+ * Here you could also update drm plane layers if you want
+ * hw composition
+ */
+ ret = drm_atomic_commit(fb->fb_id, DRM_MODE_ATOMIC_NONBLOCK);
+ if (ret) {
+ printf("failed to commit: %s\n", strerror(errno));
+ return -1;
+ }
+
+ /* release last buffer to render on again: */
+ gbm_surface_release_buffer(gbm->surface, bo);
+ bo = next_bo;
+ }
+
+ return ret;
+}
+
+/* Pick a plane.. something that at a minimum can be connected to
+ * the chosen crtc, but prefer primary plane.
+ *
+ * Seems like there is some room for a drmModeObjectGetNamedProperty()
+ * type helper in libdrm..
+ */
+static int get_plane_id(void)
+{
+ drmModePlaneResPtr plane_resources;
+ uint32_t i, j;
+ int ret = -EINVAL;
+ int found_primary = 0;
+
+ plane_resources = drmModeGetPlaneResources(drm.fd);
+ if (!plane_resources) {
+ printf("drmModeGetPlaneResources failed: %s\n", strerror(errno));
+ return -1;
+ }
+
+ for (i = 0; (i < plane_resources->count_planes) && !found_primary; i++) {
+ uint32_t id = plane_resources->planes[i];
+ drmModePlanePtr plane = drmModeGetPlane(drm.fd, id);
+ if (!plane) {
+ printf("drmModeGetPlane(%u) failed: %s\n", id, strerror(errno));
+ continue;
+ }
+
+ if (plane->possible_crtcs & (1 << drm.crtc_index)) {
+ drmModeObjectPropertiesPtr props =
+ drmModeObjectGetProperties(drm.fd, id, DRM_MODE_OBJECT_PLANE);
+
+ /* primary or not, this plane is good enough to use: */
+ ret = id;
+
+ for (j = 0; j < props->count_props; j++) {
+ drmModePropertyPtr p =
+ drmModeGetProperty(drm.fd, props->props[j]);
+
+ if ((strcmp(p->name, "type") == 0) &&
+ (props->prop_values[j] == DRM_PLANE_TYPE_PRIMARY)) {
+ /* found our primary plane, lets use that: */
+ found_primary = 1;
+ }
+
+ drmModeFreeProperty(p);
+ }
+
+ drmModeFreeObjectProperties(props);
+ }
+
+ drmModeFreePlane(plane);
+ }
+
+ drmModeFreePlaneResources(plane_resources);
+
+ return ret;
+}
+
+const struct drm * init_drm_atomic(const char *device)
+{
+ uint32_t plane_id;
+ int i, ret;
+
+ ret = init_drm(&drm, device);
+ if (ret)
+ return NULL;
+
+ ret = drmSetClientCap(drm.fd, DRM_CLIENT_CAP_ATOMIC, 1);
+ if (ret) {
+ printf("no atomic modesetting support: %s\n", strerror(errno));
+ return NULL;
+ }
+
+ ret = get_plane_id();
+ if (!ret) {
+ printf("could not find a suitable plane\n");
+ return NULL;
+ } else {
+ plane_id = ret;
+ }
+
+ /* We only do single plane to single crtc to single connector, no
+ * fancy multi-monitor or multi-plane stuff. So just grab the
+ * plane/crtc/connector property info for one of each:
+ */
+ drm.plane = calloc(1, sizeof(*drm.plane));
+ drm.crtc = calloc(1, sizeof(*drm.crtc));
+ drm.connector = calloc(1, sizeof(*drm.connector));
+
+#define get_resource(type, Type, id) do { \
+ drm.type->type = drmModeGet##Type(drm.fd, id); \
+ if (!drm.type->type) { \
+ printf("could not get %s %i: %s\n", \
+ #type, id, strerror(errno)); \
+ return NULL; \
+ } \
+ } while (0)
+
+ get_resource(plane, Plane, plane_id);
+ get_resource(crtc, Crtc, drm.crtc_id);
+ get_resource(connector, Connector, drm.connector_id);
+
+#define get_properties(type, TYPE, id) do { \
+ int i; \
+ drm.type->props = drmModeObjectGetProperties(drm.fd, \
+ id, DRM_MODE_OBJECT_##TYPE); \
+ if (!drm.type->props) { \
+ printf("could not get %s %u properties: %s\n", \
+ #type, id, strerror(errno)); \
+ return NULL; \
+ } \
+ drm.type->props_info = calloc(drm.type->props->count_props, \
+ sizeof(drm.type->props_info)); \
+ for (i = 0; i < drm.type->props->count_props; i++) { \
+ drm.type->props_info[i] = drmModeGetProperty(drm.fd,\
+ drm.type->props->props[i]); \
+ } \
+ } while (0)
+
+
+ get_properties(plane, PLANE, plane_id);
+ get_properties(crtc, CRTC, drm.crtc_id);
+ get_properties(connector, CONNECTOR, drm.connector_id);
+
+ drm.run = atomic_run;
+
+ return &drm;
+}
diff --git a/drm.c b/drm.c
index d9cd7e2..d082846 100644
--- a/drm.c
+++ b/drm.c
@@ -194,6 +194,13 @@ int init_drm(struct drm *drm, const char *device)
drm->crtc_id = crtc_id;
}
+ for (i = 0; i < resources->count_crtcs; i++) {
+ if (resources->crtcs[i] == drm->crtc_id) {
+ drm->crtc_index = i;
+ break;
+ }
+ }
+
drmModeFreeResources(resources);
drm->connector_id = connector->connector_id;
diff --git a/drm.h b/drm.h
index 6f386be..c41683b 100644
--- a/drm.h
+++ b/drm.h
@@ -27,8 +27,35 @@
#include <xf86drm.h>
#include <xf86drmMode.h>
+struct plane {
+ drmModePlane *plane;
+ drmModeObjectProperties *props;
+ drmModePropertyRes **props_info;
+};
+
+struct crtc {
+ drmModeCrtc *crtc;
+ drmModeObjectProperties *props;
+ drmModePropertyRes **props_info;
+};
+
+struct connector {
+ drmModeConnector *connector;
+ drmModeObjectProperties *props;
+ drmModePropertyRes **props_info;
+};
+
struct drm {
int fd;
+
+ /* only used for atomic: */
+ struct plane *plane;
+ struct crtc *crtc;
+ struct connector *connector;
+ int crtc_index;
+ int kms_in_fence_fd;
+ int kms_out_fence_fd;
+
drmModeModeInfo *mode;
uint32_t crtc_id;
uint32_t connector_id;
@@ -45,5 +72,6 @@ struct drm_fb * drm_fb_get_from_bo(struct gbm_bo *bo);
int init_drm(struct drm *drm, const char *device);
const struct drm * init_drm_legacy(const char *device);
+const struct drm * init_drm_atomic(const char *device);
#endif /* _DRM_H */
diff --git a/kmscube.c b/kmscube.c
index f48a2d9..e31a61d 100644
--- a/kmscube.c
+++ b/kmscube.c
@@ -38,9 +38,10 @@ static const struct egl *egl;
static const struct gbm *gbm;
static const struct drm *drm;
-static const char *shortopts = "D:M:";
+static const char *shortopts = "AD:M:";
static const struct option longopts[] = {
+ {"atomic", no_argument, 0, 'M'},
{"device", required_argument, 0, 'D'},
{"mode", required_argument, 0, 'M'},
{0, 0, 0, 0}
@@ -48,9 +49,10 @@ static const struct option longopts[] = {
static void usage(const char *name)
{
- printf("Usage: %s [-D]\n"
+ printf("Usage: %s [-ADM]\n"
"\n"
"options:\n"
+ " -A, --atomic use atomic modesetting and fencing\n"
" -D, --device=DEVICE use the given device\n"
" -M, --mode=MODE specify mode, one of:\n"
" smooth - smooth shaded cube (default)\n"
@@ -64,10 +66,14 @@ int main(int argc, char *argv[])
{
const char *device = "/dev/dri/card0";
enum mode mode = SMOOTH;
+ int atomic = 0;
int opt, ret;
while ((opt = getopt_long_only(argc, argv, shortopts, longopts, NULL)) != -1) {
switch (opt) {
+ case 'A':
+ atomic = 1;
+ break;
case 'D':
device = optarg;
break;
@@ -92,9 +98,12 @@ int main(int argc, char *argv[])
}
}
- drm = init_drm_legacy(device);
+ if (atomic)
+ drm = init_drm_atomic(device);
+ else
+ drm = init_drm_legacy(device);
if (!drm) {
- printf("failed to initialize DRM\n");
+ printf("failed to initialize %s DRM\n", atomic ? "atomic" : "legacy");
return ret;
}