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