Skip to content

Commit

Permalink
Convert ImagePipeline to Kotlin (initial)
Browse files Browse the repository at this point in the history
Reviewed By: oprisnik

Differential Revision: D53944214

fbshipit-source-id: 1711ad8ca54fdba6bbc22be7fa1a9522bd7ad1c0
  • Loading branch information
Artem Kholodnyi authored and facebook-github-bot committed Feb 23, 2024
1 parent ba72ed9 commit 32b53b7
Show file tree
Hide file tree
Showing 9 changed files with 1,130 additions and 1,208 deletions.
1,174 changes: 0 additions & 1,174 deletions imagepipeline/src/main/java/com/facebook/imagepipeline/core/ImagePipeline.java

This file was deleted.

1,083 changes: 1,083 additions & 0 deletions imagepipeline/src/main/java/com/facebook/imagepipeline/core/ImagePipeline.kt

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import com.facebook.imagepipeline.image.CloseableImage;
import com.facebook.imagepipeline.image.CloseableStaticBitmap;
import com.facebook.infer.annotation.Nullsafe;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
Expand Down Expand Up @@ -49,7 +48,7 @@ public abstract class BaseBitmapReferenceDataSubscriber
extends BaseDataSubscriber<CloseableReference<CloseableImage>> {

@Override
public void onNewResultImpl(@Nonnull DataSource<CloseableReference<CloseableImage>> dataSource) {
public void onNewResultImpl(DataSource<CloseableReference<CloseableImage>> dataSource) {
if (!dataSource.isFinished()) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ package com.facebook.imagepipeline.listener
import com.facebook.common.logging.FLog
import com.facebook.imagepipeline.producers.ProducerContext
import java.util.ArrayList
import java.util.Set

class ForwardingRequestListener2 : RequestListener2 {

private var requestListeners: MutableList<RequestListener2>
private val requestListeners: MutableList<RequestListener2>

constructor(listenersToAdd: Set<RequestListener2?>) {
constructor(listenersToAdd: Set<RequestListener2?>?) {
if (listenersToAdd == null) {
requestListeners = mutableListOf()
return
}
requestListeners = ArrayList(listenersToAdd.size)
listenersToAdd.filterNotNullTo(requestListeners)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.anyObject;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
Expand All @@ -22,6 +21,7 @@

import android.net.Uri;
import com.facebook.cache.common.CacheKey;
import com.facebook.cache.common.DebuggingCacheKey;
import com.facebook.cache.common.MultiCacheKey;
import com.facebook.cache.common.SimpleCacheKey;
import com.facebook.common.internal.Predicate;
Expand Down Expand Up @@ -56,11 +56,13 @@
/** Tests for ImagePipeline */
@RunWith(RobolectricTestRunner.class)
public class ImagePipelineTest {

@Mock public ImageRequest mImageRequest;
@Mock public ProducerSequenceFactory mProducerSequenceFactory;
@Mock public CacheKeyFactory mCacheKeyFactory;
@Mock public Object mCallerContext;
@Mock public ImagePipelineConfigInterface mConfig;
@Mock public ImagePipelineExperiments mImagePipelineExperiments;

private Supplier<Boolean> mPrefetchEnabledSupplier;
private Supplier<Boolean> mSuppressBitmapPrefetchingSupplier;
Expand Down Expand Up @@ -112,6 +114,9 @@ public void setUp() throws Exception {
when(mImageRequest.getLowestPermittedRequestLevel())
.thenReturn(ImageRequest.RequestLevel.FULL_FETCH);
when(mImageRequest.shouldDecodePrefetches()).thenReturn(null);

when(mConfig.getExperiments()).thenReturn(mImagePipelineExperiments);
when(mImagePipelineExperiments.getPrefetchShortcutEnabled()).thenReturn(false);
}

@Test
Expand Down Expand Up @@ -499,14 +504,20 @@ public void testClearMemoryCaches() {
@Test
public void testIsInDiskCacheFromMainDiskCache() {
when(mImageRequest.getCacheChoice()).thenReturn(ImageRequest.CacheChoice.DEFAULT);
when(mMainDiskStorageCache.diskCheckSync(isNull(CacheKey.class))).thenReturn(true);
CacheKey cacheKey = mock(DebuggingCacheKey.class);
when(mMainDiskStorageCache.diskCheckSync(cacheKey)).thenReturn(true);
when(mCacheKeyFactory.getEncodedCacheKey(any(ImageRequest.class), anyObject()))
.thenReturn(cacheKey);
assertTrue(mImagePipeline.isInDiskCacheSync(mImageRequest));
}

@Test
public void testIsInDiskCacheFromSmallDiskCache() {
when(mImageRequest.getCacheChoice()).thenReturn(ImageRequest.CacheChoice.SMALL);
when(mSmallImageDiskStorageCache.diskCheckSync(isNull(CacheKey.class))).thenReturn(true);
CacheKey cacheKey = mock(DebuggingCacheKey.class);
when(mSmallImageDiskStorageCache.diskCheckSync(cacheKey)).thenReturn(true);
when(mCacheKeyFactory.getEncodedCacheKey(any(ImageRequest.class), anyObject()))
.thenReturn(cacheKey);
assertTrue(mImagePipeline.isInDiskCacheSync(mImageRequest));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class FrescoVitoPrefetcherImpl(
imageOptions: ImageOptions?,
callerContext: Any?,
callsite: String
): DataSource<Void> {
): DataSource<Void?> {
return when (prefetchTarget) {
PrefetchTarget.MEMORY_DECODED ->
prefetchToBitmapCache(uri, imageOptions, callerContext, callsite)
Expand All @@ -54,7 +54,7 @@ class FrescoVitoPrefetcherImpl(
imageOptions: DecodedImageOptions?,
callerContext: Any?,
callsite: String
): DataSource<Void> {
): DataSource<Void?> {
val imageRequest = imagePipelineUtils.buildImageRequest(uri, imageOptions ?: defaults())
return prefetch(PrefetchTarget.MEMORY_DECODED, imageRequest, callerContext, null)
}
Expand All @@ -64,7 +64,7 @@ class FrescoVitoPrefetcherImpl(
imageOptions: EncodedImageOptions?,
callerContext: Any?,
callsite: String
): DataSource<Void> {
): DataSource<Void?> {
val imageRequest = imagePipelineUtils.buildEncodedImageRequest(uri, imageOptions ?: defaults())
return prefetch(PrefetchTarget.MEMORY_ENCODED, imageRequest, callerContext, null)
}
Expand All @@ -74,7 +74,7 @@ class FrescoVitoPrefetcherImpl(
imageOptions: ImageOptions?,
callerContext: Any?,
callsite: String
): DataSource<Void> {
): DataSource<Void?> {
val imageRequest = imagePipelineUtils.buildEncodedImageRequest(uri, imageOptions ?: defaults())
return prefetch(PrefetchTarget.DISK, imageRequest, callerContext, null)
}
Expand All @@ -85,15 +85,15 @@ class FrescoVitoPrefetcherImpl(
callerContext: Any?,
requestListener: RequestListener?,
callsite: String
): DataSource<Void> =
): DataSource<Void?> =
prefetch(prefetchTarget, imageRequest.finalImageRequest, callerContext, requestListener)

private fun prefetch(
prefetchTarget: PrefetchTarget,
imageRequest: ImageRequest?,
callerContext: Any?,
requestListener: RequestListener?
): DataSource<Void> {
): DataSource<Void?> {
callerContextVerifier?.verifyCallerContext(callerContext, false)
return if (imageRequest == null) {
DataSources.immediateFailedDataSource(NULL_IMAGE_MESSAGE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,36 @@ class NoOpFrescoVitoPrefetcher : FrescoVitoPrefetcher {
imageOptions: ImageOptions?,
callerContext: Any?,
callsite: String
): DataSource<Void> = throwUnsupportedOperationException()
): DataSource<Void?> = throwUnsupportedOperationException()

override fun prefetchToBitmapCache(
uri: Uri,
imageOptions: DecodedImageOptions?,
callerContext: Any?,
callsite: String
): DataSource<Void> = throwUnsupportedOperationException()
): DataSource<Void?> = throwUnsupportedOperationException()

override fun prefetchToEncodedCache(
uri: Uri,
imageOptions: EncodedImageOptions?,
callerContext: Any?,
callsite: String
): DataSource<Void> = throwUnsupportedOperationException()
): DataSource<Void?> = throwUnsupportedOperationException()

override fun prefetchToDiskCache(
uri: Uri,
imageOptions: ImageOptions?,
callerContext: Any?,
callsite: String
): DataSource<Void> = throwUnsupportedOperationException()
): DataSource<Void?> = throwUnsupportedOperationException()

override fun prefetch(
prefetchTarget: PrefetchTarget,
imageRequest: VitoImageRequest,
callerContext: Any?,
requestListener: RequestListener?,
callsite: String
): DataSource<Void> = throwUnsupportedOperationException()
): DataSource<Void?> = throwUnsupportedOperationException()

override fun setDistanceToViewport(
distance: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ interface FrescoVitoPrefetcher {
imageOptions: ImageOptions?,
callerContext: Any?,
callsite: String
): DataSource<Void>
): DataSource<Void?>

/**
* Prefetch an image to the bitmap memory cache (for decoded images). In order to cancel the
Expand All @@ -55,7 +55,7 @@ interface FrescoVitoPrefetcher {
imageOptions: DecodedImageOptions?,
callerContext: Any?,
callsite: String
): DataSource<Void>
): DataSource<Void?>

/**
* Prefetch an image to the encoded memory cache. In order to cancel the prefetch, close the
Expand All @@ -75,7 +75,7 @@ interface FrescoVitoPrefetcher {
imageOptions: EncodedImageOptions?,
callerContext: Any?,
callsite: String
): DataSource<Void>
): DataSource<Void?>

/**
* Prefetch an image to the disk cache. In order to cancel the prefetch, close the [DataSource]
Expand All @@ -95,7 +95,7 @@ interface FrescoVitoPrefetcher {
imageOptions: ImageOptions?,
callerContext: Any?,
callsite: String
): DataSource<Void>
): DataSource<Void?>

/**
* Prefetch an image to the given [PrefetchTarget] using a [VitoImageRequest]. In order to cancel
Expand All @@ -116,7 +116,7 @@ interface FrescoVitoPrefetcher {
callerContext: Any?,
requestListener: RequestListener?,
callsite: String
): DataSource<Void>
): DataSource<Void?>

/**
* Sets the image's relative distance to the viewport for the purpose of prioritization.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ object FrescoVitoImage2Spec {
@OnCreateInitialState
fun onCreateInitialState(
context: ComponentContext,
workingRangePrefetchData: StateValue<AtomicReference<DataSource<Void>>>,
workingRangePrefetchData: StateValue<AtomicReference<DataSource<Void?>>>,
) {
if (FrescoVitoProvider.hasBeenInitialized() &&
FrescoVitoProvider.getConfig().prefetchConfig.prefetchWithWorkingRange()) {
Expand Down Expand Up @@ -146,7 +146,7 @@ object FrescoVitoImage2Spec {
@Prop(optional = true) prefetch: Prefetch?,
@Prop(optional = true) prefetchRequestListener: RequestListener?,
@CachedValue requestCachedValue: VitoImageRequest?,
prefetchDataSource: Output<DataSource<Void>>,
prefetchDataSource: Output<DataSource<Void?>>,
) {
if (requestCachedValue == null) {
return
Expand Down Expand Up @@ -177,7 +177,7 @@ object FrescoVitoImage2Spec {
@FromBoundsDefined requestFromBoundsDefined: VitoImageRequest?,
@FromPrepare prefetchDataSource: DataSource<Void?>?,
@FromBoundsDefined viewportDimensions: Rect,
@State workingRangePrefetchData: AtomicReference<DataSource<Void>>?,
@State workingRangePrefetchData: AtomicReference<DataSource<Void?>>?,
@TreeProp contextChain: ContextChain?,
) {
val request = requestCachedValue ?: requestFromBoundsDefined
Expand Down Expand Up @@ -224,7 +224,7 @@ object FrescoVitoImage2Spec {
@FromBoundsDefined requestFromBoundsDefined: VitoImageRequest?,
@FromPrepare prefetchDataSource: DataSource<Void?>?,
@FromBoundsDefined viewportDimensions: Rect,
@State workingRangePrefetchData: AtomicReference<DataSource<Void>>?,
@State workingRangePrefetchData: AtomicReference<DataSource<Void?>>?,
) {
val request = requestCachedValue ?: requestFromBoundsDefined
// We fetch in both mount and bind in case an unbind event triggered a delayed release.
Expand All @@ -250,7 +250,7 @@ object FrescoVitoImage2Spec {
fun onUnbind(
c: ComponentContext,
frescoDrawable: FrescoDrawableInterface,
@FromPrepare prefetchDataSource: DataSource<Void>?,
@FromPrepare prefetchDataSource: DataSource<Void?>?,
) {
frescoDrawable.imagePerfListener.onImageUnbind(frescoDrawable)
if (FrescoVitoProvider.getConfig().useBindOnly()) {
Expand All @@ -266,7 +266,7 @@ object FrescoVitoImage2Spec {
fun onUnmount(
c: ComponentContext,
frescoDrawable: FrescoDrawableInterface,
@FromPrepare prefetchDataSource: DataSource<Void>?,
@FromPrepare prefetchDataSource: DataSource<Void?>?,
) {
frescoDrawable.imagePerfListener.onImageUnmount(frescoDrawable)
if (FrescoVitoProvider.getConfig().useBindOnly()) {
Expand Down Expand Up @@ -338,8 +338,8 @@ object FrescoVitoImage2Spec {
@Prop(optional = true) prefetch: Prefetch?,
@Prop(optional = true) callerContext: Any?,
@CachedValue requestCachedValue: VitoImageRequest?,
@FromPrepare prefetchDataSource: DataSource<Void>?,
@State workingRangePrefetchData: AtomicReference<DataSource<Void>>?,
@FromPrepare prefetchDataSource: DataSource<Void?>?,
@State workingRangePrefetchData: AtomicReference<DataSource<Void?>>?,
) {
if (requestCachedValue == null || workingRangePrefetchData == null) {
return
Expand All @@ -366,7 +366,7 @@ object FrescoVitoImage2Spec {
@OnExitedRange(name = "imagePrefetch")
fun onExitedWorkingRange(
c: ComponentContext,
@State workingRangePrefetchData: AtomicReference<DataSource<Void>>?,
@State workingRangePrefetchData: AtomicReference<DataSource<Void?>>?,
) {
cancelWorkingRangePrefetch(workingRangePrefetchData)
}
Expand Down Expand Up @@ -479,7 +479,7 @@ object FrescoVitoImage2Spec {
}

@JvmStatic
fun cancelWorkingRangePrefetch(prefetchData: AtomicReference<DataSource<Void>>?) {
fun cancelWorkingRangePrefetch(prefetchData: AtomicReference<DataSource<Void?>>?) {
if (prefetchData == null) {
return
}
Expand Down

0 comments on commit 32b53b7

Please sign in to comment.