Skip to content

Commit

Permalink
make view hierarchy optimizer smarter
Browse files Browse the repository at this point in the history
Reviewed By: shergin

Differential Revision: D5744725

fbshipit-source-id: 4042310494a2bbd3d3472485ad8496d592eb28ac
  • Loading branch information
aaronechiu authored and facebook-github-bot committed Sep 30, 2017
1 parent 38ec30b commit e7af72b
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import android.os.Build;
import android.view.View;
import android.view.ViewParent;

import com.facebook.react.R;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.uimanager.annotations.ReactProp;
Expand All @@ -21,7 +20,6 @@ public abstract class BaseViewManager<T extends View, C extends LayoutShadowNode

private static final String PROP_BACKGROUND_COLOR = ViewProps.BACKGROUND_COLOR;
private static final String PROP_TRANSFORM = "transform";
private static final String PROP_OPACITY = "opacity";
private static final String PROP_ELEVATION = "elevation";
private static final String PROP_Z_INDEX = "zIndex";
private static final String PROP_RENDER_TO_HARDWARE_TEXTURE = "renderToHardwareTextureAndroid";
Expand Down Expand Up @@ -64,7 +62,7 @@ public void setTransform(T view, ReadableArray matrix) {
}
}

@ReactProp(name = PROP_OPACITY, defaultFloat = 1.f)
@ReactProp(name = ViewProps.OPACITY, defaultFloat = 1.f)
public void setOpacity(T view, float opacity) {
view.setAlpha(opacity);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@

package com.facebook.react.uimanager;

import javax.annotation.Nullable;

import android.util.SparseBooleanArray;

import com.facebook.infer.annotation.Assertions;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMapKeySetIterator;
import javax.annotation.Nullable;

/**
* Class responsible for optimizing the native view hierarchy while still respecting the final UI
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

package com.facebook.react.uimanager;

import android.graphics.Color;
import com.facebook.react.bridge.ReadableMap;
import java.util.Arrays;
import java.util.HashSet;

import com.facebook.react.bridge.ReadableMap;

/**
* Keys for props that need to be shared across multiple classes.
*/
Expand Down Expand Up @@ -90,6 +90,7 @@ public class ViewProps {
public static final String TEXT_ALIGN_VERTICAL = "textAlignVertical";
public static final String TEXT_DECORATION_LINE = "textDecorationLine";
public static final String TEXT_BREAK_STRATEGY = "textBreakStrategy";
public static final String OPACITY = "opacity";

public static final String ALLOW_FONT_SCALING = "allowFontScaling";
public static final String INCLUDE_FONT_PADDING = "includeFontPadding";
Expand All @@ -104,6 +105,11 @@ public class ViewProps {
public static final String BORDER_TOP_RIGHT_RADIUS = "borderTopRightRadius";
public static final String BORDER_BOTTOM_LEFT_RADIUS = "borderBottomLeftRadius";
public static final String BORDER_BOTTOM_RIGHT_RADIUS = "borderBottomRightRadius";
public static final String BORDER_COLOR = "borderColor";
public static final String BORDER_LEFT_COLOR = "borderLeftColor";
public static final String BORDER_RIGHT_COLOR = "borderRightColor";
public static final String BORDER_TOP_COLOR = "borderTopColor";
public static final String BORDER_BOTTOM_COLOR = "borderBottomColor";
public static final int[] BORDER_SPACING_TYPES = {
Spacing.ALL, Spacing.START, Spacing.END, Spacing.TOP, Spacing.BOTTOM
};
Expand Down Expand Up @@ -164,14 +170,59 @@ public class ViewProps {
PADDING_TOP,
PADDING_BOTTOM));

public static boolean sIsOptimizationsEnabled;

public static boolean isLayoutOnly(ReadableMap map, String prop) {
if (LAYOUT_ONLY_PROPS.contains(prop)) {
return true;
} else if (POINTER_EVENTS.equals(prop)) {
String value = map.getString(prop);
return "auto".equals(value) || "box-none".equals(value);
} else {
return false;
return "auto".equals(value);
}

if (sIsOptimizationsEnabled) {
switch (prop) {
case OPACITY:
return map.getDouble(OPACITY) == 1d; // Ignore if explicitly set to default opacity.
case BACKGROUND_COLOR:
return map.getInt(BACKGROUND_COLOR) == Color.TRANSPARENT;
case BORDER_RADIUS: // Without a background color or border width set, a border won't show.
if (map.hasKey(BACKGROUND_COLOR) && map.getInt(BACKGROUND_COLOR) != Color.TRANSPARENT) {
return false;
}
if (map.hasKey(BORDER_WIDTH) && map.getDouble(BORDER_WIDTH) != 0d) {
return false;
}
return true;
case BORDER_COLOR:
return map.getInt(BORDER_COLOR) == Color.TRANSPARENT;
case BORDER_LEFT_COLOR:
return map.getInt(BORDER_LEFT_COLOR) == Color.TRANSPARENT;
case BORDER_RIGHT_COLOR:
return map.getInt(BORDER_RIGHT_COLOR) == Color.TRANSPARENT;
case BORDER_TOP_COLOR:
return map.getInt(BORDER_TOP_COLOR) == Color.TRANSPARENT;
case BORDER_BOTTOM_COLOR:
return map.getInt(BORDER_BOTTOM_COLOR) == Color.TRANSPARENT;
case BORDER_WIDTH:
return map.getDouble(BORDER_WIDTH) == 0d;
case BORDER_LEFT_WIDTH:
return map.getDouble(BORDER_LEFT_WIDTH) == 0d;
case BORDER_TOP_WIDTH:
return map.getDouble(BORDER_TOP_WIDTH) == 0d;
case BORDER_RIGHT_WIDTH:
return map.getDouble(BORDER_RIGHT_WIDTH) == 0d;
case BORDER_BOTTOM_WIDTH:
return map.getDouble(BORDER_BOTTOM_WIDTH) == 0d;
case "onLayout":
return true;
case "overflow": // We do nothing with this right now.
return true;
default:
return false;
}
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,10 @@

package com.facebook.react.views.view;

import javax.annotation.Nullable;

import java.util.Locale;
import java.util.Map;

import android.annotation.TargetApi;
import android.graphics.Rect;
import android.os.Build;
import android.view.View;

import com.facebook.yoga.YogaConstants;
import com.facebook.react.bridge.JSApplicationIllegalArgumentException;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
Expand All @@ -34,6 +27,10 @@
import com.facebook.react.uimanager.ViewProps;
import com.facebook.react.uimanager.annotations.ReactProp;
import com.facebook.react.uimanager.annotations.ReactPropGroup;
import com.facebook.yoga.YogaConstants;
import java.util.Locale;
import java.util.Map;
import javax.annotation.Nullable;

/**
* View manager for AndroidViews (plain React Views).
Expand Down Expand Up @@ -144,9 +141,16 @@ public void setBorderWidth(ReactViewGroup view, int index, float width) {
view.setBorderWidth(SPACING_TYPES[index], width);
}

@ReactPropGroup(names = {
"borderColor", "borderLeftColor", "borderRightColor", "borderTopColor", "borderBottomColor"
}, customType = "Color")
@ReactPropGroup(
names = {
ViewProps.BORDER_COLOR,
ViewProps.BORDER_LEFT_COLOR,
ViewProps.BORDER_RIGHT_COLOR,
ViewProps.BORDER_TOP_COLOR,
ViewProps.BORDER_BOTTOM_COLOR
},
customType = "Color"
)
public void setBorderColor(ReactViewGroup view, int index, Integer color) {
float rgbComponent = color == null ? YogaConstants.UNDEFINED : (float) ((int)color & 0x00FFFFFF);
float alphaComponent = color == null ? YogaConstants.UNDEFINED : (float) ((int)color >>> 24);
Expand Down

0 comments on commit e7af72b

Please sign in to comment.