blob: 3f9ec452749fa1b757b8b440bf5d932a055fa5c1 [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"
Mike Reedc7c65602017-07-26 10:40:25 -040010#include "SkMakeUnique.h"
Leon Scroggins46cb9bd2014-03-06 15:36:39 -050011#include "SkMath.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012#include "SkPixelRef.h"
13#include "SkStream.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014#include "SkUtils.h"
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080015#include "Utils.h"
Andreas Gampeed6b9df2014-11-20 22:02:20 -080016#include "core_jni_helpers.h"
Ben Wagner60126ef2015-08-07 12:13:48 -040017
Steven Moreland2279b252017-07-19 09:50:45 -070018#include <nativehelper/JNIHelp.h>
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080019#include <androidfw/Asset.h>
20#include <androidfw/ResourceTypes.h>
Chris Craikbd8db2e82014-08-20 16:31:57 -070021#include <cutils/compiler.h>
Ben Wagner60126ef2015-08-07 12:13:48 -040022#include <memory>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023#include <netinet/in.h>
Leon Scroggins III0102f8a2014-01-14 15:14:57 -050024#include <stdio.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025#include <sys/mman.h>
Joseph Wen2dcfbef2010-09-10 10:15:09 +080026#include <sys/stat.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027
Joseph Wenf1f48bc2010-07-19 16:59:51 +080028jfieldID gOptions_justBoundsFieldID;
29jfieldID gOptions_sampleSizeFieldID;
30jfieldID gOptions_configFieldID;
Romain Guy95648b82017-04-13 18:43:42 -070031jfieldID gOptions_colorSpaceFieldID;
Chris Craik1abf5d62013-08-16 12:47:03 -070032jfieldID gOptions_premultipliedFieldID;
Romain Guy23610982011-01-17 12:51:55 -080033jfieldID gOptions_mutableFieldID;
Joseph Wenf1f48bc2010-07-19 16:59:51 +080034jfieldID gOptions_ditherFieldID;
Wei-Ta Chen953f9092010-12-03 14:06:18 -080035jfieldID gOptions_preferQualityOverSpeedFieldID;
Chris Craik905e8242013-06-05 09:59:05 -070036jfieldID gOptions_scaledFieldID;
37jfieldID gOptions_densityFieldID;
38jfieldID gOptions_screenDensityFieldID;
39jfieldID gOptions_targetDensityFieldID;
Joseph Wenf1f48bc2010-07-19 16:59:51 +080040jfieldID gOptions_widthFieldID;
41jfieldID gOptions_heightFieldID;
42jfieldID gOptions_mimeFieldID;
Romain Guye8d2ebb2017-02-09 18:38:47 -080043jfieldID gOptions_outConfigFieldID;
Romain Guy90fc43b2017-03-30 12:35:26 -070044jfieldID gOptions_outColorSpaceFieldID;
Joseph Wenf1f48bc2010-07-19 16:59:51 +080045jfieldID gOptions_mCancelID;
Chet Haase37f74ca2010-12-08 17:56:36 -080046jfieldID gOptions_bitmapFieldID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047
Chris Craik47cd8e92014-07-08 17:13:08 -070048jfieldID gBitmap_ninePatchInsetsFieldID;
49
Romain Guye8d2ebb2017-02-09 18:38:47 -080050jclass gBitmapConfig_class;
51jmethodID gBitmapConfig_nativeToConfigMethodID;
52
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053using namespace android;
54
Hal Canary10219fb2016-11-23 20:41:22 -050055jstring encodedFormatToString(JNIEnv* env, SkEncodedImageFormat format) {
Matt Sarettb8adc9a2015-12-02 13:35:22 -050056 const char* mimeType;
57 switch (format) {
Hal Canary10219fb2016-11-23 20:41:22 -050058 case SkEncodedImageFormat::kBMP:
Matt Sarettb8adc9a2015-12-02 13:35:22 -050059 mimeType = "image/bmp";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 break;
Hal Canary10219fb2016-11-23 20:41:22 -050061 case SkEncodedImageFormat::kGIF:
Matt Sarettb8adc9a2015-12-02 13:35:22 -050062 mimeType = "image/gif";
63 break;
Hal Canary10219fb2016-11-23 20:41:22 -050064 case SkEncodedImageFormat::kICO:
Matt Sarettb8adc9a2015-12-02 13:35:22 -050065 mimeType = "image/x-ico";
66 break;
Hal Canary10219fb2016-11-23 20:41:22 -050067 case SkEncodedImageFormat::kJPEG:
Matt Sarettb8adc9a2015-12-02 13:35:22 -050068 mimeType = "image/jpeg";
69 break;
Hal Canary10219fb2016-11-23 20:41:22 -050070 case SkEncodedImageFormat::kPNG:
Matt Sarettb8adc9a2015-12-02 13:35:22 -050071 mimeType = "image/png";
72 break;
Hal Canary10219fb2016-11-23 20:41:22 -050073 case SkEncodedImageFormat::kWEBP:
Matt Sarettb8adc9a2015-12-02 13:35:22 -050074 mimeType = "image/webp";
75 break;
Chong Zhang48fa8902017-08-16 11:57:02 -070076 case SkEncodedImageFormat::kHEIF:
77 mimeType = "image/heif";
78 break;
Hal Canary10219fb2016-11-23 20:41:22 -050079 case SkEncodedImageFormat::kWBMP:
Matt Sarettb8adc9a2015-12-02 13:35:22 -050080 mimeType = "image/vnd.wap.wbmp";
81 break;
Hal Canary10219fb2016-11-23 20:41:22 -050082 case SkEncodedImageFormat::kDNG:
Yujie Qin990ea132016-03-17 14:13:22 +010083 mimeType = "image/x-adobe-dng";
84 break;
Matt Sarettb8adc9a2015-12-02 13:35:22 -050085 default:
86 mimeType = nullptr;
87 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 }
89
Vladimir Marko7ab249a2015-01-06 18:17:52 +000090 jstring jstr = nullptr;
Matt Sarettb8adc9a2015-12-02 13:35:22 -050091 if (mimeType) {
Vladimir Marko7ab249a2015-01-06 18:17:52 +000092 // NOTE: Caller should env->ExceptionCheck() for OOM
93 // (can't check for nullptr as it's a valid return value)
Matt Sarettb8adc9a2015-12-02 13:35:22 -050094 jstr = env->NewStringUTF(mimeType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 }
96 return jstr;
97}
98
Chris Craik905e8242013-06-05 09:59:05 -070099class ScaleCheckingAllocator : public SkBitmap::HeapAllocator {
100public:
101 ScaleCheckingAllocator(float scale, int size)
102 : mScale(scale), mSize(size) {
103 }
104
Mike Reed81397c42017-07-18 17:04:16 -0400105 virtual bool allocPixelRef(SkBitmap* bitmap) {
Chris Craik905e8242013-06-05 09:59:05 -0700106 // accounts for scale in final allocation, using eventual size and config
Leon Scroggins IIIef691a3d2017-07-10 17:03:55 -0400107 const int bytesPerPixel = SkColorTypeBytesPerPixel(bitmap->colorType());
Chris Craik905e8242013-06-05 09:59:05 -0700108 const int requestedSize = bytesPerPixel *
109 int(bitmap->width() * mScale + 0.5f) *
110 int(bitmap->height() * mScale + 0.5f);
111 if (requestedSize > mSize) {
112 ALOGW("bitmap for alloc reuse (%d bytes) can't fit scaled bitmap (%d bytes)",
113 mSize, requestedSize);
114 return false;
115 }
Mike Reed81397c42017-07-18 17:04:16 -0400116 return SkBitmap::HeapAllocator::allocPixelRef(bitmap);
Chris Craik905e8242013-06-05 09:59:05 -0700117 }
118private:
119 const float mScale;
120 const int mSize;
121};
122
Chris Craik7e8c03c2013-06-03 13:53:36 -0700123class RecyclingPixelAllocator : public SkBitmap::Allocator {
124public:
sergeyvc1c54062016-10-19 18:47:26 -0700125 RecyclingPixelAllocator(android::Bitmap* bitmap, unsigned int size)
John Reckf29ed282015-04-07 07:32:03 -0700126 : mBitmap(bitmap), mSize(size) {
Chris Craik7e8c03c2013-06-03 13:53:36 -0700127 }
128
129 ~RecyclingPixelAllocator() {
Chris Craik7e8c03c2013-06-03 13:53:36 -0700130 }
131
Mike Reed81397c42017-07-18 17:04:16 -0400132 virtual bool allocPixelRef(SkBitmap* bitmap) {
Leon Scroggins46cb9bd2014-03-06 15:36:39 -0500133 const SkImageInfo& info = bitmap->info();
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400134 if (info.colorType() == kUnknown_SkColorType) {
Leon Scroggins46cb9bd2014-03-06 15:36:39 -0500135 ALOGW("unable to reuse a bitmap as the target has an unknown bitmap configuration");
Chris Craik7e8c03c2013-06-03 13:53:36 -0700136 return false;
137 }
Chris Craikcd0ba712013-09-06 14:40:30 -0700138
Mike Reed7569de02017-10-06 16:25:49 -0400139 const size_t size = info.computeByteSize(bitmap->rowBytes());
140 if (size > SK_MaxS32) {
Leon Scroggins46cb9bd2014-03-06 15:36:39 -0500141 ALOGW("bitmap is too large");
142 return false;
143 }
144
Leon Scroggins46cb9bd2014-03-06 15:36:39 -0500145 if (size > mSize) {
Dan Albert46d84442014-11-18 16:07:51 -0800146 ALOGW("bitmap marked for reuse (%u bytes) can't fit new bitmap "
147 "(%zu bytes)", mSize, size);
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500148 return false;
149 }
150
Leon Scroggins IIIf51a80d2017-07-12 10:46:35 -0400151 mBitmap->reconfigure(info, bitmap->rowBytes());
Mike Reed826deef2017-04-04 15:32:04 -0400152 bitmap->setPixelRef(sk_ref_sp(mBitmap), 0, 0);
Chris Craik7e8c03c2013-06-03 13:53:36 -0700153 return true;
154 }
155
156private:
sergeyvc1c54062016-10-19 18:47:26 -0700157 android::Bitmap* const mBitmap;
Chris Craik7e8c03c2013-06-03 13:53:36 -0700158 const unsigned int mSize;
159};
160
Anton Daubert4e5ec342016-03-07 17:30:20 +0100161// Necessary for decodes when the native decoder cannot scale to appropriately match the sampleSize
162// (for example, RAW). If the sampleSize divides evenly into the dimension, we require that the
163// scale matches exactly. If sampleSize does not divide evenly, we allow the decoder to choose how
164// best to round.
165static bool needsFineScale(const int fullSize, const int decodedSize, const int sampleSize) {
166 if (fullSize % sampleSize == 0 && fullSize / sampleSize != decodedSize) {
167 return true;
168 } else if ((fullSize / sampleSize + 1) != decodedSize &&
169 (fullSize / sampleSize) != decodedSize) {
170 return true;
171 }
172 return false;
173}
174
175static bool needsFineScale(const SkISize fullSize, const SkISize decodedSize,
176 const int sampleSize) {
177 return needsFineScale(fullSize.width(), decodedSize.width(), sampleSize) ||
178 needsFineScale(fullSize.height(), decodedSize.height(), sampleSize);
179}
180
Mike Reedc7c65602017-07-26 10:40:25 -0400181static jobject doDecode(JNIEnv* env, std::unique_ptr<SkStreamRewindable> stream,
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500182 jobject padding, jobject options, jlong colorSpaceHandle) {
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500183 // Set default values for the options parameters.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 int sampleSize = 1;
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500185 bool onlyDecodeSize = false;
Mike Reed42a1d082014-07-07 18:06:18 -0400186 SkColorType prefColorType = kN32_SkColorType;
sergeyvda6c8ffc2016-11-22 18:28:54 -0800187 bool isHardware = false;
Romain Guy23610982011-01-17 12:51:55 -0800188 bool isMutable = false;
Chris Craik905e8242013-06-05 09:59:05 -0700189 float scale = 1.0f;
Chris Craik1abf5d62013-08-16 12:47:03 -0700190 bool requireUnpremultiplied = false;
Chet Haase37f74ca2010-12-08 17:56:36 -0800191 jobject javaBitmap = NULL;
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500192 sk_sp<SkColorSpace> prefColorSpace = GraphicsJNI::getNativeColorSpace(colorSpaceHandle);
Elliott Hughesa3804cf2011-04-11 16:50:19 -0700193
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500194 // Update with options supplied by the client.
Romain Guy7b2f8b82012-03-19 17:18:54 -0700195 if (options != NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 sampleSize = env->GetIntField(options, gOptions_sampleSizeFieldID);
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500197 // Correct a non-positive sampleSize. sampleSize defaults to zero within the
198 // options object, which is strange.
199 if (sampleSize <= 0) {
200 sampleSize = 1;
201 }
202
203 if (env->GetBooleanField(options, gOptions_justBoundsFieldID)) {
204 onlyDecodeSize = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 }
Romain Guy7b2f8b82012-03-19 17:18:54 -0700206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 // initialize these, in case we fail later on
208 env->SetIntField(options, gOptions_widthFieldID, -1);
209 env->SetIntField(options, gOptions_heightFieldID, -1);
210 env->SetObjectField(options, gOptions_mimeFieldID, 0);
Romain Guye8d2ebb2017-02-09 18:38:47 -0800211 env->SetObjectField(options, gOptions_outConfigFieldID, 0);
Romain Guy90fc43b2017-03-30 12:35:26 -0700212 env->SetObjectField(options, gOptions_outColorSpaceFieldID, 0);
Elliott Hughesa3804cf2011-04-11 16:50:19 -0700213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 jobject jconfig = env->GetObjectField(options, gOptions_configFieldID);
Mike Reed42a1d082014-07-07 18:06:18 -0400215 prefColorType = GraphicsJNI::getNativeBitmapColorType(env, jconfig);
sergeyvda6c8ffc2016-11-22 18:28:54 -0800216 isHardware = GraphicsJNI::isHardwareConfig(env, jconfig);
Romain Guy23610982011-01-17 12:51:55 -0800217 isMutable = env->GetBooleanField(options, gOptions_mutableFieldID);
Chris Craik1abf5d62013-08-16 12:47:03 -0700218 requireUnpremultiplied = !env->GetBooleanField(options, gOptions_premultipliedFieldID);
Chet Haase37f74ca2010-12-08 17:56:36 -0800219 javaBitmap = env->GetObjectField(options, gOptions_bitmapFieldID);
Chris Craik905e8242013-06-05 09:59:05 -0700220
221 if (env->GetBooleanField(options, gOptions_scaledFieldID)) {
222 const int density = env->GetIntField(options, gOptions_densityFieldID);
223 const int targetDensity = env->GetIntField(options, gOptions_targetDensityFieldID);
224 const int screenDensity = env->GetIntField(options, gOptions_screenDensityFieldID);
225 if (density != 0 && targetDensity != 0 && density != screenDensity) {
226 scale = (float) targetDensity / density;
227 }
228 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 }
Romain Guy7b2f8b82012-03-19 17:18:54 -0700230
sergeyv9fbb0b52016-11-23 10:27:33 -0800231 if (isMutable && isHardware) {
Hans Boehme5b337d2019-01-07 17:42:05 -0800232 doThrowIAE(env, "Bitmaps with Config.HARDWARE are always immutable");
sergeyv9fbb0b52016-11-23 10:27:33 -0800233 return nullObjectReturn("Cannot create mutable hardware bitmap");
234 }
235
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500236 // Create the codec.
237 NinePatchPeeker peeker;
Leon Scroggins IIIc782ad82018-01-14 10:50:45 -0500238 std::unique_ptr<SkAndroidCodec> codec;
239 {
240 SkCodec::Result result;
241 std::unique_ptr<SkCodec> c = SkCodec::MakeFromStream(std::move(stream), &result,
242 &peeker);
243 if (!c) {
244 SkString msg;
245 msg.printf("Failed to create image decoder with message '%s'",
246 SkCodec::ResultToString(result));
247 return nullObjectReturn(msg.c_str());
248 }
249
250 codec = SkAndroidCodec::MakeFromCodec(std::move(c));
251 if (!codec) {
252 return nullObjectReturn("SkAndroidCodec::MakeFromCodec returned null");
253 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 }
Elliott Hughesa3804cf2011-04-11 16:50:19 -0700255
Matt Sarett3b1b68d2015-12-14 13:08:33 -0500256 // Do not allow ninepatch decodes to 565. In the past, decodes to 565
257 // would dither, and we do not want to pre-dither ninepatches, since we
258 // know that they will be stretched. We no longer dither 565 decodes,
259 // but we continue to prevent ninepatches from decoding to 565, in order
260 // to maintain the old behavior.
261 if (peeker.mPatch && kRGB_565_SkColorType == prefColorType) {
262 prefColorType = kN32_SkColorType;
263 }
264
Anton Daubert4e5ec342016-03-07 17:30:20 +0100265 // Determine the output size.
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500266 SkISize size = codec->getSampledDimensions(sampleSize);
Anton Daubert4e5ec342016-03-07 17:30:20 +0100267
268 int scaledWidth = size.width();
269 int scaledHeight = size.height();
270 bool willScale = false;
271
272 // Apply a fine scaling step if necessary.
273 if (needsFineScale(codec->getInfo().dimensions(), size, sampleSize)) {
274 willScale = true;
275 scaledWidth = codec->getInfo().width() / sampleSize;
276 scaledHeight = codec->getInfo().height() / sampleSize;
277 }
278
Romain Guye8d2ebb2017-02-09 18:38:47 -0800279 // Set the decode colorType
280 SkColorType decodeColorType = codec->computeOutputColorType(prefColorType);
Romain Guy95648b82017-04-13 18:43:42 -0700281 sk_sp<SkColorSpace> decodeColorSpace = codec->computeOutputColorSpace(
282 decodeColorType, prefColorSpace);
Romain Guye8d2ebb2017-02-09 18:38:47 -0800283
Anton Daubert4e5ec342016-03-07 17:30:20 +0100284 // Set the options and return if the client only wants the size.
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500285 if (options != NULL) {
Hal Canary10219fb2016-11-23 20:41:22 -0500286 jstring mimeType = encodedFormatToString(
287 env, (SkEncodedImageFormat)codec->getEncodedFormat());
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500288 if (env->ExceptionCheck()) {
Matt Sarettd31512b2015-12-09 15:16:31 -0500289 return nullObjectReturn("OOM in encodedFormatToString()");
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500290 }
Anton Daubert4e5ec342016-03-07 17:30:20 +0100291 env->SetIntField(options, gOptions_widthFieldID, scaledWidth);
292 env->SetIntField(options, gOptions_heightFieldID, scaledHeight);
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500293 env->SetObjectField(options, gOptions_mimeFieldID, mimeType);
294
Leon Scroggins IIIef691a3d2017-07-10 17:03:55 -0400295 jint configID = GraphicsJNI::colorTypeToLegacyBitmapConfig(decodeColorType);
Romain Guye8d2ebb2017-02-09 18:38:47 -0800296 if (isHardware) {
297 configID = GraphicsJNI::kHardware_LegacyBitmapConfig;
298 }
299 jobject config = env->CallStaticObjectMethod(gBitmapConfig_class,
300 gBitmapConfig_nativeToConfigMethodID, configID);
301 env->SetObjectField(options, gOptions_outConfigFieldID, config);
302
Romain Guy90fc43b2017-03-30 12:35:26 -0700303 env->SetObjectField(options, gOptions_outColorSpaceFieldID,
Romain Guy95648b82017-04-13 18:43:42 -0700304 GraphicsJNI::getColorSpace(env, decodeColorSpace, decodeColorType));
Romain Guy90fc43b2017-03-30 12:35:26 -0700305
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500306 if (onlyDecodeSize) {
307 return nullptr;
308 }
309 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310
Anton Daubert4e5ec342016-03-07 17:30:20 +0100311 // Scale is necessary due to density differences.
312 if (scale != 1.0f) {
313 willScale = true;
314 scaledWidth = static_cast<int>(scaledWidth * scale + 0.5f);
315 scaledHeight = static_cast<int>(scaledHeight * scale + 0.5f);
316 }
317
sergeyvc1c54062016-10-19 18:47:26 -0700318 android::Bitmap* reuseBitmap = nullptr;
Chris Craik9f583612013-05-20 18:13:47 -0700319 unsigned int existingBufferSize = 0;
Chris Craik7e8c03c2013-06-03 13:53:36 -0700320 if (javaBitmap != NULL) {
sergeyvaed7f582016-10-14 16:30:21 -0700321 reuseBitmap = &bitmap::toBitmap(env, javaBitmap);
sergeyvc69853c2016-10-07 14:14:09 -0700322 if (reuseBitmap->isImmutable()) {
Derek Sollenberger2a6ecae2012-08-31 14:03:51 -0400323 ALOGW("Unable to reuse an immutable bitmap as an image decoder target.");
Chris Craik7e8c03c2013-06-03 13:53:36 -0700324 javaBitmap = NULL;
John Reckf29ed282015-04-07 07:32:03 -0700325 reuseBitmap = nullptr;
Chris Craik7e8c03c2013-06-03 13:53:36 -0700326 } else {
sergeyvc69853c2016-10-07 14:14:09 -0700327 existingBufferSize = bitmap::getBitmapAllocationByteCount(env, javaBitmap);
Derek Sollenberger2a6ecae2012-08-31 14:03:51 -0400328 }
Chet Haase37f74ca2010-12-08 17:56:36 -0800329 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330
sergeyv45082182016-09-29 18:25:40 -0700331 HeapAllocator defaultAllocator;
John Reckf29ed282015-04-07 07:32:03 -0700332 RecyclingPixelAllocator recyclingAllocator(reuseBitmap, existingBufferSize);
Chris Craik905e8242013-06-05 09:59:05 -0700333 ScaleCheckingAllocator scaleCheckingAllocator(scale, existingBufferSize);
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500334 SkBitmap::HeapAllocator heapAllocator;
335 SkBitmap::Allocator* decodeAllocator;
336 if (javaBitmap != nullptr && willScale) {
337 // This will allocate pixels using a HeapAllocator, since there will be an extra
338 // scaling step that copies these pixels into Java memory. This allocator
339 // also checks that the recycled javaBitmap is large enough.
340 decodeAllocator = &scaleCheckingAllocator;
341 } else if (javaBitmap != nullptr) {
342 decodeAllocator = &recyclingAllocator;
sergeyvda6c8ffc2016-11-22 18:28:54 -0800343 } else if (willScale || isHardware) {
344 // This will allocate pixels using a HeapAllocator,
345 // for scale case: there will be an extra scaling step.
346 // for hardware case: there will be extra swizzling & upload to gralloc step.
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500347 decodeAllocator = &heapAllocator;
348 } else {
sergeyv45082182016-09-29 18:25:40 -0700349 decodeAllocator = &defaultAllocator;
Chris Craik905e8242013-06-05 09:59:05 -0700350 }
351
Matt Sarett9e7cd632015-12-11 10:54:28 -0500352 SkAlphaType alphaType = codec->computeOutputAlphaType(requireUnpremultiplied);
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500353
Romain Guy253f2c22016-09-28 17:34:42 -0700354 const SkImageInfo decodeInfo = SkImageInfo::Make(size.width(), size.height(),
Romain Guy90fc43b2017-03-30 12:35:26 -0700355 decodeColorType, alphaType, decodeColorSpace);
Matt Sarett6c382572017-02-21 17:42:41 -0500356
357 // For wide gamut images, we will leave the color space on the SkBitmap. Otherwise,
358 // use the default.
Matt Sarett327c7202017-02-22 17:38:20 -0500359 SkImageInfo bitmapInfo = decodeInfo;
Matt Sarett2ecdfc22017-03-08 17:14:17 -0500360 if (decodeInfo.colorSpace() && decodeInfo.colorSpace()->isSRGB()) {
Derek Sollenbergerb1002312018-08-02 10:15:09 -0400361 bitmapInfo = bitmapInfo.makeColorSpace(decodeInfo.refColorSpace());
Matt Sarett6c382572017-02-21 17:42:41 -0500362 }
Matt Sarett33e37412016-12-21 12:17:17 -0500363
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500364 if (decodeColorType == kGray_8_SkColorType) {
365 // The legacy implementation of BitmapFactory used kAlpha8 for
366 // grayscale images (before kGray8 existed). While the codec
367 // recognizes kGray8, we need to decode into a kAlpha8 bitmap
368 // in order to avoid a behavior change.
Matt Sarettee80c472016-06-03 10:23:38 -0400369 bitmapInfo =
370 bitmapInfo.makeColorType(kAlpha_8_SkColorType).makeAlphaType(kPremul_SkAlphaType);
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500371 }
Chris Craik7e8c03c2013-06-03 13:53:36 -0700372 SkBitmap decodingBitmap;
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500373 if (!decodingBitmap.setInfo(bitmapInfo) ||
Mike Reed81397c42017-07-18 17:04:16 -0400374 !decodingBitmap.tryAllocPixels(decodeAllocator)) {
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500375 // SkAndroidCodec should recommend a valid SkImageInfo, so setInfo()
376 // should only only fail if the calculated value for rowBytes is too
377 // large.
378 // tryAllocPixels() can fail due to OOM on the Java heap, OOM on the
379 // native heap, or the recycled javaBitmap being too small to reuse.
380 return nullptr;
Mike Reedc70e06b2009-04-24 11:09:12 -0400381 }
382
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500383 // Use SkAndroidCodec to perform the decode.
384 SkAndroidCodec::AndroidOptions codecOptions;
Romain Guy253f2c22016-09-28 17:34:42 -0700385 codecOptions.fZeroInitialized = decodeAllocator == &defaultAllocator ?
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500386 SkCodec::kYes_ZeroInitialized : SkCodec::kNo_ZeroInitialized;
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500387 codecOptions.fSampleSize = sampleSize;
388 SkCodec::Result result = codec->getAndroidPixels(decodeInfo, decodingBitmap.getPixels(),
389 decodingBitmap.rowBytes(), &codecOptions);
390 switch (result) {
391 case SkCodec::kSuccess:
392 case SkCodec::kIncompleteInput:
393 break;
394 default:
Matt Sarett3b1b68d2015-12-14 13:08:33 -0500395 return nullObjectReturn("codec->getAndroidPixels() failed.");
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500396 }
397
Stan Iliev7aedf6f2017-12-20 12:22:59 -0500398 // This is weird so let me explain: we could use the scale parameter
399 // directly, but for historical reasons this is how the corresponding
400 // Dalvik code has always behaved. We simply recreate the behavior here.
401 // The result is slightly different from simply using scale because of
402 // the 0.5f rounding bias applied when computing the target image size
403 const float scaleX = scaledWidth / float(decodingBitmap.width());
404 const float scaleY = scaledHeight / float(decodingBitmap.height());
405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 jbyteArray ninePatchChunk = NULL;
Chris Craik47cd8e92014-07-08 17:13:08 -0700407 if (peeker.mPatch != NULL) {
Romain Guy7b2f8b82012-03-19 17:18:54 -0700408 if (willScale) {
Stan Iliev7aedf6f2017-12-20 12:22:59 -0500409 peeker.scale(scaleX, scaleY, scaledWidth, scaledHeight);
Romain Guy7b2f8b82012-03-19 17:18:54 -0700410 }
411
Chris Craik47cd8e92014-07-08 17:13:08 -0700412 size_t ninePatchArraySize = peeker.mPatch->serializedSize();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 ninePatchChunk = env->NewByteArray(ninePatchArraySize);
Romain Guy7b2f8b82012-03-19 17:18:54 -0700414 if (ninePatchChunk == NULL) {
Mike Reedc70e06b2009-04-24 11:09:12 -0400415 return nullObjectReturn("ninePatchChunk == null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 }
Romain Guy7b2f8b82012-03-19 17:18:54 -0700417
418 jbyte* array = (jbyte*) env->GetPrimitiveArrayCritical(ninePatchChunk, NULL);
419 if (array == NULL) {
Mike Reedc70e06b2009-04-24 11:09:12 -0400420 return nullObjectReturn("primitive array == null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 }
Romain Guy7b2f8b82012-03-19 17:18:54 -0700422
Chris Craik47cd8e92014-07-08 17:13:08 -0700423 memcpy(array, peeker.mPatch, peeker.mPatchSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 env->ReleasePrimitiveArrayCritical(ninePatchChunk, array, 0);
425 }
426
Chris Craik47cd8e92014-07-08 17:13:08 -0700427 jobject ninePatchInsets = NULL;
428 if (peeker.mHasInsets) {
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400429 ninePatchInsets = peeker.createNinePatchInsets(env, scale);
Mathieu Chartiera08d10f2014-08-29 16:55:55 -0700430 if (ninePatchInsets == NULL) {
431 return nullObjectReturn("nine patch insets == null");
432 }
Amith Yamasaniec4a5042012-04-04 10:27:15 -0700433 if (javaBitmap != NULL) {
Chris Craik47cd8e92014-07-08 17:13:08 -0700434 env->SetObjectField(javaBitmap, gBitmap_ninePatchInsetsFieldID, ninePatchInsets);
Amith Yamasaniec4a5042012-04-04 10:27:15 -0700435 }
436 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437
John Reckf29ed282015-04-07 07:32:03 -0700438 SkBitmap outputBitmap;
Romain Guy7b2f8b82012-03-19 17:18:54 -0700439 if (willScale) {
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500440 // Set the allocator for the outputBitmap.
441 SkBitmap::Allocator* outputAllocator;
442 if (javaBitmap != nullptr) {
443 outputAllocator = &recyclingAllocator;
444 } else {
sergeyv45082182016-09-29 18:25:40 -0700445 outputAllocator = &defaultAllocator;
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500446 }
447
Leon Scroggins IIIef691a3d2017-07-10 17:03:55 -0400448 SkColorType scaledColorType = decodingBitmap.colorType();
Leon Scroggins III8790be62013-12-03 16:26:51 -0500449 // FIXME: If the alphaType is kUnpremul and the image has alpha, the
450 // colors may not be correct, since Skia does not yet support drawing
451 // to/from unpremultiplied bitmaps.
Matt Sarettee80c472016-06-03 10:23:38 -0400452 outputBitmap.setInfo(
453 bitmapInfo.makeWH(scaledWidth, scaledHeight).makeColorType(scaledColorType));
Mike Reed81397c42017-07-18 17:04:16 -0400454 if (!outputBitmap.tryAllocPixels(outputAllocator)) {
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500455 // This should only fail on OOM. The recyclingAllocator should have
456 // enough memory since we check this before decoding using the
457 // scaleCheckingAllocator.
Raph Levien005bfc62012-09-20 22:51:47 -0700458 return nullObjectReturn("allocation failed for scaled bitmap");
459 }
Leon Scroggins III1ffe7272013-09-19 11:34:06 -0400460
Romain Guy7b2f8b82012-03-19 17:18:54 -0700461 SkPaint paint;
Romain Guy253f2c22016-09-28 17:34:42 -0700462 // kSrc_Mode instructs us to overwrite the uninitialized pixels in
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500463 // outputBitmap. Otherwise we would blend by default, which is not
464 // what we want.
Mike Reed260ab722016-10-07 15:59:20 -0400465 paint.setBlendMode(SkBlendMode::kSrc);
Romain Guy253f2c22016-09-28 17:34:42 -0700466 paint.setFilterQuality(kLow_SkFilterQuality); // bilinear filtering
Romain Guy7b2f8b82012-03-19 17:18:54 -0700467
Matt Sarettca9b7032017-04-13 12:18:47 -0400468 SkCanvas canvas(outputBitmap, SkCanvas::ColorBehavior::kLegacy);
Stan Iliev7aedf6f2017-12-20 12:22:59 -0500469 canvas.scale(scaleX, scaleY);
Chris Craik7e8c03c2013-06-03 13:53:36 -0700470 canvas.drawBitmap(decodingBitmap, 0.0f, 0.0f, &paint);
471 } else {
John Reckf29ed282015-04-07 07:32:03 -0700472 outputBitmap.swap(decodingBitmap);
Romain Guy7b2f8b82012-03-19 17:18:54 -0700473 }
474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 if (padding) {
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400476 peeker.getPadding(env, padding);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500479 // If we get here, the outputBitmap should have an installed pixelref.
John Reckf29ed282015-04-07 07:32:03 -0700480 if (outputBitmap.pixelRef() == NULL) {
Marco Nelissenb2fe3be2012-05-07 11:24:13 -0700481 return nullObjectReturn("Got null SkPixelRef");
482 }
Romain Guy23610982011-01-17 12:51:55 -0800483
Chris Craik7e8c03c2013-06-03 13:53:36 -0700484 if (!isMutable && javaBitmap == NULL) {
Romain Guy23610982011-01-17 12:51:55 -0800485 // promise we will never change our pixels (great for sharing and pictures)
John Reckf29ed282015-04-07 07:32:03 -0700486 outputBitmap.setImmutable();
Romain Guy23610982011-01-17 12:51:55 -0800487 }
Chet Haase37f74ca2010-12-08 17:56:36 -0800488
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500489 bool isPremultiplied = !requireUnpremultiplied;
490 if (javaBitmap != nullptr) {
sergeyvc69853c2016-10-07 14:14:09 -0700491 bitmap::reinitBitmap(env, javaBitmap, outputBitmap.info(), isPremultiplied);
John Reckf29ed282015-04-07 07:32:03 -0700492 outputBitmap.notifyPixelsChanged();
Chet Haase37f74ca2010-12-08 17:56:36 -0800493 // If a java bitmap was passed in for reuse, pass it back
494 return javaBitmap;
495 }
Chris Craik1abf5d62013-08-16 12:47:03 -0700496
497 int bitmapCreateFlags = 0x0;
sergeyvc69853c2016-10-07 14:14:09 -0700498 if (isMutable) bitmapCreateFlags |= android::bitmap::kBitmapCreateFlag_Mutable;
499 if (isPremultiplied) bitmapCreateFlags |= android::bitmap::kBitmapCreateFlag_Premultiplied;
Chris Craik1abf5d62013-08-16 12:47:03 -0700500
sergeyvda6c8ffc2016-11-22 18:28:54 -0800501 if (isHardware) {
502 sk_sp<Bitmap> hardwareBitmap = Bitmap::allocateHardwareBitmap(outputBitmap);
sergeyvd67bb1e2017-06-28 12:44:03 -0700503 if (!hardwareBitmap.get()) {
504 return nullObjectReturn("Failed to allocate a hardware bitmap");
505 }
sergeyvda6c8ffc2016-11-22 18:28:54 -0800506 return bitmap::createBitmap(env, hardwareBitmap.release(), bitmapCreateFlags,
507 ninePatchChunk, ninePatchInsets, -1);
508 }
509
Mike Reedc70e06b2009-04-24 11:09:12 -0400510 // now create the java bitmap
sergeyvc69853c2016-10-07 14:14:09 -0700511 return bitmap::createBitmap(env, defaultAllocator.getStorageObjAndReset(),
Chris Craik47cd8e92014-07-08 17:13:08 -0700512 bitmapCreateFlags, ninePatchChunk, ninePatchInsets, -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513}
514
Chris Craik905e8242013-06-05 09:59:05 -0700515static jobject nativeDecodeStream(JNIEnv* env, jobject clazz, jobject is, jbyteArray storage,
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500516 jobject padding, jobject options, jlong colorSpaceHandle) {
Romain Guy7b2f8b82012-03-19 17:18:54 -0700517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 jobject bitmap = NULL;
Ben Wagner60126ef2015-08-07 12:13:48 -0400519 std::unique_ptr<SkStream> stream(CreateJavaInputStreamAdaptor(env, is, storage));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520
Leon Scroggins IIIca320212013-08-20 17:59:39 -0400521 if (stream.get()) {
Ben Wagner60126ef2015-08-07 12:13:48 -0400522 std::unique_ptr<SkStreamRewindable> bufferedStream(
Mike Reedbe896ed2017-09-19 17:01:30 -0400523 SkFrontBufferedStream::Make(std::move(stream), SkCodec::MinBufferedBytesNeeded()));
Leon Scroggins III7315f1b2013-09-10 20:26:05 -0400524 SkASSERT(bufferedStream.get() != NULL);
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500525 bitmap = doDecode(env, std::move(bufferedStream), padding, options, colorSpaceHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 }
527 return bitmap;
528}
529
Romain Guy7b2f8b82012-03-19 17:18:54 -0700530static jobject nativeDecodeFileDescriptor(JNIEnv* env, jobject clazz, jobject fileDescriptor,
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500531 jobject padding, jobject bitmapFactoryOptions, jlong colorSpaceHandle) {
Romain Guy7b2f8b82012-03-19 17:18:54 -0700532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 NPE_CHECK_RETURN_ZERO(env, fileDescriptor);
534
Derek Sollenberger5cb769d2014-09-24 09:20:09 -0400535 int descriptor = jniGetFDFromFileDescriptor(env, fileDescriptor);
Mike Reedc70e06b2009-04-24 11:09:12 -0400536
Derek Sollenberger5827cb52013-07-26 14:58:06 -0400537 struct stat fdStat;
538 if (fstat(descriptor, &fdStat) == -1) {
539 doThrowIOE(env, "broken file descriptor");
540 return nullObjectReturn("fstat return -1");
541 }
542
Derek Sollenberger5cb769d2014-09-24 09:20:09 -0400543 // Restore the descriptor's offset on exiting this function. Even though
544 // we dup the descriptor, both the original and dup refer to the same open
545 // file description and changes to the file offset in one impact the other.
Jérôme Poichetd29c9022014-09-26 18:59:11 +0000546 AutoFDSeek autoRestore(descriptor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547
Derek Sollenberger5cb769d2014-09-24 09:20:09 -0400548 // Duplicate the descriptor here to prevent leaking memory. A leak occurs
549 // if we only close the file descriptor and not the file object it is used to
550 // create. If we don't explicitly clean up the file (which in turn closes the
551 // descriptor) the buffers allocated internally by fseek will be leaked.
Nick Kralevich4b3a08c2019-01-28 10:39:10 -0800552 int dupDescriptor = fcntl(descriptor, F_DUPFD_CLOEXEC, 0);
Derek Sollenberger5cb769d2014-09-24 09:20:09 -0400553
554 FILE* file = fdopen(dupDescriptor, "r");
Leon Scroggins III0102f8a2014-01-14 15:14:57 -0500555 if (file == NULL) {
Derek Sollenberger5cb769d2014-09-24 09:20:09 -0400556 // cleanup the duplicated descriptor since it will not be closed when the
557 // file is cleaned up (fclose).
558 close(dupDescriptor);
Leon Scroggins III0102f8a2014-01-14 15:14:57 -0500559 return nullObjectReturn("Could not open file");
Leon Scroggins IIIf65183f2013-10-07 16:32:14 -0400560 }
Leon Scroggins III0102f8a2014-01-14 15:14:57 -0500561
Leon Scroggins IIIdd3c06c2017-03-10 10:50:33 -0500562 std::unique_ptr<SkFILEStream> fileStream(new SkFILEStream(file));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563
Yujie Qinc1d7b7f2016-02-29 14:00:29 +0100564 // If there is no offset for the file descriptor, we use SkFILEStream directly.
565 if (::lseek(descriptor, 0, SEEK_CUR) == 0) {
566 assert(isSeekable(dupDescriptor));
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500567 return doDecode(env, std::move(fileStream), padding, bitmapFactoryOptions,
568 colorSpaceHandle);
Yujie Qinc1d7b7f2016-02-29 14:00:29 +0100569 }
570
Leon Scroggins III0aa39dc2014-06-03 12:19:32 -0400571 // Use a buffered stream. Although an SkFILEStream can be rewound, this
572 // ensures that SkImageDecoder::Factory never rewinds beyond the
573 // current position of the file descriptor.
Mike Reedbe896ed2017-09-19 17:01:30 -0400574 std::unique_ptr<SkStreamRewindable> stream(SkFrontBufferedStream::Make(std::move(fileStream),
Matt Sarett96ffbdc2016-02-11 17:03:54 -0500575 SkCodec::MinBufferedBytesNeeded()));
Leon Scroggins III2826e5f2014-02-05 19:46:02 -0500576
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500577 return doDecode(env, std::move(stream), padding, bitmapFactoryOptions, colorSpaceHandle);
Mike Reedc70e06b2009-04-24 11:09:12 -0400578}
579
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000580static jobject nativeDecodeAsset(JNIEnv* env, jobject clazz, jlong native_asset,
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500581 jobject padding, jobject options, jlong colorSpaceHandle) {
Romain Guy7b2f8b82012-03-19 17:18:54 -0700582
Mike Reedc70e06b2009-04-24 11:09:12 -0400583 Asset* asset = reinterpret_cast<Asset*>(native_asset);
Leon Scroggins III0aa39dc2014-06-03 12:19:32 -0400584 // since we know we'll be done with the asset when we return, we can
585 // just use a simple wrapper
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500586 return doDecode(env, skstd::make_unique<AssetStreamAdaptor>(asset), padding, options,
587 colorSpaceHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588}
589
590static jobject nativeDecodeByteArray(JNIEnv* env, jobject, jbyteArray byteArray,
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500591 jint offset, jint length, jobject options, jlong colorSpaceHandle) {
Romain Guy7b2f8b82012-03-19 17:18:54 -0700592
Mike Reedc70e06b2009-04-24 11:09:12 -0400593 AutoJavaByteArray ar(env, byteArray);
Mike Reedc7c65602017-07-26 10:40:25 -0400594 return doDecode(env, skstd::make_unique<SkMemoryStream>(ar.ptr() + offset, length, false),
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500595 nullptr, options, colorSpaceHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596}
597
Owen Lina9d0d472011-01-18 17:39:15 +0800598static jboolean nativeIsSeekable(JNIEnv* env, jobject, jobject fileDescriptor) {
Elliott Hughesa3804cf2011-04-11 16:50:19 -0700599 jint descriptor = jniGetFDFromFileDescriptor(env, fileDescriptor);
Yujie Qinc1d7b7f2016-02-29 14:00:29 +0100600 return isSeekable(descriptor) ? JNI_TRUE : JNI_FALSE;
Owen Lina9d0d472011-01-18 17:39:15 +0800601}
602
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603///////////////////////////////////////////////////////////////////////////////
604
Daniel Micay76f6a862015-09-19 17:31:01 -0400605static const JNINativeMethod gMethods[] = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 { "nativeDecodeStream",
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500607 "(Ljava/io/InputStream;[BLandroid/graphics/Rect;Landroid/graphics/BitmapFactory$Options;J)Landroid/graphics/Bitmap;",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 (void*)nativeDecodeStream
609 },
610
611 { "nativeDecodeFileDescriptor",
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500612 "(Ljava/io/FileDescriptor;Landroid/graphics/Rect;Landroid/graphics/BitmapFactory$Options;J)Landroid/graphics/Bitmap;",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 (void*)nativeDecodeFileDescriptor
614 },
615
616 { "nativeDecodeAsset",
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500617 "(JLandroid/graphics/Rect;Landroid/graphics/BitmapFactory$Options;J)Landroid/graphics/Bitmap;",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 (void*)nativeDecodeAsset
619 },
620
621 { "nativeDecodeByteArray",
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500622 "([BIILandroid/graphics/BitmapFactory$Options;J)Landroid/graphics/Bitmap;",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 (void*)nativeDecodeByteArray
624 },
625
Owen Lina9d0d472011-01-18 17:39:15 +0800626 { "nativeIsSeekable",
627 "(Ljava/io/FileDescriptor;)Z",
628 (void*)nativeIsSeekable
629 },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630};
631
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632int register_android_graphics_BitmapFactory(JNIEnv* env) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800633 jclass options_class = FindClassOrDie(env, "android/graphics/BitmapFactory$Options");
634 gOptions_bitmapFieldID = GetFieldIDOrDie(env, options_class, "inBitmap",
Chris Craik47cd8e92014-07-08 17:13:08 -0700635 "Landroid/graphics/Bitmap;");
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800636 gOptions_justBoundsFieldID = GetFieldIDOrDie(env, options_class, "inJustDecodeBounds", "Z");
637 gOptions_sampleSizeFieldID = GetFieldIDOrDie(env, options_class, "inSampleSize", "I");
638 gOptions_configFieldID = GetFieldIDOrDie(env, options_class, "inPreferredConfig",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 "Landroid/graphics/Bitmap$Config;");
Romain Guy95648b82017-04-13 18:43:42 -0700640 gOptions_colorSpaceFieldID = GetFieldIDOrDie(env, options_class, "inPreferredColorSpace",
641 "Landroid/graphics/ColorSpace;");
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800642 gOptions_premultipliedFieldID = GetFieldIDOrDie(env, options_class, "inPremultiplied", "Z");
643 gOptions_mutableFieldID = GetFieldIDOrDie(env, options_class, "inMutable", "Z");
644 gOptions_ditherFieldID = GetFieldIDOrDie(env, options_class, "inDither", "Z");
645 gOptions_preferQualityOverSpeedFieldID = GetFieldIDOrDie(env, options_class,
Wei-Ta Chen953f9092010-12-03 14:06:18 -0800646 "inPreferQualityOverSpeed", "Z");
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800647 gOptions_scaledFieldID = GetFieldIDOrDie(env, options_class, "inScaled", "Z");
648 gOptions_densityFieldID = GetFieldIDOrDie(env, options_class, "inDensity", "I");
649 gOptions_screenDensityFieldID = GetFieldIDOrDie(env, options_class, "inScreenDensity", "I");
650 gOptions_targetDensityFieldID = GetFieldIDOrDie(env, options_class, "inTargetDensity", "I");
651 gOptions_widthFieldID = GetFieldIDOrDie(env, options_class, "outWidth", "I");
652 gOptions_heightFieldID = GetFieldIDOrDie(env, options_class, "outHeight", "I");
653 gOptions_mimeFieldID = GetFieldIDOrDie(env, options_class, "outMimeType", "Ljava/lang/String;");
Romain Guye8d2ebb2017-02-09 18:38:47 -0800654 gOptions_outConfigFieldID = GetFieldIDOrDie(env, options_class, "outConfig",
655 "Landroid/graphics/Bitmap$Config;");
Romain Guy90fc43b2017-03-30 12:35:26 -0700656 gOptions_outColorSpaceFieldID = GetFieldIDOrDie(env, options_class, "outColorSpace",
657 "Landroid/graphics/ColorSpace;");
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800658 gOptions_mCancelID = GetFieldIDOrDie(env, options_class, "mCancel", "Z");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800660 jclass bitmap_class = FindClassOrDie(env, "android/graphics/Bitmap");
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800661 gBitmap_ninePatchInsetsFieldID = GetFieldIDOrDie(env, bitmap_class, "mNinePatchInsets",
Chris Craik47cd8e92014-07-08 17:13:08 -0700662 "Landroid/graphics/NinePatch$InsetStruct;");
663
Romain Guye8d2ebb2017-02-09 18:38:47 -0800664 gBitmapConfig_class = MakeGlobalRefOrDie(env, FindClassOrDie(env,
665 "android/graphics/Bitmap$Config"));
666 gBitmapConfig_nativeToConfigMethodID = GetStaticMethodIDOrDie(env, gBitmapConfig_class,
667 "nativeToConfig", "(I)Landroid/graphics/Bitmap$Config;");
668
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800669 return android::RegisterMethodsOrDie(env, "android/graphics/BitmapFactory",
670 gMethods, NELEM(gMethods));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671}