Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 1 | /* |
| 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 Sollenberger | 5368eda | 2019-10-25 11:20:03 -0400 | [diff] [blame] | 17 | #undef LOG_TAG |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 18 | #define LOG_TAG "BitmapRegionDecoder" |
| 19 | |
Leon Scroggins III | 23ac036 | 2020-05-04 15:38:58 -0400 | [diff] [blame] | 20 | #include "BitmapRegionDecoder.h" |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 21 | |
Leon Scroggins III | ee3bfe7 | 2019-01-31 08:42:23 -0500 | [diff] [blame] | 22 | #include <HardwareBitmapUploader.h> |
Ben Wagner | 60126ef | 2015-08-07 12:13:48 -0400 | [diff] [blame] | 23 | #include <androidfw/Asset.h> |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 24 | #include <sys/stat.h> |
| 25 | |
Ben Wagner | 18bd885 | 2016-10-24 14:50:10 -0400 | [diff] [blame] | 26 | #include <memory> |
| 27 | |
Fyodor Kyslov | b0da4a5 | 2023-01-26 18:39:33 +0000 | [diff] [blame^] | 28 | #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 Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 41 | using namespace android; |
| 42 | |
Fyodor Kyslov | b0da4a5 | 2023-01-26 18:39:33 +0000 | [diff] [blame^] | 43 | namespace android { |
| 44 | class BitmapRegionDecoderWrapper { |
| 45 | public: |
| 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; |
| 99 | HeapAllocator heapAlloc; |
| 100 | if (!mGainmapBRD->decodeRegion(&bm, &heapAlloc, desiredSubset, sampleSize, decodeColorType, |
| 101 | requireUnpremul, decodeColorSpace)) { |
| 102 | ALOGE("Error decoding Gainmap region"); |
| 103 | return false; |
| 104 | } |
| 105 | sk_sp<Bitmap> nativeBitmap(heapAlloc.getStorageObjAndReset()); |
| 106 | if (!nativeBitmap) { |
| 107 | ALOGE("OOM allocating Bitmap for Gainmap"); |
| 108 | return false; |
| 109 | } |
| 110 | auto gainmap = sp<uirenderer::Gainmap>::make(); |
| 111 | if (!gainmap) { |
| 112 | ALOGE("OOM allocating Gainmap"); |
| 113 | return false; |
| 114 | } |
| 115 | gainmap->info = mGainmapInfo; |
| 116 | gainmap->bitmap = std::move(nativeBitmap); |
| 117 | *outGainmap = std::move(gainmap); |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | SkIRect calculateGainmapRegion(const SkIRect& mainImageRegion) { |
| 122 | const float scaleX = ((float)mGainmapBRD->width()) / mMainImageBRD->width(); |
| 123 | const float scaleY = ((float)mGainmapBRD->height()) / mMainImageBRD->height(); |
| 124 | // TODO: Account for rounding error? |
| 125 | return SkIRect::MakeLTRB(mainImageRegion.left() * scaleX, mainImageRegion.top() * scaleY, |
| 126 | mainImageRegion.right() * scaleX, |
| 127 | mainImageRegion.bottom() * scaleY); |
| 128 | } |
| 129 | |
| 130 | bool hasGainmap() { return mGainmapBRD != nullptr; } |
| 131 | |
| 132 | int width() const { return mMainImageBRD->width(); } |
| 133 | int height() const { return mMainImageBRD->height(); } |
| 134 | |
| 135 | private: |
| 136 | BitmapRegionDecoderWrapper(std::unique_ptr<skia::BitmapRegionDecoder> mainImageBRD, |
| 137 | std::unique_ptr<skia::BitmapRegionDecoder> gainmapBRD, |
| 138 | SkGainmapInfo info, std::unique_ptr<SkStream> stream) |
| 139 | : mMainImageBRD(std::move(mainImageBRD)) |
| 140 | , mGainmapBRD(std::move(gainmapBRD)) |
| 141 | , mGainmapInfo(info) |
| 142 | , mGainmapStream(std::move(stream)) {} |
| 143 | |
| 144 | std::unique_ptr<skia::BitmapRegionDecoder> mMainImageBRD; |
| 145 | std::unique_ptr<skia::BitmapRegionDecoder> mGainmapBRD; |
| 146 | SkGainmapInfo mGainmapInfo; |
| 147 | std::unique_ptr<SkStream> mGainmapStream; |
| 148 | }; |
| 149 | } // namespace android |
| 150 | |
Leon Scroggins III | 23ac036 | 2020-05-04 15:38:58 -0400 | [diff] [blame] | 151 | static jobject createBitmapRegionDecoder(JNIEnv* env, sk_sp<SkData> data) { |
Fyodor Kyslov | b0da4a5 | 2023-01-26 18:39:33 +0000 | [diff] [blame^] | 152 | auto brd = android::BitmapRegionDecoderWrapper::Make(std::move(data)); |
Ben Wagner | 18bd885 | 2016-10-24 14:50:10 -0400 | [diff] [blame] | 153 | if (!brd) { |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 154 | doThrowIOE(env, "Image format not supported"); |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 155 | return nullObjectReturn("CreateBitmapRegionDecoder returned null"); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 156 | } |
| 157 | |
Ben Wagner | 18bd885 | 2016-10-24 14:50:10 -0400 | [diff] [blame] | 158 | return GraphicsJNI::createBitmapRegionDecoder(env, brd.release()); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | static jobject nativeNewInstanceFromByteArray(JNIEnv* env, jobject, jbyteArray byteArray, |
Leon Scroggins III | 96a1388 | 2020-03-04 10:29:04 -0500 | [diff] [blame] | 162 | jint offset, jint length) { |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 163 | AutoJavaByteArray ar(env, byteArray); |
Leon Scroggins III | 23ac036 | 2020-05-04 15:38:58 -0400 | [diff] [blame] | 164 | return createBitmapRegionDecoder(env, SkData::MakeWithCopy(ar.ptr() + offset, length)); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | static jobject nativeNewInstanceFromFileDescriptor(JNIEnv* env, jobject clazz, |
Leon Scroggins III | 96a1388 | 2020-03-04 10:29:04 -0500 | [diff] [blame] | 168 | jobject fileDescriptor) { |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 169 | NPE_CHECK_RETURN_ZERO(env, fileDescriptor); |
| 170 | |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 171 | jint descriptor = jniGetFDFromFileDescriptor(env, fileDescriptor); |
Derek Sollenberger | 5827cb5 | 2013-07-26 14:58:06 -0400 | [diff] [blame] | 172 | |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 173 | struct stat fdStat; |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 174 | if (fstat(descriptor, &fdStat) == -1) { |
| 175 | doThrowIOE(env, "broken file descriptor"); |
| 176 | return nullObjectReturn("fstat return -1"); |
| 177 | } |
| 178 | |
Leon Scroggins III | 23ac036 | 2020-05-04 15:38:58 -0400 | [diff] [blame] | 179 | return createBitmapRegionDecoder(env, SkData::MakeFromFD(descriptor)); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 180 | } |
| 181 | |
Leon Scroggins III | 96a1388 | 2020-03-04 10:29:04 -0500 | [diff] [blame] | 182 | static jobject nativeNewInstanceFromStream(JNIEnv* env, jobject clazz, jobject is, // InputStream |
| 183 | jbyteArray storage) { // byte[] |
Leon Scroggins III | 23ac036 | 2020-05-04 15:38:58 -0400 | [diff] [blame] | 184 | jobject brd = nullptr; |
| 185 | sk_sp<SkData> data = CopyJavaInputStream(env, is, storage); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 186 | |
Leon Scroggins III | 23ac036 | 2020-05-04 15:38:58 -0400 | [diff] [blame] | 187 | if (data) { |
| 188 | brd = createBitmapRegionDecoder(env, std::move(data)); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 189 | } |
Derek Sollenberger | 5827cb5 | 2013-07-26 14:58:06 -0400 | [diff] [blame] | 190 | return brd; |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 191 | } |
| 192 | |
Leon Scroggins III | 96a1388 | 2020-03-04 10:29:04 -0500 | [diff] [blame] | 193 | static jobject nativeNewInstanceFromAsset(JNIEnv* env, jobject clazz, jlong native_asset) { |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 194 | Asset* asset = reinterpret_cast<Asset*>(native_asset); |
Leon Scroggins III | 23ac036 | 2020-05-04 15:38:58 -0400 | [diff] [blame] | 195 | sk_sp<SkData> data = CopyAssetToData(asset); |
| 196 | if (!data) { |
| 197 | return nullptr; |
Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 198 | } |
Derek Sollenberger | 5827cb5 | 2013-07-26 14:58:06 -0400 | [diff] [blame] | 199 | |
Leon Scroggins III | 23ac036 | 2020-05-04 15:38:58 -0400 | [diff] [blame] | 200 | return createBitmapRegionDecoder(env, data); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /* |
| 204 | * nine patch not supported |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 205 | * purgeable not supported |
| 206 | * reportSizeToVM not supported |
| 207 | */ |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 208 | static jobject nativeDecodeRegion(JNIEnv* env, jobject, jlong brdHandle, jint inputX, |
Leon Scroggins III | 71fae62 | 2019-03-26 16:28:41 -0400 | [diff] [blame] | 209 | jint inputY, jint inputWidth, jint inputHeight, jobject options, jlong inBitmapHandle, |
| 210 | jlong colorSpaceHandle) { |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 211 | |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 212 | // Set default options. |
| 213 | int sampleSize = 1; |
| 214 | SkColorType colorType = kN32_SkColorType; |
| 215 | bool requireUnpremul = false; |
Leon Scroggins III | 71fae62 | 2019-03-26 16:28:41 -0400 | [diff] [blame] | 216 | jobject javaBitmap = nullptr; |
sergeyv | da6c8ffc | 2016-11-22 18:28:54 -0800 | [diff] [blame] | 217 | bool isHardware = false; |
Leon Scroggins III | 0e443d1 | 2018-12-19 11:38:35 -0500 | [diff] [blame] | 218 | sk_sp<SkColorSpace> colorSpace = GraphicsJNI::getNativeColorSpace(colorSpaceHandle); |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 219 | // Update the default options with any options supplied by the client. |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 220 | if (NULL != options) { |
| 221 | sampleSize = env->GetIntField(options, gOptions_sampleSizeFieldID); |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 222 | jobject jconfig = env->GetObjectField(options, gOptions_configFieldID); |
| 223 | colorType = GraphicsJNI::getNativeBitmapColorType(env, jconfig); |
sergeyv | da6c8ffc | 2016-11-22 18:28:54 -0800 | [diff] [blame] | 224 | isHardware = GraphicsJNI::isHardwareConfig(env, jconfig); |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 225 | requireUnpremul = !env->GetBooleanField(options, gOptions_premultipliedFieldID); |
| 226 | javaBitmap = env->GetObjectField(options, gOptions_bitmapFieldID); |
| 227 | // The Java options of ditherMode and preferQualityOverSpeed are deprecated. We will |
| 228 | // ignore the values of these fields. |
| 229 | |
| 230 | // Initialize these fields to indicate a failure. If the decode succeeds, we |
| 231 | // will update them later on. |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 232 | env->SetIntField(options, gOptions_widthFieldID, -1); |
| 233 | env->SetIntField(options, gOptions_heightFieldID, -1); |
| 234 | env->SetObjectField(options, gOptions_mimeFieldID, 0); |
Romain Guy | 95648b8 | 2017-04-13 18:43:42 -0700 | [diff] [blame] | 235 | env->SetObjectField(options, gOptions_outConfigFieldID, 0); |
| 236 | env->SetObjectField(options, gOptions_outColorSpaceFieldID, 0); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 237 | } |
| 238 | |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 239 | // Recycle a bitmap if possible. |
sergeyv | c1c5406 | 2016-10-19 18:47:26 -0700 | [diff] [blame] | 240 | android::Bitmap* recycledBitmap = nullptr; |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 241 | size_t recycledBytes = 0; |
| 242 | if (javaBitmap) { |
Leon Scroggins III | 71fae62 | 2019-03-26 16:28:41 -0400 | [diff] [blame] | 243 | recycledBitmap = &bitmap::toBitmap(inBitmapHandle); |
sergeyv | c69853c | 2016-10-07 14:14:09 -0700 | [diff] [blame] | 244 | if (recycledBitmap->isImmutable()) { |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 245 | ALOGW("Warning: Reusing an immutable bitmap as an image decoder target."); |
| 246 | } |
Leon Scroggins III | ca8aef6 | 2019-03-26 12:11:27 -0400 | [diff] [blame] | 247 | recycledBytes = recycledBitmap->getAllocationByteCount(); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 248 | } |
| 249 | |
Fyodor Kyslov | b0da4a5 | 2023-01-26 18:39:33 +0000 | [diff] [blame^] | 250 | auto* brd = reinterpret_cast<BitmapRegionDecoderWrapper*>(brdHandle); |
Leon Scroggins III | 8d592f9 | 2018-03-12 15:44:03 -0400 | [diff] [blame] | 251 | SkColorType decodeColorType = brd->computeOutputColorType(colorType); |
Alec Mouri | 1efd0a5 | 2022-01-20 13:58:23 -0800 | [diff] [blame] | 252 | |
| 253 | if (isHardware) { |
| 254 | if (decodeColorType == kRGBA_F16_SkColorType && |
Leon Scroggins III | ee3bfe7 | 2019-01-31 08:42:23 -0500 | [diff] [blame] | 255 | !uirenderer::HardwareBitmapUploader::hasFP16Support()) { |
Alec Mouri | 1efd0a5 | 2022-01-20 13:58:23 -0800 | [diff] [blame] | 256 | decodeColorType = kN32_SkColorType; |
| 257 | } |
| 258 | if (decodeColorType == kRGBA_1010102_SkColorType && |
| 259 | !uirenderer::HardwareBitmapUploader::has1010102Support()) { |
| 260 | decodeColorType = kN32_SkColorType; |
| 261 | } |
Leon Scroggins III | ee3bfe7 | 2019-01-31 08:42:23 -0500 | [diff] [blame] | 262 | } |
Leon Scroggins III | 8d592f9 | 2018-03-12 15:44:03 -0400 | [diff] [blame] | 263 | |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 264 | // Set up the pixel allocator |
Leon Scroggins III | 23ac036 | 2020-05-04 15:38:58 -0400 | [diff] [blame] | 265 | skia::BRDAllocator* allocator = nullptr; |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 266 | RecyclingClippingPixelAllocator recycleAlloc(recycledBitmap, recycledBytes); |
sergeyv | 4508218 | 2016-09-29 18:25:40 -0700 | [diff] [blame] | 267 | HeapAllocator heapAlloc; |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 268 | if (javaBitmap) { |
| 269 | allocator = &recycleAlloc; |
| 270 | // We are required to match the color type of the recycled bitmap. |
Romain Guy | 95648b8 | 2017-04-13 18:43:42 -0700 | [diff] [blame] | 271 | decodeColorType = recycledBitmap->info().colorType(); |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 272 | } else { |
sergeyv | 4508218 | 2016-09-29 18:25:40 -0700 | [diff] [blame] | 273 | allocator = &heapAlloc; |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 274 | } |
| 275 | |
Leon Scroggins III | 8d592f9 | 2018-03-12 15:44:03 -0400 | [diff] [blame] | 276 | sk_sp<SkColorSpace> decodeColorSpace = brd->computeOutputColorSpace( |
| 277 | decodeColorType, colorSpace); |
| 278 | |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 279 | // Decode the region. |
| 280 | SkIRect subset = SkIRect::MakeXYWH(inputX, inputY, inputWidth, inputHeight); |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 281 | SkBitmap bitmap; |
Romain Guy | 95648b8 | 2017-04-13 18:43:42 -0700 | [diff] [blame] | 282 | if (!brd->decodeRegion(&bitmap, allocator, subset, sampleSize, |
| 283 | decodeColorType, requireUnpremul, decodeColorSpace)) { |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 284 | return nullObjectReturn("Failed to decode region."); |
Owen Lin | f970c2e | 2012-04-25 18:49:09 +0800 | [diff] [blame] | 285 | } |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 286 | |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 287 | // If the client provided options, indicate that the decode was successful. |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 288 | if (NULL != options) { |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 289 | env->SetIntField(options, gOptions_widthFieldID, bitmap.width()); |
| 290 | env->SetIntField(options, gOptions_heightFieldID, bitmap.height()); |
Romain Guy | 95648b8 | 2017-04-13 18:43:42 -0700 | [diff] [blame] | 291 | |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 292 | env->SetObjectField(options, gOptions_mimeFieldID, |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 293 | getMimeTypeAsJavaString(env, brd->getEncodedFormat())); |
Matt Sarett | d31512b | 2015-12-09 15:16:31 -0500 | [diff] [blame] | 294 | if (env->ExceptionCheck()) { |
| 295 | return nullObjectReturn("OOM in encodedFormatToString()"); |
| 296 | } |
Romain Guy | 95648b8 | 2017-04-13 18:43:42 -0700 | [diff] [blame] | 297 | |
| 298 | jint configID = GraphicsJNI::colorTypeToLegacyBitmapConfig(decodeColorType); |
| 299 | if (isHardware) { |
| 300 | configID = GraphicsJNI::kHardware_LegacyBitmapConfig; |
| 301 | } |
| 302 | jobject config = env->CallStaticObjectMethod(gBitmapConfig_class, |
| 303 | gBitmapConfig_nativeToConfigMethodID, configID); |
| 304 | env->SetObjectField(options, gOptions_outConfigFieldID, config); |
| 305 | |
| 306 | env->SetObjectField(options, gOptions_outColorSpaceFieldID, |
Derek Sollenberger | bf3e464 | 2019-01-30 11:28:27 -0500 | [diff] [blame] | 307 | GraphicsJNI::getColorSpace(env, decodeColorSpace.get(), decodeColorType)); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 308 | } |
| 309 | |
Fyodor Kyslov | b0da4a5 | 2023-01-26 18:39:33 +0000 | [diff] [blame^] | 310 | sp<uirenderer::Gainmap> gainmap; |
| 311 | bool hasGainmap = brd->hasGainmap(); |
| 312 | if (hasGainmap) { |
| 313 | SkIRect gainmapSubset = brd->calculateGainmapRegion(subset); |
| 314 | if (!brd->decodeGainmapRegion(&gainmap, gainmapSubset, sampleSize, requireUnpremul)) { |
| 315 | // If there is an error decoding Gainmap - we don't fail. We just don't provide Gainmap |
| 316 | hasGainmap = false; |
| 317 | } |
| 318 | } |
| 319 | |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 320 | // If we may have reused a bitmap, we need to indicate that the pixels have changed. |
| 321 | if (javaBitmap) { |
| 322 | recycleAlloc.copyIfNecessary(); |
Fyodor Kyslov | b0da4a5 | 2023-01-26 18:39:33 +0000 | [diff] [blame^] | 323 | if (hasGainmap) { |
| 324 | recycledBitmap->setGainmap(std::move(gainmap)); |
| 325 | } |
Romain Guy | 5545518 | 2017-04-15 21:41:22 -0700 | [diff] [blame] | 326 | bitmap::reinitBitmap(env, javaBitmap, recycledBitmap->info(), !requireUnpremul); |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 327 | return javaBitmap; |
Owen Lin | f970c2e | 2012-04-25 18:49:09 +0800 | [diff] [blame] | 328 | } |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 329 | |
Chris Craik | 1abf5d6 | 2013-08-16 12:47:03 -0700 | [diff] [blame] | 330 | int bitmapCreateFlags = 0; |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 331 | if (!requireUnpremul) { |
sergeyv | c69853c | 2016-10-07 14:14:09 -0700 | [diff] [blame] | 332 | bitmapCreateFlags |= android::bitmap::kBitmapCreateFlag_Premultiplied; |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 333 | } |
sergeyv | da6c8ffc | 2016-11-22 18:28:54 -0800 | [diff] [blame] | 334 | if (isHardware) { |
| 335 | sk_sp<Bitmap> hardwareBitmap = Bitmap::allocateHardwareBitmap(bitmap); |
Fyodor Kyslov | b0da4a5 | 2023-01-26 18:39:33 +0000 | [diff] [blame^] | 336 | if (hasGainmap) { |
| 337 | hardwareBitmap->setGainmap(std::move(gainmap)); |
| 338 | } |
sergeyv | da6c8ffc | 2016-11-22 18:28:54 -0800 | [diff] [blame] | 339 | return bitmap::createBitmap(env, hardwareBitmap.release(), bitmapCreateFlags); |
| 340 | } |
Fyodor Kyslov | b0da4a5 | 2023-01-26 18:39:33 +0000 | [diff] [blame^] | 341 | Bitmap* heapBitmap = heapAlloc.getStorageObjAndReset(); |
| 342 | if (hasGainmap && heapBitmap != nullptr) { |
| 343 | heapBitmap->setGainmap(std::move(gainmap)); |
| 344 | } |
| 345 | return android::bitmap::createBitmap(env, heapBitmap, bitmapCreateFlags); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 346 | } |
| 347 | |
Ashok Bhat | b091d47 | 2014-01-08 14:32:49 +0000 | [diff] [blame] | 348 | static jint nativeGetHeight(JNIEnv* env, jobject, jlong brdHandle) { |
Fyodor Kyslov | b0da4a5 | 2023-01-26 18:39:33 +0000 | [diff] [blame^] | 349 | auto* brd = reinterpret_cast<BitmapRegionDecoderWrapper*>(brdHandle); |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 350 | return static_cast<jint>(brd->height()); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 351 | } |
| 352 | |
Ashok Bhat | b091d47 | 2014-01-08 14:32:49 +0000 | [diff] [blame] | 353 | static jint nativeGetWidth(JNIEnv* env, jobject, jlong brdHandle) { |
Fyodor Kyslov | b0da4a5 | 2023-01-26 18:39:33 +0000 | [diff] [blame^] | 354 | auto* brd = reinterpret_cast<BitmapRegionDecoderWrapper*>(brdHandle); |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 355 | return static_cast<jint>(brd->width()); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 356 | } |
| 357 | |
Ashok Bhat | b091d47 | 2014-01-08 14:32:49 +0000 | [diff] [blame] | 358 | static void nativeClean(JNIEnv* env, jobject, jlong brdHandle) { |
Fyodor Kyslov | b0da4a5 | 2023-01-26 18:39:33 +0000 | [diff] [blame^] | 359 | auto* brd = reinterpret_cast<BitmapRegionDecoderWrapper*>(brdHandle); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 360 | delete brd; |
| 361 | } |
| 362 | |
| 363 | /////////////////////////////////////////////////////////////////////////////// |
| 364 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 365 | static const JNINativeMethod gBitmapRegionDecoderMethods[] = { |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 366 | { "nativeDecodeRegion", |
Leon Scroggins III | 71fae62 | 2019-03-26 16:28:41 -0400 | [diff] [blame] | 367 | "(JIIIILandroid/graphics/BitmapFactory$Options;JJ)Landroid/graphics/Bitmap;", |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 368 | (void*)nativeDecodeRegion}, |
| 369 | |
Ashok Bhat | b091d47 | 2014-01-08 14:32:49 +0000 | [diff] [blame] | 370 | { "nativeGetHeight", "(J)I", (void*)nativeGetHeight}, |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 371 | |
Ashok Bhat | b091d47 | 2014-01-08 14:32:49 +0000 | [diff] [blame] | 372 | { "nativeGetWidth", "(J)I", (void*)nativeGetWidth}, |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 373 | |
Ashok Bhat | b091d47 | 2014-01-08 14:32:49 +0000 | [diff] [blame] | 374 | { "nativeClean", "(J)V", (void*)nativeClean}, |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 375 | |
| 376 | { "nativeNewInstance", |
Leon Scroggins III | 96a1388 | 2020-03-04 10:29:04 -0500 | [diff] [blame] | 377 | "([BII)Landroid/graphics/BitmapRegionDecoder;", |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 378 | (void*)nativeNewInstanceFromByteArray |
| 379 | }, |
| 380 | |
| 381 | { "nativeNewInstance", |
Leon Scroggins III | 96a1388 | 2020-03-04 10:29:04 -0500 | [diff] [blame] | 382 | "(Ljava/io/InputStream;[B)Landroid/graphics/BitmapRegionDecoder;", |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 383 | (void*)nativeNewInstanceFromStream |
| 384 | }, |
| 385 | |
| 386 | { "nativeNewInstance", |
Leon Scroggins III | 96a1388 | 2020-03-04 10:29:04 -0500 | [diff] [blame] | 387 | "(Ljava/io/FileDescriptor;)Landroid/graphics/BitmapRegionDecoder;", |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 388 | (void*)nativeNewInstanceFromFileDescriptor |
| 389 | }, |
| 390 | |
| 391 | { "nativeNewInstance", |
Leon Scroggins III | 96a1388 | 2020-03-04 10:29:04 -0500 | [diff] [blame] | 392 | "(J)Landroid/graphics/BitmapRegionDecoder;", |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 393 | (void*)nativeNewInstanceFromAsset |
| 394 | }, |
| 395 | }; |
| 396 | |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 397 | int register_android_graphics_BitmapRegionDecoder(JNIEnv* env) |
| 398 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 399 | return android::RegisterMethodsOrDie(env, "android/graphics/BitmapRegionDecoder", |
| 400 | gBitmapRegionDecoderMethods, NELEM(gBitmapRegionDecoderMethods)); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 401 | } |