Skip to content

Commit

Permalink
fix(🐛): fix concurrency issue with surface events (#2505)
Browse files Browse the repository at this point in the history
  • Loading branch information
wcandillon committed Jun 28, 2024
1 parent b657c9f commit fafff4c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public abstract class SkiaBaseView extends ReactViewGroup implements TextureView

private String tag = "SkiaView";

private boolean isDropped = false;

public SkiaBaseView(Context context) {
super(context);
mTexture = new TextureView(context);
Expand All @@ -21,11 +23,6 @@ public SkiaBaseView(Context context) {
addView(mTexture);
}

public void destroySurface() {
Log.i(tag, "destroySurface");
surfaceDestroyed();
}

private void createSurfaceTexture() {
// This API Level is >= 26, we created our own SurfaceTexture to have a faster time to first frame
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
Expand All @@ -36,6 +33,11 @@ private void createSurfaceTexture() {
}
}

void dropInstance() {
isDropped = true;
unregisterView();
}

@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
Expand Down Expand Up @@ -129,6 +131,9 @@ public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int hei

@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
if (isDropped) {
return;
}
Log.i(tag, "onSurfaceTextureSizeChanged " + width + "/" + height);
surfaceSizeChanged(width, height);
}
Expand All @@ -137,12 +142,14 @@ public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int h
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
Log.i(tag, "onSurfaceTextureDestroyed");
// https://developer.android.com/reference/android/view/TextureView.SurfaceTextureListener#onSurfaceTextureDestroyed(android.graphics.SurfaceTexture)
destroySurface();
surfaceDestroyed();
// Because of React Native Screens (which dettach the view), we always keep the surface alive.
// If not, Texture view will recreate the texture surface by itself and
// we will lose the fast first time to frame.
// We only delete the surface when the view is dropped (destroySurface invoked by SkiaBaseViewManager);
createSurfaceTexture();
if (!isDropped) {
createSurfaceTexture();
}
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public void setDebug(T view, boolean show) {
@Override
public void onDropViewInstance(@NonNull ReactViewGroup view) {
super.onDropViewInstance(view);
((SkiaBaseView)view).destroySurface();
((SkiaBaseView)view).unregisterView();
((SkiaBaseView)view).dropInstance();
}
}

0 comments on commit fafff4c

Please sign in to comment.