loosen the ReactChoreographer UI thread assert

Reviewed By: achen1

Differential Revision: D4873553

fbshipit-source-id: 7dbf771e744f6b33e6edb3ad4c227c3a63c3e3e3
This commit is contained in:
Aaron Chiu 2017-04-14 02:43:07 -07:00 committed by Facebook Github Bot
parent c9826ba4a9
commit b9b03435c4
3 changed files with 28 additions and 14 deletions

View File

@ -31,6 +31,7 @@ import com.facebook.react.cxxbridge.CatalystInstanceImpl;
import com.facebook.react.cxxbridge.JSBundleLoader; import com.facebook.react.cxxbridge.JSBundleLoader;
import com.facebook.react.cxxbridge.JSCJavaScriptExecutor; import com.facebook.react.cxxbridge.JSCJavaScriptExecutor;
import com.facebook.react.cxxbridge.JavaScriptExecutor; import com.facebook.react.cxxbridge.JavaScriptExecutor;
import com.facebook.react.modules.core.ReactChoreographer;
import com.android.internal.util.Predicate; import com.android.internal.util.Predicate;
@ -153,6 +154,7 @@ public class ReactTestHelper {
InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() { InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
@Override @Override
public void run() { public void run() {
ReactChoreographer.initialize();
instance.initialize(); instance.initialize();
} }
}); });

View File

@ -61,6 +61,7 @@ import com.facebook.react.devsupport.interfaces.PackagerStatusCallback;
import com.facebook.react.modules.appregistry.AppRegistry; import com.facebook.react.modules.appregistry.AppRegistry;
import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler; import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
import com.facebook.react.modules.core.DeviceEventManagerModule; import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.modules.core.ReactChoreographer;
import com.facebook.react.modules.debug.interfaces.DeveloperSettings; import com.facebook.react.modules.debug.interfaces.DeveloperSettings;
import com.facebook.react.uimanager.DisplayMetricsHolder; import com.facebook.react.uimanager.DisplayMetricsHolder;
import com.facebook.react.uimanager.UIImplementationProvider; import com.facebook.react.uimanager.UIImplementationProvider;
@ -348,6 +349,9 @@ public class ReactInstanceManager {
mLazyNativeModulesEnabled = lazyNativeModulesEnabled; mLazyNativeModulesEnabled = lazyNativeModulesEnabled;
mLazyViewManagersEnabled = lazyViewManagersEnabled; mLazyViewManagersEnabled = lazyViewManagersEnabled;
mUseStartupThread = useStartupThread; mUseStartupThread = useStartupThread;
// Instantiate ReactChoreographer in UI thread.
ReactChoreographer.initialize();
} }
public DevSupportManager getDevSupportManager() { public DevSupportManager getDevSupportManager() {

View File

@ -12,8 +12,8 @@ package com.facebook.react.modules.core;
import java.util.ArrayDeque; import java.util.ArrayDeque;
import com.facebook.common.logging.FLog; import com.facebook.common.logging.FLog;
import com.facebook.react.bridge.UiThreadUtil;
import com.facebook.infer.annotation.Assertions; import com.facebook.infer.annotation.Assertions;
import com.facebook.react.bridge.UiThreadUtil;
import com.facebook.react.common.ReactConstants; import com.facebook.react.common.ReactConstants;
/** /**
@ -65,11 +65,15 @@ public class ReactChoreographer {
private static ReactChoreographer sInstance; private static ReactChoreographer sInstance;
public static ReactChoreographer getInstance() { public static void initialize() {
UiThreadUtil.assertOnUiThread();
if (sInstance == null) { if (sInstance == null) {
UiThreadUtil.assertOnUiThread();
sInstance = new ReactChoreographer(); sInstance = new ReactChoreographer();
} }
}
public static ReactChoreographer getInstance() {
Assertions.assertNotNull(sInstance, "ReactChoreographer needs to be initialized.");
return sInstance; return sInstance;
} }
@ -89,8 +93,9 @@ public class ReactChoreographer {
} }
} }
public void postFrameCallback(CallbackType type, ChoreographerCompat.FrameCallback frameCallback) { public synchronized void postFrameCallback(
UiThreadUtil.assertOnUiThread(); CallbackType type,
ChoreographerCompat.FrameCallback frameCallback) {
mCallbackQueues[type.getOrder()].addLast(frameCallback); mCallbackQueues[type.getOrder()].addLast(frameCallback);
mTotalCallbacks++; mTotalCallbacks++;
Assertions.assertCondition(mTotalCallbacks > 0); Assertions.assertCondition(mTotalCallbacks > 0);
@ -100,8 +105,9 @@ public class ReactChoreographer {
} }
} }
public void removeFrameCallback(CallbackType type, ChoreographerCompat.FrameCallback frameCallback) { public synchronized void removeFrameCallback(
UiThreadUtil.assertOnUiThread(); CallbackType type,
ChoreographerCompat.FrameCallback frameCallback) {
if (mCallbackQueues[type.getOrder()].removeFirstOccurrence(frameCallback)) { if (mCallbackQueues[type.getOrder()].removeFirstOccurrence(frameCallback)) {
mTotalCallbacks--; mTotalCallbacks--;
maybeRemoveFrameCallback(); maybeRemoveFrameCallback();
@ -122,15 +128,17 @@ public class ReactChoreographer {
@Override @Override
public void doFrame(long frameTimeNanos) { public void doFrame(long frameTimeNanos) {
mHasPostedCallback = false; synchronized (ReactChoreographer.this) {
for (int i = 0; i < mCallbackQueues.length; i++) { mHasPostedCallback = false;
int initialLength = mCallbackQueues[i].size(); for (int i = 0; i < mCallbackQueues.length; i++) {
for (int callback = 0; callback < initialLength; callback++) { int initialLength = mCallbackQueues[i].size();
mCallbackQueues[i].removeFirst().doFrame(frameTimeNanos); for (int callback = 0; callback < initialLength; callback++) {
mTotalCallbacks--; mCallbackQueues[i].removeFirst().doFrame(frameTimeNanos);
mTotalCallbacks--;
}
} }
maybeRemoveFrameCallback();
} }
maybeRemoveFrameCallback();
} }
} }
} }