blob: 1c20415dcc8fe6fc96d1169e826745c626241b8f [file] [log] [blame]
Wei-Ta Chen6b849e22010-09-07 17:32:18 +08001/*
2 * Copyright (C) 2010 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
Derek Sollenberger5368eda2019-10-25 11:20:03 -040017#undef LOG_TAG
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080018#define LOG_TAG "BitmapRegionDecoder"
19
Ben Wagner60126ef2015-08-07 12:13:48 -040020#include "BitmapFactory.h"
21#include "CreateJavaOutputStreamAdaptor.h"
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080022#include "GraphicsJNI.h"
Matt Sarett1f979632015-10-27 10:33:20 -040023#include "Utils.h"
24
Leon Scroggins III23ac0362020-05-04 15:38:58 -040025#include "BitmapRegionDecoder.h"
Matt Sarett1f979632015-10-27 10:33:20 -040026#include "SkBitmap.h"
Matt Sarett1f979632015-10-27 10:33:20 -040027#include "SkCodec.h"
28#include "SkData.h"
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080029#include "SkStream.h"
Matt Sarett1f979632015-10-27 10:33:20 -040030
Leon Scroggins IIIee3bfe72019-01-31 08:42:23 -050031#include <HardwareBitmapUploader.h>
Ben Wagner60126ef2015-08-07 12:13:48 -040032#include <androidfw/Asset.h>
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080033#include <sys/stat.h>
34
Ben Wagner18bd8852016-10-24 14:50:10 -040035#include <memory>
36
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080037using namespace android;
38
Leon Scroggins III23ac0362020-05-04 15:38:58 -040039static jobject createBitmapRegionDecoder(JNIEnv* env, sk_sp<SkData> data) {
40 auto brd = skia::BitmapRegionDecoder::Make(std::move(data));
Ben Wagner18bd8852016-10-24 14:50:10 -040041 if (!brd) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080042 doThrowIOE(env, "Image format not supported");
Matt Sarett1f979632015-10-27 10:33:20 -040043 return nullObjectReturn("CreateBitmapRegionDecoder returned null");
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080044 }
45
Ben Wagner18bd8852016-10-24 14:50:10 -040046 return GraphicsJNI::createBitmapRegionDecoder(env, brd.release());
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080047}
48
49static jobject nativeNewInstanceFromByteArray(JNIEnv* env, jobject, jbyteArray byteArray,
Leon Scroggins III96a13882020-03-04 10:29:04 -050050 jint offset, jint length) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080051 AutoJavaByteArray ar(env, byteArray);
Leon Scroggins III23ac0362020-05-04 15:38:58 -040052 return createBitmapRegionDecoder(env, SkData::MakeWithCopy(ar.ptr() + offset, length));
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080053}
54
55static jobject nativeNewInstanceFromFileDescriptor(JNIEnv* env, jobject clazz,
Leon Scroggins III96a13882020-03-04 10:29:04 -050056 jobject fileDescriptor) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080057 NPE_CHECK_RETURN_ZERO(env, fileDescriptor);
58
Elliott Hughesa3804cf2011-04-11 16:50:19 -070059 jint descriptor = jniGetFDFromFileDescriptor(env, fileDescriptor);
Derek Sollenberger5827cb52013-07-26 14:58:06 -040060
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080061 struct stat fdStat;
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080062 if (fstat(descriptor, &fdStat) == -1) {
63 doThrowIOE(env, "broken file descriptor");
64 return nullObjectReturn("fstat return -1");
65 }
66
Leon Scroggins III23ac0362020-05-04 15:38:58 -040067 return createBitmapRegionDecoder(env, SkData::MakeFromFD(descriptor));
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080068}
69
Leon Scroggins III96a13882020-03-04 10:29:04 -050070static jobject nativeNewInstanceFromStream(JNIEnv* env, jobject clazz, jobject is, // InputStream
71 jbyteArray storage) { // byte[]
Leon Scroggins III23ac0362020-05-04 15:38:58 -040072 jobject brd = nullptr;
73 sk_sp<SkData> data = CopyJavaInputStream(env, is, storage);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080074
Leon Scroggins III23ac0362020-05-04 15:38:58 -040075 if (data) {
76 brd = createBitmapRegionDecoder(env, std::move(data));
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080077 }
Derek Sollenberger5827cb52013-07-26 14:58:06 -040078 return brd;
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080079}
80
Leon Scroggins III96a13882020-03-04 10:29:04 -050081static jobject nativeNewInstanceFromAsset(JNIEnv* env, jobject clazz, jlong native_asset) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080082 Asset* asset = reinterpret_cast<Asset*>(native_asset);
Leon Scroggins III23ac0362020-05-04 15:38:58 -040083 sk_sp<SkData> data = CopyAssetToData(asset);
84 if (!data) {
85 return nullptr;
Leon Scroggins IIIca320212013-08-20 17:59:39 -040086 }
Derek Sollenberger5827cb52013-07-26 14:58:06 -040087
Leon Scroggins III23ac0362020-05-04 15:38:58 -040088 return createBitmapRegionDecoder(env, data);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080089}
90
91/*
92 * nine patch not supported
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080093 * purgeable not supported
94 * reportSizeToVM not supported
95 */
Matt Sarett1f979632015-10-27 10:33:20 -040096static jobject nativeDecodeRegion(JNIEnv* env, jobject, jlong brdHandle, jint inputX,
Leon Scroggins III71fae622019-03-26 16:28:41 -040097 jint inputY, jint inputWidth, jint inputHeight, jobject options, jlong inBitmapHandle,
98 jlong colorSpaceHandle) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080099
Matt Sarett1f979632015-10-27 10:33:20 -0400100 // Set default options.
101 int sampleSize = 1;
102 SkColorType colorType = kN32_SkColorType;
103 bool requireUnpremul = false;
Leon Scroggins III71fae622019-03-26 16:28:41 -0400104 jobject javaBitmap = nullptr;
sergeyvda6c8ffc2016-11-22 18:28:54 -0800105 bool isHardware = false;
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500106 sk_sp<SkColorSpace> colorSpace = GraphicsJNI::getNativeColorSpace(colorSpaceHandle);
Matt Sarett1f979632015-10-27 10:33:20 -0400107 // Update the default options with any options supplied by the client.
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800108 if (NULL != options) {
109 sampleSize = env->GetIntField(options, gOptions_sampleSizeFieldID);
Matt Sarett1f979632015-10-27 10:33:20 -0400110 jobject jconfig = env->GetObjectField(options, gOptions_configFieldID);
111 colorType = GraphicsJNI::getNativeBitmapColorType(env, jconfig);
sergeyvda6c8ffc2016-11-22 18:28:54 -0800112 isHardware = GraphicsJNI::isHardwareConfig(env, jconfig);
Matt Sarett1f979632015-10-27 10:33:20 -0400113 requireUnpremul = !env->GetBooleanField(options, gOptions_premultipliedFieldID);
114 javaBitmap = env->GetObjectField(options, gOptions_bitmapFieldID);
115 // The Java options of ditherMode and preferQualityOverSpeed are deprecated. We will
116 // ignore the values of these fields.
117
118 // Initialize these fields to indicate a failure. If the decode succeeds, we
119 // will update them later on.
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800120 env->SetIntField(options, gOptions_widthFieldID, -1);
121 env->SetIntField(options, gOptions_heightFieldID, -1);
122 env->SetObjectField(options, gOptions_mimeFieldID, 0);
Romain Guy95648b82017-04-13 18:43:42 -0700123 env->SetObjectField(options, gOptions_outConfigFieldID, 0);
124 env->SetObjectField(options, gOptions_outColorSpaceFieldID, 0);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800125 }
126
Matt Sarett1f979632015-10-27 10:33:20 -0400127 // Recycle a bitmap if possible.
sergeyvc1c54062016-10-19 18:47:26 -0700128 android::Bitmap* recycledBitmap = nullptr;
Matt Sarett1f979632015-10-27 10:33:20 -0400129 size_t recycledBytes = 0;
130 if (javaBitmap) {
Leon Scroggins III71fae622019-03-26 16:28:41 -0400131 recycledBitmap = &bitmap::toBitmap(inBitmapHandle);
sergeyvc69853c2016-10-07 14:14:09 -0700132 if (recycledBitmap->isImmutable()) {
Matt Sarett1f979632015-10-27 10:33:20 -0400133 ALOGW("Warning: Reusing an immutable bitmap as an image decoder target.");
134 }
Leon Scroggins IIIca8aef62019-03-26 12:11:27 -0400135 recycledBytes = recycledBitmap->getAllocationByteCount();
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800136 }
137
Leon Scroggins III23ac0362020-05-04 15:38:58 -0400138 auto* brd = reinterpret_cast<skia::BitmapRegionDecoder*>(brdHandle);
Leon Scroggins III8d592f92018-03-12 15:44:03 -0400139 SkColorType decodeColorType = brd->computeOutputColorType(colorType);
Alec Mouri1efd0a52022-01-20 13:58:23 -0800140
141 if (isHardware) {
142 if (decodeColorType == kRGBA_F16_SkColorType &&
Leon Scroggins IIIee3bfe72019-01-31 08:42:23 -0500143 !uirenderer::HardwareBitmapUploader::hasFP16Support()) {
Alec Mouri1efd0a52022-01-20 13:58:23 -0800144 decodeColorType = kN32_SkColorType;
145 }
146 if (decodeColorType == kRGBA_1010102_SkColorType &&
147 !uirenderer::HardwareBitmapUploader::has1010102Support()) {
148 decodeColorType = kN32_SkColorType;
149 }
Leon Scroggins IIIee3bfe72019-01-31 08:42:23 -0500150 }
Leon Scroggins III8d592f92018-03-12 15:44:03 -0400151
Matt Sarett1f979632015-10-27 10:33:20 -0400152 // Set up the pixel allocator
Leon Scroggins III23ac0362020-05-04 15:38:58 -0400153 skia::BRDAllocator* allocator = nullptr;
Matt Sarett1f979632015-10-27 10:33:20 -0400154 RecyclingClippingPixelAllocator recycleAlloc(recycledBitmap, recycledBytes);
sergeyv45082182016-09-29 18:25:40 -0700155 HeapAllocator heapAlloc;
Matt Sarett1f979632015-10-27 10:33:20 -0400156 if (javaBitmap) {
157 allocator = &recycleAlloc;
158 // We are required to match the color type of the recycled bitmap.
Romain Guy95648b82017-04-13 18:43:42 -0700159 decodeColorType = recycledBitmap->info().colorType();
Matt Sarett1f979632015-10-27 10:33:20 -0400160 } else {
sergeyv45082182016-09-29 18:25:40 -0700161 allocator = &heapAlloc;
Matt Sarett1f979632015-10-27 10:33:20 -0400162 }
163
Leon Scroggins III8d592f92018-03-12 15:44:03 -0400164 sk_sp<SkColorSpace> decodeColorSpace = brd->computeOutputColorSpace(
165 decodeColorType, colorSpace);
166
Matt Sarett1f979632015-10-27 10:33:20 -0400167 // Decode the region.
168 SkIRect subset = SkIRect::MakeXYWH(inputX, inputY, inputWidth, inputHeight);
John Reckf29ed282015-04-07 07:32:03 -0700169 SkBitmap bitmap;
Romain Guy95648b82017-04-13 18:43:42 -0700170 if (!brd->decodeRegion(&bitmap, allocator, subset, sampleSize,
171 decodeColorType, requireUnpremul, decodeColorSpace)) {
Matt Sarett1f979632015-10-27 10:33:20 -0400172 return nullObjectReturn("Failed to decode region.");
Owen Linf970c2e2012-04-25 18:49:09 +0800173 }
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800174
Matt Sarett1f979632015-10-27 10:33:20 -0400175 // If the client provided options, indicate that the decode was successful.
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800176 if (NULL != options) {
John Reckf29ed282015-04-07 07:32:03 -0700177 env->SetIntField(options, gOptions_widthFieldID, bitmap.width());
178 env->SetIntField(options, gOptions_heightFieldID, bitmap.height());
Romain Guy95648b82017-04-13 18:43:42 -0700179
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800180 env->SetObjectField(options, gOptions_mimeFieldID,
Leon Scroggins III407b5442019-11-22 17:10:20 -0500181 getMimeTypeAsJavaString(env, brd->getEncodedFormat()));
Matt Sarettd31512b2015-12-09 15:16:31 -0500182 if (env->ExceptionCheck()) {
183 return nullObjectReturn("OOM in encodedFormatToString()");
184 }
Romain Guy95648b82017-04-13 18:43:42 -0700185
186 jint configID = GraphicsJNI::colorTypeToLegacyBitmapConfig(decodeColorType);
187 if (isHardware) {
188 configID = GraphicsJNI::kHardware_LegacyBitmapConfig;
189 }
190 jobject config = env->CallStaticObjectMethod(gBitmapConfig_class,
191 gBitmapConfig_nativeToConfigMethodID, configID);
192 env->SetObjectField(options, gOptions_outConfigFieldID, config);
193
194 env->SetObjectField(options, gOptions_outColorSpaceFieldID,
Derek Sollenbergerbf3e4642019-01-30 11:28:27 -0500195 GraphicsJNI::getColorSpace(env, decodeColorSpace.get(), decodeColorType));
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800196 }
197
Matt Sarett1f979632015-10-27 10:33:20 -0400198 // If we may have reused a bitmap, we need to indicate that the pixels have changed.
199 if (javaBitmap) {
200 recycleAlloc.copyIfNecessary();
Romain Guy55455182017-04-15 21:41:22 -0700201 bitmap::reinitBitmap(env, javaBitmap, recycledBitmap->info(), !requireUnpremul);
Matt Sarett1f979632015-10-27 10:33:20 -0400202 return javaBitmap;
Owen Linf970c2e2012-04-25 18:49:09 +0800203 }
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800204
Chris Craik1abf5d62013-08-16 12:47:03 -0700205 int bitmapCreateFlags = 0;
Matt Sarett1f979632015-10-27 10:33:20 -0400206 if (!requireUnpremul) {
sergeyvc69853c2016-10-07 14:14:09 -0700207 bitmapCreateFlags |= android::bitmap::kBitmapCreateFlag_Premultiplied;
Matt Sarett1f979632015-10-27 10:33:20 -0400208 }
sergeyvda6c8ffc2016-11-22 18:28:54 -0800209 if (isHardware) {
210 sk_sp<Bitmap> hardwareBitmap = Bitmap::allocateHardwareBitmap(bitmap);
211 return bitmap::createBitmap(env, hardwareBitmap.release(), bitmapCreateFlags);
212 }
sergeyvc69853c2016-10-07 14:14:09 -0700213 return android::bitmap::createBitmap(env, heapAlloc.getStorageObjAndReset(), bitmapCreateFlags);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800214}
215
Ashok Bhatb091d472014-01-08 14:32:49 +0000216static jint nativeGetHeight(JNIEnv* env, jobject, jlong brdHandle) {
Leon Scroggins III23ac0362020-05-04 15:38:58 -0400217 auto* brd = reinterpret_cast<skia::BitmapRegionDecoder*>(brdHandle);
Matt Sarett1f979632015-10-27 10:33:20 -0400218 return static_cast<jint>(brd->height());
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800219}
220
Ashok Bhatb091d472014-01-08 14:32:49 +0000221static jint nativeGetWidth(JNIEnv* env, jobject, jlong brdHandle) {
Leon Scroggins III23ac0362020-05-04 15:38:58 -0400222 auto* brd = reinterpret_cast<skia::BitmapRegionDecoder*>(brdHandle);
Matt Sarett1f979632015-10-27 10:33:20 -0400223 return static_cast<jint>(brd->width());
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800224}
225
Ashok Bhatb091d472014-01-08 14:32:49 +0000226static void nativeClean(JNIEnv* env, jobject, jlong brdHandle) {
Leon Scroggins III23ac0362020-05-04 15:38:58 -0400227 auto* brd = reinterpret_cast<skia::BitmapRegionDecoder*>(brdHandle);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800228 delete brd;
229}
230
231///////////////////////////////////////////////////////////////////////////////
232
Daniel Micay76f6a862015-09-19 17:31:01 -0400233static const JNINativeMethod gBitmapRegionDecoderMethods[] = {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800234 { "nativeDecodeRegion",
Leon Scroggins III71fae622019-03-26 16:28:41 -0400235 "(JIIIILandroid/graphics/BitmapFactory$Options;JJ)Landroid/graphics/Bitmap;",
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800236 (void*)nativeDecodeRegion},
237
Ashok Bhatb091d472014-01-08 14:32:49 +0000238 { "nativeGetHeight", "(J)I", (void*)nativeGetHeight},
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800239
Ashok Bhatb091d472014-01-08 14:32:49 +0000240 { "nativeGetWidth", "(J)I", (void*)nativeGetWidth},
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800241
Ashok Bhatb091d472014-01-08 14:32:49 +0000242 { "nativeClean", "(J)V", (void*)nativeClean},
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800243
244 { "nativeNewInstance",
Leon Scroggins III96a13882020-03-04 10:29:04 -0500245 "([BII)Landroid/graphics/BitmapRegionDecoder;",
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800246 (void*)nativeNewInstanceFromByteArray
247 },
248
249 { "nativeNewInstance",
Leon Scroggins III96a13882020-03-04 10:29:04 -0500250 "(Ljava/io/InputStream;[B)Landroid/graphics/BitmapRegionDecoder;",
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800251 (void*)nativeNewInstanceFromStream
252 },
253
254 { "nativeNewInstance",
Leon Scroggins III96a13882020-03-04 10:29:04 -0500255 "(Ljava/io/FileDescriptor;)Landroid/graphics/BitmapRegionDecoder;",
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800256 (void*)nativeNewInstanceFromFileDescriptor
257 },
258
259 { "nativeNewInstance",
Leon Scroggins III96a13882020-03-04 10:29:04 -0500260 "(J)Landroid/graphics/BitmapRegionDecoder;",
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800261 (void*)nativeNewInstanceFromAsset
262 },
263};
264
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800265int register_android_graphics_BitmapRegionDecoder(JNIEnv* env)
266{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800267 return android::RegisterMethodsOrDie(env, "android/graphics/BitmapRegionDecoder",
268 gBitmapRegionDecoderMethods, NELEM(gBitmapRegionDecoderMethods));
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800269}