Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "Bitmap.h" |
Leon Scroggins III | 1fad09d | 2017-12-22 12:54:20 -0500 | [diff] [blame] | 18 | #include "BitmapFactory.h" |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 19 | #include "ByteBufferStreamAdaptor.h" |
Leon Scroggins III | ed074fd | 2017-12-11 13:47:23 -0500 | [diff] [blame] | 20 | #include "CreateJavaOutputStreamAdaptor.h" |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 21 | #include "GraphicsJNI.h" |
Leon Scroggins III | 671cce2 | 2018-01-14 16:52:17 -0500 | [diff] [blame] | 22 | #include "ImageDecoder.h" |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 23 | #include "NinePatchPeeker.h" |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 24 | #include "Utils.h" |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 25 | |
| 26 | #include <hwui/Bitmap.h> |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 27 | #include <hwui/ImageDecoder.h> |
Leon Scroggins III | ee3bfe7 | 2019-01-31 08:42:23 -0500 | [diff] [blame] | 28 | #include <HardwareBitmapUploader.h> |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 29 | |
| 30 | #include <SkAndroidCodec.h> |
| 31 | #include <SkEncodedImageFormat.h> |
Leon Scroggins III | ed074fd | 2017-12-11 13:47:23 -0500 | [diff] [blame] | 32 | #include <SkFrontBufferedStream.h> |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 33 | #include <SkStream.h> |
| 34 | |
| 35 | #include <androidfw/Asset.h> |
Derek Sollenberger | c5882c4 | 2019-10-25 11:11:32 -0400 | [diff] [blame^] | 36 | #include <fcntl.h> |
Leon Scroggins III | ed074fd | 2017-12-11 13:47:23 -0500 | [diff] [blame] | 37 | #include <sys/stat.h> |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 38 | |
| 39 | using namespace android; |
| 40 | |
| 41 | static jclass gImageDecoder_class; |
Leon Scroggins III | e5de9aa | 2018-01-10 20:56:51 -0500 | [diff] [blame] | 42 | static jclass gSize_class; |
Leon Scroggins III | 1d2bf2b | 2018-03-14 16:07:43 -0400 | [diff] [blame] | 43 | static jclass gDecodeException_class; |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 44 | static jclass gCanvas_class; |
| 45 | static jmethodID gImageDecoder_constructorMethodID; |
Leon Scroggins III | 8c9d8f2 | 2018-01-14 14:41:46 -0500 | [diff] [blame] | 46 | static jmethodID gImageDecoder_postProcessMethodID; |
Leon Scroggins III | e5de9aa | 2018-01-10 20:56:51 -0500 | [diff] [blame] | 47 | static jmethodID gSize_constructorMethodID; |
Leon Scroggins III | 1d2bf2b | 2018-03-14 16:07:43 -0400 | [diff] [blame] | 48 | static jmethodID gDecodeException_constructorMethodID; |
Leon Scroggins III | edf26d6 | 2018-01-09 16:55:24 -0500 | [diff] [blame] | 49 | static jmethodID gCallback_onPartialImageMethodID; |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 50 | static jmethodID gCanvas_constructorMethodID; |
| 51 | static jmethodID gCanvas_releaseMethodID; |
| 52 | |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 53 | // These need to stay in sync with ImageDecoder.java's Allocator constants. |
| 54 | enum Allocator { |
| 55 | kDefault_Allocator = 0, |
| 56 | kSoftware_Allocator = 1, |
| 57 | kSharedMemory_Allocator = 2, |
| 58 | kHardware_Allocator = 3, |
| 59 | }; |
| 60 | |
| 61 | // These need to stay in sync with ImageDecoder.java's Error constants. |
| 62 | enum Error { |
| 63 | kSourceException = 1, |
| 64 | kSourceIncomplete = 2, |
| 65 | kSourceMalformedData = 3, |
| 66 | }; |
| 67 | |
| 68 | // These need to stay in sync with PixelFormat.java's Format constants. |
| 69 | enum PixelFormat { |
| 70 | kUnknown = 0, |
| 71 | kTranslucent = -3, |
| 72 | kOpaque = -1, |
| 73 | }; |
| 74 | |
Leon Scroggins III | 1d2bf2b | 2018-03-14 16:07:43 -0400 | [diff] [blame] | 75 | // Clear and return any pending exception for handling other than throwing directly. |
| 76 | static jthrowable get_and_clear_exception(JNIEnv* env) { |
| 77 | jthrowable jexception = env->ExceptionOccurred(); |
| 78 | if (jexception) { |
| 79 | env->ExceptionClear(); |
| 80 | } |
| 81 | return jexception; |
| 82 | } |
| 83 | |
| 84 | // Throw a new ImageDecoder.DecodeException. Returns null for convenience. |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 85 | static jobject throw_exception(JNIEnv* env, Error error, const char* msg, |
Leon Scroggins III | 1d2bf2b | 2018-03-14 16:07:43 -0400 | [diff] [blame] | 86 | jthrowable cause, jobject source) { |
| 87 | jstring jstr = nullptr; |
| 88 | if (msg) { |
| 89 | jstr = env->NewStringUTF(msg); |
| 90 | if (!jstr) { |
| 91 | // Out of memory. |
| 92 | return nullptr; |
| 93 | } |
| 94 | } |
| 95 | jthrowable exception = (jthrowable) env->NewObject(gDecodeException_class, |
| 96 | gDecodeException_constructorMethodID, error, jstr, cause, source); |
| 97 | // Only throw if not out of memory. |
| 98 | if (exception) { |
| 99 | env->Throw(exception); |
| 100 | } |
| 101 | return nullptr; |
| 102 | } |
| 103 | |
Chong Zhang | cba2792 | 2019-07-12 16:38:47 -0700 | [diff] [blame] | 104 | static jobject native_create(JNIEnv* env, std::unique_ptr<SkStream> stream, |
| 105 | jobject source, jboolean preferAnimation) { |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 106 | if (!stream.get()) { |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 107 | return throw_exception(env, kSourceMalformedData, "Failed to create a stream", |
Leon Scroggins III | 1d2bf2b | 2018-03-14 16:07:43 -0400 | [diff] [blame] | 108 | nullptr, source); |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 109 | } |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 110 | sk_sp<NinePatchPeeker> peeker(new NinePatchPeeker); |
Leon Scroggins III | c782ad8 | 2018-01-14 10:50:45 -0500 | [diff] [blame] | 111 | SkCodec::Result result; |
Chong Zhang | cba2792 | 2019-07-12 16:38:47 -0700 | [diff] [blame] | 112 | auto codec = SkCodec::MakeFromStream( |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 113 | std::move(stream), &result, peeker.get(), |
Chong Zhang | cba2792 | 2019-07-12 16:38:47 -0700 | [diff] [blame] | 114 | preferAnimation ? SkCodec::SelectionPolicy::kPreferAnimation |
| 115 | : SkCodec::SelectionPolicy::kPreferStillImage); |
Leon Scroggins III | 1d2bf2b | 2018-03-14 16:07:43 -0400 | [diff] [blame] | 116 | if (jthrowable jexception = get_and_clear_exception(env)) { |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 117 | return throw_exception(env, kSourceException, "", jexception, source); |
Leon Scroggins III | 1d2bf2b | 2018-03-14 16:07:43 -0400 | [diff] [blame] | 118 | } |
Leon Scroggins III | c782ad8 | 2018-01-14 10:50:45 -0500 | [diff] [blame] | 119 | if (!codec) { |
| 120 | switch (result) { |
| 121 | case SkCodec::kIncompleteInput: |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 122 | return throw_exception(env, kSourceIncomplete, "", nullptr, source); |
Leon Scroggins III | c782ad8 | 2018-01-14 10:50:45 -0500 | [diff] [blame] | 123 | default: |
| 124 | SkString msg; |
| 125 | msg.printf("Failed to create image decoder with message '%s'", |
| 126 | SkCodec::ResultToString(result)); |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 127 | return throw_exception(env, kSourceMalformedData, msg.c_str(), |
Leon Scroggins III | cf7294f | 2018-03-22 09:21:29 -0400 | [diff] [blame] | 128 | nullptr, source); |
Leon Scroggins III | c782ad8 | 2018-01-14 10:50:45 -0500 | [diff] [blame] | 129 | |
Leon Scroggins III | 671cce2 | 2018-01-14 16:52:17 -0500 | [diff] [blame] | 130 | } |
Leon Scroggins III | c782ad8 | 2018-01-14 10:50:45 -0500 | [diff] [blame] | 131 | } |
| 132 | |
Leon Scroggins III | 671cce2 | 2018-01-14 16:52:17 -0500 | [diff] [blame] | 133 | const bool animated = codec->getFrameCount() > 1; |
Leon Scroggins III | 1d2bf2b | 2018-03-14 16:07:43 -0400 | [diff] [blame] | 134 | if (jthrowable jexception = get_and_clear_exception(env)) { |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 135 | return throw_exception(env, kSourceException, "", jexception, source); |
Leon Scroggins III | 1d2bf2b | 2018-03-14 16:07:43 -0400 | [diff] [blame] | 136 | } |
| 137 | |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 138 | auto androidCodec = SkAndroidCodec::MakeFromCodec(std::move(codec), |
Leon Scroggins III | ac1928b | 2018-01-17 11:34:43 -0500 | [diff] [blame] | 139 | SkAndroidCodec::ExifOrientationBehavior::kRespect); |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 140 | if (!androidCodec.get()) { |
| 141 | return throw_exception(env, kSourceMalformedData, "", nullptr, source); |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 142 | } |
Leon Scroggins III | 1d2bf2b | 2018-03-14 16:07:43 -0400 | [diff] [blame] | 143 | |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 144 | const auto& info = androidCodec->getInfo(); |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 145 | const int width = info.width(); |
| 146 | const int height = info.height(); |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 147 | const bool isNinePatch = peeker->mPatch != nullptr; |
| 148 | ImageDecoder* decoder = new ImageDecoder(std::move(androidCodec), std::move(peeker)); |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 149 | return env->NewObject(gImageDecoder_class, gImageDecoder_constructorMethodID, |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 150 | reinterpret_cast<jlong>(decoder), width, height, |
Leon Scroggins III | 7d940ba | 2018-04-04 16:19:33 -0400 | [diff] [blame] | 151 | animated, isNinePatch); |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 152 | } |
| 153 | |
Leon Scroggins III | ed074fd | 2017-12-11 13:47:23 -0500 | [diff] [blame] | 154 | static jobject ImageDecoder_nCreateFd(JNIEnv* env, jobject /*clazz*/, |
Chong Zhang | cba2792 | 2019-07-12 16:38:47 -0700 | [diff] [blame] | 155 | jobject fileDescriptor, jboolean preferAnimation, jobject source) { |
Derek Sollenberger | c5882c4 | 2019-10-25 11:11:32 -0400 | [diff] [blame^] | 156 | #ifndef __ANDROID__ // LayoutLib for Windows does not support F_DUPFD_CLOEXEC |
| 157 | return throw_exception(env, kSourceException, "Only supported on Android", nullptr, source); |
| 158 | #else |
Leon Scroggins III | ed074fd | 2017-12-11 13:47:23 -0500 | [diff] [blame] | 159 | int descriptor = jniGetFDFromFileDescriptor(env, fileDescriptor); |
| 160 | |
| 161 | struct stat fdStat; |
| 162 | if (fstat(descriptor, &fdStat) == -1) { |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 163 | return throw_exception(env, kSourceMalformedData, |
Leon Scroggins III | 1d2bf2b | 2018-03-14 16:07:43 -0400 | [diff] [blame] | 164 | "broken file descriptor; fstat returned -1", nullptr, source); |
Leon Scroggins III | ed074fd | 2017-12-11 13:47:23 -0500 | [diff] [blame] | 165 | } |
| 166 | |
Nick Kralevich | 4b3a08c | 2019-01-28 10:39:10 -0800 | [diff] [blame] | 167 | int dupDescriptor = fcntl(descriptor, F_DUPFD_CLOEXEC, 0); |
Leon Scroggins III | ed074fd | 2017-12-11 13:47:23 -0500 | [diff] [blame] | 168 | FILE* file = fdopen(dupDescriptor, "r"); |
| 169 | if (file == NULL) { |
| 170 | close(dupDescriptor); |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 171 | return throw_exception(env, kSourceMalformedData, "Could not open file", |
Leon Scroggins III | cf7294f | 2018-03-22 09:21:29 -0400 | [diff] [blame] | 172 | nullptr, source); |
Leon Scroggins III | ed074fd | 2017-12-11 13:47:23 -0500 | [diff] [blame] | 173 | } |
Leon Scroggins III | 0c699ad | 2018-05-07 16:20:13 -0400 | [diff] [blame] | 174 | |
Leon Scroggins III | ed074fd | 2017-12-11 13:47:23 -0500 | [diff] [blame] | 175 | std::unique_ptr<SkFILEStream> fileStream(new SkFILEStream(file)); |
Chong Zhang | cba2792 | 2019-07-12 16:38:47 -0700 | [diff] [blame] | 176 | return native_create(env, std::move(fileStream), source, preferAnimation); |
Derek Sollenberger | c5882c4 | 2019-10-25 11:11:32 -0400 | [diff] [blame^] | 177 | #endif |
Leon Scroggins III | ed074fd | 2017-12-11 13:47:23 -0500 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | static jobject ImageDecoder_nCreateInputStream(JNIEnv* env, jobject /*clazz*/, |
Chong Zhang | cba2792 | 2019-07-12 16:38:47 -0700 | [diff] [blame] | 181 | jobject is, jbyteArray storage, jboolean preferAnimation, jobject source) { |
Leon Scroggins III | ed074fd | 2017-12-11 13:47:23 -0500 | [diff] [blame] | 182 | std::unique_ptr<SkStream> stream(CreateJavaInputStreamAdaptor(env, is, storage, false)); |
| 183 | |
| 184 | if (!stream.get()) { |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 185 | return throw_exception(env, kSourceMalformedData, "Failed to create a stream", |
Leon Scroggins III | 1d2bf2b | 2018-03-14 16:07:43 -0400 | [diff] [blame] | 186 | nullptr, source); |
Leon Scroggins III | ed074fd | 2017-12-11 13:47:23 -0500 | [diff] [blame] | 187 | } |
Leon Scroggins III | 1d2bf2b | 2018-03-14 16:07:43 -0400 | [diff] [blame] | 188 | |
Leon Scroggins III | ed074fd | 2017-12-11 13:47:23 -0500 | [diff] [blame] | 189 | std::unique_ptr<SkStream> bufferedStream( |
| 190 | SkFrontBufferedStream::Make(std::move(stream), |
| 191 | SkCodec::MinBufferedBytesNeeded())); |
Chong Zhang | cba2792 | 2019-07-12 16:38:47 -0700 | [diff] [blame] | 192 | return native_create(env, std::move(bufferedStream), source, preferAnimation); |
Leon Scroggins III | ed074fd | 2017-12-11 13:47:23 -0500 | [diff] [blame] | 193 | } |
| 194 | |
Chong Zhang | cba2792 | 2019-07-12 16:38:47 -0700 | [diff] [blame] | 195 | static jobject ImageDecoder_nCreateAsset(JNIEnv* env, jobject /*clazz*/, |
| 196 | jlong assetPtr, jboolean preferAnimation, jobject source) { |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 197 | Asset* asset = reinterpret_cast<Asset*>(assetPtr); |
| 198 | std::unique_ptr<SkStream> stream(new AssetStreamAdaptor(asset)); |
Chong Zhang | cba2792 | 2019-07-12 16:38:47 -0700 | [diff] [blame] | 199 | return native_create(env, std::move(stream), source, preferAnimation); |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 200 | } |
| 201 | |
Chong Zhang | cba2792 | 2019-07-12 16:38:47 -0700 | [diff] [blame] | 202 | static jobject ImageDecoder_nCreateByteBuffer(JNIEnv* env, jobject /*clazz*/, |
| 203 | jobject jbyteBuffer, jint initialPosition, jint limit, |
| 204 | jboolean preferAnimation, jobject source) { |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 205 | std::unique_ptr<SkStream> stream = CreateByteBufferStreamAdaptor(env, jbyteBuffer, |
| 206 | initialPosition, limit); |
| 207 | if (!stream) { |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 208 | return throw_exception(env, kSourceMalformedData, "Failed to read ByteBuffer", |
Leon Scroggins III | 1d2bf2b | 2018-03-14 16:07:43 -0400 | [diff] [blame] | 209 | nullptr, source); |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 210 | } |
Chong Zhang | cba2792 | 2019-07-12 16:38:47 -0700 | [diff] [blame] | 211 | return native_create(env, std::move(stream), source, preferAnimation); |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 212 | } |
| 213 | |
Chong Zhang | cba2792 | 2019-07-12 16:38:47 -0700 | [diff] [blame] | 214 | static jobject ImageDecoder_nCreateByteArray(JNIEnv* env, jobject /*clazz*/, |
| 215 | jbyteArray byteArray, jint offset, jint length, |
| 216 | jboolean preferAnimation, jobject source) { |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 217 | std::unique_ptr<SkStream> stream(CreateByteArrayStreamAdaptor(env, byteArray, offset, length)); |
Chong Zhang | cba2792 | 2019-07-12 16:38:47 -0700 | [diff] [blame] | 218 | return native_create(env, std::move(stream), source, preferAnimation); |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 219 | } |
| 220 | |
Leon Scroggins III | e5de9aa | 2018-01-10 20:56:51 -0500 | [diff] [blame] | 221 | jint postProcessAndRelease(JNIEnv* env, jobject jimageDecoder, std::unique_ptr<Canvas> canvas) { |
Leon Scroggins III | 8c9d8f2 | 2018-01-14 14:41:46 -0500 | [diff] [blame] | 222 | jobject jcanvas = env->NewObject(gCanvas_class, gCanvas_constructorMethodID, |
| 223 | reinterpret_cast<jlong>(canvas.get())); |
| 224 | if (!jcanvas) { |
| 225 | doThrowOOME(env, "Failed to create Java Canvas for PostProcess!"); |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 226 | return kUnknown; |
Leon Scroggins III | 8c9d8f2 | 2018-01-14 14:41:46 -0500 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | // jcanvas now owns canvas. |
| 230 | canvas.release(); |
| 231 | |
Leon Scroggins III | e5de9aa | 2018-01-10 20:56:51 -0500 | [diff] [blame] | 232 | return env->CallIntMethod(jimageDecoder, gImageDecoder_postProcessMethodID, jcanvas); |
Leon Scroggins III | 8c9d8f2 | 2018-01-14 14:41:46 -0500 | [diff] [blame] | 233 | } |
| 234 | |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 235 | static jobject ImageDecoder_nDecodeBitmap(JNIEnv* env, jobject /*clazz*/, jlong nativePtr, |
Leon Scroggins III | 1d2bf2b | 2018-03-14 16:07:43 -0400 | [diff] [blame] | 236 | jobject jdecoder, jboolean jpostProcess, |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 237 | jint targetWidth, jint targetHeight, jobject jsubset, |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 238 | jboolean requireMutable, jint allocator, |
| 239 | jboolean requireUnpremul, jboolean preferRamOverQuality, |
Leon Scroggins III | 28f3943f | 2019-02-19 11:03:44 -0500 | [diff] [blame] | 240 | jboolean asAlphaMask, jlong colorSpaceHandle, |
| 241 | jboolean extended) { |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 242 | auto* decoder = reinterpret_cast<ImageDecoder*>(nativePtr); |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 243 | if (!decoder->setTargetSize(targetWidth, targetHeight)) { |
| 244 | doThrowISE(env, "Could not scale to target size!"); |
| 245 | return nullptr; |
| 246 | } |
Leon Scroggins III | 1ade46d | 2020-01-15 05:41:06 -0500 | [diff] [blame] | 247 | if (requireUnpremul && !decoder->setUnpremultipliedRequired(true)) { |
Leon Scroggins III | ea978db | 2018-01-13 11:40:42 -0500 | [diff] [blame] | 248 | doThrowISE(env, "Cannot scale unpremultiplied pixels!"); |
| 249 | return nullptr; |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 250 | } |
| 251 | |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 252 | SkColorType colorType = kN32_SkColorType; |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 253 | if (asAlphaMask && decoder->gray()) { |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 254 | // We have to trick Skia to decode this to a single channel. |
| 255 | colorType = kGray_8_SkColorType; |
| 256 | } else if (preferRamOverQuality) { |
| 257 | // FIXME: The post-process might add alpha, which would make a 565 |
| 258 | // result incorrect. If we call the postProcess before now and record |
| 259 | // to a picture, we can know whether alpha was added, and if not, we |
| 260 | // can still use 565. |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 261 | if (decoder->opaque() && !jpostProcess) { |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 262 | // If the final result will be hardware, decoding to 565 and then |
| 263 | // uploading to the gpu as 8888 will not save memory. This still |
| 264 | // may save us from using F16, but do not go down to 565. |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 265 | if (allocator != kHardware_Allocator && |
| 266 | (allocator != kDefault_Allocator || requireMutable)) { |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 267 | colorType = kRGB_565_SkColorType; |
| 268 | } |
| 269 | } |
| 270 | // Otherwise, stick with N32 |
Leon Scroggins III | 28f3943f | 2019-02-19 11:03:44 -0500 | [diff] [blame] | 271 | } else if (extended) { |
| 272 | colorType = kRGBA_F16_SkColorType; |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 273 | } else { |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 274 | colorType = decoder->mCodec->computeOutputColorType(colorType); |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 275 | } |
Leon Scroggins III | ee3bfe7 | 2019-01-31 08:42:23 -0500 | [diff] [blame] | 276 | |
| 277 | const bool isHardware = !requireMutable |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 278 | && (allocator == kDefault_Allocator || |
| 279 | allocator == kHardware_Allocator) |
Leon Scroggins III | ee3bfe7 | 2019-01-31 08:42:23 -0500 | [diff] [blame] | 280 | && colorType != kGray_8_SkColorType; |
| 281 | |
| 282 | if (colorType == kRGBA_F16_SkColorType && isHardware && |
| 283 | !uirenderer::HardwareBitmapUploader::hasFP16Support()) { |
| 284 | colorType = kN32_SkColorType; |
| 285 | } |
| 286 | |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 287 | if (!decoder->setOutColorType(colorType)) { |
| 288 | doThrowISE(env, "Failed to set out color type!"); |
| 289 | return nullptr; |
| 290 | } |
| 291 | |
| 292 | { |
| 293 | sk_sp<SkColorSpace> colorSpace = GraphicsJNI::getNativeColorSpace(colorSpaceHandle); |
| 294 | colorSpace = decoder->mCodec->computeOutputColorSpace(colorType, colorSpace); |
| 295 | decoder->setOutColorSpace(std::move(colorSpace)); |
| 296 | } |
| 297 | |
| 298 | if (jsubset) { |
| 299 | SkIRect subset; |
| 300 | GraphicsJNI::jrect_to_irect(env, jsubset, &subset); |
| 301 | if (!decoder->setCropRect(&subset)) { |
| 302 | doThrowISE(env, "Invalid crop rect!"); |
| 303 | return nullptr; |
| 304 | } |
| 305 | } |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 306 | |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 307 | SkImageInfo bitmapInfo = decoder->getOutputInfo(); |
Leon Scroggins III | 1ade46d | 2020-01-15 05:41:06 -0500 | [diff] [blame] | 308 | if (decoder->opaque()) { |
| 309 | bitmapInfo = bitmapInfo.makeAlphaType(kOpaque_SkAlphaType); |
| 310 | } |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 311 | if (asAlphaMask && colorType == kGray_8_SkColorType) { |
| 312 | bitmapInfo = bitmapInfo.makeColorType(kAlpha_8_SkColorType); |
| 313 | } |
Leon Scroggins III | 1ade46d | 2020-01-15 05:41:06 -0500 | [diff] [blame] | 314 | |
| 315 | SkBitmap bm; |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 316 | if (!bm.setInfo(bitmapInfo)) { |
Leon Scroggins III | ed074fd | 2017-12-11 13:47:23 -0500 | [diff] [blame] | 317 | doThrowIOE(env, "Failed to setInfo properly"); |
| 318 | return nullptr; |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | sk_sp<Bitmap> nativeBitmap; |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 322 | if (allocator == kSharedMemory_Allocator) { |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 323 | nativeBitmap = Bitmap::allocateAshmemBitmap(&bm); |
| 324 | } else { |
| 325 | nativeBitmap = Bitmap::allocateHeapBitmap(&bm); |
| 326 | } |
| 327 | if (!nativeBitmap) { |
Leon Scroggins III | ed074fd | 2017-12-11 13:47:23 -0500 | [diff] [blame] | 328 | SkString msg; |
| 329 | msg.printf("OOM allocating Bitmap with dimensions %i x %i", |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 330 | bitmapInfo.width(), bitmapInfo.height()); |
Leon Scroggins III | ed074fd | 2017-12-11 13:47:23 -0500 | [diff] [blame] | 331 | doThrowOOME(env, msg.c_str()); |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 332 | return nullptr; |
| 333 | } |
| 334 | |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 335 | SkCodec::Result result = decoder->decode(bm.getPixels(), bm.rowBytes()); |
Leon Scroggins III | 1d2bf2b | 2018-03-14 16:07:43 -0400 | [diff] [blame] | 336 | jthrowable jexception = get_and_clear_exception(env); |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 337 | int onPartialImageError = jexception ? kSourceException |
Leon Scroggins III | e5de9aa | 2018-01-10 20:56:51 -0500 | [diff] [blame] | 338 | : 0; // No error. |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 339 | switch (result) { |
| 340 | case SkCodec::kSuccess: |
Leon Scroggins III | ed074fd | 2017-12-11 13:47:23 -0500 | [diff] [blame] | 341 | // Ignore the exception, since the decode was successful anyway. |
| 342 | jexception = nullptr; |
Leon Scroggins III | e5de9aa | 2018-01-10 20:56:51 -0500 | [diff] [blame] | 343 | onPartialImageError = 0; |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 344 | break; |
| 345 | case SkCodec::kIncompleteInput: |
Leon Scroggins III | e5de9aa | 2018-01-10 20:56:51 -0500 | [diff] [blame] | 346 | if (!jexception) { |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 347 | onPartialImageError = kSourceIncomplete; |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 348 | } |
| 349 | break; |
| 350 | case SkCodec::kErrorInInput: |
Leon Scroggins III | e5de9aa | 2018-01-10 20:56:51 -0500 | [diff] [blame] | 351 | if (!jexception) { |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 352 | onPartialImageError = kSourceMalformedData; |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 353 | } |
| 354 | break; |
| 355 | default: |
Leon Scroggins III | ed074fd | 2017-12-11 13:47:23 -0500 | [diff] [blame] | 356 | SkString msg; |
Leon Scroggins III | e5de9aa | 2018-01-10 20:56:51 -0500 | [diff] [blame] | 357 | msg.printf("getPixels failed with error %s", SkCodec::ResultToString(result)); |
Leon Scroggins III | ed074fd | 2017-12-11 13:47:23 -0500 | [diff] [blame] | 358 | doThrowIOE(env, msg.c_str()); |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 359 | return nullptr; |
| 360 | } |
| 361 | |
Leon Scroggins III | 1d2bf2b | 2018-03-14 16:07:43 -0400 | [diff] [blame] | 362 | if (onPartialImageError) { |
| 363 | env->CallVoidMethod(jdecoder, gCallback_onPartialImageMethodID, onPartialImageError, |
| 364 | jexception); |
Leon Scroggins III | edf26d6 | 2018-01-09 16:55:24 -0500 | [diff] [blame] | 365 | if (env->ExceptionCheck()) { |
| 366 | return nullptr; |
| 367 | } |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 368 | } |
| 369 | |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 370 | jbyteArray ninePatchChunk = nullptr; |
| 371 | jobject ninePatchInsets = nullptr; |
| 372 | |
| 373 | // Ignore ninepatch when post-processing. |
| 374 | if (!jpostProcess) { |
| 375 | // FIXME: Share more code with BitmapFactory.cpp. |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 376 | auto* peeker = reinterpret_cast<NinePatchPeeker*>(decoder->mPeeker.get()); |
| 377 | if (peeker->mPatch != nullptr) { |
| 378 | size_t ninePatchArraySize = peeker->mPatch->serializedSize(); |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 379 | ninePatchChunk = env->NewByteArray(ninePatchArraySize); |
| 380 | if (ninePatchChunk == nullptr) { |
Leon Scroggins III | ed074fd | 2017-12-11 13:47:23 -0500 | [diff] [blame] | 381 | doThrowOOME(env, "Failed to allocate nine patch chunk."); |
| 382 | return nullptr; |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 383 | } |
| 384 | |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 385 | env->SetByteArrayRegion(ninePatchChunk, 0, peeker->mPatchSize, |
| 386 | reinterpret_cast<jbyte*>(peeker->mPatch)); |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 387 | } |
| 388 | |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 389 | if (peeker->mHasInsets) { |
| 390 | ninePatchInsets = peeker->createNinePatchInsets(env, 1.0f); |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 391 | if (ninePatchInsets == nullptr) { |
Leon Scroggins III | ed074fd | 2017-12-11 13:47:23 -0500 | [diff] [blame] | 392 | doThrowOOME(env, "Failed to allocate nine patch insets."); |
| 393 | return nullptr; |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 394 | } |
| 395 | } |
| 396 | } |
| 397 | |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 398 | if (jpostProcess) { |
| 399 | std::unique_ptr<Canvas> canvas(Canvas::create_canvas(bm)); |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 400 | |
Leon Scroggins III | 1d2bf2b | 2018-03-14 16:07:43 -0400 | [diff] [blame] | 401 | jint pixelFormat = postProcessAndRelease(env, jdecoder, std::move(canvas)); |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 402 | if (env->ExceptionCheck()) { |
| 403 | return nullptr; |
| 404 | } |
| 405 | |
| 406 | SkAlphaType newAlphaType = bm.alphaType(); |
| 407 | switch (pixelFormat) { |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 408 | case kUnknown: |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 409 | break; |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 410 | case kTranslucent: |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 411 | newAlphaType = kPremul_SkAlphaType; |
| 412 | break; |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 413 | case kOpaque: |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 414 | newAlphaType = kOpaque_SkAlphaType; |
| 415 | break; |
| 416 | default: |
Leon Scroggins III | ed074fd | 2017-12-11 13:47:23 -0500 | [diff] [blame] | 417 | SkString msg; |
| 418 | msg.printf("invalid return from postProcess: %i", pixelFormat); |
| 419 | doThrowIAE(env, msg.c_str()); |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 420 | return nullptr; |
| 421 | } |
| 422 | |
| 423 | if (newAlphaType != bm.alphaType()) { |
| 424 | if (!bm.setAlphaType(newAlphaType)) { |
Leon Scroggins III | ed074fd | 2017-12-11 13:47:23 -0500 | [diff] [blame] | 425 | SkString msg; |
| 426 | msg.printf("incompatible return from postProcess: %i", pixelFormat); |
| 427 | doThrowIAE(env, msg.c_str()); |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 428 | return nullptr; |
| 429 | } |
| 430 | nativeBitmap->setAlphaType(newAlphaType); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | int bitmapCreateFlags = 0x0; |
| 435 | if (!requireUnpremul) { |
| 436 | // Even if the image is opaque, setting this flag means that |
| 437 | // if alpha is added (e.g. by PostProcess), it will be marked as |
| 438 | // premultiplied. |
| 439 | bitmapCreateFlags |= bitmap::kBitmapCreateFlag_Premultiplied; |
| 440 | } |
| 441 | |
| 442 | if (requireMutable) { |
| 443 | bitmapCreateFlags |= bitmap::kBitmapCreateFlag_Mutable; |
| 444 | } else { |
Leon Scroggins III | ee3bfe7 | 2019-01-31 08:42:23 -0500 | [diff] [blame] | 445 | if (isHardware) { |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 446 | sk_sp<Bitmap> hwBitmap = Bitmap::allocateHardwareBitmap(bm); |
| 447 | if (hwBitmap) { |
| 448 | hwBitmap->setImmutable(); |
| 449 | return bitmap::createBitmap(env, hwBitmap.release(), bitmapCreateFlags, |
| 450 | ninePatchChunk, ninePatchInsets); |
| 451 | } |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 452 | if (allocator == kHardware_Allocator) { |
Leon Scroggins III | ed074fd | 2017-12-11 13:47:23 -0500 | [diff] [blame] | 453 | doThrowOOME(env, "failed to allocate hardware Bitmap!"); |
| 454 | return nullptr; |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 455 | } |
| 456 | // If we failed to create a hardware bitmap, go ahead and create a |
| 457 | // software one. |
| 458 | } |
| 459 | |
| 460 | nativeBitmap->setImmutable(); |
| 461 | } |
| 462 | return bitmap::createBitmap(env, nativeBitmap.release(), bitmapCreateFlags, ninePatchChunk, |
| 463 | ninePatchInsets); |
| 464 | } |
| 465 | |
| 466 | static jobject ImageDecoder_nGetSampledSize(JNIEnv* env, jobject /*clazz*/, jlong nativePtr, |
| 467 | jint sampleSize) { |
| 468 | auto* decoder = reinterpret_cast<ImageDecoder*>(nativePtr); |
| 469 | SkISize size = decoder->mCodec->getSampledDimensions(sampleSize); |
Leon Scroggins III | e5de9aa | 2018-01-10 20:56:51 -0500 | [diff] [blame] | 470 | return env->NewObject(gSize_class, gSize_constructorMethodID, size.width(), size.height()); |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | static void ImageDecoder_nGetPadding(JNIEnv* env, jobject /*clazz*/, jlong nativePtr, |
| 474 | jobject outPadding) { |
| 475 | auto* decoder = reinterpret_cast<ImageDecoder*>(nativePtr); |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 476 | reinterpret_cast<NinePatchPeeker*>(decoder->mPeeker.get())->getPadding(env, outPadding); |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 477 | } |
| 478 | |
Leon Scroggins III | ed074fd | 2017-12-11 13:47:23 -0500 | [diff] [blame] | 479 | static void ImageDecoder_nClose(JNIEnv* /*env*/, jobject /*clazz*/, jlong nativePtr) { |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 480 | delete reinterpret_cast<ImageDecoder*>(nativePtr); |
| 481 | } |
| 482 | |
Leon Scroggins III | 1fad09d | 2017-12-22 12:54:20 -0500 | [diff] [blame] | 483 | static jstring ImageDecoder_nGetMimeType(JNIEnv* env, jobject /*clazz*/, jlong nativePtr) { |
| 484 | auto* decoder = reinterpret_cast<ImageDecoder*>(nativePtr); |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 485 | return getMimeTypeAsJavaString(env, decoder->mCodec->getEncodedFormat()); |
Leon Scroggins III | 1fad09d | 2017-12-22 12:54:20 -0500 | [diff] [blame] | 486 | } |
| 487 | |
Leon Scroggins III | 1a69f45 | 2018-03-29 09:48:47 -0400 | [diff] [blame] | 488 | static jobject ImageDecoder_nGetColorSpace(JNIEnv* env, jobject /*clazz*/, jlong nativePtr) { |
| 489 | auto* codec = reinterpret_cast<ImageDecoder*>(nativePtr)->mCodec.get(); |
Derek Sollenberger | bf3e464 | 2019-01-30 11:28:27 -0500 | [diff] [blame] | 490 | auto colorType = codec->computeOutputColorType(kN32_SkColorType); |
Leon Scroggins III | 1a69f45 | 2018-03-29 09:48:47 -0400 | [diff] [blame] | 491 | sk_sp<SkColorSpace> colorSpace = codec->computeOutputColorSpace(colorType); |
Derek Sollenberger | bf3e464 | 2019-01-30 11:28:27 -0500 | [diff] [blame] | 492 | return GraphicsJNI::getColorSpace(env, colorSpace.get(), colorType); |
Leon Scroggins III | 1a69f45 | 2018-03-29 09:48:47 -0400 | [diff] [blame] | 493 | } |
| 494 | |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 495 | static const JNINativeMethod gImageDecoderMethods[] = { |
Chong Zhang | cba2792 | 2019-07-12 16:38:47 -0700 | [diff] [blame] | 496 | { "nCreate", "(JZLandroid/graphics/ImageDecoder$Source;)Landroid/graphics/ImageDecoder;", (void*) ImageDecoder_nCreateAsset }, |
| 497 | { "nCreate", "(Ljava/nio/ByteBuffer;IIZLandroid/graphics/ImageDecoder$Source;)Landroid/graphics/ImageDecoder;", (void*) ImageDecoder_nCreateByteBuffer }, |
| 498 | { "nCreate", "([BIIZLandroid/graphics/ImageDecoder$Source;)Landroid/graphics/ImageDecoder;", (void*) ImageDecoder_nCreateByteArray }, |
| 499 | { "nCreate", "(Ljava/io/InputStream;[BZLandroid/graphics/ImageDecoder$Source;)Landroid/graphics/ImageDecoder;", (void*) ImageDecoder_nCreateInputStream }, |
| 500 | { "nCreate", "(Ljava/io/FileDescriptor;ZLandroid/graphics/ImageDecoder$Source;)Landroid/graphics/ImageDecoder;", (void*) ImageDecoder_nCreateFd }, |
Leon Scroggins III | 28f3943f | 2019-02-19 11:03:44 -0500 | [diff] [blame] | 501 | { "nDecodeBitmap", "(JLandroid/graphics/ImageDecoder;ZIILandroid/graphics/Rect;ZIZZZJZ)Landroid/graphics/Bitmap;", |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 502 | (void*) ImageDecoder_nDecodeBitmap }, |
Leon Scroggins III | e5de9aa | 2018-01-10 20:56:51 -0500 | [diff] [blame] | 503 | { "nGetSampledSize","(JI)Landroid/util/Size;", (void*) ImageDecoder_nGetSampledSize }, |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 504 | { "nGetPadding", "(JLandroid/graphics/Rect;)V", (void*) ImageDecoder_nGetPadding }, |
Leon Scroggins III | ed074fd | 2017-12-11 13:47:23 -0500 | [diff] [blame] | 505 | { "nClose", "(J)V", (void*) ImageDecoder_nClose}, |
Leon Scroggins III | 1fad09d | 2017-12-22 12:54:20 -0500 | [diff] [blame] | 506 | { "nGetMimeType", "(J)Ljava/lang/String;", (void*) ImageDecoder_nGetMimeType }, |
Leon Scroggins III | 1a69f45 | 2018-03-29 09:48:47 -0400 | [diff] [blame] | 507 | { "nGetColorSpace", "(J)Landroid/graphics/ColorSpace;", (void*) ImageDecoder_nGetColorSpace }, |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 508 | }; |
| 509 | |
| 510 | int register_android_graphics_ImageDecoder(JNIEnv* env) { |
| 511 | gImageDecoder_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/ImageDecoder")); |
Leon Scroggins III | 7d940ba | 2018-04-04 16:19:33 -0400 | [diff] [blame] | 512 | gImageDecoder_constructorMethodID = GetMethodIDOrDie(env, gImageDecoder_class, "<init>", "(JIIZZ)V"); |
Leon Scroggins III | e5de9aa | 2018-01-10 20:56:51 -0500 | [diff] [blame] | 513 | gImageDecoder_postProcessMethodID = GetMethodIDOrDie(env, gImageDecoder_class, "postProcessAndRelease", "(Landroid/graphics/Canvas;)I"); |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 514 | |
Leon Scroggins III | e5de9aa | 2018-01-10 20:56:51 -0500 | [diff] [blame] | 515 | gSize_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/util/Size")); |
| 516 | gSize_constructorMethodID = GetMethodIDOrDie(env, gSize_class, "<init>", "(II)V"); |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 517 | |
Leon Scroggins III | 1d2bf2b | 2018-03-14 16:07:43 -0400 | [diff] [blame] | 518 | gDecodeException_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/ImageDecoder$DecodeException")); |
| 519 | gDecodeException_constructorMethodID = GetMethodIDOrDie(env, gDecodeException_class, "<init>", "(ILjava/lang/String;Ljava/lang/Throwable;Landroid/graphics/ImageDecoder$Source;)V"); |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 520 | |
Leon Scroggins III | 1d2bf2b | 2018-03-14 16:07:43 -0400 | [diff] [blame] | 521 | gCallback_onPartialImageMethodID = GetMethodIDOrDie(env, gImageDecoder_class, "onPartialImage", "(ILjava/lang/Throwable;)V"); |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 522 | |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 523 | gCanvas_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/Canvas")); |
| 524 | gCanvas_constructorMethodID = GetMethodIDOrDie(env, gCanvas_class, "<init>", "(J)V"); |
| 525 | gCanvas_releaseMethodID = GetMethodIDOrDie(env, gCanvas_class, "release", "()V"); |
| 526 | |
| 527 | return android::RegisterMethodsOrDie(env, "android/graphics/ImageDecoder", gImageDecoderMethods, |
| 528 | NELEM(gImageDecoderMethods)); |
| 529 | } |