Skip to content

feat(html): render HTML on the GPU and hand frames over in GPU memory - #1276

Draft
srperens wants to merge 1 commit into
mainfrom
feat/html-gpu-output
Draft

srperens wants to merge 1 commit into
mainfrom
feat/html-gpu-output

Conversation

@srperens

Copy link
Copy Markdown
Collaborator

Refs #

Draft. The code is tested on one NVIDIA host (below), but it cannot ship until the gstcefsrc build is published and pinned. See "Remaining before merge".

Problem

HTML sources render in software. That is CPU-heavy for anything animated, and the BGRA frames then get uploaded again for the GPU mixer: one 8 MB copy per frame, per source. The opt-in GPU mode (STROM_CEF_GPU=1, ANGLE/Vulkan under Xvfb) still read every frame back through the CPU, so it cost more than software on simple pages. On top of that, the pinned CEF 144 cannot hand frames over on the GPU on Linux/NVIDIA at all (see docs/UPSTREAM_NOTES.md).

Change

gstcefsrc (docker/gstcefsrc/gl-output.patch, new file gstcefgl.cc)

  • When Chromium renders on the GPU, the browser is created with shared_texture_enabled.
  • In OnAcceleratedPaint, the dma-buf is imported with EGL and blitted on a worker GL context of cefsrc's own into a GL buffer from a pool. The pool allocates in the consumer's GstGLContext, which is found from the GL pool the consumer proposes in the allocation query. GL elements accept memory only from their own context object; being in the same share group is not enough.
  • The output is video/x-raw(memory:GLMemory),format=BGRA or video/x-raw,format=BGRA, whichever the consumer takes, with GL first. GL is offered only when the import works. Bytes are kept in memory order: Chromium's BGRA buffer is imported as AB24, so neither path swizzles.
  • The output is the same however the page is painted:
    • Frames Chromium paints to CPU memory are uploaded into the same GL pool.
    • A system-memory consumer gets frames read back through GStreamer's GL download, into pooled buffers.
  • Fallback without stopping the flow:
    • An import that fails, a frame layout that does not match for 100 frames in a row, or no GPU frame within 5 s (X server without DRI3) reopens the browser without shared textures, in the same request context. A warning names the reason.
    • frame-handover=gpu turns each of these into a flow error instead; cpu never uses shared textures.
  • Pushed copies carry a parent-buffer meta, so a pool frame is not reused while a copy is still downstream.
  • cefdemux accepts GL memory and routes allocation and context queries to its video pad.

CEF 144 → 158.0.3 (the newest stable). strict-network.patch: Chromium 145 split Local Network Access into local network and loopback. Strict now refuses both, plus the old type.

strom-full

  • xserver-xorg-core is added to the image.
  • With an NVIDIA GPU, the entrypoint starts headless Xorg on the NVIDIA X driver that the container toolkit mounts with the display capability (NVIDIA_DRIVER_CAPABILITIES=all, already set). GBM_BACKENDS_PATH points at the GBM backend the toolkit mounts. No extra mounts are needed in Docker or on Kubernetes with the toolkit or GPU Operator.
  • GPU mode is enabled only if /usr/local/version.txt lists the gl-output patch, so the currently pinned CEF 144 build stays in software.
  • STROM_CEF_GPU=0 forces software. STROM_CEF_GPU=1 keeps the old Vulkan path as a fallback when Xorg cannot start, now with GST_CEF_SHARED_TEXTURE=0.

HTML Input block

  • The capsfilter offers GL memory before system memory. videoconvert is dropped: every consumer adapts its own input (checked for each block that takes raw video).
  • New property Render Mode (auto/gpu/cpu, mapped to frame-handover). gpu on a gstcefsrc without the property fails the block.

Rejected alternatives

  • ozone-platform=headless: Chromium allocates no GPU frames there.
  • glReadPixels from cefsrc's context: ~680 ms per 1080p frame. GStreamer's download path takes ~8 ms.
  • A worker context in the consumer's share group only: refused by glcolorconvert in some runs.
  • Answering the consumer's GL thread on every frame: a worker context keeps Chromium's UI thread off the mixer's GL thread.

Evidence

All measurements are from one AWS g4dn.xlarge (Tesla T4, driver 580.178.04, EKS AL2023 NVIDIA AMI), with strom-full:0.8.2 plus this branch's binary, CEF build and entrypoint.

Correctness

  • Same pixels in every path: on a page with transparent, 50 % and 25 % alpha and opaque regions, RGBA at 5 points is identical for software OnPaint, GPU → GL, GPU → system readback, the forced import-failure fallback and Xvfb, on both CEF 156.0.31 and 158.0.3. Alpha is premultiplied in all of them, as before.
  • Strom end to end: HTML Input → GPU Vision Mixer → Video Encoder (nvh264enc) → WHEP Output, for both PGM and multiview at 1080p.
    • The debug graph shows memory:GLMemory on every video edge from cefsrc to nvh264enc.
    • 0 pipeline errors over 3 runs after the context fix. Before it: "input memory OpenGL context is different" in 3 of 3 runs on a heavy page.
    • The PGM thumbnail shows the page with correct colours.

Fallback and Render Mode

  • Import failure forced with GST_CEF_GL_TEST_BREAK_IMPORT=60: the browser is reopened, about 30 frames/s are uploaded afterwards, and the caps are unchanged.
  • Under Xvfb (no DRI3): the reopen happens after 5 s.
  • frame-handover=gpu posts an element error in both cases. cpu gives system memory.

Audio and leaks

  • Audio through cefdemux with GL video: −13.5 dBFS RMS for a 0.3 sine.
  • An 18 min run on the wind map at 1080p50: fds, RSS of Strom and Chromium, and GPU memory were flat.
  • 8 stop/start cycles of the flow: after each stop exactly 193 fds, 58 threads, 0 renderers and 135 MiB GPU memory.

CPU (% of one core: Strom + Chromium, HTML → GPU mixer)

Page, 1080p50 Software (STROM_CEF_GPU=0) GPU (this PR)
Lower third + ticker ~75 % ~30 %
Wind map (earth.nullschool) not measured on this host ~40 %

For comparison, docs list the wind map at ~95 % in software at 1080p30 on another machine. In a YouTube page, the remaining heavy part is Chromium decoding AV1 in software (no VA-API on NVIDIA). That is unchanged and already in the docs.

Tests

  • Ran on macOS: cargo test --lib (1335 passed), cargo clippy --all-targets (clean), cargo fmt --check.
  • New unit tests: output_caps_offer_gl_memory_before_system_memory and render_mode_is_auto_unless_gpu_or_cpu.
  • Ran by hand on the T4 host: everything under Evidence, with gst-launch scripts and a Strom e2e flow.
  • Not run in CI: the GPU path needs an NVIDIA GPU, Chromium and gstcefsrc, and CI has none of them. Per CLAUDE.md, an element CI cannot have does not hold a fix back. The block's own logic (caps order, render mode) is unit tested, and the rest was verified by hand as above.
  • Patch series: applies cleanly to upstream gstcefsrc b63340852f in Dockerfile order and reproduces exactly the tested source.

Lesson

  • docs/BLOCK_GUIDELINES.md has a new rule: GL memory a block produces belongs to the consumer's GstGLContext, not just one in its share group. It failed in some runs only. No CI guard is possible (no GPU in CI), so the rule says to check by hand on a GPU host, in more than one run.
  • docs/UPSTREAM_NOTES.md has four entries:
    • CEF 144 shared textures on NVIDIA;
    • no frames without DRI3;
    • slow glReadPixels in cefsrc's context;
    • the toolkit's host paths for the X driver and GBM.

For the reviewer

  • Default changes on GPU hosts: HTML renders on the GPU and outputs GL memory. Consumers that take system memory get one GPU readback; that was checked for every block that takes raw video.
  • CEF 144 → 158 is a major Chromium jump: pages render with Chromium 158.
  • Xorg runs inside the strom-full container on GPU hosts. It needs the NVIDIA container toolkit (or GPU Operator) with the display capability. Device plugins that mount the driver at /usr/local/nvidia without the X driver stay in software, and the entrypoint logs why.
  • Test hook: GST_CEF_GL_TEST_BREAK_IMPORT=N makes import N fail, for testing the fallback.

Remaining before merge

  1. Publish the gstcefsrc build: run build-gstcefsrc with CEF 158.0.3+g8c29be2+chromium-158.0.8127.18 and a new suffix (e.g. strom1).
  2. Pin it: set GSTCEFSRC_VERSION and both sha256 pins in docker/strom-full/Dockerfile.
  3. Build and check the image on a GPU host: the entrypoint log should say HTML renders on the GPU. Also check the arm64 build of the patch, which has not been compiled yet.
  4. Check on the next target (other GPU, driver or orchestrator): Xorg starts with the toolkit's X driver, Chromium gets DRI3, and the import works. If not, the fallback should take over, with the warning.
  5. Measure the software baseline for the wind map on the same host, for a like-for-like number.

Out of scope

  • Premultiplied alpha: Chromium hands over premultiplied BGRA and GStreamer treats it as straight, so semi-transparent edges come out dark in the mixer. The software path has always done this; it deserves its own issue.
  • A tee feeding several GL consumers that use different GstGLContext objects: the frames belong to one of them, and the other branch fails. Strom's GL elements normally share one context per flow. This is not specific to this PR.
  • Pods with several GPUs: Xorg uses the first GPU nvidia-smi lists, and GStreamer EGL device 0. If these differ, the import fails and cefsrc falls back to CPU frames.
  • Hardware video decode inside pages (needs VA-API, which NVIDIA lacks).

🤖 Generated with Claude Code

On an NVIDIA GPU, strom-full now renders HTML on the GPU by default.
Chromium hands each frame to cefsrc as a dma-buf, which cefsrc copies into
GL memory in the consumer's GstGLContext, so an HTML source reaches the GPU
vision mixer and NVENC without a CPU round trip.

- gstcefsrc gl-output patch: OnAcceleratedPaint import, readback for
  system-memory consumers, upload of CPU-painted frames into the same GL
  memory, fallback to CPU frames on import failure or when no GPU frame
  arrives within 5 s, frame-handover property (auto/gpu/cpu).
- CEF 144 -> 158.0.3; strict-network covers the local and loopback network
  permissions Chromium 145 split Local Network Access into.
- strom-full: Xorg on the NVIDIA X driver the container toolkit mounts,
  started only when the installed gstcefsrc has gl-output; Xvfb and
  software otherwise.
- HTML Input: GL memory offered before system memory, videoconvert dropped,
  Render Mode property.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant