From f528f6bae63e46d351303126f2b1d255ad0c7602 Mon Sep 17 00:00:00 2001 From: Alexander Oprisnik Date: Wed, 22 Nov 2023 04:19:02 -0800 Subject: [PATCH] Add new lightweight debug overlay for Vito v2 Reviewed By: javache Differential Revision: D51305440 fbshipit-source-id: 8a97d8c013435b3bdacd54ea6fbf8bd2a36b595c --- .../core/impl/debug/DebugOverlayDrawable.kt | 8 +++- .../impl/debug/BaseDebugOverlayFactory2.java | 1 + .../debug/DefaultDebugOverlayFactory2.java | 47 +++++++++++++++---- .../facebook/fresco/vito/init/FrescoVito.kt | 9 +++- .../impl/DefaultFrescoVitoProvider.kt | 7 ++- 5 files changed, 54 insertions(+), 18 deletions(-) diff --git a/vito/core-common-impl/src/main/java/com/facebook/fresco/vito/core/impl/debug/DebugOverlayDrawable.kt b/vito/core-common-impl/src/main/java/com/facebook/fresco/vito/core/impl/debug/DebugOverlayDrawable.kt index e6e8c41735..60ec8bac7c 100644 --- a/vito/core-common-impl/src/main/java/com/facebook/fresco/vito/core/impl/debug/DebugOverlayDrawable.kt +++ b/vito/core-common-impl/src/main/java/com/facebook/fresco/vito/core/impl/debug/DebugOverlayDrawable.kt @@ -24,13 +24,15 @@ import kotlin.math.min open class DebugOverlayDrawable @JvmOverloads constructor( - private val identifier: String = "", + val identifier: String = "", private val identifierColor: Int = 0xFF00FF00.toInt(), ) : Drawable() { @ColorInt var backgroundColor: Int = Color.TRANSPARENT var textGravity: Int = Gravity.TOP + var drawIdentifier: Boolean = true + // Internal helpers private val debugData = LinkedHashMap>() private val paint = Paint(Paint.ANTI_ALIAS_FLAG) @@ -93,7 +95,9 @@ constructor( // Reset the text position currentTextXPx = startTextXPx currentTextYPx = startTextYPx - addDebugText(canvas, "Vito", identifier, identifierColor) + if (drawIdentifier) { + addDebugText(canvas, "Vito", identifier, identifierColor) + } for ((key, value) in debugData) { addDebugText(canvas, key, value.first, value.second) } diff --git a/vito/core-java-impl/src/main/java/com/facebook/fresco/vito/core/impl/debug/BaseDebugOverlayFactory2.java b/vito/core-java-impl/src/main/java/com/facebook/fresco/vito/core/impl/debug/BaseDebugOverlayFactory2.java index 6f8eaa4cdb..010b84bde1 100644 --- a/vito/core-java-impl/src/main/java/com/facebook/fresco/vito/core/impl/debug/BaseDebugOverlayFactory2.java +++ b/vito/core-java-impl/src/main/java/com/facebook/fresco/vito/core/impl/debug/BaseDebugOverlayFactory2.java @@ -35,6 +35,7 @@ public void update(FrescoDrawable2 drawable, @Nullable ControllerListener2.Extra DebugOverlayDrawable overlay = extractOrCreate(drawable); overlay.reset(); setData(overlay, drawable, extras); + overlay.invalidateSelf(); } protected abstract void setData( diff --git a/vito/core-java-impl/src/main/java/com/facebook/fresco/vito/core/impl/debug/DefaultDebugOverlayFactory2.java b/vito/core-java-impl/src/main/java/com/facebook/fresco/vito/core/impl/debug/DefaultDebugOverlayFactory2.java index 729fa9b466..9f0564f913 100644 --- a/vito/core-java-impl/src/main/java/com/facebook/fresco/vito/core/impl/debug/DefaultDebugOverlayFactory2.java +++ b/vito/core-java-impl/src/main/java/com/facebook/fresco/vito/core/impl/debug/DefaultDebugOverlayFactory2.java @@ -23,8 +23,24 @@ @Nullsafe(Nullsafe.Mode.STRICT) public class DefaultDebugOverlayFactory2 extends BaseDebugOverlayFactory2 { + private boolean mShowExtendedInformation; + public DefaultDebugOverlayFactory2(Supplier debugOverlayEnabled) { + this(true, debugOverlayEnabled); + } + + public DefaultDebugOverlayFactory2( + boolean showExtendedInformation, Supplier debugOverlayEnabled) { super(debugOverlayEnabled); + mShowExtendedInformation = showExtendedInformation; + } + + public void setShowExtendedInformation(boolean showExtendedInformation) { + mShowExtendedInformation = showExtendedInformation; + } + + public boolean getShowExtendedInformation() { + return mShowExtendedInformation; } @Override @@ -37,18 +53,22 @@ protected void setData( setImageOriginData(overlay, extras); } - private static void setBasicData(DebugOverlayDrawable overlay, FrescoDrawableInterface drawable) { - overlay.addDebugData("ID", VitoUtils.getStringId(drawable.getImageId())); + private void setBasicData(DebugOverlayDrawable overlay, FrescoDrawableInterface drawable) { + overlay.setDrawIdentifier(mShowExtendedInformation); + String tag = mShowExtendedInformation ? "ID" : overlay.getIdentifier(); + overlay.addDebugData(tag, VitoUtils.getStringId(drawable.getImageId())); if (drawable instanceof FrescoDrawable2) { FrescoDrawable2 abstractDrawable = (FrescoDrawable2) drawable; Rect bounds = abstractDrawable.getBounds(); overlay.addDebugData("D", formatDimensions(bounds.width(), bounds.height())); - overlay.addDebugData("DAR", String.valueOf(bounds.width() / (float) bounds.height())); + if (mShowExtendedInformation) { + overlay.addDebugData("DAR", String.valueOf(bounds.width() / (float) bounds.height())); + } overlay.addDebugData( "I", formatDimensions( abstractDrawable.getActualImageWidthPx(), abstractDrawable.getActualImageHeightPx())); - if (abstractDrawable.getActualImageHeightPx() > 0) { + if (mShowExtendedInformation && abstractDrawable.getActualImageHeightPx() > 0) { overlay.addDebugData( "IAR", String.valueOf( @@ -58,7 +78,7 @@ private static void setBasicData(DebugOverlayDrawable overlay, FrescoDrawableInt } } - private static void setImageOriginData( + private void setImageOriginData( DebugOverlayDrawable overlay, @Nullable ControllerListener2.Extras extras) { String origin = "unknown"; String originSubcategory = "unknown"; @@ -74,14 +94,21 @@ private static void setImageOriginData( originSubcategory = String.valueOf(originExtras.get("origin_sub")); } } - overlay.addDebugData( - "origin", origin, DebugOverlayImageOriginColor.getImageOriginColor(origin)); - overlay.addDebugData("origin_sub", originSubcategory, Color.GRAY); + if (mShowExtendedInformation) { + overlay.addDebugData( + "origin", origin, DebugOverlayImageOriginColor.getImageOriginColor(origin)); + overlay.addDebugData("origin_sub", originSubcategory, Color.GRAY); + } else { + overlay.addDebugData( + "o", + origin + " | " + originSubcategory, + DebugOverlayImageOriginColor.getImageOriginColor(origin)); + } } - private static void setImageRequestData( + private void setImageRequestData( DebugOverlayDrawable overlay, @Nullable VitoImageRequest imageRequest) { - if (imageRequest == null) { + if (imageRequest == null || !mShowExtendedInformation) { return; } overlay.addDebugData( diff --git a/vito/init/src/main/java/com/facebook/fresco/vito/init/FrescoVito.kt b/vito/init/src/main/java/com/facebook/fresco/vito/init/FrescoVito.kt index cadcceaa02..2148ce866f 100644 --- a/vito/init/src/main/java/com/facebook/fresco/vito/init/FrescoVito.kt +++ b/vito/init/src/main/java/com/facebook/fresco/vito/init/FrescoVito.kt @@ -20,6 +20,8 @@ import com.facebook.fresco.vito.core.impl.BaseVitoImagePerfListener import com.facebook.fresco.vito.core.impl.DefaultImageDecodeOptionsProviderImpl import com.facebook.fresco.vito.core.impl.ImagePipelineUtilsImpl import com.facebook.fresco.vito.core.impl.ImagePipelineUtilsImpl.CircularBitmapRounding +import com.facebook.fresco.vito.core.impl.debug.DefaultDebugOverlayFactory2 +import com.facebook.fresco.vito.core.impl.debug.NoOpDebugOverlayFactory2 import com.facebook.fresco.vito.nativecode.NativeCircularBitmapRounding import com.facebook.fresco.vito.provider.FrescoVitoProvider import com.facebook.fresco.vito.provider.impl.DefaultFrescoVitoProvider @@ -50,12 +52,13 @@ class FrescoVito { imagePipeline: ImagePipeline? = null, lightweightBackgroundThreadExecutor: Executor? = null, uiThreadExecutor: Executor? = null, - debugOverlayEnabledSupplier: Supplier? = null, + debugOverlayEnabledSupplier: Supplier? = null, useNativeCode: Supplier = Suppliers.BOOLEAN_TRUE, vitoConfig: FrescoVitoConfig = DefaultFrescoVitoConfig(), callerContextVerifier: CallerContextVerifier = NoOpCallerContextVerifier, vitoImagePerfListener: VitoImagePerfListener = BaseVitoImagePerfListener(), imagePerfListenerSupplier: Supplier>? = null, + showExtendedDebugOverlayInformation: Boolean = true ) { if (isInitialized) { return @@ -72,9 +75,11 @@ class FrescoVito { createImagePipelineUtils(useNativeCode), lightweightBackgroundThreadExecutor, uiThreadExecutor, - debugOverlayEnabledSupplier, callerContextVerifier, vitoImagePerfListener, + debugOverlayEnabledSupplier?.let { + DefaultDebugOverlayFactory2(showExtendedDebugOverlayInformation, it) + } ?: NoOpDebugOverlayFactory2(), imagePerfListenerSupplier)) } diff --git a/vito/provider/src/main/java/com/facebook/fresco/vito/provider/impl/DefaultFrescoVitoProvider.kt b/vito/provider/src/main/java/com/facebook/fresco/vito/provider/impl/DefaultFrescoVitoProvider.kt index 9a755354fe..7f260c5afd 100644 --- a/vito/provider/src/main/java/com/facebook/fresco/vito/provider/impl/DefaultFrescoVitoProvider.kt +++ b/vito/provider/src/main/java/com/facebook/fresco/vito/provider/impl/DefaultFrescoVitoProvider.kt @@ -20,7 +20,7 @@ import com.facebook.fresco.vito.core.impl.FrescoController2Impl import com.facebook.fresco.vito.core.impl.FrescoVitoPrefetcherImpl import com.facebook.fresco.vito.core.impl.HierarcherImpl import com.facebook.fresco.vito.core.impl.VitoImagePipelineImpl -import com.facebook.fresco.vito.core.impl.debug.DefaultDebugOverlayFactory2 +import com.facebook.fresco.vito.core.impl.debug.DebugOverlayFactory2 import com.facebook.fresco.vito.core.impl.debug.NoOpDebugOverlayFactory2 import com.facebook.fresco.vito.drawable.ArrayVitoDrawableFactory import com.facebook.fresco.vito.drawable.BitmapDrawableFactory @@ -39,9 +39,9 @@ class DefaultFrescoVitoProvider( imagePipelineUtils: ImagePipelineUtils, lightweightBackgroundThreadExecutor: Executor, uiThreadExecutor: Executor, - debugOverlayEnabledSupplier: Supplier?, callerContextVerifier: CallerContextVerifier, vitoImagePerfListener: VitoImagePerfListener, + debugOverlayFactory: DebugOverlayFactory2 = NoOpDebugOverlayFactory2(), imagePerfListenerSupplier: Supplier>? = null, ) : FrescoVitoProvider.Implementation { @@ -67,8 +67,7 @@ class DefaultFrescoVitoProvider( uiThreadExecutor, vitoImagePipeline, null, - debugOverlayEnabledSupplier?.let { DefaultDebugOverlayFactory2(it) } - ?: NoOpDebugOverlayFactory2(), + debugOverlayFactory, imagePerfListenerSupplier, vitoImagePerfListener) }