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