hc
2023-02-13 e440ec23c5a540cdd3f7464e8779219be6fd3d95
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
From 4cb0cb798af7d558229036e90db4079f07e39a34 Mon Sep 17 00:00:00 2001
From: Jeffy Chen <jeffy.chen@rock-chips.com>
Date: Sat, 9 Oct 2021 12:33:33 +0800
Subject: [PATCH 50/74] HACK: pixman-renderer: Support passing dma fd to pixman
 
Usage:
pixman_image_set_destroy_function(image, NULL, (void *)(ptrdiff_t)dma_fd)
 
Signed-off-by: Jeffy Chen <jeffy.chen@rock-chips.com>
---
 libweston/backend-drm/drm-internal.h |  1 +
 libweston/backend-drm/drm.c          |  3 +++
 libweston/backend-drm/fb.c           | 15 +++++++++++++++
 libweston/pixman-renderer.c          |  2 ++
 libweston/pixman-renderer.h          |  4 ++++
 5 files changed, 25 insertions(+)
 
diff --git a/libweston/backend-drm/drm-internal.h b/libweston/backend-drm/drm-internal.h
index 161c1ee..a2019ce 100644
--- a/libweston/backend-drm/drm-internal.h
+++ b/libweston/backend-drm/drm-internal.h
@@ -388,6 +388,7 @@ struct drm_fb {
     uint64_t modifier;
     int width, height;
     int fd;
+    int dma_fd;
 
     uint32_t plane_mask;
 
diff --git a/libweston/backend-drm/drm.c b/libweston/backend-drm/drm.c
index 4979bf5..d9beca0 100644
--- a/libweston/backend-drm/drm.c
+++ b/libweston/backend-drm/drm.c
@@ -1634,6 +1634,9 @@ drm_output_init_pixman(struct drm_output *output, struct drm_backend *b)
                          output->dumb[i]->strides[0]);
         if (!output->image[i])
             goto err;
+
+        pixman_image_set_dma_fd(output->image[i],
+                    output->dumb[i]->dma_fd);
     }
 
     if (pixman_renderer_output_create(&output->base, &options) < 0)
diff --git a/libweston/backend-drm/fb.c b/libweston/backend-drm/fb.c
index af31a8a..748c641 100644
--- a/libweston/backend-drm/fb.c
+++ b/libweston/backend-drm/fb.c
@@ -58,6 +58,9 @@ drm_fb_destroy_dumb(struct drm_fb *fb)
 
     assert(fb->type == BUFFER_PIXMAN_DUMB);
 
+    if (fb->dma_fd >= 0)
+        close(fb->dma_fd);
+
     if (fb->map && fb->size > 0)
         munmap(fb->map, fb->size);
 
@@ -121,6 +124,7 @@ drm_fb_create_dumb(struct drm_backend *b, int width, int height,
     struct drm_mode_create_dumb create_arg;
     struct drm_mode_destroy_dumb destroy_arg;
     struct drm_mode_map_dumb map_arg;
+    struct drm_prime_handle prime_arg;
 
     fb = zalloc(sizeof *fb);
     if (!fb)
@@ -175,8 +179,19 @@ drm_fb_create_dumb(struct drm_backend *b, int width, int height,
     if (fb->map == MAP_FAILED)
         goto err_add_fb;
 
+    memset(&prime_arg, 0, sizeof(prime_arg));
+    prime_arg.fd = -1;
+    prime_arg.handle = fb->handles[0];
+    ret = drmIoctl(fb->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &prime_arg);
+    if (ret)
+        goto err_unmap_fb;
+
+    fb->dma_fd = prime_arg.fd;
+
     return fb;
 
+err_unmap_fb:
+    munmap(fb->map, fb->size);
 err_add_fb:
     drmModeRmFB(b->drm.fd, fb->fb_id);
 err_bo:
diff --git a/libweston/pixman-renderer.c b/libweston/pixman-renderer.c
index c6d1cbb..b56e9b5 100644
--- a/libweston/pixman-renderer.c
+++ b/libweston/pixman-renderer.c
@@ -821,6 +821,8 @@ pixman_renderer_attach_dmabuf(struct weston_surface *es,
                          data->ptr + attributes->offset[0],
                          attributes->stride[0]);
 
+    pixman_image_set_dma_fd(ps->image, attributes->fd[0]);
+
     ps->buffer_destroy_listener.notify =
         buffer_state_handle_buffer_destroy;
     wl_signal_add(&buffer->destroy_signal,
diff --git a/libweston/pixman-renderer.h b/libweston/pixman-renderer.h
index 2b81dde..bcd248f 100644
--- a/libweston/pixman-renderer.h
+++ b/libweston/pixman-renderer.h
@@ -29,6 +29,10 @@
 #include "backend.h"
 #include "libweston-internal.h"
 
+/* HACK: Pass dma fd to pixman through destroy data */
+#define pixman_image_set_dma_fd(image, fd) \
+    pixman_image_set_destroy_function(image, NULL, (void *)(ptrdiff_t)fd)
+
 int
 pixman_renderer_init(struct weston_compositor *ec);
 
-- 
2.20.1