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" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10 | #include "SkImageDecoder.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 | |
| 18 | #include <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; |
Chris Craik | 1abf5d6 | 2013-08-16 12:47:03 -0700 | [diff] [blame] | 31 | jfieldID gOptions_premultipliedFieldID; |
Romain Guy | 2361098 | 2011-01-17 12:51:55 -0800 | [diff] [blame] | 32 | jfieldID gOptions_mutableFieldID; |
Joseph Wen | f1f48bc | 2010-07-19 16:59:51 +0800 | [diff] [blame] | 33 | jfieldID gOptions_ditherFieldID; |
Wei-Ta Chen | 953f909 | 2010-12-03 14:06:18 -0800 | [diff] [blame] | 34 | jfieldID gOptions_preferQualityOverSpeedFieldID; |
Chris Craik | 905e824 | 2013-06-05 09:59:05 -0700 | [diff] [blame] | 35 | jfieldID gOptions_scaledFieldID; |
| 36 | jfieldID gOptions_densityFieldID; |
| 37 | jfieldID gOptions_screenDensityFieldID; |
| 38 | jfieldID gOptions_targetDensityFieldID; |
Joseph Wen | f1f48bc | 2010-07-19 16:59:51 +0800 | [diff] [blame] | 39 | jfieldID gOptions_widthFieldID; |
| 40 | jfieldID gOptions_heightFieldID; |
| 41 | jfieldID gOptions_mimeFieldID; |
| 42 | jfieldID gOptions_mCancelID; |
Chet Haase | 37f74ca | 2010-12-08 17:56:36 -0800 | [diff] [blame] | 43 | jfieldID gOptions_bitmapFieldID; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | |
Chris Craik | 47cd8e9 | 2014-07-08 17:13:08 -0700 | [diff] [blame] | 45 | jfieldID gBitmap_ninePatchInsetsFieldID; |
| 46 | |
| 47 | jclass gInsetStruct_class; |
| 48 | jmethodID gInsetStruct_constructorMethodID; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | using namespace android; |
| 51 | |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 52 | jstring encodedFormatToString(JNIEnv* env, SkEncodedFormat format) { |
| 53 | const char* mimeType; |
| 54 | switch (format) { |
| 55 | case SkEncodedFormat::kBMP_SkEncodedFormat: |
| 56 | mimeType = "image/bmp"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | break; |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 58 | case SkEncodedFormat::kGIF_SkEncodedFormat: |
| 59 | mimeType = "image/gif"; |
| 60 | break; |
| 61 | case SkEncodedFormat::kICO_SkEncodedFormat: |
| 62 | mimeType = "image/x-ico"; |
| 63 | break; |
| 64 | case SkEncodedFormat::kJPEG_SkEncodedFormat: |
| 65 | mimeType = "image/jpeg"; |
| 66 | break; |
| 67 | case SkEncodedFormat::kPNG_SkEncodedFormat: |
| 68 | mimeType = "image/png"; |
| 69 | break; |
| 70 | case SkEncodedFormat::kWEBP_SkEncodedFormat: |
| 71 | mimeType = "image/webp"; |
| 72 | break; |
| 73 | case SkEncodedFormat::kWBMP_SkEncodedFormat: |
| 74 | mimeType = "image/vnd.wap.wbmp"; |
| 75 | break; |
| 76 | default: |
| 77 | mimeType = nullptr; |
| 78 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Vladimir Marko | 7ab249a | 2015-01-06 18:17:52 +0000 | [diff] [blame] | 81 | jstring jstr = nullptr; |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 82 | if (mimeType) { |
Vladimir Marko | 7ab249a | 2015-01-06 18:17:52 +0000 | [diff] [blame] | 83 | // NOTE: Caller should env->ExceptionCheck() for OOM |
| 84 | // (can't check for nullptr as it's a valid return value) |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 85 | jstr = env->NewStringUTF(mimeType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | } |
| 87 | return jstr; |
| 88 | } |
| 89 | |
Chris Craik | bd8db2e8 | 2014-08-20 16:31:57 -0700 | [diff] [blame] | 90 | static void scaleDivRange(int32_t* divs, int count, float scale, int maxValue) { |
| 91 | for (int i = 0; i < count; i++) { |
| 92 | divs[i] = int32_t(divs[i] * scale + 0.5f); |
| 93 | if (i > 0 && divs[i] == divs[i - 1]) { |
| 94 | divs[i]++; // avoid collisions |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | if (CC_UNLIKELY(divs[count - 1] > maxValue)) { |
| 99 | // if the collision avoidance above put some divs outside the bounds of the bitmap, |
| 100 | // slide outer stretchable divs inward to stay within bounds |
| 101 | int highestAvailable = maxValue; |
| 102 | for (int i = count - 1; i >= 0; i--) { |
| 103 | divs[i] = highestAvailable; |
| 104 | if (i > 0 && divs[i] <= divs[i-1]){ |
| 105 | // keep shifting |
| 106 | highestAvailable = divs[i] - 1; |
| 107 | } else { |
| 108 | break; |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | static void scaleNinePatchChunk(android::Res_png_9patch* chunk, float scale, |
| 115 | int scaledWidth, int scaledHeight) { |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 116 | chunk->paddingLeft = int(chunk->paddingLeft * scale + 0.5f); |
| 117 | chunk->paddingTop = int(chunk->paddingTop * scale + 0.5f); |
| 118 | chunk->paddingRight = int(chunk->paddingRight * scale + 0.5f); |
| 119 | chunk->paddingBottom = int(chunk->paddingBottom * scale + 0.5f); |
| 120 | |
Chris Craik | bd8db2e8 | 2014-08-20 16:31:57 -0700 | [diff] [blame] | 121 | scaleDivRange(chunk->getXDivs(), chunk->numXDivs, scale, scaledWidth); |
| 122 | scaleDivRange(chunk->getYDivs(), chunk->numYDivs, scale, scaledHeight); |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Leon Scroggins | 46cb9bd | 2014-03-06 15:36:39 -0500 | [diff] [blame] | 125 | static SkColorType colorTypeForScaledOutput(SkColorType colorType) { |
| 126 | switch (colorType) { |
| 127 | case kUnknown_SkColorType: |
| 128 | case kIndex_8_SkColorType: |
Mike Reed | 4a9c389 | 2014-07-07 15:44:40 -0400 | [diff] [blame] | 129 | return kN32_SkColorType; |
Chris Craik | 905e824 | 2013-06-05 09:59:05 -0700 | [diff] [blame] | 130 | default: |
| 131 | break; |
| 132 | } |
Leon Scroggins | 46cb9bd | 2014-03-06 15:36:39 -0500 | [diff] [blame] | 133 | return colorType; |
Chris Craik | 905e824 | 2013-06-05 09:59:05 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | class ScaleCheckingAllocator : public SkBitmap::HeapAllocator { |
| 137 | public: |
| 138 | ScaleCheckingAllocator(float scale, int size) |
| 139 | : mScale(scale), mSize(size) { |
| 140 | } |
| 141 | |
| 142 | virtual bool allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable) { |
| 143 | // accounts for scale in final allocation, using eventual size and config |
Leon Scroggins | 46cb9bd | 2014-03-06 15:36:39 -0500 | [diff] [blame] | 144 | const int bytesPerPixel = SkColorTypeBytesPerPixel( |
| 145 | colorTypeForScaledOutput(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 | } |
| 154 | return SkBitmap::HeapAllocator::allocPixelRef(bitmap, ctable); |
| 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: |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 163 | RecyclingPixelAllocator(android::Bitmap* bitmap, unsigned int size) |
| 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 | |
| 170 | virtual bool allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable) { |
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 | |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 190 | mBitmap->reconfigure(info, bitmap->rowBytes(), ctable); |
John Reck | ae2e8b4 | 2015-05-06 14:55:05 -0700 | [diff] [blame] | 191 | bitmap->setPixelRef(mBitmap->refPixelRef())->unref(); |
Chris Craik | cd0ba71 | 2013-09-06 14:40:30 -0700 | [diff] [blame] | 192 | |
Chris Craik | cd0ba71 | 2013-09-06 14:40:30 -0700 | [diff] [blame] | 193 | // since we're already allocated, we lockPixels right away |
| 194 | // HeapAllocator/JavaPixelAllocator behaves this way too |
Chris Craik | 7e8c03c | 2013-06-03 13:53:36 -0700 | [diff] [blame] | 195 | bitmap->lockPixels(); |
| 196 | return true; |
| 197 | } |
| 198 | |
| 199 | private: |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 200 | android::Bitmap* const mBitmap; |
Chris Craik | 7e8c03c | 2013-06-03 13:53:36 -0700 | [diff] [blame] | 201 | const unsigned int mSize; |
| 202 | }; |
| 203 | |
Chris Craik | 47cd8e9 | 2014-07-08 17:13:08 -0700 | [diff] [blame] | 204 | static jobject doDecode(JNIEnv* env, SkStreamRewindable* stream, jobject padding, jobject options) { |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 205 | // This function takes ownership of the input stream. Since the SkAndroidCodec |
| 206 | // will take ownership of the stream, we don't necessarily need to take ownership |
| 207 | // here. This is a precaution - if we were to return before creating the codec, |
| 208 | // we need to make sure that we delete the stream. |
| 209 | std::unique_ptr<SkStreamRewindable> streamDeleter(stream); |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 210 | |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 211 | // Set default values for the options parameters. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | int sampleSize = 1; |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 213 | bool onlyDecodeSize = false; |
Mike Reed | 42a1d08 | 2014-07-07 18:06:18 -0400 | [diff] [blame] | 214 | SkColorType prefColorType = kN32_SkColorType; |
Romain Guy | 2361098 | 2011-01-17 12:51:55 -0800 | [diff] [blame] | 215 | bool isMutable = false; |
Chris Craik | 905e824 | 2013-06-05 09:59:05 -0700 | [diff] [blame] | 216 | float scale = 1.0f; |
Chris Craik | 1abf5d6 | 2013-08-16 12:47:03 -0700 | [diff] [blame] | 217 | bool requireUnpremultiplied = false; |
Chet Haase | 37f74ca | 2010-12-08 17:56:36 -0800 | [diff] [blame] | 218 | jobject javaBitmap = NULL; |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 219 | |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 220 | // Update with options supplied by the client. |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 221 | if (options != NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 222 | sampleSize = env->GetIntField(options, gOptions_sampleSizeFieldID); |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 223 | // Correct a non-positive sampleSize. sampleSize defaults to zero within the |
| 224 | // options object, which is strange. |
| 225 | if (sampleSize <= 0) { |
| 226 | sampleSize = 1; |
| 227 | } |
| 228 | |
| 229 | if (env->GetBooleanField(options, gOptions_justBoundsFieldID)) { |
| 230 | onlyDecodeSize = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | } |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 232 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 233 | // initialize these, in case we fail later on |
| 234 | env->SetIntField(options, gOptions_widthFieldID, -1); |
| 235 | env->SetIntField(options, gOptions_heightFieldID, -1); |
| 236 | env->SetObjectField(options, gOptions_mimeFieldID, 0); |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 237 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 238 | jobject jconfig = env->GetObjectField(options, gOptions_configFieldID); |
Mike Reed | 42a1d08 | 2014-07-07 18:06:18 -0400 | [diff] [blame] | 239 | prefColorType = GraphicsJNI::getNativeBitmapColorType(env, jconfig); |
Romain Guy | 2361098 | 2011-01-17 12:51:55 -0800 | [diff] [blame] | 240 | isMutable = env->GetBooleanField(options, gOptions_mutableFieldID); |
Chris Craik | 1abf5d6 | 2013-08-16 12:47:03 -0700 | [diff] [blame] | 241 | requireUnpremultiplied = !env->GetBooleanField(options, gOptions_premultipliedFieldID); |
Chet Haase | 37f74ca | 2010-12-08 17:56:36 -0800 | [diff] [blame] | 242 | javaBitmap = env->GetObjectField(options, gOptions_bitmapFieldID); |
Chris Craik | 905e824 | 2013-06-05 09:59:05 -0700 | [diff] [blame] | 243 | |
| 244 | if (env->GetBooleanField(options, gOptions_scaledFieldID)) { |
| 245 | const int density = env->GetIntField(options, gOptions_densityFieldID); |
| 246 | const int targetDensity = env->GetIntField(options, gOptions_targetDensityFieldID); |
| 247 | const int screenDensity = env->GetIntField(options, gOptions_screenDensityFieldID); |
| 248 | if (density != 0 && targetDensity != 0 && density != screenDensity) { |
| 249 | scale = (float) targetDensity / density; |
| 250 | } |
| 251 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | } |
Chris Craik | 905e824 | 2013-06-05 09:59:05 -0700 | [diff] [blame] | 253 | const bool willScale = scale != 1.0f; |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 254 | |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 255 | // Create the codec. |
| 256 | NinePatchPeeker peeker; |
| 257 | std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(streamDeleter.release(), |
| 258 | &peeker)); |
| 259 | if (!codec.get()) { |
| 260 | return nullObjectReturn("SkAndroidCodec::NewFromStream returned null"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 261 | } |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 262 | |
Matt Sarett | 3b1b68d | 2015-12-14 13:08:33 -0500 | [diff] [blame] | 263 | // Do not allow ninepatch decodes to 565. In the past, decodes to 565 |
| 264 | // would dither, and we do not want to pre-dither ninepatches, since we |
| 265 | // know that they will be stretched. We no longer dither 565 decodes, |
| 266 | // but we continue to prevent ninepatches from decoding to 565, in order |
| 267 | // to maintain the old behavior. |
| 268 | if (peeker.mPatch && kRGB_565_SkColorType == prefColorType) { |
| 269 | prefColorType = kN32_SkColorType; |
| 270 | } |
| 271 | |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 272 | // Determine the output size and return if the client only wants the size. |
| 273 | SkISize size = codec->getSampledDimensions(sampleSize); |
| 274 | if (options != NULL) { |
| 275 | jstring mimeType = encodedFormatToString(env, codec->getEncodedFormat()); |
| 276 | if (env->ExceptionCheck()) { |
Matt Sarett | d31512b | 2015-12-09 15:16:31 -0500 | [diff] [blame] | 277 | return nullObjectReturn("OOM in encodedFormatToString()"); |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 278 | } |
| 279 | env->SetIntField(options, gOptions_widthFieldID, size.width()); |
| 280 | env->SetIntField(options, gOptions_heightFieldID, size.height()); |
| 281 | env->SetObjectField(options, gOptions_mimeFieldID, mimeType); |
| 282 | |
| 283 | if (onlyDecodeSize) { |
| 284 | return nullptr; |
| 285 | } |
| 286 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 287 | |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 288 | android::Bitmap* reuseBitmap = nullptr; |
Chris Craik | 9f58361 | 2013-05-20 18:13:47 -0700 | [diff] [blame] | 289 | unsigned int existingBufferSize = 0; |
Chris Craik | 7e8c03c | 2013-06-03 13:53:36 -0700 | [diff] [blame] | 290 | if (javaBitmap != NULL) { |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 291 | reuseBitmap = GraphicsJNI::getBitmap(env, javaBitmap); |
John Reck | ae2e8b4 | 2015-05-06 14:55:05 -0700 | [diff] [blame] | 292 | if (reuseBitmap->peekAtPixelRef()->isImmutable()) { |
Derek Sollenberger | 2a6ecae | 2012-08-31 14:03:51 -0400 | [diff] [blame] | 293 | ALOGW("Unable to reuse an immutable bitmap as an image decoder target."); |
Chris Craik | 7e8c03c | 2013-06-03 13:53:36 -0700 | [diff] [blame] | 294 | javaBitmap = NULL; |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 295 | reuseBitmap = nullptr; |
Chris Craik | 7e8c03c | 2013-06-03 13:53:36 -0700 | [diff] [blame] | 296 | } else { |
| 297 | existingBufferSize = GraphicsJNI::getBitmapAllocationByteCount(env, javaBitmap); |
Derek Sollenberger | 2a6ecae | 2012-08-31 14:03:51 -0400 | [diff] [blame] | 298 | } |
Chet Haase | 37f74ca | 2010-12-08 17:56:36 -0800 | [diff] [blame] | 299 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 300 | |
Chris Craik | 905e824 | 2013-06-05 09:59:05 -0700 | [diff] [blame] | 301 | JavaPixelAllocator javaAllocator(env); |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 302 | RecyclingPixelAllocator recyclingAllocator(reuseBitmap, existingBufferSize); |
Chris Craik | 905e824 | 2013-06-05 09:59:05 -0700 | [diff] [blame] | 303 | ScaleCheckingAllocator scaleCheckingAllocator(scale, existingBufferSize); |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 304 | SkBitmap::HeapAllocator heapAllocator; |
| 305 | SkBitmap::Allocator* decodeAllocator; |
| 306 | if (javaBitmap != nullptr && willScale) { |
| 307 | // This will allocate pixels using a HeapAllocator, since there will be an extra |
| 308 | // scaling step that copies these pixels into Java memory. This allocator |
| 309 | // also checks that the recycled javaBitmap is large enough. |
| 310 | decodeAllocator = &scaleCheckingAllocator; |
| 311 | } else if (javaBitmap != nullptr) { |
| 312 | decodeAllocator = &recyclingAllocator; |
| 313 | } else if (willScale) { |
| 314 | // This will allocate pixels using a HeapAllocator, since there will be an extra |
| 315 | // scaling step that copies these pixels into Java memory. |
| 316 | decodeAllocator = &heapAllocator; |
| 317 | } else { |
| 318 | decodeAllocator = &javaAllocator; |
Chris Craik | 905e824 | 2013-06-05 09:59:05 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Matt Sarett | 9e7cd63 | 2015-12-11 10:54:28 -0500 | [diff] [blame] | 321 | // Set the decode colorType. This is necessary because we can't always support |
| 322 | // the requested colorType. |
| 323 | SkColorType decodeColorType = codec->computeOutputColorType(prefColorType); |
Derek Sollenberger | 6b0437c | 2013-06-24 15:40:54 -0400 | [diff] [blame] | 324 | |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 325 | // Construct a color table for the decode if necessary |
| 326 | SkAutoTUnref<SkColorTable> colorTable(nullptr); |
| 327 | SkPMColor* colorPtr = nullptr; |
| 328 | int* colorCount = nullptr; |
| 329 | int maxColors = 256; |
| 330 | SkPMColor colors[256]; |
| 331 | if (kIndex_8_SkColorType == decodeColorType) { |
| 332 | colorTable.reset(new SkColorTable(colors, maxColors)); |
Derek Sollenberger | 6b0437c | 2013-06-24 15:40:54 -0400 | [diff] [blame] | 333 | |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 334 | // SkColorTable expects us to initialize all of the colors before creating an |
| 335 | // SkColorTable. However, we are using SkBitmap with an Allocator to allocate |
| 336 | // memory for the decode, so we need to create the SkColorTable before decoding. |
| 337 | // It is safe for SkAndroidCodec to modify the colors because this SkBitmap is |
| 338 | // not being used elsewhere. |
| 339 | colorPtr = const_cast<SkPMColor*>(colorTable->readColors()); |
| 340 | colorCount = &maxColors; |
Derek Sollenberger | 6b0437c | 2013-06-24 15:40:54 -0400 | [diff] [blame] | 341 | } |
| 342 | |
Matt Sarett | 9e7cd63 | 2015-12-11 10:54:28 -0500 | [diff] [blame] | 343 | // Set the alpha type for the decode. |
| 344 | SkAlphaType alphaType = codec->computeOutputAlphaType(requireUnpremultiplied); |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 345 | |
| 346 | const SkImageInfo decodeInfo = SkImageInfo::Make(size.width(), size.height(), decodeColorType, |
| 347 | alphaType); |
| 348 | SkImageInfo bitmapInfo = decodeInfo; |
| 349 | if (decodeColorType == kGray_8_SkColorType) { |
| 350 | // The legacy implementation of BitmapFactory used kAlpha8 for |
| 351 | // grayscale images (before kGray8 existed). While the codec |
| 352 | // recognizes kGray8, we need to decode into a kAlpha8 bitmap |
| 353 | // in order to avoid a behavior change. |
| 354 | bitmapInfo = SkImageInfo::MakeA8(size.width(), size.height()); |
| 355 | } |
Chris Craik | 7e8c03c | 2013-06-03 13:53:36 -0700 | [diff] [blame] | 356 | SkBitmap decodingBitmap; |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 357 | if (!decodingBitmap.setInfo(bitmapInfo) || |
| 358 | !decodingBitmap.tryAllocPixels(decodeAllocator, colorTable)) { |
| 359 | // SkAndroidCodec should recommend a valid SkImageInfo, so setInfo() |
| 360 | // should only only fail if the calculated value for rowBytes is too |
| 361 | // large. |
| 362 | // tryAllocPixels() can fail due to OOM on the Java heap, OOM on the |
| 363 | // native heap, or the recycled javaBitmap being too small to reuse. |
| 364 | return nullptr; |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 365 | } |
| 366 | |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 367 | // Use SkAndroidCodec to perform the decode. |
| 368 | SkAndroidCodec::AndroidOptions codecOptions; |
| 369 | codecOptions.fZeroInitialized = (decodeAllocator == &javaAllocator) ? |
| 370 | SkCodec::kYes_ZeroInitialized : SkCodec::kNo_ZeroInitialized; |
| 371 | codecOptions.fColorPtr = colorPtr; |
| 372 | codecOptions.fColorCount = colorCount; |
| 373 | codecOptions.fSampleSize = sampleSize; |
| 374 | SkCodec::Result result = codec->getAndroidPixels(decodeInfo, decodingBitmap.getPixels(), |
| 375 | decodingBitmap.rowBytes(), &codecOptions); |
| 376 | switch (result) { |
| 377 | case SkCodec::kSuccess: |
| 378 | case SkCodec::kIncompleteInput: |
| 379 | break; |
| 380 | default: |
Matt Sarett | 3b1b68d | 2015-12-14 13:08:33 -0500 | [diff] [blame] | 381 | return nullObjectReturn("codec->getAndroidPixels() failed."); |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | int scaledWidth = size.width(); |
| 385 | int scaledHeight = size.height(); |
| 386 | if (willScale) { |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 387 | scaledWidth = int(scaledWidth * scale + 0.5f); |
| 388 | scaledHeight = int(scaledHeight * scale + 0.5f); |
| 389 | } |
| 390 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 391 | jbyteArray ninePatchChunk = NULL; |
Chris Craik | 47cd8e9 | 2014-07-08 17:13:08 -0700 | [diff] [blame] | 392 | if (peeker.mPatch != NULL) { |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 393 | if (willScale) { |
Chris Craik | bd8db2e8 | 2014-08-20 16:31:57 -0700 | [diff] [blame] | 394 | scaleNinePatchChunk(peeker.mPatch, scale, scaledWidth, scaledHeight); |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Chris Craik | 47cd8e9 | 2014-07-08 17:13:08 -0700 | [diff] [blame] | 397 | size_t ninePatchArraySize = peeker.mPatch->serializedSize(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 398 | ninePatchChunk = env->NewByteArray(ninePatchArraySize); |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 399 | if (ninePatchChunk == NULL) { |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 400 | return nullObjectReturn("ninePatchChunk == null"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 401 | } |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 402 | |
| 403 | jbyte* array = (jbyte*) env->GetPrimitiveArrayCritical(ninePatchChunk, NULL); |
| 404 | if (array == NULL) { |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 405 | return nullObjectReturn("primitive array == null"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 406 | } |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 407 | |
Chris Craik | 47cd8e9 | 2014-07-08 17:13:08 -0700 | [diff] [blame] | 408 | memcpy(array, peeker.mPatch, peeker.mPatchSize); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 409 | env->ReleasePrimitiveArrayCritical(ninePatchChunk, array, 0); |
| 410 | } |
| 411 | |
Chris Craik | 47cd8e9 | 2014-07-08 17:13:08 -0700 | [diff] [blame] | 412 | jobject ninePatchInsets = NULL; |
| 413 | if (peeker.mHasInsets) { |
| 414 | ninePatchInsets = env->NewObject(gInsetStruct_class, gInsetStruct_constructorMethodID, |
| 415 | peeker.mOpticalInsets[0], peeker.mOpticalInsets[1], peeker.mOpticalInsets[2], peeker.mOpticalInsets[3], |
| 416 | peeker.mOutlineInsets[0], peeker.mOutlineInsets[1], peeker.mOutlineInsets[2], peeker.mOutlineInsets[3], |
Chris Craik | 77b5cad | 2014-07-30 18:23:07 -0700 | [diff] [blame] | 417 | peeker.mOutlineRadius, peeker.mOutlineAlpha, scale); |
Mathieu Chartier | a08d10f | 2014-08-29 16:55:55 -0700 | [diff] [blame] | 418 | if (ninePatchInsets == NULL) { |
| 419 | return nullObjectReturn("nine patch insets == null"); |
| 420 | } |
Amith Yamasani | ec4a504 | 2012-04-04 10:27:15 -0700 | [diff] [blame] | 421 | if (javaBitmap != NULL) { |
Chris Craik | 47cd8e9 | 2014-07-08 17:13:08 -0700 | [diff] [blame] | 422 | env->SetObjectField(javaBitmap, gBitmap_ninePatchInsetsFieldID, ninePatchInsets); |
Amith Yamasani | ec4a504 | 2012-04-04 10:27:15 -0700 | [diff] [blame] | 423 | } |
| 424 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 425 | |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 426 | SkBitmap outputBitmap; |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 427 | if (willScale) { |
| 428 | // This is weird so let me explain: we could use the scale parameter |
| 429 | // directly, but for historical reasons this is how the corresponding |
| 430 | // Dalvik code has always behaved. We simply recreate the behavior here. |
| 431 | // The result is slightly different from simply using scale because of |
| 432 | // the 0.5f rounding bias applied when computing the target image size |
Chris Craik | 7e8c03c | 2013-06-03 13:53:36 -0700 | [diff] [blame] | 433 | const float sx = scaledWidth / float(decodingBitmap.width()); |
| 434 | const float sy = scaledHeight / float(decodingBitmap.height()); |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 435 | |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 436 | // Set the allocator for the outputBitmap. |
| 437 | SkBitmap::Allocator* outputAllocator; |
| 438 | if (javaBitmap != nullptr) { |
| 439 | outputAllocator = &recyclingAllocator; |
| 440 | } else { |
| 441 | outputAllocator = &javaAllocator; |
| 442 | } |
| 443 | |
| 444 | SkColorType scaledColorType = colorTypeForScaledOutput(decodingBitmap.colorType()); |
Leon Scroggins III | 8790be6 | 2013-12-03 16:26:51 -0500 | [diff] [blame] | 445 | // FIXME: If the alphaType is kUnpremul and the image has alpha, the |
| 446 | // colors may not be correct, since Skia does not yet support drawing |
| 447 | // to/from unpremultiplied bitmaps. |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 448 | outputBitmap.setInfo(SkImageInfo::Make(scaledWidth, scaledHeight, |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 449 | scaledColorType, decodingBitmap.alphaType())); |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 450 | if (!outputBitmap.tryAllocPixels(outputAllocator, NULL)) { |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 451 | // This should only fail on OOM. The recyclingAllocator should have |
| 452 | // enough memory since we check this before decoding using the |
| 453 | // scaleCheckingAllocator. |
Raph Levien | 005bfc6 | 2012-09-20 22:51:47 -0700 | [diff] [blame] | 454 | return nullObjectReturn("allocation failed for scaled bitmap"); |
| 455 | } |
Leon Scroggins III | 1ffe727 | 2013-09-19 11:34:06 -0400 | [diff] [blame] | 456 | |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 457 | SkPaint paint; |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 458 | // kSrc_Mode instructs us to overwrite the unininitialized pixels in |
| 459 | // outputBitmap. Otherwise we would blend by default, which is not |
| 460 | // what we want. |
| 461 | paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
Mike Reed | 2a1ce8a | 2015-03-16 11:16:09 -0400 | [diff] [blame] | 462 | paint.setFilterQuality(kLow_SkFilterQuality); |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 463 | |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 464 | SkCanvas canvas(outputBitmap); |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 465 | canvas.scale(sx, sy); |
Chris Craik | 7e8c03c | 2013-06-03 13:53:36 -0700 | [diff] [blame] | 466 | canvas.drawBitmap(decodingBitmap, 0.0f, 0.0f, &paint); |
| 467 | } else { |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 468 | outputBitmap.swap(decodingBitmap); |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 469 | } |
| 470 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 471 | if (padding) { |
Chris Craik | 47cd8e9 | 2014-07-08 17:13:08 -0700 | [diff] [blame] | 472 | if (peeker.mPatch != NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 473 | GraphicsJNI::set_jrect(env, padding, |
Chris Craik | 47cd8e9 | 2014-07-08 17:13:08 -0700 | [diff] [blame] | 474 | peeker.mPatch->paddingLeft, peeker.mPatch->paddingTop, |
| 475 | peeker.mPatch->paddingRight, peeker.mPatch->paddingBottom); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 476 | } else { |
| 477 | GraphicsJNI::set_jrect(env, padding, -1, -1, -1, -1); |
| 478 | } |
| 479 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 480 | |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 481 | // If we get here, the outputBitmap should have an installed pixelref. |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 482 | if (outputBitmap.pixelRef() == NULL) { |
Marco Nelissen | b2fe3be | 2012-05-07 11:24:13 -0700 | [diff] [blame] | 483 | return nullObjectReturn("Got null SkPixelRef"); |
| 484 | } |
Romain Guy | 2361098 | 2011-01-17 12:51:55 -0800 | [diff] [blame] | 485 | |
Chris Craik | 7e8c03c | 2013-06-03 13:53:36 -0700 | [diff] [blame] | 486 | if (!isMutable && javaBitmap == NULL) { |
Romain Guy | 2361098 | 2011-01-17 12:51:55 -0800 | [diff] [blame] | 487 | // promise we will never change our pixels (great for sharing and pictures) |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 488 | outputBitmap.setImmutable(); |
Romain Guy | 2361098 | 2011-01-17 12:51:55 -0800 | [diff] [blame] | 489 | } |
Chet Haase | 37f74ca | 2010-12-08 17:56:36 -0800 | [diff] [blame] | 490 | |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 491 | bool isPremultiplied = !requireUnpremultiplied; |
| 492 | if (javaBitmap != nullptr) { |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 493 | GraphicsJNI::reinitBitmap(env, javaBitmap, outputBitmap.info(), isPremultiplied); |
| 494 | outputBitmap.notifyPixelsChanged(); |
Chet Haase | 37f74ca | 2010-12-08 17:56:36 -0800 | [diff] [blame] | 495 | // If a java bitmap was passed in for reuse, pass it back |
| 496 | return javaBitmap; |
| 497 | } |
Chris Craik | 1abf5d6 | 2013-08-16 12:47:03 -0700 | [diff] [blame] | 498 | |
| 499 | int bitmapCreateFlags = 0x0; |
| 500 | if (isMutable) bitmapCreateFlags |= GraphicsJNI::kBitmapCreateFlag_Mutable; |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 501 | if (isPremultiplied) bitmapCreateFlags |= GraphicsJNI::kBitmapCreateFlag_Premultiplied; |
Chris Craik | 1abf5d6 | 2013-08-16 12:47:03 -0700 | [diff] [blame] | 502 | |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 503 | // now create the java bitmap |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 504 | return GraphicsJNI::createBitmap(env, javaAllocator.getStorageObjAndReset(), |
Chris Craik | 47cd8e9 | 2014-07-08 17:13:08 -0700 | [diff] [blame] | 505 | bitmapCreateFlags, ninePatchChunk, ninePatchInsets, -1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 506 | } |
| 507 | |
Chris Craik | 905e824 | 2013-06-05 09:59:05 -0700 | [diff] [blame] | 508 | static jobject nativeDecodeStream(JNIEnv* env, jobject clazz, jobject is, jbyteArray storage, |
| 509 | jobject padding, jobject options) { |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 510 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 511 | jobject bitmap = NULL; |
Ben Wagner | 60126ef | 2015-08-07 12:13:48 -0400 | [diff] [blame] | 512 | std::unique_ptr<SkStream> stream(CreateJavaInputStreamAdaptor(env, is, storage)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 513 | |
Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 514 | if (stream.get()) { |
Ben Wagner | 60126ef | 2015-08-07 12:13:48 -0400 | [diff] [blame] | 515 | std::unique_ptr<SkStreamRewindable> bufferedStream( |
Matt Sarett | 96ffbdc | 2016-02-11 17:03:54 -0500 | [diff] [blame] | 516 | SkFrontBufferedStream::Create(stream.release(), SkCodec::MinBufferedBytesNeeded())); |
Leon Scroggins III | 7315f1b | 2013-09-10 20:26:05 -0400 | [diff] [blame] | 517 | SkASSERT(bufferedStream.get() != NULL); |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 518 | bitmap = doDecode(env, bufferedStream.release(), padding, options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 519 | } |
| 520 | return bitmap; |
| 521 | } |
| 522 | |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 523 | static jobject nativeDecodeFileDescriptor(JNIEnv* env, jobject clazz, jobject fileDescriptor, |
| 524 | jobject padding, jobject bitmapFactoryOptions) { |
| 525 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 526 | NPE_CHECK_RETURN_ZERO(env, fileDescriptor); |
| 527 | |
Derek Sollenberger | 5cb769d | 2014-09-24 09:20:09 -0400 | [diff] [blame] | 528 | int descriptor = jniGetFDFromFileDescriptor(env, fileDescriptor); |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 529 | |
Derek Sollenberger | 5827cb5 | 2013-07-26 14:58:06 -0400 | [diff] [blame] | 530 | struct stat fdStat; |
| 531 | if (fstat(descriptor, &fdStat) == -1) { |
| 532 | doThrowIOE(env, "broken file descriptor"); |
| 533 | return nullObjectReturn("fstat return -1"); |
| 534 | } |
| 535 | |
Derek Sollenberger | 5cb769d | 2014-09-24 09:20:09 -0400 | [diff] [blame] | 536 | // Restore the descriptor's offset on exiting this function. Even though |
| 537 | // we dup the descriptor, both the original and dup refer to the same open |
| 538 | // 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] | 539 | AutoFDSeek autoRestore(descriptor); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 540 | |
Derek Sollenberger | 5cb769d | 2014-09-24 09:20:09 -0400 | [diff] [blame] | 541 | // Duplicate the descriptor here to prevent leaking memory. A leak occurs |
| 542 | // if we only close the file descriptor and not the file object it is used to |
| 543 | // create. If we don't explicitly clean up the file (which in turn closes the |
| 544 | // descriptor) the buffers allocated internally by fseek will be leaked. |
| 545 | int dupDescriptor = dup(descriptor); |
| 546 | |
| 547 | FILE* file = fdopen(dupDescriptor, "r"); |
Leon Scroggins III | 0102f8a | 2014-01-14 15:14:57 -0500 | [diff] [blame] | 548 | if (file == NULL) { |
Derek Sollenberger | 5cb769d | 2014-09-24 09:20:09 -0400 | [diff] [blame] | 549 | // cleanup the duplicated descriptor since it will not be closed when the |
| 550 | // file is cleaned up (fclose). |
| 551 | close(dupDescriptor); |
Leon Scroggins III | 0102f8a | 2014-01-14 15:14:57 -0500 | [diff] [blame] | 552 | return nullObjectReturn("Could not open file"); |
Leon Scroggins III | f65183f | 2013-10-07 16:32:14 -0400 | [diff] [blame] | 553 | } |
Leon Scroggins III | 0102f8a | 2014-01-14 15:14:57 -0500 | [diff] [blame] | 554 | |
Ben Wagner | 60126ef | 2015-08-07 12:13:48 -0400 | [diff] [blame] | 555 | std::unique_ptr<SkFILEStream> fileStream(new SkFILEStream(file, |
Leon Scroggins III | 3449789 | 2015-01-20 15:52:43 -0500 | [diff] [blame] | 556 | SkFILEStream::kCallerPasses_Ownership)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 557 | |
Leon Scroggins III | 0aa39dc | 2014-06-03 12:19:32 -0400 | [diff] [blame] | 558 | // Use a buffered stream. Although an SkFILEStream can be rewound, this |
| 559 | // ensures that SkImageDecoder::Factory never rewinds beyond the |
| 560 | // current position of the file descriptor. |
Ben Wagner | 60126ef | 2015-08-07 12:13:48 -0400 | [diff] [blame] | 561 | std::unique_ptr<SkStreamRewindable> stream(SkFrontBufferedStream::Create(fileStream.release(), |
Matt Sarett | 96ffbdc | 2016-02-11 17:03:54 -0500 | [diff] [blame] | 562 | SkCodec::MinBufferedBytesNeeded())); |
Leon Scroggins III | 2826e5f | 2014-02-05 19:46:02 -0500 | [diff] [blame] | 563 | |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 564 | return doDecode(env, stream.release(), padding, bitmapFactoryOptions); |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 565 | } |
| 566 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 567 | static jobject nativeDecodeAsset(JNIEnv* env, jobject clazz, jlong native_asset, |
Chris Craik | 905e824 | 2013-06-05 09:59:05 -0700 | [diff] [blame] | 568 | jobject padding, jobject options) { |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 569 | |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 570 | Asset* asset = reinterpret_cast<Asset*>(native_asset); |
Leon Scroggins III | 0aa39dc | 2014-06-03 12:19:32 -0400 | [diff] [blame] | 571 | // since we know we'll be done with the asset when we return, we can |
| 572 | // just use a simple wrapper |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 573 | std::unique_ptr<AssetStreamAdaptor> stream(new AssetStreamAdaptor(asset)); |
| 574 | return doDecode(env, stream.release(), padding, options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | static jobject nativeDecodeByteArray(JNIEnv* env, jobject, jbyteArray byteArray, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 578 | jint offset, jint length, jobject options) { |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 579 | |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 580 | AutoJavaByteArray ar(env, byteArray); |
Matt Sarett | b8adc9a | 2015-12-02 13:35:22 -0500 | [diff] [blame] | 581 | std::unique_ptr<SkMemoryStream> stream(new SkMemoryStream(ar.ptr() + offset, length, false)); |
| 582 | return doDecode(env, stream.release(), NULL, options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 583 | } |
| 584 | |
Owen Lin | a9d0d47 | 2011-01-18 17:39:15 +0800 | [diff] [blame] | 585 | static jboolean nativeIsSeekable(JNIEnv* env, jobject, jobject fileDescriptor) { |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 586 | jint descriptor = jniGetFDFromFileDescriptor(env, fileDescriptor); |
Owen Lin | a9d0d47 | 2011-01-18 17:39:15 +0800 | [diff] [blame] | 587 | return ::lseek64(descriptor, 0, SEEK_CUR) != -1 ? JNI_TRUE : JNI_FALSE; |
| 588 | } |
| 589 | |
John Reck | 4147877 | 2015-04-10 13:35:27 -0700 | [diff] [blame] | 590 | jobject decodeBitmap(JNIEnv* env, void* data, size_t size) { |
Ben Wagner | 60126ef | 2015-08-07 12:13:48 -0400 | [diff] [blame] | 591 | SkMemoryStream stream(data, size); |
John Reck | 4147877 | 2015-04-10 13:35:27 -0700 | [diff] [blame] | 592 | return doDecode(env, &stream, NULL, NULL); |
| 593 | } |
| 594 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 595 | /////////////////////////////////////////////////////////////////////////////// |
| 596 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 597 | static const JNINativeMethod gMethods[] = { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 598 | { "nativeDecodeStream", |
| 599 | "(Ljava/io/InputStream;[BLandroid/graphics/Rect;Landroid/graphics/BitmapFactory$Options;)Landroid/graphics/Bitmap;", |
| 600 | (void*)nativeDecodeStream |
| 601 | }, |
| 602 | |
| 603 | { "nativeDecodeFileDescriptor", |
| 604 | "(Ljava/io/FileDescriptor;Landroid/graphics/Rect;Landroid/graphics/BitmapFactory$Options;)Landroid/graphics/Bitmap;", |
| 605 | (void*)nativeDecodeFileDescriptor |
| 606 | }, |
| 607 | |
| 608 | { "nativeDecodeAsset", |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 609 | "(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] | 610 | (void*)nativeDecodeAsset |
| 611 | }, |
| 612 | |
| 613 | { "nativeDecodeByteArray", |
| 614 | "([BIILandroid/graphics/BitmapFactory$Options;)Landroid/graphics/Bitmap;", |
| 615 | (void*)nativeDecodeByteArray |
| 616 | }, |
| 617 | |
Owen Lin | a9d0d47 | 2011-01-18 17:39:15 +0800 | [diff] [blame] | 618 | { "nativeIsSeekable", |
| 619 | "(Ljava/io/FileDescriptor;)Z", |
| 620 | (void*)nativeIsSeekable |
| 621 | }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 622 | }; |
| 623 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 624 | int register_android_graphics_BitmapFactory(JNIEnv* env) { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 625 | jclass options_class = FindClassOrDie(env, "android/graphics/BitmapFactory$Options"); |
| 626 | gOptions_bitmapFieldID = GetFieldIDOrDie(env, options_class, "inBitmap", |
Chris Craik | 47cd8e9 | 2014-07-08 17:13:08 -0700 | [diff] [blame] | 627 | "Landroid/graphics/Bitmap;"); |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 628 | gOptions_justBoundsFieldID = GetFieldIDOrDie(env, options_class, "inJustDecodeBounds", "Z"); |
| 629 | gOptions_sampleSizeFieldID = GetFieldIDOrDie(env, options_class, "inSampleSize", "I"); |
| 630 | gOptions_configFieldID = GetFieldIDOrDie(env, options_class, "inPreferredConfig", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 631 | "Landroid/graphics/Bitmap$Config;"); |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 632 | gOptions_premultipliedFieldID = GetFieldIDOrDie(env, options_class, "inPremultiplied", "Z"); |
| 633 | gOptions_mutableFieldID = GetFieldIDOrDie(env, options_class, "inMutable", "Z"); |
| 634 | gOptions_ditherFieldID = GetFieldIDOrDie(env, options_class, "inDither", "Z"); |
| 635 | gOptions_preferQualityOverSpeedFieldID = GetFieldIDOrDie(env, options_class, |
Wei-Ta Chen | 953f909 | 2010-12-03 14:06:18 -0800 | [diff] [blame] | 636 | "inPreferQualityOverSpeed", "Z"); |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 637 | gOptions_scaledFieldID = GetFieldIDOrDie(env, options_class, "inScaled", "Z"); |
| 638 | gOptions_densityFieldID = GetFieldIDOrDie(env, options_class, "inDensity", "I"); |
| 639 | gOptions_screenDensityFieldID = GetFieldIDOrDie(env, options_class, "inScreenDensity", "I"); |
| 640 | gOptions_targetDensityFieldID = GetFieldIDOrDie(env, options_class, "inTargetDensity", "I"); |
| 641 | gOptions_widthFieldID = GetFieldIDOrDie(env, options_class, "outWidth", "I"); |
| 642 | gOptions_heightFieldID = GetFieldIDOrDie(env, options_class, "outHeight", "I"); |
| 643 | gOptions_mimeFieldID = GetFieldIDOrDie(env, options_class, "outMimeType", "Ljava/lang/String;"); |
| 644 | gOptions_mCancelID = GetFieldIDOrDie(env, options_class, "mCancel", "Z"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 645 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 646 | jclass bitmap_class = FindClassOrDie(env, "android/graphics/Bitmap"); |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 647 | gBitmap_ninePatchInsetsFieldID = GetFieldIDOrDie(env, bitmap_class, "mNinePatchInsets", |
Chris Craik | 47cd8e9 | 2014-07-08 17:13:08 -0700 | [diff] [blame] | 648 | "Landroid/graphics/NinePatch$InsetStruct;"); |
| 649 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 650 | gInsetStruct_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, |
| 651 | "android/graphics/NinePatch$InsetStruct")); |
| 652 | gInsetStruct_constructorMethodID = GetMethodIDOrDie(env, gInsetStruct_class, "<init>", |
| 653 | "(IIIIIIIIFIF)V"); |
Chris Craik | 47cd8e9 | 2014-07-08 17:13:08 -0700 | [diff] [blame] | 654 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 655 | return android::RegisterMethodsOrDie(env, "android/graphics/BitmapFactory", |
| 656 | gMethods, NELEM(gMethods)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 657 | } |