mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-20 21:35:59 +00:00
Merge remote-tracking branch 'droiddeck/main' into feat/malibridge
# Conflicts: # README.md # app/src/main/java/com/droiddeck/launcher/gpu/TurnipDriver.java # app/src/main/java/com/droiddeck/launcher/runtime/BwrapSpawner.kt # app/src/main/res/values-ar/strings.xml # app/src/main/res/values-b+es+419/strings.xml # app/src/main/res/values-bg/strings.xml # app/src/main/res/values-cs/strings.xml # app/src/main/res/values-da/strings.xml # app/src/main/res/values-de/strings.xml # app/src/main/res/values-el/strings.xml # app/src/main/res/values-es/strings.xml # app/src/main/res/values-fi/strings.xml # app/src/main/res/values-fr/strings.xml # app/src/main/res/values-hi/strings.xml # app/src/main/res/values-hu/strings.xml # app/src/main/res/values-in/strings.xml # app/src/main/res/values-it/strings.xml # app/src/main/res/values-ja/strings.xml # app/src/main/res/values-ko/strings.xml # app/src/main/res/values-ms/strings.xml # app/src/main/res/values-nb/strings.xml # app/src/main/res/values-nl/strings.xml # app/src/main/res/values-pl/strings.xml # app/src/main/res/values-pt-rPT/strings.xml # app/src/main/res/values-pt/strings.xml # app/src/main/res/values-ro/strings.xml # app/src/main/res/values-ru/strings.xml # app/src/main/res/values-sv/strings.xml # app/src/main/res/values-th/strings.xml # app/src/main/res/values-tr/strings.xml # app/src/main/res/values-uk/strings.xml # app/src/main/res/values-vi/strings.xml # app/src/main/res/values-zh-rCN/strings.xml # app/src/main/res/values-zh-rHK/strings.xml # app/src/main/res/values-zh-rTW/strings.xml # app/src/main/res/values/strings.xml # tools/linuxfs/overlay/usr/local/bin/droiddeck-game-env
This commit is contained in:
commit
0a5955d6fa
731 files changed
+54131
-2491
No files matched your search
@@ -407,6 +407,7 @@ int bl_kgsl_poll(int (*real)(int, unsigned long, void *), int fd, unsigned long
|
||||
int bl_ntsync_ioctl(int fd, unsigned long request, void *arg, int *rc) __attribute__((visibility("hidden")));
|
||||
int bl_drm_gem_close(int fd, unsigned long request, void *arg, int *rc) __attribute__((visibility("hidden")));
|
||||
int bl_venc_ioctl(int fd, unsigned long request, void *arg, int *rc) __attribute__((visibility("hidden")));
|
||||
int bl_vdec_ioctl(int fd, unsigned long request, void *arg, int *rc) __attribute__((visibility("hidden")));
|
||||
int bl_winebus_rumble_ioctl(int (*real)(int, unsigned long, void *), int fd, unsigned long request,
|
||||
void *arg, void *caller, int *rc) __attribute__((visibility("hidden")));
|
||||
int bl_writewatch_ioctl(int (*real)(int, unsigned long, void *), int fd, unsigned long request, void *arg,
|
||||
@@ -423,6 +424,7 @@ int ioctl(int fd, unsigned long request, ...) {
|
||||
int rc;
|
||||
if (bl_ntsync_ioctl(fd, request, arg, &rc)) return rc;
|
||||
if (bl_venc_ioctl(fd, request, arg, &rc)) return rc;
|
||||
if (bl_vdec_ioctl(fd, request, arg, &rc)) return rc;
|
||||
if (bl_drm_gem_close(fd, request, arg, &rc)) return rc;
|
||||
if (real == NULL) real = (ioctl_fn) dlsym(RTLD_NEXT, "ioctl");
|
||||
if (bl_winebus_rumble_ioctl(real, fd, request, arg, __builtin_return_address(0), &rc)) return rc;
|
||||
|
||||
@@ -1459,10 +1459,12 @@ ssize_t recvmsg(int fd, struct msghdr *msg, int flags) {
|
||||
|
||||
void bl_fsync_fds_closed(unsigned int first, unsigned int last) __attribute__((visibility("hidden")));
|
||||
void bl_venc_closed(int fd) __attribute__((visibility("hidden")));
|
||||
void bl_vdec_closed(int fd) __attribute__((visibility("hidden")));
|
||||
|
||||
int close(int fd) {
|
||||
if (fd >= 0) bl_fsync_fds_closed((unsigned int)fd, (unsigned int)fd);
|
||||
bl_venc_closed(fd);
|
||||
bl_vdec_closed(fd);
|
||||
uint64_t *t = __atomic_load_n(&ns_fdt, __ATOMIC_ACQUIRE);
|
||||
if (t && fd >= 0 && fd < NS_FDS && __atomic_load_n(&t[fd], __ATOMIC_ACQUIRE)) ns_forget(fd);
|
||||
return ns_close_fd(fd);
|
||||
|
||||
@@ -15,6 +15,7 @@ int bl_status_without_tracer(const char *path, int flags, const void *caller) __
|
||||
int bl_writable_retry(int dirfd, const char *path, int flags) __attribute__((visibility("hidden")));
|
||||
int bl_ntsync_open(const char *path, int flags) __attribute__((visibility("hidden")));
|
||||
int bl_venc_open(const char *path, int flags) __attribute__((visibility("hidden")));
|
||||
int bl_vdec_open(const char *path, int flags) __attribute__((visibility("hidden")));
|
||||
int bl_dircache_create(int dirfd, const char *path, int flags, mode_t mode,
|
||||
int (*real)(int, const char *, int, mode_t)) __attribute__((visibility("hidden")));
|
||||
|
||||
@@ -55,6 +56,7 @@ int open(const char *path, int flags, ...) {
|
||||
if ((fd = bl_status_without_tracer(path, flags, __builtin_return_address(0))) >= 0) return fd;
|
||||
if ((fd = bl_ntsync_open(path, flags)) != -2) return fd;
|
||||
if ((fd = bl_venc_open(path, flags)) != -2) return fd;
|
||||
if ((fd = bl_vdec_open(path, flags)) != -2) return fd;
|
||||
if (real_open == NULL) real_open = dlsym(RTLD_NEXT, "open");
|
||||
if (real_open == NULL) {
|
||||
errno = ENOSYS;
|
||||
@@ -81,6 +83,7 @@ int openat(int dirfd, const char *path, int flags, ...) {
|
||||
if ((fd = bl_status_without_tracer(path, flags, __builtin_return_address(0))) >= 0) return fd;
|
||||
if ((fd = bl_ntsync_open(path, flags)) != -2) return fd;
|
||||
if ((fd = bl_venc_open(path, flags)) != -2) return fd;
|
||||
if ((fd = bl_vdec_open(path, flags)) != -2) return fd;
|
||||
if (real_openat == NULL) real_openat = dlsym(RTLD_NEXT, "openat");
|
||||
if (real_openat == NULL) {
|
||||
errno = ENOSYS;
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
#define _GNU_SOURCE
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/statfs.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define FUSE_MAGIC 0x65735546
|
||||
#define SDCARDFS_MAGIC 0x5dca2df5
|
||||
|
||||
static int is_wineserver(void) {
|
||||
static int cached = -1;
|
||||
char exe[PATH_MAX];
|
||||
const char *base;
|
||||
ssize_t n;
|
||||
|
||||
if (cached >= 0) return cached;
|
||||
n = readlink("/proc/self/exe", exe, sizeof(exe) - 1);
|
||||
if (n <= 0) {
|
||||
cached = 0;
|
||||
return cached;
|
||||
}
|
||||
exe[n] = '\0';
|
||||
base = strrchr(exe, '/');
|
||||
cached = strcmp(base ? base + 1 : exe, "wineserver") == 0;
|
||||
return cached;
|
||||
}
|
||||
|
||||
__attribute__((visibility("hidden")))
|
||||
void bl_storage_owner(int fd, struct stat *st) {
|
||||
struct statfs fs;
|
||||
int saved = errno;
|
||||
uid_t uid;
|
||||
|
||||
if (st != NULL && is_wineserver()) {
|
||||
uid = getuid();
|
||||
if (st->st_uid != uid && fstatfs(fd, &fs) == 0 && (fs.f_type == FUSE_MAGIC || fs.f_type == SDCARDFS_MAGIC)) st->st_uid = uid;
|
||||
}
|
||||
errno = saved;
|
||||
}
|
||||
@@ -139,17 +139,31 @@ static int (*next_faccessat(void))(int, const char *, int, int) {
|
||||
}
|
||||
|
||||
__attribute__((visibility("hidden"))) int bl_binfmt_runnable(const char *path, int mode);
|
||||
__attribute__((visibility("hidden"))) int bl_writable_access(int dirfd, const char *path, int mode, int flags);
|
||||
|
||||
static int writable_as_owner(int (*real)(int, const char *, int, int), int dirfd, const char *path, int mode, int flags) {
|
||||
if (!bl_writable_access(dirfd, path, mode, flags)) return -1;
|
||||
if ((mode & ~W_OK) == 0 || real(dirfd, path, mode & ~W_OK, flags) == 0) return 0;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int check_cwd(const char *path, int mode, int flags) {
|
||||
int (*real)(int, const char *, int, int) = next_faccessat();
|
||||
int result;
|
||||
int err;
|
||||
|
||||
if (real == NULL) {
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
result = real(AT_FDCWD, path, mode, flags);
|
||||
return result != 0 && errno == EACCES && bl_binfmt_runnable(path, mode) ? 0 : result;
|
||||
if (result == 0) return 0;
|
||||
err = errno;
|
||||
if (err == EACCES && bl_binfmt_runnable(path, mode)) return 0;
|
||||
errno = err;
|
||||
if (writable_as_owner(real, AT_FDCWD, path, mode, flags) == 0) return 0;
|
||||
errno = err;
|
||||
return result;
|
||||
}
|
||||
|
||||
int access(const char *path, int mode) {
|
||||
@@ -166,6 +180,7 @@ int eaccess(const char *path, int mode) {
|
||||
|
||||
int faccessat(int dirfd, const char *path, int mode, int flags) {
|
||||
int (*real)(int, const char *, int, int);
|
||||
int err;
|
||||
|
||||
if (dirfd == AT_FDCWD) {
|
||||
return cached_access(path, mode, flags, check_cwd);
|
||||
@@ -175,5 +190,9 @@ int faccessat(int dirfd, const char *path, int mode, int flags) {
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
return real(dirfd, path, mode, flags);
|
||||
if (real(dirfd, path, mode, flags) == 0) return 0;
|
||||
err = errno;
|
||||
if (writable_as_owner(real, dirfd, path, mode, flags) == 0) return 0;
|
||||
errno = err;
|
||||
return -1;
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <fcntl.h>
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
@@ -23,6 +24,34 @@ static int is_steam_client(void) {
|
||||
return cached;
|
||||
}
|
||||
|
||||
static int wine_name(const char *path) {
|
||||
const char *base;
|
||||
size_t n;
|
||||
|
||||
if (path == NULL) return 0;
|
||||
base = strrchr(path, '/');
|
||||
base = base ? base + 1 : path;
|
||||
n = strlen(path);
|
||||
return strncmp(base, "wine", 4) == 0 || strstr(path, "/lib/wine/") != NULL
|
||||
|| (n > 4 && strcasecmp(path + n - 4, ".exe") == 0);
|
||||
}
|
||||
|
||||
static int is_wine(void) {
|
||||
static int cached = -1;
|
||||
char exe[PATH_MAX];
|
||||
ssize_t n;
|
||||
|
||||
if (cached >= 0) return cached;
|
||||
n = readlink("/proc/self/exe", exe, sizeof(exe) - 1);
|
||||
if (n > 0) exe[n] = '\0';
|
||||
cached = (n > 0 && wine_name(exe)) || wine_name(program_invocation_name);
|
||||
return cached;
|
||||
}
|
||||
|
||||
static int acts_as_root(void) {
|
||||
return is_steam_client() || !is_wine();
|
||||
}
|
||||
|
||||
static int grant_owner_write(int dirfd, const char *path) {
|
||||
struct stat st;
|
||||
|
||||
@@ -37,7 +66,7 @@ int bl_writable_retry(int dirfd, const char *path, int flags) {
|
||||
char parent[PATH_MAX];
|
||||
const char *slash;
|
||||
|
||||
if (saved != EACCES || path == NULL || (flags & O_ACCMODE) == O_RDONLY || !is_steam_client()) return 0;
|
||||
if (saved != EACCES || path == NULL || (flags & O_ACCMODE) == O_RDONLY || !acts_as_root()) return 0;
|
||||
if (faccessat(dirfd, path, F_OK, 0) == 0) {
|
||||
granted = grant_owner_write(dirfd, path);
|
||||
} else if (flags & O_CREAT) {
|
||||
@@ -53,3 +82,15 @@ int bl_writable_retry(int dirfd, const char *path, int flags) {
|
||||
errno = saved;
|
||||
return granted;
|
||||
}
|
||||
|
||||
__attribute__((visibility("hidden")))
|
||||
int bl_writable_access(int dirfd, const char *path, int mode, int flags) {
|
||||
int saved = errno;
|
||||
struct stat st;
|
||||
int owned;
|
||||
|
||||
if (saved != EACCES || path == NULL || !(mode & W_OK) || !acts_as_root()) return 0;
|
||||
owned = fstatat(dirfd, path, &st, flags & AT_SYMLINK_NOFOLLOW) == 0 && st.st_uid == geteuid();
|
||||
errno = saved;
|
||||
return owned;
|
||||
}
|
||||
@@ -49,6 +49,7 @@ static int openat2_missing;
|
||||
static int (*next_fstatat)(int, const char *, bl_stat *, int);
|
||||
static int (*next_openat)(int, const char *, int, ...);
|
||||
static ssize_t (*next_readlinkat)(int, const char *, char *, size_t);
|
||||
static int (*next_statx)(int, const char *, int, unsigned int, struct statx *);
|
||||
|
||||
static int find_fastpath(struct dl_phdr_info *info, size_t size, void *name) {
|
||||
(void)size;
|
||||
@@ -68,6 +69,7 @@ __attribute__((constructor)) static void find_fastpath_calls(void) {
|
||||
next_fstatat = (int (*)(int, const char *, bl_stat *, int))dlsym(fastpath, BL_FSTATAT_NAME);
|
||||
next_openat = (int (*)(int, const char *, int, ...))dlsym(fastpath, "openat");
|
||||
next_readlinkat = (ssize_t (*)(int, const char *, char *, size_t))dlsym(fastpath, "readlinkat");
|
||||
next_statx = (int (*)(int, const char *, int, unsigned int, struct statx *))dlsym(fastpath, "statx");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,6 +94,13 @@ static long path_readlink(int dirfd, const char *path, char *buf, size_t size) {
|
||||
return next_readlinkat(dirfd, path, buf, size);
|
||||
}
|
||||
|
||||
static long path_statx(int dirfd, const char *path, int flags, unsigned int mask, struct statx *out) {
|
||||
if (!next_statx) {
|
||||
return real_syscall(SYS_statx, (long)dirfd, (long)path, (long)flags, (long)mask, (long)out);
|
||||
}
|
||||
return next_statx(dirfd, path, flags, mask, out);
|
||||
}
|
||||
|
||||
void *dlopen(const char *filename, int flags) {
|
||||
if (!real_dlopen) {
|
||||
real_dlopen = (void *(*)(const char *, int))dlsym(RTLD_NEXT, "dlopen");
|
||||
@@ -329,6 +338,26 @@ static int execveat_is_execve(const long *args) {
|
||||
return path[0] == '/' || (int)args[0] == AT_FDCWD;
|
||||
}
|
||||
|
||||
static long openat2_call(const long *args) {
|
||||
if (!real_syscall) {
|
||||
real_syscall = (long (*)(long, ...)) dlsym(RTLD_NEXT, "syscall");
|
||||
}
|
||||
if (!__atomic_load_n(&openat2_missing, __ATOMIC_RELAXED)) {
|
||||
long fd = real_syscall(SYS_openat2, args[0], args[1], args[2], args[3]);
|
||||
if (fd != -1 || errno != ENOSYS) {
|
||||
return fd;
|
||||
}
|
||||
__atomic_store_n(&openat2_missing, 1, __ATOMIC_RELAXED);
|
||||
}
|
||||
return openat2_via_openat(args);
|
||||
}
|
||||
|
||||
int bl_openat2(int dirfd, const char *path, void *how, size_t size) __asm__("openat2");
|
||||
int bl_openat2(int dirfd, const char *path, void *how, size_t size) {
|
||||
long args[6] = {dirfd, (long)path, (long)how, (long)size, 0, 0};
|
||||
return (int)openat2_call(args);
|
||||
}
|
||||
|
||||
int bl_writewatch_syscall(long number, const long *args, long (*real)(long, ...), long *rc) __attribute__((visibility("hidden")));
|
||||
|
||||
long syscall(long number, ...) {
|
||||
@@ -360,18 +389,20 @@ long syscall(long number, ...) {
|
||||
return handled;
|
||||
}
|
||||
if (number == SYS_openat2) {
|
||||
if (!__atomic_load_n(&openat2_missing, __ATOMIC_RELAXED)) {
|
||||
long fd = real_syscall(number, args[0], args[1], args[2], args[3]);
|
||||
if (fd != -1 || errno != ENOSYS) {
|
||||
return fd;
|
||||
}
|
||||
__atomic_store_n(&openat2_missing, 1, __ATOMIC_RELAXED);
|
||||
}
|
||||
return openat2_via_openat(args);
|
||||
return openat2_call(args);
|
||||
}
|
||||
if (number == SYS_openat) {
|
||||
return path_open((int)args[0], (const char *)args[1], (int)args[2], args[3]);
|
||||
}
|
||||
if (number == BL_SYS_FSTATAT) {
|
||||
return path_stat((int)args[0], (const char *)args[1], (bl_stat *)args[2], (int)args[3]);
|
||||
}
|
||||
if (number == SYS_readlinkat) {
|
||||
return path_readlink((int)args[0], (const char *)args[1], (char *)args[2], (size_t)args[3]);
|
||||
}
|
||||
if (number == SYS_statx) {
|
||||
return path_statx((int)args[0], (const char *)args[1], (int)args[2], (unsigned int)args[3], (struct statx *)args[4]);
|
||||
}
|
||||
if (number == SYS_execveat && execveat_is_execve(args)) {
|
||||
return real_syscall(SYS_execve, args[1], args[2], args[3]);
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include <unistd.h>
|
||||
|
||||
static int (*next_fstat)(int, struct stat *);
|
||||
void bl_storage_owner(int fd, struct stat *st) __attribute__((visibility("hidden")));
|
||||
static int (*next_fallocate)(int, int, off_t, off_t);
|
||||
static int (*next_posix_fallocate)(int, off_t, off_t);
|
||||
static ssize_t (*next_pwrite)(int, const void *, size_t, off_t);
|
||||
@@ -285,12 +286,17 @@ int fstat(int fd, struct stat *out) {
|
||||
if (next_fstat == NULL) { errno = ENOSYS; return -1; }
|
||||
initialize();
|
||||
errno = entry_errno;
|
||||
if (!enabled) return next_fstat(fd, out);
|
||||
if (!enabled) {
|
||||
int result = next_fstat(fd, out);
|
||||
if (result == 0) bl_storage_owner(fd, out);
|
||||
return result;
|
||||
}
|
||||
uint64_t start = now_ns();
|
||||
errno = entry_errno;
|
||||
int result = next_fstat(fd, out);
|
||||
int call_errno = errno;
|
||||
uint64_t elapsed = now_ns() - start;
|
||||
if (result == 0) bl_storage_owner(fd, out);
|
||||
if (result == 0 && on_selected_device(fd, out)) {
|
||||
add(&stats.fstat_calls, 1);
|
||||
add(&stats.fstat_elapsed_ns, elapsed);
|
||||
|
||||
@@ -0,0 +1,978 @@
|
||||
#define _GNU_SOURCE
|
||||
#include <dlfcn.h>
|
||||
#include <errno.h>
|
||||
#include <poll.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#if defined(__aarch64__) || defined(BL_VDEC_ANY_ARCH)
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <linux/videodev2.h>
|
||||
#include <pthread.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/stat.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "../../../app/src/main/cpp/venc/venc_proto.h"
|
||||
|
||||
#define VDEC_DEVICE "/dev/video-dec0"
|
||||
#define VDEC_INSTANCES 4
|
||||
#define VDEC_BUFFERS 16
|
||||
#define VDEC_BITSTREAM_SIZE (4u << 20)
|
||||
#define VDEC_PADDING 64
|
||||
#define VDEC_DUMB 64
|
||||
|
||||
struct vd_dma_heap_alloc {
|
||||
uint64_t len;
|
||||
uint32_t fd;
|
||||
uint32_t fd_flags;
|
||||
uint64_t heap_flags;
|
||||
};
|
||||
#define VD_DMA_HEAP_IOCTL_ALLOC _IOWR('H', 0x0, struct vd_dma_heap_alloc)
|
||||
|
||||
struct vd_dma_buf_sync {
|
||||
uint64_t flags;
|
||||
};
|
||||
#define VD_DMA_BUF_SYNC_WRITE 2u
|
||||
#define VD_DMA_BUF_SYNC_START 0u
|
||||
#define VD_DMA_BUF_SYNC_END 4u
|
||||
#define VD_DMA_BUF_IOCTL_SYNC _IOW('b', 0, struct vd_dma_buf_sync)
|
||||
|
||||
struct vd_create_dumb {
|
||||
uint32_t height, width, bpp, flags, handle, pitch;
|
||||
uint64_t size;
|
||||
};
|
||||
struct vd_destroy_dumb {
|
||||
uint32_t handle;
|
||||
};
|
||||
struct vd_prime_handle {
|
||||
uint32_t handle, flags;
|
||||
int32_t fd;
|
||||
};
|
||||
#define VD_IOCTL_MODE_CREATE_DUMB _IOWR('d', 0xB2, struct vd_create_dumb)
|
||||
#define VD_IOCTL_MODE_DESTROY_DUMB _IOWR('d', 0xB4, struct vd_destroy_dumb)
|
||||
#define VD_IOCTL_PRIME_HANDLE_TO_FD _IOWR('d', 0x2d, struct vd_prime_handle)
|
||||
|
||||
struct vd_mode_info {
|
||||
uint32_t clock;
|
||||
uint16_t hdisplay, hsync_start, hsync_end, htotal, hskew;
|
||||
uint16_t vdisplay, vsync_start, vsync_end, vtotal, vscan;
|
||||
uint32_t vrefresh, flags, type;
|
||||
char name[32];
|
||||
};
|
||||
struct vd_res {
|
||||
int count_fbs;
|
||||
uint32_t *fbs;
|
||||
int count_crtcs;
|
||||
uint32_t *crtcs;
|
||||
int count_connectors;
|
||||
uint32_t *connectors;
|
||||
int count_encoders;
|
||||
uint32_t *encoders;
|
||||
uint32_t min_width, max_width, min_height, max_height;
|
||||
};
|
||||
struct vd_connector {
|
||||
uint32_t connector_id, encoder_id, connector_type, connector_type_id;
|
||||
int connection;
|
||||
uint32_t mm_width, mm_height;
|
||||
int subpixel;
|
||||
int count_modes;
|
||||
struct vd_mode_info *modes;
|
||||
int count_props;
|
||||
uint32_t *props;
|
||||
uint64_t *prop_values;
|
||||
int count_encoders;
|
||||
uint32_t *encoders;
|
||||
};
|
||||
struct vd_encoder {
|
||||
uint32_t encoder_id, encoder_type, crtc_id, possible_crtcs, possible_clones;
|
||||
};
|
||||
struct vd_crtc {
|
||||
uint32_t crtc_id, buffer_id, x, y, width, height;
|
||||
int mode_valid;
|
||||
struct vd_mode_info mode;
|
||||
int gamma_size;
|
||||
};
|
||||
struct vd_device {
|
||||
char **nodes;
|
||||
int available_nodes;
|
||||
int bustype;
|
||||
void *businfo;
|
||||
void *deviceinfo;
|
||||
};
|
||||
|
||||
struct vd_capture {
|
||||
int fd;
|
||||
ino_t ino;
|
||||
uint8_t *map;
|
||||
size_t len;
|
||||
};
|
||||
|
||||
struct vd_done {
|
||||
uint32_t index;
|
||||
struct timeval timestamp;
|
||||
};
|
||||
|
||||
struct vdec {
|
||||
int used, fd;
|
||||
uint32_t fourcc, width, height;
|
||||
uint32_t cap_width, cap_height, bpl, cap_size;
|
||||
uint8_t *bitstream;
|
||||
uint32_t out_count;
|
||||
uint32_t out_done[VDEC_BUFFERS], n_out_done;
|
||||
struct vd_capture cap[VDEC_BUFFERS];
|
||||
uint32_t cap_count;
|
||||
uint32_t cap_queue[VDEC_BUFFERS], n_cap_queue;
|
||||
struct vd_done cap_done[VDEC_BUFFERS];
|
||||
uint32_t n_cap_done;
|
||||
int out_on, cap_on;
|
||||
int sock, hw, hw_failed;
|
||||
int64_t pts;
|
||||
void *ctx, *packet, *frame;
|
||||
uint8_t *packet_data;
|
||||
size_t packet_capacity;
|
||||
int last;
|
||||
uint32_t frames, slow;
|
||||
double decode_ms;
|
||||
};
|
||||
|
||||
static struct {
|
||||
void *(*find_decoder_by_name)(const char *);
|
||||
void *(*alloc_context3)(const void *);
|
||||
int (*open2)(void *, const void *, void **);
|
||||
void (*free_context)(void **);
|
||||
int (*send_packet)(void *, const void *);
|
||||
int (*receive_frame)(void *, void *);
|
||||
void (*flush_buffers)(void *);
|
||||
void *(*packet_alloc)(void);
|
||||
void (*packet_free)(void **);
|
||||
void *(*frame_alloc)(void);
|
||||
void (*frame_free)(void **);
|
||||
void (*frame_unref)(void *);
|
||||
int (*dict_set)(void **, const char *, const char *, int);
|
||||
void (*dict_free)(void **);
|
||||
const char *(*pix_fmt_name)(int);
|
||||
} av;
|
||||
|
||||
static pthread_mutex_t vd_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
static struct vdec vd_all[VDEC_INSTANCES];
|
||||
static int vd_open_count;
|
||||
static int vd_state;
|
||||
static int vd_ffmpeg;
|
||||
static int vd_heap = -1;
|
||||
static int vd_dumb_fd[VDEC_DUMB];
|
||||
static char vd_drm_handle;
|
||||
|
||||
static void vd_log(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
|
||||
static void vd_log(const char *fmt, ...) {
|
||||
va_list ap;
|
||||
char line[256];
|
||||
va_start(ap, fmt);
|
||||
int n = vsnprintf(line, sizeof line - 1, fmt, ap);
|
||||
va_end(ap);
|
||||
if (n < 0) return;
|
||||
if (n > (int)sizeof line - 2) n = sizeof line - 2;
|
||||
line[n++] = '\n';
|
||||
if (write(2, "droiddeck-vdec: ", 16) < 0 || write(2, line, n) < 0) return;
|
||||
}
|
||||
|
||||
static int vd_load_ffmpeg(void) {
|
||||
void *codec = dlopen("libavcodec.so.62", RTLD_LAZY | RTLD_LOCAL);
|
||||
void *util = codec ? dlopen("libavutil.so.60", RTLD_LAZY | RTLD_LOCAL) : NULL;
|
||||
if (codec == NULL || util == NULL) {
|
||||
vd_log("FFmpeg 8 is not available (%s), Remote Play decoding stays off", dlerror());
|
||||
return 0;
|
||||
}
|
||||
#define VD_SYM(lib, field, name) \
|
||||
if ((*(void **)&av.field = dlsym(lib, name)) == NULL) { \
|
||||
vd_log("FFmpeg has no %s, Remote Play decoding stays off", name); \
|
||||
return 0; \
|
||||
}
|
||||
VD_SYM(codec, find_decoder_by_name, "avcodec_find_decoder_by_name")
|
||||
VD_SYM(codec, alloc_context3, "avcodec_alloc_context3")
|
||||
VD_SYM(codec, open2, "avcodec_open2")
|
||||
VD_SYM(codec, free_context, "avcodec_free_context")
|
||||
VD_SYM(codec, send_packet, "avcodec_send_packet")
|
||||
VD_SYM(codec, receive_frame, "avcodec_receive_frame")
|
||||
VD_SYM(codec, flush_buffers, "avcodec_flush_buffers")
|
||||
VD_SYM(codec, packet_alloc, "av_packet_alloc")
|
||||
VD_SYM(codec, packet_free, "av_packet_free")
|
||||
VD_SYM(util, frame_alloc, "av_frame_alloc")
|
||||
VD_SYM(util, frame_free, "av_frame_free")
|
||||
VD_SYM(util, frame_unref, "av_frame_unref")
|
||||
VD_SYM(util, dict_set, "av_dict_set")
|
||||
VD_SYM(util, dict_free, "av_dict_free")
|
||||
VD_SYM(util, pix_fmt_name, "av_get_pix_fmt_name")
|
||||
#undef VD_SYM
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int vd_have_ffmpeg(void) {
|
||||
if (vd_ffmpeg == 0) vd_ffmpeg = vd_load_ffmpeg() ? 1 : -1;
|
||||
return vd_ffmpeg > 0;
|
||||
}
|
||||
|
||||
__attribute__((visibility("hidden"))) int bl_venc_connect(void);
|
||||
|
||||
static int vd_enabled(void) {
|
||||
pthread_mutex_lock(&vd_lock);
|
||||
if (vd_state == 0) {
|
||||
const char *flag = getenv("BL_REMOTE_PLAY_DECODE"), *socket = getenv("BL_VENC_SOCKET");
|
||||
vd_state = -1;
|
||||
if (strcmp(program_invocation_short_name, "streaming_client") == 0 && (flag == NULL || strcmp(flag, "0") != 0)) {
|
||||
vd_heap = open("/dev/dma_heap/system", O_RDONLY | O_CLOEXEC);
|
||||
if (vd_heap < 0) vd_log("no /dev/dma_heap/system (%s), Remote Play decoding stays off", strerror(errno));
|
||||
else if ((socket && *socket) || vd_have_ffmpeg()) vd_state = 1;
|
||||
for (int i = 0; i < VDEC_DUMB; i++) vd_dumb_fd[i] = -1;
|
||||
}
|
||||
}
|
||||
int on = vd_state > 0;
|
||||
pthread_mutex_unlock(&vd_lock);
|
||||
return on;
|
||||
}
|
||||
|
||||
static struct vdec *vd_find(int fd) {
|
||||
if (fd < 0 || __atomic_load_n(&vd_open_count, __ATOMIC_ACQUIRE) == 0) return NULL;
|
||||
for (int i = 0; i < VDEC_INSTANCES; i++) if (vd_all[i].used && vd_all[i].fd == fd) return &vd_all[i];
|
||||
return NULL;
|
||||
}
|
||||
|
||||
__attribute__((visibility("hidden"))) int bl_vdec_open(const char *path, int flags) {
|
||||
(void)flags;
|
||||
if (path == NULL || strcmp(path, VDEC_DEVICE) != 0 || !vd_enabled()) return -2;
|
||||
int fd = memfd_create("droiddeck-vdec", 0);
|
||||
if (fd < 0) return -1;
|
||||
pthread_mutex_lock(&vd_lock);
|
||||
struct vdec *v = NULL;
|
||||
for (int i = 0; i < VDEC_INSTANCES && v == NULL; i++) if (!vd_all[i].used) v = &vd_all[i];
|
||||
if (v == NULL) {
|
||||
pthread_mutex_unlock(&vd_lock);
|
||||
close(fd);
|
||||
errno = EBUSY;
|
||||
return -1;
|
||||
}
|
||||
memset(v, 0, sizeof *v);
|
||||
v->used = 1;
|
||||
v->fd = fd;
|
||||
v->fourcc = V4L2_PIX_FMT_H264;
|
||||
v->last = -1;
|
||||
v->sock = -1;
|
||||
for (int i = 0; i < VDEC_BUFFERS; i++) v->cap[i].fd = -1;
|
||||
__atomic_add_fetch(&vd_open_count, 1, __ATOMIC_RELEASE);
|
||||
pthread_mutex_unlock(&vd_lock);
|
||||
return fd;
|
||||
}
|
||||
|
||||
static void vd_close_decoder(struct vdec *v) {
|
||||
if (v->frames) {
|
||||
vd_log("%u frames, %.1f ms each on average, %u over 25 ms", v->frames, v->decode_ms / v->frames, v->slow);
|
||||
}
|
||||
if (v->sock >= 0) close(v->sock);
|
||||
v->sock = -1;
|
||||
v->hw = 0;
|
||||
if (v->ctx) av.free_context(&v->ctx);
|
||||
if (v->packet) av.packet_free(&v->packet);
|
||||
if (v->frame) av.frame_free(&v->frame);
|
||||
free(v->packet_data);
|
||||
v->packet_data = NULL;
|
||||
v->packet_capacity = 0;
|
||||
v->frames = v->slow = 0;
|
||||
v->decode_ms = 0;
|
||||
v->last = -1;
|
||||
}
|
||||
|
||||
static int vd_send(int sock, const void *msg, size_t len, int fd) {
|
||||
struct iovec iov = { .iov_base = (void *)msg, .iov_len = len };
|
||||
char control[CMSG_SPACE(sizeof(int))];
|
||||
struct msghdr m = { .msg_iov = &iov, .msg_iovlen = 1 };
|
||||
if (fd >= 0) {
|
||||
memset(control, 0, sizeof control);
|
||||
m.msg_control = control;
|
||||
m.msg_controllen = sizeof control;
|
||||
struct cmsghdr *c = CMSG_FIRSTHDR(&m);
|
||||
c->cmsg_level = SOL_SOCKET;
|
||||
c->cmsg_type = SCM_RIGHTS;
|
||||
c->cmsg_len = CMSG_LEN(sizeof(int));
|
||||
memcpy(CMSG_DATA(c), &fd, sizeof fd);
|
||||
}
|
||||
return sendmsg(sock, &m, MSG_NOSIGNAL) == (ssize_t)len ? 0 : -1;
|
||||
}
|
||||
|
||||
static void vd_send_capture(struct vdec *v, uint32_t index) {
|
||||
struct vdec_capture c = { .type = VDEC_CAPTURE, .index = index, .size = (uint32_t)v->cap[index].len };
|
||||
if (v->sock >= 0 && v->cap[index].map && vd_send(v->sock, &c, sizeof c, v->cap[index].fd) != 0)
|
||||
vd_log("could not hand capture buffer %u to the app: %s", index, strerror(errno));
|
||||
}
|
||||
|
||||
static int vd_open_hardware(struct vdec *v) {
|
||||
if (v->hw_failed) return -1;
|
||||
int sock = bl_venc_connect();
|
||||
if (sock < 0) return -1;
|
||||
struct vdec_config c = {
|
||||
.type = VDEC_CONFIG, .codec = v->fourcc == V4L2_PIX_FMT_HEVC ? VENC_HEVC : VENC_H264,
|
||||
.width = v->width ? v->width : v->cap_width, .height = v->height ? v->height : v->cap_height,
|
||||
.stride = v->bpl, .rows = v->cap_height, .bitstreams = v->out_count, .bitstream_size = VDEC_BITSTREAM_SIZE,
|
||||
};
|
||||
struct vdec_result r;
|
||||
if (vd_send(sock, &c, sizeof c, v->fd) != 0 || recv(sock, &r, sizeof r, 0) != (ssize_t)sizeof r ||
|
||||
r.type != VDEC_RESULT || r.status != 0) {
|
||||
vd_log("the app has no %s decoder for %ux%u", c.codec == VENC_HEVC ? "HEVC" : "H.264", c.width, c.height);
|
||||
close(sock);
|
||||
v->hw_failed = 1;
|
||||
return -1;
|
||||
}
|
||||
v->sock = sock;
|
||||
v->hw = 1;
|
||||
for (uint32_t i = 0; i < v->cap_count; i++) vd_send_capture(v, i);
|
||||
vd_log("decoding %s %ux%u with the app's MediaCodec into %ux%u NV12 dma-bufs",
|
||||
c.codec == VENC_HEVC ? "HEVC" : "H.264", c.width, c.height, v->cap_width, v->cap_height);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int vd_open_decoder(struct vdec *v) {
|
||||
if (vd_open_hardware(v) == 0) return 0;
|
||||
if (!vd_have_ffmpeg()) return -1;
|
||||
const char *name = v->fourcc == V4L2_PIX_FMT_HEVC ? "hevc" : "h264";
|
||||
const void *codec = av.find_decoder_by_name(name);
|
||||
if (codec == NULL) {
|
||||
vd_log("FFmpeg has no %s decoder", name);
|
||||
return -1;
|
||||
}
|
||||
v->ctx = av.alloc_context3(codec);
|
||||
v->packet = av.packet_alloc();
|
||||
v->frame = av.frame_alloc();
|
||||
void *options = NULL;
|
||||
long cores = sysconf(_SC_NPROCESSORS_ONLN);
|
||||
char threads[8];
|
||||
snprintf(threads, sizeof threads, "%ld", cores > 4 ? 4L : cores < 1 ? 1L : cores);
|
||||
av.dict_set(&options, "threads", threads, 0);
|
||||
av.dict_set(&options, "thread_type", "slice", 0);
|
||||
av.dict_set(&options, "flags", "+low_delay", 0);
|
||||
av.dict_set(&options, "flags2", "+fast", 0);
|
||||
int r = v->ctx && v->packet && v->frame ? av.open2(v->ctx, codec, &options) : -1;
|
||||
av.dict_free(&options);
|
||||
if (r < 0) {
|
||||
vd_log("could not open the %s decoder (%d)", name, r);
|
||||
vd_close_decoder(v);
|
||||
return -1;
|
||||
}
|
||||
vd_log("decoding %s %ux%u in software into %ux%u NV12 dma-bufs", v->fourcc == V4L2_PIX_FMT_HEVC ? "HEVC" : "H.264",
|
||||
v->width, v->height, v->cap_width, v->cap_height);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void vd_unmap_capture(struct vdec *v) {
|
||||
for (uint32_t i = 0; i < VDEC_BUFFERS; i++) {
|
||||
if (v->cap[i].map) munmap(v->cap[i].map, v->cap[i].len);
|
||||
v->cap[i] = (struct vd_capture){ .fd = -1 };
|
||||
}
|
||||
v->cap_count = v->n_cap_queue = v->n_cap_done = 0;
|
||||
v->last = -1;
|
||||
}
|
||||
|
||||
static void vd_sync(int fd, uint64_t flags) {
|
||||
struct vd_dma_buf_sync s = { .flags = flags | VD_DMA_BUF_SYNC_WRITE };
|
||||
ioctl(fd, VD_DMA_BUF_IOCTL_SYNC, &s);
|
||||
}
|
||||
|
||||
static void vd_blank(struct vdec *v, struct vd_capture *c) {
|
||||
size_t luma = (size_t)v->bpl * v->cap_height;
|
||||
if (luma + luma / 2 > c->len) return;
|
||||
vd_sync(c->fd, VD_DMA_BUF_SYNC_START);
|
||||
memset(c->map, 16, luma);
|
||||
memset(c->map + luma, 128, luma / 2);
|
||||
vd_sync(c->fd, VD_DMA_BUF_SYNC_END);
|
||||
}
|
||||
|
||||
static int vd_map_capture(struct vdec *v, uint32_t index, int fd) {
|
||||
struct stat st;
|
||||
if (fd < 0 || fstat(fd, &st) != 0) return -EINVAL;
|
||||
struct vd_capture *c = &v->cap[index];
|
||||
if (c->map && c->ino == st.st_ino) {
|
||||
c->fd = fd;
|
||||
return 0;
|
||||
}
|
||||
if (c->map) {
|
||||
munmap(c->map, c->len);
|
||||
*c = (struct vd_capture){ .fd = -1 };
|
||||
if (v->last == (int)index) v->last = -1;
|
||||
}
|
||||
off_t len = lseek(fd, 0, SEEK_END);
|
||||
if (len < (off_t)v->cap_size) return -EINVAL;
|
||||
void *p = mmap(NULL, (size_t)len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
if (p == MAP_FAILED) {
|
||||
vd_log("could not map capture buffer %u: %s", index, strerror(errno));
|
||||
*c = (struct vd_capture){ .fd = -1 };
|
||||
return -EINVAL;
|
||||
}
|
||||
*c = (struct vd_capture){ .fd = fd, .ino = st.st_ino, .map = p, .len = (size_t)len };
|
||||
vd_blank(v, c);
|
||||
if (v->hw) vd_send_capture(v, index);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int vd_write_frame(struct vdec *v, struct vd_capture *c) {
|
||||
uint8_t **data = (uint8_t **)v->frame;
|
||||
int *linesize = (int *)((uint8_t *)v->frame + 64);
|
||||
int width = *(int *)((uint8_t *)v->frame + 104), height = *(int *)((uint8_t *)v->frame + 108);
|
||||
const char *format = av.pix_fmt_name(*(int *)((uint8_t *)v->frame + 116));
|
||||
if (format == NULL || width <= 0 || height <= 0) return -1;
|
||||
int nv12 = strcmp(format, "nv12") == 0;
|
||||
if (!nv12 && strcmp(format, "yuv420p") != 0 && strcmp(format, "yuvj420p") != 0) {
|
||||
static int warned;
|
||||
if (!warned++) vd_log("cannot show %s frames", format);
|
||||
return -1;
|
||||
}
|
||||
uint32_t w = (uint32_t)width < v->bpl ? (uint32_t)width : v->bpl;
|
||||
uint32_t h = (uint32_t)height < v->cap_height ? (uint32_t)height : v->cap_height;
|
||||
uint8_t *y = c->map, *uv = c->map + (size_t)v->bpl * v->cap_height;
|
||||
vd_sync(c->fd, VD_DMA_BUF_SYNC_START);
|
||||
for (uint32_t row = 0; row < h; row++) memcpy(y + (size_t)row * v->bpl, data[0] + (size_t)row * linesize[0], w);
|
||||
for (uint32_t row = 0; row < (h + 1) / 2; row++) {
|
||||
uint8_t *dst = uv + (size_t)row * v->bpl;
|
||||
if (nv12) {
|
||||
memcpy(dst, data[1] + (size_t)row * linesize[1], w & ~1u);
|
||||
continue;
|
||||
}
|
||||
const uint8_t *u = data[1] + (size_t)row * linesize[1], *vv = data[2] + (size_t)row * linesize[2];
|
||||
for (uint32_t x = 0; x < w / 2; x++) {
|
||||
dst[2 * x] = u[x];
|
||||
dst[2 * x + 1] = vv[x];
|
||||
}
|
||||
}
|
||||
vd_sync(c->fd, VD_DMA_BUF_SYNC_END);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void vd_copy_last(struct vdec *v, struct vd_capture *c) {
|
||||
if (v->last < 0 || v->cap[v->last].map == NULL || &v->cap[v->last] == c) return;
|
||||
size_t size = v->cap_size < c->len ? v->cap_size : c->len;
|
||||
vd_sync(c->fd, VD_DMA_BUF_SYNC_START);
|
||||
memcpy(c->map, v->cap[v->last].map, size);
|
||||
vd_sync(c->fd, VD_DMA_BUF_SYNC_END);
|
||||
}
|
||||
|
||||
static double vd_now_ms(void) {
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
return ts.tv_sec * 1000.0 + ts.tv_nsec / 1e6;
|
||||
}
|
||||
|
||||
static void vd_deliver(struct vdec *v, int written, double start, struct timeval timestamp) {
|
||||
uint32_t target = v->cap_queue[0];
|
||||
if (!written) {
|
||||
if (v->last < 0) return;
|
||||
vd_copy_last(v, &v->cap[target]);
|
||||
}
|
||||
double ms = vd_now_ms() - start;
|
||||
v->frames++;
|
||||
v->decode_ms += ms;
|
||||
if (ms > 25.0) v->slow++;
|
||||
memmove(v->cap_queue, v->cap_queue + 1, --v->n_cap_queue * sizeof v->cap_queue[0]);
|
||||
v->cap_done[v->n_cap_done++] = (struct vd_done){ .index = target, .timestamp = timestamp };
|
||||
v->last = (int)target;
|
||||
}
|
||||
|
||||
static void vd_decode_hardware(struct vdec *v, uint32_t index, uint32_t bytes, struct timeval timestamp) {
|
||||
double start = vd_now_ms();
|
||||
v->pts += 16667;
|
||||
struct vdec_packet p = { .type = VDEC_PACKET, .input = index, .bytes = bytes, .capture = v->cap_queue[0], .pts_us = v->pts };
|
||||
struct vdec_result r;
|
||||
if (vd_send(v->sock, &p, sizeof p, -1) != 0 || recv(v->sock, &r, sizeof r, 0) != (ssize_t)sizeof r ||
|
||||
r.type != VDEC_RESULT) {
|
||||
vd_log("lost the app's decoder (%s), switching to software", strerror(errno));
|
||||
vd_close_decoder(v);
|
||||
v->hw_failed = 1;
|
||||
return;
|
||||
}
|
||||
vd_deliver(v, r.status == 0 && r.frame, start, timestamp);
|
||||
}
|
||||
|
||||
static void vd_decode(struct vdec *v, uint32_t index, uint32_t bytes, struct timeval timestamp) {
|
||||
if (!v->cap_on || v->n_cap_queue == 0) return;
|
||||
if (v->ctx == NULL && !v->hw && vd_open_decoder(v) != 0) return;
|
||||
if (v->hw) {
|
||||
vd_decode_hardware(v, index, bytes, timestamp);
|
||||
return;
|
||||
}
|
||||
if (v->packet_capacity < bytes + VDEC_PADDING) {
|
||||
free(v->packet_data);
|
||||
v->packet_capacity = bytes + VDEC_PADDING + (64u << 10);
|
||||
v->packet_data = malloc(v->packet_capacity);
|
||||
if (v->packet_data == NULL) {
|
||||
v->packet_capacity = 0;
|
||||
return;
|
||||
}
|
||||
}
|
||||
memcpy(v->packet_data, v->bitstream + (size_t)index * VDEC_BITSTREAM_SIZE, bytes);
|
||||
memset(v->packet_data + bytes, 0, VDEC_PADDING);
|
||||
*(uint8_t **)((uint8_t *)v->packet + 24) = v->packet_data;
|
||||
*(int *)((uint8_t *)v->packet + 32) = (int)bytes;
|
||||
double start = vd_now_ms();
|
||||
uint32_t target = v->cap_queue[0];
|
||||
struct vd_capture *c = &v->cap[target];
|
||||
int written = 0;
|
||||
if (av.send_packet(v->ctx, v->packet) >= 0) {
|
||||
while (av.receive_frame(v->ctx, v->frame) >= 0) {
|
||||
if (vd_write_frame(v, c) == 0) written = 1;
|
||||
av.frame_unref(v->frame);
|
||||
}
|
||||
}
|
||||
*(uint8_t **)((uint8_t *)v->packet + 24) = NULL;
|
||||
*(int *)((uint8_t *)v->packet + 32) = 0;
|
||||
vd_deliver(v, written, start, timestamp);
|
||||
}
|
||||
|
||||
static int is_output(uint32_t type) { return type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE || type == V4L2_BUF_TYPE_VIDEO_OUTPUT; }
|
||||
static int is_capture(uint32_t type) { return type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE || type == V4L2_BUF_TYPE_VIDEO_CAPTURE; }
|
||||
|
||||
static void vd_fill_format(struct vdec *v, struct v4l2_format *f) {
|
||||
struct v4l2_pix_format_mplane *p = &f->fmt.pix_mp;
|
||||
memset(p, 0, sizeof *p);
|
||||
p->field = V4L2_FIELD_NONE;
|
||||
p->num_planes = 1;
|
||||
p->colorspace = V4L2_COLORSPACE_REC709;
|
||||
if (is_output(f->type)) {
|
||||
p->width = v->width;
|
||||
p->height = v->height;
|
||||
p->pixelformat = v->fourcc;
|
||||
p->plane_fmt[0].sizeimage = VDEC_BITSTREAM_SIZE;
|
||||
} else {
|
||||
p->width = v->cap_width;
|
||||
p->height = v->cap_height;
|
||||
p->pixelformat = V4L2_PIX_FMT_NV12;
|
||||
p->plane_fmt[0].bytesperline = v->bpl;
|
||||
p->plane_fmt[0].sizeimage = v->cap_size;
|
||||
}
|
||||
}
|
||||
|
||||
static int vd_set_format(struct vdec *v, struct v4l2_format *f, int apply) {
|
||||
struct v4l2_pix_format_mplane *p = &f->fmt.pix_mp;
|
||||
if (f->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE && f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) return -EINVAL;
|
||||
struct vdec t = *v;
|
||||
if (is_output(f->type)) {
|
||||
if (p->pixelformat != V4L2_PIX_FMT_H264 && p->pixelformat != V4L2_PIX_FMT_HEVC) return -EINVAL;
|
||||
t.fourcc = p->pixelformat;
|
||||
t.width = p->width;
|
||||
t.height = p->height;
|
||||
} else {
|
||||
uint32_t w = p->width > t.width ? p->width : t.width, h = p->height > t.height ? p->height : t.height;
|
||||
if (w < 16 || h < 16 || w > 8192 || h > 8192) return -EINVAL;
|
||||
t.cap_width = (w + 15) & ~15u;
|
||||
t.cap_height = (h + 15) & ~15u;
|
||||
t.bpl = t.cap_width;
|
||||
t.cap_size = t.bpl * t.cap_height * 3 / 2;
|
||||
}
|
||||
if (apply) {
|
||||
if (t.fourcc != v->fourcc || (t.hw && (t.bpl != v->bpl || t.cap_height != v->cap_height))) vd_close_decoder(&t);
|
||||
*v = t;
|
||||
}
|
||||
vd_fill_format(&t, f);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int vd_reqbufs(struct vdec *v, struct v4l2_requestbuffers *r) {
|
||||
uint32_t n = r->count > VDEC_BUFFERS ? VDEC_BUFFERS : r->count;
|
||||
if (r->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE && r->memory == V4L2_MEMORY_MMAP) {
|
||||
if (v->bitstream) munmap(v->bitstream, (size_t)v->out_count * VDEC_BITSTREAM_SIZE);
|
||||
v->bitstream = NULL;
|
||||
v->out_count = v->n_out_done = 0;
|
||||
if (n && ftruncate(v->fd, (off_t)n * VDEC_BITSTREAM_SIZE) != 0) return -ENOMEM;
|
||||
if (n) {
|
||||
void *p = mmap(NULL, (size_t)n * VDEC_BITSTREAM_SIZE, PROT_READ, MAP_SHARED, v->fd, 0);
|
||||
if (p == MAP_FAILED) return -ENOMEM;
|
||||
v->bitstream = p;
|
||||
}
|
||||
v->out_count = n;
|
||||
} else if (r->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE && r->memory == V4L2_MEMORY_DMABUF) {
|
||||
if (v->hw) vd_close_decoder(v);
|
||||
vd_unmap_capture(v);
|
||||
v->cap_count = n;
|
||||
} else {
|
||||
return -EINVAL;
|
||||
}
|
||||
r->count = n;
|
||||
r->capabilities = r->memory == V4L2_MEMORY_MMAP ? V4L2_BUF_CAP_SUPPORTS_MMAP : V4L2_BUF_CAP_SUPPORTS_DMABUF;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int vd_querybuf(struct vdec *v, struct v4l2_buffer *b) {
|
||||
if (b->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE || b->index >= v->out_count || b->m.planes == NULL || b->length < 1)
|
||||
return -EINVAL;
|
||||
b->memory = V4L2_MEMORY_MMAP;
|
||||
b->flags = V4L2_BUF_FLAG_MAPPED;
|
||||
b->length = 1;
|
||||
b->m.planes[0].length = VDEC_BITSTREAM_SIZE;
|
||||
b->m.planes[0].m.mem_offset = b->index * VDEC_BITSTREAM_SIZE;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int vd_qbuf(struct vdec *v, struct v4l2_buffer *b) {
|
||||
if (b->m.planes == NULL || b->length < 1) return -EINVAL;
|
||||
if (b->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
|
||||
if (b->index >= v->out_count || v->n_out_done >= VDEC_BUFFERS) return -EINVAL;
|
||||
uint32_t bytes = b->m.planes[0].bytesused;
|
||||
if (bytes > VDEC_BITSTREAM_SIZE) bytes = VDEC_BITSTREAM_SIZE;
|
||||
if (v->out_on && bytes) vd_decode(v, b->index, bytes, b->timestamp);
|
||||
v->out_done[v->n_out_done++] = b->index;
|
||||
} else if (b->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
|
||||
if (b->index >= v->cap_count || v->cap_size == 0) return -EINVAL;
|
||||
int r = vd_map_capture(v, b->index, b->m.planes[0].m.fd);
|
||||
if (r < 0) return r;
|
||||
for (uint32_t i = 0; i < v->n_cap_queue; i++) if (v->cap_queue[i] == b->index) return 0;
|
||||
if (v->n_cap_queue >= VDEC_BUFFERS) return -EINVAL;
|
||||
v->cap_queue[v->n_cap_queue++] = b->index;
|
||||
} else {
|
||||
return -EINVAL;
|
||||
}
|
||||
b->flags |= V4L2_BUF_FLAG_QUEUED;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int vd_dqbuf(struct vdec *v, struct v4l2_buffer *b) {
|
||||
if (b->m.planes == NULL || b->length < 1) return -EINVAL;
|
||||
if (b->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
|
||||
if (v->n_out_done == 0) return -EAGAIN;
|
||||
b->index = v->out_done[0];
|
||||
memmove(v->out_done, v->out_done + 1, --v->n_out_done * sizeof v->out_done[0]);
|
||||
b->memory = V4L2_MEMORY_MMAP;
|
||||
b->flags = V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_MAPPED;
|
||||
b->length = 1;
|
||||
b->m.planes[0].bytesused = 0;
|
||||
b->m.planes[0].length = VDEC_BITSTREAM_SIZE;
|
||||
b->m.planes[0].m.mem_offset = b->index * VDEC_BITSTREAM_SIZE;
|
||||
return 0;
|
||||
}
|
||||
if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) return -EINVAL;
|
||||
if (v->n_cap_done == 0) return -EAGAIN;
|
||||
struct vd_done d = v->cap_done[0];
|
||||
memmove(v->cap_done, v->cap_done + 1, --v->n_cap_done * sizeof d);
|
||||
b->index = d.index;
|
||||
b->memory = V4L2_MEMORY_DMABUF;
|
||||
b->flags = V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_TIMESTAMP_COPY;
|
||||
b->field = V4L2_FIELD_NONE;
|
||||
b->timestamp = d.timestamp;
|
||||
b->length = 1;
|
||||
b->m.planes[0].bytesused = v->cap_size;
|
||||
b->m.planes[0].length = v->cap_size;
|
||||
b->m.planes[0].m.fd = v->cap[d.index].fd;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int vd_ioctl(struct vdec *v, unsigned long req, void *arg) {
|
||||
switch (req) {
|
||||
case VIDIOC_QUERYCAP: {
|
||||
struct v4l2_capability *c = arg;
|
||||
memset(c, 0, sizeof *c);
|
||||
strcpy((char *)c->driver, "droiddeck_vdec");
|
||||
strcpy((char *)c->card, "DroidDeck video decoder");
|
||||
strcpy((char *)c->bus_info, "platform:droiddeck-vdec");
|
||||
c->version = 0x00060000;
|
||||
c->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
|
||||
c->capabilities = c->device_caps | V4L2_CAP_DEVICE_CAPS;
|
||||
return 0;
|
||||
}
|
||||
case VIDIOC_ENUM_FMT: {
|
||||
struct v4l2_fmtdesc *d = arg;
|
||||
static const uint32_t coded[] = { V4L2_PIX_FMT_H264, V4L2_PIX_FMT_HEVC };
|
||||
if (is_capture(d->type) && d->index == 0) {
|
||||
d->pixelformat = V4L2_PIX_FMT_NV12;
|
||||
strcpy((char *)d->description, "Y/CbCr 4:2:0");
|
||||
d->flags = 0;
|
||||
} else if (is_output(d->type) && d->index < 2) {
|
||||
d->pixelformat = coded[d->index];
|
||||
strcpy((char *)d->description, d->index ? "HEVC" : "H.264");
|
||||
d->flags = V4L2_FMT_FLAG_COMPRESSED;
|
||||
} else {
|
||||
return -EINVAL;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
case VIDIOC_G_FMT: {
|
||||
struct v4l2_format *f = arg;
|
||||
if (!is_output(f->type) && !is_capture(f->type)) return -EINVAL;
|
||||
vd_fill_format(v, f);
|
||||
return 0;
|
||||
}
|
||||
case VIDIOC_S_FMT: return vd_set_format(v, arg, 1);
|
||||
case VIDIOC_TRY_FMT: return vd_set_format(v, arg, 0);
|
||||
case VIDIOC_REQBUFS: return vd_reqbufs(v, arg);
|
||||
case VIDIOC_QUERYBUF: return vd_querybuf(v, arg);
|
||||
case VIDIOC_QBUF: return vd_qbuf(v, arg);
|
||||
case VIDIOC_DQBUF: return vd_dqbuf(v, arg);
|
||||
case VIDIOC_STREAMON:
|
||||
case VIDIOC_STREAMOFF: {
|
||||
uint32_t type = *(uint32_t *)arg;
|
||||
int on = req == VIDIOC_STREAMON;
|
||||
if (is_output(type)) {
|
||||
v->out_on = on;
|
||||
if (!on) {
|
||||
v->n_out_done = 0;
|
||||
if (v->ctx) av.flush_buffers(v->ctx);
|
||||
}
|
||||
} else if (is_capture(type)) {
|
||||
v->cap_on = on;
|
||||
if (!on) v->n_cap_queue = v->n_cap_done = 0;
|
||||
} else {
|
||||
return -EINVAL;
|
||||
}
|
||||
if (v->out_on && v->cap_on && v->ctx == NULL && !v->hw && v->out_count && v->cap_size) vd_open_decoder(v);
|
||||
return 0;
|
||||
}
|
||||
case VIDIOC_SUBSCRIBE_EVENT:
|
||||
case VIDIOC_UNSUBSCRIBE_EVENT:
|
||||
case VIDIOC_DECODER_CMD:
|
||||
case VIDIOC_TRY_DECODER_CMD:
|
||||
return 0;
|
||||
case VIDIOC_DQEVENT:
|
||||
return -ENOENT;
|
||||
default:
|
||||
vd_log("unhandled ioctl 0x%lx", req);
|
||||
return -ENOTTY;
|
||||
}
|
||||
}
|
||||
|
||||
__attribute__((visibility("hidden"))) int bl_vdec_ioctl(int fd, unsigned long req, void *arg, int *rc) {
|
||||
struct vdec *v = vd_find(fd);
|
||||
if (v == NULL) return 0;
|
||||
pthread_mutex_lock(&vd_lock);
|
||||
int r = arg ? vd_ioctl(v, req, arg) : -EFAULT;
|
||||
pthread_mutex_unlock(&vd_lock);
|
||||
if (r < 0) {
|
||||
errno = -r;
|
||||
*rc = -1;
|
||||
} else {
|
||||
*rc = 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
__attribute__((visibility("hidden"))) void bl_vdec_closed(int fd) {
|
||||
struct vdec *v = vd_find(fd);
|
||||
if (v == NULL) return;
|
||||
pthread_mutex_lock(&vd_lock);
|
||||
vd_close_decoder(v);
|
||||
vd_unmap_capture(v);
|
||||
if (v->bitstream) munmap(v->bitstream, (size_t)v->out_count * VDEC_BITSTREAM_SIZE);
|
||||
v->bitstream = NULL;
|
||||
v->used = 0;
|
||||
__atomic_sub_fetch(&vd_open_count, 1, __ATOMIC_RELEASE);
|
||||
pthread_mutex_unlock(&vd_lock);
|
||||
}
|
||||
|
||||
__attribute__((visibility("hidden"))) int bl_vdec_poll(struct pollfd *fds, nfds_t n, int timeout_ms,
|
||||
int (*real)(struct pollfd *, nfds_t, int), int *ret) {
|
||||
if (n != 1 || vd_find(fds[0].fd) == NULL) return 0;
|
||||
struct vdec *v = vd_find(fds[0].fd);
|
||||
pthread_mutex_lock(&vd_lock);
|
||||
short ready = (v->n_cap_done ? POLLIN | POLLRDNORM : 0) | (v->n_out_done ? POLLOUT | POLLWRNORM : 0);
|
||||
pthread_mutex_unlock(&vd_lock);
|
||||
fds[0].revents = ready & fds[0].events;
|
||||
if (fds[0].revents == 0 && timeout_ms != 0) real(NULL, 0, timeout_ms < 0 || timeout_ms > 1000 ? 1000 : timeout_ms);
|
||||
*ret = fds[0].revents ? 1 : 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int drm_get_devices2(uint32_t flags, struct vd_device **devices, int max) {
|
||||
(void)flags;
|
||||
static char *nodes[3] = { "/dev/null", NULL, NULL };
|
||||
static struct vd_device device = { .nodes = nodes, .available_nodes = 1 };
|
||||
if (devices == NULL || max < 1) return 1;
|
||||
devices[0] = &device;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void drm_free_devices(struct vd_device **devices, int count) { (void)devices; (void)count; }
|
||||
|
||||
static struct vd_mode_info drm_mode = {
|
||||
.clock = 148500, .hdisplay = 1920, .hsync_start = 2008, .hsync_end = 2052, .htotal = 2200,
|
||||
.vdisplay = 1080, .vsync_start = 1084, .vsync_end = 1089, .vtotal = 1125, .vrefresh = 60, .name = "1920x1080",
|
||||
};
|
||||
static uint32_t drm_crtc_ids[1] = { 1 }, drm_connector_ids[1] = { 2 }, drm_encoder_ids[1] = { 3 };
|
||||
|
||||
static struct vd_res *drm_get_resources(int fd) {
|
||||
(void)fd;
|
||||
static struct vd_res res = {
|
||||
.count_crtcs = 1, .crtcs = drm_crtc_ids, .count_connectors = 1, .connectors = drm_connector_ids,
|
||||
.count_encoders = 1, .encoders = drm_encoder_ids, .max_width = 8192, .max_height = 8192,
|
||||
};
|
||||
return &res;
|
||||
}
|
||||
|
||||
static struct vd_connector *drm_get_connector(int fd, uint32_t id) {
|
||||
(void)fd;
|
||||
static struct vd_connector connector = {
|
||||
.connector_id = 2, .encoder_id = 3, .connector_type = 14, .connection = 1, .count_modes = 1,
|
||||
.modes = &drm_mode, .count_encoders = 1, .encoders = drm_encoder_ids,
|
||||
};
|
||||
return id == 2 ? &connector : NULL;
|
||||
}
|
||||
|
||||
static struct vd_encoder *drm_get_encoder(int fd, uint32_t id) {
|
||||
(void)fd;
|
||||
static struct vd_encoder encoder = { .encoder_id = 3, .encoder_type = 8, .crtc_id = 1, .possible_crtcs = 1 };
|
||||
return id == 3 ? &encoder : NULL;
|
||||
}
|
||||
|
||||
static struct vd_crtc *drm_get_crtc(int fd, uint32_t id) {
|
||||
(void)fd;
|
||||
static struct vd_crtc crtc;
|
||||
if (id != 1) return NULL;
|
||||
crtc = (struct vd_crtc){ .crtc_id = 1, .width = 1920, .height = 1080, .mode_valid = 1, .mode = drm_mode };
|
||||
return &crtc;
|
||||
}
|
||||
|
||||
static void drm_free(void *p) { (void)p; }
|
||||
static void *drm_null(void) { return NULL; }
|
||||
static int drm_fail(void) { return -EINVAL; }
|
||||
|
||||
static int drm_dumb_slot(uint32_t handle) { return handle >= 1 && handle <= VDEC_DUMB ? (int)handle - 1 : -1; }
|
||||
|
||||
static int drm_ioctl(int fd, unsigned long request, void *arg) {
|
||||
(void)fd;
|
||||
int r = -EINVAL;
|
||||
pthread_mutex_lock(&vd_lock);
|
||||
if (request == VD_IOCTL_MODE_CREATE_DUMB && arg) {
|
||||
struct vd_create_dumb *c = arg;
|
||||
uint64_t pitch = ((uint64_t)c->width * c->bpp + 7) / 8;
|
||||
uint64_t size = (pitch * c->height + 4095) & ~4095ull;
|
||||
int slot = -1;
|
||||
for (int i = 0; i < VDEC_DUMB && slot < 0; i++) if (vd_dumb_fd[i] < 0) slot = i;
|
||||
struct vd_dma_heap_alloc a = { .len = size, .fd_flags = O_RDWR | O_CLOEXEC };
|
||||
if (slot < 0 || size == 0 || size > (512ull << 20)) {
|
||||
r = -ENOMEM;
|
||||
} else if (ioctl(vd_heap, VD_DMA_HEAP_IOCTL_ALLOC, &a) != 0) {
|
||||
r = -errno;
|
||||
vd_log("dma-heap allocation of %llu bytes failed: %s", (unsigned long long)size, strerror(errno));
|
||||
} else {
|
||||
vd_dumb_fd[slot] = (int)a.fd;
|
||||
c->handle = (uint32_t)slot + 1;
|
||||
c->pitch = (uint32_t)pitch;
|
||||
c->size = size;
|
||||
r = 0;
|
||||
}
|
||||
} else if (request == VD_IOCTL_PRIME_HANDLE_TO_FD && arg) {
|
||||
struct vd_prime_handle *h = arg;
|
||||
int slot = drm_dumb_slot(h->handle);
|
||||
int dup_fd = slot >= 0 && vd_dumb_fd[slot] >= 0 ? fcntl(vd_dumb_fd[slot], F_DUPFD_CLOEXEC, 0) : -1;
|
||||
if (dup_fd >= 0) {
|
||||
h->fd = dup_fd;
|
||||
r = 0;
|
||||
} else {
|
||||
r = slot >= 0 ? -errno : -ENOENT;
|
||||
}
|
||||
} else if (request == VD_IOCTL_MODE_DESTROY_DUMB && arg) {
|
||||
int slot = drm_dumb_slot(((struct vd_destroy_dumb *)arg)->handle);
|
||||
if (slot >= 0 && vd_dumb_fd[slot] >= 0) {
|
||||
close(vd_dumb_fd[slot]);
|
||||
vd_dumb_fd[slot] = -1;
|
||||
r = 0;
|
||||
} else {
|
||||
r = -ENOENT;
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&vd_lock);
|
||||
if (r < 0) {
|
||||
errno = -r;
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct {
|
||||
const char *name;
|
||||
void *fn;
|
||||
} drm_table[] = {
|
||||
{ "drmGetDevices2", (void *)drm_get_devices2 },
|
||||
{ "drmFreeDevices", (void *)drm_free_devices },
|
||||
{ "drmSetMaster", (void *)drm_fail },
|
||||
{ "drmSetClientCap", (void *)drm_fail },
|
||||
{ "drmModeGetResources", (void *)drm_get_resources },
|
||||
{ "drmModeFreeResources", (void *)drm_free },
|
||||
{ "drmModeGetConnector", (void *)drm_get_connector },
|
||||
{ "drmModeFreeConnector", (void *)drm_free },
|
||||
{ "drmModeGetEncoder", (void *)drm_get_encoder },
|
||||
{ "drmModeFreeEncoder", (void *)drm_free },
|
||||
{ "drmModeGetCrtc", (void *)drm_get_crtc },
|
||||
{ "drmModeSetCrtc", (void *)drm_fail },
|
||||
{ "drmModeFreeCrtc", (void *)drm_free },
|
||||
{ "drmModeGetPlaneResources", (void *)drm_null },
|
||||
{ "drmModeFreePlaneResources", (void *)drm_free },
|
||||
{ "drmModeGetPlane", (void *)drm_null },
|
||||
{ "drmModeFreePlane", (void *)drm_free },
|
||||
{ "drmModeObjectGetProperties", (void *)drm_null },
|
||||
{ "drmModeFreeObjectProperties", (void *)drm_free },
|
||||
{ "drmModeGetProperty", (void *)drm_null },
|
||||
{ "drmModeFreeProperty", (void *)drm_free },
|
||||
{ "drmModeAddFB2", (void *)drm_fail },
|
||||
{ "drmModeAddFB2WithModifiers", (void *)drm_fail },
|
||||
{ "drmModeRmFB", (void *)drm_fail },
|
||||
{ "drmModeAtomicAlloc", (void *)drm_null },
|
||||
{ "drmModeAtomicFree", (void *)drm_free },
|
||||
{ "drmModeAtomicAddProperty", (void *)drm_fail },
|
||||
{ "drmModeAtomicCommit", (void *)drm_fail },
|
||||
{ "drmIoctl", (void *)drm_ioctl },
|
||||
};
|
||||
|
||||
void *SDL_LoadObject(const char *name) {
|
||||
static void *(*real)(const char *);
|
||||
if (name && strcmp(name, "libdrm.so.2") == 0 && vd_enabled()) return &vd_drm_handle;
|
||||
if (!real) real = (void *(*)(const char *))dlsym(RTLD_NEXT, "SDL_LoadObject");
|
||||
return real ? real(name) : NULL;
|
||||
}
|
||||
|
||||
void *SDL_LoadFunction(void *handle, const char *name) {
|
||||
static void *(*real)(void *, const char *);
|
||||
if (handle == &vd_drm_handle) {
|
||||
for (size_t i = 0; name && i < sizeof drm_table / sizeof drm_table[0]; i++)
|
||||
if (strcmp(drm_table[i].name, name) == 0) return drm_table[i].fn;
|
||||
return NULL;
|
||||
}
|
||||
if (!real) real = (void *(*)(void *, const char *))dlsym(RTLD_NEXT, "SDL_LoadFunction");
|
||||
return real ? real(handle, name) : NULL;
|
||||
}
|
||||
|
||||
void SDL_UnloadObject(void *handle) {
|
||||
static void (*real)(void *);
|
||||
if (handle == &vd_drm_handle) return;
|
||||
if (!real) real = (void (*)(void *))dlsym(RTLD_NEXT, "SDL_UnloadObject");
|
||||
if (real) real(handle);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
__attribute__((visibility("hidden"))) int bl_vdec_open(const char *path, int flags) {
|
||||
(void)path;
|
||||
(void)flags;
|
||||
return -2;
|
||||
}
|
||||
|
||||
__attribute__((visibility("hidden"))) int bl_vdec_ioctl(int fd, unsigned long req, void *arg, int *rc) {
|
||||
(void)fd;
|
||||
(void)req;
|
||||
(void)arg;
|
||||
(void)rc;
|
||||
return 0;
|
||||
}
|
||||
|
||||
__attribute__((visibility("hidden"))) void bl_vdec_closed(int fd) { (void)fd; }
|
||||
|
||||
__attribute__((visibility("hidden"))) int bl_vdec_poll(struct pollfd *fds, nfds_t n, int timeout_ms,
|
||||
int (*real)(struct pollfd *, nfds_t, int), int *ret) {
|
||||
(void)fds;
|
||||
(void)n;
|
||||
(void)timeout_ms;
|
||||
(void)real;
|
||||
(void)ret;
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -625,9 +625,14 @@ static int vn_poll(struct pollfd *fds, nfds_t n, int timeout_ms, int (*real)(str
|
||||
return r + ready;
|
||||
}
|
||||
|
||||
__attribute__((weak, visibility("hidden"))) int bl_vdec_poll(struct pollfd *fds, nfds_t n, int timeout_ms,
|
||||
int (*real)(struct pollfd *, nfds_t, int), int *ret);
|
||||
|
||||
int poll(struct pollfd *fds, nfds_t n, int timeout_ms) {
|
||||
static int (*real)(struct pollfd *, nfds_t, int);
|
||||
if (!real) real = (int (*)(struct pollfd *, nfds_t, int))dlsym(RTLD_NEXT, "poll");
|
||||
int ret;
|
||||
if (bl_vdec_poll && bl_vdec_poll(fds, n, timeout_ms, real, &ret)) return ret;
|
||||
if (__atomic_load_n(&vn_open_count, __ATOMIC_ACQUIRE) == 0) return real(fds, n, timeout_ms);
|
||||
return vn_poll(fds, n, timeout_ms, real);
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
#define _GNU_SOURCE
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#ifndef WEBHELPER_DEFAULT
|
||||
#define WEBHELPER_DEFAULT "--no-zygote --in-process-gpu -AudioServiceOutOfProcess"
|
||||
@@ -25,8 +26,30 @@ static char *join_features(const char *have, const char *add) {
|
||||
return strcat(out, add);
|
||||
}
|
||||
|
||||
static char *const *chromium_argv(const char *path, char *const argv[]) {
|
||||
const char *slash = path ? strrchr(path, '/') : NULL;
|
||||
if (!slash || !argv || !argv[0]) return argv;
|
||||
int argc = 0;
|
||||
for (; argv[argc]; argc++)
|
||||
if (argc && (!strncmp(argv[argc], "--type=", 7) || !strcmp(argv[argc], "--no-sandbox"))) return argv;
|
||||
size_t dir = (size_t)(slash - path) + 1;
|
||||
char *sandbox = malloc(dir + sizeof "chrome-sandbox");
|
||||
if (!sandbox) return argv;
|
||||
memcpy(sandbox, path, dir);
|
||||
strcpy(sandbox + dir, "chrome-sandbox");
|
||||
int chromium = access(sandbox, F_OK) == 0;
|
||||
free(sandbox);
|
||||
if (!chromium) return argv;
|
||||
char **out = calloc((size_t)argc + 2, sizeof *out);
|
||||
if (!out) return argv;
|
||||
out[0] = argv[0];
|
||||
out[1] = (char *)"--no-sandbox";
|
||||
for (int i = 1; i < argc; i++) out[i + 1] = argv[i];
|
||||
return out;
|
||||
}
|
||||
|
||||
__attribute__((visibility("hidden"))) char *const *bl_webhelper_argv(const char *path, char *const argv[]) {
|
||||
if (!is_webhelper_browser(path, argv)) return argv;
|
||||
if (!is_webhelper_browser(path, argv)) return chromium_argv(path, argv);
|
||||
const char *spec = getenv("BL_WEBHELPER_ARGS");
|
||||
if (!spec || !*spec) spec = WEBHELPER_DEFAULT;
|
||||
if (!strcmp(spec, "stock")) return argv;
|
||||
|
||||
Reference in new issue
Block a user