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