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 | |
Alec Mouri | 7dcb7d2 | 2024-07-26 13:41:04 +0000 | [diff] [blame] | 90 | // 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 Kyslov | b0da4a5 | 2023-01-26 18:39:33 +0000 | [diff] [blame] | 101 | SkColorType decodeColorType = mGainmapBRD->computeOutputColorType(kN32_SkColorType); |
| 102 | sk_sp<SkColorSpace> decodeColorSpace = |
| 103 | mGainmapBRD->computeOutputColorSpace(decodeColorType, nullptr); |
| 104 | SkBitmap bm; |
John Reck | 40ffc1d | 2023-06-20 17:26:09 -0400 | [diff] [blame] | 105 | // 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 Reck | 9519757d | 2023-07-06 10:40:31 -0400 | [diff] [blame] | 118 | sk_sp<Bitmap> nativeBitmap = Bitmap::allocateHeapBitmap(SkImageInfo::Make( |
Alec Mouri | 7dcb7d2 | 2024-07-26 13:41:04 +0000 | [diff] [blame] | 119 | bitmapDimensions, decodeColorType, kPremul_SkAlphaType, decodeColorSpace)); |
Fyodor Kyslov | b0da4a5 | 2023-01-26 18:39:33 +0000 | [diff] [blame] | 120 | if (!nativeBitmap) { |
| 121 | ALOGE("OOM allocating Bitmap for Gainmap"); |
| 122 | return false; |
| 123 | } |
Alec Mouri | 7dcb7d2 | 2024-07-26 13:41:04 +0000 | [diff] [blame] | 124 | |
| 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 Reck | 40ffc1d | 2023-06-20 17:26:09 -0400 | [diff] [blame] | 143 | requireUnpremul, decodeColorSpace)) { |
| 144 | ALOGE("Error decoding Gainmap region"); |
| 145 | return false; |
| 146 | } |
| 147 | allocator.copyIfNecessary(); |
Fyodor Kyslov | b0da4a5 | 2023-01-26 18:39:33 +0000 | [diff] [blame] | 148 | 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 Mouri | 7dcb7d2 | 2024-07-26 13:41:04 +0000 | [diff] [blame] | 159 | struct Projection { |
| 160 | SkRect srcRect; |
| 161 | SkISize destSize; |
| 162 | }; |
| 163 | Projection calculateGainmapRegion(const SkIRect& mainImageRegion, SkISize dimensions) { |
Fyodor Kyslov | b0da4a5 | 2023-01-26 18:39:33 +0000 | [diff] [blame] | 164 | const float scaleX = ((float)mGainmapBRD->width()) / mMainImageBRD->width(); |
| 165 | const float scaleY = ((float)mGainmapBRD->height()) / mMainImageBRD->height(); |
Alec Mouri | 7dcb7d2 | 2024-07-26 13:41:04 +0000 | [diff] [blame] | 166 | |
| 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 Kyslov | b0da4a5 | 2023-01-26 18:39:33 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | bool hasGainmap() { return mGainmapBRD != nullptr; } |
| 187 | |
| 188 | int width() const { return mMainImageBRD->width(); } |
| 189 | int height() const { return mMainImageBRD->height(); } |
| 190 | |
| 191 | private: |
| 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 III | 23ac036 | 2020-05-04 15:38:58 -0400 | [diff] [blame] | 207 | static jobject createBitmapRegionDecoder(JNIEnv* env, sk_sp<SkData> data) { |
Fyodor Kyslov | b0da4a5 | 2023-01-26 18:39:33 +0000 | [diff] [blame] | 208 | auto brd = android::BitmapRegionDecoderWrapper::Make(std::move(data)); |
Ben Wagner | 18bd885 | 2016-10-24 14:50:10 -0400 | [diff] [blame] | 209 | if (!brd) { |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 210 | doThrowIOE(env, "Image format not supported"); |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 211 | return nullObjectReturn("CreateBitmapRegionDecoder returned null"); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 212 | } |
| 213 | |
Ben Wagner | 18bd885 | 2016-10-24 14:50:10 -0400 | [diff] [blame] | 214 | return GraphicsJNI::createBitmapRegionDecoder(env, brd.release()); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | static jobject nativeNewInstanceFromByteArray(JNIEnv* env, jobject, jbyteArray byteArray, |
Leon Scroggins III | 96a1388 | 2020-03-04 10:29:04 -0500 | [diff] [blame] | 218 | jint offset, jint length) { |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 219 | AutoJavaByteArray ar(env, byteArray); |
Leon Scroggins III | 23ac036 | 2020-05-04 15:38:58 -0400 | [diff] [blame] | 220 | return createBitmapRegionDecoder(env, SkData::MakeWithCopy(ar.ptr() + offset, length)); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | static jobject nativeNewInstanceFromFileDescriptor(JNIEnv* env, jobject clazz, |
Leon Scroggins III | 96a1388 | 2020-03-04 10:29:04 -0500 | [diff] [blame] | 224 | jobject fileDescriptor) { |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 225 | NPE_CHECK_RETURN_ZERO(env, fileDescriptor); |
| 226 | |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 227 | jint descriptor = jniGetFDFromFileDescriptor(env, fileDescriptor); |
Derek Sollenberger | 5827cb5 | 2013-07-26 14:58:06 -0400 | [diff] [blame] | 228 | |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 229 | struct stat fdStat; |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 230 | if (fstat(descriptor, &fdStat) == -1) { |
| 231 | doThrowIOE(env, "broken file descriptor"); |
| 232 | return nullObjectReturn("fstat return -1"); |
| 233 | } |
| 234 | |
Leon Scroggins III | 23ac036 | 2020-05-04 15:38:58 -0400 | [diff] [blame] | 235 | return createBitmapRegionDecoder(env, SkData::MakeFromFD(descriptor)); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 236 | } |
| 237 | |
Leon Scroggins III | 96a1388 | 2020-03-04 10:29:04 -0500 | [diff] [blame] | 238 | static jobject nativeNewInstanceFromStream(JNIEnv* env, jobject clazz, jobject is, // InputStream |
| 239 | jbyteArray storage) { // byte[] |
Leon Scroggins III | 23ac036 | 2020-05-04 15:38:58 -0400 | [diff] [blame] | 240 | jobject brd = nullptr; |
| 241 | sk_sp<SkData> data = CopyJavaInputStream(env, is, storage); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 242 | |
Leon Scroggins III | 23ac036 | 2020-05-04 15:38:58 -0400 | [diff] [blame] | 243 | if (data) { |
| 244 | brd = createBitmapRegionDecoder(env, std::move(data)); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 245 | } |
Derek Sollenberger | 5827cb5 | 2013-07-26 14:58:06 -0400 | [diff] [blame] | 246 | return brd; |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 247 | } |
| 248 | |
Leon Scroggins III | 96a1388 | 2020-03-04 10:29:04 -0500 | [diff] [blame] | 249 | static jobject nativeNewInstanceFromAsset(JNIEnv* env, jobject clazz, jlong native_asset) { |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 250 | Asset* asset = reinterpret_cast<Asset*>(native_asset); |
Leon Scroggins III | 23ac036 | 2020-05-04 15:38:58 -0400 | [diff] [blame] | 251 | sk_sp<SkData> data = CopyAssetToData(asset); |
| 252 | if (!data) { |
| 253 | return nullptr; |
Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 254 | } |
Derek Sollenberger | 5827cb5 | 2013-07-26 14:58:06 -0400 | [diff] [blame] | 255 | |
Leon Scroggins III | 23ac036 | 2020-05-04 15:38:58 -0400 | [diff] [blame] | 256 | return createBitmapRegionDecoder(env, data); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /* |
| 260 | * nine patch not supported |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 261 | * purgeable not supported |
| 262 | * reportSizeToVM not supported |
| 263 | */ |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 264 | static jobject nativeDecodeRegion(JNIEnv* env, jobject, jlong brdHandle, jint inputX, |
Leon Scroggins III | 71fae62 | 2019-03-26 16:28:41 -0400 | [diff] [blame] | 265 | jint inputY, jint inputWidth, jint inputHeight, jobject options, jlong inBitmapHandle, |
| 266 | jlong colorSpaceHandle) { |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 267 | |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 268 | // Set default options. |
| 269 | int sampleSize = 1; |
| 270 | SkColorType colorType = kN32_SkColorType; |
| 271 | bool requireUnpremul = false; |
Leon Scroggins III | 71fae62 | 2019-03-26 16:28:41 -0400 | [diff] [blame] | 272 | jobject javaBitmap = nullptr; |
sergeyv | da6c8ffc | 2016-11-22 18:28:54 -0800 | [diff] [blame] | 273 | bool isHardware = false; |
Leon Scroggins III | 0e443d1 | 2018-12-19 11:38:35 -0500 | [diff] [blame] | 274 | sk_sp<SkColorSpace> colorSpace = GraphicsJNI::getNativeColorSpace(colorSpaceHandle); |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 275 | // Update the default options with any options supplied by the client. |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 276 | if (NULL != options) { |
| 277 | sampleSize = env->GetIntField(options, gOptions_sampleSizeFieldID); |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 278 | jobject jconfig = env->GetObjectField(options, gOptions_configFieldID); |
| 279 | colorType = GraphicsJNI::getNativeBitmapColorType(env, jconfig); |
sergeyv | da6c8ffc | 2016-11-22 18:28:54 -0800 | [diff] [blame] | 280 | isHardware = GraphicsJNI::isHardwareConfig(env, jconfig); |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 281 | 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 Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 288 | env->SetIntField(options, gOptions_widthFieldID, -1); |
| 289 | env->SetIntField(options, gOptions_heightFieldID, -1); |
| 290 | env->SetObjectField(options, gOptions_mimeFieldID, 0); |
Romain Guy | 95648b8 | 2017-04-13 18:43:42 -0700 | [diff] [blame] | 291 | env->SetObjectField(options, gOptions_outConfigFieldID, 0); |
| 292 | env->SetObjectField(options, gOptions_outColorSpaceFieldID, 0); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 293 | } |
| 294 | |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 295 | // Recycle a bitmap if possible. |
sergeyv | c1c5406 | 2016-10-19 18:47:26 -0700 | [diff] [blame] | 296 | android::Bitmap* recycledBitmap = nullptr; |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 297 | if (javaBitmap) { |
Leon Scroggins III | 71fae62 | 2019-03-26 16:28:41 -0400 | [diff] [blame] | 298 | recycledBitmap = &bitmap::toBitmap(inBitmapHandle); |
sergeyv | c69853c | 2016-10-07 14:14:09 -0700 | [diff] [blame] | 299 | if (recycledBitmap->isImmutable()) { |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 300 | ALOGW("Warning: Reusing an immutable bitmap as an image decoder target."); |
| 301 | } |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 302 | } |
| 303 | |
Fyodor Kyslov | b0da4a5 | 2023-01-26 18:39:33 +0000 | [diff] [blame] | 304 | auto* brd = reinterpret_cast<BitmapRegionDecoderWrapper*>(brdHandle); |
Leon Scroggins III | 8d592f9 | 2018-03-12 15:44:03 -0400 | [diff] [blame] | 305 | SkColorType decodeColorType = brd->computeOutputColorType(colorType); |
Alec Mouri | 1efd0a5 | 2022-01-20 13:58:23 -0800 | [diff] [blame] | 306 | |
| 307 | if (isHardware) { |
| 308 | if (decodeColorType == kRGBA_F16_SkColorType && |
Leon Scroggins III | ee3bfe7 | 2019-01-31 08:42:23 -0500 | [diff] [blame] | 309 | !uirenderer::HardwareBitmapUploader::hasFP16Support()) { |
Alec Mouri | 1efd0a5 | 2022-01-20 13:58:23 -0800 | [diff] [blame] | 310 | decodeColorType = kN32_SkColorType; |
| 311 | } |
| 312 | if (decodeColorType == kRGBA_1010102_SkColorType && |
| 313 | !uirenderer::HardwareBitmapUploader::has1010102Support()) { |
| 314 | decodeColorType = kN32_SkColorType; |
| 315 | } |
Leon Scroggins III | ee3bfe7 | 2019-01-31 08:42:23 -0500 | [diff] [blame] | 316 | } |
Leon Scroggins III | 8d592f9 | 2018-03-12 15:44:03 -0400 | [diff] [blame] | 317 | |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 318 | // Set up the pixel allocator |
Leon Scroggins III | 23ac036 | 2020-05-04 15:38:58 -0400 | [diff] [blame] | 319 | skia::BRDAllocator* allocator = nullptr; |
John Reck | 40ffc1d | 2023-06-20 17:26:09 -0400 | [diff] [blame] | 320 | RecyclingClippingPixelAllocator recycleAlloc(recycledBitmap); |
sergeyv | 4508218 | 2016-09-29 18:25:40 -0700 | [diff] [blame] | 321 | HeapAllocator heapAlloc; |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 322 | if (javaBitmap) { |
| 323 | allocator = &recycleAlloc; |
| 324 | // We are required to match the color type of the recycled bitmap. |
Romain Guy | 95648b8 | 2017-04-13 18:43:42 -0700 | [diff] [blame] | 325 | decodeColorType = recycledBitmap->info().colorType(); |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 326 | } else { |
sergeyv | 4508218 | 2016-09-29 18:25:40 -0700 | [diff] [blame] | 327 | allocator = &heapAlloc; |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 328 | } |
| 329 | |
Leon Scroggins III | 8d592f9 | 2018-03-12 15:44:03 -0400 | [diff] [blame] | 330 | sk_sp<SkColorSpace> decodeColorSpace = brd->computeOutputColorSpace( |
| 331 | decodeColorType, colorSpace); |
| 332 | |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 333 | // Decode the region. |
John Reck | 40ffc1d | 2023-06-20 17:26:09 -0400 | [diff] [blame] | 334 | const SkIRect subset = SkIRect::MakeXYWH(inputX, inputY, inputWidth, inputHeight); |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 335 | SkBitmap bitmap; |
Romain Guy | 95648b8 | 2017-04-13 18:43:42 -0700 | [diff] [blame] | 336 | if (!brd->decodeRegion(&bitmap, allocator, subset, sampleSize, |
| 337 | decodeColorType, requireUnpremul, decodeColorSpace)) { |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 338 | return nullObjectReturn("Failed to decode region."); |
Owen Lin | f970c2e | 2012-04-25 18:49:09 +0800 | [diff] [blame] | 339 | } |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 340 | |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 341 | // If the client provided options, indicate that the decode was successful. |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 342 | if (NULL != options) { |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 343 | env->SetIntField(options, gOptions_widthFieldID, bitmap.width()); |
| 344 | env->SetIntField(options, gOptions_heightFieldID, bitmap.height()); |
Romain Guy | 95648b8 | 2017-04-13 18:43:42 -0700 | [diff] [blame] | 345 | |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 346 | env->SetObjectField(options, gOptions_mimeFieldID, |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 347 | getMimeTypeAsJavaString(env, brd->getEncodedFormat())); |
Matt Sarett | d31512b | 2015-12-09 15:16:31 -0500 | [diff] [blame] | 348 | if (env->ExceptionCheck()) { |
| 349 | return nullObjectReturn("OOM in encodedFormatToString()"); |
| 350 | } |
Romain Guy | 95648b8 | 2017-04-13 18:43:42 -0700 | [diff] [blame] | 351 | |
| 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 Sollenberger | bf3e464 | 2019-01-30 11:28:27 -0500 | [diff] [blame] | 361 | GraphicsJNI::getColorSpace(env, decodeColorSpace.get(), decodeColorType)); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 362 | } |
| 363 | |
John Reck | 40ffc1d | 2023-06-20 17:26:09 -0400 | [diff] [blame] | 364 | if (javaBitmap) { |
| 365 | recycleAlloc.copyIfNecessary(); |
| 366 | } |
| 367 | |
Fyodor Kyslov | b0da4a5 | 2023-01-26 18:39:33 +0000 | [diff] [blame] | 368 | sp<uirenderer::Gainmap> gainmap; |
| 369 | bool hasGainmap = brd->hasGainmap(); |
| 370 | if (hasGainmap) { |
Alec Mouri | 7dcb7d2 | 2024-07-26 13:41:04 +0000 | [diff] [blame] | 371 | SkISize gainmapDims = SkISize::Make(bitmap.width(), bitmap.height()); |
John Reck | 40ffc1d | 2023-06-20 17:26:09 -0400 | [diff] [blame] | 372 | if (javaBitmap) { |
John Reck | 9519757d | 2023-07-06 10:40:31 -0400 | [diff] [blame] | 373 | // If we are recycling we must match the inBitmap's relative dimensions |
Alec Mouri | 7dcb7d2 | 2024-07-26 13:41:04 +0000 | [diff] [blame] | 374 | gainmapDims.fWidth = recycledBitmap->width(); |
| 375 | gainmapDims.fHeight = recycledBitmap->height(); |
John Reck | 40ffc1d | 2023-06-20 17:26:09 -0400 | [diff] [blame] | 376 | } |
Alec Mouri | 7dcb7d2 | 2024-07-26 13:41:04 +0000 | [diff] [blame] | 377 | BitmapRegionDecoderWrapper::Projection gainmapProjection = |
| 378 | brd->calculateGainmapRegion(subset, gainmapDims); |
| 379 | if (!brd->decodeGainmapRegion(&gainmap, gainmapProjection.destSize, |
| 380 | gainmapProjection.srcRect, sampleSize, requireUnpremul)) { |
Fyodor Kyslov | b0da4a5 | 2023-01-26 18:39:33 +0000 | [diff] [blame] | 381 | // If there is an error decoding Gainmap - we don't fail. We just don't provide Gainmap |
| 382 | hasGainmap = false; |
| 383 | } |
| 384 | } |
| 385 | |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 386 | // If we may have reused a bitmap, we need to indicate that the pixels have changed. |
| 387 | if (javaBitmap) { |
Fyodor Kyslov | b0da4a5 | 2023-01-26 18:39:33 +0000 | [diff] [blame] | 388 | if (hasGainmap) { |
| 389 | recycledBitmap->setGainmap(std::move(gainmap)); |
| 390 | } |
Romain Guy | 5545518 | 2017-04-15 21:41:22 -0700 | [diff] [blame] | 391 | bitmap::reinitBitmap(env, javaBitmap, recycledBitmap->info(), !requireUnpremul); |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 392 | return javaBitmap; |
Owen Lin | f970c2e | 2012-04-25 18:49:09 +0800 | [diff] [blame] | 393 | } |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 394 | |
Chris Craik | 1abf5d6 | 2013-08-16 12:47:03 -0700 | [diff] [blame] | 395 | int bitmapCreateFlags = 0; |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 396 | if (!requireUnpremul) { |
sergeyv | c69853c | 2016-10-07 14:14:09 -0700 | [diff] [blame] | 397 | bitmapCreateFlags |= android::bitmap::kBitmapCreateFlag_Premultiplied; |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 398 | } |
John Reck | 40ffc1d | 2023-06-20 17:26:09 -0400 | [diff] [blame] | 399 | |
sergeyv | da6c8ffc | 2016-11-22 18:28:54 -0800 | [diff] [blame] | 400 | if (isHardware) { |
| 401 | sk_sp<Bitmap> hardwareBitmap = Bitmap::allocateHardwareBitmap(bitmap); |
Fyodor Kyslov | b0da4a5 | 2023-01-26 18:39:33 +0000 | [diff] [blame] | 402 | if (hasGainmap) { |
Sally Qi | 587fb57 | 2023-03-03 15:50:06 -0800 | [diff] [blame] | 403 | auto gm = uirenderer::Gainmap::allocateHardwareGainmap(gainmap); |
| 404 | if (gm) { |
| 405 | hardwareBitmap->setGainmap(std::move(gm)); |
| 406 | } |
Fyodor Kyslov | b0da4a5 | 2023-01-26 18:39:33 +0000 | [diff] [blame] | 407 | } |
sergeyv | da6c8ffc | 2016-11-22 18:28:54 -0800 | [diff] [blame] | 408 | return bitmap::createBitmap(env, hardwareBitmap.release(), bitmapCreateFlags); |
| 409 | } |
Fyodor Kyslov | b0da4a5 | 2023-01-26 18:39:33 +0000 | [diff] [blame] | 410 | 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 Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 415 | } |
| 416 | |
Ashok Bhat | b091d47 | 2014-01-08 14:32:49 +0000 | [diff] [blame] | 417 | static jint nativeGetHeight(JNIEnv* env, jobject, jlong brdHandle) { |
Fyodor Kyslov | b0da4a5 | 2023-01-26 18:39:33 +0000 | [diff] [blame] | 418 | auto* brd = reinterpret_cast<BitmapRegionDecoderWrapper*>(brdHandle); |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 419 | return static_cast<jint>(brd->height()); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 420 | } |
| 421 | |
Ashok Bhat | b091d47 | 2014-01-08 14:32:49 +0000 | [diff] [blame] | 422 | static jint nativeGetWidth(JNIEnv* env, jobject, jlong brdHandle) { |
Fyodor Kyslov | b0da4a5 | 2023-01-26 18:39:33 +0000 | [diff] [blame] | 423 | auto* brd = reinterpret_cast<BitmapRegionDecoderWrapper*>(brdHandle); |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 424 | return static_cast<jint>(brd->width()); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 425 | } |
| 426 | |
Ashok Bhat | b091d47 | 2014-01-08 14:32:49 +0000 | [diff] [blame] | 427 | static void nativeClean(JNIEnv* env, jobject, jlong brdHandle) { |
Fyodor Kyslov | b0da4a5 | 2023-01-26 18:39:33 +0000 | [diff] [blame] | 428 | auto* brd = reinterpret_cast<BitmapRegionDecoderWrapper*>(brdHandle); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 429 | delete brd; |
| 430 | } |
| 431 | |
| 432 | /////////////////////////////////////////////////////////////////////////////// |
| 433 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 434 | static const JNINativeMethod gBitmapRegionDecoderMethods[] = { |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 435 | { "nativeDecodeRegion", |
Leon Scroggins III | 71fae62 | 2019-03-26 16:28:41 -0400 | [diff] [blame] | 436 | "(JIIIILandroid/graphics/BitmapFactory$Options;JJ)Landroid/graphics/Bitmap;", |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 437 | (void*)nativeDecodeRegion}, |
| 438 | |
Ashok Bhat | b091d47 | 2014-01-08 14:32:49 +0000 | [diff] [blame] | 439 | { "nativeGetHeight", "(J)I", (void*)nativeGetHeight}, |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 440 | |
Ashok Bhat | b091d47 | 2014-01-08 14:32:49 +0000 | [diff] [blame] | 441 | { "nativeGetWidth", "(J)I", (void*)nativeGetWidth}, |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 442 | |
Ashok Bhat | b091d47 | 2014-01-08 14:32:49 +0000 | [diff] [blame] | 443 | { "nativeClean", "(J)V", (void*)nativeClean}, |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 444 | |
| 445 | { "nativeNewInstance", |
Leon Scroggins III | 96a1388 | 2020-03-04 10:29:04 -0500 | [diff] [blame] | 446 | "([BII)Landroid/graphics/BitmapRegionDecoder;", |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 447 | (void*)nativeNewInstanceFromByteArray |
| 448 | }, |
| 449 | |
| 450 | { "nativeNewInstance", |
Leon Scroggins III | 96a1388 | 2020-03-04 10:29:04 -0500 | [diff] [blame] | 451 | "(Ljava/io/InputStream;[B)Landroid/graphics/BitmapRegionDecoder;", |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 452 | (void*)nativeNewInstanceFromStream |
| 453 | }, |
| 454 | |
| 455 | { "nativeNewInstance", |
Leon Scroggins III | 96a1388 | 2020-03-04 10:29:04 -0500 | [diff] [blame] | 456 | "(Ljava/io/FileDescriptor;)Landroid/graphics/BitmapRegionDecoder;", |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 457 | (void*)nativeNewInstanceFromFileDescriptor |
| 458 | }, |
| 459 | |
| 460 | { "nativeNewInstance", |
Leon Scroggins III | 96a1388 | 2020-03-04 10:29:04 -0500 | [diff] [blame] | 461 | "(J)Landroid/graphics/BitmapRegionDecoder;", |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 462 | (void*)nativeNewInstanceFromAsset |
| 463 | }, |
| 464 | }; |
| 465 | |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 466 | int register_android_graphics_BitmapRegionDecoder(JNIEnv* env) |
| 467 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 468 | return android::RegisterMethodsOrDie(env, "android/graphics/BitmapRegionDecoder", |
| 469 | gBitmapRegionDecoderMethods, NELEM(gBitmapRegionDecoderMethods)); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 470 | } |