[igt-dev] [PATCH i-g-t 6/6] igt/shell: Add primitive vgem wrapping

Chris Wilson chris at chris-wilson.co.uk
Mon Aug 20 13:01:49 UTC 2018


Signed-off-by: Chris Wilson <chris at chris-wilson.co.uk>
---
 shell/drm/vgem/vgem-builtin.c        | 36 +++++++++++++++++++
 shell/drm/vgem/vgem-constants.c      | 33 ++++++++++++++++++
 shell/drm/vgem/vgem-constants.h      |  6 ++++
 shell/drm/vgem/vgem-object.c         | 52 ++++++++++++++++++++++++++++
 shell/drm/vgem/vgem-object.h         |  6 ++++
 shell/examples/vgem/module-unload.js | 32 +++++++++++++++++
 shell/meson.build                    |  3 ++
 7 files changed, 168 insertions(+)
 create mode 100644 shell/drm/vgem/vgem-builtin.c
 create mode 100644 shell/drm/vgem/vgem-constants.c
 create mode 100644 shell/drm/vgem/vgem-constants.h
 create mode 100644 shell/drm/vgem/vgem-object.c
 create mode 100644 shell/drm/vgem/vgem-object.h
 create mode 100644 shell/examples/vgem/module-unload.js

diff --git a/shell/drm/vgem/vgem-builtin.c b/shell/drm/vgem/vgem-builtin.c
new file mode 100644
index 000000000..6bedfcc6c
--- /dev/null
+++ b/shell/drm/vgem/vgem-builtin.c
@@ -0,0 +1,36 @@
+#include "duktape.h"
+#include "igt-builtins.h"
+#include "drm-builtin.h"
+
+#include "vgem-constants.h"
+#include "vgem-object.h"
+
+#define MODULE_NAME "vgem"
+
+static duk_ret_t vgem_builtin_ctor(duk_context *ctx)
+{
+	duk_push_object(ctx);
+
+	duk_get_global_string(ctx, "drm");
+	duk_dup_top(ctx);
+	duk_put_prop_string(ctx, -3, "drm");
+	duk_set_prototype(ctx, -2);
+
+	duk_push_string(ctx, MODULE_NAME);
+	duk_put_prop_string(ctx, -2, "name");
+
+	vgem_constants(ctx);
+	vgem_object_setup(ctx);
+
+	return 1;
+}
+
+__attribute__((constructor))
+static void __vgem_register_builtin__(void)
+{
+	static struct drm_driver driver = {
+		.name = MODULE_NAME,
+		.ctor = vgem_builtin_ctor
+	};
+	drm_module_register_driver(&driver);
+}
diff --git a/shell/drm/vgem/vgem-constants.c b/shell/drm/vgem/vgem-constants.c
new file mode 100644
index 000000000..0776e4b59
--- /dev/null
+++ b/shell/drm/vgem/vgem-constants.c
@@ -0,0 +1,33 @@
+#include "drm-uapi/vgem_drm.h"
+#include "duktape.h"
+
+#include "vgem-constants.h"
+
+#define C(x) { #x, x }
+
+static const duk_number_list_entry vgem_ioctl[] = {
+	C(DRM_IOCTL_VGEM_FENCE_ATTACH),
+	C(DRM_IOCTL_VGEM_FENCE_SIGNAL),
+
+	{ },
+};
+
+static const duk_number_list_entry vgem_fence[] = {
+	C(DRM_VGEM_FENCE_ATTACH),
+	C(DRM_VGEM_FENCE_SIGNAL),
+
+	{},
+};
+
+void vgem_constants(duk_context *ctx)
+{
+	duk_push_object(ctx);
+	duk_put_number_list(ctx, -1, vgem_ioctl);
+	duk_put_prop_string(ctx, -2, "ioctl");
+
+	duk_push_object(ctx);
+	duk_put_number_list(ctx, -1, vgem_fence);
+	duk_put_prop_string(ctx, -2, "fence");
+}
+
+#undef C
diff --git a/shell/drm/vgem/vgem-constants.h b/shell/drm/vgem/vgem-constants.h
new file mode 100644
index 000000000..04b5b946a
--- /dev/null
+++ b/shell/drm/vgem/vgem-constants.h
@@ -0,0 +1,6 @@
+#ifndef VGEM_CONSTANTS_H
+#define VGEM_CONSTANTS_H
+
+void vgem_constants(duk_context *ctx);
+
+#endif /* VGEM_CONSTANTS_H */
diff --git a/shell/drm/vgem/vgem-object.c b/shell/drm/vgem/vgem-object.c
new file mode 100644
index 000000000..0cab2ef7f
--- /dev/null
+++ b/shell/drm/vgem/vgem-object.c
@@ -0,0 +1,52 @@
+#include <sys/ioctl.h>
+
+#include "drm-gem.h"
+#include "duktape.h"
+
+#include "vgem-object.h"
+
+#include "drm-uapi/vgem_drm.h"
+
+static duk_ret_t vgem_object_ctor(duk_context *ctx)
+{
+	struct drm_mode_create_dumb arg = {
+		.width = duk_get_uint(ctx, -3),
+		.height = duk_get_uint(ctx, -2),
+		.bpp = duk_get_uint(ctx, -1),
+	};
+	int fd;
+
+	duk_push_this(ctx);
+	duk_get_prop_string(ctx, -1, "fd");
+	fd = duk_get_int(ctx, -1);
+
+	if (ioctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &arg) < 0)
+		return -1;
+
+	duk_push_object(ctx);
+
+	duk_push_uint(ctx, arg.handle);
+	duk_put_prop_string(ctx, -2, "handle");
+
+	duk_push_uint(ctx, arg.size);
+	duk_put_prop_string(ctx, -2, "size");
+
+	duk_push_uint(ctx, arg.pitch);
+	duk_put_prop_string(ctx, -2, "pitch");
+
+	duk_push_this(ctx);
+	duk_put_prop_string(ctx, -2, "driver");
+
+	duk_push_c_function(ctx, drm_gem_object_dtor, 1);
+	duk_set_finalizer(ctx, -2);
+
+	return 1;
+}
+
+duk_ret_t vgem_object_setup(duk_context *ctx)
+{
+	duk_push_c_function(ctx, vgem_object_ctor, 1);
+	duk_put_prop_string(ctx, -2, "Object");
+
+	return 1;
+}
diff --git a/shell/drm/vgem/vgem-object.h b/shell/drm/vgem/vgem-object.h
new file mode 100644
index 000000000..7769c7460
--- /dev/null
+++ b/shell/drm/vgem/vgem-object.h
@@ -0,0 +1,6 @@
+#ifndef VGEM_OBJECT_H
+#define VGEM_OBJECT_H
+
+duk_ret_t vgem_object_setup(duk_context *ctx);
+
+#endif /* VGEM_OBJECT_H */
diff --git a/shell/examples/vgem/module-unload.js b/shell/examples/vgem/module-unload.js
new file mode 100644
index 000000000..070a38984
--- /dev/null
+++ b/shell/examples/vgem/module-unload.js
@@ -0,0 +1,32 @@
+const vgem = igt.require(drm.drivers.vgem);
+
+var device, object;
+
+device = vgem.card();
+igt.expect(function() { vgem.unload() }, -os.EAGAIN);
+device = undefined;
+vgem.unload();
+
+/*
+device = vgem.card();
+object = device.Object(64, 64, 4);
+dmabuf = object.get_prime_fd();
+device = undefined;
+
+igt.expect(function() { vgem.unload() }, -os.EAGAIN);
+dmabuf = undefined;
+vgem.unload();
+*/
+
+/*
+device = vgem.card();
+object = device.Object(64, 64, 4);
+dmabuf = object.get_prime_fd();
+device = undefined;
+map = dmabuf.mmap();
+dmabuf = undefined;
+
+igt.expect(function() { vgem.unload() }, -os.EAGAIN);
+map = undefined;
+vgem.unload();
+*/
diff --git a/shell/meson.build b/shell/meson.build
index c29950dba..ee11b0b51 100644
--- a/shell/meson.build
+++ b/shell/meson.build
@@ -8,6 +8,9 @@ executable('igt', [
 	     'drm/i915/i915-builtin.c',
 	     'drm/i915/i915-constants.c',
 	     'drm/i915/i915-object.c',
+	     'drm/vgem/vgem-builtin.c',
+	     'drm/vgem/vgem-constants.c',
+	     'drm/vgem/vgem-object.c',
 	     'igt-builtins.c',
 	     'igt-builtins.c',
 	     'igt-math.c',
-- 
2.18.0



More information about the igt-dev mailing list