blob: ea5c14486ea4e9747f02c35f254b0286fd240f61 [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
Leon Scroggins III23ac0362020-05-04 15:38:58 -040017#include "BitmapRegionDecoder.h"
Matt Sarett1f979632015-10-27 10:33:20 -040018
Leon Scroggins IIIee3bfe72019-01-31 08:42:23 -050019#include <HardwareBitmapUploader.h>
Ben Wagner60126ef2015-08-07 12:13:48 -040020#include <androidfw/Asset.h>
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080021#include <sys/stat.h>
22
Ben Wagner18bd8852016-10-24 14:50:10 -040023#include <memory>
24
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +000025#include "BitmapFactory.h"
26#include "CreateJavaOutputStreamAdaptor.h"
27#include "Gainmap.h"
28#include "GraphicsJNI.h"
29#include "SkBitmap.h"
30#include "SkCodec.h"
31#include "SkColorSpace.h"
32#include "SkData.h"
33#include "SkGainmapInfo.h"
34#include "SkStream.h"
35#include "SkStreamPriv.h"
36#include "Utils.h"
37
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080038using namespace android;
39
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +000040namespace android {
41class BitmapRegionDecoderWrapper {
42public:
43 static std::unique_ptr<BitmapRegionDecoderWrapper> Make(sk_sp<SkData> data) {
44 std::unique_ptr<skia::BitmapRegionDecoder> mainImageBRD =
45 skia::BitmapRegionDecoder::Make(std::move(data));
46 if (!mainImageBRD) {
47 return nullptr;
48 }
49
50 SkGainmapInfo gainmapInfo;
51 std::unique_ptr<SkStream> gainmapStream;
52 std::unique_ptr<skia::BitmapRegionDecoder> gainmapBRD = nullptr;
53 if (mainImageBRD->getAndroidGainmap(&gainmapInfo, &gainmapStream)) {
54 sk_sp<SkData> data = nullptr;
55 if (gainmapStream->getMemoryBase()) {
56 // It is safe to make without copy because we'll hold onto the stream.
57 data = SkData::MakeWithoutCopy(gainmapStream->getMemoryBase(),
58 gainmapStream->getLength());
59 } else {
60 data = SkCopyStreamToData(gainmapStream.get());
61 // We don't need to hold the stream anymore
62 gainmapStream = nullptr;
63 }
64 gainmapBRD = skia::BitmapRegionDecoder::Make(std::move(data));
65 }
66
67 return std::unique_ptr<BitmapRegionDecoderWrapper>(
68 new BitmapRegionDecoderWrapper(std::move(mainImageBRD), std::move(gainmapBRD),
69 gainmapInfo, std::move(gainmapStream)));
70 }
71
72 SkEncodedImageFormat getEncodedFormat() { return mMainImageBRD->getEncodedFormat(); }
73
74 SkColorType computeOutputColorType(SkColorType requestedColorType) {
75 return mMainImageBRD->computeOutputColorType(requestedColorType);
76 }
77
78 sk_sp<SkColorSpace> computeOutputColorSpace(SkColorType outputColorType,
79 sk_sp<SkColorSpace> prefColorSpace = nullptr) {
80 return mMainImageBRD->computeOutputColorSpace(outputColorType, prefColorSpace);
81 }
82
83 bool decodeRegion(SkBitmap* bitmap, skia::BRDAllocator* allocator, const SkIRect& desiredSubset,
84 int sampleSize, SkColorType colorType, bool requireUnpremul,
85 sk_sp<SkColorSpace> prefColorSpace) {
86 return mMainImageBRD->decodeRegion(bitmap, allocator, desiredSubset, sampleSize, colorType,
87 requireUnpremul, prefColorSpace);
88 }
89
John Reck95197572023-07-06 10:40:31 -040090 bool decodeGainmapRegion(sp<uirenderer::Gainmap>* outGainmap, int outWidth, int outHeight,
91 const SkIRect& desiredSubset, int sampleSize, bool requireUnpremul) {
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +000092 SkColorType decodeColorType = mGainmapBRD->computeOutputColorType(kN32_SkColorType);
93 sk_sp<SkColorSpace> decodeColorSpace =
94 mGainmapBRD->computeOutputColorSpace(decodeColorType, nullptr);
95 SkBitmap bm;
John Reck40ffc1d2023-06-20 17:26:09 -040096 // Because we must match the dimensions of the base bitmap, we always use a
97 // recycling allocator even though we are allocating a new bitmap. This is to ensure
98 // that if a recycled bitmap was used for the base image that we match the relative
99 // dimensions of that base image. The behavior of BRD here is:
100 // if inBitmap is specified -> output dimensions are always equal to the inBitmap's
101 // if no bitmap is reused -> output dimensions are the intersect of the desiredSubset &
102 // the image bounds
103 // The handling of the above conditionals are baked into the desiredSubset, so we
104 // simply need to ensure that the resulting bitmap is the exact same width/height as
105 // the specified desiredSubset regardless of the intersection to the image bounds.
106 // kPremul_SkAlphaType is used just as a placeholder as it doesn't change the underlying
107 // allocation type. RecyclingClippingPixelAllocator will populate this with the
108 // actual alpha type in either allocPixelRef() or copyIfNecessary()
John Reck95197572023-07-06 10:40:31 -0400109 sk_sp<Bitmap> nativeBitmap = Bitmap::allocateHeapBitmap(SkImageInfo::Make(
110 outWidth, outHeight, decodeColorType, kPremul_SkAlphaType, decodeColorSpace));
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000111 if (!nativeBitmap) {
112 ALOGE("OOM allocating Bitmap for Gainmap");
113 return false;
114 }
John Reck40ffc1d2023-06-20 17:26:09 -0400115 RecyclingClippingPixelAllocator allocator(nativeBitmap.get(), false);
116 if (!mGainmapBRD->decodeRegion(&bm, &allocator, desiredSubset, sampleSize, decodeColorType,
117 requireUnpremul, decodeColorSpace)) {
118 ALOGE("Error decoding Gainmap region");
119 return false;
120 }
121 allocator.copyIfNecessary();
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000122 auto gainmap = sp<uirenderer::Gainmap>::make();
123 if (!gainmap) {
124 ALOGE("OOM allocating Gainmap");
125 return false;
126 }
127 gainmap->info = mGainmapInfo;
128 gainmap->bitmap = std::move(nativeBitmap);
129 *outGainmap = std::move(gainmap);
130 return true;
131 }
132
John Reck95197572023-07-06 10:40:31 -0400133 SkIRect calculateGainmapRegion(const SkIRect& mainImageRegion, int* inOutWidth,
134 int* inOutHeight) {
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000135 const float scaleX = ((float)mGainmapBRD->width()) / mMainImageBRD->width();
136 const float scaleY = ((float)mGainmapBRD->height()) / mMainImageBRD->height();
John Reck95197572023-07-06 10:40:31 -0400137 *inOutWidth *= scaleX;
138 *inOutHeight *= scaleY;
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000139 // TODO: Account for rounding error?
140 return SkIRect::MakeLTRB(mainImageRegion.left() * scaleX, mainImageRegion.top() * scaleY,
141 mainImageRegion.right() * scaleX,
142 mainImageRegion.bottom() * scaleY);
143 }
144
145 bool hasGainmap() { return mGainmapBRD != nullptr; }
146
147 int width() const { return mMainImageBRD->width(); }
148 int height() const { return mMainImageBRD->height(); }
149
150private:
151 BitmapRegionDecoderWrapper(std::unique_ptr<skia::BitmapRegionDecoder> mainImageBRD,
152 std::unique_ptr<skia::BitmapRegionDecoder> gainmapBRD,
153 SkGainmapInfo info, std::unique_ptr<SkStream> stream)
154 : mMainImageBRD(std::move(mainImageBRD))
155 , mGainmapBRD(std::move(gainmapBRD))
156 , mGainmapInfo(info)
157 , mGainmapStream(std::move(stream)) {}
158
159 std::unique_ptr<skia::BitmapRegionDecoder> mMainImageBRD;
160 std::unique_ptr<skia::BitmapRegionDecoder> mGainmapBRD;
161 SkGainmapInfo mGainmapInfo;
162 std::unique_ptr<SkStream> mGainmapStream;
163};
164} // namespace android
165
Leon Scroggins III23ac0362020-05-04 15:38:58 -0400166static jobject createBitmapRegionDecoder(JNIEnv* env, sk_sp<SkData> data) {
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000167 auto brd = android::BitmapRegionDecoderWrapper::Make(std::move(data));
Ben Wagner18bd8852016-10-24 14:50:10 -0400168 if (!brd) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800169 doThrowIOE(env, "Image format not supported");
Matt Sarett1f979632015-10-27 10:33:20 -0400170 return nullObjectReturn("CreateBitmapRegionDecoder returned null");
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800171 }
172
Ben Wagner18bd8852016-10-24 14:50:10 -0400173 return GraphicsJNI::createBitmapRegionDecoder(env, brd.release());
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800174}
175
176static jobject nativeNewInstanceFromByteArray(JNIEnv* env, jobject, jbyteArray byteArray,
Leon Scroggins III96a13882020-03-04 10:29:04 -0500177 jint offset, jint length) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800178 AutoJavaByteArray ar(env, byteArray);
Leon Scroggins III23ac0362020-05-04 15:38:58 -0400179 return createBitmapRegionDecoder(env, SkData::MakeWithCopy(ar.ptr() + offset, length));
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800180}
181
182static jobject nativeNewInstanceFromFileDescriptor(JNIEnv* env, jobject clazz,
Leon Scroggins III96a13882020-03-04 10:29:04 -0500183 jobject fileDescriptor) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800184 NPE_CHECK_RETURN_ZERO(env, fileDescriptor);
185
Elliott Hughesa3804cf2011-04-11 16:50:19 -0700186 jint descriptor = jniGetFDFromFileDescriptor(env, fileDescriptor);
Derek Sollenberger5827cb52013-07-26 14:58:06 -0400187
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800188 struct stat fdStat;
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800189 if (fstat(descriptor, &fdStat) == -1) {
190 doThrowIOE(env, "broken file descriptor");
191 return nullObjectReturn("fstat return -1");
192 }
193
Leon Scroggins III23ac0362020-05-04 15:38:58 -0400194 return createBitmapRegionDecoder(env, SkData::MakeFromFD(descriptor));
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800195}
196
Leon Scroggins III96a13882020-03-04 10:29:04 -0500197static jobject nativeNewInstanceFromStream(JNIEnv* env, jobject clazz, jobject is, // InputStream
198 jbyteArray storage) { // byte[]
Leon Scroggins III23ac0362020-05-04 15:38:58 -0400199 jobject brd = nullptr;
200 sk_sp<SkData> data = CopyJavaInputStream(env, is, storage);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800201
Leon Scroggins III23ac0362020-05-04 15:38:58 -0400202 if (data) {
203 brd = createBitmapRegionDecoder(env, std::move(data));
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800204 }
Derek Sollenberger5827cb52013-07-26 14:58:06 -0400205 return brd;
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800206}
207
Leon Scroggins III96a13882020-03-04 10:29:04 -0500208static jobject nativeNewInstanceFromAsset(JNIEnv* env, jobject clazz, jlong native_asset) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800209 Asset* asset = reinterpret_cast<Asset*>(native_asset);
Leon Scroggins III23ac0362020-05-04 15:38:58 -0400210 sk_sp<SkData> data = CopyAssetToData(asset);
211 if (!data) {
212 return nullptr;
Leon Scroggins IIIca320212013-08-20 17:59:39 -0400213 }
Derek Sollenberger5827cb52013-07-26 14:58:06 -0400214
Leon Scroggins III23ac0362020-05-04 15:38:58 -0400215 return createBitmapRegionDecoder(env, data);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800216}
217
218/*
219 * nine patch not supported
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800220 * purgeable not supported
221 * reportSizeToVM not supported
222 */
Matt Sarett1f979632015-10-27 10:33:20 -0400223static jobject nativeDecodeRegion(JNIEnv* env, jobject, jlong brdHandle, jint inputX,
Leon Scroggins III71fae622019-03-26 16:28:41 -0400224 jint inputY, jint inputWidth, jint inputHeight, jobject options, jlong inBitmapHandle,
225 jlong colorSpaceHandle) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800226
Matt Sarett1f979632015-10-27 10:33:20 -0400227 // Set default options.
228 int sampleSize = 1;
229 SkColorType colorType = kN32_SkColorType;
230 bool requireUnpremul = false;
Leon Scroggins III71fae622019-03-26 16:28:41 -0400231 jobject javaBitmap = nullptr;
sergeyvda6c8ffc2016-11-22 18:28:54 -0800232 bool isHardware = false;
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500233 sk_sp<SkColorSpace> colorSpace = GraphicsJNI::getNativeColorSpace(colorSpaceHandle);
Matt Sarett1f979632015-10-27 10:33:20 -0400234 // Update the default options with any options supplied by the client.
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800235 if (NULL != options) {
236 sampleSize = env->GetIntField(options, gOptions_sampleSizeFieldID);
Matt Sarett1f979632015-10-27 10:33:20 -0400237 jobject jconfig = env->GetObjectField(options, gOptions_configFieldID);
238 colorType = GraphicsJNI::getNativeBitmapColorType(env, jconfig);
sergeyvda6c8ffc2016-11-22 18:28:54 -0800239 isHardware = GraphicsJNI::isHardwareConfig(env, jconfig);
Matt Sarett1f979632015-10-27 10:33:20 -0400240 requireUnpremul = !env->GetBooleanField(options, gOptions_premultipliedFieldID);
241 javaBitmap = env->GetObjectField(options, gOptions_bitmapFieldID);
242 // The Java options of ditherMode and preferQualityOverSpeed are deprecated. We will
243 // ignore the values of these fields.
244
245 // Initialize these fields to indicate a failure. If the decode succeeds, we
246 // will update them later on.
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800247 env->SetIntField(options, gOptions_widthFieldID, -1);
248 env->SetIntField(options, gOptions_heightFieldID, -1);
249 env->SetObjectField(options, gOptions_mimeFieldID, 0);
Romain Guy95648b82017-04-13 18:43:42 -0700250 env->SetObjectField(options, gOptions_outConfigFieldID, 0);
251 env->SetObjectField(options, gOptions_outColorSpaceFieldID, 0);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800252 }
253
Matt Sarett1f979632015-10-27 10:33:20 -0400254 // Recycle a bitmap if possible.
sergeyvc1c54062016-10-19 18:47:26 -0700255 android::Bitmap* recycledBitmap = nullptr;
Matt Sarett1f979632015-10-27 10:33:20 -0400256 if (javaBitmap) {
Leon Scroggins III71fae622019-03-26 16:28:41 -0400257 recycledBitmap = &bitmap::toBitmap(inBitmapHandle);
sergeyvc69853c2016-10-07 14:14:09 -0700258 if (recycledBitmap->isImmutable()) {
Matt Sarett1f979632015-10-27 10:33:20 -0400259 ALOGW("Warning: Reusing an immutable bitmap as an image decoder target.");
260 }
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800261 }
262
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000263 auto* brd = reinterpret_cast<BitmapRegionDecoderWrapper*>(brdHandle);
Leon Scroggins III8d592f92018-03-12 15:44:03 -0400264 SkColorType decodeColorType = brd->computeOutputColorType(colorType);
Alec Mouri1efd0a52022-01-20 13:58:23 -0800265
266 if (isHardware) {
267 if (decodeColorType == kRGBA_F16_SkColorType &&
Leon Scroggins IIIee3bfe72019-01-31 08:42:23 -0500268 !uirenderer::HardwareBitmapUploader::hasFP16Support()) {
Alec Mouri1efd0a52022-01-20 13:58:23 -0800269 decodeColorType = kN32_SkColorType;
270 }
271 if (decodeColorType == kRGBA_1010102_SkColorType &&
272 !uirenderer::HardwareBitmapUploader::has1010102Support()) {
273 decodeColorType = kN32_SkColorType;
274 }
Leon Scroggins IIIee3bfe72019-01-31 08:42:23 -0500275 }
Leon Scroggins III8d592f92018-03-12 15:44:03 -0400276
Matt Sarett1f979632015-10-27 10:33:20 -0400277 // Set up the pixel allocator
Leon Scroggins III23ac0362020-05-04 15:38:58 -0400278 skia::BRDAllocator* allocator = nullptr;
John Reck40ffc1d2023-06-20 17:26:09 -0400279 RecyclingClippingPixelAllocator recycleAlloc(recycledBitmap);
sergeyv45082182016-09-29 18:25:40 -0700280 HeapAllocator heapAlloc;
Matt Sarett1f979632015-10-27 10:33:20 -0400281 if (javaBitmap) {
282 allocator = &recycleAlloc;
283 // We are required to match the color type of the recycled bitmap.
Romain Guy95648b82017-04-13 18:43:42 -0700284 decodeColorType = recycledBitmap->info().colorType();
Matt Sarett1f979632015-10-27 10:33:20 -0400285 } else {
sergeyv45082182016-09-29 18:25:40 -0700286 allocator = &heapAlloc;
Matt Sarett1f979632015-10-27 10:33:20 -0400287 }
288
Leon Scroggins III8d592f92018-03-12 15:44:03 -0400289 sk_sp<SkColorSpace> decodeColorSpace = brd->computeOutputColorSpace(
290 decodeColorType, colorSpace);
291
Matt Sarett1f979632015-10-27 10:33:20 -0400292 // Decode the region.
John Reck40ffc1d2023-06-20 17:26:09 -0400293 const SkIRect subset = SkIRect::MakeXYWH(inputX, inputY, inputWidth, inputHeight);
John Reckf29ed282015-04-07 07:32:03 -0700294 SkBitmap bitmap;
Romain Guy95648b82017-04-13 18:43:42 -0700295 if (!brd->decodeRegion(&bitmap, allocator, subset, sampleSize,
296 decodeColorType, requireUnpremul, decodeColorSpace)) {
Matt Sarett1f979632015-10-27 10:33:20 -0400297 return nullObjectReturn("Failed to decode region.");
Owen Linf970c2e2012-04-25 18:49:09 +0800298 }
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800299
Matt Sarett1f979632015-10-27 10:33:20 -0400300 // If the client provided options, indicate that the decode was successful.
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800301 if (NULL != options) {
John Reckf29ed282015-04-07 07:32:03 -0700302 env->SetIntField(options, gOptions_widthFieldID, bitmap.width());
303 env->SetIntField(options, gOptions_heightFieldID, bitmap.height());
Romain Guy95648b82017-04-13 18:43:42 -0700304
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800305 env->SetObjectField(options, gOptions_mimeFieldID,
Leon Scroggins III407b5442019-11-22 17:10:20 -0500306 getMimeTypeAsJavaString(env, brd->getEncodedFormat()));
Matt Sarettd31512b2015-12-09 15:16:31 -0500307 if (env->ExceptionCheck()) {
308 return nullObjectReturn("OOM in encodedFormatToString()");
309 }
Romain Guy95648b82017-04-13 18:43:42 -0700310
311 jint configID = GraphicsJNI::colorTypeToLegacyBitmapConfig(decodeColorType);
312 if (isHardware) {
313 configID = GraphicsJNI::kHardware_LegacyBitmapConfig;
314 }
315 jobject config = env->CallStaticObjectMethod(gBitmapConfig_class,
316 gBitmapConfig_nativeToConfigMethodID, configID);
317 env->SetObjectField(options, gOptions_outConfigFieldID, config);
318
319 env->SetObjectField(options, gOptions_outColorSpaceFieldID,
Derek Sollenbergerbf3e4642019-01-30 11:28:27 -0500320 GraphicsJNI::getColorSpace(env, decodeColorSpace.get(), decodeColorType));
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800321 }
322
John Reck40ffc1d2023-06-20 17:26:09 -0400323 if (javaBitmap) {
324 recycleAlloc.copyIfNecessary();
325 }
326
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000327 sp<uirenderer::Gainmap> gainmap;
328 bool hasGainmap = brd->hasGainmap();
329 if (hasGainmap) {
John Reck95197572023-07-06 10:40:31 -0400330 int gainmapWidth = bitmap.width();
331 int gainmapHeight = bitmap.height();
John Reck40ffc1d2023-06-20 17:26:09 -0400332 if (javaBitmap) {
John Reck95197572023-07-06 10:40:31 -0400333 // If we are recycling we must match the inBitmap's relative dimensions
334 gainmapWidth = recycledBitmap->width();
335 gainmapHeight = recycledBitmap->height();
John Reck40ffc1d2023-06-20 17:26:09 -0400336 }
John Reck95197572023-07-06 10:40:31 -0400337 SkIRect gainmapSubset = brd->calculateGainmapRegion(subset, &gainmapWidth, &gainmapHeight);
338 if (!brd->decodeGainmapRegion(&gainmap, gainmapWidth, gainmapHeight, gainmapSubset,
339 sampleSize, requireUnpremul)) {
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000340 // If there is an error decoding Gainmap - we don't fail. We just don't provide Gainmap
341 hasGainmap = false;
342 }
343 }
344
Matt Sarett1f979632015-10-27 10:33:20 -0400345 // If we may have reused a bitmap, we need to indicate that the pixels have changed.
346 if (javaBitmap) {
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000347 if (hasGainmap) {
348 recycledBitmap->setGainmap(std::move(gainmap));
349 }
Romain Guy55455182017-04-15 21:41:22 -0700350 bitmap::reinitBitmap(env, javaBitmap, recycledBitmap->info(), !requireUnpremul);
Matt Sarett1f979632015-10-27 10:33:20 -0400351 return javaBitmap;
Owen Linf970c2e2012-04-25 18:49:09 +0800352 }
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800353
Chris Craik1abf5d62013-08-16 12:47:03 -0700354 int bitmapCreateFlags = 0;
Matt Sarett1f979632015-10-27 10:33:20 -0400355 if (!requireUnpremul) {
sergeyvc69853c2016-10-07 14:14:09 -0700356 bitmapCreateFlags |= android::bitmap::kBitmapCreateFlag_Premultiplied;
Matt Sarett1f979632015-10-27 10:33:20 -0400357 }
John Reck40ffc1d2023-06-20 17:26:09 -0400358
sergeyvda6c8ffc2016-11-22 18:28:54 -0800359 if (isHardware) {
360 sk_sp<Bitmap> hardwareBitmap = Bitmap::allocateHardwareBitmap(bitmap);
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000361 if (hasGainmap) {
Sally Qi587fb572023-03-03 15:50:06 -0800362 auto gm = uirenderer::Gainmap::allocateHardwareGainmap(gainmap);
363 if (gm) {
364 hardwareBitmap->setGainmap(std::move(gm));
365 }
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000366 }
sergeyvda6c8ffc2016-11-22 18:28:54 -0800367 return bitmap::createBitmap(env, hardwareBitmap.release(), bitmapCreateFlags);
368 }
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000369 Bitmap* heapBitmap = heapAlloc.getStorageObjAndReset();
370 if (hasGainmap && heapBitmap != nullptr) {
371 heapBitmap->setGainmap(std::move(gainmap));
372 }
373 return android::bitmap::createBitmap(env, heapBitmap, bitmapCreateFlags);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800374}
375
Ashok Bhatb091d472014-01-08 14:32:49 +0000376static jint nativeGetHeight(JNIEnv* env, jobject, jlong brdHandle) {
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000377 auto* brd = reinterpret_cast<BitmapRegionDecoderWrapper*>(brdHandle);
Matt Sarett1f979632015-10-27 10:33:20 -0400378 return static_cast<jint>(brd->height());
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800379}
380
Ashok Bhatb091d472014-01-08 14:32:49 +0000381static jint nativeGetWidth(JNIEnv* env, jobject, jlong brdHandle) {
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000382 auto* brd = reinterpret_cast<BitmapRegionDecoderWrapper*>(brdHandle);
Matt Sarett1f979632015-10-27 10:33:20 -0400383 return static_cast<jint>(brd->width());
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800384}
385
Ashok Bhatb091d472014-01-08 14:32:49 +0000386static void nativeClean(JNIEnv* env, jobject, jlong brdHandle) {
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000387 auto* brd = reinterpret_cast<BitmapRegionDecoderWrapper*>(brdHandle);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800388 delete brd;
389}
390
391///////////////////////////////////////////////////////////////////////////////
392
Daniel Micay76f6a862015-09-19 17:31:01 -0400393static const JNINativeMethod gBitmapRegionDecoderMethods[] = {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800394 { "nativeDecodeRegion",
Leon Scroggins III71fae622019-03-26 16:28:41 -0400395 "(JIIIILandroid/graphics/BitmapFactory$Options;JJ)Landroid/graphics/Bitmap;",
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800396 (void*)nativeDecodeRegion},
397
Ashok Bhatb091d472014-01-08 14:32:49 +0000398 { "nativeGetHeight", "(J)I", (void*)nativeGetHeight},
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800399
Ashok Bhatb091d472014-01-08 14:32:49 +0000400 { "nativeGetWidth", "(J)I", (void*)nativeGetWidth},
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800401
Ashok Bhatb091d472014-01-08 14:32:49 +0000402 { "nativeClean", "(J)V", (void*)nativeClean},
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800403
404 { "nativeNewInstance",
Leon Scroggins III96a13882020-03-04 10:29:04 -0500405 "([BII)Landroid/graphics/BitmapRegionDecoder;",
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800406 (void*)nativeNewInstanceFromByteArray
407 },
408
409 { "nativeNewInstance",
Leon Scroggins III96a13882020-03-04 10:29:04 -0500410 "(Ljava/io/InputStream;[B)Landroid/graphics/BitmapRegionDecoder;",
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800411 (void*)nativeNewInstanceFromStream
412 },
413
414 { "nativeNewInstance",
Leon Scroggins III96a13882020-03-04 10:29:04 -0500415 "(Ljava/io/FileDescriptor;)Landroid/graphics/BitmapRegionDecoder;",
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800416 (void*)nativeNewInstanceFromFileDescriptor
417 },
418
419 { "nativeNewInstance",
Leon Scroggins III96a13882020-03-04 10:29:04 -0500420 "(J)Landroid/graphics/BitmapRegionDecoder;",
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800421 (void*)nativeNewInstanceFromAsset
422 },
423};
424
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800425int register_android_graphics_BitmapRegionDecoder(JNIEnv* env)
426{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800427 return android::RegisterMethodsOrDie(env, "android/graphics/BitmapRegionDecoder",
428 gBitmapRegionDecoderMethods, NELEM(gBitmapRegionDecoderMethods));
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800429}