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