blob: 06b4ff84909706b95fd3b67a1e6336885c4efea4 [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 "SkStream.h"
Matt Sarett1f979632015-10-27 10:33:20 -040029
Ben Wagner60126ef2015-08-07 12:13:48 -040030#include "core_jni_helpers.h"
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080031
Leon Scroggins IIIee3bfe72019-01-31 08:42:23 -050032#include <HardwareBitmapUploader.h>
Steven Moreland2279b252017-07-19 09:50:45 -070033#include <nativehelper/JNIHelp.h>
Ben Wagner60126ef2015-08-07 12:13:48 -040034#include <androidfw/Asset.h>
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080035#include <jni.h>
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080036#include <sys/stat.h>
37
Ben Wagner18bd8852016-10-24 14:50:10 -040038#include <memory>
39
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080040using namespace android;
41
Ben Wagnerd8b5c312016-08-03 15:55:25 -040042static jobject createBitmapRegionDecoder(JNIEnv* env, std::unique_ptr<SkStreamRewindable> stream) {
Ben Wagner18bd8852016-10-24 14:50:10 -040043 std::unique_ptr<SkBitmapRegionDecoder> brd(
Ben Wagnerd8b5c312016-08-03 15:55:25 -040044 SkBitmapRegionDecoder::Create(stream.release(),
45 SkBitmapRegionDecoder::kAndroidCodec_Strategy));
Ben Wagner18bd8852016-10-24 14:50:10 -040046 if (!brd) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080047 doThrowIOE(env, "Image format not supported");
Matt Sarett1f979632015-10-27 10:33:20 -040048 return nullObjectReturn("CreateBitmapRegionDecoder returned null");
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080049 }
50
Ben Wagner18bd8852016-10-24 14:50:10 -040051 return GraphicsJNI::createBitmapRegionDecoder(env, brd.release());
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080052}
53
54static jobject nativeNewInstanceFromByteArray(JNIEnv* env, jobject, jbyteArray byteArray,
Ashok Bhatb091d472014-01-08 14:32:49 +000055 jint offset, jint length, jboolean isShareable) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080056 /* If isShareable we could decide to just wrap the java array and
57 share it, but that means adding a globalref to the java array object
58 For now we just always copy the array's data if isShareable.
59 */
60 AutoJavaByteArray ar(env, byteArray);
Ben Wagnerd8b5c312016-08-03 15:55:25 -040061 std::unique_ptr<SkMemoryStream> stream(new SkMemoryStream(ar.ptr() + offset, length, true));
Derek Sollenberger5827cb52013-07-26 14:58:06 -040062
Leon Scroggins III34497892015-01-20 15:52:43 -050063 // the decoder owns the stream.
Ben Wagnerd8b5c312016-08-03 15:55:25 -040064 jobject brd = createBitmapRegionDecoder(env, std::move(stream));
Derek Sollenberger5827cb52013-07-26 14:58:06 -040065 return brd;
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080066}
67
68static jobject nativeNewInstanceFromFileDescriptor(JNIEnv* env, jobject clazz,
69 jobject fileDescriptor, jboolean isShareable) {
70 NPE_CHECK_RETURN_ZERO(env, fileDescriptor);
71
Elliott Hughesa3804cf2011-04-11 16:50:19 -070072 jint descriptor = jniGetFDFromFileDescriptor(env, fileDescriptor);
Derek Sollenberger5827cb52013-07-26 14:58:06 -040073
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080074 struct stat fdStat;
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080075 if (fstat(descriptor, &fdStat) == -1) {
76 doThrowIOE(env, "broken file descriptor");
77 return nullObjectReturn("fstat return -1");
78 }
79
Ben Wagnerd8b5c312016-08-03 15:55:25 -040080 sk_sp<SkData> data(SkData::MakeFromFD(descriptor));
81 std::unique_ptr<SkMemoryStream> stream(new SkMemoryStream(std::move(data)));
Wei-Ta Chen58c1579c2010-10-21 20:56:28 -070082
Leon Scroggins III34497892015-01-20 15:52:43 -050083 // the decoder owns the stream.
Ben Wagnerd8b5c312016-08-03 15:55:25 -040084 jobject brd = createBitmapRegionDecoder(env, std::move(stream));
Derek Sollenberger5827cb52013-07-26 14:58:06 -040085 return brd;
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080086}
87
88static jobject nativeNewInstanceFromStream(JNIEnv* env, jobject clazz,
89 jobject is, // InputStream
90 jbyteArray storage, // byte[]
91 jboolean isShareable) {
Derek Sollenberger5827cb52013-07-26 14:58:06 -040092 jobject brd = NULL;
Leon Scroggins IIIca320212013-08-20 17:59:39 -040093 // for now we don't allow shareable with java inputstreams
Ben Wagnerd8b5c312016-08-03 15:55:25 -040094 std::unique_ptr<SkStreamRewindable> stream(CopyJavaInputStream(env, is, storage));
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080095
96 if (stream) {
Leon Scroggins III34497892015-01-20 15:52:43 -050097 // the decoder owns the stream.
Ben Wagnerd8b5c312016-08-03 15:55:25 -040098 brd = createBitmapRegionDecoder(env, std::move(stream));
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080099 }
Derek Sollenberger5827cb52013-07-26 14:58:06 -0400100 return brd;
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800101}
102
103static jobject nativeNewInstanceFromAsset(JNIEnv* env, jobject clazz,
Ashok Bhatb091d472014-01-08 14:32:49 +0000104 jlong native_asset, // Asset
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800105 jboolean isShareable) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800106 Asset* asset = reinterpret_cast<Asset*>(native_asset);
Ben Wagnerd8b5c312016-08-03 15:55:25 -0400107 std::unique_ptr<SkMemoryStream> stream(CopyAssetToStream(asset));
Leon Scroggins III34497892015-01-20 15:52:43 -0500108 if (NULL == stream) {
Leon Scroggins IIIca320212013-08-20 17:59:39 -0400109 return NULL;
110 }
Derek Sollenberger5827cb52013-07-26 14:58:06 -0400111
Leon Scroggins III34497892015-01-20 15:52:43 -0500112 // the decoder owns the stream.
Ben Wagnerd8b5c312016-08-03 15:55:25 -0400113 jobject brd = createBitmapRegionDecoder(env, std::move(stream));
Derek Sollenberger5827cb52013-07-26 14:58:06 -0400114 return brd;
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800115}
116
117/*
118 * nine patch not supported
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800119 * purgeable not supported
120 * reportSizeToVM not supported
121 */
Matt Sarett1f979632015-10-27 10:33:20 -0400122static jobject nativeDecodeRegion(JNIEnv* env, jobject, jlong brdHandle, jint inputX,
Leon Scroggins III71fae622019-03-26 16:28:41 -0400123 jint inputY, jint inputWidth, jint inputHeight, jobject options, jlong inBitmapHandle,
124 jlong colorSpaceHandle) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800125
Matt Sarett1f979632015-10-27 10:33:20 -0400126 // Set default options.
127 int sampleSize = 1;
128 SkColorType colorType = kN32_SkColorType;
129 bool requireUnpremul = false;
Leon Scroggins III71fae622019-03-26 16:28:41 -0400130 jobject javaBitmap = nullptr;
sergeyvda6c8ffc2016-11-22 18:28:54 -0800131 bool isHardware = false;
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500132 sk_sp<SkColorSpace> colorSpace = GraphicsJNI::getNativeColorSpace(colorSpaceHandle);
Matt Sarett1f979632015-10-27 10:33:20 -0400133 // Update the default options with any options supplied by the client.
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800134 if (NULL != options) {
135 sampleSize = env->GetIntField(options, gOptions_sampleSizeFieldID);
Matt Sarett1f979632015-10-27 10:33:20 -0400136 jobject jconfig = env->GetObjectField(options, gOptions_configFieldID);
137 colorType = GraphicsJNI::getNativeBitmapColorType(env, jconfig);
sergeyvda6c8ffc2016-11-22 18:28:54 -0800138 isHardware = GraphicsJNI::isHardwareConfig(env, jconfig);
Matt Sarett1f979632015-10-27 10:33:20 -0400139 requireUnpremul = !env->GetBooleanField(options, gOptions_premultipliedFieldID);
140 javaBitmap = env->GetObjectField(options, gOptions_bitmapFieldID);
141 // The Java options of ditherMode and preferQualityOverSpeed are deprecated. We will
142 // ignore the values of these fields.
143
144 // Initialize these fields to indicate a failure. If the decode succeeds, we
145 // will update them later on.
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800146 env->SetIntField(options, gOptions_widthFieldID, -1);
147 env->SetIntField(options, gOptions_heightFieldID, -1);
148 env->SetObjectField(options, gOptions_mimeFieldID, 0);
Romain Guy95648b82017-04-13 18:43:42 -0700149 env->SetObjectField(options, gOptions_outConfigFieldID, 0);
150 env->SetObjectField(options, gOptions_outColorSpaceFieldID, 0);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800151 }
152
Matt Sarett1f979632015-10-27 10:33:20 -0400153 // Recycle a bitmap if possible.
sergeyvc1c54062016-10-19 18:47:26 -0700154 android::Bitmap* recycledBitmap = nullptr;
Matt Sarett1f979632015-10-27 10:33:20 -0400155 size_t recycledBytes = 0;
156 if (javaBitmap) {
Leon Scroggins III71fae622019-03-26 16:28:41 -0400157 recycledBitmap = &bitmap::toBitmap(inBitmapHandle);
sergeyvc69853c2016-10-07 14:14:09 -0700158 if (recycledBitmap->isImmutable()) {
Matt Sarett1f979632015-10-27 10:33:20 -0400159 ALOGW("Warning: Reusing an immutable bitmap as an image decoder target.");
160 }
Leon Scroggins IIIca8aef62019-03-26 12:11:27 -0400161 recycledBytes = recycledBitmap->getAllocationByteCount();
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800162 }
163
Leon Scroggins III8d592f92018-03-12 15:44:03 -0400164 SkBitmapRegionDecoder* brd = reinterpret_cast<SkBitmapRegionDecoder*>(brdHandle);
165 SkColorType decodeColorType = brd->computeOutputColorType(colorType);
Leon Scroggins IIIee3bfe72019-01-31 08:42:23 -0500166 if (decodeColorType == kRGBA_F16_SkColorType && isHardware &&
167 !uirenderer::HardwareBitmapUploader::hasFP16Support()) {
168 decodeColorType = kN32_SkColorType;
169 }
Leon Scroggins III8d592f92018-03-12 15:44:03 -0400170
Matt Sarett1f979632015-10-27 10:33:20 -0400171 // Set up the pixel allocator
172 SkBRDAllocator* allocator = nullptr;
173 RecyclingClippingPixelAllocator recycleAlloc(recycledBitmap, recycledBytes);
sergeyv45082182016-09-29 18:25:40 -0700174 HeapAllocator heapAlloc;
Matt Sarett1f979632015-10-27 10:33:20 -0400175 if (javaBitmap) {
176 allocator = &recycleAlloc;
177 // We are required to match the color type of the recycled bitmap.
Romain Guy95648b82017-04-13 18:43:42 -0700178 decodeColorType = recycledBitmap->info().colorType();
Matt Sarett1f979632015-10-27 10:33:20 -0400179 } else {
sergeyv45082182016-09-29 18:25:40 -0700180 allocator = &heapAlloc;
Matt Sarett1f979632015-10-27 10:33:20 -0400181 }
182
Leon Scroggins III8d592f92018-03-12 15:44:03 -0400183 sk_sp<SkColorSpace> decodeColorSpace = brd->computeOutputColorSpace(
184 decodeColorType, colorSpace);
185
Matt Sarett1f979632015-10-27 10:33:20 -0400186 // Decode the region.
187 SkIRect subset = SkIRect::MakeXYWH(inputX, inputY, inputWidth, inputHeight);
John Reckf29ed282015-04-07 07:32:03 -0700188 SkBitmap bitmap;
Romain Guy95648b82017-04-13 18:43:42 -0700189 if (!brd->decodeRegion(&bitmap, allocator, subset, sampleSize,
190 decodeColorType, requireUnpremul, decodeColorSpace)) {
Matt Sarett1f979632015-10-27 10:33:20 -0400191 return nullObjectReturn("Failed to decode region.");
Owen Linf970c2e2012-04-25 18:49:09 +0800192 }
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800193
Matt Sarett1f979632015-10-27 10:33:20 -0400194 // If the client provided options, indicate that the decode was successful.
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800195 if (NULL != options) {
John Reckf29ed282015-04-07 07:32:03 -0700196 env->SetIntField(options, gOptions_widthFieldID, bitmap.width());
197 env->SetIntField(options, gOptions_heightFieldID, bitmap.height());
Romain Guy95648b82017-04-13 18:43:42 -0700198
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800199 env->SetObjectField(options, gOptions_mimeFieldID,
Leon Scroggins III407b5442019-11-22 17:10:20 -0500200 getMimeTypeAsJavaString(env, brd->getEncodedFormat()));
Matt Sarettd31512b2015-12-09 15:16:31 -0500201 if (env->ExceptionCheck()) {
202 return nullObjectReturn("OOM in encodedFormatToString()");
203 }
Romain Guy95648b82017-04-13 18:43:42 -0700204
205 jint configID = GraphicsJNI::colorTypeToLegacyBitmapConfig(decodeColorType);
206 if (isHardware) {
207 configID = GraphicsJNI::kHardware_LegacyBitmapConfig;
208 }
209 jobject config = env->CallStaticObjectMethod(gBitmapConfig_class,
210 gBitmapConfig_nativeToConfigMethodID, configID);
211 env->SetObjectField(options, gOptions_outConfigFieldID, config);
212
213 env->SetObjectField(options, gOptions_outColorSpaceFieldID,
Derek Sollenbergerbf3e4642019-01-30 11:28:27 -0500214 GraphicsJNI::getColorSpace(env, decodeColorSpace.get(), decodeColorType));
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800215 }
216
Matt Sarett1f979632015-10-27 10:33:20 -0400217 // If we may have reused a bitmap, we need to indicate that the pixels have changed.
218 if (javaBitmap) {
219 recycleAlloc.copyIfNecessary();
Romain Guy55455182017-04-15 21:41:22 -0700220 bitmap::reinitBitmap(env, javaBitmap, recycledBitmap->info(), !requireUnpremul);
Matt Sarett1f979632015-10-27 10:33:20 -0400221 return javaBitmap;
Owen Linf970c2e2012-04-25 18:49:09 +0800222 }
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800223
Chris Craik1abf5d62013-08-16 12:47:03 -0700224 int bitmapCreateFlags = 0;
Matt Sarett1f979632015-10-27 10:33:20 -0400225 if (!requireUnpremul) {
sergeyvc69853c2016-10-07 14:14:09 -0700226 bitmapCreateFlags |= android::bitmap::kBitmapCreateFlag_Premultiplied;
Matt Sarett1f979632015-10-27 10:33:20 -0400227 }
sergeyvda6c8ffc2016-11-22 18:28:54 -0800228 if (isHardware) {
229 sk_sp<Bitmap> hardwareBitmap = Bitmap::allocateHardwareBitmap(bitmap);
230 return bitmap::createBitmap(env, hardwareBitmap.release(), bitmapCreateFlags);
231 }
sergeyvc69853c2016-10-07 14:14:09 -0700232 return android::bitmap::createBitmap(env, heapAlloc.getStorageObjAndReset(), bitmapCreateFlags);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800233}
234
Ashok Bhatb091d472014-01-08 14:32:49 +0000235static jint nativeGetHeight(JNIEnv* env, jobject, jlong brdHandle) {
Matt Sarett1f979632015-10-27 10:33:20 -0400236 SkBitmapRegionDecoder* brd =
237 reinterpret_cast<SkBitmapRegionDecoder*>(brdHandle);
238 return static_cast<jint>(brd->height());
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800239}
240
Ashok Bhatb091d472014-01-08 14:32:49 +0000241static jint nativeGetWidth(JNIEnv* env, jobject, jlong brdHandle) {
Matt Sarett1f979632015-10-27 10:33:20 -0400242 SkBitmapRegionDecoder* brd =
243 reinterpret_cast<SkBitmapRegionDecoder*>(brdHandle);
244 return static_cast<jint>(brd->width());
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800245}
246
Ashok Bhatb091d472014-01-08 14:32:49 +0000247static void nativeClean(JNIEnv* env, jobject, jlong brdHandle) {
Matt Sarett1f979632015-10-27 10:33:20 -0400248 SkBitmapRegionDecoder* brd =
249 reinterpret_cast<SkBitmapRegionDecoder*>(brdHandle);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800250 delete brd;
251}
252
253///////////////////////////////////////////////////////////////////////////////
254
Daniel Micay76f6a862015-09-19 17:31:01 -0400255static const JNINativeMethod gBitmapRegionDecoderMethods[] = {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800256 { "nativeDecodeRegion",
Leon Scroggins III71fae622019-03-26 16:28:41 -0400257 "(JIIIILandroid/graphics/BitmapFactory$Options;JJ)Landroid/graphics/Bitmap;",
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800258 (void*)nativeDecodeRegion},
259
Ashok Bhatb091d472014-01-08 14:32:49 +0000260 { "nativeGetHeight", "(J)I", (void*)nativeGetHeight},
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800261
Ashok Bhatb091d472014-01-08 14:32:49 +0000262 { "nativeGetWidth", "(J)I", (void*)nativeGetWidth},
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800263
Ashok Bhatb091d472014-01-08 14:32:49 +0000264 { "nativeClean", "(J)V", (void*)nativeClean},
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800265
266 { "nativeNewInstance",
267 "([BIIZ)Landroid/graphics/BitmapRegionDecoder;",
268 (void*)nativeNewInstanceFromByteArray
269 },
270
271 { "nativeNewInstance",
272 "(Ljava/io/InputStream;[BZ)Landroid/graphics/BitmapRegionDecoder;",
273 (void*)nativeNewInstanceFromStream
274 },
275
276 { "nativeNewInstance",
277 "(Ljava/io/FileDescriptor;Z)Landroid/graphics/BitmapRegionDecoder;",
278 (void*)nativeNewInstanceFromFileDescriptor
279 },
280
281 { "nativeNewInstance",
Ashok Bhatb091d472014-01-08 14:32:49 +0000282 "(JZ)Landroid/graphics/BitmapRegionDecoder;",
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800283 (void*)nativeNewInstanceFromAsset
284 },
285};
286
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800287int register_android_graphics_BitmapRegionDecoder(JNIEnv* env)
288{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800289 return android::RegisterMethodsOrDie(env, "android/graphics/BitmapRegionDecoder",
290 gBitmapRegionDecoderMethods, NELEM(gBitmapRegionDecoderMethods));
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800291}