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