blob: 9c07e2d64c6efe8c757e288748ce7b468ba52aa4 [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
17#define LOG_TAG "BitmapRegionDecoder"
18
Ben Wagner60126ef2015-08-07 12:13:48 -040019#include "BitmapFactory.h"
20#include "CreateJavaOutputStreamAdaptor.h"
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080021#include "GraphicsJNI.h"
Matt Sarett1f979632015-10-27 10:33:20 -040022#include "Utils.h"
23
24#include "SkBitmap.h"
25#include "SkBitmapRegionDecoder.h"
26#include "SkCodec.h"
27#include "SkData.h"
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080028#include "SkUtils.h"
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080029#include "SkPixelRef.h"
30#include "SkStream.h"
Matt Sarett1f979632015-10-27 10:33:20 -040031
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080032#include "android_nio_utils.h"
Ben Wagner60126ef2015-08-07 12:13:48 -040033#include "android_util_Binder.h"
34#include "core_jni_helpers.h"
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080035
Leon Scroggins IIIee3bfe72019-01-31 08:42:23 -050036#include <HardwareBitmapUploader.h>
Steven Moreland2279b252017-07-19 09:50:45 -070037#include <nativehelper/JNIHelp.h>
Ben Wagner60126ef2015-08-07 12:13:48 -040038#include <androidfw/Asset.h>
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080039#include <binder/Parcel.h>
40#include <jni.h>
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080041#include <sys/stat.h>
42
Ben Wagner18bd8852016-10-24 14:50:10 -040043#include <memory>
44
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080045using namespace android;
46
Ben Wagnerd8b5c312016-08-03 15:55:25 -040047static jobject createBitmapRegionDecoder(JNIEnv* env, std::unique_ptr<SkStreamRewindable> stream) {
Ben Wagner18bd8852016-10-24 14:50:10 -040048 std::unique_ptr<SkBitmapRegionDecoder> brd(
Ben Wagnerd8b5c312016-08-03 15:55:25 -040049 SkBitmapRegionDecoder::Create(stream.release(),
50 SkBitmapRegionDecoder::kAndroidCodec_Strategy));
Ben Wagner18bd8852016-10-24 14:50:10 -040051 if (!brd) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080052 doThrowIOE(env, "Image format not supported");
Matt Sarett1f979632015-10-27 10:33:20 -040053 return nullObjectReturn("CreateBitmapRegionDecoder returned null");
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080054 }
55
Ben Wagner18bd8852016-10-24 14:50:10 -040056 return GraphicsJNI::createBitmapRegionDecoder(env, brd.release());
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080057}
58
59static jobject nativeNewInstanceFromByteArray(JNIEnv* env, jobject, jbyteArray byteArray,
Ashok Bhatb091d472014-01-08 14:32:49 +000060 jint offset, jint length, jboolean isShareable) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080061 /* If isShareable we could decide to just wrap the java array and
62 share it, but that means adding a globalref to the java array object
63 For now we just always copy the array's data if isShareable.
64 */
65 AutoJavaByteArray ar(env, byteArray);
Ben Wagnerd8b5c312016-08-03 15:55:25 -040066 std::unique_ptr<SkMemoryStream> stream(new SkMemoryStream(ar.ptr() + offset, length, true));
Derek Sollenberger5827cb52013-07-26 14:58:06 -040067
Leon Scroggins III34497892015-01-20 15:52:43 -050068 // the decoder owns the stream.
Ben Wagnerd8b5c312016-08-03 15:55:25 -040069 jobject brd = createBitmapRegionDecoder(env, std::move(stream));
Derek Sollenberger5827cb52013-07-26 14:58:06 -040070 return brd;
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080071}
72
73static jobject nativeNewInstanceFromFileDescriptor(JNIEnv* env, jobject clazz,
74 jobject fileDescriptor, jboolean isShareable) {
75 NPE_CHECK_RETURN_ZERO(env, fileDescriptor);
76
Elliott Hughesa3804cf2011-04-11 16:50:19 -070077 jint descriptor = jniGetFDFromFileDescriptor(env, fileDescriptor);
Derek Sollenberger5827cb52013-07-26 14:58:06 -040078
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080079 struct stat fdStat;
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080080 if (fstat(descriptor, &fdStat) == -1) {
81 doThrowIOE(env, "broken file descriptor");
82 return nullObjectReturn("fstat return -1");
83 }
84
Ben Wagnerd8b5c312016-08-03 15:55:25 -040085 sk_sp<SkData> data(SkData::MakeFromFD(descriptor));
86 std::unique_ptr<SkMemoryStream> stream(new SkMemoryStream(std::move(data)));
Wei-Ta Chen58c1579c2010-10-21 20:56:28 -070087
Leon Scroggins III34497892015-01-20 15:52:43 -050088 // the decoder owns the stream.
Ben Wagnerd8b5c312016-08-03 15:55:25 -040089 jobject brd = createBitmapRegionDecoder(env, std::move(stream));
Derek Sollenberger5827cb52013-07-26 14:58:06 -040090 return brd;
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080091}
92
93static jobject nativeNewInstanceFromStream(JNIEnv* env, jobject clazz,
94 jobject is, // InputStream
95 jbyteArray storage, // byte[]
96 jboolean isShareable) {
Derek Sollenberger5827cb52013-07-26 14:58:06 -040097 jobject brd = NULL;
Leon Scroggins IIIca320212013-08-20 17:59:39 -040098 // for now we don't allow shareable with java inputstreams
Ben Wagnerd8b5c312016-08-03 15:55:25 -040099 std::unique_ptr<SkStreamRewindable> stream(CopyJavaInputStream(env, is, storage));
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800100
101 if (stream) {
Leon Scroggins III34497892015-01-20 15:52:43 -0500102 // the decoder owns the stream.
Ben Wagnerd8b5c312016-08-03 15:55:25 -0400103 brd = createBitmapRegionDecoder(env, std::move(stream));
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800104 }
Derek Sollenberger5827cb52013-07-26 14:58:06 -0400105 return brd;
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800106}
107
108static jobject nativeNewInstanceFromAsset(JNIEnv* env, jobject clazz,
Ashok Bhatb091d472014-01-08 14:32:49 +0000109 jlong native_asset, // Asset
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800110 jboolean isShareable) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800111 Asset* asset = reinterpret_cast<Asset*>(native_asset);
Ben Wagnerd8b5c312016-08-03 15:55:25 -0400112 std::unique_ptr<SkMemoryStream> stream(CopyAssetToStream(asset));
Leon Scroggins III34497892015-01-20 15:52:43 -0500113 if (NULL == stream) {
Leon Scroggins IIIca320212013-08-20 17:59:39 -0400114 return NULL;
115 }
Derek Sollenberger5827cb52013-07-26 14:58:06 -0400116
Leon Scroggins III34497892015-01-20 15:52:43 -0500117 // the decoder owns the stream.
Ben Wagnerd8b5c312016-08-03 15:55:25 -0400118 jobject brd = createBitmapRegionDecoder(env, std::move(stream));
Derek Sollenberger5827cb52013-07-26 14:58:06 -0400119 return brd;
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800120}
121
122/*
123 * nine patch not supported
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800124 * purgeable not supported
125 * reportSizeToVM not supported
126 */
Matt Sarett1f979632015-10-27 10:33:20 -0400127static jobject nativeDecodeRegion(JNIEnv* env, jobject, jlong brdHandle, jint inputX,
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500128 jint inputY, jint inputWidth, jint inputHeight, jobject options, jlong colorSpaceHandle) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800129
Matt Sarett1f979632015-10-27 10:33:20 -0400130 // Set default options.
131 int sampleSize = 1;
132 SkColorType colorType = kN32_SkColorType;
133 bool requireUnpremul = false;
134 jobject javaBitmap = NULL;
sergeyvda6c8ffc2016-11-22 18:28:54 -0800135 bool isHardware = false;
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500136 sk_sp<SkColorSpace> colorSpace = GraphicsJNI::getNativeColorSpace(colorSpaceHandle);
Matt Sarett1f979632015-10-27 10:33:20 -0400137 // Update the default options with any options supplied by the client.
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800138 if (NULL != options) {
139 sampleSize = env->GetIntField(options, gOptions_sampleSizeFieldID);
Matt Sarett1f979632015-10-27 10:33:20 -0400140 jobject jconfig = env->GetObjectField(options, gOptions_configFieldID);
141 colorType = GraphicsJNI::getNativeBitmapColorType(env, jconfig);
sergeyvda6c8ffc2016-11-22 18:28:54 -0800142 isHardware = GraphicsJNI::isHardwareConfig(env, jconfig);
Matt Sarett1f979632015-10-27 10:33:20 -0400143 requireUnpremul = !env->GetBooleanField(options, gOptions_premultipliedFieldID);
144 javaBitmap = env->GetObjectField(options, gOptions_bitmapFieldID);
145 // The Java options of ditherMode and preferQualityOverSpeed are deprecated. We will
146 // ignore the values of these fields.
147
148 // Initialize these fields to indicate a failure. If the decode succeeds, we
149 // will update them later on.
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800150 env->SetIntField(options, gOptions_widthFieldID, -1);
151 env->SetIntField(options, gOptions_heightFieldID, -1);
152 env->SetObjectField(options, gOptions_mimeFieldID, 0);
Romain Guy95648b82017-04-13 18:43:42 -0700153 env->SetObjectField(options, gOptions_outConfigFieldID, 0);
154 env->SetObjectField(options, gOptions_outColorSpaceFieldID, 0);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800155 }
156
Matt Sarett1f979632015-10-27 10:33:20 -0400157 // Recycle a bitmap if possible.
sergeyvc1c54062016-10-19 18:47:26 -0700158 android::Bitmap* recycledBitmap = nullptr;
Matt Sarett1f979632015-10-27 10:33:20 -0400159 size_t recycledBytes = 0;
160 if (javaBitmap) {
sergeyvaed7f582016-10-14 16:30:21 -0700161 recycledBitmap = &bitmap::toBitmap(env, javaBitmap);
sergeyvc69853c2016-10-07 14:14:09 -0700162 if (recycledBitmap->isImmutable()) {
Matt Sarett1f979632015-10-27 10:33:20 -0400163 ALOGW("Warning: Reusing an immutable bitmap as an image decoder target.");
164 }
sergeyvc69853c2016-10-07 14:14:09 -0700165 recycledBytes = bitmap::getBitmapAllocationByteCount(env, javaBitmap);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800166 }
167
Leon Scroggins III8d592f92018-03-12 15:44:03 -0400168 SkBitmapRegionDecoder* brd = reinterpret_cast<SkBitmapRegionDecoder*>(brdHandle);
169 SkColorType decodeColorType = brd->computeOutputColorType(colorType);
Leon Scroggins IIIee3bfe72019-01-31 08:42:23 -0500170 if (decodeColorType == kRGBA_F16_SkColorType && isHardware &&
171 !uirenderer::HardwareBitmapUploader::hasFP16Support()) {
172 decodeColorType = kN32_SkColorType;
173 }
Leon Scroggins III8d592f92018-03-12 15:44:03 -0400174
Matt Sarett1f979632015-10-27 10:33:20 -0400175 // Set up the pixel allocator
176 SkBRDAllocator* allocator = nullptr;
177 RecyclingClippingPixelAllocator recycleAlloc(recycledBitmap, recycledBytes);
sergeyv45082182016-09-29 18:25:40 -0700178 HeapAllocator heapAlloc;
Matt Sarett1f979632015-10-27 10:33:20 -0400179 if (javaBitmap) {
180 allocator = &recycleAlloc;
181 // We are required to match the color type of the recycled bitmap.
Romain Guy95648b82017-04-13 18:43:42 -0700182 decodeColorType = recycledBitmap->info().colorType();
Matt Sarett1f979632015-10-27 10:33:20 -0400183 } else {
sergeyv45082182016-09-29 18:25:40 -0700184 allocator = &heapAlloc;
Matt Sarett1f979632015-10-27 10:33:20 -0400185 }
186
Leon Scroggins III8d592f92018-03-12 15:44:03 -0400187 sk_sp<SkColorSpace> decodeColorSpace = brd->computeOutputColorSpace(
188 decodeColorType, colorSpace);
189
Matt Sarett1f979632015-10-27 10:33:20 -0400190 // Decode the region.
191 SkIRect subset = SkIRect::MakeXYWH(inputX, inputY, inputWidth, inputHeight);
John Reckf29ed282015-04-07 07:32:03 -0700192 SkBitmap bitmap;
Romain Guy95648b82017-04-13 18:43:42 -0700193 if (!brd->decodeRegion(&bitmap, allocator, subset, sampleSize,
194 decodeColorType, requireUnpremul, decodeColorSpace)) {
Matt Sarett1f979632015-10-27 10:33:20 -0400195 return nullObjectReturn("Failed to decode region.");
Owen Linf970c2e2012-04-25 18:49:09 +0800196 }
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800197
Matt Sarett1f979632015-10-27 10:33:20 -0400198 // If the client provided options, indicate that the decode was successful.
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800199 if (NULL != options) {
John Reckf29ed282015-04-07 07:32:03 -0700200 env->SetIntField(options, gOptions_widthFieldID, bitmap.width());
201 env->SetIntField(options, gOptions_heightFieldID, bitmap.height());
Romain Guy95648b82017-04-13 18:43:42 -0700202
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800203 env->SetObjectField(options, gOptions_mimeFieldID,
Hal Canary10219fb2016-11-23 20:41:22 -0500204 encodedFormatToString(env, (SkEncodedImageFormat)brd->getEncodedFormat()));
Matt Sarettd31512b2015-12-09 15:16:31 -0500205 if (env->ExceptionCheck()) {
206 return nullObjectReturn("OOM in encodedFormatToString()");
207 }
Romain Guy95648b82017-04-13 18:43:42 -0700208
209 jint configID = GraphicsJNI::colorTypeToLegacyBitmapConfig(decodeColorType);
210 if (isHardware) {
211 configID = GraphicsJNI::kHardware_LegacyBitmapConfig;
212 }
213 jobject config = env->CallStaticObjectMethod(gBitmapConfig_class,
214 gBitmapConfig_nativeToConfigMethodID, configID);
215 env->SetObjectField(options, gOptions_outConfigFieldID, config);
216
217 env->SetObjectField(options, gOptions_outColorSpaceFieldID,
Derek Sollenbergerbf3e4642019-01-30 11:28:27 -0500218 GraphicsJNI::getColorSpace(env, decodeColorSpace.get(), decodeColorType));
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800219 }
220
Matt Sarett1f979632015-10-27 10:33:20 -0400221 // If we may have reused a bitmap, we need to indicate that the pixels have changed.
222 if (javaBitmap) {
223 recycleAlloc.copyIfNecessary();
Romain Guy55455182017-04-15 21:41:22 -0700224 bitmap::reinitBitmap(env, javaBitmap, recycledBitmap->info(), !requireUnpremul);
Matt Sarett1f979632015-10-27 10:33:20 -0400225 return javaBitmap;
Owen Linf970c2e2012-04-25 18:49:09 +0800226 }
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800227
Chris Craik1abf5d62013-08-16 12:47:03 -0700228 int bitmapCreateFlags = 0;
Matt Sarett1f979632015-10-27 10:33:20 -0400229 if (!requireUnpremul) {
sergeyvc69853c2016-10-07 14:14:09 -0700230 bitmapCreateFlags |= android::bitmap::kBitmapCreateFlag_Premultiplied;
Matt Sarett1f979632015-10-27 10:33:20 -0400231 }
sergeyvda6c8ffc2016-11-22 18:28:54 -0800232 if (isHardware) {
233 sk_sp<Bitmap> hardwareBitmap = Bitmap::allocateHardwareBitmap(bitmap);
234 return bitmap::createBitmap(env, hardwareBitmap.release(), bitmapCreateFlags);
235 }
sergeyvc69853c2016-10-07 14:14:09 -0700236 return android::bitmap::createBitmap(env, heapAlloc.getStorageObjAndReset(), bitmapCreateFlags);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800237}
238
Ashok Bhatb091d472014-01-08 14:32:49 +0000239static jint nativeGetHeight(JNIEnv* env, jobject, jlong brdHandle) {
Matt Sarett1f979632015-10-27 10:33:20 -0400240 SkBitmapRegionDecoder* brd =
241 reinterpret_cast<SkBitmapRegionDecoder*>(brdHandle);
242 return static_cast<jint>(brd->height());
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800243}
244
Ashok Bhatb091d472014-01-08 14:32:49 +0000245static jint nativeGetWidth(JNIEnv* env, jobject, jlong brdHandle) {
Matt Sarett1f979632015-10-27 10:33:20 -0400246 SkBitmapRegionDecoder* brd =
247 reinterpret_cast<SkBitmapRegionDecoder*>(brdHandle);
248 return static_cast<jint>(brd->width());
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800249}
250
Ashok Bhatb091d472014-01-08 14:32:49 +0000251static void nativeClean(JNIEnv* env, jobject, jlong brdHandle) {
Matt Sarett1f979632015-10-27 10:33:20 -0400252 SkBitmapRegionDecoder* brd =
253 reinterpret_cast<SkBitmapRegionDecoder*>(brdHandle);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800254 delete brd;
255}
256
257///////////////////////////////////////////////////////////////////////////////
258
Daniel Micay76f6a862015-09-19 17:31:01 -0400259static const JNINativeMethod gBitmapRegionDecoderMethods[] = {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800260 { "nativeDecodeRegion",
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500261 "(JIIIILandroid/graphics/BitmapFactory$Options;J)Landroid/graphics/Bitmap;",
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800262 (void*)nativeDecodeRegion},
263
Ashok Bhatb091d472014-01-08 14:32:49 +0000264 { "nativeGetHeight", "(J)I", (void*)nativeGetHeight},
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800265
Ashok Bhatb091d472014-01-08 14:32:49 +0000266 { "nativeGetWidth", "(J)I", (void*)nativeGetWidth},
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800267
Ashok Bhatb091d472014-01-08 14:32:49 +0000268 { "nativeClean", "(J)V", (void*)nativeClean},
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800269
270 { "nativeNewInstance",
271 "([BIIZ)Landroid/graphics/BitmapRegionDecoder;",
272 (void*)nativeNewInstanceFromByteArray
273 },
274
275 { "nativeNewInstance",
276 "(Ljava/io/InputStream;[BZ)Landroid/graphics/BitmapRegionDecoder;",
277 (void*)nativeNewInstanceFromStream
278 },
279
280 { "nativeNewInstance",
281 "(Ljava/io/FileDescriptor;Z)Landroid/graphics/BitmapRegionDecoder;",
282 (void*)nativeNewInstanceFromFileDescriptor
283 },
284
285 { "nativeNewInstance",
Ashok Bhatb091d472014-01-08 14:32:49 +0000286 "(JZ)Landroid/graphics/BitmapRegionDecoder;",
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800287 (void*)nativeNewInstanceFromAsset
288 },
289};
290
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800291int register_android_graphics_BitmapRegionDecoder(JNIEnv* env)
292{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800293 return android::RegisterMethodsOrDie(env, "android/graphics/BitmapRegionDecoder",
294 gBitmapRegionDecoderMethods, NELEM(gBitmapRegionDecoderMethods));
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800295}