From 17e0943fdc9e24ce4f105df53e91c7cdea60edf6 Mon Sep 17 00:00:00 2001
From: Sam Judd <judds@google.com>
Date: Wed, 17 Jan 2018 12:17:06 -0800
Subject: [PATCH] Allow ARGB_8888 to be re-used for RGBA_F16 if size
 appropriate.

---
 .../com/bumptech/glide/WideGamutTest.java     | 20 ++++++++++++++
 .../bitmap_recycle/SizeConfigStrategy.java    | 26 +++++++++----------
 2 files changed, 33 insertions(+), 13 deletions(-)

diff --git a/instrumentation/src/androidTest/java/com/bumptech/glide/WideGamutTest.java b/instrumentation/src/androidTest/java/com/bumptech/glide/WideGamutTest.java
index 0b72432c92..17db2efb4c 100644
--- a/instrumentation/src/androidTest/java/com/bumptech/glide/WideGamutTest.java
+++ b/instrumentation/src/androidTest/java/com/bumptech/glide/WideGamutTest.java
@@ -188,6 +188,26 @@ public void roundedCorners_withWideGamutBitmap_producesWideGamutBitmap() {
     assertThat(result.getConfig()).isEqualTo(Config.RGBA_F16);
   }
 
+  @Test
+  public void loadWideGamutImage_withArgb888OfSufficientSizeInPool_usesArgb8888Bitmap() {
+    Bitmap wideGamut = Bitmap.createBitmap(100, 50, Bitmap.Config.RGBA_F16);
+    byte[] data = asPng(wideGamut);
+
+    Bitmap argb8888 = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
+    Glide.init(context, new GlideBuilder()
+        .setBitmapPool(new LruBitmapPool(wideGamut.getAllocationByteCount() * 5)));
+    Glide.get(context).getBitmapPool().put(argb8888);
+
+    Bitmap result =
+        concurrency.get(
+            Glide.with(context)
+                .asBitmap()
+                .load(data)
+                .submit());
+
+    assertThat(result).isSameAs(argb8888);
+  }
+
   private static byte[] asJpeg(Bitmap bitmap) {
     return toByteArray(bitmap, CompressFormat.JPEG);
   }
diff --git a/library/src/main/java/com/bumptech/glide/load/engine/bitmap_recycle/SizeConfigStrategy.java b/library/src/main/java/com/bumptech/glide/load/engine/bitmap_recycle/SizeConfigStrategy.java
index d32b658b78..e496fde4e2 100644
--- a/library/src/main/java/com/bumptech/glide/load/engine/bitmap_recycle/SizeConfigStrategy.java
+++ b/library/src/main/java/com/bumptech/glide/load/engine/bitmap_recycle/SizeConfigStrategy.java
@@ -1,12 +1,14 @@
 package com.bumptech.glide.load.engine.bitmap_recycle;
 
 import android.graphics.Bitmap;
+import android.graphics.Bitmap.Config;
 import android.os.Build;
 import android.support.annotation.Nullable;
 import android.support.annotation.RequiresApi;
 import android.support.annotation.VisibleForTesting;
 import com.bumptech.glide.util.Synthetic;
 import com.bumptech.glide.util.Util;
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.NavigableMap;
@@ -27,24 +29,22 @@
 public class SizeConfigStrategy implements LruPoolStrategy {
   private static final int MAX_SIZE_MULTIPLE = 8;
 
-  // TODO: This can probably be combined with ARGB_8888, it's separate only because we haven't
-  // yet tested ARGB_8888/RGBA_F16 re-use thoroughly yet.
-  private static final Bitmap.Config[] RGBA_F16_IN_CONFIGS;
+  private static final Bitmap.Config[] ARGB_8888_IN_CONFIGS;
   static {
+    Bitmap.Config[] result =
+        new Bitmap.Config[] {
+            Bitmap.Config.ARGB_8888,
+            // The value returned by Bitmaps with the hidden Bitmap config.
+            null,
+        };
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
-      RGBA_F16_IN_CONFIGS = new Bitmap.Config[] { Bitmap.Config.RGBA_F16 };
-    } else {
-      // THis will never be used pre O.
-      RGBA_F16_IN_CONFIGS = new Bitmap.Config[0];
+      result = Arrays.copyOf(result, result.length + 1);
+      result[result.length - 1] = Config.RGBA_F16;
     }
+    ARGB_8888_IN_CONFIGS = result;
   }
+  private static final Bitmap.Config[] RGBA_F16_IN_CONFIGS = ARGB_8888_IN_CONFIGS;
 
-  private static final Bitmap.Config[] ARGB_8888_IN_CONFIGS =
-      new Bitmap.Config[] {
-          Bitmap.Config.ARGB_8888,
-          // The value returned by Bitmaps with the hidden Bitmap config.
-          null,
-      };
   // We probably could allow ARGB_4444 and RGB_565 to decode into each other, but ARGB_4444 is
   // deprecated and we'd rather be safe.
   private static final Bitmap.Config[] RGB_565_IN_CONFIGS =