blob: 4c9a23d3fde0798dadb222862ed7d8b9a3956554 [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
Leon Scroggins III23ac0362020-05-04 15:38:58 -040020#include "BitmapRegionDecoder.h"
Matt Sarett1f979632015-10-27 10:33:20 -040021
Leon Scroggins IIIee3bfe72019-01-31 08:42:23 -050022#include <HardwareBitmapUploader.h>
Ben Wagner60126ef2015-08-07 12:13:48 -040023#include <androidfw/Asset.h>
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080024#include <sys/stat.h>
25
Ben Wagner18bd8852016-10-24 14:50:10 -040026#include <memory>
27
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +000028#include "BitmapFactory.h"
29#include "CreateJavaOutputStreamAdaptor.h"
30#include "Gainmap.h"
31#include "GraphicsJNI.h"
32#include "SkBitmap.h"
33#include "SkCodec.h"
34#include "SkColorSpace.h"
35#include "SkData.h"
36#include "SkGainmapInfo.h"
37#include "SkStream.h"
38#include "SkStreamPriv.h"
39#include "Utils.h"
40
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080041using namespace android;
42
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +000043namespace android {
44class BitmapRegionDecoderWrapper {
45public:
46 static std::unique_ptr<BitmapRegionDecoderWrapper> Make(sk_sp<SkData> data) {
47 std::unique_ptr<skia::BitmapRegionDecoder> mainImageBRD =
48 skia::BitmapRegionDecoder::Make(std::move(data));
49 if (!mainImageBRD) {
50 return nullptr;
51 }
52
53 SkGainmapInfo gainmapInfo;
54 std::unique_ptr<SkStream> gainmapStream;
55 std::unique_ptr<skia::BitmapRegionDecoder> gainmapBRD = nullptr;
56 if (mainImageBRD->getAndroidGainmap(&gainmapInfo, &gainmapStream)) {
57 sk_sp<SkData> data = nullptr;
58 if (gainmapStream->getMemoryBase()) {
59 // It is safe to make without copy because we'll hold onto the stream.
60 data = SkData::MakeWithoutCopy(gainmapStream->getMemoryBase(),
61 gainmapStream->getLength());
62 } else {
63 data = SkCopyStreamToData(gainmapStream.get());
64 // We don't need to hold the stream anymore
65 gainmapStream = nullptr;
66 }
67 gainmapBRD = skia::BitmapRegionDecoder::Make(std::move(data));
68 }
69
70 return std::unique_ptr<BitmapRegionDecoderWrapper>(
71 new BitmapRegionDecoderWrapper(std::move(mainImageBRD), std::move(gainmapBRD),
72 gainmapInfo, std::move(gainmapStream)));
73 }
74
75 SkEncodedImageFormat getEncodedFormat() { return mMainImageBRD->getEncodedFormat(); }
76
77 SkColorType computeOutputColorType(SkColorType requestedColorType) {
78 return mMainImageBRD->computeOutputColorType(requestedColorType);
79 }
80
81 sk_sp<SkColorSpace> computeOutputColorSpace(SkColorType outputColorType,
82 sk_sp<SkColorSpace> prefColorSpace = nullptr) {
83 return mMainImageBRD->computeOutputColorSpace(outputColorType, prefColorSpace);
84 }
85
86 bool decodeRegion(SkBitmap* bitmap, skia::BRDAllocator* allocator, const SkIRect& desiredSubset,
87 int sampleSize, SkColorType colorType, bool requireUnpremul,
88 sk_sp<SkColorSpace> prefColorSpace) {
89 return mMainImageBRD->decodeRegion(bitmap, allocator, desiredSubset, sampleSize, colorType,
90 requireUnpremul, prefColorSpace);
91 }
92
93 bool decodeGainmapRegion(sp<uirenderer::Gainmap>* outGainmap, const SkIRect& desiredSubset,
94 int sampleSize, bool requireUnpremul) {
95 SkColorType decodeColorType = mGainmapBRD->computeOutputColorType(kN32_SkColorType);
96 sk_sp<SkColorSpace> decodeColorSpace =
97 mGainmapBRD->computeOutputColorSpace(decodeColorType, nullptr);
98 SkBitmap bm;
John Reck40ffc1d2023-06-20 17:26:09 -040099 // Because we must match the dimensions of the base bitmap, we always use a
100 // recycling allocator even though we are allocating a new bitmap. This is to ensure
101 // that if a recycled bitmap was used for the base image that we match the relative
102 // dimensions of that base image. The behavior of BRD here is:
103 // if inBitmap is specified -> output dimensions are always equal to the inBitmap's
104 // if no bitmap is reused -> output dimensions are the intersect of the desiredSubset &
105 // the image bounds
106 // The handling of the above conditionals are baked into the desiredSubset, so we
107 // simply need to ensure that the resulting bitmap is the exact same width/height as
108 // the specified desiredSubset regardless of the intersection to the image bounds.
109 // kPremul_SkAlphaType is used just as a placeholder as it doesn't change the underlying
110 // allocation type. RecyclingClippingPixelAllocator will populate this with the
111 // actual alpha type in either allocPixelRef() or copyIfNecessary()
112 sk_sp<Bitmap> nativeBitmap = Bitmap::allocateHeapBitmap(
113 SkImageInfo::Make(desiredSubset.width(), desiredSubset.height(), decodeColorType,
114 kPremul_SkAlphaType, decodeColorSpace));
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000115 if (!nativeBitmap) {
116 ALOGE("OOM allocating Bitmap for Gainmap");
117 return false;
118 }
John Reck40ffc1d2023-06-20 17:26:09 -0400119 RecyclingClippingPixelAllocator allocator(nativeBitmap.get(), false);
120 if (!mGainmapBRD->decodeRegion(&bm, &allocator, desiredSubset, sampleSize, decodeColorType,
121 requireUnpremul, decodeColorSpace)) {
122 ALOGE("Error decoding Gainmap region");
123 return false;
124 }
125 allocator.copyIfNecessary();
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000126 auto gainmap = sp<uirenderer::Gainmap>::make();
127 if (!gainmap) {
128 ALOGE("OOM allocating Gainmap");
129 return false;
130 }
131 gainmap->info = mGainmapInfo;
132 gainmap->bitmap = std::move(nativeBitmap);
133 *outGainmap = std::move(gainmap);
134 return true;
135 }
136
137 SkIRect calculateGainmapRegion(const SkIRect& mainImageRegion) {
138 const float scaleX = ((float)mGainmapBRD->width()) / mMainImageBRD->width();
139 const float scaleY = ((float)mGainmapBRD->height()) / mMainImageBRD->height();
140 // TODO: Account for rounding error?
141 return SkIRect::MakeLTRB(mainImageRegion.left() * scaleX, mainImageRegion.top() * scaleY,
142 mainImageRegion.right() * scaleX,
143 mainImageRegion.bottom() * scaleY);
144 }
145
146 bool hasGainmap() { return mGainmapBRD != nullptr; }
147
148 int width() const { return mMainImageBRD->width(); }
149 int height() const { return mMainImageBRD->height(); }
150
151private:
152 BitmapRegionDecoderWrapper(std::unique_ptr<skia::BitmapRegionDecoder> mainImageBRD,
153 std::unique_ptr<skia::BitmapRegionDecoder> gainmapBRD,
154 SkGainmapInfo info, std::unique_ptr<SkStream> stream)
155 : mMainImageBRD(std::move(mainImageBRD))
156 , mGainmapBRD(std::move(gainmapBRD))
157 , mGainmapInfo(info)
158 , mGainmapStream(std::move(stream)) {}
159
160 std::unique_ptr<skia::BitmapRegionDecoder> mMainImageBRD;
161 std::unique_ptr<skia::BitmapRegionDecoder> mGainmapBRD;
162 SkGainmapInfo mGainmapInfo;
163 std::unique_ptr<SkStream> mGainmapStream;
164};
165} // namespace android
166
Leon Scroggins III23ac0362020-05-04 15:38:58 -0400167static jobject createBitmapRegionDecoder(JNIEnv* env, sk_sp<SkData> data) {
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000168 auto brd = android::BitmapRegionDecoderWrapper::Make(std::move(data));
Ben Wagner18bd8852016-10-24 14:50:10 -0400169 if (!brd) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800170 doThrowIOE(env, "Image format not supported");
Matt Sarett1f979632015-10-27 10:33:20 -0400171 return nullObjectReturn("CreateBitmapRegionDecoder returned null");
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800172 }
173
Ben Wagner18bd8852016-10-24 14:50:10 -0400174 return GraphicsJNI::createBitmapRegionDecoder(env, brd.release());
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800175}
176
177static jobject nativeNewInstanceFromByteArray(JNIEnv* env, jobject, jbyteArray byteArray,
Leon Scroggins III96a13882020-03-04 10:29:04 -0500178 jint offset, jint length) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800179 AutoJavaByteArray ar(env, byteArray);
Leon Scroggins III23ac0362020-05-04 15:38:58 -0400180 return createBitmapRegionDecoder(env, SkData::MakeWithCopy(ar.ptr() + offset, length));
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800181}
182
183static jobject nativeNewInstanceFromFileDescriptor(JNIEnv* env, jobject clazz,
Leon Scroggins III96a13882020-03-04 10:29:04 -0500184 jobject fileDescriptor) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800185 NPE_CHECK_RETURN_ZERO(env, fileDescriptor);
186
Elliott Hughesa3804cf2011-04-11 16:50:19 -0700187 jint descriptor = jniGetFDFromFileDescriptor(env, fileDescriptor);
Derek Sollenberger5827cb52013-07-26 14:58:06 -0400188
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800189 struct stat fdStat;
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800190 if (fstat(descriptor, &fdStat) == -1) {
191 doThrowIOE(env, "broken file descriptor");
192 return nullObjectReturn("fstat return -1");
193 }
194
Leon Scroggins III23ac0362020-05-04 15:38:58 -0400195 return createBitmapRegionDecoder(env, SkData::MakeFromFD(descriptor));
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800196}
197
Leon Scroggins III96a13882020-03-04 10:29:04 -0500198static jobject nativeNewInstanceFromStream(JNIEnv* env, jobject clazz, jobject is, // InputStream
199 jbyteArray storage) { // byte[]
Leon Scroggins III23ac0362020-05-04 15:38:58 -0400200 jobject brd = nullptr;
201 sk_sp<SkData> data = CopyJavaInputStream(env, is, storage);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800202
Leon Scroggins III23ac0362020-05-04 15:38:58 -0400203 if (data) {
204 brd = createBitmapRegionDecoder(env, std::move(data));
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800205 }
Derek Sollenberger5827cb52013-07-26 14:58:06 -0400206 return brd;
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800207}
208
Leon Scroggins III96a13882020-03-04 10:29:04 -0500209static jobject nativeNewInstanceFromAsset(JNIEnv* env, jobject clazz, jlong native_asset) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800210 Asset* asset = reinterpret_cast<Asset*>(native_asset);
Leon Scroggins III23ac0362020-05-04 15:38:58 -0400211 sk_sp<SkData> data = CopyAssetToData(asset);
212 if (!data) {
213 return nullptr;
Leon Scroggins IIIca320212013-08-20 17:59:39 -0400214 }
Derek Sollenberger5827cb52013-07-26 14:58:06 -0400215
Leon Scroggins III23ac0362020-05-04 15:38:58 -0400216 return createBitmapRegionDecoder(env, data);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800217}
218
219/*
220 * nine patch not supported
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800221 * purgeable not supported
222 * reportSizeToVM not supported
223 */
Matt Sarett1f979632015-10-27 10:33:20 -0400224static jobject nativeDecodeRegion(JNIEnv* env, jobject, jlong brdHandle, jint inputX,
Leon Scroggins III71fae622019-03-26 16:28:41 -0400225 jint inputY, jint inputWidth, jint inputHeight, jobject options, jlong inBitmapHandle,
226 jlong colorSpaceHandle) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800227
Matt Sarett1f979632015-10-27 10:33:20 -0400228 // Set default options.
229 int sampleSize = 1;
230 SkColorType colorType = kN32_SkColorType;
231 bool requireUnpremul = false;
Leon Scroggins III71fae622019-03-26 16:28:41 -0400232 jobject javaBitmap = nullptr;
sergeyvda6c8ffc2016-11-22 18:28:54 -0800233 bool isHardware = false;
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500234 sk_sp<SkColorSpace> colorSpace = GraphicsJNI::getNativeColorSpace(colorSpaceHandle);
Matt Sarett1f979632015-10-27 10:33:20 -0400235 // Update the default options with any options supplied by the client.
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800236 if (NULL != options) {
237 sampleSize = env->GetIntField(options, gOptions_sampleSizeFieldID);
Matt Sarett1f979632015-10-27 10:33:20 -0400238 jobject jconfig = env->GetObjectField(options, gOptions_configFieldID);
239 colorType = GraphicsJNI::getNativeBitmapColorType(env, jconfig);
sergeyvda6c8ffc2016-11-22 18:28:54 -0800240 isHardware = GraphicsJNI::isHardwareConfig(env, jconfig);
Matt Sarett1f979632015-10-27 10:33:20 -0400241 requireUnpremul = !env->GetBooleanField(options, gOptions_premultipliedFieldID);
242 javaBitmap = env->GetObjectField(options, gOptions_bitmapFieldID);
243 // The Java options of ditherMode and preferQualityOverSpeed are deprecated. We will
244 // ignore the values of these fields.
245
246 // Initialize these fields to indicate a failure. If the decode succeeds, we
247 // will update them later on.
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800248 env->SetIntField(options, gOptions_widthFieldID, -1);
249 env->SetIntField(options, gOptions_heightFieldID, -1);
250 env->SetObjectField(options, gOptions_mimeFieldID, 0);
Romain Guy95648b82017-04-13 18:43:42 -0700251 env->SetObjectField(options, gOptions_outConfigFieldID, 0);
252 env->SetObjectField(options, gOptions_outColorSpaceFieldID, 0);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800253 }
254
Matt Sarett1f979632015-10-27 10:33:20 -0400255 // Recycle a bitmap if possible.
sergeyvc1c54062016-10-19 18:47:26 -0700256 android::Bitmap* recycledBitmap = nullptr;
Matt Sarett1f979632015-10-27 10:33:20 -0400257 if (javaBitmap) {
Leon Scroggins III71fae622019-03-26 16:28:41 -0400258 recycledBitmap = &bitmap::toBitmap(inBitmapHandle);
sergeyvc69853c2016-10-07 14:14:09 -0700259 if (recycledBitmap->isImmutable()) {
Matt Sarett1f979632015-10-27 10:33:20 -0400260 ALOGW("Warning: Reusing an immutable bitmap as an image decoder target.");
261 }
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800262 }
263
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000264 auto* brd = reinterpret_cast<BitmapRegionDecoderWrapper*>(brdHandle);
Leon Scroggins III8d592f92018-03-12 15:44:03 -0400265 SkColorType decodeColorType = brd->computeOutputColorType(colorType);
Alec Mouri1efd0a52022-01-20 13:58:23 -0800266
267 if (isHardware) {
268 if (decodeColorType == kRGBA_F16_SkColorType &&
Leon Scroggins IIIee3bfe72019-01-31 08:42:23 -0500269 !uirenderer::HardwareBitmapUploader::hasFP16Support()) {
Alec Mouri1efd0a52022-01-20 13:58:23 -0800270 decodeColorType = kN32_SkColorType;
271 }
272 if (decodeColorType == kRGBA_1010102_SkColorType &&
273 !uirenderer::HardwareBitmapUploader::has1010102Support()) {
274 decodeColorType = kN32_SkColorType;
275 }
Leon Scroggins IIIee3bfe72019-01-31 08:42:23 -0500276 }
Leon Scroggins III8d592f92018-03-12 15:44:03 -0400277
Matt Sarett1f979632015-10-27 10:33:20 -0400278 // Set up the pixel allocator
Leon Scroggins III23ac0362020-05-04 15:38:58 -0400279 skia::BRDAllocator* allocator = nullptr;
John Reck40ffc1d2023-06-20 17:26:09 -0400280 RecyclingClippingPixelAllocator recycleAlloc(recycledBitmap);
sergeyv45082182016-09-29 18:25:40 -0700281 HeapAllocator heapAlloc;
Matt Sarett1f979632015-10-27 10:33:20 -0400282 if (javaBitmap) {
283 allocator = &recycleAlloc;
284 // We are required to match the color type of the recycled bitmap.
Romain Guy95648b82017-04-13 18:43:42 -0700285 decodeColorType = recycledBitmap->info().colorType();
Matt Sarett1f979632015-10-27 10:33:20 -0400286 } else {
sergeyv45082182016-09-29 18:25:40 -0700287 allocator = &heapAlloc;
Matt Sarett1f979632015-10-27 10:33:20 -0400288 }
289
Leon Scroggins III8d592f92018-03-12 15:44:03 -0400290 sk_sp<SkColorSpace> decodeColorSpace = brd->computeOutputColorSpace(
291 decodeColorType, colorSpace);
292
Matt Sarett1f979632015-10-27 10:33:20 -0400293 // Decode the region.
John Reck40ffc1d2023-06-20 17:26:09 -0400294 const SkIRect subset = SkIRect::MakeXYWH(inputX, inputY, inputWidth, inputHeight);
John Reckf29ed282015-04-07 07:32:03 -0700295 SkBitmap bitmap;
Romain Guy95648b82017-04-13 18:43:42 -0700296 if (!brd->decodeRegion(&bitmap, allocator, subset, sampleSize,
297 decodeColorType, requireUnpremul, decodeColorSpace)) {
Matt Sarett1f979632015-10-27 10:33:20 -0400298 return nullObjectReturn("Failed to decode region.");
Owen Linf970c2e2012-04-25 18:49:09 +0800299 }
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800300
Matt Sarett1f979632015-10-27 10:33:20 -0400301 // If the client provided options, indicate that the decode was successful.
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800302 if (NULL != options) {
John Reckf29ed282015-04-07 07:32:03 -0700303 env->SetIntField(options, gOptions_widthFieldID, bitmap.width());
304 env->SetIntField(options, gOptions_heightFieldID, bitmap.height());
Romain Guy95648b82017-04-13 18:43:42 -0700305
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800306 env->SetObjectField(options, gOptions_mimeFieldID,
Leon Scroggins III407b5442019-11-22 17:10:20 -0500307 getMimeTypeAsJavaString(env, brd->getEncodedFormat()));
Matt Sarettd31512b2015-12-09 15:16:31 -0500308 if (env->ExceptionCheck()) {
309 return nullObjectReturn("OOM in encodedFormatToString()");
310 }
Romain Guy95648b82017-04-13 18:43:42 -0700311
312 jint configID = GraphicsJNI::colorTypeToLegacyBitmapConfig(decodeColorType);
313 if (isHardware) {
314 configID = GraphicsJNI::kHardware_LegacyBitmapConfig;
315 }
316 jobject config = env->CallStaticObjectMethod(gBitmapConfig_class,
317 gBitmapConfig_nativeToConfigMethodID, configID);
318 env->SetObjectField(options, gOptions_outConfigFieldID, config);
319
320 env->SetObjectField(options, gOptions_outColorSpaceFieldID,
Derek Sollenbergerbf3e4642019-01-30 11:28:27 -0500321 GraphicsJNI::getColorSpace(env, decodeColorSpace.get(), decodeColorType));
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800322 }
323
John Reck40ffc1d2023-06-20 17:26:09 -0400324 if (javaBitmap) {
325 recycleAlloc.copyIfNecessary();
326 }
327
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000328 sp<uirenderer::Gainmap> gainmap;
329 bool hasGainmap = brd->hasGainmap();
330 if (hasGainmap) {
John Reck40ffc1d2023-06-20 17:26:09 -0400331 SkIRect adjustedSubset{};
332 if (javaBitmap) {
333 // Clamp to the width/height of the recycled bitmap in case the reused bitmap
334 // was too small for the specified rectangle, in which case we need to clip
335 adjustedSubset = SkIRect::MakeXYWH(inputX, inputY,
336 std::min(subset.width(), recycledBitmap->width()),
337 std::min(subset.height(), recycledBitmap->height()));
338 } else {
339 // We are not recycling, so use the decoded width/height for calculating the gainmap
340 // subset instead to ensure the gainmap region proportionally matches
341 adjustedSubset = SkIRect::MakeXYWH(std::max(0, inputX), std::max(0, inputY),
342 bitmap.width(), bitmap.height());
343 }
344 SkIRect gainmapSubset = brd->calculateGainmapRegion(adjustedSubset);
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000345 if (!brd->decodeGainmapRegion(&gainmap, gainmapSubset, sampleSize, requireUnpremul)) {
346 // If there is an error decoding Gainmap - we don't fail. We just don't provide Gainmap
347 hasGainmap = false;
348 }
349 }
350
Matt Sarett1f979632015-10-27 10:33:20 -0400351 // If we may have reused a bitmap, we need to indicate that the pixels have changed.
352 if (javaBitmap) {
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000353 if (hasGainmap) {
354 recycledBitmap->setGainmap(std::move(gainmap));
355 }
Romain Guy55455182017-04-15 21:41:22 -0700356 bitmap::reinitBitmap(env, javaBitmap, recycledBitmap->info(), !requireUnpremul);
Matt Sarett1f979632015-10-27 10:33:20 -0400357 return javaBitmap;
Owen Linf970c2e2012-04-25 18:49:09 +0800358 }
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800359
Chris Craik1abf5d62013-08-16 12:47:03 -0700360 int bitmapCreateFlags = 0;
Matt Sarett1f979632015-10-27 10:33:20 -0400361 if (!requireUnpremul) {
sergeyvc69853c2016-10-07 14:14:09 -0700362 bitmapCreateFlags |= android::bitmap::kBitmapCreateFlag_Premultiplied;
Matt Sarett1f979632015-10-27 10:33:20 -0400363 }
John Reck40ffc1d2023-06-20 17:26:09 -0400364
sergeyvda6c8ffc2016-11-22 18:28:54 -0800365 if (isHardware) {
366 sk_sp<Bitmap> hardwareBitmap = Bitmap::allocateHardwareBitmap(bitmap);
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000367 if (hasGainmap) {
Sally Qi587fb572023-03-03 15:50:06 -0800368 auto gm = uirenderer::Gainmap::allocateHardwareGainmap(gainmap);
369 if (gm) {
370 hardwareBitmap->setGainmap(std::move(gm));
371 }
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000372 }
sergeyvda6c8ffc2016-11-22 18:28:54 -0800373 return bitmap::createBitmap(env, hardwareBitmap.release(), bitmapCreateFlags);
374 }
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000375 Bitmap* heapBitmap = heapAlloc.getStorageObjAndReset();
376 if (hasGainmap && heapBitmap != nullptr) {
377 heapBitmap->setGainmap(std::move(gainmap));
378 }
379 return android::bitmap::createBitmap(env, heapBitmap, bitmapCreateFlags);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800380}
381
Ashok Bhatb091d472014-01-08 14:32:49 +0000382static jint nativeGetHeight(JNIEnv* env, jobject, jlong brdHandle) {
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000383 auto* brd = reinterpret_cast<BitmapRegionDecoderWrapper*>(brdHandle);
Matt Sarett1f979632015-10-27 10:33:20 -0400384 return static_cast<jint>(brd->height());
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800385}
386
Ashok Bhatb091d472014-01-08 14:32:49 +0000387static jint nativeGetWidth(JNIEnv* env, jobject, jlong brdHandle) {
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000388 auto* brd = reinterpret_cast<BitmapRegionDecoderWrapper*>(brdHandle);
Matt Sarett1f979632015-10-27 10:33:20 -0400389 return static_cast<jint>(brd->width());
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800390}
391
Ashok Bhatb091d472014-01-08 14:32:49 +0000392static void nativeClean(JNIEnv* env, jobject, jlong brdHandle) {
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000393 auto* brd = reinterpret_cast<BitmapRegionDecoderWrapper*>(brdHandle);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800394 delete brd;
395}
396
397///////////////////////////////////////////////////////////////////////////////
398
Daniel Micay76f6a862015-09-19 17:31:01 -0400399static const JNINativeMethod gBitmapRegionDecoderMethods[] = {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800400 { "nativeDecodeRegion",
Leon Scroggins III71fae622019-03-26 16:28:41 -0400401 "(JIIIILandroid/graphics/BitmapFactory$Options;JJ)Landroid/graphics/Bitmap;",
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800402 (void*)nativeDecodeRegion},
403
Ashok Bhatb091d472014-01-08 14:32:49 +0000404 { "nativeGetHeight", "(J)I", (void*)nativeGetHeight},
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800405
Ashok Bhatb091d472014-01-08 14:32:49 +0000406 { "nativeGetWidth", "(J)I", (void*)nativeGetWidth},
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800407
Ashok Bhatb091d472014-01-08 14:32:49 +0000408 { "nativeClean", "(J)V", (void*)nativeClean},
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800409
410 { "nativeNewInstance",
Leon Scroggins III96a13882020-03-04 10:29:04 -0500411 "([BII)Landroid/graphics/BitmapRegionDecoder;",
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800412 (void*)nativeNewInstanceFromByteArray
413 },
414
415 { "nativeNewInstance",
Leon Scroggins III96a13882020-03-04 10:29:04 -0500416 "(Ljava/io/InputStream;[B)Landroid/graphics/BitmapRegionDecoder;",
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800417 (void*)nativeNewInstanceFromStream
418 },
419
420 { "nativeNewInstance",
Leon Scroggins III96a13882020-03-04 10:29:04 -0500421 "(Ljava/io/FileDescriptor;)Landroid/graphics/BitmapRegionDecoder;",
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800422 (void*)nativeNewInstanceFromFileDescriptor
423 },
424
425 { "nativeNewInstance",
Leon Scroggins III96a13882020-03-04 10:29:04 -0500426 "(J)Landroid/graphics/BitmapRegionDecoder;",
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800427 (void*)nativeNewInstanceFromAsset
428 },
429};
430
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800431int register_android_graphics_BitmapRegionDecoder(JNIEnv* env)
432{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800433 return android::RegisterMethodsOrDie(env, "android/graphics/BitmapRegionDecoder",
434 gBitmapRegionDecoderMethods, NELEM(gBitmapRegionDecoderMethods));
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800435}