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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
|
/*
* Copyright © 2025 Collabora, Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "config.h"
#include "client-buffer-util.h"
#include <fcntl.h>
#include <linux/dma-buf.h>
#include <linux/udmabuf.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <wayland-client.h>
#include "linux-dmabuf-unstable-v1-client-protocol.h"
#include "pixel-formats.h"
#include "shared/os-compatibility.h"
#include "shared/weston-drm-fourcc.h"
#include "shared/xalloc.h"
#define UDMABUF_PATH "/dev/udmabuf"
/* Align buffers to 256 bytes - required by e.g. AMD GPUs */
#define STRIDE_ALIGN_MASK 255
static size_t
get_aligned_stride(size_t width_bytes, bool align_for_gpu)
{
if (align_for_gpu)
return (width_bytes + STRIDE_ALIGN_MASK) & ~STRIDE_ALIGN_MASK;
else
return (width_bytes + 3) & ~3u;
}
static bool
client_buffer_util_fill_buffer_args(struct client_buffer *buf,
bool align_for_gpu)
{
buf->dmabuf_fd = -1;
switch (buf->fmt->format) {
case DRM_FORMAT_RGBX4444:
case DRM_FORMAT_RGBA4444:
case DRM_FORMAT_BGRX4444:
case DRM_FORMAT_BGRA4444:
case DRM_FORMAT_XRGB4444:
case DRM_FORMAT_ARGB4444:
case DRM_FORMAT_XBGR4444:
case DRM_FORMAT_ABGR4444:
case DRM_FORMAT_RGBX5551:
case DRM_FORMAT_RGBA5551:
case DRM_FORMAT_BGRX5551:
case DRM_FORMAT_BGRA5551:
case DRM_FORMAT_RGB565:
case DRM_FORMAT_BGR565:
case DRM_FORMAT_YUYV:
case DRM_FORMAT_YVYU:
case DRM_FORMAT_UYVY:
case DRM_FORMAT_VYUY:
buf->bytes_per_line[0] = buf->width * 2;
buf->strides[0] = get_aligned_stride(buf->bytes_per_line[0],
align_for_gpu);
buf->offsets[0] = 0;
buf->bytes = buf->strides[0] * buf->height;
break;
case DRM_FORMAT_RGB888:
case DRM_FORMAT_BGR888:
buf->bytes_per_line[0] = buf->width * 3;
buf->strides[0] = get_aligned_stride(buf->bytes_per_line[0],
align_for_gpu);
buf->offsets[0] = 0;
buf->bytes = buf->strides[0] * buf->height;
break;
case DRM_FORMAT_RGBX8888:
case DRM_FORMAT_RGBA8888:
case DRM_FORMAT_BGRX8888:
case DRM_FORMAT_BGRA8888:
case DRM_FORMAT_XRGB8888:
case DRM_FORMAT_ARGB8888:
case DRM_FORMAT_XBGR8888:
case DRM_FORMAT_ABGR8888:
case DRM_FORMAT_XRGB2101010:
case DRM_FORMAT_ARGB2101010:
case DRM_FORMAT_XBGR2101010:
case DRM_FORMAT_ABGR2101010:
case DRM_FORMAT_XYUV8888:
buf->bytes_per_line[0] = buf->width * 4;
buf->strides[0] = get_aligned_stride(buf->bytes_per_line[0],
align_for_gpu);
buf->offsets[0] = 0;
buf->bytes = buf->strides[0] * buf->height;
break;
case DRM_FORMAT_XRGB16161616:
case DRM_FORMAT_ARGB16161616:
case DRM_FORMAT_XBGR16161616:
case DRM_FORMAT_ABGR16161616:
case DRM_FORMAT_XRGB16161616F:
case DRM_FORMAT_ARGB16161616F:
case DRM_FORMAT_XBGR16161616F:
case DRM_FORMAT_ABGR16161616F:
buf->bytes_per_line[0] = buf->width * 8;
buf->strides[0] = get_aligned_stride(buf->bytes_per_line[0],
align_for_gpu);
buf->offsets[0] = 0;
buf->bytes = buf->strides[0] * buf->height;
break;
case DRM_FORMAT_NV12:
case DRM_FORMAT_NV21:
case DRM_FORMAT_NV16:
case DRM_FORMAT_NV61:
case DRM_FORMAT_NV24:
case DRM_FORMAT_NV42:
buf->bytes_per_line[0] = buf->width;
buf->bytes_per_line[1] =
buf->width / pixel_format_hsub(buf->fmt, 1) * sizeof(uint16_t);
buf->strides[0] = get_aligned_stride(buf->bytes_per_line[0],
align_for_gpu);
buf->strides[1] = get_aligned_stride(buf->bytes_per_line[1],
align_for_gpu);
buf->offsets[0] = 0;
buf->offsets[1] = buf->strides[0] * buf->height;
buf->bytes =
buf->strides[0] * buf->height +
buf->strides[1] * (buf->height / pixel_format_vsub(buf->fmt, 1));
break;
case DRM_FORMAT_P010:
case DRM_FORMAT_P012:
case DRM_FORMAT_P016:
buf->bytes_per_line[0] = buf->width * sizeof(uint16_t);
buf->bytes_per_line[1] =
buf->width / pixel_format_hsub(buf->fmt, 1) * sizeof(uint32_t);
buf->strides[0] = get_aligned_stride(buf->bytes_per_line[0],
align_for_gpu);
buf->strides[1] = get_aligned_stride(buf->bytes_per_line[1],
align_for_gpu);
buf->offsets[0] = 0;
buf->offsets[1] = buf->strides[0] * buf->height;
buf->bytes =
buf->strides[0] * buf->height +
buf->strides[1] * (buf->height / pixel_format_vsub(buf->fmt, 1));
break;
case DRM_FORMAT_YUV420:
case DRM_FORMAT_YVU420:
case DRM_FORMAT_YUV422:
case DRM_FORMAT_YVU422:
case DRM_FORMAT_YUV444:
case DRM_FORMAT_YVU444:
buf->bytes_per_line[0] = buf->width;
buf->bytes_per_line[1] = buf->bytes_per_line[2] = buf->width / pixel_format_hsub(buf->fmt, 1);
buf->strides[0] = get_aligned_stride(buf->bytes_per_line[0],
align_for_gpu);
buf->strides[1] = buf->strides[2] = get_aligned_stride(buf->bytes_per_line[1],
align_for_gpu);
buf->offsets[0] = 0;
buf->offsets[1] = buf->strides[0] * buf->height;
buf->offsets[2] = buf->offsets[1] + buf->strides[1] * (buf->height / pixel_format_vsub(buf->fmt, 1));
buf->bytes =
buf->strides[0] * buf->height +
buf->strides[1] * (buf->height / pixel_format_vsub(buf->fmt, 1)) * 2;
break;
default:
fprintf(stderr, "Format not handled\n");
return false;
}
if (align_for_gpu) {
int pagesizemask = getpagesize() - 1;
buf->bytes = (buf->bytes + pagesizemask) & ~pagesizemask;
}
return true;
}
bool
client_buffer_util_is_dmabuf_supported(void)
{
int udmabuf_fd;
udmabuf_fd = open (UDMABUF_PATH, O_RDWR | O_CLOEXEC, 0);
if (udmabuf_fd == -1)
return false;
close(udmabuf_fd);
return true;
}
void
client_buffer_util_destroy_buffer(struct client_buffer *buf)
{
if (buf->wl_buffer)
wl_buffer_destroy(buf->wl_buffer);
if (buf->data)
munmap(buf->data, buf->bytes);
if (buf->dmabuf_fd > -1)
close(buf->dmabuf_fd);
free(buf);
}
struct client_buffer *
client_buffer_util_create_shm_buffer(struct wl_shm *shm,
const struct pixel_format_info *fmt,
int width,
int height)
{
struct client_buffer *buf;
struct wl_shm_pool *pool;
int fd = -1;
buf = xzalloc(sizeof *buf);
buf->fmt = fmt;
buf->type = CLIENT_BUFFER_TYPE_SHM;
buf->width = width;
buf->height = height;
if (!client_buffer_util_fill_buffer_args(buf, false))
goto error;
fd = os_create_anonymous_file(buf->bytes);
if (fd == -1) {
fprintf(stderr, "os_create_anonymous_file() failed: %s\n",
strerror (errno));
goto error;
}
buf->data = mmap(NULL, buf->bytes,
PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (buf->data == MAP_FAILED) {
fprintf(stderr, "mmap() failed: %s\n", strerror (errno));
goto error;
}
pool = wl_shm_create_pool(shm, fd, buf->bytes);
buf->wl_buffer = wl_shm_pool_create_buffer(pool, 0, buf->width,
buf->height, buf->strides[0],
pixel_format_get_shm_format(fmt));
wl_shm_pool_destroy(pool);
close(fd);
if (!buf->wl_buffer) {
fprintf(stderr, "wl_shm_pool_create_buffer() failed\n");
goto error;
}
return buf;
error:
if (fd != -1)
close(fd);
client_buffer_util_destroy_buffer(buf);
return NULL;
}
struct buffer_create_data {
struct client_buffer *buf;
bool failed;
};
static void
create_succeeded(void *data,
struct zwp_linux_buffer_params_v1 *params,
struct wl_buffer *new_buffer)
{
struct buffer_create_data *create_data = data;
create_data->buf->wl_buffer = new_buffer;
wl_proxy_set_queue ((struct wl_proxy *) create_data->buf->wl_buffer, NULL);
zwp_linux_buffer_params_v1_destroy(params);
}
static void
create_failed(void *data, struct zwp_linux_buffer_params_v1 *params)
{
struct buffer_create_data *create_data = data;
create_data->failed = true;
zwp_linux_buffer_params_v1_destroy(params);
}
static const struct zwp_linux_buffer_params_v1_listener params_listener = {
create_succeeded,
create_failed
};
struct client_buffer *
client_buffer_util_create_dmabuf_buffer(struct wl_display *display,
struct zwp_linux_dmabuf_v1 *dmabuf,
const struct pixel_format_info *fmt,
int width,
int height)
{
struct client_buffer *buf;
struct buffer_create_data create_data = { 0 };
struct zwp_linux_buffer_params_v1 *params;
struct wl_event_queue *event_queue;
struct udmabuf_create create;
int udmabuf_fd = -1;
int mem_fd = -1;
buf = xzalloc(sizeof *buf);
buf->fmt = fmt;
buf->type = CLIENT_BUFFER_TYPE_DMABUF;
buf->width = width;
buf->height = height;
if (!client_buffer_util_fill_buffer_args(buf, true))
goto error;
udmabuf_fd = open (UDMABUF_PATH, O_RDWR | O_CLOEXEC, 0);
if (udmabuf_fd == -1) {
fprintf(stderr, "udmabuf not supported\n");
goto error;
}
mem_fd = memfd_create ("udmabuf", MFD_CLOEXEC | MFD_ALLOW_SEALING);
if (mem_fd == -1) {
fprintf(stderr, "memfd_create() failed: %s\n", strerror (errno));
goto error;
}
if (ftruncate (mem_fd, buf->bytes) < 0) {
fprintf(stderr, "ftruncate() failed: %s\n", strerror (errno));
close (mem_fd);
goto error;
}
if (fcntl (mem_fd, F_ADD_SEALS, F_SEAL_SHRINK) < 0) {
fprintf(stderr, "adding seals failed: %s\n", strerror (errno));
close (mem_fd);
goto error;
}
create.memfd = mem_fd;
create.flags = UDMABUF_FLAGS_CLOEXEC;
create.offset = 0;
create.size = buf->bytes;
buf->dmabuf_fd = ioctl (udmabuf_fd, UDMABUF_CREATE, &create);
if (buf->dmabuf_fd == -1) {
fprintf(stderr, "creating udmabuf failed: %s\n", strerror (errno));
goto error;
}
/* The underlying memfd is kept as as a reference in the kernel. */
close (mem_fd);
mem_fd = -1;
close (udmabuf_fd);
udmabuf_fd = -1;
buf->data = mmap(NULL, buf->bytes, PROT_READ | PROT_WRITE, MAP_SHARED,
buf->dmabuf_fd, 0);
if (buf->data == MAP_FAILED) {
fprintf(stderr, "mmap() failed: %s\n", strerror (errno));
goto error;
}
params = zwp_linux_dmabuf_v1_create_params(dmabuf);
event_queue = wl_display_create_queue (display);
wl_proxy_set_queue ((struct wl_proxy *) params, event_queue);
for (unsigned int i = 0; i < pixel_format_get_plane_count(buf->fmt); i++) {
zwp_linux_buffer_params_v1_add(params,
buf->dmabuf_fd,
i /* plane id */,
buf->offsets[i],
buf->strides[i],
DRM_FORMAT_MOD_LINEAR >> 32,
DRM_FORMAT_MOD_LINEAR & 0xffffffff);
}
create_data.buf = buf;
zwp_linux_buffer_params_v1_add_listener(params,
¶ms_listener,
&create_data);
zwp_linux_buffer_params_v1_create(params,
buf->width,
buf->height,
fmt->format,
0 /* flags */);
while (!buf->wl_buffer && !create_data.failed) {
if (wl_display_dispatch_queue(display, event_queue) == -1)
break;
}
wl_event_queue_destroy(event_queue);
if (!buf->wl_buffer) {
fprintf(stderr, "zwp_linux_buffer_params_v1_create() failed\n");
goto error;
}
return buf;
error:
if (udmabuf_fd != -1)
close(udmabuf_fd);
if (mem_fd != -1)
close(mem_fd);
client_buffer_util_destroy_buffer(buf);
return NULL;
}
void
client_buffer_util_maybe_sync_dmabuf_start(struct client_buffer *buf)
{
struct dma_buf_sync sync = { DMA_BUF_SYNC_START | DMA_BUF_SYNC_READ | DMA_BUF_SYNC_WRITE };
int ret;
if (buf->dmabuf_fd == -1)
return;
do {
ret = ioctl(buf->dmabuf_fd, DMA_BUF_IOCTL_SYNC, &sync);
} while (ret && (errno == EINTR || errno == EAGAIN));
}
void
client_buffer_util_maybe_sync_dmabuf_end(struct client_buffer *buf)
{
struct dma_buf_sync sync = { DMA_BUF_SYNC_END | DMA_BUF_SYNC_READ | DMA_BUF_SYNC_WRITE };
int ret;
if (buf->dmabuf_fd == -1)
return;
do {
ret = ioctl(buf->dmabuf_fd, DMA_BUF_IOCTL_SYNC, &sync);
} while (ret && (errno == EINTR || errno == EAGAIN));
}
|