From 8357b39908a9156b76b426400ed473b9d529577f Mon Sep 17 00:00:00 2001 From: Joshua Gross Date: Fri, 22 Jan 2021 19:29:00 -0800 Subject: [PATCH] Rename String surfaceID to surfaceName/moduleName, add int surfaceId to ThemedReactContext Summary: There's a field called `surfaceID` in a couple of classes that isn't the same as the integer `surfaceId` in Fabric. For consistency, I've deprecated a couple of them, or renamed when appropriate. In addition, now we're passing the actual integer surfaceId into the ThemedReactContext. This means that every single View created in Fabric gets annotated with the surfaceId it's in. Currently this isn't used, but the idea is that now each View has a mapping back to its surface, which could be used to simplify / optimize operations with SurfaceMountingManager. In particular, we might be able to use this in the future to optimize animations and/or event emitters. Changelog: [Internal] Reviewed By: mdvacca Differential Revision: D26021571 fbshipit-source-id: b7db7de123db07fa928a6f815be86bdbb030e62c --- .../react/fabric/FabricUIManager.java | 4 +- .../react/uimanager/ThemedReactContext.java | 42 +++++++++++++++---- .../react/uimanager/UIManagerModule.java | 7 +++- .../image/ReactCallerContextFactory.java | 4 +- .../react/views/image/ReactImageManager.java | 4 +- 5 files changed, 47 insertions(+), 14 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java b/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java index 51be066ce2..b3ef093749 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java @@ -188,7 +188,7 @@ public class FabricUIManager implements UIManager, LifecycleEventListener { ThemedReactContext reactContext = new ThemedReactContext( - mReactApplicationContext, rootView.getContext(), reactRootView.getSurfaceID()); + mReactApplicationContext, rootView.getContext(), reactRootView.getSurfaceID(), rootTag); mMountingManager.startSurface(rootTag, rootView, reactContext); String moduleName = reactRootView.getJSModuleName(); if (ENABLE_FABRIC_LOGS) { @@ -220,7 +220,7 @@ public class FabricUIManager implements UIManager, LifecycleEventListener { final int rootTag = ReactRootViewTagGenerator.getNextRootViewTag(); Context context = rootView.getContext(); ThemedReactContext reactContext = - new ThemedReactContext(mReactApplicationContext, context, moduleName); + new ThemedReactContext(mReactApplicationContext, context, moduleName, rootTag); if (ENABLE_FABRIC_LOGS) { FLog.d(TAG, "Starting surface for module: %s and reactTag: %d", moduleName, rootTag); } diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/ThemedReactContext.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/ThemedReactContext.java index 834208a962..15701d6361 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/ThemedReactContext.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/ThemedReactContext.java @@ -27,20 +27,32 @@ import com.facebook.react.bridge.ReactContext; public class ThemedReactContext extends ReactContext { private final ReactApplicationContext mReactApplicationContext; - @Nullable private final String mSurfaceID; + @Nullable private final String mModuleName; + private final int mSurfaceId; + @Deprecated public ThemedReactContext(ReactApplicationContext reactApplicationContext, Context base) { - this(reactApplicationContext, base, null); + this(reactApplicationContext, base, null, -1); + } + + @Deprecated + public ThemedReactContext( + ReactApplicationContext reactApplicationContext, Context base, @Nullable String moduleName) { + this(reactApplicationContext, base, moduleName, -1); } public ThemedReactContext( - ReactApplicationContext reactApplicationContext, Context base, @Nullable String surfaceID) { + ReactApplicationContext reactApplicationContext, + Context base, + @Nullable String moduleName, + int surfaceId) { super(base); if (reactApplicationContext.hasCatalystInstance()) { initializeWithInstance(reactApplicationContext.getCatalystInstance()); } mReactApplicationContext = reactApplicationContext; - mSurfaceID = surfaceID; + mModuleName = moduleName; + mSurfaceId = surfaceId; } @Override @@ -64,11 +76,27 @@ public class ThemedReactContext extends ReactContext { } /** - * @return a {@link String} that represents the ID of the js application that is being rendered - * with this {@link ThemedReactContext} + * This is misnamed but has some uses out in the wild. It will be deleted in a future release of + * RN. + * + * @return a {@link String} that represents the module name of the js application that is being + * rendered with this {@link ThemedReactContext} */ + @Deprecated public @Nullable String getSurfaceID() { - return mSurfaceID; + return mModuleName; + } + + /** + * @return a {@link String} that represents the module name of the js application that is being + * rendered with this {@link ThemedReactContext} + */ + public @Nullable String getModuleName() { + return mModuleName; + } + + public int getSurfaceId() { + return mSurfaceId; } public ReactApplicationContext getReactApplicationContext() { diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java index 324885a494..5c1383d0e1 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java @@ -451,9 +451,14 @@ public class UIManagerModule extends ReactContextBaseJavaModule Systrace.beginSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "UIManagerModule.addRootView"); final int tag = ReactRootViewTagGenerator.getNextRootViewTag(); final ReactApplicationContext reactApplicationContext = getReactApplicationContext(); + + // We pass in a surfaceId of -1 here - it is used only in Fabric. final ThemedReactContext themedRootContext = new ThemedReactContext( - reactApplicationContext, rootView.getContext(), ((ReactRoot) rootView).getSurfaceID()); + reactApplicationContext, + rootView.getContext(), + ((ReactRoot) rootView).getSurfaceID(), + -1); mUIImplementation.registerRootView(rootView, tag, themedRootContext); Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE); diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/image/ReactCallerContextFactory.java b/ReactAndroid/src/main/java/com/facebook/react/views/image/ReactCallerContextFactory.java index 46e9573fdb..cb1c6a3360 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/image/ReactCallerContextFactory.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/image/ReactCallerContextFactory.java @@ -20,9 +20,9 @@ public interface ReactCallerContextFactory { /** * This method will be called at the time {@link ReactImageManager} creates {@link ReactImageView} * - * @param surfaceID {@link String} used to log the name of the surface + * @param surfaceName {@link String} used to log the name of the surface * @return an {@link Object} that represents the CallerContext. */ @Nullable - Object getOrCreateCallerContext(@Nullable String surfaceID, @Nullable String analyticTag); + Object getOrCreateCallerContext(@Nullable String surfaceName, @Nullable String analyticTag); } diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/image/ReactImageManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/image/ReactImageManager.java index 397a8378d6..d1250c7563 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/image/ReactImageManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/image/ReactImageManager.java @@ -107,7 +107,7 @@ public class ReactImageManager extends SimpleViewManager { public ReactImageView createViewInstance(ThemedReactContext context) { Object callerContext = mCallerContextFactory != null - ? mCallerContextFactory.getOrCreateCallerContext(context.getSurfaceID(), null) + ? mCallerContextFactory.getOrCreateCallerContext(context.getModuleName(), null) : getCallerContext(); return new ReactImageView( context, getDraweeControllerBuilder(), mGlobalImageLoadListener, callerContext); @@ -129,7 +129,7 @@ public class ReactImageManager extends SimpleViewManager { if (mCallerContextFactory != null) { view.updateCallerContext( mCallerContextFactory.getOrCreateCallerContext( - ((ThemedReactContext) view.getContext()).getSurfaceID(), analyticTag)); + ((ThemedReactContext) view.getContext()).getModuleName(), analyticTag)); } }