From f450e849427c877fa75986ccd6b2008a63a6c58d Mon Sep 17 00:00:00 2001 From: Ahmed El-Helw Date: Mon, 15 Aug 2016 15:20:14 -0700 Subject: [PATCH] Implement progressive image loading for Nodes Summary: Support progressiveRenderingEnabled property for images on Nodes. Differential Revision: D3718834 --- .../main/java/com/facebook/react/flat/DrawImage.java | 2 ++ .../com/facebook/react/flat/DrawImageWithDrawee.java | 8 ++++++++ .../facebook/react/flat/DrawImageWithPipeline.java | 12 +++++++++--- .../java/com/facebook/react/flat/RCTImageView.java | 5 +++++ 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/flat/DrawImage.java b/ReactAndroid/src/main/java/com/facebook/react/flat/DrawImage.java index 6679ab1e4..ded71cfdb 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/flat/DrawImage.java +++ b/ReactAndroid/src/main/java/com/facebook/react/flat/DrawImage.java @@ -64,4 +64,6 @@ import com.facebook.react.bridge.ReadableArray; int getBorderColor(); void setFadeDuration(int fadeDuration); + + void setProgressiveRenderingEnabled(boolean enabled); } diff --git a/ReactAndroid/src/main/java/com/facebook/react/flat/DrawImageWithDrawee.java b/ReactAndroid/src/main/java/com/facebook/react/flat/DrawImageWithDrawee.java index c458e90fc..81fe946a4 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/flat/DrawImageWithDrawee.java +++ b/ReactAndroid/src/main/java/com/facebook/react/flat/DrawImageWithDrawee.java @@ -55,6 +55,7 @@ import com.facebook.react.views.imagehelper.MultiSourceHelper.MultiSourceResult; private float mBorderRadius; private int mBorderColor; private int mReactTag; + private boolean mProgressiveRenderingEnabled; private int mFadeDuration = ReactImageView.REMOTE_IMAGE_FADE_DURATION_MS; private @Nullable FlatViewGroup.InvalidateCallback mCallback; @@ -138,6 +139,11 @@ import com.facebook.react.views.imagehelper.MultiSourceHelper.MultiSourceResult; mFadeDuration = fadeDuration; } + @Override + public void setProgressiveRenderingEnabled(boolean enabled) { + mProgressiveRenderingEnabled = enabled; + } + @Override public void setReactTag(int reactTag) { mReactTag = reactTag; @@ -266,12 +272,14 @@ import com.facebook.react.views.imagehelper.MultiSourceHelper.MultiSourceResult; ImageRequest imageRequest = ImageRequestBuilder.newBuilderWithSource(source.getUri()) .setResizeOptions(resizeOptions) + .setProgressiveRenderingEnabled(mProgressiveRenderingEnabled) .build(); ImageRequest cachedImageRequest = null; if (cachedSource != null) { cachedImageRequest = ImageRequestBuilder.newBuilderWithSource(cachedSource.getUri()) .setResizeOptions(resizeOptions) + .setProgressiveRenderingEnabled(mProgressiveRenderingEnabled) .build(); } mRequestHelper = new 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 565774407..50fa00771 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/flat/DrawImageWithPipeline.java +++ b/ReactAndroid/src/main/java/com/facebook/react/flat/DrawImageWithPipeline.java @@ -48,7 +48,6 @@ import com.facebook.react.views.imagehelper.MultiSourceHelper.MultiSourceResult; private static final int BORDER_BITMAP_PATH_DIRTY = 1 << 1; private final List mSources = new LinkedList<>(); - private @Nullable Context mContext; private final Matrix mTransform = new Matrix(); private ScaleType mScaleType = ImageResizeMode.defaultValue(); private @Nullable PipelineRequestHelper mRequestHelper; @@ -62,6 +61,7 @@ import com.facebook.react.views.imagehelper.MultiSourceHelper.MultiSourceResult; // variables used for fading the image in private long mFirstDrawTime = -1; + private boolean mProgressiveRenderingEnabled; private int mFadeDuration = ReactImageView.REMOTE_IMAGE_FADE_DURATION_MS; @Override @@ -88,7 +88,6 @@ import com.facebook.react.views.imagehelper.MultiSourceHelper.MultiSourceResult; } } } - mContext = context; mBitmapShader = null; } @@ -121,6 +120,11 @@ import com.facebook.react.views.imagehelper.MultiSourceHelper.MultiSourceResult; mFadeDuration = fadeDuration; } + @Override + public void setProgressiveRenderingEnabled(boolean enabled) { + mProgressiveRenderingEnabled = enabled; + } + /** * If the bitmap isn't loaded by the first draw, fade it in, otherwise don't fade. */ @@ -241,7 +245,9 @@ import com.facebook.react.views.imagehelper.MultiSourceHelper.MultiSourceResult; mRequestHelper = null; return; } - ImageRequest imageRequest = ImageRequestBuilder.newBuilderWithSource(source.getUri()).build(); + ImageRequest imageRequest = ImageRequestBuilder.newBuilderWithSource(source.getUri()) + .setProgressiveRenderingEnabled(mProgressiveRenderingEnabled) + .build(); // DrawImageWithPipeline does now support displaying low res cache images before request mRequestHelper = new PipelineRequestHelper(Assertions.assertNotNull(imageRequest)); diff --git a/ReactAndroid/src/main/java/com/facebook/react/flat/RCTImageView.java b/ReactAndroid/src/main/java/com/facebook/react/flat/RCTImageView.java index c179d3e6a..08c17163d 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/flat/RCTImageView.java +++ b/ReactAndroid/src/main/java/com/facebook/react/flat/RCTImageView.java @@ -132,6 +132,11 @@ import com.facebook.react.views.image.ImageResizeMode; getMutableDrawImage().setFadeDuration(durationMs); } + @ReactProp(name = "progressiveRenderingEnabled") + public void setProgressiveRenderingEnabled(boolean enabled) { + getMutableDrawImage().setProgressiveRenderingEnabled(enabled); + } + private T getMutableDrawImage() { if (mDrawImage.isFrozen()) { mDrawImage = (T) mDrawImage.mutableCopy();