blob: bad710dec274c924145b40680cec8c92d56454be [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
17#include "Bitmap.h"
Leon Scroggins III1fad09d2017-12-22 12:54:20 -050018#include "BitmapFactory.h"
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -040019#include "ByteBufferStreamAdaptor.h"
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -050020#include "CreateJavaOutputStreamAdaptor.h"
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -040021#include "GraphicsJNI.h"
Leon Scroggins III671cce22018-01-14 16:52:17 -050022#include "ImageDecoder.h"
Leon Scroggins III753a56f2019-12-11 11:02:15 -050023#include "NinePatchPeeker.h"
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -040024#include "Utils.h"
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -040025
26#include <hwui/Bitmap.h>
Leon Scroggins III753a56f2019-12-11 11:02:15 -050027#include <hwui/ImageDecoder.h>
Leon Scroggins IIIee3bfe72019-01-31 08:42:23 -050028#include <HardwareBitmapUploader.h>
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -040029
Leon Scroggins III2bcdf6f2020-04-21 14:08:32 -040030#include <FrontBufferedStream.h>
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -040031#include <SkAndroidCodec.h>
Kevin Lubick1175dc02022-02-28 12:41:27 -050032#include <SkBitmap.h>
33#include <SkColorSpace.h>
34#include <SkImageInfo.h>
35#include <SkRect.h>
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -040036#include <SkStream.h>
Kevin Lubick1175dc02022-02-28 12:41:27 -050037#include <SkString.h>
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -040038
39#include <androidfw/Asset.h>
Derek Sollenbergerc5882c42019-10-25 11:11:32 -040040#include <fcntl.h>
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -050041#include <sys/stat.h>
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -040042
43using namespace android;
44
45static jclass gImageDecoder_class;
Leon Scroggins IIIe5de9aa2018-01-10 20:56:51 -050046static jclass gSize_class;
Leon Scroggins III1d2bf2b2018-03-14 16:07:43 -040047static jclass gDecodeException_class;
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -040048static jclass gCanvas_class;
49static jmethodID gImageDecoder_constructorMethodID;
Leon Scroggins III8c9d8f22018-01-14 14:41:46 -050050static jmethodID gImageDecoder_postProcessMethodID;
Leon Scroggins IIIe5de9aa2018-01-10 20:56:51 -050051static jmethodID gSize_constructorMethodID;
Leon Scroggins III1d2bf2b2018-03-14 16:07:43 -040052static jmethodID gDecodeException_constructorMethodID;
Leon Scroggins IIIedf26d62018-01-09 16:55:24 -050053static jmethodID gCallback_onPartialImageMethodID;
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -040054static jmethodID gCanvas_constructorMethodID;
55static jmethodID gCanvas_releaseMethodID;
56
Leon Scroggins III753a56f2019-12-11 11:02:15 -050057// These need to stay in sync with ImageDecoder.java's Allocator constants.
58enum Allocator {
59 kDefault_Allocator = 0,
60 kSoftware_Allocator = 1,
61 kSharedMemory_Allocator = 2,
62 kHardware_Allocator = 3,
63};
64
65// These need to stay in sync with ImageDecoder.java's Error constants.
66enum Error {
67 kSourceException = 1,
68 kSourceIncomplete = 2,
69 kSourceMalformedData = 3,
70};
71
72// These need to stay in sync with PixelFormat.java's Format constants.
73enum PixelFormat {
74 kUnknown = 0,
75 kTranslucent = -3,
76 kOpaque = -1,
77};
78
Leon Scroggins III1d2bf2b2018-03-14 16:07:43 -040079// Clear and return any pending exception for handling other than throwing directly.
80static jthrowable get_and_clear_exception(JNIEnv* env) {
81 jthrowable jexception = env->ExceptionOccurred();
82 if (jexception) {
83 env->ExceptionClear();
84 }
85 return jexception;
86}
87
88// Throw a new ImageDecoder.DecodeException. Returns null for convenience.
Leon Scroggins III753a56f2019-12-11 11:02:15 -050089static jobject throw_exception(JNIEnv* env, Error error, const char* msg,
Leon Scroggins III1d2bf2b2018-03-14 16:07:43 -040090 jthrowable cause, jobject source) {
91 jstring jstr = nullptr;
92 if (msg) {
93 jstr = env->NewStringUTF(msg);
94 if (!jstr) {
95 // Out of memory.
96 return nullptr;
97 }
98 }
99 jthrowable exception = (jthrowable) env->NewObject(gDecodeException_class,
100 gDecodeException_constructorMethodID, error, jstr, cause, source);
101 // Only throw if not out of memory.
102 if (exception) {
103 env->Throw(exception);
104 }
105 return nullptr;
106}
107
Chong Zhangcba27922019-07-12 16:38:47 -0700108static jobject native_create(JNIEnv* env, std::unique_ptr<SkStream> stream,
109 jobject source, jboolean preferAnimation) {
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400110 if (!stream.get()) {
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500111 return throw_exception(env, kSourceMalformedData, "Failed to create a stream",
Leon Scroggins III1d2bf2b2018-03-14 16:07:43 -0400112 nullptr, source);
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400113 }
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500114 sk_sp<NinePatchPeeker> peeker(new NinePatchPeeker);
Leon Scroggins IIIc782ad82018-01-14 10:50:45 -0500115 SkCodec::Result result;
Chong Zhangcba27922019-07-12 16:38:47 -0700116 auto codec = SkCodec::MakeFromStream(
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500117 std::move(stream), &result, peeker.get(),
Chong Zhangcba27922019-07-12 16:38:47 -0700118 preferAnimation ? SkCodec::SelectionPolicy::kPreferAnimation
119 : SkCodec::SelectionPolicy::kPreferStillImage);
Leon Scroggins III1d2bf2b2018-03-14 16:07:43 -0400120 if (jthrowable jexception = get_and_clear_exception(env)) {
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500121 return throw_exception(env, kSourceException, "", jexception, source);
Leon Scroggins III1d2bf2b2018-03-14 16:07:43 -0400122 }
Leon Scroggins IIIc782ad82018-01-14 10:50:45 -0500123 if (!codec) {
124 switch (result) {
125 case SkCodec::kIncompleteInput:
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500126 return throw_exception(env, kSourceIncomplete, "", nullptr, source);
Leon Scroggins IIIc782ad82018-01-14 10:50:45 -0500127 default:
128 SkString msg;
129 msg.printf("Failed to create image decoder with message '%s'",
130 SkCodec::ResultToString(result));
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500131 return throw_exception(env, kSourceMalformedData, msg.c_str(),
Leon Scroggins IIIcf7294f2018-03-22 09:21:29 -0400132 nullptr, source);
Leon Scroggins IIIc782ad82018-01-14 10:50:45 -0500133
Leon Scroggins III671cce22018-01-14 16:52:17 -0500134 }
Leon Scroggins IIIc782ad82018-01-14 10:50:45 -0500135 }
136
Leon Scroggins III671cce22018-01-14 16:52:17 -0500137 const bool animated = codec->getFrameCount() > 1;
Leon Scroggins III1d2bf2b2018-03-14 16:07:43 -0400138 if (jthrowable jexception = get_and_clear_exception(env)) {
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500139 return throw_exception(env, kSourceException, "", jexception, source);
Leon Scroggins III1d2bf2b2018-03-14 16:07:43 -0400140 }
141
Leon Scroggins III139145b2020-12-17 15:43:54 -0500142 auto androidCodec = SkAndroidCodec::MakeFromCodec(std::move(codec));
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500143 if (!androidCodec.get()) {
144 return throw_exception(env, kSourceMalformedData, "", nullptr, source);
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400145 }
Leon Scroggins III1d2bf2b2018-03-14 16:07:43 -0400146
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500147 const bool isNinePatch = peeker->mPatch != nullptr;
Leon Scroggins III368a7a52020-11-20 12:23:27 -0500148 ImageDecoder* decoder = new ImageDecoder(std::move(androidCodec), std::move(peeker),
149 SkCodec::kYes_ZeroInitialized);
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400150 return env->NewObject(gImageDecoder_class, gImageDecoder_constructorMethodID,
Leon Scroggins III139145b2020-12-17 15:43:54 -0500151 reinterpret_cast<jlong>(decoder), decoder->width(), decoder->height(),
Leon Scroggins III7d940ba2018-04-04 16:19:33 -0400152 animated, isNinePatch);
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400153}
154
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500155static jobject ImageDecoder_nCreateFd(JNIEnv* env, jobject /*clazz*/,
Leon Scroggins IIIc3fda892020-09-10 14:57:11 -0400156 jobject fileDescriptor, jlong length, jboolean preferAnimation, jobject source) {
Derek Sollenbergerc5882c42019-10-25 11:11:32 -0400157#ifndef __ANDROID__ // LayoutLib for Windows does not support F_DUPFD_CLOEXEC
158 return throw_exception(env, kSourceException, "Only supported on Android", nullptr, source);
159#else
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500160 int descriptor = jniGetFDFromFileDescriptor(env, fileDescriptor);
161
162 struct stat fdStat;
163 if (fstat(descriptor, &fdStat) == -1) {
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500164 return throw_exception(env, kSourceMalformedData,
Leon Scroggins III1d2bf2b2018-03-14 16:07:43 -0400165 "broken file descriptor; fstat returned -1", nullptr, source);
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500166 }
167
Nick Kralevich4b3a08c2019-01-28 10:39:10 -0800168 int dupDescriptor = fcntl(descriptor, F_DUPFD_CLOEXEC, 0);
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500169 FILE* file = fdopen(dupDescriptor, "r");
170 if (file == NULL) {
171 close(dupDescriptor);
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500172 return throw_exception(env, kSourceMalformedData, "Could not open file",
Leon Scroggins IIIcf7294f2018-03-22 09:21:29 -0400173 nullptr, source);
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500174 }
Leon Scroggins III0c699ad2018-05-07 16:20:13 -0400175
Leon Scroggins IIIc3fda892020-09-10 14:57:11 -0400176 std::unique_ptr<SkFILEStream> fileStream;
177 if (length == -1) {
178 // -1 corresponds to AssetFileDescriptor.UNKNOWN_LENGTH. Pass no length
179 // so SkFILEStream will figure out the size of the file on its own.
180 fileStream.reset(new SkFILEStream(file));
181 } else {
182 fileStream.reset(new SkFILEStream(file, length));
183 }
Chong Zhangcba27922019-07-12 16:38:47 -0700184 return native_create(env, std::move(fileStream), source, preferAnimation);
Derek Sollenbergerc5882c42019-10-25 11:11:32 -0400185#endif
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500186}
187
188static jobject ImageDecoder_nCreateInputStream(JNIEnv* env, jobject /*clazz*/,
Chong Zhangcba27922019-07-12 16:38:47 -0700189 jobject is, jbyteArray storage, jboolean preferAnimation, jobject source) {
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500190 std::unique_ptr<SkStream> stream(CreateJavaInputStreamAdaptor(env, is, storage, false));
191
192 if (!stream.get()) {
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500193 return throw_exception(env, kSourceMalformedData, "Failed to create a stream",
Leon Scroggins III1d2bf2b2018-03-14 16:07:43 -0400194 nullptr, source);
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500195 }
Leon Scroggins III1d2bf2b2018-03-14 16:07:43 -0400196
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500197 std::unique_ptr<SkStream> bufferedStream(
Leon Scroggins III2bcdf6f2020-04-21 14:08:32 -0400198 skia::FrontBufferedStream::Make(std::move(stream), SkCodec::MinBufferedBytesNeeded()));
Chong Zhangcba27922019-07-12 16:38:47 -0700199 return native_create(env, std::move(bufferedStream), source, preferAnimation);
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500200}
201
Chong Zhangcba27922019-07-12 16:38:47 -0700202static jobject ImageDecoder_nCreateAsset(JNIEnv* env, jobject /*clazz*/,
203 jlong assetPtr, jboolean preferAnimation, jobject source) {
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400204 Asset* asset = reinterpret_cast<Asset*>(assetPtr);
205 std::unique_ptr<SkStream> stream(new AssetStreamAdaptor(asset));
Chong Zhangcba27922019-07-12 16:38:47 -0700206 return native_create(env, std::move(stream), source, preferAnimation);
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400207}
208
Chong Zhangcba27922019-07-12 16:38:47 -0700209static jobject ImageDecoder_nCreateByteBuffer(JNIEnv* env, jobject /*clazz*/,
210 jobject jbyteBuffer, jint initialPosition, jint limit,
211 jboolean preferAnimation, jobject source) {
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400212 std::unique_ptr<SkStream> stream = CreateByteBufferStreamAdaptor(env, jbyteBuffer,
213 initialPosition, limit);
214 if (!stream) {
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500215 return throw_exception(env, kSourceMalformedData, "Failed to read ByteBuffer",
Leon Scroggins III1d2bf2b2018-03-14 16:07:43 -0400216 nullptr, source);
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400217 }
Chong Zhangcba27922019-07-12 16:38:47 -0700218 return native_create(env, std::move(stream), source, preferAnimation);
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400219}
220
Chong Zhangcba27922019-07-12 16:38:47 -0700221static jobject ImageDecoder_nCreateByteArray(JNIEnv* env, jobject /*clazz*/,
222 jbyteArray byteArray, jint offset, jint length,
223 jboolean preferAnimation, jobject source) {
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400224 std::unique_ptr<SkStream> stream(CreateByteArrayStreamAdaptor(env, byteArray, offset, length));
Chong Zhangcba27922019-07-12 16:38:47 -0700225 return native_create(env, std::move(stream), source, preferAnimation);
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400226}
227
Leon Scroggins IIIe5de9aa2018-01-10 20:56:51 -0500228jint postProcessAndRelease(JNIEnv* env, jobject jimageDecoder, std::unique_ptr<Canvas> canvas) {
Leon Scroggins III8c9d8f22018-01-14 14:41:46 -0500229 jobject jcanvas = env->NewObject(gCanvas_class, gCanvas_constructorMethodID,
230 reinterpret_cast<jlong>(canvas.get()));
231 if (!jcanvas) {
232 doThrowOOME(env, "Failed to create Java Canvas for PostProcess!");
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500233 return kUnknown;
Leon Scroggins III8c9d8f22018-01-14 14:41:46 -0500234 }
235
236 // jcanvas now owns canvas.
237 canvas.release();
238
Leon Scroggins IIIe5de9aa2018-01-10 20:56:51 -0500239 return env->CallIntMethod(jimageDecoder, gImageDecoder_postProcessMethodID, jcanvas);
Leon Scroggins III8c9d8f22018-01-14 14:41:46 -0500240}
241
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400242static jobject ImageDecoder_nDecodeBitmap(JNIEnv* env, jobject /*clazz*/, jlong nativePtr,
Leon Scroggins III1d2bf2b2018-03-14 16:07:43 -0400243 jobject jdecoder, jboolean jpostProcess,
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500244 jint targetWidth, jint targetHeight, jobject jsubset,
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400245 jboolean requireMutable, jint allocator,
246 jboolean requireUnpremul, jboolean preferRamOverQuality,
Leon Scroggins III28f3943f2019-02-19 11:03:44 -0500247 jboolean asAlphaMask, jlong colorSpaceHandle,
248 jboolean extended) {
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400249 auto* decoder = reinterpret_cast<ImageDecoder*>(nativePtr);
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500250 if (!decoder->setTargetSize(targetWidth, targetHeight)) {
251 doThrowISE(env, "Could not scale to target size!");
252 return nullptr;
253 }
Leon Scroggins III1ade46d2020-01-15 05:41:06 -0500254 if (requireUnpremul && !decoder->setUnpremultipliedRequired(true)) {
Leon Scroggins IIIea978db2018-01-13 11:40:42 -0500255 doThrowISE(env, "Cannot scale unpremultiplied pixels!");
256 return nullptr;
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400257 }
258
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400259 SkColorType colorType = kN32_SkColorType;
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500260 if (asAlphaMask && decoder->gray()) {
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400261 // We have to trick Skia to decode this to a single channel.
262 colorType = kGray_8_SkColorType;
263 } else if (preferRamOverQuality) {
264 // FIXME: The post-process might add alpha, which would make a 565
265 // result incorrect. If we call the postProcess before now and record
266 // to a picture, we can know whether alpha was added, and if not, we
267 // can still use 565.
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500268 if (decoder->opaque() && !jpostProcess) {
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400269 // If the final result will be hardware, decoding to 565 and then
270 // uploading to the gpu as 8888 will not save memory. This still
271 // may save us from using F16, but do not go down to 565.
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500272 if (allocator != kHardware_Allocator &&
273 (allocator != kDefault_Allocator || requireMutable)) {
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400274 colorType = kRGB_565_SkColorType;
275 }
276 }
277 // Otherwise, stick with N32
Leon Scroggins III28f3943f2019-02-19 11:03:44 -0500278 } else if (extended) {
279 colorType = kRGBA_F16_SkColorType;
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400280 } else {
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500281 colorType = decoder->mCodec->computeOutputColorType(colorType);
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400282 }
Leon Scroggins IIIee3bfe72019-01-31 08:42:23 -0500283
284 const bool isHardware = !requireMutable
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500285 && (allocator == kDefault_Allocator ||
286 allocator == kHardware_Allocator)
Leon Scroggins IIIee3bfe72019-01-31 08:42:23 -0500287 && colorType != kGray_8_SkColorType;
288
289 if (colorType == kRGBA_F16_SkColorType && isHardware &&
290 !uirenderer::HardwareBitmapUploader::hasFP16Support()) {
291 colorType = kN32_SkColorType;
292 }
293
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500294 if (!decoder->setOutColorType(colorType)) {
295 doThrowISE(env, "Failed to set out color type!");
296 return nullptr;
297 }
298
299 {
300 sk_sp<SkColorSpace> colorSpace = GraphicsJNI::getNativeColorSpace(colorSpaceHandle);
301 colorSpace = decoder->mCodec->computeOutputColorSpace(colorType, colorSpace);
302 decoder->setOutColorSpace(std::move(colorSpace));
303 }
304
305 if (jsubset) {
306 SkIRect subset;
307 GraphicsJNI::jrect_to_irect(env, jsubset, &subset);
308 if (!decoder->setCropRect(&subset)) {
309 doThrowISE(env, "Invalid crop rect!");
310 return nullptr;
311 }
312 }
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400313
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500314 SkImageInfo bitmapInfo = decoder->getOutputInfo();
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400315 if (asAlphaMask && colorType == kGray_8_SkColorType) {
316 bitmapInfo = bitmapInfo.makeColorType(kAlpha_8_SkColorType);
317 }
Leon Scroggins III1ade46d2020-01-15 05:41:06 -0500318
319 SkBitmap bm;
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400320 if (!bm.setInfo(bitmapInfo)) {
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500321 doThrowIOE(env, "Failed to setInfo properly");
322 return nullptr;
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400323 }
324
325 sk_sp<Bitmap> nativeBitmap;
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500326 if (allocator == kSharedMemory_Allocator) {
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400327 nativeBitmap = Bitmap::allocateAshmemBitmap(&bm);
328 } else {
329 nativeBitmap = Bitmap::allocateHeapBitmap(&bm);
330 }
331 if (!nativeBitmap) {
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500332 SkString msg;
333 msg.printf("OOM allocating Bitmap with dimensions %i x %i",
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500334 bitmapInfo.width(), bitmapInfo.height());
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500335 doThrowOOME(env, msg.c_str());
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400336 return nullptr;
337 }
338
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500339 SkCodec::Result result = decoder->decode(bm.getPixels(), bm.rowBytes());
Leon Scroggins III1d2bf2b2018-03-14 16:07:43 -0400340 jthrowable jexception = get_and_clear_exception(env);
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500341 int onPartialImageError = jexception ? kSourceException
Leon Scroggins IIIe5de9aa2018-01-10 20:56:51 -0500342 : 0; // No error.
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400343 switch (result) {
344 case SkCodec::kSuccess:
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500345 // Ignore the exception, since the decode was successful anyway.
346 jexception = nullptr;
Leon Scroggins IIIe5de9aa2018-01-10 20:56:51 -0500347 onPartialImageError = 0;
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400348 break;
349 case SkCodec::kIncompleteInput:
Leon Scroggins IIIe5de9aa2018-01-10 20:56:51 -0500350 if (!jexception) {
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500351 onPartialImageError = kSourceIncomplete;
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400352 }
353 break;
354 case SkCodec::kErrorInInput:
Leon Scroggins IIIe5de9aa2018-01-10 20:56:51 -0500355 if (!jexception) {
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500356 onPartialImageError = kSourceMalformedData;
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400357 }
358 break;
359 default:
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500360 SkString msg;
Leon Scroggins IIIe5de9aa2018-01-10 20:56:51 -0500361 msg.printf("getPixels failed with error %s", SkCodec::ResultToString(result));
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500362 doThrowIOE(env, msg.c_str());
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400363 return nullptr;
364 }
365
Leon Scroggins III1d2bf2b2018-03-14 16:07:43 -0400366 if (onPartialImageError) {
367 env->CallVoidMethod(jdecoder, gCallback_onPartialImageMethodID, onPartialImageError,
368 jexception);
Leon Scroggins IIIedf26d62018-01-09 16:55:24 -0500369 if (env->ExceptionCheck()) {
370 return nullptr;
371 }
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400372 }
373
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400374 jbyteArray ninePatchChunk = nullptr;
375 jobject ninePatchInsets = nullptr;
376
377 // Ignore ninepatch when post-processing.
378 if (!jpostProcess) {
379 // FIXME: Share more code with BitmapFactory.cpp.
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500380 auto* peeker = reinterpret_cast<NinePatchPeeker*>(decoder->mPeeker.get());
381 if (peeker->mPatch != nullptr) {
382 size_t ninePatchArraySize = peeker->mPatch->serializedSize();
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400383 ninePatchChunk = env->NewByteArray(ninePatchArraySize);
384 if (ninePatchChunk == nullptr) {
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500385 doThrowOOME(env, "Failed to allocate nine patch chunk.");
386 return nullptr;
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400387 }
388
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500389 env->SetByteArrayRegion(ninePatchChunk, 0, peeker->mPatchSize,
390 reinterpret_cast<jbyte*>(peeker->mPatch));
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400391 }
392
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500393 if (peeker->mHasInsets) {
394 ninePatchInsets = peeker->createNinePatchInsets(env, 1.0f);
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400395 if (ninePatchInsets == nullptr) {
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500396 doThrowOOME(env, "Failed to allocate nine patch insets.");
397 return nullptr;
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400398 }
399 }
400 }
401
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400402 if (jpostProcess) {
403 std::unique_ptr<Canvas> canvas(Canvas::create_canvas(bm));
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400404
Leon Scroggins III1d2bf2b2018-03-14 16:07:43 -0400405 jint pixelFormat = postProcessAndRelease(env, jdecoder, std::move(canvas));
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400406 if (env->ExceptionCheck()) {
407 return nullptr;
408 }
409
410 SkAlphaType newAlphaType = bm.alphaType();
411 switch (pixelFormat) {
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500412 case kUnknown:
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400413 break;
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500414 case kTranslucent:
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400415 newAlphaType = kPremul_SkAlphaType;
416 break;
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500417 case kOpaque:
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400418 newAlphaType = kOpaque_SkAlphaType;
419 break;
420 default:
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500421 SkString msg;
422 msg.printf("invalid return from postProcess: %i", pixelFormat);
423 doThrowIAE(env, msg.c_str());
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400424 return nullptr;
425 }
426
427 if (newAlphaType != bm.alphaType()) {
428 if (!bm.setAlphaType(newAlphaType)) {
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500429 SkString msg;
430 msg.printf("incompatible return from postProcess: %i", pixelFormat);
431 doThrowIAE(env, msg.c_str());
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400432 return nullptr;
433 }
434 nativeBitmap->setAlphaType(newAlphaType);
435 }
436 }
437
438 int bitmapCreateFlags = 0x0;
439 if (!requireUnpremul) {
440 // Even if the image is opaque, setting this flag means that
441 // if alpha is added (e.g. by PostProcess), it will be marked as
442 // premultiplied.
443 bitmapCreateFlags |= bitmap::kBitmapCreateFlag_Premultiplied;
444 }
445
446 if (requireMutable) {
447 bitmapCreateFlags |= bitmap::kBitmapCreateFlag_Mutable;
448 } else {
Leon Scroggins IIIee3bfe72019-01-31 08:42:23 -0500449 if (isHardware) {
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400450 sk_sp<Bitmap> hwBitmap = Bitmap::allocateHardwareBitmap(bm);
451 if (hwBitmap) {
452 hwBitmap->setImmutable();
453 return bitmap::createBitmap(env, hwBitmap.release(), bitmapCreateFlags,
454 ninePatchChunk, ninePatchInsets);
455 }
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500456 if (allocator == kHardware_Allocator) {
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500457 doThrowOOME(env, "failed to allocate hardware Bitmap!");
458 return nullptr;
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400459 }
460 // If we failed to create a hardware bitmap, go ahead and create a
461 // software one.
462 }
463
464 nativeBitmap->setImmutable();
465 }
466 return bitmap::createBitmap(env, nativeBitmap.release(), bitmapCreateFlags, ninePatchChunk,
467 ninePatchInsets);
468}
469
470static jobject ImageDecoder_nGetSampledSize(JNIEnv* env, jobject /*clazz*/, jlong nativePtr,
471 jint sampleSize) {
472 auto* decoder = reinterpret_cast<ImageDecoder*>(nativePtr);
Leon Scroggins III5a5c2ce2021-01-15 14:09:13 -0500473 SkISize size = decoder->getSampledDimensions(sampleSize);
Leon Scroggins IIIe5de9aa2018-01-10 20:56:51 -0500474 return env->NewObject(gSize_class, gSize_constructorMethodID, size.width(), size.height());
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400475}
476
477static void ImageDecoder_nGetPadding(JNIEnv* env, jobject /*clazz*/, jlong nativePtr,
478 jobject outPadding) {
479 auto* decoder = reinterpret_cast<ImageDecoder*>(nativePtr);
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500480 reinterpret_cast<NinePatchPeeker*>(decoder->mPeeker.get())->getPadding(env, outPadding);
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400481}
482
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500483static void ImageDecoder_nClose(JNIEnv* /*env*/, jobject /*clazz*/, jlong nativePtr) {
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400484 delete reinterpret_cast<ImageDecoder*>(nativePtr);
485}
486
Leon Scroggins III1fad09d2017-12-22 12:54:20 -0500487static jstring ImageDecoder_nGetMimeType(JNIEnv* env, jobject /*clazz*/, jlong nativePtr) {
488 auto* decoder = reinterpret_cast<ImageDecoder*>(nativePtr);
Leon Scroggins III407b5442019-11-22 17:10:20 -0500489 return getMimeTypeAsJavaString(env, decoder->mCodec->getEncodedFormat());
Leon Scroggins III1fad09d2017-12-22 12:54:20 -0500490}
491
Leon Scroggins III1a69f452018-03-29 09:48:47 -0400492static jobject ImageDecoder_nGetColorSpace(JNIEnv* env, jobject /*clazz*/, jlong nativePtr) {
493 auto* codec = reinterpret_cast<ImageDecoder*>(nativePtr)->mCodec.get();
Derek Sollenbergerbf3e4642019-01-30 11:28:27 -0500494 auto colorType = codec->computeOutputColorType(kN32_SkColorType);
Leon Scroggins III1a69f452018-03-29 09:48:47 -0400495 sk_sp<SkColorSpace> colorSpace = codec->computeOutputColorSpace(colorType);
Derek Sollenbergerbf3e4642019-01-30 11:28:27 -0500496 return GraphicsJNI::getColorSpace(env, colorSpace.get(), colorType);
Leon Scroggins III1a69f452018-03-29 09:48:47 -0400497}
498
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400499static const JNINativeMethod gImageDecoderMethods[] = {
Chong Zhangcba27922019-07-12 16:38:47 -0700500 { "nCreate", "(JZLandroid/graphics/ImageDecoder$Source;)Landroid/graphics/ImageDecoder;", (void*) ImageDecoder_nCreateAsset },
501 { "nCreate", "(Ljava/nio/ByteBuffer;IIZLandroid/graphics/ImageDecoder$Source;)Landroid/graphics/ImageDecoder;", (void*) ImageDecoder_nCreateByteBuffer },
502 { "nCreate", "([BIIZLandroid/graphics/ImageDecoder$Source;)Landroid/graphics/ImageDecoder;", (void*) ImageDecoder_nCreateByteArray },
503 { "nCreate", "(Ljava/io/InputStream;[BZLandroid/graphics/ImageDecoder$Source;)Landroid/graphics/ImageDecoder;", (void*) ImageDecoder_nCreateInputStream },
Leon Scroggins IIIc3fda892020-09-10 14:57:11 -0400504 { "nCreate", "(Ljava/io/FileDescriptor;JZLandroid/graphics/ImageDecoder$Source;)Landroid/graphics/ImageDecoder;", (void*) ImageDecoder_nCreateFd },
Leon Scroggins III28f3943f2019-02-19 11:03:44 -0500505 { "nDecodeBitmap", "(JLandroid/graphics/ImageDecoder;ZIILandroid/graphics/Rect;ZIZZZJZ)Landroid/graphics/Bitmap;",
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400506 (void*) ImageDecoder_nDecodeBitmap },
Leon Scroggins IIIe5de9aa2018-01-10 20:56:51 -0500507 { "nGetSampledSize","(JI)Landroid/util/Size;", (void*) ImageDecoder_nGetSampledSize },
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400508 { "nGetPadding", "(JLandroid/graphics/Rect;)V", (void*) ImageDecoder_nGetPadding },
Leon Scroggins IIIed074fd2017-12-11 13:47:23 -0500509 { "nClose", "(J)V", (void*) ImageDecoder_nClose},
Leon Scroggins III1fad09d2017-12-22 12:54:20 -0500510 { "nGetMimeType", "(J)Ljava/lang/String;", (void*) ImageDecoder_nGetMimeType },
Leon Scroggins III1a69f452018-03-29 09:48:47 -0400511 { "nGetColorSpace", "(J)Landroid/graphics/ColorSpace;", (void*) ImageDecoder_nGetColorSpace },
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400512};
513
514int register_android_graphics_ImageDecoder(JNIEnv* env) {
515 gImageDecoder_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/ImageDecoder"));
Leon Scroggins III7d940ba2018-04-04 16:19:33 -0400516 gImageDecoder_constructorMethodID = GetMethodIDOrDie(env, gImageDecoder_class, "<init>", "(JIIZZ)V");
Leon Scroggins IIIe5de9aa2018-01-10 20:56:51 -0500517 gImageDecoder_postProcessMethodID = GetMethodIDOrDie(env, gImageDecoder_class, "postProcessAndRelease", "(Landroid/graphics/Canvas;)I");
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400518
Leon Scroggins IIIe5de9aa2018-01-10 20:56:51 -0500519 gSize_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/util/Size"));
520 gSize_constructorMethodID = GetMethodIDOrDie(env, gSize_class, "<init>", "(II)V");
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400521
Leon Scroggins III1d2bf2b2018-03-14 16:07:43 -0400522 gDecodeException_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/ImageDecoder$DecodeException"));
523 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 -0400524
Leon Scroggins III1d2bf2b2018-03-14 16:07:43 -0400525 gCallback_onPartialImageMethodID = GetMethodIDOrDie(env, gImageDecoder_class, "onPartialImage", "(ILjava/lang/Throwable;)V");
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400526
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400527 gCanvas_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/Canvas"));
528 gCanvas_constructorMethodID = GetMethodIDOrDie(env, gCanvas_class, "<init>", "(J)V");
529 gCanvas_releaseMethodID = GetMethodIDOrDie(env, gCanvas_class, "release", "()V");
530
531 return android::RegisterMethodsOrDie(env, "android/graphics/ImageDecoder", gImageDecoderMethods,
532 NELEM(gImageDecoderMethods));
533}