Home/docs/kernel/Zero-copy import

Zero-copy import

Zero-copy Import

The hardware decoder's output is imported directly into the renderer as a native texture or buffer handle, with no CPU copy in between. Each import path has a matching fallback: when a frame cannot be imported, it goes through CPU upload instead, and the frame counters accurately reflect how often each path is used.

Import paths by platform#

PlatformDecoder outputImport targetMechanism
macOS / iOS / tvOSCVPixelBufferMTLTextureMetal holds the pixel buffer's texture directly and references it with zero copy through IOSurface or shared memory.
WindowsD3D11VA textureD3D11 textureThe decoder texture interoperates with the renderer texture, and the render pass samples the decoded output directly.
AndroidMediaCodec SurfaceAHardwareBuffer → VulkanSurface frames are imported into wgpu / Vulkan through an AHardwareBuffer handle and mapped to a VkImage.
HarmonyOSAVCodec SurfaceOHNativeBuffer → VulkanImported from OHNativeBuffer with zero copy and presented through wgpu / Vulkan.

Fallback and counters#

A frame that fails to import falls back to CPU upload: the decoded output is copied through avcodec into CPU-side memory and then uploaded as an ordinary texture. The renderer does not distinguish where a frame came from, and both paths use the same rendering pipeline.

Fallbacks happen for reasons such as failing to obtain a handle, an incompatible format, or a failed video memory allocation. The frame counters track the two paths separately:

FieldDescription
zero_copy_video_framesNumber of frames imported successfully with zero copy.
direct_zero_copy_video_framesNumber of frames that reference the decoder output directly (no copy).
shared_handle_video_framesNumber of frames imported through a shared handle (AHardwareBuffer, OHNativeBuffer, and so on).
cpu_video_frame_fallbacksNumber of frames that fell back to CPU upload.
import_failuresNumber of import failures, including those where the fallback then succeeded.

zero_copy_video_frames / (zero_copy_video_frames + cpu_video_frame_fallbacks) gives the zero-copy hit rate over a period of time. windows_native_demo --smoke-seconds 3 prints these counters before it exits (the smoke mode of macOS's macos_native_demo only sets the run time and does not print the counters).

A failed import does not interrupt playback. When a single frame fails to import, only that frame falls back to the CPU path and playback continues. Persistent failures are usually caused by insufficient video memory or a driver fault, and show up as growth in import_failures.

Linux GPU import#

On Linux, FFmpeg maps VA-API NV12 / P010 frames into Vulkan images for wgpu sampling. Version 0.3.7 removes the forced AV_HWFRAME_MAP_DIRECT flag that made some FFmpeg 8.1 / 9 driver paths fail with EINVAL. This experimental path still requires compatible devices, external memory and synchronization support.

The regular NVDEC and WSL OpenGL paths pass planes through the CPU; the optional WSL D3D12 path uses a GPU copy. A native Wayland video layer can reduce Flutter RGBA readback, but that is a separate matter from whether decoded frames are zero-copy. For build steps, see Linux integration.

Linear dma-buf output on Linux (Rust)#

export_frame_dmabuf(w, h, scale) drives playback and composites video, subtitles and danmaku, then uses one same-device GPU copy to produce linear RGBA8. GTK / EGL / Wayland hosts need no Vulkan device of their own. Import using raw_fd(), stride() and offset(); fourcc is ABGR8888 and modifier is 0. Slots are reused after three exports, so finish consuming and close the descriptor before reuse. This differs from the independently retained immutable export_frame_texture. The C API has no new dma-buf entry point.

Native texture ownership and import contracts

liberika v0.3.8 Section Kernel License MPL-2.0