blob: 6a65b82731945f6c7474f5509d72be0e4247a9d5 [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
Alec Mouri7dcb7d22024-07-26 13:41:04 +000090 // Decodes the gainmap region. If decoding succeeded, returns true and
91 // populate outGainmap with the decoded gainmap. Otherwise, returns false.
92 //
93 // Note that the desiredSubset is the logical region within the source
94 // gainmap that we want to decode. This is used for scaling into the final
95 // bitmap, since we do not want to include portions of the gainmap outside
96 // of this region. desiredSubset is also _not_ guaranteed to be
97 // pixel-aligned, so it's not possible to simply resize the resulting
98 // bitmap to accomplish this.
99 bool decodeGainmapRegion(sp<uirenderer::Gainmap>* outGainmap, SkISize bitmapDimensions,
100 const SkRect& desiredSubset, int sampleSize, bool requireUnpremul) {
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000101 SkColorType decodeColorType = mGainmapBRD->computeOutputColorType(kN32_SkColorType);
102 sk_sp<SkColorSpace> decodeColorSpace =
103 mGainmapBRD->computeOutputColorSpace(decodeColorType, nullptr);
104 SkBitmap bm;
John Reck40ffc1d2023-06-20 17:26:09 -0400105 // Because we must match the dimensions of the base bitmap, we always use a
106 // recycling allocator even though we are allocating a new bitmap. This is to ensure
107 // that if a recycled bitmap was used for the base image that we match the relative
108 // dimensions of that base image. The behavior of BRD here is:
109 // if inBitmap is specified -> output dimensions are always equal to the inBitmap's
110 // if no bitmap is reused -> output dimensions are the intersect of the desiredSubset &
111 // the image bounds
112 // The handling of the above conditionals are baked into the desiredSubset, so we
113 // simply need to ensure that the resulting bitmap is the exact same width/height as
114 // the specified desiredSubset regardless of the intersection to the image bounds.
115 // kPremul_SkAlphaType is used just as a placeholder as it doesn't change the underlying
116 // allocation type. RecyclingClippingPixelAllocator will populate this with the
117 // actual alpha type in either allocPixelRef() or copyIfNecessary()
John Reck9519757d2023-07-06 10:40:31 -0400118 sk_sp<Bitmap> nativeBitmap = Bitmap::allocateHeapBitmap(SkImageInfo::Make(
Alec Mouri7dcb7d22024-07-26 13:41:04 +0000119 bitmapDimensions, decodeColorType, kPremul_SkAlphaType, decodeColorSpace));
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000120 if (!nativeBitmap) {
121 ALOGE("OOM allocating Bitmap for Gainmap");
122 return false;
123 }
Alec Mouri7dcb7d22024-07-26 13:41:04 +0000124
125 // Round out the subset so that we decode a slightly larger region, in
126 // case the subset has fractional components.
127 SkIRect roundedSubset = desiredSubset.roundOut();
128
129 // Map the desired subset to the space of the decoded gainmap. The
130 // subset is repositioned relative to the resulting bitmap, and then
131 // scaled to respect the sampleSize.
132 // This assumes that the subset will not be modified by the decoder, which is true
133 // for existing gainmap formats.
134 SkRect logicalSubset = desiredSubset.makeOffset(-std::floorf(desiredSubset.left()),
135 -std::floorf(desiredSubset.top()));
136 logicalSubset.fLeft /= sampleSize;
137 logicalSubset.fTop /= sampleSize;
138 logicalSubset.fRight /= sampleSize;
139 logicalSubset.fBottom /= sampleSize;
140
141 RecyclingClippingPixelAllocator allocator(nativeBitmap.get(), false, logicalSubset);
142 if (!mGainmapBRD->decodeRegion(&bm, &allocator, roundedSubset, sampleSize, decodeColorType,
John Reck40ffc1d2023-06-20 17:26:09 -0400143 requireUnpremul, decodeColorSpace)) {
144 ALOGE("Error decoding Gainmap region");
145 return false;
146 }
147 allocator.copyIfNecessary();
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000148 auto gainmap = sp<uirenderer::Gainmap>::make();
149 if (!gainmap) {
150 ALOGE("OOM allocating Gainmap");
151 return false;
152 }
153 gainmap->info = mGainmapInfo;
154 gainmap->bitmap = std::move(nativeBitmap);
155 *outGainmap = std::move(gainmap);
156 return true;
157 }
158
Alec Mouri7dcb7d22024-07-26 13:41:04 +0000159 struct Projection {
160 SkRect srcRect;
161 SkISize destSize;
162 };
163 Projection calculateGainmapRegion(const SkIRect& mainImageRegion, SkISize dimensions) {
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000164 const float scaleX = ((float)mGainmapBRD->width()) / mMainImageBRD->width();
165 const float scaleY = ((float)mGainmapBRD->height()) / mMainImageBRD->height();
Alec Mouri7dcb7d22024-07-26 13:41:04 +0000166
167 if (uirenderer::Properties::resampleGainmapRegions) {
168 const auto srcRect = SkRect::MakeLTRB(
169 mainImageRegion.left() * scaleX, mainImageRegion.top() * scaleY,
170 mainImageRegion.right() * scaleX, mainImageRegion.bottom() * scaleY);
171 // Request a slightly larger destination size so that the gainmap
172 // subset we want fits entirely in this size.
173 const auto destSize = SkISize::Make(std::ceil(dimensions.width() * scaleX),
174 std::ceil(dimensions.height() * scaleY));
175 return Projection{.srcRect = srcRect, .destSize = destSize};
176 } else {
177 const auto srcRect = SkRect::Make(SkIRect::MakeLTRB(
178 mainImageRegion.left() * scaleX, mainImageRegion.top() * scaleY,
179 mainImageRegion.right() * scaleX, mainImageRegion.bottom() * scaleY));
180 const auto destSize =
181 SkISize::Make(dimensions.width() * scaleX, dimensions.height() * scaleY);
182 return Projection{.srcRect = srcRect, .destSize = destSize};
183 }
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000184 }
185
186 bool hasGainmap() { return mGainmapBRD != nullptr; }
187
188 int width() const { return mMainImageBRD->width(); }
189 int height() const { return mMainImageBRD->height(); }
190
191private:
192 BitmapRegionDecoderWrapper(std::unique_ptr<skia::BitmapRegionDecoder> mainImageBRD,
193 std::unique_ptr<skia::BitmapRegionDecoder> gainmapBRD,
194 SkGainmapInfo info, std::unique_ptr<SkStream> stream)
195 : mMainImageBRD(std::move(mainImageBRD))
196 , mGainmapBRD(std::move(gainmapBRD))
197 , mGainmapInfo(info)
198 , mGainmapStream(std::move(stream)) {}
199
200 std::unique_ptr<skia::BitmapRegionDecoder> mMainImageBRD;
201 std::unique_ptr<skia::BitmapRegionDecoder> mGainmapBRD;
202 SkGainmapInfo mGainmapInfo;
203 std::unique_ptr<SkStream> mGainmapStream;
204};
205} // namespace android
206
Leon Scroggins III23ac0362020-05-04 15:38:58 -0400207static jobject createBitmapRegionDecoder(JNIEnv* env, sk_sp<SkData> data) {
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000208 auto brd = android::BitmapRegionDecoderWrapper::Make(std::move(data));
Ben Wagner18bd8852016-10-24 14:50:10 -0400209 if (!brd) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800210 doThrowIOE(env, "Image format not supported");
Matt Sarett1f979632015-10-27 10:33:20 -0400211 return nullObjectReturn("CreateBitmapRegionDecoder returned null");
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800212 }
213
Ben Wagner18bd8852016-10-24 14:50:10 -0400214 return GraphicsJNI::createBitmapRegionDecoder(env, brd.release());
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800215}
216
217static jobject nativeNewInstanceFromByteArray(JNIEnv* env, jobject, jbyteArray byteArray,
Leon Scroggins III96a13882020-03-04 10:29:04 -0500218 jint offset, jint length) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800219 AutoJavaByteArray ar(env, byteArray);
Leon Scroggins III23ac0362020-05-04 15:38:58 -0400220 return createBitmapRegionDecoder(env, SkData::MakeWithCopy(ar.ptr() + offset, length));
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800221}
222
223static jobject nativeNewInstanceFromFileDescriptor(JNIEnv* env, jobject clazz,
Leon Scroggins III96a13882020-03-04 10:29:04 -0500224 jobject fileDescriptor) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800225 NPE_CHECK_RETURN_ZERO(env, fileDescriptor);
226
Elliott Hughesa3804cf2011-04-11 16:50:19 -0700227 jint descriptor = jniGetFDFromFileDescriptor(env, fileDescriptor);
Derek Sollenberger5827cb52013-07-26 14:58:06 -0400228
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800229 struct stat fdStat;
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800230 if (fstat(descriptor, &fdStat) == -1) {
231 doThrowIOE(env, "broken file descriptor");
232 return nullObjectReturn("fstat return -1");
233 }
234
Leon Scroggins III23ac0362020-05-04 15:38:58 -0400235 return createBitmapRegionDecoder(env, SkData::MakeFromFD(descriptor));
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800236}
237
Leon Scroggins III96a13882020-03-04 10:29:04 -0500238static jobject nativeNewInstanceFromStream(JNIEnv* env, jobject clazz, jobject is, // InputStream
239 jbyteArray storage) { // byte[]
Leon Scroggins III23ac0362020-05-04 15:38:58 -0400240 jobject brd = nullptr;
241 sk_sp<SkData> data = CopyJavaInputStream(env, is, storage);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800242
Leon Scroggins III23ac0362020-05-04 15:38:58 -0400243 if (data) {
244 brd = createBitmapRegionDecoder(env, std::move(data));
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800245 }
Derek Sollenberger5827cb52013-07-26 14:58:06 -0400246 return brd;
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800247}
248
Leon Scroggins III96a13882020-03-04 10:29:04 -0500249static jobject nativeNewInstanceFromAsset(JNIEnv* env, jobject clazz, jlong native_asset) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800250 Asset* asset = reinterpret_cast<Asset*>(native_asset);
Leon Scroggins III23ac0362020-05-04 15:38:58 -0400251 sk_sp<SkData> data = CopyAssetToData(asset);
252 if (!data) {
253 return nullptr;
Leon Scroggins IIIca320212013-08-20 17:59:39 -0400254 }
Derek Sollenberger5827cb52013-07-26 14:58:06 -0400255
Leon Scroggins III23ac0362020-05-04 15:38:58 -0400256 return createBitmapRegionDecoder(env, data);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800257}
258
259/*
260 * nine patch not supported
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800261 * purgeable not supported
262 * reportSizeToVM not supported
263 */
Matt Sarett1f979632015-10-27 10:33:20 -0400264static jobject nativeDecodeRegion(JNIEnv* env, jobject, jlong brdHandle, jint inputX,
Leon Scroggins III71fae622019-03-26 16:28:41 -0400265 jint inputY, jint inputWidth, jint inputHeight, jobject options, jlong inBitmapHandle,
266 jlong colorSpaceHandle) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800267
Matt Sarett1f979632015-10-27 10:33:20 -0400268 // Set default options.
269 int sampleSize = 1;
270 SkColorType colorType = kN32_SkColorType;
271 bool requireUnpremul = false;
Leon Scroggins III71fae622019-03-26 16:28:41 -0400272 jobject javaBitmap = nullptr;
sergeyvda6c8ffc2016-11-22 18:28:54 -0800273 bool isHardware = false;
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500274 sk_sp<SkColorSpace> colorSpace = GraphicsJNI::getNativeColorSpace(colorSpaceHandle);
Matt Sarett1f979632015-10-27 10:33:20 -0400275 // Update the default options with any options supplied by the client.
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800276 if (NULL != options) {
277 sampleSize = env->GetIntField(options, gOptions_sampleSizeFieldID);
Matt Sarett1f979632015-10-27 10:33:20 -0400278 jobject jconfig = env->GetObjectField(options, gOptions_configFieldID);
279 colorType = GraphicsJNI::getNativeBitmapColorType(env, jconfig);
sergeyvda6c8ffc2016-11-22 18:28:54 -0800280 isHardware = GraphicsJNI::isHardwareConfig(env, jconfig);
Matt Sarett1f979632015-10-27 10:33:20 -0400281 requireUnpremul = !env->GetBooleanField(options, gOptions_premultipliedFieldID);
282 javaBitmap = env->GetObjectField(options, gOptions_bitmapFieldID);
283 // The Java options of ditherMode and preferQualityOverSpeed are deprecated. We will
284 // ignore the values of these fields.
285
286 // Initialize these fields to indicate a failure. If the decode succeeds, we
287 // will update them later on.
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800288 env->SetIntField(options, gOptions_widthFieldID, -1);
289 env->SetIntField(options, gOptions_heightFieldID, -1);
290 env->SetObjectField(options, gOptions_mimeFieldID, 0);
Romain Guy95648b82017-04-13 18:43:42 -0700291 env->SetObjectField(options, gOptions_outConfigFieldID, 0);
292 env->SetObjectField(options, gOptions_outColorSpaceFieldID, 0);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800293 }
294
Matt Sarett1f979632015-10-27 10:33:20 -0400295 // Recycle a bitmap if possible.
sergeyvc1c54062016-10-19 18:47:26 -0700296 android::Bitmap* recycledBitmap = nullptr;
Matt Sarett1f979632015-10-27 10:33:20 -0400297 if (javaBitmap) {
Leon Scroggins III71fae622019-03-26 16:28:41 -0400298 recycledBitmap = &bitmap::toBitmap(inBitmapHandle);
sergeyvc69853c2016-10-07 14:14:09 -0700299 if (recycledBitmap->isImmutable()) {
Matt Sarett1f979632015-10-27 10:33:20 -0400300 ALOGW("Warning: Reusing an immutable bitmap as an image decoder target.");
301 }
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800302 }
303
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000304 auto* brd = reinterpret_cast<BitmapRegionDecoderWrapper*>(brdHandle);
Leon Scroggins III8d592f92018-03-12 15:44:03 -0400305 SkColorType decodeColorType = brd->computeOutputColorType(colorType);
Alec Mouri1efd0a52022-01-20 13:58:23 -0800306
307 if (isHardware) {
308 if (decodeColorType == kRGBA_F16_SkColorType &&
Leon Scroggins IIIee3bfe72019-01-31 08:42:23 -0500309 !uirenderer::HardwareBitmapUploader::hasFP16Support()) {
Alec Mouri1efd0a52022-01-20 13:58:23 -0800310 decodeColorType = kN32_SkColorType;
311 }
312 if (decodeColorType == kRGBA_1010102_SkColorType &&
313 !uirenderer::HardwareBitmapUploader::has1010102Support()) {
314 decodeColorType = kN32_SkColorType;
315 }
Leon Scroggins IIIee3bfe72019-01-31 08:42:23 -0500316 }
Leon Scroggins III8d592f92018-03-12 15:44:03 -0400317
Matt Sarett1f979632015-10-27 10:33:20 -0400318 // Set up the pixel allocator
Leon Scroggins III23ac0362020-05-04 15:38:58 -0400319 skia::BRDAllocator* allocator = nullptr;
John Reck40ffc1d2023-06-20 17:26:09 -0400320 RecyclingClippingPixelAllocator recycleAlloc(recycledBitmap);
sergeyv45082182016-09-29 18:25:40 -0700321 HeapAllocator heapAlloc;
Matt Sarett1f979632015-10-27 10:33:20 -0400322 if (javaBitmap) {
323 allocator = &recycleAlloc;
324 // We are required to match the color type of the recycled bitmap.
Romain Guy95648b82017-04-13 18:43:42 -0700325 decodeColorType = recycledBitmap->info().colorType();
Matt Sarett1f979632015-10-27 10:33:20 -0400326 } else {
sergeyv45082182016-09-29 18:25:40 -0700327 allocator = &heapAlloc;
Matt Sarett1f979632015-10-27 10:33:20 -0400328 }
329
Leon Scroggins III8d592f92018-03-12 15:44:03 -0400330 sk_sp<SkColorSpace> decodeColorSpace = brd->computeOutputColorSpace(
331 decodeColorType, colorSpace);
332
Matt Sarett1f979632015-10-27 10:33:20 -0400333 // Decode the region.
John Reck40ffc1d2023-06-20 17:26:09 -0400334 const SkIRect subset = SkIRect::MakeXYWH(inputX, inputY, inputWidth, inputHeight);
John Reckf29ed282015-04-07 07:32:03 -0700335 SkBitmap bitmap;
Romain Guy95648b82017-04-13 18:43:42 -0700336 if (!brd->decodeRegion(&bitmap, allocator, subset, sampleSize,
337 decodeColorType, requireUnpremul, decodeColorSpace)) {
Matt Sarett1f979632015-10-27 10:33:20 -0400338 return nullObjectReturn("Failed to decode region.");
Owen Linf970c2e2012-04-25 18:49:09 +0800339 }
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800340
Matt Sarett1f979632015-10-27 10:33:20 -0400341 // If the client provided options, indicate that the decode was successful.
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800342 if (NULL != options) {
John Reckf29ed282015-04-07 07:32:03 -0700343 env->SetIntField(options, gOptions_widthFieldID, bitmap.width());
344 env->SetIntField(options, gOptions_heightFieldID, bitmap.height());
Romain Guy95648b82017-04-13 18:43:42 -0700345
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800346 env->SetObjectField(options, gOptions_mimeFieldID,
Leon Scroggins III407b5442019-11-22 17:10:20 -0500347 getMimeTypeAsJavaString(env, brd->getEncodedFormat()));
Matt Sarettd31512b2015-12-09 15:16:31 -0500348 if (env->ExceptionCheck()) {
349 return nullObjectReturn("OOM in encodedFormatToString()");
350 }
Romain Guy95648b82017-04-13 18:43:42 -0700351
352 jint configID = GraphicsJNI::colorTypeToLegacyBitmapConfig(decodeColorType);
353 if (isHardware) {
354 configID = GraphicsJNI::kHardware_LegacyBitmapConfig;
355 }
356 jobject config = env->CallStaticObjectMethod(gBitmapConfig_class,
357 gBitmapConfig_nativeToConfigMethodID, configID);
358 env->SetObjectField(options, gOptions_outConfigFieldID, config);
359
360 env->SetObjectField(options, gOptions_outColorSpaceFieldID,
Derek Sollenbergerbf3e4642019-01-30 11:28:27 -0500361 GraphicsJNI::getColorSpace(env, decodeColorSpace.get(), decodeColorType));
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800362 }
363
John Reck40ffc1d2023-06-20 17:26:09 -0400364 if (javaBitmap) {
365 recycleAlloc.copyIfNecessary();
366 }
367
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000368 sp<uirenderer::Gainmap> gainmap;
369 bool hasGainmap = brd->hasGainmap();
370 if (hasGainmap) {
Alec Mouri7dcb7d22024-07-26 13:41:04 +0000371 SkISize gainmapDims = SkISize::Make(bitmap.width(), bitmap.height());
John Reck40ffc1d2023-06-20 17:26:09 -0400372 if (javaBitmap) {
John Reck9519757d2023-07-06 10:40:31 -0400373 // If we are recycling we must match the inBitmap's relative dimensions
Alec Mouri7dcb7d22024-07-26 13:41:04 +0000374 gainmapDims.fWidth = recycledBitmap->width();
375 gainmapDims.fHeight = recycledBitmap->height();
John Reck40ffc1d2023-06-20 17:26:09 -0400376 }
Alec Mouri7dcb7d22024-07-26 13:41:04 +0000377 BitmapRegionDecoderWrapper::Projection gainmapProjection =
378 brd->calculateGainmapRegion(subset, gainmapDims);
379 if (!brd->decodeGainmapRegion(&gainmap, gainmapProjection.destSize,
380 gainmapProjection.srcRect, sampleSize, requireUnpremul)) {
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000381 // If there is an error decoding Gainmap - we don't fail. We just don't provide Gainmap
382 hasGainmap = false;
383 }
384 }
385
Matt Sarett1f979632015-10-27 10:33:20 -0400386 // If we may have reused a bitmap, we need to indicate that the pixels have changed.
387 if (javaBitmap) {
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000388 if (hasGainmap) {
389 recycledBitmap->setGainmap(std::move(gainmap));
390 }
Romain Guy55455182017-04-15 21:41:22 -0700391 bitmap::reinitBitmap(env, javaBitmap, recycledBitmap->info(), !requireUnpremul);
Matt Sarett1f979632015-10-27 10:33:20 -0400392 return javaBitmap;
Owen Linf970c2e2012-04-25 18:49:09 +0800393 }
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800394
Chris Craik1abf5d62013-08-16 12:47:03 -0700395 int bitmapCreateFlags = 0;
Matt Sarett1f979632015-10-27 10:33:20 -0400396 if (!requireUnpremul) {
sergeyvc69853c2016-10-07 14:14:09 -0700397 bitmapCreateFlags |= android::bitmap::kBitmapCreateFlag_Premultiplied;
Matt Sarett1f979632015-10-27 10:33:20 -0400398 }
John Reck40ffc1d2023-06-20 17:26:09 -0400399
sergeyvda6c8ffc2016-11-22 18:28:54 -0800400 if (isHardware) {
401 sk_sp<Bitmap> hardwareBitmap = Bitmap::allocateHardwareBitmap(bitmap);
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000402 if (hasGainmap) {
Sally Qi587fb572023-03-03 15:50:06 -0800403 auto gm = uirenderer::Gainmap::allocateHardwareGainmap(gainmap);
404 if (gm) {
405 hardwareBitmap->setGainmap(std::move(gm));
406 }
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000407 }
sergeyvda6c8ffc2016-11-22 18:28:54 -0800408 return bitmap::createBitmap(env, hardwareBitmap.release(), bitmapCreateFlags);
409 }
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000410 Bitmap* heapBitmap = heapAlloc.getStorageObjAndReset();
411 if (hasGainmap && heapBitmap != nullptr) {
412 heapBitmap->setGainmap(std::move(gainmap));
413 }
414 return android::bitmap::createBitmap(env, heapBitmap, bitmapCreateFlags);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800415}
416
Ashok Bhatb091d472014-01-08 14:32:49 +0000417static jint nativeGetHeight(JNIEnv* env, jobject, jlong brdHandle) {
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000418 auto* brd = reinterpret_cast<BitmapRegionDecoderWrapper*>(brdHandle);
Matt Sarett1f979632015-10-27 10:33:20 -0400419 return static_cast<jint>(brd->height());
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800420}
421
Ashok Bhatb091d472014-01-08 14:32:49 +0000422static jint nativeGetWidth(JNIEnv* env, jobject, jlong brdHandle) {
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000423 auto* brd = reinterpret_cast<BitmapRegionDecoderWrapper*>(brdHandle);
Matt Sarett1f979632015-10-27 10:33:20 -0400424 return static_cast<jint>(brd->width());
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800425}
426
Ashok Bhatb091d472014-01-08 14:32:49 +0000427static void nativeClean(JNIEnv* env, jobject, jlong brdHandle) {
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000428 auto* brd = reinterpret_cast<BitmapRegionDecoderWrapper*>(brdHandle);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800429 delete brd;
430}
431
432///////////////////////////////////////////////////////////////////////////////
433
Daniel Micay76f6a862015-09-19 17:31:01 -0400434static const JNINativeMethod gBitmapRegionDecoderMethods[] = {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800435 { "nativeDecodeRegion",
Leon Scroggins III71fae622019-03-26 16:28:41 -0400436 "(JIIIILandroid/graphics/BitmapFactory$Options;JJ)Landroid/graphics/Bitmap;",
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800437 (void*)nativeDecodeRegion},
438
Ashok Bhatb091d472014-01-08 14:32:49 +0000439 { "nativeGetHeight", "(J)I", (void*)nativeGetHeight},
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800440
Ashok Bhatb091d472014-01-08 14:32:49 +0000441 { "nativeGetWidth", "(J)I", (void*)nativeGetWidth},
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800442
Ashok Bhatb091d472014-01-08 14:32:49 +0000443 { "nativeClean", "(J)V", (void*)nativeClean},
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800444
445 { "nativeNewInstance",
Leon Scroggins III96a13882020-03-04 10:29:04 -0500446 "([BII)Landroid/graphics/BitmapRegionDecoder;",
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800447 (void*)nativeNewInstanceFromByteArray
448 },
449
450 { "nativeNewInstance",
Leon Scroggins III96a13882020-03-04 10:29:04 -0500451 "(Ljava/io/InputStream;[B)Landroid/graphics/BitmapRegionDecoder;",
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800452 (void*)nativeNewInstanceFromStream
453 },
454
455 { "nativeNewInstance",
Leon Scroggins III96a13882020-03-04 10:29:04 -0500456 "(Ljava/io/FileDescriptor;)Landroid/graphics/BitmapRegionDecoder;",
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800457 (void*)nativeNewInstanceFromFileDescriptor
458 },
459
460 { "nativeNewInstance",
Leon Scroggins III96a13882020-03-04 10:29:04 -0500461 "(J)Landroid/graphics/BitmapRegionDecoder;",
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800462 (void*)nativeNewInstanceFromAsset
463 },
464};
465
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800466int register_android_graphics_BitmapRegionDecoder(JNIEnv* env)
467{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800468 return android::RegisterMethodsOrDie(env, "android/graphics/BitmapRegionDecoder",
469 gBitmapRegionDecoderMethods, NELEM(gBitmapRegionDecoderMethods));
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800470}