blob: f7e8e073a272818d321ae4678d2c6eece9e1f6b8 [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;
Christopher Cameroncf3d3fc2024-09-11 14:18:44 +000051 std::unique_ptr<SkAndroidCodec> gainmapCodec;
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +000052 std::unique_ptr<skia::BitmapRegionDecoder> gainmapBRD = nullptr;
Christopher Cameroncf3d3fc2024-09-11 14:18:44 +000053 if (!mainImageBRD->getGainmapBitmapRegionDecoder(&gainmapInfo, &gainmapBRD)) {
54 gainmapBRD = nullptr;
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +000055 }
56
Christopher Cameroncf3d3fc2024-09-11 14:18:44 +000057 return std::unique_ptr<BitmapRegionDecoderWrapper>(new BitmapRegionDecoderWrapper(
58 std::move(mainImageBRD), std::move(gainmapBRD), gainmapInfo));
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +000059 }
60
61 SkEncodedImageFormat getEncodedFormat() { return mMainImageBRD->getEncodedFormat(); }
62
63 SkColorType computeOutputColorType(SkColorType requestedColorType) {
64 return mMainImageBRD->computeOutputColorType(requestedColorType);
65 }
66
67 sk_sp<SkColorSpace> computeOutputColorSpace(SkColorType outputColorType,
68 sk_sp<SkColorSpace> prefColorSpace = nullptr) {
69 return mMainImageBRD->computeOutputColorSpace(outputColorType, prefColorSpace);
70 }
71
72 bool decodeRegion(SkBitmap* bitmap, skia::BRDAllocator* allocator, const SkIRect& desiredSubset,
73 int sampleSize, SkColorType colorType, bool requireUnpremul,
74 sk_sp<SkColorSpace> prefColorSpace) {
75 return mMainImageBRD->decodeRegion(bitmap, allocator, desiredSubset, sampleSize, colorType,
76 requireUnpremul, prefColorSpace);
77 }
78
Alec Mouri7dcb7d22024-07-26 13:41:04 +000079 // Decodes the gainmap region. If decoding succeeded, returns true and
80 // populate outGainmap with the decoded gainmap. Otherwise, returns false.
81 //
82 // Note that the desiredSubset is the logical region within the source
83 // gainmap that we want to decode. This is used for scaling into the final
84 // bitmap, since we do not want to include portions of the gainmap outside
85 // of this region. desiredSubset is also _not_ guaranteed to be
86 // pixel-aligned, so it's not possible to simply resize the resulting
87 // bitmap to accomplish this.
88 bool decodeGainmapRegion(sp<uirenderer::Gainmap>* outGainmap, SkISize bitmapDimensions,
89 const SkRect& desiredSubset, int sampleSize, bool requireUnpremul) {
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +000090 SkColorType decodeColorType = mGainmapBRD->computeOutputColorType(kN32_SkColorType);
91 sk_sp<SkColorSpace> decodeColorSpace =
92 mGainmapBRD->computeOutputColorSpace(decodeColorType, nullptr);
93 SkBitmap bm;
John Reck40ffc1d2023-06-20 17:26:09 -040094 // Because we must match the dimensions of the base bitmap, we always use a
95 // recycling allocator even though we are allocating a new bitmap. This is to ensure
96 // that if a recycled bitmap was used for the base image that we match the relative
97 // dimensions of that base image. The behavior of BRD here is:
98 // if inBitmap is specified -> output dimensions are always equal to the inBitmap's
99 // if no bitmap is reused -> output dimensions are the intersect of the desiredSubset &
100 // the image bounds
101 // The handling of the above conditionals are baked into the desiredSubset, so we
102 // simply need to ensure that the resulting bitmap is the exact same width/height as
103 // the specified desiredSubset regardless of the intersection to the image bounds.
104 // kPremul_SkAlphaType is used just as a placeholder as it doesn't change the underlying
105 // allocation type. RecyclingClippingPixelAllocator will populate this with the
106 // actual alpha type in either allocPixelRef() or copyIfNecessary()
John Reck95197572023-07-06 10:40:31 -0400107 sk_sp<Bitmap> nativeBitmap = Bitmap::allocateHeapBitmap(SkImageInfo::Make(
Alec Mouri7dcb7d22024-07-26 13:41:04 +0000108 bitmapDimensions, decodeColorType, kPremul_SkAlphaType, decodeColorSpace));
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000109 if (!nativeBitmap) {
110 ALOGE("OOM allocating Bitmap for Gainmap");
111 return false;
112 }
Alec Mouri7dcb7d22024-07-26 13:41:04 +0000113
114 // Round out the subset so that we decode a slightly larger region, in
115 // case the subset has fractional components.
116 SkIRect roundedSubset = desiredSubset.roundOut();
117
118 // Map the desired subset to the space of the decoded gainmap. The
119 // subset is repositioned relative to the resulting bitmap, and then
120 // scaled to respect the sampleSize.
121 // This assumes that the subset will not be modified by the decoder, which is true
122 // for existing gainmap formats.
123 SkRect logicalSubset = desiredSubset.makeOffset(-std::floorf(desiredSubset.left()),
124 -std::floorf(desiredSubset.top()));
125 logicalSubset.fLeft /= sampleSize;
126 logicalSubset.fTop /= sampleSize;
127 logicalSubset.fRight /= sampleSize;
128 logicalSubset.fBottom /= sampleSize;
129
130 RecyclingClippingPixelAllocator allocator(nativeBitmap.get(), false, logicalSubset);
131 if (!mGainmapBRD->decodeRegion(&bm, &allocator, roundedSubset, sampleSize, decodeColorType,
John Reck40ffc1d2023-06-20 17:26:09 -0400132 requireUnpremul, decodeColorSpace)) {
133 ALOGE("Error decoding Gainmap region");
134 return false;
135 }
136 allocator.copyIfNecessary();
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000137 auto gainmap = sp<uirenderer::Gainmap>::make();
138 if (!gainmap) {
139 ALOGE("OOM allocating Gainmap");
140 return false;
141 }
142 gainmap->info = mGainmapInfo;
143 gainmap->bitmap = std::move(nativeBitmap);
144 *outGainmap = std::move(gainmap);
145 return true;
146 }
147
Alec Mouri7dcb7d22024-07-26 13:41:04 +0000148 struct Projection {
149 SkRect srcRect;
150 SkISize destSize;
151 };
152 Projection calculateGainmapRegion(const SkIRect& mainImageRegion, SkISize dimensions) {
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000153 const float scaleX = ((float)mGainmapBRD->width()) / mMainImageBRD->width();
154 const float scaleY = ((float)mGainmapBRD->height()) / mMainImageBRD->height();
Alec Mouri7dcb7d22024-07-26 13:41:04 +0000155
156 if (uirenderer::Properties::resampleGainmapRegions) {
157 const auto srcRect = SkRect::MakeLTRB(
158 mainImageRegion.left() * scaleX, mainImageRegion.top() * scaleY,
159 mainImageRegion.right() * scaleX, mainImageRegion.bottom() * scaleY);
160 // Request a slightly larger destination size so that the gainmap
161 // subset we want fits entirely in this size.
162 const auto destSize = SkISize::Make(std::ceil(dimensions.width() * scaleX),
163 std::ceil(dimensions.height() * scaleY));
164 return Projection{.srcRect = srcRect, .destSize = destSize};
165 } else {
166 const auto srcRect = SkRect::Make(SkIRect::MakeLTRB(
167 mainImageRegion.left() * scaleX, mainImageRegion.top() * scaleY,
168 mainImageRegion.right() * scaleX, mainImageRegion.bottom() * scaleY));
169 const auto destSize =
170 SkISize::Make(dimensions.width() * scaleX, dimensions.height() * scaleY);
171 return Projection{.srcRect = srcRect, .destSize = destSize};
172 }
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000173 }
174
175 bool hasGainmap() { return mGainmapBRD != nullptr; }
176
177 int width() const { return mMainImageBRD->width(); }
178 int height() const { return mMainImageBRD->height(); }
179
180private:
181 BitmapRegionDecoderWrapper(std::unique_ptr<skia::BitmapRegionDecoder> mainImageBRD,
182 std::unique_ptr<skia::BitmapRegionDecoder> gainmapBRD,
Christopher Cameroncf3d3fc2024-09-11 14:18:44 +0000183 SkGainmapInfo info)
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000184 : mMainImageBRD(std::move(mainImageBRD))
185 , mGainmapBRD(std::move(gainmapBRD))
Christopher Cameroncf3d3fc2024-09-11 14:18:44 +0000186 , mGainmapInfo(info) {}
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000187
188 std::unique_ptr<skia::BitmapRegionDecoder> mMainImageBRD;
189 std::unique_ptr<skia::BitmapRegionDecoder> mGainmapBRD;
190 SkGainmapInfo mGainmapInfo;
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000191};
192} // namespace android
193
Leon Scroggins III23ac0362020-05-04 15:38:58 -0400194static jobject createBitmapRegionDecoder(JNIEnv* env, sk_sp<SkData> data) {
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000195 auto brd = android::BitmapRegionDecoderWrapper::Make(std::move(data));
Ben Wagner18bd8852016-10-24 14:50:10 -0400196 if (!brd) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800197 doThrowIOE(env, "Image format not supported");
Matt Sarett1f979632015-10-27 10:33:20 -0400198 return nullObjectReturn("CreateBitmapRegionDecoder returned null");
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800199 }
200
Ben Wagner18bd8852016-10-24 14:50:10 -0400201 return GraphicsJNI::createBitmapRegionDecoder(env, brd.release());
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800202}
203
204static jobject nativeNewInstanceFromByteArray(JNIEnv* env, jobject, jbyteArray byteArray,
Leon Scroggins III96a13882020-03-04 10:29:04 -0500205 jint offset, jint length) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800206 AutoJavaByteArray ar(env, byteArray);
Leon Scroggins III23ac0362020-05-04 15:38:58 -0400207 return createBitmapRegionDecoder(env, SkData::MakeWithCopy(ar.ptr() + offset, length));
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800208}
209
210static jobject nativeNewInstanceFromFileDescriptor(JNIEnv* env, jobject clazz,
Leon Scroggins III96a13882020-03-04 10:29:04 -0500211 jobject fileDescriptor) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800212 NPE_CHECK_RETURN_ZERO(env, fileDescriptor);
213
Elliott Hughesa3804cf2011-04-11 16:50:19 -0700214 jint descriptor = jniGetFDFromFileDescriptor(env, fileDescriptor);
Derek Sollenberger5827cb52013-07-26 14:58:06 -0400215
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800216 struct stat fdStat;
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800217 if (fstat(descriptor, &fdStat) == -1) {
218 doThrowIOE(env, "broken file descriptor");
219 return nullObjectReturn("fstat return -1");
220 }
221
Leon Scroggins III23ac0362020-05-04 15:38:58 -0400222 return createBitmapRegionDecoder(env, SkData::MakeFromFD(descriptor));
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800223}
224
Leon Scroggins III96a13882020-03-04 10:29:04 -0500225static jobject nativeNewInstanceFromStream(JNIEnv* env, jobject clazz, jobject is, // InputStream
226 jbyteArray storage) { // byte[]
Leon Scroggins III23ac0362020-05-04 15:38:58 -0400227 jobject brd = nullptr;
228 sk_sp<SkData> data = CopyJavaInputStream(env, is, storage);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800229
Leon Scroggins III23ac0362020-05-04 15:38:58 -0400230 if (data) {
231 brd = createBitmapRegionDecoder(env, std::move(data));
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800232 }
Derek Sollenberger5827cb52013-07-26 14:58:06 -0400233 return brd;
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800234}
235
Leon Scroggins III96a13882020-03-04 10:29:04 -0500236static jobject nativeNewInstanceFromAsset(JNIEnv* env, jobject clazz, jlong native_asset) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800237 Asset* asset = reinterpret_cast<Asset*>(native_asset);
Leon Scroggins III23ac0362020-05-04 15:38:58 -0400238 sk_sp<SkData> data = CopyAssetToData(asset);
239 if (!data) {
240 return nullptr;
Leon Scroggins IIIca320212013-08-20 17:59:39 -0400241 }
Derek Sollenberger5827cb52013-07-26 14:58:06 -0400242
Leon Scroggins III23ac0362020-05-04 15:38:58 -0400243 return createBitmapRegionDecoder(env, data);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800244}
245
246/*
247 * nine patch not supported
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800248 * purgeable not supported
249 * reportSizeToVM not supported
250 */
Matt Sarett1f979632015-10-27 10:33:20 -0400251static jobject nativeDecodeRegion(JNIEnv* env, jobject, jlong brdHandle, jint inputX,
Leon Scroggins III71fae622019-03-26 16:28:41 -0400252 jint inputY, jint inputWidth, jint inputHeight, jobject options, jlong inBitmapHandle,
253 jlong colorSpaceHandle) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800254
Matt Sarett1f979632015-10-27 10:33:20 -0400255 // Set default options.
256 int sampleSize = 1;
257 SkColorType colorType = kN32_SkColorType;
258 bool requireUnpremul = false;
Leon Scroggins III71fae622019-03-26 16:28:41 -0400259 jobject javaBitmap = nullptr;
sergeyvda6c8ffc2016-11-22 18:28:54 -0800260 bool isHardware = false;
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500261 sk_sp<SkColorSpace> colorSpace = GraphicsJNI::getNativeColorSpace(colorSpaceHandle);
Matt Sarett1f979632015-10-27 10:33:20 -0400262 // Update the default options with any options supplied by the client.
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800263 if (NULL != options) {
264 sampleSize = env->GetIntField(options, gOptions_sampleSizeFieldID);
Matt Sarett1f979632015-10-27 10:33:20 -0400265 jobject jconfig = env->GetObjectField(options, gOptions_configFieldID);
266 colorType = GraphicsJNI::getNativeBitmapColorType(env, jconfig);
sergeyvda6c8ffc2016-11-22 18:28:54 -0800267 isHardware = GraphicsJNI::isHardwareConfig(env, jconfig);
Matt Sarett1f979632015-10-27 10:33:20 -0400268 requireUnpremul = !env->GetBooleanField(options, gOptions_premultipliedFieldID);
269 javaBitmap = env->GetObjectField(options, gOptions_bitmapFieldID);
270 // The Java options of ditherMode and preferQualityOverSpeed are deprecated. We will
271 // ignore the values of these fields.
272
273 // Initialize these fields to indicate a failure. If the decode succeeds, we
274 // will update them later on.
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800275 env->SetIntField(options, gOptions_widthFieldID, -1);
276 env->SetIntField(options, gOptions_heightFieldID, -1);
277 env->SetObjectField(options, gOptions_mimeFieldID, 0);
Romain Guy95648b82017-04-13 18:43:42 -0700278 env->SetObjectField(options, gOptions_outConfigFieldID, 0);
279 env->SetObjectField(options, gOptions_outColorSpaceFieldID, 0);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800280 }
281
Matt Sarett1f979632015-10-27 10:33:20 -0400282 // Recycle a bitmap if possible.
sergeyvc1c54062016-10-19 18:47:26 -0700283 android::Bitmap* recycledBitmap = nullptr;
Matt Sarett1f979632015-10-27 10:33:20 -0400284 if (javaBitmap) {
Leon Scroggins III71fae622019-03-26 16:28:41 -0400285 recycledBitmap = &bitmap::toBitmap(inBitmapHandle);
sergeyvc69853c2016-10-07 14:14:09 -0700286 if (recycledBitmap->isImmutable()) {
Matt Sarett1f979632015-10-27 10:33:20 -0400287 ALOGW("Warning: Reusing an immutable bitmap as an image decoder target.");
288 }
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800289 }
290
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000291 auto* brd = reinterpret_cast<BitmapRegionDecoderWrapper*>(brdHandle);
Leon Scroggins III8d592f92018-03-12 15:44:03 -0400292 SkColorType decodeColorType = brd->computeOutputColorType(colorType);
Alec Mouri1efd0a52022-01-20 13:58:23 -0800293
294 if (isHardware) {
295 if (decodeColorType == kRGBA_F16_SkColorType &&
Leon Scroggins IIIee3bfe72019-01-31 08:42:23 -0500296 !uirenderer::HardwareBitmapUploader::hasFP16Support()) {
Alec Mouri1efd0a52022-01-20 13:58:23 -0800297 decodeColorType = kN32_SkColorType;
298 }
299 if (decodeColorType == kRGBA_1010102_SkColorType &&
300 !uirenderer::HardwareBitmapUploader::has1010102Support()) {
301 decodeColorType = kN32_SkColorType;
302 }
Leon Scroggins IIIee3bfe72019-01-31 08:42:23 -0500303 }
Leon Scroggins III8d592f92018-03-12 15:44:03 -0400304
Matt Sarett1f979632015-10-27 10:33:20 -0400305 // Set up the pixel allocator
Leon Scroggins III23ac0362020-05-04 15:38:58 -0400306 skia::BRDAllocator* allocator = nullptr;
John Reck40ffc1d2023-06-20 17:26:09 -0400307 RecyclingClippingPixelAllocator recycleAlloc(recycledBitmap);
sergeyv45082182016-09-29 18:25:40 -0700308 HeapAllocator heapAlloc;
Matt Sarett1f979632015-10-27 10:33:20 -0400309 if (javaBitmap) {
310 allocator = &recycleAlloc;
311 // We are required to match the color type of the recycled bitmap.
Romain Guy95648b82017-04-13 18:43:42 -0700312 decodeColorType = recycledBitmap->info().colorType();
Matt Sarett1f979632015-10-27 10:33:20 -0400313 } else {
sergeyv45082182016-09-29 18:25:40 -0700314 allocator = &heapAlloc;
Matt Sarett1f979632015-10-27 10:33:20 -0400315 }
316
Leon Scroggins III8d592f92018-03-12 15:44:03 -0400317 sk_sp<SkColorSpace> decodeColorSpace = brd->computeOutputColorSpace(
318 decodeColorType, colorSpace);
319
Matt Sarett1f979632015-10-27 10:33:20 -0400320 // Decode the region.
John Reck40ffc1d2023-06-20 17:26:09 -0400321 const SkIRect subset = SkIRect::MakeXYWH(inputX, inputY, inputWidth, inputHeight);
John Reckf29ed282015-04-07 07:32:03 -0700322 SkBitmap bitmap;
Romain Guy95648b82017-04-13 18:43:42 -0700323 if (!brd->decodeRegion(&bitmap, allocator, subset, sampleSize,
324 decodeColorType, requireUnpremul, decodeColorSpace)) {
Matt Sarett1f979632015-10-27 10:33:20 -0400325 return nullObjectReturn("Failed to decode region.");
Owen Linf970c2e2012-04-25 18:49:09 +0800326 }
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800327
Matt Sarett1f979632015-10-27 10:33:20 -0400328 // If the client provided options, indicate that the decode was successful.
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800329 if (NULL != options) {
John Reckf29ed282015-04-07 07:32:03 -0700330 env->SetIntField(options, gOptions_widthFieldID, bitmap.width());
331 env->SetIntField(options, gOptions_heightFieldID, bitmap.height());
Romain Guy95648b82017-04-13 18:43:42 -0700332
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800333 env->SetObjectField(options, gOptions_mimeFieldID,
Leon Scroggins III407b5442019-11-22 17:10:20 -0500334 getMimeTypeAsJavaString(env, brd->getEncodedFormat()));
Matt Sarettd31512b2015-12-09 15:16:31 -0500335 if (env->ExceptionCheck()) {
336 return nullObjectReturn("OOM in encodedFormatToString()");
337 }
Romain Guy95648b82017-04-13 18:43:42 -0700338
339 jint configID = GraphicsJNI::colorTypeToLegacyBitmapConfig(decodeColorType);
340 if (isHardware) {
341 configID = GraphicsJNI::kHardware_LegacyBitmapConfig;
342 }
343 jobject config = env->CallStaticObjectMethod(gBitmapConfig_class,
344 gBitmapConfig_nativeToConfigMethodID, configID);
345 env->SetObjectField(options, gOptions_outConfigFieldID, config);
346
347 env->SetObjectField(options, gOptions_outColorSpaceFieldID,
Derek Sollenbergerbf3e4642019-01-30 11:28:27 -0500348 GraphicsJNI::getColorSpace(env, decodeColorSpace.get(), decodeColorType));
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800349 }
350
John Reck40ffc1d2023-06-20 17:26:09 -0400351 if (javaBitmap) {
352 recycleAlloc.copyIfNecessary();
353 }
354
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000355 sp<uirenderer::Gainmap> gainmap;
356 bool hasGainmap = brd->hasGainmap();
357 if (hasGainmap) {
Alec Mouri7dcb7d22024-07-26 13:41:04 +0000358 SkISize gainmapDims = SkISize::Make(bitmap.width(), bitmap.height());
John Reck40ffc1d2023-06-20 17:26:09 -0400359 if (javaBitmap) {
John Reck95197572023-07-06 10:40:31 -0400360 // If we are recycling we must match the inBitmap's relative dimensions
Alec Mouri7dcb7d22024-07-26 13:41:04 +0000361 gainmapDims.fWidth = recycledBitmap->width();
362 gainmapDims.fHeight = recycledBitmap->height();
John Reck40ffc1d2023-06-20 17:26:09 -0400363 }
Alec Mouri7dcb7d22024-07-26 13:41:04 +0000364 BitmapRegionDecoderWrapper::Projection gainmapProjection =
365 brd->calculateGainmapRegion(subset, gainmapDims);
366 if (!brd->decodeGainmapRegion(&gainmap, gainmapProjection.destSize,
367 gainmapProjection.srcRect, sampleSize, requireUnpremul)) {
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000368 // If there is an error decoding Gainmap - we don't fail. We just don't provide Gainmap
369 hasGainmap = false;
370 }
371 }
372
Matt Sarett1f979632015-10-27 10:33:20 -0400373 // If we may have reused a bitmap, we need to indicate that the pixels have changed.
374 if (javaBitmap) {
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000375 if (hasGainmap) {
376 recycledBitmap->setGainmap(std::move(gainmap));
377 }
Romain Guy55455182017-04-15 21:41:22 -0700378 bitmap::reinitBitmap(env, javaBitmap, recycledBitmap->info(), !requireUnpremul);
Matt Sarett1f979632015-10-27 10:33:20 -0400379 return javaBitmap;
Owen Linf970c2e2012-04-25 18:49:09 +0800380 }
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800381
Chris Craik1abf5d62013-08-16 12:47:03 -0700382 int bitmapCreateFlags = 0;
Matt Sarett1f979632015-10-27 10:33:20 -0400383 if (!requireUnpremul) {
sergeyvc69853c2016-10-07 14:14:09 -0700384 bitmapCreateFlags |= android::bitmap::kBitmapCreateFlag_Premultiplied;
Matt Sarett1f979632015-10-27 10:33:20 -0400385 }
John Reck40ffc1d2023-06-20 17:26:09 -0400386
sergeyvda6c8ffc2016-11-22 18:28:54 -0800387 if (isHardware) {
388 sk_sp<Bitmap> hardwareBitmap = Bitmap::allocateHardwareBitmap(bitmap);
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000389 if (hasGainmap) {
Sally Qi587fb572023-03-03 15:50:06 -0800390 auto gm = uirenderer::Gainmap::allocateHardwareGainmap(gainmap);
391 if (gm) {
392 hardwareBitmap->setGainmap(std::move(gm));
393 }
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000394 }
sergeyvda6c8ffc2016-11-22 18:28:54 -0800395 return bitmap::createBitmap(env, hardwareBitmap.release(), bitmapCreateFlags);
396 }
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000397 Bitmap* heapBitmap = heapAlloc.getStorageObjAndReset();
398 if (hasGainmap && heapBitmap != nullptr) {
399 heapBitmap->setGainmap(std::move(gainmap));
400 }
401 return android::bitmap::createBitmap(env, heapBitmap, bitmapCreateFlags);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800402}
403
Ashok Bhatb091d472014-01-08 14:32:49 +0000404static jint nativeGetHeight(JNIEnv* env, jobject, jlong brdHandle) {
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000405 auto* brd = reinterpret_cast<BitmapRegionDecoderWrapper*>(brdHandle);
Matt Sarett1f979632015-10-27 10:33:20 -0400406 return static_cast<jint>(brd->height());
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800407}
408
Ashok Bhatb091d472014-01-08 14:32:49 +0000409static jint nativeGetWidth(JNIEnv* env, jobject, jlong brdHandle) {
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000410 auto* brd = reinterpret_cast<BitmapRegionDecoderWrapper*>(brdHandle);
Matt Sarett1f979632015-10-27 10:33:20 -0400411 return static_cast<jint>(brd->width());
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800412}
413
Ashok Bhatb091d472014-01-08 14:32:49 +0000414static void nativeClean(JNIEnv* env, jobject, jlong brdHandle) {
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000415 auto* brd = reinterpret_cast<BitmapRegionDecoderWrapper*>(brdHandle);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800416 delete brd;
417}
418
419///////////////////////////////////////////////////////////////////////////////
420
Daniel Micay76f6a862015-09-19 17:31:01 -0400421static const JNINativeMethod gBitmapRegionDecoderMethods[] = {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800422 { "nativeDecodeRegion",
Leon Scroggins III71fae622019-03-26 16:28:41 -0400423 "(JIIIILandroid/graphics/BitmapFactory$Options;JJ)Landroid/graphics/Bitmap;",
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800424 (void*)nativeDecodeRegion},
425
Ashok Bhatb091d472014-01-08 14:32:49 +0000426 { "nativeGetHeight", "(J)I", (void*)nativeGetHeight},
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800427
Ashok Bhatb091d472014-01-08 14:32:49 +0000428 { "nativeGetWidth", "(J)I", (void*)nativeGetWidth},
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800429
Ashok Bhatb091d472014-01-08 14:32:49 +0000430 { "nativeClean", "(J)V", (void*)nativeClean},
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800431
432 { "nativeNewInstance",
Leon Scroggins III96a13882020-03-04 10:29:04 -0500433 "([BII)Landroid/graphics/BitmapRegionDecoder;",
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800434 (void*)nativeNewInstanceFromByteArray
435 },
436
437 { "nativeNewInstance",
Leon Scroggins III96a13882020-03-04 10:29:04 -0500438 "(Ljava/io/InputStream;[B)Landroid/graphics/BitmapRegionDecoder;",
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800439 (void*)nativeNewInstanceFromStream
440 },
441
442 { "nativeNewInstance",
Leon Scroggins III96a13882020-03-04 10:29:04 -0500443 "(Ljava/io/FileDescriptor;)Landroid/graphics/BitmapRegionDecoder;",
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800444 (void*)nativeNewInstanceFromFileDescriptor
445 },
446
447 { "nativeNewInstance",
Leon Scroggins III96a13882020-03-04 10:29:04 -0500448 "(J)Landroid/graphics/BitmapRegionDecoder;",
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800449 (void*)nativeNewInstanceFromAsset
450 },
451};
452
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800453int register_android_graphics_BitmapRegionDecoder(JNIEnv* env)
454{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800455 return android::RegisterMethodsOrDie(env, "android/graphics/BitmapRegionDecoder",
456 gBitmapRegionDecoderMethods, NELEM(gBitmapRegionDecoderMethods));
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800457}