diff options
Diffstat (limited to 'library/src/main/java/com/davemorrissey/labs/subscaleview/decoder')
7 files changed, 887 insertions, 0 deletions
diff --git a/library/src/main/java/com/davemorrissey/labs/subscaleview/decoder/CompatDecoderFactory.java b/library/src/main/java/com/davemorrissey/labs/subscaleview/decoder/CompatDecoderFactory.java new file mode 100644 index 0000000..870489f --- /dev/null +++ b/library/src/main/java/com/davemorrissey/labs/subscaleview/decoder/CompatDecoderFactory.java @@ -0,0 +1,47 @@ +package com.davemorrissey.labs.subscaleview.decoder; + +import android.graphics.Bitmap; +import android.support.annotation.NonNull; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; + +/** + * Compatibility factory to instantiate decoders with empty public constructors. + * @param <T> The base type of the decoder this factory will produce. + */ +@SuppressWarnings("WeakerAccess") +public class CompatDecoderFactory<T> implements DecoderFactory<T> { + + private final Class<? extends T> clazz; + private final Bitmap.Config bitmapConfig; + + /** + * Construct a factory for the given class. This must have a default constructor. + * @param clazz a class that implements {@link ImageDecoder} or {@link ImageRegionDecoder}. + */ + public CompatDecoderFactory(@NonNull Class<? extends T> clazz) { + this(clazz, null); + } + + /** + * Construct a factory for the given class. This must have a constructor that accepts a {@link Bitmap.Config} instance. + * @param clazz a class that implements {@link ImageDecoder} or {@link ImageRegionDecoder}. + * @param bitmapConfig bitmap configuration to be used when loading images. + */ + public CompatDecoderFactory(@NonNull Class<? extends T> clazz, Bitmap.Config bitmapConfig) { + this.clazz = clazz; + this.bitmapConfig = bitmapConfig; + } + + @Override + public T make() throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException { + if (bitmapConfig == null) { + return clazz.newInstance(); + } else { + Constructor<? extends T> ctor = clazz.getConstructor(Bitmap.Config.class); + return ctor.newInstance(bitmapConfig); + } + } + +} diff --git a/library/src/main/java/com/davemorrissey/labs/subscaleview/decoder/DecoderFactory.java b/library/src/main/java/com/davemorrissey/labs/subscaleview/decoder/DecoderFactory.java new file mode 100644 index 0000000..4b521c8 --- /dev/null +++ b/library/src/main/java/com/davemorrissey/labs/subscaleview/decoder/DecoderFactory.java @@ -0,0 +1,21 @@ +package com.davemorrissey.labs.subscaleview.decoder; + +import java.lang.reflect.InvocationTargetException; + +/** + * Interface for {@link ImageDecoder} and {@link ImageRegionDecoder} factories. + * @param <T> the class of decoder that will be produced. + */ +public interface DecoderFactory<T> { + + /** + * Produce a new instance of a decoder with type {@link T}. + * @return a new instance of your decoder. + * @throws IllegalAccessException if the factory class cannot be instantiated. + * @throws InstantiationException if the factory class cannot be instantiated. + * @throws NoSuchMethodException if the factory class cannot be instantiated. + * @throws InvocationTargetException if the factory class cannot be instantiated. + */ + T make() throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException; + +} diff --git a/library/src/main/java/com/davemorrissey/labs/subscaleview/decoder/ImageDecoder.java b/library/src/main/java/com/davemorrissey/labs/subscaleview/decoder/ImageDecoder.java new file mode 100644 index 0000000..004f875 --- /dev/null +++ b/library/src/main/java/com/davemorrissey/labs/subscaleview/decoder/ImageDecoder.java @@ -0,0 +1,29 @@ +package com.davemorrissey.labs.subscaleview.decoder; + +import android.content.Context; +import android.graphics.Bitmap; +import android.net.Uri; + +/** + * Interface for image decoding classes, allowing the default {@link android.graphics.BitmapFactory} + * based on the Skia library to be replaced with a custom class. + */ +public interface ImageDecoder { + + /** + * Decode an image. The URI can be in one of the following formats: + * <br> + * File: <code>file:///scard/picture.jpg</code> + * <br> + * Asset: <code>file:///android_asset/picture.png</code> + * <br> + * Resource: <code>android.resource://com.example.app/drawable/picture</code> + * + * @param context Application context + * @param uri URI of the image + * @return the decoded bitmap + * @throws Exception if decoding fails. + */ + Bitmap decode(Context context, Uri uri) throws Exception; + +} diff --git a/library/src/main/java/com/davemorrissey/labs/subscaleview/decoder/ImageRegionDecoder.java b/library/src/main/java/com/davemorrissey/labs/subscaleview/decoder/ImageRegionDecoder.java new file mode 100644 index 0000000..22ee955 --- /dev/null +++ b/library/src/main/java/com/davemorrissey/labs/subscaleview/decoder/ImageRegionDecoder.java @@ -0,0 +1,60 @@ +package com.davemorrissey.labs.subscaleview.decoder; + +import android.content.Context; +import android.graphics.Bitmap; +import android.graphics.Point; +import android.graphics.Rect; +import android.net.Uri; + +/** + * Interface for image decoding classes, allowing the default {@link android.graphics.BitmapRegionDecoder} + * based on the Skia library to be replaced with a custom class. + */ +public interface ImageRegionDecoder { + + /** + * Initialise the decoder. When possible, perform initial setup work once in this method. The + * dimensions of the image must be returned. The URI can be in one of the following formats: + * <br> + * File: <code>file:///scard/picture.jpg</code> + * <br> + * Asset: <code>file:///android_asset/picture.png</code> + * <br> + * Resource: <code>android.resource://com.example.app/drawable/picture</code> + * @param context Application context. A reference may be held, but must be cleared on recycle. + * @param uri URI of the image. + * @return Dimensions of the image. + * @throws Exception if initialisation fails. + */ + Point init(Context context, Uri uri) throws Exception; + + /** + * <p> + * Decode a region of the image with the given sample size. This method is called off the UI + * thread so it can safely load the image on the current thread. It is called from + * {@link android.os.AsyncTask}s running in an executor that may have multiple threads, so + * implementations must be thread safe. Adding <code>synchronized</code> to the method signature + * is the simplest way to achieve this, but bear in mind the {@link #recycle()} method can be + * called concurrently. + * </p><p> + * See {@link SkiaImageRegionDecoder} and {@link SkiaPooledImageRegionDecoder} for examples of + * internal locking and synchronization. + * </p> + * @param sRect Source image rectangle to decode. + * @param sampleSize Sample size. + * @return The decoded region. It is safe to return null if decoding fails. + */ + Bitmap decodeRegion(Rect sRect, int sampleSize); + + /** + * Status check. Should return false before initialisation and after recycle. + * @return true if the decoder is ready to be used. + */ + boolean isReady(); + + /** + * This method will be called when the decoder is no longer required. It should clean up any resources still in use. + */ + void recycle(); + +} diff --git a/library/src/main/java/com/davemorrissey/labs/subscaleview/decoder/SkiaImageDecoder.java b/library/src/main/java/com/davemorrissey/labs/subscaleview/decoder/SkiaImageDecoder.java new file mode 100644 index 0000000..be1cd32 --- /dev/null +++ b/library/src/main/java/com/davemorrissey/labs/subscaleview/decoder/SkiaImageDecoder.java @@ -0,0 +1,102 @@ +package com.davemorrissey.labs.subscaleview.decoder; + +import android.content.ContentResolver; +import android.content.Context; +import android.content.pm.PackageManager; +import android.content.res.Resources; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.net.Uri; +import android.support.annotation.Keep; +import android.text.TextUtils; + +import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView; + +import java.io.InputStream; +import java.util.List; + +/** + * Default implementation of {@link com.davemorrissey.labs.subscaleview.decoder.ImageDecoder} + * using Android's {@link android.graphics.BitmapFactory}, based on the Skia library. This + * works well in most circumstances and has reasonable performance, however it has some problems + * with grayscale, indexed and CMYK images. + */ +public class SkiaImageDecoder implements ImageDecoder { + + private static final String FILE_PREFIX = "file://"; + private static final String ASSET_PREFIX = FILE_PREFIX + "/android_asset/"; + private static final String RESOURCE_PREFIX = ContentResolver.SCHEME_ANDROID_RESOURCE + "://"; + + private final Bitmap.Config bitmapConfig; + + @Keep + @SuppressWarnings("unused") + public SkiaImageDecoder() { + this(null); + } + + @SuppressWarnings({"WeakerAccess", "SameParameterValue"}) + public SkiaImageDecoder(Bitmap.Config bitmapConfig) { + Bitmap.Config globalBitmapConfig = SubsamplingScaleImageView.getPreferredBitmapConfig(); + if (bitmapConfig != null) { + this.bitmapConfig = bitmapConfig; + } else if (globalBitmapConfig != null) { + this.bitmapConfig = globalBitmapConfig; + } else { + this.bitmapConfig = Bitmap.Config.RGB_565; + } + } + + @Override + public Bitmap decode(Context context, Uri uri) throws Exception { + String uriString = uri.toString(); + BitmapFactory.Options options = new BitmapFactory.Options(); + Bitmap bitmap; + options.inPreferredConfig = bitmapConfig; + if (uriString.startsWith(RESOURCE_PREFIX)) { + Resources res; + String packageName = uri.getAuthority(); + if (context.getPackageName().equals(packageName)) { + res = context.getResources(); + } else { + PackageManager pm = context.getPackageManager(); + res = pm.getResourcesForApplication(packageName); + } + + int id = 0; + List<String> segments = uri.getPathSegments(); + int size = segments.size(); + if (size == 2 && segments.get(0).equals("drawable")) { + String resName = segments.get(1); + id = res.getIdentifier(resName, "drawable", packageName); + } else if (size == 1 && TextUtils.isDigitsOnly(segments.get(0))) { + try { + id = Integer.parseInt(segments.get(0)); + } catch (NumberFormatException ignored) { + } + } + + bitmap = BitmapFactory.decodeResource(context.getResources(), id, options); + } else if (uriString.startsWith(ASSET_PREFIX)) { + String assetName = uriString.substring(ASSET_PREFIX.length()); + bitmap = BitmapFactory.decodeStream(context.getAssets().open(assetName), null, options); + } else if (uriString.startsWith(FILE_PREFIX)) { + bitmap = BitmapFactory.decodeFile(uriString.substring(FILE_PREFIX.length()), options); + } else { + InputStream inputStream = null; + try { + ContentResolver contentResolver = context.getContentResolver(); + inputStream = contentResolver.openInputStream(uri); + bitmap = BitmapFactory.decodeStream(inputStream, null, options); + } finally { + if (inputStream != null) { + try { inputStream.close(); } catch (Exception e) { /* Ignore */ } + } + } + } + if (bitmap == null) { + throw new RuntimeException("Skia image region decoder returned null bitmap - image format may not be supported"); + } + return bitmap; + } +} diff --git a/library/src/main/java/com/davemorrissey/labs/subscaleview/decoder/SkiaImageRegionDecoder.java b/library/src/main/java/com/davemorrissey/labs/subscaleview/decoder/SkiaImageRegionDecoder.java new file mode 100644 index 0000000..eebe2bb --- /dev/null +++ b/library/src/main/java/com/davemorrissey/labs/subscaleview/decoder/SkiaImageRegionDecoder.java @@ -0,0 +1,158 @@ +package com.davemorrissey.labs.subscaleview.decoder; + +import android.content.ContentResolver; +import android.content.Context; +import android.content.pm.PackageManager; +import android.content.res.AssetManager; +import android.content.res.Resources; +import android.graphics.*; +import android.net.Uri; +import android.os.Build; +import android.support.annotation.Keep; +import android.text.TextUtils; + +import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView; + +import java.io.InputStream; +import java.util.List; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +/** + * Default implementation of {@link com.davemorrissey.labs.subscaleview.decoder.ImageRegionDecoder} + * using Android's {@link android.graphics.BitmapRegionDecoder}, based on the Skia library. This + * works well in most circumstances and has reasonable performance due to the cached decoder instance, + * however it has some problems with grayscale, indexed and CMYK images. + * + * A {@link ReadWriteLock} is used to delegate responsibility for multi threading behaviour to the + * {@link BitmapRegionDecoder} instance on SDK >= 21, whilst allowing this class to block until no + * tiles are being loaded before recycling the decoder. In practice, {@link BitmapRegionDecoder} is + * synchronized internally so this has no real impact on performance. + */ +public class SkiaImageRegionDecoder implements ImageRegionDecoder { + + private BitmapRegionDecoder decoder; + private final ReadWriteLock decoderLock = new ReentrantReadWriteLock(true); + + private static final String FILE_PREFIX = "file://"; + private static final String ASSET_PREFIX = FILE_PREFIX + "/android_asset/"; + private static final String RESOURCE_PREFIX = ContentResolver.SCHEME_ANDROID_RESOURCE + "://"; + + private final Bitmap.Config bitmapConfig; + + @Keep + @SuppressWarnings("unused") + public SkiaImageRegionDecoder() { + this(null); + } + + @SuppressWarnings({"WeakerAccess", "SameParameterValue"}) + public SkiaImageRegionDecoder(Bitmap.Config bitmapConfig) { + Bitmap.Config globalBitmapConfig = SubsamplingScaleImageView.getPreferredBitmapConfig(); + if (bitmapConfig != null) { + this.bitmapConfig = bitmapConfig; + } else if (globalBitmapConfig != null) { + this.bitmapConfig = globalBitmapConfig; + } else { + this.bitmapConfig = Bitmap.Config.RGB_565; + } + } + + @Override + public Point init(Context context, Uri uri) throws Exception { + String uriString = uri.toString(); + if (uriString.startsWith(RESOURCE_PREFIX)) { + Resources res; + String packageName = uri.getAuthority(); + if (context.getPackageName().equals(packageName)) { + res = context.getResources(); + } else { + PackageManager pm = context.getPackageManager(); + res = pm.getResourcesForApplication(packageName); + } + + int id = 0; + List<String> segments = uri.getPathSegments(); + int size = segments.size(); + if (size == 2 && segments.get(0).equals("drawable")) { + String resName = segments.get(1); + id = res.getIdentifier(resName, "drawable", packageName); + } else if (size == 1 && TextUtils.isDigitsOnly(segments.get(0))) { + try { + id = Integer.parseInt(segments.get(0)); + } catch (NumberFormatException ignored) { + } + } + + decoder = BitmapRegionDecoder.newInstance(context.getResources().openRawResource(id), false); + } else if (uriString.startsWith(ASSET_PREFIX)) { + String assetName = uriString.substring(ASSET_PREFIX.length()); + decoder = BitmapRegionDecoder.newInstance(context.getAssets().open(assetName, AssetManager.ACCESS_RANDOM), false); + } else if (uriString.startsWith(FILE_PREFIX)) { + decoder = BitmapRegionDecoder.newInstance(uriString.substring(FILE_PREFIX.length()), false); + } else { + InputStream inputStream = null; + try { + ContentResolver contentResolver = context.getContentResolver(); + inputStream = contentResolver.openInputStream(uri); + decoder = BitmapRegionDecoder.newInstance(inputStream, false); + } finally { + if (inputStream != null) { + try { inputStream.close(); } catch (Exception e) { /* Ignore */ } + } + } + } + return new Point(decoder.getWidth(), decoder.getHeight()); + } + + @Override + public Bitmap decodeRegion(Rect sRect, int sampleSize) { + getDecodeLock().lock(); + try { + if (decoder != null && !decoder.isRecycled()) { + BitmapFactory.Options options = new BitmapFactory.Options(); + options.inSampleSize = sampleSize; + options.inPreferredConfig = bitmapConfig; + Bitmap bitmap = decoder.decodeRegion(sRect, options); + if (bitmap == null) { + throw new RuntimeException("Skia image decoder returned null bitmap - image format may not be supported"); + } + return bitmap; + } else { + throw new IllegalStateException("Cannot decode region after decoder has been recycled"); + } + } finally { + getDecodeLock().unlock(); + } + } + + @Override + public synchronized boolean isReady() { + return decoder != null && !decoder.isRecycled(); + } + + @Override + public synchronized void recycle() { + decoderLock.writeLock().lock(); + try { + decoder.recycle(); + decoder = null; + } finally { + decoderLock.writeLock().unlock(); + } + } + + /** + * Before SDK 21, BitmapRegionDecoder was not synchronized internally. Any attempt to decode + * regions from multiple threads with one decoder instance causes a segfault. For old versions + * use the write lock to enforce single threaded decoding. + */ + private Lock getDecodeLock() { + if (Build.VERSION.SDK_INT < 21) { + return decoderLock.writeLock(); + } else { + return decoderLock.readLock(); + } + } +} diff --git a/library/src/main/java/com/davemorrissey/labs/subscaleview/decoder/SkiaPooledImageRegionDecoder.java b/library/src/main/java/com/davemorrissey/labs/subscaleview/decoder/SkiaPooledImageRegionDecoder.java new file mode 100644 index 0000000..80e415b --- /dev/null +++ b/library/src/main/java/com/davemorrissey/labs/subscaleview/decoder/SkiaPooledImageRegionDecoder.java @@ -0,0 +1,470 @@ +package com.davemorrissey.labs.subscaleview.decoder; + +import android.app.ActivityManager; +import android.content.ContentResolver; +import android.content.Context; +import android.content.pm.PackageManager; +import android.content.res.AssetFileDescriptor; +import android.content.res.AssetManager; +import android.content.res.Resources; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.graphics.BitmapRegionDecoder; +import android.graphics.Point; +import android.graphics.Rect; +import android.net.Uri; +import android.os.Build; +import android.support.annotation.Keep; +import android.text.TextUtils; +import android.util.Log; + +import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView; + +import java.io.File; +import java.io.FileFilter; +import java.io.InputStream; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.Executor; +import java.util.concurrent.Semaphore; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; +import java.util.regex.Pattern; + +import static android.content.Context.ACTIVITY_SERVICE; + +/** + * <p> + * An implementation of {@link ImageRegionDecoder} using a pool of {@link BitmapRegionDecoder}s, + * to provide true parallel loading of tiles. This is only effective if parallel loading has been + * enabled in the view by calling {@link com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView#setExecutor(Executor)} + * with a multi-threaded {@link Executor} instance. + * </p><p> + * One decoder is initialised when the class is initialised. This is enough to decode base layer tiles. + * Additional decoders are initialised when a subregion of the image is first requested, which indicates + * interaction with the view. Creation of additional encoders stops when {@link #allowAdditionalDecoder(int, long)} + * returns false. The default implementation takes into account the file size, number of CPU cores, + * low memory status and a hard limit of 4. Extend this class to customise this. + * </p><p> + * <b>WARNING:</b> This class is highly experimental and not proven to be stable on a wide range of + * devices. You are advised to test it thoroughly on all available devices, and code your app to use + * {@link SkiaImageRegionDecoder} on old or low powered devices you could not test. + * </p> + */ +public class SkiaPooledImageRegionDecoder implements ImageRegionDecoder { + + private static final String TAG = SkiaPooledImageRegionDecoder.class.getSimpleName(); + + private static boolean debug = false; + + private DecoderPool decoderPool = new DecoderPool(); + private final ReadWriteLock decoderLock = new ReentrantReadWriteLock(true); + + private static final String FILE_PREFIX = "file://"; + private static final String ASSET_PREFIX = FILE_PREFIX + "/android_asset/"; + private static final String RESOURCE_PREFIX = ContentResolver.SCHEME_ANDROID_RESOURCE + "://"; + + private final Bitmap.Config bitmapConfig; + + private Context context; + private Uri uri; + + private long fileLength = Long.MAX_VALUE; + private final Point imageDimensions = new Point(0, 0); + private final AtomicBoolean lazyInited = new AtomicBoolean(false); + + @Keep + @SuppressWarnings("unused") + public SkiaPooledImageRegionDecoder() { + this(null); + } + + @SuppressWarnings({"WeakerAccess", "SameParameterValue"}) + public SkiaPooledImageRegionDecoder(Bitmap.Config bitmapConfig) { + Bitmap.Config globalBitmapConfig = SubsamplingScaleImageView.getPreferredBitmapConfig(); + if (bitmapConfig != null) { + this.bitmapConfig = bitmapConfig; + } else if (globalBitmapConfig != null) { + this.bitmapConfig = globalBitmapConfig; + } else { + this.bitmapConfig = Bitmap.Config.RGB_565; + } + } + + /** + * Controls logging of debug messages. All instances are affected. + * @param debug true to enable debug logging, false to disable. + */ + @Keep + @SuppressWarnings("unused") + public static void setDebug(boolean debug) { + SkiaPooledImageRegionDecoder.debug = debug; + } + + /** + * Initialises the decoder pool. This method creates one decoder on the current thread and uses + * it to decode the bounds, then spawns an independent thread to populate the pool with an + * additional three decoders. The thread will abort if {@link #recycle()} is called. + */ + @Override + public Point init(final Context context, final Uri uri) throws Exception { + this.context = context; + this.uri = uri; + initialiseDecoder(); + return this.imageDimensions; + } + + /** + * Initialises extra decoders for as long as {@link #allowAdditionalDecoder(int, long)} returns + * true and the pool has not been recycled. + */ + private void lazyInit() { + if (lazyInited.compareAndSet(false, true) && fileLength < Long.MAX_VALUE) { + debug("Starting lazy init of additional decoders"); + Thread thread = new Thread() { + @Override + public void run() { + while (decoderPool != null && allowAdditionalDecoder(decoderPool.size(), fileLength)) { + // New decoders can be created while reading tiles but this read lock prevents + // them being initialised while the pool is being recycled. + try { + if (decoderPool != null) { + long start = System.currentTimeMillis(); + debug("Starting decoder"); + initialiseDecoder(); + long end = System.currentTimeMillis(); + debug("Started decoder, took " + (end - start) + "ms"); + } + } catch (Exception e) { + // A decoder has already been successfully created so we can ignore this + debug("Failed to start decoder: " + e.getMessage()); + } + } + } + }; + thread.start(); + } + } + + /** + * Initialises a new {@link BitmapRegionDecoder} and adds it to the pool, unless the pool has + * been recycled while it was created. + */ + private void initialiseDecoder() throws Exception { + String uriString = uri.toString(); + BitmapRegionDecoder decoder; + long fileLength = Long.MAX_VALUE; + if (uriString.startsWith(RESOURCE_PREFIX)) { + Resources res; + String packageName = uri.getAuthority(); + if (context.getPackageName().equals(packageName)) { + res = context.getResources(); + } else { + PackageManager pm = context.getPackageManager(); + res = pm.getResourcesForApplication(packageName); + } + + int id = 0; + List<String> segments = uri.getPathSegments(); + int size = segments.size(); + if (size == 2 && segments.get(0).equals("drawable")) { + String resName = segments.get(1); + id = res.getIdentifier(resName, "drawable", packageName); + } else if (size == 1 && TextUtils.isDigitsOnly(segments.get(0))) { + try { + id = Integer.parseInt(segments.get(0)); + } catch (NumberFormatException ignored) { + } + } + try { + AssetFileDescriptor descriptor = context.getResources().openRawResourceFd(id); + fileLength = descriptor.getLength(); + } catch (Exception e) { + // Pooling disabled + } + decoder = BitmapRegionDecoder.newInstance(context.getResources().openRawResource(id), false); + } else if (uriString.startsWith(ASSET_PREFIX)) { + String assetName = uriString.substring(ASSET_PREFIX.length()); + try { + AssetFileDescriptor descriptor = context.getAssets().openFd(assetName); + fileLength = descriptor.getLength(); + } catch (Exception e) { + // Pooling disabled + } + decoder = BitmapRegionDecoder.newInstance(context.getAssets().open(assetName, AssetManager.ACCESS_RANDOM), false); + } else if (uriString.startsWith(FILE_PREFIX)) { + decoder = BitmapRegionDecoder.newInstance(uriString.substring(FILE_PREFIX.length()), false); + try { + File file = new File(uriString); + if (file.exists()) { + fileLength = file.length(); + } + } catch (Exception e) { + // Pooling disabled + } + } else { + InputStream inputStream = null; + try { + ContentResolver contentResolver = context.getContentResolver(); + inputStream = contentResolver.openInputStream(uri); + decoder = BitmapRegionDecoder.newInstance(inputStream, false); + try { + AssetFileDescriptor descriptor = contentResolver.openAssetFileDescriptor(uri, "r"); + if (descriptor != null) { + fileLength = descriptor.getLength(); + } + } catch (Exception e) { + // Stick with MAX_LENGTH + } + } finally { + if (inputStream != null) { + try { inputStream.close(); } catch (Exception e) { /* Ignore */ } + } + } + } + + this.fileLength = fileLength; + this.imageDimensions.set(decoder.getWidth(), decoder.getHeight()); + decoderLock.writeLock().lock(); + try { + if (decoderPool != null) { + decoderPool.add(decoder); + } + } finally { + decoderLock.writeLock().unlock(); + } + } + + /** + * Acquire a read lock to prevent decoding overlapping with recycling, then check the pool still + * exists and acquire a decoder to load the requested region. There is no check whether the pool + * currently has decoders, because it's guaranteed to have one decoder after {@link #init(Context, Uri)} + * is called and be null once {@link #recycle()} is called. In practice the view can't call this + * method until after {@link #init(Context, Uri)}, so there will be no blocking on an empty pool. + */ + @Override + public Bitmap decodeRegion(Rect sRect, int sampleSize) { + debug("Decode region " + sRect + " on thread " + Thread.currentThread().getName()); + if (sRect.width() < imageDimensions.x || sRect.height() < imageDimensions.y) { + lazyInit(); + } + decoderLock.readLock().lock(); + try { + if (decoderPool != null) { + BitmapRegionDecoder decoder = decoderPool.acquire(); + try { + // Decoder can't be null or recycled in practice + if (decoder != null && !decoder.isRecycled()) { + BitmapFactory.Options options = new BitmapFactory.Options(); + options.inSampleSize = sampleSize; + options.inPreferredConfig = bitmapConfig; + Bitmap bitmap = decoder.decodeRegion(sRect, options); + if (bitmap == null) { + throw new RuntimeException("Skia image decoder returned null bitmap - image format may not be supported"); + } + return bitmap; + } + } finally { + if (decoder != null) { + decoderPool.release(decoder); + } + } + } + throw new IllegalStateException("Cannot decode region after decoder has been recycled"); + } finally { + decoderLock.readLock().unlock(); + } + } + + /** + * Holding a read lock to avoid returning true while the pool is being recycled, this returns + * true if the pool has at least one decoder available. + */ + @Override + public synchronized boolean isReady() { + return decoderPool != null && !decoderPool.isEmpty(); + } + + /** + * Wait until all read locks held by {@link #decodeRegion(Rect, int)} are released, then recycle + * and destroy the pool. Elsewhere, when a read lock is acquired, we must check the pool is not null. + */ + @Override + public synchronized void recycle() { + decoderLock.writeLock().lock(); + try { + if (decoderPool != null) { + decoderPool.recycle(); + decoderPool = null; + context = null; + uri = null; + } + } finally { + decoderLock.writeLock().unlock(); + } + } + + /** + * Called before creating a new decoder. Based on number of CPU cores, available memory, and the + * size of the image file, determines whether another decoder can be created. Subclasses can + * override and customise this. + * @param numberOfDecoders the number of decoders that have been created so far + * @param fileLength the size of the image file in bytes. Creating another decoder will use approximately this much native memory. + * @return true if another decoder can be created. + */ + @SuppressWarnings("WeakerAccess") + protected boolean allowAdditionalDecoder(int numberOfDecoders, long fileLength) { + if (numberOfDecoders >= 4) { + debug("No additional decoders allowed, reached hard limit (4)"); + return false; + } else if (numberOfDecoders * fileLength > 20 * 1024 * 1024) { + debug("No additional encoders allowed, reached hard memory limit (20Mb)"); + return false; + } else if (numberOfDecoders >= getNumberOfCores()) { + debug("No additional encoders allowed, limited by CPU cores (" + getNumberOfCores() + ")"); + return false; + } else if (isLowMemory()) { + debug("No additional encoders allowed, memory is low"); + return false; + } + debug("Additional decoder allowed, current count is " + numberOfDecoders + ", estimated native memory " + ((fileLength * numberOfDecoders)/(1024 * 1024)) + "Mb"); + return true; + } + + + /** + * A simple pool of {@link BitmapRegionDecoder} instances, all loading from the same source. + */ + private static class DecoderPool { + private final Semaphore available = new Semaphore(0, true); + private final Map<BitmapRegionDecoder, Boolean> decoders = new ConcurrentHashMap<>(); + + /** + * Returns false if there is at least one decoder in the pool. + */ + private synchronized boolean isEmpty() { + return decoders.isEmpty(); + } + + /** + * Returns number of encoders. + */ + private synchronized int size() { + return decoders.size(); + } + + /** + * Acquire a decoder. Blocks until one is available. + */ + private BitmapRegionDecoder acquire() { + available.acquireUninterruptibly(); + return getNextAvailable(); + } + + /** + * Release a decoder back to the pool. + */ + private void release(BitmapRegionDecoder decoder) { + if (markAsUnused(decoder)) { + available.release(); + } + } + + /** + * Adds a newly created decoder to the pool, releasing an additional permit. + */ + private synchronized void add(BitmapRegionDecoder decoder) { + decoders.put(decoder, false); + available.release(); + } + + /** + * While there are decoders in the map, wait until each is available before acquiring, + * recycling and removing it. After this is called, any call to {@link #acquire()} will + * block forever, so this call should happen within a write lock, and all calls to + * {@link #acquire()} should be made within a read lock so they cannot end up blocking on + * the semaphore when it has no permits. + */ + private synchronized void recycle() { + while (!decoders.isEmpty()) { + BitmapRegionDecoder decoder = acquire(); + decoder.recycle(); + decoders.remove(decoder); + } + } + + private synchronized BitmapRegionDecoder getNextAvailable() { + for (Map.Entry<BitmapRegionDecoder, Boolean> entry : decoders.entrySet()) { + if (!entry.getValue()) { + entry.setValue(true); + return entry.getKey(); + } + } + return null; + } + + private synchronized boolean markAsUnused(BitmapRegionDecoder decoder) { + for (Map.Entry<BitmapRegionDecoder, Boolean> entry : decoders.entrySet()) { + if (decoder == entry.getKey()) { + if (entry.getValue()) { + entry.setValue(false); + return true; + } else { + return false; + } + } + } + return false; + } + + } + + private int getNumberOfCores() { + if (Build.VERSION.SDK_INT >= 17) { + return Runtime.getRuntime().availableProcessors(); + } else { + return getNumCoresOldPhones(); + } + } + + /** + * Gets the number of cores available in this device, across all processors. + * Requires: Ability to peruse the filesystem at "/sys/devices/system/cpu" + * @return The number of cores, or 1 if failed to get result + */ + private int getNumCoresOldPhones() { + class CpuFilter implements FileFilter { + @Override + public boolean accept(File pathname) { + return Pattern.matches("cpu[0-9]+", pathname.getName()); + } + } + try { + File dir = new File("/sys/devices/system/cpu/"); + File[] files = dir.listFiles(new CpuFilter()); + return files.length; + } catch(Exception e) { + return 1; + } + } + + private boolean isLowMemory() { + ActivityManager activityManager = (ActivityManager)context.getSystemService(ACTIVITY_SERVICE); + if (activityManager != null) { + ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo(); + activityManager.getMemoryInfo(memoryInfo); + return memoryInfo.lowMemory; + } else { + return true; + } + } + + private void debug(String message) { + if (debug) { + Log.d(TAG, message); + } + } + +} |