From ecae4dece8636161e0e384fea61087f76c502757 Mon Sep 17 00:00:00 2001 From: Peter Abbondanzo Date: Tue, 1 Dec 2026 17:20:02 -0800 Subject: [PATCH 1/3] Stop crashing on invalid background url() uris on Android (#60409) Summary: Building an image request validates its uri and throws for one the pipeline can't load, such as a `res:` uri without a resource id. That ran in the `backgroundImage` prop setter on the UI thread, so a bad style value crashed the app. The loader now logs a warning and skips that layer, like any other failed load. Also replaces `conic-gradient` as the unknown layer type in a parsing test with a made-up type, since conic gradients are supported now. Changelog: [Internal] Differential Revision: D135152981 --- .../drawable/BackgroundImageURLLoader.kt | 11 +++++++-- .../drawable/BackgroundImageURLLoaderTest.kt | 24 ++++++++++++++++++- .../style/BackgroundImageLayerTest.kt | 2 +- 3 files changed, 33 insertions(+), 4 deletions(-) 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 09a765205351..7ccfd622de05 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 @@ -115,8 +115,15 @@ internal class BackgroundImageURLLoader( ImageDecodeOptions.newBuilder().setForceStaticImage(true).build() ) } - val dataSource = - Fresco.getImagePipeline().fetchDecodedImage(requestBuilder.build(), CALLER_CONTEXT) + 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(imageRequest, CALLER_CONTEXT) this.dataSource = dataSource dataSource.subscribe( object : BaseDataSubscriber>() { 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 2be1ac52af18..75889288aaab 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 0dc44cf739fa..18301264ca09 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() } From fe6ca3893ce33c65b5c5d1f386391396213b14e4 Mon Sep 17 00:00:00 2001 From: Peter Abbondanzo Date: Tue, 1 Dec 2026 17:20:02 -0800 Subject: [PATCH 2/3] Drop Drawee from Android background image loading (#60411) Summary: The `url()` background image loader used Drawee only to reach the image pipeline and to defer releasing a bitmap. It now gets the same pipeline from `ImagePipelineFactory`, and releases the bitmap on the next main-thread turn, which is what the Drawee releaser does on the UI thread. Background images now depend on the image pipeline alone. Changelog: [Internal] Differential Revision: D135152979 --- .../uimanager/drawable/BackgroundImageURLLoader.kt | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) 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 7ccfd622de05..fb10b912c100 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 @@ -123,7 +123,10 @@ internal class BackgroundImageURLLoader( FLog.w(ReactConstants.TAG, e, "Invalid background image uri: %s", uri) return } - val dataSource = Fresco.getImagePipeline().fetchDecodedImage(imageRequest, CALLER_CONTEXT) + val dataSource = + ImagePipelineFactory.getInstance() + .imagePipeline + .fetchDecodedImage(imageRequest, CALLER_CONTEXT) this.dataSource = dataSource dataSource.subscribe( object : BaseDataSubscriber>() { @@ -188,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) } } } } From adb70e1a1059217e8813161d8711637be6e22138 Mon Sep 17 00:00:00 2001 From: Peter Abbondanzo Date: Tue, 1 Dec 2026 17:20:02 -0800 Subject: [PATCH 3/3] Support cover and contain backgroundSize for url() images on Android (#60410) Summary: Android only parsed length and percentage values for `backgroundSize`, so `cover` and `contain` were dropped and a `url()` image drew at its natural size. They now scale an image that has an aspect ratio to cover or fit inside the background area. Gradients have no aspect ratio, so they keep filling it. Today the keywords only reach Android as items of an array value with native CSS parsing on. The JS style processor drops them, and the native parser drops a bare string. Changelog: [Internal] Differential Revision: D135152982 --- .../drawable/BackgroundImageDrawable.kt | 13 +++++ .../react/uimanager/style/BackgroundSize.kt | 18 +++++- .../drawable/BackgroundImageDrawableTest.kt | 57 +++++++++++++++++++ 3 files changed, 85 insertions(+), 3 deletions(-) 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 7640f1b1fd97..3ec4e8a67f00 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/style/BackgroundSize.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/style/BackgroundSize.kt index 3b764409572f..4e7bd0a9225d 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 713b257c989f..e0b068484873 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) + } }