Joseph Wen | f1f48bc | 2010-07-19 16:59:51 +0800 | [diff] [blame] | 1 | #include "BitmapFactory.h" |
Fyodor Kyslov | 45b0a0f | 2023-02-04 00:41:47 +0000 | [diff] [blame] | 2 | |
| 3 | #include <Gainmap.h> |
| 4 | #include <HardwareBitmapUploader.h> |
| 5 | #include <androidfw/Asset.h> |
| 6 | #include <androidfw/ResourceTypes.h> |
| 7 | #include <cutils/compiler.h> |
| 8 | #include <fcntl.h> |
| 9 | #include <nativehelper/JNIPlatformHelp.h> |
| 10 | #include <stdint.h> |
| 11 | #include <stdio.h> |
| 12 | #include <sys/stat.h> |
| 13 | |
| 14 | #include <memory> |
| 15 | |
Ben Wagner | 60126ef | 2015-08-07 12:13:48 -0400 | [diff] [blame] | 16 | #include "CreateJavaOutputStreamAdaptor.h" |
Leon Scroggins III | 2bcdf6f | 2020-04-21 14:08:32 -0400 | [diff] [blame] | 17 | #include "FrontBufferedStream.h" |
Ben Wagner | 60126ef | 2015-08-07 12:13:48 -0400 | [diff] [blame] | 18 | #include "GraphicsJNI.h" |
Derek Sollenberger | 2173ea2 | 2020-02-19 15:37:29 -0500 | [diff] [blame] | 19 | #include "MimeType.h" |
Leon Scroggins | a06d86a | 2011-03-02 16:56:54 -0500 | [diff] [blame] | 20 | #include "NinePatchPeeker.h" |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 21 | #include "SkAndroidCodec.h" |
Kevin Lubick | 1175dc0 | 2022-02-28 12:41:27 -0500 | [diff] [blame] | 22 | #include "SkBitmap.h" |
| 23 | #include "SkBlendMode.h" |
John Reck | be67195 | 2021-01-13 22:39:32 -0500 | [diff] [blame] | 24 | #include "SkCanvas.h" |
Kevin Lubick | 1175dc0 | 2022-02-28 12:41:27 -0500 | [diff] [blame] | 25 | #include "SkColorSpace.h" |
| 26 | #include "SkEncodedImageFormat.h" |
Fyodor Kyslov | 45b0a0f | 2023-02-04 00:41:47 +0000 | [diff] [blame] | 27 | #include "SkGainmapInfo.h" |
Kevin Lubick | 1175dc0 | 2022-02-28 12:41:27 -0500 | [diff] [blame] | 28 | #include "SkImageInfo.h" |
Kevin Lubick | 1175dc0 | 2022-02-28 12:41:27 -0500 | [diff] [blame] | 29 | #include "SkPaint.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | #include "SkPixelRef.h" |
Kevin Lubick | 1175dc0 | 2022-02-28 12:41:27 -0500 | [diff] [blame] | 31 | #include "SkRect.h" |
| 32 | #include "SkRefCnt.h" |
| 33 | #include "SkSamplingOptions.h" |
| 34 | #include "SkSize.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | #include "SkStream.h" |
John Reck | be67195 | 2021-01-13 22:39:32 -0500 | [diff] [blame] | 36 | #include "SkString.h" |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 37 | #include "Utils.h" |
Ben Wagner | 60126ef | 2015-08-07 12:13:48 -0400 | [diff] [blame] | 38 | |
Joseph Wen | f1f48bc | 2010-07-19 16:59:51 +0800 | [diff] [blame] | 39 | jfieldID gOptions_justBoundsFieldID; |
| 40 | jfieldID gOptions_sampleSizeFieldID; |
| 41 | jfieldID gOptions_configFieldID; |
Romain Guy | 95648b8 | 2017-04-13 18:43:42 -0700 | [diff] [blame] | 42 | jfieldID gOptions_colorSpaceFieldID; |
Chris Craik | 1abf5d6 | 2013-08-16 12:47:03 -0700 | [diff] [blame] | 43 | jfieldID gOptions_premultipliedFieldID; |
Romain Guy | 2361098 | 2011-01-17 12:51:55 -0800 | [diff] [blame] | 44 | jfieldID gOptions_mutableFieldID; |
Joseph Wen | f1f48bc | 2010-07-19 16:59:51 +0800 | [diff] [blame] | 45 | jfieldID gOptions_ditherFieldID; |
Wei-Ta Chen | 953f909 | 2010-12-03 14:06:18 -0800 | [diff] [blame] | 46 | jfieldID gOptions_preferQualityOverSpeedFieldID; |
Chris Craik | 905e824 | 2013-06-05 09:59:05 -0700 | [diff] [blame] | 47 | jfieldID gOptions_scaledFieldID; |
| 48 | jfieldID gOptions_densityFieldID; |
| 49 | jfieldID gOptions_screenDensityFieldID; |
| 50 | jfieldID gOptions_targetDensityFieldID; |
Joseph Wen | f1f48bc | 2010-07-19 16:59:51 +0800 | [diff] [blame] | 51 | jfieldID gOptions_widthFieldID; |
| 52 | jfieldID gOptions_heightFieldID; |
| 53 | jfieldID gOptions_mimeFieldID; |
Romain Guy | e8d2ebb | 2017-02-09 18:38:47 -0800 | [diff] [blame] | 54 | jfieldID gOptions_outConfigFieldID; |
Romain Guy | 90fc43b | 2017-03-30 12:35:26 -0700 | [diff] [blame] | 55 | jfieldID gOptions_outColorSpaceFieldID; |
Joseph Wen | f1f48bc | 2010-07-19 16:59:51 +0800 | [diff] [blame] | 56 | jfieldID gOptions_mCancelID; |
Chet Haase | 37f74ca | 2010-12-08 17:56:36 -0800 | [diff] [blame] | 57 | jfieldID gOptions_bitmapFieldID; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | |
Chris Craik | 47cd8e9 | 2014-07-08 17:13:08 -0700 | [diff] [blame] | 59 | jfieldID gBitmap_ninePatchInsetsFieldID; |
| 60 | |
Romain Guy | e8d2ebb | 2017-02-09 18:38:47 -0800 | [diff] [blame] | 61 | jclass gBitmapConfig_class; |
| 62 | jmethodID gBitmapConfig_nativeToConfigMethodID; |
| 63 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | using namespace android; |
| 65 | |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 66 | const char* getMimeType(SkEncodedImageFormat format) { |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 67 | switch (format) { |
Hal Canary | 10219fb | 2016-11-23 20:41:22 -0500 | [diff] [blame] | 68 | case SkEncodedImageFormat::kBMP: |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 69 | return "image/bmp"; |
Hal Canary | 10219fb | 2016-11-23 20:41:22 -0500 | [diff] [blame] | 70 | case SkEncodedImageFormat::kGIF: |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 71 | return "image/gif"; |
Hal Canary | 10219fb | 2016-11-23 20:41:22 -0500 | [diff] [blame] | 72 | case SkEncodedImageFormat::kICO: |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 73 | return "image/x-ico"; |
Hal Canary | 10219fb | 2016-11-23 20:41:22 -0500 | [diff] [blame] | 74 | case SkEncodedImageFormat::kJPEG: |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 75 | return "image/jpeg"; |
Hal Canary | 10219fb | 2016-11-23 20:41:22 -0500 | [diff] [blame] | 76 | case SkEncodedImageFormat::kPNG: |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 77 | return "image/png"; |
Hal Canary | 10219fb | 2016-11-23 20:41:22 -0500 | [diff] [blame] | 78 | case SkEncodedImageFormat::kWEBP: |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 79 | return "image/webp"; |
Chong Zhang | 48fa890 | 2017-08-16 11:57:02 -0700 | [diff] [blame] | 80 | case SkEncodedImageFormat::kHEIF: |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 81 | return "image/heif"; |
Vignesh Venkatasubramanian | 4a46b9e | 2020-11-05 22:55:43 -0800 | [diff] [blame] | 82 | case SkEncodedImageFormat::kAVIF: |
| 83 | return "image/avif"; |
Hal Canary | 10219fb | 2016-11-23 20:41:22 -0500 | [diff] [blame] | 84 | case SkEncodedImageFormat::kWBMP: |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 85 | return "image/vnd.wap.wbmp"; |
Hal Canary | 10219fb | 2016-11-23 20:41:22 -0500 | [diff] [blame] | 86 | case SkEncodedImageFormat::kDNG: |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 87 | return "image/x-adobe-dng"; |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 88 | default: |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 89 | return nullptr; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | } |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 91 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 93 | jstring getMimeTypeAsJavaString(JNIEnv* env, SkEncodedImageFormat format) { |
Vladimir Marko | 7ab249a | 2015-01-06 18:17:52 +0000 | [diff] [blame] | 94 | jstring jstr = nullptr; |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 95 | const char* mimeType = getMimeType(format); |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 96 | if (mimeType) { |
Vladimir Marko | 7ab249a | 2015-01-06 18:17:52 +0000 | [diff] [blame] | 97 | // NOTE: Caller should env->ExceptionCheck() for OOM |
| 98 | // (can't check for nullptr as it's a valid return value) |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 99 | jstr = env->NewStringUTF(mimeType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | } |
| 101 | return jstr; |
| 102 | } |
| 103 | |
Chris Craik | 905e824 | 2013-06-05 09:59:05 -0700 | [diff] [blame] | 104 | class ScaleCheckingAllocator : public SkBitmap::HeapAllocator { |
| 105 | public: |
| 106 | ScaleCheckingAllocator(float scale, int size) |
| 107 | : mScale(scale), mSize(size) { |
| 108 | } |
| 109 | |
Mike Reed | 81397c4 | 2017-07-18 17:04:16 -0400 | [diff] [blame] | 110 | virtual bool allocPixelRef(SkBitmap* bitmap) { |
Chris Craik | 905e824 | 2013-06-05 09:59:05 -0700 | [diff] [blame] | 111 | // accounts for scale in final allocation, using eventual size and config |
Leon Scroggins III | ef691a3d | 2017-07-10 17:03:55 -0400 | [diff] [blame] | 112 | const int bytesPerPixel = SkColorTypeBytesPerPixel(bitmap->colorType()); |
Chris Craik | 905e824 | 2013-06-05 09:59:05 -0700 | [diff] [blame] | 113 | const int requestedSize = bytesPerPixel * |
| 114 | int(bitmap->width() * mScale + 0.5f) * |
| 115 | int(bitmap->height() * mScale + 0.5f); |
| 116 | if (requestedSize > mSize) { |
| 117 | ALOGW("bitmap for alloc reuse (%d bytes) can't fit scaled bitmap (%d bytes)", |
| 118 | mSize, requestedSize); |
| 119 | return false; |
| 120 | } |
Mike Reed | 81397c4 | 2017-07-18 17:04:16 -0400 | [diff] [blame] | 121 | return SkBitmap::HeapAllocator::allocPixelRef(bitmap); |
Chris Craik | 905e824 | 2013-06-05 09:59:05 -0700 | [diff] [blame] | 122 | } |
| 123 | private: |
| 124 | const float mScale; |
| 125 | const int mSize; |
| 126 | }; |
| 127 | |
Chris Craik | 7e8c03c | 2013-06-03 13:53:36 -0700 | [diff] [blame] | 128 | class RecyclingPixelAllocator : public SkBitmap::Allocator { |
| 129 | public: |
sergeyv | c1c5406 | 2016-10-19 18:47:26 -0700 | [diff] [blame] | 130 | RecyclingPixelAllocator(android::Bitmap* bitmap, unsigned int size) |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 131 | : mBitmap(bitmap), mSize(size) { |
Chris Craik | 7e8c03c | 2013-06-03 13:53:36 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | ~RecyclingPixelAllocator() { |
Chris Craik | 7e8c03c | 2013-06-03 13:53:36 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Mike Reed | 81397c4 | 2017-07-18 17:04:16 -0400 | [diff] [blame] | 137 | virtual bool allocPixelRef(SkBitmap* bitmap) { |
Leon Scroggins | 46cb9bd | 2014-03-06 15:36:39 -0500 | [diff] [blame] | 138 | const SkImageInfo& info = bitmap->info(); |
Leon Scroggins III | f35b989 | 2015-07-31 10:38:40 -0400 | [diff] [blame] | 139 | if (info.colorType() == kUnknown_SkColorType) { |
Leon Scroggins | 46cb9bd | 2014-03-06 15:36:39 -0500 | [diff] [blame] | 140 | ALOGW("unable to reuse a bitmap as the target has an unknown bitmap configuration"); |
Chris Craik | 7e8c03c | 2013-06-03 13:53:36 -0700 | [diff] [blame] | 141 | return false; |
| 142 | } |
Chris Craik | cd0ba71 | 2013-09-06 14:40:30 -0700 | [diff] [blame] | 143 | |
Mike Reed | 7569de0 | 2017-10-06 16:25:49 -0400 | [diff] [blame] | 144 | const size_t size = info.computeByteSize(bitmap->rowBytes()); |
Kevin Lubick | bab0bb3 | 2023-01-17 15:30:34 +0000 | [diff] [blame] | 145 | if (size > INT32_MAX) { |
Leon Scroggins | 46cb9bd | 2014-03-06 15:36:39 -0500 | [diff] [blame] | 146 | ALOGW("bitmap is too large"); |
| 147 | return false; |
| 148 | } |
| 149 | |
Leon Scroggins | 46cb9bd | 2014-03-06 15:36:39 -0500 | [diff] [blame] | 150 | if (size > mSize) { |
Dan Albert | 46d8444 | 2014-11-18 16:07:51 -0800 | [diff] [blame] | 151 | ALOGW("bitmap marked for reuse (%u bytes) can't fit new bitmap " |
| 152 | "(%zu bytes)", mSize, size); |
Derek Sollenberger | b644a3b | 2014-01-17 15:45:10 -0500 | [diff] [blame] | 153 | return false; |
| 154 | } |
| 155 | |
Leon Scroggins III | f51a80d | 2017-07-12 10:46:35 -0400 | [diff] [blame] | 156 | mBitmap->reconfigure(info, bitmap->rowBytes()); |
Mike Reed | 826deef | 2017-04-04 15:32:04 -0400 | [diff] [blame] | 157 | bitmap->setPixelRef(sk_ref_sp(mBitmap), 0, 0); |
Chris Craik | 7e8c03c | 2013-06-03 13:53:36 -0700 | [diff] [blame] | 158 | return true; |
| 159 | } |
| 160 | |
| 161 | private: |
sergeyv | c1c5406 | 2016-10-19 18:47:26 -0700 | [diff] [blame] | 162 | android::Bitmap* const mBitmap; |
Chris Craik | 7e8c03c | 2013-06-03 13:53:36 -0700 | [diff] [blame] | 163 | const unsigned int mSize; |
| 164 | }; |
| 165 | |
Anton Daubert | 4e5ec34 | 2016-03-07 17:30:20 +0100 | [diff] [blame] | 166 | // Necessary for decodes when the native decoder cannot scale to appropriately match the sampleSize |
| 167 | // (for example, RAW). If the sampleSize divides evenly into the dimension, we require that the |
| 168 | // scale matches exactly. If sampleSize does not divide evenly, we allow the decoder to choose how |
| 169 | // best to round. |
| 170 | static bool needsFineScale(const int fullSize, const int decodedSize, const int sampleSize) { |
| 171 | if (fullSize % sampleSize == 0 && fullSize / sampleSize != decodedSize) { |
| 172 | return true; |
| 173 | } else if ((fullSize / sampleSize + 1) != decodedSize && |
| 174 | (fullSize / sampleSize) != decodedSize) { |
| 175 | return true; |
| 176 | } |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | static bool needsFineScale(const SkISize fullSize, const SkISize decodedSize, |
| 181 | const int sampleSize) { |
| 182 | return needsFineScale(fullSize.width(), decodedSize.width(), sampleSize) || |
| 183 | needsFineScale(fullSize.height(), decodedSize.height(), sampleSize); |
| 184 | } |
| 185 | |
Christopher Cameron | cf3d3fc | 2024-09-11 14:18:44 +0000 | [diff] [blame] | 186 | static bool decodeGainmap(std::unique_ptr<SkAndroidCodec> codec, const SkGainmapInfo& gainmapInfo, |
Fyodor Kyslov | 45b0a0f | 2023-02-04 00:41:47 +0000 | [diff] [blame] | 187 | sp<uirenderer::Gainmap>* outGainmap, const int sampleSize, float scale) { |
John Reck | 818328c | 2023-05-09 17:50:33 -0400 | [diff] [blame] | 188 | SkColorType decodeColorType = kN32_SkColorType; |
| 189 | if (codec->getInfo().colorType() == kGray_8_SkColorType) { |
| 190 | decodeColorType = kGray_8_SkColorType; |
| 191 | } |
| 192 | decodeColorType = codec->computeOutputColorType(decodeColorType); |
Fyodor Kyslov | 45b0a0f | 2023-02-04 00:41:47 +0000 | [diff] [blame] | 193 | sk_sp<SkColorSpace> decodeColorSpace = codec->computeOutputColorSpace(decodeColorType, nullptr); |
| 194 | |
| 195 | SkISize size = codec->getSampledDimensions(sampleSize); |
| 196 | |
| 197 | int scaledWidth = size.width(); |
| 198 | int scaledHeight = size.height(); |
| 199 | bool willScale = false; |
| 200 | |
| 201 | // Apply a fine scaling step if necessary. |
| 202 | if (needsFineScale(codec->getInfo().dimensions(), size, sampleSize) || scale != 1.0f) { |
| 203 | willScale = true; |
| 204 | // The operation below may loose precision (integer division), but it is put this way to |
| 205 | // mimic main image scale calculation |
| 206 | scaledWidth = static_cast<int>((codec->getInfo().width() / sampleSize) * scale + 0.5f); |
| 207 | scaledHeight = static_cast<int>((codec->getInfo().height() / sampleSize) * scale + 0.5f); |
| 208 | } |
| 209 | |
| 210 | SkAlphaType alphaType = codec->computeOutputAlphaType(false); |
| 211 | |
| 212 | const SkImageInfo decodeInfo = SkImageInfo::Make(size.width(), size.height(), decodeColorType, |
| 213 | alphaType, decodeColorSpace); |
| 214 | |
John Reck | 818328c | 2023-05-09 17:50:33 -0400 | [diff] [blame] | 215 | SkImageInfo bitmapInfo = decodeInfo; |
| 216 | if (decodeColorType == kGray_8_SkColorType) { |
| 217 | // We treat gray8 as alpha8 in Bitmap's API surface |
| 218 | bitmapInfo = bitmapInfo.makeColorType(kAlpha_8_SkColorType); |
| 219 | } |
Fyodor Kyslov | 45b0a0f | 2023-02-04 00:41:47 +0000 | [diff] [blame] | 220 | SkBitmap decodeBitmap; |
| 221 | sk_sp<Bitmap> nativeBitmap = nullptr; |
| 222 | |
| 223 | if (!decodeBitmap.setInfo(bitmapInfo)) { |
| 224 | ALOGE("Failed to setInfo."); |
| 225 | return false; |
| 226 | } |
| 227 | |
| 228 | if (willScale) { |
| 229 | if (!decodeBitmap.tryAllocPixels(nullptr)) { |
| 230 | ALOGE("OOM allocating gainmap pixels."); |
| 231 | return false; |
| 232 | } |
| 233 | } else { |
| 234 | nativeBitmap = android::Bitmap::allocateHeapBitmap(&decodeBitmap); |
| 235 | if (!nativeBitmap) { |
| 236 | ALOGE("OOM allocating gainmap pixels."); |
| 237 | return false; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | // Use SkAndroidCodec to perform the decode. |
| 242 | SkAndroidCodec::AndroidOptions codecOptions; |
| 243 | codecOptions.fZeroInitialized = SkCodec::kYes_ZeroInitialized; |
| 244 | codecOptions.fSampleSize = sampleSize; |
| 245 | SkCodec::Result result = codec->getAndroidPixels(decodeInfo, decodeBitmap.getPixels(), |
| 246 | decodeBitmap.rowBytes(), &codecOptions); |
| 247 | switch (result) { |
| 248 | case SkCodec::kSuccess: |
| 249 | case SkCodec::kIncompleteInput: |
| 250 | break; |
| 251 | default: |
| 252 | ALOGE("Error decoding gainmap."); |
| 253 | return false; |
| 254 | } |
| 255 | |
| 256 | if (willScale) { |
| 257 | SkBitmap gainmapBitmap; |
| 258 | const float scaleX = scaledWidth / float(decodeBitmap.width()); |
| 259 | const float scaleY = scaledHeight / float(decodeBitmap.height()); |
| 260 | |
| 261 | SkColorType scaledColorType = decodeBitmap.colorType(); |
| 262 | gainmapBitmap.setInfo( |
| 263 | bitmapInfo.makeWH(scaledWidth, scaledHeight).makeColorType(scaledColorType)); |
| 264 | |
| 265 | nativeBitmap = android::Bitmap::allocateHeapBitmap(&gainmapBitmap); |
| 266 | if (!nativeBitmap) { |
| 267 | ALOGE("OOM allocating gainmap pixels."); |
| 268 | return false; |
| 269 | } |
| 270 | |
| 271 | SkPaint paint; |
| 272 | // kSrc_Mode instructs us to overwrite the uninitialized pixels in |
| 273 | // outputBitmap. Otherwise we would blend by default, which is not |
| 274 | // what we want. |
| 275 | paint.setBlendMode(SkBlendMode::kSrc); |
| 276 | |
| 277 | SkCanvas canvas(gainmapBitmap, SkCanvas::ColorBehavior::kLegacy); |
| 278 | canvas.scale(scaleX, scaleY); |
| 279 | decodeBitmap.setImmutable(); // so .asImage() doesn't make a copy |
| 280 | canvas.drawImage(decodeBitmap.asImage(), 0.0f, 0.0f, |
| 281 | SkSamplingOptions(SkFilterMode::kLinear), &paint); |
| 282 | } |
| 283 | |
| 284 | auto gainmap = sp<uirenderer::Gainmap>::make(); |
| 285 | if (!gainmap) { |
| 286 | ALOGE("OOM allocating Gainmap"); |
| 287 | return false; |
| 288 | } |
| 289 | |
| 290 | gainmap->info = gainmapInfo; |
| 291 | gainmap->bitmap = std::move(nativeBitmap); |
| 292 | *outGainmap = std::move(gainmap); |
| 293 | |
| 294 | return true; |
| 295 | } |
| 296 | |
Mike Reed | c7c6560 | 2017-07-26 10:40:25 -0400 | [diff] [blame] | 297 | static jobject doDecode(JNIEnv* env, std::unique_ptr<SkStreamRewindable> stream, |
Leon Scroggins III | 71fae62 | 2019-03-26 16:28:41 -0400 | [diff] [blame] | 298 | jobject padding, jobject options, jlong inBitmapHandle, |
| 299 | jlong colorSpaceHandle) { |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 300 | // Set default values for the options parameters. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 301 | int sampleSize = 1; |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 302 | bool onlyDecodeSize = false; |
Mike Reed | 42a1d08 | 2014-07-07 18:06:18 -0400 | [diff] [blame] | 303 | SkColorType prefColorType = kN32_SkColorType; |
sergeyv | da6c8ffc | 2016-11-22 18:28:54 -0800 | [diff] [blame] | 304 | bool isHardware = false; |
Romain Guy | 2361098 | 2011-01-17 12:51:55 -0800 | [diff] [blame] | 305 | bool isMutable = false; |
Chris Craik | 905e824 | 2013-06-05 09:59:05 -0700 | [diff] [blame] | 306 | float scale = 1.0f; |
Chris Craik | 1abf5d6 | 2013-08-16 12:47:03 -0700 | [diff] [blame] | 307 | bool requireUnpremultiplied = false; |
Chet Haase | 37f74ca | 2010-12-08 17:56:36 -0800 | [diff] [blame] | 308 | jobject javaBitmap = NULL; |
Leon Scroggins III | 0e443d1 | 2018-12-19 11:38:35 -0500 | [diff] [blame] | 309 | sk_sp<SkColorSpace> prefColorSpace = GraphicsJNI::getNativeColorSpace(colorSpaceHandle); |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 310 | |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 311 | // Update with options supplied by the client. |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 312 | if (options != NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 313 | sampleSize = env->GetIntField(options, gOptions_sampleSizeFieldID); |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 314 | // Correct a non-positive sampleSize. sampleSize defaults to zero within the |
| 315 | // options object, which is strange. |
| 316 | if (sampleSize <= 0) { |
| 317 | sampleSize = 1; |
| 318 | } |
| 319 | |
| 320 | if (env->GetBooleanField(options, gOptions_justBoundsFieldID)) { |
| 321 | onlyDecodeSize = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 322 | } |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 323 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 324 | // initialize these, in case we fail later on |
| 325 | env->SetIntField(options, gOptions_widthFieldID, -1); |
| 326 | env->SetIntField(options, gOptions_heightFieldID, -1); |
| 327 | env->SetObjectField(options, gOptions_mimeFieldID, 0); |
Romain Guy | e8d2ebb | 2017-02-09 18:38:47 -0800 | [diff] [blame] | 328 | env->SetObjectField(options, gOptions_outConfigFieldID, 0); |
Romain Guy | 90fc43b | 2017-03-30 12:35:26 -0700 | [diff] [blame] | 329 | env->SetObjectField(options, gOptions_outColorSpaceFieldID, 0); |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 330 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 331 | jobject jconfig = env->GetObjectField(options, gOptions_configFieldID); |
Mike Reed | 42a1d08 | 2014-07-07 18:06:18 -0400 | [diff] [blame] | 332 | prefColorType = GraphicsJNI::getNativeBitmapColorType(env, jconfig); |
sergeyv | da6c8ffc | 2016-11-22 18:28:54 -0800 | [diff] [blame] | 333 | isHardware = GraphicsJNI::isHardwareConfig(env, jconfig); |
Romain Guy | 2361098 | 2011-01-17 12:51:55 -0800 | [diff] [blame] | 334 | isMutable = env->GetBooleanField(options, gOptions_mutableFieldID); |
Chris Craik | 1abf5d6 | 2013-08-16 12:47:03 -0700 | [diff] [blame] | 335 | requireUnpremultiplied = !env->GetBooleanField(options, gOptions_premultipliedFieldID); |
Chet Haase | 37f74ca | 2010-12-08 17:56:36 -0800 | [diff] [blame] | 336 | javaBitmap = env->GetObjectField(options, gOptions_bitmapFieldID); |
Chris Craik | 905e824 | 2013-06-05 09:59:05 -0700 | [diff] [blame] | 337 | |
| 338 | if (env->GetBooleanField(options, gOptions_scaledFieldID)) { |
| 339 | const int density = env->GetIntField(options, gOptions_densityFieldID); |
| 340 | const int targetDensity = env->GetIntField(options, gOptions_targetDensityFieldID); |
| 341 | const int screenDensity = env->GetIntField(options, gOptions_screenDensityFieldID); |
| 342 | if (density != 0 && targetDensity != 0 && density != screenDensity) { |
| 343 | scale = (float) targetDensity / density; |
| 344 | } |
| 345 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 346 | } |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 347 | |
sergeyv | 9fbb0b5 | 2016-11-23 10:27:33 -0800 | [diff] [blame] | 348 | if (isMutable && isHardware) { |
Hans Boehm | e5b337d | 2019-01-07 17:42:05 -0800 | [diff] [blame] | 349 | doThrowIAE(env, "Bitmaps with Config.HARDWARE are always immutable"); |
sergeyv | 9fbb0b5 | 2016-11-23 10:27:33 -0800 | [diff] [blame] | 350 | return nullObjectReturn("Cannot create mutable hardware bitmap"); |
| 351 | } |
| 352 | |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 353 | // Create the codec. |
| 354 | NinePatchPeeker peeker; |
Leon Scroggins III | c782ad8 | 2018-01-14 10:50:45 -0500 | [diff] [blame] | 355 | std::unique_ptr<SkAndroidCodec> codec; |
| 356 | { |
| 357 | SkCodec::Result result; |
| 358 | std::unique_ptr<SkCodec> c = SkCodec::MakeFromStream(std::move(stream), &result, |
| 359 | &peeker); |
| 360 | if (!c) { |
| 361 | SkString msg; |
| 362 | msg.printf("Failed to create image decoder with message '%s'", |
| 363 | SkCodec::ResultToString(result)); |
| 364 | return nullObjectReturn(msg.c_str()); |
| 365 | } |
| 366 | |
| 367 | codec = SkAndroidCodec::MakeFromCodec(std::move(c)); |
| 368 | if (!codec) { |
| 369 | return nullObjectReturn("SkAndroidCodec::MakeFromCodec returned null"); |
| 370 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 371 | } |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 372 | |
Matt Sarett | 3b1b68d | 2015-12-14 13:08:33 -0500 | [diff] [blame] | 373 | // Do not allow ninepatch decodes to 565. In the past, decodes to 565 |
| 374 | // would dither, and we do not want to pre-dither ninepatches, since we |
| 375 | // know that they will be stretched. We no longer dither 565 decodes, |
| 376 | // but we continue to prevent ninepatches from decoding to 565, in order |
| 377 | // to maintain the old behavior. |
| 378 | if (peeker.mPatch && kRGB_565_SkColorType == prefColorType) { |
| 379 | prefColorType = kN32_SkColorType; |
| 380 | } |
| 381 | |
Anton Daubert | 4e5ec34 | 2016-03-07 17:30:20 +0100 | [diff] [blame] | 382 | // Determine the output size. |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 383 | SkISize size = codec->getSampledDimensions(sampleSize); |
Anton Daubert | 4e5ec34 | 2016-03-07 17:30:20 +0100 | [diff] [blame] | 384 | |
| 385 | int scaledWidth = size.width(); |
| 386 | int scaledHeight = size.height(); |
| 387 | bool willScale = false; |
| 388 | |
| 389 | // Apply a fine scaling step if necessary. |
| 390 | if (needsFineScale(codec->getInfo().dimensions(), size, sampleSize)) { |
| 391 | willScale = true; |
| 392 | scaledWidth = codec->getInfo().width() / sampleSize; |
| 393 | scaledHeight = codec->getInfo().height() / sampleSize; |
| 394 | } |
| 395 | |
Romain Guy | e8d2ebb | 2017-02-09 18:38:47 -0800 | [diff] [blame] | 396 | // Set the decode colorType |
| 397 | SkColorType decodeColorType = codec->computeOutputColorType(prefColorType); |
Leon Scroggins III | ee3bfe7 | 2019-01-31 08:42:23 -0500 | [diff] [blame] | 398 | if (decodeColorType == kRGBA_F16_SkColorType && isHardware && |
| 399 | !uirenderer::HardwareBitmapUploader::hasFP16Support()) { |
| 400 | decodeColorType = kN32_SkColorType; |
| 401 | } |
| 402 | |
Xiao Huang | fff1a29 | 2023-04-12 17:37:36 +0000 | [diff] [blame] | 403 | // b/276879147, fallback to RGBA_8888 when decoding HEIF and P010 is not supported. |
| 404 | if (decodeColorType == kRGBA_1010102_SkColorType && |
| 405 | codec->getEncodedFormat() == SkEncodedImageFormat::kHEIF && |
| 406 | env->CallStaticBooleanMethod(gImageDecoder_class, |
| 407 | gImageDecoder_isP010SupportedForHEVCMethodID) == JNI_FALSE) { |
| 408 | decodeColorType = kN32_SkColorType; |
| 409 | } |
| 410 | |
Romain Guy | 95648b8 | 2017-04-13 18:43:42 -0700 | [diff] [blame] | 411 | sk_sp<SkColorSpace> decodeColorSpace = codec->computeOutputColorSpace( |
| 412 | decodeColorType, prefColorSpace); |
Romain Guy | e8d2ebb | 2017-02-09 18:38:47 -0800 | [diff] [blame] | 413 | |
Anton Daubert | 4e5ec34 | 2016-03-07 17:30:20 +0100 | [diff] [blame] | 414 | // Set the options and return if the client only wants the size. |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 415 | if (options != NULL) { |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 416 | jstring mimeType = getMimeTypeAsJavaString(env, codec->getEncodedFormat()); |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 417 | if (env->ExceptionCheck()) { |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 418 | return nullObjectReturn("OOM in getMimeTypeAsJavaString()"); |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 419 | } |
Anton Daubert | 4e5ec34 | 2016-03-07 17:30:20 +0100 | [diff] [blame] | 420 | env->SetIntField(options, gOptions_widthFieldID, scaledWidth); |
| 421 | env->SetIntField(options, gOptions_heightFieldID, scaledHeight); |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 422 | env->SetObjectField(options, gOptions_mimeFieldID, mimeType); |
| 423 | |
Leon Scroggins III | ef691a3d | 2017-07-10 17:03:55 -0400 | [diff] [blame] | 424 | jint configID = GraphicsJNI::colorTypeToLegacyBitmapConfig(decodeColorType); |
Romain Guy | e8d2ebb | 2017-02-09 18:38:47 -0800 | [diff] [blame] | 425 | if (isHardware) { |
| 426 | configID = GraphicsJNI::kHardware_LegacyBitmapConfig; |
| 427 | } |
| 428 | jobject config = env->CallStaticObjectMethod(gBitmapConfig_class, |
| 429 | gBitmapConfig_nativeToConfigMethodID, configID); |
| 430 | env->SetObjectField(options, gOptions_outConfigFieldID, config); |
| 431 | |
Romain Guy | 90fc43b | 2017-03-30 12:35:26 -0700 | [diff] [blame] | 432 | env->SetObjectField(options, gOptions_outColorSpaceFieldID, |
Derek Sollenberger | bf3e464 | 2019-01-30 11:28:27 -0500 | [diff] [blame] | 433 | GraphicsJNI::getColorSpace(env, decodeColorSpace.get(), decodeColorType)); |
Romain Guy | 90fc43b | 2017-03-30 12:35:26 -0700 | [diff] [blame] | 434 | |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 435 | if (onlyDecodeSize) { |
| 436 | return nullptr; |
| 437 | } |
| 438 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 439 | |
Anton Daubert | 4e5ec34 | 2016-03-07 17:30:20 +0100 | [diff] [blame] | 440 | // Scale is necessary due to density differences. |
| 441 | if (scale != 1.0f) { |
| 442 | willScale = true; |
| 443 | scaledWidth = static_cast<int>(scaledWidth * scale + 0.5f); |
| 444 | scaledHeight = static_cast<int>(scaledHeight * scale + 0.5f); |
| 445 | } |
| 446 | |
sergeyv | c1c5406 | 2016-10-19 18:47:26 -0700 | [diff] [blame] | 447 | android::Bitmap* reuseBitmap = nullptr; |
Chris Craik | 9f58361 | 2013-05-20 18:13:47 -0700 | [diff] [blame] | 448 | unsigned int existingBufferSize = 0; |
Leon Scroggins III | 71fae62 | 2019-03-26 16:28:41 -0400 | [diff] [blame] | 449 | if (javaBitmap != nullptr) { |
| 450 | reuseBitmap = &bitmap::toBitmap(inBitmapHandle); |
sergeyv | c69853c | 2016-10-07 14:14:09 -0700 | [diff] [blame] | 451 | if (reuseBitmap->isImmutable()) { |
Derek Sollenberger | 2a6ecae | 2012-08-31 14:03:51 -0400 | [diff] [blame] | 452 | ALOGW("Unable to reuse an immutable bitmap as an image decoder target."); |
Leon Scroggins III | ca8aef6 | 2019-03-26 12:11:27 -0400 | [diff] [blame] | 453 | javaBitmap = nullptr; |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 454 | reuseBitmap = nullptr; |
Chris Craik | 7e8c03c | 2013-06-03 13:53:36 -0700 | [diff] [blame] | 455 | } else { |
Leon Scroggins III | ca8aef6 | 2019-03-26 12:11:27 -0400 | [diff] [blame] | 456 | existingBufferSize = reuseBitmap->getAllocationByteCount(); |
Derek Sollenberger | 2a6ecae | 2012-08-31 14:03:51 -0400 | [diff] [blame] | 457 | } |
Chet Haase | 37f74ca | 2010-12-08 17:56:36 -0800 | [diff] [blame] | 458 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 459 | |
sergeyv | 4508218 | 2016-09-29 18:25:40 -0700 | [diff] [blame] | 460 | HeapAllocator defaultAllocator; |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 461 | RecyclingPixelAllocator recyclingAllocator(reuseBitmap, existingBufferSize); |
Chris Craik | 905e824 | 2013-06-05 09:59:05 -0700 | [diff] [blame] | 462 | ScaleCheckingAllocator scaleCheckingAllocator(scale, existingBufferSize); |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 463 | SkBitmap::HeapAllocator heapAllocator; |
| 464 | SkBitmap::Allocator* decodeAllocator; |
| 465 | if (javaBitmap != nullptr && willScale) { |
| 466 | // This will allocate pixels using a HeapAllocator, since there will be an extra |
| 467 | // scaling step that copies these pixels into Java memory. This allocator |
| 468 | // also checks that the recycled javaBitmap is large enough. |
| 469 | decodeAllocator = &scaleCheckingAllocator; |
| 470 | } else if (javaBitmap != nullptr) { |
| 471 | decodeAllocator = &recyclingAllocator; |
sergeyv | da6c8ffc | 2016-11-22 18:28:54 -0800 | [diff] [blame] | 472 | } else if (willScale || isHardware) { |
| 473 | // This will allocate pixels using a HeapAllocator, |
| 474 | // for scale case: there will be an extra scaling step. |
| 475 | // for hardware case: there will be extra swizzling & upload to gralloc step. |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 476 | decodeAllocator = &heapAllocator; |
| 477 | } else { |
sergeyv | 4508218 | 2016-09-29 18:25:40 -0700 | [diff] [blame] | 478 | decodeAllocator = &defaultAllocator; |
Chris Craik | 905e824 | 2013-06-05 09:59:05 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Matt Sarett | 9e7cd63 | 2015-12-11 10:54:28 -0500 | [diff] [blame] | 481 | SkAlphaType alphaType = codec->computeOutputAlphaType(requireUnpremultiplied); |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 482 | |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 483 | const SkImageInfo decodeInfo = SkImageInfo::Make(size.width(), size.height(), |
Romain Guy | 90fc43b | 2017-03-30 12:35:26 -0700 | [diff] [blame] | 484 | decodeColorType, alphaType, decodeColorSpace); |
Matt Sarett | 6c38257 | 2017-02-21 17:42:41 -0500 | [diff] [blame] | 485 | |
Matt Sarett | 327c720 | 2017-02-22 17:38:20 -0500 | [diff] [blame] | 486 | SkImageInfo bitmapInfo = decodeInfo; |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 487 | if (decodeColorType == kGray_8_SkColorType) { |
| 488 | // The legacy implementation of BitmapFactory used kAlpha8 for |
| 489 | // grayscale images (before kGray8 existed). While the codec |
| 490 | // recognizes kGray8, we need to decode into a kAlpha8 bitmap |
| 491 | // in order to avoid a behavior change. |
Matt Sarett | ee80c471 | 2016-06-03 10:23:38 -0400 | [diff] [blame] | 492 | bitmapInfo = |
| 493 | bitmapInfo.makeColorType(kAlpha_8_SkColorType).makeAlphaType(kPremul_SkAlphaType); |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 494 | } |
Chris Craik | 7e8c03c | 2013-06-03 13:53:36 -0700 | [diff] [blame] | 495 | SkBitmap decodingBitmap; |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 496 | if (!decodingBitmap.setInfo(bitmapInfo) || |
Mike Reed | 81397c4 | 2017-07-18 17:04:16 -0400 | [diff] [blame] | 497 | !decodingBitmap.tryAllocPixels(decodeAllocator)) { |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 498 | // SkAndroidCodec should recommend a valid SkImageInfo, so setInfo() |
| 499 | // should only only fail if the calculated value for rowBytes is too |
| 500 | // large. |
| 501 | // tryAllocPixels() can fail due to OOM on the Java heap, OOM on the |
| 502 | // native heap, or the recycled javaBitmap being too small to reuse. |
| 503 | return nullptr; |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 504 | } |
| 505 | |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 506 | // Use SkAndroidCodec to perform the decode. |
| 507 | SkAndroidCodec::AndroidOptions codecOptions; |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 508 | codecOptions.fZeroInitialized = decodeAllocator == &defaultAllocator ? |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 509 | SkCodec::kYes_ZeroInitialized : SkCodec::kNo_ZeroInitialized; |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 510 | codecOptions.fSampleSize = sampleSize; |
| 511 | SkCodec::Result result = codec->getAndroidPixels(decodeInfo, decodingBitmap.getPixels(), |
| 512 | decodingBitmap.rowBytes(), &codecOptions); |
| 513 | switch (result) { |
| 514 | case SkCodec::kSuccess: |
| 515 | case SkCodec::kIncompleteInput: |
| 516 | break; |
| 517 | default: |
Matt Sarett | 3b1b68d | 2015-12-14 13:08:33 -0500 | [diff] [blame] | 518 | return nullObjectReturn("codec->getAndroidPixels() failed."); |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 519 | } |
| 520 | |
Stan Iliev | 7aedf6f | 2017-12-20 12:22:59 -0500 | [diff] [blame] | 521 | // This is weird so let me explain: we could use the scale parameter |
| 522 | // directly, but for historical reasons this is how the corresponding |
| 523 | // Dalvik code has always behaved. We simply recreate the behavior here. |
| 524 | // The result is slightly different from simply using scale because of |
| 525 | // the 0.5f rounding bias applied when computing the target image size |
| 526 | const float scaleX = scaledWidth / float(decodingBitmap.width()); |
| 527 | const float scaleY = scaledHeight / float(decodingBitmap.height()); |
| 528 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 529 | jbyteArray ninePatchChunk = NULL; |
Chris Craik | 47cd8e9 | 2014-07-08 17:13:08 -0700 | [diff] [blame] | 530 | if (peeker.mPatch != NULL) { |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 531 | if (willScale) { |
Stan Iliev | 7aedf6f | 2017-12-20 12:22:59 -0500 | [diff] [blame] | 532 | peeker.scale(scaleX, scaleY, scaledWidth, scaledHeight); |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 533 | } |
| 534 | |
Chris Craik | 47cd8e9 | 2014-07-08 17:13:08 -0700 | [diff] [blame] | 535 | size_t ninePatchArraySize = peeker.mPatch->serializedSize(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 536 | ninePatchChunk = env->NewByteArray(ninePatchArraySize); |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 537 | if (ninePatchChunk == NULL) { |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 538 | return nullObjectReturn("ninePatchChunk == null"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 539 | } |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 540 | |
| 541 | jbyte* array = (jbyte*) env->GetPrimitiveArrayCritical(ninePatchChunk, NULL); |
| 542 | if (array == NULL) { |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 543 | return nullObjectReturn("primitive array == null"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 544 | } |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 545 | |
Chris Craik | 47cd8e9 | 2014-07-08 17:13:08 -0700 | [diff] [blame] | 546 | memcpy(array, peeker.mPatch, peeker.mPatchSize); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 547 | env->ReleasePrimitiveArrayCritical(ninePatchChunk, array, 0); |
| 548 | } |
| 549 | |
Chris Craik | 47cd8e9 | 2014-07-08 17:13:08 -0700 | [diff] [blame] | 550 | jobject ninePatchInsets = NULL; |
| 551 | if (peeker.mHasInsets) { |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 552 | ninePatchInsets = peeker.createNinePatchInsets(env, scale); |
Mathieu Chartier | a08d10f | 2014-08-29 16:55:55 -0700 | [diff] [blame] | 553 | if (ninePatchInsets == NULL) { |
| 554 | return nullObjectReturn("nine patch insets == null"); |
| 555 | } |
Amith Yamasani | ec4a504 | 2012-04-04 10:27:15 -0700 | [diff] [blame] | 556 | if (javaBitmap != NULL) { |
Chris Craik | 47cd8e9 | 2014-07-08 17:13:08 -0700 | [diff] [blame] | 557 | env->SetObjectField(javaBitmap, gBitmap_ninePatchInsetsFieldID, ninePatchInsets); |
Amith Yamasani | ec4a504 | 2012-04-04 10:27:15 -0700 | [diff] [blame] | 558 | } |
| 559 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 560 | |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 561 | SkBitmap outputBitmap; |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 562 | if (willScale) { |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 563 | // Set the allocator for the outputBitmap. |
| 564 | SkBitmap::Allocator* outputAllocator; |
| 565 | if (javaBitmap != nullptr) { |
| 566 | outputAllocator = &recyclingAllocator; |
| 567 | } else { |
sergeyv | 4508218 | 2016-09-29 18:25:40 -0700 | [diff] [blame] | 568 | outputAllocator = &defaultAllocator; |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 569 | } |
| 570 | |
Leon Scroggins III | ef691a3d | 2017-07-10 17:03:55 -0400 | [diff] [blame] | 571 | SkColorType scaledColorType = decodingBitmap.colorType(); |
Leon Scroggins III | 8790be6 | 2013-12-03 16:26:51 -0500 | [diff] [blame] | 572 | // FIXME: If the alphaType is kUnpremul and the image has alpha, the |
| 573 | // colors may not be correct, since Skia does not yet support drawing |
| 574 | // to/from unpremultiplied bitmaps. |
Matt Sarett | ee80c471 | 2016-06-03 10:23:38 -0400 | [diff] [blame] | 575 | outputBitmap.setInfo( |
| 576 | bitmapInfo.makeWH(scaledWidth, scaledHeight).makeColorType(scaledColorType)); |
Mike Reed | 81397c4 | 2017-07-18 17:04:16 -0400 | [diff] [blame] | 577 | if (!outputBitmap.tryAllocPixels(outputAllocator)) { |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 578 | // This should only fail on OOM. The recyclingAllocator should have |
| 579 | // enough memory since we check this before decoding using the |
| 580 | // scaleCheckingAllocator. |
Raph Levien | 005bfc6 | 2012-09-20 22:51:47 -0700 | [diff] [blame] | 581 | return nullObjectReturn("allocation failed for scaled bitmap"); |
| 582 | } |
Leon Scroggins III | 1ffe727 | 2013-09-19 11:34:06 -0400 | [diff] [blame] | 583 | |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 584 | SkPaint paint; |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 585 | // kSrc_Mode instructs us to overwrite the uninitialized pixels in |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 586 | // outputBitmap. Otherwise we would blend by default, which is not |
| 587 | // what we want. |
Mike Reed | 260ab72 | 2016-10-07 15:59:20 -0400 | [diff] [blame] | 588 | paint.setBlendMode(SkBlendMode::kSrc); |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 589 | |
Matt Sarett | ca9b703 | 2017-04-13 12:18:47 -0400 | [diff] [blame] | 590 | SkCanvas canvas(outputBitmap, SkCanvas::ColorBehavior::kLegacy); |
Stan Iliev | 7aedf6f | 2017-12-20 12:22:59 -0500 | [diff] [blame] | 591 | canvas.scale(scaleX, scaleY); |
Mike Reed | 7994a31 | 2021-01-28 18:06:26 -0500 | [diff] [blame] | 592 | decodingBitmap.setImmutable(); // so .asImage() doesn't make a copy |
| 593 | canvas.drawImage(decodingBitmap.asImage(), 0.0f, 0.0f, |
| 594 | SkSamplingOptions(SkFilterMode::kLinear), &paint); |
Chris Craik | 7e8c03c | 2013-06-03 13:53:36 -0700 | [diff] [blame] | 595 | } else { |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 596 | outputBitmap.swap(decodingBitmap); |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 597 | } |
| 598 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 599 | if (padding) { |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 600 | peeker.getPadding(env, padding); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 601 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 602 | |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 603 | // If we get here, the outputBitmap should have an installed pixelref. |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 604 | if (outputBitmap.pixelRef() == NULL) { |
Marco Nelissen | b2fe3be | 2012-05-07 11:24:13 -0700 | [diff] [blame] | 605 | return nullObjectReturn("Got null SkPixelRef"); |
| 606 | } |
Romain Guy | 2361098 | 2011-01-17 12:51:55 -0800 | [diff] [blame] | 607 | |
Fyodor Kyslov | 45b0a0f | 2023-02-04 00:41:47 +0000 | [diff] [blame] | 608 | bool hasGainmap = false; |
| 609 | SkGainmapInfo gainmapInfo; |
Christopher Cameron | cf3d3fc | 2024-09-11 14:18:44 +0000 | [diff] [blame] | 610 | std::unique_ptr<SkAndroidCodec> gainmapCodec; |
Fyodor Kyslov | 45b0a0f | 2023-02-04 00:41:47 +0000 | [diff] [blame] | 611 | sp<uirenderer::Gainmap> gainmap = nullptr; |
| 612 | if (result == SkCodec::kSuccess) { |
Christopher Cameron | cf3d3fc | 2024-09-11 14:18:44 +0000 | [diff] [blame] | 613 | hasGainmap = codec->getGainmapAndroidCodec(&gainmapInfo, &gainmapCodec); |
Fyodor Kyslov | 45b0a0f | 2023-02-04 00:41:47 +0000 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | if (hasGainmap) { |
| 617 | hasGainmap = |
Christopher Cameron | cf3d3fc | 2024-09-11 14:18:44 +0000 | [diff] [blame] | 618 | decodeGainmap(std::move(gainmapCodec), gainmapInfo, &gainmap, sampleSize, scale); |
Fyodor Kyslov | 45b0a0f | 2023-02-04 00:41:47 +0000 | [diff] [blame] | 619 | } |
| 620 | |
Chris Craik | 7e8c03c | 2013-06-03 13:53:36 -0700 | [diff] [blame] | 621 | if (!isMutable && javaBitmap == NULL) { |
Romain Guy | 2361098 | 2011-01-17 12:51:55 -0800 | [diff] [blame] | 622 | // promise we will never change our pixels (great for sharing and pictures) |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 623 | outputBitmap.setImmutable(); |
Romain Guy | 2361098 | 2011-01-17 12:51:55 -0800 | [diff] [blame] | 624 | } |
Chet Haase | 37f74ca | 2010-12-08 17:56:36 -0800 | [diff] [blame] | 625 | |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 626 | bool isPremultiplied = !requireUnpremultiplied; |
| 627 | if (javaBitmap != nullptr) { |
Fyodor Kyslov | 45b0a0f | 2023-02-04 00:41:47 +0000 | [diff] [blame] | 628 | if (hasGainmap) { |
| 629 | reuseBitmap->setGainmap(std::move(gainmap)); |
| 630 | } |
sergeyv | c69853c | 2016-10-07 14:14:09 -0700 | [diff] [blame] | 631 | bitmap::reinitBitmap(env, javaBitmap, outputBitmap.info(), isPremultiplied); |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 632 | outputBitmap.notifyPixelsChanged(); |
Chet Haase | 37f74ca | 2010-12-08 17:56:36 -0800 | [diff] [blame] | 633 | // If a java bitmap was passed in for reuse, pass it back |
| 634 | return javaBitmap; |
| 635 | } |
Chris Craik | 1abf5d6 | 2013-08-16 12:47:03 -0700 | [diff] [blame] | 636 | |
| 637 | int bitmapCreateFlags = 0x0; |
sergeyv | c69853c | 2016-10-07 14:14:09 -0700 | [diff] [blame] | 638 | if (isMutable) bitmapCreateFlags |= android::bitmap::kBitmapCreateFlag_Mutable; |
| 639 | if (isPremultiplied) bitmapCreateFlags |= android::bitmap::kBitmapCreateFlag_Premultiplied; |
Chris Craik | 1abf5d6 | 2013-08-16 12:47:03 -0700 | [diff] [blame] | 640 | |
sergeyv | da6c8ffc | 2016-11-22 18:28:54 -0800 | [diff] [blame] | 641 | if (isHardware) { |
| 642 | sk_sp<Bitmap> hardwareBitmap = Bitmap::allocateHardwareBitmap(outputBitmap); |
sergeyv | d67bb1e | 2017-06-28 12:44:03 -0700 | [diff] [blame] | 643 | if (!hardwareBitmap.get()) { |
| 644 | return nullObjectReturn("Failed to allocate a hardware bitmap"); |
| 645 | } |
Fyodor Kyslov | 45b0a0f | 2023-02-04 00:41:47 +0000 | [diff] [blame] | 646 | if (hasGainmap) { |
Sally Qi | 587fb57 | 2023-03-03 15:50:06 -0800 | [diff] [blame] | 647 | auto gm = uirenderer::Gainmap::allocateHardwareGainmap(gainmap); |
| 648 | if (gm) { |
| 649 | hardwareBitmap->setGainmap(std::move(gm)); |
| 650 | } |
Fyodor Kyslov | 45b0a0f | 2023-02-04 00:41:47 +0000 | [diff] [blame] | 651 | } |
| 652 | |
sergeyv | da6c8ffc | 2016-11-22 18:28:54 -0800 | [diff] [blame] | 653 | return bitmap::createBitmap(env, hardwareBitmap.release(), bitmapCreateFlags, |
| 654 | ninePatchChunk, ninePatchInsets, -1); |
| 655 | } |
| 656 | |
Fyodor Kyslov | 45b0a0f | 2023-02-04 00:41:47 +0000 | [diff] [blame] | 657 | Bitmap* heapBitmap = defaultAllocator.getStorageObjAndReset(); |
| 658 | if (hasGainmap && heapBitmap != nullptr) { |
| 659 | heapBitmap->setGainmap(std::move(gainmap)); |
| 660 | } |
| 661 | |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 662 | // now create the java bitmap |
Fyodor Kyslov | 45b0a0f | 2023-02-04 00:41:47 +0000 | [diff] [blame] | 663 | return bitmap::createBitmap(env, heapBitmap, bitmapCreateFlags, ninePatchChunk, ninePatchInsets, |
| 664 | -1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 665 | } |
| 666 | |
Chris Craik | 905e824 | 2013-06-05 09:59:05 -0700 | [diff] [blame] | 667 | static jobject nativeDecodeStream(JNIEnv* env, jobject clazz, jobject is, jbyteArray storage, |
Leon Scroggins III | 71fae62 | 2019-03-26 16:28:41 -0400 | [diff] [blame] | 668 | jobject padding, jobject options, jlong inBitmapHandle, jlong colorSpaceHandle) { |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 669 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 670 | jobject bitmap = NULL; |
Ben Wagner | 60126ef | 2015-08-07 12:13:48 -0400 | [diff] [blame] | 671 | std::unique_ptr<SkStream> stream(CreateJavaInputStreamAdaptor(env, is, storage)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 672 | |
Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 673 | if (stream.get()) { |
Leon Scroggins III | 2bcdf6f | 2020-04-21 14:08:32 -0400 | [diff] [blame] | 674 | std::unique_ptr<SkStreamRewindable> bufferedStream(skia::FrontBufferedStream::Make( |
| 675 | std::move(stream), SkCodec::MinBufferedBytesNeeded())); |
Leon Scroggins III | 7315f1b | 2013-09-10 20:26:05 -0400 | [diff] [blame] | 676 | SkASSERT(bufferedStream.get() != NULL); |
Leon Scroggins III | 71fae62 | 2019-03-26 16:28:41 -0400 | [diff] [blame] | 677 | bitmap = doDecode(env, std::move(bufferedStream), padding, options, inBitmapHandle, |
| 678 | colorSpaceHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 679 | } |
| 680 | return bitmap; |
| 681 | } |
| 682 | |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 683 | static jobject nativeDecodeFileDescriptor(JNIEnv* env, jobject clazz, jobject fileDescriptor, |
Leon Scroggins III | 71fae62 | 2019-03-26 16:28:41 -0400 | [diff] [blame] | 684 | jobject padding, jobject bitmapFactoryOptions, jlong inBitmapHandle, jlong colorSpaceHandle) { |
Michael Hoisie | a7e2e59 | 2024-05-21 00:05:11 +0000 | [diff] [blame] | 685 | #ifdef _WIN32 // LayoutLib for Windows does not support F_DUPFD_CLOEXEC |
| 686 | return nullObjectReturn("Not supported on Windows"); |
Derek Sollenberger | c5882c4 | 2019-10-25 11:11:32 -0400 | [diff] [blame] | 687 | #else |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 688 | NPE_CHECK_RETURN_ZERO(env, fileDescriptor); |
| 689 | |
Derek Sollenberger | 5cb769d | 2014-09-24 09:20:09 -0400 | [diff] [blame] | 690 | int descriptor = jniGetFDFromFileDescriptor(env, fileDescriptor); |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 691 | |
Derek Sollenberger | 5827cb5 | 2013-07-26 14:58:06 -0400 | [diff] [blame] | 692 | struct stat fdStat; |
| 693 | if (fstat(descriptor, &fdStat) == -1) { |
| 694 | doThrowIOE(env, "broken file descriptor"); |
| 695 | return nullObjectReturn("fstat return -1"); |
| 696 | } |
| 697 | |
Derek Sollenberger | 5cb769d | 2014-09-24 09:20:09 -0400 | [diff] [blame] | 698 | // Restore the descriptor's offset on exiting this function. Even though |
| 699 | // we dup the descriptor, both the original and dup refer to the same open |
| 700 | // file description and changes to the file offset in one impact the other. |
Jérôme Poichet | d29c902 | 2014-09-26 18:59:11 +0000 | [diff] [blame] | 701 | AutoFDSeek autoRestore(descriptor); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 702 | |
Derek Sollenberger | 5cb769d | 2014-09-24 09:20:09 -0400 | [diff] [blame] | 703 | // Duplicate the descriptor here to prevent leaking memory. A leak occurs |
| 704 | // if we only close the file descriptor and not the file object it is used to |
| 705 | // create. If we don't explicitly clean up the file (which in turn closes the |
| 706 | // descriptor) the buffers allocated internally by fseek will be leaked. |
Nick Kralevich | 4b3a08c | 2019-01-28 10:39:10 -0800 | [diff] [blame] | 707 | int dupDescriptor = fcntl(descriptor, F_DUPFD_CLOEXEC, 0); |
Derek Sollenberger | 5cb769d | 2014-09-24 09:20:09 -0400 | [diff] [blame] | 708 | |
| 709 | FILE* file = fdopen(dupDescriptor, "r"); |
Leon Scroggins III | 0102f8a | 2014-01-14 15:14:57 -0500 | [diff] [blame] | 710 | if (file == NULL) { |
Derek Sollenberger | 5cb769d | 2014-09-24 09:20:09 -0400 | [diff] [blame] | 711 | // cleanup the duplicated descriptor since it will not be closed when the |
| 712 | // file is cleaned up (fclose). |
| 713 | close(dupDescriptor); |
Leon Scroggins III | 0102f8a | 2014-01-14 15:14:57 -0500 | [diff] [blame] | 714 | return nullObjectReturn("Could not open file"); |
Leon Scroggins III | f65183f | 2013-10-07 16:32:14 -0400 | [diff] [blame] | 715 | } |
Leon Scroggins III | 0102f8a | 2014-01-14 15:14:57 -0500 | [diff] [blame] | 716 | |
Leon Scroggins III | dd3c06c | 2017-03-10 10:50:33 -0500 | [diff] [blame] | 717 | std::unique_ptr<SkFILEStream> fileStream(new SkFILEStream(file)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 718 | |
Yujie Qin | c1d7b7f | 2016-02-29 14:00:29 +0100 | [diff] [blame] | 719 | // If there is no offset for the file descriptor, we use SkFILEStream directly. |
| 720 | if (::lseek(descriptor, 0, SEEK_CUR) == 0) { |
| 721 | assert(isSeekable(dupDescriptor)); |
Leon Scroggins III | 0e443d1 | 2018-12-19 11:38:35 -0500 | [diff] [blame] | 722 | return doDecode(env, std::move(fileStream), padding, bitmapFactoryOptions, |
Leon Scroggins III | 71fae62 | 2019-03-26 16:28:41 -0400 | [diff] [blame] | 723 | inBitmapHandle, colorSpaceHandle); |
Yujie Qin | c1d7b7f | 2016-02-29 14:00:29 +0100 | [diff] [blame] | 724 | } |
| 725 | |
Leon Scroggins III | 0aa39dc | 2014-06-03 12:19:32 -0400 | [diff] [blame] | 726 | // Use a buffered stream. Although an SkFILEStream can be rewound, this |
| 727 | // ensures that SkImageDecoder::Factory never rewinds beyond the |
| 728 | // current position of the file descriptor. |
Leon Scroggins III | 2bcdf6f | 2020-04-21 14:08:32 -0400 | [diff] [blame] | 729 | std::unique_ptr<SkStreamRewindable> stream(skia::FrontBufferedStream::Make( |
| 730 | std::move(fileStream), SkCodec::MinBufferedBytesNeeded())); |
Leon Scroggins III | 2826e5f | 2014-02-05 19:46:02 -0500 | [diff] [blame] | 731 | |
Leon Scroggins III | 71fae62 | 2019-03-26 16:28:41 -0400 | [diff] [blame] | 732 | return doDecode(env, std::move(stream), padding, bitmapFactoryOptions, inBitmapHandle, |
| 733 | colorSpaceHandle); |
Derek Sollenberger | c5882c4 | 2019-10-25 11:11:32 -0400 | [diff] [blame] | 734 | #endif |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 735 | } |
| 736 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 737 | static jobject nativeDecodeAsset(JNIEnv* env, jobject clazz, jlong native_asset, |
Leon Scroggins III | 71fae62 | 2019-03-26 16:28:41 -0400 | [diff] [blame] | 738 | jobject padding, jobject options, jlong inBitmapHandle, jlong colorSpaceHandle) { |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 739 | |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 740 | Asset* asset = reinterpret_cast<Asset*>(native_asset); |
Leon Scroggins III | 0aa39dc | 2014-06-03 12:19:32 -0400 | [diff] [blame] | 741 | // since we know we'll be done with the asset when we return, we can |
| 742 | // just use a simple wrapper |
Mike Reed | 3cf4282 | 2019-12-12 11:59:49 -0500 | [diff] [blame] | 743 | return doDecode(env, std::make_unique<AssetStreamAdaptor>(asset), padding, options, |
Leon Scroggins III | 71fae62 | 2019-03-26 16:28:41 -0400 | [diff] [blame] | 744 | inBitmapHandle, colorSpaceHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | static jobject nativeDecodeByteArray(JNIEnv* env, jobject, jbyteArray byteArray, |
Leon Scroggins III | 71fae62 | 2019-03-26 16:28:41 -0400 | [diff] [blame] | 748 | jint offset, jint length, jobject options, jlong inBitmapHandle, jlong colorSpaceHandle) { |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 749 | |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 750 | AutoJavaByteArray ar(env, byteArray); |
Mike Reed | 3cf4282 | 2019-12-12 11:59:49 -0500 | [diff] [blame] | 751 | return doDecode(env, std::make_unique<SkMemoryStream>(ar.ptr() + offset, length, false), |
Leon Scroggins III | 71fae62 | 2019-03-26 16:28:41 -0400 | [diff] [blame] | 752 | nullptr, options, inBitmapHandle, colorSpaceHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 753 | } |
| 754 | |
Owen Lin | a9d0d47 | 2011-01-18 17:39:15 +0800 | [diff] [blame] | 755 | static jboolean nativeIsSeekable(JNIEnv* env, jobject, jobject fileDescriptor) { |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 756 | jint descriptor = jniGetFDFromFileDescriptor(env, fileDescriptor); |
Yujie Qin | c1d7b7f | 2016-02-29 14:00:29 +0100 | [diff] [blame] | 757 | return isSeekable(descriptor) ? JNI_TRUE : JNI_FALSE; |
Owen Lin | a9d0d47 | 2011-01-18 17:39:15 +0800 | [diff] [blame] | 758 | } |
| 759 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 760 | /////////////////////////////////////////////////////////////////////////////// |
| 761 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 762 | static const JNINativeMethod gMethods[] = { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 763 | { "nativeDecodeStream", |
Leon Scroggins III | 71fae62 | 2019-03-26 16:28:41 -0400 | [diff] [blame] | 764 | "(Ljava/io/InputStream;[BLandroid/graphics/Rect;Landroid/graphics/BitmapFactory$Options;JJ)Landroid/graphics/Bitmap;", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 765 | (void*)nativeDecodeStream |
| 766 | }, |
| 767 | |
| 768 | { "nativeDecodeFileDescriptor", |
Leon Scroggins III | 71fae62 | 2019-03-26 16:28:41 -0400 | [diff] [blame] | 769 | "(Ljava/io/FileDescriptor;Landroid/graphics/Rect;Landroid/graphics/BitmapFactory$Options;JJ)Landroid/graphics/Bitmap;", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 770 | (void*)nativeDecodeFileDescriptor |
| 771 | }, |
| 772 | |
| 773 | { "nativeDecodeAsset", |
Leon Scroggins III | 71fae62 | 2019-03-26 16:28:41 -0400 | [diff] [blame] | 774 | "(JLandroid/graphics/Rect;Landroid/graphics/BitmapFactory$Options;JJ)Landroid/graphics/Bitmap;", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 775 | (void*)nativeDecodeAsset |
| 776 | }, |
| 777 | |
| 778 | { "nativeDecodeByteArray", |
Leon Scroggins III | 71fae62 | 2019-03-26 16:28:41 -0400 | [diff] [blame] | 779 | "([BIILandroid/graphics/BitmapFactory$Options;JJ)Landroid/graphics/Bitmap;", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 780 | (void*)nativeDecodeByteArray |
| 781 | }, |
| 782 | |
Owen Lin | a9d0d47 | 2011-01-18 17:39:15 +0800 | [diff] [blame] | 783 | { "nativeIsSeekable", |
| 784 | "(Ljava/io/FileDescriptor;)Z", |
| 785 | (void*)nativeIsSeekable |
| 786 | }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 787 | }; |
| 788 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 789 | int register_android_graphics_BitmapFactory(JNIEnv* env) { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 790 | jclass options_class = FindClassOrDie(env, "android/graphics/BitmapFactory$Options"); |
| 791 | gOptions_bitmapFieldID = GetFieldIDOrDie(env, options_class, "inBitmap", |
Chris Craik | 47cd8e9 | 2014-07-08 17:13:08 -0700 | [diff] [blame] | 792 | "Landroid/graphics/Bitmap;"); |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 793 | gOptions_justBoundsFieldID = GetFieldIDOrDie(env, options_class, "inJustDecodeBounds", "Z"); |
| 794 | gOptions_sampleSizeFieldID = GetFieldIDOrDie(env, options_class, "inSampleSize", "I"); |
| 795 | gOptions_configFieldID = GetFieldIDOrDie(env, options_class, "inPreferredConfig", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 796 | "Landroid/graphics/Bitmap$Config;"); |
Romain Guy | 95648b8 | 2017-04-13 18:43:42 -0700 | [diff] [blame] | 797 | gOptions_colorSpaceFieldID = GetFieldIDOrDie(env, options_class, "inPreferredColorSpace", |
| 798 | "Landroid/graphics/ColorSpace;"); |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 799 | gOptions_premultipliedFieldID = GetFieldIDOrDie(env, options_class, "inPremultiplied", "Z"); |
| 800 | gOptions_mutableFieldID = GetFieldIDOrDie(env, options_class, "inMutable", "Z"); |
| 801 | gOptions_ditherFieldID = GetFieldIDOrDie(env, options_class, "inDither", "Z"); |
| 802 | gOptions_preferQualityOverSpeedFieldID = GetFieldIDOrDie(env, options_class, |
Wei-Ta Chen | 953f909 | 2010-12-03 14:06:18 -0800 | [diff] [blame] | 803 | "inPreferQualityOverSpeed", "Z"); |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 804 | gOptions_scaledFieldID = GetFieldIDOrDie(env, options_class, "inScaled", "Z"); |
| 805 | gOptions_densityFieldID = GetFieldIDOrDie(env, options_class, "inDensity", "I"); |
| 806 | gOptions_screenDensityFieldID = GetFieldIDOrDie(env, options_class, "inScreenDensity", "I"); |
| 807 | gOptions_targetDensityFieldID = GetFieldIDOrDie(env, options_class, "inTargetDensity", "I"); |
| 808 | gOptions_widthFieldID = GetFieldIDOrDie(env, options_class, "outWidth", "I"); |
| 809 | gOptions_heightFieldID = GetFieldIDOrDie(env, options_class, "outHeight", "I"); |
| 810 | gOptions_mimeFieldID = GetFieldIDOrDie(env, options_class, "outMimeType", "Ljava/lang/String;"); |
Romain Guy | e8d2ebb | 2017-02-09 18:38:47 -0800 | [diff] [blame] | 811 | gOptions_outConfigFieldID = GetFieldIDOrDie(env, options_class, "outConfig", |
| 812 | "Landroid/graphics/Bitmap$Config;"); |
Romain Guy | 90fc43b | 2017-03-30 12:35:26 -0700 | [diff] [blame] | 813 | gOptions_outColorSpaceFieldID = GetFieldIDOrDie(env, options_class, "outColorSpace", |
| 814 | "Landroid/graphics/ColorSpace;"); |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 815 | gOptions_mCancelID = GetFieldIDOrDie(env, options_class, "mCancel", "Z"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 816 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 817 | jclass bitmap_class = FindClassOrDie(env, "android/graphics/Bitmap"); |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 818 | gBitmap_ninePatchInsetsFieldID = GetFieldIDOrDie(env, bitmap_class, "mNinePatchInsets", |
Chris Craik | 47cd8e9 | 2014-07-08 17:13:08 -0700 | [diff] [blame] | 819 | "Landroid/graphics/NinePatch$InsetStruct;"); |
| 820 | |
Romain Guy | e8d2ebb | 2017-02-09 18:38:47 -0800 | [diff] [blame] | 821 | gBitmapConfig_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, |
| 822 | "android/graphics/Bitmap$Config")); |
| 823 | gBitmapConfig_nativeToConfigMethodID = GetStaticMethodIDOrDie(env, gBitmapConfig_class, |
| 824 | "nativeToConfig", "(I)Landroid/graphics/Bitmap$Config;"); |
| 825 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 826 | return android::RegisterMethodsOrDie(env, "android/graphics/BitmapFactory", |
| 827 | gMethods, NELEM(gMethods)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 828 | } |