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