blob: df7a09703fac60ebc50f348f6cd7eaba65782b8e [file] [log] [blame]
Dongwon Kim6e6ae492024-05-08 10:53:59 -07001/*
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
Dongwon Kimdb81dd62024-05-08 10:54:03 -070013struct 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
Dongwon Kim6e6ae492024-05-08 10:53:59 -070032QemuDmaBuf *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
59void qemu_dmabuf_free(QemuDmaBuf *dmabuf)
60{
61 if (dmabuf == NULL) {
62 return;
63 }
64
65 g_free(dmabuf);
66}
67
68int qemu_dmabuf_get_fd(QemuDmaBuf *dmabuf)
69{
70 assert(dmabuf != NULL);
71
72 return dmabuf->fd;
73}
74
75int 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
86void 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
96uint32_t qemu_dmabuf_get_width(QemuDmaBuf *dmabuf)
97{
98 assert(dmabuf != NULL);
99
100 return dmabuf->width;
101}
102
103uint32_t qemu_dmabuf_get_height(QemuDmaBuf *dmabuf)
104{
105 assert(dmabuf != NULL);
106
107 return dmabuf->height;
108}
109
110uint32_t qemu_dmabuf_get_stride(QemuDmaBuf *dmabuf)
111{
112 assert(dmabuf != NULL);
113
114 return dmabuf->stride;
115}
116
117uint32_t qemu_dmabuf_get_fourcc(QemuDmaBuf *dmabuf)
118{
119 assert(dmabuf != NULL);
120
121 return dmabuf->fourcc;
122}
123
124uint64_t qemu_dmabuf_get_modifier(QemuDmaBuf *dmabuf)
125{
126 assert(dmabuf != NULL);
127
128 return dmabuf->modifier;
129}
130
131uint32_t qemu_dmabuf_get_texture(QemuDmaBuf *dmabuf)
132{
133 assert(dmabuf != NULL);
134
135 return dmabuf->texture;
136}
137
138uint32_t qemu_dmabuf_get_x(QemuDmaBuf *dmabuf)
139{
140 assert(dmabuf != NULL);
141
142 return dmabuf->x;
143}
144
145uint32_t qemu_dmabuf_get_y(QemuDmaBuf *dmabuf)
146{
147 assert(dmabuf != NULL);
148
149 return dmabuf->y;
150}
151
152uint32_t qemu_dmabuf_get_backing_width(QemuDmaBuf *dmabuf)
153{
154 assert(dmabuf != NULL);
155
156 return dmabuf->backing_width;
157}
158
159uint32_t qemu_dmabuf_get_backing_height(QemuDmaBuf *dmabuf)
160{
161 assert(dmabuf != NULL);
162
163 return dmabuf->backing_height;
164}
165
166bool qemu_dmabuf_get_y0_top(QemuDmaBuf *dmabuf)
167{
168 assert(dmabuf != NULL);
169
170 return dmabuf->y0_top;
171}
172
173void *qemu_dmabuf_get_sync(QemuDmaBuf *dmabuf)
174{
175 assert(dmabuf != NULL);
176
177 return dmabuf->sync;
178}
179
180int32_t qemu_dmabuf_get_fence_fd(QemuDmaBuf *dmabuf)
181{
182 assert(dmabuf != NULL);
183
184 return dmabuf->fence_fd;
185}
186
187bool qemu_dmabuf_get_allow_fences(QemuDmaBuf *dmabuf)
188{
189 assert(dmabuf != NULL);
190
191 return dmabuf->allow_fences;
192}
193
194bool qemu_dmabuf_get_draw_submitted(QemuDmaBuf *dmabuf)
195{
196 assert(dmabuf != NULL);
197
198 return dmabuf->draw_submitted;
199}
200
201void qemu_dmabuf_set_texture(QemuDmaBuf *dmabuf, uint32_t texture)
202{
203 assert(dmabuf != NULL);
204 dmabuf->texture = texture;
205}
206
207void qemu_dmabuf_set_fence_fd(QemuDmaBuf *dmabuf, int32_t fence_fd)
208{
209 assert(dmabuf != NULL);
210 dmabuf->fence_fd = fence_fd;
211}
212
213void qemu_dmabuf_set_sync(QemuDmaBuf *dmabuf, void *sync)
214{
215 assert(dmabuf != NULL);
216 dmabuf->sync = sync;
217}
218
219void qemu_dmabuf_set_draw_submitted(QemuDmaBuf *dmabuf, bool draw_submitted)
220{
221 assert(dmabuf != NULL);
222 dmabuf->draw_submitted = draw_submitted;
223}
224
225void qemu_dmabuf_set_fd(QemuDmaBuf *dmabuf, int32_t fd)
226{
227 assert(dmabuf != NULL);
228 dmabuf->fd = fd;
229}