Merge main: one linux-dmabuf, imported on the GPU or mapped without one

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
nandiandClaude Opus 5.5 committed 2026-11-30 16:47:03 -08:00
commit d478f58d9b
54 files changed
+1110 -376

No files matched your search

+28 -3
View File
@@ -20,6 +20,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <linux/dma-buf.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/stat.h>
@@ -835,6 +837,12 @@ static uint32_t surface_add(int32_t w, int32_t h, uint8_t *rgba, uint32_t tex, i
return sf->id;
}
/* Wait for the GPU to finish writing a dma-buf before the CPU reads it; harmless on shm files. */
static int dmabuf_sync(int fd, int start) {
struct dma_buf_sync sync = { .flags = (start ? DMA_BUF_SYNC_START : DMA_BUF_SYNC_END) | DMA_BUF_SYNC_READ };
return ioctl(fd, DMA_BUF_IOCTL_SYNC, &sync);
}
/* Copy a client buffer (stride * height bytes at `offset` of the file behind fd, little-endian
* B, G, R, A/X) into a surface. 0 on failure. */
uint32_t rs_capture(int32_t fd, int32_t offset, int32_t stride, int32_t w, int32_t h, int32_t format) {
@@ -842,16 +850,33 @@ uint32_t rs_capture(int32_t fd, int32_t offset, int32_t stride, int32_t w, int32
int64_t len = (int64_t)stride * h, end = offset + len;
struct stat st;
if (fstat(fd, &st) < 0 || st.st_size < end) return 0;
const uint8_t *p = pool_map(fd, &st);
if (!p) return 0;
/* A dma-buf is mapped afresh every time: the cache would hand a later buffer the mapping of an
* earlier one that had the same inode and size (the window then shows another app's pixels). */
int is_dmabuf = dmabuf_sync(fd, 1) == 0;
const uint8_t *p;
if (is_dmabuf) {
p = mmap(NULL, (size_t)st.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (p == MAP_FAILED) { dmabuf_sync(fd, 0); return 0; }
} else {
p = pool_map(fd, &st);
if (!p) return 0;
}
if (gpu_on) {
/* Whether ARGB pixels are all opaque is not worth reading them for: the GPU blends. */
uint32_t tex = gpu_tex_shm(p + offset, stride, w, h, format == 1);
if (is_dmabuf) { munmap((void *)p, (size_t)st.st_size); dmabuf_sync(fd, 0); }
return tex ? surface_add(w, h, NULL, tex, format == 1) : 0;
}
uint8_t *rgba = px_alloc((size_t)w * h * 4);
if (!rgba) return 0;
if (!rgba) {
if (is_dmabuf) { munmap((void *)p, (size_t)st.st_size); dmabuf_sync(fd, 0); }
return 0;
}
int opaque = swizzle_rows(p + offset, stride, rgba, w, h, format == 1);
if (is_dmabuf) {
dmabuf_sync(fd, 0);
munmap((void *)p, (size_t)st.st_size);
}
return surface_add(w, h, rgba, 0, opaque);
}