1From cc29cd4d7e5cda2ee39456aed5fd92eca03e9cf5 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 60/92] 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 | 50 +++++++++++++++++++++++---------------
12 1 file changed, 31 insertions(+), 19 deletions(-)
13
14diff --git a/libweston/backend-drm/fb.c b/libweston/backend-drm/fb.c
15index 8c4c49c..6dc098d 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@@ -294,13 +302,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@@ -329,16 +330,27 @@ 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-			*try_view_on_plane_failure_reasons |=
64-				FAILURE_REASONS_GBM_BO_GET_HANDLE_FAILED;
65-			goto err_free;
66+	if (fb->bo) {
67+		for (i = 0; i < fb->num_planes; i++) {
68+			union gbm_bo_handle handle;
69+
70+		        handle = gbm_bo_get_handle_for_plane(fb->bo, i);
71+			if (handle.s32 == -1) {
72+				*try_view_on_plane_failure_reasons |=
73+					FAILURE_REASONS_GBM_BO_GET_HANDLE_FAILED;
74+				goto err_free;
75+			}
76+			fb->handles[i] = handle.u32;
77+		}
78+	} else {
79+		for (i = 0; i < fb->num_planes; i++) {
80+			if (drmPrimeFDToHandle(fb->fd, import_mod.fds[i],
81+					       &fb->handles[i])) {
82+				*try_view_on_plane_failure_reasons |=
83+					FAILURE_REASONS_GBM_BO_IMPORT_FAILED;
84+				goto err_free;
85+			}
86 		}
87-		fb->handles[i] = handle.u32;
88 	}
89
90 	if (drm_fb_addfb(device, fb) != 0) {
91--
922.20.1
93
94