diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/drawable/BackgroundImageDrawable.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/drawable/BackgroundImageDrawable.kt index 7640f1b1fd9..3ec4e8a67f0 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/drawable/BackgroundImageDrawable.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/drawable/BackgroundImageDrawable.kt @@ -77,6 +77,19 @@ internal fun calculateBackgroundImageSize( finalWidth = finalHeight * ratio } } + } else if ( + keepsRatio && + (backgroundSize is BackgroundSize.Cover || backgroundSize is BackgroundSize.Contain) + ) { + xAuto = false + yAuto = false + val widthScale = containerWidth / imageWidth + val heightScale = containerHeight / imageHeight + val scale = + if (backgroundSize is BackgroundSize.Cover) maxOf(widthScale, heightScale) + else minOf(widthScale, heightScale) + finalWidth = imageWidth * scale + finalHeight = imageHeight * scale } if (repeat?.x == BackgroundRepeatKeyword.Round && finalWidth > 0) { diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/drawable/BackgroundImageURLLoader.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/drawable/BackgroundImageURLLoader.kt index 09a76520535..fb10b912c10 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/drawable/BackgroundImageURLLoader.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/drawable/BackgroundImageURLLoader.kt @@ -15,13 +15,13 @@ import com.facebook.common.logging.FLog import com.facebook.common.references.CloseableReference import com.facebook.datasource.BaseDataSubscriber import com.facebook.datasource.DataSource -import com.facebook.drawee.backends.pipeline.Fresco -import com.facebook.drawee.components.DeferredReleaser import com.facebook.fresco.middleware.HasExtraData import com.facebook.imagepipeline.common.ImageDecodeOptions +import com.facebook.imagepipeline.core.ImagePipelineFactory import com.facebook.imagepipeline.image.CloseableImage import com.facebook.imagepipeline.image.CloseableStaticBitmap import com.facebook.imagepipeline.request.ImageRequestBuilder +import com.facebook.react.bridge.UiThreadUtil import com.facebook.react.common.ReactConstants import com.facebook.react.uimanager.style.BackgroundImageLayer import com.facebook.react.views.imagehelper.ImageSource @@ -115,8 +115,18 @@ internal class BackgroundImageURLLoader( ImageDecodeOptions.newBuilder().setForceStaticImage(true).build() ) } + val imageRequest = + try { + requestBuilder.build() + } catch (e: RuntimeException) { + // `build()` validates the uri, e.g. a `res:` uri must name a resource id. + FLog.w(ReactConstants.TAG, e, "Invalid background image uri: %s", uri) + return + } val dataSource = - Fresco.getImagePipeline().fetchDecodedImage(requestBuilder.build(), CALLER_CONTEXT) + ImagePipelineFactory.getInstance() + .imagePipeline + .fetchDecodedImage(imageRequest, CALLER_CONTEXT) this.dataSource = dataSource dataSource.subscribe( object : BaseDataSubscriber>() { @@ -181,9 +191,7 @@ internal class BackgroundImageURLLoader( this.imageReference = null // The last frame drawn can still use the bitmap, so release it after that frame, like // Fresco's drawee. - DeferredReleaser.getInstance().scheduleDeferredRelease { - CloseableReference.closeSafely(imageReference) - } + UiThreadUtil.runOnUiThread { CloseableReference.closeSafely(imageReference) } } } } diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/style/BackgroundSize.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/style/BackgroundSize.kt index 3b764409572..4e7bd0a9225 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/style/BackgroundSize.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/style/BackgroundSize.kt @@ -110,8 +110,8 @@ internal class BackgroundSizeLengthPercentage( /** * Sealed class representing CSS background-size property values. * - * This class models the different ways a background size can be specified in CSS, currently - * supporting length/percentage/auto values for both dimensions. + * This class models the different ways a background size can be specified in CSS: length, + * percentage, or auto values for both dimensions, or the `cover` and `contain` keywords. * * @see BackgroundSizeLengthPercentage */ @@ -124,11 +124,17 @@ internal sealed class BackgroundSize { class LengthPercentageAuto(val lengthPercentage: BackgroundSizeLengthPercentage) : BackgroundSize() + /** Scales an image that has an aspect ratio to cover the whole background area. */ + object Cover : BackgroundSize() + + /** Scales an image that has an aspect ratio to fit inside the background area. */ + object Contain : BackgroundSize() + companion object { /** * Parses a Dynamic value into a BackgroundSize. * - * Currently supports map values containing x/y dimensions. + * Supports map values containing x/y dimensions, and the `cover` and `contain` keywords. * * @param backgroundSizeValue The dynamic value to parse * @return A BackgroundSize instance, or null if parsing fails @@ -146,6 +152,12 @@ internal sealed class BackgroundSize { null } } + ReadableType.String -> + when (backgroundSizeValue.asString()) { + "cover" -> Cover + "contain" -> Contain + else -> null + } else -> null } } diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/drawable/BackgroundImageDrawableTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/drawable/BackgroundImageDrawableTest.kt index 713b257c989..e0b06848487 100644 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/drawable/BackgroundImageDrawableTest.kt +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/drawable/BackgroundImageDrawableTest.kt @@ -9,6 +9,8 @@ package com.facebook.react.uimanager.drawable import com.facebook.react.uimanager.LengthPercentage import com.facebook.react.uimanager.LengthPercentageType +import com.facebook.react.uimanager.style.BackgroundRepeat +import com.facebook.react.uimanager.style.BackgroundRepeatKeyword import com.facebook.react.uimanager.style.BackgroundSize import com.facebook.react.uimanager.style.BackgroundSizeLengthPercentage import org.assertj.core.api.Assertions.assertThat @@ -44,4 +46,59 @@ class BackgroundImageDrawableTest { assertThat(result).isEqualTo(50f to 12f) } + + @Test + fun coverFillsTheAreaKeepingTheRatio() { + val result = + calculateBackgroundImageSize( + 300f, + 200f, + 100f, + 100f, + BackgroundSize.Cover, + null, + hasIntrinsicRatio = true, + ) + + assertThat(result).isEqualTo(300f to 300f) + } + + @Test + fun containFitsInsideTheAreaKeepingTheRatio() { + val result = + calculateBackgroundImageSize( + 300f, + 200f, + 100f, + 100f, + BackgroundSize.Contain, + null, + hasIntrinsicRatio = true, + ) + + assertThat(result).isEqualTo(200f to 200f) + } + + @Test + fun coverWithoutARatioKeepsTheGivenSize() { + val result = calculateBackgroundImageSize(300f, 200f, 300f, 200f, BackgroundSize.Cover, null) + + assertThat(result).isEqualTo(300f to 200f) + } + + @Test + fun roundOnOneAxisDoesNotRescaleContainOnTheOther() { + val result = + calculateBackgroundImageSize( + 300f, + 200f, + 100f, + 100f, + BackgroundSize.Contain, + BackgroundRepeat(BackgroundRepeatKeyword.Round, BackgroundRepeatKeyword.NoRepeat), + hasIntrinsicRatio = true, + ) + + assertThat(result).isEqualTo(150f to 200f) + } } diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/drawable/BackgroundImageURLLoaderTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/drawable/BackgroundImageURLLoaderTest.kt index 2be1ac52af1..75889288aaa 100644 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/drawable/BackgroundImageURLLoaderTest.kt +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/drawable/BackgroundImageURLLoaderTest.kt @@ -8,13 +8,18 @@ package com.facebook.react.uimanager.drawable import android.graphics.Bitmap +import com.facebook.imagepipeline.core.ImagePipelineFactory +import com.facebook.react.bridge.JavaOnlyMap +import com.facebook.react.uimanager.style.BackgroundImageLayer import org.assertj.core.api.Assertions.* +import org.junit.After import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment import org.robolectric.annotation.GraphicsMode -/** Tests for [BackgroundImageURLLoader.LoadedImage] */ +/** Tests for [BackgroundImageURLLoader] */ @RunWith(RobolectricTestRunner::class) @GraphicsMode(GraphicsMode.Mode.NATIVE) class BackgroundImageURLLoaderTest { @@ -50,4 +55,21 @@ class BackgroundImageURLLoaderTest { assertThat(image.height).isEqualTo(40f) } } + + @After + fun tearDown() { + ImagePipelineFactory.shutDown() + } + + @Test + fun testTreatsInvalidResourceUriAsFailedLoad() { + val context = RuntimeEnvironment.getApplication() + ImagePipelineFactory.initialize(context) + val loader = BackgroundImageURLLoader(context) {} + for (uri in listOf("res:///icon", "res:icon")) { + val layer = BackgroundImageLayer.parse(JavaOnlyMap.of("type", "url", "uri", uri), context) + loader.loadBackgroundImages(listOfNotNull(layer)) + assertThat(loader.imageForUri(uri)).isNull() + } + } } diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/style/BackgroundImageLayerTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/style/BackgroundImageLayerTest.kt index 0dc44cf739f..18301264ca0 100644 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/style/BackgroundImageLayerTest.kt +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/style/BackgroundImageLayerTest.kt @@ -103,7 +103,7 @@ class BackgroundImageLayerTest { @Test fun testRejectsUnknownType() { - assertThat(BackgroundImageLayer.parse(JavaOnlyMap.of("type", "conic-gradient"), context)) + assertThat(BackgroundImageLayer.parse(JavaOnlyMap.of("type", "unknown-gradient"), context)) .isNull() assertThat(BackgroundImageLayer.parse(null, context)).isNull() }