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