Skip to content

Commit

Permalink
Disable loading XML on main thread
Browse files Browse the repository at this point in the history
Summary:
## Summary

Now that XML resources are loaded via Fresco decoder, this change removes the custom branch that attempts to load drawables on the main thread. Loading (i.e. binary XML decompression) is performed on a background decoder thread from Fresco the same as every other image type.

## Changelog
[Android][Fixed] - Avoid blocking the main thread when decompressing drawable resources

Differential Revision: D63476282
  • Loading branch information
Abbondanzo authored and facebook-github-bot committed Sep 27, 2024
1 parent 2a1f1be commit f93b716
Showing 1 changed file with 1 addition and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ import com.facebook.react.common.annotations.UnstableReactNativeAPI
import com.facebook.react.common.annotations.VisibleForTesting
import com.facebook.react.common.build.ReactBuildConfig
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags.loadVectorDrawablesOnImages
import com.facebook.react.modules.fresco.ReactNetworkImageRequest
import com.facebook.react.uimanager.BackgroundStyleApplicator
import com.facebook.react.uimanager.LengthPercentage
Expand Down Expand Up @@ -394,12 +393,7 @@ public class ReactImageView(
else -> REMOTE_IMAGE_FADE_DURATION_MS
}

val drawable = getDrawableIfUnsupported(imageSourceSafe)
if (drawable != null) {
maybeUpdateViewFromDrawable(drawable)
} else {
maybeUpdateViewFromRequest(doResize)
}
maybeUpdateViewFromRequest(doResize)

isDirty = false
}
Expand Down Expand Up @@ -474,34 +468,6 @@ public class ReactImageView(
builder.reset()
}

private fun maybeUpdateViewFromDrawable(drawable: Drawable) {
val shouldNotify = downloadListener != null

val eventDispatcher =
if (shouldNotify) {
UIManagerHelper.getEventDispatcherForReactTag((context as ReactContext), id)
} else {
null
}

eventDispatcher?.dispatchEvent(
createLoadStartEvent(UIManagerHelper.getSurfaceId(this@ReactImageView), id))

hierarchy.setImage(drawable, 1f, false)

if (eventDispatcher != null && imageSource != null) {
eventDispatcher.dispatchEvent(
createLoadEvent(
UIManagerHelper.getSurfaceId(this@ReactImageView),
id,
imageSource?.source,
width,
height))
eventDispatcher.dispatchEvent(
createLoadEndEvent(UIManagerHelper.getSurfaceId(this@ReactImageView), id))
}
}

@VisibleForTesting
public fun setControllerListener(controllerListener: ControllerListener<ImageInfo>?) {
controllerForTesting = controllerListener
Expand Down Expand Up @@ -547,30 +513,6 @@ public class ReactImageView(
else -> false
}

/**
* Checks if the provided ImageSource should not be requested through Fresco and instead loaded
* directly from the resources table. Fresco explicitly does not support a number of drawable
* types like VectorDrawable but they can still be mounted in the image hierarchy.
*
* @param imageSource
* @return drawable resource if Fresco cannot load the image, null otherwise
*/
private fun getDrawableIfUnsupported(imageSource: ImageSource): Drawable? {
if (!loadVectorDrawablesOnImages()) {
return null
}
val resourceName = imageSource.source
if (!imageSource.isResource || resourceName == null) {
return null
}
val drawableHelper = instance
val isVectorDrawable = drawableHelper.isVectorDrawable(context, resourceName)
if (!isVectorDrawable) {
return null
}
return drawableHelper.getResourceDrawable(context, resourceName)
}

private val resizeOptions: ResizeOptions?
get() {
val width = Math.round(width.toFloat() * resizeMultiplier)
Expand Down

0 comments on commit f93b716

Please sign in to comment.