Skip to content

Commit

Permalink
Move determineStrategy from FrescoVitoImage2Spec to VitoImagePipeline…
Browse files Browse the repository at this point in the history
…Impl

Differential Revision: D69949526

fbshipit-source-id: c61ea011569a863d45177250b96d4d23213ee578
  • Loading branch information
Artem Kholodnyi authored and facebook-github-bot committed Feb 21, 2025
1 parent a2c094f commit 9fb94ef
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 77 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
/*
* 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.fresco.urimod

Expand All @@ -16,6 +21,7 @@ enum class SmartFetchStrategy : FetchStrategy {
}

enum class ClassicFetchStrategy : FetchStrategy {
DEFAULT,
APP_DISABLED,
PRODUCT_DISABLED,
APP_STARTING,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package com.facebook.fresco.vito.core.impl

import android.content.res.Resources
import android.graphics.Rect
import android.os.Looper
import com.facebook.common.callercontext.ContextChain
import com.facebook.common.references.CloseableReference
import com.facebook.datasource.DataSource
Expand Down Expand Up @@ -204,4 +205,73 @@ class VitoImagePipelineImpl(
else -> "Other"
}
}

override fun determineFetchStrategy(
requestBeforeLayout: VitoImageRequest?,
callerContext: Any?,
contextChain: ContextChain?
): FetchStrategy {
if (requestBeforeLayout == null) {
if (experimentalDynamicSizeVito2()) {
return SmartFetchStrategy.DEFAULT
} else {
return NoPrefetchInOnPrepareStrategy
}
}

if (!experimentalDynamicSizeVito2()) {
return ClassicFetchStrategy.APP_DISABLED
}

if (experimentalDynamicSizeWithCacheFallbackVito2() &&
Looper.myLooper() == Looper.getMainLooper()) {
// We don't want to check cache if we are running on the main thread
// By default uses the original URL
val shouldSmartFetchOnMainThread = config.experimentalDynamicSizeOnPrepareMainThreadVito2()
if (shouldSmartFetchOnMainThread) {
return SmartFetchStrategy.MAIN_THREAD
} else {
return ClassicFetchStrategy.MAIN_THREAD
}
}

if (isProductEnabled(callerContext, contextChain) == false) {
return ClassicFetchStrategy.PRODUCT_DISABLED
}

if (config.experimentalDynamicSizeDisableWhenAppIsStarting() && config.isAppStarting()) {
return ClassicFetchStrategy.APP_STARTING
}

if (experimentalDynamicSizeWithCacheFallbackVito2()) {
val isInDiskCache =
isInDiskCacheSync(
requestBeforeLayout,
config.experimentalDynamicSizeDiskCacheCheckTimeoutMs(),
TimeUnit.MILLISECONDS)

if (isInDiskCache == null) {
// Disk cache request timed out
if (config.experimentalDynamicSizeUseSfOnDiskCacheTimeout()) {
return SmartFetchStrategy.DISK_CACHE_TIMEOUT
} else {
return ClassicFetchStrategy.DISK_CACHE_TIMEOUT
}
}

if (isInDiskCache) {
// Disk MBP fallback
return ClassicFetchStrategy.DISK_CACHE_HIT
}
}

return SmartFetchStrategy.DEFAULT
}

private fun isProductEnabled(callerContext: Any?, contextChain: ContextChain?): Boolean? {
if (!config.experimentalDynamicSizeCheckIfProductIsEnabled()) {
return null
}
return config.experimentalDynamicSizeIsProductEnabled(callerContext, contextChain)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import android.graphics.Rect
import com.facebook.common.callercontext.ContextChain
import com.facebook.common.references.CloseableReference
import com.facebook.datasource.DataSource
import com.facebook.fresco.urimod.ClassicFetchStrategy
import com.facebook.fresco.urimod.FetchStrategy
import com.facebook.fresco.vito.options.ImageOptions
import com.facebook.fresco.vito.source.ImageSource
Expand Down Expand Up @@ -67,4 +68,10 @@ interface VitoImagePipeline {
}

fun hintUnmodifiedUri(imageRequest: VitoImageRequest) = Unit

fun determineFetchStrategy(
requestBeforeLayout: VitoImageRequest?,
callerContext: Any?,
contextChain: ContextChain?
): FetchStrategy = ClassicFetchStrategy.DEFAULT
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import android.content.pm.ActivityInfo
import android.graphics.Rect
import android.net.Uri
import android.os.Build
import android.os.Looper
import android.view.View
import androidx.core.util.ObjectsCompat
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat
Expand Down Expand Up @@ -62,7 +61,6 @@ import com.facebook.litho.annotations.ShouldExcludeFromIncrementalMount
import com.facebook.litho.annotations.ShouldUpdate
import com.facebook.litho.annotations.TreeProp
import com.facebook.litho.utils.MeasureUtils
import java.util.concurrent.TimeUnit

/** Fresco Vito component for Litho */
@MountSpec(isPureRender = true, canPreallocate = true, poolSize = 15)
Expand Down Expand Up @@ -165,7 +163,9 @@ object FrescoVitoImage2Spec {
prefetchDataSource: Output<DataSource<Void?>>,
fetchStrategy: Output<FetchStrategy>,
) {
val result = determineFetchStrategy(requestBeforeLayout, callerContext, contextChain)
val result =
FrescoVitoProvider.getImagePipeline()
.determineFetchStrategy(requestBeforeLayout, callerContext, contextChain)
when (result) {
is ClassicFetchStrategy -> {
checkNotNull(requestBeforeLayout)
Expand All @@ -184,14 +184,6 @@ object FrescoVitoImage2Spec {
fetchStrategy.set(result)
}

private fun isProductEnabled(callerContext: Any?, contextChain: ContextChain?): Boolean? {
val config = FrescoVitoProvider.getConfig()
if (!config.experimentalDynamicSizeCheckIfProductIsEnabled()) {
return null
}
return config.experimentalDynamicSizeIsProductEnabled(callerContext, contextChain)
}

@JvmStatic
@OnMount
fun onMount(
Expand Down Expand Up @@ -517,69 +509,4 @@ object FrescoVitoImage2Spec {
}
}
}

/** Determine whether to SmartFetch for a given request */
private fun determineFetchStrategy(
requestBeforeLayout: VitoImageRequest?,
callerContext: Any?,
contextChain: ContextChain?
): FetchStrategy {
if (requestBeforeLayout == null) {
if (experimentalDynamicSizeVito2()) {
return SmartFetchStrategy.DEFAULT
} else {
return NoPrefetchInOnPrepareStrategy
}
}

if (!experimentalDynamicSizeVito2()) {
return ClassicFetchStrategy.APP_DISABLED
}

if (experimentalDynamicSizeWithCacheFallbackVito2() &&
Looper.myLooper() == Looper.getMainLooper()) {
// We don't want to check cache if we are running on the main thread
// By default uses the original URL
val shouldSmartFetchOnMainThread = experimentalDynamicSizeOnPrepareMainThreadVito2()
if (shouldSmartFetchOnMainThread) {
return SmartFetchStrategy.MAIN_THREAD
} else {
return ClassicFetchStrategy.MAIN_THREAD
}
}

if (isProductEnabled(callerContext, contextChain) == false) {
return ClassicFetchStrategy.PRODUCT_DISABLED
}

val config = FrescoVitoProvider.getConfig()
if (config.experimentalDynamicSizeDisableWhenAppIsStarting() && config.isAppStarting()) {
return ClassicFetchStrategy.APP_STARTING
}

if (experimentalDynamicSizeWithCacheFallbackVito2()) {
val isInDiskCache =
FrescoVitoProvider.getImagePipeline()
.isInDiskCacheSync(
requestBeforeLayout,
FrescoVitoProvider.getConfig().experimentalDynamicSizeDiskCacheCheckTimeoutMs(),
TimeUnit.MILLISECONDS)

if (isInDiskCache == null) {
// Disk cache request timed out
if (config.experimentalDynamicSizeUseSfOnDiskCacheTimeout()) {
return SmartFetchStrategy.DISK_CACHE_TIMEOUT
} else {
return ClassicFetchStrategy.DISK_CACHE_TIMEOUT
}
}

if (isInDiskCache) {
// Disk MBP fallback
return ClassicFetchStrategy.DISK_CACHE_HIT
}
}

return SmartFetchStrategy.DEFAULT
}
}

0 comments on commit 9fb94ef

Please sign in to comment.