Skip to content

Commit

Permalink
Add a bitmap interface
Browse files Browse the repository at this point in the history
Reviewed By: oprisnik

Differential Revision: D43738353

fbshipit-source-id: 05aef668878381c868bcf10aa77cac198d2dc6e2
  • Loading branch information
Yu Si authored and facebook-github-bot committed Mar 7, 2023
1 parent 0a8b67c commit 1f91511
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package com.facebook.fresco.vito.core.impl

import android.content.res.Resources
import android.graphics.Bitmap
import android.graphics.Rect
import android.graphics.drawable.Animatable
import android.graphics.drawable.Drawable
Expand Down Expand Up @@ -36,9 +37,12 @@ import com.facebook.fresco.vito.renderer.AnimatedDrawableImageDataModel
import com.facebook.fresco.vito.renderer.BitmapImageDataModel
import com.facebook.fresco.vito.renderer.DrawableImageDataModel
import com.facebook.fresco.vito.renderer.ImageDataModel
import com.facebook.fresco.vito.source.BitmapImageSource
import com.facebook.imagepipeline.image.CloseableBitmap
import com.facebook.imagepipeline.image.CloseableImage
import com.facebook.imagepipeline.image.CloseableStaticBitmap
import com.facebook.imagepipeline.image.ImageInfo
import com.facebook.imagepipeline.image.ImmutableQualityInfo
import java.util.concurrent.Executor

class KFrescoController(
Expand Down Expand Up @@ -98,9 +102,11 @@ class KFrescoController(
ImageReleaseScheduler.cancelAllReleasing(drawable)
return true
}

if (drawable.isFetchSubmitted) {
drawable.imagePerfListener.onDrawableReconfigured(drawable)
}

// We didn't -> reset everything and set up new fetch
// TODO(T105148151): move to new package so that no legacy impl dep
val imageId: Long = VitoUtils.generateIdentifier()
Expand All @@ -120,11 +126,36 @@ class KFrescoController(
this.viewportDimensions = viewportDimensions
}

val options: ImageOptions = imageRequest.imageOptions
val resources: Resources = imageRequest.resources
// Direct bitmap available
if (imageRequest.imageSource is BitmapImageSource) {
val bitmap: Bitmap = (imageRequest.imageSource as BitmapImageSource).bitmap
val closeableBitmap: CloseableBitmap =
CloseableStaticBitmap.of(bitmap, {}, ImmutableQualityInfo.FULL_QUALITY, 0)
val bitmapRef = CloseableReference.of<CloseableImage>(closeableBitmap)
return try {
// Immediately display the actual image.
drawable.setFetchSubmitted(true)
drawable.actualImageLayer.setActualImage(
resources, options, closeableBitmap, imageToDataModelMapper)
drawable.invalidateSelf()
drawable.listenerManager.onFinalImageSet(
imageId,
imageRequest,
ImageOrigin.MEMORY_BITMAP_SHORTCUT,
closeableBitmap.imageInfo,
drawable.obtainExtras(null, bitmapRef),
drawable.actualImageDrawable)
debugOverlayHandler?.update(drawable)
true
} finally {
CloseableReference.closeSafely(bitmapRef)
}
}

drawable.listenerManager.onSubmit(imageId, imageRequest, callerContext, drawable.obtainExtras())
drawable.imagePerfListener.onImageFetch(drawable)

val options = imageRequest.imageOptions

drawable.overlayImageLayer.setOverlay(imageRequest.resources, options)

// Check if the image is in cache
Expand All @@ -137,7 +168,7 @@ class KFrescoController(
drawable.setFetchSubmitted(true)
drawable.closeable = cachedImage.clone()
drawable.actualImageLayer.setActualImage(
imageRequest.resources, options, image, imageToDataModelMapper)
resources, options, image, imageToDataModelMapper)
// TODO(T105148151): trigger listeners
drawable.invalidateSelf()
val imageInfo = image.imageInfo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package com.facebook.fresco.vito.core.impl;

import android.graphics.Bitmap;
import android.graphics.Rect;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.Drawable;
Expand Down Expand Up @@ -37,9 +38,13 @@
import com.facebook.fresco.vito.core.VitoImageRequestListener;
import com.facebook.fresco.vito.core.impl.debug.DebugOverlayFactory2;
import com.facebook.fresco.vito.listener.ImageListener;
import com.facebook.fresco.vito.source.BitmapImageSource;
import com.facebook.fresco.vito.source.EmptyImageSource;
import com.facebook.imagepipeline.image.CloseableBitmap;
import com.facebook.imagepipeline.image.CloseableImage;
import com.facebook.imagepipeline.image.CloseableStaticBitmap;
import com.facebook.imagepipeline.image.ImageInfo;
import com.facebook.imagepipeline.image.ImmutableQualityInfo;
import com.facebook.infer.annotation.Nullsafe;
import java.util.Map;
import java.util.concurrent.Executor;
Expand Down Expand Up @@ -127,6 +132,27 @@ public boolean fetch(
frescoDrawable.cancelReleaseDelayed();
return true; // already set
}

// Direct bitmap available
if (imageRequest.imageSource instanceof BitmapImageSource) {
Bitmap bitmap = ((BitmapImageSource) imageRequest.imageSource).getBitmap();
CloseableBitmap closeableBitmap =
CloseableStaticBitmap.of(
bitmap, noOpReleaser -> {}, ImmutableQualityInfo.FULL_QUALITY, 0);

CloseableReference<CloseableImage> bitmapRef = CloseableReference.of(closeableBitmap);
try {
frescoDrawable.setImageOrigin(ImageOrigin.MEMORY_BITMAP);
// Immediately display the actual image.
setActualImage(frescoDrawable, imageRequest, bitmapRef, true, null);
frescoDrawable.setFetchSubmitted(true);
mDebugOverlayFactory.update(frescoDrawable, obtainExtras(null, null, frescoDrawable));
return true;
} finally {
CloseableReference.closeSafely(bitmapRef);
}
}

if (frescoDrawable.isFetchSubmitted()) {
frescoDrawable.getImagePerfListener().onDrawableReconfigured(frescoDrawable);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* 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.source

import android.graphics.Bitmap

data class BitmapImageSource(val bitmap: Bitmap) : ImageSource
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package com.facebook.fresco.vito.source

import android.graphics.Bitmap
import android.net.Uri

/**
Expand Down Expand Up @@ -89,4 +90,12 @@ object ImageSourceProvider {
if (lowResImageUri == null) {
forUri(highResImageUri)
} else IncreasingQualityImageSource(forUri(lowResImageUri), forUri(highResImageUri))

/**
* Create a bitmap image source for a given bitmap image buffer.
*
* @param bitmap the bitmap image buffer to be used
* @return the ImageSource be be passed to the UI component
*/
@JvmStatic fun bitmap(bitmap: Bitmap): ImageSource = BitmapImageSource(bitmap)
}

0 comments on commit 1f91511

Please sign in to comment.