forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom Fresco decoder for binary XML
Summary: ## Summary Vector drawable decompression is a blocking operation and can sometimes take upwards of 20-30ms per image, especially if that vector drawable uses complex colors. Less complex vector drawables still take several milliseconds to decompress and are served from a cache on later load attempts. Here's a list of all the load times of vector drawable images in FBVR: {F1891592104} This diff aims to shift decompression to one of Fresco's decode threads, off the main thread, so we don't block while waiting for decompression operations to complete. This relies on adding a new custom decoder that reads the header of XML binary and converts encoded image requests to drawable objects that are yielded from the new `XmlDrawableFactory`. It's important to note that this change does not stop `ReactImageView` from loading XML-based drawables on the main thread, it merely offers a new mechanism for loading them. ## Changelog [Android][Added] - Add a new Fresco decoder for XML resource types Differential Revision: D63476283
- Loading branch information
1 parent
7c60a65
commit 2185c3b
Showing
3 changed files
with
148 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
...es/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/fresco/XmlFormat.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* 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.modules.fresco | ||
|
||
import android.content.Context | ||
import android.graphics.drawable.Drawable | ||
import android.net.Uri | ||
import com.facebook.common.logging.FLog | ||
import com.facebook.imageformat.ImageFormat | ||
import com.facebook.imageformat.ImageFormat.FormatChecker | ||
import com.facebook.imageformat.ImageFormatCheckerUtils | ||
import com.facebook.imagepipeline.common.ImageDecodeOptions | ||
import com.facebook.imagepipeline.decoder.ImageDecoder | ||
import com.facebook.imagepipeline.drawable.DrawableFactory | ||
import com.facebook.imagepipeline.image.CloseableImage | ||
import com.facebook.imagepipeline.image.DefaultCloseableImage | ||
import com.facebook.imagepipeline.image.EncodedImage | ||
import com.facebook.imagepipeline.image.QualityInfo | ||
|
||
public object XmlFormat { | ||
public val FORMAT: ImageFormat = ImageFormat("XML", "xml") | ||
private const val TAG: String = "XmlFormat" | ||
// https://justanapplication.wordpress.com/category/android/android-binary-xml/ | ||
private val BINARY_XML_HEADER: ByteArray = | ||
byteArrayOf( | ||
3.toByte(), | ||
0.toByte(), | ||
8.toByte(), | ||
0.toByte(), | ||
) | ||
|
||
public class XmlFormatChecker : FormatChecker { | ||
override val headerSize: Int = BINARY_XML_HEADER.size | ||
|
||
override fun determineFormat(headerBytes: ByteArray, headerSize: Int): ImageFormat { | ||
return when { | ||
headerSize < BINARY_XML_HEADER.size -> ImageFormat.UNKNOWN | ||
ImageFormatCheckerUtils.startsWithPattern(headerBytes, BINARY_XML_HEADER) -> FORMAT | ||
else -> ImageFormat.UNKNOWN | ||
} | ||
} | ||
} | ||
|
||
private class CloseableXmlImage(val name: String, val drawable: Drawable) : | ||
DefaultCloseableImage() { | ||
private var closed = false | ||
|
||
override fun getSizeInBytes(): Int { | ||
return getWidth() * getHeight() * 4 // 4 bytes ARGB per pixel | ||
} | ||
|
||
override fun close() { | ||
closed = true | ||
} | ||
|
||
override fun isClosed(): Boolean { | ||
return closed | ||
} | ||
|
||
override fun getWidth(): Int { | ||
return drawable.intrinsicWidth.takeIf { it >= 0 } ?: 0 | ||
} | ||
|
||
override fun getHeight(): Int { | ||
return drawable.intrinsicHeight.takeIf { it >= 0 } ?: 0 | ||
} | ||
} | ||
|
||
public class XmlFormatDecoder(private val context: Context) : ImageDecoder { | ||
override fun decode( | ||
encodedImage: EncodedImage, | ||
length: Int, | ||
qualityInfo: QualityInfo, | ||
options: ImageDecodeOptions | ||
): CloseableImage? { | ||
return try { | ||
val xmlResourceName = encodedImage.source ?: error("No source in encoded image") | ||
val xmlResource = Uri.parse(xmlResourceName) | ||
// Android resource names are of the format [res|resources]:/[?package]/[res id] | ||
val xmlResourceId = | ||
xmlResource.pathSegments.lastOrNull()?.toIntOrNull() ?: error("Invalid resource id") | ||
// Use application context to avoid leaking the activity | ||
val drawable = context.applicationContext.resources.getDrawable(xmlResourceId, null) | ||
CloseableXmlImage(xmlResourceName, drawable) | ||
} catch (error: Throwable) { | ||
FLog.e(TAG, "Cannot decode xml ${error}", error) | ||
null | ||
} | ||
} | ||
} | ||
|
||
public class XmlDrawableFactory : DrawableFactory { | ||
override fun supportsImageType(image: CloseableImage): Boolean { | ||
return image is CloseableXmlImage | ||
} | ||
|
||
override fun createDrawable(image: CloseableImage): Drawable? { | ||
return (image as CloseableXmlImage).drawable | ||
} | ||
} | ||
} |