Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions packages/webgpu/android/cpp/cpp-adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 &registry = 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 &registry = 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,
Expand Down
17 changes: 16 additions & 1 deletion packages/webgpu/android/src/main/java/com/webgpu/WebGPUAPI.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.webgpu;

import android.view.Surface;
import android.view.View;

import com.facebook.proguard.annotations.DoNotStrip;

Expand All @@ -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 {

Expand All @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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));
}
}
}
42 changes: 42 additions & 0 deletions packages/webgpu/android/src/main/java/com/webgpu/WebGPUView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down
Loading