Skip to content

Commit

Permalink
Added new FrescoVitoComponents class that FrescoVitoProvider delegate…
Browse files Browse the repository at this point in the history
…s to

Differential Revision: D54695897

fbshipit-source-id: 260aa5f076328dece2307436c3d0b143cb150f97
  • Loading branch information
oprisnik authored and facebook-github-bot committed Apr 24, 2024
1 parent 9208572 commit 2b854a6
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import com.facebook.fresco.vito.core.impl.ImagePipelineUtilsImpl.CircularBitmapR
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.components.FrescoVitoComponents
import com.facebook.fresco.vito.provider.impl.DefaultFrescoVitoProvider
import com.facebook.fresco.vito.provider.impl.NoOpCallerContextVerifier
import com.facebook.imagepipeline.core.ImagePipeline
Expand Down Expand Up @@ -90,11 +90,11 @@ class FrescoVito {
* @param providerImplementation the provider implementation to be used
*/
@Synchronized
fun initialize(providerImplementation: FrescoVitoProvider.Implementation) {
fun initialize(providerImplementation: FrescoVitoComponents.Implementation) {
if (isInitialized) {
return
}
FrescoVitoProvider.setImplementation(providerImplementation)
FrescoVitoComponents.setImplementation(providerImplementation)
isInitialized = true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,71 +7,29 @@

package com.facebook.fresco.vito.provider

import com.facebook.common.logging.FLog
import com.facebook.fresco.vito.core.FrescoController2
import com.facebook.fresco.vito.core.FrescoVitoConfig
import com.facebook.fresco.vito.core.FrescoVitoPrefetcher
import com.facebook.fresco.vito.core.VitoImagePipeline
import java.lang.RuntimeException
import com.facebook.fresco.vito.provider.components.FrescoVitoComponents

object FrescoVitoProvider {

private var _implementation: Implementation? = null

@JvmStatic
@Synchronized
fun getController(): FrescoController2 = getImplementation().getController()

@JvmStatic
@Synchronized
fun getPrefetcher(): FrescoVitoPrefetcher = getImplementation().getPrefetcher()
fun getController(): FrescoController2 = FrescoVitoComponents.getController()

@JvmStatic
@Synchronized
fun getImagePipeline(): VitoImagePipeline = getImplementation().getImagePipeline()

@JvmStatic @Synchronized fun getConfig(): FrescoVitoConfig = getImplementation().getConfig()
fun getPrefetcher(): FrescoVitoPrefetcher = FrescoVitoComponents.getPrefetcher()

/**
* Reset the implementation. This will remove any implementation currently set up and has to be
* used with caution.
*/
@JvmStatic
@Synchronized
fun resetImplementation() {
_implementation = null
}

@JvmStatic @Synchronized fun hasBeenInitialized(): Boolean = _implementation != null
fun getImagePipeline(): VitoImagePipeline = FrescoVitoComponents.getImagePipeline()

// We do not allow to re-initialize Vito directly.
// You can use #resetImplementation() if you must manually tear down Vito.
@JvmStatic
@Synchronized
fun getImplementation(): Implementation {
return _implementation ?: throw RuntimeException("Fresco context provider must be set")
}
@JvmStatic @Synchronized fun getConfig(): FrescoVitoConfig = FrescoVitoComponents.getConfig()

@JvmStatic
@Synchronized
fun setImplementation(implementation: Implementation) {
// We do not allow to re-initialize Vito directly.
// You can use #resetImplementation() if you must manually tear down Vito.
if (_implementation != null) {
FLog.e(
"FrescoVitoProvider",
"Fresco Vito already initialized! Vito must be initialized only once.")
}
_implementation = implementation
}

interface Implementation {
fun getController(): FrescoController2

fun getPrefetcher(): FrescoVitoPrefetcher

fun getImagePipeline(): VitoImagePipeline

fun getConfig(): FrescoVitoConfig
}
fun hasBeenInitialized(): Boolean = FrescoVitoComponents.hasBeenInitialized()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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.vito.provider.components

import com.facebook.common.logging.FLog
import com.facebook.fresco.vito.core.FrescoController2
import com.facebook.fresco.vito.core.FrescoVitoConfig
import com.facebook.fresco.vito.core.FrescoVitoPrefetcher
import com.facebook.fresco.vito.core.VitoImagePipeline
import java.lang.RuntimeException

object FrescoVitoComponents {

private var _implementation: Implementation? = null

@JvmStatic
@Synchronized
fun getController(): FrescoController2 = getImplementation().getController()

@JvmStatic
@Synchronized
fun getPrefetcher(): FrescoVitoPrefetcher = getImplementation().getPrefetcher()

@JvmStatic
@Synchronized
fun getImagePipeline(): VitoImagePipeline = getImplementation().getImagePipeline()

@JvmStatic @Synchronized fun getConfig(): FrescoVitoConfig = getImplementation().getConfig()

/**
* Reset the implementation. This will remove any implementation currently set up and has to be
* used with caution.
*/
@JvmStatic
@Synchronized
fun resetImplementation() {
_implementation = null
}

@JvmStatic @Synchronized fun hasBeenInitialized(): Boolean = _implementation != null

// We do not allow to re-initialize Vito directly.
// You can use #resetImplementation() if you must manually tear down Vito.
@JvmStatic
@Synchronized
fun getImplementation(): Implementation {
return _implementation ?: throw RuntimeException("Fresco context provider must be set")
}

@JvmStatic
@Synchronized
fun setImplementation(implementation: Implementation) {
// We do not allow to re-initialize Vito directly.
// You can use #resetImplementation() if you must manually tear down Vito.
if (_implementation != null) {
FLog.e(
"FrescoVitoProvider",
"Fresco Vito already initialized! Vito must be initialized only once.")
}
_implementation = implementation
}

interface Implementation {
fun getController(): FrescoController2

fun getPrefetcher(): FrescoVitoPrefetcher

fun getImagePipeline(): VitoImagePipeline

fun getConfig(): FrescoVitoConfig
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import com.facebook.fresco.vito.drawable.ArrayVitoDrawableFactory
import com.facebook.fresco.vito.drawable.BitmapDrawableFactory
import com.facebook.fresco.vito.draweesupport.DrawableFactoryWrapper
import com.facebook.fresco.vito.options.ImageOptionsDrawableFactory
import com.facebook.fresco.vito.provider.FrescoVitoProvider
import com.facebook.fresco.vito.provider.components.FrescoVitoComponents
import com.facebook.imagepipeline.core.ImagePipeline
import com.facebook.imagepipeline.core.ImagePipelineFactory
import com.facebook.imagepipeline.image.ImageInfo
Expand All @@ -43,7 +43,7 @@ class DefaultFrescoVitoProvider(
vitoImagePerfListener: VitoImagePerfListener,
debugOverlayFactory: DebugOverlayFactory2 = NoOpDebugOverlayFactory2(),
imagePerfListenerSupplier: Supplier<ControllerListener2<ImageInfo>>? = null,
) : FrescoVitoProvider.Implementation {
) : FrescoVitoComponents.Implementation {

private val frescoController: FrescoController2

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import com.facebook.fresco.vito.core.impl.KFrescoController
import com.facebook.fresco.vito.core.impl.VitoImagePipelineImpl
import com.facebook.fresco.vito.draweesupport.DrawableFactoryWrapper
import com.facebook.fresco.vito.options.ImageOptionsDrawableFactory
import com.facebook.fresco.vito.provider.FrescoVitoProvider
import com.facebook.fresco.vito.provider.components.FrescoVitoComponents
import com.facebook.fresco.vito.provider.impl.NoOpCallerContextVerifier
import com.facebook.imagepipeline.core.ImagePipeline
import com.facebook.imagepipeline.core.ImagePipelineFactory
Expand All @@ -33,7 +33,7 @@ class KFrescoVitoProvider(
private val lightweightBackgroundExecutor: Executor,
private val callerContextVerifier: CallerContextVerifier = NoOpCallerContextVerifier,
private val debugOverlayHandler: DebugOverlayHandler? = null
) : FrescoVitoProvider.Implementation {
) : FrescoVitoComponents.Implementation {

private val _imagePipeline: VitoImagePipeline by lazy {
VitoImagePipelineImpl(frescoImagePipeline, imagePipelineUtils)
Expand Down

0 comments on commit 2b854a6

Please sign in to comment.