Skip to content

fix(🤖): keep a TextureView canvas's frame when the view is moved - #657

Open
camchis wants to merge 1 commit into
wcandillon:mainfrom
camchis:fix/android-restore-frame-on-reattach
Open

camchis wants to merge 1 commit into
wcandillon:mainfrom
camchis:fix/android-restore-frame-on-reattach

Conversation

@camchis

@camchis camchis commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Description

On Android with the TextureView backing, a canvas that is moved in the view hierarchy comes back blank unless it draws again. React Native's Fabric applies a change of zIndex exactly this way: it sorts siblings by zIndex and removes and re-inserts the native views. In our app, a carousel of 3D models (one small Canvas per model, rendered on demand) with an animated zIndex lost the model to the left of the front one after every flick.

The rest was written with a coding agent.

Cause

  • A TextureView releases its SurfaceTexture when it is detached from its window.
  • WebGPUTextureView.onSurfaceTextureDestroyed calls surfaceOffscreen(), and SurfaceInfo::detach replaces the surface with a new, empty offscreen texture.
  • On reattach the TextureView creates a new SurfaceTexture, the canvas gets a new Dawn surface, and applyPendingAttach blits _texture whenever it exists. So it presents the empty fallback.
  • A canvas that renders every frame recovers on its next frame. A canvas at rest stays blank.

On the web, and on iOS where the CAMetalLayer moves with its view, a moved canvas keeps its content.

Fix

Keep the surface across a detach (WebGPUTextureView, SurfaceInfo::park / unpark)

  • View.dispatchDetachedFromWindow calls onDetachedFromWindow() just before TextureView.onDetachedFromWindowInternal releases the SurfaceTexture (the same order on API 26, 29 and 35). So the view marks a window detach, keeps its SurfaceTexture by returning false from onSurfaceTextureDestroyed, and calls a new surfaceParked().
  • SurfaceInfo::park() keeps the Dawn surface and routes frames offscreen.
    • While parked, nothing is adopted: neither the kept surface nor a surface that attached before the view left.
    • A detach also cancels a resume that hasn't been taken yet.
  • On reattach the view hands the SurfaceTexture back with setSurfaceTexture, but waits until it is bound to its layer again. TextureView does that on its next draw, then calls onSurfaceTextureUpdated from applyUpdate() (forced by mUpdateSurface).
  • Only then does the view call surfaceUnparked(). unpark() latches like an attach: it's taken at the next frame boundary, or by RNWebGPUManager::flushPendingSurfaceTransition for contexts that aren't rendering.
  • The kept surface is reconfigured only if its configuration changed while parked, so it still shows its last frame.

Why keep the Dawn surface rather than make a new one for the kept window
Android's Vulkan loader connects an ANativeWindow when the VkSurfaceKHR is created and disconnects it when it is destroyed, which Dawn does lazily. A new surface on the same window failed with connect: already connected / native_window_api_connect() failed on our device, and every canvas went blank.

When the kept surface can't be used: the device changed or was destroyed while parked

  • Recreating a surface on the kept window would hit that same reconnect failure. So releaseSurfaceForDevice and unpark() give the parked surface up together with its window, and unpark() returns false.
  • WebGPUView then replaces its backing TextureView with a new one, which gets a new window through the standard onSurfaceTextureAvailable. The replacement only happens if the old view is still the current, attached one; a view that left its window again retries when it is next attached.
  • A released device is not configured again, acquired from, or blitted from, until configure() names another device.

Blit gating (any adoption, not only unpark)

  • The offscreen texture is blitted only if it holds a presented frame. Starting another offscreen frame withdraws it until that one is presented, so a frame abandoned half-drawn never shows.
  • An onscreen present retires it.
  • Before the first surface, any acquired frame counts, as before (the fast-first-frame path).

Unchanged: SurfaceViews, the HardwareBuffer view, MakeWebGPUCanvasContextFromNativeWindow surfaces, and iOS, which never parks.

Known limitations

  • A device change racing a resume. A device change in the short interval after unpark() has succeeded but before the surface is adopted (for example a concurrent configure() with a new device, or device.destroy()) takes the existing same-window recreation path, as for any attached canvas today. If the old connection is still held, that surface may not reconnect. This was traced in code review, not observed.
  • A released device and the offscreen fallback. After a device is released and before the app configures a new one, resizing and the offscreen fallback can still create textures on the released device. This is unchanged from today.

Evidence

From our app (fiddl) on react-native-webgpu 0.21.0 with the same change ported (older SurfaceRegistry.h layout, same logic), not from this branch. Solana Seeker (Android 16, Mali-G615), Release build, carousel with an animated zIndex, scripted flicks:

Build Model left of the front after each flick already connected errors
0.21.0 as released missing, every time not counted
keep the SurfaceTexture, new Dawn surface (rejected) every canvas blank 145
this change present, every time 0

With this change:

  • The playing front model keeps animating after being moved.
  • Closing and reopening the carousel, and backgrounding and foregrounding the app, keep every model, with no crash.

The device give-up and backing-view replacement paths could not be triggered from the app and are not covered by these runs.

What I ran on this branch

  • yarn install, then assembleDebug of the example app for x86_64 (as CI) and arm64-v8a: both pass, with no warnings from the changed files.
  • cpplint (the repo's flags) and clang-format on the changed C++ are clean.
  • Not run on a device from this branch: the evidence above is from the 0.21.0 port.

Reproducing

Put a few Canvas elements side by side as siblings, draw each once (no render loop), and give them a zIndex that changes after mount, for example from state on a button press. On Android with the default TextureView backing, a canvas that is re-inserted comes back blank on main. This branch should keep its picture. That outcome is expected from the change, but I haven't run this exact reproducer.

Android moves a view by detaching it from its window and attaching it again,
which is also how React Native applies a change of zIndex. A TextureView
releases its SurfaceTexture on detach, the canvas fell back to an empty
offscreen texture, and on reattach that empty texture was blitted and
presented: a canvas that was not drawing every frame came back blank.

The TextureView now keeps its SurfaceTexture across a detach and the native
side parks the canvas (SurfaceInfo::park): nothing is adopted and frames
render offscreen while the view is out of its window, and a detach cancels a
resume not yet taken. When the view binds the SurfaceTexture to its layer
again (TextureView reports onSurfaceTextureUpdated from that draw), the kept
surface is handed back at the next frame boundary (unpark), reconfigured only
if its configuration changed, so it still shows its last frame.

A new surface for the kept window cannot connect while Dawn still holds the
old one, so a parked surface whose device changes or is destroyed before the
view asks for it back is given up with its window, and the canvas replaces
its backing TextureView with a new one, which gets a new window the usual way.
A released device is not configured again, nor its offscreen frame blitted,
until configure() names another device.

On any adoption, the offscreen texture is blitted only if it holds a frame
that was presented (before the first surface, any acquired frame, as before);
starting another offscreen frame withdraws it until that one is presented,
and an onscreen present retires it.

This branch has not been deployed

No deployments
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