Go back to object approach instead of manual drawable layer handling for CompositeBackgroundDrawable (#48835)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/48835

D65907786 ended up regressing a bit of the performance gains from new Background and Border Drawables. changing back to the previous approach.

Changelog: [Internal]

Reviewed By: javache

Differential Revision: D68354292

fbshipit-source-id: f2db6d7ad5c1590d5d4d8261d76281f3592f488a
This commit is contained in:
Jorge Cabiedes Acosta authored and Facebook GitHub Bot committed 2025-01-23 12:39:25 -08:00
1 parent 0353648f46
commit f89f191c26
3 files changed
+157 -285

No files matched your search

@@ -13,7 +13,6 @@ import android.graphics.Path
import android.graphics.Rect
import android.graphics.RectF
import android.graphics.drawable.Drawable
import android.graphics.drawable.LayerDrawable
import android.os.Build
import android.view.View
import android.widget.ImageView
@@ -106,14 +105,15 @@ public object BackgroundStyleApplicator {
composite.borderInsets = composite.borderInsets ?: BorderInsets()
composite.borderInsets?.setBorderWidth(edge, width)
if (Build.VERSION.SDK_INT >= MIN_OUTSET_BOX_SHADOW_SDK_VERSION) {
for (shadow in composite.outerShadows.filterIsInstance<OutsetBoxShadowDrawable>()) {
shadow.borderRadius = composite.borderRadius
}
}
if (Build.VERSION.SDK_INT >= MIN_INSET_BOX_SHADOW_SDK_VERSION) {
val innerShadows = composite.innerShadows
if (innerShadows != null) {
for (i in 0 until innerShadows.numberOfLayers) {
val shadow = innerShadows.getDrawable(i)
(shadow as InsetBoxShadowDrawable).borderInsets = composite.borderInsets
shadow.invalidateSelf()
}
for (shadow in composite.innerShadows.filterIsInstance<InsetBoxShadowDrawable>()) {
shadow.borderRadius = composite.borderRadius
}
}
}
@@ -174,30 +174,16 @@ public object BackgroundStyleApplicator {
}
if (Build.VERSION.SDK_INT >= MIN_OUTSET_BOX_SHADOW_SDK_VERSION) {
val outerShadows = compositeBackgroundDrawable.outerShadows
if (outerShadows != null) {
for (i in 0 until outerShadows.numberOfLayers) {
val shadow = outerShadows.getDrawable(i)
if (shadow is OutsetBoxShadowDrawable) {
shadow.borderRadius = shadow.borderRadius ?: BorderRadiusStyle()
shadow.borderRadius?.set(corner, radius)
shadow.invalidateSelf()
}
}
for (shadow in
compositeBackgroundDrawable.outerShadows.filterIsInstance<OutsetBoxShadowDrawable>()) {
shadow.borderRadius = compositeBackgroundDrawable.borderRadius
}
}
if (Build.VERSION.SDK_INT >= MIN_INSET_BOX_SHADOW_SDK_VERSION) {
val innerShadows = compositeBackgroundDrawable.innerShadows
if (innerShadows != null) {
for (i in 0 until innerShadows.numberOfLayers) {
val shadow = innerShadows.getDrawable(i)
if (shadow is InsetBoxShadowDrawable) {
shadow.borderRadius = shadow.borderRadius ?: BorderRadiusStyle()
shadow.borderRadius?.set(corner, radius)
shadow.invalidateSelf()
}
}
for (shadow in
compositeBackgroundDrawable.outerShadows.filterIsInstance<InsetBoxShadowDrawable>()) {
shadow.borderRadius = compositeBackgroundDrawable.borderRadius
}
}
@@ -291,8 +277,8 @@ public object BackgroundStyleApplicator {
return
}
var innerShadows: LayerDrawable? = null
var outerShadows: LayerDrawable? = null
var innerShadows = mutableListOf<InsetBoxShadowDrawable>()
var outerShadows = mutableListOf<OutsetBoxShadowDrawable>()
val compositeBackgroundDrawable = ensureCompositeBackgroundDrawable(view)
val borderInsets = compositeBackgroundDrawable.borderInsets
@@ -302,7 +288,7 @@ public object BackgroundStyleApplicator {
* z-ordering of user-provided shadow-list is opposite direction of LayerDrawable z-ordering
* https://drafts.csswg.org/css-backgrounds/#shadow-layers
*/
for (boxShadow in shadows.asReversed()) {
for (boxShadow in shadows) {
val offsetX = boxShadow.offsetX
val offsetY = boxShadow.offsetY
val color = boxShadow.color ?: Color.BLACK
@@ -311,10 +297,7 @@ public object BackgroundStyleApplicator {
val inset = boxShadow.inset ?: false
if (inset && Build.VERSION.SDK_INT >= MIN_INSET_BOX_SHADOW_SDK_VERSION) {
if (innerShadows == null) {
innerShadows = LayerDrawable(emptyArray())
}
innerShadows.addLayer(
innerShadows.add(
InsetBoxShadowDrawable(
context = view.context,
borderRadius = borderRadius,
@@ -325,10 +308,7 @@ public object BackgroundStyleApplicator {
blurRadius = blurRadius,
spread = spreadDistance))
} else if (!inset && Build.VERSION.SDK_INT >= MIN_OUTSET_BOX_SHADOW_SDK_VERSION) {
if (outerShadows == null) {
outerShadows = LayerDrawable(emptyArray())
}
outerShadows.addLayer(
outerShadows.add(
OutsetBoxShadowDrawable(
context = view.context,
borderRadius = borderRadius,
@@ -340,8 +320,9 @@ public object BackgroundStyleApplicator {
}
}
view.background = ensureCompositeBackgroundDrawable(view).withNewOuterShadow(outerShadows)
view.background = ensureCompositeBackgroundDrawable(view).withNewInnerShadow(innerShadows)
view.background =
ensureCompositeBackgroundDrawable(view)
.withNewShadows(outerShadows = outerShadows, innerShadows = innerShadows)
}
@JvmStatic
@@ -15,7 +15,6 @@ import android.graphics.drawable.Drawable
import android.graphics.drawable.LayerDrawable
import android.os.Build
import com.facebook.react.common.annotations.UnstableReactNativeAPI
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags
import com.facebook.react.uimanager.style.BorderInsets
import com.facebook.react.uimanager.style.BorderRadiusStyle
@@ -33,7 +32,7 @@ internal class CompositeBackgroundDrawable(
public val originalBackground: Drawable? = null,
/** Non-inset box shadows */
outerShadows: LayerDrawable? = null,
public val outerShadows: List<Drawable> = emptyList(),
/**
* CSS background layer and border rendering
@@ -44,65 +43,28 @@ internal class CompositeBackgroundDrawable(
public val cssBackground: CSSBackgroundDrawable? = null,
/** Background rendering Layer */
background: BackgroundDrawable? = null,
public val background: BackgroundDrawable? = null,
/** Border rendering Layer */
border: BorderDrawable? = null,
public val border: BorderDrawable? = null,
/** TouchableNativeFeeback set selection background, like "SelectableBackground" */
feedbackUnderlay: Drawable? = null,
public val feedbackUnderlay: Drawable? = null,
/** Inset box-shadows */
innerShadows: LayerDrawable? = null,
public val innerShadows: List<Drawable> = emptyList(),
/** Outline */
outline: OutlineDrawable? = null,
) : LayerDrawable(emptyArray()) {
public var outerShadows: LayerDrawable? = outerShadows
private set
public val outline: OutlineDrawable? = null,
public var background: BackgroundDrawable? = background
private set
// Holder value for currently set insets
public var borderInsets: BorderInsets? = null,
public var border: BorderDrawable? = border
private set
public var feedbackUnderlay: Drawable? = feedbackUnderlay
private set
public var innerShadows: LayerDrawable? = innerShadows
private set
public var outline: OutlineDrawable? = outline
private set
// Holder value for currently set insets
public var borderInsets: BorderInsets? = null
// Holder value for currently set border radius
public var borderRadius: BorderRadiusStyle? = null
init {
// We want to overlay drawables, instead of placing future drawables within the content area of
// previous ones. E.g. an EditText style may set padding on a TextInput, but we don't want to
// constrain background color to the area inside of the padding.
setPaddingMode(LayerDrawable.PADDING_MODE_STACK)
addLayer(originalBackground, ORIGINAL_BACKGROUND_ID)
addLayer(outerShadows, OUTER_SHADOWS_ID)
addLayer(cssBackground, CSS_BACKGROUND_ID)
addLayer(background, BACKGROUND_ID)
addLayer(border, BORDER_ID)
addLayer(feedbackUnderlay, FEEDBACK_UNDERLAY_ID)
addLayer(innerShadows, INNER_SHADOWS_ID)
addLayer(outline, OUTLINE_ID)
}
public fun withNewCssBackground(
cssBackground: CSSBackgroundDrawable?
): CompositeBackgroundDrawable {
return CompositeBackgroundDrawable(
context,
// Holder value for currently set border radius
public var borderRadius: BorderRadiusStyle? = null,
) :
LayerDrawable(
createLayersArray(
originalBackground,
outerShadows,
cssBackground,
@@ -110,78 +72,114 @@ internal class CompositeBackgroundDrawable(
border,
feedbackUnderlay,
innerShadows,
outline)
.also { composite ->
composite.borderInsets = this.borderInsets
composite.borderRadius = this.borderRadius
}
outline)) {
init {
// We want to overlay drawables, instead of placing future drawables within the content area of
// previous ones. E.g. an EditText style may set padding on a TextInput, but we don't want to
// constrain background color to the area inside of the padding.
setPaddingMode(LayerDrawable.PADDING_MODE_STACK)
}
public fun withNewOuterShadow(outerShadows: LayerDrawable?): CompositeBackgroundDrawable =
withNewLayer(outerShadows, OUTER_SHADOWS_ID, this::outerShadows::set)
public fun withNewBackground(background: BackgroundDrawable?): CompositeBackgroundDrawable =
withNewLayer(background, BACKGROUND_ID, this::background::set)
public fun withNewBorder(border: BorderDrawable?): CompositeBackgroundDrawable =
withNewLayer(border, BORDER_ID, this::border::set)
public fun withNewFeedbackUnderlay(newUnderlay: Drawable?): CompositeBackgroundDrawable =
withNewLayer(newUnderlay, FEEDBACK_UNDERLAY_ID, this::feedbackUnderlay::set)
public fun withNewInnerShadow(innerShadows: LayerDrawable?): CompositeBackgroundDrawable =
withNewLayer(innerShadows, INNER_SHADOWS_ID, this::innerShadows::set)
public fun withNewOutline(outline: OutlineDrawable?): CompositeBackgroundDrawable =
withNewLayer(outline, OUTLINE_ID, this::outline::set)
/** @return true if the layer was updated, false if it was not */
private fun updateLayer(layer: Drawable?, id: Int): Boolean {
if (layer == null) {
return findDrawableByLayerId(id) == null
}
if (findDrawableByLayerId(id) == null) {
insertNewLayer(layer, id)
} else {
setDrawableByLayerId(id, layer)
}
invalidateSelf()
return true
public fun withNewCssBackground(
cssBackground: CSSBackgroundDrawable?
): CompositeBackgroundDrawable {
return CompositeBackgroundDrawable(
context,
originalBackground,
outerShadows,
cssBackground,
background,
border,
feedbackUnderlay,
innerShadows,
outline,
borderInsets,
borderRadius,
)
}
private fun insertNewLayer(layer: Drawable?, id: Int) {
layer ?: return
if (numberOfLayers == 0) {
addLayer(layer, id)
return
}
for (i in 0..<numberOfLayers) {
if (id < getId(i)) {
val tempDrawable: Drawable = getDrawable(i)
val tempId = getId(i)
setDrawable(i, layer)
setId(i, id)
insertNewLayer(tempDrawable, tempId)
return
} else if (i == numberOfLayers - 1) {
addLayer(layer, id)
return
}
}
public fun withNewBackground(background: BackgroundDrawable?): CompositeBackgroundDrawable {
return CompositeBackgroundDrawable(
context,
originalBackground,
outerShadows,
cssBackground,
background,
border,
feedbackUnderlay,
innerShadows,
outline,
borderInsets,
borderRadius,
)
}
private fun addLayer(layer: Drawable?, id: Int) {
if (layer == null) {
return
}
public fun withNewShadows(
outerShadows: List<Drawable>,
innerShadows: List<Drawable>
): CompositeBackgroundDrawable {
return CompositeBackgroundDrawable(
context,
originalBackground,
outerShadows,
cssBackground,
background,
border,
feedbackUnderlay,
innerShadows,
outline,
borderInsets,
borderRadius,
)
}
addLayer(layer)
layer.callback = this
setId(numberOfLayers - 1, id)
invalidateSelf()
public fun withNewBorder(border: BorderDrawable): CompositeBackgroundDrawable {
return CompositeBackgroundDrawable(
context,
originalBackground,
outerShadows,
cssBackground,
background,
border,
feedbackUnderlay,
innerShadows,
outline,
borderInsets,
borderRadius,
)
}
public fun withNewOutline(outline: OutlineDrawable): CompositeBackgroundDrawable {
return CompositeBackgroundDrawable(
context,
originalBackground,
outerShadows,
cssBackground,
background,
border,
feedbackUnderlay,
innerShadows,
outline,
borderInsets,
borderRadius,
)
}
public fun withNewFeedbackUnderlay(newUnderlay: Drawable?): CompositeBackgroundDrawable {
return CompositeBackgroundDrawable(
context,
originalBackground,
outerShadows,
cssBackground,
background,
border,
newUnderlay,
innerShadows,
outline,
borderInsets,
borderRadius,
)
}
/* Android's elevation implementation requires this to be implemented to know where to draw the
@@ -221,41 +219,27 @@ internal class CompositeBackgroundDrawable(
}
}
private fun <T> withNewLayer(
newLayer: T,
id: Int,
setNewLayer: (T) -> Unit,
): CompositeBackgroundDrawable where T : Drawable? {
setNewLayer(newLayer)
if (ReactNativeFeatureFlags.enableNewBackgroundAndBorderDrawables()) {
if (updateLayer(newLayer, id)) {
return this
}
companion object {
private fun createLayersArray(
originalBackground: Drawable?,
outerShadows: List<Drawable>,
cssBackground: CSSBackgroundDrawable?,
background: BackgroundDrawable?,
border: BorderDrawable?,
feedbackUnderlay: Drawable?,
innerShadows: List<Drawable>,
outline: OutlineDrawable?
): Array<Drawable?> {
val layers = mutableListOf<Drawable?>()
originalBackground?.let { layers.add(it) }
layers.addAll(outerShadows.asReversed())
cssBackground?.let { layers.add(it) }
background?.let { layers.add(it) }
border?.let { layers.add(it) }
feedbackUnderlay?.let { layers.add(it) }
layers.addAll(innerShadows.asReversed())
outline?.let { layers.add(it) }
return layers.toTypedArray()
}
return CompositeBackgroundDrawable(
context,
originalBackground,
outerShadows,
cssBackground,
background,
border,
feedbackUnderlay,
innerShadows,
outline)
.also { composite ->
composite.borderInsets = this.borderInsets
composite.borderRadius = this.borderRadius
}
}
private companion object {
private const val ORIGINAL_BACKGROUND_ID: Int = 0
private const val OUTER_SHADOWS_ID: Int = 1
private const val CSS_BACKGROUND_ID: Int = 2
private const val BACKGROUND_ID: Int = 3
private const val BORDER_ID: Int = 4
private const val FEEDBACK_UNDERLAY_ID: Int = 5
private const val INNER_SHADOWS_ID: Int = 6
private const val OUTLINE_ID: Int = 7
}
}
@@ -1,93 +0,0 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.uimanager
import android.annotation.TargetApi
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.LayerDrawable
import com.facebook.react.common.annotations.UnstableReactNativeAPI
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags
import com.facebook.react.uimanager.drawable.BackgroundDrawable
import com.facebook.react.uimanager.drawable.BorderDrawable
import com.facebook.react.uimanager.drawable.CompositeBackgroundDrawable
import com.facebook.react.uimanager.drawable.InsetBoxShadowDrawable
import com.facebook.react.uimanager.drawable.OutlineDrawable
import com.facebook.react.uimanager.drawable.OutsetBoxShadowDrawable
import com.facebook.react.uimanager.style.OutlineStyle
import org.assertj.core.api.Assertions.assertThat
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.MockedStatic
import org.mockito.Mockito.mockStatic
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
@TargetApi(29)
@OptIn(UnstableReactNativeAPI::class)
@RunWith(RobolectricTestRunner::class)
class CompositeBackgroundDrawableTest {
private val ctx: Context = RuntimeEnvironment.getApplication()
private lateinit var rnFeatureFlags: MockedStatic<ReactNativeFeatureFlags>
@Before
fun setup() {
rnFeatureFlags = mockStatic(ReactNativeFeatureFlags::class.java)
rnFeatureFlags
.`when`<Boolean> { ReactNativeFeatureFlags.enableNewBackgroundAndBorderDrawables() }
.thenReturn(true)
}
@Test
fun testCompositeBackgroundDrawableLayerOrdering() {
val background = BackgroundDrawable(ctx)
val outerShadows =
LayerDrawable(arrayOf(OutsetBoxShadowDrawable(ctx, Color.BLACK, 1f, 1f, 0f, 1f)))
val innerShadows =
LayerDrawable(arrayOf(InsetBoxShadowDrawable(ctx, Color.BLACK, 1f, 1f, 0f, 1f)))
val border = BorderDrawable(ctx, null, null, null, null)
val outline =
OutlineDrawable(
ctx,
outlineColor = Color.BLACK,
outlineOffset = 0f,
outlineStyle = OutlineStyle.SOLID,
outlineWidth = 1f)
/** Create CompositeBackgroundDrawable with constructor */
val control =
CompositeBackgroundDrawable(
context = ctx,
outerShadows = outerShadows,
background = background,
border = border,
innerShadows = innerShadows,
outline = outline)
/**
* Create CompositeBackgroundDrawable with shuffled method functions (triggers layer ordering
* logic)
*/
val test = CompositeBackgroundDrawable(ctx)
test.withNewInnerShadow(innerShadows)
test.withNewOuterShadow(outerShadows)
test.withNewBorder(border)
test.withNewOutline(outline)
test.withNewBackground(background)
/** Verify that the two CompositeBackgroundDrawables are equivalent */
assertThat(test.getDrawable(0)).isEqualTo(control.getDrawable(0))
assertThat(test.getDrawable(1)).isEqualTo(control.getDrawable(1))
assertThat(test.getDrawable(2)).isEqualTo(control.getDrawable(2))
assertThat(test.getDrawable(3)).isEqualTo(control.getDrawable(3))
assertThat(test.getDrawable(4)).isEqualTo(control.getDrawable(4))
}
}