blob: 48a5783e646465577225fe31764efbec5f3907b2 [file] [log] [blame]
Derek Sollenberger5368eda2019-10-25 11:20:03 -04001#undef LOG_TAG
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002#define LOG_TAG "BitmapFactory"
3
Joseph Wenf1f48bc2010-07-19 16:59:51 +08004#include "BitmapFactory.h"
Ben Wagner60126ef2015-08-07 12:13:48 -04005#include "CreateJavaOutputStreamAdaptor.h"
6#include "GraphicsJNI.h"
Leon Scrogginsa06d86a2011-03-02 16:56:54 -05007#include "NinePatchPeeker.h"
Matt Sarettb8adc9a2015-12-02 13:35:22 -05008#include "SkAndroidCodec.h"
9#include "SkBRDAllocator.h"
Leon Scroggins III7315f1b2013-09-10 20:26:05 -040010#include "SkFrontBufferedStream.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
Leon Scroggins IIIee3bfe72019-01-31 08:42:23 -050018#include <HardwareBitmapUploader.h>
Steven Moreland2279b252017-07-19 09:50:45 -070019#include <nativehelper/JNIHelp.h>
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080020#include <androidfw/Asset.h>
21#include <androidfw/ResourceTypes.h>
Chris Craikbd8db2e82014-08-20 16:31:57 -070022#include <cutils/compiler.h>
Ben Wagner60126ef2015-08-07 12:13:48 -040023#include <memory>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024#include <netinet/in.h>
Leon Scroggins III0102f8a2014-01-14 15:14:57 -050025#include <stdio.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026#include <sys/mman.h>
Joseph Wen2dcfbef2010-09-10 10:15:09 +080027#include <sys/stat.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028
Joseph Wenf1f48bc2010-07-19 16:59:51 +080029jfieldID gOptions_justBoundsFieldID;
30jfieldID gOptions_sampleSizeFieldID;
31jfieldID gOptions_configFieldID;
Romain Guy95648b82017-04-13 18:43:42 -070032jfieldID gOptions_colorSpaceFieldID;
Chris Craik1abf5d62013-08-16 12:47:03 -070033jfieldID gOptions_premultipliedFieldID;
Romain Guy23610982011-01-17 12:51:55 -080034jfieldID gOptions_mutableFieldID;
Joseph Wenf1f48bc2010-07-19 16:59:51 +080035jfieldID gOptions_ditherFieldID;
Wei-Ta Chen953f9092010-12-03 14:06:18 -080036jfieldID gOptions_preferQualityOverSpeedFieldID;
Chris Craik905e8242013-06-05 09:59:05 -070037jfieldID gOptions_scaledFieldID;
38jfieldID gOptions_densityFieldID;
39jfieldID gOptions_screenDensityFieldID;
40jfieldID gOptions_targetDensityFieldID;
Joseph Wenf1f48bc2010-07-19 16:59:51 +080041jfieldID gOptions_widthFieldID;
42jfieldID gOptions_heightFieldID;
43jfieldID gOptions_mimeFieldID;
Romain Guye8d2ebb2017-02-09 18:38:47 -080044jfieldID gOptions_outConfigFieldID;
Romain Guy90fc43b2017-03-30 12:35:26 -070045jfieldID gOptions_outColorSpaceFieldID;
Joseph Wenf1f48bc2010-07-19 16:59:51 +080046jfieldID gOptions_mCancelID;
Chet Haase37f74ca2010-12-08 17:56:36 -080047jfieldID gOptions_bitmapFieldID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048
Chris Craik47cd8e92014-07-08 17:13:08 -070049jfieldID gBitmap_ninePatchInsetsFieldID;
50
Romain Guye8d2ebb2017-02-09 18:38:47 -080051jclass gBitmapConfig_class;
52jmethodID gBitmapConfig_nativeToConfigMethodID;
53
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054using namespace android;
55
Leon Scroggins III407b5442019-11-22 17:10:20 -050056const char* getMimeType(SkEncodedImageFormat format) {
Matt Sarettb8adc9a2015-12-02 13:35:22 -050057 switch (format) {
Hal Canary10219fb2016-11-23 20:41:22 -050058 case SkEncodedImageFormat::kBMP:
Leon Scroggins III407b5442019-11-22 17:10:20 -050059 return "image/bmp";
Hal Canary10219fb2016-11-23 20:41:22 -050060 case SkEncodedImageFormat::kGIF:
Leon Scroggins III407b5442019-11-22 17:10:20 -050061 return "image/gif";
Hal Canary10219fb2016-11-23 20:41:22 -050062 case SkEncodedImageFormat::kICO:
Leon Scroggins III407b5442019-11-22 17:10:20 -050063 return "image/x-ico";
Hal Canary10219fb2016-11-23 20:41:22 -050064 case SkEncodedImageFormat::kJPEG:
Leon Scroggins III407b5442019-11-22 17:10:20 -050065 return "image/jpeg";
Hal Canary10219fb2016-11-23 20:41:22 -050066 case SkEncodedImageFormat::kPNG:
Leon Scroggins III407b5442019-11-22 17:10:20 -050067 return "image/png";
Hal Canary10219fb2016-11-23 20:41:22 -050068 case SkEncodedImageFormat::kWEBP:
Leon Scroggins III407b5442019-11-22 17:10:20 -050069 return "image/webp";
Chong Zhang48fa8902017-08-16 11:57:02 -070070 case SkEncodedImageFormat::kHEIF:
Leon Scroggins III407b5442019-11-22 17:10:20 -050071 return "image/heif";
Hal Canary10219fb2016-11-23 20:41:22 -050072 case SkEncodedImageFormat::kWBMP:
Leon Scroggins III407b5442019-11-22 17:10:20 -050073 return "image/vnd.wap.wbmp";
Hal Canary10219fb2016-11-23 20:41:22 -050074 case SkEncodedImageFormat::kDNG:
Leon Scroggins III407b5442019-11-22 17:10:20 -050075 return "image/x-adobe-dng";
Matt Sarettb8adc9a2015-12-02 13:35:22 -050076 default:
Leon Scroggins III407b5442019-11-22 17:10:20 -050077 return nullptr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 }
Leon Scroggins III407b5442019-11-22 17:10:20 -050079}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080
Leon Scroggins III407b5442019-11-22 17:10:20 -050081jstring getMimeTypeAsJavaString(JNIEnv* env, SkEncodedImageFormat format) {
Vladimir Marko7ab249a2015-01-06 18:17:52 +000082 jstring jstr = nullptr;
Leon Scroggins III407b5442019-11-22 17:10:20 -050083 const char* mimeType = getMimeType(format);
Matt Sarettb8adc9a2015-12-02 13:35:22 -050084 if (mimeType) {
Vladimir Marko7ab249a2015-01-06 18:17:52 +000085 // NOTE: Caller should env->ExceptionCheck() for OOM
86 // (can't check for nullptr as it's a valid return value)
Matt Sarettb8adc9a2015-12-02 13:35:22 -050087 jstr = env->NewStringUTF(mimeType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 }
89 return jstr;
90}
91
Chris Craik905e8242013-06-05 09:59:05 -070092class ScaleCheckingAllocator : public SkBitmap::HeapAllocator {
93public:
94 ScaleCheckingAllocator(float scale, int size)
95 : mScale(scale), mSize(size) {
96 }
97
Mike Reed81397c42017-07-18 17:04:16 -040098 virtual bool allocPixelRef(SkBitmap* bitmap) {
Chris Craik905e8242013-06-05 09:59:05 -070099 // accounts for scale in final allocation, using eventual size and config
Leon Scroggins IIIef691a3d2017-07-10 17:03:55 -0400100 const int bytesPerPixel = SkColorTypeBytesPerPixel(bitmap->colorType());
Chris Craik905e8242013-06-05 09:59:05 -0700101 const int requestedSize = bytesPerPixel *
102 int(bitmap->width() * mScale + 0.5f) *
103 int(bitmap->height() * mScale + 0.5f);
104 if (requestedSize > mSize) {
105 ALOGW("bitmap for alloc reuse (%d bytes) can't fit scaled bitmap (%d bytes)",
106 mSize, requestedSize);
107 return false;
108 }
Mike Reed81397c42017-07-18 17:04:16 -0400109 return SkBitmap::HeapAllocator::allocPixelRef(bitmap);
Chris Craik905e8242013-06-05 09:59:05 -0700110 }
111private:
112 const float mScale;
113 const int mSize;
114};
115
Chris Craik7e8c03c2013-06-03 13:53:36 -0700116class RecyclingPixelAllocator : public SkBitmap::Allocator {
117public:
sergeyvc1c54062016-10-19 18:47:26 -0700118 RecyclingPixelAllocator(android::Bitmap* bitmap, unsigned int size)
John Reckf29ed282015-04-07 07:32:03 -0700119 : mBitmap(bitmap), mSize(size) {
Chris Craik7e8c03c2013-06-03 13:53:36 -0700120 }
121
122 ~RecyclingPixelAllocator() {
Chris Craik7e8c03c2013-06-03 13:53:36 -0700123 }
124
Mike Reed81397c42017-07-18 17:04:16 -0400125 virtual bool allocPixelRef(SkBitmap* bitmap) {
Leon Scroggins46cb9bd2014-03-06 15:36:39 -0500126 const SkImageInfo& info = bitmap->info();
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400127 if (info.colorType() == kUnknown_SkColorType) {
Leon Scroggins46cb9bd2014-03-06 15:36:39 -0500128 ALOGW("unable to reuse a bitmap as the target has an unknown bitmap configuration");
Chris Craik7e8c03c2013-06-03 13:53:36 -0700129 return false;
130 }
Chris Craikcd0ba712013-09-06 14:40:30 -0700131
Mike Reed7569de02017-10-06 16:25:49 -0400132 const size_t size = info.computeByteSize(bitmap->rowBytes());
133 if (size > SK_MaxS32) {
Leon Scroggins46cb9bd2014-03-06 15:36:39 -0500134 ALOGW("bitmap is too large");
135 return false;
136 }
137
Leon Scroggins46cb9bd2014-03-06 15:36:39 -0500138 if (size > mSize) {
Dan Albert46d84442014-11-18 16:07:51 -0800139 ALOGW("bitmap marked for reuse (%u bytes) can't fit new bitmap "
140 "(%zu bytes)", mSize, size);
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500141 return false;
142 }
143
Leon Scroggins IIIf51a80d2017-07-12 10:46:35 -0400144 mBitmap->reconfigure(info, bitmap->rowBytes());
Mike Reed826deef2017-04-04 15:32:04 -0400145 bitmap->setPixelRef(sk_ref_sp(mBitmap), 0, 0);
Chris Craik7e8c03c2013-06-03 13:53:36 -0700146 return true;
147 }
148
149private:
sergeyvc1c54062016-10-19 18:47:26 -0700150 android::Bitmap* const mBitmap;
Chris Craik7e8c03c2013-06-03 13:53:36 -0700151 const unsigned int mSize;
152};
153
Anton Daubert4e5ec342016-03-07 17:30:20 +0100154// Necessary for decodes when the native decoder cannot scale to appropriately match the sampleSize
155// (for example, RAW). If the sampleSize divides evenly into the dimension, we require that the
156// scale matches exactly. If sampleSize does not divide evenly, we allow the decoder to choose how
157// best to round.
158static bool needsFineScale(const int fullSize, const int decodedSize, const int sampleSize) {
159 if (fullSize % sampleSize == 0 && fullSize / sampleSize != decodedSize) {
160 return true;
161 } else if ((fullSize / sampleSize + 1) != decodedSize &&
162 (fullSize / sampleSize) != decodedSize) {
163 return true;
164 }
165 return false;
166}
167
168static bool needsFineScale(const SkISize fullSize, const SkISize decodedSize,
169 const int sampleSize) {
170 return needsFineScale(fullSize.width(), decodedSize.width(), sampleSize) ||
171 needsFineScale(fullSize.height(), decodedSize.height(), sampleSize);
172}
173
Mike Reedc7c65602017-07-26 10:40:25 -0400174static jobject doDecode(JNIEnv* env, std::unique_ptr<SkStreamRewindable> stream,
Leon Scroggins III71fae622019-03-26 16:28:41 -0400175 jobject padding, jobject options, jlong inBitmapHandle,
176 jlong colorSpaceHandle) {
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500177 // Set default values for the options parameters.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 int sampleSize = 1;
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500179 bool onlyDecodeSize = false;
Mike Reed42a1d082014-07-07 18:06:18 -0400180 SkColorType prefColorType = kN32_SkColorType;
sergeyvda6c8ffc2016-11-22 18:28:54 -0800181 bool isHardware = false;
Romain Guy23610982011-01-17 12:51:55 -0800182 bool isMutable = false;
Chris Craik905e8242013-06-05 09:59:05 -0700183 float scale = 1.0f;
Chris Craik1abf5d62013-08-16 12:47:03 -0700184 bool requireUnpremultiplied = false;
Chet Haase37f74ca2010-12-08 17:56:36 -0800185 jobject javaBitmap = NULL;
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500186 sk_sp<SkColorSpace> prefColorSpace = GraphicsJNI::getNativeColorSpace(colorSpaceHandle);
Elliott Hughesa3804cf2011-04-11 16:50:19 -0700187
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500188 // Update with options supplied by the client.
Romain Guy7b2f8b82012-03-19 17:18:54 -0700189 if (options != NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 sampleSize = env->GetIntField(options, gOptions_sampleSizeFieldID);
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500191 // Correct a non-positive sampleSize. sampleSize defaults to zero within the
192 // options object, which is strange.
193 if (sampleSize <= 0) {
194 sampleSize = 1;
195 }
196
197 if (env->GetBooleanField(options, gOptions_justBoundsFieldID)) {
198 onlyDecodeSize = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 }
Romain Guy7b2f8b82012-03-19 17:18:54 -0700200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 // initialize these, in case we fail later on
202 env->SetIntField(options, gOptions_widthFieldID, -1);
203 env->SetIntField(options, gOptions_heightFieldID, -1);
204 env->SetObjectField(options, gOptions_mimeFieldID, 0);
Romain Guye8d2ebb2017-02-09 18:38:47 -0800205 env->SetObjectField(options, gOptions_outConfigFieldID, 0);
Romain Guy90fc43b2017-03-30 12:35:26 -0700206 env->SetObjectField(options, gOptions_outColorSpaceFieldID, 0);
Elliott Hughesa3804cf2011-04-11 16:50:19 -0700207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 jobject jconfig = env->GetObjectField(options, gOptions_configFieldID);
Mike Reed42a1d082014-07-07 18:06:18 -0400209 prefColorType = GraphicsJNI::getNativeBitmapColorType(env, jconfig);
sergeyvda6c8ffc2016-11-22 18:28:54 -0800210 isHardware = GraphicsJNI::isHardwareConfig(env, jconfig);
Romain Guy23610982011-01-17 12:51:55 -0800211 isMutable = env->GetBooleanField(options, gOptions_mutableFieldID);
Chris Craik1abf5d62013-08-16 12:47:03 -0700212 requireUnpremultiplied = !env->GetBooleanField(options, gOptions_premultipliedFieldID);
Chet Haase37f74ca2010-12-08 17:56:36 -0800213 javaBitmap = env->GetObjectField(options, gOptions_bitmapFieldID);
Chris Craik905e8242013-06-05 09:59:05 -0700214
215 if (env->GetBooleanField(options, gOptions_scaledFieldID)) {
216 const int density = env->GetIntField(options, gOptions_densityFieldID);
217 const int targetDensity = env->GetIntField(options, gOptions_targetDensityFieldID);
218 const int screenDensity = env->GetIntField(options, gOptions_screenDensityFieldID);
219 if (density != 0 && targetDensity != 0 && density != screenDensity) {
220 scale = (float) targetDensity / density;
221 }
222 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 }
Romain Guy7b2f8b82012-03-19 17:18:54 -0700224
sergeyv9fbb0b52016-11-23 10:27:33 -0800225 if (isMutable && isHardware) {
Hans Boehme5b337d2019-01-07 17:42:05 -0800226 doThrowIAE(env, "Bitmaps with Config.HARDWARE are always immutable");
sergeyv9fbb0b52016-11-23 10:27:33 -0800227 return nullObjectReturn("Cannot create mutable hardware bitmap");
228 }
229
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500230 // Create the codec.
231 NinePatchPeeker peeker;
Leon Scroggins IIIc782ad82018-01-14 10:50:45 -0500232 std::unique_ptr<SkAndroidCodec> codec;
233 {
234 SkCodec::Result result;
235 std::unique_ptr<SkCodec> c = SkCodec::MakeFromStream(std::move(stream), &result,
236 &peeker);
237 if (!c) {
238 SkString msg;
239 msg.printf("Failed to create image decoder with message '%s'",
240 SkCodec::ResultToString(result));
241 return nullObjectReturn(msg.c_str());
242 }
243
244 codec = SkAndroidCodec::MakeFromCodec(std::move(c));
245 if (!codec) {
246 return nullObjectReturn("SkAndroidCodec::MakeFromCodec returned null");
247 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 }
Elliott Hughesa3804cf2011-04-11 16:50:19 -0700249
Matt Sarett3b1b68d2015-12-14 13:08:33 -0500250 // Do not allow ninepatch decodes to 565. In the past, decodes to 565
251 // would dither, and we do not want to pre-dither ninepatches, since we
252 // know that they will be stretched. We no longer dither 565 decodes,
253 // but we continue to prevent ninepatches from decoding to 565, in order
254 // to maintain the old behavior.
255 if (peeker.mPatch && kRGB_565_SkColorType == prefColorType) {
256 prefColorType = kN32_SkColorType;
257 }
258
Anton Daubert4e5ec342016-03-07 17:30:20 +0100259 // Determine the output size.
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500260 SkISize size = codec->getSampledDimensions(sampleSize);
Anton Daubert4e5ec342016-03-07 17:30:20 +0100261
262 int scaledWidth = size.width();
263 int scaledHeight = size.height();
264 bool willScale = false;
265
266 // Apply a fine scaling step if necessary.
267 if (needsFineScale(codec->getInfo().dimensions(), size, sampleSize)) {
268 willScale = true;
269 scaledWidth = codec->getInfo().width() / sampleSize;
270 scaledHeight = codec->getInfo().height() / sampleSize;
271 }
272
Romain Guye8d2ebb2017-02-09 18:38:47 -0800273 // Set the decode colorType
274 SkColorType decodeColorType = codec->computeOutputColorType(prefColorType);
Leon Scroggins IIIee3bfe72019-01-31 08:42:23 -0500275 if (decodeColorType == kRGBA_F16_SkColorType && isHardware &&
276 !uirenderer::HardwareBitmapUploader::hasFP16Support()) {
277 decodeColorType = kN32_SkColorType;
278 }
279
Romain Guy95648b82017-04-13 18:43:42 -0700280 sk_sp<SkColorSpace> decodeColorSpace = codec->computeOutputColorSpace(
281 decodeColorType, prefColorSpace);
Romain Guye8d2ebb2017-02-09 18:38:47 -0800282
Anton Daubert4e5ec342016-03-07 17:30:20 +0100283 // Set the options and return if the client only wants the size.
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500284 if (options != NULL) {
Leon Scroggins III407b5442019-11-22 17:10:20 -0500285 jstring mimeType = getMimeTypeAsJavaString(env, codec->getEncodedFormat());
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500286 if (env->ExceptionCheck()) {
Leon Scroggins III407b5442019-11-22 17:10:20 -0500287 return nullObjectReturn("OOM in getMimeTypeAsJavaString()");
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500288 }
Anton Daubert4e5ec342016-03-07 17:30:20 +0100289 env->SetIntField(options, gOptions_widthFieldID, scaledWidth);
290 env->SetIntField(options, gOptions_heightFieldID, scaledHeight);
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500291 env->SetObjectField(options, gOptions_mimeFieldID, mimeType);
292
Leon Scroggins IIIef691a3d2017-07-10 17:03:55 -0400293 jint configID = GraphicsJNI::colorTypeToLegacyBitmapConfig(decodeColorType);
Romain Guye8d2ebb2017-02-09 18:38:47 -0800294 if (isHardware) {
295 configID = GraphicsJNI::kHardware_LegacyBitmapConfig;
296 }
297 jobject config = env->CallStaticObjectMethod(gBitmapConfig_class,
298 gBitmapConfig_nativeToConfigMethodID, configID);
299 env->SetObjectField(options, gOptions_outConfigFieldID, config);
300
Romain Guy90fc43b2017-03-30 12:35:26 -0700301 env->SetObjectField(options, gOptions_outColorSpaceFieldID,
Derek Sollenbergerbf3e4642019-01-30 11:28:27 -0500302 GraphicsJNI::getColorSpace(env, decodeColorSpace.get(), decodeColorType));
Romain Guy90fc43b2017-03-30 12:35:26 -0700303
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500304 if (onlyDecodeSize) {
305 return nullptr;
306 }
307 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308
Anton Daubert4e5ec342016-03-07 17:30:20 +0100309 // Scale is necessary due to density differences.
310 if (scale != 1.0f) {
311 willScale = true;
312 scaledWidth = static_cast<int>(scaledWidth * scale + 0.5f);
313 scaledHeight = static_cast<int>(scaledHeight * scale + 0.5f);
314 }
315
sergeyvc1c54062016-10-19 18:47:26 -0700316 android::Bitmap* reuseBitmap = nullptr;
Chris Craik9f583612013-05-20 18:13:47 -0700317 unsigned int existingBufferSize = 0;
Leon Scroggins III71fae622019-03-26 16:28:41 -0400318 if (javaBitmap != nullptr) {
319 reuseBitmap = &bitmap::toBitmap(inBitmapHandle);
sergeyvc69853c2016-10-07 14:14:09 -0700320 if (reuseBitmap->isImmutable()) {
Derek Sollenberger2a6ecae2012-08-31 14:03:51 -0400321 ALOGW("Unable to reuse an immutable bitmap as an image decoder target.");
Leon Scroggins IIIca8aef62019-03-26 12:11:27 -0400322 javaBitmap = nullptr;
John Reckf29ed282015-04-07 07:32:03 -0700323 reuseBitmap = nullptr;
Chris Craik7e8c03c2013-06-03 13:53:36 -0700324 } else {
Leon Scroggins IIIca8aef62019-03-26 12:11:27 -0400325 existingBufferSize = reuseBitmap->getAllocationByteCount();
Derek Sollenberger2a6ecae2012-08-31 14:03:51 -0400326 }
Chet Haase37f74ca2010-12-08 17:56:36 -0800327 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328
sergeyv45082182016-09-29 18:25:40 -0700329 HeapAllocator defaultAllocator;
John Reckf29ed282015-04-07 07:32:03 -0700330 RecyclingPixelAllocator recyclingAllocator(reuseBitmap, existingBufferSize);
Chris Craik905e8242013-06-05 09:59:05 -0700331 ScaleCheckingAllocator scaleCheckingAllocator(scale, existingBufferSize);
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500332 SkBitmap::HeapAllocator heapAllocator;
333 SkBitmap::Allocator* decodeAllocator;
334 if (javaBitmap != nullptr && willScale) {
335 // This will allocate pixels using a HeapAllocator, since there will be an extra
336 // scaling step that copies these pixels into Java memory. This allocator
337 // also checks that the recycled javaBitmap is large enough.
338 decodeAllocator = &scaleCheckingAllocator;
339 } else if (javaBitmap != nullptr) {
340 decodeAllocator = &recyclingAllocator;
sergeyvda6c8ffc2016-11-22 18:28:54 -0800341 } else if (willScale || isHardware) {
342 // This will allocate pixels using a HeapAllocator,
343 // for scale case: there will be an extra scaling step.
344 // for hardware case: there will be extra swizzling & upload to gralloc step.
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500345 decodeAllocator = &heapAllocator;
346 } else {
sergeyv45082182016-09-29 18:25:40 -0700347 decodeAllocator = &defaultAllocator;
Chris Craik905e8242013-06-05 09:59:05 -0700348 }
349
Matt Sarett9e7cd632015-12-11 10:54:28 -0500350 SkAlphaType alphaType = codec->computeOutputAlphaType(requireUnpremultiplied);
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500351
Romain Guy253f2c22016-09-28 17:34:42 -0700352 const SkImageInfo decodeInfo = SkImageInfo::Make(size.width(), size.height(),
Romain Guy90fc43b2017-03-30 12:35:26 -0700353 decodeColorType, alphaType, decodeColorSpace);
Matt Sarett6c382572017-02-21 17:42:41 -0500354
Matt Sarett327c7202017-02-22 17:38:20 -0500355 SkImageInfo bitmapInfo = decodeInfo;
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500356 if (decodeColorType == kGray_8_SkColorType) {
357 // The legacy implementation of BitmapFactory used kAlpha8 for
358 // grayscale images (before kGray8 existed). While the codec
359 // recognizes kGray8, we need to decode into a kAlpha8 bitmap
360 // in order to avoid a behavior change.
Matt Sarettee80c472016-06-03 10:23:38 -0400361 bitmapInfo =
362 bitmapInfo.makeColorType(kAlpha_8_SkColorType).makeAlphaType(kPremul_SkAlphaType);
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500363 }
Chris Craik7e8c03c2013-06-03 13:53:36 -0700364 SkBitmap decodingBitmap;
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500365 if (!decodingBitmap.setInfo(bitmapInfo) ||
Mike Reed81397c42017-07-18 17:04:16 -0400366 !decodingBitmap.tryAllocPixels(decodeAllocator)) {
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500367 // SkAndroidCodec should recommend a valid SkImageInfo, so setInfo()
368 // should only only fail if the calculated value for rowBytes is too
369 // large.
370 // tryAllocPixels() can fail due to OOM on the Java heap, OOM on the
371 // native heap, or the recycled javaBitmap being too small to reuse.
372 return nullptr;
Mike Reedc70e06b2009-04-24 11:09:12 -0400373 }
374
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500375 // Use SkAndroidCodec to perform the decode.
376 SkAndroidCodec::AndroidOptions codecOptions;
Romain Guy253f2c22016-09-28 17:34:42 -0700377 codecOptions.fZeroInitialized = decodeAllocator == &defaultAllocator ?
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500378 SkCodec::kYes_ZeroInitialized : SkCodec::kNo_ZeroInitialized;
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500379 codecOptions.fSampleSize = sampleSize;
380 SkCodec::Result result = codec->getAndroidPixels(decodeInfo, decodingBitmap.getPixels(),
381 decodingBitmap.rowBytes(), &codecOptions);
382 switch (result) {
383 case SkCodec::kSuccess:
384 case SkCodec::kIncompleteInput:
385 break;
386 default:
Matt Sarett3b1b68d2015-12-14 13:08:33 -0500387 return nullObjectReturn("codec->getAndroidPixels() failed.");
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500388 }
389
Stan Iliev7aedf6f2017-12-20 12:22:59 -0500390 // This is weird so let me explain: we could use the scale parameter
391 // directly, but for historical reasons this is how the corresponding
392 // Dalvik code has always behaved. We simply recreate the behavior here.
393 // The result is slightly different from simply using scale because of
394 // the 0.5f rounding bias applied when computing the target image size
395 const float scaleX = scaledWidth / float(decodingBitmap.width());
396 const float scaleY = scaledHeight / float(decodingBitmap.height());
397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 jbyteArray ninePatchChunk = NULL;
Chris Craik47cd8e92014-07-08 17:13:08 -0700399 if (peeker.mPatch != NULL) {
Romain Guy7b2f8b82012-03-19 17:18:54 -0700400 if (willScale) {
Stan Iliev7aedf6f2017-12-20 12:22:59 -0500401 peeker.scale(scaleX, scaleY, scaledWidth, scaledHeight);
Romain Guy7b2f8b82012-03-19 17:18:54 -0700402 }
403
Chris Craik47cd8e92014-07-08 17:13:08 -0700404 size_t ninePatchArraySize = peeker.mPatch->serializedSize();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 ninePatchChunk = env->NewByteArray(ninePatchArraySize);
Romain Guy7b2f8b82012-03-19 17:18:54 -0700406 if (ninePatchChunk == NULL) {
Mike Reedc70e06b2009-04-24 11:09:12 -0400407 return nullObjectReturn("ninePatchChunk == null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 }
Romain Guy7b2f8b82012-03-19 17:18:54 -0700409
410 jbyte* array = (jbyte*) env->GetPrimitiveArrayCritical(ninePatchChunk, NULL);
411 if (array == NULL) {
Mike Reedc70e06b2009-04-24 11:09:12 -0400412 return nullObjectReturn("primitive array == null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 }
Romain Guy7b2f8b82012-03-19 17:18:54 -0700414
Chris Craik47cd8e92014-07-08 17:13:08 -0700415 memcpy(array, peeker.mPatch, peeker.mPatchSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 env->ReleasePrimitiveArrayCritical(ninePatchChunk, array, 0);
417 }
418
Chris Craik47cd8e92014-07-08 17:13:08 -0700419 jobject ninePatchInsets = NULL;
420 if (peeker.mHasInsets) {
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400421 ninePatchInsets = peeker.createNinePatchInsets(env, scale);
Mathieu Chartiera08d10f2014-08-29 16:55:55 -0700422 if (ninePatchInsets == NULL) {
423 return nullObjectReturn("nine patch insets == null");
424 }
Amith Yamasaniec4a5042012-04-04 10:27:15 -0700425 if (javaBitmap != NULL) {
Chris Craik47cd8e92014-07-08 17:13:08 -0700426 env->SetObjectField(javaBitmap, gBitmap_ninePatchInsetsFieldID, ninePatchInsets);
Amith Yamasaniec4a5042012-04-04 10:27:15 -0700427 }
428 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429
John Reckf29ed282015-04-07 07:32:03 -0700430 SkBitmap outputBitmap;
Romain Guy7b2f8b82012-03-19 17:18:54 -0700431 if (willScale) {
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500432 // Set the allocator for the outputBitmap.
433 SkBitmap::Allocator* outputAllocator;
434 if (javaBitmap != nullptr) {
435 outputAllocator = &recyclingAllocator;
436 } else {
sergeyv45082182016-09-29 18:25:40 -0700437 outputAllocator = &defaultAllocator;
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500438 }
439
Leon Scroggins IIIef691a3d2017-07-10 17:03:55 -0400440 SkColorType scaledColorType = decodingBitmap.colorType();
Leon Scroggins III8790be62013-12-03 16:26:51 -0500441 // FIXME: If the alphaType is kUnpremul and the image has alpha, the
442 // colors may not be correct, since Skia does not yet support drawing
443 // to/from unpremultiplied bitmaps.
Matt Sarettee80c472016-06-03 10:23:38 -0400444 outputBitmap.setInfo(
445 bitmapInfo.makeWH(scaledWidth, scaledHeight).makeColorType(scaledColorType));
Mike Reed81397c42017-07-18 17:04:16 -0400446 if (!outputBitmap.tryAllocPixels(outputAllocator)) {
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500447 // This should only fail on OOM. The recyclingAllocator should have
448 // enough memory since we check this before decoding using the
449 // scaleCheckingAllocator.
Raph Levien005bfc62012-09-20 22:51:47 -0700450 return nullObjectReturn("allocation failed for scaled bitmap");
451 }
Leon Scroggins III1ffe7272013-09-19 11:34:06 -0400452
Romain Guy7b2f8b82012-03-19 17:18:54 -0700453 SkPaint paint;
Romain Guy253f2c22016-09-28 17:34:42 -0700454 // kSrc_Mode instructs us to overwrite the uninitialized pixels in
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500455 // outputBitmap. Otherwise we would blend by default, which is not
456 // what we want.
Mike Reed260ab722016-10-07 15:59:20 -0400457 paint.setBlendMode(SkBlendMode::kSrc);
Romain Guy253f2c22016-09-28 17:34:42 -0700458 paint.setFilterQuality(kLow_SkFilterQuality); // bilinear filtering
Romain Guy7b2f8b82012-03-19 17:18:54 -0700459
Matt Sarettca9b7032017-04-13 12:18:47 -0400460 SkCanvas canvas(outputBitmap, SkCanvas::ColorBehavior::kLegacy);
Stan Iliev7aedf6f2017-12-20 12:22:59 -0500461 canvas.scale(scaleX, scaleY);
Chris Craik7e8c03c2013-06-03 13:53:36 -0700462 canvas.drawBitmap(decodingBitmap, 0.0f, 0.0f, &paint);
463 } else {
John Reckf29ed282015-04-07 07:32:03 -0700464 outputBitmap.swap(decodingBitmap);
Romain Guy7b2f8b82012-03-19 17:18:54 -0700465 }
466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 if (padding) {
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400468 peeker.getPadding(env, padding);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500471 // If we get here, the outputBitmap should have an installed pixelref.
John Reckf29ed282015-04-07 07:32:03 -0700472 if (outputBitmap.pixelRef() == NULL) {
Marco Nelissenb2fe3be2012-05-07 11:24:13 -0700473 return nullObjectReturn("Got null SkPixelRef");
474 }
Romain Guy23610982011-01-17 12:51:55 -0800475
Chris Craik7e8c03c2013-06-03 13:53:36 -0700476 if (!isMutable && javaBitmap == NULL) {
Romain Guy23610982011-01-17 12:51:55 -0800477 // promise we will never change our pixels (great for sharing and pictures)
John Reckf29ed282015-04-07 07:32:03 -0700478 outputBitmap.setImmutable();
Romain Guy23610982011-01-17 12:51:55 -0800479 }
Chet Haase37f74ca2010-12-08 17:56:36 -0800480
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500481 bool isPremultiplied = !requireUnpremultiplied;
482 if (javaBitmap != nullptr) {
sergeyvc69853c2016-10-07 14:14:09 -0700483 bitmap::reinitBitmap(env, javaBitmap, outputBitmap.info(), isPremultiplied);
John Reckf29ed282015-04-07 07:32:03 -0700484 outputBitmap.notifyPixelsChanged();
Chet Haase37f74ca2010-12-08 17:56:36 -0800485 // If a java bitmap was passed in for reuse, pass it back
486 return javaBitmap;
487 }
Chris Craik1abf5d62013-08-16 12:47:03 -0700488
489 int bitmapCreateFlags = 0x0;
sergeyvc69853c2016-10-07 14:14:09 -0700490 if (isMutable) bitmapCreateFlags |= android::bitmap::kBitmapCreateFlag_Mutable;
491 if (isPremultiplied) bitmapCreateFlags |= android::bitmap::kBitmapCreateFlag_Premultiplied;
Chris Craik1abf5d62013-08-16 12:47:03 -0700492
sergeyvda6c8ffc2016-11-22 18:28:54 -0800493 if (isHardware) {
494 sk_sp<Bitmap> hardwareBitmap = Bitmap::allocateHardwareBitmap(outputBitmap);
sergeyvd67bb1e2017-06-28 12:44:03 -0700495 if (!hardwareBitmap.get()) {
496 return nullObjectReturn("Failed to allocate a hardware bitmap");
497 }
sergeyvda6c8ffc2016-11-22 18:28:54 -0800498 return bitmap::createBitmap(env, hardwareBitmap.release(), bitmapCreateFlags,
499 ninePatchChunk, ninePatchInsets, -1);
500 }
501
Mike Reedc70e06b2009-04-24 11:09:12 -0400502 // now create the java bitmap
Leon Scroggins III300af262019-03-14 08:07:15 -0400503 return bitmap::createBitmap(env, defaultAllocator.getStorageObjAndReset(),
504 bitmapCreateFlags, ninePatchChunk, ninePatchInsets, -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505}
506
Chris Craik905e8242013-06-05 09:59:05 -0700507static jobject nativeDecodeStream(JNIEnv* env, jobject clazz, jobject is, jbyteArray storage,
Leon Scroggins III71fae622019-03-26 16:28:41 -0400508 jobject padding, jobject options, jlong inBitmapHandle, jlong colorSpaceHandle) {
Romain Guy7b2f8b82012-03-19 17:18:54 -0700509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 jobject bitmap = NULL;
Ben Wagner60126ef2015-08-07 12:13:48 -0400511 std::unique_ptr<SkStream> stream(CreateJavaInputStreamAdaptor(env, is, storage));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512
Leon Scroggins IIIca320212013-08-20 17:59:39 -0400513 if (stream.get()) {
Ben Wagner60126ef2015-08-07 12:13:48 -0400514 std::unique_ptr<SkStreamRewindable> bufferedStream(
Mike Reedbe896ed2017-09-19 17:01:30 -0400515 SkFrontBufferedStream::Make(std::move(stream), SkCodec::MinBufferedBytesNeeded()));
Leon Scroggins III7315f1b2013-09-10 20:26:05 -0400516 SkASSERT(bufferedStream.get() != NULL);
Leon Scroggins III71fae622019-03-26 16:28:41 -0400517 bitmap = doDecode(env, std::move(bufferedStream), padding, options, inBitmapHandle,
518 colorSpaceHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 }
520 return bitmap;
521}
522
Romain Guy7b2f8b82012-03-19 17:18:54 -0700523static jobject nativeDecodeFileDescriptor(JNIEnv* env, jobject clazz, jobject fileDescriptor,
Leon Scroggins III71fae622019-03-26 16:28:41 -0400524 jobject padding, jobject bitmapFactoryOptions, jlong inBitmapHandle, jlong colorSpaceHandle) {
Romain Guy7b2f8b82012-03-19 17:18:54 -0700525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 NPE_CHECK_RETURN_ZERO(env, fileDescriptor);
527
Derek Sollenberger5cb769d2014-09-24 09:20:09 -0400528 int descriptor = jniGetFDFromFileDescriptor(env, fileDescriptor);
Mike Reedc70e06b2009-04-24 11:09:12 -0400529
Derek Sollenberger5827cb52013-07-26 14:58:06 -0400530 struct stat fdStat;
531 if (fstat(descriptor, &fdStat) == -1) {
532 doThrowIOE(env, "broken file descriptor");
533 return nullObjectReturn("fstat return -1");
534 }
535
Derek Sollenberger5cb769d2014-09-24 09:20:09 -0400536 // Restore the descriptor's offset on exiting this function. Even though
537 // we dup the descriptor, both the original and dup refer to the same open
538 // file description and changes to the file offset in one impact the other.
Jérôme Poichetd29c9022014-09-26 18:59:11 +0000539 AutoFDSeek autoRestore(descriptor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540
Derek Sollenberger5cb769d2014-09-24 09:20:09 -0400541 // Duplicate the descriptor here to prevent leaking memory. A leak occurs
542 // if we only close the file descriptor and not the file object it is used to
543 // create. If we don't explicitly clean up the file (which in turn closes the
544 // descriptor) the buffers allocated internally by fseek will be leaked.
Nick Kralevich4b3a08c2019-01-28 10:39:10 -0800545 int dupDescriptor = fcntl(descriptor, F_DUPFD_CLOEXEC, 0);
Derek Sollenberger5cb769d2014-09-24 09:20:09 -0400546
547 FILE* file = fdopen(dupDescriptor, "r");
Leon Scroggins III0102f8a2014-01-14 15:14:57 -0500548 if (file == NULL) {
Derek Sollenberger5cb769d2014-09-24 09:20:09 -0400549 // cleanup the duplicated descriptor since it will not be closed when the
550 // file is cleaned up (fclose).
551 close(dupDescriptor);
Leon Scroggins III0102f8a2014-01-14 15:14:57 -0500552 return nullObjectReturn("Could not open file");
Leon Scroggins IIIf65183f2013-10-07 16:32:14 -0400553 }
Leon Scroggins III0102f8a2014-01-14 15:14:57 -0500554
Leon Scroggins IIIdd3c06c2017-03-10 10:50:33 -0500555 std::unique_ptr<SkFILEStream> fileStream(new SkFILEStream(file));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556
Yujie Qinc1d7b7f2016-02-29 14:00:29 +0100557 // If there is no offset for the file descriptor, we use SkFILEStream directly.
558 if (::lseek(descriptor, 0, SEEK_CUR) == 0) {
559 assert(isSeekable(dupDescriptor));
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500560 return doDecode(env, std::move(fileStream), padding, bitmapFactoryOptions,
Leon Scroggins III71fae622019-03-26 16:28:41 -0400561 inBitmapHandle, colorSpaceHandle);
Yujie Qinc1d7b7f2016-02-29 14:00:29 +0100562 }
563
Leon Scroggins III0aa39dc2014-06-03 12:19:32 -0400564 // Use a buffered stream. Although an SkFILEStream can be rewound, this
565 // ensures that SkImageDecoder::Factory never rewinds beyond the
566 // current position of the file descriptor.
Mike Reedbe896ed2017-09-19 17:01:30 -0400567 std::unique_ptr<SkStreamRewindable> stream(SkFrontBufferedStream::Make(std::move(fileStream),
Matt Sarett96ffbdc2016-02-11 17:03:54 -0500568 SkCodec::MinBufferedBytesNeeded()));
Leon Scroggins III2826e5f2014-02-05 19:46:02 -0500569
Leon Scroggins III71fae622019-03-26 16:28:41 -0400570 return doDecode(env, std::move(stream), padding, bitmapFactoryOptions, inBitmapHandle,
571 colorSpaceHandle);
Mike Reedc70e06b2009-04-24 11:09:12 -0400572}
573
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000574static jobject nativeDecodeAsset(JNIEnv* env, jobject clazz, jlong native_asset,
Leon Scroggins III71fae622019-03-26 16:28:41 -0400575 jobject padding, jobject options, jlong inBitmapHandle, jlong colorSpaceHandle) {
Romain Guy7b2f8b82012-03-19 17:18:54 -0700576
Mike Reedc70e06b2009-04-24 11:09:12 -0400577 Asset* asset = reinterpret_cast<Asset*>(native_asset);
Leon Scroggins III0aa39dc2014-06-03 12:19:32 -0400578 // since we know we'll be done with the asset when we return, we can
579 // just use a simple wrapper
Mike Reed3cf42822019-12-12 11:59:49 -0500580 return doDecode(env, std::make_unique<AssetStreamAdaptor>(asset), padding, options,
Leon Scroggins III71fae622019-03-26 16:28:41 -0400581 inBitmapHandle, colorSpaceHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582}
583
584static jobject nativeDecodeByteArray(JNIEnv* env, jobject, jbyteArray byteArray,
Leon Scroggins III71fae622019-03-26 16:28:41 -0400585 jint offset, jint length, jobject options, jlong inBitmapHandle, jlong colorSpaceHandle) {
Romain Guy7b2f8b82012-03-19 17:18:54 -0700586
Mike Reedc70e06b2009-04-24 11:09:12 -0400587 AutoJavaByteArray ar(env, byteArray);
Mike Reed3cf42822019-12-12 11:59:49 -0500588 return doDecode(env, std::make_unique<SkMemoryStream>(ar.ptr() + offset, length, false),
Leon Scroggins III71fae622019-03-26 16:28:41 -0400589 nullptr, options, inBitmapHandle, colorSpaceHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590}
591
Owen Lina9d0d472011-01-18 17:39:15 +0800592static jboolean nativeIsSeekable(JNIEnv* env, jobject, jobject fileDescriptor) {
Elliott Hughesa3804cf2011-04-11 16:50:19 -0700593 jint descriptor = jniGetFDFromFileDescriptor(env, fileDescriptor);
Yujie Qinc1d7b7f2016-02-29 14:00:29 +0100594 return isSeekable(descriptor) ? JNI_TRUE : JNI_FALSE;
Owen Lina9d0d472011-01-18 17:39:15 +0800595}
596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597///////////////////////////////////////////////////////////////////////////////
598
Daniel Micay76f6a862015-09-19 17:31:01 -0400599static const JNINativeMethod gMethods[] = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 { "nativeDecodeStream",
Leon Scroggins III71fae622019-03-26 16:28:41 -0400601 "(Ljava/io/InputStream;[BLandroid/graphics/Rect;Landroid/graphics/BitmapFactory$Options;JJ)Landroid/graphics/Bitmap;",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 (void*)nativeDecodeStream
603 },
604
605 { "nativeDecodeFileDescriptor",
Leon Scroggins III71fae622019-03-26 16:28:41 -0400606 "(Ljava/io/FileDescriptor;Landroid/graphics/Rect;Landroid/graphics/BitmapFactory$Options;JJ)Landroid/graphics/Bitmap;",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 (void*)nativeDecodeFileDescriptor
608 },
609
610 { "nativeDecodeAsset",
Leon Scroggins III71fae622019-03-26 16:28:41 -0400611 "(JLandroid/graphics/Rect;Landroid/graphics/BitmapFactory$Options;JJ)Landroid/graphics/Bitmap;",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 (void*)nativeDecodeAsset
613 },
614
615 { "nativeDecodeByteArray",
Leon Scroggins III71fae622019-03-26 16:28:41 -0400616 "([BIILandroid/graphics/BitmapFactory$Options;JJ)Landroid/graphics/Bitmap;",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 (void*)nativeDecodeByteArray
618 },
619
Owen Lina9d0d472011-01-18 17:39:15 +0800620 { "nativeIsSeekable",
621 "(Ljava/io/FileDescriptor;)Z",
622 (void*)nativeIsSeekable
623 },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624};
625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626int register_android_graphics_BitmapFactory(JNIEnv* env) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800627 jclass options_class = FindClassOrDie(env, "android/graphics/BitmapFactory$Options");
628 gOptions_bitmapFieldID = GetFieldIDOrDie(env, options_class, "inBitmap",
Chris Craik47cd8e92014-07-08 17:13:08 -0700629 "Landroid/graphics/Bitmap;");
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800630 gOptions_justBoundsFieldID = GetFieldIDOrDie(env, options_class, "inJustDecodeBounds", "Z");
631 gOptions_sampleSizeFieldID = GetFieldIDOrDie(env, options_class, "inSampleSize", "I");
632 gOptions_configFieldID = GetFieldIDOrDie(env, options_class, "inPreferredConfig",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 "Landroid/graphics/Bitmap$Config;");
Romain Guy95648b82017-04-13 18:43:42 -0700634 gOptions_colorSpaceFieldID = GetFieldIDOrDie(env, options_class, "inPreferredColorSpace",
635 "Landroid/graphics/ColorSpace;");
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800636 gOptions_premultipliedFieldID = GetFieldIDOrDie(env, options_class, "inPremultiplied", "Z");
637 gOptions_mutableFieldID = GetFieldIDOrDie(env, options_class, "inMutable", "Z");
638 gOptions_ditherFieldID = GetFieldIDOrDie(env, options_class, "inDither", "Z");
639 gOptions_preferQualityOverSpeedFieldID = GetFieldIDOrDie(env, options_class,
Wei-Ta Chen953f9092010-12-03 14:06:18 -0800640 "inPreferQualityOverSpeed", "Z");
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800641 gOptions_scaledFieldID = GetFieldIDOrDie(env, options_class, "inScaled", "Z");
642 gOptions_densityFieldID = GetFieldIDOrDie(env, options_class, "inDensity", "I");
643 gOptions_screenDensityFieldID = GetFieldIDOrDie(env, options_class, "inScreenDensity", "I");
644 gOptions_targetDensityFieldID = GetFieldIDOrDie(env, options_class, "inTargetDensity", "I");
645 gOptions_widthFieldID = GetFieldIDOrDie(env, options_class, "outWidth", "I");
646 gOptions_heightFieldID = GetFieldIDOrDie(env, options_class, "outHeight", "I");
647 gOptions_mimeFieldID = GetFieldIDOrDie(env, options_class, "outMimeType", "Ljava/lang/String;");
Romain Guye8d2ebb2017-02-09 18:38:47 -0800648 gOptions_outConfigFieldID = GetFieldIDOrDie(env, options_class, "outConfig",
649 "Landroid/graphics/Bitmap$Config;");
Romain Guy90fc43b2017-03-30 12:35:26 -0700650 gOptions_outColorSpaceFieldID = GetFieldIDOrDie(env, options_class, "outColorSpace",
651 "Landroid/graphics/ColorSpace;");
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800652 gOptions_mCancelID = GetFieldIDOrDie(env, options_class, "mCancel", "Z");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800654 jclass bitmap_class = FindClassOrDie(env, "android/graphics/Bitmap");
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800655 gBitmap_ninePatchInsetsFieldID = GetFieldIDOrDie(env, bitmap_class, "mNinePatchInsets",
Chris Craik47cd8e92014-07-08 17:13:08 -0700656 "Landroid/graphics/NinePatch$InsetStruct;");
657
Romain Guye8d2ebb2017-02-09 18:38:47 -0800658 gBitmapConfig_class = MakeGlobalRefOrDie(env, FindClassOrDie(env,
659 "android/graphics/Bitmap$Config"));
660 gBitmapConfig_nativeToConfigMethodID = GetStaticMethodIDOrDie(env, gBitmapConfig_class,
661 "nativeToConfig", "(I)Landroid/graphics/Bitmap$Config;");
662
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800663 return android::RegisterMethodsOrDie(env, "android/graphics/BitmapFactory",
664 gMethods, NELEM(gMethods));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665}