Repository navigation
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
zIndexexactly this way: it sorts siblings byzIndexand removes and re-inserts the native views. In our app, a carousel of 3D models (one smallCanvasper model, rendered on demand) with an animatedzIndexlost the model to the left of the front one after every flick.The rest was written with a coding agent.
Cause
WebGPUTextureView.onSurfaceTextureDestroyedcallssurfaceOffscreen(), andSurfaceInfo::detachreplaces the surface with a new, empty offscreen texture.applyPendingAttachblits_texturewhenever it exists. So it presents the empty fallback.On the web, and on iOS where the
CAMetalLayermoves with its view, a moved canvas keeps its content.Fix
Keep the surface across a detach (
WebGPUTextureView,SurfaceInfo::park/unpark)View.dispatchDetachedFromWindowcallsonDetachedFromWindow()just beforeTextureView.onDetachedFromWindowInternalreleases the SurfaceTexture (the same order on API 26, 29 and 35). So the view marks a window detach, keeps its SurfaceTexture by returningfalsefromonSurfaceTextureDestroyed, and calls a newsurfaceParked().SurfaceInfo::park()keeps the Dawn surface and routes frames offscreen.setSurfaceTexture, but waits until it is bound to its layer again. TextureView does that on its next draw, then callsonSurfaceTextureUpdatedfromapplyUpdate()(forced bymUpdateSurface).surfaceUnparked().unpark()latches like an attach: it's taken at the next frame boundary, or byRNWebGPUManager::flushPendingSurfaceTransitionfor contexts that aren't rendering.Why keep the Dawn surface rather than make a new one for the kept window
Android's Vulkan loader connects an
ANativeWindowwhen theVkSurfaceKHRis created and disconnects it when it is destroyed, which Dawn does lazily. A new surface on the same window failed withconnect: already connected/native_window_api_connect() failedon our device, and every canvas went blank.When the kept surface can't be used: the device changed or was destroyed while parked
releaseSurfaceForDeviceandunpark()give the parked surface up together with its window, andunpark()returnsfalse.WebGPUViewthen replaces its backing TextureView with a new one, which gets a new window through the standardonSurfaceTextureAvailable. 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.configure()names another device.Blit gating (any adoption, not only unpark)
Unchanged: SurfaceViews, the HardwareBuffer view,
MakeWebGPUCanvasContextFromNativeWindowsurfaces, and iOS, which never parks.Known limitations
unpark()has succeeded but before the surface is adopted (for example a concurrentconfigure()with a new device, ordevice.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.Evidence
From our app (fiddl) on react-native-webgpu 0.21.0 with the same change ported (older
SurfaceRegistry.hlayout, same logic), not from this branch. Solana Seeker (Android 16, Mali-G615), Release build, carousel with an animatedzIndex, scripted flicks:already connectederrorsWith this change:
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, thenassembleDebugof 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.Reproducing
Put a few
Canvaselements side by side as siblings, draw each once (no render loop), and give them azIndexthat 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 onmain. This branch should keep its picture. That outcome is expected from the change, but I haven't run this exact reproducer.