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[DMABUF_MAX_PLANES]; 15 uint32_t width; 16 uint32_t height; 17 uint32_t offset[DMABUF_MAX_PLANES]; 18 uint32_t stride[DMABUF_MAX_PLANES]; 19 uint32_t num_planes; 20 uint32_t fourcc; 21 uint64_t modifier; 22 uint32_t texture; 23 uint32_t x; 24 uint32_t y; 25 uint32_t backing_width; 26 uint32_t backing_height; 27 bool y0_top; 28 void *sync; 29 int fence_fd; 30 bool allow_fences; 31 bool draw_submitted; 32 }; 33 34 QemuDmaBuf *qemu_dmabuf_new(uint32_t width, uint32_t height, 35 const uint32_t *offset, const uint32_t *stride, 36 uint32_t x, uint32_t y, 37 uint32_t backing_width, uint32_t backing_height, 38 uint32_t fourcc, uint64_t modifier, 39 const int32_t *dmabuf_fd, uint32_t num_planes, 40 bool allow_fences, bool y0_top) { 41 QemuDmaBuf *dmabuf; 42 43 assert(num_planes > 0 && num_planes <= DMABUF_MAX_PLANES); 44 45 dmabuf = g_new0(QemuDmaBuf, 1); 46 47 dmabuf->width = width; 48 dmabuf->height = height; 49 memcpy(dmabuf->offset, offset, num_planes * sizeof(*offset)); 50 memcpy(dmabuf->stride, stride, num_planes * sizeof(*stride)); 51 dmabuf->x = x; 52 dmabuf->y = y; 53 dmabuf->backing_width = backing_width; 54 dmabuf->backing_height = backing_height; 55 dmabuf->fourcc = fourcc; 56 dmabuf->modifier = modifier; 57 memcpy(dmabuf->fd, dmabuf_fd, num_planes * sizeof(*dmabuf_fd)); 58 dmabuf->allow_fences = allow_fences; 59 dmabuf->y0_top = y0_top; 60 dmabuf->fence_fd = -1; 61 dmabuf->num_planes = num_planes; 62 63 return dmabuf; 64 } 65 66 void qemu_dmabuf_free(QemuDmaBuf *dmabuf) 67 { 68 if (dmabuf == NULL) { 69 return; 70 } 71 72 g_free(dmabuf); 73 } 74 75 const int *qemu_dmabuf_get_fds(QemuDmaBuf *dmabuf, int *nfds) 76 { 77 assert(dmabuf != NULL); 78 79 if (nfds) { 80 *nfds = ARRAY_SIZE(dmabuf->fd); 81 } 82 83 return dmabuf->fd; 84 } 85 86 void qemu_dmabuf_dup_fds(QemuDmaBuf *dmabuf, int *fds, int nfds) 87 { 88 int i; 89 90 assert(dmabuf != NULL); 91 assert(nfds >= dmabuf->num_planes); 92 93 for (i = 0; i < dmabuf->num_planes; i++) { 94 fds[i] = dmabuf->fd[i] >= 0 ? dup(dmabuf->fd[i]) : -1; 95 } 96 } 97 98 void qemu_dmabuf_close(QemuDmaBuf *dmabuf) 99 { 100 int i; 101 102 assert(dmabuf != NULL); 103 104 for (i = 0; i < dmabuf->num_planes; i++) { 105 if (dmabuf->fd[i] >= 0) { 106 close(dmabuf->fd[i]); 107 dmabuf->fd[i] = -1; 108 } 109 } 110 } 111 112 uint32_t qemu_dmabuf_get_width(QemuDmaBuf *dmabuf) 113 { 114 assert(dmabuf != NULL); 115 116 return dmabuf->width; 117 } 118 119 uint32_t qemu_dmabuf_get_height(QemuDmaBuf *dmabuf) 120 { 121 assert(dmabuf != NULL); 122 123 return dmabuf->height; 124 } 125 126 const uint32_t *qemu_dmabuf_get_offsets(QemuDmaBuf *dmabuf, int *noffsets) 127 { 128 assert(dmabuf != NULL); 129 130 if (noffsets) { 131 *noffsets = ARRAY_SIZE(dmabuf->offset); 132 } 133 134 return dmabuf->offset; 135 } 136 137 const uint32_t *qemu_dmabuf_get_strides(QemuDmaBuf *dmabuf, int *nstrides) 138 { 139 assert(dmabuf != NULL); 140 141 if (nstrides) { 142 *nstrides = ARRAY_SIZE(dmabuf->stride); 143 } 144 145 return dmabuf->stride; 146 } 147 148 uint32_t qemu_dmabuf_get_num_planes(QemuDmaBuf *dmabuf) 149 { 150 assert(dmabuf != NULL); 151 152 return dmabuf->num_planes; 153 } 154 155 uint32_t qemu_dmabuf_get_fourcc(QemuDmaBuf *dmabuf) 156 { 157 assert(dmabuf != NULL); 158 159 return dmabuf->fourcc; 160 } 161 162 uint64_t qemu_dmabuf_get_modifier(QemuDmaBuf *dmabuf) 163 { 164 assert(dmabuf != NULL); 165 166 return dmabuf->modifier; 167 } 168 169 uint32_t qemu_dmabuf_get_texture(QemuDmaBuf *dmabuf) 170 { 171 assert(dmabuf != NULL); 172 173 return dmabuf->texture; 174 } 175 176 uint32_t qemu_dmabuf_get_x(QemuDmaBuf *dmabuf) 177 { 178 assert(dmabuf != NULL); 179 180 return dmabuf->x; 181 } 182 183 uint32_t qemu_dmabuf_get_y(QemuDmaBuf *dmabuf) 184 { 185 assert(dmabuf != NULL); 186 187 return dmabuf->y; 188 } 189 190 uint32_t qemu_dmabuf_get_backing_width(QemuDmaBuf *dmabuf) 191 { 192 assert(dmabuf != NULL); 193 194 return dmabuf->backing_width; 195 } 196 197 uint32_t qemu_dmabuf_get_backing_height(QemuDmaBuf *dmabuf) 198 { 199 assert(dmabuf != NULL); 200 201 return dmabuf->backing_height; 202 } 203 204 bool qemu_dmabuf_get_y0_top(QemuDmaBuf *dmabuf) 205 { 206 assert(dmabuf != NULL); 207 208 return dmabuf->y0_top; 209 } 210 211 void *qemu_dmabuf_get_sync(QemuDmaBuf *dmabuf) 212 { 213 assert(dmabuf != NULL); 214 215 return dmabuf->sync; 216 } 217 218 int32_t qemu_dmabuf_get_fence_fd(QemuDmaBuf *dmabuf) 219 { 220 assert(dmabuf != NULL); 221 222 return dmabuf->fence_fd; 223 } 224 225 bool qemu_dmabuf_get_allow_fences(QemuDmaBuf *dmabuf) 226 { 227 assert(dmabuf != NULL); 228 229 return dmabuf->allow_fences; 230 } 231 232 bool qemu_dmabuf_get_draw_submitted(QemuDmaBuf *dmabuf) 233 { 234 assert(dmabuf != NULL); 235 236 return dmabuf->draw_submitted; 237 } 238 239 void qemu_dmabuf_set_texture(QemuDmaBuf *dmabuf, uint32_t texture) 240 { 241 assert(dmabuf != NULL); 242 dmabuf->texture = texture; 243 } 244 245 void qemu_dmabuf_set_fence_fd(QemuDmaBuf *dmabuf, int32_t fence_fd) 246 { 247 assert(dmabuf != NULL); 248 dmabuf->fence_fd = fence_fd; 249 } 250 251 void qemu_dmabuf_set_sync(QemuDmaBuf *dmabuf, void *sync) 252 { 253 assert(dmabuf != NULL); 254 dmabuf->sync = sync; 255 } 256 257 void qemu_dmabuf_set_draw_submitted(QemuDmaBuf *dmabuf, bool draw_submitted) 258 { 259 assert(dmabuf != NULL); 260 dmabuf->draw_submitted = draw_submitted; 261 } 262