Skip to content

Commit

Permalink
ARTSurfaceViewShadowNode: redraw itself if the host resumed the execu…
Browse files Browse the repository at this point in the history
…tion

Starting on API level 25, Android will destroy the TextureView's TextureLayer when the
an app is sent to the background and automatically create a new one once the TextureView is
drawed again. This causes a problem for ARTSurfaceView, because this component does not draw itself
and the Android API explicitly forbids to do such action. Since there's no listeners available to know
when the new TextureLayer was created, the ARTSufurfaceViewShadowNode will listen for life cycle events
and redraw its children once the activity is resumed.

It's was also discussed that ARTSurfaceView should extends SurfaceView and not TextureView, however
this may introduce two problems:

* SurfaceView is not hardware accelerated, which may cause impact on performance
* SurfaceView does not support opacity, so it will be impossible to set the ARTSufraceView as
transparent

With this problems in mind, this commit keeps the ARTSurfaceView extending TextureView.

Fixes facebook#17565
  • Loading branch information
cabelitos committed Dec 12, 2018
1 parent 4129ae0 commit 9f3b11c
Showing 1 changed file with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,23 @@
import android.graphics.PorterDuff;
import android.graphics.SurfaceTexture;
import android.view.TextureView;
import android.os.Build;

import com.facebook.common.logging.FLog;
import com.facebook.react.common.ReactConstants;
import com.facebook.react.uimanager.LayoutShadowNode;
import com.facebook.react.uimanager.UIViewOperationQueue;
import com.facebook.react.uimanager.ReactShadowNode;
import com.facebook.react.uimanager.ViewProps;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.annotations.ReactProp;
import com.facebook.react.bridge.LifecycleEventListener;

/**
* Shadow node for ART virtual tree root - ARTSurfaceView
*/
public class ARTSurfaceViewShadowNode extends LayoutShadowNode
implements TextureView.SurfaceTextureListener {
implements TextureView.SurfaceTextureListener, LifecycleEventListener {

private @Nullable Surface mSurface;

Expand Down Expand Up @@ -96,6 +99,33 @@ private void markChildrenUpdatesSeen(ReactShadowNode shadowNode) {
}
}

@Override
public void setThemedContext(ThemedReactContext themedContext) {
super.setThemedContext(themedContext);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) {
themedContext.addLifecycleEventListener(this);
}
}

@Override
public void dispose() {
super.dispose();
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) {
getThemedContext().removeLifecycleEventListener(this);
}
}

@Override
public void onHostResume() {
drawOutput();
}

@Override
public void onHostPause() {}

@Override
public void onHostDestroy() {}

@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
mSurface = new Surface(surface);
Expand Down

0 comments on commit 9f3b11c

Please sign in to comment.