blob: 325f6d4615d7652f161a4896604d14f546ec3e5b [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001#define LOG_TAG "BitmapFactory"
2
Joseph Wenf1f48bc2010-07-19 16:59:51 +08003#include "BitmapFactory.h"
Ben Wagner60126ef2015-08-07 12:13:48 -04004#include "CreateJavaOutputStreamAdaptor.h"
5#include "GraphicsJNI.h"
Leon Scrogginsa06d86a2011-03-02 16:56:54 -05006#include "NinePatchPeeker.h"
Matt Sarettb8adc9a2015-12-02 13:35:22 -05007#include "SkAndroidCodec.h"
8#include "SkBRDAllocator.h"
Leon Scroggins III7315f1b2013-09-10 20:26:05 -04009#include "SkFrontBufferedStream.h"
Leon Scroggins46cb9bd2014-03-06 15:36:39 -050010#include "SkMath.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011#include "SkPixelRef.h"
12#include "SkStream.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013#include "SkUtils.h"
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080014#include "Utils.h"
Andreas Gampeed6b9df2014-11-20 22:02:20 -080015#include "core_jni_helpers.h"
Ben Wagner60126ef2015-08-07 12:13:48 -040016
Steven Moreland2279b252017-07-19 09:50:45 -070017#include <nativehelper/JNIHelp.h>
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080018#include <androidfw/Asset.h>
19#include <androidfw/ResourceTypes.h>
Chris Craikbd8db2e82014-08-20 16:31:57 -070020#include <cutils/compiler.h>
Ben Wagner60126ef2015-08-07 12:13:48 -040021#include <memory>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022#include <netinet/in.h>
Leon Scroggins III0102f8a2014-01-14 15:14:57 -050023#include <stdio.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024#include <sys/mman.h>
Joseph Wen2dcfbef2010-09-10 10:15:09 +080025#include <sys/stat.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026
Joseph Wenf1f48bc2010-07-19 16:59:51 +080027jfieldID gOptions_justBoundsFieldID;
28jfieldID gOptions_sampleSizeFieldID;
29jfieldID gOptions_configFieldID;
Romain Guy95648b82017-04-13 18:43:42 -070030jfieldID gOptions_colorSpaceFieldID;
Chris Craik1abf5d62013-08-16 12:47:03 -070031jfieldID gOptions_premultipliedFieldID;
Romain Guy23610982011-01-17 12:51:55 -080032jfieldID gOptions_mutableFieldID;
Joseph Wenf1f48bc2010-07-19 16:59:51 +080033jfieldID gOptions_ditherFieldID;
Wei-Ta Chen953f9092010-12-03 14:06:18 -080034jfieldID gOptions_preferQualityOverSpeedFieldID;
Chris Craik905e8242013-06-05 09:59:05 -070035jfieldID gOptions_scaledFieldID;
36jfieldID gOptions_densityFieldID;
37jfieldID gOptions_screenDensityFieldID;
38jfieldID gOptions_targetDensityFieldID;
Joseph Wenf1f48bc2010-07-19 16:59:51 +080039jfieldID gOptions_widthFieldID;
40jfieldID gOptions_heightFieldID;
41jfieldID gOptions_mimeFieldID;
Romain Guye8d2ebb2017-02-09 18:38:47 -080042jfieldID gOptions_outConfigFieldID;
Romain Guy90fc43b2017-03-30 12:35:26 -070043jfieldID gOptions_outColorSpaceFieldID;
Joseph Wenf1f48bc2010-07-19 16:59:51 +080044jfieldID gOptions_mCancelID;
Chet Haase37f74ca2010-12-08 17:56:36 -080045jfieldID gOptions_bitmapFieldID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046
Chris Craik47cd8e92014-07-08 17:13:08 -070047jfieldID gBitmap_ninePatchInsetsFieldID;
48
49jclass gInsetStruct_class;
50jmethodID gInsetStruct_constructorMethodID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051
Romain Guye8d2ebb2017-02-09 18:38:47 -080052jclass gBitmapConfig_class;
53jmethodID gBitmapConfig_nativeToConfigMethodID;
54
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055using namespace android;
56
Hal Canary10219fb2016-11-23 20:41:22 -050057jstring encodedFormatToString(JNIEnv* env, SkEncodedImageFormat format) {
Matt Sarettb8adc9a2015-12-02 13:35:22 -050058 const char* mimeType;
59 switch (format) {
Hal Canary10219fb2016-11-23 20:41:22 -050060 case SkEncodedImageFormat::kBMP:
Matt Sarettb8adc9a2015-12-02 13:35:22 -050061 mimeType = "image/bmp";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 break;
Hal Canary10219fb2016-11-23 20:41:22 -050063 case SkEncodedImageFormat::kGIF:
Matt Sarettb8adc9a2015-12-02 13:35:22 -050064 mimeType = "image/gif";
65 break;
Hal Canary10219fb2016-11-23 20:41:22 -050066 case SkEncodedImageFormat::kICO:
Matt Sarettb8adc9a2015-12-02 13:35:22 -050067 mimeType = "image/x-ico";
68 break;
Hal Canary10219fb2016-11-23 20:41:22 -050069 case SkEncodedImageFormat::kJPEG:
Matt Sarettb8adc9a2015-12-02 13:35:22 -050070 mimeType = "image/jpeg";
71 break;
Hal Canary10219fb2016-11-23 20:41:22 -050072 case SkEncodedImageFormat::kPNG:
Matt Sarettb8adc9a2015-12-02 13:35:22 -050073 mimeType = "image/png";
74 break;
Hal Canary10219fb2016-11-23 20:41:22 -050075 case SkEncodedImageFormat::kWEBP:
Matt Sarettb8adc9a2015-12-02 13:35:22 -050076 mimeType = "image/webp";
77 break;
Chong Zhang6e18cce2017-08-16 11:57:02 -070078 case SkEncodedImageFormat::kHEIF:
79 mimeType = "image/heif";
80 break;
Hal Canary10219fb2016-11-23 20:41:22 -050081 case SkEncodedImageFormat::kWBMP:
Matt Sarettb8adc9a2015-12-02 13:35:22 -050082 mimeType = "image/vnd.wap.wbmp";
83 break;
Hal Canary10219fb2016-11-23 20:41:22 -050084 case SkEncodedImageFormat::kDNG:
Yujie Qin990ea132016-03-17 14:13:22 +010085 mimeType = "image/x-adobe-dng";
86 break;
Matt Sarettb8adc9a2015-12-02 13:35:22 -050087 default:
88 mimeType = nullptr;
89 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 }
91
Vladimir Marko7ab249a2015-01-06 18:17:52 +000092 jstring jstr = nullptr;
Matt Sarettb8adc9a2015-12-02 13:35:22 -050093 if (mimeType) {
Vladimir Marko7ab249a2015-01-06 18:17:52 +000094 // NOTE: Caller should env->ExceptionCheck() for OOM
95 // (can't check for nullptr as it's a valid return value)
Matt Sarettb8adc9a2015-12-02 13:35:22 -050096 jstr = env->NewStringUTF(mimeType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 }
98 return jstr;
99}
100
Chris Craikbd8db2e82014-08-20 16:31:57 -0700101static void scaleDivRange(int32_t* divs, int count, float scale, int maxValue) {
102 for (int i = 0; i < count; i++) {
103 divs[i] = int32_t(divs[i] * scale + 0.5f);
104 if (i > 0 && divs[i] == divs[i - 1]) {
105 divs[i]++; // avoid collisions
106 }
107 }
108
109 if (CC_UNLIKELY(divs[count - 1] > maxValue)) {
110 // if the collision avoidance above put some divs outside the bounds of the bitmap,
111 // slide outer stretchable divs inward to stay within bounds
112 int highestAvailable = maxValue;
113 for (int i = count - 1; i >= 0; i--) {
114 divs[i] = highestAvailable;
115 if (i > 0 && divs[i] <= divs[i-1]){
116 // keep shifting
117 highestAvailable = divs[i] - 1;
118 } else {
119 break;
120 }
121 }
122 }
123}
124
125static void scaleNinePatchChunk(android::Res_png_9patch* chunk, float scale,
126 int scaledWidth, int scaledHeight) {
Romain Guy7b2f8b82012-03-19 17:18:54 -0700127 chunk->paddingLeft = int(chunk->paddingLeft * scale + 0.5f);
128 chunk->paddingTop = int(chunk->paddingTop * scale + 0.5f);
129 chunk->paddingRight = int(chunk->paddingRight * scale + 0.5f);
130 chunk->paddingBottom = int(chunk->paddingBottom * scale + 0.5f);
131
Chris Craikbd8db2e82014-08-20 16:31:57 -0700132 scaleDivRange(chunk->getXDivs(), chunk->numXDivs, scale, scaledWidth);
133 scaleDivRange(chunk->getYDivs(), chunk->numYDivs, scale, scaledHeight);
Romain Guy7b2f8b82012-03-19 17:18:54 -0700134}
135
Chris Craik905e8242013-06-05 09:59:05 -0700136class ScaleCheckingAllocator : public SkBitmap::HeapAllocator {
137public:
138 ScaleCheckingAllocator(float scale, int size)
139 : mScale(scale), mSize(size) {
140 }
141
Mike Reed81397c42017-07-18 17:04:16 -0400142 virtual bool allocPixelRef(SkBitmap* bitmap) {
Chris Craik905e8242013-06-05 09:59:05 -0700143 // accounts for scale in final allocation, using eventual size and config
Leon Scroggins IIIef691a3d2017-07-10 17:03:55 -0400144 const int bytesPerPixel = SkColorTypeBytesPerPixel(bitmap->colorType());
Chris Craik905e8242013-06-05 09:59:05 -0700145 const int requestedSize = bytesPerPixel *
146 int(bitmap->width() * mScale + 0.5f) *
147 int(bitmap->height() * mScale + 0.5f);
148 if (requestedSize > mSize) {
149 ALOGW("bitmap for alloc reuse (%d bytes) can't fit scaled bitmap (%d bytes)",
150 mSize, requestedSize);
151 return false;
152 }
Mike Reed81397c42017-07-18 17:04:16 -0400153 return SkBitmap::HeapAllocator::allocPixelRef(bitmap);
Chris Craik905e8242013-06-05 09:59:05 -0700154 }
155private:
156 const float mScale;
157 const int mSize;
158};
159
Chris Craik7e8c03c2013-06-03 13:53:36 -0700160class RecyclingPixelAllocator : public SkBitmap::Allocator {
161public:
sergeyvc1c54062016-10-19 18:47:26 -0700162 RecyclingPixelAllocator(android::Bitmap* bitmap, unsigned int size)
John Reckf29ed282015-04-07 07:32:03 -0700163 : mBitmap(bitmap), mSize(size) {
Chris Craik7e8c03c2013-06-03 13:53:36 -0700164 }
165
166 ~RecyclingPixelAllocator() {
Chris Craik7e8c03c2013-06-03 13:53:36 -0700167 }
168
Mike Reed81397c42017-07-18 17:04:16 -0400169 virtual bool allocPixelRef(SkBitmap* bitmap) {
Leon Scroggins46cb9bd2014-03-06 15:36:39 -0500170 const SkImageInfo& info = bitmap->info();
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400171 if (info.colorType() == kUnknown_SkColorType) {
Leon Scroggins46cb9bd2014-03-06 15:36:39 -0500172 ALOGW("unable to reuse a bitmap as the target has an unknown bitmap configuration");
Chris Craik7e8c03c2013-06-03 13:53:36 -0700173 return false;
174 }
Chris Craikcd0ba712013-09-06 14:40:30 -0700175
Leon Scroggins46cb9bd2014-03-06 15:36:39 -0500176 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 Albert46d84442014-11-18 16:07:51 -0800184 ALOGW("bitmap marked for reuse (%u bytes) can't fit new bitmap "
185 "(%zu bytes)", mSize, size);
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500186 return false;
187 }
188
Leon Scroggins IIIf51a80d2017-07-12 10:46:35 -0400189 mBitmap->reconfigure(info, bitmap->rowBytes());
Mike Reed826deef2017-04-04 15:32:04 -0400190 bitmap->setPixelRef(sk_ref_sp(mBitmap), 0, 0);
Chris Craik7e8c03c2013-06-03 13:53:36 -0700191 return true;
192 }
193
194private:
sergeyvc1c54062016-10-19 18:47:26 -0700195 android::Bitmap* const mBitmap;
Chris Craik7e8c03c2013-06-03 13:53:36 -0700196 const unsigned int mSize;
197};
198
Anton Daubert4e5ec342016-03-07 17:30:20 +0100199// Necessary for decodes when the native decoder cannot scale to appropriately match the sampleSize
200// (for example, RAW). If the sampleSize divides evenly into the dimension, we require that the
201// scale matches exactly. If sampleSize does not divide evenly, we allow the decoder to choose how
202// best to round.
203static bool needsFineScale(const int fullSize, const int decodedSize, const int sampleSize) {
204 if (fullSize % sampleSize == 0 && fullSize / sampleSize != decodedSize) {
205 return true;
206 } else if ((fullSize / sampleSize + 1) != decodedSize &&
207 (fullSize / sampleSize) != decodedSize) {
208 return true;
209 }
210 return false;
211}
212
213static bool needsFineScale(const SkISize fullSize, const SkISize decodedSize,
214 const int sampleSize) {
215 return needsFineScale(fullSize.width(), decodedSize.width(), sampleSize) ||
216 needsFineScale(fullSize.height(), decodedSize.height(), sampleSize);
217}
218
Chris Craik47cd8e92014-07-08 17:13:08 -0700219static jobject doDecode(JNIEnv* env, SkStreamRewindable* stream, jobject padding, jobject options) {
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500220 // This function takes ownership of the input stream. Since the SkAndroidCodec
221 // will take ownership of the stream, we don't necessarily need to take ownership
222 // here. This is a precaution - if we were to return before creating the codec,
223 // we need to make sure that we delete the stream.
224 std::unique_ptr<SkStreamRewindable> streamDeleter(stream);
Romain Guy7b2f8b82012-03-19 17:18:54 -0700225
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500226 // Set default values for the options parameters.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 int sampleSize = 1;
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500228 bool onlyDecodeSize = false;
Mike Reed42a1d082014-07-07 18:06:18 -0400229 SkColorType prefColorType = kN32_SkColorType;
sergeyvda6c8ffc2016-11-22 18:28:54 -0800230 bool isHardware = false;
Romain Guy23610982011-01-17 12:51:55 -0800231 bool isMutable = false;
Chris Craik905e8242013-06-05 09:59:05 -0700232 float scale = 1.0f;
Chris Craik1abf5d62013-08-16 12:47:03 -0700233 bool requireUnpremultiplied = false;
Chet Haase37f74ca2010-12-08 17:56:36 -0800234 jobject javaBitmap = NULL;
Romain Guy95648b82017-04-13 18:43:42 -0700235 sk_sp<SkColorSpace> prefColorSpace = nullptr;
Elliott Hughesa3804cf2011-04-11 16:50:19 -0700236
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500237 // Update with options supplied by the client.
Romain Guy7b2f8b82012-03-19 17:18:54 -0700238 if (options != NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 sampleSize = env->GetIntField(options, gOptions_sampleSizeFieldID);
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500240 // Correct a non-positive sampleSize. sampleSize defaults to zero within the
241 // options object, which is strange.
242 if (sampleSize <= 0) {
243 sampleSize = 1;
244 }
245
246 if (env->GetBooleanField(options, gOptions_justBoundsFieldID)) {
247 onlyDecodeSize = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 }
Romain Guy7b2f8b82012-03-19 17:18:54 -0700249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 // initialize these, in case we fail later on
251 env->SetIntField(options, gOptions_widthFieldID, -1);
252 env->SetIntField(options, gOptions_heightFieldID, -1);
253 env->SetObjectField(options, gOptions_mimeFieldID, 0);
Romain Guye8d2ebb2017-02-09 18:38:47 -0800254 env->SetObjectField(options, gOptions_outConfigFieldID, 0);
Romain Guy90fc43b2017-03-30 12:35:26 -0700255 env->SetObjectField(options, gOptions_outColorSpaceFieldID, 0);
Elliott Hughesa3804cf2011-04-11 16:50:19 -0700256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 jobject jconfig = env->GetObjectField(options, gOptions_configFieldID);
Mike Reed42a1d082014-07-07 18:06:18 -0400258 prefColorType = GraphicsJNI::getNativeBitmapColorType(env, jconfig);
Romain Guy95648b82017-04-13 18:43:42 -0700259 jobject jcolorSpace = env->GetObjectField(options, gOptions_colorSpaceFieldID);
260 prefColorSpace = GraphicsJNI::getNativeColorSpace(env, jcolorSpace);
sergeyvda6c8ffc2016-11-22 18:28:54 -0800261 isHardware = GraphicsJNI::isHardwareConfig(env, jconfig);
Romain Guy23610982011-01-17 12:51:55 -0800262 isMutable = env->GetBooleanField(options, gOptions_mutableFieldID);
Chris Craik1abf5d62013-08-16 12:47:03 -0700263 requireUnpremultiplied = !env->GetBooleanField(options, gOptions_premultipliedFieldID);
Chet Haase37f74ca2010-12-08 17:56:36 -0800264 javaBitmap = env->GetObjectField(options, gOptions_bitmapFieldID);
Chris Craik905e8242013-06-05 09:59:05 -0700265
266 if (env->GetBooleanField(options, gOptions_scaledFieldID)) {
267 const int density = env->GetIntField(options, gOptions_densityFieldID);
268 const int targetDensity = env->GetIntField(options, gOptions_targetDensityFieldID);
269 const int screenDensity = env->GetIntField(options, gOptions_screenDensityFieldID);
270 if (density != 0 && targetDensity != 0 && density != screenDensity) {
271 scale = (float) targetDensity / density;
272 }
273 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 }
Romain Guy7b2f8b82012-03-19 17:18:54 -0700275
sergeyv9fbb0b52016-11-23 10:27:33 -0800276 if (isMutable && isHardware) {
277 doThrowIAE(env, "Bitmaps with Config.HARWARE are always immutable");
278 return nullObjectReturn("Cannot create mutable hardware bitmap");
279 }
280
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500281 // Create the codec.
282 NinePatchPeeker peeker;
Romain Guy9505a652016-12-14 09:43:50 -0800283 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(
284 streamDeleter.release(), &peeker));
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500285 if (!codec.get()) {
286 return nullObjectReturn("SkAndroidCodec::NewFromStream returned null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 }
Elliott Hughesa3804cf2011-04-11 16:50:19 -0700288
Matt Sarett3b1b68d2015-12-14 13:08:33 -0500289 // Do not allow ninepatch decodes to 565. In the past, decodes to 565
290 // would dither, and we do not want to pre-dither ninepatches, since we
291 // know that they will be stretched. We no longer dither 565 decodes,
292 // but we continue to prevent ninepatches from decoding to 565, in order
293 // to maintain the old behavior.
294 if (peeker.mPatch && kRGB_565_SkColorType == prefColorType) {
295 prefColorType = kN32_SkColorType;
296 }
297
Anton Daubert4e5ec342016-03-07 17:30:20 +0100298 // Determine the output size.
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500299 SkISize size = codec->getSampledDimensions(sampleSize);
Anton Daubert4e5ec342016-03-07 17:30:20 +0100300
301 int scaledWidth = size.width();
302 int scaledHeight = size.height();
303 bool willScale = false;
304
305 // Apply a fine scaling step if necessary.
306 if (needsFineScale(codec->getInfo().dimensions(), size, sampleSize)) {
307 willScale = true;
308 scaledWidth = codec->getInfo().width() / sampleSize;
309 scaledHeight = codec->getInfo().height() / sampleSize;
310 }
311
Romain Guye8d2ebb2017-02-09 18:38:47 -0800312 // Set the decode colorType
313 SkColorType decodeColorType = codec->computeOutputColorType(prefColorType);
Romain Guy95648b82017-04-13 18:43:42 -0700314 sk_sp<SkColorSpace> decodeColorSpace = codec->computeOutputColorSpace(
315 decodeColorType, prefColorSpace);
Romain Guye8d2ebb2017-02-09 18:38:47 -0800316
Anton Daubert4e5ec342016-03-07 17:30:20 +0100317 // Set the options and return if the client only wants the size.
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500318 if (options != NULL) {
Hal Canary10219fb2016-11-23 20:41:22 -0500319 jstring mimeType = encodedFormatToString(
320 env, (SkEncodedImageFormat)codec->getEncodedFormat());
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500321 if (env->ExceptionCheck()) {
Matt Sarettd31512b2015-12-09 15:16:31 -0500322 return nullObjectReturn("OOM in encodedFormatToString()");
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500323 }
Anton Daubert4e5ec342016-03-07 17:30:20 +0100324 env->SetIntField(options, gOptions_widthFieldID, scaledWidth);
325 env->SetIntField(options, gOptions_heightFieldID, scaledHeight);
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500326 env->SetObjectField(options, gOptions_mimeFieldID, mimeType);
327
Leon Scroggins IIIef691a3d2017-07-10 17:03:55 -0400328 jint configID = GraphicsJNI::colorTypeToLegacyBitmapConfig(decodeColorType);
Romain Guye8d2ebb2017-02-09 18:38:47 -0800329 if (isHardware) {
330 configID = GraphicsJNI::kHardware_LegacyBitmapConfig;
331 }
332 jobject config = env->CallStaticObjectMethod(gBitmapConfig_class,
333 gBitmapConfig_nativeToConfigMethodID, configID);
334 env->SetObjectField(options, gOptions_outConfigFieldID, config);
335
Romain Guy90fc43b2017-03-30 12:35:26 -0700336 env->SetObjectField(options, gOptions_outColorSpaceFieldID,
Romain Guy95648b82017-04-13 18:43:42 -0700337 GraphicsJNI::getColorSpace(env, decodeColorSpace, decodeColorType));
Romain Guy90fc43b2017-03-30 12:35:26 -0700338
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500339 if (onlyDecodeSize) {
340 return nullptr;
341 }
342 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343
Anton Daubert4e5ec342016-03-07 17:30:20 +0100344 // Scale is necessary due to density differences.
345 if (scale != 1.0f) {
346 willScale = true;
347 scaledWidth = static_cast<int>(scaledWidth * scale + 0.5f);
348 scaledHeight = static_cast<int>(scaledHeight * scale + 0.5f);
349 }
350
sergeyvc1c54062016-10-19 18:47:26 -0700351 android::Bitmap* reuseBitmap = nullptr;
Chris Craik9f583612013-05-20 18:13:47 -0700352 unsigned int existingBufferSize = 0;
Chris Craik7e8c03c2013-06-03 13:53:36 -0700353 if (javaBitmap != NULL) {
sergeyvaed7f582016-10-14 16:30:21 -0700354 reuseBitmap = &bitmap::toBitmap(env, javaBitmap);
sergeyvc69853c2016-10-07 14:14:09 -0700355 if (reuseBitmap->isImmutable()) {
Derek Sollenberger2a6ecae2012-08-31 14:03:51 -0400356 ALOGW("Unable to reuse an immutable bitmap as an image decoder target.");
Chris Craik7e8c03c2013-06-03 13:53:36 -0700357 javaBitmap = NULL;
John Reckf29ed282015-04-07 07:32:03 -0700358 reuseBitmap = nullptr;
Chris Craik7e8c03c2013-06-03 13:53:36 -0700359 } else {
sergeyvc69853c2016-10-07 14:14:09 -0700360 existingBufferSize = bitmap::getBitmapAllocationByteCount(env, javaBitmap);
Derek Sollenberger2a6ecae2012-08-31 14:03:51 -0400361 }
Chet Haase37f74ca2010-12-08 17:56:36 -0800362 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363
sergeyv45082182016-09-29 18:25:40 -0700364 HeapAllocator defaultAllocator;
John Reckf29ed282015-04-07 07:32:03 -0700365 RecyclingPixelAllocator recyclingAllocator(reuseBitmap, existingBufferSize);
Chris Craik905e8242013-06-05 09:59:05 -0700366 ScaleCheckingAllocator scaleCheckingAllocator(scale, existingBufferSize);
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500367 SkBitmap::HeapAllocator heapAllocator;
368 SkBitmap::Allocator* decodeAllocator;
369 if (javaBitmap != nullptr && willScale) {
370 // This will allocate pixels using a HeapAllocator, since there will be an extra
371 // scaling step that copies these pixels into Java memory. This allocator
372 // also checks that the recycled javaBitmap is large enough.
373 decodeAllocator = &scaleCheckingAllocator;
374 } else if (javaBitmap != nullptr) {
375 decodeAllocator = &recyclingAllocator;
sergeyvda6c8ffc2016-11-22 18:28:54 -0800376 } else if (willScale || isHardware) {
377 // This will allocate pixels using a HeapAllocator,
378 // for scale case: there will be an extra scaling step.
379 // for hardware case: there will be extra swizzling & upload to gralloc step.
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500380 decodeAllocator = &heapAllocator;
381 } else {
sergeyv45082182016-09-29 18:25:40 -0700382 decodeAllocator = &defaultAllocator;
Chris Craik905e8242013-06-05 09:59:05 -0700383 }
384
Matt Sarett9e7cd632015-12-11 10:54:28 -0500385 SkAlphaType alphaType = codec->computeOutputAlphaType(requireUnpremultiplied);
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500386
Romain Guy253f2c22016-09-28 17:34:42 -0700387 const SkImageInfo decodeInfo = SkImageInfo::Make(size.width(), size.height(),
Romain Guy90fc43b2017-03-30 12:35:26 -0700388 decodeColorType, alphaType, decodeColorSpace);
Matt Sarett6c382572017-02-21 17:42:41 -0500389
390 // For wide gamut images, we will leave the color space on the SkBitmap. Otherwise,
391 // use the default.
Matt Sarett327c7202017-02-22 17:38:20 -0500392 SkImageInfo bitmapInfo = decodeInfo;
Matt Sarett2ecdfc22017-03-08 17:14:17 -0500393 if (decodeInfo.colorSpace() && decodeInfo.colorSpace()->isSRGB()) {
Matt Sarett6c382572017-02-21 17:42:41 -0500394 bitmapInfo = bitmapInfo.makeColorSpace(GraphicsJNI::colorSpaceForType(decodeColorType));
395 }
Matt Sarett33e37412016-12-21 12:17:17 -0500396
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500397 if (decodeColorType == kGray_8_SkColorType) {
398 // The legacy implementation of BitmapFactory used kAlpha8 for
399 // grayscale images (before kGray8 existed). While the codec
400 // recognizes kGray8, we need to decode into a kAlpha8 bitmap
401 // in order to avoid a behavior change.
Matt Sarettee80c472016-06-03 10:23:38 -0400402 bitmapInfo =
403 bitmapInfo.makeColorType(kAlpha_8_SkColorType).makeAlphaType(kPremul_SkAlphaType);
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500404 }
Chris Craik7e8c03c2013-06-03 13:53:36 -0700405 SkBitmap decodingBitmap;
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500406 if (!decodingBitmap.setInfo(bitmapInfo) ||
Mike Reed81397c42017-07-18 17:04:16 -0400407 !decodingBitmap.tryAllocPixels(decodeAllocator)) {
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500408 // SkAndroidCodec should recommend a valid SkImageInfo, so setInfo()
409 // should only only fail if the calculated value for rowBytes is too
410 // large.
411 // tryAllocPixels() can fail due to OOM on the Java heap, OOM on the
412 // native heap, or the recycled javaBitmap being too small to reuse.
413 return nullptr;
Mike Reedc70e06b2009-04-24 11:09:12 -0400414 }
415
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500416 // Use SkAndroidCodec to perform the decode.
417 SkAndroidCodec::AndroidOptions codecOptions;
Romain Guy253f2c22016-09-28 17:34:42 -0700418 codecOptions.fZeroInitialized = decodeAllocator == &defaultAllocator ?
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500419 SkCodec::kYes_ZeroInitialized : SkCodec::kNo_ZeroInitialized;
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500420 codecOptions.fSampleSize = sampleSize;
421 SkCodec::Result result = codec->getAndroidPixels(decodeInfo, decodingBitmap.getPixels(),
422 decodingBitmap.rowBytes(), &codecOptions);
423 switch (result) {
424 case SkCodec::kSuccess:
425 case SkCodec::kIncompleteInput:
426 break;
427 default:
Matt Sarett3b1b68d2015-12-14 13:08:33 -0500428 return nullObjectReturn("codec->getAndroidPixels() failed.");
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500429 }
430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 jbyteArray ninePatchChunk = NULL;
Chris Craik47cd8e92014-07-08 17:13:08 -0700432 if (peeker.mPatch != NULL) {
Romain Guy7b2f8b82012-03-19 17:18:54 -0700433 if (willScale) {
Chris Craikbd8db2e82014-08-20 16:31:57 -0700434 scaleNinePatchChunk(peeker.mPatch, scale, scaledWidth, scaledHeight);
Romain Guy7b2f8b82012-03-19 17:18:54 -0700435 }
436
Chris Craik47cd8e92014-07-08 17:13:08 -0700437 size_t ninePatchArraySize = peeker.mPatch->serializedSize();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 ninePatchChunk = env->NewByteArray(ninePatchArraySize);
Romain Guy7b2f8b82012-03-19 17:18:54 -0700439 if (ninePatchChunk == NULL) {
Mike Reedc70e06b2009-04-24 11:09:12 -0400440 return nullObjectReturn("ninePatchChunk == null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 }
Romain Guy7b2f8b82012-03-19 17:18:54 -0700442
443 jbyte* array = (jbyte*) env->GetPrimitiveArrayCritical(ninePatchChunk, NULL);
444 if (array == NULL) {
Mike Reedc70e06b2009-04-24 11:09:12 -0400445 return nullObjectReturn("primitive array == null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 }
Romain Guy7b2f8b82012-03-19 17:18:54 -0700447
Chris Craik47cd8e92014-07-08 17:13:08 -0700448 memcpy(array, peeker.mPatch, peeker.mPatchSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 env->ReleasePrimitiveArrayCritical(ninePatchChunk, array, 0);
450 }
451
Chris Craik47cd8e92014-07-08 17:13:08 -0700452 jobject ninePatchInsets = NULL;
453 if (peeker.mHasInsets) {
454 ninePatchInsets = env->NewObject(gInsetStruct_class, gInsetStruct_constructorMethodID,
Romain Guy253f2c22016-09-28 17:34:42 -0700455 peeker.mOpticalInsets[0], peeker.mOpticalInsets[1],
456 peeker.mOpticalInsets[2], peeker.mOpticalInsets[3],
457 peeker.mOutlineInsets[0], peeker.mOutlineInsets[1],
458 peeker.mOutlineInsets[2], peeker.mOutlineInsets[3],
Chris Craik77b5cad2014-07-30 18:23:07 -0700459 peeker.mOutlineRadius, peeker.mOutlineAlpha, scale);
Mathieu Chartiera08d10f2014-08-29 16:55:55 -0700460 if (ninePatchInsets == NULL) {
461 return nullObjectReturn("nine patch insets == null");
462 }
Amith Yamasaniec4a5042012-04-04 10:27:15 -0700463 if (javaBitmap != NULL) {
Chris Craik47cd8e92014-07-08 17:13:08 -0700464 env->SetObjectField(javaBitmap, gBitmap_ninePatchInsetsFieldID, ninePatchInsets);
Amith Yamasaniec4a5042012-04-04 10:27:15 -0700465 }
466 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467
John Reckf29ed282015-04-07 07:32:03 -0700468 SkBitmap outputBitmap;
Romain Guy7b2f8b82012-03-19 17:18:54 -0700469 if (willScale) {
470 // This is weird so let me explain: we could use the scale parameter
471 // directly, but for historical reasons this is how the corresponding
472 // Dalvik code has always behaved. We simply recreate the behavior here.
473 // The result is slightly different from simply using scale because of
474 // the 0.5f rounding bias applied when computing the target image size
Chris Craik7e8c03c2013-06-03 13:53:36 -0700475 const float sx = scaledWidth / float(decodingBitmap.width());
476 const float sy = scaledHeight / float(decodingBitmap.height());
Romain Guy7b2f8b82012-03-19 17:18:54 -0700477
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500478 // Set the allocator for the outputBitmap.
479 SkBitmap::Allocator* outputAllocator;
480 if (javaBitmap != nullptr) {
481 outputAllocator = &recyclingAllocator;
482 } else {
sergeyv45082182016-09-29 18:25:40 -0700483 outputAllocator = &defaultAllocator;
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500484 }
485
Leon Scroggins IIIef691a3d2017-07-10 17:03:55 -0400486 SkColorType scaledColorType = decodingBitmap.colorType();
Leon Scroggins III8790be62013-12-03 16:26:51 -0500487 // FIXME: If the alphaType is kUnpremul and the image has alpha, the
488 // colors may not be correct, since Skia does not yet support drawing
489 // to/from unpremultiplied bitmaps.
Matt Sarettee80c472016-06-03 10:23:38 -0400490 outputBitmap.setInfo(
491 bitmapInfo.makeWH(scaledWidth, scaledHeight).makeColorType(scaledColorType));
Mike Reed81397c42017-07-18 17:04:16 -0400492 if (!outputBitmap.tryAllocPixels(outputAllocator)) {
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500493 // This should only fail on OOM. The recyclingAllocator should have
494 // enough memory since we check this before decoding using the
495 // scaleCheckingAllocator.
Raph Levien005bfc62012-09-20 22:51:47 -0700496 return nullObjectReturn("allocation failed for scaled bitmap");
497 }
Leon Scroggins III1ffe7272013-09-19 11:34:06 -0400498
Romain Guy7b2f8b82012-03-19 17:18:54 -0700499 SkPaint paint;
Romain Guy253f2c22016-09-28 17:34:42 -0700500 // kSrc_Mode instructs us to overwrite the uninitialized pixels in
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500501 // outputBitmap. Otherwise we would blend by default, which is not
502 // what we want.
Mike Reed260ab722016-10-07 15:59:20 -0400503 paint.setBlendMode(SkBlendMode::kSrc);
Romain Guy253f2c22016-09-28 17:34:42 -0700504 paint.setFilterQuality(kLow_SkFilterQuality); // bilinear filtering
Romain Guy7b2f8b82012-03-19 17:18:54 -0700505
Matt Sarettca9b7032017-04-13 12:18:47 -0400506 SkCanvas canvas(outputBitmap, SkCanvas::ColorBehavior::kLegacy);
Romain Guy7b2f8b82012-03-19 17:18:54 -0700507 canvas.scale(sx, sy);
Chris Craik7e8c03c2013-06-03 13:53:36 -0700508 canvas.drawBitmap(decodingBitmap, 0.0f, 0.0f, &paint);
509 } else {
John Reckf29ed282015-04-07 07:32:03 -0700510 outputBitmap.swap(decodingBitmap);
Romain Guy7b2f8b82012-03-19 17:18:54 -0700511 }
512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 if (padding) {
Chris Craik47cd8e92014-07-08 17:13:08 -0700514 if (peeker.mPatch != NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 GraphicsJNI::set_jrect(env, padding,
Chris Craik47cd8e92014-07-08 17:13:08 -0700516 peeker.mPatch->paddingLeft, peeker.mPatch->paddingTop,
517 peeker.mPatch->paddingRight, peeker.mPatch->paddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 } else {
519 GraphicsJNI::set_jrect(env, padding, -1, -1, -1, -1);
520 }
521 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500523 // If we get here, the outputBitmap should have an installed pixelref.
John Reckf29ed282015-04-07 07:32:03 -0700524 if (outputBitmap.pixelRef() == NULL) {
Marco Nelissenb2fe3be2012-05-07 11:24:13 -0700525 return nullObjectReturn("Got null SkPixelRef");
526 }
Romain Guy23610982011-01-17 12:51:55 -0800527
Chris Craik7e8c03c2013-06-03 13:53:36 -0700528 if (!isMutable && javaBitmap == NULL) {
Romain Guy23610982011-01-17 12:51:55 -0800529 // promise we will never change our pixels (great for sharing and pictures)
John Reckf29ed282015-04-07 07:32:03 -0700530 outputBitmap.setImmutable();
Romain Guy23610982011-01-17 12:51:55 -0800531 }
Chet Haase37f74ca2010-12-08 17:56:36 -0800532
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500533 bool isPremultiplied = !requireUnpremultiplied;
534 if (javaBitmap != nullptr) {
sergeyvc69853c2016-10-07 14:14:09 -0700535 bitmap::reinitBitmap(env, javaBitmap, outputBitmap.info(), isPremultiplied);
John Reckf29ed282015-04-07 07:32:03 -0700536 outputBitmap.notifyPixelsChanged();
Chet Haase37f74ca2010-12-08 17:56:36 -0800537 // If a java bitmap was passed in for reuse, pass it back
538 return javaBitmap;
539 }
Chris Craik1abf5d62013-08-16 12:47:03 -0700540
541 int bitmapCreateFlags = 0x0;
sergeyvc69853c2016-10-07 14:14:09 -0700542 if (isMutable) bitmapCreateFlags |= android::bitmap::kBitmapCreateFlag_Mutable;
543 if (isPremultiplied) bitmapCreateFlags |= android::bitmap::kBitmapCreateFlag_Premultiplied;
Chris Craik1abf5d62013-08-16 12:47:03 -0700544
sergeyvda6c8ffc2016-11-22 18:28:54 -0800545 if (isHardware) {
546 sk_sp<Bitmap> hardwareBitmap = Bitmap::allocateHardwareBitmap(outputBitmap);
sergeyvd67bb1e2017-06-28 12:44:03 -0700547 if (!hardwareBitmap.get()) {
548 return nullObjectReturn("Failed to allocate a hardware bitmap");
549 }
sergeyvda6c8ffc2016-11-22 18:28:54 -0800550 return bitmap::createBitmap(env, hardwareBitmap.release(), bitmapCreateFlags,
551 ninePatchChunk, ninePatchInsets, -1);
552 }
553
Mike Reedc70e06b2009-04-24 11:09:12 -0400554 // now create the java bitmap
sergeyvc69853c2016-10-07 14:14:09 -0700555 return bitmap::createBitmap(env, defaultAllocator.getStorageObjAndReset(),
Chris Craik47cd8e92014-07-08 17:13:08 -0700556 bitmapCreateFlags, ninePatchChunk, ninePatchInsets, -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557}
558
Chris Craik905e8242013-06-05 09:59:05 -0700559static jobject nativeDecodeStream(JNIEnv* env, jobject clazz, jobject is, jbyteArray storage,
560 jobject padding, jobject options) {
Romain Guy7b2f8b82012-03-19 17:18:54 -0700561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 jobject bitmap = NULL;
Ben Wagner60126ef2015-08-07 12:13:48 -0400563 std::unique_ptr<SkStream> stream(CreateJavaInputStreamAdaptor(env, is, storage));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564
Leon Scroggins IIIca320212013-08-20 17:59:39 -0400565 if (stream.get()) {
Ben Wagner60126ef2015-08-07 12:13:48 -0400566 std::unique_ptr<SkStreamRewindable> bufferedStream(
Matt Sarett96ffbdc2016-02-11 17:03:54 -0500567 SkFrontBufferedStream::Create(stream.release(), SkCodec::MinBufferedBytesNeeded()));
Leon Scroggins III7315f1b2013-09-10 20:26:05 -0400568 SkASSERT(bufferedStream.get() != NULL);
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500569 bitmap = doDecode(env, bufferedStream.release(), padding, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 }
571 return bitmap;
572}
573
Romain Guy7b2f8b82012-03-19 17:18:54 -0700574static jobject nativeDecodeFileDescriptor(JNIEnv* env, jobject clazz, jobject fileDescriptor,
575 jobject padding, jobject bitmapFactoryOptions) {
576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 NPE_CHECK_RETURN_ZERO(env, fileDescriptor);
578
Derek Sollenberger5cb769d2014-09-24 09:20:09 -0400579 int descriptor = jniGetFDFromFileDescriptor(env, fileDescriptor);
Mike Reedc70e06b2009-04-24 11:09:12 -0400580
Derek Sollenberger5827cb52013-07-26 14:58:06 -0400581 struct stat fdStat;
582 if (fstat(descriptor, &fdStat) == -1) {
583 doThrowIOE(env, "broken file descriptor");
584 return nullObjectReturn("fstat return -1");
585 }
586
Derek Sollenberger5cb769d2014-09-24 09:20:09 -0400587 // Restore the descriptor's offset on exiting this function. Even though
588 // we dup the descriptor, both the original and dup refer to the same open
589 // file description and changes to the file offset in one impact the other.
Jérôme Poichetd29c9022014-09-26 18:59:11 +0000590 AutoFDSeek autoRestore(descriptor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591
Derek Sollenberger5cb769d2014-09-24 09:20:09 -0400592 // Duplicate the descriptor here to prevent leaking memory. A leak occurs
593 // if we only close the file descriptor and not the file object it is used to
594 // create. If we don't explicitly clean up the file (which in turn closes the
595 // descriptor) the buffers allocated internally by fseek will be leaked.
596 int dupDescriptor = dup(descriptor);
597
598 FILE* file = fdopen(dupDescriptor, "r");
Leon Scroggins III0102f8a2014-01-14 15:14:57 -0500599 if (file == NULL) {
Derek Sollenberger5cb769d2014-09-24 09:20:09 -0400600 // cleanup the duplicated descriptor since it will not be closed when the
601 // file is cleaned up (fclose).
602 close(dupDescriptor);
Leon Scroggins III0102f8a2014-01-14 15:14:57 -0500603 return nullObjectReturn("Could not open file");
Leon Scroggins IIIf65183f2013-10-07 16:32:14 -0400604 }
Leon Scroggins III0102f8a2014-01-14 15:14:57 -0500605
Leon Scroggins IIIdd3c06c2017-03-10 10:50:33 -0500606 std::unique_ptr<SkFILEStream> fileStream(new SkFILEStream(file));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607
Yujie Qinc1d7b7f2016-02-29 14:00:29 +0100608 // If there is no offset for the file descriptor, we use SkFILEStream directly.
609 if (::lseek(descriptor, 0, SEEK_CUR) == 0) {
610 assert(isSeekable(dupDescriptor));
611 return doDecode(env, fileStream.release(), padding, bitmapFactoryOptions);
612 }
613
Leon Scroggins III0aa39dc2014-06-03 12:19:32 -0400614 // Use a buffered stream. Although an SkFILEStream can be rewound, this
615 // ensures that SkImageDecoder::Factory never rewinds beyond the
616 // current position of the file descriptor.
Ben Wagner60126ef2015-08-07 12:13:48 -0400617 std::unique_ptr<SkStreamRewindable> stream(SkFrontBufferedStream::Create(fileStream.release(),
Matt Sarett96ffbdc2016-02-11 17:03:54 -0500618 SkCodec::MinBufferedBytesNeeded()));
Leon Scroggins III2826e5f2014-02-05 19:46:02 -0500619
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500620 return doDecode(env, stream.release(), padding, bitmapFactoryOptions);
Mike Reedc70e06b2009-04-24 11:09:12 -0400621}
622
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000623static jobject nativeDecodeAsset(JNIEnv* env, jobject clazz, jlong native_asset,
Chris Craik905e8242013-06-05 09:59:05 -0700624 jobject padding, jobject options) {
Romain Guy7b2f8b82012-03-19 17:18:54 -0700625
Mike Reedc70e06b2009-04-24 11:09:12 -0400626 Asset* asset = reinterpret_cast<Asset*>(native_asset);
Leon Scroggins III0aa39dc2014-06-03 12:19:32 -0400627 // since we know we'll be done with the asset when we return, we can
628 // just use a simple wrapper
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500629 std::unique_ptr<AssetStreamAdaptor> stream(new AssetStreamAdaptor(asset));
630 return doDecode(env, stream.release(), padding, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631}
632
633static jobject nativeDecodeByteArray(JNIEnv* env, jobject, jbyteArray byteArray,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000634 jint offset, jint length, jobject options) {
Romain Guy7b2f8b82012-03-19 17:18:54 -0700635
Mike Reedc70e06b2009-04-24 11:09:12 -0400636 AutoJavaByteArray ar(env, byteArray);
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500637 std::unique_ptr<SkMemoryStream> stream(new SkMemoryStream(ar.ptr() + offset, length, false));
638 return doDecode(env, stream.release(), NULL, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639}
640
Owen Lina9d0d472011-01-18 17:39:15 +0800641static jboolean nativeIsSeekable(JNIEnv* env, jobject, jobject fileDescriptor) {
Elliott Hughesa3804cf2011-04-11 16:50:19 -0700642 jint descriptor = jniGetFDFromFileDescriptor(env, fileDescriptor);
Yujie Qinc1d7b7f2016-02-29 14:00:29 +0100643 return isSeekable(descriptor) ? JNI_TRUE : JNI_FALSE;
Owen Lina9d0d472011-01-18 17:39:15 +0800644}
645
John Reck41478772015-04-10 13:35:27 -0700646jobject decodeBitmap(JNIEnv* env, void* data, size_t size) {
Ben Wagner60126ef2015-08-07 12:13:48 -0400647 SkMemoryStream stream(data, size);
John Reck41478772015-04-10 13:35:27 -0700648 return doDecode(env, &stream, NULL, NULL);
649}
650
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651///////////////////////////////////////////////////////////////////////////////
652
Daniel Micay76f6a862015-09-19 17:31:01 -0400653static const JNINativeMethod gMethods[] = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 { "nativeDecodeStream",
655 "(Ljava/io/InputStream;[BLandroid/graphics/Rect;Landroid/graphics/BitmapFactory$Options;)Landroid/graphics/Bitmap;",
656 (void*)nativeDecodeStream
657 },
658
659 { "nativeDecodeFileDescriptor",
660 "(Ljava/io/FileDescriptor;Landroid/graphics/Rect;Landroid/graphics/BitmapFactory$Options;)Landroid/graphics/Bitmap;",
661 (void*)nativeDecodeFileDescriptor
662 },
663
664 { "nativeDecodeAsset",
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000665 "(JLandroid/graphics/Rect;Landroid/graphics/BitmapFactory$Options;)Landroid/graphics/Bitmap;",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 (void*)nativeDecodeAsset
667 },
668
669 { "nativeDecodeByteArray",
670 "([BIILandroid/graphics/BitmapFactory$Options;)Landroid/graphics/Bitmap;",
671 (void*)nativeDecodeByteArray
672 },
673
Owen Lina9d0d472011-01-18 17:39:15 +0800674 { "nativeIsSeekable",
675 "(Ljava/io/FileDescriptor;)Z",
676 (void*)nativeIsSeekable
677 },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678};
679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680int register_android_graphics_BitmapFactory(JNIEnv* env) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800681 jclass options_class = FindClassOrDie(env, "android/graphics/BitmapFactory$Options");
682 gOptions_bitmapFieldID = GetFieldIDOrDie(env, options_class, "inBitmap",
Chris Craik47cd8e92014-07-08 17:13:08 -0700683 "Landroid/graphics/Bitmap;");
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800684 gOptions_justBoundsFieldID = GetFieldIDOrDie(env, options_class, "inJustDecodeBounds", "Z");
685 gOptions_sampleSizeFieldID = GetFieldIDOrDie(env, options_class, "inSampleSize", "I");
686 gOptions_configFieldID = GetFieldIDOrDie(env, options_class, "inPreferredConfig",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 "Landroid/graphics/Bitmap$Config;");
Romain Guy95648b82017-04-13 18:43:42 -0700688 gOptions_colorSpaceFieldID = GetFieldIDOrDie(env, options_class, "inPreferredColorSpace",
689 "Landroid/graphics/ColorSpace;");
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800690 gOptions_premultipliedFieldID = GetFieldIDOrDie(env, options_class, "inPremultiplied", "Z");
691 gOptions_mutableFieldID = GetFieldIDOrDie(env, options_class, "inMutable", "Z");
692 gOptions_ditherFieldID = GetFieldIDOrDie(env, options_class, "inDither", "Z");
693 gOptions_preferQualityOverSpeedFieldID = GetFieldIDOrDie(env, options_class,
Wei-Ta Chen953f9092010-12-03 14:06:18 -0800694 "inPreferQualityOverSpeed", "Z");
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800695 gOptions_scaledFieldID = GetFieldIDOrDie(env, options_class, "inScaled", "Z");
696 gOptions_densityFieldID = GetFieldIDOrDie(env, options_class, "inDensity", "I");
697 gOptions_screenDensityFieldID = GetFieldIDOrDie(env, options_class, "inScreenDensity", "I");
698 gOptions_targetDensityFieldID = GetFieldIDOrDie(env, options_class, "inTargetDensity", "I");
699 gOptions_widthFieldID = GetFieldIDOrDie(env, options_class, "outWidth", "I");
700 gOptions_heightFieldID = GetFieldIDOrDie(env, options_class, "outHeight", "I");
701 gOptions_mimeFieldID = GetFieldIDOrDie(env, options_class, "outMimeType", "Ljava/lang/String;");
Romain Guye8d2ebb2017-02-09 18:38:47 -0800702 gOptions_outConfigFieldID = GetFieldIDOrDie(env, options_class, "outConfig",
703 "Landroid/graphics/Bitmap$Config;");
Romain Guy90fc43b2017-03-30 12:35:26 -0700704 gOptions_outColorSpaceFieldID = GetFieldIDOrDie(env, options_class, "outColorSpace",
705 "Landroid/graphics/ColorSpace;");
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800706 gOptions_mCancelID = GetFieldIDOrDie(env, options_class, "mCancel", "Z");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800708 jclass bitmap_class = FindClassOrDie(env, "android/graphics/Bitmap");
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800709 gBitmap_ninePatchInsetsFieldID = GetFieldIDOrDie(env, bitmap_class, "mNinePatchInsets",
Chris Craik47cd8e92014-07-08 17:13:08 -0700710 "Landroid/graphics/NinePatch$InsetStruct;");
711
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800712 gInsetStruct_class = MakeGlobalRefOrDie(env, FindClassOrDie(env,
713 "android/graphics/NinePatch$InsetStruct"));
714 gInsetStruct_constructorMethodID = GetMethodIDOrDie(env, gInsetStruct_class, "<init>",
715 "(IIIIIIIIFIF)V");
Chris Craik47cd8e92014-07-08 17:13:08 -0700716
Romain Guye8d2ebb2017-02-09 18:38:47 -0800717 gBitmapConfig_class = MakeGlobalRefOrDie(env, FindClassOrDie(env,
718 "android/graphics/Bitmap$Config"));
719 gBitmapConfig_nativeToConfigMethodID = GetStaticMethodIDOrDie(env, gBitmapConfig_class,
720 "nativeToConfig", "(I)Landroid/graphics/Bitmap$Config;");
721
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800722 return android::RegisterMethodsOrDie(env, "android/graphics/BitmapFactory",
723 gMethods, NELEM(gMethods));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724}