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" |
Leon Scroggins | a06d86a | 2011-03-02 16:56:54 -0500 | [diff] [blame] | 4 | #include "NinePatchPeeker.h" |
Derek Sollenberger | 5827cb5 | 2013-07-26 14:58:06 -0400 | [diff] [blame] | 5 | #include "SkData.h" |
Leon Scroggins III | 7315f1b | 2013-09-10 20:26:05 -0400 | [diff] [blame] | 6 | #include "SkFrontBufferedStream.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7 | #include "SkImageDecoder.h" |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 8 | #include "SkImageRef_ashmem.h" |
| 9 | #include "SkImageRef_GlobalPool.h" |
Leon Scroggins | 46cb9bd | 2014-03-06 15:36:39 -0500 | [diff] [blame] | 10 | #include "SkMath.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11 | #include "SkPixelRef.h" |
| 12 | #include "SkStream.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13 | #include "SkTemplates.h" |
| 14 | #include "SkUtils.h" |
| 15 | #include "CreateJavaOutputStreamAdaptor.h" |
Joseph Wen | f1f48bc | 2010-07-19 16:59:51 +0800 | [diff] [blame] | 16 | #include "AutoDecodeCancel.h" |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 17 | #include "Utils.h" |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 18 | #include "JNIHelp.h" |
Chris Craik | 1abf5d6 | 2013-08-16 12:47:03 -0700 | [diff] [blame] | 19 | #include "GraphicsJNI.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | |
| 21 | #include <android_runtime/AndroidRuntime.h> |
Mathias Agopian | b13b9bd | 2012-02-17 18:27:36 -0800 | [diff] [blame] | 22 | #include <androidfw/Asset.h> |
| 23 | #include <androidfw/ResourceTypes.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | #include <netinet/in.h> |
Leon Scroggins III | 0102f8a | 2014-01-14 15:14:57 -0500 | [diff] [blame] | 25 | #include <stdio.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | #include <sys/mman.h> |
Joseph Wen | 2dcfbef | 2010-09-10 10:15:09 +0800 | [diff] [blame] | 27 | #include <sys/stat.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | |
Joseph Wen | f1f48bc | 2010-07-19 16:59:51 +0800 | [diff] [blame] | 29 | jfieldID gOptions_justBoundsFieldID; |
| 30 | jfieldID gOptions_sampleSizeFieldID; |
| 31 | jfieldID gOptions_configFieldID; |
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; |
| 35 | jfieldID gOptions_purgeableFieldID; |
| 36 | jfieldID gOptions_shareableFieldID; |
Wei-Ta Chen | 953f909 | 2010-12-03 14:06:18 -0800 | [diff] [blame] | 37 | jfieldID gOptions_preferQualityOverSpeedFieldID; |
Chris Craik | 905e824 | 2013-06-05 09:59:05 -0700 | [diff] [blame] | 38 | jfieldID gOptions_scaledFieldID; |
| 39 | jfieldID gOptions_densityFieldID; |
| 40 | jfieldID gOptions_screenDensityFieldID; |
| 41 | jfieldID gOptions_targetDensityFieldID; |
Joseph Wen | f1f48bc | 2010-07-19 16:59:51 +0800 | [diff] [blame] | 42 | jfieldID gOptions_widthFieldID; |
| 43 | jfieldID gOptions_heightFieldID; |
| 44 | jfieldID gOptions_mimeFieldID; |
| 45 | jfieldID gOptions_mCancelID; |
Chet Haase | 37f74ca | 2010-12-08 17:56:36 -0800 | [diff] [blame] | 46 | jfieldID gOptions_bitmapFieldID; |
Chet Haase | 37f74ca | 2010-12-08 17:56:36 -0800 | [diff] [blame] | 47 | jfieldID gBitmap_nativeBitmapFieldID; |
Amith Yamasani | ec4a504 | 2012-04-04 10:27:15 -0700 | [diff] [blame] | 48 | jfieldID gBitmap_layoutBoundsFieldID; |
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 | #if 0 |
| 51 | #define TRACE_BITMAP(code) code |
| 52 | #else |
| 53 | #define TRACE_BITMAP(code) |
| 54 | #endif |
| 55 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | using namespace android; |
| 57 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | static inline int32_t validOrNeg1(bool isValid, int32_t value) { |
| 59 | // return isValid ? value : -1; |
| 60 | SkASSERT((int)isValid == 0 || (int)isValid == 1); |
| 61 | return ((int32_t)isValid - 1) | value; |
| 62 | } |
| 63 | |
Joseph Wen | f1f48bc | 2010-07-19 16:59:51 +0800 | [diff] [blame] | 64 | jstring getMimeTypeString(JNIEnv* env, SkImageDecoder::Format format) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | static const struct { |
| 66 | SkImageDecoder::Format fFormat; |
| 67 | const char* fMimeType; |
| 68 | } gMimeTypes[] = { |
| 69 | { SkImageDecoder::kBMP_Format, "image/bmp" }, |
| 70 | { SkImageDecoder::kGIF_Format, "image/gif" }, |
| 71 | { SkImageDecoder::kICO_Format, "image/x-ico" }, |
| 72 | { SkImageDecoder::kJPEG_Format, "image/jpeg" }, |
| 73 | { SkImageDecoder::kPNG_Format, "image/png" }, |
Chris Craik | 95587f9 | 2013-07-12 19:46:19 -0700 | [diff] [blame] | 74 | { SkImageDecoder::kWEBP_Format, "image/webp" }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | { SkImageDecoder::kWBMP_Format, "image/vnd.wap.wbmp" } |
| 76 | }; |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 77 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | const char* cstr = NULL; |
| 79 | for (size_t i = 0; i < SK_ARRAY_COUNT(gMimeTypes); i++) { |
| 80 | if (gMimeTypes[i].fFormat == format) { |
| 81 | cstr = gMimeTypes[i].fMimeType; |
| 82 | break; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | jstring jstr = 0; |
| 87 | if (NULL != cstr) { |
| 88 | jstr = env->NewStringUTF(cstr); |
| 89 | } |
| 90 | return jstr; |
| 91 | } |
| 92 | |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 93 | static bool optionsPurgeable(JNIEnv* env, jobject options) { |
Romain Guy | caf813f | 2012-03-15 18:57:48 -0700 | [diff] [blame] | 94 | return options != NULL && env->GetBooleanField(options, gOptions_purgeableFieldID); |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 95 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 97 | static bool optionsShareable(JNIEnv* env, jobject options) { |
Romain Guy | caf813f | 2012-03-15 18:57:48 -0700 | [diff] [blame] | 98 | return options != NULL && env->GetBooleanField(options, gOptions_shareableFieldID); |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 99 | } |
| 100 | |
Bryan Mawhinney | 2a3d754 | 2010-11-03 17:23:57 +0000 | [diff] [blame] | 101 | static bool optionsJustBounds(JNIEnv* env, jobject options) { |
Romain Guy | caf813f | 2012-03-15 18:57:48 -0700 | [diff] [blame] | 102 | return options != NULL && env->GetBooleanField(options, gOptions_justBoundsFieldID); |
Bryan Mawhinney | 2a3d754 | 2010-11-03 17:23:57 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 105 | static void scaleNinePatchChunk(android::Res_png_9patch* chunk, float scale) { |
| 106 | chunk->paddingLeft = int(chunk->paddingLeft * scale + 0.5f); |
| 107 | chunk->paddingTop = int(chunk->paddingTop * scale + 0.5f); |
| 108 | chunk->paddingRight = int(chunk->paddingRight * scale + 0.5f); |
| 109 | chunk->paddingBottom = int(chunk->paddingBottom * scale + 0.5f); |
| 110 | |
Narayan Kamath | 6381dd4 | 2014-03-03 17:12:03 +0000 | [diff] [blame] | 111 | int32_t* xDivs = chunk->getXDivs(); |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 112 | for (int i = 0; i < chunk->numXDivs; i++) { |
Narayan Kamath | 6381dd4 | 2014-03-03 17:12:03 +0000 | [diff] [blame] | 113 | xDivs[i] = int32_t(xDivs[i] * scale + 0.5f); |
| 114 | if (i > 0 && xDivs[i] == xDivs[i - 1]) { |
| 115 | xDivs[i]++; |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
Narayan Kamath | 42a51ae | 2014-03-11 16:50:30 +0000 | [diff] [blame] | 119 | int32_t* yDivs = chunk->getYDivs(); |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 120 | for (int i = 0; i < chunk->numYDivs; i++) { |
Narayan Kamath | 6381dd4 | 2014-03-03 17:12:03 +0000 | [diff] [blame] | 121 | yDivs[i] = int32_t(yDivs[i] * scale + 0.5f); |
| 122 | if (i > 0 && yDivs[i] == yDivs[i - 1]) { |
| 123 | yDivs[i]++; |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
Leon Scroggins III | 7315f1b | 2013-09-10 20:26:05 -0400 | [diff] [blame] | 128 | static SkPixelRef* installPixelRef(SkBitmap* bitmap, SkStreamRewindable* stream, |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 129 | int sampleSize, bool ditherImage) { |
| 130 | |
Leon Scroggins III | 28408c2 | 2014-05-27 15:55:39 -0400 | [diff] [blame^] | 131 | if (kUnknown_SkColorType == bitmap->colorType()) { |
Derek Sollenberger | b644a3b | 2014-01-17 15:45:10 -0500 | [diff] [blame] | 132 | ALOGW("bitmap has unknown configuration so no memory has been allocated"); |
| 133 | return NULL; |
| 134 | } |
| 135 | |
Mike Reed | 1715441 | 2009-09-24 12:35:27 -0400 | [diff] [blame] | 136 | SkImageRef* pr; |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 137 | // only use ashmem for large images, since mmaps come at a price |
Wei-Ta Chen | 2a2c5cd | 2009-06-03 14:08:04 -0700 | [diff] [blame] | 138 | if (bitmap->getSize() >= 32 * 1024) { |
Leon Scroggins III | 28408c2 | 2014-05-27 15:55:39 -0400 | [diff] [blame^] | 139 | pr = new SkImageRef_ashmem(bitmap->info(), stream, sampleSize); |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 140 | } else { |
Leon Scroggins III | 28408c2 | 2014-05-27 15:55:39 -0400 | [diff] [blame^] | 141 | pr = new SkImageRef_GlobalPool(bitmap->info(), stream, sampleSize); |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 142 | } |
Mike Reed | 1715441 | 2009-09-24 12:35:27 -0400 | [diff] [blame] | 143 | pr->setDitherImage(ditherImage); |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 144 | bitmap->setPixelRef(pr)->unref(); |
Romain Guy | 288471d | 2010-08-19 14:41:16 -0700 | [diff] [blame] | 145 | pr->isOpaque(bitmap); |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 146 | return pr; |
| 147 | } |
| 148 | |
Leon Scroggins | 46cb9bd | 2014-03-06 15:36:39 -0500 | [diff] [blame] | 149 | static SkColorType colorTypeForScaledOutput(SkColorType colorType) { |
| 150 | switch (colorType) { |
| 151 | case kUnknown_SkColorType: |
| 152 | case kIndex_8_SkColorType: |
Leon Scroggins | cc11f15 | 2014-03-31 16:52:13 -0400 | [diff] [blame] | 153 | return kNative_8888_SkColorType; |
Chris Craik | 905e824 | 2013-06-05 09:59:05 -0700 | [diff] [blame] | 154 | default: |
| 155 | break; |
| 156 | } |
Leon Scroggins | 46cb9bd | 2014-03-06 15:36:39 -0500 | [diff] [blame] | 157 | return colorType; |
Chris Craik | 905e824 | 2013-06-05 09:59:05 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | class ScaleCheckingAllocator : public SkBitmap::HeapAllocator { |
| 161 | public: |
| 162 | ScaleCheckingAllocator(float scale, int size) |
| 163 | : mScale(scale), mSize(size) { |
| 164 | } |
| 165 | |
| 166 | virtual bool allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable) { |
| 167 | // accounts for scale in final allocation, using eventual size and config |
Leon Scroggins | 46cb9bd | 2014-03-06 15:36:39 -0500 | [diff] [blame] | 168 | const int bytesPerPixel = SkColorTypeBytesPerPixel( |
| 169 | colorTypeForScaledOutput(bitmap->colorType())); |
Chris Craik | 905e824 | 2013-06-05 09:59:05 -0700 | [diff] [blame] | 170 | const int requestedSize = bytesPerPixel * |
| 171 | int(bitmap->width() * mScale + 0.5f) * |
| 172 | int(bitmap->height() * mScale + 0.5f); |
| 173 | if (requestedSize > mSize) { |
| 174 | ALOGW("bitmap for alloc reuse (%d bytes) can't fit scaled bitmap (%d bytes)", |
| 175 | mSize, requestedSize); |
| 176 | return false; |
| 177 | } |
| 178 | return SkBitmap::HeapAllocator::allocPixelRef(bitmap, ctable); |
| 179 | } |
| 180 | private: |
| 181 | const float mScale; |
| 182 | const int mSize; |
| 183 | }; |
| 184 | |
Chris Craik | 7e8c03c | 2013-06-03 13:53:36 -0700 | [diff] [blame] | 185 | class RecyclingPixelAllocator : public SkBitmap::Allocator { |
| 186 | public: |
| 187 | RecyclingPixelAllocator(SkPixelRef* pixelRef, unsigned int size) |
Chris Craik | 905e824 | 2013-06-05 09:59:05 -0700 | [diff] [blame] | 188 | : mPixelRef(pixelRef), mSize(size) { |
Chris Craik | 7e8c03c | 2013-06-03 13:53:36 -0700 | [diff] [blame] | 189 | SkSafeRef(mPixelRef); |
| 190 | } |
| 191 | |
| 192 | ~RecyclingPixelAllocator() { |
| 193 | SkSafeUnref(mPixelRef); |
| 194 | } |
| 195 | |
| 196 | virtual bool allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable) { |
Leon Scroggins | 46cb9bd | 2014-03-06 15:36:39 -0500 | [diff] [blame] | 197 | const SkImageInfo& info = bitmap->info(); |
| 198 | if (info.fColorType == kUnknown_SkColorType) { |
| 199 | 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] | 200 | return false; |
| 201 | } |
Chris Craik | cd0ba71 | 2013-09-06 14:40:30 -0700 | [diff] [blame] | 202 | |
Leon Scroggins | 46cb9bd | 2014-03-06 15:36:39 -0500 | [diff] [blame] | 203 | const int64_t size64 = info.getSafeSize64(bitmap->rowBytes()); |
| 204 | if (!sk_64_isS32(size64)) { |
| 205 | ALOGW("bitmap is too large"); |
| 206 | return false; |
| 207 | } |
| 208 | |
| 209 | const size_t size = sk_64_asS32(size64); |
| 210 | if (size > mSize) { |
| 211 | ALOGW("bitmap marked for reuse (%d bytes) can't fit new bitmap (%d bytes)", |
| 212 | mSize, size); |
Derek Sollenberger | b644a3b | 2014-01-17 15:45:10 -0500 | [diff] [blame] | 213 | return false; |
| 214 | } |
| 215 | |
Chris Craik | cd0ba71 | 2013-09-06 14:40:30 -0700 | [diff] [blame] | 216 | // Create a new pixelref with the new ctable that wraps the previous pixelref |
Derek Sollenberger | b644a3b | 2014-01-17 15:45:10 -0500 | [diff] [blame] | 217 | SkPixelRef* pr = new AndroidPixelRef(*static_cast<AndroidPixelRef*>(mPixelRef), |
Leon Scroggins | 46cb9bd | 2014-03-06 15:36:39 -0500 | [diff] [blame] | 218 | info, bitmap->rowBytes(), ctable); |
Chris Craik | cd0ba71 | 2013-09-06 14:40:30 -0700 | [diff] [blame] | 219 | |
| 220 | bitmap->setPixelRef(pr)->unref(); |
| 221 | // since we're already allocated, we lockPixels right away |
| 222 | // HeapAllocator/JavaPixelAllocator behaves this way too |
Chris Craik | 7e8c03c | 2013-06-03 13:53:36 -0700 | [diff] [blame] | 223 | bitmap->lockPixels(); |
| 224 | return true; |
| 225 | } |
| 226 | |
| 227 | private: |
| 228 | SkPixelRef* const mPixelRef; |
| 229 | const unsigned int mSize; |
| 230 | }; |
| 231 | |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 232 | // since we "may" create a purgeable imageref, we require the stream be ref'able |
| 233 | // i.e. dynamically allocated, since its lifetime may exceed the current stack |
| 234 | // frame. |
Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 235 | static jobject doDecode(JNIEnv* env, SkStreamRewindable* stream, jobject padding, |
Chris Craik | 905e824 | 2013-06-05 09:59:05 -0700 | [diff] [blame] | 236 | jobject options, bool allowPurgeable, bool forcePurgeable = false) { |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 237 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 238 | int sampleSize = 1; |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 239 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | SkImageDecoder::Mode mode = SkImageDecoder::kDecodePixels_Mode; |
Romain Guy | 99b7b7a | 2011-07-14 12:42:19 -0700 | [diff] [blame] | 241 | SkBitmap::Config prefConfig = SkBitmap::kARGB_8888_Config; |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 242 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 243 | bool doDither = true; |
Romain Guy | 2361098 | 2011-01-17 12:51:55 -0800 | [diff] [blame] | 244 | bool isMutable = false; |
Chris Craik | 905e824 | 2013-06-05 09:59:05 -0700 | [diff] [blame] | 245 | float scale = 1.0f; |
| 246 | bool isPurgeable = forcePurgeable || (allowPurgeable && optionsPurgeable(env, options)); |
Wei-Ta Chen | 953f909 | 2010-12-03 14:06:18 -0800 | [diff] [blame] | 247 | bool preferQualityOverSpeed = false; |
Chris Craik | 1abf5d6 | 2013-08-16 12:47:03 -0700 | [diff] [blame] | 248 | bool requireUnpremultiplied = false; |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 249 | |
Chet Haase | 37f74ca | 2010-12-08 17:56:36 -0800 | [diff] [blame] | 250 | jobject javaBitmap = NULL; |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 251 | |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 252 | if (options != NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 253 | sampleSize = env->GetIntField(options, gOptions_sampleSizeFieldID); |
Bryan Mawhinney | 2a3d754 | 2010-11-03 17:23:57 +0000 | [diff] [blame] | 254 | if (optionsJustBounds(env, options)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 | mode = SkImageDecoder::kDecodeBounds_Mode; |
| 256 | } |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 257 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | // initialize these, in case we fail later on |
| 259 | env->SetIntField(options, gOptions_widthFieldID, -1); |
| 260 | env->SetIntField(options, gOptions_heightFieldID, -1); |
| 261 | env->SetObjectField(options, gOptions_mimeFieldID, 0); |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 262 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 263 | jobject jconfig = env->GetObjectField(options, gOptions_configFieldID); |
| 264 | prefConfig = GraphicsJNI::getNativeBitmapConfig(env, jconfig); |
Romain Guy | 2361098 | 2011-01-17 12:51:55 -0800 | [diff] [blame] | 265 | isMutable = env->GetBooleanField(options, gOptions_mutableFieldID); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 266 | doDither = env->GetBooleanField(options, gOptions_ditherFieldID); |
Wei-Ta Chen | 953f909 | 2010-12-03 14:06:18 -0800 | [diff] [blame] | 267 | preferQualityOverSpeed = env->GetBooleanField(options, |
| 268 | gOptions_preferQualityOverSpeedFieldID); |
Chris Craik | 1abf5d6 | 2013-08-16 12:47:03 -0700 | [diff] [blame] | 269 | requireUnpremultiplied = !env->GetBooleanField(options, gOptions_premultipliedFieldID); |
Chet Haase | 37f74ca | 2010-12-08 17:56:36 -0800 | [diff] [blame] | 270 | javaBitmap = env->GetObjectField(options, gOptions_bitmapFieldID); |
Chris Craik | 905e824 | 2013-06-05 09:59:05 -0700 | [diff] [blame] | 271 | |
| 272 | if (env->GetBooleanField(options, gOptions_scaledFieldID)) { |
| 273 | const int density = env->GetIntField(options, gOptions_densityFieldID); |
| 274 | const int targetDensity = env->GetIntField(options, gOptions_targetDensityFieldID); |
| 275 | const int screenDensity = env->GetIntField(options, gOptions_screenDensityFieldID); |
| 276 | if (density != 0 && targetDensity != 0 && density != screenDensity) { |
| 277 | scale = (float) targetDensity / density; |
| 278 | } |
| 279 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 280 | } |
| 281 | |
Chris Craik | 905e824 | 2013-06-05 09:59:05 -0700 | [diff] [blame] | 282 | const bool willScale = scale != 1.0f; |
| 283 | isPurgeable &= !willScale; |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 284 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 285 | SkImageDecoder* decoder = SkImageDecoder::Factory(stream); |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 286 | if (decoder == NULL) { |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 287 | return nullObjectReturn("SkImageDecoder::Factory returned null"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 288 | } |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 289 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 290 | decoder->setSampleSize(sampleSize); |
| 291 | decoder->setDitherImage(doDither); |
Wei-Ta Chen | 953f909 | 2010-12-03 14:06:18 -0800 | [diff] [blame] | 292 | decoder->setPreferQualityOverSpeed(preferQualityOverSpeed); |
Chris Craik | 1abf5d6 | 2013-08-16 12:47:03 -0700 | [diff] [blame] | 293 | decoder->setRequireUnpremultipliedColors(requireUnpremultiplied); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 294 | |
Chris Craik | 7e8c03c | 2013-06-03 13:53:36 -0700 | [diff] [blame] | 295 | SkBitmap* outputBitmap = NULL; |
Chris Craik | 9f58361 | 2013-05-20 18:13:47 -0700 | [diff] [blame] | 296 | unsigned int existingBufferSize = 0; |
Chris Craik | 7e8c03c | 2013-06-03 13:53:36 -0700 | [diff] [blame] | 297 | if (javaBitmap != NULL) { |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 298 | outputBitmap = (SkBitmap*) env->GetLongField(javaBitmap, gBitmap_nativeBitmapFieldID); |
Chris Craik | 7e8c03c | 2013-06-03 13:53:36 -0700 | [diff] [blame] | 299 | if (outputBitmap->isImmutable()) { |
Derek Sollenberger | 2a6ecae | 2012-08-31 14:03:51 -0400 | [diff] [blame] | 300 | ALOGW("Unable to reuse an immutable bitmap as an image decoder target."); |
Chris Craik | 7e8c03c | 2013-06-03 13:53:36 -0700 | [diff] [blame] | 301 | javaBitmap = NULL; |
| 302 | outputBitmap = NULL; |
| 303 | } else { |
| 304 | existingBufferSize = GraphicsJNI::getBitmapAllocationByteCount(env, javaBitmap); |
Derek Sollenberger | 2a6ecae | 2012-08-31 14:03:51 -0400 | [diff] [blame] | 305 | } |
Chet Haase | 37f74ca | 2010-12-08 17:56:36 -0800 | [diff] [blame] | 306 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 307 | |
Chris Craik | 7e8c03c | 2013-06-03 13:53:36 -0700 | [diff] [blame] | 308 | SkAutoTDelete<SkBitmap> adb(outputBitmap == NULL ? new SkBitmap : NULL); |
| 309 | if (outputBitmap == NULL) outputBitmap = adb.get(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 310 | |
Chris Craik | 7e8c03c | 2013-06-03 13:53:36 -0700 | [diff] [blame] | 311 | NinePatchPeeker peeker(decoder); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 312 | decoder->setPeeker(&peeker); |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 313 | |
Chris Craik | 7e8c03c | 2013-06-03 13:53:36 -0700 | [diff] [blame] | 314 | SkImageDecoder::Mode decodeMode = isPurgeable ? SkImageDecoder::kDecodeBounds_Mode : mode; |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 315 | |
Chris Craik | 905e824 | 2013-06-05 09:59:05 -0700 | [diff] [blame] | 316 | JavaPixelAllocator javaAllocator(env); |
| 317 | RecyclingPixelAllocator recyclingAllocator(outputBitmap->pixelRef(), existingBufferSize); |
| 318 | ScaleCheckingAllocator scaleCheckingAllocator(scale, existingBufferSize); |
| 319 | SkBitmap::Allocator* outputAllocator = (javaBitmap != NULL) ? |
| 320 | (SkBitmap::Allocator*)&recyclingAllocator : (SkBitmap::Allocator*)&javaAllocator; |
| 321 | if (decodeMode != SkImageDecoder::kDecodeBounds_Mode) { |
| 322 | if (!willScale) { |
Leon Scroggins III | 1ffe727 | 2013-09-19 11:34:06 -0400 | [diff] [blame] | 323 | // If the java allocator is being used to allocate the pixel memory, the decoder |
| 324 | // need not write zeroes, since the memory is initialized to 0. |
| 325 | decoder->setSkipWritingZeroes(outputAllocator == &javaAllocator); |
Chris Craik | 905e824 | 2013-06-05 09:59:05 -0700 | [diff] [blame] | 326 | decoder->setAllocator(outputAllocator); |
| 327 | } else if (javaBitmap != NULL) { |
| 328 | // check for eventual scaled bounds at allocation time, so we don't decode the bitmap |
| 329 | // only to find the scaled result too large to fit in the allocation |
| 330 | decoder->setAllocator(&scaleCheckingAllocator); |
| 331 | } |
| 332 | } |
| 333 | |
Derek Sollenberger | 6b0437c | 2013-06-24 15:40:54 -0400 | [diff] [blame] | 334 | // Only setup the decoder to be deleted after its stack-based, refcounted |
| 335 | // components (allocators, peekers, etc) are declared. This prevents RefCnt |
| 336 | // asserts from firing due to the order objects are deleted from the stack. |
| 337 | SkAutoTDelete<SkImageDecoder> add(decoder); |
| 338 | |
| 339 | AutoDecoderCancel adc(options, decoder); |
| 340 | |
| 341 | // To fix the race condition in case "requestCancelDecode" |
| 342 | // happens earlier than AutoDecoderCancel object is added |
| 343 | // to the gAutoDecoderCancelMutex linked list. |
| 344 | if (options != NULL && env->GetBooleanField(options, gOptions_mCancelID)) { |
| 345 | return nullObjectReturn("gOptions_mCancelID"); |
| 346 | } |
| 347 | |
Chris Craik | 7e8c03c | 2013-06-03 13:53:36 -0700 | [diff] [blame] | 348 | SkBitmap decodingBitmap; |
| 349 | if (!decoder->decode(stream, &decodingBitmap, prefConfig, decodeMode)) { |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 350 | return nullObjectReturn("decoder->decode returned false"); |
| 351 | } |
| 352 | |
Chris Craik | 7e8c03c | 2013-06-03 13:53:36 -0700 | [diff] [blame] | 353 | int scaledWidth = decodingBitmap.width(); |
| 354 | int scaledHeight = decodingBitmap.height(); |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 355 | |
| 356 | if (willScale && mode != SkImageDecoder::kDecodeBounds_Mode) { |
| 357 | scaledWidth = int(scaledWidth * scale + 0.5f); |
| 358 | scaledHeight = int(scaledHeight * scale + 0.5f); |
| 359 | } |
| 360 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 361 | // update options (if any) |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 362 | if (options != NULL) { |
| 363 | env->SetIntField(options, gOptions_widthFieldID, scaledWidth); |
| 364 | env->SetIntField(options, gOptions_heightFieldID, scaledHeight); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 365 | env->SetObjectField(options, gOptions_mimeFieldID, |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 366 | getMimeTypeString(env, decoder->getFormat())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 367 | } |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 368 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 369 | // if we're in justBounds mode, return now (skip the java bitmap) |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 370 | if (mode == SkImageDecoder::kDecodeBounds_Mode) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 371 | return NULL; |
| 372 | } |
| 373 | |
| 374 | jbyteArray ninePatchChunk = NULL; |
Amith Yamasani | ec4a504 | 2012-04-04 10:27:15 -0700 | [diff] [blame] | 375 | if (peeker.fPatch != NULL) { |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 376 | if (willScale) { |
| 377 | scaleNinePatchChunk(peeker.fPatch, scale); |
| 378 | } |
| 379 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 380 | size_t ninePatchArraySize = peeker.fPatch->serializedSize(); |
| 381 | ninePatchChunk = env->NewByteArray(ninePatchArraySize); |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 382 | if (ninePatchChunk == NULL) { |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 383 | return nullObjectReturn("ninePatchChunk == null"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 384 | } |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 385 | |
| 386 | jbyte* array = (jbyte*) env->GetPrimitiveArrayCritical(ninePatchChunk, NULL); |
| 387 | if (array == NULL) { |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 388 | return nullObjectReturn("primitive array == null"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 389 | } |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 390 | |
Narayan Kamath | 6381dd4 | 2014-03-03 17:12:03 +0000 | [diff] [blame] | 391 | memcpy(array, peeker.fPatch, peeker.fPatchSize); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 392 | env->ReleasePrimitiveArrayCritical(ninePatchChunk, array, 0); |
| 393 | } |
| 394 | |
Amith Yamasani | ec4a504 | 2012-04-04 10:27:15 -0700 | [diff] [blame] | 395 | jintArray layoutBounds = NULL; |
| 396 | if (peeker.fLayoutBounds != NULL) { |
| 397 | layoutBounds = env->NewIntArray(4); |
| 398 | if (layoutBounds == NULL) { |
| 399 | return nullObjectReturn("layoutBounds == null"); |
| 400 | } |
| 401 | |
Dianne Hackborn | 7634424 | 2012-04-30 14:05:09 -0700 | [diff] [blame] | 402 | jint scaledBounds[4]; |
| 403 | if (willScale) { |
| 404 | for (int i=0; i<4; i++) { |
| 405 | scaledBounds[i] = (jint)((((jint*)peeker.fLayoutBounds)[i]*scale) + .5f); |
| 406 | } |
| 407 | } else { |
| 408 | memcpy(scaledBounds, (jint*)peeker.fLayoutBounds, sizeof(scaledBounds)); |
| 409 | } |
| 410 | env->SetIntArrayRegion(layoutBounds, 0, 4, scaledBounds); |
Amith Yamasani | ec4a504 | 2012-04-04 10:27:15 -0700 | [diff] [blame] | 411 | if (javaBitmap != NULL) { |
| 412 | env->SetObjectField(javaBitmap, gBitmap_layoutBoundsFieldID, layoutBounds); |
| 413 | } |
| 414 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 415 | |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 416 | if (willScale) { |
| 417 | // This is weird so let me explain: we could use the scale parameter |
| 418 | // directly, but for historical reasons this is how the corresponding |
| 419 | // Dalvik code has always behaved. We simply recreate the behavior here. |
| 420 | // The result is slightly different from simply using scale because of |
| 421 | // the 0.5f rounding bias applied when computing the target image size |
Chris Craik | 7e8c03c | 2013-06-03 13:53:36 -0700 | [diff] [blame] | 422 | const float sx = scaledWidth / float(decodingBitmap.width()); |
| 423 | const float sy = scaledHeight / float(decodingBitmap.height()); |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 424 | |
Chris Craik | 905e824 | 2013-06-05 09:59:05 -0700 | [diff] [blame] | 425 | // TODO: avoid copying when scaled size equals decodingBitmap size |
Leon Scroggins | 46cb9bd | 2014-03-06 15:36:39 -0500 | [diff] [blame] | 426 | SkColorType colorType = colorTypeForScaledOutput(decodingBitmap.colorType()); |
Leon Scroggins III | 8790be6 | 2013-12-03 16:26:51 -0500 | [diff] [blame] | 427 | // FIXME: If the alphaType is kUnpremul and the image has alpha, the |
| 428 | // colors may not be correct, since Skia does not yet support drawing |
| 429 | // to/from unpremultiplied bitmaps. |
Leon Scroggins | 46cb9bd | 2014-03-06 15:36:39 -0500 | [diff] [blame] | 430 | outputBitmap->setConfig(SkImageInfo::Make(scaledWidth, scaledHeight, |
| 431 | colorType, decodingBitmap.alphaType())); |
Chris Craik | 905e824 | 2013-06-05 09:59:05 -0700 | [diff] [blame] | 432 | if (!outputBitmap->allocPixels(outputAllocator, NULL)) { |
Raph Levien | 005bfc6 | 2012-09-20 22:51:47 -0700 | [diff] [blame] | 433 | return nullObjectReturn("allocation failed for scaled bitmap"); |
| 434 | } |
Leon Scroggins III | 1ffe727 | 2013-09-19 11:34:06 -0400 | [diff] [blame] | 435 | |
| 436 | // If outputBitmap's pixels are newly allocated by Java, there is no need |
| 437 | // to erase to 0, since the pixels were initialized to 0. |
| 438 | if (outputAllocator != &javaAllocator) { |
| 439 | outputBitmap->eraseColor(0); |
| 440 | } |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 441 | |
| 442 | SkPaint paint; |
Derek Sollenberger | b644a3b | 2014-01-17 15:45:10 -0500 | [diff] [blame] | 443 | paint.setFilterLevel(SkPaint::kLow_FilterLevel); |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 444 | |
Chris Craik | 7e8c03c | 2013-06-03 13:53:36 -0700 | [diff] [blame] | 445 | SkCanvas canvas(*outputBitmap); |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 446 | canvas.scale(sx, sy); |
Chris Craik | 7e8c03c | 2013-06-03 13:53:36 -0700 | [diff] [blame] | 447 | canvas.drawBitmap(decodingBitmap, 0.0f, 0.0f, &paint); |
| 448 | } else { |
| 449 | outputBitmap->swap(decodingBitmap); |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 450 | } |
| 451 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 452 | if (padding) { |
Amith Yamasani | ec4a504 | 2012-04-04 10:27:15 -0700 | [diff] [blame] | 453 | if (peeker.fPatch != NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 454 | GraphicsJNI::set_jrect(env, padding, |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 455 | peeker.fPatch->paddingLeft, peeker.fPatch->paddingTop, |
| 456 | peeker.fPatch->paddingRight, peeker.fPatch->paddingBottom); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 457 | } else { |
| 458 | GraphicsJNI::set_jrect(env, padding, -1, -1, -1, -1); |
| 459 | } |
| 460 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 461 | |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 462 | SkPixelRef* pr; |
| 463 | if (isPurgeable) { |
Chris Craik | 7e8c03c | 2013-06-03 13:53:36 -0700 | [diff] [blame] | 464 | pr = installPixelRef(outputBitmap, stream, sampleSize, doDither); |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 465 | } else { |
| 466 | // if we get here, we're in kDecodePixels_Mode and will therefore |
| 467 | // already have a pixelref installed. |
Chris Craik | 7e8c03c | 2013-06-03 13:53:36 -0700 | [diff] [blame] | 468 | pr = outputBitmap->pixelRef(); |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 469 | } |
Marco Nelissen | b2fe3be | 2012-05-07 11:24:13 -0700 | [diff] [blame] | 470 | if (pr == NULL) { |
| 471 | return nullObjectReturn("Got null SkPixelRef"); |
| 472 | } |
Romain Guy | 2361098 | 2011-01-17 12:51:55 -0800 | [diff] [blame] | 473 | |
Chris Craik | 7e8c03c | 2013-06-03 13:53:36 -0700 | [diff] [blame] | 474 | if (!isMutable && javaBitmap == NULL) { |
Romain Guy | 2361098 | 2011-01-17 12:51:55 -0800 | [diff] [blame] | 475 | // promise we will never change our pixels (great for sharing and pictures) |
| 476 | pr->setImmutable(); |
| 477 | } |
Chet Haase | 37f74ca | 2010-12-08 17:56:36 -0800 | [diff] [blame] | 478 | |
Jeff Brown | 27d8383 | 2012-05-09 17:30:31 -0700 | [diff] [blame] | 479 | // detach bitmap from its autodeleter, since we want to own it now |
| 480 | adb.detach(); |
| 481 | |
Chris Craik | 7e8c03c | 2013-06-03 13:53:36 -0700 | [diff] [blame] | 482 | if (javaBitmap != NULL) { |
Chris Craik | 1abf5d6 | 2013-08-16 12:47:03 -0700 | [diff] [blame] | 483 | bool isPremultiplied = !requireUnpremultiplied; |
| 484 | GraphicsJNI::reinitBitmap(env, javaBitmap, outputBitmap, isPremultiplied); |
Chris Craik | 7e8c03c | 2013-06-03 13:53:36 -0700 | [diff] [blame] | 485 | outputBitmap->notifyPixelsChanged(); |
Chet Haase | 37f74ca | 2010-12-08 17:56:36 -0800 | [diff] [blame] | 486 | // If a java bitmap was passed in for reuse, pass it back |
| 487 | return javaBitmap; |
| 488 | } |
Chris Craik | 1abf5d6 | 2013-08-16 12:47:03 -0700 | [diff] [blame] | 489 | |
| 490 | int bitmapCreateFlags = 0x0; |
| 491 | if (isMutable) bitmapCreateFlags |= GraphicsJNI::kBitmapCreateFlag_Mutable; |
| 492 | if (!requireUnpremultiplied) bitmapCreateFlags |= GraphicsJNI::kBitmapCreateFlag_Premultiplied; |
| 493 | |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 494 | // now create the java bitmap |
Chris Craik | 7e8c03c | 2013-06-03 13:53:36 -0700 | [diff] [blame] | 495 | return GraphicsJNI::createBitmap(env, outputBitmap, javaAllocator.getStorageObj(), |
Chris Craik | 1abf5d6 | 2013-08-16 12:47:03 -0700 | [diff] [blame] | 496 | bitmapCreateFlags, ninePatchChunk, layoutBounds, -1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 497 | } |
| 498 | |
Leon Scroggins III | 2826e5f | 2014-02-05 19:46:02 -0500 | [diff] [blame] | 499 | // Need to buffer enough input to be able to rewind as much as might be read by a decoder |
| 500 | // trying to determine the stream's format. Currently the most is 64, read by |
| 501 | // SkImageDecoder_libwebp. |
| 502 | // FIXME: Get this number from SkImageDecoder |
| 503 | #define BYTES_TO_BUFFER 64 |
| 504 | |
Chris Craik | 905e824 | 2013-06-05 09:59:05 -0700 | [diff] [blame] | 505 | static jobject nativeDecodeStream(JNIEnv* env, jobject clazz, jobject is, jbyteArray storage, |
| 506 | jobject padding, jobject options) { |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 507 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 508 | jobject bitmap = NULL; |
Leon Scroggins III | 7315f1b | 2013-09-10 20:26:05 -0400 | [diff] [blame] | 509 | SkAutoTUnref<SkStream> stream(CreateJavaInputStreamAdaptor(env, is, storage)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 510 | |
Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 511 | if (stream.get()) { |
Leon Scroggins III | 2826e5f | 2014-02-05 19:46:02 -0500 | [diff] [blame] | 512 | SkAutoTUnref<SkStreamRewindable> bufferedStream( |
| 513 | SkFrontBufferedStream::Create(stream, BYTES_TO_BUFFER)); |
Leon Scroggins III | 7315f1b | 2013-09-10 20:26:05 -0400 | [diff] [blame] | 514 | SkASSERT(bufferedStream.get() != NULL); |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 515 | // for now we don't allow purgeable with java inputstreams |
Leon Scroggins III | 7315f1b | 2013-09-10 20:26:05 -0400 | [diff] [blame] | 516 | bitmap = doDecode(env, bufferedStream, padding, options, false, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 517 | } |
| 518 | return bitmap; |
| 519 | } |
| 520 | |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 521 | static jobject nativeDecodeFileDescriptor(JNIEnv* env, jobject clazz, jobject fileDescriptor, |
| 522 | jobject padding, jobject bitmapFactoryOptions) { |
| 523 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 524 | NPE_CHECK_RETURN_ZERO(env, fileDescriptor); |
| 525 | |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 526 | jint descriptor = jniGetFDFromFileDescriptor(env, fileDescriptor); |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 527 | |
Derek Sollenberger | 5827cb5 | 2013-07-26 14:58:06 -0400 | [diff] [blame] | 528 | struct stat fdStat; |
| 529 | if (fstat(descriptor, &fdStat) == -1) { |
| 530 | doThrowIOE(env, "broken file descriptor"); |
| 531 | return nullObjectReturn("fstat return -1"); |
| 532 | } |
| 533 | |
Leon Scroggins III | 2826e5f | 2014-02-05 19:46:02 -0500 | [diff] [blame] | 534 | // Restore the descriptor's offset on exiting this function. |
| 535 | AutoFDSeek autoRestore(descriptor); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 536 | |
Leon Scroggins III | 0102f8a | 2014-01-14 15:14:57 -0500 | [diff] [blame] | 537 | FILE* file = fdopen(descriptor, "r"); |
| 538 | if (file == NULL) { |
| 539 | return nullObjectReturn("Could not open file"); |
Leon Scroggins III | f65183f | 2013-10-07 16:32:14 -0400 | [diff] [blame] | 540 | } |
Leon Scroggins III | 0102f8a | 2014-01-14 15:14:57 -0500 | [diff] [blame] | 541 | |
Leon Scroggins III | 2826e5f | 2014-02-05 19:46:02 -0500 | [diff] [blame] | 542 | SkAutoTUnref<SkFILEStream> fileStream(new SkFILEStream(file, |
Leon Scroggins III | 0102f8a | 2014-01-14 15:14:57 -0500 | [diff] [blame] | 543 | SkFILEStream::kCallerRetains_Ownership)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 544 | |
Leon Scroggins III | 2826e5f | 2014-02-05 19:46:02 -0500 | [diff] [blame] | 545 | SkAutoTUnref<SkStreamRewindable> stream; |
| 546 | |
| 547 | // Retain the old behavior of allowing purgeable if both purgeable and |
| 548 | // shareable are set to true. |
| 549 | bool isPurgeable = optionsPurgeable(env, bitmapFactoryOptions) |
| 550 | && optionsShareable(env, bitmapFactoryOptions); |
| 551 | if (isPurgeable) { |
| 552 | // Copy the stream, so the image can be decoded multiple times without |
| 553 | // continuing to modify the original file descriptor. |
| 554 | // Copy beginning from the current position. |
| 555 | const size_t fileSize = fileStream->getLength() - fileStream->getPosition(); |
| 556 | void* buffer = sk_malloc_flags(fileSize, 0); |
| 557 | if (buffer == NULL) { |
| 558 | return nullObjectReturn("Could not make a copy for ashmem"); |
| 559 | } |
| 560 | |
| 561 | SkAutoTUnref<SkData> data(SkData::NewFromMalloc(buffer, fileSize)); |
| 562 | |
| 563 | if (fileStream->read(buffer, fileSize) != fileSize) { |
| 564 | return nullObjectReturn("Could not read the file."); |
| 565 | } |
| 566 | |
| 567 | stream.reset(new SkMemoryStream(data)); |
| 568 | } else { |
| 569 | // Use a buffered stream. Although an SkFILEStream can be rewound, this |
| 570 | // ensures that SkImageDecoder::Factory never rewinds beyond the |
| 571 | // current position of the file descriptor. |
| 572 | stream.reset(SkFrontBufferedStream::Create(fileStream, BYTES_TO_BUFFER)); |
| 573 | } |
| 574 | |
| 575 | return doDecode(env, stream, padding, bitmapFactoryOptions, isPurgeable); |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 576 | } |
| 577 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 578 | static jobject nativeDecodeAsset(JNIEnv* env, jobject clazz, jlong native_asset, |
Chris Craik | 905e824 | 2013-06-05 09:59:05 -0700 | [diff] [blame] | 579 | jobject padding, jobject options) { |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 580 | |
Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 581 | SkStreamRewindable* stream; |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 582 | Asset* asset = reinterpret_cast<Asset*>(native_asset); |
Romain Guy | 18ef3725 | 2010-08-19 15:10:54 -0700 | [diff] [blame] | 583 | bool forcePurgeable = optionsPurgeable(env, options); |
| 584 | if (forcePurgeable) { |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 585 | // if we could "ref/reopen" the asset, we may not need to copy it here |
| 586 | // and we could assume optionsShareable, since assets are always RO |
Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 587 | stream = CopyAssetToStream(asset); |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 588 | if (stream == NULL) { |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 589 | return NULL; |
| 590 | } |
| 591 | } else { |
| 592 | // since we know we'll be done with the asset when we return, we can |
| 593 | // just use a simple wrapper |
Leon Scroggins III | b9c58ab | 2013-12-03 15:10:04 -0500 | [diff] [blame] | 594 | stream = new AssetStreamAdaptor(asset, |
| 595 | AssetStreamAdaptor::kNo_OwnAsset, |
| 596 | AssetStreamAdaptor::kNo_HasMemoryBase); |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 597 | } |
| 598 | SkAutoUnref aur(stream); |
Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 599 | return doDecode(env, stream, padding, options, forcePurgeable, forcePurgeable); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | static jobject nativeDecodeByteArray(JNIEnv* env, jobject, jbyteArray byteArray, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 603 | jint offset, jint length, jobject options) { |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 604 | |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 605 | /* If optionsShareable() we could decide to just wrap the java array and |
| 606 | share it, but that means adding a globalref to the java array object |
| 607 | and managing its lifetime. For now we just always copy the array's data |
Bryan Mawhinney | 2a3d754 | 2010-11-03 17:23:57 +0000 | [diff] [blame] | 608 | if optionsPurgeable(), unless we're just decoding bounds. |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 609 | */ |
Romain Guy | 7b2f8b8 | 2012-03-19 17:18:54 -0700 | [diff] [blame] | 610 | bool purgeable = optionsPurgeable(env, options) && !optionsJustBounds(env, options); |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 611 | AutoJavaByteArray ar(env, byteArray); |
Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 612 | SkMemoryStream* stream = new SkMemoryStream(ar.ptr() + offset, length, purgeable); |
Mike Reed | c70e06b | 2009-04-24 11:09:12 -0400 | [diff] [blame] | 613 | SkAutoUnref aur(stream); |
Bryan Mawhinney | 2a3d754 | 2010-11-03 17:23:57 +0000 | [diff] [blame] | 614 | return doDecode(env, stream, NULL, options, purgeable); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | static void nativeRequestCancel(JNIEnv*, jobject joptions) { |
| 618 | (void)AutoDecoderCancel::RequestCancel(joptions); |
| 619 | } |
| 620 | |
Owen Lin | a9d0d47 | 2011-01-18 17:39:15 +0800 | [diff] [blame] | 621 | static jboolean nativeIsSeekable(JNIEnv* env, jobject, jobject fileDescriptor) { |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 622 | jint descriptor = jniGetFDFromFileDescriptor(env, fileDescriptor); |
Owen Lin | a9d0d47 | 2011-01-18 17:39:15 +0800 | [diff] [blame] | 623 | return ::lseek64(descriptor, 0, SEEK_CUR) != -1 ? JNI_TRUE : JNI_FALSE; |
| 624 | } |
| 625 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 626 | /////////////////////////////////////////////////////////////////////////////// |
| 627 | |
| 628 | static JNINativeMethod gMethods[] = { |
| 629 | { "nativeDecodeStream", |
| 630 | "(Ljava/io/InputStream;[BLandroid/graphics/Rect;Landroid/graphics/BitmapFactory$Options;)Landroid/graphics/Bitmap;", |
| 631 | (void*)nativeDecodeStream |
| 632 | }, |
| 633 | |
| 634 | { "nativeDecodeFileDescriptor", |
| 635 | "(Ljava/io/FileDescriptor;Landroid/graphics/Rect;Landroid/graphics/BitmapFactory$Options;)Landroid/graphics/Bitmap;", |
| 636 | (void*)nativeDecodeFileDescriptor |
| 637 | }, |
| 638 | |
| 639 | { "nativeDecodeAsset", |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 640 | "(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] | 641 | (void*)nativeDecodeAsset |
| 642 | }, |
| 643 | |
| 644 | { "nativeDecodeByteArray", |
| 645 | "([BIILandroid/graphics/BitmapFactory$Options;)Landroid/graphics/Bitmap;", |
| 646 | (void*)nativeDecodeByteArray |
| 647 | }, |
| 648 | |
Owen Lin | a9d0d47 | 2011-01-18 17:39:15 +0800 | [diff] [blame] | 649 | { "nativeIsSeekable", |
| 650 | "(Ljava/io/FileDescriptor;)Z", |
| 651 | (void*)nativeIsSeekable |
| 652 | }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 653 | }; |
| 654 | |
| 655 | static JNINativeMethod gOptionsMethods[] = { |
| 656 | { "requestCancel", "()V", (void*)nativeRequestCancel } |
| 657 | }; |
| 658 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 659 | static jfieldID getFieldIDCheck(JNIEnv* env, jclass clazz, |
| 660 | const char fieldname[], const char type[]) { |
| 661 | jfieldID id = env->GetFieldID(clazz, fieldname, type); |
| 662 | SkASSERT(id); |
| 663 | return id; |
| 664 | } |
| 665 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 666 | int register_android_graphics_BitmapFactory(JNIEnv* env) { |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 667 | jclass options_class = env->FindClass("android/graphics/BitmapFactory$Options"); |
| 668 | SkASSERT(options_class); |
| 669 | gOptions_bitmapFieldID = getFieldIDCheck(env, options_class, "inBitmap", |
Chet Haase | 37f74ca | 2010-12-08 17:56:36 -0800 | [diff] [blame] | 670 | "Landroid/graphics/Bitmap;"); |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 671 | gOptions_justBoundsFieldID = getFieldIDCheck(env, options_class, "inJustDecodeBounds", "Z"); |
| 672 | gOptions_sampleSizeFieldID = getFieldIDCheck(env, options_class, "inSampleSize", "I"); |
| 673 | gOptions_configFieldID = getFieldIDCheck(env, options_class, "inPreferredConfig", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 674 | "Landroid/graphics/Bitmap$Config;"); |
Chris Craik | 1abf5d6 | 2013-08-16 12:47:03 -0700 | [diff] [blame] | 675 | gOptions_premultipliedFieldID = getFieldIDCheck(env, options_class, "inPremultiplied", "Z"); |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 676 | gOptions_mutableFieldID = getFieldIDCheck(env, options_class, "inMutable", "Z"); |
| 677 | gOptions_ditherFieldID = getFieldIDCheck(env, options_class, "inDither", "Z"); |
| 678 | gOptions_purgeableFieldID = getFieldIDCheck(env, options_class, "inPurgeable", "Z"); |
| 679 | gOptions_shareableFieldID = getFieldIDCheck(env, options_class, "inInputShareable", "Z"); |
| 680 | gOptions_preferQualityOverSpeedFieldID = getFieldIDCheck(env, options_class, |
Wei-Ta Chen | 953f909 | 2010-12-03 14:06:18 -0800 | [diff] [blame] | 681 | "inPreferQualityOverSpeed", "Z"); |
Chris Craik | 905e824 | 2013-06-05 09:59:05 -0700 | [diff] [blame] | 682 | gOptions_scaledFieldID = getFieldIDCheck(env, options_class, "inScaled", "Z"); |
| 683 | gOptions_densityFieldID = getFieldIDCheck(env, options_class, "inDensity", "I"); |
| 684 | gOptions_screenDensityFieldID = getFieldIDCheck(env, options_class, "inScreenDensity", "I"); |
| 685 | gOptions_targetDensityFieldID = getFieldIDCheck(env, options_class, "inTargetDensity", "I"); |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 686 | gOptions_widthFieldID = getFieldIDCheck(env, options_class, "outWidth", "I"); |
| 687 | gOptions_heightFieldID = getFieldIDCheck(env, options_class, "outHeight", "I"); |
| 688 | gOptions_mimeFieldID = getFieldIDCheck(env, options_class, "outMimeType", "Ljava/lang/String;"); |
| 689 | gOptions_mCancelID = getFieldIDCheck(env, options_class, "mCancel", "Z"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 690 | |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 691 | jclass bitmap_class = env->FindClass("android/graphics/Bitmap"); |
| 692 | SkASSERT(bitmap_class); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 693 | gBitmap_nativeBitmapFieldID = getFieldIDCheck(env, bitmap_class, "mNativeBitmap", "J"); |
Amith Yamasani | ec4a504 | 2012-04-04 10:27:15 -0700 | [diff] [blame] | 694 | gBitmap_layoutBoundsFieldID = getFieldIDCheck(env, bitmap_class, "mLayoutBounds", "[I"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 695 | int ret = AndroidRuntime::registerNativeMethods(env, |
| 696 | "android/graphics/BitmapFactory$Options", |
| 697 | gOptionsMethods, |
| 698 | SK_ARRAY_COUNT(gOptionsMethods)); |
| 699 | if (ret) { |
| 700 | return ret; |
| 701 | } |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 702 | return android::AndroidRuntime::registerNativeMethods(env, "android/graphics/BitmapFactory", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 703 | gMethods, SK_ARRAY_COUNT(gMethods)); |
| 704 | } |