Skip to content

Commit

Permalink
Set layout metrics during initial render of Fabric
Browse files Browse the repository at this point in the history
Summary:
This diff refactors the initial render of Fabric in order to set the layout metrics as we start the surface.

This prevents to create an additional fabric commit during initial render. Also this migth help fixing T63495589 (I wasn't able to repro T63495589 again when using this diff)

changelog: [Internal][Android] Internal change to reduce the amount of commits during initial render of Fabric

Reviewed By: JoshuaGross

Differential Revision: D21330072

fbshipit-source-id: 758c49b52ea4c12d5623b7c7d68c7318f4a6cd83
  • Loading branch information
mdvacca authored and facebook-github-bot committed May 1, 2020
1 parent 09487e4 commit 65d52a5
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1150,21 +1150,29 @@ private void attachRootViewToInstance(final ReactRoot reactRoot) {

@Nullable Bundle initialProperties = reactRoot.getAppProperties();

final int rootTag =
uiManager.addRootView(
reactRoot.getRootViewGroup(),
initialProperties == null
? new WritableNativeMap()
: Arguments.fromBundle(initialProperties),
reactRoot.getInitialUITemplate());
reactRoot.setRootViewTag(rootTag);
final int rootTag;

if (reactRoot.getUIManagerType() == FABRIC) {
// Fabric requires to call updateRootLayoutSpecs before starting JS Application,
// this ensures the root will hace the correct pointScaleFactor.
uiManager.updateRootLayoutSpecs(
rootTag, reactRoot.getWidthMeasureSpec(), reactRoot.getHeightMeasureSpec());
rootTag =
uiManager.startSurface(
reactRoot.getRootViewGroup(),
reactRoot.getJSModuleName(),
initialProperties == null
? new WritableNativeMap()
: Arguments.fromBundle(initialProperties),
reactRoot.getWidthMeasureSpec(),
reactRoot.getHeightMeasureSpec());
reactRoot.setRootViewTag(rootTag);
reactRoot.setShouldLogContentAppeared(true);
} else {
rootTag =
uiManager.addRootView(
reactRoot.getRootViewGroup(),
initialProperties == null
? new WritableNativeMap()
: Arguments.fromBundle(initialProperties),
reactRoot.getInitialUITemplate());
reactRoot.setRootViewTag(rootTag);
reactRoot.runApplication();
}
Systrace.beginAsyncSection(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static com.facebook.infer.annotation.ThreadConfined.UI;

import android.view.View;
import androidx.annotation.AnyThread;
import androidx.annotation.Nullable;
import androidx.annotation.UiThread;
import com.facebook.infer.annotation.ThreadConfined;
Expand All @@ -22,6 +23,15 @@ public interface UIManager extends JSIModule, PerformanceCounter {
<T extends View> int addRootView(
final T rootView, WritableMap initialProps, @Nullable String initialUITemplate);

/** Registers a new root view with width and height. */
@AnyThread
<T extends View> int startSurface(
final T rootView,
final String moduleName,
final WritableMap initialProps,
int widthMeasureSpec,
int heightMeasureSpec);

/**
* Updates the layout specs of the RootShadowNode based on the Measure specs received by
* parameters.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static com.facebook.react.uimanager.common.UIManagerType.FABRIC;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.SystemClock;
import android.view.View;
import androidx.annotation.AnyThread;
Expand Down Expand Up @@ -195,6 +196,7 @@ public <T extends View> int addRootView(
return rootTag;
}

@Override
@AnyThread
@ThreadConfined(ANY)
public <T extends View> int startSurface(
Expand All @@ -204,8 +206,9 @@ public <T extends View> int startSurface(
int widthMeasureSpec,
int heightMeasureSpec) {
final int rootTag = ReactRootViewTagGenerator.getNextRootViewTag();
Context context = rootView.getContext();
ThemedReactContext reactContext =
new ThemedReactContext(mReactApplicationContext, rootView.getContext(), moduleName);
new ThemedReactContext(mReactApplicationContext, context, moduleName);
if (ENABLE_FABRIC_LOGS) {
FLog.d(TAG, "Starting surface for module: %s and reactTag: %d", moduleName, rootTag);
}
Expand All @@ -219,8 +222,8 @@ public <T extends View> int startSurface(
getMaxSize(widthMeasureSpec),
getMinSize(heightMeasureSpec),
getMaxSize(heightMeasureSpec),
I18nUtil.getInstance().isRTL(rootView.getContext()),
I18nUtil.getInstance().doLeftAndRightSwapInRTL(rootView.getContext()));
I18nUtil.getInstance().isRTL(context),
I18nUtil.getInstance().doLeftAndRightSwapInRTL(context));
return rootTag;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ public interface ReactRoot {
/** Return native view for root */
ViewGroup getRootViewGroup();

/** @return Cached values for widthMeasureSpec and heightMeasureSpec */
/** @return Cached values for widthMeasureSpec. */
int getWidthMeasureSpec();

/** @return Cached values for and heightMeasureSpec. */
int getHeightMeasureSpec();

/** Sets a flag that determines whether to log that content appeared on next view added. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,16 @@ public <T extends View> int addRootView(
return tag;
}

@Override
public <T extends View> int startSurface(
final T rootView,
final String moduleName,
final WritableMap initialProps,
int widthMeasureSpec,
int heightMeasureSpec) {
throw new UnsupportedOperationException();
}

/** Unregisters a new root view. */
@ReactMethod
public void removeRootView(int rootViewTag) {
Expand Down

0 comments on commit 65d52a5

Please sign in to comment.