Rename BitmapRequestHelper to PipelineRequestHelper
Summary: Minor cleanup; renames BitmapRequestHelper to PipelineRequestHelper and adds BitmapUpdateListener interface. I plan reusing these classes in an upcoming RCTTextInlineImage implementation. Reviewed By: ahmedre Differential Revision: D2792535
This commit is contained in:
parent
fd3e213593
commit
368e0361dd
|
@ -0,0 +1,17 @@
|
|||
/**
|
||||
* Copyright (c) 2015-present, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
package com.facebook.react.flat;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
|
||||
/* package */ interface BitmapUpdateListener {
|
||||
public void onSecondaryAttach(Bitmap bitmap);
|
||||
public void onBitmapReady(Bitmap bitmap);
|
||||
}
|
|
@ -28,16 +28,17 @@ import com.facebook.react.views.image.ImageResizeMode;
|
|||
|
||||
/**
|
||||
* DrawImageWithPipeline is DrawCommand that can draw a local or remote image.
|
||||
* It uses BitmapRequestHelper internally to fetch and cache the images.
|
||||
* It uses PipelineRequestHelper internally to fetch and cache the images.
|
||||
*/
|
||||
/* package */ final class DrawImageWithPipeline extends AbstractDrawBorder implements DrawImage {
|
||||
/* package */ final class DrawImageWithPipeline extends AbstractDrawBorder
|
||||
implements DrawImage, BitmapUpdateListener {
|
||||
|
||||
private static final Paint PAINT = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
|
||||
private static final int BORDER_BITMAP_PATH_DIRTY = 1 << 1;
|
||||
|
||||
private final Matrix mTransform = new Matrix();
|
||||
private ScaleType mScaleType = ImageResizeMode.defaultValue();
|
||||
private @Nullable BitmapRequestHelper mBitmapRequestHelper;
|
||||
private @Nullable PipelineRequestHelper mRequestHelper;
|
||||
private @Nullable PorterDuffColorFilter mColorFilter;
|
||||
private @Nullable FlatViewGroup.InvalidateCallback mCallback;
|
||||
private @Nullable Path mPathForRoundedBitmap;
|
||||
|
@ -46,7 +47,7 @@ import com.facebook.react.views.image.ImageResizeMode;
|
|||
|
||||
@Override
|
||||
public boolean hasImageRequest() {
|
||||
return mBitmapRequestHelper != null;
|
||||
return mRequestHelper != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -54,9 +55,9 @@ import com.facebook.react.views.image.ImageResizeMode;
|
|||
mBitmapShader = null;
|
||||
|
||||
if (imageRequest == null) {
|
||||
mBitmapRequestHelper = null;
|
||||
mRequestHelper = null;
|
||||
} else {
|
||||
mBitmapRequestHelper = new BitmapRequestHelper(imageRequest, this);
|
||||
mRequestHelper = new PipelineRequestHelper(imageRequest);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -81,7 +82,7 @@ import com.facebook.react.views.image.ImageResizeMode;
|
|||
|
||||
@Override
|
||||
protected void onDraw(Canvas canvas) {
|
||||
Bitmap bitmap = Assertions.assumeNotNull(mBitmapRequestHelper).getBitmap();
|
||||
Bitmap bitmap = Assertions.assumeNotNull(mRequestHelper).getBitmap();
|
||||
if (bitmap == null) {
|
||||
return;
|
||||
}
|
||||
|
@ -116,16 +117,18 @@ import com.facebook.react.views.image.ImageResizeMode;
|
|||
@Override
|
||||
public void onAttached(FlatViewGroup.InvalidateCallback callback) {
|
||||
mCallback = callback;
|
||||
Assertions.assumeNotNull(mBitmapRequestHelper).attach();
|
||||
Assertions.assumeNotNull(mRequestHelper).attach(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDetached() {
|
||||
Assertions.assumeNotNull(mBitmapRequestHelper).detach();
|
||||
Assertions.assumeNotNull(mRequestHelper).detach();
|
||||
|
||||
if (mBitmapRequestHelper.isDetached()) {
|
||||
if (mRequestHelper.isDetached()) {
|
||||
// Make sure we don't hold on to the Bitmap.
|
||||
mBitmapShader = null;
|
||||
// this is optional
|
||||
mCallback = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -135,6 +138,16 @@ import com.facebook.react.views.image.ImageResizeMode;
|
|||
setFlag(BORDER_BITMAP_PATH_DIRTY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSecondaryAttach(Bitmap bitmap) {
|
||||
updateBounds(bitmap);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBitmapReady(Bitmap bitmap) {
|
||||
updateBounds(bitmap);
|
||||
}
|
||||
|
||||
/* package */ void updateBounds(Bitmap bitmap) {
|
||||
Assertions.assumeNotNull(mCallback).invalidate();
|
||||
|
||||
|
|
|
@ -33,27 +33,28 @@ import com.facebook.infer.annotation.Assertions;
|
|||
* 3) mDataSource == null, mImageRef != null : request successfully finished.
|
||||
* 4) mDataSource != null, mImageRef != null : invalid state (should never happen)
|
||||
*/
|
||||
/* package */ final class BitmapRequestHelper
|
||||
/* package */ final class PipelineRequestHelper
|
||||
implements DataSubscriber<CloseableReference<CloseableImage>> {
|
||||
|
||||
private final ImageRequest mImageRequest;
|
||||
private final DrawImageWithPipeline mDrawImage;
|
||||
private @Nullable BitmapUpdateListener mBitmapUpdateListener;
|
||||
private @Nullable DataSource<CloseableReference<CloseableImage>> mDataSource;
|
||||
private @Nullable CloseableReference<CloseableImage> mImageRef;
|
||||
private int mAttachCounter;
|
||||
|
||||
/* package */ BitmapRequestHelper(ImageRequest imageRequest, DrawImageWithPipeline drawImage) {
|
||||
/* package */ PipelineRequestHelper(ImageRequest imageRequest) {
|
||||
mImageRequest = imageRequest;
|
||||
mDrawImage = drawImage;
|
||||
}
|
||||
|
||||
/* package */ void attach() {
|
||||
/* package */ void attach(BitmapUpdateListener listener) {
|
||||
mBitmapUpdateListener = listener;
|
||||
|
||||
mAttachCounter++;
|
||||
if (mAttachCounter != 1) {
|
||||
// this is a secondary attach, ignore it, only updating Bitmap boundaries if needed.
|
||||
Bitmap bitmap = getBitmap();
|
||||
if (bitmap != null) {
|
||||
mDrawImage.updateBounds(bitmap);
|
||||
mBitmapUpdateListener.onSecondaryAttach(bitmap);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
@ -86,6 +87,8 @@ import com.facebook.infer.annotation.Assertions;
|
|||
mImageRef.close();
|
||||
mImageRef = null;
|
||||
}
|
||||
|
||||
mBitmapUpdateListener = null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -147,9 +150,7 @@ import com.facebook.infer.annotation.Assertions;
|
|||
return;
|
||||
}
|
||||
|
||||
// now that we have the Bitmap, DrawImage can finally initialize its
|
||||
// tranformation matrix to satisfy requested ScaleType.
|
||||
mDrawImage.updateBounds(bitmap);
|
||||
Assertions.assumeNotNull(mBitmapUpdateListener).onBitmapReady(bitmap);
|
||||
} finally {
|
||||
dataSource.close();
|
||||
}
|
Loading…
Reference in New Issue