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