[i-g-t,1/4] lib/igt_vgem: extract vgem_create_and_import
diff mbox

Message ID 1507847444-3593-2-git-send-email-daniele.ceraolospurio@intel.com
State New
Headers show

Commit Message

Daniele Ceraolo Spurio Oct. 12, 2017, 10:30 p.m. UTC
The same code to create and import a vgem object is used in a couple of
places and a couple more are coming up in the next patches so extract
the code into a common function

Signed-off-by: Daniele Ceraolo Spurio <daniele.ceraolospurio@intel.com>
---
 lib/igt_dummyload.c | 10 ++--------
 lib/igt_vgem.c      | 35 +++++++++++++++++++++++++++++++++
 lib/igt_vgem.h      |  2 ++
 tests/prime_vgem.c  | 56 +++++++++++++----------------------------------------
 4 files changed, 52 insertions(+), 51 deletions(-)

Comments

Chris Wilson Oct. 19, 2017, 7:53 a.m. UTC | #1
Quoting Daniele Ceraolo Spurio (2017-10-12 23:30:41)
> The same code to create and import a vgem object is used in a couple of
> places and a couple more are coming up in the next patches so extract
> the code into a common function
> 
> Signed-off-by: Daniele Ceraolo Spurio <daniele.ceraolospurio@intel.com>
> ---
>  lib/igt_dummyload.c | 10 ++--------
>  lib/igt_vgem.c      | 35 +++++++++++++++++++++++++++++++++
>  lib/igt_vgem.h      |  2 ++
>  tests/prime_vgem.c  | 56 +++++++++++++----------------------------------------
>  4 files changed, 52 insertions(+), 51 deletions(-)
> 
> diff --git a/lib/igt_dummyload.c b/lib/igt_dummyload.c
> index 913cc93..03541f7 100644
> --- a/lib/igt_dummyload.c
> +++ b/lib/igt_dummyload.c
> @@ -317,23 +317,17 @@ igt_cork_t *igt_cork_new(int fd)
>  {
>         igt_cork_t *cork;
>         struct vgem_bo bo;
> -       int dmabuf;
>  
>         cork = calloc(1, sizeof(igt_cork_t));
>         igt_assert(cork);
>  
>         cork->device = drm_open_driver(DRIVER_VGEM);
>  
> -       igt_require(vgem_has_fences(cork->device));
> -
>         bo.width = bo.height = 1;
>         bo.bpp = 4;
> -       vgem_create(cork->device, &bo);
> -       cork->fence = vgem_fence_attach(cork->device, &bo, VGEM_FENCE_WRITE);
>  
> -       dmabuf = prime_handle_to_fd(cork->device, bo.handle);
> -       cork->handle = prime_fd_to_handle(fd, dmabuf);
> -       close(dmabuf);
> +       cork->handle = vgem_create_and_import(cork->device, &bo, fd,
> +                                             &cork->fence);
>  
>         return cork;
>  }
> diff --git a/lib/igt_vgem.c b/lib/igt_vgem.c
> index 7f933b2..7fc62f2 100644
> --- a/lib/igt_vgem.c
> +++ b/lib/igt_vgem.c
> @@ -66,6 +66,41 @@ void vgem_create(int fd, struct vgem_bo *bo)
>         igt_assert_eq(__vgem_create(fd, bo), 0);
>  }
>  
> +/**
> + * vgem_create_and_import:
> + * @vgem_fd: open vgem file descriptor
> + * @bo: vgem_bo struct containing width, height and bpp of the object to open
> + * @import_fd: open drm file descriptor to be used to import the vgem bo
> + * @fence: optional return variable to store a fence attached to the vgem bo
> + *
> + * This function creates a vgem bo and imports it to the provided device. If
> + * the fence parameter if provided a fence is attached to the bo and returned.
> + * The provided vgem_bo struct is updated as in vgem_create.
> + *
> + * Returns:
> + * Handle of the imported bo.
> + */
> +uint32_t vgem_create_and_import(int vgem_fd, struct vgem_bo *bo, int import_fd,
> +                               uint32_t *fence)
> +{
> +       int dmabuf;
> +       uint32_t handle;
> +
> +       vgem_create(vgem_fd, bo);
> +
> +       if (fence) {
> +               igt_require(vgem_has_fences(vgem_fd));
> +               *fence = vgem_fence_attach(vgem_fd, bo, VGEM_FENCE_WRITE);
> +       }

Leave this to the caller. Don't have overly specific functions, try to
provide tools for writing future tests.

> +       dmabuf = prime_handle_to_fd(vgem_fd, bo->handle);
> +       handle = prime_fd_to_handle(import_fd, dmabuf);
> +       igt_assert(handle);

Not your concern. Add it to prime_fd_to_handle() if you want.

> +       close(dmabuf);
> +

This is common enough to be refactored,
	import_handle = prime_link(export_fd, export_handle, import_fd);
?

> +       return handle;
> +}

Patch
diff mbox

diff --git a/lib/igt_dummyload.c b/lib/igt_dummyload.c
index 913cc93..03541f7 100644
--- a/lib/igt_dummyload.c
+++ b/lib/igt_dummyload.c
@@ -317,23 +317,17 @@  igt_cork_t *igt_cork_new(int fd)
 {
 	igt_cork_t *cork;
 	struct vgem_bo bo;
-	int dmabuf;
 
 	cork = calloc(1, sizeof(igt_cork_t));
 	igt_assert(cork);
 
 	cork->device = drm_open_driver(DRIVER_VGEM);
 
-	igt_require(vgem_has_fences(cork->device));
-
 	bo.width = bo.height = 1;
 	bo.bpp = 4;
-	vgem_create(cork->device, &bo);
-	cork->fence = vgem_fence_attach(cork->device, &bo, VGEM_FENCE_WRITE);
 
-	dmabuf = prime_handle_to_fd(cork->device, bo.handle);
-	cork->handle = prime_fd_to_handle(fd, dmabuf);
-	close(dmabuf);
+	cork->handle = vgem_create_and_import(cork->device, &bo, fd,
+					      &cork->fence);
 
 	return cork;
 }
diff --git a/lib/igt_vgem.c b/lib/igt_vgem.c
index 7f933b2..7fc62f2 100644
--- a/lib/igt_vgem.c
+++ b/lib/igt_vgem.c
@@ -66,6 +66,41 @@  void vgem_create(int fd, struct vgem_bo *bo)
 	igt_assert_eq(__vgem_create(fd, bo), 0);
 }
 
+/**
+ * vgem_create_and_import:
+ * @vgem_fd: open vgem file descriptor
+ * @bo: vgem_bo struct containing width, height and bpp of the object to open
+ * @import_fd: open drm file descriptor to be used to import the vgem bo
+ * @fence: optional return variable to store a fence attached to the vgem bo
+ *
+ * This function creates a vgem bo and imports it to the provided device. If
+ * the fence parameter if provided a fence is attached to the bo and returned.
+ * The provided vgem_bo struct is updated as in vgem_create.
+ *
+ * Returns:
+ * Handle of the imported bo.
+ */
+uint32_t vgem_create_and_import(int vgem_fd, struct vgem_bo *bo, int import_fd,
+				uint32_t *fence)
+{
+	int dmabuf;
+	uint32_t handle;
+
+	vgem_create(vgem_fd, bo);
+
+	if (fence) {
+		igt_require(vgem_has_fences(vgem_fd));
+		*fence = vgem_fence_attach(vgem_fd, bo, VGEM_FENCE_WRITE);
+	}
+
+	dmabuf = prime_handle_to_fd(vgem_fd, bo->handle);
+	handle = prime_fd_to_handle(import_fd, dmabuf);
+	igt_assert(handle);
+	close(dmabuf);
+
+	return handle;
+}
+
 void *__vgem_mmap(int fd, struct vgem_bo *bo, unsigned prot)
 {
 	struct drm_mode_map_dumb arg;
diff --git a/lib/igt_vgem.h b/lib/igt_vgem.h
index 92045f0..94b1186 100644
--- a/lib/igt_vgem.h
+++ b/lib/igt_vgem.h
@@ -36,6 +36,8 @@  struct vgem_bo {
 
 int __vgem_create(int fd, struct vgem_bo *bo);
 void vgem_create(int fd, struct vgem_bo *bo);
+uint32_t vgem_create_and_import(int vgem_fd, struct vgem_bo *bo, int import_fd,
+				uint32_t *fence);
 
 void *__vgem_mmap(int fd, struct vgem_bo *bo, unsigned prot);
 void *vgem_mmap(int fd, struct vgem_bo *bo, unsigned prot);
diff --git a/tests/prime_vgem.c b/tests/prime_vgem.c
index 0ffaee9..489e9b6 100644
--- a/tests/prime_vgem.c
+++ b/tests/prime_vgem.c
@@ -35,16 +35,12 @@  static void test_read(int vgem, int i915)
 	struct vgem_bo scratch;
 	uint32_t handle;
 	uint32_t *ptr;
-	int dmabuf, i;
+	int i;
 
 	scratch.width = 1024;
 	scratch.height = 1024;
 	scratch.bpp = 32;
-	vgem_create(vgem, &scratch);
-
-	dmabuf = prime_handle_to_fd(vgem, scratch.handle);
-	handle = prime_fd_to_handle(i915, dmabuf);
-	close(dmabuf);
+	handle = vgem_create_and_import(vgem, &scratch, i915, NULL);
 
 	ptr = vgem_mmap(vgem, &scratch, PROT_WRITE);
 	for (i = 0; i < 1024; i++)
@@ -66,7 +62,7 @@  static void test_fence_read(int i915, int vgem)
 	uint32_t handle;
 	uint32_t *ptr;
 	uint32_t fence;
-	int dmabuf, i;
+	int i;
 	int master[2], slave[2];
 
 	igt_assert(pipe(master) == 0);
@@ -75,11 +71,7 @@  static void test_fence_read(int i915, int vgem)
 	scratch.width = 1024;
 	scratch.height = 1024;
 	scratch.bpp = 32;
-	vgem_create(vgem, &scratch);
-
-	dmabuf = prime_handle_to_fd(vgem, scratch.handle);
-	handle = prime_fd_to_handle(i915, dmabuf);
-	close(dmabuf);
+	handle = vgem_create_and_import(vgem, &scratch, i915, NULL);
 
 	igt_fork(child, 1) {
 		for (i = 0; i < 1024; i++) {
@@ -121,7 +113,7 @@  static void test_fence_mmap(int i915, int vgem)
 	uint32_t handle;
 	uint32_t *ptr;
 	uint32_t fence;
-	int dmabuf, i;
+	int i;
 	int master[2], slave[2];
 
 	igt_assert(pipe(master) == 0);
@@ -130,11 +122,7 @@  static void test_fence_mmap(int i915, int vgem)
 	scratch.width = 1024;
 	scratch.height = 1024;
 	scratch.bpp = 32;
-	vgem_create(vgem, &scratch);
-
-	dmabuf = prime_handle_to_fd(vgem, scratch.handle);
-	handle = prime_fd_to_handle(i915, dmabuf);
-	close(dmabuf);
+	handle = vgem_create_and_import(vgem, &scratch, i915, NULL);
 
 	igt_fork(child, 1) {
 		ptr = gem_mmap__gtt(i915, handle, 4096*1024, PROT_READ);
@@ -176,16 +164,12 @@  static void test_write(int vgem, int i915)
 	struct vgem_bo scratch;
 	uint32_t handle;
 	uint32_t *ptr;
-	int dmabuf, i;
+	int i;
 
 	scratch.width = 1024;
 	scratch.height = 1024;
 	scratch.bpp = 32;
-	vgem_create(vgem, &scratch);
-
-	dmabuf = prime_handle_to_fd(vgem, scratch.handle);
-	handle = prime_fd_to_handle(i915, dmabuf);
-	close(dmabuf);
+	handle = vgem_create_and_import(vgem, &scratch, i915, NULL);
 
 	ptr = vgem_mmap(vgem, &scratch, PROT_READ);
 	gem_close(vgem, scratch.handle);
@@ -204,16 +188,12 @@  static void test_gtt(int vgem, int i915)
 	struct vgem_bo scratch;
 	uint32_t handle;
 	uint32_t *ptr;
-	int dmabuf, i;
+	int i;
 
 	scratch.width = 1024;
 	scratch.height = 1024;
 	scratch.bpp = 32;
-	vgem_create(vgem, &scratch);
-
-	dmabuf = prime_handle_to_fd(vgem, scratch.handle);
-	handle = prime_fd_to_handle(i915, dmabuf);
-	close(dmabuf);
+	handle = vgem_create_and_import(vgem, &scratch, i915, NULL);
 
 	ptr = gem_mmap__gtt(i915, handle, scratch.size, PROT_WRITE);
 	for (i = 0; i < 1024; i++)
@@ -241,16 +221,12 @@  static void test_gtt_interleaved(int vgem, int i915)
 	struct vgem_bo scratch;
 	uint32_t handle;
 	uint32_t *ptr, *gtt;
-	int dmabuf, i;
+	int i;
 
 	scratch.width = 1024;
 	scratch.height = 1024;
 	scratch.bpp = 32;
-	vgem_create(vgem, &scratch);
-
-	dmabuf = prime_handle_to_fd(vgem, scratch.handle);
-	handle = prime_fd_to_handle(i915, dmabuf);
-	close(dmabuf);
+	handle = vgem_create_and_import(vgem, &scratch, i915, NULL);
 
 	/* This assumes that GTT is perfectedly coherent. On certain machines,
 	 * it is possible for a direct acces to bypass the GTT indirection.
@@ -709,17 +685,11 @@  static void test_flip(int i915, int vgem, unsigned hang)
 	signal(SIGHUP, sighandler);
 
 	for (int i = 0; i < 2; i++) {
-		int fd;
-
 		bo[i].width = 1024;
 		bo[i].height = 768;
 		bo[i].bpp = 32;
-		vgem_create(vgem, &bo[i]);
 
-		fd = prime_handle_to_fd(vgem, bo[i].handle);
-		handle[i] = prime_fd_to_handle(i915, fd);
-		igt_assert(handle[i]);
-		close(fd);
+		handle[i] = vgem_create_and_import(vgem, &bo[i], i915, NULL);
 
 		do_or_die(__kms_addfb(i915, handle[i],
 				      bo[i].width, bo[i].height, bo[i].pitch,