diff mbox

[5/6] modetest: add atomic modeset support

Message ID 1439945924-22630-5-git-send-email-human.hwang@samsung.com (mailing list archive)
State New, archived
Headers show

Commit Message

Hyungwon Hwang Aug. 19, 2015, 12:58 a.m. UTC
This patch adds support for atomic modeset. Using -a option, user can
make modeset to use DRM_IOCTL_MODE_ATOMIC instead of legacy IOCTLs.
Also, by using -W option, user can set the value of each object's property.

Signed-off-by: Hyungwon Hwang <human.hwang@samsung.com>
---
 tests/modetest/modetest.c | 237 +++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 204 insertions(+), 33 deletions(-)
diff mbox

Patch

diff --git a/tests/modetest/modetest.c b/tests/modetest/modetest.c
index 43bd06f..bf9222d 100644
--- a/tests/modetest/modetest.c
+++ b/tests/modetest/modetest.c
@@ -1433,15 +1433,21 @@  static int parse_property(struct property_arg *p, const char *arg)
 
 static void usage(char *name)
 {
-	fprintf(stderr, "usage: %s [-cDdefMPpsCvw]\n", name);
+	fprintf(stderr, "usage: %s [-acDdefMPpsCvwW]\n", name);
+	fprintf(stderr, "\tA: supported in atomic modeset\n");
+	fprintf(stderr, "\tL: supported in legacy modeset\n");
 
-	fprintf(stderr, "\n Query options:\n\n");
+	fprintf(stderr, "\n Query options: [AL]\n\n");
 	fprintf(stderr, "\t-c\tlist connectors\n");
 	fprintf(stderr, "\t-e\tlist encoders\n");
 	fprintf(stderr, "\t-f\tlist framebuffers\n");
 	fprintf(stderr, "\t-p\tlist CRTCs and planes (pipes)\n");
 
-	fprintf(stderr, "\n Test options:\n\n");
+	fprintf(stderr, "\n Atomic Test options: [A]\n\n");
+	fprintf(stderr, "\t-a\tuse atomic modeset\n");
+	fprintf(stderr, "\t-W <obj_id>:<prop_name>:<value>\tset property\n");
+
+	fprintf(stderr, "\n Legacy test options: [L]\n\n");
 	fprintf(stderr, "\t-P <crtc_id>:<w>x<h>[+<x>+<y>][*<scale>][@<format>]\tset a plane\n");
 	fprintf(stderr, "\t-s <connector_id>[,<connector_id>][@<crtc_id>]:<mode>[-<vrefresh>][@<format>]\tset a mode\n");
 	fprintf(stderr, "\t-C\ttest hw cursor\n");
@@ -1449,9 +1455,9 @@  static void usage(char *name)
 	fprintf(stderr, "\t-w <obj_id>:<prop_name>:<value>\tset property\n");
 
 	fprintf(stderr, "\n Generic options:\n\n");
-	fprintf(stderr, "\t-d\tdrop master after mode set\n");
-	fprintf(stderr, "\t-M module\tuse the given driver\n");
-	fprintf(stderr, "\t-D device\tuse the given device\n");
+	fprintf(stderr, "\t-d\tdrop master after mode set [L]\n");
+	fprintf(stderr, "\t-M module\tuse the given driver [AL]\n");
+	fprintf(stderr, "\t-D device\tuse the given device [AL]\n");
 
 	fprintf(stderr, "\n\tDefault is to dump all info.\n");
 	exit(0);
@@ -1484,7 +1490,154 @@  static int cursor_supported(void)
 	return 1;
 }
 
-static char optstr[] = "cdD:efM:P:ps:Cvw:";
+static int parse_atomic_args(drmModeAtomicReqPtr req, const char *p)
+{
+	uint32_t object_id, property_id;
+	uint64_t value;
+
+	if (sscanf(p, "%u:%u:%llu", &object_id, &property_id, &value) != 3)
+		return -1;
+
+	drmModeAtomicAddProperty(req, object_id, property_id, value);
+
+	return 0;
+}
+
+static uint32_t get_atomic_plane_prop_id(struct resources *res, uint32_t obj_id,
+								const char *name)
+{
+	drmModePropertyRes *props_info;
+	struct plane *plane;
+	unsigned int i, j;
+
+	for (i = 0; i < res->plane_res->count_planes; i++) {
+		plane = &res->planes[i];
+		if (plane->plane->plane_id != obj_id)
+			continue;
+
+		for (j = 0; j < plane->props->count_props; j++) {
+			props_info = plane->props_info[j];
+			if (!strcmp(props_info->name, name))
+				return props_info->prop_id;
+		}
+	}
+
+	return 0;
+}
+
+static uint64_t get_atomic_plane_prop_value_in_request(drmModeAtomicReqPtr req,
+			struct resources *res, uint32_t obj_id, const char *name)
+{
+	uint32_t prop_id, i;
+	drmModeAtomicReqItemPtr item;
+
+	prop_id = get_atomic_plane_prop_id(res, obj_id, name);
+
+	for (i = 0; i < req->cursor; i++) {
+		item = &req->items[i];
+
+		if (item->object_id != obj_id)
+			continue;
+
+		if (item->property_id != prop_id)
+			continue;
+
+		return item->value;
+	}
+
+	return -1;
+}
+
+static bool is_atomic_obj_in_request(drmModeAtomicReqPtr req,
+						uint32_t obj_id)
+{
+	unsigned int i;
+
+	for (i = 0; i < req->cursor; i++)
+		if (req->items[i].object_id == obj_id)
+			return true;
+
+	return false;
+}
+
+static int allocate_fb(struct device *dev, drmModeAtomicReqPtr req, struct resources *res,
+			uint32_t width,	uint32_t height, uint32_t pixel_format,
+			int pattern, struct bo **bo, uint32_t *fb_id)
+{
+	uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
+	int ret;
+
+	*bo = bo_create(dev->fd, pixel_format, width, height,
+			handles, pitches, offsets, pattern);
+	if (*bo == NULL) {
+		fprintf(stderr, "failed to create bo (%ux%u): %s\n",
+				width, height, strerror(errno));
+		return -1;
+	}
+
+	ret = drmModeAddFB2(dev->fd, width, height, pixel_format,
+			handles, pitches, offsets, fb_id, 0);
+	if (ret) {
+		fprintf(stderr, "failed to add fb (%ux%u): %s\n",
+				width, height, strerror(errno));
+		bo_destroy(*bo);
+		return ret;
+	}
+
+	return 0;
+}
+
+static int allocate_fbs(struct device *dev, drmModeAtomicReqPtr req, struct resources *res)
+{
+	uint32_t plane_id, fb_obj_id, fb_id, i, width,	height, pixel_format;
+	struct bo *bo;
+	int ret;
+
+	for (i = 0; i < res->plane_res->count_planes; i++) {
+		plane_id = res->planes[i].plane->plane_id;
+
+		if (!is_atomic_obj_in_request(req, plane_id))
+			continue;
+
+		fb_obj_id = get_atomic_plane_prop_id(res, plane_id, "FB_ID");
+		if (!fb_obj_id)
+			return -1;
+
+		width = get_atomic_plane_prop_value_in_request(req, res,
+							plane_id, "SRC_W");
+		height = get_atomic_plane_prop_value_in_request(req, res,
+							plane_id, "SRC_H");
+		pixel_format = DRM_FORMAT_XRGB8888;
+
+		ret = allocate_fb(dev, req, res, width, height, pixel_format,
+						PATTERN_SMPTE, &bo, &fb_id);
+		if (ret < 0)
+			return ret;
+
+		dev->mode.bo = bo;
+		dev->mode.fb_id = fb_id;
+
+		ret = drmModeAtomicAddProperty(req, plane_id, fb_obj_id, fb_id);
+		if (ret < 0)
+			return ret;
+	}
+
+	return 0;
+}
+
+static void atomic_modeset(struct device *dev, drmModeAtomicReqPtr req,
+		struct resources *res)
+{
+	uint32_t flags = 0;
+
+	drmModeAtomicCleanup(req);
+
+	allocate_fbs(dev, req, res);
+
+	drmModeAtomicCommit(dev->fd, req, flags, NULL);
+}
+
+static char optstr[] = "acdD:efM:P:ps:Cvw:W:";
 
 int main(int argc, char **argv)
 {
@@ -1504,6 +1657,7 @@  int main(int argc, char **argv)
 	struct pipe_arg *pipe_args = NULL;
 	struct plane_arg *plane_args = NULL;
 	struct property_arg *prop_args = NULL;
+	drmModeAtomicReqPtr req = NULL;
 	unsigned int args = 0;
 	int ret;
 
@@ -1514,6 +1668,11 @@  int main(int argc, char **argv)
 		args++;
 
 		switch (c) {
+		case 'a':
+			if (!req)
+				req = drmModeAtomicAlloc();
+			args--;
+			break;
 		case 'c':
 			connectors = 1;
 			break;
@@ -1587,6 +1746,10 @@  int main(int argc, char **argv)
 
 			prop_count++;
 			break;
+		case 'W':
+			if (!req || parse_atomic_args(req, optarg) < 0)
+				usage(argv[0]);
+			break;
 		default:
 			usage(argv[0]);
 			break;
@@ -1635,6 +1798,9 @@  int main(int argc, char **argv)
 		return -1;
 	}
 
+	if (req)
+		drmSetClientCap(dev.fd, DRM_CLIENT_CAP_ATOMIC, 1);
+
 	dev.resources = get_resources(&dev);
 	if (!dev.resources) {
 		drmClose(dev.fd);
@@ -1649,46 +1815,51 @@  int main(int argc, char **argv)
 	dump_resource(&dev, planes);
 	dump_resource(&dev, framebuffers);
 
-	for (i = 0; i < prop_count; ++i)
-		set_property(&dev, &prop_args[i]);
+	if (req) {
+		atomic_modeset(&dev, req, dev.resources);
 
-	if (count || plane_count) {
-		uint64_t cap = 0;
+		getchar();
+	} else {
+		for (i = 0; i < prop_count; ++i)
+			set_property(&dev, &prop_args[i]);
 
-		ret = drmGetCap(dev.fd, DRM_CAP_DUMB_BUFFER, &cap);
-		if (ret || cap == 0) {
-			fprintf(stderr, "driver doesn't support the dumb buffer API\n");
-			return 1;
-		}
+		if (count || plane_count) {
+			uint64_t cap = 0;
 
-		if (count)
-			set_mode(&dev, pipe_args, count);
+			ret = drmGetCap(dev.fd, DRM_CAP_DUMB_BUFFER, &cap);
+			if (ret || cap == 0) {
+				fprintf(stderr, "driver doesn't support the dumb buffer API\n");
+				return 1;
+			}
 
-		if (plane_count)
-			set_planes(&dev, plane_args, plane_count);
+			if (count)
+				set_mode(&dev, pipe_args, count);
 
-		if (test_cursor)
-			set_cursors(&dev, pipe_args, count);
+			if (plane_count)
+				set_planes(&dev, plane_args, plane_count);
 
-		if (test_vsync)
-			test_page_flip(&dev, pipe_args, count);
+			if (test_cursor)
+				set_cursors(&dev, pipe_args, count);
 
-		if (drop_master)
-			drmDropMaster(dev.fd);
+			if (test_vsync)
+				test_page_flip(&dev, pipe_args, count);
 
-		getchar();
+			if (drop_master)
+				drmDropMaster(dev.fd);
 
-		if (test_cursor)
-			clear_cursors(&dev);
+			getchar();
 
-		if (plane_count)
-			clear_planes(&dev, plane_args, plane_count);
+			if (test_cursor)
+				clear_cursors(&dev);
 
-		if (count)
-			clear_mode(&dev);
+			if (plane_count)
+				clear_planes(&dev, plane_args, plane_count);
+		}
 	}
 
+	clear_mode(&dev);
 	free_resources(dev.resources);
+	drmFree(req);
 
 	return 0;
 }