Skip to content

Commit

Permalink
Fix issues with SurfaceTexture management (#1864)
Browse files Browse the repository at this point in the history
  • Loading branch information
wcandillon authored Sep 24, 2023
1 parent 14307b5 commit 7379f5c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,35 @@ public abstract class SkiaBaseView extends ReactViewGroup implements TextureView

private String tag = "SkiaView";

public SkiaBaseView(Context context) {
private boolean manageTexture = false;

public SkiaBaseView(Context context, boolean manageTexture) {
super(context);
this.manageTexture = manageTexture;
mTexture = new TextureView(context);
mTexture.setSurfaceTextureListener(this);
mTexture.setOpaque(false);
addView(mTexture);
}

public void destroySurface() {
Log.i(tag, "destroySurface");
surfaceDestroyed();
mSurface.release();
mSurface = null;
if (mSurface != null) {
Log.i(tag, "destroySurface");
surfaceDestroyed();
mSurface.release();
mSurface = null;
}
}

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) {
Log.i(tag, "Create SurfaceTexture");
SurfaceTexture surface = new SurfaceTexture(false);
mTexture.setSurfaceTexture(surface);
this.onSurfaceTextureAvailable(surface, this.getMeasuredWidth(), this.getMeasuredHeight());
if (manageTexture) {
// 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) {
Log.i(tag, "Create SurfaceTexture");
SurfaceTexture surface = new SurfaceTexture(false);
mTexture.setSurfaceTexture(surface);
this.onSurfaceTextureAvailable(surface, this.getMeasuredWidth(), this.getMeasuredHeight());
}
}
}

Expand Down Expand Up @@ -145,7 +152,7 @@ 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)
surfaceDestroyed();
destroySurface();
createSurfaceTexture();
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void setDebug(ReactViewGroup view, boolean show) {
@Override
public void onDropViewInstance(@NonNull ReactViewGroup view) {
super.onDropViewInstance(view);
((SkiaBaseView)view).unregisterView();
((SkiaBaseView)view).destroySurface();
((SkiaBaseView)view).unregisterView();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class SkiaDomView extends SkiaBaseView {
private HybridData mHybridData;

public SkiaDomView(Context context) {
super(context);
super(context, true);
RNSkiaModule skiaModule = ((ReactContext) context).getNativeModule(RNSkiaModule.class);
mHybridData = initHybrid(skiaModule.getSkiaManager());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class SkiaDrawView extends SkiaBaseView {
private HybridData mHybridData;

public SkiaDrawView(Context context) {
super(context);
super(context, false);
RNSkiaModule skiaModule = ((ReactContext) context).getNativeModule(RNSkiaModule.class);
mHybridData = initHybrid(skiaModule.getSkiaManager());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class SkiaPictureView extends SkiaBaseView {
private HybridData mHybridData;

public SkiaPictureView(Context context) {
super(context);
super(context, true);
RNSkiaModule skiaModule = ((ReactContext) context).getNativeModule(RNSkiaModule.class);
mHybridData = initHybrid(skiaModule.getSkiaManager());
}
Expand Down

0 comments on commit 7379f5c

Please sign in to comment.