blob: aebc4db37898e19ec2fd3cdcbc44c0edb1f6a827 [file] [log] [blame]
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -04001/*
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 III671cce22018-01-14 16:52:17 -050017#include "ImageDecoder.h"
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -040018
Leon Scroggins III2bcdf6f2020-04-21 14:08:32 -040019#include <FrontBufferedStream.h>
John Reck5bd537e2023-01-24 20:13:45 -050020#include <HardwareBitmapUploader.h>
Kevin Lubick08409e22023-02-14 14:29:25 +000021#include <SkAlphaType.h>
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -040022#include <SkAndroidCodec.h>
Kevin Lubick1175dc02022-02-28 12:41:27 -050023#include <SkBitmap.h>
Kevin Lubick08409e22023-02-14 14:29:25 +000024#include <SkCodec.h>
25#include <SkCodecAnimation.h>
Kevin Lubick1175dc02022-02-28 12:41:27 -050026#include <SkColorSpace.h>
Kevin Lubick08409e22023-02-14 14:29:25 +000027#include <SkColorType.h>
Xiao Huangfff1a292023-04-12 17:37:36 +000028#include <SkEncodedImageFormat.h>
Kevin Lubick1175dc02022-02-28 12:41:27 -050029#include <SkImageInfo.h>
30#include <SkRect.h>
Kevin Lubick08409e22023-02-14 14:29:25 +000031#include <SkSize.h>
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -040032#include <SkStream.h>
Kevin Lubick1175dc02022-02-28 12:41:27 -050033#include <SkString.h>
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -040034#include <androidfw/Asset.h>
Derek Sollenbergerc5882c42019-10-25 11:11:32 -040035#include <fcntl.h>
John Reck5bd537e2023-01-24 20:13:45 -050036#include <gui/TraceUtils.h>
37#include <hwui/Bitmap.h>
38#include <hwui/ImageDecoder.h>
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -050039#include <sys/stat.h>
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -040040
John Reck5bd537e2023-01-24 20:13:45 -050041#include "Bitmap.h"
42#include "BitmapFactory.h"
43#include "ByteBufferStreamAdaptor.h"
44#include "CreateJavaOutputStreamAdaptor.h"
45#include "Gainmap.h"
46#include "GraphicsJNI.h"
47#include "NinePatchPeeker.h"
48#include "Utils.h"
49
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -040050using namespace android;
51
Xiao Huangfff1a292023-04-12 17:37:36 +000052jclass gImageDecoder_class;
53jmethodID gImageDecoder_isP010SupportedForHEVCMethodID;
Leon Scroggins IIIe5de9aa2018-01-10 20:56:51 -050054static jclass gSize_class;
Leon Scroggins III1d2bf2b2018-03-14 16:07:43 -040055static jclass gDecodeException_class;
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -040056static jclass gCanvas_class;
57static jmethodID gImageDecoder_constructorMethodID;
Leon Scroggins III8c9d8f22018-01-14 14:41:46 -050058static jmethodID gImageDecoder_postProcessMethodID;
Leon Scroggins IIIe5de9aa2018-01-10 20:56:51 -050059static jmethodID gSize_constructorMethodID;
Leon Scroggins III1d2bf2b2018-03-14 16:07:43 -040060static jmethodID gDecodeException_constructorMethodID;
Leon Scroggins IIIedf26d62018-01-09 16:55:24 -050061static jmethodID gCallback_onPartialImageMethodID;
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -040062static jmethodID gCanvas_constructorMethodID;
63static jmethodID gCanvas_releaseMethodID;
64
Leon Scroggins III753a56f2019-12-11 11:02:15 -050065// These need to stay in sync with ImageDecoder.java's Allocator constants.
66enum Allocator {
67 kDefault_Allocator = 0,
68 kSoftware_Allocator = 1,
69 kSharedMemory_Allocator = 2,
70 kHardware_Allocator = 3,
71};
72
73// These need to stay in sync with ImageDecoder.java's Error constants.
74enum Error {
75 kSourceException = 1,
76 kSourceIncomplete = 2,
77 kSourceMalformedData = 3,
78};
79
80// These need to stay in sync with PixelFormat.java's Format constants.
81enum PixelFormat {
82 kUnknown = 0,
83 kTranslucent = -3,
84 kOpaque = -1,
85};
86
Leon Scroggins III1d2bf2b2018-03-14 16:07:43 -040087// Clear and return any pending exception for handling other than throwing directly.
88static jthrowable get_and_clear_exception(JNIEnv* env) {
89 jthrowable jexception = env->ExceptionOccurred();
90 if (jexception) {
91 env->ExceptionClear();
92 }
93 return jexception;
94}
95
96// Throw a new ImageDecoder.DecodeException. Returns null for convenience.
Leon Scroggins III753a56f2019-12-11 11:02:15 -050097static jobject throw_exception(JNIEnv* env, Error error, const char* msg,
Leon Scroggins III1d2bf2b2018-03-14 16:07:43 -040098 jthrowable cause, jobject source) {
99 jstring jstr = nullptr;
100 if (msg) {
101 jstr = env->NewStringUTF(msg);
102 if (!jstr) {
103 // Out of memory.
104 return nullptr;
105 }
106 }
107 jthrowable exception = (jthrowable) env->NewObject(gDecodeException_class,
108 gDecodeException_constructorMethodID, error, jstr, cause, source);
109 // Only throw if not out of memory.
110 if (exception) {
111 env->Throw(exception);
112 }
113 return nullptr;
114}
115
Chong Zhangcba27922019-07-12 16:38:47 -0700116static jobject native_create(JNIEnv* env, std::unique_ptr<SkStream> stream,
117 jobject source, jboolean preferAnimation) {
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400118 if (!stream.get()) {
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500119 return throw_exception(env, kSourceMalformedData, "Failed to create a stream",
Leon Scroggins III1d2bf2b2018-03-14 16:07:43 -0400120 nullptr, source);
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400121 }
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500122 sk_sp<NinePatchPeeker> peeker(new NinePatchPeeker);
Leon Scroggins IIIc782ad82018-01-14 10:50:45 -0500123 SkCodec::Result result;
Chong Zhangcba27922019-07-12 16:38:47 -0700124 auto codec = SkCodec::MakeFromStream(
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500125 std::move(stream), &result, peeker.get(),
Chong Zhangcba27922019-07-12 16:38:47 -0700126 preferAnimation ? SkCodec::SelectionPolicy::kPreferAnimation
127 : SkCodec::SelectionPolicy::kPreferStillImage);
Leon Scroggins III1d2bf2b2018-03-14 16:07:43 -0400128 if (jthrowable jexception = get_and_clear_exception(env)) {
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500129 return throw_exception(env, kSourceException, "", jexception, source);
Leon Scroggins III1d2bf2b2018-03-14 16:07:43 -0400130 }
Leon Scroggins IIIc782ad82018-01-14 10:50:45 -0500131 if (!codec) {
132 switch (result) {
133 case SkCodec::kIncompleteInput:
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500134 return throw_exception(env, kSourceIncomplete, "", nullptr, source);
Leon Scroggins IIIc782ad82018-01-14 10:50:45 -0500135 default:
136 SkString msg;
137 msg.printf("Failed to create image decoder with message '%s'",
138 SkCodec::ResultToString(result));
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500139 return throw_exception(env, kSourceMalformedData, msg.c_str(),
Leon Scroggins IIIcf7294f2018-03-22 09:21:29 -0400140 nullptr, source);
Leon Scroggins IIIc782ad82018-01-14 10:50:45 -0500141
Leon Scroggins III671cce22018-01-14 16:52:17 -0500142 }
Leon Scroggins IIIc782ad82018-01-14 10:50:45 -0500143 }
144
Leon Scroggins III671cce22018-01-14 16:52:17 -0500145 const bool animated = codec->getFrameCount() > 1;
Leon Scroggins III1d2bf2b2018-03-14 16:07:43 -0400146 if (jthrowable jexception = get_and_clear_exception(env)) {
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500147 return throw_exception(env, kSourceException, "", jexception, source);
Leon Scroggins III1d2bf2b2018-03-14 16:07:43 -0400148 }
149
Leon Scroggins III139145b2020-12-17 15:43:54 -0500150 auto androidCodec = SkAndroidCodec::MakeFromCodec(std::move(codec));
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500151 if (!androidCodec.get()) {
152 return throw_exception(env, kSourceMalformedData, "", nullptr, source);
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400153 }
Leon Scroggins III1d2bf2b2018-03-14 16:07:43 -0400154
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500155 const bool isNinePatch = peeker->mPatch != nullptr;
Leon Scroggins III368a7a52020-11-20 12:23:27 -0500156 ImageDecoder* decoder = new ImageDecoder(std::move(androidCodec), std::move(peeker),
157 SkCodec::kYes_ZeroInitialized);
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400158 return env->NewObject(gImageDecoder_class, gImageDecoder_constructorMethodID,
Leon Scroggins III139145b2020-12-17 15:43:54 -0500159 reinterpret_cast<jlong>(decoder), decoder->width(), decoder->height(),
Leon Scroggins III7d940ba2018-04-04 16:19:33 -0400160 animated, isNinePatch);
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400161}
162
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500163static jobject ImageDecoder_nCreateFd(JNIEnv* env, jobject /*clazz*/,
Leon Scroggins IIIc3fda892020-09-10 14:57:11 -0400164 jobject fileDescriptor, jlong length, jboolean preferAnimation, jobject source) {
Michael Hoisiebbaf2ab2024-07-11 20:51:16 +0000165#ifdef _WIN32 // LayoutLib for Windows does not support F_DUPFD_CLOEXEC
166 return throw_exception(env, kSourceException, "Not supported on Windows", nullptr, source);
Derek Sollenbergerc5882c42019-10-25 11:11:32 -0400167#else
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500168 int descriptor = jniGetFDFromFileDescriptor(env, fileDescriptor);
169
170 struct stat fdStat;
171 if (fstat(descriptor, &fdStat) == -1) {
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500172 return throw_exception(env, kSourceMalformedData,
Leon Scroggins III1d2bf2b2018-03-14 16:07:43 -0400173 "broken file descriptor; fstat returned -1", nullptr, source);
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500174 }
175
Nick Kralevich4b3a08c2019-01-28 10:39:10 -0800176 int dupDescriptor = fcntl(descriptor, F_DUPFD_CLOEXEC, 0);
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500177 FILE* file = fdopen(dupDescriptor, "r");
178 if (file == NULL) {
179 close(dupDescriptor);
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500180 return throw_exception(env, kSourceMalformedData, "Could not open file",
Leon Scroggins IIIcf7294f2018-03-22 09:21:29 -0400181 nullptr, source);
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500182 }
Leon Scroggins III0c699ad2018-05-07 16:20:13 -0400183
Leon Scroggins IIIc3fda892020-09-10 14:57:11 -0400184 std::unique_ptr<SkFILEStream> fileStream;
185 if (length == -1) {
186 // -1 corresponds to AssetFileDescriptor.UNKNOWN_LENGTH. Pass no length
187 // so SkFILEStream will figure out the size of the file on its own.
188 fileStream.reset(new SkFILEStream(file));
189 } else {
190 fileStream.reset(new SkFILEStream(file, length));
191 }
Chong Zhangcba27922019-07-12 16:38:47 -0700192 return native_create(env, std::move(fileStream), source, preferAnimation);
Derek Sollenbergerc5882c42019-10-25 11:11:32 -0400193#endif
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500194}
195
196static jobject ImageDecoder_nCreateInputStream(JNIEnv* env, jobject /*clazz*/,
Chong Zhangcba27922019-07-12 16:38:47 -0700197 jobject is, jbyteArray storage, jboolean preferAnimation, jobject source) {
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500198 std::unique_ptr<SkStream> stream(CreateJavaInputStreamAdaptor(env, is, storage, false));
199
200 if (!stream.get()) {
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500201 return throw_exception(env, kSourceMalformedData, "Failed to create a stream",
Leon Scroggins III1d2bf2b2018-03-14 16:07:43 -0400202 nullptr, source);
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500203 }
Leon Scroggins III1d2bf2b2018-03-14 16:07:43 -0400204
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500205 std::unique_ptr<SkStream> bufferedStream(
Leon Scroggins III2bcdf6f2020-04-21 14:08:32 -0400206 skia::FrontBufferedStream::Make(std::move(stream), SkCodec::MinBufferedBytesNeeded()));
Chong Zhangcba27922019-07-12 16:38:47 -0700207 return native_create(env, std::move(bufferedStream), source, preferAnimation);
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500208}
209
Chong Zhangcba27922019-07-12 16:38:47 -0700210static jobject ImageDecoder_nCreateAsset(JNIEnv* env, jobject /*clazz*/,
211 jlong assetPtr, jboolean preferAnimation, jobject source) {
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400212 Asset* asset = reinterpret_cast<Asset*>(assetPtr);
213 std::unique_ptr<SkStream> stream(new AssetStreamAdaptor(asset));
Chong Zhangcba27922019-07-12 16:38:47 -0700214 return native_create(env, std::move(stream), source, preferAnimation);
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400215}
216
Chong Zhangcba27922019-07-12 16:38:47 -0700217static jobject ImageDecoder_nCreateByteBuffer(JNIEnv* env, jobject /*clazz*/,
218 jobject jbyteBuffer, jint initialPosition, jint limit,
219 jboolean preferAnimation, jobject source) {
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400220 std::unique_ptr<SkStream> stream = CreateByteBufferStreamAdaptor(env, jbyteBuffer,
221 initialPosition, limit);
222 if (!stream) {
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500223 return throw_exception(env, kSourceMalformedData, "Failed to read ByteBuffer",
Leon Scroggins III1d2bf2b2018-03-14 16:07:43 -0400224 nullptr, source);
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400225 }
Chong Zhangcba27922019-07-12 16:38:47 -0700226 return native_create(env, std::move(stream), source, preferAnimation);
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400227}
228
Chong Zhangcba27922019-07-12 16:38:47 -0700229static jobject ImageDecoder_nCreateByteArray(JNIEnv* env, jobject /*clazz*/,
230 jbyteArray byteArray, jint offset, jint length,
231 jboolean preferAnimation, jobject source) {
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400232 std::unique_ptr<SkStream> stream(CreateByteArrayStreamAdaptor(env, byteArray, offset, length));
Chong Zhangcba27922019-07-12 16:38:47 -0700233 return native_create(env, std::move(stream), source, preferAnimation);
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400234}
235
Leon Scroggins IIIe5de9aa2018-01-10 20:56:51 -0500236jint postProcessAndRelease(JNIEnv* env, jobject jimageDecoder, std::unique_ptr<Canvas> canvas) {
Leon Scroggins III8c9d8f22018-01-14 14:41:46 -0500237 jobject jcanvas = env->NewObject(gCanvas_class, gCanvas_constructorMethodID,
238 reinterpret_cast<jlong>(canvas.get()));
239 if (!jcanvas) {
240 doThrowOOME(env, "Failed to create Java Canvas for PostProcess!");
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500241 return kUnknown;
Leon Scroggins III8c9d8f22018-01-14 14:41:46 -0500242 }
243
244 // jcanvas now owns canvas.
245 canvas.release();
246
Leon Scroggins IIIe5de9aa2018-01-10 20:56:51 -0500247 return env->CallIntMethod(jimageDecoder, gImageDecoder_postProcessMethodID, jcanvas);
Leon Scroggins III8c9d8f22018-01-14 14:41:46 -0500248}
249
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400250static jobject ImageDecoder_nDecodeBitmap(JNIEnv* env, jobject /*clazz*/, jlong nativePtr,
Leon Scroggins III1d2bf2b2018-03-14 16:07:43 -0400251 jobject jdecoder, jboolean jpostProcess,
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500252 jint targetWidth, jint targetHeight, jobject jsubset,
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400253 jboolean requireMutable, jint allocator,
254 jboolean requireUnpremul, jboolean preferRamOverQuality,
Leon Scroggins III28f3943f2019-02-19 11:03:44 -0500255 jboolean asAlphaMask, jlong colorSpaceHandle,
256 jboolean extended) {
John Reck5bd537e2023-01-24 20:13:45 -0500257 ATRACE_CALL();
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400258 auto* decoder = reinterpret_cast<ImageDecoder*>(nativePtr);
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500259 if (!decoder->setTargetSize(targetWidth, targetHeight)) {
260 doThrowISE(env, "Could not scale to target size!");
261 return nullptr;
262 }
Leon Scroggins III1ade46d2020-01-15 05:41:06 -0500263 if (requireUnpremul && !decoder->setUnpremultipliedRequired(true)) {
Leon Scroggins IIIea978db2018-01-13 11:40:42 -0500264 doThrowISE(env, "Cannot scale unpremultiplied pixels!");
265 return nullptr;
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400266 }
267
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400268 SkColorType colorType = kN32_SkColorType;
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500269 if (asAlphaMask && decoder->gray()) {
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400270 // We have to trick Skia to decode this to a single channel.
271 colorType = kGray_8_SkColorType;
272 } else if (preferRamOverQuality) {
273 // FIXME: The post-process might add alpha, which would make a 565
274 // result incorrect. If we call the postProcess before now and record
275 // to a picture, we can know whether alpha was added, and if not, we
276 // can still use 565.
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500277 if (decoder->opaque() && !jpostProcess) {
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400278 // If the final result will be hardware, decoding to 565 and then
279 // uploading to the gpu as 8888 will not save memory. This still
280 // may save us from using F16, but do not go down to 565.
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500281 if (allocator != kHardware_Allocator &&
282 (allocator != kDefault_Allocator || requireMutable)) {
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400283 colorType = kRGB_565_SkColorType;
284 }
285 }
286 // Otherwise, stick with N32
Leon Scroggins III28f3943f2019-02-19 11:03:44 -0500287 } else if (extended) {
288 colorType = kRGBA_F16_SkColorType;
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400289 } else {
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500290 colorType = decoder->mCodec->computeOutputColorType(colorType);
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400291 }
Leon Scroggins IIIee3bfe72019-01-31 08:42:23 -0500292
293 const bool isHardware = !requireMutable
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500294 && (allocator == kDefault_Allocator ||
295 allocator == kHardware_Allocator)
Leon Scroggins IIIee3bfe72019-01-31 08:42:23 -0500296 && colorType != kGray_8_SkColorType;
297
298 if (colorType == kRGBA_F16_SkColorType && isHardware &&
299 !uirenderer::HardwareBitmapUploader::hasFP16Support()) {
300 colorType = kN32_SkColorType;
301 }
302
Xiao Huangfff1a292023-04-12 17:37:36 +0000303 // b/276879147, fallback to RGBA_8888 when decoding HEIF and P010 is not supported.
304 if (colorType == kRGBA_1010102_SkColorType &&
305 decoder->mCodec->getEncodedFormat() == SkEncodedImageFormat::kHEIF &&
306 env->CallStaticBooleanMethod(gImageDecoder_class,
307 gImageDecoder_isP010SupportedForHEVCMethodID) == JNI_FALSE) {
308 colorType = kN32_SkColorType;
309 }
310
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500311 if (!decoder->setOutColorType(colorType)) {
312 doThrowISE(env, "Failed to set out color type!");
313 return nullptr;
314 }
315
316 {
317 sk_sp<SkColorSpace> colorSpace = GraphicsJNI::getNativeColorSpace(colorSpaceHandle);
318 colorSpace = decoder->mCodec->computeOutputColorSpace(colorType, colorSpace);
319 decoder->setOutColorSpace(std::move(colorSpace));
320 }
321
322 if (jsubset) {
323 SkIRect subset;
324 GraphicsJNI::jrect_to_irect(env, jsubset, &subset);
325 if (!decoder->setCropRect(&subset)) {
326 doThrowISE(env, "Invalid crop rect!");
327 return nullptr;
328 }
329 }
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400330
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500331 SkImageInfo bitmapInfo = decoder->getOutputInfo();
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400332 if (asAlphaMask && colorType == kGray_8_SkColorType) {
333 bitmapInfo = bitmapInfo.makeColorType(kAlpha_8_SkColorType);
334 }
Leon Scroggins III1ade46d2020-01-15 05:41:06 -0500335
336 SkBitmap bm;
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400337 if (!bm.setInfo(bitmapInfo)) {
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500338 doThrowIOE(env, "Failed to setInfo properly");
339 return nullptr;
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400340 }
341
342 sk_sp<Bitmap> nativeBitmap;
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500343 if (allocator == kSharedMemory_Allocator) {
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400344 nativeBitmap = Bitmap::allocateAshmemBitmap(&bm);
345 } else {
346 nativeBitmap = Bitmap::allocateHeapBitmap(&bm);
347 }
348 if (!nativeBitmap) {
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500349 SkString msg;
350 msg.printf("OOM allocating Bitmap with dimensions %i x %i",
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500351 bitmapInfo.width(), bitmapInfo.height());
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500352 doThrowOOME(env, msg.c_str());
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400353 return nullptr;
354 }
355
John Reck5bd537e2023-01-24 20:13:45 -0500356 ATRACE_FORMAT("Decoding %dx%d bitmap", bitmapInfo.width(), bitmapInfo.height());
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500357 SkCodec::Result result = decoder->decode(bm.getPixels(), bm.rowBytes());
Leon Scroggins III1d2bf2b2018-03-14 16:07:43 -0400358 jthrowable jexception = get_and_clear_exception(env);
John Reck5bd537e2023-01-24 20:13:45 -0500359 int onPartialImageError = jexception ? kSourceException : 0; // No error.
360
361 // Only attempt to extract the gainmap if we're not post-processing, as we can't automatically
362 // mimic that to the gainmap and expect it to be meaningful. And also don't extract the gainmap
363 // if we're prioritizing RAM over quality, since the gainmap improves quality at the
364 // cost of RAM
365 if (result == SkCodec::kSuccess && !jpostProcess && !preferRamOverQuality) {
366 // The gainmap costs RAM to improve quality, so skip this if we're prioritizing RAM instead
Sally Qicaa2ef52023-03-07 15:13:16 -0800367 result = decoder->extractGainmap(nativeBitmap.get(),
368 allocator == kSharedMemory_Allocator ? true : false);
John Reck5bd537e2023-01-24 20:13:45 -0500369 jexception = get_and_clear_exception(env);
370 }
371
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400372 switch (result) {
373 case SkCodec::kSuccess:
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500374 // Ignore the exception, since the decode was successful anyway.
375 jexception = nullptr;
Leon Scroggins IIIe5de9aa2018-01-10 20:56:51 -0500376 onPartialImageError = 0;
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400377 break;
378 case SkCodec::kIncompleteInput:
Leon Scroggins IIIe5de9aa2018-01-10 20:56:51 -0500379 if (!jexception) {
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500380 onPartialImageError = kSourceIncomplete;
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400381 }
382 break;
383 case SkCodec::kErrorInInput:
Leon Scroggins IIIe5de9aa2018-01-10 20:56:51 -0500384 if (!jexception) {
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500385 onPartialImageError = kSourceMalformedData;
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400386 }
387 break;
388 default:
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500389 SkString msg;
Leon Scroggins IIIe5de9aa2018-01-10 20:56:51 -0500390 msg.printf("getPixels failed with error %s", SkCodec::ResultToString(result));
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500391 doThrowIOE(env, msg.c_str());
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400392 return nullptr;
393 }
394
Leon Scroggins III1d2bf2b2018-03-14 16:07:43 -0400395 if (onPartialImageError) {
396 env->CallVoidMethod(jdecoder, gCallback_onPartialImageMethodID, onPartialImageError,
397 jexception);
Leon Scroggins IIIedf26d62018-01-09 16:55:24 -0500398 if (env->ExceptionCheck()) {
399 return nullptr;
400 }
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400401 }
402
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400403 jbyteArray ninePatchChunk = nullptr;
404 jobject ninePatchInsets = nullptr;
405
406 // Ignore ninepatch when post-processing.
407 if (!jpostProcess) {
408 // FIXME: Share more code with BitmapFactory.cpp.
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500409 auto* peeker = reinterpret_cast<NinePatchPeeker*>(decoder->mPeeker.get());
410 if (peeker->mPatch != nullptr) {
411 size_t ninePatchArraySize = peeker->mPatch->serializedSize();
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400412 ninePatchChunk = env->NewByteArray(ninePatchArraySize);
413 if (ninePatchChunk == nullptr) {
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500414 doThrowOOME(env, "Failed to allocate nine patch chunk.");
415 return nullptr;
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400416 }
417
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500418 env->SetByteArrayRegion(ninePatchChunk, 0, peeker->mPatchSize,
419 reinterpret_cast<jbyte*>(peeker->mPatch));
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400420 }
421
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500422 if (peeker->mHasInsets) {
423 ninePatchInsets = peeker->createNinePatchInsets(env, 1.0f);
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400424 if (ninePatchInsets == nullptr) {
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500425 doThrowOOME(env, "Failed to allocate nine patch insets.");
426 return nullptr;
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400427 }
428 }
429 }
430
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400431 if (jpostProcess) {
432 std::unique_ptr<Canvas> canvas(Canvas::create_canvas(bm));
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400433
Leon Scroggins III1d2bf2b2018-03-14 16:07:43 -0400434 jint pixelFormat = postProcessAndRelease(env, jdecoder, std::move(canvas));
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400435 if (env->ExceptionCheck()) {
436 return nullptr;
437 }
438
439 SkAlphaType newAlphaType = bm.alphaType();
440 switch (pixelFormat) {
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500441 case kUnknown:
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400442 break;
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500443 case kTranslucent:
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400444 newAlphaType = kPremul_SkAlphaType;
445 break;
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500446 case kOpaque:
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400447 newAlphaType = kOpaque_SkAlphaType;
448 break;
449 default:
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500450 SkString msg;
451 msg.printf("invalid return from postProcess: %i", pixelFormat);
452 doThrowIAE(env, msg.c_str());
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400453 return nullptr;
454 }
455
456 if (newAlphaType != bm.alphaType()) {
457 if (!bm.setAlphaType(newAlphaType)) {
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500458 SkString msg;
459 msg.printf("incompatible return from postProcess: %i", pixelFormat);
460 doThrowIAE(env, msg.c_str());
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400461 return nullptr;
462 }
463 nativeBitmap->setAlphaType(newAlphaType);
464 }
465 }
466
467 int bitmapCreateFlags = 0x0;
468 if (!requireUnpremul) {
469 // Even if the image is opaque, setting this flag means that
470 // if alpha is added (e.g. by PostProcess), it will be marked as
471 // premultiplied.
472 bitmapCreateFlags |= bitmap::kBitmapCreateFlag_Premultiplied;
473 }
474
475 if (requireMutable) {
476 bitmapCreateFlags |= bitmap::kBitmapCreateFlag_Mutable;
477 } else {
Leon Scroggins IIIee3bfe72019-01-31 08:42:23 -0500478 if (isHardware) {
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400479 sk_sp<Bitmap> hwBitmap = Bitmap::allocateHardwareBitmap(bm);
480 if (hwBitmap) {
481 hwBitmap->setImmutable();
John Reck5bd537e2023-01-24 20:13:45 -0500482 if (nativeBitmap->hasGainmap()) {
Sally Qi587fb572023-03-03 15:50:06 -0800483 auto gm = uirenderer::Gainmap::allocateHardwareGainmap(nativeBitmap->gainmap());
484 if (gm) {
485 hwBitmap->setGainmap(std::move(gm));
486 }
John Reck5bd537e2023-01-24 20:13:45 -0500487 }
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400488 return bitmap::createBitmap(env, hwBitmap.release(), bitmapCreateFlags,
489 ninePatchChunk, ninePatchInsets);
490 }
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500491 if (allocator == kHardware_Allocator) {
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500492 doThrowOOME(env, "failed to allocate hardware Bitmap!");
493 return nullptr;
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400494 }
495 // If we failed to create a hardware bitmap, go ahead and create a
496 // software one.
497 }
498
499 nativeBitmap->setImmutable();
500 }
501 return bitmap::createBitmap(env, nativeBitmap.release(), bitmapCreateFlags, ninePatchChunk,
502 ninePatchInsets);
503}
504
505static jobject ImageDecoder_nGetSampledSize(JNIEnv* env, jobject /*clazz*/, jlong nativePtr,
506 jint sampleSize) {
507 auto* decoder = reinterpret_cast<ImageDecoder*>(nativePtr);
Leon Scroggins III5a5c2ce2021-01-15 14:09:13 -0500508 SkISize size = decoder->getSampledDimensions(sampleSize);
Leon Scroggins IIIe5de9aa2018-01-10 20:56:51 -0500509 return env->NewObject(gSize_class, gSize_constructorMethodID, size.width(), size.height());
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400510}
511
512static void ImageDecoder_nGetPadding(JNIEnv* env, jobject /*clazz*/, jlong nativePtr,
513 jobject outPadding) {
514 auto* decoder = reinterpret_cast<ImageDecoder*>(nativePtr);
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500515 reinterpret_cast<NinePatchPeeker*>(decoder->mPeeker.get())->getPadding(env, outPadding);
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400516}
517
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500518static void ImageDecoder_nClose(JNIEnv* /*env*/, jobject /*clazz*/, jlong nativePtr) {
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400519 delete reinterpret_cast<ImageDecoder*>(nativePtr);
520}
521
Leon Scroggins III1fad09d2017-12-22 12:54:20 -0500522static jstring ImageDecoder_nGetMimeType(JNIEnv* env, jobject /*clazz*/, jlong nativePtr) {
523 auto* decoder = reinterpret_cast<ImageDecoder*>(nativePtr);
Leon Scroggins III407b5442019-11-22 17:10:20 -0500524 return getMimeTypeAsJavaString(env, decoder->mCodec->getEncodedFormat());
Leon Scroggins III1fad09d2017-12-22 12:54:20 -0500525}
526
Leon Scroggins III1a69f452018-03-29 09:48:47 -0400527static jobject ImageDecoder_nGetColorSpace(JNIEnv* env, jobject /*clazz*/, jlong nativePtr) {
528 auto* codec = reinterpret_cast<ImageDecoder*>(nativePtr)->mCodec.get();
Derek Sollenbergerbf3e4642019-01-30 11:28:27 -0500529 auto colorType = codec->computeOutputColorType(kN32_SkColorType);
Leon Scroggins III1a69f452018-03-29 09:48:47 -0400530 sk_sp<SkColorSpace> colorSpace = codec->computeOutputColorSpace(colorType);
Derek Sollenbergerbf3e4642019-01-30 11:28:27 -0500531 return GraphicsJNI::getColorSpace(env, colorSpace.get(), colorType);
Leon Scroggins III1a69f452018-03-29 09:48:47 -0400532}
533
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400534static const JNINativeMethod gImageDecoderMethods[] = {
Chong Zhangcba27922019-07-12 16:38:47 -0700535 { "nCreate", "(JZLandroid/graphics/ImageDecoder$Source;)Landroid/graphics/ImageDecoder;", (void*) ImageDecoder_nCreateAsset },
536 { "nCreate", "(Ljava/nio/ByteBuffer;IIZLandroid/graphics/ImageDecoder$Source;)Landroid/graphics/ImageDecoder;", (void*) ImageDecoder_nCreateByteBuffer },
537 { "nCreate", "([BIIZLandroid/graphics/ImageDecoder$Source;)Landroid/graphics/ImageDecoder;", (void*) ImageDecoder_nCreateByteArray },
538 { "nCreate", "(Ljava/io/InputStream;[BZLandroid/graphics/ImageDecoder$Source;)Landroid/graphics/ImageDecoder;", (void*) ImageDecoder_nCreateInputStream },
Leon Scroggins IIIc3fda892020-09-10 14:57:11 -0400539 { "nCreate", "(Ljava/io/FileDescriptor;JZLandroid/graphics/ImageDecoder$Source;)Landroid/graphics/ImageDecoder;", (void*) ImageDecoder_nCreateFd },
Leon Scroggins III28f3943f2019-02-19 11:03:44 -0500540 { "nDecodeBitmap", "(JLandroid/graphics/ImageDecoder;ZIILandroid/graphics/Rect;ZIZZZJZ)Landroid/graphics/Bitmap;",
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400541 (void*) ImageDecoder_nDecodeBitmap },
Leon Scroggins IIIe5de9aa2018-01-10 20:56:51 -0500542 { "nGetSampledSize","(JI)Landroid/util/Size;", (void*) ImageDecoder_nGetSampledSize },
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400543 { "nGetPadding", "(JLandroid/graphics/Rect;)V", (void*) ImageDecoder_nGetPadding },
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500544 { "nClose", "(J)V", (void*) ImageDecoder_nClose},
Leon Scroggins III1fad09d2017-12-22 12:54:20 -0500545 { "nGetMimeType", "(J)Ljava/lang/String;", (void*) ImageDecoder_nGetMimeType },
Leon Scroggins III1a69f452018-03-29 09:48:47 -0400546 { "nGetColorSpace", "(J)Landroid/graphics/ColorSpace;", (void*) ImageDecoder_nGetColorSpace },
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400547};
548
549int register_android_graphics_ImageDecoder(JNIEnv* env) {
550 gImageDecoder_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/ImageDecoder"));
Leon Scroggins III7d940ba2018-04-04 16:19:33 -0400551 gImageDecoder_constructorMethodID = GetMethodIDOrDie(env, gImageDecoder_class, "<init>", "(JIIZZ)V");
Leon Scroggins IIIe5de9aa2018-01-10 20:56:51 -0500552 gImageDecoder_postProcessMethodID = GetMethodIDOrDie(env, gImageDecoder_class, "postProcessAndRelease", "(Landroid/graphics/Canvas;)I");
Xiao Huangfff1a292023-04-12 17:37:36 +0000553 gImageDecoder_isP010SupportedForHEVCMethodID =
554 GetStaticMethodIDOrDie(env, gImageDecoder_class, "isP010SupportedForHEVC", "()Z");
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400555
Leon Scroggins IIIe5de9aa2018-01-10 20:56:51 -0500556 gSize_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/util/Size"));
557 gSize_constructorMethodID = GetMethodIDOrDie(env, gSize_class, "<init>", "(II)V");
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400558
Leon Scroggins III1d2bf2b2018-03-14 16:07:43 -0400559 gDecodeException_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/ImageDecoder$DecodeException"));
560 gDecodeException_constructorMethodID = GetMethodIDOrDie(env, gDecodeException_class, "<init>", "(ILjava/lang/String;Ljava/lang/Throwable;Landroid/graphics/ImageDecoder$Source;)V");
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400561
Leon Scroggins III1d2bf2b2018-03-14 16:07:43 -0400562 gCallback_onPartialImageMethodID = GetMethodIDOrDie(env, gImageDecoder_class, "onPartialImage", "(ILjava/lang/Throwable;)V");
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400563
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400564 gCanvas_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/Canvas"));
565 gCanvas_constructorMethodID = GetMethodIDOrDie(env, gCanvas_class, "<init>", "(J)V");
566 gCanvas_releaseMethodID = GetMethodIDOrDie(env, gCanvas_class, "release", "()V");
567
568 return android::RegisterMethodsOrDie(env, "android/graphics/ImageDecoder", gImageDecoderMethods,
569 NELEM(gImageDecoderMethods));
570}