Compare commits

..
Author SHA1 Message Date
swung0x48 5331106e90 [CI] Use uname for Arch runner hostname 2026-10-05 08:57:17 -04:00
swung0x48 ca981a8fc1 [CI] Add Arch WSL GPU runner smoke workflow and configuration 2026-10-05 08:56:27 -04:00
swung0x48 c90383db16 [Fix] (MG_Remote): bound socket buffers in log flood tests 2026-10-05 07:13:39 -04:00
swung0x48 698f04574b [Fix] (MG_Remote): wait for records emitted while draining return events 2026-10-05 06:21:42 -04:00
swung0x48 f497ff9741 [Fix] (DirectVulkan): detach ended render passes before attachment layout reconciliation 2026-10-05 05:12:42 -04:00
swung0x48 946e63d810 [Fix] (MG_IntegrationTest): check pre-finish work completion in present credit tests 2026-10-05 04:22:18 -04:00
swung0x48 918d40dd4f [Fix] (Init): allow MobileGL initialization with LIBGL_ALWAYS_SOFTWARE 2026-10-05 02:55:35 -04:00
swung0x48 f5b8fe48aa [Fix] (MGPipe, MG_Remote): verify server pack state and reject records after guilty GPU resets 2026-10-05 02:51:19 -04:00
swung0x48 c5d25868cf [Fix] (ShaderTranspiler): preserve block identity when deduplicating image types 2026-10-05 01:27:50 -04:00
swung0x48 c464947bdd [Fix] (TraceReplay): share Android window surfaces and release current bindings before resize 2026-10-05 00:47:56 -04:00
swung0x48 4fb75f4e00 [Fix] (DirectVulkan): prepare image copy recording after uploads and share texture usage checks 2026-10-05 00:19:21 -04:00
swung0x48 4d6904184d [Fix] (ShaderTranspiler): deduplicate image types after Vulkan image rewrites 2026-10-05 00:08:32 -04:00
swung0x48 2f1a673513 [Fix] (BackendLoader, DirectGLES): check EGL extensions before resolving damage swap entry points 2026-10-05 00:08:31 -04:00
swung0x48 88bc35b711 [Refactor] (BackendLoader): share extension name matching between GLES and EGL probes 2026-10-04 20:54:28 -04:00
swung0x48 880be57bb6 [CI] (Watchdog): disable GPU hang detection in all CI runs 2026-10-04 10:39:39 -04:00
swung0x48 70e4691b3a [Docs] (Anland): refresh the handoff to the 2026-10-04 state, with the perf, CI, YUV and VA-API streams 2026-10-04 07:25:23 -04:00
Swung0x48 2d42414667 [CI] (Test): annotate failed ctest entries with their failure lines, since the run logs need a signed-in reader 2026-10-04 07:20:04 -04:00
Swung0x48 32d27b1491 [Fix] (DirectGLES): a session's backend registration no longer trips the strict apply-thread pin while another session is applying 2026-10-04 07:20:04 -04:00
Swung0x48 78c5f8dae7 [CI] (Census): record Refuse{Recover} as a local refusal; the client declines to redial and there is no peer to tell 2026-10-04 07:20:04 -04:00
swung0x48 96bd540ba1 [Docs] (Performance): record the device state at the glmark2 handoff 2026-10-04 06:46:51 -04:00
swung0x48 710beefd02 [Docs] (Performance): glmark2 optimization handoff with the landed fixes, current numbers, remaining work and the bench scripts 2026-10-04 06:44:49 -04:00
Swung0x48 c1e957cb98 [CI] (Gates): retire the P3a/P4a untouched-region gates; those phases closed and later phases legitimately changed the regions 2026-10-04 06:30:24 -04:00
Swung0x48 eb71b669b3 [Fix] (Build): make the pull and verify builds compile again 2026-10-04 06:30:24 -04:00
swung0x48 bb31f374a3 [Perf] (DirectVulkan): keep the wire draw's render pass open across draws to the same attachments, closing it only for work that cannot sit inside one 2026-10-04 06:18:35 -04:00
swung0x48 e5ee31b022 [Build] (Anland): build the anland server for android-30, the APK's minSdk, so thread_local uses native TLS 2026-10-04 06:02:45 -04:00
swung0x48 915882b293 [Perf] (Server): time records only when the per-frame statistics are on 2026-10-04 06:02:44 -04:00
swung0x48 963f4d8177 [Perf] (DirectVulkan): reuse completed command buffers instead of freeing and allocating one per submission 2026-10-04 06:02:43 -04:00
swung0x48 d627fca387 [Perf] (Transport): the server rings a parked client only once the sequence it waits for is applied 2026-10-04 05:40:14 -04:00
swung0x48 2de0d6dd25 [Perf] (Pipe): reuse client-array buffers instead of a blocking create per draw, and copy contiguous ranges in one go 2026-10-04 04:54:54 -04:00
swung0x48 66b47a101b [Fix] (Remote): glFinish waits for the GPU, Magma bounds unsubmitted clear objects, ES contexts advertise OES_mapbuffer/rgb8_rgba8/depth24 2026-10-04 04:54:53 -04:00
swung0x48 4722fa38bb [Docs] (Performance): glmark2 baseline of Espryt and Magma against the stock kgsl stack on anland, with profiles and ranked bottlenecks 2026-10-04 04:33:24 -04:00
swung0x48 3a289222af [Docs] (MobileVA): plan a vendor-agnostic VA-API driver on Android MediaCodec, H.264 first 2026-10-03 23:46:13 -04:00
swung0x48 18a5dba0e4 [Docs] (Anland): Chrome decodes video in hardware through the YUV shared images, with probes to check it 2026-10-03 22:46:01 -04:00
swung0x48 7218bacb49 [Feature] (SharedImage): sample NV12/P010 dma-bufs through GL_OES_EGL_image_external on both backends, copying foreign buffers into server YUV images 2026-10-03 22:46:00 -04:00
swung0x48 4633ca223c [Fix] (Server): log the per-frame P65ServerFrame line only when MOBILEGL_SERVER_FRAME_STATS asks for it 2026-10-03 14:20:11 -04:00
swung0x48 0ef13ab0a6 [Docs] (Anland): Chrome decodes video in software because MobileGL cannot import the container VA-API driver's NV12 frames. 2026-10-03 14:16:04 -04:00
swung0x48 287e1df8dc [Fix] (Server, Magma, Espryt): end the session whose GPU work hangs the GPU so it cannot stall the desktop again. 2026-10-03 12:28:39 -04:00
159 changed files with 8118 additions and 666 deletions
@@ -126,8 +126,10 @@ timeout 25 mgrun offscreen glmark2-es2-wayland -s 1280x720 --run-forever > /tmp/
sleep 8; grep -a "presented through" /tmp/mgl-run.client.log
```
Chrome: `bash scripts/chrome-launch.sh <tag> [flags]` (log `/tmp/mgl-chrome-<tag>.client.log`, screenshot `ch-<tag>.png`; `CHROME_GPU=in-process` puts the GPU back in the browser, which cannot recover from a device loss).
Video: Chrome decodes in hardware. The GPU process's render node has a VA-API driver in the container (`/usr/lib/dri/msm_drm_drv_video.so`, DroidSpaces' V4L2 driver): Chrome allocates its frames with `vaCreateSurfaces` and exports them (`vaExportSurfaceHandle`, one fd, R8 + GR88 layers) - the driver's own buffers, never GBM ones - and imports each as an NV12 (P010 for 10-bit) EGLImage bound to `GL_TEXTURE_EXTERNAL_OES`. MobileGL takes such a FOREIGN YUV dma-buf through its labelled CPU-copy fallback: the server maps it and copies it into a YUV AHardwareBuffer of its own (`AHardwareBuffer_lockPlanes`) at the reader's first use in each frame (any present, publishing flush or native fence), then converts that into the texture's RGBA level on the GPU (Espryt: `GL_EXT_YUV_target` with the import's colour hints, `MOBILEGL_YUV_DRIVER_CONVERSION=1` for the driver's external sampler; Magma: `VkSamplerYcbcrConversion` on the AHB's external format). A server-allocated NV12/P010 buffer (GBM `gbm_bo_create(NV12)`) needs no copy. `MOBILEGL_CHROME_VIDEO_DECODE=software` in `mobilegl-startup.sh chrome` brings back `--disable-accelerated-video-decode`. Check a video with `scripts/probes/cdpmedia.py <port> <s>` in the container (CDP `Media.enable`: `kVideoDecoderName = VaapiVideoDecoder`, `kIsPlatformVideoDecoder = true`; `CDP_NAVIGATE=<url>` / `CDP_EXPR=<js>`), which needs Chrome started with `--remote-debugging-port` and a non-default `--user-data-dir` (a fresh profile also needs `--password-store=basic`, else page loads wait forever on KWallet), and grep Chrome's log for `exit_code` (a GPU-process restart). `scripts/probes/yuvprobe.c` pixel-checks foreign NV12/P010 imports, their per-frame refresh and a GBM NV12 buffer (`mgrun offscreen mgl-yuvprobe`, exit 0 = pass). KWin's own NV12 path (it splits a YUV buffer into R8 + GR88 imports) is not served: MobileGL imports no single-channel dma-bufs, so KWin advertises no YUV format over linux-dmabuf. A vendor-agnostic replacement driver (MobileVA: VA-API on Android MediaCodec, decoding into server YUV images with no CPU copy, the current driver kept as an A/B fallback) is planned, not built: `docs/MobileVA/plan.md`.
Device loss: `setprop debug.mobilegl.inject_device_lost_pid <pid>` (as root) loses the server session of the client whose pid - as it sees itself in the container - is `<pid>`, once per value, on any backend and whether or not it presents; `debug.mobilegl.inject_device_lost_at N` instead loses whichever session makes the N-th device check (Espryt presents and read-backs only). `bash scripts/chrome-recover.sh <in-process|process> [rounds]` checks that Chrome repaints after such a loss without a restart (battery-guarded, stops below 40%). `bash scripts/kwin-recover.sh [rounds]` loses KWin's own session (default twice) and checks that KWin restarts compositing on a fresh session - same KWin, plasmashell and client processes, the screen still animating (glmark2 on Wayland, glxgears on X11, `CHROME=1` adds Chrome) - which needs the anland backend + kwin.patch reset handling deployed (`kwin-stage.sh build`).
Probes: `scripts/probes/texfmt.c` (texture formats keep alpha?), `dmahash.c` (hash a process's dma-bufs); build in the container, e.g. `gcc -O1 /root/texfmt.c -o /usr/local/bin/mgl-texfmt -lEGL`, run with `mgrun offscreen`.
Probes: `scripts/probes/texfmt.c` (texture formats keep alpha?), `dmahash.c` (hash a process's dma-bufs), `yuvprobe.c` (YUV dma-buf sampling, `gcc -O1 /root/yuvprobe.c -o /usr/local/bin/mgl-yuvprobe -lEGL -lGLESv2 -lgbm -lm`), `cdpmedia.py` (which decoder a Chrome page uses); build in the container, e.g. `gcc -O1 /root/texfmt.c -o /usr/local/bin/mgl-texfmt -lEGL`, run with `mgrun offscreen`.
GPU hangs: a shader that never ends stops the WHOLE GPU (every process, MobileGL or not) until the kernel resets it - ~2 s, or ~10 s ("Preemption Fault" in dmesg) when a higher-priority context waits; nothing in user space shortens one stall. What the server does is end the guilty session so it cannot hang the GPU again (Magma: event-bracketed submissions + `mgl-gpu-watch`, budget `MOBILEGL_GPU_HANG_BUDGET_MS` / `debug.mobilegl.gpu_hang_budget_ms`, default 2000; Espryt: the driver's GUILTY reset status, which it reports after the ~2 s reset only). Repro: `scripts/probes/hangclient.c` (Wayland + GL 3.3 core; `-H 60 -N 1 -E 60` hangs every 60 frames, `-H -1` is a frame-gap observer; build with wayland-scanner's xdg-shell files, `-lwayland-client -lwayland-egl -lEGL -lOpenGL -lm`), and `gpuprobe.c` (NDK, plain adb shell; `gpuprobe 30 high` is a high-priority GLES context outside MobileGL - it both measures the device-wide stall and forces the 10 s preemption path).
A result counts only if ALL hold:
- client log says `presented through linux-dmabuf shared images` (not `wl_shm`; `could not be presented through a linux-dmabuf shared image` = fell back);
@@ -72,7 +72,7 @@ bash scripts/build-android.sh # -> <worktree>/build-android/libMobileGL
```
The script runs, with the NDK toolchain:
```
-DANDROID_ABI=arm64-v8a -DANDROID_PLATFORM=android-26 -DCMAKE_BUILD_TYPE=RelWithDebInfo
-DANDROID_ABI=arm64-v8a -DANDROID_PLATFORM=android-30 -DCMAKE_BUILD_TYPE=RelWithDebInfo
-DMOBILEGL_PIPE_PUSH=ON -DMOBILEGL_BUILD_DISAGGREGATED=ON -DMOBILEGL_BUILD_DISAGGREGATED_INPROC=ON
-DMOBILEGL_BUILD_TEST=OFF -DMOBILEGL_BUILD_BENCHMARK=OFF
```
@@ -4,7 +4,8 @@
# llvm-addr2line, it is what cyc.sh pushes). Stamp = worktree HEAD, so the container client
# (xbuild.sh, same stamp) and the server agree on the build ("compatible wire with different build"
# WARN otherwise). Env: MGL_WORKTREE, ANDROID_NDK, BUILD_DIR (default build-android), JOBS (12).
# Log level is compiled to INFO.
# Log level is compiled to INFO. android-30 = the APK's minSdk: thread_local is then the platform's
# own TLS rather than the emulated one (which cost 7-12% of an apply thread on the API 26 build).
set -euo pipefail
. "$(dirname "$0")/env.sh"
W=$MGL_WORKTREE
@@ -12,7 +13,7 @@ B=$W/${BUILD_DIR:-build-android}
STAMP=$(git -C "$W" rev-parse HEAD)
if [ ! -f "$B/build.ninja" ]; then
cmake -S "$W" -B "$B" -G Ninja -DCMAKE_TOOLCHAIN_FILE="$ANDROID_NDK/build/cmake/android.toolchain.cmake" \
-DANDROID_ABI=arm64-v8a -DANDROID_PLATFORM=android-26 -DCMAKE_BUILD_TYPE=RelWithDebInfo \
-DANDROID_ABI=arm64-v8a -DANDROID_PLATFORM=android-30 -DCMAKE_BUILD_TYPE=RelWithDebInfo \
-DMOBILEGL_PIPE_PUSH=ON -DMOBILEGL_BUILD_DISAGGREGATED=ON -DMOBILEGL_BUILD_DISAGGREGATED_INPROC=ON \
-DMOBILEGL_BUILD_TEST=OFF -DMOBILEGL_BUILD_BENCHMARK=OFF \
-DCMAKE_CXX_FLAGS=-DMOBILEGL_LOG_ACTIVE_LEVEL=MOBILEGL_LOG_LEVEL_INFO \
@@ -0,0 +1,94 @@
#!/usr/bin/env python3
# cdpmedia.py [port] [seconds] - ask a Chrome started with --remote-debugging-port=<port> (and a
# non-default --user-data-dir) which video decoder its first page's players use, and evaluate
# `window.__mglStats` / the element `#t` text of the page. Standard library only (no websocket
# module in the container): a minimal RFC 6455 client.
# python3 cdpmedia.py 9333 6
# Prints kVideoDecoderName / kIsPlatformVideoDecoder / kVideoTracks properties and media errors as
# they arrive, then the page's stats text.
import base64, json, os, socket, struct, sys, time, urllib.request
port = int(sys.argv[1]) if len(sys.argv) > 1 else 9333
seconds = float(sys.argv[2]) if len(sys.argv) > 2 else 6.0
targets = json.load(urllib.request.urlopen(f"http://127.0.0.1:{port}/json"))
page = next(t for t in targets if t.get("type") == "page")
url = page["webSocketDebuggerUrl"]
path = url.split(f"127.0.0.1:{port}", 1)[1]
sock = socket.create_connection(("127.0.0.1", port))
key = base64.b64encode(os.urandom(16)).decode()
sock.sendall((f"GET {path} HTTP/1.1\r\nHost: 127.0.0.1:{port}\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n"
f"Sec-WebSocket-Key: {key}\r\nSec-WebSocket-Version: 13\r\n\r\n").encode())
response = b""
while b"\r\n\r\n" not in response:
response += sock.recv(4096)
buffer = response.split(b"\r\n\r\n", 1)[1]
def send(message):
data = json.dumps(message).encode()
header = bytes([0x81])
mask = os.urandom(4)
if len(data) < 126:
header += bytes([0x80 | len(data)])
elif len(data) < 65536:
header += bytes([0x80 | 126]) + struct.pack(">H", len(data))
else:
header += bytes([0x80 | 127]) + struct.pack(">Q", len(data))
sock.sendall(header + mask + bytes(b ^ mask[i % 4] for i, b in enumerate(data)))
def recv_exact(n):
global buffer
while len(buffer) < n:
chunk = sock.recv(65536)
if not chunk:
raise EOFError
buffer += chunk
out, buffer = buffer[:n], buffer[n:]
return out
def recv():
first, second = recv_exact(2)
length = second & 0x7F
if length == 126:
length = struct.unpack(">H", recv_exact(2))[0]
elif length == 127:
length = struct.unpack(">Q", recv_exact(8))[0]
payload = recv_exact(length)
return json.loads(payload) if first & 0x0F == 1 else None
send({"id": 1, "method": "Media.enable"})
# Players created before the domain was enabled report nothing: reload so the page makes new ones.
if os.environ.get("CDP_NAVIGATE"):
send({"id": 3, "method": "Page.navigate", "params": {"url": os.environ["CDP_NAVIGATE"]}})
elif os.environ.get("CDP_RELOAD", "1") == "1":
send({"id": 3, "method": "Page.reload"})
deadline = time.time() + seconds
sock.settimeout(0.5)
seen = set()
while time.time() < deadline:
try:
message = recv()
except socket.timeout:
continue
if not message or "method" not in message:
continue
if message["method"] == "Media.playerPropertiesChanged":
for prop in message["params"]["properties"]:
if prop["name"] in ("kVideoDecoderName", "kIsPlatformVideoDecoder", "kVideoTracks", "kResolution"):
line = f"{prop['name']} = {prop['value']}"
if line not in seen:
seen.add(line)
print(line)
elif message["method"] in ("Media.playerErrorsRaised",):
print("media error:", json.dumps(message["params"])[:300])
send({"id": 2, "method": "Runtime.evaluate",
"params": {"expression": os.environ.get("CDP_EXPR", "(document.getElementById('t')||{}).textContent"), "returnByValue": True}})
sock.settimeout(3)
while True:
message = recv()
if message and message.get("id") == 2:
print("page:", message.get("result", {}).get("result", {}).get("value"))
break
@@ -0,0 +1,55 @@
// gpuprobe - a plain Android process (outside MobileGL) that measures whether the GPU keeps executing
// other contexts' work: a pbuffer GLES context clears + draws and glFinish()es every ~10 ms, printing any
// round trip over 50 ms with a CLOCK_REALTIME timestamp. Usage: gpuprobe <seconds> [priority: low|med|high]
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <GLES3/gl3.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#ifndef EGL_CONTEXT_PRIORITY_LEVEL_IMG
#define EGL_CONTEXT_PRIORITY_LEVEL_IMG 0x3100
#define EGL_CONTEXT_PRIORITY_HIGH_IMG 0x3101
#define EGL_CONTEXT_PRIORITY_MEDIUM_IMG 0x3102
#define EGL_CONTEXT_PRIORITY_LOW_IMG 0x3103
#endif
static double now_ms(void) { struct timespec ts; clock_gettime(CLOCK_REALTIME, &ts); return ts.tv_sec * 1e3 + ts.tv_nsec / 1e6; }
int main(int argc, char** argv) {
int seconds = argc > 1 ? atoi(argv[1]) : 20;
EGLint prio = EGL_CONTEXT_PRIORITY_MEDIUM_IMG;
if (argc > 2 && !strcmp(argv[2], "high")) prio = EGL_CONTEXT_PRIORITY_HIGH_IMG;
if (argc > 2 && !strcmp(argv[2], "low")) prio = EGL_CONTEXT_PRIORITY_LOW_IMG;
setvbuf(stdout, NULL, _IOLBF, 0);
EGLDisplay d = eglGetDisplay(EGL_DEFAULT_DISPLAY);
eglInitialize(d, NULL, NULL);
const EGLint ca[] = {EGL_SURFACE_TYPE, EGL_PBUFFER_BIT, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT, EGL_RED_SIZE, 8, EGL_NONE};
EGLConfig c; EGLint n;
eglChooseConfig(d, ca, &c, 1, &n);
const EGLint pa[] = {EGL_WIDTH, 256, EGL_HEIGHT, 256, EGL_NONE};
EGLSurface s = eglCreatePbufferSurface(d, c, pa);
const EGLint xa[] = {EGL_CONTEXT_CLIENT_VERSION, 3, EGL_CONTEXT_PRIORITY_LEVEL_IMG, prio, EGL_NONE};
EGLContext x = eglCreateContext(d, c, EGL_NO_CONTEXT, xa);
if (x == EGL_NO_CONTEXT) { printf("no ctx 0x%x\n", eglGetError()); return 1; }
eglMakeCurrent(d, s, s, x);
EGLint got = 0; eglQueryContext(d, x, EGL_CONTEXT_PRIORITY_LEVEL_IMG, &got);
const double t0 = now_ms(); double maxRt = 0; long iters = 0, slow = 0;
printf("[probe] start %.0f prio=0x%x\n", t0, got);
while (now_ms() - t0 < seconds * 1000.0) {
const double a = now_ms();
glClearColor((iters & 1) ? 1.f : 0.f, 0.f, 0.f, 1.f);
glClear(GL_COLOR_BUFFER_BIT);
glFinish();
const double rt = now_ms() - a;
if (rt > maxRt) maxRt = rt;
if (rt > 50) { ++slow; printf("[probe] glFinish %.0f ms ending at %.0f\n", rt, now_ms()); }
++iters;
usleep(10000);
}
printf("[probe] END iters=%ld max=%.0f ms slow=%ld\n", iters, maxRt, slow);
return 0;
}
@@ -0,0 +1,150 @@
// hangclient - a Wayland xdg-toplevel, EGL + desktop GL 3.3 core client that animates and, on request,
// submits fragment work that never ends (the driver has to reset it). Prints per-frame gaps so the same
// binary measures "other app" frame pacing (frame callbacks come from the compositor) when run with -H -1.
//
// hangclient -T tag [-H hang_at_frame] [-N hang_repeat] [-I iters] [-S seconds] [-W w] [-X h] [-F]
// -H n frame at which the hanging draw is submitted (-1: never; default -1)
// -N k how many consecutive hanging frames (default 1)
// -E e re-arm a hang every e frames after the first (0: once)
// -I i loop count of the hanging shader (default 2^31-1)
// -S s run time in seconds (default 30)
// -F glFinish after every frame (puts the wait inside the client's own session)
#define GL_GLEXT_PROTOTYPES 1
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <GL/glcorearb.h>
#include <wayland-client.h>
#include <wayland-egl.h>
#include "xdg-shell-client-protocol.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
static struct wl_compositor* g_comp;
static struct xdg_wm_base* g_wm;
static int g_configured, g_closed;
static int g_w = 480, g_h = 360;
static void reg_global(void* d, struct wl_registry* r, uint32_t name, const char* iface, uint32_t ver) {
(void)d;
if (!strcmp(iface, "wl_compositor")) g_comp = wl_registry_bind(r, name, &wl_compositor_interface, 4);
else if (!strcmp(iface, "xdg_wm_base")) g_wm = wl_registry_bind(r, name, &xdg_wm_base_interface, 1);
}
static void reg_remove(void* d, struct wl_registry* r, uint32_t n) { (void)d; (void)r; (void)n; }
static const struct wl_registry_listener reg_l = {reg_global, reg_remove};
static void wm_ping(void* d, struct xdg_wm_base* wm, uint32_t s) { (void)d; xdg_wm_base_pong(wm, s); }
static const struct xdg_wm_base_listener wm_l = {wm_ping};
static void xs_conf(void* d, struct xdg_surface* s, uint32_t serial) { (void)d; xdg_surface_ack_configure(s, serial); g_configured = 1; }
static const struct xdg_surface_listener xs_l = {xs_conf};
static void tl_conf(void* d, struct xdg_toplevel* t, int32_t w, int32_t h, struct wl_array* st) { (void)d; (void)t; (void)st; (void)w; (void)h; }
static void tl_close(void* d, struct xdg_toplevel* t) { (void)d; (void)t; g_closed = 1; }
static const struct xdg_toplevel_listener tl_l = {tl_conf, tl_close};
static double now_ms(void) { struct timespec ts; clock_gettime(CLOCK_REALTIME, &ts); return ts.tv_sec * 1e3 + ts.tv_nsec / 1e6; }
static GLuint sh(GLenum type, const char* src) {
GLuint s = glCreateShader(type);
glShaderSource(s, 1, &src, NULL);
glCompileShader(s);
GLint ok = 0; glGetShaderiv(s, GL_COMPILE_STATUS, &ok);
if (!ok) { char log[2048]; glGetShaderInfoLog(s, sizeof log, NULL, log); fprintf(stderr, "shader: %s\n", log); exit(2); }
return s;
}
int main(int argc, char** argv) {
const char* tag = "hc"; int hangAt = -1, hangN = 1, every = 0, seconds = 30, finish = 0; long iters = 2147483647L;
for (int i = 1; i < argc; ++i) {
if (!strcmp(argv[i], "-T") && i + 1 < argc) tag = argv[++i];
else if (!strcmp(argv[i], "-H") && i + 1 < argc) hangAt = atoi(argv[++i]);
else if (!strcmp(argv[i], "-N") && i + 1 < argc) hangN = atoi(argv[++i]);
else if (!strcmp(argv[i], "-E") && i + 1 < argc) every = atoi(argv[++i]);
else if (!strcmp(argv[i], "-I") && i + 1 < argc) iters = atol(argv[++i]);
else if (!strcmp(argv[i], "-S") && i + 1 < argc) seconds = atoi(argv[++i]);
else if (!strcmp(argv[i], "-W") && i + 1 < argc) g_w = atoi(argv[++i]);
else if (!strcmp(argv[i], "-X") && i + 1 < argc) g_h = atoi(argv[++i]);
else if (!strcmp(argv[i], "-F")) finish = 1;
}
setvbuf(stdout, NULL, _IOLBF, 0);
struct wl_display* dpy = wl_display_connect(NULL);
if (!dpy) { fprintf(stderr, "no wayland\n"); return 1; }
struct wl_registry* reg = wl_display_get_registry(dpy);
wl_registry_add_listener(reg, &reg_l, NULL);
wl_display_roundtrip(dpy);
if (!g_comp || !g_wm) { fprintf(stderr, "no compositor/xdg_wm_base\n"); return 1; }
xdg_wm_base_add_listener(g_wm, &wm_l, NULL);
struct wl_surface* surf = wl_compositor_create_surface(g_comp);
struct xdg_surface* xs = xdg_wm_base_get_xdg_surface(g_wm, surf);
xdg_surface_add_listener(xs, &xs_l, NULL);
struct xdg_toplevel* tl = xdg_surface_get_toplevel(xs);
xdg_toplevel_add_listener(tl, &tl_l, NULL);
xdg_toplevel_set_title(tl, tag);
wl_surface_commit(surf);
while (!g_configured) wl_display_dispatch(dpy);
EGLDisplay ed = eglGetPlatformDisplay(EGL_PLATFORM_WAYLAND_KHR, dpy, NULL);
if (!eglInitialize(ed, NULL, NULL)) { fprintf(stderr, "eglInitialize\n"); return 1; }
eglBindAPI(EGL_OPENGL_API);
const EGLint ca[] = {EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_NONE};
EGLConfig cfg; EGLint n = 0;
if (!eglChooseConfig(ed, ca, &cfg, 1, &n) || n < 1) { fprintf(stderr, "no config\n"); return 1; }
const EGLint xa[] = {EGL_CONTEXT_MAJOR_VERSION, 3, EGL_CONTEXT_MINOR_VERSION, 3, EGL_CONTEXT_OPENGL_PROFILE_MASK, EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT, EGL_NONE};
EGLContext ctx = eglCreateContext(ed, cfg, EGL_NO_CONTEXT, xa);
if (ctx == EGL_NO_CONTEXT) { fprintf(stderr, "no context 0x%x\n", eglGetError()); return 1; }
struct wl_egl_window* ew = wl_egl_window_create(surf, g_w, g_h);
EGLSurface es = eglCreatePlatformWindowSurface(ed, cfg, ew, NULL);
eglMakeCurrent(ed, es, es, ctx);
eglSwapInterval(ed, 1);
const char* vs = "#version 330 core\nlayout(location=0) in vec2 p; uniform float t; out vec2 uv;\n"
"void main(){ uv=p*0.5+0.5; gl_Position=vec4(p*0.4+vec2(0.5*sin(t),0.0),0.0,1.0);}";
const char* fs = "#version 330 core\nin vec2 uv; uniform int iters; uniform float t; out vec4 c;\n"
"void main(){ float a=uv.x; for(int i=0;i<iters;++i){ a=fract(a*1.0001+0.37+uv.y); }\n"
" c=vec4(uv, 0.5+0.5*sin(t)+a*1e-6, 1.0);}";
GLuint prog = glCreateProgram();
glAttachShader(prog, sh(GL_VERTEX_SHADER, vs));
glAttachShader(prog, sh(GL_FRAGMENT_SHADER, fs));
glLinkProgram(prog);
GLint ok = 0; glGetProgramiv(prog, GL_LINK_STATUS, &ok);
if (!ok) { fprintf(stderr, "link failed\n"); return 2; }
GLint uIt = glGetUniformLocation(prog, "iters"), uT = glGetUniformLocation(prog, "t");
GLuint vao, vbo; glGenVertexArrays(1, &vao); glBindVertexArray(vao);
const float q[] = {-1, -1, 1, -1, -1, 1, 1, 1};
glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, sizeof q, q, GL_STATIC_DRAW);
glEnableVertexAttribArray(0); glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
const double t0 = now_ms(); double last = t0, maxGap = 0; long frames = 0, gaps100 = 0;
printf("[%s] start %.0f renderer=%s hangAt=%d N=%d every=%d iters=%ld\n", tag, t0, glGetString(GL_RENDERER), hangAt, hangN, every, iters);
while (!g_closed && now_ms() - t0 < seconds * 1000.0) {
wl_display_dispatch_pending(dpy);
const double t = (now_ms() - t0) / 1000.0;
int hang = 0;
if (hangAt >= 0 && frames >= hangAt) {
long rel = frames - hangAt;
if (every > 0) rel %= every;
hang = rel < hangN;
}
glViewport(0, 0, g_w, g_h);
glClearColor(0.5f + 0.5f * (float)sin(t * 3.0), 0.2f, 0.4f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(prog);
glUniform1i(uIt, hang ? (int)iters : 0);
glUniform1f(uT, (float)t);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
if (hang) printf("[%s] HANG submitted frame %ld at %.0f\n", tag, frames, now_ms());
if (finish) glFinish();
if (!eglSwapBuffers(ed, es)) printf("[%s] swap failed 0x%x at %.0f\n", tag, eglGetError(), now_ms());
const GLenum rs = glGetError();
if (rs != GL_NO_ERROR) printf("[%s] glGetError 0x%x at %.0f\n", tag, rs, now_ms());
const double nowv = now_ms(), gap = nowv - last;
last = nowv; ++frames;
if (gap > maxGap) maxGap = gap;
if (gap > 100.0) { ++gaps100; printf("[%s] GAP %.0f ms ending at %.0f (frame %ld)\n", tag, gap, nowv, frames); }
if (frames % 120 == 0) printf("[%s] frame %ld t=%.1fs fps=%.1f\n", tag, frames, (nowv - t0) / 1e3, frames * 1e3 / (nowv - t0));
}
const double total = now_ms() - t0;
printf("[%s] END frames=%ld fps=%.1f maxGap=%.0f ms gaps>100ms=%ld\n", tag, frames, frames * 1e3 / total, maxGap, gaps100);
return 0;
}
@@ -0,0 +1,410 @@
// yuvprobe.c - pixel check of YUV dma-buf sampling through MobileGL (GL_OES_EGL_image_external).
//
// gcc -O1 yuvprobe.c -o /usr/local/bin/mgl-yuvprobe -lEGL -lGLESv2 -lgbm
// mgrun offscreen mgl-yuvprobe # exit 0 = every check passed
//
// What it does, in an ES 3.0 context on a pbuffer:
// 1. FOREIGN NV12 (and P010): a dma-buf from /dev/dma_heap/system - a buffer the MobileGL server
// did not allocate, as a video decoder's would be - with padded pitches and a gap before the
// chroma plane, filled with four vertical bars (red, green, blue, white) in BT.709 narrow
// range. Imported with eglCreateImage(EGL_LINUX_DMA_BUF_EXT) and its colour hints, bound to
// GL_TEXTURE_EXTERNAL_OES, drawn through a samplerExternalOES shader into an RGBA8 FBO and read
// back: each bar must come out its colour. The buffer is then rewritten with the bars in
// reverse order and, after a native fence (what ends a frame for an offscreen producer), drawn
// again: the new order must show (the server refreshes a foreign buffer at each frame).
// 2. GBM NV12: gbm_bo_create(NV12) through the mobilegl GBM backend (a server-allocated YUV
// buffer, zero-copy), its planes printed, imported and sampled once (content undefined; the
// check is that every call succeeds).
// Also prints the dma-buf formats/modifiers EGL lists for NV12/P010.
#define _GNU_SOURCE
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <GLES3/gl3.h>
#include <GLES2/gl2ext.h>
#include <fcntl.h>
#include <gbm.h>
#include <linux/dma-buf.h>
#include <linux/dma-heap.h>
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <unistd.h>
#ifndef DRM_FORMAT_NV12
#define DRM_FORMAT_NV12 0x3231564E
#endif
#ifndef DRM_FORMAT_P010
#define DRM_FORMAT_P010 0x30313050
#endif
static int g_failures = 0;
#define CHECK(cond, ...) \
do { \
if (!(cond)) { \
printf("FAIL: " __VA_ARGS__); \
printf("\n"); \
++g_failures; \
} \
} while (0)
static PFNEGLCREATEIMAGEKHRPROC pCreateImage;
static PFNEGLDESTROYIMAGEKHRPROC pDestroyImage;
static PFNGLEGLIMAGETARGETTEXTURE2DOESPROC pTargetTexture;
static PFNEGLQUERYDMABUFFORMATSEXTPROC pQueryFormats;
static PFNEGLQUERYDMABUFMODIFIERSEXTPROC pQueryModifiers;
static PFNEGLCREATESYNCKHRPROC pCreateSync;
static PFNEGLDESTROYSYNCKHRPROC pDestroySync;
static EGLDisplay g_display;
enum { W = 256, H = 128, BARS = 4 };
static const unsigned char kBars[BARS][3] = {{255, 0, 0}, {0, 255, 0}, {0, 0, 255}, {255, 255, 255}};
// RGB -> BT.709 narrow-range codes at `bits` (8 or 10).
static void Encode709(const unsigned char rgb[3], int bits, int out[3]) {
const double kr = 0.2126, kb = 0.0722, kg = 1.0 - kr - kb;
const double r = rgb[0] / 255.0, g = rgb[1] / 255.0, b = rgb[2] / 255.0;
const double y = kr * r + kg * g + kb * b;
const double cb = (b - y) / (2.0 * (1.0 - kb)), cr = (r - y) / (2.0 * (1.0 - kr));
const double scale = (double)(1 << (bits - 8));
out[0] = (int)lround((16.0 + 219.0 * y) * scale);
out[1] = (int)lround((128.0 + 224.0 * cb) * scale);
out[2] = (int)lround((128.0 + 224.0 * cr) * scale);
}
// Writes the bars (`reverse`d or not) into a semi-planar buffer: 1 byte samples, or P010's 16-bit
// samples with the 10 significant bits high.
static void FillBars(uint8_t* base, uint32_t pitchY, uint32_t offsetC, uint32_t pitchC, int bytes, int reverse) {
const int bits = bytes == 1 ? 8 : 10;
for (int y = 0; y < H; ++y) {
for (int x = 0; x < W; ++x) {
int bar = x * BARS / W;
if (reverse) bar = BARS - 1 - bar;
int code[3];
Encode709(kBars[bar], bits, code);
uint8_t* py = base + (size_t)y * pitchY + (size_t)x * bytes;
if (bytes == 1) py[0] = (uint8_t)code[0];
else *(uint16_t*)py = (uint16_t)(code[0] << 6);
if ((x & 1) == 0 && (y & 1) == 0) {
uint8_t* pc = base + offsetC + (size_t)(y / 2) * pitchC + (size_t)(x / 2) * 2 * bytes;
if (bytes == 1) {
pc[0] = (uint8_t)code[1];
pc[1] = (uint8_t)code[2];
} else {
((uint16_t*)pc)[0] = (uint16_t)(code[1] << 6);
((uint16_t*)pc)[1] = (uint16_t)(code[2] << 6);
}
}
}
}
}
static GLuint Compile(GLenum type, const char* source) {
GLuint shader = glCreateShader(type);
glShaderSource(shader, 1, &source, NULL);
glCompileShader(shader);
GLint ok = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &ok);
if (!ok) {
char log[2048];
glGetShaderInfoLog(shader, sizeof(log), NULL, log);
printf("shader: %s\n", log);
}
return shader;
}
static GLuint g_program, g_fbo, g_rgba;
static void SetUpDraw(void) {
const char* vs = "#version 300 es\n"
"out vec2 uv;\n"
"void main() {\n"
" vec2 p = vec2(float((gl_VertexID << 1) & 2), float(gl_VertexID & 2));\n"
" gl_Position = vec4(p * 2.0 - 1.0, 0.0, 1.0);\n"
" uv = p;\n"
"}\n";
const char* fs = "#version 300 es\n"
"#extension GL_OES_EGL_image_external_essl3 : require\n"
"precision mediump float;\n"
"uniform samplerExternalOES tex;\n"
"in vec2 uv;\n"
"out vec4 color;\n"
"void main() { color = texture(tex, uv); }\n";
g_program = glCreateProgram();
glAttachShader(g_program, Compile(GL_VERTEX_SHADER, vs));
glAttachShader(g_program, Compile(GL_FRAGMENT_SHADER, fs));
glLinkProgram(g_program);
GLint linked = 0;
glGetProgramiv(g_program, GL_LINK_STATUS, &linked);
CHECK(linked, "the samplerExternalOES program did not link");
GLint type = 0, size = 0;
char name[64];
glGetActiveUniform(g_program, 0, sizeof(name), NULL, &size, (GLenum*)&type, name);
printf("uniform 0: %s type 0x%04x\n", name, type);
CHECK(type == GL_SAMPLER_EXTERNAL_OES, "the sampler reports type 0x%04x, not GL_SAMPLER_EXTERNAL_OES", type);
glGenTextures(1, &g_rgba);
glBindTexture(GL_TEXTURE_2D, g_rgba);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, W, H);
glGenFramebuffers(1, &g_fbo);
glBindFramebuffer(GL_FRAMEBUFFER, g_fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, g_rgba, 0);
CHECK(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE, "the readback FBO is incomplete");
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
}
// Draws the external texture `texture` and checks each bar's center (row `y` of the image).
static void DrawAndCheck(GLuint texture, const char* what, int reverse, int tolerance) {
glBindFramebuffer(GL_FRAMEBUFFER, g_fbo);
glViewport(0, 0, W, H);
glUseProgram(g_program);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture);
glUniform1i(glGetUniformLocation(g_program, "tex"), 0);
glDrawArrays(GL_TRIANGLES, 0, 3);
static unsigned char pixels[W * H * 4];
glReadPixels(0, 0, W, H, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
GLenum error = glGetError();
CHECK(error == GL_NO_ERROR, "%s: GL error 0x%04x", what, error);
if (tolerance < 0) {
printf("%s: center pixel %d %d %d %d (content undefined)\n", what, pixels[(H / 2 * W + W / 2) * 4],
pixels[(H / 2 * W + W / 2) * 4 + 1], pixels[(H / 2 * W + W / 2) * 4 + 2],
pixels[(H / 2 * W + W / 2) * 4 + 3]);
return;
}
// Rows: GL row 0 is the image's first row (the uv mapping above), and the bars are vertical,
// so any row does; take two to catch a broken chroma row pitch.
const int rows[2] = {H / 4, (3 * H) / 4};
for (int r = 0; r < 2; ++r) {
for (int bar = 0; bar < BARS; ++bar) {
const int x = bar * (W / BARS) + W / (2 * BARS);
const unsigned char* p = &pixels[(rows[r] * W + x) * 4];
const unsigned char* want = kBars[reverse ? BARS - 1 - bar : bar];
int worst = 0;
for (int c = 0; c < 3; ++c) {
int d = abs((int)p[c] - (int)want[c]);
if (d > worst) worst = d;
}
printf("%s: row %d bar %d -> %3d %3d %3d %3d (want %3d %3d %3d)%s\n", what, rows[r], bar, p[0], p[1], p[2],
p[3], want[0], want[1], want[2], worst > tolerance ? " <-- off" : "");
CHECK(worst <= tolerance, "%s: bar %d is off by %d", what, bar, worst);
}
}
}
static int AllocateDmaHeap(size_t size) {
int heap = open("/dev/dma_heap/system", O_RDWR | O_CLOEXEC);
if (heap < 0) {
perror("open /dev/dma_heap/system");
return -1;
}
struct dma_heap_allocation_data data = {.len = size, .fd_flags = O_RDWR | O_CLOEXEC};
if (ioctl(heap, DMA_HEAP_IOCTL_ALLOC, &data) != 0) {
perror("DMA_HEAP_IOCTL_ALLOC");
close(heap);
return -1;
}
close(heap);
return (int)data.fd;
}
static void Sync(int fd, int start) {
struct dma_buf_sync sync = {.flags = DMA_BUF_SYNC_WRITE | (start ? DMA_BUF_SYNC_START : DMA_BUF_SYNC_END)};
ioctl(fd, DMA_BUF_IOCTL_SYNC, &sync);
}
static EGLImageKHR Import(int fd, uint32_t fourcc, uint32_t pitchY, uint32_t offsetC, uint32_t pitchC, int modifier) {
EGLint attribs[64];
int n = 0;
#define A(k, v) (attribs[n++] = (k), attribs[n++] = (EGLint)(v))
A(EGL_WIDTH, W);
A(EGL_HEIGHT, H);
A(EGL_LINUX_DRM_FOURCC_EXT, fourcc);
A(EGL_DMA_BUF_PLANE0_FD_EXT, fd);
A(EGL_DMA_BUF_PLANE0_OFFSET_EXT, 0);
A(EGL_DMA_BUF_PLANE0_PITCH_EXT, pitchY);
A(EGL_DMA_BUF_PLANE1_FD_EXT, fd);
A(EGL_DMA_BUF_PLANE1_OFFSET_EXT, offsetC);
A(EGL_DMA_BUF_PLANE1_PITCH_EXT, pitchC);
if (modifier) {
A(EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT, 0);
A(EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT, 0);
A(EGL_DMA_BUF_PLANE1_MODIFIER_LO_EXT, 0);
A(EGL_DMA_BUF_PLANE1_MODIFIER_HI_EXT, 0);
}
A(EGL_YUV_COLOR_SPACE_HINT_EXT, EGL_ITU_REC709_EXT);
A(EGL_SAMPLE_RANGE_HINT_EXT, EGL_YUV_NARROW_RANGE_EXT);
attribs[n] = EGL_NONE;
#undef A
EGLImageKHR image = pCreateImage(g_display, EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT, NULL, attribs);
if (image == EGL_NO_IMAGE_KHR) printf("eglCreateImage failed: 0x%04x\n", eglGetError());
return image;
}
static GLuint ExternalTexture(EGLImageKHR image) {
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture);
pTargetTexture(GL_TEXTURE_EXTERNAL_OES, image);
GLenum error = glGetError();
CHECK(error == GL_NO_ERROR, "glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES) -> 0x%04x", error);
GLint bound = 0;
glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &bound);
CHECK((GLuint)bound == texture, "GL_TEXTURE_BINDING_EXTERNAL_OES reads %d", bound);
return texture;
}
// A frame boundary as an offscreen producer makes one: a native fence.
static void EndFrame(void) {
EGLint attribs[] = {EGL_NONE};
EGLSyncKHR sync = pCreateSync(g_display, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
CHECK(sync != EGL_NO_SYNC_KHR, "eglCreateSync(native fence) failed");
if (sync != EGL_NO_SYNC_KHR) pDestroySync(g_display, sync);
}
static void ForeignTest(uint32_t fourcc, int bytes, int tolerance) {
const char* what = fourcc == DRM_FORMAT_NV12 ? "foreign NV12" : "foreign P010";
// Padded pitches and a gap before the chroma plane, as decoders lay buffers out.
const uint32_t pitchY = W * bytes + 64, pitchC = W * bytes + 128;
const uint32_t offsetC = pitchY * (H + 16);
const size_t size = offsetC + (size_t)pitchC * (H / 2) + 4096;
int fd = AllocateDmaHeap(size);
if (fd < 0) {
CHECK(0, "%s: no dma-buf to test with", what);
return;
}
uint8_t* map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
CHECK(map != MAP_FAILED, "%s: mmap failed", what);
if (map == MAP_FAILED) return;
Sync(fd, 1);
FillBars(map, pitchY, offsetC, pitchC, bytes, 0);
Sync(fd, 0);
EGLImageKHR image = Import(fd, fourcc, pitchY, offsetC, pitchC, fourcc == DRM_FORMAT_NV12);
CHECK(image != EGL_NO_IMAGE_KHR, "%s: import refused", what);
if (image == EGL_NO_IMAGE_KHR) return;
GLuint texture = ExternalTexture(image);
DrawAndCheck(texture, what, 0, tolerance);
EndFrame();
// New content, same buffer: shows after the frame boundary.
Sync(fd, 1);
FillBars(map, pitchY, offsetC, pitchC, bytes, 1);
Sync(fd, 0);
EndFrame();
DrawAndCheck(texture, what, 1, tolerance);
glDeleteTextures(1, &texture);
pDestroyImage(g_display, image);
munmap(map, size);
close(fd);
}
static void GbmTest(void) {
int node = open("/dev/dri/renderD128", O_RDWR | O_CLOEXEC);
if (node < 0) node = open("/dev/null", O_RDWR | O_CLOEXEC);
struct gbm_device* gbm = gbm_create_device(node);
if (gbm == NULL) {
CHECK(0, "gbm_create_device failed");
return;
}
printf("gbm backend: %s\n", gbm_device_get_backend_name(gbm));
const int supported = gbm_device_is_format_supported(gbm, DRM_FORMAT_NV12, 0);
printf("gbm NV12 supported: %d, P010: %d\n", supported,
gbm_device_is_format_supported(gbm, DRM_FORMAT_P010, 0));
CHECK(supported == 1, "gbm does not support NV12");
struct gbm_bo* bo = gbm_bo_create(gbm, W, H, DRM_FORMAT_NV12, 0);
CHECK(bo != NULL, "gbm_bo_create(NV12) failed");
if (bo != NULL) {
const int planes = gbm_bo_get_plane_count(bo);
printf("gbm NV12 bo: planes %d, stride %u/%u, offset %u/%u, modifier 0x%llx\n", planes,
gbm_bo_get_stride_for_plane(bo, 0), gbm_bo_get_stride_for_plane(bo, 1), gbm_bo_get_offset(bo, 0),
gbm_bo_get_offset(bo, 1), (unsigned long long)gbm_bo_get_modifier(bo));
CHECK(planes == 2, "the NV12 bo has %d planes", planes);
int fd = gbm_bo_get_fd(bo);
EGLImageKHR image =
Import(fd, DRM_FORMAT_NV12, gbm_bo_get_stride_for_plane(bo, 0), gbm_bo_get_offset(bo, 1),
gbm_bo_get_stride_for_plane(bo, 1), 0);
CHECK(image != EGL_NO_IMAGE_KHR, "the server's own NV12 buffer was not imported");
if (image != EGL_NO_IMAGE_KHR) {
GLuint texture = ExternalTexture(image);
DrawAndCheck(texture, "gbm NV12", 0, -1);
glDeleteTextures(1, &texture);
pDestroyImage(g_display, image);
}
close(fd);
gbm_bo_destroy(bo);
}
gbm_device_destroy(gbm);
close(node);
}
int main(void) {
setvbuf(stdout, NULL, _IONBF, 0);
g_display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
EGLint major, minor;
if (!eglInitialize(g_display, &major, &minor)) {
printf("FAIL: eglInitialize\n");
return 2;
}
const char* eglExtensions = eglQueryString(g_display, EGL_EXTENSIONS);
printf("EGL %d.%d dma_buf_import: %s\n", major, minor,
strstr(eglExtensions, "EGL_EXT_image_dma_buf_import") ? "yes" : "no");
eglBindAPI(EGL_OPENGL_ES_API);
const EGLint configAttribs[] = {EGL_SURFACE_TYPE, EGL_PBUFFER_BIT, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT,
EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_NONE};
EGLConfig config;
EGLint count = 0;
eglChooseConfig(g_display, configAttribs, &config, 1, &count);
if (count < 1) {
printf("FAIL: no config\n");
return 2;
}
const EGLint pbufferAttribs[] = {EGL_WIDTH, 16, EGL_HEIGHT, 16, EGL_NONE};
EGLSurface surface = eglCreatePbufferSurface(g_display, config, pbufferAttribs);
const EGLint contextAttribs[] = {EGL_CONTEXT_MAJOR_VERSION, 3, EGL_NONE};
EGLContext context = eglCreateContext(g_display, config, EGL_NO_CONTEXT, contextAttribs);
if (context == EGL_NO_CONTEXT || !eglMakeCurrent(g_display, surface, surface, context)) {
printf("FAIL: no ES 3 context\n");
return 2;
}
pCreateImage = (PFNEGLCREATEIMAGEKHRPROC)eglGetProcAddress("eglCreateImageKHR");
pDestroyImage = (PFNEGLDESTROYIMAGEKHRPROC)eglGetProcAddress("eglDestroyImageKHR");
pTargetTexture = (PFNGLEGLIMAGETARGETTEXTURE2DOESPROC)eglGetProcAddress("glEGLImageTargetTexture2DOES");
pQueryFormats = (PFNEGLQUERYDMABUFFORMATSEXTPROC)eglGetProcAddress("eglQueryDmaBufFormatsEXT");
pQueryModifiers = (PFNEGLQUERYDMABUFMODIFIERSEXTPROC)eglGetProcAddress("eglQueryDmaBufModifiersEXT");
pCreateSync = (PFNEGLCREATESYNCKHRPROC)eglGetProcAddress("eglCreateSyncKHR");
pDestroySync = (PFNEGLDESTROYSYNCKHRPROC)eglGetProcAddress("eglDestroySyncKHR");
printf("GL_RENDERER: %s\n", glGetString(GL_RENDERER));
const char* glExtensions = (const char*)glGetString(GL_EXTENSIONS);
const int external = strstr(glExtensions, "GL_OES_EGL_image_external_essl3") != NULL;
printf("GL_OES_EGL_image_external_essl3: %s\n", external ? "yes" : "no");
CHECK(external, "GL_OES_EGL_image_external_essl3 is not advertised");
EGLint formats[32], formatCount = 0;
pQueryFormats(g_display, 32, formats, &formatCount);
int nv12 = 0;
for (int i = 0; i < formatCount; ++i) {
printf("dma-buf format %.4s", (const char*)&formats[i]);
EGLuint64KHR modifiers[4];
EGLBoolean externalOnly[4];
EGLint modifierCount = 0;
pQueryModifiers(g_display, formats[i], 4, modifiers, externalOnly, &modifierCount);
for (int m = 0; m < modifierCount; ++m)
printf(" [modifier 0x%llx%s]", (unsigned long long)modifiers[m], externalOnly[m] ? " external-only" : "");
printf("\n");
nv12 |= formats[i] == (EGLint)DRM_FORMAT_NV12;
}
CHECK(nv12, "NV12 is not a listed dma-buf format");
SetUpDraw();
ForeignTest(DRM_FORMAT_NV12, 1, 12);
ForeignTest(DRM_FORMAT_P010, 2, 16);
GbmTest();
printf("%s (%d failure(s))\n", g_failures ? "FAILED" : "PASSED", g_failures);
eglMakeCurrent(g_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
eglTerminate(g_display);
return g_failures ? 1 : 0;
}
+7 -3
View File
@@ -18,6 +18,10 @@ on:
- '**/*.md'
workflow_dispatch:
env:
# Software-rendered CI submissions may exceed the interactive GPU hang budget.
MOBILEGL_GPU_HANG_BUDGET_MS: "0"
# ONE LIVE RUN PER BRANCH, as in test.yml: a superseded push is cancelled rather than left queued
# behind its successor; a manual dispatch gets a group of its own; dev queues rather than cancels.
concurrency:
@@ -477,7 +481,7 @@ jobs:
extra_retrace_args=()
if [ "${MOBILEGL_CI_TRANSPORT}" = "inproc" ]; then
extra_retrace_args+=(
--env "MOBILEGL_TRANSPORT=inproc;MOBILEGL_IPC_ROLE_SPLIT_STATE=1;MOBILEGL_IPC_STRICT_ERRORS=1;MOBILEGL_IPC_RUN_AHEAD=1"
--env "MOBILEGL_TRANSPORT=inproc;MOBILEGL_IPC_ROLE_SPLIT_STATE=1;MOBILEGL_IPC_STRICT_ERRORS=1;MOBILEGL_IPC_RUN_AHEAD=1;MOBILEGL_GPU_HANG_BUDGET_MS=${MOBILEGL_GPU_HANG_BUDGET_MS}"
--require-inproc
)
elif [ "${MOBILEGL_CI_TRANSPORT}" = "spawn" ]; then
@@ -493,12 +497,12 @@ jobs:
# into the CLIENT's process and SetWindowHandle is refused on the way across with
# Fatal{UnmigratedSurface, "AndroidNativeWindow@P12"} (Rule H).
extra_retrace_args+=(
--env "MOBILEGL_TRANSPORT=spawn;MOBILEGL_IPC_ROLE_SPLIT_STATE=1;MOBILEGL_IPC_STRICT_ERRORS=1;MOBILEGL_IPC_RUN_AHEAD=1"
--env "MOBILEGL_TRANSPORT=spawn;MOBILEGL_IPC_ROLE_SPLIT_STATE=1;MOBILEGL_IPC_STRICT_ERRORS=1;MOBILEGL_IPC_RUN_AHEAD=1;MOBILEGL_GPU_HANG_BUDGET_MS=${MOBILEGL_GPU_HANG_BUDGET_MS}"
--use-pbuffer
--require-spawn
)
else
extra_retrace_args+=(--env "MOBILEGL_TRANSPORT=monolith")
extra_retrace_args+=(--env "MOBILEGL_TRANSPORT=monolith;MOBILEGL_GPU_HANG_BUDGET_MS=${MOBILEGL_GPU_HANG_BUDGET_MS}")
fi
# Bliss needs the newer signed ANGLE variant plus sampler mipmap
# min-filter downgrading on ANGLE llvmpipe.
+35 -90
View File
@@ -21,6 +21,10 @@ on:
required: false
default: "087685d1"
env:
# Software-rendered CI submissions may exceed the interactive GPU hang budget.
MOBILEGL_GPU_HANG_BUDGET_MS: "0"
# ONE LIVE RUN PER BRANCH. The disaggregation branch pushes (often docs-only) commits minutes apart
# and each push fans out to ~330 retrace legs here and ~275 jobs in apk.yml; with no group, runs
# sat queued for 10+ hours behind their own successors. A manual dispatch gets a group of its own,
@@ -413,6 +417,12 @@ jobs:
--discovery "${RUNNER_TEMP}/primary-inproc-lane.json" \
--junit "${RUNNER_TEMP}/primary-inproc.xml" --output "${RUNNER_TEMP}/primary-inproc-proof.json"
# The run log needs a signed-in reader; annotations do not. Name every failed ctest entry,
# with the lines of its output that say why, where anyone reading the run can see them.
- name: Annotate failed entries
if: failure()
run: python3 scripts/ci/junit_annotate.py "${RUNNER_TEMP}"
- name: Upload integration lane evidence
if: always()
uses: actions/upload-artifact@v7
@@ -1169,6 +1179,12 @@ jobs:
# arm proof read; verify-split-logs/ is whatever the control steps left behind, which for
# twelve entries is a deliberately red re-run under the same name. Uploading only the
# second - which is what this step did before the copy above existed - loses the first.
# The run log needs a signed-in reader; annotations do not. Name every failed ctest entry,
# with the lines of its output that say why, where anyone reading the run can see them.
- name: Annotate failed entries
if: failure()
run: python3 scripts/ci/junit_annotate.py "${RUNNER_TEMP}"
- name: Upload verify-split lane logs
if: always()
uses: actions/upload-artifact@v7
@@ -1481,7 +1497,7 @@ jobs:
working-directory: build-split
env:
MOBILEGL_ITEST_REQUIRE_GPU: "1"
run: ctest --output-on-failure -L unit --no-tests=error -j "$(nproc)"
run: ctest --output-on-failure -L unit --no-tests=error -j "$(nproc)" --output-junit "${RUNNER_TEMP}/unit-split.xml"
# P7 wave 2-F, PH-7 (1). THE TCP SUPERVISOR LANE NEEDS flatc, AND CI HAD NONE.
#
@@ -1648,6 +1664,12 @@ jobs:
CONTROL_TMPDIR: ${{ runner.temp }}
run: bash "${GITHUB_WORKSPACE}/scripts/ci/split_negative_controls.sh"
# The run log needs a signed-in reader; annotations do not. Name every failed ctest entry,
# with the lines of its output that say why, where anyone reading the run can see them.
- name: Annotate failed entries
if: failure()
run: python3 scripts/ci/junit_annotate.py "${RUNNER_TEMP}"
- name: Upload split lane logs
if: always()
uses: actions/upload-artifact@v7
@@ -1737,7 +1759,7 @@ jobs:
env:
MOBILEGL_ITEST_REQUIRE_GPU: "1"
MOBILEGL_IPC_STRICT_ERRORS: "1"
run: ctest --output-on-failure -L unit --no-tests=error -j "$(nproc)"
run: ctest --output-on-failure -L unit --no-tests=error -j "$(nproc)" --output-junit "${RUNNER_TEMP}/unit-strict.xml"
# ---- P5e (ra), MG_Remote/CONTRACT-P5E.md §7: ONE STEP, TWO ARMS, AND THE TREE PICKS ----
#
@@ -2246,6 +2268,12 @@ jobs:
slp.require_green(lane, sys.argv[3], names)
PY
# The run log needs a signed-in reader; annotations do not. Name every failed ctest entry,
# with the lines of its output that say why, where anyone reading the run can see them.
- name: Annotate failed entries
if: failure()
run: python3 scripts/ci/junit_annotate.py "${RUNNER_TEMP}"
- name: Upload strict lane logs
if: always()
uses: actions/upload-artifact@v7
@@ -2751,7 +2779,11 @@ jobs:
&& [ '${{ matrix.case }}' = 'improved-transparency-minecraft-26.3' ]; then
export MOBILEGL_MAGMA_DISABLE_BLENDED_DEPTH_WRITE=1
fi
ctest -V --no-tests=error -R '^MobileGLTraceReplay\.${{ matrix.case }}\.${{ matrix.backend }}$'
ctest -V --no-tests=error -R '^MobileGLTraceReplay\.${{ matrix.case }}\.${{ matrix.backend }}$' --output-junit "${RUNNER_TEMP}/retrace.xml"
- name: Annotate failed entries
if: failure()
run: python3 scripts/ci/junit_annotate.py --excerpts 1 "${RUNNER_TEMP}"
- name: Upload core dumps
if: failure()
@@ -3506,27 +3538,10 @@ jobs:
runs-on: ubuntu-latest
# Deliberately independent of build-linux: these are source-level gates, they take
# seconds, and a broken build must not hide a drifted interface.
env:
# THE CURRENT PHASE's base ref, for the two G5 region gates below. It is 37da3c3a - P4a's
# base ref, INTEGRATOR-DECISIONS ID-1 - and NOT the workflow's baseline_sha input: that input
# is the SYMBOL baseline (087685d1, P1's G1 reading) and it is empty on a push, whereas these
# gates ask "did the do-not-touch list move since the phase started".
#
# IT MOVED FROM P3a's 44c2b5cf TO P4a's 37da3c3a WITH THE PHASE, and that is a deliberate
# narrowing rather than a loss: P3a's eleven functions were compared against 44c2b5cf at P3a's
# own exit and were byte-identical there, so 37da3c3a carries the same bodies (measured: the
# eleven shas at 37da3c3a are the eleven shas at 44c2b5cf, and FlushPendingRangesFrom's is
# still the sha pinned in the script at 3e298c9a). What the two gates now both answer is "did
# anything on the list move during P4a", which is the question this phase can act on.
BASELINE: "37da3c3a"
steps:
- name: Checkout repo
uses: actions/checkout@v6
with:
# The G5 gate reads Managers.cpp at BASELINE with `git show`, which a depth-1 checkout
# does not have. Nothing else in this job needs history.
fetch-depth: 0
# The seven generators all read MG_Pipe/*.def, so regenerating and diffing is what
# keeps the two interface tables, the wire records, the verify comparators, the
@@ -3655,76 +3670,6 @@ jobs:
python3 tools/trace_replay/test_run_tcp_matrix.py
python3 tools/trace_replay/test_compare_actuals.py
# A GATE as of P3a (G5). "pool 与延迟释放原样搬" (ROADMAP.md:19) is meant literally: the
# buffer pool, the deferred-release drain and the three persistently mapped rings move
# VERBATIM, and ARCHITECTURE.md:515 says why - their retire happens only inside Present, so
# a batching or ordering change there starves them, and nothing else in this workflow can
# see it. P3a rewrites the rest of Managers.cpp by design, so a file diff says nothing; the
# script extracts the ELEVEN named bodies and compares their hashes on their own.
#
# Eleven and not ten (ID-15): Managers.cpp carries the three-tier flush drain TWICE, once
# per preprocessor arm, and a push build compiles only FlushPendingRangesFrom while the
# untouched FlushPendingRangesNow lives in the `#else`. Hashing the pull name alone would
# protect text no shipping build compiles, so both are hashed - the pull ladder against
# BASELINE, the push ladder against a sha pinned in the script at 3dadd4c1 (ID-41), because
# that one was born in P3a and is compared against the reviewed body rather than against the
# base ref.
#
# Scoped to the disaggregation branch and to a manual dispatch, deliberately: the question
# is "did these eleven move since P3a started", and BASELINE is P3a's base ref. On dev,
# where unrelated buffer fixes land on their own schedule, the same comparison would be
# asking a question nobody posed - it belongs with the TEMPORARY trigger lines at the top
# of this file and retires with them.
#
# --self-test is the half that keeps it honest, and it is not optional: a comparison that
# silently stopped comparing produces exactly the same green as eleven untouched bodies. It
# runs eight canned controls - eleven bodies extracted, an untouched copy compared equal, an
# edit OUTSIDE them ignored, each of the three perturbation targets (ClearBufferPool and BOTH
# flush ladders) reported BY NAME, plus the pin-precedence control and a one-token edit to the
# pinned ladder compared against the pin - and fails if any of them does not answer.
# Same shape as gen_pipe.py --self-test above.
- name: P3a regions unchanged (G5)
if: ${{ github.ref == 'refs/heads/feat/disaggregated' || github.event_name == 'workflow_dispatch' }}
run: bash scripts/p3a_untouched_regions.sh "${BASELINE}" HEAD
- name: P3a untouched-region self-test (G5)
if: ${{ github.ref == 'refs/heads/feat/disaggregated' || github.event_name == 'workflow_dispatch' }}
run: bash scripts/p3a_untouched_regions.sh --self-test
# A GATE AS OF P4a (G5), and a SECOND script rather than an edit to the one above. P4a extends
# the same claim to the rest of ARCHITECTURE.md:318's do-not-touch list - the unpack PBO ring's
# staging repack and its two helpers, the attachment permutation, the D24S8 sampling-emulation
# core and the format-caveat handler - which is SEVENTEEN regions across THREE files
# (BRIEF-P4A.md D-N: P3a's eleven, which P4a must not touch either, plus P4a's six). The
# parent's SOURCE_PATH is a single file, so the extension needed a per-region source path and
# a region KIND (DepthStencilSamplingReadImpl is a namespace, not a function); everything else
# about the extraction is its parent's, verbatim.
#
# Both scripts run. The parent keeps answering its own question against its own eleven, so a
# regression in either half names itself, and neither gate can be silenced by editing the
# other's list.
#
# Same feat/disaggregated-or-dispatch guard as the P3a step, for the same reason: the question
# is "did these move since the phase started", and on dev - where unrelated buffer and texture
# fixes land on their own schedule - it would be a question nobody posed. It belongs with the
# TEMPORARY trigger lines at the top of this file and retires with them.
#
# --self-test is the half that keeps it honest and is not optional: a comparison that silently
# stopped comparing produces exactly the same green as seventeen untouched regions. It runs
# three positive controls (seventeen regions extracted, an untouched copy compared equal, an
# edit OUTSIDE them invisible in all three files) and NINE negative ones - ClearBufferPool,
# FlushPendingRangesNow, RecomputeBackendColorSlots and StageBlocksIntoUnpackRing, each
# perturbed at its HEAD and at its TAIL and each required to be named BY NAME, plus a
# one-token edit to the pinned FlushPendingRangesFrom body compared against the pin - and
# fails if any of them does not answer.
- name: P4a regions unchanged (G5)
if: ${{ github.ref == 'refs/heads/feat/disaggregated' || github.event_name == 'workflow_dispatch' }}
run: bash scripts/p4a_untouched_regions.sh "${BASELINE}" HEAD
- name: P4a untouched-region self-test (G5)
if: ${{ github.ref == 'refs/heads/feat/disaggregated' || github.event_name == 'workflow_dispatch' }}
run: bash scripts/p4a_untouched_regions.sh --self-test
# Warning only for now: the disaggregation documents are still being written, and a
# lint that fails a rewrite in progress teaches people to ignore it. It becomes
# --strict when the documents settle.
+31
View File
@@ -0,0 +1,31 @@
name: Arch WSL GPU smoke
on:
workflow_dispatch:
push:
branches:
- ci/arch-wsl-gpu-runner
paths:
- .github/workflows/wsl-gpu-smoke.yml
- docs/ci/arch-wsl-gpu-runner.md
permissions: {}
concurrency:
group: arch-wsl-gpu-smoke
cancel-in-progress: false
jobs:
gpu-smoke:
runs-on: [self-hosted, Linux, X64, arch, wsl, gpu, gles, vulkan]
timeout-minutes: 5
steps:
- name: Report runner identity
shell: bash
run: |
printf 'Runner: %s\n' "$RUNNER_NAME"
uname -n
uname -a
- name: Verify hardware GLES and Vulkan
shell: bash
run: /usr/local/libexec/mobilegl-runner/gpu-smoke
+3
View File
@@ -372,6 +372,7 @@ set(SOURCE_FILES
MobileGL/MG_Util/ShaderTranspiler/SpirvPasses/FixIterationRPSubgroupScratchPass.cpp
MobileGL/MG_Util/ShaderTranspiler/SpirvPasses/EmulateSubgroupsPass.cpp
MobileGL/MG_Util/ShaderTranspiler/SpirvPasses/NormalizeRectCoordinatesPass.cpp
MobileGL/MG_Util/ShaderTranspiler/SpirvPasses/DeduplicateImageTypesPass.cpp
MobileGL/MG_Util/ShaderTranspiler/SpirvPasses/Lower1DArrayImagesPass.cpp
MobileGL/MG_Util/ShaderTranspiler/SpirvPasses/Lower1DSampledImagesPass.cpp
MobileGL/MG_Util/ShaderTranspiler/SpirvPasses/BakeImageFormatsPass.cpp
@@ -440,6 +441,7 @@ set(SOURCE_FILES
MobileGL/MG_Backend/Init.cpp
MobileGL/MG_Backend/BackendObject.cpp
MobileGL/MG_Backend/GpuHangWatch.cpp
MobileGL/MG_Backend/DirectGLES/DirectGLES.cpp
MobileGL/MG_Backend/DirectGLES/BackendObject_DirectGLES.cpp
@@ -467,6 +469,7 @@ set(SOURCE_FILES
MobileGL/MG_Backend/DirectVulkan/Renderer/VkSamplerManager.cpp
MobileGL/MG_Backend/DirectVulkan/Renderer/VkClearManager.cpp
MobileGL/MG_Backend/DirectVulkan/Renderer/VkRenderPassManager.cpp
MobileGL/MG_Backend/DirectVulkan/Renderer/GpuProgressMarkers.cpp
MobileGL/MG_State/GLState/Core.cpp
MobileGL/MG_State/GLState/ShareGroupState.cpp
-12
View File
@@ -238,18 +238,6 @@ namespace MobileGL {
#endif
return true;
}
#if defined(__linux__) && !defined(__ANDROID__)
// LIBGL_ALWAYS_SOFTWARE is how a process (or whoever launched it) asks the system's GL for a
// software renderer. MobileGL is a GPU forwarder, so it steps aside and lets the system's
// other implementation give the process what it asked for.
{
String software;
if (MG_ConfigLoader::LookupSetting("LIBGL_ALWAYS_SOFTWARE", software) && !software.empty() &&
software != "0" && software != "false") {
return false;
}
}
#endif
#if MOBILEGL_BUILD_DISAGGREGATED
// Not yet: the bring-up would dial the configured server and wait for it. Ask first.
return MG_Remote::Client::ConfiguredServerAvailable();
+2
View File
@@ -97,6 +97,8 @@ namespace MobileGL::MG_Backend {
static_cast<SizeT>(target) >= kFormatCapabilityTextureTargetCount) {
return kFormatCapabilityTargetCount;
}
// An external texture is a 2D one wherever a format can go.
if (target == TextureTarget::External) return static_cast<SizeT>(TextureTarget::Texture2D);
return static_cast<SizeT>(target);
}
+32 -1
View File
@@ -9,9 +9,9 @@
#pragma once
#include <Includes.h>
#include "MG_State/GLState/TextureState/TextureEnum.h"
#include <MG_Util/Damage/Damage.h>
#if MOBILEGL_BUILD_DISAGGREGATED
#include <MG_Pipe/MGPipeHandles.h>
#include <MG_Util/Damage/Damage.h>
#endif
namespace MobileGL {
@@ -610,6 +610,24 @@ namespace MobileGL {
Uint32 Stride = 0;
Uint32 Offset = 0;
Uint64 Modifier = 0;
// A YUV image's Cb Cr plane: in the same descriptor, at this offset and pitch.
Uint32 Plane1Stride = 0;
Uint32 Plane1Offset = 0;
};
// An import of a two-plane (YUV) dma-buf: where its planes are in the one descriptor, and
// EGL_EXT_image_dma_buf_import's colour hints (0 = not given).
struct SharedImageImportLayout {
Uint32 PlaneCount = 1;
Uint32 Offset[2] = {0, 0};
Uint32 Pitch[2] = {0, 0};
Uint32 ColorSpace = 0;
Uint32 Range = 0;
Uint32 SitingX = 0;
Uint32 SitingY = 0;
// The DRM format modifier the import named; HasModifier false = none (the implicit one).
Bool HasModifier = false;
Uint64 Modifier = 0;
};
// THE DAMAGE OF THE PRESENT BEING APPLIED on this thread: the server's apply thread sets it
@@ -697,6 +715,13 @@ namespace MobileGL {
(void)fd, (void)width, (void)height, (void)fourcc, (void)outId;
return false;
}
// The same for a YUV fourcc, whose planes and hints `layout` gives. A YUV buffer the
// server did not export is taken too (its labelled CPU-copy fallback).
virtual Bool ImportSharedImagePlanes(int fd, Uint32 width, Uint32 height, Uint32 fourcc,
const SharedImageImportLayout& layout, Uint64* outId) {
(void)fd, (void)width, (void)height, (void)fourcc, (void)layout, (void)outId;
return false;
}
// Drops this client's reference (an allocation's or an import's).
virtual Bool ReleaseSharedImage(Uint64 id) {
(void)id;
@@ -749,6 +774,12 @@ namespace MobileGL {
// native state is process-global by construction gives this object its own copy and binds
// it to the calling thread. The default has nothing per process to separate.
virtual void BindSessionStateToThisThread() {}
// A served session's apply thread asks this each time it wakes, BEFORE it applies another
// record: true when this session's own GPU work faulted or hung the GPU and the backend has
// latched the session for it (MGPipeSessionLatch, family DeviceLost), so the next frame -
// which would very likely stop the GPU again - is never applied. Cheap by contract: no
// wait, at most a status read. The default knows of no fault.
virtual Bool LatchIfGpuFaulted() { return false; }
// A served session's ring moved to another of its client's contexts (bind_context). A
// backend whose surface follows the native context it binds needs nothing; one that keeps
// a single native surface binding re-binds the surface that context was last made current
@@ -961,6 +961,14 @@ namespace MobileGL::MG_Backend::DirectGLES {
return true;
}
Bool BackendObject_DirectGLES::LatchIfGpuFaulted() {
#if MOBILEGL_BUILD_DISAGGREGATED
return LatchIfGuiltyBeforeApply();
#else
return false;
#endif
}
void BackendObject_DirectGLES::Initialize() {
MG_Util::BackendLoader::AcquireEGLFunctions(m_EGLFunctions);
MG_Util::BackendLoader::AcquireGLESFunctions(m_GLESFunctions, m_EGLFunctions.eglGetProcAddress);
@@ -32,6 +32,8 @@ namespace MobileGL::MG_Backend::DirectGLES {
public:
~BackendObject_DirectGLES() override;
// The driver's verdict on this session's context (DirectGLES::LatchIfDeviceLost).
Bool LatchIfGpuFaulted() override;
void Initialize() override;
Bool InitCapabilities() override;
Bool InitWindowSurface() override;
+355 -23
View File
@@ -427,6 +427,8 @@ namespace MobileGL::MG_Backend::DirectGLES {
// targets with no mip chain map to Unknown so the lookup falls back to no bias.
TextureTarget SamplerUniformTextureTarget(GLenum uniformType) {
switch (uniformType) {
case GL_SAMPLER_EXTERNAL_OES:
return TextureTarget::External;
case GL_SAMPLER_1D:
case GL_INT_SAMPLER_1D:
case GL_UNSIGNED_INT_SAMPLER_1D:
@@ -16982,6 +16984,53 @@ namespace MobileGL::MG_Backend::DirectGLES {
#endif
}
// RESET NOTIFICATION, decided once per process by its first native context (EnsureNativeContext
// says why). MOBILEGL_ESPRYT_RESET_NOTIFICATION=0 keeps the driver's default.
namespace {
// EGL_EXT_create_context_robustness (eglext.h is not part of this file's include set).
constexpr EGLint kEglResetNotificationStrategyExt = 0x3138;
constexpr EGLint kEglLoseContextOnResetExt = 0x31BF;
enum class ResetNotification : Uint8 { Undecided, On, Off };
std::atomic<ResetNotification> g_resetNotification{ResetNotification::Undecided};
Bool ResetNotificationWanted() {
const ResetNotification decided = g_resetNotification.load(std::memory_order_acquire);
if (decided != ResetNotification::Undecided) return decided == ResetNotification::On;
if (const char* value = std::getenv("MOBILEGL_ESPRYT_RESET_NOTIFICATION"); value != nullptr && value[0] == '0')
return false;
if (g_Display == EGL_NO_DISPLAY || !g_EGLFuncs.eglQueryString) return false;
const char* extensions = g_EGLFuncs.eglQueryString(g_Display, EGL_EXTENSIONS);
if (extensions == nullptr) return false;
constexpr const char kWord[] = "EGL_EXT_create_context_robustness";
for (const char* at = std::strstr(extensions, kWord); at != nullptr; at = std::strstr(at + 1, kWord)) {
const char after = at[sizeof(kWord) - 1];
if ((at == extensions || at[-1] == ' ') && (after == '\0' || after == ' ')) return true;
}
return false;
}
void NoteResetNotificationRefused() {
ResetNotification expected = ResetNotification::Undecided;
if (g_resetNotification.compare_exchange_strong(expected, ResetNotification::Off, std::memory_order_acq_rel))
MGLOG_W("DirectGLES: the driver refused EGL_LOSE_CONTEXT_ON_RESET; a context that hangs the GPU is "
"not told, and its session cannot be ended for it");
}
void NoteResetNotificationDecided(Bool used) {
ResetNotification expected = ResetNotification::Undecided;
const ResetNotification decided = used ? ResetNotification::On : ResetNotification::Off;
if (g_resetNotification.compare_exchange_strong(expected, decided, std::memory_order_acq_rel))
MGLOG_I("DirectGLES: native contexts %s reset notification (EGL_LOSE_CONTEXT_ON_RESET): a context "
"whose work hangs or faults the GPU %s",
used ? "ask for" : "do not ask for",
used ? "is named by the driver and ends its own session" : "is not told");
}
} // namespace
Bool NativeContextsNotifyResets() {
return g_resetNotification.load(std::memory_order_acquire) == ResetNotification::On;
}
static Bool EnsureNativeContext(NativeContextTuple& tuple, EGLint surfaceBit, NativeWindowType window) {
if (!EnsureDisplayInitialized()) {
MGLOG_E("EnsureNativeContext: the EGL display did not initialize");
@@ -17030,21 +17079,47 @@ namespace MobileGL::MG_Backend::DirectGLES {
// request yields a 3.0 context that lacks the 3.1/3.2 texture targets the
// capability probes exercise; mobile drivers ignore the minor and hand out
// their maximum either way.
//
// RESET NOTIFICATION. With the driver's default strategy a context whose work hung the GPU
// is never told: after Adreno's ~2 s timeout reset it silently drops everything that context
// submits (the client's window just stops), and its client never learns it should recreate.
// Asking for EGL_LOSE_CONTEXT_ON_RESET lets the driver name the guilty context
// (glGetGraphicsResetStatus answers GUILTY, once) and LatchIfDeviceLost ends that session
// alone, so the client reads a lost context and can recover. (A ~10 s reset that a failed
// preemption ends was not reported to the server's contexts in measurement - see
// GpuHangWatch.h.) Every context of the process shares one group, and contexts that share
// must agree on the strategy, so it is decided once, by the first context, and kept.
for (const EGLint minorVersion : {2, 1, 0}) {
const Bool notify = ResetNotificationWanted();
const EGLint contextAttribs[] = {EGL_CONTEXT_MAJOR_VERSION, 3,
EGL_CONTEXT_MINOR_VERSION, minorVersion,
notify ? kEglResetNotificationStrategyExt : EGL_NONE,
kEglLoseContextOnResetExt,
EGL_NONE};
tuple.Context = g_EGLFuncs.eglCreateContext(g_Display, tuple.Config, share, contextAttribs);
if (tuple.Context == EGL_NO_CONTEXT && notify && share == EGL_NO_CONTEXT) {
// The process's first context: if the same version WITHOUT the strategy is accepted,
// it was the strategy the driver refused, and no context of the process asks for it.
const EGLint plainAttribs[] = {EGL_CONTEXT_MAJOR_VERSION, 3, EGL_CONTEXT_MINOR_VERSION, minorVersion,
EGL_NONE};
tuple.Context = g_EGLFuncs.eglCreateContext(g_Display, tuple.Config, share, plainAttribs);
if (tuple.Context != EGL_NO_CONTEXT) NoteResetNotificationRefused();
}
if (tuple.Context != EGL_NO_CONTEXT) {
NoteResetNotificationDecided(notify);
tuple.Serial = g_nativeContextSerials.fetch_add(1, std::memory_order_relaxed) + 1;
++g_displayUsers;
return true;
}
}
const EGLint legacyContextAttribs[] = {EGL_CONTEXT_CLIENT_VERSION, 3, EGL_NONE};
const Bool notify = ResetNotificationWanted();
const EGLint legacyContextAttribs[] = {EGL_CONTEXT_CLIENT_VERSION, 3,
notify ? kEglResetNotificationStrategyExt : EGL_NONE,
kEglLoseContextOnResetExt, EGL_NONE};
tuple.Context = g_EGLFuncs.eglCreateContext(g_Display, tuple.Config, share, legacyContextAttribs);
if (tuple.Context == EGL_NO_CONTEXT) return false;
NoteResetNotificationDecided(notify);
tuple.Serial = g_nativeContextSerials.fetch_add(1, std::memory_order_relaxed) + 1;
++g_displayUsers;
return true;
@@ -17692,6 +17767,8 @@ namespace MobileGL::MG_Backend::DirectGLES {
MGLOG_E_ONCE("DirectGLES::MakeCurrent failed: native eglMakeCurrent returned error 0x%04x", error);
return false;
}
g_EGLFuncs.eglSwapBuffersWithDamageEXT =
MG_Util::BackendLoader::ResolveSwapBuffersWithDamage(g_EGLFuncs);
InvalidateEglVerifiedStamp();
t_boundNativeContext = tuple.Context;
t_boundNativeDraw = session.Draw;
@@ -18229,25 +18306,61 @@ namespace MobileGL::MG_Backend::DirectGLES {
}
#if MOBILEGL_BUILD_DISAGGREGATED
Bool LatchIfDeviceLost(const char* site, Bool debugKnob) {
if (MG_Config::Transport == MG_Config::TransportMode::Monolith) return false;
namespace {
// Once per thread: a latched session declines everything after this verb anyway, and the
// apply thread is the session's own.
static thread_local Bool latched = false;
if (latched) return true;
thread_local Bool t_deviceLossLatched = false;
Bool LatchOnResetStatus(const char* site, GLenum status) {
if (status == GL_NO_ERROR) return false;
// ONLY THE GUILTY CONTEXT ENDS ITS SESSION. The contexts ask for reset notification
// (EnsureNativeContext) so the driver can name the context whose work hung or faulted the
// GPU; a reset it reports to an INNOCENT context - one whose work only waited behind the
// stall, the compositor's above all - is noted and the session goes on as it did before
// contexts were told. Adreno 750 measured: the guilty context reads GUILTY once, after
// which the driver drops everything it submits; innocent ones read NO_ERROR.
static thread_local Bool innocentNoted = false;
if (status == GL_INNOCENT_CONTEXT_RESET) {
if (!innocentNoted) {
innocentNoted = true;
MGLOG_W("Espryt:%s - the driver reports a GPU reset this session's context did not cause "
"(GL_INNOCENT_CONTEXT_RESET); the session goes on",
site != nullptr ? site : "?");
}
return false;
}
t_deviceLossLatched = true;
(void)MG_Pipe::MGPipeSessionLatch(MG_Pipe::MGPipeFatalFamily::DeviceLost,
"MGPipe: Fatal{BackendDeviceLost, \"Espryt:%s\"} - this session's EGL "
"context was reset (glGetGraphicsResetStatus 0x%x); the session ends "
"and its client reads a lost context, every other session keeps running",
site != nullptr ? site : "?", static_cast<unsigned>(status));
return true;
}
} // namespace
Bool LatchIfDeviceLost(const char* site, Bool debugKnob) {
if (MG_Config::Transport == MG_Config::TransportMode::Monolith) return false;
if (t_deviceLossLatched) return true;
GLenum status = GL_NO_ERROR;
if (g_GLESFuncs.glGetGraphicsResetStatus != nullptr && IsBackendContextCurrentOnThisThread()) {
status = g_GLESFuncs.glGetGraphicsResetStatus();
}
if (status == GL_NO_ERROR && debugKnob && MG_Pipe::MGPipeDebugDeviceLossDue()) status = GL_UNKNOWN_CONTEXT_RESET;
if (status == GL_NO_ERROR) return false;
latched = true;
(void)MG_Pipe::MGPipeSessionLatch(MG_Pipe::MGPipeFatalFamily::DeviceLost,
"MGPipe: Fatal{BackendDeviceLost, \"Espryt:%s\"} - this session's EGL "
"context was reset (glGetGraphicsResetStatus 0x%x); the session ends "
"and its client reads a lost context, every other session keeps running",
site != nullptr ? site : "?", static_cast<unsigned>(status));
return true;
return LatchOnResetStatus(site, status);
}
Bool LatchIfGuiltyBeforeApply() {
if (MG_Config::Transport == MG_Config::TransportMode::Monolith) return false;
if (t_deviceLossLatched) return true;
// Asked every time the apply thread wakes, so it must leave nothing behind: not
// IsBackendContextCurrentOnThisThread, whose once-per-frame EGL verification stamp it would
// spend at the top of the loop (where the session may be between two of its contexts) - only
// whether this thread holds a native context and the backend's ownership.
if (g_GLESFuncs.glGetGraphicsResetStatus == nullptr || t_boundNativeContext == EGL_NO_CONTEXT ||
g_backendContextOwnerThread.load(std::memory_order_acquire) != std::this_thread::get_id())
return false;
return LatchOnResetStatus("apply", g_GLESFuncs.glGetGraphicsResetStatus());
}
#endif
@@ -18495,18 +18608,14 @@ namespace MobileGL::MG_Backend::DirectGLES {
WaitSyncKhrFn WaitSync = nullptr;
ClientWaitSyncKhrFn ClientWaitSync = nullptr;
DupNativeFenceFdFn DupNativeFenceFd = nullptr;
// YUV images: sampled as external textures (GL_OES_EGL_image_external_essl3), and
// through GL_EXT_YUV_target's raw Y'CbCr sampler where the driver has it, so the
// conversion is the import's own (its colour hints) rather than the driver's guess.
Bool ExternalEssl3 = false;
Bool YuvTarget = false;
};
Bool HasExtensionWord(const char* list, const char* word) {
if (list == nullptr) return false;
const SizeT n = std::strlen(word);
for (const char* at = std::strstr(list, word); at != nullptr; at = std::strstr(at + 1, word)) {
const Bool startOk = at == list || at[-1] == ' ';
const Bool endOk = at[n] == '\0' || at[n] == ' ';
if (startOk && endOk) return true;
}
return false;
}
using MG_Util::BackendLoader::HasExtensionWord;
// None of these is in the loader's tables, and eglGetProcAddress hands back live-looking
// stubs for entry points a driver lacks, so the extension strings decide first. Resolved
@@ -18558,6 +18667,8 @@ namespace MobileGL::MG_Backend::DirectGLES {
entry->Why = "GL_OES_EGL_image is not advertised";
return *entry;
}
entry->ExternalEssl3 = HasExtensionWord(gl, "GL_OES_EGL_image_external_essl3");
entry->YuvTarget = HasExtensionWord(gl, "GL_EXT_YUV_target");
entry->GetNativeClientBuffer = reinterpret_cast<GetNativeClientBufferFn>(
g_EGLFuncs.eglGetProcAddress("eglGetNativeClientBufferANDROID"));
entry->CreateImage =
@@ -18597,6 +18708,11 @@ namespace MobileGL::MG_Backend::DirectGLES {
Uint64 WaitedGeneration = 0;
Uint64 WaitedContext = 0;
Uint64 NotedFrame = 0;
// A YUV image (ConvertYuvForSampling): an external texture over the EGLImage, the source
// of the conversion into the texture's own RGBA storage, and the reader's frame the
// conversion last ran in.
GLuint ExternalTexture = 0;
Uint64 ConvertedFrame = 0;
~EglImage() {
// eglDestroyImageKHR needs the display, not a context, so a session's twin teardown
@@ -18604,6 +18720,10 @@ namespace MobileGL::MG_Backend::DirectGLES {
// display - which destroyed the image itself - skip it.
if (Handle == nullptr || Destroy == nullptr || InProcessExit()) return;
if (g_displayTerminations.load(std::memory_order_acquire) != DisplayTerminations) return;
// Texture names are the share group's: deleting one needs any context of it, and
// without one current the name goes with the display.
if (ExternalTexture != 0 && t_boundNativeContext != EGL_NO_CONTEXT)
g_GLESFuncs.glDeleteTextures(1, &ExternalTexture);
Destroy(Display, Handle);
}
#endif
@@ -18829,14 +18949,23 @@ namespace MobileGL::MG_Backend::DirectGLES {
// The session ends frames at publishing flushes (PublishPendingAccesses): it writes
// images, so its first use of one in a frame waits for that image's pending reads.
Bool ImplicitSync = false;
// When a YUV image is converted again (ConvertYuvForSampling): renewed at EVERY
// boundary - present, publishing flush, and each native fence whatever the session
// is - since an offscreen producer (a browser's GPU process) may have no other.
Uint64 YuvEpoch = 0;
};
std::atomic<Uint64> g_readFrameIds{0};
ReadFrame& CurrentReadFrame() {
thread_local ReadFrame frame;
if (frame.Id == 0) frame.Id = g_readFrameIds.fetch_add(1, std::memory_order_relaxed) + 1;
if (frame.YuvEpoch == 0) frame.YuvEpoch = g_readFrameIds.fetch_add(1, std::memory_order_relaxed) + 1;
return frame;
}
void RenewYuvEpoch(ReadFrame& frame) {
frame.YuvEpoch = g_readFrameIds.fetch_add(1, std::memory_order_relaxed) + 1;
}
} // namespace
void AcquireForSampling(const EglImageRef& image) {
@@ -18866,9 +18995,201 @@ namespace MobileGL::MG_Backend::DirectGLES {
}
}
Bool IsYuv(const EglImageRef& image) {
return image != nullptr && image->Image != nullptr && SI::FourccIsYuv(image->Image->Fourcc);
}
namespace {
// THE CONVERSION PASS: one triangle over the texture, sampling the image through an
// external texture. Programs are the share group's and rebuilt with a new context
// generation; the framebuffer and vertex array are per context (never shared).
struct YuvConversion {
Uint ContextGeneration = ~0u;
GLuint ExternalProgram = 0; // the driver's conversion (samplerExternalOES)
GLuint TargetProgram = 0; // the import's own (GL_EXT_YUV_target)
GLint TargetMatrix = -1;
GLint TargetOffset = -1;
Bool Failed = false;
NativeContextName Framebuffer;
NativeContextName VertexArray;
};
YuvConversion& Conversion() {
static auto* conversion = new YuvConversion();
return *conversion;
}
constexpr const char* kYuvVertexSource =
"#version 300 es\n"
"out vec2 vUv;\n"
"void main() {\n"
" vec2 p = vec2(float((gl_VertexID << 1) & 2), float(gl_VertexID & 2));\n"
" gl_Position = vec4(p * 2.0 - 1.0, 0.0, 1.0);\n"
" vUv = p;\n"
"}\n";
constexpr const char* kYuvExternalFragmentSource =
"#version 300 es\n"
"#extension GL_OES_EGL_image_external_essl3 : require\n"
"precision highp float;\n"
"uniform samplerExternalOES uSource;\n"
"in vec2 vUv;\n"
"out vec4 oColor;\n"
"void main() { oColor = vec4(texture(uSource, vUv).rgb, 1.0); }\n";
constexpr const char* kYuvTargetFragmentSource =
"#version 300 es\n"
"#extension GL_EXT_YUV_target : require\n"
"precision highp float;\n"
"uniform __samplerExternal2DY2YEXT uSource;\n"
"uniform mat3 uMatrix;\n"
"uniform vec3 uOffset;\n"
"in vec2 vUv;\n"
"out vec4 oColor;\n"
"void main() {\n"
" vec3 ycbcr = texture(uSource, vUv).xyz;\n"
" oColor = vec4(clamp(uMatrix * (ycbcr - uOffset), 0.0, 1.0), 1.0);\n"
"}\n";
GLuint BuildYuvProgram(const char* fragmentSource) {
const GLuint vertexShader = g_GLESFuncs.glCreateShader(GL_VERTEX_SHADER);
const GLuint fragmentShader = g_GLESFuncs.glCreateShader(GL_FRAGMENT_SHADER);
GLint linked = GL_FALSE;
GLuint program = 0;
if (vertexShader != 0 && fragmentShader != 0) {
g_GLESFuncs.glShaderSource(vertexShader, 1, &kYuvVertexSource, nullptr);
g_GLESFuncs.glCompileShader(vertexShader);
g_GLESFuncs.glShaderSource(fragmentShader, 1, &fragmentSource, nullptr);
g_GLESFuncs.glCompileShader(fragmentShader);
program = g_GLESFuncs.glCreateProgram();
if (program != 0) {
g_GLESFuncs.glAttachShader(program, vertexShader);
g_GLESFuncs.glAttachShader(program, fragmentShader);
g_GLESFuncs.glLinkProgram(program);
g_GLESFuncs.glGetProgramiv(program, GL_LINK_STATUS, &linked);
}
}
if (vertexShader != 0) g_GLESFuncs.glDeleteShader(vertexShader);
if (fragmentShader != 0) g_GLESFuncs.glDeleteShader(fragmentShader);
if (linked != GL_TRUE) {
if (program != 0) g_GLESFuncs.glDeleteProgram(program);
return 0;
}
return program;
}
// MOBILEGL_YUV_DRIVER_CONVERSION=1: always the driver's conversion (samplerExternalOES),
// even where GL_EXT_YUV_target would let the import's hints decide.
Bool DriverConversionForced() {
static const Bool forced = [] {
const char* value = std::getenv("MOBILEGL_YUV_DRIVER_CONVERSION");
return value != nullptr && value[0] == '1';
}();
return forced;
}
} // namespace
void ConvertYuvForSampling(const EglImageRef& image, GLuint target, Uint32 width, Uint32 height) {
if (!IsYuv(image) || target == 0 || width == 0 || height == 0) return;
EglImage& state = *image;
ReadFrame& frame = CurrentReadFrame();
if (state.ConvertedFrame == frame.YuvEpoch) return;
// Whatever happens below, this frame does not try again.
state.ConvertedFrame = frame.YuvEpoch;
const EntryPoints& entry = ResolvedEntryPoints();
if (!entry.Usable || !entry.ExternalEssl3) {
MGLOG_E_ONCE("Shared images: a YUV image cannot be sampled here (%s)",
entry.Usable ? "no GL_OES_EGL_image_external_essl3" : entry.Why.c_str());
return;
}
// THE CPU-COPY FALLBACK, for a buffer this server did not allocate (SharedImageRegistry.h).
String why;
if (state.Image->IsForeign() && !SI::RefreshForeign(*state.Image, why)) {
MGLOG_E_ONCE("Shared images: foreign YUV image %llu could not be refreshed: %s",
static_cast<unsigned long long>(state.Image->Id), why.c_str());
}
YuvConversion& conversion = Conversion();
if (conversion.ContextGeneration != g_backendContextGeneration) {
conversion.ExternalProgram = 0;
conversion.TargetProgram = 0;
conversion.TargetMatrix = conversion.TargetOffset = -1;
conversion.Failed = false;
conversion.ContextGeneration = g_backendContextGeneration;
}
if (conversion.Failed) return;
const Bool ownConversion = entry.YuvTarget && !DriverConversionForced();
GLuint& program = ownConversion ? conversion.TargetProgram : conversion.ExternalProgram;
if (program == 0) {
program = BuildYuvProgram(ownConversion ? kYuvTargetFragmentSource : kYuvExternalFragmentSource);
if (program == 0) {
conversion.Failed = true;
MGLOG_E_ONCE("Shared images: the YUV conversion program could not be built");
return;
}
if (ownConversion) {
conversion.TargetMatrix = g_GLESFuncs.glGetUniformLocation(program, "uMatrix");
conversion.TargetOffset = g_GLESFuncs.glGetUniformLocation(program, "uOffset");
}
MGLOG_I("Shared images: YUV images convert through %s",
ownConversion ? "GL_EXT_YUV_target with the import's colour hints"
: "the driver's external-sampler conversion");
}
if (conversion.Framebuffer == 0) g_GLESFuncs.glGenFramebuffers(1, &conversion.Framebuffer);
if (conversion.VertexArray == 0) g_GLESFuncs.glGenVertexArrays(1, &conversion.VertexArray);
if (conversion.Framebuffer == 0 || conversion.VertexArray == 0) return;
// Every binding goes through Espryt's shadows or a guard that puts it back.
ScopedEmulationDrawState drawState; // also activates TextureImpl::TempTextureUnit
ScopedFramebufferBinding binding(/*saveRead=*/false, /*saveDraw=*/true);
DrainErrors();
if (state.ExternalTexture == 0) {
g_GLESFuncs.glGenTextures(1, &state.ExternalTexture);
g_GLESFuncs.glBindTexture(GL_TEXTURE_EXTERNAL_OES, state.ExternalTexture);
entry.TargetTexture2D(GL_TEXTURE_EXTERNAL_OES, state.Handle);
g_GLESFuncs.glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
g_GLESFuncs.glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
g_GLESFuncs.glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
g_GLESFuncs.glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
const GLenum error = g_GLESFuncs.glGetError();
if (error != GL_NO_ERROR) {
MGLOG_E_ONCE("Shared images: YUV image %llu cannot be bound as an external texture (0x%04x)",
static_cast<unsigned long long>(state.Image->Id), static_cast<unsigned>(error));
}
} else {
g_GLESFuncs.glBindTexture(GL_TEXTURE_EXTERNAL_OES, state.ExternalTexture);
}
FramebufferImpl::BindFramebufferId(GL_DRAW_FRAMEBUFFER, conversion.Framebuffer);
g_GLESFuncs.glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, target, 0);
const GLenum status = g_GLESFuncs.glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER);
if (status == GL_FRAMEBUFFER_COMPLETE) {
VertexArrayImpl::BindBackendVAOId(conversion.VertexArray);
g_GLESFuncs.glUseProgram(program);
g_GLESFuncs.glUniform1i(g_GLESFuncs.glGetUniformLocation(program, "uSource"),
static_cast<GLint>(TextureImpl::TempTextureUnit));
if (ownConversion) {
// A Y2Y sample is the stored code normalized as the format stores it: 8 bits, or
// P010's 10 in the high end of 16.
const double sampleMax = state.Image->Fourcc == SI::kFourccP010 ? 65535.0 / 64.0 : 255.0;
const auto c = SI::Yuv::ConversionFor(SI::GetYuvHints(*state.Image), sampleMax);
g_GLESFuncs.glUniformMatrix3fv(conversion.TargetMatrix, 1, GL_FALSE, c.Matrix);
g_GLESFuncs.glUniform3fv(conversion.TargetOffset, 1, c.Offset);
}
g_GLESFuncs.glViewport(0, 0, static_cast<GLsizei>(width), static_cast<GLsizei>(height));
g_GLESFuncs.glDrawArrays(GL_TRIANGLES, 0, 3);
const GLenum error = g_GLESFuncs.glGetError();
if (error != GL_NO_ERROR) {
MGLOG_E_ONCE("Shared images: the YUV conversion draw failed (0x%04x)", static_cast<unsigned>(error));
}
} else {
MGLOG_E_ONCE("Shared images: a YUV conversion target is not renderable (0x%04x)",
static_cast<unsigned>(status));
}
g_GLESFuncs.glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
g_GLESFuncs.glBindTexture(GL_TEXTURE_EXTERNAL_OES, 0);
}
Bool PublishPendingAccesses() {
ReadFrame& frame = CurrentReadFrame();
frame.ImplicitSync = true;
RenewYuvEpoch(frame);
// Sub-data waiting for a sync that nothing of this session would otherwise trigger.
if (t_boundNativeContext != EGL_NO_CONTEXT) TextureImpl::WritePendingSharedImageUploads();
if (frame.Tracker.Pending() == 0) return true;
@@ -18885,6 +19206,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
Bool ExportNativeFence(int* fence) {
*fence = -1;
ReadFrame& frame = CurrentReadFrame();
RenewYuvEpoch(frame);
// No context current: nothing of this session is in flight to fence.
if (t_boundNativeContext == EGL_NO_CONTEXT) return true;
TextureImpl::WritePendingSharedImageUploads();
@@ -18906,6 +19228,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
void PublishPendingReads() {
ReadFrame& frame = CurrentReadFrame();
RenewYuvEpoch(frame);
if (frame.Tracker.Pending() == 0) return;
int fence = -1;
// No context current: the reads' context is gone (and its work with it), and there is
@@ -19098,6 +19421,15 @@ namespace MobileGL::MG_Backend::DirectGLES {
return false;
}
void AcquireForSampling(const EglImageRef& image) { (void)image; }
Bool IsYuv(const EglImageRef& image) {
(void)image;
return false;
}
void ConvertYuvForSampling(const EglImageRef& image, GLuint target, Uint32 width, Uint32 height) {
(void)image, (void)target, (void)width, (void)height;
}
void PublishPendingReads() {}
Bool PublishPendingAccesses() { return true; }
Bool ExportNativeFence(int* fence) {
+18 -4
View File
@@ -162,14 +162,21 @@ namespace MobileGL::MG_Backend::DirectGLES {
// driver whether the calling thread's context was reset (glGetGraphicsResetStatus); a reset -
// or the debug knob MGPipeDebugDeviceLossDue - latches THIS session through
// MGPipeSessionLatch and answers true, and the caller returns from its verb without issuing
// more GL. Asked at the frame boundaries (Present, a shared-image present) and before a
// readback, never per draw. The contexts keep the driver's default reset strategy: asking
// every context to lose itself on reset would also end the INNOCENT ones - the compositor's
// among them - on any other client's GPU fault.
// more GL. Asked at the frame boundaries (Present, a shared-image present), before a readback,
// and by the apply loop each time it wakes (LatchIfGpuFaulted), never per draw. The contexts ask
// for reset notification (NativeContextsNotifyResets) so the driver can name the GUILTY one;
// only GUILTY (or a reset of unknown cause) ends the session - an INNOCENT report, a context
// whose work merely waited behind another's hang (the compositor's among them), is only noted.
// `debugKnob` false: the knob is not consulted here (the on-screen Present, so an injected loss
// lands on an offscreen client's shared-image present or readback, never on the compositor's).
Bool LatchIfDeviceLost(const char* site, Bool debugKnob = true);
// The apply loop's form of it (BackendObject::LatchIfGpuFaulted), asked each time the apply thread
// wakes: the driver's report only, no debug knob, and no EGL ground-truth verification spent.
Bool LatchIfGuiltyBeforeApply();
#endif
// True once the process's native contexts were created with EGL_LOSE_CONTEXT_ON_RESET (decided
// by the first one; the driver must offer EGL_EXT_create_context_robustness).
Bool NativeContextsNotifyResets();
// GL fence sync objects, backed by native ES fences. FenceSync returns null
// (the frontend then falls back to an always-signaled sync) when the calling
// thread does not own the ES context. Waits/queries degrade to "signaled" in
@@ -541,6 +548,13 @@ namespace MobileGL::MG_Backend::DirectGLES {
// landed since this context last waited, the context's later commands wait for its fence
// on the GPU; and the image is noted as read by the calling session's current frame.
void AcquireForSampling(const EglImageRef& image);
// YUV IMAGES (NV12/P010, MG_Remote/Server/SharedImageYuv.h) are never a name's level 0: the
// name keeps its own RGBA8 storage `target` (width x height), into which the image is
// converted at its first use in each of the calling session's frames - the image bound as
// an external texture, one triangle drawn - after the registry's CPU copy for a foreign
// buffer. Every touched binding is put back. Called after AcquireForSampling.
Bool IsYuv(const EglImageRef& image);
void ConvertYuvForSampling(const EglImageRef& image, GLuint target, Uint32 width, Uint32 height);
// The calling session's frame boundary for what it has read: hands a fence for everything
// the current context has submitted to every image noted since the last boundary. Called
// at Present and wherever the current native context stops being current (its reads can
+26 -13
View File
@@ -2898,16 +2898,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
kT0MapReadBit | kT0MapWriteBit | kMapPersistentBit | kMapCoherentBit | kDynamicStorageBit;
constexpr GLbitfield kT0MapFlags = kT0MapReadBit | kT0MapWriteBit | kMapPersistentBit | kMapCoherentBit;
Bool HasExtensionWord(const char* list, const char* word) {
if (list == nullptr) return false;
const SizeT n = std::strlen(word);
for (const char* at = std::strstr(list, word); at != nullptr; at = std::strstr(at + 1, word)) {
const Bool startOk = at == list || at[-1] == ' ';
const Bool endOk = at[n] == '\0' || at[n] == ' ';
if (startOk && endOk) return true;
}
return false;
}
using MG_Util::BackendLoader::HasExtensionWord;
Bool ResolveT0EntryPoints(String& why) {
if (g_t0GetNativeClientBuffer != nullptr && g_t0BufferStorageExternal != nullptr) return true;
@@ -3903,8 +3894,13 @@ namespace MobileGL::MG_Backend::DirectGLES {
MG_Pipe::MGPipeSetResourceOps(&g_glesResourceOps);
#endif
// Frontend writes issued while ops were unregistered advanced change
// serials with no per-op bump; re-open every draw-clean memo.
BumpBufferMutationEpoch();
// serials with no per-op bump; re-open every draw-clean memo. Advanced directly,
// past BumpBufferMutationEpoch's apply-thread pin: a registration mutates no buffer
// and only makes every memo probe again, and with several sessions in one process
// a new session's backend registers on its own thread while another session's
// apply thread is already running - which the pin would otherwise read as a
// client-thread producer and, under the strict lane, abort the server for.
g_bufferMutationEpoch.fetch_add(1, std::memory_order_release);
}
void UnregisterBufferBackendOps() {
@@ -8893,7 +8889,20 @@ namespace MobileGL::MG_Backend::DirectGLES {
return true;
}
Bind(GL_TEXTURE_2D);
if (!SharedImageImpl::TargetBoundTexture2D(image)) {
if (SharedImageImpl::IsYuv(image)) {
// A YUV image is CONVERTED into the name's own RGBA8 level at each frame's first use
// (SharedImageImpl::ConvertYuvForSampling), never made its storage.
while (g_GLESFuncs.glGetError() != GL_NO_ERROR) {
}
g_GLESFuncs.glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, static_cast<GLsizei>(record.Desc.Width),
static_cast<GLsizei>(record.Desc.Height));
if (g_GLESFuncs.glGetError() != GL_NO_ERROR) {
MGLOG_E_ONCE("Shared image texture %u: no %ux%u RGBA8 storage for YUV image %llu; left empty",
record.Desc.GlNameForDiag, record.Desc.Width, record.Desc.Height,
static_cast<unsigned long long>(imageId));
return true;
}
} else if (!SharedImageImpl::TargetBoundTexture2D(image)) {
MGLOG_E_ONCE("Shared image texture %u: glEGLImageTargetTexture2DOES refused image %llu; left empty",
record.Desc.GlNameForDiag, static_cast<unsigned long long>(imageId));
return true;
@@ -9042,6 +9051,10 @@ namespace MobileGL::MG_Backend::DirectGLES {
// before the command this sync is for, and the session's frame notes the read.
if (m_sharedImage != nullptr) SharedImageImpl::AcquireForSampling(m_sharedImage);
ReplaySharedImageUploads(*pushedStorage);
// A YUV image is converted into the name at the frame's first use.
if (m_sharedImage != nullptr && SharedImageImpl::IsYuv(m_sharedImage))
SharedImageImpl::ConvertYuvForSampling(m_sharedImage, GetBackendTextureId(),
pushedStorage->Desc.Width, pushedStorage->Desc.Height);
return;
}
// P5e (tx2), CONTRACT-P5E §5.2 (scout G-S2-2): THE VIEW TEST MOVES BEHIND THE
@@ -1944,6 +1944,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
switch (target) {
case TextureTarget::Texture1D:
case TextureTarget::TextureRectangle:
case TextureTarget::External:
return TextureTarget::Texture2D;
case TextureTarget::Texture1DArray:
return TextureTarget::Texture2DArray;
@@ -422,6 +422,14 @@ namespace MobileGL::MG_Backend::DirectVulkan {
static_cast<unsigned long long>(m_magmaSession->Id()));
}
Bool BackendObject_DirectVulkan::LatchIfGpuFaulted() {
#if MOBILEGL_BUILD_DISAGGREGATED
return pVulkanRenderer != nullptr && pVulkanRenderer->LatchIfGpuHung("apply");
#else
return false;
#endif
}
void BackendObject_DirectVulkan::Initialize() {
#if MOBILEGL_BUILD_DISAGGREGATED
VkBufferManager::RegisterWireResourceOps();
@@ -28,6 +28,8 @@ namespace MobileGL::MG_Backend::DirectVulkan {
void Initialize() override;
void BindSessionStateToThisThread() override;
// The GPU hang watch's verdict on this session's device (VulkanRenderer::LatchIfGpuHung).
Bool LatchIfGpuFaulted() override;
void OnClientContextBound(Uint64 token) override;
Bool InitWindowSurface() override;
Bool InitCapabilities() override;
@@ -66,7 +66,11 @@ namespace MobileGL::MG_Backend::DirectVulkan {
FreeRetiredCommandBuffers(frame);
}
vkFreeCommandBuffers(device, commandPool, frameCount * 2, commandBuffers.data());
if (!m_spareCommandBuffers.empty())
vkFreeCommandBuffers(device, commandPool, static_cast<Uint32>(m_spareCommandBuffers.size()),
m_spareCommandBuffers.data());
}
m_spareCommandBuffers.clear();
m_frames.clear();
currentFrameIndex = 0;
m_device = VK_NULL_HANDLE;
@@ -362,21 +366,16 @@ namespace MobileGL::MG_Backend::DirectVulkan {
MOBILEGL_ASSERT(!frame.isPreCommandRecording,
"RetireCurrentCommandBuffer called while the pre-pass stream is still recording");
VkCommandBufferAllocateInfo allocInfo{};
allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
allocInfo.commandPool = m_commandPool;
allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
allocInfo.commandBufferCount = 1;
VkCommandBuffer replacement = VK_NULL_HANDLE;
VkResult result = vkAllocateCommandBuffers(m_device, &allocInfo, &replacement);
VkResult result = TakeCommandBuffer(replacement);
if (result != VK_SUCCESS) {
return result;
}
if (retirePreCommandBuffer) {
VkCommandBuffer preReplacement = VK_NULL_HANDLE;
result = vkAllocateCommandBuffers(m_device, &allocInfo, &preReplacement);
result = TakeCommandBuffer(preReplacement);
if (result != VK_SUCCESS) {
vkFreeCommandBuffers(m_device, m_commandPool, 1, &replacement);
RecycleCommandBuffer(replacement);
return result;
}
frame.retiredCommandBuffers.push_back({frame.preCommandBuffer, frame.lastSubmitIndex});
@@ -411,8 +410,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
SizeT completedCount = 0;
while (completedCount < frame.retiredCommandBuffers.size() &&
frame.retiredCommandBuffers[completedCount].submitIndex <= completedSubmitIndex) {
vkFreeCommandBuffers(m_device, m_commandPool, 1,
&frame.retiredCommandBuffers[completedCount].commandBuffer);
RecycleCommandBuffer(frame.retiredCommandBuffers[completedCount].commandBuffer);
++completedCount;
}
if (completedCount > 0) {
@@ -422,6 +420,31 @@ namespace MobileGL::MG_Backend::DirectVulkan {
}
}
VkResult FrameContext::TakeCommandBuffer(VkCommandBuffer& out) {
if (!m_spareCommandBuffers.empty()) {
out = m_spareCommandBuffers.back();
m_spareCommandBuffers.pop_back();
return VK_SUCCESS;
}
VkCommandBufferAllocateInfo allocInfo{};
allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
allocInfo.commandPool = m_commandPool;
allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
allocInfo.commandBufferCount = 1;
return vkAllocateCommandBuffers(m_device, &allocInfo, &out);
}
void FrameContext::RecycleCommandBuffer(VkCommandBuffer commandBuffer) {
// A handful covers the buffers in flight between two completions; beyond that, free.
constexpr SizeT kSpareCommandBuffers = 8;
if (commandBuffer == VK_NULL_HANDLE) return;
if (m_spareCommandBuffers.size() < kSpareCommandBuffers) {
m_spareCommandBuffers.push_back(commandBuffer);
return;
}
vkFreeCommandBuffers(m_device, m_commandPool, 1, &commandBuffer);
}
void FrameContext::FreeAllRetiredCommandBuffers() {
for (auto& frame : m_frames) {
FreeRetiredCommandBuffers(frame);
@@ -150,5 +150,10 @@ namespace MobileGL::MG_Backend::DirectVulkan {
// Stored at Initialize for retired-command-buffer management.
VkDevice m_device = VK_NULL_HANDLE;
VkCommandPool m_commandPool = VK_NULL_HANDLE;
// Completed command buffers kept for the next retirement instead of being freed and
// reallocated: the pool resets a buffer implicitly at its next vkBeginCommandBuffer.
Vector<VkCommandBuffer> m_spareCommandBuffers;
VkResult TakeCommandBuffer(VkCommandBuffer& out);
void RecycleCommandBuffer(VkCommandBuffer commandBuffer);
};
} // namespace MobileGL::MG_Backend::DirectVulkan
@@ -0,0 +1,202 @@
// MobileGL - MobileGL/MG_Backend/DirectVulkan/Renderer/GpuProgressMarkers.cpp
// Copyright (c) 2025-2026 MobileGL-Dev
// Licensed under the GNU Lesser General Public License v3.0:
// https://www.gnu.org/licenses/gpl-3.0.txt
// https://www.gnu.org/licenses/lgpl-3.0.txt
// SPDX-License-Identifier: LGPL-3.0-only
// End of Source File Header
#include "GpuProgressMarkers.h"
#include <MG_Util/Debug/Log.h>
namespace MobileGL::MG_Backend::DirectVulkan {
namespace Watch = MG_Backend::GpuHangWatch;
void GpuProgressMarkers::Init(VkDevice device, Uint32 queueFamilyIndex) {
if (m_device != VK_NULL_HANDLE || device == VK_NULL_HANDLE) return;
if (Watch::BudgetMs() == 0) {
MGLOG_I("Magma: GPU hang watch off (MOBILEGL_GPU_HANG_BUDGET_MS=0)");
return;
}
VkCommandPoolCreateInfo poolInfo{VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO};
poolInfo.queueFamilyIndex = queueFamilyIndex;
if (vkCreateCommandPool(device, &poolInfo, nullptr, &m_pool) != VK_SUCCESS) {
MGLOG_W("Magma: GPU hang watch off - no command pool for its markers");
m_pool = VK_NULL_HANDLE;
return;
}
m_device = device;
{
const std::lock_guard<std::mutex> lock(m_mutex);
if (!AddSlot()) {
MGLOG_W("Magma: GPU hang watch off - the device cannot make its progress markers");
vkDestroyCommandPool(m_device, m_pool, nullptr);
m_pool = VK_NULL_HANDLE;
m_device = VK_NULL_HANDLE;
return;
}
}
m_watch = Watch::Register(this, &Sample, &OnHung);
MGLOG_I("Magma: GPU hang watch on - a submission the GPU started and has not finished after %u ms ends "
"its session",
Watch::BudgetMs());
}
void GpuProgressMarkers::Shutdown() {
// The watch first, outside m_mutex: Unregister waits out a sample in progress, which takes it.
Watch::Unregister(m_watch);
m_watch = 0;
if (m_device == VK_NULL_HANDLE) return;
const std::lock_guard<std::mutex> lock(m_mutex);
for (Slot& slot : m_slots) {
if (slot.Started != VK_NULL_HANDLE) vkDestroyEvent(m_device, slot.Started, nullptr);
if (slot.Finished != VK_NULL_HANDLE) vkDestroyEvent(m_device, slot.Finished, nullptr);
}
m_slots.clear();
if (m_pool != VK_NULL_HANDLE) vkDestroyCommandPool(m_device, m_pool, nullptr); // frees the buffers
m_pool = VK_NULL_HANDLE;
m_device = VK_NULL_HANDLE;
}
Bool GpuProgressMarkers::AddSlot() {
if (m_slots.size() >= kMaxSlots) return false;
Slot slot;
const VkEventCreateInfo eventInfo{VK_STRUCTURE_TYPE_EVENT_CREATE_INFO};
VkCommandBuffer buffers[2] = {VK_NULL_HANDLE, VK_NULL_HANDLE};
VkCommandBufferAllocateInfo allocInfo{VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO};
allocInfo.commandPool = m_pool;
allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
allocInfo.commandBufferCount = 2;
const auto fail = [&] {
if (slot.Started != VK_NULL_HANDLE) vkDestroyEvent(m_device, slot.Started, nullptr);
if (slot.Finished != VK_NULL_HANDLE) vkDestroyEvent(m_device, slot.Finished, nullptr);
if (buffers[0] != VK_NULL_HANDLE) vkFreeCommandBuffers(m_device, m_pool, 2, buffers);
return false;
};
if (vkCreateEvent(m_device, &eventInfo, nullptr, &slot.Started) != VK_SUCCESS ||
vkCreateEvent(m_device, &eventInfo, nullptr, &slot.Finished) != VK_SUCCESS ||
vkAllocateCommandBuffers(m_device, &allocInfo, buffers) != VK_SUCCESS)
return fail();
// Prerecorded once and resubmitted for every submission the slot brackets; SIMULTANEOUS_USE
// because the slot is reused once its `finished` event is set, which can be a moment before
// the submission that set it formally leaves the pending state.
VkCommandBufferBeginInfo beginInfo{VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO};
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;
if (vkBeginCommandBuffer(buffers[0], &beginInfo) != VK_SUCCESS) return fail();
vkCmdSetEvent(buffers[0], slot.Started, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT);
if (vkEndCommandBuffer(buffers[0]) != VK_SUCCESS) return fail();
if (vkBeginCommandBuffer(buffers[1], &beginInfo) != VK_SUCCESS) return fail();
vkCmdSetEvent(buffers[1], slot.Finished, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
if (vkEndCommandBuffer(buffers[1]) != VK_SUCCESS) return fail();
slot.Begin = buffers[0];
slot.End = buffers[1];
m_slots.push_back(slot);
return true;
}
GpuProgressMarkers::Bracket GpuProgressMarkers::Acquire() {
Bracket bracket;
if (m_device == VK_NULL_HANDLE) return bracket;
const std::lock_guard<std::mutex> lock(m_mutex);
if (m_lost) return bracket;
Slot* free = nullptr;
for (Slot& slot : m_slots) {
if (slot.Claimed) continue;
// Retire what finished since the watch last looked: frames come faster than its polls.
if (slot.InFlight && vkGetEventStatus(m_device, slot.Finished) == VK_EVENT_SET) slot.InFlight = false;
if (!slot.InFlight && free == nullptr) free = &slot;
}
if (free == nullptr) {
if (!AddSlot()) return bracket;
free = &m_slots.back();
}
if (free->Dirty) {
if (vkResetEvent(m_device, free->Started) != VK_SUCCESS ||
vkResetEvent(m_device, free->Finished) != VK_SUCCESS)
return bracket;
free->Dirty = false;
}
free->Claimed = true;
bracket.Begin = free->Begin;
bracket.End = free->End;
bracket.Slot = static_cast<Uint32>(free - m_slots.data());
return bracket;
}
void GpuProgressMarkers::Submitted(const Bracket& bracket) {
if (!bracket.Valid()) return;
const std::lock_guard<std::mutex> lock(m_mutex);
if (bracket.Slot >= m_slots.size()) return;
Slot& slot = m_slots[bracket.Slot];
slot.Claimed = false;
slot.InFlight = true;
slot.Dirty = true;
slot.Id = m_nextId++;
}
void GpuProgressMarkers::Abandon(const Bracket& bracket) {
if (!bracket.Valid()) return;
const std::lock_guard<std::mutex> lock(m_mutex);
if (bracket.Slot >= m_slots.size()) return;
// A failed vkQueueSubmit may still have consumed nothing or something: reset before reuse.
m_slots[bracket.Slot].Claimed = false;
m_slots[bracket.Slot].Dirty = true;
}
Bool GpuProgressMarkers::Sample(void* self, Vector<MG_Backend::GpuHangWatch::SubmissionProgress>& out) {
auto* markers = static_cast<GpuProgressMarkers*>(self);
const std::lock_guard<std::mutex> lock(markers->m_mutex);
if (markers->m_lost || markers->m_device == VK_NULL_HANDLE) return false;
for (Slot& slot : markers->m_slots) {
if (!slot.InFlight) continue;
const VkResult finished = vkGetEventStatus(markers->m_device, slot.Finished);
const VkResult started = vkGetEventStatus(markers->m_device, slot.Started);
if ((finished != VK_EVENT_SET && finished != VK_EVENT_RESET) ||
(started != VK_EVENT_SET && started != VK_EVENT_RESET)) {
// VK_ERROR_DEVICE_LOST: the driver already reset the device and says so itself.
markers->m_lost = true;
return false;
}
if (finished == VK_EVENT_SET) {
slot.InFlight = false;
continue;
}
out.push_back({slot.Id, started == VK_EVENT_SET, false});
}
return true;
}
void GpuProgressMarkers::OnHung(void* self, Uint64 submissionId, Uint64 runningMs) {
auto* markers = static_cast<GpuProgressMarkers*>(self);
markers->m_hungRunningMs.store(runningMs, std::memory_order_release);
markers->m_hung.store(true, std::memory_order_release);
MGLOG_E("Magma: submission %llu of this session's VkDevice has run %llu ms on the GPU and has not finished - "
"it is what stops the GPU for every process; the session ends at its next record and submits "
"nothing more (budget %u ms, MOBILEGL_GPU_HANG_BUDGET_MS)",
static_cast<unsigned long long>(submissionId), static_cast<unsigned long long>(runningMs),
MG_Backend::GpuHangWatch::BudgetMs());
}
VkResult GpuProgressMarkers::SubmitBracketed(VkQueue queue, VkSubmitInfo& info, VkFence fence,
const Bracket& bracket) {
constexpr Uint32 kMaxOwn = 6;
if (!bracket.Valid() || info.commandBufferCount == 0 || info.commandBufferCount > kMaxOwn)
return vkQueueSubmit(queue, 1, &info, fence);
VkCommandBuffer buffers[kMaxOwn + 2];
Uint32 count = 0;
buffers[count++] = bracket.Begin;
for (Uint32 i = 0; i < info.commandBufferCount; ++i) buffers[count++] = info.pCommandBuffers[i];
buffers[count++] = bracket.End;
const VkCommandBuffer* own = info.pCommandBuffers;
const Uint32 ownCount = info.commandBufferCount;
info.pCommandBuffers = buffers;
info.commandBufferCount = count;
const VkResult result = vkQueueSubmit(queue, 1, &info, fence);
info.pCommandBuffers = own;
info.commandBufferCount = ownCount;
return result;
}
} // namespace MobileGL::MG_Backend::DirectVulkan
@@ -0,0 +1,95 @@
// MobileGL - MobileGL/MG_Backend/DirectVulkan/Renderer/GpuProgressMarkers.h
// Copyright (c) 2025-2026 MobileGL-Dev
// Licensed under the GNU Lesser General Public License v3.0:
// https://www.gnu.org/licenses/gpl-3.0.txt
// https://www.gnu.org/licenses/lgpl-3.0.txt
// SPDX-License-Identifier: LGPL-3.0-only
// End of Source File Header
#pragma once
#include "../VkIncludes.h"
#include <Includes.h>
#include <MG_Backend/GpuHangWatch.h>
#include <atomic>
#include <mutex>
namespace MobileGL::MG_Backend::DirectVulkan {
// WHICH OF THIS DEVICE'S SUBMISSIONS THE GPU HAS STARTED, AND WHICH IT HAS FINISHED
// (GpuHangWatch.h says why that is the whole attribution of a GPU hang).
//
// A frame's submission is bracketed by two prerecorded one-command buffers: the first sets a
// `started` event at TOP_OF_PIPE, the last sets a `finished` event once everything before it in
// the queue has completed. The host reads both with vkGetEventStatus from the watch thread - no
// fence is shared with the renderer's own waits and resets. On Adreno 750 a submission whose
// fragment shader never ends reads started=SET from its first poll until the kernel resets the
// GPU, and a submission queued behind it reads started=RESET for the whole stall.
//
// The renderer's thread brackets submissions (Acquire / Submitted / Abandon) and asks Hung(); the
// watch thread samples. Everything Vulkan the two share - the events - is touched under m_mutex.
class GpuProgressMarkers {
public:
struct Bracket {
VkCommandBuffer Begin = VK_NULL_HANDLE;
VkCommandBuffer End = VK_NULL_HANDLE;
Uint32 Slot = ~0u;
Bool Valid() const { return Slot != ~0u; }
};
GpuProgressMarkers() = default;
~GpuProgressMarkers() { Shutdown(); }
GpuProgressMarkers(const GpuProgressMarkers&) = delete;
GpuProgressMarkers& operator=(const GpuProgressMarkers&) = delete;
// Off (no brackets, never hung) when the budget is 0 or the device cannot make events.
void Init(VkDevice device, Uint32 queueFamilyIndex);
// Before the device goes; its queue must be idle. Stops the watch first.
void Shutdown();
// The two buffers to put first and last in one submission (an invalid bracket when off or
// when every slot is in flight: the submission then goes unwatched).
Bracket Acquire();
// The submission carrying `bracket` was accepted by vkQueueSubmit / was not submitted.
void Submitted(const Bracket& bracket);
void Abandon(const Bracket& bracket);
// The watch named one of this device's submissions as hanging the GPU.
Bool Hung() const { return m_hung.load(std::memory_order_acquire); }
Uint64 HungRunningMs() const { return m_hungRunningMs.load(std::memory_order_acquire); }
Bool Active() const { return m_watch != 0; }
// vkQueueSubmit of `info` with `bracket` wrapped around its command buffers (which it puts
// back before returning). At most 6 buffers of the caller's.
static VkResult SubmitBracketed(VkQueue queue, VkSubmitInfo& info, VkFence fence, const Bracket& bracket);
private:
struct Slot {
VkEvent Started = VK_NULL_HANDLE;
VkEvent Finished = VK_NULL_HANDLE;
VkCommandBuffer Begin = VK_NULL_HANDLE;
VkCommandBuffer End = VK_NULL_HANDLE;
Uint64 Id = 0;
Bool InFlight = false; // submitted and not yet seen finished
Bool Claimed = false; // handed out by Acquire, not yet submitted or abandoned
Bool Dirty = false; // its events were set by a past submission: reset before reuse
};
static constexpr Uint32 kMaxSlots = 16;
Bool AddSlot();
static Bool Sample(void* self, Vector<MG_Backend::GpuHangWatch::SubmissionProgress>& out);
static void OnHung(void* self, Uint64 submissionId, Uint64 runningMs);
std::mutex m_mutex;
VkDevice m_device = VK_NULL_HANDLE;
VkCommandPool m_pool = VK_NULL_HANDLE;
Vector<Slot> m_slots;
Uint64 m_nextId = 1;
Uint64 m_watch = 0;
Bool m_lost = false;
std::atomic<Bool> m_hung{false};
std::atomic<Uint64> m_hungRunningMs{0};
};
} // namespace MobileGL::MG_Backend::DirectVulkan
@@ -2529,6 +2529,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
return SamplerNumericDomain::UnsignedInteger;
case GL_SAMPLER_1D:
case GL_SAMPLER_2D:
case GL_SAMPLER_EXTERNAL_OES:
case GL_SAMPLER_3D:
case GL_SAMPLER_CUBE:
case GL_SAMPLER_2D_RECT:
@@ -2719,6 +2720,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
case GL_UNSIGNED_INT_IMAGE_CUBE_MAP_ARRAY:
return TextureTarget::TextureCubeMapArray;
case GL_SAMPLER_2D:
case GL_SAMPLER_EXTERNAL_OES: // a 2D texture below the frontend
case GL_SAMPLER_2D_SHADOW:
case GL_INT_SAMPLER_2D:
case GL_UNSIGNED_INT_SAMPLER_2D:
@@ -0,0 +1,27 @@
// MobileGL - MobileGL/MG_Backend/DirectVulkan/Renderer/RenderPassGuard.h
// Copyright (c) 2025-2026 MobileGL-Dev
// Licensed under the GNU Lesser General Public License v3.0:
// https://www.gnu.org/licenses/gpl-3.0.txt
// https://www.gnu.org/licenses/lgpl-3.0.txt
// SPDX-License-Identifier: LGPL-3.0-only
// End of Source File Header
#pragma once
#include "../VkIncludes.h"
#include <Includes.h>
namespace MobileGL::MG_Backend::DirectVulkan {
// Ends the render pass that is active on `commandBuffer`, if there is one. Called right before
// recording a command a render pass may not contain (a barrier, a copy, a blit, a clear of an
// image) into a command buffer that may hold an open pass: the wire draw path keeps its pass
// open across consecutive draws to the same attachments, so the resource work that falls
// between two draws must close it first. A pass on another command buffer is left alone.
void EndActiveRenderPassOn(VkCommandBuffer commandBuffer);
// A counter that moves whenever images may have been written since it was last read: a render
// pass ended (its attachments), resource work recorded outside a pass (copies, clears, blits,
// dispatches all close the pass first), or a draw with storage images. A sampled image needs
// the memory barrier that makes earlier writes visible only when this moved since the last one.
Uint64 WireImageWriteEpoch();
void BumpWireImageWriteEpoch();
} // namespace MobileGL::MG_Backend::DirectVulkan
@@ -6,6 +6,7 @@
// SPDX-License-Identifier: LGPL-3.0-only
// End of Source File Header
#include "RenderPassGuard.h"
#include "UniformManager.h"
#include "MG_Backend/DirectVulkan/DirectVulkanResourceState.h"
@@ -251,11 +252,18 @@ namespace MobileGL::MG_Backend::DirectVulkan {
WireDescriptorFatal("image-descriptor-transition");
// A second descriptor/draw can keep GENERAL after a shader write. A layout
// equality fast return is not a memory dependency for that prior write.
VkMemoryBarrier memory{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
memory.srcAccessMask = VK_ACCESS_MEMORY_WRITE_BIT;
memory.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT;
vkCmdPipelineBarrier(commandBuffer, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
0, 1, &memory, 0, nullptr, 0, nullptr);
// Only when something may have written an image since the last such barrier
// (WireImageWriteEpoch): with nothing written in between, consecutive draws keep their
// render pass open instead of closing it for a barrier that orders nothing.
if (WireImageWriteEpoch() != m_wireImageBarrierEpoch) {
VkMemoryBarrier memory{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
memory.srcAccessMask = VK_ACCESS_MEMORY_WRITE_BIT;
memory.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT;
EndActiveRenderPassOn(commandBuffer);
vkCmdPipelineBarrier(commandBuffer, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
0, 1, &memory, 0, nullptr, 0, nullptr);
m_wireImageBarrierEpoch = WireImageWriteEpoch();
}
VkImageViewCreateInfo info{VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO};
info.image = resource->image;
info.viewType = type;
@@ -3198,7 +3206,8 @@ namespace MobileGL::MG_Backend::DirectVulkan {
const auto* resource = m_textureManager->SyncTextureResourceByHandle(handle, false, storage);
if (resource == nullptr) return false;
// Another session writes it: the renderer acquires it before this use.
if (resource->sharedImageId != 0) m_textureManager->NoteSharedImageUse(*resource);
if (resource->sharedImageId != 0 || resource->yuvImageId != 0)
m_textureManager->NoteSharedImageUse(*resource);
}
}
}
@@ -660,5 +660,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
m_samplerResolveMemoHighWater = binding + 1;
}
}
// WireImageWriteEpoch() when ResolveWireImageDescriptor last made image writes visible.
mutable Uint64 m_wireImageBarrierEpoch = 0;
};
} // namespace MobileGL::MG_Backend::DirectVulkan
@@ -6,6 +6,7 @@
// SPDX-License-Identifier: LGPL-3.0-only
// End of Source File Header
#include "RenderPassGuard.h"
#include "VkBufferManager.h"
#include "../DirectVulkan.h"
#include "VulkanRenderer.h"
@@ -337,6 +338,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
VkMemoryBarrier barrier{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
barrier.srcAccessMask = VK_ACCESS_MEMORY_WRITE_BIT;
barrier.dstAccessMask = VK_ACCESS_HOST_READ_BIT | VK_ACCESS_HOST_WRITE_BIT;
EndActiveRenderPassOn(commands);
vkCmdPipelineBarrier(commands, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_HOST_BIT,
0, 1, &barrier, 0, nullptr, 0, nullptr);
// P8-D: counted here, past the idle early-out, so `whw` is waits TAKEN, not calls.
@@ -531,13 +533,16 @@ namespace MobileGL::MG_Backend::DirectVulkan {
VkMemoryBarrier before{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
before.srcAccessMask = VK_ACCESS_MEMORY_WRITE_BIT | VK_ACCESS_HOST_WRITE_BIT;
before.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_TRANSFER_WRITE_BIT;
EndActiveRenderPassOn(commands);
vkCmdPipelineBarrier(commands, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT | VK_PIPELINE_STAGE_HOST_BIT,
VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 1, &before, 0, nullptr, 0, nullptr);
const VkBufferCopy copy{offset, dst.offset, size};
EndActiveRenderPassOn(commands);
vkCmdCopyBuffer(commands, resource->buffer.GetHandle(), dst.buffer, 1, &copy);
VkMemoryBarrier after{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
after.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
after.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT;
EndActiveRenderPassOn(commands);
vkCmdPipelineBarrier(commands, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
0, 1, &after, 0, nullptr, 0, nullptr);
resource->lastUseSerial = m_frameSerial;
@@ -575,22 +580,27 @@ namespace MobileGL::MG_Backend::DirectVulkan {
VkMemoryBarrier before{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
before.srcAccessMask = VK_ACCESS_MEMORY_WRITE_BIT | VK_ACCESS_HOST_WRITE_BIT;
before.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_TRANSFER_WRITE_BIT;
EndActiveRenderPassOn(commands);
vkCmdPipelineBarrier(commands, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT | VK_PIPELINE_STAGE_HOST_BIT,
VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 1, &before, 0, nullptr, 0, nullptr);
const VkBufferCopy widen{alignedOffset, staging.offset, stagedSize};
EndActiveRenderPassOn(commands);
vkCmdCopyBuffer(commands, resource->buffer.GetHandle(), staging.buffer, 1, &widen);
// The shift reads what the widening copy just wrote, so it needs its own edge even
// though both halves are transfers on one command buffer.
VkMemoryBarrier staged{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
staged.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
staged.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
EndActiveRenderPassOn(commands);
vkCmdPipelineBarrier(commands, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
0, 1, &staged, 0, nullptr, 0, nullptr);
const VkBufferCopy shift{staging.offset + headPad, dst.offset + dstSkip, size};
EndActiveRenderPassOn(commands);
vkCmdCopyBuffer(commands, staging.buffer, dst.buffer, 1, &shift);
VkMemoryBarrier after{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
after.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
after.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT;
EndActiveRenderPassOn(commands);
vkCmdPipelineBarrier(commands, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
0, 1, &after, 0, nullptr, 0, nullptr);
resource->lastUseSerial = m_frameSerial;
@@ -908,14 +918,18 @@ namespace MobileGL::MG_Backend::DirectVulkan {
VkMemoryBarrier hostWrites{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
hostWrites.srcAccessMask = VK_ACCESS_HOST_WRITE_BIT;
hostWrites.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_TRANSFER_WRITE_BIT;
EndActiveRenderPassOn(commands);
vkCmdPipelineBarrier(commands, VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 1,
&hostWrites, 0, nullptr, 0, nullptr);
VkBufferCopy region{0, 0, kWords * 4};
EndActiveRenderPassOn(commands);
vkCmdCopyBuffer(commands, imported.buffer, staging.buffer, 1, &region);
EndActiveRenderPassOn(commands);
vkCmdFillBuffer(commands, imported.buffer, kFillAt, kWords * 4, kFillWord);
VkMemoryBarrier toHost{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
toHost.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
toHost.dstAccessMask = VK_ACCESS_HOST_READ_BIT;
EndActiveRenderPassOn(commands);
vkCmdPipelineBarrier(commands, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_HOST_BIT, 0, 1, &toHost,
0, nullptr, 0, nullptr);
vkEndCommandBuffer(commands);
@@ -965,11 +979,15 @@ namespace MobileGL::MG_Backend::DirectVulkan {
verdict = "steady: the second submission could not be recorded";
break;
}
EndActiveRenderPassOn(commands);
vkCmdPipelineBarrier(commands, VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 1,
&hostWrites, 0, nullptr, 0, nullptr);
VkBufferCopy steadyRegion{kSteadyAt, 0, kWords * 4};
EndActiveRenderPassOn(commands);
vkCmdCopyBuffer(commands, imported.buffer, staging.buffer, 1, &steadyRegion);
EndActiveRenderPassOn(commands);
vkCmdFillBuffer(commands, imported.buffer, kFillAt, kWords * 4, kSteadyFillWord);
EndActiveRenderPassOn(commands);
vkCmdPipelineBarrier(commands, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_HOST_BIT, 0, 1, &toHost,
0, nullptr, 0, nullptr);
vkEndCommandBuffer(commands);
@@ -1488,6 +1506,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
VkMemoryBarrier beforeBarrier{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
beforeBarrier.srcAccessMask = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT;
beforeBarrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
EndActiveRenderPassOn(commandBuffer);
vkCmdPipelineBarrier(commandBuffer, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 1,
&beforeBarrier, 0, nullptr, 0, nullptr);
@@ -1495,11 +1514,13 @@ namespace MobileGL::MG_Backend::DirectVulkan {
region.srcOffset = staging.offset;
region.dstOffset = static_cast<VkDeviceSize>(offset);
region.size = static_cast<VkDeviceSize>(size);
EndActiveRenderPassOn(commandBuffer);
vkCmdCopyBuffer(commandBuffer, staging.buffer, resource.buffer.GetHandle(), 1, &region);
VkMemoryBarrier afterBarrier{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
afterBarrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
afterBarrier.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT;
EndActiveRenderPassOn(commandBuffer);
vkCmdPipelineBarrier(commandBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, 1,
&afterBarrier, 0, nullptr, 0, nullptr);
@@ -1534,6 +1555,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
VkMemoryBarrier beforeBarrier{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
beforeBarrier.srcAccessMask = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT;
beforeBarrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
EndActiveRenderPassOn(commandBuffer);
vkCmdPipelineBarrier(commandBuffer, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 1,
&beforeBarrier, 0, nullptr, 0, nullptr);
@@ -1541,11 +1563,13 @@ namespace MobileGL::MG_Backend::DirectVulkan {
region.srcOffset = staging.offset;
region.dstOffset = static_cast<VkDeviceSize>(offset);
region.size = static_cast<VkDeviceSize>(size);
EndActiveRenderPassOn(commandBuffer);
vkCmdCopyBuffer(commandBuffer, staging.buffer, resource.buffer.GetHandle(), 1, &region);
VkMemoryBarrier afterBarrier{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
afterBarrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
afterBarrier.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT;
EndActiveRenderPassOn(commandBuffer);
vkCmdPipelineBarrier(commandBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, 1,
&afterBarrier, 0, nullptr, 0, nullptr);
@@ -7,6 +7,8 @@
// End of Source File Header
#include "VkRenderPassManager.h"
#include "RenderPassGuard.h"
#include <atomic>
#include "MG_Impl/GLImpl/Framebuffer/GL_Framebuffer.h"
#include "MG_State/GLState/TextureState/TextureObject2D.h"
@@ -1666,6 +1668,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
s_clearManager.Get()->PopPendingClear(pending.key);
}
}
s_activeRenderPass.Get().commandBuffer = commandBuffer;
s_activeRenderPass.Get().hash = renderPassEntry.hash;
s_activeRenderPass.Get().compatibilityHash = renderPassEntry.compatibilityHash;
s_activeRenderPass.Get().trackedAttachmentLayouts = renderPassEntry.trackedAttachmentLayouts;
@@ -1676,15 +1679,24 @@ namespace MobileGL::MG_Backend::DirectVulkan {
}
Bool VkRenderPassManager::EndRenderPass(VkCommandBuffer commandBuffer) {
BumpWireImageWriteEpoch();
auto* activeRenderPass = GetActiveRenderPass();
Vector<TrackedAttachmentLayoutInfo> trackedAttachmentLayouts;
if (activeRenderPass != nullptr) {
trackedAttachmentLayouts = Move(activeRenderPass->trackedAttachmentLayouts);
}
vkCmdEndRenderPass(commandBuffer);
// Layout reconciliation below may record barriers, whose guard closes an active
// pass. Detach the ended pass first so that guard cannot recursively end it again.
s_activeRenderPass.Get() = {};
s_hasActiveRenderPass.Get() = false;
// The fast-path memo reuses the ACTIVE render pass; once the pass ends it must not carry
// over (the next span may be a different FBO resolved before its render pass is begun).
if (s_renderPassManager.Get() != nullptr) {
s_renderPassManager.Get()->m_rpFastValid = false;
}
if (activeRenderPass != nullptr && !activeRenderPass->trackedAttachmentLayouts.empty()) {
for (const auto& trackedAttachment : activeRenderPass->trackedAttachmentLayouts) {
if (!trackedAttachmentLayouts.empty()) {
for (const auto& trackedAttachment : trackedAttachmentLayouts) {
switch (trackedAttachment.target) {
case TrackedAttachmentTarget::Texture:
MOBILEGL_ASSERT(s_textureManager.Get() != nullptr, "EndRenderPass: texture manager is null");
@@ -1726,12 +1738,23 @@ namespace MobileGL::MG_Backend::DirectVulkan {
}
}
}
s_activeRenderPass.Get() = {};
s_hasActiveRenderPass.Get() = false;
return true;
}
ActiveRenderPassInfo* VkRenderPassManager::GetActiveRenderPass() {
return s_hasActiveRenderPass.Get() ? &s_activeRenderPass.Get() : nullptr;
}
namespace {
std::atomic<Uint64> g_wireImageWriteEpoch{1};
}
Uint64 WireImageWriteEpoch() { return g_wireImageWriteEpoch.load(std::memory_order_relaxed); }
void BumpWireImageWriteEpoch() { g_wireImageWriteEpoch.fetch_add(1, std::memory_order_relaxed); }
void EndActiveRenderPassOn(VkCommandBuffer commandBuffer) {
BumpWireImageWriteEpoch();
const auto* active = VkRenderPassManager::GetActiveRenderPass();
if (active != nullptr && active->commandBuffer == commandBuffer && commandBuffer != VK_NULL_HANDLE)
VkRenderPassManager::EndRenderPass(commandBuffer);
}
} // namespace MobileGL::MG_Backend::DirectVulkan
@@ -185,6 +185,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
struct ActiveRenderPassInfo {
Uint64 hash = 0;
Uint64 compatibilityHash = 0;
VkCommandBuffer commandBuffer = VK_NULL_HANDLE;
Vector<TrackedAttachmentLayoutInfo> trackedAttachmentLayouts;
IntVec2 extent = {0, 0};
@@ -6,6 +6,7 @@
// SPDX-License-Identifier: LGPL-3.0-only
// End of Source File Header
#include "RenderPassGuard.h"
#include "VkTextureManager.h"
#include "ProgramFactory.h"
@@ -55,6 +56,22 @@ namespace MobileGL::MG_Backend::DirectVulkan {
};
static SessionLocal<SampledReadStages> s_sampledReadStagesSlot;
static VkImageUsageFlags ResolveTextureUsage(VkImageAspectFlags aspect,
VkFormatFeatureFlags formatFeatures,
Bool supportsStorageImage) {
VkImageUsageFlags usage = VK_IMAGE_USAGE_SAMPLED_BIT;
if (supportsStorageImage) usage |= VK_IMAGE_USAGE_STORAGE_BIT;
// A color aspect does not imply renderability (for example, RGB9_E5).
if ((aspect & VK_IMAGE_ASPECT_COLOR_BIT) &&
(formatFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT)) {
usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
}
if (aspect & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) {
usage |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
}
return usage;
}
static Uint32 ComputeFullMipLevelCount(const IntVec3& baseTexelSize) {
Int maxDimension = std::max<Int>(baseTexelSize.x(),
std::max<Int>(baseTexelSize.y(), std::max<Int>(baseTexelSize.z(), 1)));
@@ -376,6 +393,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
copyRegions.push_back(copy);
}
EndActiveRenderPassOn(commandBuffer);
vkCmdCopyImage(commandBuffer,
oldResource.image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
newResource.image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
@@ -1658,6 +1676,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
std::max(source->depth >> level, 1u)};
copyRegions.push_back(copy);
}
EndActiveRenderPassOn(commandBuffer);
vkCmdCopyImage(commandBuffer, source->image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, snapshot.image,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, static_cast<Uint32>(copyRegions.size()), copyRegions.data());
@@ -1772,6 +1791,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
// slices" but will read as "depth slice 0" once VK_KHR_maintenance9 is enabled. The
// validation layer warns about that literal 1 by name.
barrier.subresourceRange.layerCount = VK_REMAINING_ARRAY_LAYERS;
EndActiveRenderPassOn(commandBuffer);
vkCmdPipelineBarrier(commandBuffer, srcStageMask, dstStageMask, 0, 0, nullptr, 0, nullptr, 1, &barrier);
trackedLayout = newLayout;
@@ -2115,9 +2135,41 @@ namespace MobileGL::MG_Backend::DirectVulkan {
Bool VkTextureManager::SyncWireTextureShape(const MG_Pipe::MGPipeResourceRecord& record,
TextureResource& resource, Bool requireStorage) {
const MG_Pipe::MGPResourceDesc& desc = record.Desc;
// A YUV shared image is converted INTO the texture's own RGBA8 storage, which the rest of
// this function defines as for any texture; the image is only named here, and the
// renderer converts it at its first use in each frame (WireYuvImage.inc).
if (record.SharedImageId != resource.yuvImageId && !m_syncingYuvTextureStorage) {
// Respecified away from (or to another) YUV image: its source goes with it, once the
// GPU is done with it (a released resource's Reset is deferred).
if (resource.yuvSource != nullptr) {
TextureResource retired;
retired.yuvSource = std::move(resource.yuvSource);
DeferResourceRelease(Move(retired));
}
resource.yuvImageId = 0;
resource.yuvOwner.reset();
resource.yuvSource.reset();
resource.yuvConvertedFrame = 0;
}
if (record.SharedImageId != 0 && !m_syncingYuvTextureStorage) {
const auto yuv = MG_Remote::Server::SharedImages::Find(record.SharedImageId);
if (yuv != nullptr && MG_Remote::Server::SharedImages::FourccIsYuv(yuv->Fourcc)) {
m_syncingYuvTextureStorage = true;
const Bool defined = SyncWireTextureShape(record, resource, requireStorage);
m_syncingYuvTextureStorage = false;
if (!defined) return false;
if (resource.yuvImageId != record.SharedImageId) {
resource.yuvImageId = record.SharedImageId;
resource.yuvOwner = yuv;
resource.yuvSource.reset();
resource.yuvConvertedFrame = 0;
}
return true;
}
}
// Level 0 is a shared image: bind its buffer. An image that cannot be bound here leaves
// the texture the storage its NULL-data definition asked for, below.
if (record.SharedImageId != 0) {
if (record.SharedImageId != 0 && !m_syncingYuvTextureStorage) {
const SharedImageBind bound = SyncWireSharedImage(record, resource, requireStorage);
if (bound != SharedImageBind::Unresolved) return bound == SharedImageBind::Bound;
}
@@ -2220,13 +2272,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
}
VkImageUsageFlags desiredUsage =
VK_IMAGE_USAGE_SAMPLED_BIT | (supportsStorageImage ? VK_IMAGE_USAGE_STORAGE_BIT : 0) |
((aspect & VK_IMAGE_ASPECT_COLOR_BIT) &&
(formatProperties.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT)
? VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT : 0) |
(((aspect & VK_IMAGE_ASPECT_DEPTH_BIT) || (aspect & VK_IMAGE_ASPECT_STENCIL_BIT))
? VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
: 0);
ResolveTextureUsage(aspect, formatProperties.optimalTilingFeatures, supportsStorageImage);
// Multisample attachments are also transfer sources for vkCmdResolveImage.
desiredUsage |= VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
@@ -2702,9 +2748,11 @@ namespace MobileGL::MG_Backend::DirectVulkan {
stencilCopy.imageSubresource.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
stencilCopy.bufferOffset = stagingBase + item.offset + static_cast<VkDeviceSize>(texelCount) * 4;
const VkBufferImageCopy copies[2] = {depthCopy, stencilCopy};
EndActiveRenderPassOn(commandBuffer);
vkCmdCopyBufferToImage(commandBuffer, stagingBuffer, resource.image,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 2, copies);
} else {
EndActiveRenderPassOn(commandBuffer);
vkCmdCopyBufferToImage(commandBuffer, stagingBuffer, resource.image,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy);
}
@@ -3057,7 +3105,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
handle.Slot, handle.Gen);
return nullptr;
}
resource.sharedImageKey = resource.sharedImageId != 0 ? key : 0;
resource.sharedImageKey = resource.sharedImageId != 0 || resource.yuvImageId != 0 ? key : 0;
resource.syncedWireSerial = record.Serial;
return &resource;
}
@@ -3179,6 +3227,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
if ((aspect & copyAspect) == 0) continue;
copy.srcSubresource.aspectMask = copyAspect;
copy.dstSubresource.aspectMask = copyAspect;
EndActiveRenderPassOn(commandBuffer);
vkCmdCopyImage(commandBuffer, old.image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, grown.image,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy);
}
@@ -3474,12 +3523,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
}
VkImageUsageFlags desiredUsage =
VK_IMAGE_USAGE_SAMPLED_BIT |
(supportsStorageImage ? VK_IMAGE_USAGE_STORAGE_BIT : 0) |
((aspect & VK_IMAGE_ASPECT_COLOR_BIT) ? VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT : 0) |
(((aspect & VK_IMAGE_ASPECT_DEPTH_BIT) || (aspect & VK_IMAGE_ASPECT_STENCIL_BIT)) ?
VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT :
0);
ResolveTextureUsage(aspect, formatProperties.optimalTilingFeatures, supportsStorageImage);
if (!isMultisampleTexture) {
desiredUsage |= VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
}
@@ -3774,7 +3818,11 @@ namespace MobileGL::MG_Backend::DirectVulkan {
resource.sampledView == VK_NULL_HANDLE &&
resource.perMipViews.empty() && resource.perMipSampledViews.empty() &&
resource.attachmentViews.empty() && resource.alternateSampledViews.empty() &&
resource.storageImageViews.empty()) {
resource.storageImageViews.empty()
#if MOBILEGL_BUILD_DISAGGREGATED
&& resource.yuvSource == nullptr
#endif
) {
return;
}
@@ -4734,6 +4782,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
rectCopies[rectCopyCount++] = rectCopy;
runningOffset += static_cast<VkDeviceSize>(rect.TexelCount() * item.texelBytes);
}
EndActiveRenderPassOn(commandBuffer);
vkCmdCopyBufferToImage(commandBuffer, stagingBuffer, outResource.image,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, rectCopyCount, rectCopies.data());
continue;
@@ -4775,10 +4824,12 @@ namespace MobileGL::MG_Backend::DirectVulkan {
stencilCopy.imageSubresource.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
stencilCopy.bufferOffset = stagingBase + item.offset + static_cast<VkDeviceSize>(texelCount) * 4;
const VkBufferImageCopy copies[2] = {depthCopy, stencilCopy};
EndActiveRenderPassOn(commandBuffer);
vkCmdCopyBufferToImage(commandBuffer, stagingBuffer, outResource.image,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 2, copies);
continue;
}
EndActiveRenderPassOn(commandBuffer);
vkCmdCopyBufferToImage(commandBuffer, stagingBuffer, outResource.image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1, &copy);
}
@@ -372,6 +372,15 @@ public:
Bool sharedImageHeld = false;
Uint64 sharedImageGeneration = 0;
Uint64 sharedImageKey = 0;
// A YUV shared image (NV12/P010) is never the texture's image: the texture keeps its own
// RGBA8 level 0, into which the renderer converts the image at its first use in each frame
// (WireYuvImage.inc) - `yuvSource` being that conversion's import of the buffer, made on
// first use, and `yuvConvertedFrame` the frame it last ran in. `yuvOwner` holds the
// registry's image for as long as the texture names it.
Uint64 yuvImageId = 0;
SharedPtr<const void> yuvOwner;
SharedPtr<void> yuvSource;
Uint64 yuvConvertedFrame = 0;
#endif
TextureResource() = default;
@@ -415,6 +424,10 @@ public:
std::swap(this->sharedImageHeld, that.sharedImageHeld);
std::swap(this->sharedImageGeneration, that.sharedImageGeneration);
std::swap(this->sharedImageKey, that.sharedImageKey);
std::swap(this->yuvImageId, that.yuvImageId);
std::swap(this->yuvOwner, that.yuvOwner);
std::swap(this->yuvSource, that.yuvSource);
std::swap(this->yuvConvertedFrame, that.yuvConvertedFrame);
#endif
}
@@ -465,6 +478,10 @@ public:
sharedImageHeld = false;
sharedImageGeneration = 0;
sharedImageKey = 0;
yuvImageId = 0;
yuvOwner.reset();
yuvSource.reset();
yuvConvertedFrame = 0;
#endif
fullView = VK_NULL_HANDLE;
sampledView = VK_NULL_HANDLE;
@@ -882,6 +899,8 @@ private:
// Active only between BeginDrawSyncScope/EndDrawSyncScope; identities of
// textures already fully synced in the current draw (small N -> flat scan).
Bool m_drawSyncScopeActive = false;
// SyncWireTextureShape defining the RGBA8 storage a YUV shared image is converted into.
Bool m_syncingYuvTextureStorage = false;
// Per-draw sync memo: the identity plus the resolved resource pointer. The pointer is stable
// across rehash in the node-based m_textureResources and stays valid for the draw (a texture
// synced this draw is alive and is not erased mid-draw), so a repeat sync of the same texture
@@ -6,6 +6,7 @@
// SPDX-License-Identifier: LGPL-3.0-only
// End of Source File Header
#include "RenderPassGuard.h"
#include "VulkanRenderer.h"
#include "SubmitFencePrefix.h"
#include "MG_Util/X11/DisplayGuard.h"
@@ -3275,6 +3276,9 @@ void main() {
CreateAllocator();
CreateCommandPool();
#if MOBILEGL_BUILD_DISAGGREGATED
m_progressMarkers.Init(m_device, m_physicalDevice.queueFamilies.graphicsFamily);
#endif
// Frames-in-flight is a request, not a guarantee: it also seeds the swapchain image
// count (SwapchainObject clamps the hint into [minImageCount, maxImageCount]). Not every
@@ -3504,6 +3508,9 @@ void main() {
if (m_device != VK_NULL_HANDLE) {
VK_VERIFY(vkDeviceWaitIdle(m_device));
}
#if MOBILEGL_BUILD_DISAGGREGATED
m_progressMarkers.Shutdown();
#endif
// Parked surface targets (ActivateSurfaceTarget) go before the device and instance.
for (auto& [_, target] : m_parkedTargets) DestroyParkedTarget(target);
m_parkedTargets.clear();
@@ -3519,6 +3526,7 @@ void main() {
CollectWireObjects(m_submitCounter, true);
ClearAllWireDrawPassCaches();
DestroyWireColorBlitResources();
DestroyWireYuvResources();
DestroyWireDepthMipmapResources();
DestroyWireMultisampleResolveResources();
DestroySharedImagePresentTargets(false);
@@ -5193,6 +5201,7 @@ void main() {
static_cast<Uint32>(dstTexelSize.x()), static_cast<Uint32>(dstTexelSize.y())
};
EndActiveRenderPassOn(frame.commandBuffer);
vkCmdBeginRenderPass(frame.commandBuffer, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
VkViewport viewport{};
@@ -7822,6 +7831,7 @@ void main() {
}
MGLOG_D("DirectVulkan: glDispatchCompute(%u, %u, %u)", numGroupsX, numGroupsY, numGroupsZ);
EndActiveRenderPassOn(frame.commandBuffer);
vkCmdDispatch(frame.commandBuffer, numGroupsX, numGroupsY, numGroupsZ);
}
@@ -7899,6 +7909,7 @@ void main() {
}
MGLOG_D("DirectVulkan: glDispatchComputeIndirect(offset=%zu)", static_cast<SizeT>(indirect));
EndActiveRenderPassOn(frame.commandBuffer);
vkCmdDispatchIndirect(frame.commandBuffer, slice.buffer, slice.offset + static_cast<VkDeviceSize>(indirect));
}
@@ -8101,6 +8112,8 @@ void main() {
#include "WireDraw.inc"
// AFTER WireFramebuffer.inc: the shared-image present is that file's readback, into an image.
#include "WireSharedImage.inc"
// AFTER WireSharedImage.inc: a YUV image is that file's reading side, converted on the way.
#include "WireYuvImage.inc"
void VulkanRenderer::Clear(GLbitfield mask) {
#if MOBILEGL_BUILD_DISAGGREGATED
@@ -8741,6 +8754,7 @@ void main() {
beginInfo.clearValueCount = 1;
beginInfo.pClearValues = &clearValue;
// The load op is the whole operation: begin and end with nothing in between.
EndActiveRenderPassOn(commandBuffer);
vkCmdBeginRenderPass(commandBuffer, &beginInfo, VK_SUBPASS_CONTENTS_INLINE);
vkCmdEndRenderPass(commandBuffer);
@@ -10664,15 +10678,6 @@ void main() {
return;
}
auto& frame = m_frameContext.GetCurrent();
if (!frame.isCommandRecording) {
m_frameContext.BeginCommandRecording();
}
if (VkRenderPassManager::GetActiveRenderPass() != nullptr) {
VkRenderPassManager::EndRenderPass(frame.commandBuffer);
}
// One resolver for both object kinds. The texture arm is the same
// SyncTextureAndGetDescriptor the copy always used; the renderbuffer arm goes through the
// render-pass manager, which is where a renderbuffer's VkImage lives.
@@ -10924,6 +10929,16 @@ void main() {
}
#endif
// Syncing either endpoint can flush and retire the current command buffer for
// a texture upload. Prepare recording only after both images and uploads are ready.
auto& frame = m_frameContext.GetCurrent();
if (!frame.isCommandRecording) {
m_frameContext.BeginCommandRecording();
}
if (VkRenderPassManager::GetActiveRenderPass() != nullptr) {
VkRenderPassManager::EndRenderPass(frame.commandBuffer);
}
const auto materializeClear = [this, &frame](const CopyImageEndpoint& endpoint) {
#if MOBILEGL_BUILD_DISAGGREGATED
if (MG_Config::Transport != MG_Config::TransportMode::Monolith) return true;
@@ -14173,10 +14188,16 @@ void main() {
submitInfo.pCommandBuffers = commandBuffers;
#if MOBILEGL_BUILD_DISAGGREGATED
// Shared-image fences staged for the work recorded since the last submission.
if (LatchIfGpuHung("flush-submit")) return false;
SharedImageSubmitSync sharedImageSync;
AttachSharedImageSync(submitInfo, sharedImageSync);
#endif
const GpuProgressMarkers::Bracket bracket = m_progressMarkers.Acquire();
const VkResult result = GpuProgressMarkers::SubmitBracketed(m_graphicsQueue, submitInfo, fence, bracket);
if (result == VK_SUCCESS) m_progressMarkers.Submitted(bracket);
else m_progressMarkers.Abandon(bracket);
#else
const VkResult result = vkQueueSubmit(m_graphicsQueue, 1, &submitInfo, fence);
#endif
if (result != VK_SUCCESS) {
NoteDeviceLoss(result, "SubmitPendingCommandBuffer vkQueueSubmit");
MGLOG_E_ONCE("SubmitPendingCommandBuffer: vkQueueSubmit returned %d", result);
@@ -14307,6 +14328,31 @@ void main() {
}
return true;
}
Bool VulkanRenderer::LatchIfGpuHung(const char* site) {
if (!m_progressMarkers.Hung()) return false;
if (!m_deviceLost) {
// NOT LOST TO THE DRIVER: a submission that a failed preemption leaves running is reset
// without a device loss, and the next one would stop the GPU again. Treated as lost from
// here on - nothing more is submitted - and the session ends as one whose device was.
m_deviceLost = true;
MGLOG_E("DirectVulkan: the GPU hang watch named this session's VkDevice (a submission ran %llu ms "
"without finishing, at %s); the device is treated as LOST and nothing more is submitted",
static_cast<unsigned long long>(m_progressMarkers.HungRunningMs()),
site != nullptr ? site : "?");
}
// Where no session latch is armed (no served session) the device only stays "lost".
if (!m_deviceLossLatched && MG_Pipe::MGPipeSessionLatchArmed()) {
m_deviceLossLatched = true;
(void)MG_Pipe::MGPipeSessionLatch(MG_Pipe::MGPipeFatalFamily::DeviceLost,
"MGPipe: Fatal{BackendDeviceLost, \"Magma:gpu-hang:%s\"} - this "
"session's GPU work ran past the hang budget and stopped the GPU for "
"every process; the session ends and its client reads a lost context, "
"every other session keeps running",
site != nullptr ? site : "?");
}
return true;
}
#endif
Bool VulkanRenderer::FlushForSyncPoint(Uint64 submitIndex) {
@@ -14656,10 +14702,17 @@ void main() {
}
auto submitPacket = m_frameContext.GetSubmitInfo(shouldSubmitCommandBuffer, m_imageIndexAcquired);
#if MOBILEGL_BUILD_DISAGGREGATED
if (LatchIfGpuHung("present-submit")) return;
SharedImageSubmitSync sharedImageSync;
AttachSharedImageSync(submitPacket.submitInfo, sharedImageSync);
#endif
const GpuProgressMarkers::Bracket bracket = m_progressMarkers.Acquire();
const VkResult presentSubmit = GpuProgressMarkers::SubmitBracketed(m_graphicsQueue, submitPacket.submitInfo,
frame.imageInFlightFence, bracket);
if (presentSubmit == VK_SUCCESS) m_progressMarkers.Submitted(bracket);
else m_progressMarkers.Abandon(bracket);
#else
const VkResult presentSubmit = vkQueueSubmit(m_graphicsQueue, 1, &submitPacket.submitInfo, frame.imageInFlightFence);
#endif
NoteDeviceLoss(presentSubmit, "Present vkQueueSubmit");
#if MOBILEGL_BUILD_DISAGGREGATED
if (presentSubmit != VK_SUCCESS && LatchWireDeviceLoss("present-submit")) return;
@@ -15988,6 +16041,25 @@ void main() {
}
}
#if MOBILEGL_BUILD_DISAGGREGATED
// YUV shared images (WireYuvImage.inc) are sampled through a VkSamplerYcbcrConversion, a
// feature of its own; asked only where AHardwareBuffers are imported at all.
m_samplerYcbcrConversion = false;
VkPhysicalDeviceSamplerYcbcrConversionFeatures ycbcrFeatures{};
ycbcrFeatures.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES;
if (m_wireAhbImport && getPhysicalDeviceFeatures2 != nullptr) {
VkPhysicalDeviceFeatures2 featureQuery{};
featureQuery.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
featureQuery.pNext = &ycbcrFeatures;
getPhysicalDeviceFeatures2(m_physicalDevice.handle, &featureQuery);
if (ycbcrFeatures.samplerYcbcrConversion == VK_TRUE) {
ycbcrFeatures.pNext = const_cast<void*>(deviceCreateInfo.pNext);
deviceCreateInfo.pNext = &ycbcrFeatures;
m_samplerYcbcrConversion = true;
}
}
#endif
// VK_EXT_multi_draw: tier 1 of the multi-draw dispatch - one vkCmdDrawMulti(Indexed)EXT
// for a whole glMultiDraw* batch (VkMultiDrawIndexedInfoEXT carries per-draw
// firstIndex/indexCount/vertexOffset, so glMultiDrawElementsBaseVertex fits natively).
@@ -23,6 +23,7 @@
#include "VkTextureManager.h"
#include "VkTimerQueryManager.h"
#if MOBILEGL_BUILD_DISAGGREGATED
#include "GpuProgressMarkers.h"
#include "WireRenderPassCompatibility.h"
#include <MG_Remote/Server/SharedImageRegistry.h>
#endif
@@ -145,6 +146,10 @@ namespace MobileGL::MG_Backend::DirectVulkan {
// IBufferCopyCommandProvider: recording command buffer, outside any
// render pass, for immediate staged buffer copies.
VkCommandBuffer AcquireBufferCopyCommandBuffer() override;
// EGL_BUFFER_AGE_EXT of the active target's default framebuffer for the next frame: the age
// of the swapchain image acquired for it (SwapchainObject::BufferAgeOf). Asking makes the
// target keep its presented images' content from then on.
Int32 CurrentDrawBufferAge();
#if MOBILEGL_BUILD_DISAGGREGATED
VkBufferManager& GetWireBufferManager() { return m_bufferManager; }
Bool FlushWirePendingCommandsForTextureUpdate() {
@@ -200,10 +205,6 @@ namespace MobileGL::MG_Backend::DirectVulkan {
// submission's sync_file in `*fence` (-1: none, and the work is waited out here), and - for
// a session that writes images - the boundary PublishSharedImageAccesses makes.
Bool ExportNativeFence(int* fence);
// EGL_BUFFER_AGE_EXT of the active target's default framebuffer for the next frame: the age
// of the swapchain image acquired for it (SwapchainObject::BufferAgeOf). Asking makes the
// target keep its presented images' content from then on.
Int32 CurrentDrawBufferAge();
// The reading side's per-use hook (WireSharedImage.inc): every shared-image texture the
// wire draw or dispatch being set up samples (VkTextureManager::NoteSharedImageUse) is
// acquired from the foreign family before it, when it moved or is not held.
@@ -408,6 +409,11 @@ namespace MobileGL::MG_Backend::DirectVulkan {
// without recording or submitting anything more. False: an ordinary failure, the site
// keeps the death it had. `site` names it in the latch line.
Bool LatchWireDeviceLoss(const char* site);
// THE GPU HANG WATCH (GpuProgressMarkers.h): true once the watch named one of this device's
// submissions as the one that stops the GPU. The device is then treated as lost, this session
// is latched, and the caller returns from its verb without submitting anything more. Asked
// before every frame submission and by the apply loop before it applies another record.
Bool LatchIfGpuHung(const char* site);
#endif
// Flush gated on usefulness: only flushes when `submitIndex` is still
// unsubmitted, so poll loops on already-submitted fences do not split
@@ -626,6 +632,8 @@ namespace MobileGL::MG_Backend::DirectVulkan {
Bool m_deviceLost = false;
Bool m_deviceLossLatched = false;
#if MOBILEGL_BUILD_DISAGGREGATED
// Brackets this device's frame submissions for the GPU hang watch (LatchIfGpuHung).
GpuProgressMarkers m_progressMarkers;
struct WireImage {
VkImage image = VK_NULL_HANDLE;
VkFormat format = VK_FORMAT_UNDEFINED;
@@ -709,6 +717,16 @@ namespace MobileGL::MG_Backend::DirectVulkan {
// one entry point the import needs (PFN_vkGetAndroidHardwareBufferPropertiesANDROID).
Bool m_wireAhbImport = false;
void* m_wireGetAhbProperties = nullptr;
// YUV shared images (WireYuvImage.inc): the device enabled samplerYcbcrConversion, and the
// conversion pipelines made so far (one per buffer format and colour conversion).
Bool m_samplerYcbcrConversion = false;
struct WireYuvResources;
WireYuvResources* m_wireYuvResources = nullptr;
void DestroyWireYuvResources();
// A YUV texture's first use in a frame converts its image (yuvConvertedFrame against this,
// which every frame boundary - present, publishing flush, native fence - moves).
Uint64 m_yuvFrameSerial = 1;
Bool ConvertYuvSharedImage(VkTextureManager::TextureResource& resource);
// Shared images this session presents into, imported once each and keyed by the image's
// id (monotonic, never reused). `owner` watches the registry's image: an entry whose image
// died is dropped at a later present once `lastSubmit`, its last copy, has completed.
@@ -378,6 +378,7 @@
begin.renderPass = target->renderPass;
begin.framebuffer = framebuffer;
begin.renderArea = {{0, 0}, destination.extent};
EndActiveRenderPassOn(frame.commandBuffer);
vkCmdBeginRenderPass(frame.commandBuffer, &begin, VK_SUBPASS_CONTENTS_INLINE);
vkCmdBindPipeline(frame.commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, target->pipeline);
const VkViewport viewport{0.0f, 0.0f, static_cast<float>(destination.extent.width),
@@ -299,6 +299,7 @@
begin.renderPass = target->renderPass;
begin.framebuffer = framebuffer;
begin.renderArea = {{0, 0}, destination.extent};
EndActiveRenderPassOn(frame.commandBuffer);
vkCmdBeginRenderPass(frame.commandBuffer, &begin, VK_SUBPASS_CONTENTS_INLINE);
vkCmdBindPipeline(frame.commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, target->pipeline);
const VkViewport viewport{0.0f, 0.0f, static_cast<float>(destination.extent.width),
@@ -11,6 +11,7 @@
// PrimitivesGeneratedNoXfbProbe.cpp): one throwaway command buffer, a bounded fence wait that
// leaks rather than idle-waits a hung GPU, teardown on every other path.
#include "RenderPassGuard.h"
#include "WireDepthResolveProbe.h"
#include <MG_Util/Debug/Log.h>
@@ -237,6 +238,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
// The RECTANGLE is the render area, exactly as the render-pass arm's renderArea is - the
// scratch outside it is never read.
begin.renderArea = rect;
EndActiveRenderPassOn(commandBuffer);
vkCmdBeginRenderPass(commandBuffer, &begin, VK_SUBPASS_CONTENTS_INLINE);
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
// THE VIEWPORT IS THE WHOLE ATTACHMENT AND THE SCISSOR IS THE RECT, and the two are not
@@ -746,6 +748,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
begin.renderPass = clear.pass;
begin.framebuffer = clear.framebuffer;
begin.renderArea = fullRect;
EndActiveRenderPassOn(cmd);
vkCmdBeginRenderPass(cmd, &begin, VK_SUBPASS_CONTENTS_INLINE);
VkClearAttachment attachment{};
attachment.aspectMask = clear.aspects;
@@ -772,6 +775,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
begin.renderPass = item.resolvePass;
begin.framebuffer = item.resolveFramebuffer;
begin.renderArea = fullRect;
EndActiveRenderPassOn(cmd);
vkCmdBeginRenderPass(cmd, &begin, VK_SUBPASS_CONTENTS_INLINE);
vkCmdEndRenderPass(cmd);
}
@@ -215,7 +215,10 @@
MakeXfbWritesVisible();
const auto* fbo = state.DrawFramebuffer();
if (!fbo) MagmaWireFatal("draw-framebuffer-record");
RetireWireDrawPass();
// THE PREVIOUS DRAW'S RENDER PASS STAYS OPEN until this draw's attachments are known: a
// draw to the same attachments continues it (below), and on a tiler a pass per draw also
// loaded and stored every attachment once per draw. Resource work in between that may not
// sit inside a pass (a barrier, a copy, a clear) closes it itself (RenderPassGuard.h).
// CPU preparation may synchronize GPU-produced vertex/index bytes. Do
// not rewind an arena while this draw owns as-yet-unrecorded slices.
WirePreparationScope preparing(m_wirePreparationDepth);
@@ -490,6 +493,7 @@
// attachments below resolve the same index, so this key has to be taken after it.
passKey.swapchainImageIndex = DefaultFramebufferWriteIndex(true);
}
Vector<WireImage> sameLayoutAttachments;
const auto add = [&](const MG_Pipe::MGPSurface& surface, VkImageAspectFlags aspect) -> Uint32 {
WireImage image = ResolveWireImage(*fbo, surface, aspect, /*isWriteTarget=*/true);
if (!image.image) return VK_ATTACHMENT_UNUSED;
@@ -499,7 +503,10 @@
MGB_CTX->IsCapabilityEnabled(CapabilityInput::FramebufferSrgb));
const VkImageLayout layout = aspect == VK_IMAGE_ASPECT_COLOR_BIT ?
VK_IMAGE_LAYOUT_GENERAL : VK_IMAGE_LAYOUT_GENERAL;
TransitionWireImage(image, layout);
// Already in its attachment layout: its barrier (which closes any open pass) waits until
// it is known whether the open pass continues with this very attachment.
if (image.layout == layout) sameLayoutAttachments.push_back(image);
else TransitionWireImage(image, layout);
VkImageViewCreateInfo info{VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO};
info.image = image.image;
info.viewType = image.viewType;
@@ -565,7 +572,17 @@
passInfo.pAttachments = attachments.data();
passInfo.subpassCount = 1;
passInfo.pSubpasses = &subpass;
if (!AcquireCachedWireDrawPass(passKey)) {
const Bool continuing = m_wireDrawPass && m_wireDrawPassKey == passKey;
{
const auto* active = VkRenderPassManager::GetActiveRenderPass();
const Bool stillOpen = continuing && active != nullptr && active->commandBuffer == frame.commandBuffer;
// A new pass orders itself after earlier writes to its attachments; a continued one
// is the same pass, whose attachment writes are ordered by rasterization.
if (!stillOpen)
for (auto& image : sameLayoutAttachments) TransitionWireImage(image, VK_IMAGE_LAYOUT_GENERAL);
}
if (!continuing) RetireWireDrawPass();
if (!continuing && !AcquireCachedWireDrawPass(passKey)) {
m_wireDrawPassKey = Move(passKey);
Vector<VkImageView> views;
views.reserve(viewInfos.size());
@@ -629,13 +646,20 @@
vkCmdBindVertexBuffers(frame.commandBuffer, binding++, 1, &slice.buffer, &slice.offset);
}
if (indexSlice.IsValid()) vkCmdBindIndexBuffer(frame.commandBuffer, indexSlice.buffer, indexSlice.offset, indexType);
if (!VkRenderPassManager::BeginRenderPass(frame.commandBuffer, pass)) {
// Still open from the previous draw (nothing closed it, same command buffer): continue.
const auto* active = VkRenderPassManager::GetActiveRenderPass();
const Bool open = continuing && active != nullptr && active->commandBuffer == frame.commandBuffer &&
active->hash == pass.hash;
if (!open && active != nullptr) VkRenderPassManager::EndRenderPass(frame.commandBuffer);
if (!open && !VkRenderPassManager::BeginRenderPass(frame.commandBuffer, pass)) {
MGL_WIRE_DECLINE_AT(BeginRenderPass, "the render pass for this draw could not be begun");
return false;
}
vkCmdBindPipeline(frame.commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
ApplyDynamicDrawStateTail(frame, pass.extent, fbo->IsDefault,
ResolveDrawViewportCount(programObj.writesViewportIndexBuiltin));
// Storage images this draw writes are what the next sampled-image barrier must cover.
if (programObj.hasStorageImages) BumpWireImageWriteEpoch();
if (MG_Util::PipeStats::Enabled()) MG_Util::PipeStats::AddCalls(MG_Util::PipeStats::CallClass::Draws, 1);
return true;
}
@@ -800,6 +824,7 @@
return;
}
vkCmdBindPipeline(frame.commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline);
EndActiveRenderPassOn(frame.commandBuffer);
vkCmdDispatch(frame.commandBuffer, x, y, z);
}
@@ -878,6 +903,7 @@
return;
}
vkCmdBindPipeline(dispatchFrame.commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline);
EndActiveRenderPassOn(dispatchFrame.commandBuffer);
vkCmdDispatchIndirect(dispatchFrame.commandBuffer, parameters.buffer,
parameters.offset + static_cast<VkDeviceSize>(offset));
if (MG_Util::PipeStats::Enabled())
@@ -233,6 +233,13 @@
void VulkanRenderer::ClearWireFramebuffer(const MG_Pipe::MGPFramebufferState& fbo,
const ClearAttachmentPayload& payload, GLint drawbuffer) {
RetireWireDrawPass();
// A stream that neither presents nor finishes (an off-screen loop) never submits, so its
// recording and the per-clear objects retired below would grow until host memory runs
// out. Past a bound, submit what is recorded: the objects then retire with that work.
constexpr SizeT kRetiredObjectsBeforeSubmit = 1024;
if (m_wireRetiredObjects.size() >= kRetiredObjectsBeforeSubmit && m_wirePreparationDepth == 0 &&
HasPendingRecordedWork())
(void)FlushPendingCommands();
if (MGB_CTX->IsCapabilityEnabled(CapabilityInput::RasterizerDiscard)) return;
const auto clearSurface = [&](const MG_Pipe::MGPSurface& surface, VkImageAspectFlags aspect,
Uint32 colorIndex) {
@@ -319,6 +326,7 @@
if (!color) clear.clearValue.depthStencil = {payload.depth, payload.stencil};
VkClearRect rect{area, 0, image.layers};
auto& frame = m_frameContext.GetCurrent();
EndActiveRenderPassOn(frame.commandBuffer);
vkCmdBeginRenderPass(frame.commandBuffer, &begin, VK_SUBPASS_CONTENTS_INLINE);
vkCmdClearAttachments(frame.commandBuffer, 1, &clear, 1, &rect);
vkCmdEndRenderPass(frame.commandBuffer);
@@ -830,6 +838,7 @@
begin.renderPass = pass;
begin.framebuffer = framebuffer;
begin.renderArea = {{sxMin, syMin}, {static_cast<Uint32>(width), static_cast<Uint32>(height)}};
EndActiveRenderPassOn(m_frameContext.GetCurrent().commandBuffer);
vkCmdBeginRenderPass(m_frameContext.GetCurrent().commandBuffer, &begin, VK_SUBPASS_CONTENTS_INLINE);
vkCmdEndRenderPass(m_frameContext.GetCurrent().commandBuffer);
} else if (!resolvedByShader) {
@@ -215,6 +215,7 @@
VkRenderPassBeginInfo begin{VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO};
begin.renderPass = image.clearPass; begin.framebuffer = image.clearFramebuffer;
begin.renderArea.extent = {1, 1}; begin.clearValueCount = 1; begin.pClearValues = &clear;
EndActiveRenderPassOn(commands);
vkCmdBeginRenderPass(commands, &begin, VK_SUBPASS_CONTENTS_INLINE);
vkCmdEndRenderPass(commands);
image.layout = VK_IMAGE_LAYOUT_GENERAL;
@@ -226,6 +227,7 @@
const VkImageSubresourceRange range{image.aspect, 0, 1, 0, image.layers};
if (shadow) {
const VkClearDepthStencilValue depth{0.0f, 0};
EndActiveRenderPassOn(commands);
vkCmdClearDepthStencilImage(commands, image.image, image.layout, &depth, 1, &range);
} else vkCmdClearColorImage(commands, image.image, image.layout, &clear.color, 1, &range);
if (!VkTextureManager::TransitionImageLayout(commands, image.image, image.layout,
@@ -236,6 +238,7 @@
VkMemoryBarrier visible{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
visible.srcAccessMask = multisample ? VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT : VK_ACCESS_TRANSFER_WRITE_BIT;
visible.dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT;
EndActiveRenderPassOn(commands);
vkCmdPipelineBarrier(commands, multisample ? VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT : VK_PIPELINE_STAGE_TRANSFER_BIT,
VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, 1, &visible, 0, nullptr, 0, nullptr);
}
@@ -499,6 +499,11 @@
void VulkanRenderer::AcquireSharedImage(VkTextureManager::TextureResource& resource) {
namespace SI = MG_Remote::Server::SharedImages;
// A YUV image is converted into the texture's own image instead (WireYuvImage.inc).
if (resource.yuvImageId != 0) {
(void)ConvertYuvSharedImage(resource);
return;
}
if (resource.sharedImageId == 0 || resource.image == VK_NULL_HANDLE || resource.sharedImageOwner == nullptr)
return;
const auto image = std::static_pointer_cast<const SI::Image>(resource.sharedImageOwner);
@@ -567,6 +572,8 @@
// family in the frame's last recording, and that recording's submission signals the semaphore
// whose sync_file is published as the frame's read fence for all of them at once.
void VulkanRenderer::ReleaseHeldSharedImages() {
// A frame boundary: a YUV texture's next use converts its image again.
++m_yuvFrameSerial;
if (m_sharedImagesHeld.empty()) return;
const Uint32 family = static_cast<Uint32>(m_physicalDevice.queueFamilies.graphicsFamily);
Vector<VkImageMemoryBarrier> releases;
@@ -633,6 +640,9 @@
// submission, and it is tracked like any other (the semaphore's reuse waits for it).
Bool VulkanRenderer::ExportNativeFence(int* fence) {
*fence = -1;
// Every fence the application asks for ends one of its frames as far as a YUV texture is
// concerned (an offscreen producer - a browser's GPU process - has no present to do it).
++m_yuvFrameSerial;
if (m_device == VK_NULL_HANDLE || m_graphicsQueue == VK_NULL_HANDLE || m_frameContext.GetFrameCount() == 0)
return true; // nothing was ever submitted: nothing to fence
// A session that writes images ends its frame here, as at a flush; a compositor that only
@@ -0,0 +1,10 @@
#version 450
// The image is bound through an immutable sampler that carries its VkSamplerYcbcrConversion, so
// a plain sample already returns RGB.
layout(set = 0, binding = 0) uniform sampler2D yuvImage;
layout(location = 0) in vec2 texCoord;
layout(location = 0) out vec4 outColor;
void main() {
outColor = vec4(textureLod(yuvImage, texCoord, 0.0).rgb, 1.0);
}
@@ -0,0 +1,11 @@
#version 450
// A YUV shared image converted into an RGBA texture (WireYuvImage.inc): one triangle over the
// whole target, its first row at the image's first row.
layout(location = 0) out vec2 texCoord;
void main() {
const vec2 corners[3] = vec2[](vec2(-1.0, -1.0), vec2(3.0, -1.0), vec2(-1.0, 3.0));
vec2 p = corners[gl_VertexIndex];
gl_Position = vec4(p, 0.0, 1.0);
texCoord = (p + 1.0) * 0.5;
}
@@ -0,0 +1,78 @@
// Generated from WireYuvConvert.vert / WireYuvConvert.frag (Vulkan 1.0 / SPIR-V 1.0) by
// scripts/bake_internal_shaders.py, which runs MG_Test's BakedInternalShadersTest with the
// IN-TREE glslang and splices the words it emits into the arrays below. Do not edit the words
// by hand: MOBILEGL_BAKED_INTERNAL_SHADERS (the same test, run as a gate) recompiles every
// source in its table and reds on the first word that disagrees.
#pragma once
static constexpr Uint32 kWireYuvConvertVertexSpirv[] = {
0x07230203u, 0x00010000u, 0x0008000bu, 0x00000031u, 0x00000000u, 0x00020011u, 0x00000001u, 0x0006000bu,
0x00000001u, 0x4c534c47u, 0x6474732eu, 0x3035342eu, 0x00000000u, 0x0003000eu, 0x00000000u, 0x00000001u,
0x0008000fu, 0x00000000u, 0x00000004u, 0x6e69616du, 0x00000000u, 0x00000015u, 0x00000020u, 0x0000002bu,
0x00030003u, 0x00000002u, 0x000001c2u, 0x00040005u, 0x00000004u, 0x6e69616du, 0x00000000u, 0x00030005u,
0x00000009u, 0x00000070u, 0x00060005u, 0x00000015u, 0x565f6c67u, 0x65747265u, 0x646e4978u, 0x00007865u,
0x00050005u, 0x00000018u, 0x65646e69u, 0x6c626178u, 0x00000065u, 0x00060005u, 0x0000001eu, 0x505f6c67u,
0x65567265u, 0x78657472u, 0x00000000u, 0x00060006u, 0x0000001eu, 0x00000000u, 0x505f6c67u, 0x7469736fu,
0x006e6f69u, 0x00070006u, 0x0000001eu, 0x00000001u, 0x505f6c67u, 0x746e696fu, 0x657a6953u, 0x00000000u,
0x00070006u, 0x0000001eu, 0x00000002u, 0x435f6c67u, 0x4470696cu, 0x61747369u, 0x0065636eu, 0x00070006u,
0x0000001eu, 0x00000003u, 0x435f6c67u, 0x446c6c75u, 0x61747369u, 0x0065636eu, 0x00030005u, 0x00000020u,
0x00000000u, 0x00050005u, 0x0000002bu, 0x43786574u, 0x64726f6fu, 0x00000000u, 0x00040047u, 0x00000015u,
0x0000000bu, 0x0000002au, 0x00030047u, 0x0000001eu, 0x00000002u, 0x00050048u, 0x0000001eu, 0x00000000u,
0x0000000bu, 0x00000000u, 0x00050048u, 0x0000001eu, 0x00000001u, 0x0000000bu, 0x00000001u, 0x00050048u,
0x0000001eu, 0x00000002u, 0x0000000bu, 0x00000003u, 0x00050048u, 0x0000001eu, 0x00000003u, 0x0000000bu,
0x00000004u, 0x00040047u, 0x0000002bu, 0x0000001eu, 0x00000000u, 0x00020013u, 0x00000002u, 0x00030021u,
0x00000003u, 0x00000002u, 0x00030016u, 0x00000006u, 0x00000020u, 0x00040017u, 0x00000007u, 0x00000006u,
0x00000002u, 0x00040020u, 0x00000008u, 0x00000007u, 0x00000007u, 0x00040015u, 0x0000000au, 0x00000020u,
0x00000000u, 0x0004002bu, 0x0000000au, 0x0000000bu, 0x00000003u, 0x0004001cu, 0x0000000cu, 0x00000007u,
0x0000000bu, 0x0004002bu, 0x00000006u, 0x0000000du, 0xbf800000u, 0x0005002cu, 0x00000007u, 0x0000000eu,
0x0000000du, 0x0000000du, 0x0004002bu, 0x00000006u, 0x0000000fu, 0x40400000u, 0x0005002cu, 0x00000007u,
0x00000010u, 0x0000000fu, 0x0000000du, 0x0005002cu, 0x00000007u, 0x00000011u, 0x0000000du, 0x0000000fu,
0x0006002cu, 0x0000000cu, 0x00000012u, 0x0000000eu, 0x00000010u, 0x00000011u, 0x00040015u, 0x00000013u,
0x00000020u, 0x00000001u, 0x00040020u, 0x00000014u, 0x00000001u, 0x00000013u, 0x0004003bu, 0x00000014u,
0x00000015u, 0x00000001u, 0x00040020u, 0x00000017u, 0x00000007u, 0x0000000cu, 0x00040017u, 0x0000001bu,
0x00000006u, 0x00000004u, 0x0004002bu, 0x0000000au, 0x0000001cu, 0x00000001u, 0x0004001cu, 0x0000001du,
0x00000006u, 0x0000001cu, 0x0006001eu, 0x0000001eu, 0x0000001bu, 0x00000006u, 0x0000001du, 0x0000001du,
0x00040020u, 0x0000001fu, 0x00000003u, 0x0000001eu, 0x0004003bu, 0x0000001fu, 0x00000020u, 0x00000003u,
0x0004002bu, 0x00000013u, 0x00000021u, 0x00000000u, 0x0004002bu, 0x00000006u, 0x00000023u, 0x00000000u,
0x0004002bu, 0x00000006u, 0x00000024u, 0x3f800000u, 0x00040020u, 0x00000028u, 0x00000003u, 0x0000001bu,
0x00040020u, 0x0000002au, 0x00000003u, 0x00000007u, 0x0004003bu, 0x0000002au, 0x0000002bu, 0x00000003u,
0x0004002bu, 0x00000006u, 0x0000002fu, 0x3f000000u, 0x00050036u, 0x00000002u, 0x00000004u, 0x00000000u,
0x00000003u, 0x000200f8u, 0x00000005u, 0x0004003bu, 0x00000008u, 0x00000009u, 0x00000007u, 0x0004003bu,
0x00000017u, 0x00000018u, 0x00000007u, 0x0004003du, 0x00000013u, 0x00000016u, 0x00000015u, 0x0003003eu,
0x00000018u, 0x00000012u, 0x00050041u, 0x00000008u, 0x00000019u, 0x00000018u, 0x00000016u, 0x0004003du,
0x00000007u, 0x0000001au, 0x00000019u, 0x0003003eu, 0x00000009u, 0x0000001au, 0x0004003du, 0x00000007u,
0x00000022u, 0x00000009u, 0x00050051u, 0x00000006u, 0x00000025u, 0x00000022u, 0x00000000u, 0x00050051u,
0x00000006u, 0x00000026u, 0x00000022u, 0x00000001u, 0x00070050u, 0x0000001bu, 0x00000027u, 0x00000025u,
0x00000026u, 0x00000023u, 0x00000024u, 0x00050041u, 0x00000028u, 0x00000029u, 0x00000020u, 0x00000021u,
0x0003003eu, 0x00000029u, 0x00000027u, 0x0004003du, 0x00000007u, 0x0000002cu, 0x00000009u, 0x00050050u,
0x00000007u, 0x0000002du, 0x00000024u, 0x00000024u, 0x00050081u, 0x00000007u, 0x0000002eu, 0x0000002cu,
0x0000002du, 0x0005008eu, 0x00000007u, 0x00000030u, 0x0000002eu, 0x0000002fu, 0x0003003eu, 0x0000002bu,
0x00000030u, 0x000100fdu, 0x00010038u,
};
static constexpr Uint32 kWireYuvConvertFragmentSpirv[] = {
0x07230203u, 0x00010000u, 0x0008000bu, 0x0000001cu, 0x00000000u, 0x00020011u, 0x00000001u, 0x0006000bu,
0x00000001u, 0x4c534c47u, 0x6474732eu, 0x3035342eu, 0x00000000u, 0x0003000eu, 0x00000000u, 0x00000001u,
0x0007000fu, 0x00000004u, 0x00000004u, 0x6e69616du, 0x00000000u, 0x00000009u, 0x00000011u, 0x00030010u,
0x00000004u, 0x00000007u, 0x00030003u, 0x00000002u, 0x000001c2u, 0x00040005u, 0x00000004u, 0x6e69616du,
0x00000000u, 0x00050005u, 0x00000009u, 0x4374756fu, 0x726f6c6fu, 0x00000000u, 0x00050005u, 0x0000000du,
0x49767579u, 0x6567616du, 0x00000000u, 0x00050005u, 0x00000011u, 0x43786574u, 0x64726f6fu, 0x00000000u,
0x00040047u, 0x00000009u, 0x0000001eu, 0x00000000u, 0x00040047u, 0x0000000du, 0x00000021u, 0x00000000u,
0x00040047u, 0x0000000du, 0x00000022u, 0x00000000u, 0x00040047u, 0x00000011u, 0x0000001eu, 0x00000000u,
0x00020013u, 0x00000002u, 0x00030021u, 0x00000003u, 0x00000002u, 0x00030016u, 0x00000006u, 0x00000020u,
0x00040017u, 0x00000007u, 0x00000006u, 0x00000004u, 0x00040020u, 0x00000008u, 0x00000003u, 0x00000007u,
0x0004003bu, 0x00000008u, 0x00000009u, 0x00000003u, 0x00090019u, 0x0000000au, 0x00000006u, 0x00000001u,
0x00000000u, 0x00000000u, 0x00000000u, 0x00000001u, 0x00000000u, 0x0003001bu, 0x0000000bu, 0x0000000au,
0x00040020u, 0x0000000cu, 0x00000000u, 0x0000000bu, 0x0004003bu, 0x0000000cu, 0x0000000du, 0x00000000u,
0x00040017u, 0x0000000fu, 0x00000006u, 0x00000002u, 0x00040020u, 0x00000010u, 0x00000001u, 0x0000000fu,
0x0004003bu, 0x00000010u, 0x00000011u, 0x00000001u, 0x0004002bu, 0x00000006u, 0x00000013u, 0x00000000u,
0x00040017u, 0x00000015u, 0x00000006u, 0x00000003u, 0x0004002bu, 0x00000006u, 0x00000017u, 0x3f800000u,
0x00050036u, 0x00000002u, 0x00000004u, 0x00000000u, 0x00000003u, 0x000200f8u, 0x00000005u, 0x0004003du,
0x0000000bu, 0x0000000eu, 0x0000000du, 0x0004003du, 0x0000000fu, 0x00000012u, 0x00000011u, 0x00070058u,
0x00000007u, 0x00000014u, 0x0000000eu, 0x00000012u, 0x00000002u, 0x00000013u, 0x0008004fu, 0x00000015u,
0x00000016u, 0x00000014u, 0x00000014u, 0x00000000u, 0x00000001u, 0x00000002u, 0x00050051u, 0x00000006u,
0x00000018u, 0x00000016u, 0x00000000u, 0x00050051u, 0x00000006u, 0x00000019u, 0x00000016u, 0x00000001u,
0x00050051u, 0x00000006u, 0x0000001au, 0x00000016u, 0x00000002u, 0x00070050u, 0x00000007u, 0x0000001bu,
0x00000018u, 0x00000019u, 0x0000001au, 0x00000017u, 0x0003003eu, 0x00000009u, 0x0000001bu, 0x000100fdu,
0x00010038u,
};
@@ -0,0 +1,515 @@
// Included by VulkanRenderer.cpp inside the backend namespace, after WireSharedImage.inc.
//
// YUV SHARED IMAGES (NV12, P010; MG_Remote/Server/SharedImageYuv.h). A YUV image is never a
// texture's image: the texture keeps its own RGBA8 level 0 (VkTextureManager::SyncWireTextureShape)
// and, at its first use in each frame, the image is CONVERTED into it here - the image's
// AHardwareBuffer imported with its external format, sampled through an immutable sampler that
// carries a VkSamplerYcbcrConversion built from the import's colour hints, one triangle over the
// target. Zero CPU copies for an image this server allocated; a foreign one (a video decoder's
// buffer) is first brought up to date by the registry's labelled CPU copy (RefreshForeign).
//
// Why converted rather than sampled in place: a Y'CbCr conversion is an immutable sampler, which
// lives in the descriptor set layout and therefore in every application pipeline that samples the
// texture. A conversion pass keeps those pipelines exactly what they are for an RGBA texture, at
// the cost of one small pass per new frame of the video.
#if MOBILEGL_BUILD_DISAGGREGATED
#include "WireYuvConvertSpirv.h"
struct VulkanRenderer::WireYuvResources {
PFN_vkCreateSamplerYcbcrConversion createConversion = nullptr;
PFN_vkDestroySamplerYcbcrConversion destroyConversion = nullptr;
VkShaderModule vertex = VK_NULL_HANDLE, fragment = VK_NULL_HANDLE;
// One per (buffer format, conversion, target format). Never dropped before shutdown: a
// device sees a handful of them.
struct Pipeline {
Uint64 externalFormat = 0;
VkFormat format = VK_FORMAT_UNDEFINED;
VkSamplerYcbcrModelConversion model = VK_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY;
VkSamplerYcbcrRange range = VK_SAMPLER_YCBCR_RANGE_ITU_FULL;
VkComponentMapping components{};
VkChromaLocation xOffset = VK_CHROMA_LOCATION_COSITED_EVEN;
VkChromaLocation yOffset = VK_CHROMA_LOCATION_COSITED_EVEN;
VkFilter chromaFilter = VK_FILTER_NEAREST;
VkFormat target = VK_FORMAT_UNDEFINED;
VkSamplerYcbcrConversion conversion = VK_NULL_HANDLE;
VkSampler sampler = VK_NULL_HANDLE;
VkDescriptorSetLayout descriptorLayout = VK_NULL_HANDLE;
VkPipelineLayout pipelineLayout = VK_NULL_HANDLE;
VkRenderPass renderPass = VK_NULL_HANDLE;
VkPipeline pipeline = VK_NULL_HANDLE;
};
Vector<Pipeline> pipelines;
};
namespace {
// One texture's import of its YUV buffer (TextureResource::yuvSource): the image on the
// buffer's memory and a view through the conversion of `pipeline`. Destroyed by the
// resource's (deferred) Reset, so never under a submission still reading it.
struct WireYuvSource {
VkDevice device = VK_NULL_HANDLE;
VkImage image = VK_NULL_HANDLE;
VkDeviceMemory memory = VK_NULL_HANDLE;
VkImageView view = VK_NULL_HANDLE;
SizeT pipeline = 0;
MG_Remote::Server::SharedImages::Yuv::Hints hints;
~WireYuvSource() {
if (device == VK_NULL_HANDLE) return;
if (view != VK_NULL_HANDLE) vkDestroyImageView(device, view, nullptr);
if (image != VK_NULL_HANDLE) vkDestroyImage(device, image, nullptr);
if (memory != VK_NULL_HANDLE) vkFreeMemory(device, memory, nullptr);
}
};
VkSamplerYcbcrModelConversion YcbcrModelFor(const MG_Remote::Server::SharedImages::Yuv::Hints& hints) {
namespace Yuv = MG_Remote::Server::SharedImages::Yuv;
switch (hints.EffectiveColorSpace()) {
case Yuv::kEglItuRec709:
return VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_709;
case Yuv::kEglItuRec2020:
return VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_2020;
default:
return VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_601;
}
}
} // namespace
void VulkanRenderer::DestroyWireYuvResources() {
if (!m_wireYuvResources) return;
auto& cache = *m_wireYuvResources;
for (const auto& entry : cache.pipelines) {
vkDestroyPipeline(m_device, entry.pipeline, nullptr);
vkDestroyRenderPass(m_device, entry.renderPass, nullptr);
vkDestroyPipelineLayout(m_device, entry.pipelineLayout, nullptr);
vkDestroyDescriptorSetLayout(m_device, entry.descriptorLayout, nullptr);
vkDestroySampler(m_device, entry.sampler, nullptr);
if (cache.destroyConversion != nullptr) cache.destroyConversion(m_device, entry.conversion, nullptr);
}
vkDestroyShaderModule(m_device, cache.vertex, nullptr);
vkDestroyShaderModule(m_device, cache.fragment, nullptr);
delete m_wireYuvResources;
m_wireYuvResources = nullptr;
}
Bool VulkanRenderer::ConvertYuvSharedImage(VkTextureManager::TextureResource& resource) {
#if defined(__ANDROID__)
namespace SI = MG_Remote::Server::SharedImages;
if (resource.yuvConvertedFrame == m_yuvFrameSerial) return true;
const auto image = std::static_pointer_cast<const SI::Image>(resource.yuvOwner);
if (image == nullptr || image->Native == nullptr || resource.image == VK_NULL_HANDLE) return false;
if (!m_samplerYcbcrConversion) {
MGLOG_E_ONCE("Magma: YUV shared image %llu cannot be sampled: the device has no samplerYcbcrConversion",
static_cast<unsigned long long>(image->Id));
return false;
}
// Whatever happens below, this frame does not try again.
resource.yuvConvertedFrame = m_yuvFrameSerial;
// THE CPU-COPY FALLBACK, for a buffer this server did not allocate (SharedImageRegistry.h).
String why;
if (image->IsForeign() && !SI::RefreshForeign(*image, why)) {
MGLOG_E_ONCE("Magma: foreign YUV image %llu could not be refreshed: %s",
static_cast<unsigned long long>(image->Id), why.c_str());
}
const SI::Yuv::Hints hints = SI::GetYuvHints(*image);
if (!m_wireYuvResources) {
m_wireYuvResources = new WireYuvResources;
auto& cache = *m_wireYuvResources;
cache.createConversion = reinterpret_cast<PFN_vkCreateSamplerYcbcrConversion>(
vkGetDeviceProcAddr(m_device, "vkCreateSamplerYcbcrConversion"));
if (cache.createConversion == nullptr)
cache.createConversion = reinterpret_cast<PFN_vkCreateSamplerYcbcrConversion>(
vkGetDeviceProcAddr(m_device, "vkCreateSamplerYcbcrConversionKHR"));
cache.destroyConversion = reinterpret_cast<PFN_vkDestroySamplerYcbcrConversion>(
vkGetDeviceProcAddr(m_device, "vkDestroySamplerYcbcrConversion"));
if (cache.destroyConversion == nullptr)
cache.destroyConversion = reinterpret_cast<PFN_vkDestroySamplerYcbcrConversion>(
vkGetDeviceProcAddr(m_device, "vkDestroySamplerYcbcrConversionKHR"));
const auto shader = [&](const Uint32* code, SizeT bytes, VkShaderModule& module) {
VkShaderModuleCreateInfo info{VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO};
info.codeSize = bytes;
info.pCode = code;
VK_VERIFY(vkCreateShaderModule(m_device, &info, nullptr, &module));
};
shader(kWireYuvConvertVertexSpirv, sizeof(kWireYuvConvertVertexSpirv), cache.vertex);
shader(kWireYuvConvertFragmentSpirv, sizeof(kWireYuvConvertFragmentSpirv), cache.fragment);
}
auto& cache = *m_wireYuvResources;
if (cache.createConversion == nullptr || cache.destroyConversion == nullptr) {
MGLOG_E_ONCE("Magma: vkCreateSamplerYcbcrConversion is not available; YUV images cannot be sampled");
return false;
}
// ---- the texture's import of the buffer, and the pipeline its conversion needs ----
auto* source = static_cast<WireYuvSource*>(resource.yuvSource.get());
if (source != nullptr && !(source->hints == hints)) {
// Re-imported with other colour hints: the conversion (and so the view) changes.
auto retired = MakeUnique<VkTextureManager::TextureResource>();
retired->yuvSource = std::move(resource.yuvSource);
WireRetiredObjects objects{};
objects.textures.push_back(Move(retired));
RetireWireObjects(Move(objects));
source = nullptr;
}
if (source == nullptr) {
WireAhbImport ctx;
if (!GetWireAhbImport(ctx)) return false;
auto* ahb = static_cast<AHardwareBuffer*>(image->Native);
const auto getProperties =
reinterpret_cast<PFN_vkGetAndroidHardwareBufferPropertiesANDROID>(ctx.getAhbProperties);
VkAndroidHardwareBufferFormatPropertiesANDROID format{
VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_FORMAT_PROPERTIES_ANDROID};
VkAndroidHardwareBufferPropertiesANDROID properties{
VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_PROPERTIES_ANDROID};
properties.pNext = &format;
if (getProperties(m_device, ahb, &properties) != VK_SUCCESS) {
MGLOG_E_ONCE("Magma: YUV image %llu: vkGetAndroidHardwareBufferPropertiesANDROID failed",
static_cast<unsigned long long>(image->Id));
return false;
}
// The conversion: the import's hints where it gave them, else what the buffer suggests.
WireYuvResources::Pipeline key;
key.externalFormat = format.format == VK_FORMAT_UNDEFINED ? format.externalFormat : 0;
key.format = format.format;
key.model = hints.ColorSpace != 0 ? YcbcrModelFor(hints) : format.suggestedYcbcrModel;
key.range = hints.Range != 0 ? (hints.FullRange() ? VK_SAMPLER_YCBCR_RANGE_ITU_FULL
: VK_SAMPLER_YCBCR_RANGE_ITU_NARROW)
: format.suggestedYcbcrRange;
key.components = format.samplerYcbcrConversionComponents;
const auto chroma = [&format](Uint32 siting, VkChromaLocation suggested) {
const Bool midpoint = siting == SI::Yuv::kEglYuvChromaSiting0_5;
const Bool cosited = siting == SI::Yuv::kEglYuvChromaSiting0;
if (midpoint && (format.formatFeatures & VK_FORMAT_FEATURE_MIDPOINT_CHROMA_SAMPLES_BIT))
return VK_CHROMA_LOCATION_MIDPOINT;
if (cosited && (format.formatFeatures & VK_FORMAT_FEATURE_COSITED_CHROMA_SAMPLES_BIT))
return VK_CHROMA_LOCATION_COSITED_EVEN;
return suggested;
};
key.xOffset = chroma(hints.SitingX, format.suggestedXChromaOffset);
key.yOffset = chroma(hints.SitingY, format.suggestedYChromaOffset);
key.chromaFilter =
(format.formatFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_YCBCR_CONVERSION_LINEAR_FILTER_BIT) != 0
? VK_FILTER_LINEAR
: VK_FILTER_NEAREST;
key.target = resource.format;
SizeT index = cache.pipelines.size();
for (SizeT i = 0; i < cache.pipelines.size(); ++i) {
const auto& p = cache.pipelines[i];
if (p.externalFormat == key.externalFormat && p.format == key.format && p.model == key.model &&
p.range == key.range && p.components.r == key.components.r &&
p.components.g == key.components.g && p.components.b == key.components.b &&
p.components.a == key.components.a && p.xOffset == key.xOffset && p.yOffset == key.yOffset &&
p.chromaFilter == key.chromaFilter && p.target == key.target) {
index = i;
break;
}
}
if (index == cache.pipelines.size()) {
VkExternalFormatANDROID external{VK_STRUCTURE_TYPE_EXTERNAL_FORMAT_ANDROID};
external.externalFormat = key.externalFormat;
VkSamplerYcbcrConversionCreateInfo conversion{VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_CREATE_INFO};
conversion.pNext = key.externalFormat != 0 ? &external : nullptr;
conversion.format = key.format;
conversion.ycbcrModel = key.model;
conversion.ycbcrRange = key.range;
conversion.components = key.components;
conversion.xChromaOffset = key.xOffset;
conversion.yChromaOffset = key.yOffset;
conversion.chromaFilter = key.chromaFilter;
conversion.forceExplicitReconstruction = VK_FALSE;
if (cache.createConversion(m_device, &conversion, nullptr, &key.conversion) != VK_SUCCESS) {
MGLOG_E_ONCE("Magma: YUV image %llu: vkCreateSamplerYcbcrConversion failed",
static_cast<unsigned long long>(image->Id));
return false;
}
VkSamplerYcbcrConversionInfo conversionInfo{VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_INFO};
conversionInfo.conversion = key.conversion;
VkSamplerCreateInfo sampler{VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO};
sampler.pNext = &conversionInfo;
// Without separate reconstruction filters the sampler filters as the conversion does.
sampler.magFilter = sampler.minFilter = key.chromaFilter;
sampler.mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST;
sampler.addressModeU = sampler.addressModeV = sampler.addressModeW =
VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
sampler.maxLod = 0.0f;
VK_VERIFY(vkCreateSampler(m_device, &sampler, nullptr, &key.sampler));
const VkDescriptorSetLayoutBinding binding{0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1,
VK_SHADER_STAGE_FRAGMENT_BIT, &key.sampler};
VkDescriptorSetLayoutCreateInfo layout{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO};
layout.bindingCount = 1;
layout.pBindings = &binding;
VK_VERIFY(vkCreateDescriptorSetLayout(m_device, &layout, nullptr, &key.descriptorLayout));
VkPipelineLayoutCreateInfo pipelineLayout{VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO};
pipelineLayout.setLayoutCount = 1;
pipelineLayout.pSetLayouts = &key.descriptorLayout;
VK_VERIFY(vkCreatePipelineLayout(m_device, &pipelineLayout, nullptr, &key.pipelineLayout));
// Every texel of the target is written: nothing to load.
VkAttachmentDescription attachment{};
attachment.format = key.target;
attachment.samples = VK_SAMPLE_COUNT_1_BIT;
attachment.loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
attachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
attachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
attachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
attachment.initialLayout = attachment.finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
const VkAttachmentReference color{0, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL};
VkSubpassDescription subpass{};
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
subpass.colorAttachmentCount = 1;
subpass.pColorAttachments = &color;
VkRenderPassCreateInfo pass{VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO};
pass.attachmentCount = 1;
pass.pAttachments = &attachment;
pass.subpassCount = 1;
pass.pSubpasses = &subpass;
VK_VERIFY(vkCreateRenderPass(m_device, &pass, nullptr, &key.renderPass));
VkPipelineShaderStageCreateInfo stages[2]{};
for (auto& stage : stages) {
stage.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
stage.pName = "main";
}
stages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
stages[0].module = cache.vertex;
stages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
stages[1].module = cache.fragment;
const VkPipelineVertexInputStateCreateInfo vertex{
VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO};
VkPipelineInputAssemblyStateCreateInfo assembly{
VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO};
assembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
VkPipelineViewportStateCreateInfo viewport{VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO};
viewport.viewportCount = viewport.scissorCount = 1;
VkPipelineRasterizationStateCreateInfo raster{
VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO};
raster.polygonMode = VK_POLYGON_MODE_FILL;
raster.cullMode = VK_CULL_MODE_NONE;
raster.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
raster.lineWidth = 1.0f;
VkPipelineMultisampleStateCreateInfo samples{
VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO};
samples.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
VkPipelineColorBlendAttachmentState colorMask{};
colorMask.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT |
VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
VkPipelineColorBlendStateCreateInfo blend{VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO};
blend.attachmentCount = 1;
blend.pAttachments = &colorMask;
const VkDynamicState dynamicStates[]{VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR};
VkPipelineDynamicStateCreateInfo dynamic{VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO};
dynamic.dynamicStateCount = 2;
dynamic.pDynamicStates = dynamicStates;
VkGraphicsPipelineCreateInfo pipeline{VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO};
pipeline.stageCount = 2;
pipeline.pStages = stages;
pipeline.pVertexInputState = &vertex;
pipeline.pInputAssemblyState = &assembly;
pipeline.pViewportState = &viewport;
pipeline.pRasterizationState = &raster;
pipeline.pMultisampleState = &samples;
pipeline.pColorBlendState = &blend;
pipeline.pDynamicState = &dynamic;
pipeline.layout = key.pipelineLayout;
pipeline.renderPass = key.renderPass;
VK_VERIFY(vkCreateGraphicsPipelines(m_device, VK_NULL_HANDLE, 1, &pipeline, nullptr, &key.pipeline));
cache.pipelines.push_back(key);
MGLOG_I("Magma: YUV conversion pipeline %zu: %s format %llu, model %d, range %d, chroma %d/%d, "
"filter %d",
index, key.externalFormat != 0 ? "external" : "Vulkan",
static_cast<unsigned long long>(key.externalFormat != 0 ? key.externalFormat
: static_cast<Uint64>(key.format)),
static_cast<Int>(key.model), static_cast<Int>(key.range), static_cast<Int>(key.xOffset),
static_cast<Int>(key.yOffset), static_cast<Int>(key.chromaFilter));
}
const auto& chosen = cache.pipelines[index];
// The buffer, imported with its external format, sampled only.
auto created = std::make_shared<WireYuvSource>();
created->device = m_device;
created->pipeline = index;
created->hints = hints;
VkExternalFormatANDROID external{VK_STRUCTURE_TYPE_EXTERNAL_FORMAT_ANDROID};
external.externalFormat = chosen.externalFormat;
VkExternalMemoryImageCreateInfo externalMemory{VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO};
externalMemory.pNext = chosen.externalFormat != 0 ? &external : nullptr;
externalMemory.handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID;
VkImageCreateInfo imageInfo{VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO};
imageInfo.pNext = &externalMemory;
imageInfo.imageType = VK_IMAGE_TYPE_2D;
imageInfo.format = chosen.format;
imageInfo.extent = {image->Width, image->Height, 1};
imageInfo.mipLevels = 1;
imageInfo.arrayLayers = 1;
imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
imageInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
imageInfo.usage = VK_IMAGE_USAGE_SAMPLED_BIT;
imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
if (vkCreateImage(m_device, &imageInfo, nullptr, &created->image) != VK_SUCCESS) {
created->image = VK_NULL_HANDLE;
MGLOG_E_ONCE("Magma: YUV image %llu: vkCreateImage(external format) failed",
static_cast<unsigned long long>(image->Id));
return false;
}
Int32 type = -1;
for (Uint32 i = 0; i < ctx.memory.memoryTypeCount && type < 0; ++i) {
if ((properties.memoryTypeBits & (1u << i)) != 0) type = static_cast<Int32>(i);
}
VkMemoryDedicatedAllocateInfo dedicated{VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO};
dedicated.image = created->image;
VkImportAndroidHardwareBufferInfoANDROID import{
VK_STRUCTURE_TYPE_IMPORT_ANDROID_HARDWARE_BUFFER_INFO_ANDROID};
import.pNext = &dedicated;
import.buffer = ahb;
VkMemoryAllocateInfo allocate{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO};
allocate.pNext = &import;
allocate.allocationSize = properties.allocationSize;
allocate.memoryTypeIndex = static_cast<Uint32>(type < 0 ? 0 : type);
if (type < 0 || vkAllocateMemory(m_device, &allocate, nullptr, &created->memory) != VK_SUCCESS ||
vkBindImageMemory(m_device, created->image, created->memory, 0) != VK_SUCCESS) {
MGLOG_E_ONCE("Magma: YUV image %llu: its buffer could not be imported",
static_cast<unsigned long long>(image->Id));
return false;
}
VkSamplerYcbcrConversionInfo conversionInfo{VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_INFO};
conversionInfo.conversion = chosen.conversion;
VkImageViewCreateInfo view{VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO};
view.pNext = &conversionInfo;
view.image = created->image;
view.viewType = VK_IMAGE_VIEW_TYPE_2D;
view.format = chosen.format;
view.components = {VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY,
VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY};
view.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1};
if (vkCreateImageView(m_device, &view, nullptr, &created->view) != VK_SUCCESS) {
created->view = VK_NULL_HANDLE;
MGLOG_E_ONCE("Magma: YUV image %llu: its view could not be created",
static_cast<unsigned long long>(image->Id));
return false;
}
MGLOG_I("Magma: YUV image %llu (%ux%u%s) sampled through conversion pipeline %zu",
static_cast<unsigned long long>(image->Id), image->Width, image->Height,
image->IsForeign() ? ", foreign: CPU-copied" : ", zero-copy", index);
resource.yuvSource = created;
source = created.get();
}
const auto& chosen = cache.pipelines[source->pipeline];
// ---- the pass ----
auto& frame = m_frameContext.GetCurrent();
if (!frame.isCommandRecording) m_frameContext.BeginCommandRecording();
if (VkRenderPassManager::GetActiveRenderPass()) VkRenderPassManager::EndRenderPass(frame.commandBuffer);
m_textureManager->FlushPendingUploads();
const Uint32 family = static_cast<Uint32>(m_physicalDevice.queueFamilies.graphicsFamily);
// The buffer's content is its producer's (the CPU's copy, or the allocator's writer): it is
// acquired from the foreign family for the read and handed back after it.
VkImageMemoryBarrier acquire{VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER};
acquire.srcAccessMask = 0;
acquire.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
acquire.oldLayout = VK_IMAGE_LAYOUT_GENERAL;
acquire.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
acquire.srcQueueFamilyIndex = VK_QUEUE_FAMILY_FOREIGN_EXT;
acquire.dstQueueFamilyIndex = family;
acquire.image = source->image;
acquire.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1};
EndActiveRenderPassOn(frame.commandBuffer);
vkCmdPipelineBarrier(frame.commandBuffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, 0, 0, nullptr, 0, nullptr, 1, &acquire);
WireImage target{};
target.image = resource.image;
target.format = resource.format;
target.viewType = VK_IMAGE_VIEW_TYPE_2D;
target.extent = resource.extent;
target.layout = resource.layout;
target.trackedLayout = &resource.layout;
target.aspect = VK_IMAGE_ASPECT_COLOR_BIT;
target.usageFlags = resource.usageFlags;
target.levels = resource.mipLevels;
TransitionWireImage(target, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
VkImageViewCreateInfo targetViewInfo{VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO};
targetViewInfo.image = resource.image;
targetViewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
targetViewInfo.format = resource.format;
targetViewInfo.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1};
VkImageView targetView = VK_NULL_HANDLE;
VK_VERIFY(vkCreateImageView(m_device, &targetViewInfo, nullptr, &targetView));
VkFramebufferCreateInfo framebufferInfo{VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO};
framebufferInfo.renderPass = chosen.renderPass;
framebufferInfo.attachmentCount = 1;
framebufferInfo.pAttachments = &targetView;
framebufferInfo.width = resource.extent.width;
framebufferInfo.height = resource.extent.height;
framebufferInfo.layers = 1;
VkFramebuffer framebuffer = VK_NULL_HANDLE;
VK_VERIFY(vkCreateFramebuffer(m_device, &framebufferInfo, nullptr, &framebuffer));
// A multi-planar image may take more than one descriptor of its binding.
VkDescriptorPool descriptorPool = VK_NULL_HANDLE;
VkDescriptorSet descriptorSet = VK_NULL_HANDLE;
const VkDescriptorPoolSize poolSize{VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 3};
VkDescriptorPoolCreateInfo pool{VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO};
pool.maxSets = 1;
pool.poolSizeCount = 1;
pool.pPoolSizes = &poolSize;
VK_VERIFY(vkCreateDescriptorPool(m_device, &pool, nullptr, &descriptorPool));
VkDescriptorSetAllocateInfo alloc{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO};
alloc.descriptorPool = descriptorPool;
alloc.descriptorSetCount = 1;
alloc.pSetLayouts = &chosen.descriptorLayout;
VK_VERIFY(vkAllocateDescriptorSets(m_device, &alloc, &descriptorSet));
const VkDescriptorImageInfo sampled{chosen.sampler, source->view, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL};
VkWriteDescriptorSet write{VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET};
write.dstSet = descriptorSet;
write.dstBinding = 0;
write.descriptorCount = 1;
write.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
write.pImageInfo = &sampled;
vkUpdateDescriptorSets(m_device, 1, &write, 0, nullptr);
VkRenderPassBeginInfo begin{VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO};
begin.renderPass = chosen.renderPass;
begin.framebuffer = framebuffer;
begin.renderArea = {{0, 0}, resource.extent};
vkCmdBeginRenderPass(frame.commandBuffer, &begin, VK_SUBPASS_CONTENTS_INLINE);
vkCmdBindPipeline(frame.commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, chosen.pipeline);
const VkViewport viewport{0.0f, 0.0f, static_cast<float>(resource.extent.width),
static_cast<float>(resource.extent.height), 0.0f, 1.0f};
const VkRect2D scissor{{0, 0}, resource.extent};
vkCmdSetViewport(frame.commandBuffer, 0, 1, &viewport);
vkCmdSetScissor(frame.commandBuffer, 0, 1, &scissor);
vkCmdBindDescriptorSets(frame.commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, chosen.pipelineLayout, 0, 1,
&descriptorSet, 0, nullptr);
vkCmdDraw(frame.commandBuffer, 3, 1, 0, 0);
vkCmdEndRenderPass(frame.commandBuffer);
VkImageMemoryBarrier release = acquire;
release.srcAccessMask = VK_ACCESS_SHADER_READ_BIT;
release.dstAccessMask = 0;
release.oldLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
release.newLayout = VK_IMAGE_LAYOUT_GENERAL;
release.srcQueueFamilyIndex = family;
release.dstQueueFamilyIndex = VK_QUEUE_FAMILY_FOREIGN_EXT;
EndActiveRenderPassOn(frame.commandBuffer);
vkCmdPipelineBarrier(frame.commandBuffer, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, nullptr, 0, nullptr, 1, &release);
// GENERAL, the layout every wire descriptor samples in, with the pass's writes made visible.
target.layout = resource.layout;
TransitionWireImage(target, VK_IMAGE_LAYOUT_GENERAL);
ResetDynamicStateShadow();
InvalidatePipelineMemo();
if (m_uniformManager) m_uniformManager->OnCommandBufferBoundary();
m_textureManager->StampResourceRecordingUse(resource);
m_textureManager->StampResourceRecordingWrite(resource);
WireRetiredObjects retired{};
retired.framebuffers.push_back(framebuffer);
retired.imageViews = {targetView};
retired.descriptorPools.push_back(descriptorPool);
RetireWireObjects(Move(retired));
// The frame's fence orders the next refresh of a foreign buffer after this read.
m_sharedImageReads.NoteRead(image);
return true;
#else
(void)resource;
return false;
#endif
}
#endif // MOBILEGL_BUILD_DISAGGREGATED
+184
View File
@@ -0,0 +1,184 @@
// MobileGL - MobileGL/MG_Backend/GpuHangWatch.cpp
// Copyright (c) 2025-2026 MobileGL-Dev
// Licensed under the GNU Lesser General Public License v3.0:
// https://www.gnu.org/licenses/gpl-3.0.txt
// https://www.gnu.org/licenses/lgpl-3.0.txt
// SPDX-License-Identifier: LGPL-3.0-only
// End of Source File Header
#include "GpuHangWatch.h"
#include <MG_Util/Debug/Log.h>
#include <algorithm>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <cstdlib>
#include <thread>
#if defined(__ANDROID__)
#include <sys/system_properties.h>
#endif
#if defined(__linux__) || defined(__ANDROID__)
#include <pthread.h>
#endif
namespace MobileGL::MG_Backend::GpuHangWatch {
namespace {
constexpr Uint32 kDefaultPollMs = 250;
std::atomic<Uint32> g_pollOverrideMs{0};
std::atomic<Uint32> g_budgetOverrideMs{0};
std::atomic<Bool> g_budgetOverridden{false};
Uint64 NowMs() {
return static_cast<Uint64>(std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now().time_since_epoch())
.count());
}
Uint32 ReadBudgetMs() {
const char* value = std::getenv("MOBILEGL_GPU_HANG_BUDGET_MS");
#if defined(__ANDROID__)
char property[PROP_VALUE_MAX] = {};
if (value == nullptr && __system_property_get("debug.mobilegl.gpu_hang_budget_ms", property) > 0)
value = property;
#endif
if (value == nullptr || *value == '\0') return kDefaultBudgetMs;
char* end = nullptr;
const unsigned long parsed = std::strtoul(value, &end, 10);
if (end == value) return kDefaultBudgetMs;
return static_cast<Uint32>(std::min<unsigned long>(parsed, 600000ul));
}
struct Entry {
Uint64 Handle = 0;
void* Self = nullptr;
SampleFn Sample = nullptr;
HungFn Hung = nullptr;
Detector Decide{0};
Bool Done = false; // named a hang, or its device is gone: never sampled again
};
// Leaked at exit, with its thread: the watch holds no GPU object of its own, and a backend
// unregisters before it destroys what its sampler reads.
struct Watch {
std::mutex Mutex;
std::condition_variable Wake;
Vector<Entry> Entries;
Uint64 NextHandle = 1;
Bool ThreadStarted = false;
};
Watch& TheWatch() {
static Watch& watch = *new Watch();
return watch;
}
void PollOnce(Watch& watch) {
const Uint64 now = NowMs();
Vector<SubmissionProgress> sample;
for (Entry& entry : watch.Entries) {
if (entry.Done) continue;
sample.clear();
if (!entry.Sample(entry.Self, sample)) {
entry.Done = true;
continue;
}
const Uint64 hung = entry.Decide.Observe(sample, now);
if (hung == 0) continue;
entry.Done = true;
entry.Hung(entry.Self, hung, entry.Decide.RunningMs(hung, now));
}
}
void WatchThreadMain() {
#if defined(__linux__) || defined(__ANDROID__)
pthread_setname_np(pthread_self(), "mgl-gpu-watch");
#endif
Watch& watch = TheWatch();
std::unique_lock<std::mutex> lock(watch.Mutex);
for (;;) {
watch.Wake.wait_for(lock, std::chrono::milliseconds(PollIntervalMs()));
PollOnce(watch);
}
}
} // namespace
Uint32 BudgetMs() {
if (g_budgetOverridden.load(std::memory_order_acquire)) return g_budgetOverrideMs.load(std::memory_order_acquire);
static const Uint32 budget = ReadBudgetMs();
return budget;
}
Uint32 PollIntervalMs() {
const Uint32 overrideMs = g_pollOverrideMs.load(std::memory_order_acquire);
return overrideMs != 0 ? overrideMs : kDefaultPollMs;
}
void SetPollIntervalForTesting(Uint32 ms) { g_pollOverrideMs.store(ms, std::memory_order_release); }
void SetBudgetForTesting(Uint32 ms) {
g_budgetOverrideMs.store(ms, std::memory_order_release);
g_budgetOverridden.store(ms != 0, std::memory_order_release);
}
Uint64 Detector::Observe(const Vector<SubmissionProgress>& sample, Uint64 nowMs) {
Vector<Running> still;
still.reserve(sample.size());
Uint64 named = 0;
for (const SubmissionProgress& progress : sample) {
if (!progress.Started || progress.Finished) continue;
Uint64 since = nowMs;
for (const Running& seen : m_running) {
if (seen.Id == progress.Id) {
since = seen.SinceMs;
break;
}
}
still.push_back({progress.Id, since});
if (named == 0 && m_budgetMs != 0 && nowMs - since >= m_budgetMs) named = progress.Id;
}
m_running.swap(still);
return named;
}
Uint64 Detector::RunningMs(Uint64 id, Uint64 nowMs) const {
for (const Running& seen : m_running)
if (seen.Id == id) return nowMs - seen.SinceMs;
return 0;
}
Uint64 Register(void* self, SampleFn sample, HungFn hung) {
const Uint32 budget = BudgetMs();
if (budget == 0 || sample == nullptr || hung == nullptr) return 0;
Watch& watch = TheWatch();
const std::lock_guard<std::mutex> lock(watch.Mutex);
Entry entry;
entry.Handle = watch.NextHandle++;
entry.Self = self;
entry.Sample = sample;
entry.Hung = hung;
entry.Decide = Detector(budget);
watch.Entries.push_back(entry);
if (!watch.ThreadStarted) {
watch.ThreadStarted = true;
std::thread(WatchThreadMain).detach();
}
return entry.Handle;
}
void Unregister(Uint64 handle) {
if (handle == 0) return;
Watch& watch = TheWatch();
// The poll pass runs under this lock, so taking it waits out any call into the entry.
const std::lock_guard<std::mutex> lock(watch.Mutex);
for (SizeT i = 0; i < watch.Entries.size(); ++i) {
if (watch.Entries[i].Handle != handle) continue;
watch.Entries.erase(watch.Entries.begin() + static_cast<std::ptrdiff_t>(i));
return;
}
}
} // namespace MobileGL::MG_Backend::GpuHangWatch
+94
View File
@@ -0,0 +1,94 @@
// MobileGL - MobileGL/MG_Backend/GpuHangWatch.h
// Copyright (c) 2025-2026 MobileGL-Dev
// Licensed under the GNU Lesser General Public License v3.0:
// https://www.gnu.org/licenses/gpl-3.0.txt
// https://www.gnu.org/licenses/lgpl-3.0.txt
// SPDX-License-Identifier: LGPL-3.0-only
// End of Source File Header
#pragma once
// A SESSION WHOSE GPU WORK NEVER ENDS IS FOUND AND NAMED, SO IT CANNOT HANG THE GPU AGAIN.
//
// A submission whose shader never ends stops the whole GPU, not one context: every process's work -
// the compositor's, other clients', processes outside MobileGL at the highest context priority -
// waits until the kernel resets it. Measured on Adreno 750: ~2 s when nothing of higher priority
// waits (the context is then lost: Vulkan answers VK_ERROR_DEVICE_LOST), ~10 s when something of
// higher priority does (a failed preemption; the context is NOT lost, and its next frame hangs the
// GPU again). Nothing in user space can shorten one such stall; what the server can do is make sure
// the session that caused it never submits again, so a page or program that hangs every frame
// costs the desktop one stall instead of one per frame.
//
// The attribution must be exact - every session's work is pending while the GPU is stuck, and the
// compositor's must never be blamed. A backend that can tell, per in-flight submission, whether the
// GPU has STARTED it and whether it has FINISHED it gives exactly that: the GPU runs one submission
// at a time on a ring, so the one that started and does not finish is the one that hangs, and the
// ones queued behind it have not started. Magma brackets each submission with two events
// (DirectVulkan/Renderer/GpuProgressMarkers.h). Espryt relies on the driver instead: its contexts ask
// for reset notification and glGetGraphicsResetStatus names the guilty one - measured after a ~2 s
// timeout reset; after a ~10 s failed-preemption reset the server's contexts were NOT told (a
// standalone process's context was), so on Espryt that path is not contained yet.
//
// The Detector is the decision, pure and testable; the watch thread samples every registered backend
// on its own clock, because the session that hangs is often not running - its apply thread waits for
// its own fence, or for the backend turn another session holds while it waits for the GPU.
#include <Includes.h>
#include <mutex>
namespace MobileGL::MG_Backend::GpuHangWatch {
// MOBILEGL_GPU_HANG_BUDGET_MS: how long one submission may run once the GPU has started it before
// its session is ended as the one that hangs the GPU. Default 2000 - the kernel resets a hung GPU
// by itself after about that when nothing preempts it, and no desktop frame runs that long. 0 = off.
Uint32 BudgetMs();
constexpr Uint32 kDefaultBudgetMs = 2000;
// One in-flight submission as the GPU reports progress on it.
struct SubmissionProgress {
Uint64 Id = 0; // the backend's own monotonic id for it; never reused
Bool Started = false; // the GPU has begun executing it
Bool Finished = false; // ... and completed it
};
// Which submission, if any, has run past the budget: started, not finished, and seen in that
// state for at least `budgetMs` (from the first sample that saw it running - the watch cannot know
// when between two samples it began, so a hang is named between budget and budget + one interval).
class Detector {
public:
explicit Detector(Uint32 budgetMs) : m_budgetMs(budgetMs) {}
// One sample of every in-flight submission at `nowMs`. Returns the id of the submission that
// ran past the budget, or 0. Submissions missing from the sample are forgotten.
Uint64 Observe(const Vector<SubmissionProgress>& sample, Uint64 nowMs);
// How long the named submission had been seen running when Observe named it.
Uint64 RunningMs(Uint64 id, Uint64 nowMs) const;
private:
struct Running {
Uint64 Id = 0;
Uint64 SinceMs = 0;
};
Uint32 m_budgetMs = 0;
Vector<Running> m_running;
};
// ---- the process-wide watch ---------------------------------------------------------------
//
// A backend registers a sampler and a callback. The watch thread calls the sampler every
// PollIntervalMs() on its own thread, and the callback once, when the Detector names a
// submission; the backend acts on it from its own thread (it may not latch a session from this
// one). Both are called with the watch's lock held: keep them short, and never call Unregister
// from them. Unregister returns only once no call into the registration runs or will run.
using SampleFn = Bool (*)(void* self, Vector<SubmissionProgress>& out); // false: stop watching it
using HungFn = void (*)(void* self, Uint64 submissionId, Uint64 runningMs);
Uint64 Register(void* self, SampleFn sample, HungFn hung);
void Unregister(Uint64 handle);
Uint32 PollIntervalMs();
// Tests: the interval and the budget the next Register uses (0 restores the defaults).
void SetPollIntervalForTesting(Uint32 ms);
void SetBudgetForTesting(Uint32 ms);
} // namespace MobileGL::MG_Backend::GpuHangWatch
+114 -17
View File
@@ -17,9 +17,14 @@
// process's MobileGL client session. Whatever node the caller holds is therefore equally good,
// and none is vendor-specific.
//
// THE LAYOUT IS THE ALLOCATOR'S. Buffers report DRM_FORMAT_MOD_INVALID and a single plane; a
// request that needs a known layout (LINEAR, or a modifier list without INVALID) is refused
// rather than answered with a buffer whose layout is not what was asked for.
// THE LAYOUT IS THE ALLOCATOR'S. Buffers report DRM_FORMAT_MOD_INVALID and a single plane (two for
// the YUV formats, both in the one descriptor); a request that needs a known layout (LINEAR, or a
// modifier list without INVALID) is refused rather than answered with a buffer whose layout is not
// what was asked for.
//
// YUV (NV12, P010) buffers exist when the server's platform allocates them (the shared-image ABI's
// format list says so). They are sampled, never rendered to: a GBM_BO_USE_RENDERING request for one
// is refused.
//
// Buffer operations reach the server through libMobileGL.so's shared-image C ABI
// (MG_Impl/SharedImageApi.h), resolved with dlsym on first use - preferably from the instance the
@@ -40,6 +45,7 @@
#include <dlfcn.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#define MOBILEGL_GBM_EXPORT extern "C" __attribute__((visibility("default")))
@@ -65,6 +71,10 @@ namespace {
int fd; // owned; every get_fd hands out a duplicate
uint32_t offset;
uint64_t modifier;
// A YUV buffer's plane 1, in the same descriptor.
uint32_t planes;
uint32_t plane1Stride;
uint32_t plane1Offset;
};
// ---- libMobileGL's shared-image ABI -----------------------------------------------------
@@ -74,6 +84,9 @@ namespace {
PFN_mobilegl_shared_image_import import = nullptr;
PFN_mobilegl_shared_image_release release = nullptr;
PFN_mobilegl_server_available serverAvailable = nullptr; // optional (older libraries lack it)
// ABI 2 (YUV); null on an older library.
PFN_mobilegl_shared_image_formats formats = nullptr;
PFN_mobilegl_shared_image_import_planes importPlanes = nullptr;
};
// The library the process already has (by its soname: glvnd loads it by path, which records
@@ -107,6 +120,12 @@ namespace {
std::fprintf(stderr, "mobilegl_gbm: this libMobileGL.so has no shared-image ABI\n");
return;
}
if (version() >= 2) {
api.formats = reinterpret_cast<PFN_mobilegl_shared_image_formats>(
dlsym(lib, "mobilegl_shared_image_formats"));
api.importPlanes = reinterpret_cast<PFN_mobilegl_shared_image_import_planes>(
dlsym(lib, "mobilegl_shared_image_import_planes"));
}
resolved = true;
});
return resolved ? &api : nullptr;
@@ -125,11 +144,41 @@ namespace {
// What a shared image can be. The ARGB/XRGB orders are stored like the ABGR/XBGR ones: the
// layout is the server's (modifier INVALID, no map is offered), so no reader can tell, and
// every GL that touches the image addresses its channels logically.
bool FormatIsYuv(uint32_t format) {
return format == MOBILEGL_SHARED_IMAGE_FOURCC_NV12 || format == MOBILEGL_SHARED_IMAGE_FOURCC_P010;
}
// Whether the server holds YUV images: the library's format list, asked once (it takes the
// process's session, which the first buffer would anyway).
bool YuvFormatAvailable(uint32_t format) {
static std::once_flag once;
static bool nv12 = false, p010 = false;
std::call_once(once, [] {
const SharedImageApi* api = Api();
if (api == nullptr || api->formats == nullptr || api->importPlanes == nullptr) return;
uint32_t formats[16] = {};
const int total = api->formats(formats, 16);
for (int i = 0; i < total && i < 16; ++i) {
nv12 |= formats[i] == MOBILEGL_SHARED_IMAGE_FOURCC_NV12;
p010 |= formats[i] == MOBILEGL_SHARED_IMAGE_FOURCC_P010;
}
});
return format == MOBILEGL_SHARED_IMAGE_FOURCC_NV12 ? nv12 : format == MOBILEGL_SHARED_IMAGE_FOURCC_P010 && p010;
}
bool FormatSupported(uint32_t format) {
if (FormatIsYuv(format)) return YuvFormatAvailable(format);
return format == MOBILEGL_SHARED_IMAGE_FOURCC_ABGR8888 || format == MOBILEGL_SHARED_IMAGE_FOURCC_XBGR8888 ||
format == MOBILEGL_SHARED_IMAGE_FOURCC_ARGB8888 || format == MOBILEGL_SHARED_IMAGE_FOURCC_XRGB8888;
}
uint32_t PlaneCount(uint32_t format) { return FormatIsYuv(format) ? 2 : 1; }
// A YUV buffer is sampled only.
uint32_t RefusedUsage(uint32_t format) {
return kRefusedUsage | (FormatIsYuv(format) ? static_cast<uint32_t>(GBM_BO_USE_RENDERING) : 0u);
}
bool ModifierAcceptable(uint64_t modifier) { return modifier == kModifierInvalid || modifier == kModifierLinear; }
// There is no GEM handle behind a shared image; callers that only use the handle as a key
@@ -142,7 +191,8 @@ namespace {
}
gbm_bo* NewBo(gbm_device* gbm, uint64_t imageId, int fd, uint32_t width, uint32_t height, uint32_t format,
uint32_t stride, uint32_t offset, uint64_t modifier) {
uint32_t stride, uint32_t offset, uint64_t modifier, uint32_t plane1Stride = 0,
uint32_t plane1Offset = 0) {
auto* bo = new (std::nothrow) Bo{};
if (bo == nullptr) {
errno = ENOMEM;
@@ -158,6 +208,9 @@ namespace {
bo->fd = fd;
bo->offset = offset;
bo->modifier = modifier;
bo->planes = PlaneCount(format);
bo->plane1Stride = plane1Stride;
bo->plane1Offset = plane1Offset;
return &bo->base;
}
@@ -168,18 +221,20 @@ namespace {
void DeviceDestroy(gbm_device* gbm) { delete reinterpret_cast<Device*>(gbm); }
int IsFormatSupported(gbm_device*, uint32_t format, uint32_t usage) {
return FormatSupported(Canonical(format)) && (usage & kRefusedUsage) == 0 ? 1 : 0;
format = Canonical(format);
return FormatSupported(format) && (usage & RefusedUsage(format)) == 0 ? 1 : 0;
}
int GetFormatModifierPlaneCount(gbm_device*, uint32_t format, uint64_t modifier) {
if (!FormatSupported(Canonical(format)) || !ModifierAcceptable(modifier)) return -1;
return 1;
format = Canonical(format);
if (!FormatSupported(format) || !ModifierAcceptable(modifier)) return -1;
return static_cast<int>(PlaneCount(format));
}
gbm_bo* BoCreate(gbm_device* gbm, uint32_t width, uint32_t height, uint32_t format, uint32_t usage,
const uint64_t* modifiers, const unsigned int count) {
format = Canonical(format);
if (!FormatSupported(format) || (usage & kRefusedUsage) != 0) {
if (!FormatSupported(format) || (usage & RefusedUsage(format)) != 0) {
errno = EINVAL;
return nullptr;
}
@@ -207,7 +262,7 @@ namespace {
return nullptr;
}
gbm_bo* bo = NewBo(gbm, image.id, image.fd, image.width, image.height, image.fourcc, image.stride,
image.offset, image.modifier);
image.offset, image.modifier, image.plane1_stride, image.plane1_offset);
if (bo == nullptr) {
api->release(image.id);
::close(image.fd);
@@ -215,6 +270,13 @@ namespace {
return bo;
}
// Whether two descriptors name one dma-buf: a YUV buffer's planes must be in one.
bool SameBuffer(int a, int b) {
if (a == b) return true;
struct stat sa {}, sb {};
return ::fstat(a, &sa) == 0 && ::fstat(b, &sb) == 0 && sa.st_dev == sb.st_dev && sa.st_ino == sb.st_ino;
}
gbm_bo* BoImport(gbm_device* gbm, uint32_t type, void* buffer, uint32_t usage) {
(void)usage;
if (buffer == nullptr) {
@@ -223,6 +285,7 @@ namespace {
}
int fd = -1;
uint32_t width = 0, height = 0, format = 0, stride = 0, offset = 0;
uint32_t plane1Stride = 0, plane1Offset = 0;
uint64_t modifier = kModifierInvalid;
switch (type) {
case GBM_BO_IMPORT_FD: {
@@ -232,11 +295,19 @@ namespace {
height = data->height;
format = data->format;
stride = data->stride;
// One fd and one stride cannot say where a second plane is.
if (FormatIsYuv(Canonical(format))) {
errno = EINVAL;
return nullptr;
}
break;
}
case GBM_BO_IMPORT_FD_MODIFIER: {
const auto* data = static_cast<const gbm_import_fd_modifier_data*>(buffer);
if (data->num_fds != 1 || !ModifierAcceptable(data->modifier)) {
const bool yuv = FormatIsYuv(Canonical(data->format));
const unsigned int planes = yuv ? 2u : 1u;
if (data->num_fds < 1 || data->num_fds > planes || !ModifierAcceptable(data->modifier) ||
(data->num_fds == 2 && !SameBuffer(data->fds[0], data->fds[1]))) {
errno = EINVAL;
return nullptr;
}
@@ -246,6 +317,10 @@ namespace {
format = data->format;
stride = static_cast<uint32_t>(data->strides[0]);
offset = static_cast<uint32_t>(data->offsets[0]);
if (yuv) {
plane1Stride = static_cast<uint32_t>(data->strides[1]);
plane1Offset = static_cast<uint32_t>(data->offsets[1]);
}
modifier = data->modifier;
break;
}
@@ -269,13 +344,26 @@ namespace {
const int owned = ::fcntl(fd, F_DUPFD_CLOEXEC, 0);
if (owned < 0) return nullptr;
uint64_t id = 0;
const int rc = api->import(owned, width, height, format, &id);
int rc = 0;
if (FormatIsYuv(format)) {
mobilegl_shared_image_planes planes{};
planes.struct_size = sizeof(planes);
planes.plane_count = 2;
planes.offset[0] = offset;
planes.offset[1] = plane1Offset;
planes.pitch[0] = stride;
planes.pitch[1] = plane1Stride;
planes.modifier = modifier;
rc = api->importPlanes(owned, width, height, format, &planes, &id);
} else {
rc = api->import(owned, width, height, format, &id);
}
if (rc != 0) {
::close(owned);
errno = -rc;
return nullptr;
}
gbm_bo* bo = NewBo(gbm, id, owned, width, height, format, stride, offset, modifier);
gbm_bo* bo = NewBo(gbm, id, owned, width, height, format, stride, offset, modifier, plane1Stride, plane1Offset);
if (bo == nullptr) {
api->release(id);
::close(owned);
@@ -299,11 +387,14 @@ namespace {
int BoGetFd(gbm_bo* bo) { return ::fcntl(AsBo(bo)->fd, F_DUPFD_CLOEXEC, 0); }
int BoGetPlanes(gbm_bo*) { return 1; }
int BoGetPlanes(gbm_bo* bo) { return static_cast<int>(AsBo(bo)->planes); }
bool ValidPlane(gbm_bo* bo, int plane) { return plane >= 0 && static_cast<uint32_t>(plane) < AsBo(bo)->planes; }
// Every plane is in the one buffer, so every plane has its handle.
gbm_bo_handle BoGetHandle(gbm_bo* bo, int plane) {
gbm_bo_handle handle{};
if (plane != 0) {
if (!ValidPlane(bo, plane)) {
errno = EINVAL;
handle.s32 = -1;
return handle;
@@ -312,16 +403,22 @@ namespace {
}
int BoGetPlaneFd(gbm_bo* bo, int plane) {
if (plane != 0) {
if (!ValidPlane(bo, plane)) {
errno = EINVAL;
return -1;
}
return BoGetFd(bo);
}
uint32_t BoGetStride(gbm_bo* bo, int plane) { return plane == 0 ? bo->v0.stride : 0; }
uint32_t BoGetStride(gbm_bo* bo, int plane) {
if (plane == 0) return bo->v0.stride;
return plane == 1 && AsBo(bo)->planes == 2 ? AsBo(bo)->plane1Stride : 0;
}
uint32_t BoGetOffset(gbm_bo* bo, int plane) { return plane == 0 ? AsBo(bo)->offset : 0; }
uint32_t BoGetOffset(gbm_bo* bo, int plane) {
if (plane == 0) return AsBo(bo)->offset;
return plane == 1 && AsBo(bo)->planes == 2 ? AsBo(bo)->plane1Offset : 0;
}
uint64_t BoGetModifier(gbm_bo* bo) { return AsBo(bo)->modifier; }
+73 -6
View File
@@ -38,6 +38,10 @@
#include <cstring>
#include <mutex>
#include <sstream>
#if !defined(_WIN32)
#include <sys/stat.h>
#include <unistd.h>
#endif
#if defined(__linux__) && !defined(__ANDROID__)
#include <dlfcn.h>
#endif
@@ -1608,6 +1612,38 @@ namespace MobileGL::MG_Impl::EGLImpl {
#endif
}
// Whether the server can hold YUV images (its platform allocates the formats): asked once, by
// allocating and dropping a small NV12 image, and only when a YUV question first comes up.
Bool SharedImageYuvAvailable() {
if (!SharedImagesAvailable()) return false;
static std::atomic<Int> answer{-1};
const Int known = answer.load(std::memory_order_acquire);
if (known >= 0) return known != 0;
auto* backendObject = MG_Backend::pActiveBackendObject.get();
if (backendObject == nullptr) return false;
MG_Backend::SharedImageExport probe{};
const Bool allocated = backendObject->AllocateSharedImage(
16, 16, MG_State::EGLState::EGLContext::kDrmFourccNv12, &probe);
#if !defined(_WIN32)
if (probe.Fd >= 0) ::close(probe.Fd);
#endif
if (allocated && probe.Id != 0) (void)backendObject->ReleaseSharedImage(probe.Id);
answer.store(allocated ? 1 : 0, std::memory_order_release);
MGLOG_I("Shared images: the server %s YUV (NV12) images", allocated ? "holds" : "has no");
return allocated;
}
// Whether two descriptors name one dma-buf (a YUV import's planes must be in one buffer).
Bool SameDmaBuf(int a, int b) {
if (a == b) return true;
#if defined(_WIN32)
return false;
#else
struct stat sa {}, sb {};
return ::fstat(a, &sa) == 0 && ::fstat(b, &sb) == 0 && sa.st_dev == sb.st_dev && sa.st_ino == sb.st_ino;
#endif
}
Bool LookupSharedImage(EGLImage image, Uint64* id, EGLint* width, EGLint* height) {
auto* state = GetState();
MG_State::EGLState::EGLContext::SharedImageInfo info;
@@ -1673,10 +1709,41 @@ namespace MobileGL::MG_Impl::EGLImpl {
}
auto* backendObject = GetBackendObject(state);
Uint64 sharedImageId = 0;
if (!backendObject ||
!backendObject->ImportSharedImage(request.Fd, static_cast<Uint32>(request.Width),
static_cast<Uint32>(request.Height), request.Fourcc, &sharedImageId) ||
sharedImageId == 0) {
Bool imported = false;
if (MG_State::EGLState::EGLContext::IsDmaBufFourccYuv(request.Fourcc)) {
// YUV: both planes in one buffer (what video decoders and this server's allocator hand
// out), sampled through GL_OES_EGL_image_external. Planes in two buffers are refused.
if (!SharedImageYuvAvailable() || !SameDmaBuf(request.Fd, request.Plane1Fd)) {
MGLOG_I_ONCE("eglCreateImage: a %dx%d YUV dma-buf cannot be imported (%s); refused (EGL_BAD_MATCH)",
request.Width, request.Height,
SharedImageYuvAvailable() ? "its planes are in two buffers"
: "the server has no YUV images");
state->SetError(EGL_BAD_MATCH);
return EGL_NO_IMAGE;
}
MG_Backend::SharedImageImportLayout layout;
layout.PlaneCount = 2;
layout.Offset[0] = static_cast<Uint32>(request.Offset);
layout.Offset[1] = static_cast<Uint32>(request.Plane1Offset);
layout.Pitch[0] = static_cast<Uint32>(request.Pitch);
layout.Pitch[1] = static_cast<Uint32>(request.Plane1Pitch);
layout.ColorSpace = request.ColorSpace;
layout.Range = request.Range;
layout.SitingX = request.SitingX;
layout.SitingY = request.SitingY;
layout.HasModifier = request.HasModifier;
layout.Modifier = request.Modifier;
imported = backendObject != nullptr &&
backendObject->ImportSharedImagePlanes(request.Fd, static_cast<Uint32>(request.Width),
static_cast<Uint32>(request.Height), request.Fourcc,
layout, &sharedImageId);
} else {
imported = backendObject != nullptr &&
backendObject->ImportSharedImage(request.Fd, static_cast<Uint32>(request.Width),
static_cast<Uint32>(request.Height), request.Fourcc,
&sharedImageId);
}
if (!imported || sharedImageId == 0) {
MGLOG_I_ONCE("eglCreateImage: a %dx%d dma-buf is not one of the server's shared images; refused "
"(EGL_BAD_MATCH)",
request.Width, request.Height);
@@ -1731,7 +1798,7 @@ namespace MobileGL::MG_Impl::EGLImpl {
if (!state) {
return EGL_FALSE;
}
return state->QueryDmaBufFormats(dpy, SharedImagesAvailable(), max_formats, formats, num_formats)
return state->QueryDmaBufFormats(dpy, SharedImagesAvailable(), SharedImageYuvAvailable(), max_formats, formats, num_formats)
? EGL_TRUE
: EGL_FALSE;
}
@@ -1743,7 +1810,7 @@ namespace MobileGL::MG_Impl::EGLImpl {
if (!state) {
return EGL_FALSE;
}
return state->QueryDmaBufModifiers(dpy, SharedImagesAvailable(), format, max_modifiers,
return state->QueryDmaBufModifiers(dpy, SharedImagesAvailable(), SharedImageYuvAvailable(), format, max_modifiers,
reinterpret_cast<Uint64*>(modifiers), external_only, num_modifiers)
? EGL_TRUE
: EGL_FALSE;
+3
View File
@@ -68,6 +68,9 @@ namespace MobileGL::MG_Impl::EGLImpl {
// a split client's does, a monolith's does not. Decides the dma-buf EGL extensions, the
// Wayland linux-dmabuf presentation and GL_OES_EGL_image.
Bool SharedImagesAvailable();
// Whether the server also holds YUV (NV12/P010) images: decides the YUV dma-buf formats and
// GL_OES_EGL_image_external. One round trip the first time it is asked, cached after.
Bool SharedImageYuvAvailable();
// Under a dispatching libEGL the dispatcher, not this library, owns eglBindAPI's state: it does
// not forward a bind of the API it already reports (EGL_OPENGL_ES_API, EGL's default), so a
// context would otherwise be created for whatever this library assumed. The glvnd vendor
+44 -16
View File
@@ -434,6 +434,7 @@ namespace MobileGL::MG_Impl::GLImpl {
case GL_TEXTURE_BINDING_CUBE_MAP: outTarget = TextureTarget::TextureCubeMap; return true;
case GL_TEXTURE_BINDING_CUBE_MAP_ARRAY: outTarget = TextureTarget::TextureCubeMapArray; return true;
case GL_TEXTURE_BINDING_RECTANGLE: outTarget = TextureTarget::TextureRectangle; return true;
case GL_TEXTURE_BINDING_EXTERNAL_OES: outTarget = TextureTarget::External; return true;
default: return false;
}
}
@@ -606,23 +607,50 @@ namespace MobileGL::MG_Impl::GLImpl {
Bool EsIdentityActive();
const Vector<const char*>& ClientSideExtensions() {
static const Vector<const char*> withImages = {"GL_OES_EGL_image", "GL_KHR_robustness",
"GL_ARB_robustness", "GL_EXT_robustness"};
static const Vector<const char*> withoutImages = {"GL_KHR_robustness", "GL_ARB_robustness",
"GL_EXT_robustness"};
// Under the ES identity (EsIdentityActive) the ES names of what desktop GL has in core:
// BGRA textures, render targets and read-backs. GL_BGRA_EXT / GL_BGRA8_EXT as internal
// formats are taken as RGBA storage (TextureEnumConverter), the byte order being the
// pixel transfer format's, as on desktop GL.
static const Vector<const char*> esWithImages = {
"GL_OES_EGL_image", "GL_KHR_robustness", "GL_ARB_robustness",
"GL_EXT_robustness", "GL_EXT_texture_format_BGRA8888", "GL_EXT_read_format_bgra"};
static const Vector<const char*> esWithoutImages = {"GL_KHR_robustness", "GL_ARB_robustness",
"GL_EXT_robustness", "GL_EXT_texture_format_BGRA8888",
"GL_EXT_read_format_bgra"};
// GL_OES_EGL_image_external(_essl3) only where the server samples YUV images: an
// external texture is a 2D texture holding the image converted to RGBA, which needs
// the server to hold the YUV image in the first place (EGLImpl::SharedImageYuvAvailable).
static const auto build = [](Bool images, Bool external, Bool es, Bool esApi) {
Vector<const char*> list;
if (images) list.push_back("GL_OES_EGL_image");
if (images && external) {
list.push_back("GL_OES_EGL_image_external");
list.push_back("GL_OES_EGL_image_external_essl3");
}
for (const char* name : {"GL_KHR_robustness", "GL_ARB_robustness", "GL_EXT_robustness"})
list.push_back(name);
// Under the ES identity (EsIdentityActive) the ES names of what desktop GL has in core:
// BGRA textures, render targets and read-backs. GL_BGRA_EXT / GL_BGRA8_EXT as internal
// formats are taken as RGBA storage (TextureEnumConverter), the byte order being the
// pixel transfer format's, as on desktop GL.
if (es) {
list.push_back("GL_EXT_texture_format_BGRA8888");
list.push_back("GL_EXT_read_format_bgra");
}
// Any context created for the OpenGL ES API, whatever it calls itself: the ES 2.0
// names of what desktop GL has in core - RGB8/RGBA8 and DEPTH_COMPONENT24
// renderbuffers, and glMapBuffer (exported under its OES names as well).
if (esApi) {
list.push_back("GL_OES_rgb8_rgba8");
list.push_back("GL_OES_depth24");
list.push_back("GL_OES_mapbuffer");
}
return list;
};
static const Vector<const char*> lists[16] = {
build(false, false, false, false), build(false, false, false, true),
build(false, false, true, false), build(false, false, true, true),
build(false, true, false, false), build(false, true, false, true),
build(false, true, true, false), build(false, true, true, true),
build(true, false, false, false), build(true, false, false, true),
build(true, false, true, false), build(true, false, true, true),
build(true, true, false, false), build(true, true, false, true),
build(true, true, true, false), build(true, true, true, true)};
const Bool images = MG_Impl::EGLImpl::SharedImagesAvailable();
if (EsIdentityActive()) return images ? esWithImages : esWithoutImages;
return images ? withImages : withoutImages;
const Bool external = images && MG_Impl::EGLImpl::SharedImageYuvAvailable();
const Bool esApi = MG_State::pEGLContext &&
MG_State::pEGLContext->GetCurrentContextClientAPI() == EGL_OPENGL_ES_API;
return lists[(images ? 8 : 0) + (external ? 4 : 0) + (EsIdentityActive() ? 2 : 0) + (esApi ? 1 : 0)];
}
// Robust buffer access as GL 4.5 / KHR_robust_buffer_access_behavior define it: the backend
+64 -11
View File
@@ -1194,6 +1194,25 @@ namespace MobileGL::MG_Impl::GLImpl {
return false;
}
// GL_OES_EGL_image_external: base level 0, CLAMP_TO_EDGE only, NEAREST or LINEAR only.
if (target == TextureTarget::External) {
if (pname == GL_TEXTURE_BASE_LEVEL && param != 0) {
MG_State::pGLContext->RecordError(
ErrorCode::InvalidOperation,
MakeUnique<GenericErrorInfo>("MG_Impl/GLImpl", caller, "External texture base level must be zero."));
return false;
}
if (((pname == GL_TEXTURE_WRAP_S || pname == GL_TEXTURE_WRAP_T || pname == GL_TEXTURE_WRAP_R) &&
param != GL_CLAMP_TO_EDGE) ||
(pname == GL_TEXTURE_MIN_FILTER && param != GL_NEAREST && param != GL_LINEAR)) {
MG_State::pGLContext->RecordError(
ErrorCode::InvalidEnum,
MakeUnique<GenericErrorInfo>("MG_Impl/GLImpl", caller,
"External textures clamp to edge and filter without mipmaps."));
return false;
}
}
if (target == TextureTarget::TextureRectangle && pname == GL_TEXTURE_BASE_LEVEL && param != 0) {
MG_State::pGLContext->RecordError(
ErrorCode::InvalidOperation,
@@ -1635,6 +1654,7 @@ namespace MobileGL::MG_Impl::GLImpl {
case GL_TEXTURE_CUBE_MAP_ARRAY:
case GL_TEXTURE_2D_MULTISAMPLE:
case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
case GL_TEXTURE_EXTERNAL_OES:
return true;
default:
return false;
@@ -3145,6 +3165,17 @@ namespace MobileGL::MG_Impl::GLImpl {
if (!TextureImpl::ValidateTextureObject(textureObject)) return false;
switch (pname) {
// GL_OES_EGL_image_external: one unit per external texture (the image arrives as RGBA).
case GL_REQUIRED_TEXTURE_IMAGE_UNITS_OES:
if (textureTarget != TextureTarget::External) {
MG_State::pGLContext->RecordError(
ErrorCode::InvalidEnum,
MakeUnique<GenericErrorInfo>("MG_Impl/GLImpl", __func__,
"GL_REQUIRED_TEXTURE_IMAGE_UNITS_OES is an external texture's."));
return false;
}
if (params) *params = 1;
break;
case GL_TEXTURE_MAG_FILTER:
if (params) {
*params = (GLint)MG_Util::ConvertSamplerFilterModeToGLEnum(
@@ -6805,6 +6836,13 @@ namespace MobileGL::MG_Impl::GLImpl {
const char* caller) {
if (!textureObject) return false;
const auto target = textureObject->GetTarget();
// GL_OES_EGL_image_external: an external texture has no mip chain to generate.
if (target == TextureTarget::External) {
MG_State::pGLContext->RecordError(
ErrorCode::InvalidEnum,
MakeUnique<GenericErrorInfo>("MG_Impl/GLImpl", caller, "External textures have no mipmaps."));
return false;
}
if ((target == TextureTarget::TextureCubeMap || target == TextureTarget::TextureCubeMapArray) &&
!textureObject->IsComplete()) {
MG_State::pGLContext->RecordError(
@@ -7138,15 +7176,20 @@ namespace MobileGL::MG_Impl::GLImpl {
// GL_OES_EGL_image. The only EGLImages with storage behind them are the server's shared images
// (EGL_LINUX_DMA_BUF_EXT): level 0 of the bound texture is defined at the image's size through
// glTexImage2D's own path - so the respecification is recorded and announced as for any other
// - and the backend then swaps that storage for the image. The frontend has no external-image
// target, so GL_TEXTURE_2D is the only one taken.
// - and the backend then swaps that storage for the image.
//
// GL_OES_EGL_image_external's target takes it the same way: an external texture is a 2D one
// below the frontend. A YUV image (NV12/P010) is taken by either target; the backend converts it
// into the texture's RGBA level at the first use in each frame (SharedImageYuv.h).
void EGLImageTargetTexture2DOES(GLenum target, GLeglImageOES image) {
if (target != GL_TEXTURE_2D) {
if (target != GL_TEXTURE_2D && target != GL_TEXTURE_EXTERNAL_OES) {
MG_State::pGLContext->RecordError(
ErrorCode::InvalidEnum,
MakeUnique<GenericErrorInfo>("MG_Impl/GLImpl", __func__, "target must be GL_TEXTURE_2D."));
MakeUnique<GenericErrorInfo>("MG_Impl/GLImpl", __func__,
"target must be GL_TEXTURE_2D or GL_TEXTURE_EXTERNAL_OES."));
return;
}
const Bool external = target == GL_TEXTURE_EXTERNAL_OES;
Uint64 sharedImageId = 0;
EGLint width = 0;
EGLint height = 0;
@@ -7158,17 +7201,27 @@ namespace MobileGL::MG_Impl::GLImpl {
}
auto& activeUnit = MG_State::pGLContext->GetTextureUnitObject(MG_State::pGLContext->GetActiveTextureUnit());
const SharedPtr<MG_State::GLState::ITextureObject> textureObject =
activeUnit.GetBindingSlot(TextureTarget::Texture2D).GetBoundObject();
activeUnit.GetBindingSlot(external ? TextureTarget::External : TextureTarget::Texture2D).GetBoundObject();
if (!TextureImpl::ValidateTextureObject(textureObject)) return;
if (!ValidateTextureMutable(textureObject, __func__)) return;
if (!TextureImpl::ValidateTextureSizeRange(width, height, 1)) return;
// NULL is an offset into a bound unpack buffer; the definition must read nothing.
GLint unpackBuffer = 0;
GetIntegerv(GL_PIXEL_UNPACK_BUFFER_BINDING, &unpackBuffer);
if (unpackBuffer != 0) BindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
TexImage2D_State(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
if (unpackBuffer != 0) BindBuffer(GL_PIXEL_UNPACK_BUFFER, static_cast<GLuint>(unpackBuffer));
if (external) {
// glTexImage2D does not take the external target, so its level 0 is defined here the way
// that call defines a 2D one without data: an RGBA8 base, any older chain dropped.
textureObject->SetInternalFormat(TextureInternalFormat::RGBA8);
auto* mipmapObject = static_cast<MG_State::GLState::TextureObjectMipmap*>(textureObject.get());
DiscardMipmapChainOnBaseRespecification(mipmapObject, TextureUploadTarget::Texture2D, 0);
mipmapObject->AllocateStorage(TextureUploadTarget::Texture2D, 0,
{{width, height, 1}, static_cast<SizeT>(width) * height * 4});
} else {
// NULL is an offset into a bound unpack buffer; the definition must read nothing.
GLint unpackBuffer = 0;
GetIntegerv(GL_PIXEL_UNPACK_BUFFER_BINDING, &unpackBuffer);
if (unpackBuffer != 0) BindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
TexImage2D_State(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
if (unpackBuffer != 0) BindBuffer(GL_PIXEL_UNPACK_BUFFER, static_cast<GLuint>(unpackBuffer));
}
auto* backendObject = MG_Backend::pActiveBackendObject.get();
if (backendObject == nullptr ||
+4
View File
@@ -246,6 +246,10 @@ namespace MobileGL::MG_Impl {
GETPROC(glBufferSubData, name);
GETPROC(glGetBufferSubData, name);
GETPROC(glMapBuffer, name);
// GL_OES_mapbuffer (ES contexts): the same three calls under their OES names.
if (strcmp(name, "glMapBufferOES") == 0) return (void*)glMapBuffer;
if (strcmp(name, "glUnmapBufferOES") == 0) return (void*)glUnmapBuffer;
if (strcmp(name, "glGetBufferPointervOES") == 0) return (void*)glGetBufferPointerv;
GETPROC(glUnmapBuffer, name);
GETPROC(glGetBufferParameteriv, name);
GETPROC(glGetBufferPointerv, name);
+3 -1
View File
@@ -234,7 +234,9 @@ namespace MobileGL::MG_Pipe {
m_vertices.push_back(static_cast<Uint64>(vertex));
}
}
std::sort(m_vertices.begin(), m_vertices.end());
// An array draw's single range arrives ascending already; sorting it again was most of
// the client's time on a client-array draw.
if (!std::is_sorted(m_vertices.begin(), m_vertices.end())) std::sort(m_vertices.begin(), m_vertices.end());
m_vertices.erase(std::unique(m_vertices.begin(), m_vertices.end()), m_vertices.end());
return true;
}
+1 -1
View File
@@ -132,7 +132,7 @@ namespace MobileGL::MG_Pipe {
// TextureUploadTarget -> TextureTarget inverse exists anywhere in the tree, so
// without this the inline InternalFormat cannot make them fall out at push time and
// the backend keeps reading the frontend attachment objects.
surface.TextureTarget = static_cast<Uint16>(texture->GetTarget());
surface.TextureTarget = static_cast<Uint16>(MGPipeWireTextureTarget(texture->GetTarget()));
return surface;
}
const auto& renderbuffer = attachment.GetRenderbuffer();
+20 -4
View File
@@ -34,6 +34,12 @@ namespace MobileGL::MG_Pipe {
MGPipeVertexInputEmitterInstance().EmitVertexBuffers(m_context, m_baseInstance);
if (m_indexBindingChanged)
MGPipeVertexInputEmitterInstance().EmitIndexBuffer(m_context);
// The bindings no longer name them: the buffers go back to the share group for the
// next client-array draw. Their next respecify orphans the store this draw reads.
auto& group = *m_context.GetShareGroup();
for (auto& buffer : m_vertexBuffers)
if (buffer) group.ReturnClientArrayBuffer(std::move(buffer));
if (m_indexBuffer) group.ReturnClientArrayBuffer(std::move(m_indexBuffer));
}
Bool Prepare(MGPDrawInfo& info, const MGPDrawRange* ranges, Uint32 rangeCount,
@@ -46,7 +52,8 @@ namespace MobileGL::MG_Pipe {
// Start remains an element offset, so base vertex and draw IDs are unchanged.
if (clientIndexBytes != 0) {
if (!clientIndices || clientIndexBytes > std::numeric_limits<Uint32>::max()) return false;
m_indexBuffer = MakeOwnedBuffer(clientIndices, static_cast<SizeT>(clientIndexBytes), BufferTarget::Index);
m_indexBuffer = MakeOwnedBuffer(m_context, clientIndices, static_cast<SizeT>(clientIndexBytes),
BufferTarget::Index);
info.IndexResource = MGPipeResourceTrackerInstance().Find(*m_indexBuffer);
info.Flags &= ~static_cast<Uint8>(kDrawHasUserIndices);
MGPIndexBuffer binding{};
@@ -115,8 +122,14 @@ namespace MobileGL::MG_Pipe {
if (attribute.Offset == 0 || byteCount > std::numeric_limits<SizeT>::max() - attribute.Offset)
return false;
const auto* source = reinterpret_cast<const Uint8*>(attribute.Offset);
const Uint64 first = referenced.front();
if (stride == 0) {
std::memcpy(bytes.data(), source, elementBytes);
} else if (last - first + 1 == referenced.size()) {
// Contiguous elements (every array draw, most indexed ones): the vertex
// records from the first fetched one to the end of the last, in one copy.
const SizeT begin = static_cast<SizeT>(first * stride);
std::memcpy(bytes.data() + begin, source + begin, byteCount - begin);
} else {
// Only dereference fetched elements: sparse indices and first>0
// do not grant permission to read intervening application memory.
@@ -126,7 +139,7 @@ namespace MobileGL::MG_Pipe {
}
}
}
m_vertexBuffers[location] = MakeOwnedBuffer(bytes.data(), bytes.size(), BufferTarget::Vertex);
m_vertexBuffers[location] = MakeOwnedBuffer(m_context, bytes.data(), bytes.size(), BufferTarget::Vertex);
handles[location] = MGPipeResourceTrackerInstance().Find(*m_vertexBuffers[location]);
}
MGPipeVertexInputEmitterInstance().EmitVertexBuffers(m_context, info.StartInstance, &handles);
@@ -173,8 +186,11 @@ namespace MobileGL::MG_Pipe {
return attribute.Size > 0 && attribute.Size <= 4 ? component * attribute.Size : 0;
}
static UniquePtr<Buffer> MakeOwnedBuffer(const void* bytes, SizeT size, BufferTarget target) {
auto buffer = MakeUnique<Buffer>(0);
static UniquePtr<Buffer> MakeOwnedBuffer(Context& context, const void* bytes, SizeT size, BufferTarget target) {
// A kept buffer is already a published resource: respecifying it is fire-and-forget,
// where a new one costs a blocking create (and a destroy after the draw).
auto buffer = context.GetShareGroup()->TakeClientArrayBuffer();
if (!buffer) buffer = MakeUnique<Buffer>(0);
const auto handle = MGPipeResourceTrackerInstance().Find(*buffer);
MGPipeResourceTrackerInstance().NoteBoundAs(handle, target);
buffer->SetUsage(BufferUsage::StreamDraw);
+28 -107
View File
@@ -70,6 +70,11 @@ namespace MobileGL::MG_Pipe {
// The one door into PipeInputs' storage on the client side. A struct rather than a
// list of friend functions so the header names exactly one friend.
struct MGPipeFillAccess {
static void CopyServerPackReference(PipeInputs& dst, const PipeInputs& actual,
const PixelStoreParameters& pack) {
dst.m_pixelStore[0] = pack;
dst.m_pixelStore[1] = actual.m_pixelStore[1];
}
// Copies ONE field's storage out of the live context by calling the GLContext
// accessor of the same name (P1 brief D4: no derivation logic is re-implemented
// here, which is what keeps the copy semantically identical by construction).
@@ -659,56 +664,6 @@ namespace MobileGL::MG_Pipe {
if (!MGPipeVerifyInputs(inputs, g_snapshot, mask, &differing)) ReportDivergence(differing, "entry");
}
#if MOBILEGL_BUILD_DISAGGREGATED
// ---- P7 wave 3 (V1): the compare-at-read oracle inside the server's read_pixels ----
//
// WHAT THE SPLIT ARM FOUND THE FIRST TIME VERIFY AND SPLIT SHARED A BUILD. Every
// glReadPixels on the inproc arm - 8530 reads over 220 of the lane's 1068 entries, and the
// VerifySplitArming. entries on both backends - aborted with
//
// MGPipe: verify read of GetPixelStoreParameters (index 0, 0) differs from the live context
// MGPipe: Fatal{PipeVerifyDiffer, "GetPixelStoreParameters@ReadPixels", verb=6, where=read}
//
// on the server's apply thread, and it was the ONLY field that diverged anywhere in the
// lane. The mechanism is ID-49 and is deliberate: the pack state never shapes the wire
// answer, so MG_Remote/Server/PipeApplier.cpp's read_pixels body writes a NEUTRAL pack
// into the applier's copy of the field (MGPipeApplySetPixelPackState), makes the backend
// read, and restores the pushed one; the client then scatters the tight reply through the
// application's own pack state. The frontend context - this comparator's oracle - still
// holds the application's pack (Alignment 4 by default), so the backend's read of the
// pack half answers {Alignment 1} where the live context says {Alignment 4}. On the
// monolith arm the same neutral window is opened on the frontend context itself
// (GL_Texture.cpp's ScopedNeutralPackState), which is why the monolith lane never saw it.
//
// SO THE ORACLE, NOT THE RULE, IS WHAT CHANGES, AND ONLY IN THAT WINDOW: under a server
// stamp, on the ReadPixels verb, the pack half's expected value is the neutral pack the
// applier reads with, and the UNPACK half and every other field keep the live context as
// their oracle. A server read of the pack half that is neither the application's value
// nor exactly the neutral one is still a divergence and still Fatal.
//
// WHAT THIS GIVES UP IS ONE VALUE, NOT THE FIELD (corrected in P7 wave 3's V1 fix round;
// the first statement of it here said the comparator could no longer see a client that
// pushed a WRONG pack state at all, which is too broad). PipeApplier.cpp's read_pixels
// reads `savedPack` THROUGH THE ACCESSOR - and therefore through this hook - BEFORE it
// installs the neutral value, so the pushed pack has already been compared against the
// live one by then: a correct push is equal and returns, and a wrong push that is not
// exactly the neutral pack fails both compares and is still Fatal. The blind spot is the
// single value {SwapBytes 0, LSBFirst 0, RowLength 0, ImageHeight 0, Skip* 0, Alignment 1},
// and only while the application's own pack differs from it. The client-side scatter that
// consumes the real pack is covered by the readback matrix cases, which compare bytes,
// not fields.
Bool ServerReadsInsideTheNeutralPackWindow(const PipeInputs& self, MGPipeInputField field) {
return field == MGPipeInputField::GetPixelStoreParameters && self.ServerStampedVerb() &&
self.CurrentVerb() == MGPipeVerb::ReadPixels;
}
// The constant itself is MG_Pipe's (MGPipeTypes.h's MGPipeNeutralReadPixelsPack), which is
// the same function MG_Remote/Server/PipeApplier.cpp's read_pixels installs. It used to be
// re-typed here, field for field, to avoid reaching into MG_Remote/Server from MG_Impl -
// but the owner of MGPPixelPackState is MG_Pipe, both sites already include it, and a
// drift between the two spellings would have cost a false divergence rather than a build
// break.
#endif // MOBILEGL_BUILD_DISAGGREGATED
#endif // MOBILEGL_PIPE_VERIFY
} // namespace
@@ -726,9 +681,28 @@ namespace MobileGL::MG_Pipe {
}
void MGPipeVerifyReadHook(const PipeInputs& self, MGPipeInputField field, Uint index0, Uint index1) {
if (&self != &gPipeInputs || !g_verify.Enabled || g_verify.InHook) return;
if (&self != &gPipeInputs || !g_verify.Enabled) return;
const auto index = static_cast<SizeT>(field);
if (kMGPipeInputFieldSticky[index]) return;
#if MOBILEGL_BUILD_DISAGGREGATED
if (self.ServerStampedVerb() && field == MGPipeInputField::GetPixelStoreParameters) {
// Compare the backend's pack field against the last payload applied to this
// context, including the neutral-pack override during readback. Never obtain
// the reference by reading the field being checked, or consult the client's
// GLContext from the server thread. Unpack has no server-side reader.
static thread_local PipeInputs reference;
MGPipeFillAccess::CopyServerPackReference(reference, self, MGPipeApplier().VerifyPixelPack);
if (g_verify.Corrupt && *g_verify.Corrupt == field) {
MGPipeApplyVerifyCorruption(reference, field);
}
if (!MGPipeInputsFieldEqual(field, self, reference)) {
MGLOG_E("MGPipe: verify read of pixel pack differs from the applied pack payload");
ReportDivergence(field, "read");
}
return;
}
#endif
if (g_verify.InHook) return;
auto* ctx = LiveContext();
if (ctx == nullptr) return;
// The whole field is re-read and compared - a superset of "the same indices", so a
@@ -743,68 +717,13 @@ namespace MobileGL::MG_Pipe {
// never recurses (and never reports the inner read against a half-copied scratch).
g_verify.InHook = true;
MGPipeFillAccess::CopyField(g_readScratch, *ctx, field);
// P7 wave 3 (V1): NEGATIVE CONTROL A REACHES THIS ARM TOO. Until this line the knob only
// ever perturbed EntryCompare's snapshot, so every red it could produce said `where=entry`
// and was produced on the CLIENT thread - which left the compare-at-read hook, the arm
// that runs on the server's apply thread and does the whole of the split lane's per-read
// work, with no falsifier at all: a hook that had stopped comparing would have looked
// exactly like a hook with nothing to report. The perturbation goes on the ORACLE, for
// EntryCompare's reason: the arm under test is the stored value, so corrupting THAT would
// be testing the corruption.
//
// WHICH READ THIS BLOCK TURNS RED (V1 fix round 2). The server reads the pack field TWICE
// inside the ReadPixels verb window, and both reads sit inside
// ServerReadsInsideTheNeutralPackWindow below (the verb stamp is up for the whole apply):
// PipeApplier::read_pixels saves the application's pack through the accessor BEFORE it
// installs the neutral one, and the backend's ReadPixels reads the field AFTER. This block
// is what turns the FIRST of them red: at that read `self` still holds the application's
// pack, so the first compare only reaches the window if this perturbation made it differ,
// and inside the window the application's pack against the neutral oracle differs on its
// own. It does nothing for the second read - `self` IS the neutral pack there - which is
// what the re-application inside the window is for.
// Corrupt the reference, not the backend field, to test the comparison itself.
if (g_verify.Corrupt && *g_verify.Corrupt == field) {
MGPipeApplyVerifyCorruption(g_readScratch, field);
}
const Bool equal = MGPipeInputsFieldEqual(field, self, g_readScratch);
g_verify.InHook = false;
if (equal) return;
#if MOBILEGL_BUILD_DISAGGREGATED
if (ServerReadsInsideTheNeutralPackWindow(self, field)) {
// The live context is the application's pack state and the server is, by ID-49,
// reading with the neutral one; the oracle for THAT HALF is the neutral pack, and
// every other byte of the field still has to match the live context.
g_verify.InHook = true;
PipeInputs::VisitStorage(field, g_readScratch, g_readScratch, [](auto& live, auto&) {
if constexpr (std::is_same_v<std::remove_reference_t<decltype(live)>, PixelStoreParameters[2]>) {
live[0] = MGPipeNeutralReadPixelsPack().Pack;
}
return true;
});
// AND THE CONTROL IS RE-APPLIED, because the overwrite above replaces the pack half
// WHOLESALE - the corruption included, since CorruptStorage perturbs an array's FIRST
// ELEMENT and the pack half is element 0. This is the block for the POST-INSTALL read
// (the backend's ReadPixels, after PipeApplier::read_pixels installed the neutral pack
// into gPipeInputs): there `self` is the neutral pack, the overwrite just made the
// oracle equal to it, and only a perturbation applied AFTER the overwrite can make
// that compare differ - the block above cannot reach it. Without this line the one
// window in which this arm is most load-bearing (the server's read_pixels, 8530 reads
// over 220 of the lane's entries) would be the one window MOBILEGL_PIPE_VERIFY_CORRUPT=
// GetPixelStoreParameters cannot turn red - and a control that accepted ONE report
// would never notice, because the saved-pack read still reports through the block
// above. The VerifySplitReadCorrupted. entries and the CI step therefore require at
// least TWO `where=read` reports in the server half (V1 fix round 2). Measured: 3 on
// DirectGLES (its backend reads the field twice after the install) / 2 on
// DirectVulkan; without this block both drop to 1, without the block above
// DirectVulkan drops to 1 while DirectGLES's two post-install reports keep it at 2 -
// so this block is falsified on both backends and the one above on DirectVulkan.
if (g_verify.Corrupt && *g_verify.Corrupt == field) {
MGPipeApplyVerifyCorruption(g_readScratch, field);
}
const Bool equalToTheNeutralPack = MGPipeInputsFieldEqual(field, self, g_readScratch);
g_verify.InHook = false;
if (equalToTheNeutralPack) return;
}
#endif
MGLOG_E("MGPipe: verify read of %s (index %u, %u) differs from the live context", kMGPipeInputFieldNames[index],
index0, index1);
ReportDivergence(field, "read");
@@ -3988,6 +3907,7 @@ namespace MobileGL::MG_Pipe {
}
void MGPipeClientDrainTextureUploads() {
#if MOBILEGL_BUILD_DISAGGREGATED
if (MG_Config::Transport == MG_Config::TransportMode::Monolith) return;
if (MG_Remote::Client::RunsAsTheServerRole()) return;
auto* ctx = LiveContext();
@@ -3999,6 +3919,7 @@ namespace MobileGL::MG_Pipe {
P4aFamilyDependenciesAreSet(kMGPipeSubsystemTextureResources, pushMask)) {
(void)MGPipeTextureEmitterInstance().DrainTextureSubData(*ctx);
}
#endif
}
#if MOBILEGL_BUILD_DISAGGREGATED
+4 -1
View File
@@ -629,6 +629,9 @@ namespace MobileGL::MG_Pipe {
// sample this unit".
inline MobileGL::TextureTarget MGPipeSamplerUniformTextureTarget(GLenum uniformType) {
switch (uniformType) {
// GL_OES_EGL_image_external: the unit's external binding, whose texture is a 2D one below.
case GL_SAMPLER_EXTERNAL_OES:
return TextureTarget::External;
case GL_SAMPLER_1D:
case GL_INT_SAMPLER_1D:
case GL_UNSIGNED_INT_SAMPLER_1D:
@@ -995,7 +998,7 @@ namespace MobileGL::MG_Pipe {
// and not the storage owner's, which is the whole point of the call; for an
// ordinary texture it is simply its format.
m_lastView.InternalFormat = static_cast<Uint32>(texture.GetFormat());
m_lastView.Target = static_cast<Uint8>(texture.GetTarget());
m_lastView.Target = static_cast<Uint8>(MGPipeWireTextureTarget(texture.GetTarget()));
// THE FOUR RESTRICTIONS COME FROM ONE PLACE. TextureObjectBase leaves all four at
// 0 for an ordinary texture and glTextureView writes them for a view, and a view
// always has NumLevels >= 1 - so a zero here unambiguously means "no restriction,
+50 -7
View File
@@ -15,6 +15,7 @@
#include <Defines.h>
#include <Init.h>
#include <MG_Backend/BackendObjects.h>
#include <MG_Impl/EGLImpl/EGLImpl.h>
#include <algorithm>
#include <cerrno>
@@ -26,14 +27,19 @@
namespace {
using MobileGL::MG_Backend::BackendObject;
constexpr uint32_t kFormats[] = {MOBILEGL_SHARED_IMAGE_FOURCC_ABGR8888, MOBILEGL_SHARED_IMAGE_FOURCC_XBGR8888,
MOBILEGL_SHARED_IMAGE_FOURCC_ARGB8888, MOBILEGL_SHARED_IMAGE_FOURCC_XRGB8888};
constexpr uint32_t kRgbFormats[] = {MOBILEGL_SHARED_IMAGE_FOURCC_ABGR8888, MOBILEGL_SHARED_IMAGE_FOURCC_XBGR8888,
MOBILEGL_SHARED_IMAGE_FOURCC_ARGB8888, MOBILEGL_SHARED_IMAGE_FOURCC_XRGB8888};
constexpr uint32_t kYuvFormats[] = {MOBILEGL_SHARED_IMAGE_FOURCC_NV12, MOBILEGL_SHARED_IMAGE_FOURCC_P010};
bool FourccIsYuv(uint32_t fourcc) {
return fourcc == MOBILEGL_SHARED_IMAGE_FOURCC_NV12 || fourcc == MOBILEGL_SHARED_IMAGE_FOURCC_P010;
}
bool FourccSupported(uint32_t fourcc) {
for (uint32_t format : kFormats) {
for (uint32_t format : kRgbFormats) {
if (format == fourcc) return true;
}
return false;
return FourccIsYuv(fourcc);
}
// Serializes the calls against each other (not against GL calls: see the header).
@@ -54,10 +60,20 @@ MOBILEGL_EXPORT uint32_t mobilegl_shared_image_abi_version(void) {
return MOBILEGL_SHARED_IMAGE_ABI_VERSION;
}
// The YUV formats only when the server holds them, which takes the session (and one round trip,
// once): a caller asking which formats exist is about to allocate one.
MOBILEGL_EXPORT int mobilegl_shared_image_formats(uint32_t* formats, uint32_t capacity) {
constexpr uint32_t total = sizeof(kFormats) / sizeof(kFormats[0]);
if (formats == nullptr && capacity != 0) return -EINVAL;
for (uint32_t i = 0; i < total && i < capacity; ++i) formats[i] = kFormats[i];
const std::lock_guard<std::mutex> lock(g_apiMutex);
uint32_t total = 0;
const auto put = [&](uint32_t format) {
if (total < capacity) formats[total] = format;
++total;
};
for (uint32_t format : kRgbFormats) put(format);
if (ActiveBackend(true) != nullptr && MobileGL::MG_Impl::EGLImpl::SharedImageYuvAvailable()) {
for (uint32_t format : kYuvFormats) put(format);
}
return static_cast<int>(total);
}
@@ -83,6 +99,8 @@ MOBILEGL_EXPORT int mobilegl_shared_image_allocate(uint32_t width, uint32_t heig
answer.stride = image.Stride;
answer.offset = image.Offset;
answer.modifier = image.Modifier;
answer.plane1_stride = image.Plane1Stride;
answer.plane1_offset = image.Plane1Offset;
// An older caller's struct is a prefix of this one; a newer caller's tail stays as it was.
std::memcpy(out, &answer, std::min<size_t>(out->struct_size, sizeof(answer)));
return 0;
@@ -90,7 +108,8 @@ MOBILEGL_EXPORT int mobilegl_shared_image_allocate(uint32_t width, uint32_t heig
MOBILEGL_EXPORT int mobilegl_shared_image_import(int fd, uint32_t width, uint32_t height, uint32_t fourcc,
uint64_t* out_id) {
if (fd < 0 || out_id == nullptr || width == 0 || height == 0 || !FourccSupported(fourcc)) return -EINVAL;
if (fd < 0 || out_id == nullptr || width == 0 || height == 0 || !FourccSupported(fourcc) || FourccIsYuv(fourcc))
return -EINVAL;
const std::lock_guard<std::mutex> lock(g_apiMutex);
BackendObject* backend = ActiveBackend(true);
if (backend == nullptr) return -EIO;
@@ -100,6 +119,30 @@ MOBILEGL_EXPORT int mobilegl_shared_image_import(int fd, uint32_t width, uint32_
return 0;
}
MOBILEGL_EXPORT int mobilegl_shared_image_import_planes(int fd, uint32_t width, uint32_t height, uint32_t fourcc,
const struct mobilegl_shared_image_planes* planes,
uint64_t* out_id) {
if (fd < 0 || out_id == nullptr || planes == nullptr || width == 0 || height == 0 || !FourccIsYuv(fourcc) ||
planes->struct_size < sizeof(mobilegl_shared_image_planes) || planes->plane_count != 2)
return -EINVAL;
const std::lock_guard<std::mutex> lock(g_apiMutex);
BackendObject* backend = ActiveBackend(true);
if (backend == nullptr) return -EIO;
MobileGL::MG_Backend::SharedImageImportLayout layout;
layout.PlaneCount = 2;
for (int i = 0; i < 2; ++i) {
layout.Offset[i] = planes->offset[i];
layout.Pitch[i] = planes->pitch[i];
}
constexpr uint64_t kModifierInvalid = 0x00ffffffffffffffull;
layout.HasModifier = planes->modifier != kModifierInvalid;
layout.Modifier = planes->modifier;
uint64_t id = 0;
if (!backend->ImportSharedImagePlanes(fd, width, height, fourcc, layout, &id)) return -EIO;
*out_id = id;
return 0;
}
MOBILEGL_EXPORT int mobilegl_server_available(void) {
return MobileGL::ImplementationAvailable() ? 1 : 0;
}
+28 -1
View File
@@ -36,7 +36,9 @@
extern "C" {
#endif
#define MOBILEGL_SHARED_IMAGE_ABI_VERSION 1u
/* 2: YUV images (NV12, P010), the plane-1 fields of mobilegl_shared_image, and
* mobilegl_shared_image_import_planes. */
#define MOBILEGL_SHARED_IMAGE_ABI_VERSION 2u
/* DRM fourccs (little-endian packed). ABGR8888 is R, G, B, A in memory - GL's RGBA8. */
#define MOBILEGL_SHARED_IMAGE_FOURCC_ABGR8888 0x34324241u /* 'AB24' */
@@ -46,6 +48,12 @@ extern "C" {
* writes them, so the name an image is allocated under never changes its colours. */
#define MOBILEGL_SHARED_IMAGE_FOURCC_ARGB8888 0x34325241u /* 'AR24' */
#define MOBILEGL_SHARED_IMAGE_FOURCC_XRGB8888 0x34325258u /* 'XR24' */
/* Two-plane YUV 4:2:0 (ABI 2), when the server's platform allocates it (listed by
* mobilegl_shared_image_formats only then): plane 0 is Y, plane 1 Cb Cr interleaved, both in the one
* descriptor at the offsets and pitches the allocation reports. They are SAMPLED only - an EGLImage
* of one binds through GL_OES_EGL_image_external - and their layout is still the allocator's. */
#define MOBILEGL_SHARED_IMAGE_FOURCC_NV12 0x3231564Eu /* 'NV12' */
#define MOBILEGL_SHARED_IMAGE_FOURCC_P010 0x30313050u /* 'P010' */
/* One allocated image. VERSIONED BY SIZE: the caller sets struct_size to the sizeof it was built
* against and the library writes no byte past it, so fields are only ever appended. */
@@ -60,6 +68,18 @@ struct mobilegl_shared_image {
uint32_t offset; /* plane 0, bytes */
uint32_t reserved; /* zero */
uint64_t modifier; /* DRM format modifier of the layout (DRM_FORMAT_MOD_INVALID today) */
/* ABI 2: a YUV image's plane 1 (same fd); zero for the one-plane formats. */
uint32_t plane1_stride;
uint32_t plane1_offset;
};
/* Where an imported two-plane buffer's planes are (ABI 2). */
struct mobilegl_shared_image_planes {
uint32_t struct_size;
uint32_t plane_count; /* 2 */
uint32_t offset[2];
uint32_t pitch[2];
uint64_t modifier; /* DRM_FORMAT_MOD_INVALID when none was given */
};
/* MOBILEGL_SHARED_IMAGE_ABI_VERSION of the library. */
@@ -78,6 +98,11 @@ int mobilegl_shared_image_allocate(uint32_t width, uint32_t height, uint32_t fou
* -EIO: not one of the server's images, or the size/format disagree. */
int mobilegl_shared_image_import(int fd, uint32_t width, uint32_t height, uint32_t fourcc, uint64_t* out_id);
/* The same for a YUV fourcc whose two planes are in `fd` as `planes` says (ABI 2). A YUV buffer the
* server did not allocate is taken too, read through the server's CPU-copy fallback. */
int mobilegl_shared_image_import_planes(int fd, uint32_t width, uint32_t height, uint32_t fourcc,
const struct mobilegl_shared_image_planes* planes, uint64_t* out_id);
/* Drops this process's reference (an allocation's or an import's). */
int mobilegl_shared_image_release(uint64_t id);
@@ -95,6 +120,8 @@ typedef int (*PFN_mobilegl_shared_image_formats)(uint32_t*, uint32_t);
typedef int (*PFN_mobilegl_shared_image_allocate)(uint32_t, uint32_t, uint32_t, struct mobilegl_shared_image*);
typedef int (*PFN_mobilegl_shared_image_import)(int, uint32_t, uint32_t, uint32_t, uint64_t*);
typedef int (*PFN_mobilegl_shared_image_release)(uint64_t);
typedef int (*PFN_mobilegl_shared_image_import_planes)(int, uint32_t, uint32_t, uint32_t,
const struct mobilegl_shared_image_planes*, uint64_t*);
#ifdef __cplusplus
}
@@ -374,5 +374,47 @@ void main() { o_color = vec4(0.1, 0.2, 0.8, 1.0); }
<< (std::fabs(blitted - 0.125f) < 1e-3f ? " - the destination kept its own clear" : "");
}
TEST_F(LayeredAttachmentBarrierScenario, EndingAMiddleMipDrawPreservesOtherLevels) {
if (!Ready()) return;
GLuint texture = 0;
glGenTextures(1, &texture);
m_textures.push_back(texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexStorage2D(GL_TEXTURE_2D, 3, GL_RGBA8, 16, 16);
for (int level = 0; level < 3; ++level) {
const int size = 16 >> level;
const std::vector<Rgba8> initial(size * size, LayerFill(level));
glTexSubImage2D(GL_TEXTURE_2D, level, 0, 0, size, size,
GL_RGBA, GL_UNSIGNED_BYTE, initial.data());
}
GLuint fbo = 0;
glGenFramebuffers(1, &fbo);
m_fbos.push_back(fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 1);
ASSERT_EQ(glCheckFramebufferStatus(GL_FRAMEBUFFER), GL_FRAMEBUFFER_COMPLETE);
glViewport(0, 0, 8, 8);
glDisable(GL_SCISSOR_TEST);
glDisable(GL_DEPTH_TEST);
glDisable(GL_BLEND);
glDrawBuffer(GL_COLOR_ATTACHMENT0);
DrawFullViewportQuad(m_program);
// Ending this pass reconciles both lower and upper mip layouts. Their barriers
// must not see the pass as active and recursively end it a second time.
glFinish();
for (int level = 0; level < 3; ++level) {
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, level);
glReadBuffer(GL_COLOR_ATTACHMENT0);
Rgba8 actual{};
glReadPixels(0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &actual);
const Rgba8 expected = level == 1 ? kPaintedColor : LayerFill(level);
EXPECT_NEAR(actual.r, expected.r, 2) << "level " << level;
EXPECT_NEAR(actual.g, expected.g, 2);
EXPECT_NEAR(actual.b, expected.b, 2);
EXPECT_EQ(actual.a, expected.a);
}
EXPECT_EQ(FirstGLError(), 0u);
}
} // namespace
} // namespace MGITest
@@ -322,11 +322,14 @@ TEST_F(MagmaRunAheadScenario, PresentCreditActuallyParksTheClientAtItsConfigured
Gl().EndFrame(); // credit + 1 must park before it can publish its present.
stop.store(true); observer.join(); hold.Release();
EXPECT_TRUE(sawPark.load()) << "no real producer park while the present window was full";
const auto issuedBeforeFinish = PeekSplitRuntime().emitSeq;
glFinish();
const auto after=PeekSplitRuntime();
EXPECT_EQ(after.presentCreditWaits,before.presentCreditWaits+1);
EXPECT_EQ(after.presentAckSerial,before.presentCredit+1);
EXPECT_EQ(after.appliedSeq,after.emitSeq);
// Finish waits for prior work, then asynchronously destroys its internal fence.
// That cleanup may still be queued when Finish returns.
EXPECT_GE(after.appliedSeq,issuedBeforeFinish);
RecordProperty("observed_present_credit",std::to_string(before.presentCredit));
RecordProperty("observed_producer_park",sawPark.load() ? "yes" : "no");
}
+34 -1
View File
@@ -263,6 +263,11 @@ namespace MobileGL::MG_Pipe {
return static_cast<Uint32>(MGPipeResourceTarget::TexCubeArray);
case MobileGL::TextureTarget::Texture2DMultisampleArray:
return static_cast<Uint32>(MGPipeResourceTarget::Tex2DMSArray);
// THE ONE FOLD (rather than an enumerator of its own, D-A3): an external texture
// (GL_OES_EGL_image_external) is a 2D texture to every backend - a single RGBA level 0,
// sampled through a sampler2D - and only the frontend tells the two binding points apart.
case MobileGL::TextureTarget::External:
return static_cast<Uint32>(MGPipeResourceTarget::Tex2D);
// NOT TEXTURE TARGETS. Listed rather than defaulted so the completeness assert still
// sees them, and mapped to the sentinel because no descriptor may carry either: the
// count is the enum's bound and Unknown is what an unresolved GL enum becomes.
@@ -273,6 +278,12 @@ namespace MobileGL::MG_Pipe {
return kMGPipeResourceTargetUnmapped;
}
// The TextureTarget a record that carries one raw (MGPSamplerView, MGPSurface) names: the
// frontend's own, but for the external target, which is a 2D texture below the frontend.
constexpr MobileGL::TextureTarget MGPipeWireTextureTarget(MobileGL::TextureTarget target) {
return target == MobileGL::TextureTarget::External ? MobileGL::TextureTarget::Texture2D : target;
}
constexpr Bool MGPipeEveryTextureTargetIsMapped() {
for (SizeT i = 0; i < static_cast<SizeT>(MobileGL::TextureTarget::TextureTargetCount); ++i) {
if (MGPipeResourceTargetForTextureTarget(static_cast<MobileGL::TextureTarget>(i)) ==
@@ -2044,6 +2055,25 @@ namespace MobileGL::MG_Pipe {
inline constexpr Uint32 kMGPBufferAgeDamageRegionFollows = 1u;
inline constexpr Uint32 kMGPNativeFenceFdFollows = 1u;
// An Import of a YUV fourcc (NV12, P010) carries its planes and colour hints in Damage, with
// DamageCount = kMGPImportPlaneWords (the field is a Present's otherwise, and 0 on an RGBA
// Import): both planes live in the one descriptor the Import sends, at these byte offsets and
// row pitches. The hints are EGL_EXT_image_dma_buf_import's values, 0 where not given.
enum MGPImportPlaneWord : Uint32 {
kMGPImportPlaneCount = 0,
kMGPImportPlane0Offset = 1,
kMGPImportPlane1Offset = 2,
kMGPImportPlane0Pitch = 3,
kMGPImportPlane1Pitch = 4,
kMGPImportYuvColorSpace = 5,
kMGPImportYuvRange = 6,
kMGPImportYuvSiting = 7, // horizontal | vertical << 16
kMGPImportModifierLo = 8,
kMGPImportModifierHi = 9,
kMGPImportHasModifier = 10,
kMGPImportPlaneWords = 11,
};
struct MGPSharedImageOp {
Uint32 Op;
Uint32 Format; // DRM fourcc
@@ -2067,8 +2097,11 @@ namespace MobileGL::MG_Pipe {
Uint32 Stride;
Uint32 Offset;
Int32 BufferAge; // QueryBufferAge only
// A YUV image's Cb Cr plane (Allocate, Import): same descriptor, this offset and pitch.
Uint32 Plane1Stride;
Uint32 Plane1Offset;
};
MGP_ASSERT_POD(MGPSharedImageReply, 40);
MGP_ASSERT_POD(MGPSharedImageReply, 48);
// The aux-socket sideband an Import's descriptor travels with (and an Allocate's answer).
inline constexpr Uint32 kMGPSharedImageFdMagic = 0x49534D47u; // 'MGSI'
+3
View File
@@ -2097,6 +2097,9 @@ namespace MobileGL::MG_Pipe {
}
void MGPipeApplySetPixelPackState(const MGPPixelPackState& pack) {
#if MOBILEGL_PIPE_VERIFY
MGPipeApplier().VerifyPixelPack = pack.Pack;
#endif
MGPipeApplyAccess::PackState(gPipeInputs) = pack.Pack;
}
+5
View File
@@ -602,6 +602,11 @@ namespace MobileGL::MG_Pipe {
// The per-context half. Everything here is cleared by MGPipeApplierReset, EXCEPT the serials,
// which only ever advance (see there).
struct MGPipeWorkingState {
#if MOBILEGL_PIPE_VERIFY
// Reference from the pack-state payload, separate from the backend-visible storage.
// The server has no frontend GLContext to use as a read-time oracle.
PixelStoreParameters VerifyPixelPack{};
#endif
// Indexed by slot; slot 0 is the reserved null handle and is never live
// (MGPipeHandles.h kMGPipeFirstAllocatableSlot).
Vector<MGPipeRenderStateCsoRecord> RenderStateCsos;
@@ -604,6 +604,8 @@ namespace MobileGL::MG_Remote::Client {
out->Stride = reply.Stride;
out->Offset = reply.Offset;
out->Modifier = reply.Modifier;
out->Plane1Stride = reply.Plane1Stride;
out->Plane1Offset = reply.Plane1Offset;
{
const std::lock_guard<std::recursive_mutex> lock(m_eglStateMutex);
++m_sessionSharedImages[reply.ImageId];
@@ -630,6 +632,40 @@ namespace MobileGL::MG_Remote::Client {
return true;
}
Bool BackendObject_Remote::ImportSharedImagePlanes(int fd, Uint32 width, Uint32 height, Uint32 fourcc,
const MG_Backend::SharedImageImportLayout& layout,
Uint64* outId) {
ClientSession* session = ClientSession::Active();
if (session == nullptr || fd < 0) return false;
const ClientSession::ScopedCurrentSessionWork tableOp;
MG_Pipe::MGPSharedImageOp op{};
op.Op = MG_Pipe::kMGPSharedImageImport;
op.Format = fourcc;
op.Width = width;
op.Height = height;
// The planes and hints ride in Damage (MGPImportPlaneWord).
op.DamageCount = MG_Pipe::kMGPImportPlaneWords;
op.Damage[MG_Pipe::kMGPImportPlaneCount] = static_cast<Int32>(layout.PlaneCount);
op.Damage[MG_Pipe::kMGPImportPlane0Offset] = static_cast<Int32>(layout.Offset[0]);
op.Damage[MG_Pipe::kMGPImportPlane1Offset] = static_cast<Int32>(layout.Offset[1]);
op.Damage[MG_Pipe::kMGPImportPlane0Pitch] = static_cast<Int32>(layout.Pitch[0]);
op.Damage[MG_Pipe::kMGPImportPlane1Pitch] = static_cast<Int32>(layout.Pitch[1]);
op.Damage[MG_Pipe::kMGPImportYuvColorSpace] = static_cast<Int32>(layout.ColorSpace);
op.Damage[MG_Pipe::kMGPImportYuvRange] = static_cast<Int32>(layout.Range);
op.Damage[MG_Pipe::kMGPImportYuvSiting] = static_cast<Int32>((layout.SitingX & 0xffffu) | (layout.SitingY << 16));
op.Damage[MG_Pipe::kMGPImportModifierLo] = static_cast<Int32>(static_cast<Uint32>(layout.Modifier));
op.Damage[MG_Pipe::kMGPImportModifierHi] = static_cast<Int32>(static_cast<Uint32>(layout.Modifier >> 32));
op.Damage[MG_Pipe::kMGPImportHasModifier] = layout.HasModifier ? 1 : 0;
MG_Pipe::MGPSharedImageReply reply{};
if (!session->EmitSharedImage(op, fd, &reply, nullptr)) return false;
if (outId != nullptr) *outId = reply.ImageId;
{
const std::lock_guard<std::recursive_mutex> lock(m_eglStateMutex);
++m_sessionSharedImages[reply.ImageId];
}
return true;
}
Bool BackendObject_Remote::ReleaseSharedImage(Uint64 id) {
ClientSession* session = ClientSession::Active();
if (session == nullptr || !session->Started()) return false;
@@ -90,6 +90,8 @@ namespace MobileGL::MG_Remote::Client {
Bool AllocateSharedImage(Uint32 width, Uint32 height, Uint32 fourcc,
MG_Backend::SharedImageExport* out) override;
Bool ImportSharedImage(int fd, Uint32 width, Uint32 height, Uint32 fourcc, Uint64* outId) override;
Bool ImportSharedImagePlanes(int fd, Uint32 width, Uint32 height, Uint32 fourcc,
const MG_Backend::SharedImageImportLayout& layout, Uint64* outId) override;
Bool ReleaseSharedImage(Uint64 id) override;
Bool PresentToSharedImage(Uint64 id, const MG_Util::Damage::Region& region) override;
Bool QueryBufferAge(Bool damageRegionFollows, EGLint* age) override;
+41 -30
View File
@@ -2492,38 +2492,47 @@ namespace MobileGL::MG_Remote::Client {
// LastPublishedSeq by construction and a second wait would only add a park on a
// watermark that is already reached.
if (!m_runAheadArmed || !m_started) return;
const Uint64 target = m_producer.LastPublishedSeq();
if (target == 0) return;
if (m_producer.LastPublishedSeq() == 0) return;
const auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(kBarrierTimeoutMs);
const BarrierWaitScope waiting;
const Transport::SessionWait wait =
WaitForAppliedBudget(m_producer, *m_events, target, kBarrierTimeoutMs);
if (wait == Transport::SessionWait::ShutDown) {
// `dl`: the same two-events distinction the verb barrier makes. Only a peer that
// HUNG UP is a device loss; Stop() reaches this path too.
if (const Transport::Doorbell* bell = m_producer.SelfDoorbell();
bell != nullptr && bell->PeerHungUp()) {
LatchDeviceLost("a forced run-ahead wait woke on a peer that had hung up");
for (;;) {
const Uint64 target = m_producer.LastPublishedSeq();
const auto now = std::chrono::steady_clock::now();
const auto remaining = std::chrono::duration_cast<std::chrono::milliseconds>(deadline - now).count();
const Transport::SessionWait wait = now >= deadline
? Transport::SessionWait::TimedOut
: m_producer.WaitForAppliedOrEventBacklog(
target, static_cast<Uint32>(std::max<Int64>(remaining, 1)));
if (wait == Transport::SessionWait::ShutDown) {
// Only a peer that hung up is a device loss; Stop() reaches this path too.
if (const Transport::Doorbell* bell = m_producer.SelfDoorbell();
bell != nullptr && bell->PeerHungUp()) {
LatchDeviceLost("a forced run-ahead wait woke on a peer that had hung up");
}
if (!DeviceLost())
MGLOG_E("MG_Remote client: the forced wait for %s woke on a dead doorbell; the "
"server is gone", why);
return;
}
if (wait != Transport::SessionWait::Reached) {
SessionFail(MGFatalFamily::BarrierTimeout, "MGPipe: Fatal{BarrierTimeout, \"%s\"} - a forced run-ahead wait did not "
"reach appliedSeq %llu within %u ms. The forced waits of CONTRACT-P5E §2.5 "
"are the points at which the client must be in step again, so a wedge here "
"is a wedge in the queue and not a slow frame",
why, static_cast<unsigned long long>(target), kBarrierTimeoutMs);
}
// Drain even when only the event backlog woke us: the server may need that
// space before it can advance appliedSeq. SurfaceChanged callbacks and pin
// destruction can themselves publish records, so the original target alone
// no longer proves the applier is idle after these operations.
DrainEventRing(*m_events);
ReleaseFillPins();
const auto* progress = m_producer.Progress();
if (m_producer.LastPublishedSeq() == target && progress != nullptr &&
Transport::Watermark::Reached(progress->appliedSeq, target)) {
return;
}
// The doorbell died: teardown, not a fault - the same answer EmitAndWaitTails
// gives, and for the same reason.
if (!DeviceLost()) // see the barrier above: the latch already said it
MGLOG_E("MG_Remote client: the forced wait for %s woke on a dead doorbell; the "
"server is gone", why);
return;
}
if (wait != Transport::SessionWait::Reached) {
SessionFail(MGFatalFamily::BarrierTimeout, "MGPipe: Fatal{BarrierTimeout, \"%s\"} - a forced run-ahead wait did not "
"reach appliedSeq %llu within %u ms. The forced waits of CONTRACT-P5E §2.5 "
"are the points at which the client must be in step again, so a wedge here "
"is a wedge in the queue and not a slow frame",
why, static_cast<unsigned long long>(target), kBarrierTimeoutMs);
}
// EVERY WAIT EXIT DRAINS (§2.6). It is what the flow-control deadlock argument rests
// on: the server blocks only on a full event ring, the client blocks only on
// watermarks the server advances, and every client wait drains - so at most one side
// is parked at any instant.
DrainEventRing(*m_events);
ReleaseFillPins();
#else
(void)why;
#endif
@@ -2551,8 +2560,10 @@ namespace MobileGL::MG_Remote::Client {
// glFinish. Nothing goes on the wire - Flush and Finish have no record (ARCHITECTURE
// §11) - and under lockstep there was nothing to do, because the client had already
// waited out every command it issued. Under run-ahead "the commands issued so far have
// completed" is a promise the queue can break, so this is where it is kept.
// completed" is a promise the queue can break, so this is where it is kept - and
// "completed" includes the GPU, so a fence round trip follows the apply.
WaitForApplyToCatchUp("glFinish");
EmitFinishWait();
}
Uint64 ClientSession::AcquirePresentCredit() {
+17
View File
@@ -2085,6 +2085,23 @@ namespace MobileGL::MG_Remote::Client {
} // namespace
// glFinish's GPU half: a server fence created after everything the client issued, waited
// with GL_SYNC_FLUSH_COMMANDS_BIT, then destroyed. Waiting for the apply thread alone left
// the GPU queue unbounded behind a "finished" glFinish.
void EmitFinishWait() {
ClientSession* session = ClientSession::Active();
if (session == nullptr || !session->Started() || session->DeviceLost()) return;
const std::lock_guard<std::mutex> lock(g_fenceMutex);
const MG_Pipe::MGPipeHandle handle = MG_Pipe::MGPipeSlots().Allocate(MG_Pipe::MGPipeKind::Fence);
const MG_Pipe::MGPHandleOnly desc{handle, static_cast<Uint32>(MG_Pipe::MGPipeKind::Fence), 0};
session->EmitAndWait(MG_Pipe::MGPWireOp::FenceCreate, &desc, sizeof(desc), nullptr, 0, nullptr, 0, nullptr);
constexpr GLuint64 kFinishTimeoutNs = 10ull * 1000 * 1000 * 1000;
const MG_Pipe::MGPFenceWait request{handle, kFinishTimeoutNs, GL_SYNC_FLUSH_COMMANDS_BIT, 0};
(void)ReadFenceReply(*session, MG_Pipe::MGPWireOp::FenceWait, &request, sizeof(request));
session->EmitAndWait(MG_Pipe::MGPWireOp::FenceDestroy, &desc, sizeof(desc), nullptr, 0, nullptr, 0, nullptr);
MG_Pipe::MGPipeSlots().Free(MG_Pipe::MGPipeKind::Fence, handle);
}
void NoteFenceSignaledByServer(MG_Pipe::MGPipeHandle fence) {
RememberFenceSignaled(fence);
g_fenceServerReports.fetch_add(1, std::memory_order_relaxed);
+2
View File
@@ -291,6 +291,8 @@ namespace MobileGL::MG_Remote::Client {
//
// The event drain's half: the server reported `fence` signaled (kEventFenceSignaled). Every
// later glClientWaitSync / glGetSynciv(GL_SYNC_STATUS) on it answers without a round trip.
// glFinish's wait for the GPU (a server fence created, waited and destroyed).
void EmitFinishWait();
void NoteFenceSignaledByServer(MG_Pipe::MGPipeHandle fence);
// What the fence polls cost, for the lanes. `LocalAnswers` = polls answered with no record
+43
View File
@@ -1517,6 +1517,8 @@ namespace MobileGL::MG_Remote::Server {
reply.Format = image.Fourcc;
reply.Stride = image.Stride;
reply.Offset = image.Offset;
reply.Plane1Stride = image.Plane1Stride;
reply.Plane1Offset = image.Plane1Offset;
};
switch (op.Op) {
case MG_Pipe::kMGPSharedImageAllocate: {
@@ -1554,6 +1556,33 @@ namespace MobileGL::MG_Remote::Server {
return false;
}
SI::ImageRef image = SI::Identify(fd, why);
// YUV: the planes and colour hints ride in Damage (MGPImportPlaneWord). A buffer this
// server did not allocate is taken too, through the labelled CPU-copy fallback
// (SharedImageRegistry.h, Image::ForeignSource); RGBA ones never are.
const Bool yuv = SI::FourccIsYuv(op.Format) && op.DamageCount == MG_Pipe::kMGPImportPlaneWords;
if (image == nullptr && yuv) {
SI::PlaneLayout layout;
layout.Count = static_cast<Uint32>(op.Damage[MG_Pipe::kMGPImportPlaneCount]);
layout.Offset[0] = static_cast<Uint32>(op.Damage[MG_Pipe::kMGPImportPlane0Offset]);
layout.Offset[1] = static_cast<Uint32>(op.Damage[MG_Pipe::kMGPImportPlane1Offset]);
layout.Pitch[0] = static_cast<Uint32>(op.Damage[MG_Pipe::kMGPImportPlane0Pitch]);
layout.Pitch[1] = static_cast<Uint32>(op.Damage[MG_Pipe::kMGPImportPlane1Pitch]);
// The copy reads rows: only a buffer said to be linear (or said nothing) is one.
const Uint64 modifier =
static_cast<Uint64>(static_cast<Uint32>(op.Damage[MG_Pipe::kMGPImportModifierLo])) |
(static_cast<Uint64>(static_cast<Uint32>(op.Damage[MG_Pipe::kMGPImportModifierHi])) << 32);
if (op.Damage[MG_Pipe::kMGPImportHasModifier] != 0 && modifier != SI::kModifierLinear &&
modifier != SI::kModifierInvalid) {
char text[96];
std::snprintf(text, sizeof(text), "; a foreign buffer of modifier 0x%016llx is not linear",
static_cast<unsigned long long>(modifier));
why += text;
} else {
std::string foreignWhy;
image = SI::ImportForeignYuv(fd, op.Width, op.Height, op.Format, layout, foreignWhy);
if (image == nullptr) why += "; as a foreign YUV buffer: " + foreignWhy;
}
}
#if !defined(_WIN32)
::close(fd);
#endif
@@ -1561,6 +1590,20 @@ namespace MobileGL::MG_Remote::Server {
MGLOG_W_ONCE("MG_Remote server: a dma-buf import was refused: %s", why.c_str());
return false;
}
if (SI::FourccIsYuv(image->Fourcc) != SI::FourccIsYuv(op.Format) ||
(SI::FourccIsYuv(op.Format) && image->Fourcc != op.Format)) {
MGLOG_W("MG_Remote server: a dma-buf import of image %llu (fourcc 0x%08x) as fourcc 0x%08x was refused",
static_cast<unsigned long long>(image->Id), image->Fourcc, op.Format);
return false;
}
if (yuv) {
SI::Yuv::Hints hints;
hints.ColorSpace = static_cast<Uint32>(op.Damage[MG_Pipe::kMGPImportYuvColorSpace]);
hints.Range = static_cast<Uint32>(op.Damage[MG_Pipe::kMGPImportYuvRange]);
hints.SitingX = static_cast<Uint32>(op.Damage[MG_Pipe::kMGPImportYuvSiting]) & 0xffffu;
hints.SitingY = static_cast<Uint32>(op.Damage[MG_Pipe::kMGPImportYuvSiting]) >> 16;
SI::SetYuvHints(*image, hints);
}
if (image->Width != op.Width || image->Height != op.Height || !SI::FourccSupported(op.Format)) {
MGLOG_W("MG_Remote server: a dma-buf import of image %llu (%ux%u) as %ux%u fourcc 0x%08x was refused",
static_cast<unsigned long long>(image->Id), image->Width, image->Height, op.Width, op.Height,
+65 -24
View File
@@ -28,6 +28,9 @@
#include <condition_variable>
#include <cstdlib>
#include <cstdio>
#if defined(__ANDROID__)
#include <sys/system_properties.h>
#endif
#include <cstring>
#include <mutex>
#include <optional>
@@ -219,6 +222,23 @@ namespace MobileGL::MG_Remote::Server {
return text != nullptr && text[0] != '\0';
}
// The per-frame P65ServerFrame line is a measurement aid, not a log: at one line per present
// of every session it floods the device log (and pushes the lines tests grep for out of the
// ring). Opt in with MOBILEGL_SERVER_FRAME_STATS=1, or on Android the property
// debug.mobilegl.server_frame_stats=1; read once.
Bool ServerFrameStatsEnabled() {
static const Bool enabled = [] {
const char* value = std::getenv("MOBILEGL_SERVER_FRAME_STATS");
#if defined(__ANDROID__)
char property[PROP_VALUE_MAX] = {};
if (value == nullptr && __system_property_get("debug.mobilegl.server_frame_stats", property) > 0)
value = property;
#endif
return value != nullptr && value[0] != '' && value[0] != '0';
}();
return enabled;
}
void NameThisThread(const char* name) {
#if defined(__linux__) || defined(__ANDROID__)
pthread_setname_np(pthread_self(), name);
@@ -681,6 +701,13 @@ namespace MobileGL::MG_Remote::Server {
std::optional<ScopedBackendTurn> turn(std::in_place);
PumpControlRequest();
if (stopOrForfeit()) break;
// A SESSION WHOSE OWN GPU WORK HUNG THE GPU APPLIES NOTHING MORE. A shader that never
// ends stops every process's GPU work until the kernel resets it (MG_Backend/
// GpuHangWatch.h), and the same client's next frame usually carries the same shader. The
// backend's verdict - the driver's GUILTY report on Espryt, the hang watch on Magma - is
// asked HERE, before the drain, so that frame is never applied: the session latches
// (DeviceLost, its client reads a lost context) and leaves below. Cheap by contract.
if (m_backend != nullptr && !SessionLatched()) (void)m_backend->LatchIfGpuFaulted();
if (eventRingHasRoom()) DrainRing();
if (stopOrForfeit()) break;
// The session-targeted device-loss debug knob (PipeSessionFail.h): asked here, after a
@@ -1058,6 +1085,7 @@ namespace MobileGL::MG_Remote::Server {
static std::uint64_t sFrameRecords = 0;
static std::uint64_t sFrameIndex = 0;
Uint64 applied = 0;
const bool timed = ServerFrameStatsEnabled();
for (;;) {
if (SessionLatched()) break;
if (m_windowRequests.load(std::memory_order_acquire) != 0) {
@@ -1066,15 +1094,25 @@ namespace MobileGL::MG_Remote::Server {
}
bool corrupt = false;
const bool popped = consumer.ApplyOne(
[this, &applier](const Transport::RingRecordView& record) {
const auto applyStarted = std::chrono::steady_clock::now();
applier.ApplyOne(record);
[this, &applier, timed](const Transport::RingRecordView& record) {
// Check after acquiring each record: a producer may publish more work
// while this drain is running, after the outer-loop reset check.
if (m_backend != nullptr && m_backend->LatchIfGpuFaulted()) return false;
if (SessionLatched()) return false;
// P65ServerFrame: timed around the ONE call that is the server's work, and
// summed across the drains a frame spans. See the block above DrainRing's loop.
sFrameApplyNs += static_cast<std::uint64_t>(
std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::steady_clock::now() - applyStarted)
.count());
// Only when the line is on: two clock reads per record were a measurable part
// of a record's cost.
if (timed) {
const auto applyStarted = std::chrono::steady_clock::now();
applier.ApplyOne(record);
sFrameApplyNs += static_cast<std::uint64_t>(
std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::steady_clock::now() - applyStarted)
.count());
} else {
applier.ApplyOne(record);
}
++sFrameRecords;
// P9 W3, F2: outside the apply timing above; 0 in every run that is not F2.
if (m_testLogFloodLines != 0) FloodLogForTest(m_testLogFloodLines);
@@ -1092,23 +1130,25 @@ namespace MobileGL::MG_Remote::Server {
// reader spent WAITING for bytes, and one that is mostly recv calls with
// little read_ms is a reader that is copying.
const auto read = Transport::LinkMetricsTakeReadStats();
MGLOG_I("P65ServerFrame frame=%llu records=%llu apply_ms=%.1f wall_ms=%.1f "
"outside_ms=%.1f | read: %llu B in %llu recv(%llu reads) "
"recv_ms=%.1f total_ms=%.1f (%.0f%% blocked, %.1f MB/s)",
static_cast<unsigned long long>(++sFrameIndex),
static_cast<unsigned long long>(sFrameRecords), applyMs, wallMs,
wallMs - applyMs,
static_cast<unsigned long long>(read.bytes),
static_cast<unsigned long long>(read.calls),
static_cast<unsigned long long>(read.reads),
static_cast<double>(read.nsInRecv) / 1e6,
static_cast<double>(read.nsTotal) / 1e6,
read.nsTotal ? 100.0 * static_cast<double>(read.nsInRecv) /
static_cast<double>(read.nsTotal)
: 0.0,
read.nsTotal ? static_cast<double>(read.bytes) /
(static_cast<double>(read.nsTotal) / 1e9) / 1e6
: 0.0);
if (ServerFrameStatsEnabled()) {
MGLOG_I("P65ServerFrame frame=%llu records=%llu apply_ms=%.1f wall_ms=%.1f "
"outside_ms=%.1f | read: %llu B in %llu recv(%llu reads) "
"recv_ms=%.1f total_ms=%.1f (%.0f%% blocked, %.1f MB/s)",
static_cast<unsigned long long>(++sFrameIndex),
static_cast<unsigned long long>(sFrameRecords), applyMs, wallMs,
wallMs - applyMs,
static_cast<unsigned long long>(read.bytes),
static_cast<unsigned long long>(read.calls),
static_cast<unsigned long long>(read.reads),
static_cast<double>(read.nsInRecv) / 1e6,
static_cast<double>(read.nsTotal) / 1e6,
read.nsTotal ? 100.0 * static_cast<double>(read.nsInRecv) /
static_cast<double>(read.nsTotal)
: 0.0,
read.nsTotal ? static_cast<double>(read.bytes) /
(static_cast<double>(read.nsTotal) / 1e9) / 1e6
: 0.0);
}
sFrameWallStart = now;
sFrameApplyNs = 0;
sFrameRecords = 0;
@@ -1126,6 +1166,7 @@ namespace MobileGL::MG_Remote::Server {
// DrainedRecords() one behind appliedSeq on 1 run in ~40 under `-j 8`.
// Per-record, before the publish, it can never be behind.
m_drained.fetch_add(1, std::memory_order_acq_rel);
return true;
},
&corrupt);
if (corrupt) {
+10
View File
@@ -663,6 +663,16 @@ int RunSession(std::unique_ptr<SocketTransport> control, std::vector<std::uint8_
const char* forwarding = std::getenv("MOBILEGL_IPC_LOG_FORWARD");
if (tcp && (!forwarding || std::strcmp(forwarding, "0") != 0)) {
// F2 fault injection: bound the kernel queue as well as the log queue so a
// non-reading peer reliably blocks the sender, regardless of host autotuning.
if (const char* stalled = std::getenv("MOBILEGL_TEST_LOG_SMALL_SNDBUF");
stalled && std::strcmp(stalled, "1") == 0) {
const int bytes = 16 * 1024;
if (::setsockopt(control->StreamFd(), SOL_SOCKET, SO_SNDBUF, &bytes, sizeof(bytes)) != 0) {
WireLogError("MG_Remote server: could not set F2 control send buffer");
return endSession(70);
}
}
// P9 W3: the forward is a queue with a sender thread of its own, graded by severity
// (LogForward.h): a client that stops reading control no longer holds any logging thread -
// the apply thread above all - inside a socket write.
+382 -31
View File
@@ -19,10 +19,13 @@
#if defined(__ANDROID__)
#include <android/hardware_buffer.h>
#include <dlfcn.h>
#endif
#if !defined(_WIN32)
#include <fcntl.h>
#include <linux/dma-buf.h>
#include <poll.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/stat.h>
@@ -43,6 +46,8 @@ namespace MobileGL::MG_Remote::Server::SharedImages {
std::atomic<Uint64> nextId{1};
UnorderedMap<Uint64, std::weak_ptr<const Image>> byId;
std::map<std::pair<Uint64, Uint64>, std::weak_ptr<const Image>> byIdentity;
// Foreign YUV images, by the identity of the buffer they copy from.
std::map<std::pair<Uint64, Uint64>, std::weak_ptr<const Image>> foreignByIdentity;
// Set once two live images were seen with one identity: the kernel shares an inode
// between dma-bufs, so a descriptor can no longer say which image it is.
Bool identityAmbiguous = false;
@@ -107,14 +112,93 @@ namespace MobileGL::MG_Remote::Server::SharedImages {
return true;
}
// The two AHardwareBuffer entry points newer than this library's minimum platform (29),
// resolved at run time. Null on an older platform: YUV images are then not allocatable.
struct AhbPlaneEntryPoints {
using LockPlanesFn = int (*)(AHardwareBuffer*, uint64_t, int32_t, const ARect*, AHardwareBuffer_Planes*);
using IsSupportedFn = int (*)(const AHardwareBuffer_Desc*);
LockPlanesFn LockPlanes = nullptr;
IsSupportedFn IsSupported = nullptr;
};
const AhbPlaneEntryPoints& AhbPlanes() {
static const AhbPlaneEntryPoints entry = [] {
AhbPlaneEntryPoints e;
void* lib = dlopen("libnativewindow.so", RTLD_NOW | RTLD_NOLOAD);
if (lib == nullptr) lib = dlopen("libnativewindow.so", RTLD_NOW);
if (lib != nullptr) {
e.LockPlanes =
reinterpret_cast<AhbPlaneEntryPoints::LockPlanesFn>(dlsym(lib, "AHardwareBuffer_lockPlanes"));
e.IsSupported =
reinterpret_cast<AhbPlaneEntryPoints::IsSupportedFn>(dlsym(lib, "AHardwareBuffer_isSupported"));
}
return e;
}();
return entry;
}
// A YUV image's buffer: sampled by the GPU, written by the CPU (a foreign image's copy). The
// CPU usage also keeps the layout one the platform can lock plane by plane, which is how
// every layout question about it is answered (AHardwareBuffer_lockPlanes) - never by
// assuming one.
constexpr uint64_t kYuvUsage = AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE |
AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN | AHARDWAREBUFFER_USAGE_CPU_READ_RARELY;
uint32_t AhbFormatOf(Uint32 fourcc) {
if (fourcc == kFourccNv12) return AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420;
if (fourcc == kFourccP010) return AHARDWAREBUFFER_FORMAT_YCbCr_P010;
return FourccIgnoresAlpha(fourcc) ? AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM
: AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM;
}
Bool YuvAllocatableNative(Uint32 fourcc) {
const AhbPlaneEntryPoints& entry = AhbPlanes();
if (entry.LockPlanes == nullptr || entry.IsSupported == nullptr) return false;
AHardwareBuffer_Desc desc{};
desc.width = 64;
desc.height = 64;
desc.layers = 1;
desc.format = AhbFormatOf(fourcc);
desc.usage = kYuvUsage;
return entry.IsSupported(&desc) == 1;
}
// The planes of a locked YUV buffer as the plane copy addresses them.
Bool LockYuvPlanes(AHardwareBuffer* ahb, Yuv::PlanarLayout& out, std::string& why) {
const AhbPlaneEntryPoints& entry = AhbPlanes();
if (entry.LockPlanes == nullptr) {
why = "AHardwareBuffer_lockPlanes is not available";
return false;
}
AHardwareBuffer_Planes planes{};
const int locked = entry.LockPlanes(ahb, AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN, -1, nullptr, &planes);
if (locked != 0) {
why = Format("AHardwareBuffer_lockPlanes rc=%lld", locked);
return false;
}
if (planes.planeCount < 3) {
AHardwareBuffer_unlock(ahb, nullptr);
why = Format("the locked buffer has %lld planes, not 3", planes.planeCount);
return false;
}
const auto view = [](const AHardwareBuffer_Plane& plane) {
return Yuv::PlaneView{static_cast<Uint8*>(plane.data), static_cast<Int64>(plane.rowStride),
plane.pixelStride};
};
out.Y = view(planes.planes[0]);
out.Cb = view(planes.planes[1]);
out.Cr = view(planes.planes[2]);
return true;
}
Bool AllocateNative(Image& image, std::string& why) {
AHardwareBuffer_Desc desc{};
desc.width = image.Width;
desc.height = image.Height;
desc.layers = 1;
desc.format = FourccIgnoresAlpha(image.Fourcc) ? AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM
: AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM;
desc.usage = AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE | AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT;
desc.format = AhbFormatOf(image.Fourcc);
desc.usage = FourccIsYuv(image.Fourcc)
? kYuvUsage
: AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE | AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT;
AHardwareBuffer* ahb = nullptr;
const int allocated = AHardwareBuffer_allocate(&desc, &ahb);
if (allocated != 0 || ahb == nullptr) {
@@ -131,8 +215,32 @@ namespace MobileGL::MG_Remote::Server::SharedImages {
image.Native = ahb;
image.Fd = fds[0];
for (SizeT i = 1; i < fds.size(); ++i) ::close(fds[i]);
image.Stride = described.stride * 4;
image.Offset = 0;
if (!FourccIsYuv(image.Fourcc)) {
image.Stride = described.stride * 4;
return true;
}
// Plane positions as the platform reports them when the buffer is locked: offsets from
// the luma plane's start, the Cb Cr plane at whichever of Cb and Cr comes first.
const Uint32 sampleBytes = FourccYuvSampleBytes(image.Fourcc);
image.Stride = described.stride * sampleBytes;
image.Plane1Stride = image.Stride;
image.Plane1Offset = image.Stride * image.Height;
Yuv::PlanarLayout planes;
std::string lockWhy;
if (LockYuvPlanes(ahb, planes, lockWhy)) {
const Uint8* chroma = std::min(planes.Cb.Data, planes.Cr.Data);
if (planes.Y.RowStride > 0 && chroma > planes.Y.Data) {
image.Stride = static_cast<Uint32>(planes.Y.RowStride);
image.Plane1Stride = static_cast<Uint32>(planes.Cb.RowStride);
image.Plane1Offset = static_cast<Uint32>(chroma - planes.Y.Data);
}
AHardwareBuffer_unlock(ahb, nullptr);
} else {
MGLOG_W_ONCE("MG_Remote server: a YUV shared image's planes could not be located (%s); its "
"plane offsets are nominal",
lockWhy.c_str());
}
return true;
}
@@ -143,15 +251,29 @@ namespace MobileGL::MG_Remote::Server::SharedImages {
// A host has no AHardwareBuffer. A memfd stands in for the dma-buf so the registry, the
// descriptor round trip and identification can be exercised by the unit tests; no backend
// binds one.
Bool YuvAllocatableNative(Uint32) { return true; }
Bool AllocateNative(Image& image, std::string& why) {
image.Stride = image.Width * 4;
Uint64 bytes = 0;
image.Offset = 0;
if (FourccIsYuv(image.Fourcc)) {
// A tight semi-planar layout stands in for the platform's.
const Uint32 sampleBytes = FourccYuvSampleBytes(image.Fourcc);
image.Stride = image.Width * sampleBytes;
image.Plane1Stride = ((image.Width + 1) / 2) * 2 * sampleBytes;
image.Plane1Offset = image.Stride * image.Height;
bytes = static_cast<Uint64>(image.Plane1Offset) +
static_cast<Uint64>(image.Plane1Stride) * ((image.Height + 1) / 2);
} else {
image.Stride = image.Width * 4;
bytes = static_cast<Uint64>(image.Stride) * image.Height;
}
const int fd = ::memfd_create("mobilegl-shared-image", MFD_CLOEXEC);
if (fd < 0) {
why = Format("memfd_create errno=%lld", errno);
return false;
}
if (::ftruncate(fd, static_cast<off_t>(image.Stride) * image.Height) != 0) {
if (::ftruncate(fd, static_cast<off_t>(bytes)) != 0) {
why = Format("ftruncate errno=%lld", errno);
::close(fd);
return false;
@@ -162,6 +284,8 @@ namespace MobileGL::MG_Remote::Server::SharedImages {
void ReleaseNative(void*) {}
#else
Bool YuvAllocatableNative(Uint32) { return false; }
Bool AllocateNative(Image&, std::string& why) {
why = "shared images need POSIX descriptors";
return false;
@@ -169,6 +293,66 @@ namespace MobileGL::MG_Remote::Server::SharedImages {
void ReleaseNative(void*) {}
#endif
#if !defined(_WIN32)
// The planes of a YUV image as the CPU writes them, held until the guard goes: the locked
// AHardwareBuffer on Android, a mapping of the stand-in memfd on a host.
class YuvWriteAccess {
public:
YuvWriteAccess(const Image& image, std::string& why) : m_image(image) {
#if defined(__ANDROID__)
auto* ahb = static_cast<AHardwareBuffer*>(image.Native);
m_ok = ahb != nullptr && LockYuvPlanes(ahb, m_planes, why);
#else
const SizeT bytes = static_cast<SizeT>(image.Plane1Offset) +
static_cast<SizeT>(image.Plane1Stride) * ((image.Height + 1) / 2);
void* map = ::mmap(nullptr, bytes, PROT_READ | PROT_WRITE, MAP_SHARED, image.Fd, 0);
if (map == MAP_FAILED) {
why = Format("mmap of the stand-in buffer errno=%lld", errno);
return;
}
m_map = map;
m_mapSize = bytes;
const Uint32 sampleBytes = FourccYuvSampleBytes(image.Fourcc);
auto* base = static_cast<Uint8*>(map);
m_planes.Y = {base, image.Stride, sampleBytes};
m_planes.Cb = {base + image.Plane1Offset, image.Plane1Stride, 2 * sampleBytes};
m_planes.Cr = {base + image.Plane1Offset + sampleBytes, image.Plane1Stride, 2 * sampleBytes};
m_ok = true;
#endif
}
~YuvWriteAccess() {
#if defined(__ANDROID__)
if (m_ok) AHardwareBuffer_unlock(static_cast<AHardwareBuffer*>(m_image.Native), nullptr);
#else
if (m_map != nullptr) ::munmap(m_map, m_mapSize);
#endif
}
YuvWriteAccess(const YuvWriteAccess&) = delete;
YuvWriteAccess& operator=(const YuvWriteAccess&) = delete;
Bool Ok() const { return m_ok; }
const Yuv::PlanarLayout& Planes() const { return m_planes; }
private:
const Image& m_image;
Yuv::PlanarLayout m_planes;
Bool m_ok = false;
#if !defined(__ANDROID__)
void* m_map = nullptr;
SizeT m_mapSize = 0;
#endif
};
// DMA_BUF_IOCTL_SYNC around a CPU read of a foreign buffer: the producer's CPU-side writes
// are made visible (cache maintenance) and its implicit fences waited for. A buffer that
// does not take the ioctl (not a dma-buf: a host test's memfd) is read as it is.
void DmaBufSync(int fd, Bool start) {
dma_buf_sync sync{};
sync.flags = DMA_BUF_SYNC_READ | (start ? DMA_BUF_SYNC_START : DMA_BUF_SYNC_END);
while (::ioctl(fd, DMA_BUF_IOCTL_SYNC, &sync) != 0 && (errno == EINTR || errno == EAGAIN)) {
}
}
#endif
} // namespace
Image::~Image() {
@@ -181,8 +365,18 @@ namespace MobileGL::MG_Remote::Server::SharedImages {
const auto key = std::make_pair(IdentityDev, IdentityIno);
if (auto it = reg.byIdentity.find(key); it != reg.byIdentity.end() && it->second.expired())
reg.byIdentity.erase(it);
if (Foreign != nullptr) {
const auto source = std::make_pair(Foreign->IdentityDev, Foreign->IdentityIno);
if (auto it = reg.foreignByIdentity.find(source);
it != reg.foreignByIdentity.end() && it->second.expired())
reg.foreignByIdentity.erase(it);
}
}
#if !defined(_WIN32)
if (Foreign != nullptr) {
if (Foreign->Map != nullptr) ::munmap(Foreign->Map, Foreign->MapSize);
if (Foreign->Fd >= 0) ::close(Foreign->Fd);
}
if (Fd >= 0) ::close(Fd);
if (WriteFence >= 0) ::close(WriteFence);
for (const int fence : ReadFences) ::close(fence);
@@ -190,49 +384,206 @@ namespace MobileGL::MG_Remote::Server::SharedImages {
ReleaseNative(Native);
}
Bool FourccIsYuv(Uint32 fourcc) { return fourcc == kFourccNv12 || fourcc == kFourccP010; }
Uint32 FourccYuvSampleBytes(Uint32 fourcc) {
if (fourcc == kFourccNv12) return 1;
if (fourcc == kFourccP010) return 2;
return 0;
}
Bool YuvFourccAllocatable(Uint32 fourcc) {
if (!FourccIsYuv(fourcc)) return false;
static std::mutex mutex;
static std::map<Uint32, Bool> answers;
const std::lock_guard<std::mutex> lock(mutex);
const auto it = answers.find(fourcc);
if (it != answers.end()) return it->second;
const Bool answer = YuvAllocatableNative(fourcc);
answers.emplace(fourcc, answer);
return answer;
}
Bool FourccSupported(Uint32 fourcc) {
return fourcc == kFourccAbgr8888 || fourcc == kFourccXbgr8888 || fourcc == kFourccArgb8888 ||
fourcc == kFourccXrgb8888;
fourcc == kFourccXrgb8888 || YuvFourccAllocatable(fourcc);
}
Bool FourccIgnoresAlpha(Uint32 fourcc) { return fourcc == kFourccXbgr8888 || fourcc == kFourccXrgb8888; }
namespace {
// Allocate's body. `foreign`, when given, is on the image before anything can find it.
ImageRef AllocateImage(Uint32 width, Uint32 height, Uint32 fourcc, UniquePtr<Image::ForeignSource> foreign,
std::string& why) {
if (width == 0 || height == 0 || width > 16384 || height > 16384) {
why = Format("%lldx%lld is not an image size", width, height);
return nullptr;
}
if (!FourccSupported(fourcc)) {
why = Format("fourcc 0x%08llx is not a shared-image format", fourcc);
return nullptr;
}
Registry& reg = Reg();
auto image = std::make_shared<Image>();
image->Id = reg.nextId.fetch_add(1, std::memory_order_relaxed);
image->Width = width;
image->Height = height;
image->Fourcc = fourcc;
image->Foreign = std::move(foreign);
if (!AllocateNative(*image, why)) return nullptr;
#if !defined(_WIN32)
if (!IdentityOf(image->Fd, &image->IdentityDev, &image->IdentityIno)) {
why = Format("fstat on the exported descriptor errno=%lld", errno);
return nullptr;
}
#endif
const std::lock_guard<std::mutex> lock(reg.mutex);
reg.byId[image->Id] = image;
const auto key = std::make_pair(image->IdentityDev, image->IdentityIno);
if (auto it = reg.byIdentity.find(key); it != reg.byIdentity.end() && !it->second.expired()) {
if (!reg.identityAmbiguous) {
MGLOG_E("MG_Remote server: two live shared images have one dma-buf identity (dev %llu ino %llu); "
"this kernel does not keep dma-buf inodes unique, so a returned descriptor cannot name its "
"image and every shared-image import is refused from here on",
static_cast<unsigned long long>(key.first), static_cast<unsigned long long>(key.second));
}
reg.identityAmbiguous = true;
}
reg.byIdentity[key] = image;
if (image->Foreign != nullptr) {
reg.foreignByIdentity[std::make_pair(image->Foreign->IdentityDev, image->Foreign->IdentityIno)] =
image;
}
return image;
}
} // namespace
ImageRef Allocate(Uint32 width, Uint32 height, Uint32 fourcc, std::string& why) {
return AllocateImage(width, height, fourcc, nullptr, why);
}
ImageRef ImportForeignYuv(int fd, Uint32 width, Uint32 height, Uint32 fourcc, const PlaneLayout& layout,
std::string& why) {
#if defined(_WIN32)
(void)fd, (void)width, (void)height, (void)fourcc, (void)layout;
why = "shared images need POSIX descriptors";
return nullptr;
#else
if (!FourccIsYuv(fourcc) || !YuvFourccAllocatable(fourcc)) {
why = Format("fourcc 0x%08llx is not a YUV format this server can hold", fourcc);
return nullptr;
}
if (width == 0 || height == 0 || width > 16384 || height > 16384) {
why = Format("%lldx%lld is not an image size", width, height);
return nullptr;
}
if (!FourccSupported(fourcc)) {
why = Format("fourcc 0x%08llx is not a shared-image format", fourcc);
if (layout.Count != 2) {
why = Format("a YUV import needs 2 planes, not %lld", layout.Count);
return nullptr;
}
Uint64 dev = 0, ino = 0;
if (!IdentityOf(fd, &dev, &ino)) {
why = Format("fstat on the imported descriptor errno=%lld", errno);
return nullptr;
}
Registry& reg = Reg();
auto image = std::make_shared<Image>();
image->Id = reg.nextId.fetch_add(1, std::memory_order_relaxed);
image->Width = width;
image->Height = height;
image->Fourcc = fourcc;
if (!AllocateNative(*image, why)) return nullptr;
#if !defined(_WIN32)
if (!IdentityOf(image->Fd, &image->IdentityDev, &image->IdentityIno)) {
why = Format("fstat on the exported descriptor errno=%lld", errno);
{
const std::lock_guard<std::mutex> lock(reg.mutex);
const auto it = reg.foreignByIdentity.find(std::make_pair(dev, ino));
ImageRef existing = it != reg.foreignByIdentity.end() ? it->second.lock() : nullptr;
if (existing != nullptr && existing->Width == width && existing->Height == height &&
existing->Fourcc == fourcc && existing->Foreign->Layout.Offset[0] == layout.Offset[0] &&
existing->Foreign->Layout.Offset[1] == layout.Offset[1] &&
existing->Foreign->Layout.Pitch[0] == layout.Pitch[0] &&
existing->Foreign->Layout.Pitch[1] == layout.Pitch[1])
return existing;
}
// The planes must fit the buffer: every row the copy reads is inside the mapping.
const Uint32 sampleBytes = FourccYuvSampleBytes(fourcc);
const off_t end = ::lseek(fd, 0, SEEK_END);
if (end <= 0) {
why = Format("the buffer's size is unknown (lseek errno=%lld)", errno);
return nullptr;
}
#endif
const std::lock_guard<std::mutex> lock(reg.mutex);
reg.byId[image->Id] = image;
const auto key = std::make_pair(image->IdentityDev, image->IdentityIno);
if (auto it = reg.byIdentity.find(key); it != reg.byIdentity.end() && !it->second.expired()) {
if (!reg.identityAmbiguous) {
MGLOG_E("MG_Remote server: two live shared images have one dma-buf identity (dev %llu ino %llu); "
"this kernel does not keep dma-buf inodes unique, so a returned descriptor cannot name its "
"image and every shared-image import is refused from here on",
static_cast<unsigned long long>(key.first), static_cast<unsigned long long>(key.second));
}
reg.identityAmbiguous = true;
const SizeT size = static_cast<SizeT>(end);
const Uint32 chromaHeight = (height + 1) / 2;
const Uint64 chromaRow = static_cast<Uint64>((width + 1) / 2) * 2 * sampleBytes;
const Uint64 lumaEnd = static_cast<Uint64>(layout.Offset[0]) +
static_cast<Uint64>(layout.Pitch[0]) * (height - 1) +
static_cast<Uint64>(width) * sampleBytes;
const Uint64 chromaEnd =
static_cast<Uint64>(layout.Offset[1]) + static_cast<Uint64>(layout.Pitch[1]) * (chromaHeight - 1) + chromaRow;
if (layout.Pitch[0] < width * sampleBytes || layout.Pitch[1] < chromaRow || lumaEnd > size ||
chromaEnd > size) {
why = Format("its planes do not fit the %lld-byte buffer", static_cast<long long>(size));
return nullptr;
}
reg.byIdentity[key] = image;
void* map = ::mmap(nullptr, size, PROT_READ, MAP_SHARED, fd, 0);
if (map == MAP_FAILED) {
why = Format("the buffer cannot be mapped for reading (errno=%lld)", errno);
return nullptr;
}
auto foreign = MakeUnique<Image::ForeignSource>();
foreign->Fd = ::fcntl(fd, F_DUPFD_CLOEXEC, 0);
foreign->Layout = layout;
foreign->Map = map;
foreign->MapSize = size;
foreign->IdentityDev = dev;
foreign->IdentityIno = ino;
if (foreign->Fd < 0) {
::munmap(map, size);
why = Format("dup errno=%lld", errno);
return nullptr;
}
// A failed allocation drops the half-built image, and ~Image unmaps and closes the source.
ImageRef image = AllocateImage(width, height, fourcc, std::move(foreign), why);
if (image == nullptr) return nullptr;
MGLOG_I("MG_Remote server: a foreign %ux%u %s dma-buf is sampled through image %llu, refreshed by a CPU "
"copy (the fallback for a buffer this platform cannot import)",
width, height, fourcc == kFourccNv12 ? "NV12" : "P010", static_cast<unsigned long long>(image->Id));
return image;
#endif
}
Bool RefreshForeign(const Image& image, std::string& why) {
#if defined(_WIN32)
(void)image;
why = "shared images need POSIX descriptors";
return false;
#else
if (image.Foreign == nullptr) return true;
Image::ForeignSource& source = *image.Foreign;
// WRITE AFTER READ: what a frame still samples of the old content finishes first. The copy
// is the CPU's, so this is a CPU wait - bounded, like every wait on another session.
if (!WaitForReads(image, 1000)) {
MGLOG_W_ONCE("MG_Remote server: a foreign image's readers did not finish within 1 s; refreshed anyway");
}
const std::lock_guard<std::mutex> lock(source.Mutex);
YuvWriteAccess access(image, why);
if (!access.Ok()) return false;
const auto* base = static_cast<const Uint8*>(source.Map);
DmaBufSync(source.Fd, true);
const Bool copied = Yuv::CopySemiPlanar(base + source.Layout.Offset[0], source.Layout.Pitch[0],
base + source.Layout.Offset[1], source.Layout.Pitch[1], image.Width,
image.Height, FourccYuvSampleBytes(image.Fourcc), access.Planes());
DmaBufSync(source.Fd, false);
if (!copied) {
why = "the locked buffer's planes cannot hold the samples";
return false;
}
++source.Copies;
return true;
#endif
}
void SetYuvHints(const Image& image, const Yuv::Hints& hints) {
const std::lock_guard<std::mutex> lock(image.SyncMutex);
image.YuvHints = hints;
}
Yuv::Hints GetYuvHints(const Image& image) {
const std::lock_guard<std::mutex> lock(image.SyncMutex);
return image.YuvHints;
}
ImageRef Identify(int fd, std::string& why) {
@@ -25,6 +25,8 @@
#pragma once
#include <Includes.h>
#include "SharedImageYuv.h"
#include <mutex>
#include <string>
@@ -44,6 +46,11 @@ namespace MobileGL::MG_Remote::Server::SharedImages {
// client renders as red samples as red, whichever of the four names it allocated under.
inline constexpr Uint32 kFourccArgb8888 = FourCC('A', 'R', '2', '4');
inline constexpr Uint32 kFourccXrgb8888 = FourCC('X', 'R', '2', '4');
// YUV 4:2:0, two planes: Y, then Cb Cr interleaved at half resolution (8-bit samples; P010 the
// same with 10 significant bits in the high end of 16). A YUV image is never rendered to: it is
// SAMPLED, converted into RGBA by whoever binds it (SharedImageYuv.h).
inline constexpr Uint32 kFourccNv12 = FourCC('N', 'V', '1', '2');
inline constexpr Uint32 kFourccP010 = FourCC('P', '0', '1', '0');
// "The layout is the allocator's": what an AHardwareBuffer is to anybody but its allocator.
inline constexpr Uint64 kModifierInvalid = 0x00ffffffffffffffull;
inline constexpr Uint64 kModifierLinear = 0;
@@ -52,6 +59,20 @@ namespace MobileGL::MG_Remote::Server::SharedImages {
Bool FourccSupported(Uint32 fourcc);
// The X formats: alpha reads as 1 whatever is stored.
Bool FourccIgnoresAlpha(Uint32 fourcc);
// NV12 / P010.
Bool FourccIsYuv(Uint32 fourcc);
// Bytes of one sample of a YUV fourcc (1, or 2 for P010); 0 for anything else.
Uint32 FourccYuvSampleBytes(Uint32 fourcc);
// Whether this server can allocate images of a YUV fourcc (the platform's allocator takes the
// format for sampling). Asked once per format, cached.
Bool YuvFourccAllocatable(Uint32 fourcc);
// Where an imported dma-buf's planes are: byte offset and row pitch of each.
struct PlaneLayout {
Uint32 Count = 1;
Uint32 Offset[2] = {0, 0};
Uint32 Pitch[2] = {0, 0};
};
struct Image {
Uint64 Id = 0;
@@ -61,6 +82,11 @@ namespace MobileGL::MG_Remote::Server::SharedImages {
Uint32 Stride = 0; // bytes
Uint32 Offset = 0;
Uint64 Modifier = kModifierInvalid;
// A YUV image's second plane (Cb Cr): same descriptor, this offset and pitch. The layout is
// still the allocator's - these are what it reported when the buffer was locked, so a
// client can name the plane, not a promise that the descriptor maps that way.
Uint32 Plane1Stride = 0;
Uint32 Plane1Offset = 0;
// AHardwareBuffer* on Android; null on a host, where the memory is a memfd (unit tests).
void* Native = nullptr;
// The descriptor clients receive duplicates of. Owned.
@@ -77,6 +103,31 @@ namespace MobileGL::MG_Remote::Server::SharedImages {
mutable Uint64 WriteGeneration = 0;
mutable Vector<int> ReadFences;
// FOREIGN YUV - THE CPU-COPY FALLBACK. A YUV dma-buf this server did not allocate (a video
// decoder's frame) cannot be imported vendor-neutrally: neither the platform's EGL nor its
// Vulkan takes a raw dma-buf. Such an import becomes a YUV image of this server's (Native,
// an AHardwareBuffer the CPU can write) whose content is COPIED from a CPU mapping of the
// foreign buffer (RefreshForeign) at a reader's first use of it in each of its frames - the
// moments the producer's content can have moved for that reader. Every other image is
// zero-copy; this one costs a frame's worth of memcpy per refresh, and its log says so.
struct ForeignSource {
int Fd = -1; // a duplicate, owned
PlaneLayout Layout;
void* Map = nullptr;
SizeT MapSize = 0;
std::mutex Mutex; // one refresh at a time
Uint64 Copies = 0;
// The foreign buffer's identity, under which a later import of it finds this image.
Uint64 IdentityDev = 0;
Uint64 IdentityIno = 0;
};
UniquePtr<ForeignSource> Foreign;
Bool IsForeign() const { return Foreign != nullptr; }
// How the samples of a YUV image convert to RGB: the EGL_EXT_image_dma_buf_import hints
// of its last import. Guarded by SyncMutex.
mutable Yuv::Hints YuvHints;
Image() = default;
Image(const Image&) = delete;
Image& operator=(const Image&) = delete;
@@ -91,6 +142,22 @@ namespace MobileGL::MG_Remote::Server::SharedImages {
// this server never exported, or one whose identity the kernel does not keep unique.
ImageRef Identify(int fd, std::string& why);
// A YUV import of a dma-buf this server did not export (Image::ForeignSource): the live
// foreign image already made from the same buffer with the same shape, or a new one, which
// keeps a duplicate of `fd`. Null with `why` when the buffer cannot be mapped, its planes do
// not fit in it, or no YUV image can be allocated to copy it into.
ImageRef ImportForeignYuv(int fd, Uint32 width, Uint32 height, Uint32 fourcc, const PlaneLayout& layout,
std::string& why);
// A foreign image's content brought up to date from its source (the copy). Waits for the
// image's pending reads first, so a frame still sampling the old content keeps it. True for an
// image that is not foreign: there is nothing to do.
Bool RefreshForeign(const Image& image, std::string& why);
// The colour hints a YUV image is converted with.
void SetYuvHints(const Image& image, const Yuv::Hints& hints);
Yuv::Hints GetYuvHints(const Image& image);
// The live image with this id, or null.
ImageRef Find(Uint64 id);
+158
View File
@@ -0,0 +1,158 @@
// MobileGL - MobileGL/MG_Remote/Server/SharedImageYuv.h
// Copyright (c) 2025-2026 MobileGL-Dev
// Licensed under the GNU Lesser General Public License v3.0:
// https://www.gnu.org/licenses/gpl-3.0.txt
// https://www.gnu.org/licenses/lgpl-3.0.txt
// SPDX-License-Identifier: LGPL-3.0-only
// End of Source File Header
// YUV SHARED IMAGES: the pure parts - plane copies and the YCbCr -> RGB conversion an image's
// colour hints select - kept free of Android and of any backend so the host tests can check them.
//
// A YUV image is sampled as RGBA: whoever binds it to a texture gets the image CONVERTED into the
// texture's own RGBA8 storage at its first use in each of the reader's frames (the backends do
// that on the GPU, from the image's AHardwareBuffer). The conversion is the one the EGLImage's
// EGL_EXT_image_dma_buf_import hints name - colour space, sample range and chroma siting - with
// that extension's defaults (ITU-R BT.601, narrow range, siting 0) where a hint is absent.
#pragma once
#include <Includes.h>
#include <cstring>
namespace MobileGL::MG_Remote::Server::SharedImages::Yuv {
// EGL_EXT_image_dma_buf_import's hint values.
inline constexpr Uint32 kEglItuRec601 = 0x327F;
inline constexpr Uint32 kEglItuRec709 = 0x3280;
inline constexpr Uint32 kEglItuRec2020 = 0x3281;
inline constexpr Uint32 kEglYuvFullRange = 0x3282;
inline constexpr Uint32 kEglYuvNarrowRange = 0x3283;
inline constexpr Uint32 kEglYuvChromaSiting0 = 0x3284;
inline constexpr Uint32 kEglYuvChromaSiting0_5 = 0x3285;
// What an import said about its samples. 0 = not given (the extension's default applies).
struct Hints {
Uint32 ColorSpace = 0;
Uint32 Range = 0;
Uint32 SitingX = 0;
Uint32 SitingY = 0;
Uint32 EffectiveColorSpace() const {
return ColorSpace == kEglItuRec709 || ColorSpace == kEglItuRec2020 ? ColorSpace : kEglItuRec601;
}
Bool FullRange() const { return Range == kEglYuvFullRange; }
Bool MidpointX() const { return SitingX == kEglYuvChromaSiting0_5; }
Bool MidpointY() const { return SitingY == kEglYuvChromaSiting0_5; }
Bool operator==(const Hints& other) const {
return ColorSpace == other.ColorSpace && Range == other.Range && SitingX == other.SitingX &&
SitingY == other.SitingY;
}
};
// rgb = Matrix * (ycbcr - Offset), every value normalized to [0, 1] as a UNORM texture of the
// image's sample size returns it. Matrix is COLUMN-major (a GLSL mat3 uniform as is).
struct Conversion {
float Matrix[9];
float Offset[3];
};
// `sampleMax` is what a full-scale code reads as before normalization: 255 for 8-bit samples,
// 65535/64 for P010 (10 bits in the high end of 16, read as UNORM16).
inline Conversion ConversionFor(const Hints& hints, double sampleMax) {
double kr = 0.299, kb = 0.114;
if (hints.EffectiveColorSpace() == kEglItuRec709) {
kr = 0.2126;
kb = 0.0722;
} else if (hints.EffectiveColorSpace() == kEglItuRec2020) {
kr = 0.2627;
kb = 0.0593;
}
const double kg = 1.0 - kr - kb;
// Code values of an 8-bit-equivalent scale, multiplied up for deeper samples.
const double scale = sampleMax / 255.0;
double yOffset = 0.0, yScale = 1.0, cScale = 1.0;
const double cOffset = 128.0 * scale / sampleMax;
if (!hints.FullRange()) {
yOffset = 16.0 * scale / sampleMax;
yScale = sampleMax / (219.0 * scale);
cScale = sampleMax / (224.0 * scale);
}
Conversion c{};
// Columns: Y, Cb, Cr.
const double m[9] = {
yScale,
yScale,
yScale,
0.0,
-cScale * 2.0 * kb * (1.0 - kb) / kg,
cScale * 2.0 * (1.0 - kb),
cScale * 2.0 * (1.0 - kr),
-cScale * 2.0 * kr * (1.0 - kr) / kg,
0.0,
};
for (SizeT i = 0; i < 9; ++i) c.Matrix[i] = static_cast<float>(m[i]);
c.Offset[0] = static_cast<float>(yOffset);
c.Offset[1] = static_cast<float>(cOffset);
c.Offset[2] = static_cast<float>(cOffset);
return c;
}
// One component plane of a destination: where (x, y)'s sample of it lives is
// Data + y * RowStride + x * PixelStride.
struct PlaneView {
Uint8* Data = nullptr;
Int64 RowStride = 0;
Uint32 PixelStride = 0;
};
struct PlanarLayout {
PlaneView Y, Cb, Cr;
};
// A two-plane semi-planar source (NV12: Y, then Cb Cr interleaved at half resolution; P010
// the same with 16-bit samples, `sampleBytes` = 2) into any destination layout: semi-planar
// Cb-first rows are copied as they are, everything else (Cr first, fully planar) sample by
// sample. False when the destination cannot hold the samples (null planes, a pixel stride
// smaller than a sample).
inline Bool CopySemiPlanar(const Uint8* srcY, Int64 srcYPitch, const Uint8* srcCbCr, Int64 srcCbCrPitch,
Uint32 width, Uint32 height, Uint32 sampleBytes, const PlanarLayout& dst) {
if (srcY == nullptr || srcCbCr == nullptr || dst.Y.Data == nullptr || dst.Cb.Data == nullptr ||
dst.Cr.Data == nullptr || (sampleBytes != 1 && sampleBytes != 2) || dst.Y.PixelStride < sampleBytes ||
dst.Cb.PixelStride < sampleBytes || dst.Cr.PixelStride < sampleBytes)
return false;
const Uint32 chromaWidth = (width + 1) / 2;
const Uint32 chromaHeight = (height + 1) / 2;
// Luma.
for (Uint32 y = 0; y < height; ++y) {
const Uint8* from = srcY + static_cast<Int64>(y) * srcYPitch;
Uint8* to = dst.Y.Data + static_cast<Int64>(y) * dst.Y.RowStride;
if (dst.Y.PixelStride == sampleBytes) {
std::memcpy(to, from, static_cast<SizeT>(width) * sampleBytes);
} else {
for (Uint32 x = 0; x < width; ++x)
std::memcpy(to + static_cast<SizeT>(x) * dst.Y.PixelStride, from + static_cast<SizeT>(x) * sampleBytes,
sampleBytes);
}
}
// Chroma.
const Bool sameInterleave = dst.Cb.PixelStride == 2 * sampleBytes && dst.Cr.PixelStride == 2 * sampleBytes &&
dst.Cr.Data == dst.Cb.Data + sampleBytes && dst.Cr.RowStride == dst.Cb.RowStride;
for (Uint32 y = 0; y < chromaHeight; ++y) {
const Uint8* from = srcCbCr + static_cast<Int64>(y) * srcCbCrPitch;
if (sameInterleave) {
std::memcpy(dst.Cb.Data + static_cast<Int64>(y) * dst.Cb.RowStride, from,
static_cast<SizeT>(chromaWidth) * 2 * sampleBytes);
continue;
}
Uint8* cb = dst.Cb.Data + static_cast<Int64>(y) * dst.Cb.RowStride;
Uint8* cr = dst.Cr.Data + static_cast<Int64>(y) * dst.Cr.RowStride;
for (Uint32 x = 0; x < chromaWidth; ++x) {
const Uint8* pair = from + static_cast<SizeT>(x) * 2 * sampleBytes;
std::memcpy(cb + static_cast<SizeT>(x) * dst.Cb.PixelStride, pair, sampleBytes);
std::memcpy(cr + static_cast<SizeT>(x) * dst.Cr.PixelStride, pair + sampleBytes, sampleBytes);
}
}
return true;
}
} // namespace MobileGL::MG_Remote::Server::SharedImages::Yuv
+4 -1
View File
@@ -274,7 +274,10 @@ namespace MobileGL::MG_Remote::Transport {
for (;;) {
// Announce, FENCE, then re-test. The fence is the mechanism -
// see the file header - so setting the flag itself is relaxed.
parked.store(1, std::memory_order_relaxed);
// Release, so a notifier that acquires this 1 also sees what the waiter wrote
// before parking (RingControl::producerWakeSeq). The fence below is still what
// closes the lost-wakeup window.
parked.store(1, std::memory_order_release);
std::atomic_thread_fence(std::memory_order_seq_cst);
if (ready()) {
parked.store(0, std::memory_order_relaxed);
+24 -1
View File
@@ -497,11 +497,24 @@ namespace MobileGL::MG_Remote::Transport {
return m_selfBell->Dead() ? SessionWait::ShutDown : SessionWait::TimedOut;
}
// The wake target for the duration of one wait; 0 again afterwards, so a later wait that
// needs any progress (or one outside this class on the same flag) is rung as before.
namespace {
struct ScopedWakeTarget {
std::atomic<std::uint64_t>& target;
ScopedWakeTarget(std::atomic<std::uint64_t>& t, std::uint64_t seq) : target(t) {
target.store(seq, std::memory_order_seq_cst);
}
~ScopedWakeTarget() { target.store(0, std::memory_order_seq_cst); }
};
} // namespace
SessionWait SessionProducer::WaitForApplied(std::uint64_t seq, std::uint32_t timeoutMs) {
if (!Valid()) {
return SessionWait::TimedOut;
}
RingControl* control = m_control;
const ScopedWakeTarget wake(control->producerWakeSeq, seq);
return Park([control, seq] { return Watermark::Reached(control->Progress.appliedSeq, seq); },
timeoutMs);
}
@@ -527,6 +540,7 @@ namespace MobileGL::MG_Remote::Transport {
return SessionWait::TimedOut;
}
RingControl* control = m_control;
const ScopedWakeTarget wake(control->producerWakeSeq, seq);
return Park(
[control, seq] {
return Watermark::Reached(control->Progress.appliedSeq, seq) ||
@@ -670,7 +684,16 @@ namespace MobileGL::MG_Remote::Transport {
void SessionConsumer::NotifyClient() {
if (m_link) m_link->ProgressChanged();
if (m_control != nullptr && m_peerBell != nullptr) {
NotifyIfParked(*m_peerBell, m_control->producerParked);
// NotifyIfParked, narrowed by the client's wake target: a client parked for an
// applied sequence the server has not reached yet is not rung (unless the event
// ring filled, which that wait also wakes for).
std::atomic_thread_fence(std::memory_order_seq_cst);
if (m_control->producerParked.load(std::memory_order_acquire) == 0) return;
const std::uint64_t target = m_control->producerWakeSeq.load(std::memory_order_seq_cst);
if (target != 0 && !Watermark::Reached(m_control->Progress.appliedSeq, target) &&
m_control->eventRingFull.load(std::memory_order_acquire) == 0)
return;
m_peerBell->Notify();
}
}
+14
View File
@@ -164,6 +164,16 @@ namespace MobileGL::MG_Remote::Transport {
std::atomic<std::uint32_t> producerParked; // client asleep, server must ring
std::atomic<std::uint32_t> eventRingFull; // SEG_EVENT full, server stopped applying
std::atomic<std::uint32_t> eventDropped; // dropped lossy events
// ---- the parked producer's wake target -------------------------------
//
// What a parked client is waiting for, so the server rings only when it is there: 0 =
// any progress (present credit, ring space, events), otherwise the appliedSeq it needs.
// Without it every record the server applied while the client waited for a later one
// was a socket write and a futile wake-up. Written by the client before it parks (and
// reset to 0 after), read by the server only after it saw producerParked set; on a link
// without this shared page it stays 0, which is the old ring-on-any-progress rule.
alignas(64) std::atomic<std::uint64_t> producerWakeSeq;
};
static_assert(sizeof(RingControl) == 4096, "RingControl must be exactly one page");
@@ -232,6 +242,10 @@ namespace MobileGL::MG_Remote::Transport {
// The page's real content must fit the page it claims. sizeof(RingControl) == 4096
// above cannot catch a member that grew, because alignas(4096) forces that number;
// the struct's content ends far short of it and the tail is padding.
static_assert(offsetof(RingControl, producerWakeSeq) / 64 != offsetof(RingControl, serverEpoch) / 64,
"the wake target has a line of its own");
static_assert(offsetof(RingControl, producerWakeSeq) + sizeof(std::uint64_t) <= 4096,
"RingControl's members must fit the page it claims to be");
static_assert(offsetof(RingControl, eventDropped) + sizeof(std::uint32_t) <= 4096,
"RingControl's members must fit the page it claims to be");
static_assert(alignof(RingControl) == 4096, "RingControl must be page aligned");
+9 -1
View File
@@ -73,6 +73,7 @@
#include <atomic>
#include <cstdint>
#include <type_traits>
namespace MobileGL::MG_Remote::Transport {
@@ -462,7 +463,14 @@ namespace MobileGL::MG_Remote::Transport {
if (!m_cmd->Pop(view, outCorrupt)) {
return false;
}
apply(view);
// A terminal rejection after Pop must not acknowledge an unapplied record.
// A bool callback returning false ends this consumer's session; it cannot retry
// the popped record. Existing void callbacks retain their original semantics.
if constexpr (std::is_same_v<std::invoke_result_t<Apply, const RingRecordView&>, bool>) {
if (!apply(view)) return false;
} else {
apply(view);
}
++m_appliedSeq;
Watermark::AdvanceApplied(*m_control, m_appliedSeq);
m_cmd->PublishApplied();
+95 -37
View File
@@ -1874,9 +1874,14 @@ namespace MobileGL {
}
Bool EGLContext::IsDmaBufFourccSupported(Uint32 fourcc) {
// The four orders a shared image can be allocated under (SharedImageApi.h).
// The four orders a shared image can be allocated under (SharedImageApi.h), and the
// two YUV ones that are sampled only.
return fourcc == kDrmFourccAbgr8888 || fourcc == kDrmFourccXbgr8888 || fourcc == kDrmFourccArgb8888 ||
fourcc == kDrmFourccXrgb8888;
fourcc == kDrmFourccXrgb8888 || IsDmaBufFourccYuv(fourcc);
}
Bool EGLContext::IsDmaBufFourccYuv(Uint32 fourcc) {
return fourcc == kDrmFourccNv12 || fourcc == kDrmFourccP010;
}
Bool EGLContext::PrepareDmaBufImport(EGLDisplayHandle display, EGLContextHandle context,
@@ -1899,9 +1904,11 @@ namespace MobileGL {
}
DmaBufImportAttribs attribs;
Bool hasWidth = false, hasHeight = false, hasFourcc = false, hasFd = false, hasOffset = false,
hasPitch = false, hasModifierLo = false, hasModifierHi = false, otherPlane = false;
Uint64 modifierLo = 0, modifierHi = 0;
Bool hasWidth = false, hasHeight = false, hasFourcc = false;
// Per plane: fd, offset, pitch, modifier lo, modifier hi.
Bool has[2][5] = {};
Uint64 modifierLo[2] = {0, 0}, modifierHi[2] = {0, 0};
Bool laterPlane = false;
for (SizeT i = 0; attribList != nullptr && attribList[i] != EGL_NONE; i += 2) {
const EGLAttrib value = attribList[i + 1];
switch (attribList[i]) {
@@ -1919,29 +1926,44 @@ namespace MobileGL {
break;
case EGL_DMA_BUF_PLANE0_FD_EXT:
attribs.Fd = static_cast<int>(value);
hasFd = true;
has[0][0] = true;
break;
case EGL_DMA_BUF_PLANE0_OFFSET_EXT:
attribs.Offset = static_cast<EGLint>(value);
hasOffset = true;
has[0][1] = true;
break;
case EGL_DMA_BUF_PLANE0_PITCH_EXT:
attribs.Pitch = static_cast<EGLint>(value);
hasPitch = true;
has[0][2] = true;
break;
case EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT:
modifierLo = static_cast<Uint32>(value);
hasModifierLo = true;
modifierLo[0] = static_cast<Uint32>(value);
has[0][3] = true;
break;
case EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT:
modifierHi = static_cast<Uint32>(value);
hasModifierHi = true;
modifierHi[0] = static_cast<Uint32>(value);
has[0][4] = true;
break;
case EGL_DMA_BUF_PLANE1_FD_EXT:
attribs.Plane1Fd = static_cast<int>(value);
has[1][0] = true;
break;
case EGL_DMA_BUF_PLANE1_OFFSET_EXT:
attribs.Plane1Offset = static_cast<EGLint>(value);
has[1][1] = true;
break;
case EGL_DMA_BUF_PLANE1_PITCH_EXT:
attribs.Plane1Pitch = static_cast<EGLint>(value);
has[1][2] = true;
break;
case EGL_DMA_BUF_PLANE1_MODIFIER_LO_EXT:
modifierLo[1] = static_cast<Uint32>(value);
has[1][3] = true;
break;
case EGL_DMA_BUF_PLANE1_MODIFIER_HI_EXT:
modifierHi[1] = static_cast<Uint32>(value);
has[1][4] = true;
break;
case EGL_DMA_BUF_PLANE2_FD_EXT:
case EGL_DMA_BUF_PLANE2_OFFSET_EXT:
case EGL_DMA_BUF_PLANE2_PITCH_EXT:
@@ -1952,14 +1974,22 @@ namespace MobileGL {
case EGL_DMA_BUF_PLANE3_PITCH_EXT:
case EGL_DMA_BUF_PLANE3_MODIFIER_LO_EXT:
case EGL_DMA_BUF_PLANE3_MODIFIER_HI_EXT:
otherPlane = true;
laterPlane = true;
break;
// Hints for YUV formats, and EGL_KHR_image_base's own attribute: meaningless
// for a single-plane RGB image, accepted and ignored.
// The YUV hints: kept for a YUV import, which is converted with them; meaningless
// for an RGB one and ignored there. EGL_KHR_image_base's own attribute likewise.
case EGL_YUV_COLOR_SPACE_HINT_EXT:
attribs.ColorSpace = static_cast<Uint32>(value);
break;
case EGL_SAMPLE_RANGE_HINT_EXT:
attribs.Range = static_cast<Uint32>(value);
break;
case EGL_YUV_CHROMA_HORIZONTAL_SITING_HINT_EXT:
attribs.SitingX = static_cast<Uint32>(value);
break;
case EGL_YUV_CHROMA_VERTICAL_SITING_HINT_EXT:
attribs.SitingY = static_cast<Uint32>(value);
break;
case EGL_IMAGE_PRESERVED_KHR:
break;
default:
@@ -1967,8 +1997,8 @@ namespace MobileGL {
return false;
}
}
if (!hasWidth || !hasHeight || !hasFourcc || !hasFd || !hasOffset || !hasPitch ||
hasModifierLo != hasModifierHi) {
if (!hasWidth || !hasHeight || !hasFourcc || !has[0][0] || !has[0][1] || !has[0][2] ||
has[0][3] != has[0][4]) {
SetError(EGL_BAD_PARAMETER);
return false;
}
@@ -1976,23 +2006,37 @@ namespace MobileGL {
SetError(EGL_BAD_MATCH);
return false;
}
// Every supported format has exactly one plane.
if (otherPlane) {
const Bool yuv = IsDmaBufFourccYuv(attribs.Fourcc);
attribs.PlaneCount = yuv ? 2u : 1u;
const Bool anyPlane1 = has[1][0] || has[1][1] || has[1][2] || has[1][3] || has[1][4];
// Planes the format does not have are an attribute error; planes it has but the
// list leaves out (or half a modifier) a parameter error.
if (laterPlane || (!yuv && anyPlane1)) {
SetError(EGL_BAD_ATTRIBUTE);
return false;
}
if (attribs.Width <= 0 || attribs.Height <= 0 || attribs.Fd < 0) {
if (yuv && (!has[1][0] || !has[1][1] || !has[1][2] || has[1][3] != has[1][4] || has[1][3] != has[0][3])) {
SetError(EGL_BAD_PARAMETER);
return false;
}
if (attribs.Offset < 0 || attribs.Pitch <= 0) {
if (attribs.Width <= 0 || attribs.Height <= 0 || attribs.Fd < 0 || (yuv && attribs.Plane1Fd < 0)) {
SetError(EGL_BAD_PARAMETER);
return false;
}
if (attribs.Offset < 0 || attribs.Pitch <= 0 || (yuv && (attribs.Plane1Offset < 0 || attribs.Plane1Pitch <= 0))) {
SetError(EGL_BAD_ACCESS);
return false;
}
// Any modifier is taken: the image is identified by its descriptor, never read
// through the layout a modifier describes.
attribs.HasModifier = hasModifierLo;
attribs.Modifier = (modifierHi << 32) | modifierLo;
// Every plane names the one modifier of the buffer.
if (yuv && has[0][3] && (modifierLo[0] != modifierLo[1] || modifierHi[0] != modifierHi[1])) {
SetError(EGL_BAD_MATCH);
return false;
}
// An RGBA image is identified by its descriptor, never read through the layout a
// modifier describes, so any modifier is taken; a YUV one may be a buffer the server
// copies (a foreign one), which it reads as linear rows - the server decides.
attribs.HasModifier = has[0][3];
attribs.Modifier = (modifierHi[0] << 32) | modifierLo[0];
if (out != nullptr) *out = attribs;
return true;
}
@@ -2034,8 +2078,8 @@ namespace MobileGL {
return orphaned;
}
Bool EGLContext::QueryDmaBufFormats(EGLDisplayHandle display, Bool available, EGLint maxFormats,
EGLint* formats, EGLint* numFormats) {
Bool EGLContext::QueryDmaBufFormats(EGLDisplayHandle display, Bool available, Bool yuvAvailable,
EGLint maxFormats, EGLint* formats, EGLint* numFormats) {
const std::lock_guard<std::recursive_mutex> lock(m_mutex);
if (!ValidateDisplay(display)) {
SetError(EGL_BAD_DISPLAY);
@@ -2050,8 +2094,10 @@ namespace MobileGL {
return false;
}
static constexpr Uint32 kFormats[] = {kDrmFourccAbgr8888, kDrmFourccXbgr8888, kDrmFourccArgb8888,
kDrmFourccXrgb8888};
const EGLint total = available ? static_cast<EGLint>(std::size(kFormats)) : 0;
kDrmFourccXrgb8888, kDrmFourccNv12, kDrmFourccP010};
constexpr EGLint kRgbCount = 4;
const EGLint total =
!available ? 0 : (yuvAvailable ? static_cast<EGLint>(std::size(kFormats)) : kRgbCount);
if (maxFormats == 0) {
*numFormats = total;
return true;
@@ -2062,11 +2108,10 @@ namespace MobileGL {
return true;
}
Bool EGLContext::QueryDmaBufModifiers(EGLDisplayHandle display, Bool available, EGLint format,
EGLint maxModifiers, Uint64* modifiers,
Bool EGLContext::QueryDmaBufModifiers(EGLDisplayHandle display, Bool available, Bool yuvAvailable,
EGLint format, EGLint maxModifiers, Uint64* modifiers,
EGLBoolean* externalOnly, EGLint* numModifiers) {
const std::lock_guard<std::recursive_mutex> lock(m_mutex);
(void)externalOnly;
if (!ValidateDisplay(display)) {
SetError(EGL_BAD_DISPLAY);
return false;
@@ -2075,14 +2120,27 @@ namespace MobileGL {
SetError(EGL_NOT_INITIALIZED);
return false;
}
if (!available || !IsDmaBufFourccSupported(static_cast<Uint32>(format)) || maxModifiers < 0 ||
numModifiers == nullptr || (maxModifiers > 0 && modifiers == nullptr)) {
const Bool yuv = IsDmaBufFourccYuv(static_cast<Uint32>(format));
if (!available || !IsDmaBufFourccSupported(static_cast<Uint32>(format)) || (yuv && !yuvAvailable) ||
maxModifiers < 0 || numModifiers == nullptr || (maxModifiers > 0 && modifiers == nullptr)) {
SetError(EGL_BAD_PARAMETER);
return false;
}
// No explicit modifiers: an image's layout is its allocator's, which is what an
// import without modifier attributes (the implicit modifier) means.
*numModifiers = 0;
if (!yuv) {
// No explicit modifiers: an image's layout is its allocator's, which is what an
// import without modifier attributes (the implicit modifier) means.
*numModifiers = 0;
return true;
}
// A YUV buffer is taken as linear rows when it is not one of the server's own (which
// are identified, whatever modifier is named), and only ever sampled: external only.
if (maxModifiers == 0) {
*numModifiers = 1;
return true;
}
modifiers[0] = 0; // DRM_FORMAT_MOD_LINEAR
if (externalOnly != nullptr) externalOnly[0] = EGL_TRUE;
*numModifiers = 1;
return true;
}
} // namespace EGLState
+22 -6
View File
@@ -200,6 +200,15 @@ namespace MobileGL {
EGLint Pitch = 0;
Bool HasModifier = false;
Uint64 Modifier = 0;
// YUV (two planes): the second plane, and the colour hints (0 = not given).
Uint32 PlaneCount = 1;
int Plane1Fd = -1;
EGLint Plane1Offset = 0;
EGLint Plane1Pitch = 0;
Uint32 ColorSpace = 0;
Uint32 Range = 0;
Uint32 SitingX = 0;
Uint32 SitingY = 0;
};
struct SharedImageInfo {
Uint64 Id = 0;
@@ -215,11 +224,16 @@ namespace MobileGL {
static constexpr Uint32 kDrmFourccXbgr8888 = 0x34324258u; // 'XB24'
static constexpr Uint32 kDrmFourccArgb8888 = 0x34325241u; // 'AR24'
static constexpr Uint32 kDrmFourccXrgb8888 = 0x34325258u; // 'XR24'
// Two-plane YUV 4:2:0, sampled only (through GL_OES_EGL_image_external, or as RGBA).
static constexpr Uint32 kDrmFourccNv12 = 0x3231564Eu; // 'NV12'
static constexpr Uint32 kDrmFourccP010 = 0x30313050u; // 'P010'
static Bool IsDmaBufFourccSupported(Uint32 fourcc);
static Bool IsDmaBufFourccYuv(Uint32 fourcc);
// The checks eglCreateImage owes an EGL_LINUX_DMA_BUF_EXT target before anything
// is imported: the display, a null context and buffer, and a complete single-plane
// attribute list of a supported format. False with the EGL error set.
// is imported: the display, a null context and buffer, and a complete attribute
// list of a supported format - one plane, or two for YUV. False with the EGL error
// set. Whether the YUV formats can be served at all is the caller's question.
Bool PrepareDmaBufImport(EGLDisplayHandle display, EGLContextHandle context, EGLClientBuffer buffer,
const EGLAttrib* attribList, DmaBufImportAttribs* out);
// An EGLImage naming an imported shared image.
@@ -230,10 +244,12 @@ namespace MobileGL {
// Shared images whose EGLImages went with an eglTerminate: the caller releases them.
Vector<Uint64> TakeOrphanedSharedImages();
// eglQueryDmaBufFormatsEXT / eglQueryDmaBufModifiersEXT. `available` is whether the
// backend has shared images at all; without them the lists are empty.
Bool QueryDmaBufFormats(EGLDisplayHandle display, Bool available, EGLint maxFormats, EGLint* formats,
EGLint* numFormats);
Bool QueryDmaBufModifiers(EGLDisplayHandle display, Bool available, EGLint format,
// backend has shared images at all; without them the lists are empty. `yuvAvailable`
// adds the YUV formats, which are external-only (OES_EGL_image_external) and LINEAR
// as far as an explicit modifier goes.
Bool QueryDmaBufFormats(EGLDisplayHandle display, Bool available, Bool yuvAvailable,
EGLint maxFormats, EGLint* formats, EGLint* numFormats);
Bool QueryDmaBufModifiers(EGLDisplayHandle display, Bool available, Bool yuvAvailable, EGLint format,
EGLint maxModifiers, Uint64* modifiers, EGLBoolean* externalOnly,
EGLint* numModifiers);
@@ -1875,8 +1875,25 @@ namespace MobileGL::MG_State::GLState {
artifacts.uniformReflection.reserve(static_cast<SizeT>(uniformCount));
artifacts.uniformIndexByName.clear();
artifacts.uniformIndexByName.reserve(static_cast<SizeT>(uniformCount));
// GL_OES_EGL_image_external: the uniforms a stage declared samplerExternalOES were
// compiled as sampler2D (LowerExternalSamplers) and are reported with their own type,
// which is what sends their units to the external binding point.
Vector<String> externalSamplers;
for (const LinkShaderInput& shader : in.shaders) {
if (shader.compiled == nullptr) continue;
for (String& name :
MG_Util::ShaderTranspiler::ExternalSamplerUniformNames(CompiledArtifacts(shader.compiled).preprocessedSource))
externalSamplers.push_back(Move(name));
}
const auto isExternalSampler = [&externalSamplers](const String& reflected) {
if (externalSamplers.empty()) return false;
const String base = reflected.substr(0, reflected.find('['));
return std::find(externalSamplers.begin(), externalSamplers.end(), base) != externalSamplers.end();
};
for (Int i = 0; i < uniformCount; ++i) {
ProgramObject::UniformReflection record = MakeResourceReflection(program.getUniform(i));
if (record.glDefineType == GL_SAMPLER_2D && isExternalSampler(record.name))
record.glDefineType = GL_SAMPLER_EXTERNAL_OES;
// A block-level layout(row_major)/(column_major) that the member did not inherit
// in its own qualifier. Resolved once HERE rather than at every GL_UNIFORM_* query,
// which is what the getUniformBlock() fallback in the old accessors was doing.
@@ -43,6 +43,8 @@ namespace MobileGL::MG_State::GLState {
return MakeShared<TextureObjectCubeMapArray>(index);
case TextureTarget::Texture2DMultisampleArray:
return MakeShared<TextureObject2DMultisampleArray>(index);
case TextureTarget::External:
return MakeShared<TextureObjectExternal>(index);
default:
MOBILEGL_ASSERT(false, "Unimplemented texture type when creating texture object!: %d", (int)target);
return nullptr;
@@ -185,4 +187,26 @@ namespace MobileGL::MG_State::GLState {
Bool ShareGroupState::ValidateRenderbufferObject(Uint index) const {
return m_renderbufferObjects.find(index) != m_renderbufferObjects.end();
}
UniquePtr<BufferObject> ShareGroupState::TakeClientArrayBuffer() {
const std::lock_guard<std::mutex> lock(m_clientArrayBuffersMutex);
if (m_clientArrayBuffers.empty()) return nullptr;
UniquePtr<BufferObject> buffer = std::move(m_clientArrayBuffers.back());
m_clientArrayBuffers.pop_back();
return buffer;
}
void ShareGroupState::ReturnClientArrayBuffer(UniquePtr<BufferObject> buffer) {
// Enough for a draw's every attribute plus its indices; more is never in use at once.
constexpr SizeT kKeptClientArrayBuffers = 48;
if (!buffer) return;
{
const std::lock_guard<std::mutex> lock(m_clientArrayBuffersMutex);
if (m_clientArrayBuffers.size() < kKeptClientArrayBuffers) {
m_clientArrayBuffers.push_back(std::move(buffer));
return;
}
}
// A surplus buffer is destroyed outside the lock (its destructor emits).
}
} // namespace MobileGL::MG_State::GLState
@@ -8,6 +8,7 @@
#pragma once
#include <Includes.h>
#include <mutex>
#include <MG_Util/Miscellany/IndexGenerator.h>
#include "BufferState/BufferObject.h"
#include "ProgramState/ProgramState.h"
@@ -67,6 +68,12 @@ namespace MobileGL::MG_State::GLState {
ProgramState& Programs() { return m_programState; }
const ProgramState& Programs() const { return m_programState; }
// Unnamed buffers that carried a draw's client-memory arrays, kept for the next such draw
// so it respecifies one of them (a non-blocking record) instead of creating a resource
// (a round trip) and destroying it again. Buffers are share-group objects, hence here.
UniquePtr<BufferObject> TakeClientArrayBuffer();
void ReturnClientArrayBuffer(UniquePtr<BufferObject> buffer);
private:
Uint64 m_token = 0;
UnorderedMap<Uint, SharedPtr<BufferObject>> m_bufferObjects;
@@ -77,5 +84,7 @@ namespace MobileGL::MG_State::GLState {
IndexGenerator<Uint> m_renderbufferNames{1024, 1};
SamplerState m_samplerState;
ProgramState m_programState;
std::mutex m_clientArrayBuffersMutex;
Vector<UniquePtr<BufferObject>> m_clientArrayBuffers;
};
} // namespace MobileGL::MG_State::GLState
@@ -21,6 +21,10 @@ namespace MobileGL {
Texture2DArray,
TextureCubeMapArray,
Texture2DMultisampleArray,
// GL_OES_EGL_image_external: a 2D texture whose only storage is an EGLImage (a YUV one is
// converted to RGBA by the backend), with its own binding point on every unit. Below the
// frontend it IS a 2D texture: the pipe and the backends never see this target.
External,
TextureTargetCount,
Unknown = -1
};
@@ -127,7 +127,9 @@ namespace MobileGL {
TextureObjectBase::TextureObjectBase(TextureTarget target, Uint externalIndex)
: m_externalIndex(externalIndex), m_lifetimeId(AllocateLifetimeId()), m_target(target) {
m_sampler = MakeShared<SamplerObject>(0);
if (target == TextureTarget::TextureRectangle) {
if (target == TextureTarget::TextureRectangle || target == TextureTarget::External) {
// (An external texture, GL_OES_EGL_image_external, starts the same way: LINEAR
// and CLAMP_TO_EDGE, and it has no mip chain either.)
// A rectangle texture has no mip chain, so its initial sampler state is not
// the shared one: TEXTURE_MIN_FILTER is LINEAR and TEXTURE_WRAP_S/T are
// CLAMP_TO_EDGE (GL 4.6 core table 23.15). Leaving the 2D default of
@@ -53,6 +53,10 @@ namespace MobileGL {
STUB_TEXTURE_OBJECT_CLASS_DEFINITION(TextureObject2DMultisampleArray,
TextureTarget::Texture2DMultisampleArray,
{TextureUploadTarget::Texture2DMultisampleArray});
// GL_OES_EGL_image_external: level 0 is defined as a 2D image (by glEGLImageTargetTexture2DOES
// only - glTexImage2D does not take the target).
STUB_TEXTURE_OBJECT_CLASS_DEFINITION(TextureObjectExternal, TextureTarget::External,
{TextureUploadTarget::Texture2D});
#undef STUB_TEXTURE_OBJECT_CLASS_DEFINITION
} // namespace GLState
@@ -0,0 +1,177 @@
// MobileGL - MobileGL/MG_Test/Backend/GpuHangWatchTest.cpp
// Copyright (c) 2025-2026 MobileGL-Dev
// Licensed under the GNU Lesser General Public License v3.0:
// https://www.gnu.org/licenses/gpl-3.0.txt
// https://www.gnu.org/licenses/lgpl-3.0.txt
// SPDX-License-Identifier: LGPL-3.0-only
// End of Source File Header
// THE GPU HANG WATCH (MG_Backend/GpuHangWatch.h): which session's submission stops the GPU.
//
// While one submission hangs, EVERY session's work is pending - the compositor's included - so the
// attribution may only rest on "the GPU started it and it did not finish". These cases pin that:
// * the submission seen running past the budget is named, and only once;
// * a submission queued behind it (never started) is never named, however long it waits - red with
// the Detector counting pending-but-not-started work as running (the compositor would be blamed);
// * a long but finishing submission is never named;
// * the budget runs from the first sample that saw it running, not from submission;
// * on the watch thread: a fake device whose submission never finishes is named within budget plus
// one interval, an innocent device waiting behind it is not, and Unregister waits out a sample.
#include <gtest/gtest.h>
#include <MG_Backend/GpuHangWatch.h>
#include <atomic>
#include <chrono>
#include <thread>
using namespace MobileGL;
namespace Watch = MobileGL::MG_Backend::GpuHangWatch;
namespace {
Vector<Watch::SubmissionProgress> One(Uint64 id, Bool started, Bool finished) {
return {Watch::SubmissionProgress{id, started, finished}};
}
} // namespace
TEST(GpuHangDetectorTest, ASubmissionRunningPastTheBudgetIsNamed) {
Watch::Detector detector(2000);
EXPECT_EQ(detector.Observe(One(7, true, false), 10000), 0u);
EXPECT_EQ(detector.Observe(One(7, true, false), 11000), 0u);
EXPECT_EQ(detector.Observe(One(7, true, false), 11999), 0u);
EXPECT_EQ(detector.Observe(One(7, true, false), 12000), 7u);
EXPECT_EQ(detector.RunningMs(7, 12000), 2000u);
}
TEST(GpuHangDetectorTest, ASubmissionQueuedBehindTheHangIsNeverNamed) {
// The compositor's frame waits behind a client's hang for the whole ~10 s stall: never started.
Watch::Detector detector(2000);
for (Uint64 now = 0; now <= 12000; now += 250) {
const Vector<Watch::SubmissionProgress> sample = {{1, true, false}, {2, false, false}};
const Uint64 named = detector.Observe(sample, now);
if (named != 0) {
EXPECT_EQ(named, 1u) << "the queued submission was blamed at " << now << " ms";
}
}
Watch::Detector innocentOnly(2000);
for (Uint64 now = 0; now <= 12000; now += 250)
EXPECT_EQ(innocentOnly.Observe(One(2, false, false), now), 0u) << "at " << now << " ms";
}
TEST(GpuHangDetectorTest, ALongSubmissionThatFinishesIsNeverNamed) {
Watch::Detector detector(2000);
EXPECT_EQ(detector.Observe(One(3, true, false), 0), 0u);
EXPECT_EQ(detector.Observe(One(3, true, false), 1900), 0u);
EXPECT_EQ(detector.Observe(One(3, true, true), 2100), 0u);
// Gone from the sample once retired: forgotten, so a new submission starts its own clock.
EXPECT_EQ(detector.Observe({}, 2200), 0u);
EXPECT_EQ(detector.Observe(One(4, true, false), 2300), 0u);
EXPECT_EQ(detector.Observe(One(4, true, false), 4200), 0u);
EXPECT_EQ(detector.Observe(One(4, true, false), 4300), 4u);
}
TEST(GpuHangDetectorTest, TheBudgetRunsFromTheFirstSampleThatSawItRunning) {
Watch::Detector detector(2000);
// Submitted and queued for 5 s (another process's work), then started.
EXPECT_EQ(detector.Observe(One(9, false, false), 0), 0u);
EXPECT_EQ(detector.Observe(One(9, false, false), 5000), 0u);
EXPECT_EQ(detector.Observe(One(9, true, false), 5250), 0u);
EXPECT_EQ(detector.Observe(One(9, true, false), 7000), 0u);
EXPECT_EQ(detector.Observe(One(9, true, false), 7250), 9u);
}
TEST(GpuHangDetectorTest, AZeroBudgetNamesNothing) {
Watch::Detector detector(0);
for (Uint64 now = 0; now <= 60000; now += 1000) EXPECT_EQ(detector.Observe(One(1, true, false), now), 0u);
}
namespace {
// A device double: its one submission's progress, and what the watch told it.
struct FakeDevice {
std::atomic<Bool> started{false};
std::atomic<Bool> finished{false};
std::atomic<int> samples{0};
std::atomic<int> named{0};
std::atomic<Uint64> namedId{0};
std::atomic<int> sampleDelayMs{0};
static Bool Sample(void* self, Vector<Watch::SubmissionProgress>& out) {
auto* device = static_cast<FakeDevice*>(self);
device->samples.fetch_add(1);
if (const int delay = device->sampleDelayMs.load(); delay > 0)
std::this_thread::sleep_for(std::chrono::milliseconds(delay));
out.push_back({42, device->started.load(), device->finished.load()});
return true;
}
static void Hung(void* self, Uint64 id, Uint64) {
auto* device = static_cast<FakeDevice*>(self);
device->namedId.store(id);
device->named.fetch_add(1);
}
};
template <typename F>
Bool PollUntil(F condition, int timeoutMs) {
const auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(timeoutMs);
while (std::chrono::steady_clock::now() < deadline) {
if (condition()) return true;
std::this_thread::sleep_for(std::chrono::milliseconds(5));
}
return condition();
}
struct ScopedWatchTiming {
ScopedWatchTiming(Uint32 pollMs, Uint32 budgetMs) {
Watch::SetPollIntervalForTesting(pollMs);
Watch::SetBudgetForTesting(budgetMs);
}
~ScopedWatchTiming() {
Watch::SetPollIntervalForTesting(0);
Watch::SetBudgetForTesting(0);
}
};
} // namespace
TEST(GpuHangWatchThreadTest, TheHangingDeviceIsNamedOnceAndTheOneWaitingBehindItIsNot) {
const ScopedWatchTiming timing(20, 200);
FakeDevice guilty;
FakeDevice innocent;
guilty.started = true; // the GPU runs it and it never ends
innocent.started = false; // queued behind it for the whole stall
const Uint64 a = Watch::Register(&guilty, &FakeDevice::Sample, &FakeDevice::Hung);
const Uint64 b = Watch::Register(&innocent, &FakeDevice::Sample, &FakeDevice::Hung);
ASSERT_NE(a, 0u);
ASSERT_NE(b, 0u);
const auto start = std::chrono::steady_clock::now();
EXPECT_TRUE(PollUntil([&] { return guilty.named.load() > 0; }, 3000));
const auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start).count();
EXPECT_GE(ms, 180) << "named before the budget ran out";
EXPECT_LE(ms, 1500) << "named far past budget + interval";
EXPECT_EQ(guilty.namedId.load(), 42u);
// Long after, still once, and the innocent never.
std::this_thread::sleep_for(std::chrono::milliseconds(400));
EXPECT_EQ(guilty.named.load(), 1);
EXPECT_EQ(innocent.named.load(), 0);
EXPECT_GT(innocent.samples.load(), 5) << "the innocent device was not even sampled";
Watch::Unregister(a);
Watch::Unregister(b);
}
TEST(GpuHangWatchThreadTest, UnregisterWaitsOutASampleInProgressAndNothingRunsAfterIt) {
const ScopedWatchTiming timing(10, 5000);
FakeDevice device;
device.sampleDelayMs = 100;
const Uint64 handle = Watch::Register(&device, &FakeDevice::Sample, &FakeDevice::Hung);
ASSERT_NE(handle, 0u);
ASSERT_TRUE(PollUntil([&] { return device.samples.load() > 0; }, 2000));
Watch::Unregister(handle);
const int after = device.samples.load();
std::this_thread::sleep_for(std::chrono::milliseconds(200));
EXPECT_EQ(device.samples.load(), after) << "the watch sampled a device after it unregistered";
}
TEST(GpuHangWatchThreadTest, ARegistrationWithoutASamplerIsRefused) {
EXPECT_EQ(Watch::Register(nullptr, nullptr, nullptr), 0u);
Watch::Unregister(0); // and unregistering "nothing" is harmless
}
@@ -1590,6 +1590,19 @@ TEST(ComputeWorkGroupCapabilities, TakesEveryAxisFromTheIndexedQuery) {
}
namespace {
const char* g_damageExtensions = nullptr;
EGLDisplay g_damageDisplay = EGL_NO_DISPLAY;
bool g_damageExtAvailable = false;
bool g_damageKhrAvailable = false;
std::vector<std::string> g_damageQueries;
EGLDisplay FakeDamageDisplay() { return g_damageDisplay; }
const char* FakeDamageQueryString(EGLDisplay display, EGLint name) {
EXPECT_EQ(display, g_damageDisplay);
EXPECT_EQ(name, EGL_EXTENSIONS);
return g_damageExtensions;
}
EGLBoolean FakeSwapBuffersWithDamage(EGLDisplay dpy, EGLSurface surface, const EGLint* rects, EGLint nRects) {
(void)dpy;
(void)surface;
@@ -1597,6 +1610,14 @@ namespace {
(void)nRects;
return EGL_TRUE;
}
__eglMustCastToProperFunctionPointerType FakeDamageGetProcAddress(const char* name) {
g_damageQueries.emplace_back(name);
const bool available = std::string(name) == "eglSwapBuffersWithDamageEXT"
? g_damageExtAvailable : g_damageKhrAvailable;
return available ? reinterpret_cast<__eglMustCastToProperFunctionPointerType>(&FakeSwapBuffersWithDamage)
: nullptr;
}
} // namespace
// EXT -> KHR -> nothing, the whole policy behind the loader's single damage-present slot.
@@ -1609,13 +1630,46 @@ namespace {
// call to either spelling is defined to be equivalent to that anyway.
TEST(SwapBuffersWithDamageResolution, PrefersExtThenKhrThenAnswersNothing) {
using namespace MobileGL;
MG_External::EGL::eglSwapBuffersWithDamageEXT_PTR ext = &FakeSwapBuffersWithDamage;
MG_External::EGL::eglSwapBuffersWithDamageKHR_PTR khr = &FakeSwapBuffersWithDamage;
MG_External::EGLFunctionsTable funcs{};
funcs.eglGetCurrentDisplay = &FakeDamageDisplay;
funcs.eglQueryString = &FakeDamageQueryString;
funcs.eglGetProcAddress = &FakeDamageGetProcAddress;
g_damageDisplay = reinterpret_cast<EGLDisplay>(1);
g_damageExtensions = "EGL_EXT_swap_buffers_with_damage EGL_KHR_swap_buffers_with_damage";
g_damageExtAvailable = g_damageKhrAvailable = true;
g_damageQueries.clear();
EXPECT_EQ(MG_Util::BackendLoader::ResolveSwapBuffersWithDamage(funcs), &FakeSwapBuffersWithDamage);
EXPECT_EQ(g_damageQueries, (std::vector<std::string>{"eglSwapBuffersWithDamageEXT"}));
EXPECT_EQ(MG_Util::BackendLoader::ResolveSwapBuffersWithDamage(ext, khr), ext)
<< "a driver that exports both spellings is answered with the EXT one";
EXPECT_EQ(MG_Util::BackendLoader::ResolveSwapBuffersWithDamage(nullptr, khr), khr)
<< "the KHR spelling alone must still fill the slot";
EXPECT_EQ(MG_Util::BackendLoader::ResolveSwapBuffersWithDamage(nullptr, nullptr), nullptr)
<< "neither spelling stays null, so the caller keeps eglSwapBuffers";
g_damageExtAvailable = false;
g_damageQueries.clear();
EXPECT_EQ(MG_Util::BackendLoader::ResolveSwapBuffersWithDamage(funcs), &FakeSwapBuffersWithDamage);
EXPECT_EQ(g_damageQueries, (std::vector<std::string>{"eglSwapBuffersWithDamageEXT", "eglSwapBuffersWithDamageKHR"}));
g_damageExtensions = "EGL_KHR_swap_buffers_with_damage";
g_damageQueries.clear();
EXPECT_EQ(MG_Util::BackendLoader::ResolveSwapBuffersWithDamage(funcs), &FakeSwapBuffersWithDamage);
EXPECT_EQ(g_damageQueries, (std::vector<std::string>{"eglSwapBuffersWithDamageKHR"}));
g_damageKhrAvailable = false;
EXPECT_EQ(MG_Util::BackendLoader::ResolveSwapBuffersWithDamage(funcs), nullptr);
g_damageExtensions = "EGL_EXT_swap_buffers_with_damage";
g_damageQueries.clear();
g_damageKhrAvailable = true;
EXPECT_EQ(MG_Util::BackendLoader::ResolveSwapBuffersWithDamage(funcs), nullptr);
EXPECT_EQ(g_damageQueries, (std::vector<std::string>{"eglSwapBuffersWithDamageEXT"}));
g_damageExtAvailable = true;
for (const char* extensions : {"", "EGL_EXT_swap_buffers_with_damage_extra", static_cast<const char*>(nullptr)}) {
g_damageExtensions = extensions;
g_damageQueries.clear();
EXPECT_EQ(MG_Util::BackendLoader::ResolveSwapBuffersWithDamage(funcs), nullptr);
EXPECT_TRUE(g_damageQueries.empty());
}
g_damageExtensions = "EGL_EXT_swap_buffers_with_damage EGL_KHR_swap_buffers_with_damage";
g_damageDisplay = EGL_NO_DISPLAY;
g_damageQueries.clear();
EXPECT_EQ(MG_Util::BackendLoader::ResolveSwapBuffersWithDamage(funcs), nullptr);
EXPECT_TRUE(g_damageQueries.empty());
}
+5
View File
@@ -98,6 +98,11 @@ add_subdirectory(SelfTest)
# The DirectGLES post-transpile ESSL passes are pure String -> String, so unlike the
# DirectVulkan suite below this one needs no device and always builds.
add_subdirectory(Backend/DirectGLES)
# Which session's submission stops the GPU (MG_Backend/GpuHangWatch.h): no device, no driver.
add_executable(GpuHangWatchTest Backend/GpuHangWatchTest.cpp)
target_include_directories(GpuHangWatchTest PRIVATE ${MGL_ROOT}/include ${MGL_ROOT}/MobileGL)
target_link_libraries(GpuHangWatchTest PRIVATE GTest::gtest_main ${LINK_LIBRARIES})
gtest_discover_tests(GpuHangWatchTest DISCOVERY_TIMEOUT 30 PROPERTIES LABELS unit)
if (ENABLE_INTEGRATION_TESTS)
add_subdirectory(Backend/DirectVulkan)
endif()
+111 -8
View File
@@ -405,40 +405,143 @@ TEST(EGLStateDmaBufImport, SharedImageLifetime) {
TEST(EGLStateDmaBufImport, FormatAndModifierQueries) {
auto fixture = CreateFixture();
EGLint count = -1;
ASSERT_TRUE(fixture->State.QueryDmaBufFormats(fixture->Display, true, 0, nullptr, &count));
ASSERT_TRUE(fixture->State.QueryDmaBufFormats(fixture->Display, true, false, 0, nullptr, &count));
EXPECT_EQ(count, 4);
EGLint formats[4] = {};
ASSERT_TRUE(fixture->State.QueryDmaBufFormats(fixture->Display, true, 4, formats, &count));
ASSERT_TRUE(fixture->State.QueryDmaBufFormats(fixture->Display, true, false, 4, formats, &count));
ASSERT_EQ(count, 4);
EXPECT_EQ(formats[0], static_cast<EGLint>(kAbgr8888));
EXPECT_EQ(formats[1], static_cast<EGLint>(kXbgr8888));
EXPECT_EQ(formats[2], static_cast<EGLint>(kArgb8888));
EXPECT_EQ(formats[3], static_cast<EGLint>(kXrgb8888));
ASSERT_TRUE(fixture->State.QueryDmaBufFormats(fixture->Display, true, 1, formats, &count));
ASSERT_TRUE(fixture->State.QueryDmaBufFormats(fixture->Display, true, false, 1, formats, &count));
EXPECT_EQ(count, 1);
// Without shared images there is nothing to import.
ASSERT_TRUE(fixture->State.QueryDmaBufFormats(fixture->Display, false, 0, nullptr, &count));
ASSERT_TRUE(fixture->State.QueryDmaBufFormats(fixture->Display, false, false, 0, nullptr, &count));
EXPECT_EQ(count, 0);
EXPECT_FALSE(fixture->State.QueryDmaBufFormats(fixture->Display, true, -1, formats, &count));
EXPECT_FALSE(fixture->State.QueryDmaBufFormats(fixture->Display, true, false, -1, formats, &count));
EXPECT_EQ(fixture->State.ConsumeError(), EGL_BAD_PARAMETER);
// No explicit modifiers: importers fall back to the implicit one.
uint64_t modifiers[2] = {};
EGLBoolean externalOnly[2] = {};
count = -1;
ASSERT_TRUE(fixture->State.QueryDmaBufModifiers(fixture->Display, true, static_cast<EGLint>(kAbgr8888), 2,
ASSERT_TRUE(fixture->State.QueryDmaBufModifiers(fixture->Display, true, false, static_cast<EGLint>(kAbgr8888), 2,
modifiers, externalOnly, &count));
EXPECT_EQ(count, 0);
EXPECT_FALSE(fixture->State.QueryDmaBufModifiers(fixture->Display, true, static_cast<EGLint>(kRgb565), 0,
EXPECT_FALSE(fixture->State.QueryDmaBufModifiers(fixture->Display, true, false, static_cast<EGLint>(kRgb565), 0,
nullptr, nullptr, &count));
EXPECT_EQ(fixture->State.ConsumeError(), EGL_BAD_PARAMETER);
ASSERT_TRUE(fixture->State.TerminateDisplay(fixture->Display));
EXPECT_FALSE(fixture->State.QueryDmaBufFormats(fixture->Display, true, 0, nullptr, &count));
EXPECT_FALSE(fixture->State.QueryDmaBufFormats(fixture->Display, true, false, 0, nullptr, &count));
EXPECT_EQ(fixture->State.ConsumeError(), EGL_NOT_INITIALIZED);
}
namespace {
constexpr EGLAttrib kNv12 = StateContext::kDrmFourccNv12;
constexpr EGLAttrib kP010 = StateContext::kDrmFourccP010;
// A complete NV12 import as a video decoder exports one: both planes in one buffer.
std::vector<EGLAttrib> Nv12Attribs(EGLAttrib fourcc = kNv12) {
return {EGL_WIDTH,
1280,
EGL_HEIGHT,
720,
EGL_LINUX_DRM_FOURCC_EXT,
fourcc,
EGL_DMA_BUF_PLANE0_FD_EXT,
7,
EGL_DMA_BUF_PLANE0_OFFSET_EXT,
0,
EGL_DMA_BUF_PLANE0_PITCH_EXT,
1280,
EGL_DMA_BUF_PLANE1_FD_EXT,
9,
EGL_DMA_BUF_PLANE1_OFFSET_EXT,
942080,
EGL_DMA_BUF_PLANE1_PITCH_EXT,
1280,
EGL_NONE};
}
} // namespace
TEST(EGLStateDmaBufImport, ParsesATwoPlaneYuvImportWithItsHints) {
auto fixture = CreateFixture();
auto attribs = WithAttrib(Nv12Attribs(), EGL_YUV_COLOR_SPACE_HINT_EXT, EGL_ITU_REC709_EXT);
attribs = WithAttrib(attribs, EGL_SAMPLE_RANGE_HINT_EXT, EGL_YUV_NARROW_RANGE_EXT);
attribs = WithAttrib(attribs, EGL_YUV_CHROMA_HORIZONTAL_SITING_HINT_EXT, EGL_YUV_CHROMA_SITING_0_5_EXT);
StateContext::DmaBufImportAttribs parsed;
ASSERT_TRUE(fixture->State.PrepareDmaBufImport(fixture->Display, EGL_NO_CONTEXT, nullptr, attribs.data(), &parsed));
EXPECT_EQ(parsed.PlaneCount, 2u);
EXPECT_EQ(parsed.Fd, 7);
EXPECT_EQ(parsed.Plane1Fd, 9);
EXPECT_EQ(parsed.Plane1Offset, 942080);
EXPECT_EQ(parsed.Plane1Pitch, 1280);
EXPECT_EQ(parsed.ColorSpace, static_cast<uint32_t>(EGL_ITU_REC709_EXT));
EXPECT_EQ(parsed.Range, static_cast<uint32_t>(EGL_YUV_NARROW_RANGE_EXT));
EXPECT_EQ(parsed.SitingX, static_cast<uint32_t>(EGL_YUV_CHROMA_SITING_0_5_EXT));
EXPECT_EQ(parsed.SitingY, 0u);
EXPECT_FALSE(parsed.HasModifier);
EXPECT_EQ(PrepareError(*fixture, Nv12Attribs(kP010)), EGL_SUCCESS);
// One modifier for the buffer: the same on both planes, or none on either.
auto linear = WithAttrib(WithAttrib(Nv12Attribs(), EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT, 0),
EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT, 0);
EXPECT_EQ(PrepareError(*fixture, linear), EGL_BAD_PARAMETER);
linear = WithAttrib(WithAttrib(linear, EGL_DMA_BUF_PLANE1_MODIFIER_LO_EXT, 0), EGL_DMA_BUF_PLANE1_MODIFIER_HI_EXT, 0);
EXPECT_EQ(PrepareError(*fixture, linear), EGL_SUCCESS);
auto mixed = WithAttrib(WithAttrib(WithoutAttrib(WithoutAttrib(linear, EGL_DMA_BUF_PLANE1_MODIFIER_LO_EXT),
EGL_DMA_BUF_PLANE1_MODIFIER_HI_EXT),
EGL_DMA_BUF_PLANE1_MODIFIER_LO_EXT, 1),
EGL_DMA_BUF_PLANE1_MODIFIER_HI_EXT, 0);
EXPECT_EQ(PrepareError(*fixture, mixed), EGL_BAD_MATCH);
}
TEST(EGLStateDmaBufImport, RefusesAnIncompleteYuvImport) {
auto fixture = CreateFixture();
for (const EGLAttrib required :
{EGL_DMA_BUF_PLANE1_FD_EXT, EGL_DMA_BUF_PLANE1_OFFSET_EXT, EGL_DMA_BUF_PLANE1_PITCH_EXT}) {
EXPECT_EQ(PrepareError(*fixture, WithoutAttrib(Nv12Attribs(), required)), EGL_BAD_PARAMETER)
<< "missing 0x" << std::hex << required;
}
// NV12 has two planes, not three.
EXPECT_EQ(PrepareError(*fixture, WithAttrib(Nv12Attribs(), EGL_DMA_BUF_PLANE2_FD_EXT, 7)), EGL_BAD_ATTRIBUTE);
auto badPitch = WithAttrib(WithoutAttrib(Nv12Attribs(), EGL_DMA_BUF_PLANE1_PITCH_EXT), EGL_DMA_BUF_PLANE1_PITCH_EXT, 0);
EXPECT_EQ(PrepareError(*fixture, badPitch), EGL_BAD_ACCESS);
}
TEST(EGLStateDmaBufImport, YuvFormatsAreListedOnlyWhenTheServerHoldsThem) {
auto fixture = CreateFixture();
EGLint count = -1;
ASSERT_TRUE(fixture->State.QueryDmaBufFormats(fixture->Display, true, false, 0, nullptr, &count));
EXPECT_EQ(count, 4);
ASSERT_TRUE(fixture->State.QueryDmaBufFormats(fixture->Display, true, true, 0, nullptr, &count));
EXPECT_EQ(count, 6);
EGLint formats[6] = {};
ASSERT_TRUE(fixture->State.QueryDmaBufFormats(fixture->Display, true, true, 6, formats, &count));
EXPECT_EQ(formats[4], static_cast<EGLint>(kNv12));
EXPECT_EQ(formats[5], static_cast<EGLint>(kP010));
ASSERT_TRUE(fixture->State.QueryDmaBufFormats(fixture->Display, false, true, 0, nullptr, &count));
EXPECT_EQ(count, 0);
// A YUV format: LINEAR, external only.
uint64_t modifiers[2] = {99, 99};
EGLBoolean externalOnly[2] = {EGL_FALSE, EGL_FALSE};
ASSERT_TRUE(fixture->State.QueryDmaBufModifiers(fixture->Display, true, true, static_cast<EGLint>(kNv12), 0,
nullptr, nullptr, &count));
EXPECT_EQ(count, 1);
ASSERT_TRUE(fixture->State.QueryDmaBufModifiers(fixture->Display, true, true, static_cast<EGLint>(kNv12), 2,
modifiers, externalOnly, &count));
EXPECT_EQ(count, 1);
EXPECT_EQ(modifiers[0], 0u);
EXPECT_EQ(externalOnly[0], EGL_TRUE);
EXPECT_FALSE(fixture->State.QueryDmaBufModifiers(fixture->Display, true, false, static_cast<EGLint>(kNv12), 0,
nullptr, nullptr, &count));
EXPECT_EQ(fixture->State.ConsumeError(), EGL_BAD_PARAMETER);
}
// EGL_EXT_create_context_robustness / EGL 1.5: the reset notification behavior a context is
// created with, under both names of the attribute (the extension's, which Chrome, KWin and Qt
// pass, and EGL 1.5's, which ANGLE's GL-on-EGL backend passes for its own native context), the
@@ -36,7 +36,8 @@ namespace {
bool Supported(uint32_t fourcc) {
return fourcc == MOBILEGL_SHARED_IMAGE_FOURCC_ABGR8888 || fourcc == MOBILEGL_SHARED_IMAGE_FOURCC_XBGR8888 ||
fourcc == MOBILEGL_SHARED_IMAGE_FOURCC_ARGB8888 || fourcc == MOBILEGL_SHARED_IMAGE_FOURCC_XRGB8888;
fourcc == MOBILEGL_SHARED_IMAGE_FOURCC_ARGB8888 || fourcc == MOBILEGL_SHARED_IMAGE_FOURCC_XRGB8888 ||
fourcc == MOBILEGL_SHARED_IMAGE_FOURCC_NV12;
}
} // namespace
@@ -44,7 +45,13 @@ FAKE_EXPORT uint32_t mobilegl_shared_image_abi_version(void) { return MOBILEGL_S
FAKE_EXPORT int mobilegl_server_available(void) { return 1; }
FAKE_EXPORT int mobilegl_shared_image_formats(uint32_t*, uint32_t) { return 4; }
FAKE_EXPORT int mobilegl_shared_image_formats(uint32_t* formats, uint32_t capacity) {
const uint32_t all[] = {MOBILEGL_SHARED_IMAGE_FOURCC_ABGR8888, MOBILEGL_SHARED_IMAGE_FOURCC_XBGR8888,
MOBILEGL_SHARED_IMAGE_FOURCC_ARGB8888, MOBILEGL_SHARED_IMAGE_FOURCC_XRGB8888,
MOBILEGL_SHARED_IMAGE_FOURCC_NV12};
for (uint32_t i = 0; i < 5 && i < capacity; ++i) formats[i] = all[i];
return 5;
}
FAKE_EXPORT int mobilegl_shared_image_allocate(uint32_t width, uint32_t height, uint32_t fourcc,
struct mobilegl_shared_image* out) {
@@ -64,6 +71,11 @@ FAKE_EXPORT int mobilegl_shared_image_allocate(uint32_t width, uint32_t height,
out->stride = width * 4;
out->offset = 0;
out->modifier = 0x00ffffffffffffffull;
if (fourcc == MOBILEGL_SHARED_IMAGE_FOURCC_NV12) {
out->stride = width;
out->plane1_stride = width;
out->plane1_offset = width * height;
}
return 0;
}
@@ -103,3 +115,10 @@ FAKE_EXPORT int fake_shared_image_imports(void) {
const std::lock_guard<std::mutex> lock(g_mutex);
return g_imports;
}
// A YUV import names an image by its descriptor as an RGBA one does; the planes are not looked at.
FAKE_EXPORT int mobilegl_shared_image_import_planes(int fd, uint32_t width, uint32_t height, uint32_t fourcc,
const struct mobilegl_shared_image_planes* planes, uint64_t* out_id) {
if (planes == nullptr || planes->plane_count != 2 || fourcc != MOBILEGL_SHARED_IMAGE_FOURCC_NV12) return -EINVAL;
return mobilegl_shared_image_import(fd, width, height, fourcc, out_id);
}
+56
View File
@@ -180,3 +180,59 @@ TEST_F(GbmGlamor, AForeignDmaBufIsRefused) {
EXPECT_EQ(gbm_bo_import(m_gbm, GBM_BO_IMPORT_FD, &data, GBM_BO_USE_RENDERING), nullptr);
::close(foreign);
}
// YUV buffers (ABI 2): NV12 where the library lists it, two planes in one descriptor, sampled only.
TEST_F(GbmGlamor, AnNv12BufferHasTwoPlanesInOneDescriptor) {
EXPECT_EQ(gbm_device_is_format_supported(m_gbm, GBM_FORMAT_NV12, 0), 1);
EXPECT_EQ(gbm_device_is_format_supported(m_gbm, GBM_FORMAT_NV12, GBM_BO_USE_RENDERING), 0);
EXPECT_EQ(gbm_device_is_format_supported(m_gbm, 0x30313050u /* P010 */, 0), 0) << "the library does not list P010";
EXPECT_EQ(gbm_device_get_format_modifier_plane_count(m_gbm, GBM_FORMAT_NV12, DRM_FORMAT_MOD_INVALID), 2);
EXPECT_EQ(gbm_bo_create(m_gbm, 64, 32, GBM_FORMAT_NV12, GBM_BO_USE_RENDERING), nullptr);
gbm_bo* bo = gbm_bo_create(m_gbm, 64, 32, GBM_FORMAT_NV12, 0);
ASSERT_NE(bo, nullptr);
EXPECT_EQ(gbm_bo_get_plane_count(bo), 2);
EXPECT_EQ(gbm_bo_get_stride_for_plane(bo, 0), 64u);
EXPECT_EQ(gbm_bo_get_stride_for_plane(bo, 1), 64u);
EXPECT_EQ(gbm_bo_get_offset(bo, 1), 64u * 32u);
EXPECT_EQ(gbm_bo_get_handle_for_plane(bo, 1).u32, gbm_bo_get_handle(bo).u32);
const int fd = gbm_bo_get_fd(bo);
const int plane1 = gbm_bo_get_fd_for_plane(bo, 1);
ASSERT_GE(fd, 0);
ASSERT_GE(plane1, 0);
// Imported back with both planes, through one descriptor or two of the same buffer.
gbm_import_fd_modifier_data data{};
data.width = 64;
data.height = 32;
data.format = GBM_FORMAT_NV12;
data.num_fds = 2;
data.fds[0] = fd;
data.fds[1] = plane1;
data.strides[0] = data.strides[1] = 64;
data.offsets[1] = 64 * 32;
data.modifier = DRM_FORMAT_MOD_INVALID;
gbm_bo* imported = gbm_bo_import(m_gbm, GBM_BO_IMPORT_FD_MODIFIER, &data, 0);
ASSERT_NE(imported, nullptr);
EXPECT_EQ(gbm_bo_get_plane_count(imported), 2);
EXPECT_EQ(gbm_bo_get_offset(imported, 1), 64u * 32u);
// Planes in two different buffers, and a one-plane import that cannot say where plane 1 is.
const int other = ::memfd_create("other", MFD_CLOEXEC);
data.fds[1] = other;
EXPECT_EQ(gbm_bo_import(m_gbm, GBM_BO_IMPORT_FD_MODIFIER, &data, 0), nullptr);
gbm_import_fd_data plain{};
plain.fd = fd;
plain.width = 64;
plain.height = 32;
plain.stride = 64;
plain.format = GBM_FORMAT_NV12;
EXPECT_EQ(gbm_bo_import(m_gbm, GBM_BO_IMPORT_FD, &plain, 0), nullptr);
::close(other);
gbm_bo_destroy(imported);
gbm_bo_destroy(bo);
::close(fd);
::close(plane1);
EXPECT_EQ(m_live(), m_liveAtStart) << "every reference was dropped";
}
+1 -1
View File
@@ -704,7 +704,7 @@ TEST(PipeCatalogue, LateArrivalsAreAppendedWithoutRenumbering) {
// region (kMGPMaxDamageRects rectangles), as the present record carries its frame's damage.
EXPECT_EQ(sizeof(MGPSharedImageOp), 296u);
EXPECT_EQ(sizeof(MGPPresent), 272u);
EXPECT_EQ(sizeof(MGPSharedImageReply), 40u);
EXPECT_EQ(sizeof(MGPSharedImageReply), 48u);
EXPECT_EQ(sizeof(MGPSharedImageFdOffer), 32u);
EXPECT_EQ(sizeof(MGPHandleOnly), 16u);
// rv's two (§5.3/§7.6): the residual-value POD - 2 + 15 Uint32s, 2 Uint8s and 2 pad bytes,
@@ -384,6 +384,13 @@ TEST(TextureEmit, EveryTextureTargetMapsToItsOwnResourceTarget) {
<< "TextureTarget " << i << " maps onto the BUFFER row, which the ack predicate reads";
EXPECT_NE(resourceTarget, static_cast<Uint32>(MGPipeResourceTarget::Renderbuffer))
<< "TextureTarget " << i << " maps onto the RENDERBUFFER row";
// The one deliberate fold: an external texture (GL_OES_EGL_image_external) is a 2D texture
// below the frontend.
if (target == TextureTarget::External) {
EXPECT_EQ(resourceTarget, static_cast<Uint32>(MGPipeResourceTarget::Tex2D));
seen.push_back(resourceTarget);
continue;
}
for (const Uint32 previous : seen) {
EXPECT_NE(previous, resourceTarget)
<< "TextureTarget " << i << " shares its resource target with an earlier one";
+99
View File
@@ -8,6 +8,7 @@
#include <gtest/gtest.h>
#include <spirv_reflect.h>
#include <cstdio>
#include <cstring>
#include <utility>
#include <vector>
@@ -5276,3 +5277,101 @@ TEST_F(ProgramTest, ASpirvModulesXfbDecorationsDoNotSurviveIntoTheTranslatedSour
DrainProgramTestErrors();
}
// GL_OES_EGL_image_external. A samplerExternalOES uniform is compiled as a sampler2D (the desktop
// profile the shader is compiled under has no external overloads; below the frontend an external
// texture is a 2D one) and reported with its own type, which is what sends its unit to the
// external binding point.
namespace {
GLuint LinkExternalSamplerProgram(const char* vertexSource, const char* fragmentSource, String& log) {
char info[2048] = "";
const GLuint vs = CreateShader(GL_VERTEX_SHADER);
ShaderSource(vs, 1, &vertexSource, nullptr);
CompileShader(vs);
const GLuint fs = CreateShader(GL_FRAGMENT_SHADER);
ShaderSource(fs, 1, &fragmentSource, nullptr);
CompileShader(fs);
GLint compiled = GL_FALSE;
GetShaderiv(fs, GL_COMPILE_STATUS, &compiled);
GetShaderInfoLog(fs, sizeof(info), nullptr, info);
log = info;
if (compiled != GL_TRUE) return 0;
const GLuint program = CreateProgram();
AttachShader(program, vs);
AttachShader(program, fs);
LinkProgram(program);
GLint linked = GL_FALSE;
GetProgramiv(program, GL_LINK_STATUS, &linked);
GetProgramInfoLog(program, sizeof(info), nullptr, info);
log += info;
return linked == GL_TRUE ? program : 0;
}
GLenum UniformTypeByName(GLuint program, const char* wanted) {
GLint count = 0;
GetProgramiv(program, GL_ACTIVE_UNIFORMS, &count);
for (GLint i = 0; i < count; ++i) {
char name[64] = "";
GLsizei length = 0;
GLint size = 0;
GLenum type = 0;
GetActiveUniform(program, static_cast<GLuint>(i), sizeof(name), &length, &size, &type, name);
if (String(name) == wanted) return type;
}
return GL_NONE;
}
const char* kExternalVertexSource = "#version 300 es\n"
"out vec2 uv;\n"
"void main() { uv = vec2(0.5); gl_Position = vec4(0.0, 0.0, 0.0, 1.0); }\n";
} // namespace
TEST_F(ProgramTest, AnEsslExternalSamplerReportsItsOwnType) {
DrainProgramTestErrors();
const char* fs = "#version 300 es\n"
"#extension GL_OES_EGL_image_external_essl3 : require\n"
"precision mediump float;\n"
"uniform highp samplerExternalOES videoFrame;\n"
"uniform sampler2D overlay;\n"
"in vec2 uv;\n"
"out vec4 color;\n"
"void main() { color = texture(videoFrame, uv) + texture(overlay, uv); }\n";
String log;
const GLuint program = LinkExternalSamplerProgram(kExternalVertexSource, fs, log);
ASSERT_NE(program, 0u) << log;
EXPECT_EQ(UniformTypeByName(program, "videoFrame"), static_cast<GLenum>(GL_SAMPLER_EXTERNAL_OES));
EXPECT_EQ(UniformTypeByName(program, "overlay"), static_cast<GLenum>(GL_SAMPLER_2D));
DrainProgramTestErrors();
}
// A compositor's shader generator declares one name both ways, picking by #if: only the branch the
// preprocessor keeps says what the uniform is.
TEST_F(ProgramTest, AnExternalSamplerInAnInactiveBranchLeavesTheSampler2DAlone) {
DrainProgramTestErrors();
const char* fsTemplate = "#version 140\n"
"%s"
"#if TRAIT_MAP_TEXTURE\n"
"uniform sampler2D sampler;\n"
"#endif\n"
"#if TRAIT_MAP_EXTERNAL_TEXTURE\n"
"#extension GL_OES_EGL_image_external : require\n"
"uniform samplerExternalOES sampler;\n"
"#endif\n"
"in vec2 uv;\n"
"out vec4 color;\n"
"void main() { color = texture(sampler, uv); }\n";
const char* vs = "#version 140\n"
"out vec2 uv;\n"
"void main() { uv = vec2(0.5); gl_Position = vec4(0.0, 0.0, 0.0, 1.0); }\n";
for (const auto& [traits, expected] :
{std::pair{"#define TRAIT_MAP_TEXTURE 1\n", static_cast<GLenum>(GL_SAMPLER_2D)},
std::pair{"#define TRAIT_MAP_EXTERNAL_TEXTURE 1\n", static_cast<GLenum>(GL_SAMPLER_EXTERNAL_OES)}}) {
char fs[1024];
std::snprintf(fs, sizeof(fs), fsTemplate, traits);
String log;
const GLuint program = LinkExternalSamplerProgram(vs, fs, log);
ASSERT_NE(program, 0u) << traits << log;
EXPECT_EQ(UniformTypeByName(program, "sampler"), expected) << traits;
}
DrainProgramTestErrors();
}

Some files were not shown because too many files have changed in this diff Show More