blob: 712351382d97805ea4483ba1371b3b85a2306eeb [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
25#include "SkBitmap.h"
26#include "SkBitmapRegionDecoder.h"
27#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
Ben Wagnerd8b5c312016-08-03 15:55:25 -040039static jobject createBitmapRegionDecoder(JNIEnv* env, std::unique_ptr<SkStreamRewindable> stream) {
Ben Wagner18bd8852016-10-24 14:50:10 -040040 std::unique_ptr<SkBitmapRegionDecoder> brd(
Ben Wagnerd8b5c312016-08-03 15:55:25 -040041 SkBitmapRegionDecoder::Create(stream.release(),
42 SkBitmapRegionDecoder::kAndroidCodec_Strategy));
Ben Wagner18bd8852016-10-24 14:50:10 -040043 if (!brd) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080044 doThrowIOE(env, "Image format not supported");
Matt Sarett1f979632015-10-27 10:33:20 -040045 return nullObjectReturn("CreateBitmapRegionDecoder returned null");
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080046 }
47
Ben Wagner18bd8852016-10-24 14:50:10 -040048 return GraphicsJNI::createBitmapRegionDecoder(env, brd.release());
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080049}
50
51static jobject nativeNewInstanceFromByteArray(JNIEnv* env, jobject, jbyteArray byteArray,
Ashok Bhatb091d472014-01-08 14:32:49 +000052 jint offset, jint length, jboolean isShareable) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080053 /* If isShareable we could decide to just wrap the java array and
54 share it, but that means adding a globalref to the java array object
55 For now we just always copy the array's data if isShareable.
56 */
57 AutoJavaByteArray ar(env, byteArray);
Ben Wagnerd8b5c312016-08-03 15:55:25 -040058 std::unique_ptr<SkMemoryStream> stream(new SkMemoryStream(ar.ptr() + offset, length, true));
Derek Sollenberger5827cb52013-07-26 14:58:06 -040059
Leon Scroggins III34497892015-01-20 15:52:43 -050060 // the decoder owns the stream.
Ben Wagnerd8b5c312016-08-03 15:55:25 -040061 jobject brd = createBitmapRegionDecoder(env, std::move(stream));
Derek Sollenberger5827cb52013-07-26 14:58:06 -040062 return brd;
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080063}
64
65static jobject nativeNewInstanceFromFileDescriptor(JNIEnv* env, jobject clazz,
66 jobject fileDescriptor, jboolean isShareable) {
67 NPE_CHECK_RETURN_ZERO(env, fileDescriptor);
68
Elliott Hughesa3804cf2011-04-11 16:50:19 -070069 jint descriptor = jniGetFDFromFileDescriptor(env, fileDescriptor);
Derek Sollenberger5827cb52013-07-26 14:58:06 -040070
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080071 struct stat fdStat;
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080072 if (fstat(descriptor, &fdStat) == -1) {
73 doThrowIOE(env, "broken file descriptor");
74 return nullObjectReturn("fstat return -1");
75 }
76
Ben Wagnerd8b5c312016-08-03 15:55:25 -040077 sk_sp<SkData> data(SkData::MakeFromFD(descriptor));
78 std::unique_ptr<SkMemoryStream> stream(new SkMemoryStream(std::move(data)));
Wei-Ta Chen58c1579c2010-10-21 20:56:28 -070079
Leon Scroggins III34497892015-01-20 15:52:43 -050080 // the decoder owns the stream.
Ben Wagnerd8b5c312016-08-03 15:55:25 -040081 jobject brd = createBitmapRegionDecoder(env, std::move(stream));
Derek Sollenberger5827cb52013-07-26 14:58:06 -040082 return brd;
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080083}
84
85static jobject nativeNewInstanceFromStream(JNIEnv* env, jobject clazz,
86 jobject is, // InputStream
87 jbyteArray storage, // byte[]
88 jboolean isShareable) {
Derek Sollenberger5827cb52013-07-26 14:58:06 -040089 jobject brd = NULL;
Leon Scroggins IIIca320212013-08-20 17:59:39 -040090 // for now we don't allow shareable with java inputstreams
Ben Wagnerd8b5c312016-08-03 15:55:25 -040091 std::unique_ptr<SkStreamRewindable> stream(CopyJavaInputStream(env, is, storage));
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080092
93 if (stream) {
Leon Scroggins III34497892015-01-20 15:52:43 -050094 // the decoder owns the stream.
Ben Wagnerd8b5c312016-08-03 15:55:25 -040095 brd = createBitmapRegionDecoder(env, std::move(stream));
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080096 }
Derek Sollenberger5827cb52013-07-26 14:58:06 -040097 return brd;
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080098}
99
100static jobject nativeNewInstanceFromAsset(JNIEnv* env, jobject clazz,
Ashok Bhatb091d472014-01-08 14:32:49 +0000101 jlong native_asset, // Asset
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800102 jboolean isShareable) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800103 Asset* asset = reinterpret_cast<Asset*>(native_asset);
Ben Wagnerd8b5c312016-08-03 15:55:25 -0400104 std::unique_ptr<SkMemoryStream> stream(CopyAssetToStream(asset));
Leon Scroggins III34497892015-01-20 15:52:43 -0500105 if (NULL == stream) {
Leon Scroggins IIIca320212013-08-20 17:59:39 -0400106 return NULL;
107 }
Derek Sollenberger5827cb52013-07-26 14:58:06 -0400108
Leon Scroggins III34497892015-01-20 15:52:43 -0500109 // the decoder owns the stream.
Ben Wagnerd8b5c312016-08-03 15:55:25 -0400110 jobject brd = createBitmapRegionDecoder(env, std::move(stream));
Derek Sollenberger5827cb52013-07-26 14:58:06 -0400111 return brd;
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800112}
113
114/*
115 * nine patch not supported
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800116 * purgeable not supported
117 * reportSizeToVM not supported
118 */
Matt Sarett1f979632015-10-27 10:33:20 -0400119static jobject nativeDecodeRegion(JNIEnv* env, jobject, jlong brdHandle, jint inputX,
Leon Scroggins III71fae622019-03-26 16:28:41 -0400120 jint inputY, jint inputWidth, jint inputHeight, jobject options, jlong inBitmapHandle,
121 jlong colorSpaceHandle) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800122
Matt Sarett1f979632015-10-27 10:33:20 -0400123 // Set default options.
124 int sampleSize = 1;
125 SkColorType colorType = kN32_SkColorType;
126 bool requireUnpremul = false;
Leon Scroggins III71fae622019-03-26 16:28:41 -0400127 jobject javaBitmap = nullptr;
sergeyvda6c8ffc2016-11-22 18:28:54 -0800128 bool isHardware = false;
Leon Scroggins III0e443d162018-12-19 11:38:35 -0500129 sk_sp<SkColorSpace> colorSpace = GraphicsJNI::getNativeColorSpace(colorSpaceHandle);
Matt Sarett1f979632015-10-27 10:33:20 -0400130 // Update the default options with any options supplied by the client.
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800131 if (NULL != options) {
132 sampleSize = env->GetIntField(options, gOptions_sampleSizeFieldID);
Matt Sarett1f979632015-10-27 10:33:20 -0400133 jobject jconfig = env->GetObjectField(options, gOptions_configFieldID);
134 colorType = GraphicsJNI::getNativeBitmapColorType(env, jconfig);
sergeyvda6c8ffc2016-11-22 18:28:54 -0800135 isHardware = GraphicsJNI::isHardwareConfig(env, jconfig);
Matt Sarett1f979632015-10-27 10:33:20 -0400136 requireUnpremul = !env->GetBooleanField(options, gOptions_premultipliedFieldID);
137 javaBitmap = env->GetObjectField(options, gOptions_bitmapFieldID);
138 // The Java options of ditherMode and preferQualityOverSpeed are deprecated. We will
139 // ignore the values of these fields.
140
141 // Initialize these fields to indicate a failure. If the decode succeeds, we
142 // will update them later on.
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800143 env->SetIntField(options, gOptions_widthFieldID, -1);
144 env->SetIntField(options, gOptions_heightFieldID, -1);
145 env->SetObjectField(options, gOptions_mimeFieldID, 0);
Romain Guy95648b82017-04-13 18:43:42 -0700146 env->SetObjectField(options, gOptions_outConfigFieldID, 0);
147 env->SetObjectField(options, gOptions_outColorSpaceFieldID, 0);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800148 }
149
Matt Sarett1f979632015-10-27 10:33:20 -0400150 // Recycle a bitmap if possible.
sergeyvc1c54062016-10-19 18:47:26 -0700151 android::Bitmap* recycledBitmap = nullptr;
Matt Sarett1f979632015-10-27 10:33:20 -0400152 size_t recycledBytes = 0;
153 if (javaBitmap) {
Leon Scroggins III71fae622019-03-26 16:28:41 -0400154 recycledBitmap = &bitmap::toBitmap(inBitmapHandle);
sergeyvc69853c2016-10-07 14:14:09 -0700155 if (recycledBitmap->isImmutable()) {
Matt Sarett1f979632015-10-27 10:33:20 -0400156 ALOGW("Warning: Reusing an immutable bitmap as an image decoder target.");
157 }
Leon Scroggins IIIca8aef62019-03-26 12:11:27 -0400158 recycledBytes = recycledBitmap->getAllocationByteCount();
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800159 }
160
Leon Scroggins III8d592f92018-03-12 15:44:03 -0400161 SkBitmapRegionDecoder* brd = reinterpret_cast<SkBitmapRegionDecoder*>(brdHandle);
162 SkColorType decodeColorType = brd->computeOutputColorType(colorType);
Leon Scroggins IIIee3bfe72019-01-31 08:42:23 -0500163 if (decodeColorType == kRGBA_F16_SkColorType && isHardware &&
164 !uirenderer::HardwareBitmapUploader::hasFP16Support()) {
165 decodeColorType = kN32_SkColorType;
166 }
Leon Scroggins III8d592f92018-03-12 15:44:03 -0400167
Matt Sarett1f979632015-10-27 10:33:20 -0400168 // Set up the pixel allocator
169 SkBRDAllocator* allocator = nullptr;
170 RecyclingClippingPixelAllocator recycleAlloc(recycledBitmap, recycledBytes);
sergeyv45082182016-09-29 18:25:40 -0700171 HeapAllocator heapAlloc;
Matt Sarett1f979632015-10-27 10:33:20 -0400172 if (javaBitmap) {
173 allocator = &recycleAlloc;
174 // We are required to match the color type of the recycled bitmap.
Romain Guy95648b82017-04-13 18:43:42 -0700175 decodeColorType = recycledBitmap->info().colorType();
Matt Sarett1f979632015-10-27 10:33:20 -0400176 } else {
sergeyv45082182016-09-29 18:25:40 -0700177 allocator = &heapAlloc;
Matt Sarett1f979632015-10-27 10:33:20 -0400178 }
179
Leon Scroggins III8d592f92018-03-12 15:44:03 -0400180 sk_sp<SkColorSpace> decodeColorSpace = brd->computeOutputColorSpace(
181 decodeColorType, colorSpace);
182
Matt Sarett1f979632015-10-27 10:33:20 -0400183 // Decode the region.
184 SkIRect subset = SkIRect::MakeXYWH(inputX, inputY, inputWidth, inputHeight);
John Reckf29ed282015-04-07 07:32:03 -0700185 SkBitmap bitmap;
Romain Guy95648b82017-04-13 18:43:42 -0700186 if (!brd->decodeRegion(&bitmap, allocator, subset, sampleSize,
187 decodeColorType, requireUnpremul, decodeColorSpace)) {
Matt Sarett1f979632015-10-27 10:33:20 -0400188 return nullObjectReturn("Failed to decode region.");
Owen Linf970c2e2012-04-25 18:49:09 +0800189 }
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800190
Matt Sarett1f979632015-10-27 10:33:20 -0400191 // If the client provided options, indicate that the decode was successful.
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800192 if (NULL != options) {
John Reckf29ed282015-04-07 07:32:03 -0700193 env->SetIntField(options, gOptions_widthFieldID, bitmap.width());
194 env->SetIntField(options, gOptions_heightFieldID, bitmap.height());
Romain Guy95648b82017-04-13 18:43:42 -0700195
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800196 env->SetObjectField(options, gOptions_mimeFieldID,
Leon Scroggins III407b5442019-11-22 17:10:20 -0500197 getMimeTypeAsJavaString(env, brd->getEncodedFormat()));
Matt Sarettd31512b2015-12-09 15:16:31 -0500198 if (env->ExceptionCheck()) {
199 return nullObjectReturn("OOM in encodedFormatToString()");
200 }
Romain Guy95648b82017-04-13 18:43:42 -0700201
202 jint configID = GraphicsJNI::colorTypeToLegacyBitmapConfig(decodeColorType);
203 if (isHardware) {
204 configID = GraphicsJNI::kHardware_LegacyBitmapConfig;
205 }
206 jobject config = env->CallStaticObjectMethod(gBitmapConfig_class,
207 gBitmapConfig_nativeToConfigMethodID, configID);
208 env->SetObjectField(options, gOptions_outConfigFieldID, config);
209
210 env->SetObjectField(options, gOptions_outColorSpaceFieldID,
Derek Sollenbergerbf3e4642019-01-30 11:28:27 -0500211 GraphicsJNI::getColorSpace(env, decodeColorSpace.get(), decodeColorType));
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800212 }
213
Matt Sarett1f979632015-10-27 10:33:20 -0400214 // If we may have reused a bitmap, we need to indicate that the pixels have changed.
215 if (javaBitmap) {
216 recycleAlloc.copyIfNecessary();
Romain Guy55455182017-04-15 21:41:22 -0700217 bitmap::reinitBitmap(env, javaBitmap, recycledBitmap->info(), !requireUnpremul);
Matt Sarett1f979632015-10-27 10:33:20 -0400218 return javaBitmap;
Owen Linf970c2e2012-04-25 18:49:09 +0800219 }
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800220
Chris Craik1abf5d62013-08-16 12:47:03 -0700221 int bitmapCreateFlags = 0;
Matt Sarett1f979632015-10-27 10:33:20 -0400222 if (!requireUnpremul) {
sergeyvc69853c2016-10-07 14:14:09 -0700223 bitmapCreateFlags |= android::bitmap::kBitmapCreateFlag_Premultiplied;
Matt Sarett1f979632015-10-27 10:33:20 -0400224 }
sergeyvda6c8ffc2016-11-22 18:28:54 -0800225 if (isHardware) {
226 sk_sp<Bitmap> hardwareBitmap = Bitmap::allocateHardwareBitmap(bitmap);
227 return bitmap::createBitmap(env, hardwareBitmap.release(), bitmapCreateFlags);
228 }
sergeyvc69853c2016-10-07 14:14:09 -0700229 return android::bitmap::createBitmap(env, heapAlloc.getStorageObjAndReset(), bitmapCreateFlags);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800230}
231
Ashok Bhatb091d472014-01-08 14:32:49 +0000232static jint nativeGetHeight(JNIEnv* env, jobject, jlong brdHandle) {
Matt Sarett1f979632015-10-27 10:33:20 -0400233 SkBitmapRegionDecoder* brd =
234 reinterpret_cast<SkBitmapRegionDecoder*>(brdHandle);
235 return static_cast<jint>(brd->height());
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800236}
237
Ashok Bhatb091d472014-01-08 14:32:49 +0000238static jint nativeGetWidth(JNIEnv* env, jobject, jlong brdHandle) {
Matt Sarett1f979632015-10-27 10:33:20 -0400239 SkBitmapRegionDecoder* brd =
240 reinterpret_cast<SkBitmapRegionDecoder*>(brdHandle);
241 return static_cast<jint>(brd->width());
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800242}
243
Ashok Bhatb091d472014-01-08 14:32:49 +0000244static void nativeClean(JNIEnv* env, jobject, jlong brdHandle) {
Matt Sarett1f979632015-10-27 10:33:20 -0400245 SkBitmapRegionDecoder* brd =
246 reinterpret_cast<SkBitmapRegionDecoder*>(brdHandle);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800247 delete brd;
248}
249
250///////////////////////////////////////////////////////////////////////////////
251
Daniel Micay76f6a862015-09-19 17:31:01 -0400252static const JNINativeMethod gBitmapRegionDecoderMethods[] = {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800253 { "nativeDecodeRegion",
Leon Scroggins III71fae622019-03-26 16:28:41 -0400254 "(JIIIILandroid/graphics/BitmapFactory$Options;JJ)Landroid/graphics/Bitmap;",
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800255 (void*)nativeDecodeRegion},
256
Ashok Bhatb091d472014-01-08 14:32:49 +0000257 { "nativeGetHeight", "(J)I", (void*)nativeGetHeight},
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800258
Ashok Bhatb091d472014-01-08 14:32:49 +0000259 { "nativeGetWidth", "(J)I", (void*)nativeGetWidth},
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800260
Ashok Bhatb091d472014-01-08 14:32:49 +0000261 { "nativeClean", "(J)V", (void*)nativeClean},
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800262
263 { "nativeNewInstance",
264 "([BIIZ)Landroid/graphics/BitmapRegionDecoder;",
265 (void*)nativeNewInstanceFromByteArray
266 },
267
268 { "nativeNewInstance",
269 "(Ljava/io/InputStream;[BZ)Landroid/graphics/BitmapRegionDecoder;",
270 (void*)nativeNewInstanceFromStream
271 },
272
273 { "nativeNewInstance",
274 "(Ljava/io/FileDescriptor;Z)Landroid/graphics/BitmapRegionDecoder;",
275 (void*)nativeNewInstanceFromFileDescriptor
276 },
277
278 { "nativeNewInstance",
Ashok Bhatb091d472014-01-08 14:32:49 +0000279 "(JZ)Landroid/graphics/BitmapRegionDecoder;",
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800280 (void*)nativeNewInstanceFromAsset
281 },
282};
283
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800284int register_android_graphics_BitmapRegionDecoder(JNIEnv* env)
285{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800286 return android::RegisterMethodsOrDie(env, "android/graphics/BitmapRegionDecoder",
287 gBitmapRegionDecoderMethods, NELEM(gBitmapRegionDecoderMethods));
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800288}