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