diff options
author | 2020-05-04 15:38:58 -0400 | |
---|---|---|
committer | 2020-05-21 13:42:38 -0400 | |
commit | 23ac036a66bb002b92b48584fee13bd41dd15651 (patch) | |
tree | ddefa52e6a006afbb96f6bf3b8f5133d229caed2 | |
parent | 3e53fece5f655d5a4c95313dbc421056ba19d72a (diff) |
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
-rw-r--r-- | libs/hwui/jni/BitmapFactory.cpp | 1 | ||||
-rw-r--r-- | libs/hwui/jni/BitmapRegionDecoder.cpp | 53 | ||||
-rw-r--r-- | libs/hwui/jni/CreateJavaOutputStreamAdaptor.cpp | 21 | ||||
-rw-r--r-- | libs/hwui/jni/CreateJavaOutputStreamAdaptor.h | 8 | ||||
-rw-r--r-- | libs/hwui/jni/Graphics.cpp | 2 | ||||
-rw-r--r-- | libs/hwui/jni/GraphicsJNI.h | 15 | ||||
-rw-r--r-- | libs/hwui/jni/Utils.cpp | 4 | ||||
-rw-r--r-- | libs/hwui/jni/Utils.h | 5 |
8 files changed, 43 insertions, 66 deletions
diff --git a/libs/hwui/jni/BitmapFactory.cpp b/libs/hwui/jni/BitmapFactory.cpp index 09196cf5f096..0113b28d5e45 100644 --- a/libs/hwui/jni/BitmapFactory.cpp +++ b/libs/hwui/jni/BitmapFactory.cpp @@ -8,7 +8,6 @@ #include "MimeType.h" #include "NinePatchPeeker.h" #include "SkAndroidCodec.h" -#include "SkBRDAllocator.h" #include "SkMath.h" #include "SkPixelRef.h" #include "SkStream.h" diff --git a/libs/hwui/jni/BitmapRegionDecoder.cpp b/libs/hwui/jni/BitmapRegionDecoder.cpp index 3c427f91cefe..4cc05ef6f13b 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 createBitmapRegionDecoder(JNIEnv* env, std::unique_ptr<SkStreamRe 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 @@ static jobject nativeNewInstanceFromFileDescriptor(JNIEnv* env, jobject clazz, 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 @@ static jobject nativeDecodeRegion(JNIEnv* env, jobject, jlong brdHandle, jint in 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 @@ static jobject nativeDecodeRegion(JNIEnv* env, jobject, jlong brdHandle, jint in } // 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 jobject nativeDecodeRegion(JNIEnv* env, jobject, jlong brdHandle, jint in } 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; } diff --git a/libs/hwui/jni/CreateJavaOutputStreamAdaptor.cpp b/libs/hwui/jni/CreateJavaOutputStreamAdaptor.cpp index f1c6b29204b2..785a5dc995ab 100644 --- a/libs/hwui/jni/CreateJavaOutputStreamAdaptor.cpp +++ b/libs/hwui/jni/CreateJavaOutputStreamAdaptor.cpp @@ -177,8 +177,12 @@ SkStream* CreateJavaInputStreamAdaptor(JNIEnv* env, jobject stream, jbyteArray s return JavaInputStreamAdaptor::Create(env, stream, storage, swallowExceptions); } -static SkMemoryStream* adaptor_to_mem_stream(SkStream* stream) { - SkASSERT(stream != NULL); +sk_sp<SkData> CopyJavaInputStream(JNIEnv* env, jobject inputStream, jbyteArray storage) { + std::unique_ptr<SkStream> stream(CreateJavaInputStreamAdaptor(env, inputStream, storage)); + if (!stream) { + return nullptr; + } + size_t bufferSize = 4096; size_t streamLen = 0; size_t len; @@ -194,18 +198,7 @@ static SkMemoryStream* adaptor_to_mem_stream(SkStream* stream) { } data = (char*)sk_realloc_throw(data, streamLen); - SkMemoryStream* streamMem = new SkMemoryStream(); - streamMem->setMemoryOwned(data, streamLen); - return streamMem; -} - -SkStreamRewindable* CopyJavaInputStream(JNIEnv* env, jobject stream, - jbyteArray storage) { - std::unique_ptr<SkStream> adaptor(CreateJavaInputStreamAdaptor(env, stream, storage)); - if (NULL == adaptor.get()) { - return NULL; - } - return adaptor_to_mem_stream(adaptor.get()); + return SkData::MakeFromMalloc(data, streamLen); } /////////////////////////////////////////////////////////////////////////////// diff --git a/libs/hwui/jni/CreateJavaOutputStreamAdaptor.h b/libs/hwui/jni/CreateJavaOutputStreamAdaptor.h index 849418da01a1..bae40f1e8d2f 100644 --- a/libs/hwui/jni/CreateJavaOutputStreamAdaptor.h +++ b/libs/hwui/jni/CreateJavaOutputStreamAdaptor.h @@ -2,6 +2,7 @@ #define _ANDROID_GRAPHICS_CREATE_JAVA_OUTPUT_STREAM_ADAPTOR_H_ #include "jni.h" +#include "SkData.h" class SkMemoryStream; class SkStream; @@ -27,15 +28,14 @@ SkStream* CreateJavaInputStreamAdaptor(JNIEnv* env, jobject stream, jbyteArray s bool swallowExceptions = true); /** - * Copy a Java InputStream. The result will be rewindable. + * Copy a Java InputStream to an SkData. * @param env JNIEnv object. * @param stream Pointer to Java InputStream. * @param storage Java byte array for retrieving data from the * Java InputStream. - * @return SkStreamRewindable The data in stream will be copied - * to a new SkStreamRewindable. + * @return SkData containing the stream's data. */ -SkStreamRewindable* CopyJavaInputStream(JNIEnv* env, jobject stream, jbyteArray storage); +sk_sp<SkData> CopyJavaInputStream(JNIEnv* env, jobject stream, jbyteArray storage); SkWStream* CreateJavaOutputStreamAdaptor(JNIEnv* env, jobject stream, jbyteArray storage); diff --git a/libs/hwui/jni/Graphics.cpp b/libs/hwui/jni/Graphics.cpp index f76ecb4c9c8a..ecbb55ec878d 100644 --- a/libs/hwui/jni/Graphics.cpp +++ b/libs/hwui/jni/Graphics.cpp @@ -470,7 +470,7 @@ SkRegion* GraphicsJNI::getNativeRegion(JNIEnv* env, jobject region) /////////////////////////////////////////////////////////////////////////////////////////// -jobject GraphicsJNI::createBitmapRegionDecoder(JNIEnv* env, SkBitmapRegionDecoder* bitmap) +jobject GraphicsJNI::createBitmapRegionDecoder(JNIEnv* env, skia::BitmapRegionDecoder* bitmap) { ALOG_ASSERT(bitmap != NULL); diff --git a/libs/hwui/jni/GraphicsJNI.h b/libs/hwui/jni/GraphicsJNI.h index b58a740a4c27..79ab617411e3 100644 --- a/libs/hwui/jni/GraphicsJNI.h +++ b/libs/hwui/jni/GraphicsJNI.h @@ -4,8 +4,8 @@ #include <cutils/compiler.h> #include "Bitmap.h" +#include "BRDAllocator.h" #include "SkBitmap.h" -#include "SkBRDAllocator.h" #include "SkCodec.h" #include "SkPixelRef.h" #include "SkMallocPixelRef.h" @@ -17,10 +17,12 @@ #include "graphics_jni_helpers.h" -class SkBitmapRegionDecoder; class SkCanvas; namespace android { +namespace skia { + class BitmapRegionDecoder; +} class Paint; struct Typeface; } @@ -103,7 +105,8 @@ public: static jobject createRegion(JNIEnv* env, SkRegion* region); - static jobject createBitmapRegionDecoder(JNIEnv* env, SkBitmapRegionDecoder* bitmap); + static jobject createBitmapRegionDecoder(JNIEnv* env, + android::skia::BitmapRegionDecoder* bitmap); /** * Given a bitmap we natively allocate a memory block to store the contents @@ -154,7 +157,7 @@ private: static JavaVM* mJavaVM; }; -class HeapAllocator : public SkBRDAllocator { +class HeapAllocator : public android::skia::BRDAllocator { public: HeapAllocator() { }; ~HeapAllocator() { }; @@ -181,7 +184,7 @@ private: * the decoded output to fit in the recycled bitmap if necessary. * This allocator implements that behavior. * - * Skia's SkBitmapRegionDecoder expects the memory that + * Skia's BitmapRegionDecoder expects the memory that * is allocated to be large enough to decode the entire region * that is requested. It will decode directly into the memory * that is provided. @@ -200,7 +203,7 @@ private: * reuse it again, given that it still may be in use from our * first allocation. */ -class RecyclingClippingPixelAllocator : public SkBRDAllocator { +class RecyclingClippingPixelAllocator : public android::skia::BRDAllocator { public: RecyclingClippingPixelAllocator(android::Bitmap* recycledBitmap, diff --git a/libs/hwui/jni/Utils.cpp b/libs/hwui/jni/Utils.cpp index 34fd6687d52c..ac2f5b77d23a 100644 --- a/libs/hwui/jni/Utils.cpp +++ b/libs/hwui/jni/Utils.cpp @@ -114,7 +114,7 @@ size_t AssetStreamAdaptor::read(void* buffer, size_t size) { return amount; } -SkMemoryStream* android::CopyAssetToStream(Asset* asset) { +sk_sp<SkData> android::CopyAssetToData(Asset* asset) { if (NULL == asset) { return NULL; } @@ -138,7 +138,7 @@ SkMemoryStream* android::CopyAssetToStream(Asset* asset) { return NULL; } - return new SkMemoryStream(std::move(data)); + return data; } jobject android::nullObjectReturn(const char msg[]) { diff --git a/libs/hwui/jni/Utils.h b/libs/hwui/jni/Utils.h index f628cc3c85ed..6cdf44d85a5a 100644 --- a/libs/hwui/jni/Utils.h +++ b/libs/hwui/jni/Utils.h @@ -46,12 +46,11 @@ private: }; /** - * Make a deep copy of the asset, and return it as a stream, or NULL if there + * Make a deep copy of the asset, and return it as an SkData, or NULL if there * was an error. - * FIXME: If we could "ref/reopen" the asset, we may not need to copy it here. */ -SkMemoryStream* CopyAssetToStream(Asset*); +sk_sp<SkData> CopyAssetToData(Asset*); /** Restore the file descriptor's offset in our destructor */ |