From 368e0361ddde6c947d1ac52ab164a0bcc16cf514 Mon Sep 17 00:00:00 2001 From: Denis Koroskin Date: Mon, 4 Jan 2016 12:54:51 -0800 Subject: [PATCH] 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 --- .../react/flat/BitmapUpdateListener.java | 17 ++++++++++ .../react/flat/DrawImageWithPipeline.java | 33 +++++++++++++------ ...Helper.java => PipelineRequestHelper.java} | 19 ++++++----- 3 files changed, 50 insertions(+), 19 deletions(-) create mode 100644 ReactAndroid/src/main/java/com/facebook/react/flat/BitmapUpdateListener.java rename ReactAndroid/src/main/java/com/facebook/react/flat/{BitmapRequestHelper.java => PipelineRequestHelper.java} (91%) diff --git a/ReactAndroid/src/main/java/com/facebook/react/flat/BitmapUpdateListener.java b/ReactAndroid/src/main/java/com/facebook/react/flat/BitmapUpdateListener.java new file mode 100644 index 00000000000..115c20fa837 --- /dev/null +++ b/ReactAndroid/src/main/java/com/facebook/react/flat/BitmapUpdateListener.java @@ -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); +} diff --git a/ReactAndroid/src/main/java/com/facebook/react/flat/DrawImageWithPipeline.java b/ReactAndroid/src/main/java/com/facebook/react/flat/DrawImageWithPipeline.java index f8f36d9eda0..e4fba1b7715 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/flat/DrawImageWithPipeline.java +++ b/ReactAndroid/src/main/java/com/facebook/react/flat/DrawImageWithPipeline.java @@ -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(); diff --git a/ReactAndroid/src/main/java/com/facebook/react/flat/BitmapRequestHelper.java b/ReactAndroid/src/main/java/com/facebook/react/flat/PipelineRequestHelper.java similarity index 91% rename from ReactAndroid/src/main/java/com/facebook/react/flat/BitmapRequestHelper.java rename to ReactAndroid/src/main/java/com/facebook/react/flat/PipelineRequestHelper.java index 87711aac702..f30983bb288 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/flat/BitmapRequestHelper.java +++ b/ReactAndroid/src/main/java/com/facebook/react/flat/PipelineRequestHelper.java @@ -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> { private final ImageRequest mImageRequest; - private final DrawImageWithPipeline mDrawImage; + private @Nullable BitmapUpdateListener mBitmapUpdateListener; private @Nullable DataSource> mDataSource; private @Nullable CloseableReference 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(); }