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