Skip to content

Commit

Permalink
Merge branch 'main' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnylqm authored Nov 25, 2023
2 parents dfa54cf + ebca343 commit d5fcc31
Show file tree
Hide file tree
Showing 19 changed files with 160 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,7 @@ class BufferFrameLoader(
bitmapRef = bufferFrame.bitmapRef.clone()
}
bufferFrame.isUpdatingFrame = true
obtainFrame(bitmapRef, newFrameNumber, width, height)
bitmapRef.close()
bitmapRef.use { obtainFrame(it, newFrameNumber, width, height) }
bufferFramesHash.remove(deprecatedFrameNumber)
bufferFrame.isUpdatingFrame = false

Expand All @@ -188,9 +187,8 @@ class BufferFrameLoader(
height: Int
) {
val nearestFrame = findNearestFrame(targetFrame)
val nearestBitmap = nearestFrame?.bitmap

if (nearestFrame != null && nearestBitmap != null) {
nearestFrame?.bitmap?.cloneOrNull()?.use { nearestBitmap ->
val from = nearestFrame.frameNumber

if (from < targetFrame) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.facebook.common.internal.Preconditions;
import com.facebook.common.internal.Supplier;
import com.facebook.common.time.MonotonicClock;
import com.facebook.drawee.drawable.VisibilityCallback;
import com.facebook.fresco.ui.common.BaseControllerListener2;
import com.facebook.fresco.ui.common.ControllerListener2;
import com.facebook.fresco.ui.common.DimensionsInfo;
Expand All @@ -32,7 +33,10 @@

@Nullsafe(Nullsafe.Mode.LOCAL)
public class ImagePerfControllerListener2 extends BaseControllerListener2<ImageInfo>
implements ImagePerfNotifierHolder, OnDrawControllerListener<ImageInfo>, Closeable {
implements ImagePerfNotifierHolder,
OnDrawControllerListener<ImageInfo>,
Closeable,
VisibilityCallback {

private static final int WHAT_STATUS = 1;
private static final int WHAT_VISIBILITY = 2;
Expand All @@ -46,6 +50,8 @@ public class ImagePerfControllerListener2 extends BaseControllerListener2<ImageI

private @Nullable ImagePerfNotifier mLocalImagePerfNotifier = null;

private final boolean mReportVisibleOnSubmitAndRelease;

static class LogHandler extends Handler implements ImagePerfNotifierHolder {

private final ImagePerfNotifier mNotifier;
Expand Down Expand Up @@ -100,11 +106,20 @@ public ImagePerfControllerListener2(
ImagePerfState imagePerfState,
ImagePerfNotifier globalImagePerfNotifier,
Supplier<Boolean> asyncLogging) {
this(clock, imagePerfState, globalImagePerfNotifier, asyncLogging, true);
}

public ImagePerfControllerListener2(
MonotonicClock clock,
ImagePerfState imagePerfState,
ImagePerfNotifier globalImagePerfNotifier,
Supplier<Boolean> asyncLogging,
boolean reportVisibleOnSubmitAndRelease) {
mClock = clock;
mImagePerfState = imagePerfState;
mImagePerfNotifier = globalImagePerfNotifier;

mAsyncLogging = asyncLogging;
mReportVisibleOnSubmitAndRelease = reportVisibleOnSubmitAndRelease;
}

@Override
Expand All @@ -130,7 +145,9 @@ public void onSubmit(
state.setExtraData(extraData);

updateStatus(state, ImageLoadStatus.REQUESTED);
reportViewVisible(state, now);
if (mReportVisibleOnSubmitAndRelease) {
reportViewVisible(state, now);
}
}

@Override
Expand Down Expand Up @@ -198,8 +215,11 @@ public void onRelease(String id, @Nullable ControllerListener2.Extras extras) {
// The image request was canceled
updateStatus(state, ImageLoadStatus.CANCELED);
}
updateStatus(state, ImageLoadStatus.RELEASED);

reportViewInvisible(state, now);
if (mReportVisibleOnSubmitAndRelease) {
reportViewInvisible(state, now);
}
}

@Override
Expand Down Expand Up @@ -300,4 +320,18 @@ public void onEmptyEvent(@androidx.annotation.Nullable Object callerContext) {
localImagePerfNotifier.notifyStatusUpdated(state, ImageLoadStatus.EMPTY_EVENT);
}
}

@Override
public void onVisibilityChange(boolean visible) {
if (visible) {
reportViewVisible(mImagePerfState, mClock.now());
} else {
reportViewInvisible(mImagePerfState, mClock.now());
}
}

@Override
public void onDraw() {
// No-op
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,11 @@ private static BitmapFactory.Options getDecodeOptionsForStream(
options.inSampleSize = encodedImage.getSampleSize();
options.inJustDecodeBounds = true;
options.inDither = true;
options.inPreferredConfig = bitmapConfig;
boolean isHardwareBitmap =
Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && bitmapConfig == Bitmap.Config.HARDWARE;
if (!isHardwareBitmap) {
options.inPreferredConfig = bitmapConfig;
}
options.inMutable = true;
if (!skipDecoding) {
// fill outWidth and outHeight
Expand All @@ -340,7 +344,9 @@ private static BitmapFactory.Options getDecodeOptionsForStream(
throw new IllegalArgumentException();
}
}

if (isHardwareBitmap) {
options.inPreferredConfig = bitmapConfig;
}
options.inJustDecodeBounds = false;
return options;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,12 @@ class ShowcaseApplication : Application() {
vitoConfig,
ImagePipelineFactory.getInstance().imagePipeline,
FrescoVito.createImagePipelineUtils(Suppliers.BOOLEAN_TRUE),
UiThreadImmediateExecutorService.getInstance(),
ImagePipelineFactory.getInstance()
.imagePipeline
.config
.executorSupplier
.forLightweightBackgroundTasks(),
UiThreadImmediateExecutorService.getInstance(),
NoOpCallerContextVerifier,
DebugOverlayHandler(DebugOverlaySupplierSingleton.getInstance(applicationContext))))
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ enum class ImageLoadStatus(val value: Int) {
CANCELED(4),
ERROR(5),
DRAW(6),
EMPTY_EVENT(7);
EMPTY_EVENT(7),
RELEASED(8);

/**
* This was probably only used in open source version, so we might be able to remove this custom
Expand All @@ -28,6 +29,7 @@ enum class ImageLoadStatus(val value: Int) {
CANCELED -> "canceled"
INTERMEDIATE_AVAILABLE -> "intermediate_available"
ERROR -> "error"
RELEASED -> "released"
else -> "unknown"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ class CombinedImageListenerImpl : CombinedImageListener {
checkAndSetLocalImagePerfStateListener()
}

override fun getImagePerfControllerListener(): ControllerListener2<ImageInfo>? {
return imagePerfControllerListener
}

override fun setLocalImagePerfStateListener(imagePerfNotifier: ImagePerfNotifier?) {
localImagePerfStateListener = imagePerfNotifier
checkAndSetLocalImagePerfStateListener()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Pair<String, Int>>()
private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ import android.graphics.drawable.Drawable
import com.facebook.common.closeables.AutoCleanupDelegate
import com.facebook.datasource.DataSource
import com.facebook.drawee.drawable.VisibilityCallback
import com.facebook.fresco.ui.common.ControllerListener2
import com.facebook.fresco.vito.core.CombinedImageListener
import com.facebook.fresco.vito.core.FrescoDrawableInterface
import com.facebook.fresco.vito.core.VitoImagePerfListener
import com.facebook.fresco.vito.core.VitoImageRequest
import com.facebook.fresco.vito.listener.ImageListener
import com.facebook.fresco.vito.renderer.DrawableImageDataModel
import com.facebook.imagepipeline.image.ImageInfo
import java.io.Closeable
import java.io.IOException

Expand Down Expand Up @@ -56,6 +58,9 @@ class KFrescoVitoDrawable(

override var refetchRunnable: Runnable? = null

override fun getImagePerfControllerListener(): ControllerListener2<ImageInfo>? =
listenerManager.getImagePerfControllerListener()

override val imageId: Long
get() = _imageId

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,4 +423,10 @@ public int getIntrinsicHeight() {
}
return super.getIntrinsicHeight();
}

@Nullable
@Override
public ControllerListener2<ImageInfo> getImagePerfControllerListener() {
return mImageListener.getImagePerfControllerListener();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,24 @@
@Nullsafe(Nullsafe.Mode.STRICT)
public class DefaultDebugOverlayFactory2 extends BaseDebugOverlayFactory2 {

private boolean mShowExtendedInformation;

public DefaultDebugOverlayFactory2(Supplier<Boolean> debugOverlayEnabled) {
this(true, debugOverlayEnabled);
}

public DefaultDebugOverlayFactory2(
boolean showExtendedInformation, Supplier<Boolean> debugOverlayEnabled) {
super(debugOverlayEnabled);
mShowExtendedInformation = showExtendedInformation;
}

public void setShowExtendedInformation(boolean showExtendedInformation) {
mShowExtendedInformation = showExtendedInformation;
}

public boolean getShowExtendedInformation() {
return mShowExtendedInformation;
}

@Override
Expand All @@ -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(
Expand All @@ -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";
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ interface CombinedImageListener : VitoImageRequestListener {

fun setImagePerfControllerListener(imagePerfControllerListener: ControllerListener2<ImageInfo>?)

fun getImagePerfControllerListener(): ControllerListener2<ImageInfo>?

fun setLocalImagePerfStateListener(imagePerfNotifier: ImagePerfNotifier?)

fun onReset()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ package com.facebook.fresco.vito.core

import android.graphics.drawable.Drawable
import com.facebook.drawee.drawable.VisibilityCallback
import com.facebook.fresco.ui.common.ControllerListener2
import com.facebook.fresco.vito.listener.ImageListener
import com.facebook.imagepipeline.image.ImageInfo

interface FrescoDrawableInterface {

Expand Down Expand Up @@ -42,4 +44,6 @@ interface FrescoDrawableInterface {
* @return the refetch runnable if set
*/
var refetchRunnable: Runnable?

fun getImagePerfControllerListener(): ControllerListener2<ImageInfo>?
}
1 change: 1 addition & 0 deletions vito/init/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dependencies {

implementation project(':fbcore')
implementation project(':imagepipeline')
implementation project(':ui-common')
implementation project(':vito:core')
implementation project(':vito:core-common-impl')
implementation project(':vito:core-java-impl')
Expand Down
Loading

0 comments on commit d5fcc31

Please sign in to comment.