Fix the observer effect in Images Flipper plugin

Reviewed By: oprisnik, wizh

Differential Revision: D31379424

fbshipit-source-id: 96040dd0d37442d80a660814bd356571d075aa2c
This commit is contained in:
Artem Kholodnyi
2021-10-12 09:14:35 -07:00
committed by Facebook GitHub Bot
parent 1fae3a24c9
commit 11e7bbf9cf
2 changed files with 29 additions and 31 deletions

View File

@@ -16,6 +16,7 @@ import com.facebook.cache.common.CacheKey;
import com.facebook.cache.common.SimpleCacheKey; import com.facebook.cache.common.SimpleCacheKey;
import com.facebook.cache.disk.DiskStorage; import com.facebook.cache.disk.DiskStorage;
import com.facebook.common.internal.ByteStreams; import com.facebook.common.internal.ByteStreams;
import com.facebook.common.internal.Preconditions;
import com.facebook.common.internal.Predicate; import com.facebook.common.internal.Predicate;
import com.facebook.common.memory.PooledByteBuffer; import com.facebook.common.memory.PooledByteBuffer;
import com.facebook.common.memory.PooledByteBufferInputStream; import com.facebook.common.memory.PooledByteBufferInputStream;
@@ -50,6 +51,7 @@ import com.facebook.imagepipeline.image.EncodedImage;
import com.facebook.imageutils.BitmapUtil; import com.facebook.imageutils.BitmapUtil;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.StringWriter; import java.io.StringWriter;
import java.util.ArrayList; import java.util.ArrayList;
@@ -214,16 +216,16 @@ public class FrescoFlipperPlugin extends BufferingFlipperPlugin
final ImagePipelineFactory imagePipelineFactory = Fresco.getImagePipelineFactory(); final ImagePipelineFactory imagePipelineFactory = Fresco.getImagePipelineFactory();
// try to load from bitmap cache // try to load from bitmap cache
CloseableReference<CloseableImage> ref = @Nullable
imagePipelineFactory.getBitmapCountingMemoryCache().get(cacheKey); CloseableImage closeableImage =
try { imagePipelineFactory.getBitmapCountingMemoryCache().inspect(cacheKey);
if (ref != null) { if (closeableImage instanceof CloseableBitmap) {
loadFromBitmapCache(ref, imageId, cacheKey, responder); @Nullable Bitmap bitmap = ((CloseableBitmap) closeableImage).getUnderlyingBitmap();
if (bitmap != null) {
loadFromBitmapCache(bitmap, imageId, cacheKey, responder);
mPerfLogger.endMarker("Sonar.Fresco.getImage"); mPerfLogger.endMarker("Sonar.Fresco.getImage");
return; return;
} }
} finally {
CloseableReference.closeSafely(ref);
} }
// try to load from encoded cache // try to load from encoded cache
@@ -244,28 +246,19 @@ public class FrescoFlipperPlugin extends BufferingFlipperPlugin
} }
private void loadFromBitmapCache( private void loadFromBitmapCache(
final CloseableReference<CloseableImage> ref, final Bitmap bitmap,
final String imageId, final String imageId,
final CacheKey cacheKey, final CacheKey cacheKey,
final FlipperResponder responder) { final FlipperResponder responder) {
if (ref.get() instanceof CloseableBitmap) { String encodedBitmap = bitmapToBase64Preview(bitmap, mPlatformBitmapFactory);
final CloseableBitmap bitmap = (CloseableBitmap) ref.get(); responder.success(
String encodedBitmap = getImageData(
bitmapToBase64Preview(bitmap.getUnderlyingBitmap(), mPlatformBitmapFactory); imageId,
mFlipperImageTracker.getUriString(cacheKey),
responder.success( bitmap.getWidth(),
getImageData( bitmap.getHeight(),
imageId, BitmapUtil.getSizeInBytes(bitmap),
mFlipperImageTracker.getUriString(cacheKey), encodedBitmap));
bitmap.getWidth(),
bitmap.getHeight(),
bitmap.getSizeInBytes(),
encodedBitmap));
} else {
// TODO: T48376327, it might happened that ref.get() may not be casted to
// CloseableBitmap, this issue is tracked in the before mentioned task
responder.success();
}
} }
private void loadFromEncodedCache( private void loadFromEncodedCache(
@@ -307,14 +300,17 @@ public class FrescoFlipperPlugin extends BufferingFlipperPlugin
mPerfLogger.cancelMarker("Sonar.Fresco.getImage"); mPerfLogger.cancelMarker("Sonar.Fresco.getImage");
return null; return null;
} }
Preconditions.checkNotNull(task);
final EncodedImage image = task.getResult(); final EncodedImage image = task.getResult();
try { try {
byte[] encodedArray = ByteStreams.toByteArray(image.getInputStream()); InputStream stream = Preconditions.checkNotNull(image.getInputStream());
byte[] encodedArray = ByteStreams.toByteArray(stream);
responder.success( responder.success(
getImageData( getImageData(
imageId, imageId,
mFlipperImageTracker.getLocalPath(cacheKey), Preconditions.checkNotNull(
mFlipperImageTracker.getLocalPath(cacheKey)),
image.getWidth(), image.getWidth(),
image.getHeight(), image.getHeight(),
encodedArray.length, encodedArray.length,
@@ -640,10 +636,12 @@ public class FrescoFlipperPlugin extends BufferingFlipperPlugin
@Override @Override
public void onCloseableReferenceLeak( public void onCloseableReferenceLeak(
SharedReference<Object> reference, @Nullable Throwable stacktrace) { SharedReference<Object> reference, @Nullable Throwable stacktrace) {
Object object = reference.get();
Preconditions.checkNotNull(object);
final FlipperObject.Builder builder = final FlipperObject.Builder builder =
new FlipperObject.Builder() new FlipperObject.Builder()
.put("identityHashCode", System.identityHashCode(reference)) .put("identityHashCode", System.identityHashCode(reference))
.put("className", reference.get().getClass().getName()); .put("className", object.getClass().getName());
if (stacktrace != null) { if (stacktrace != null) {
builder.put("stacktrace", getStackTraceString(stacktrace)); builder.put("stacktrace", getStackTraceString(stacktrace));
} }

View File

@@ -210,7 +210,7 @@ public class FlipperObject {
return put(name, v.toFlipperObject()); return put(name, v.toFlipperObject());
} }
public Builder put(String name, FlipperArray a) { public Builder put(String name, @Nullable FlipperArray a) {
try { try {
mJson.put(name, a == null ? null : a.mJson); mJson.put(name, a == null ? null : a.mJson);
} catch (JSONException e) { } catch (JSONException e) {
@@ -223,7 +223,7 @@ public class FlipperObject {
return put(name, b.build()); return put(name, b.build());
} }
public Builder put(String name, FlipperObject o) { public Builder put(String name, @Nullable FlipperObject o) {
try { try {
mJson.put(name, o == null ? null : o.mJson); mJson.put(name, o == null ? null : o.mJson);
} catch (JSONException e) { } catch (JSONException e) {