From 6386487234c4166f02b40c9fbd69e122d0c292b6 Mon Sep 17 00:00:00 2001 From: Cameron Chisholm Date: Thu, 26 Nov 2026 19:14:38 +0000 Subject: [PATCH] =?UTF-8?q?fix(=F0=9F=A4=96):=20keep=20a=20TextureView=20c?= =?UTF-8?q?anvas's=20frame=20when=20the=20view=20is=20moved?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- packages/webgpu/android/cpp/cpp-adapter.cpp | 33 +++ .../src/main/java/com/webgpu/WebGPUAPI.java | 17 +- .../java/com/webgpu/WebGPUTextureView.java | 92 ++++++- .../src/main/java/com/webgpu/WebGPUView.java | 42 +++ packages/webgpu/cpp/rnwgpu/SurfaceRegistry.h | 249 +++++++++++++++--- 5 files changed, 397 insertions(+), 36 deletions(-) diff --git a/packages/webgpu/android/cpp/cpp-adapter.cpp b/packages/webgpu/android/cpp/cpp-adapter.cpp index 03112b7200..2d496bca08 100644 --- a/packages/webgpu/android/cpp/cpp-adapter.cpp +++ b/packages/webgpu/android/cpp/cpp-adapter.cpp @@ -400,6 +400,39 @@ Java_com_webgpu_WebGPUView_switchToOffscreenSurface(JNIEnv *env, jobject thiz, } } +// A TextureView left its window but kept its SurfaceTexture: keep the surface +// too, rendering offscreen until the view is back (see SurfaceInfo::park). +extern "C" JNIEXPORT void JNICALL Java_com_webgpu_WebGPUView_parkSurface( + JNIEnv *env, jobject thiz, jint contextId) { + auto ®istry = rnwgpu::SurfaceRegistry::getInstance(); + if (auto info = registry.getSurfaceInfo(contextId)) { + info->park(); + } +} + +// The TextureView is back in a window with the SurfaceTexture it kept: use the +// kept surface again at the next frame boundary (see SurfaceInfo::unpark). +// Returns false when the surface was given up while parked: the view then +// starts over with a new SurfaceTexture. True when there is no entry (nothing +// to start over for). +extern "C" JNIEXPORT jboolean JNICALL Java_com_webgpu_WebGPUView_unparkSurface( + JNIEnv *env, jobject thiz, jint contextId) { + if (manager == nullptr) { + return JNI_TRUE; + } + auto ®istry = rnwgpu::SurfaceRegistry::getInstance(); + auto info = registry.getSurfaceInfo(contextId); + if (!info) { + return JNI_TRUE; + } + if (!info->unpark()) { + return JNI_FALSE; + } + // Contexts that are not rendering pick it up here. + manager->flushPendingSurfaceTransition(info); + return JNI_TRUE; +} + // See AnimationReporter::releaseIfIdle; 0 when the surface reports nothing. extern "C" JNIEXPORT jlong JNICALL Java_com_webgpu_WebGPUView_releaseFrameRateVoteIfIdle(JNIEnv *env, jobject thiz, diff --git a/packages/webgpu/android/src/main/java/com/webgpu/WebGPUAPI.java b/packages/webgpu/android/src/main/java/com/webgpu/WebGPUAPI.java index 219a649fd7..abc5bb6c7c 100644 --- a/packages/webgpu/android/src/main/java/com/webgpu/WebGPUAPI.java +++ b/packages/webgpu/android/src/main/java/com/webgpu/WebGPUAPI.java @@ -1,6 +1,7 @@ package com.webgpu; import android.view.Surface; +import android.view.View; import com.facebook.proguard.annotations.DoNotStrip; @@ -9,7 +10,8 @@ * itself is owned by the JS Canvas component (created lazily, removed via * RNWebGPU.destroyContext on unmount); views only attach and detach surfaces. * A detached context keeps rendering into an offscreen texture whose content - * is blitted onto the next attached surface. + * is blitted onto the next attached surface. A parked one keeps its surface + * for when the view is back (see WebGPUTextureView). */ public interface WebGPUAPI { @@ -22,4 +24,17 @@ void surfaceChanged( ); void surfaceOffscreen(); + + // The view left its window but kept its surface: keep it, rendering + // offscreen, until surfaceUnparked(). + void surfaceParked(); + + // The view is back in a window with the surface it kept. False when the + // native side gave that surface up while the view was away. + boolean surfaceUnparked(); + + // The native side gave up the surface this backing view kept (see + // surfaceUnparked()): replace the view with a new one, which gets a new + // window the usual way. + void backingViewLost(View view); } diff --git a/packages/webgpu/android/src/main/java/com/webgpu/WebGPUTextureView.java b/packages/webgpu/android/src/main/java/com/webgpu/WebGPUTextureView.java index 27c2e2dc64..4136fb17da 100644 --- a/packages/webgpu/android/src/main/java/com/webgpu/WebGPUTextureView.java +++ b/packages/webgpu/android/src/main/java/com/webgpu/WebGPUTextureView.java @@ -8,12 +8,31 @@ import android.view.TextureView; import android.view.View; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; @SuppressLint("ViewConstructor") public class WebGPUTextureView extends TextureView implements TextureView.SurfaceTextureListener { WebGPUAPI mApi; private Surface mSurface; + // A canvas keeps its picture when it is moved, as it does on the web and on + // iOS (where the CAMetalLayer moves with its view). 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, and a TextureView releases its + // SurfaceTexture on detach. So the SurfaceTexture a detach releases is kept, + // with the last frame it latched, and the native side keeps its surface too + // (WebGPUAPI.surfaceParked); on reattach both are handed back, and the + // canvas shows that frame without being drawn again. mDetaching marks the + // release a detach causes: View calls onDetachedFromWindow() just before + // TextureView releases the SurfaceTexture (onDetachedFromWindowInternal). + private boolean mDetaching = false; + @Nullable + private SurfaceTexture mKeptSurfaceTexture = null; + // Set by release(): the view is gone for good and keeps nothing. + private boolean mReleased = false; + // The kept SurfaceTexture was handed back and waits for this view to bind + // it to its layer again before the canvas uses it (onSurfaceTextureUpdated). + private boolean mUnparkOnBind = false; public WebGPUTextureView(Context context, WebGPUAPI api, boolean opaque) { super(context); @@ -33,6 +52,52 @@ public void setOpaque(boolean opaque) { super.setOpaque(opaque && Build.VERSION.SDK_INT != Build.VERSION_CODES.Q); } + @Override + protected void onAttachedToWindow() { + super.onAttachedToWindow(); + mDetaching = false; + SurfaceTexture kept = mKeptSurfaceTexture; + mKeptSurfaceTexture = null; + if (kept == null) { + return; + } + // TextureView reports onSurfaceTextureAvailable() only for a + // SurfaceTexture it creates itself, so the canvas takes its surface back + // here. Its size may have changed while it was out of the window. + setSurfaceTexture(kept); + // TextureView binds a SurfaceTexture it is handed to its layer when it next + // draws; until then nothing would consume the frames presented to it. So + // the canvas stays parked until then (onSurfaceTextureUpdated). + mApi.surfaceChanged(mSurface); + mUnparkOnBind = true; + } + + + @Override + protected void onDetachedFromWindow() { + mUnparkOnBind = false; + mDetaching = !mReleased; + super.onDetachedFromWindow(); + } + + /** + * The view is gone for good (WebGPUView.destroy(), after the native side has + * let go of the surface): release what a detach kept, and keep nothing from + * a detach still to come. + */ + void release() { + mReleased = true; + mUnparkOnBind = false; + if (mKeptSurfaceTexture != null) { + if (mSurface != null) { + mSurface.release(); + mSurface = null; + } + mKeptSurfaceTexture.release(); + mKeptSurfaceTexture = null; + } + } + @Override public void onSurfaceTextureAvailable(@NonNull SurfaceTexture surfaceTexture, int width, int height) { mSurface = new Surface(surfaceTexture); @@ -46,6 +111,14 @@ public void onSurfaceTextureSizeChanged(@NonNull SurfaceTexture surfaceTexture, @Override public boolean onSurfaceTextureDestroyed(@NonNull SurfaceTexture surfaceTexture) { + if (mDetaching) { + // Kept, with mSurface, for onAttachedToWindow(); release() frees them if + // the view never comes back. + mDetaching = false; + mKeptSurfaceTexture = surfaceTexture; + mApi.surfaceParked(); + return false; + } // Detach first (synchronous through JNI) so the native side has dropped // its window reference before we release ours. mApi.surfaceOffscreen(); @@ -76,8 +149,25 @@ void latchPresentedFrame() { super.onVisibilityChanged(this, View.VISIBLE); } + /** + * TextureView calls this from its draw after it latched a new frame, and + * also right after it bound a SurfaceTexture it was handed to its layer + * (getTextureLayer() forces that update). So a surface waiting for the bind + * can be handed to the canvas now. Main thread. + */ @Override public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surfaceTexture) { - // No implementation needed + if (!mUnparkOnBind || mReleased) { + return; + } + mUnparkOnBind = false; + if (!mApi.surfaceUnparked()) { + // The native side gave the kept surface up while the view was away (its + // device changed or was destroyed), and a new surface for the same + // window could not connect. The canvas replaces this view with a new + // one, which gets a new window the usual way. Posted: this draw is still + // using the view. + post(() -> mApi.backingViewLost(this)); + } } } diff --git a/packages/webgpu/android/src/main/java/com/webgpu/WebGPUView.java b/packages/webgpu/android/src/main/java/com/webgpu/WebGPUView.java index 17fc3846a1..ba451bce63 100644 --- a/packages/webgpu/android/src/main/java/com/webgpu/WebGPUView.java +++ b/packages/webgpu/android/src/main/java/com/webgpu/WebGPUView.java @@ -35,6 +35,8 @@ public class WebGPUView extends ReactViewGroup implements WebGPUAPI { private View mView = null; @Nullable private BackingViewKind mKind = null; + // Set by destroy(): the canvas is gone. + private boolean mDestroyed = false; // Only a SurfaceView votes: a TextureView is composited into the window, // which votes for it. private final SurfaceFrameRateVote mFrameRateVote = new SurfaceFrameRateVote( @@ -194,6 +196,36 @@ public void surfaceChanged(Surface surface) { onSurfaceChanged(surface, mContextId, width, height); } + @Override + public void surfaceParked() { + parkSurface(mContextId); + } + + @Override + public boolean surfaceUnparked() { + return unparkSurface(mContextId); + } + + @Override + public void backingViewLost(View view) { + // Posted by the view; it may have been replaced or this canvas dropped since. + // A view that left its window again in the meantime has parked the canvas + // once more: it retries when it is attached again (another unpark that + // fails), and replacing it now would leave the new view stuck behind that + // park, since removing a detached view reports no detach. + if (view != mView || mDestroyed || mKind != BackingViewKind.TEXTURE_VIEW + || !view.isAttachedToWindow()) { + return; + } + WebGPUTextureView lost = (WebGPUTextureView) mView; + // Released first, so its SurfaceTexture goes with it rather than being kept. + lost.release(); + removeView(lost); + mView = new WebGPUTextureView(getContext(), this, mOpaque); + addView(mView); + layoutChild(); + } + @Override public void surfaceOffscreen() { // The surface is still valid here, so the vote can be taken back. @@ -227,8 +259,12 @@ void onFramePresentedOnMainThread() { * view: the view dies with its Canvas, so it retires the registry entry. */ public void destroy() { + mDestroyed = true; mFrameRateVote.detach(); onViewDestroyed(mContextId); + if (mView instanceof WebGPUTextureView) { + ((WebGPUTextureView) mView).release(); + } } // reportAnimation asks native to call onAnimating() when frames start being @@ -256,6 +292,12 @@ private native void onSurfaceChanged( @DoNotStrip private native void switchToOffscreenSurface(int contextId); + @DoNotStrip + private native void parkSurface(int contextId); + + @DoNotStrip + private native boolean unparkSurface(int contextId); + @DoNotStrip private native void onViewDestroyed(int contextId); diff --git a/packages/webgpu/cpp/rnwgpu/SurfaceRegistry.h b/packages/webgpu/cpp/rnwgpu/SurfaceRegistry.h index 0c06bb6421..81cc452afc 100644 --- a/packages/webgpu/cpp/rnwgpu/SurfaceRegistry.h +++ b/packages/webgpu/cpp/rnwgpu/SurfaceRegistry.h @@ -73,6 +73,17 @@ using NativeSurfaceReleaser = std::function; // content appears without waiting for the next render — the same mechanism // that gives a fast time-to-first-frame when rendering starts before the // native surface exists. +// - Parking (park/unpark) is for a view that leaves its window but keeps the +// window this surface draws to: an Android TextureView keeps its +// SurfaceTexture across a detach (see WebGPUTextureView.java), which is how +// Android moves a view, and how React Native applies a change of zIndex. +// The Dawn surface is kept as well and frames render offscreen until the +// view is back; unpark() then hands the kept surface back at the next frame +// boundary. Nothing is reconfigured or blitted unless something changed in +// the meantime, so the canvas still shows its last frame without being drawn +// again, as a canvas that is moved does on the web. A new surface for the +// same window could not even be made: the window stays connected to the old +// VkSurfaceKHR until Dawn destroys it, which it does lazily. class SurfaceInfo { public: // releaseSurfaceOnUnconfigure is for a surface on a window the caller @@ -155,6 +166,60 @@ class SurfaceInfo { // then. Safe to call when already offscreen. void switchToOffscreen() { detach(/* createFallbackTexture = */ true); } + // The view left its window but kept the window this surface draws to: keep + // the surface and render offscreen until unpark(). A frame already acquired + // from the surface still presents. Repeating it is harmless. + void park() { + std::unique_lock lock(_mutex); + // Whatever surface the view has, adopted or still pending, waits for the + // view; a detach also cancels an unpark that has not been taken yet. + _parked = true; + _unparkPending = false; + } + + // The view is back in a window with the window it kept: use the kept surface + // again at the next frame boundary (applyPendingAttach), like an attach. + // Callers should follow up with + // RNWebGPUManager::flushPendingSurfaceTransition. + // Returns false when there is nothing to hand back: the surface was given up + // while parked (its device changed or was destroyed, see giveUpParkedLocked) + // or there never was one. The view then starts over with a new window, as a + // new view would. + bool unpark() { + void *released = nullptr; + NativeSurfaceReleaser releaser; + bool kept = true; + { + std::unique_lock lock(_mutex); + if (!_parked) { + return true; + } +#if defined(__ANDROID__) + // DAWN_WORKAROUND_DEVICE_DESTROY_BEFORE_SURFACE_RELEASE: configuring + // the kept surface for another device would make a new surface for the + // same window, which cannot connect while Dawn still holds this one. + if (_surface && _surfaceDevice && _config.device != nullptr && + _surfaceDevice.Get() != _config.device.Get()) { + giveUpParkedLocked(&released, &releaser); + } +#endif + kept = !_parkedLost && (_surface || _hasPendingAttach); + if (kept) { + _unparkPending = true; + } else { + _parked = false; + _unparkPending = false; + _parkedReconfigure = false; + _parkedLost = false; + _frameEpoch++; + } + } + if (releaser && released) { + releaser(released); + } + return kept; + } + // Detach without creating the offscreen fallback: used when the context is // being destroyed and nothing will consume further frames. void detachSurface() { detach(/* createFallbackTexture = */ false); } @@ -192,44 +257,67 @@ class SurfaceInfo { _frameInFlight = false; _acquiredFromSurface = false; } - if (!_hasPendingAttach || _frameInFlight) { + // Nothing is adopted while the view is out of its window (parked): not + // the kept surface, and not a surface that attached before it left. + if (_frameInFlight || + (!_unparkPending && (_parked || !_hasPendingAttach))) { return; } - // Attach over attach without a detach in between: replace. Ownership - // tracks the native window pointer, not the Dawn surface handle (which - // can be null if surface creation failed). - replacedSurface = _nativeSurface; - replacedReleaser = std::move(_releaser); - _surface = std::move(_pendingSurface); + // A new surface is configured; a kept one only when its configuration + // changed while it was parked, so that it still shows its last frame. + bool needsConfigure = true; + if (_hasPendingAttach) { + // Attach over attach without a detach in between, or over a parked + // surface: replace. Ownership tracks the native window pointer, not + // the Dawn surface handle (which can be null if surface creation + // failed). + replacedSurface = _nativeSurface; + replacedReleaser = std::move(_releaser); + _surface = std::move(_pendingSurface); #if defined(__ANDROID__) - // DAWN_WORKAROUND_DEVICE_DESTROY_BEFORE_SURFACE_RELEASE - _surfaceDevice = nullptr; + // DAWN_WORKAROUND_DEVICE_DESTROY_BEFORE_SURFACE_RELEASE + _surfaceDevice = nullptr; #endif - _nativeSurface = _pendingNativeSurface; - _releaser = std::move(_pendingReleaser); - _hasPendingAttach = false; - _pendingNativeSurface = nullptr; + _nativeSurface = _pendingNativeSurface; + _releaser = std::move(_pendingReleaser); + _hasPendingAttach = false; + _pendingNativeSurface = nullptr; + } else { + needsConfigure = _parkedReconfigure; + } + _parked = false; + _unparkPending = false; + _parkedReconfigure = false; + _parkedLost = false; + if (_surface) { + _hadSurface = true; + } _frameEpoch++; // _surface can be null here when Dawn surface creation failed for a // valid native window; the context then just keeps rendering offscreen. - if (_config.device != nullptr && _surface) { - bool blit = _texture != nullptr; - // The blit needs CopyDst on the surface. Configure with a widened - // copy while keeping _config at the usage the user asked for, so any - // later reconfigure drops the extra flag again. - wgpu::SurfaceConfiguration config = _config; - if (blit) { - config.usage |= wgpu::TextureUsage::CopyDst; + if (_config.device != nullptr && !_configDeviceReleased && _surface) { + // Only a frame rendered offscreen is blitted: an empty fallback must + // not replace what a kept surface still shows. + bool blit = _texture != nullptr && _offscreenFrame; + if (needsConfigure || blit) { + // The blit needs CopyDst on the surface. Configure with a widened + // copy while keeping _config at the usage the user asked for, so + // any later reconfigure drops the extra flag again. + wgpu::SurfaceConfiguration config = _config; + if (blit) { + config.usage |= wgpu::TextureUsage::CopyDst; + } + configureSurfaceLocked(config); } - configureSurfaceLocked(config); if (blit) { presentBlit = blitOffscreenToSurfaceLocked(); device = _config.device; - // Consumed either way; on failure the next frame renders fresh. - _texture = nullptr; blitEpoch = _frameEpoch; } + // Consumed either way; on failure the next frame renders fresh. + _texture = nullptr; + _offscreenFrame = false; } } if (replacedReleaser && replacedSurface) { @@ -267,6 +355,7 @@ class SurfaceInfo { std::unique_lock lock(_mutex); _viewFormats = std::move(viewFormats); _config = newConfig; + _configDeviceReleased = false; // The caller's viewFormats storage dies with the call; point the stored // configuration at our own copy. _config.viewFormats = _viewFormats.empty() ? nullptr : _viewFormats.data(); @@ -345,14 +434,41 @@ class SurfaceInfo { // commit, so that decision is not forgotten. void releaseSurfaceForDevice(const wgpu::Device &device) { #if defined(__ANDROID__) - std::unique_lock lock(_mutex); - if (!_surface || !_surfaceDevice || _surfaceDevice.Get() != device.Get()) { - return; + void *released = nullptr; + NativeSurfaceReleaser releaser; + { + std::unique_lock lock(_mutex); + if (_config.device != nullptr && _config.device.Get() == device.Get()) { + // Nothing may configure a surface with the device from here on, or + // blit a frame it rendered: an adoption or unpark that is still to + // come would otherwise put a swapchain back on it. + _configDeviceReleased = true; + _texture = nullptr; + _offscreenFrame = false; + _frameOffscreen = false; + _frameEpoch++; + } + if (!_surface || !_surfaceDevice || + _surfaceDevice.Get() != device.Get()) { + return; + } + if (_parked && !_unparkPending) { + // A new surface for the window the view keeps could not connect + // while Dawn still holds this one: give the window up, and the view + // starts over with a new one when it comes back (unpark()). Once it + // has asked for its surface back it is no longer waiting to hear + // that, so the surface is recreated as for any attached canvas. + giveUpParkedLocked(&released, &releaser); + } else { + // Left unconfigured: _config still names the destroyed device, and + // configuring against it would only raise a validation error. The + // next configure() from JS (or a surface re-attach) configures it. + recreateSurfaceLocked(); + } + } + if (releaser && released) { + releaser(released); } - // Left unconfigured: _config still names the destroyed device, and - // configuring against it would only raise a validation error. The next - // configure() from JS (or a surface re-attach) configures it. - recreateSurfaceLocked(); #else (void)device; #endif @@ -384,11 +500,12 @@ class SurfaceInfo { } _frameInFlight = true; _acquiredFromSurface = false; + _frameOffscreen = false; _frameEpoch++; if (frameEpoch) { *frameEpoch = _frameEpoch; } - if (_surface) { + if (_surface && !_parked && !_configDeviceReleased) { auto texture = acquireSurfaceTextureLocked(); if (texture) { _acquiredFromSurface = true; @@ -401,6 +518,12 @@ class SurfaceInfo { if (!_texture) { _texture = createOffscreenTextureLocked(); } + _frameOffscreen = true; + // Before the first surface, an acquired frame is shown when one comes, as + // before (a fast first frame). After that, only a presented one: this + // frame draws into the same texture, so the previous frame is withdrawn + // until this one is presented, and one abandoned half-drawn never shows. + _offscreenFrame = !_hadSurface; return _texture; } @@ -412,7 +535,8 @@ class SurfaceInfo { // would. bool isCurrentFrame(uint64_t frameEpoch) { std::shared_lock lock(_mutex); - return _frameInFlight && !_hasPendingAttach && _frameEpoch == frameEpoch; + return _frameInFlight && !_hasPendingAttach && !_unparkPending && + _frameEpoch == frameEpoch; } // Present the current frame. Runs synchronously on the thread that did @@ -445,6 +569,14 @@ class SurfaceInfo { framePresented = _framePresented; #endif } + // What the frame left: a picture on the surface, newer than any offscreen + // one, or a completed offscreen frame to show when a surface comes back. + if (_surface && _acquiredFromSurface) { + _offscreenFrame = false; + } else if (_frameInFlight && _frameOffscreen) { + _offscreenFrame = true; + } + _frameOffscreen = false; _acquiredFromSurface = false; _frameInFlight = false; _frameEpoch++; @@ -501,9 +633,15 @@ class SurfaceInfo { releasers[0] = std::move(_pendingReleaser); _pendingNativeSurface = nullptr; } + _parked = false; + _unparkPending = false; + _parkedReconfigure = false; + _parkedLost = false; if (_surface) { if (createFallbackTexture && _config.device != nullptr) { _texture = createOffscreenTextureLocked(); + _offscreenFrame = false; + _frameOffscreen = false; } _surface = nullptr; #if defined(__ANDROID__) @@ -610,10 +748,16 @@ class SurfaceInfo { } void _configureLocked() { - if (_surface) { + if (_surface && !_parked && !_configDeviceReleased) { configureSurfaceLocked(_config); } else { + // A parked surface takes the new configuration when it is unparked. + if (_parked) { + _parkedReconfigure = true; + } _texture = createOffscreenTextureLocked(); + _offscreenFrame = false; + _frameOffscreen = false; } } @@ -641,6 +785,23 @@ class SurfaceInfo { #endif } +#if defined(__ANDROID__) + // Drop a parked surface together with its window: the view keeps the window + // for when it comes back, and unpark() then tells it to start over. The + // caller releases *released with *releaser outside the lock. + void giveUpParkedLocked(void **released, NativeSurfaceReleaser *releaser) { + _surface = nullptr; + // DAWN_WORKAROUND_DEVICE_DESTROY_BEFORE_SURFACE_RELEASE + _surfaceDevice = nullptr; + _acquiredFromSurface = false; + _frameEpoch++; + *released = _nativeSurface; + *releaser = std::move(_releaser); + _nativeSurface = nullptr; + _parkedLost = true; + } +#endif + // Replace _surface with a new, unconfigured surface for the same native // window: after unconfigure() destroyed it (see the constructor), or on // Android for DAWN_WORKAROUND_DEVICE_DESTROY_BEFORE_SURFACE_RELEASE, where @@ -682,6 +843,26 @@ class SurfaceInfo { NativeSurfaceReleaser _releaser; // Offscreen fallback drawing buffer. wgpu::Texture _texture = nullptr; + // Whether _texture holds a frame to blit onto the next adopted surface: the + // last offscreen frame that was presented, or before the first surface the + // last one acquired (see getCurrentTexture). + bool _offscreenFrame = false; + // The frame in flight renders into _texture (set by getCurrentTexture). + bool _frameOffscreen = false; + // A surface has been adopted at least once. + bool _hadSurface = false; + // The surface is kept while its view is out of a window (park): frames + // render offscreen. _unparkPending hands it back at the next frame boundary; + // _parkedReconfigure says the configuration changed in the meantime. + bool _parked = false; + bool _unparkPending = false; + bool _parkedReconfigure = false; + // The parked surface and its window were given up (giveUpParkedLocked). + bool _parkedLost = false; + // _config names a device that was released (releaseSurfaceForDevice): no + // surface is configured with it, and its offscreen frame is gone, until + // configure() names another. + bool _configDeviceReleased = false; // Surface attached by the UI thread, awaiting adoption at a frame boundary. bool _hasPendingAttach = false; void *_pendingNativeSurface = nullptr;