From a5f3571770e9f5da454f4eb8eff8e57de7acd551 Mon Sep 17 00:00:00 2001 From: Ram N Date: Wed, 3 Oct 2018 13:39:11 -0700 Subject: [PATCH] Refactor the usages of ChoreographerCompat Summary: Reducing the places where we directly access `ChoreographerCompat.getInstance()`. Since this is a singleton anyway, there was no need to pass this as an argument. In subsequent diffs, we will also ensure that `ChoreographerCompat.getInstance()` runs on the UI thread, so that the `Choreographer` it gets is based on the `Looper` of the main thread. Reviewed By: achen1 Differential Revision: D10136624 fbshipit-source-id: ad18f7b61eb8b05094aff310f2eb90eb225427dc --- .../facebook/react/devsupport/FpsView.java | 2 +- .../modules/debug/AnimationsDebugModule.java | 1 - .../modules/debug/FpsDebugFrameCallback.java | 20 +++++++++++++------ 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/devsupport/FpsView.java b/ReactAndroid/src/main/java/com/facebook/react/devsupport/FpsView.java index bcc5e3345..d190c0c40 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/devsupport/FpsView.java +++ b/ReactAndroid/src/main/java/com/facebook/react/devsupport/FpsView.java @@ -39,7 +39,7 @@ public class FpsView extends FrameLayout { super(reactContext); inflate(reactContext, R.layout.fps_view, this); mTextView = (TextView) findViewById(R.id.fps_text); - mFrameCallback = new FpsDebugFrameCallback(ChoreographerCompat.getInstance(), reactContext); + mFrameCallback = new FpsDebugFrameCallback(reactContext); mFPSMonitorRunnable = new FPSMonitorRunnable(); setCurrentFPS(0, 0, 0, 0); } diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/debug/AnimationsDebugModule.java b/ReactAndroid/src/main/java/com/facebook/react/modules/debug/AnimationsDebugModule.java index 69b0f70a1..df103404c 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/debug/AnimationsDebugModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/debug/AnimationsDebugModule.java @@ -59,7 +59,6 @@ public class AnimationsDebugModule extends ReactContextBaseJavaModule { } mFrameCallback = new FpsDebugFrameCallback( - ChoreographerCompat.getInstance(), getReactApplicationContext()); mFrameCallback.startAndRecordFpsAtEachFrame(); } diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/debug/FpsDebugFrameCallback.java b/ReactAndroid/src/main/java/com/facebook/react/modules/debug/FpsDebugFrameCallback.java index f0de89626..eab166f2a 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/debug/FpsDebugFrameCallback.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/debug/FpsDebugFrameCallback.java @@ -7,6 +7,7 @@ package com.facebook.react.modules.debug; +import com.facebook.react.bridge.UiThreadUtil; import javax.annotation.Nullable; import java.util.Map; @@ -59,7 +60,7 @@ public class FpsDebugFrameCallback extends ChoreographerCompat.FrameCallback { private static final double EXPECTED_FRAME_TIME = 16.9; - private final ChoreographerCompat mChoreographer; + private @Nullable ChoreographerCompat mChoreographer; private final ReactContext mReactContext; private final UIManagerModule mUIManagerModule; private final DidJSUpdateUiDuringFrameDetector mDidJSUpdateUiDuringFrameDetector; @@ -74,8 +75,7 @@ public class FpsDebugFrameCallback extends ChoreographerCompat.FrameCallback { private boolean mIsRecordingFpsInfoAtEachFrame = false; private @Nullable TreeMap mTimeToFps; - public FpsDebugFrameCallback(ChoreographerCompat choreographer, ReactContext reactContext) { - mChoreographer = choreographer; + public FpsDebugFrameCallback(ReactContext reactContext) { mReactContext = reactContext; mUIManagerModule = reactContext.getNativeModule(UIManagerModule.class); mDidJSUpdateUiDuringFrameDetector = new DidJSUpdateUiDuringFrameDetector(); @@ -120,8 +120,9 @@ public class FpsDebugFrameCallback extends ChoreographerCompat.FrameCallback { mTimeToFps.put(System.currentTimeMillis(), info); } mExpectedNumFramesPrev = expectedNumFrames; - - mChoreographer.postFrameCallback(this); + if (mChoreographer != null) { + mChoreographer.postFrameCallback(this); + } } public void start() { @@ -129,7 +130,14 @@ public class FpsDebugFrameCallback extends ChoreographerCompat.FrameCallback { mReactContext.getCatalystInstance().addBridgeIdleDebugListener( mDidJSUpdateUiDuringFrameDetector); mUIManagerModule.setViewHierarchyUpdateDebugListener(mDidJSUpdateUiDuringFrameDetector); - mChoreographer.postFrameCallback(this); + final FpsDebugFrameCallback fpsDebugFrameCallback = this; + UiThreadUtil.runOnUiThread(new Runnable() { + @Override + public void run() { + mChoreographer = ChoreographerCompat.getInstance(); + mChoreographer.postFrameCallback(fpsDebugFrameCallback); + } + }); } public void startAndRecordFpsAtEachFrame() {