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