1From 1ae29ca69d05cb28e60488ba90811ff9fe5fadbf Mon Sep 17 00:00:00 2001
2From: Jeffy Chen <jeffy.chen@rock-chips.com>
3Date: Mon, 7 Mar 2022 15:56:19 +0800
4Subject: [PATCH 57/79] backend-drm: Support getting drm fb from dmabuf
5 directly
6
7Try to import dmabuf to drm fb directly when GBM fd-import not working.
8
9Signed-off-by: Jeffy Chen <jeffy.chen@rock-chips.com>
10---
11 libweston/backend-drm/fb.c | 46 ++++++++++++++++++++++++--------------
12 1 file changed, 29 insertions(+), 17 deletions(-)
13
14diff --git a/libweston/backend-drm/fb.c b/libweston/backend-drm/fb.c
15index 3790a73..aba17bf 100644
16--- a/libweston/backend-drm/fb.c
17+++ b/libweston/backend-drm/fb.c
18@@ -224,10 +224,18 @@ drm_fb_destroy_gbm(struct gbm_bo *bo, void *data)
19 static void
20 drm_fb_destroy_dmabuf(struct drm_fb *fb)
21 {
22-	/* We deliberately do not close the GEM handles here; GBM manages
23-	 * their lifetime through the BO. */
24-	if (fb->bo)
25+	if (fb->bo) {
26+		/* We deliberately do not close the GEM handles here; GBM manages
27+		 * their lifetime through the BO. */
28 		gbm_bo_destroy(fb->bo);
29+	} else {
30+		int i;
31+		for (i = 0; i < fb->num_planes; i++) {
32+			struct drm_gem_close arg = { fb->handles[i], };
33+			drmIoctl(fb->fd, DRM_IOCTL_GEM_CLOSE, &arg);
34+		}
35+	}
36+
37 	drm_fb_destroy(fb);
38 }
39
40@@ -293,13 +301,6 @@ drm_fb_get_from_dmabuf(struct linux_dmabuf_buffer *dmabuf,
41
42 	fb->bo = gbm_bo_import(backend->gbm, GBM_BO_IMPORT_FD_MODIFIER,
43 			       &import_mod, GBM_BO_USE_SCANOUT);
44-	if (!fb->bo) {
45-		if (try_view_on_plane_failure_reasons)
46-			*try_view_on_plane_failure_reasons |=
47-				FAILURE_REASONS_GBM_BO_IMPORT_FAILED;
48-		goto err_free;
49-	}
50-
51 	fb->width = dmabuf->attributes.width;
52 	fb->height = dmabuf->attributes.height;
53 	fb->modifier = dmabuf->attributes.modifier[0];
54@@ -328,13 +329,24 @@ drm_fb_get_from_dmabuf(struct linux_dmabuf_buffer *dmabuf,
55 	}
56
57 	fb->num_planes = dmabuf->attributes.n_planes;
58-	for (i = 0; i < dmabuf->attributes.n_planes; i++) {
59-		union gbm_bo_handle handle;
60-
61-	        handle = gbm_bo_get_handle_for_plane(fb->bo, i);
62-		if (handle.s32 == -1)
63-			goto err_free;
64-		fb->handles[i] = handle.u32;
65+	if (fb->bo) {
66+		for (i = 0; i < fb->num_planes; i++) {
67+			union gbm_bo_handle handle;
68+
69+			handle = gbm_bo_get_handle_for_plane(fb->bo, i);
70+			if (handle.s32 == -1)
71+				goto err_free;
72+			fb->handles[i] = handle.u32;
73+		}
74+	} else {
75+		for (i = 0; i < fb->num_planes; i++) {
76+			if (drmPrimeFDToHandle(fb->fd, import_mod.fds[i],
77+					       &fb->handles[i])) {
78+				*try_view_on_plane_failure_reasons |=
79+					FAILURE_REASONS_GBM_BO_IMPORT_FAILED;
80+				goto err_free;
81+			}
82+		}
83 	}
84
85 	if (drm_fb_addfb(backend, fb) != 0) {
86--
872.20.1
88
89