1 /*
2 * SPDX-License-Identifier: GPL-2.0-or-later
3 *
4 * QemuDmaBuf struct and helpers used for accessing its data
5 *
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
8 */
9
10 #include "qemu/osdep.h"
11 #include "ui/dmabuf.h"
12
13 struct QemuDmaBuf {
14 int fd;
15 uint32_t width;
16 uint32_t height;
17 uint32_t stride;
18 uint32_t fourcc;
19 uint64_t modifier;
20 uint32_t texture;
21 uint32_t x;
22 uint32_t y;
23 uint32_t backing_width;
24 uint32_t backing_height;
25 bool y0_top;
26 void *sync;
27 int fence_fd;
28 bool allow_fences;
29 bool draw_submitted;
30 };
31
qemu_dmabuf_new(uint32_t width,uint32_t height,uint32_t stride,uint32_t x,uint32_t y,uint32_t backing_width,uint32_t backing_height,uint32_t fourcc,uint64_t modifier,int32_t dmabuf_fd,bool allow_fences,bool y0_top)32 QemuDmaBuf *qemu_dmabuf_new(uint32_t width, uint32_t height,
33 uint32_t stride, uint32_t x,
34 uint32_t y, uint32_t backing_width,
35 uint32_t backing_height, uint32_t fourcc,
36 uint64_t modifier, int32_t dmabuf_fd,
37 bool allow_fences, bool y0_top) {
38 QemuDmaBuf *dmabuf;
39
40 dmabuf = g_new0(QemuDmaBuf, 1);
41
42 dmabuf->width = width;
43 dmabuf->height = height;
44 dmabuf->stride = stride;
45 dmabuf->x = x;
46 dmabuf->y = y;
47 dmabuf->backing_width = backing_width;
48 dmabuf->backing_height = backing_height;
49 dmabuf->fourcc = fourcc;
50 dmabuf->modifier = modifier;
51 dmabuf->fd = dmabuf_fd;
52 dmabuf->allow_fences = allow_fences;
53 dmabuf->y0_top = y0_top;
54 dmabuf->fence_fd = -1;
55
56 return dmabuf;
57 }
58
qemu_dmabuf_free(QemuDmaBuf * dmabuf)59 void qemu_dmabuf_free(QemuDmaBuf *dmabuf)
60 {
61 if (dmabuf == NULL) {
62 return;
63 }
64
65 g_free(dmabuf);
66 }
67
qemu_dmabuf_get_fd(QemuDmaBuf * dmabuf)68 int qemu_dmabuf_get_fd(QemuDmaBuf *dmabuf)
69 {
70 assert(dmabuf != NULL);
71
72 return dmabuf->fd;
73 }
74
qemu_dmabuf_dup_fd(QemuDmaBuf * dmabuf)75 int qemu_dmabuf_dup_fd(QemuDmaBuf *dmabuf)
76 {
77 assert(dmabuf != NULL);
78
79 if (dmabuf->fd >= 0) {
80 return dup(dmabuf->fd);
81 } else {
82 return -1;
83 }
84 }
85
qemu_dmabuf_close(QemuDmaBuf * dmabuf)86 void qemu_dmabuf_close(QemuDmaBuf *dmabuf)
87 {
88 assert(dmabuf != NULL);
89
90 if (dmabuf->fd >= 0) {
91 close(dmabuf->fd);
92 dmabuf->fd = -1;
93 }
94 }
95
qemu_dmabuf_get_width(QemuDmaBuf * dmabuf)96 uint32_t qemu_dmabuf_get_width(QemuDmaBuf *dmabuf)
97 {
98 assert(dmabuf != NULL);
99
100 return dmabuf->width;
101 }
102
qemu_dmabuf_get_height(QemuDmaBuf * dmabuf)103 uint32_t qemu_dmabuf_get_height(QemuDmaBuf *dmabuf)
104 {
105 assert(dmabuf != NULL);
106
107 return dmabuf->height;
108 }
109
qemu_dmabuf_get_stride(QemuDmaBuf * dmabuf)110 uint32_t qemu_dmabuf_get_stride(QemuDmaBuf *dmabuf)
111 {
112 assert(dmabuf != NULL);
113
114 return dmabuf->stride;
115 }
116
qemu_dmabuf_get_fourcc(QemuDmaBuf * dmabuf)117 uint32_t qemu_dmabuf_get_fourcc(QemuDmaBuf *dmabuf)
118 {
119 assert(dmabuf != NULL);
120
121 return dmabuf->fourcc;
122 }
123
qemu_dmabuf_get_modifier(QemuDmaBuf * dmabuf)124 uint64_t qemu_dmabuf_get_modifier(QemuDmaBuf *dmabuf)
125 {
126 assert(dmabuf != NULL);
127
128 return dmabuf->modifier;
129 }
130
qemu_dmabuf_get_texture(QemuDmaBuf * dmabuf)131 uint32_t qemu_dmabuf_get_texture(QemuDmaBuf *dmabuf)
132 {
133 assert(dmabuf != NULL);
134
135 return dmabuf->texture;
136 }
137
qemu_dmabuf_get_x(QemuDmaBuf * dmabuf)138 uint32_t qemu_dmabuf_get_x(QemuDmaBuf *dmabuf)
139 {
140 assert(dmabuf != NULL);
141
142 return dmabuf->x;
143 }
144
qemu_dmabuf_get_y(QemuDmaBuf * dmabuf)145 uint32_t qemu_dmabuf_get_y(QemuDmaBuf *dmabuf)
146 {
147 assert(dmabuf != NULL);
148
149 return dmabuf->y;
150 }
151
qemu_dmabuf_get_backing_width(QemuDmaBuf * dmabuf)152 uint32_t qemu_dmabuf_get_backing_width(QemuDmaBuf *dmabuf)
153 {
154 assert(dmabuf != NULL);
155
156 return dmabuf->backing_width;
157 }
158
qemu_dmabuf_get_backing_height(QemuDmaBuf * dmabuf)159 uint32_t qemu_dmabuf_get_backing_height(QemuDmaBuf *dmabuf)
160 {
161 assert(dmabuf != NULL);
162
163 return dmabuf->backing_height;
164 }
165
qemu_dmabuf_get_y0_top(QemuDmaBuf * dmabuf)166 bool qemu_dmabuf_get_y0_top(QemuDmaBuf *dmabuf)
167 {
168 assert(dmabuf != NULL);
169
170 return dmabuf->y0_top;
171 }
172
qemu_dmabuf_get_sync(QemuDmaBuf * dmabuf)173 void *qemu_dmabuf_get_sync(QemuDmaBuf *dmabuf)
174 {
175 assert(dmabuf != NULL);
176
177 return dmabuf->sync;
178 }
179
qemu_dmabuf_get_fence_fd(QemuDmaBuf * dmabuf)180 int32_t qemu_dmabuf_get_fence_fd(QemuDmaBuf *dmabuf)
181 {
182 assert(dmabuf != NULL);
183
184 return dmabuf->fence_fd;
185 }
186
qemu_dmabuf_get_allow_fences(QemuDmaBuf * dmabuf)187 bool qemu_dmabuf_get_allow_fences(QemuDmaBuf *dmabuf)
188 {
189 assert(dmabuf != NULL);
190
191 return dmabuf->allow_fences;
192 }
193
qemu_dmabuf_get_draw_submitted(QemuDmaBuf * dmabuf)194 bool qemu_dmabuf_get_draw_submitted(QemuDmaBuf *dmabuf)
195 {
196 assert(dmabuf != NULL);
197
198 return dmabuf->draw_submitted;
199 }
200
qemu_dmabuf_set_texture(QemuDmaBuf * dmabuf,uint32_t texture)201 void qemu_dmabuf_set_texture(QemuDmaBuf *dmabuf, uint32_t texture)
202 {
203 assert(dmabuf != NULL);
204 dmabuf->texture = texture;
205 }
206
qemu_dmabuf_set_fence_fd(QemuDmaBuf * dmabuf,int32_t fence_fd)207 void qemu_dmabuf_set_fence_fd(QemuDmaBuf *dmabuf, int32_t fence_fd)
208 {
209 assert(dmabuf != NULL);
210 dmabuf->fence_fd = fence_fd;
211 }
212
qemu_dmabuf_set_sync(QemuDmaBuf * dmabuf,void * sync)213 void qemu_dmabuf_set_sync(QemuDmaBuf *dmabuf, void *sync)
214 {
215 assert(dmabuf != NULL);
216 dmabuf->sync = sync;
217 }
218
qemu_dmabuf_set_draw_submitted(QemuDmaBuf * dmabuf,bool draw_submitted)219 void qemu_dmabuf_set_draw_submitted(QemuDmaBuf *dmabuf, bool draw_submitted)
220 {
221 assert(dmabuf != NULL);
222 dmabuf->draw_submitted = draw_submitted;
223 }
224
qemu_dmabuf_set_fd(QemuDmaBuf * dmabuf,int32_t fd)225 void qemu_dmabuf_set_fd(QemuDmaBuf *dmabuf, int32_t fd)
226 {
227 assert(dmabuf != NULL);
228 dmabuf->fd = fd;
229 }
230