Use android::skia::BitmapRegionDecoder

Bug: skbug.com/10154
Bug: 140600285
Test: BitmapRegionDecoderTest in CtsGraphicsTestCases

Skia is renaming this class in https://skia-review.googlesource.com/c/skia/+/287498
Use the new name for this and android::skia::BitmapRegionDecoder.

Convert CopyAssetToStream to CopyAssetToData. It was already using an
SkData, held by an SkMemoryStream. Using an SkData directly will allow
sharing it with other decoders for a multithreaded BitmapRegionDecoder.
Remove comment about ref/reopen-ing the asset.

Change-Id: Ib51a8d697ad0f5dc1f461257311b31443b5f9dfb
diff --git a/libs/hwui/jni/BitmapRegionDecoder.cpp b/libs/hwui/jni/BitmapRegionDecoder.cpp
index 3c427f9..4cc05ef 100644
--- a/libs/hwui/jni/BitmapRegionDecoder.cpp
+++ b/libs/hwui/jni/BitmapRegionDecoder.cpp
@@ -22,8 +22,8 @@
 #include "GraphicsJNI.h"
 #include "Utils.h"
 
+#include "BitmapRegionDecoder.h"
 #include "SkBitmap.h"
-#include "SkBitmapRegionDecoder.h"
 #include "SkCodec.h"
 #include "SkData.h"
 #include "SkStream.h"
@@ -36,10 +36,8 @@
 
 using namespace android;
 
-static jobject createBitmapRegionDecoder(JNIEnv* env, std::unique_ptr<SkStreamRewindable> stream) {
-  std::unique_ptr<SkBitmapRegionDecoder> brd(
-            SkBitmapRegionDecoder::Create(stream.release(),
-                                          SkBitmapRegionDecoder::kAndroidCodec_Strategy));
+static jobject createBitmapRegionDecoder(JNIEnv* env, sk_sp<SkData> data) {
+    auto brd = skia::BitmapRegionDecoder::Make(std::move(data));
     if (!brd) {
         doThrowIOE(env, "Image format not supported");
         return nullObjectReturn("CreateBitmapRegionDecoder returned null");
@@ -51,11 +49,7 @@
 static jobject nativeNewInstanceFromByteArray(JNIEnv* env, jobject, jbyteArray byteArray,
                                               jint offset, jint length) {
     AutoJavaByteArray ar(env, byteArray);
-    std::unique_ptr<SkMemoryStream> stream(new SkMemoryStream(ar.ptr() + offset, length, true));
-
-    // the decoder owns the stream.
-    jobject brd = createBitmapRegionDecoder(env, std::move(stream));
-    return brd;
+    return createBitmapRegionDecoder(env, SkData::MakeWithCopy(ar.ptr() + offset, length));
 }
 
 static jobject nativeNewInstanceFromFileDescriptor(JNIEnv* env, jobject clazz,
@@ -70,36 +64,28 @@
         return nullObjectReturn("fstat return -1");
     }
 
-    sk_sp<SkData> data(SkData::MakeFromFD(descriptor));
-    std::unique_ptr<SkMemoryStream> stream(new SkMemoryStream(std::move(data)));
-
-    // the decoder owns the stream.
-    jobject brd = createBitmapRegionDecoder(env, std::move(stream));
-    return brd;
+    return createBitmapRegionDecoder(env, SkData::MakeFromFD(descriptor));
 }
 
 static jobject nativeNewInstanceFromStream(JNIEnv* env, jobject clazz, jobject is, // InputStream
                                            jbyteArray storage) { // byte[]
-    jobject brd = NULL;
-    std::unique_ptr<SkStreamRewindable> stream(CopyJavaInputStream(env, is, storage));
+    jobject brd = nullptr;
+    sk_sp<SkData> data = CopyJavaInputStream(env, is, storage);
 
-    if (stream) {
-        // the decoder owns the stream.
-        brd = createBitmapRegionDecoder(env, std::move(stream));
+    if (data) {
+        brd = createBitmapRegionDecoder(env, std::move(data));
     }
     return brd;
 }
 
 static jobject nativeNewInstanceFromAsset(JNIEnv* env, jobject clazz, jlong native_asset) {
     Asset* asset = reinterpret_cast<Asset*>(native_asset);
-    std::unique_ptr<SkMemoryStream> stream(CopyAssetToStream(asset));
-    if (NULL == stream) {
-        return NULL;
+    sk_sp<SkData> data = CopyAssetToData(asset);
+    if (!data) {
+        return nullptr;
     }
 
-    // the decoder owns the stream.
-    jobject brd = createBitmapRegionDecoder(env, std::move(stream));
-    return brd;
+    return createBitmapRegionDecoder(env, data);
 }
 
 /*
@@ -149,7 +135,7 @@
         recycledBytes = recycledBitmap->getAllocationByteCount();
     }
 
-    SkBitmapRegionDecoder* brd = reinterpret_cast<SkBitmapRegionDecoder*>(brdHandle);
+    auto* brd = reinterpret_cast<skia::BitmapRegionDecoder*>(brdHandle);
     SkColorType decodeColorType = brd->computeOutputColorType(colorType);
     if (decodeColorType == kRGBA_F16_SkColorType && isHardware &&
             !uirenderer::HardwareBitmapUploader::hasFP16Support()) {
@@ -157,7 +143,7 @@
     }
 
     // Set up the pixel allocator
-    SkBRDAllocator* allocator = nullptr;
+    skia::BRDAllocator* allocator = nullptr;
     RecyclingClippingPixelAllocator recycleAlloc(recycledBitmap, recycledBytes);
     HeapAllocator heapAlloc;
     if (javaBitmap) {
@@ -221,20 +207,17 @@
 }
 
 static jint nativeGetHeight(JNIEnv* env, jobject, jlong brdHandle) {
-    SkBitmapRegionDecoder* brd =
-            reinterpret_cast<SkBitmapRegionDecoder*>(brdHandle);
+    auto* brd = reinterpret_cast<skia::BitmapRegionDecoder*>(brdHandle);
     return static_cast<jint>(brd->height());
 }
 
 static jint nativeGetWidth(JNIEnv* env, jobject, jlong brdHandle) {
-    SkBitmapRegionDecoder* brd =
-            reinterpret_cast<SkBitmapRegionDecoder*>(brdHandle);
+    auto* brd = reinterpret_cast<skia::BitmapRegionDecoder*>(brdHandle);
     return static_cast<jint>(brd->width());
 }
 
 static void nativeClean(JNIEnv* env, jobject, jlong brdHandle) {
-    SkBitmapRegionDecoder* brd =
-            reinterpret_cast<SkBitmapRegionDecoder*>(brdHandle);
+    auto* brd = reinterpret_cast<skia::BitmapRegionDecoder*>(brdHandle);
     delete brd;
 }