blob: cf02051831c670cbb714f6a9c82b83c04908fd34 [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"
Leon Scroggins III2bcdf6f2020-04-21 14:08:32 -04006#include "FrontBufferedStream.h"
Ben Wagner60126ef2015-08-07 12:13:48 -04007#include "GraphicsJNI.h"
Derek Sollenberger2173ea22020-02-19 15:37:29 -05008#include "MimeType.h"
Leon Scrogginsa06d86a2011-03-02 16:56:54 -05009#include "NinePatchPeeker.h"
Matt Sarettb8adc9a2015-12-02 13:35:22 -050010#include "SkAndroidCodec.h"
John Reckbe671952021-01-13 22:39:32 -050011#include "SkCanvas.h"
Leon Scroggins46cb9bd2014-03-06 15:36:39 -050012#include "SkMath.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013#include "SkPixelRef.h"
14#include "SkStream.h"
John Reckbe671952021-01-13 22:39:32 -050015#include "SkString.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016#include "SkUtils.h"
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080017#include "Utils.h"
Ben Wagner60126ef2015-08-07 12:13:48 -040018
Leon Scroggins IIIee3bfe72019-01-31 08:42:23 -050019#include <HardwareBitmapUploader.h>
Orion Hodson329c6122020-06-02 13:22:06 +010020#include <nativehelper/JNIPlatformHelp.h>
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080021#include <androidfw/Asset.h>
22#include <androidfw/ResourceTypes.h>
Chris Craikbd8db2e82014-08-20 16:31:57 -070023#include <cutils/compiler.h>
Derek Sollenbergerc5882c42019-10-25 11:11:32 -040024#include <fcntl.h>
Ben Wagner60126ef2015-08-07 12:13:48 -040025#include <memory>
Leon Scroggins III0102f8a2014-01-14 15:14:57 -050026#include <stdio.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";
Vignesh Venkatasubramanian4a46b9e2020-11-05 22:55:43 -080072 case SkEncodedImageFormat::kAVIF:
73 return "image/avif";
Hal Canary10219fb2016-11-23 20:41:22 -050074 case SkEncodedImageFormat::kWBMP:
Leon Scroggins III407b5442019-11-22 17:10:20 -050075 return "image/vnd.wap.wbmp";
Hal Canary10219fb2016-11-23 20:41:22 -050076 case SkEncodedImageFormat::kDNG:
Leon Scroggins III407b5442019-11-22 17:10:20 -050077 return "image/x-adobe-dng";
Matt Sarettb8adc9a2015-12-02 13:35:22 -050078 default:
Leon Scroggins III407b5442019-11-22 17:10:20 -050079 return nullptr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 }
Leon Scroggins III407b5442019-11-22 17:10:20 -050081}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082
Leon Scroggins III407b5442019-11-22 17:10:20 -050083jstring getMimeTypeAsJavaString(JNIEnv* env, SkEncodedImageFormat format) {
Vladimir Marko7ab249a2015-01-06 18:17:52 +000084 jstring jstr = nullptr;
Leon Scroggins III407b5442019-11-22 17:10:20 -050085 const char* mimeType = getMimeType(format);
Matt Sarettb8adc9a2015-12-02 13:35:22 -050086 if (mimeType) {
Vladimir Marko7ab249a2015-01-06 18:17:52 +000087 // NOTE: Caller should env->ExceptionCheck() for OOM
88 // (can't check for nullptr as it's a valid return value)
Matt Sarettb8adc9a2015-12-02 13:35:22 -050089 jstr = env->NewStringUTF(mimeType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 }
91 return jstr;
92}
93
Chris Craik905e8242013-06-05 09:59:05 -070094class ScaleCheckingAllocator : public SkBitmap::HeapAllocator {
95public:
96 ScaleCheckingAllocator(float scale, int size)
97 : mScale(scale), mSize(size) {
98 }
99
Mike Reed81397c42017-07-18 17:04:16 -0400100 virtual bool allocPixelRef(SkBitmap* bitmap) {
Chris Craik905e8242013-06-05 09:59:05 -0700101 // accounts for scale in final allocation, using eventual size and config
Leon Scroggins IIIef691a3d2017-07-10 17:03:55 -0400102 const int bytesPerPixel = SkColorTypeBytesPerPixel(bitmap->colorType());
Chris Craik905e8242013-06-05 09:59:05 -0700103 const int requestedSize = bytesPerPixel *
104 int(bitmap->width() * mScale + 0.5f) *
105 int(bitmap->height() * mScale + 0.5f);
106 if (requestedSize > mSize) {
107 ALOGW("bitmap for alloc reuse (%d bytes) can't fit scaled bitmap (%d bytes)",
108 mSize, requestedSize);
109 return false;
110 }
Mike Reed81397c42017-07-18 17:04:16 -0400111 return SkBitmap::HeapAllocator::allocPixelRef(bitmap);
Chris Craik905e8242013-06-05 09:59:05 -0700112 }
113private:
114 const float mScale;
115 const int mSize;
116};
117
Chris Craik7e8c03c2013-06-03 13:53:36 -0700118class RecyclingPixelAllocator : public SkBitmap::Allocator {
119public:
sergeyvc1c54062016-10-19 18:47:26 -0700120 RecyclingPixelAllocator(android::Bitmap* bitmap, unsigned int size)
John Reckf29ed282015-04-07 07:32:03 -0700121 : mBitmap(bitmap), mSize(size) {
Chris Craik7e8c03c2013-06-03 13:53:36 -0700122 }
123
124 ~RecyclingPixelAllocator() {
Chris Craik7e8c03c2013-06-03 13:53:36 -0700125 }
126
Mike Reed81397c42017-07-18 17:04:16 -0400127 virtual bool allocPixelRef(SkBitmap* bitmap) {
Leon Scroggins46cb9bd2014-03-06 15:36:39 -0500128 const SkImageInfo& info = bitmap->info();
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400129 if (info.colorType() == kUnknown_SkColorType) {
Leon Scroggins46cb9bd2014-03-06 15:36:39 -0500130 ALOGW("unable to reuse a bitmap as the target has an unknown bitmap configuration");
Chris Craik7e8c03c2013-06-03 13:53:36 -0700131 return false;
132 }
Chris Craikcd0ba712013-09-06 14:40:30 -0700133
Mike Reed7569de02017-10-06 16:25:49 -0400134 const size_t size = info.computeByteSize(bitmap->rowBytes());
135 if (size > SK_MaxS32) {
Leon Scroggins46cb9bd2014-03-06 15:36:39 -0500136 ALOGW("bitmap is too large");
137 return false;
138 }
139
Leon Scroggins46cb9bd2014-03-06 15:36:39 -0500140 if (size > mSize) {
Dan Albert46d84442014-11-18 16:07:51 -0800141 ALOGW("bitmap marked for reuse (%u bytes) can't fit new bitmap "
142 "(%zu bytes)", mSize, size);
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500143 return false;
144 }
145
Leon Scroggins IIIf51a80d2017-07-12 10:46:35 -0400146 mBitmap->reconfigure(info, bitmap->rowBytes());
Mike Reed826deef2017-04-04 15:32:04 -0400147 bitmap->setPixelRef(sk_ref_sp(mBitmap), 0, 0);
Chris Craik7e8c03c2013-06-03 13:53:36 -0700148 return true;
149 }
150
151private:
sergeyvc1c54062016-10-19 18:47:26 -0700152 android::Bitmap* const mBitmap;
Chris Craik7e8c03c2013-06-03 13:53:36 -0700153 const unsigned int mSize;
154};
155
Anton Daubert4e5ec342016-03-07 17:30:20 +0100156// Necessary for decodes when the native decoder cannot scale to appropriately match the sampleSize
157// (for example, RAW). If the sampleSize divides evenly into the dimension, we require that the
158// scale matches exactly. If sampleSize does not divide evenly, we allow the decoder to choose how
159// best to round.
160static bool needsFineScale(const int fullSize, const int decodedSize, const int sampleSize) {
161 if (fullSize % sampleSize == 0 && fullSize / sampleSize != decodedSize) {
162 return true;
163 } else if ((fullSize / sampleSize + 1) != decodedSize &&
164 (fullSize / sampleSize) != decodedSize) {
165 return true;
166 }
167 return false;
168}
169
170static bool needsFineScale(const SkISize fullSize, const SkISize decodedSize,
171 const int sampleSize) {
172 return needsFineScale(fullSize.width(), decodedSize.width(), sampleSize) ||
173 needsFineScale(fullSize.height(), decodedSize.height(), sampleSize);
174}
175
Mike Reedc7c65602017-07-26 10:40:25 -0400176static jobject doDecode(JNIEnv* env, std::unique_ptr<SkStreamRewindable> stream,
Leon Scroggins III71fae622019-03-26 16:28:41 -0400177 jobject padding, jobject options, jlong inBitmapHandle,
178 jlong colorSpaceHandle) {
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500179 // Set default values for the options parameters.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 int sampleSize = 1;
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500181 bool onlyDecodeSize = false;
Mike Reed42a1d082014-07-07 18:06:18 -0400182 SkColorType prefColorType = kN32_SkColorType;
sergeyvda6c8ffc2016-11-22 18:28:54 -0800183 bool isHardware = false;
Romain Guy23610982011-01-17 12:51:55 -0800184 bool isMutable = false;
Chris Craik905e8242013-06-05 09:59:05 -0700185 float scale = 1.0f;
Chris Craik1abf5d62013-08-16 12:47:03 -0700186 bool requireUnpremultiplied = false;
Chet Haase37f74ca2010-12-08 17:56:36 -0800187 jobject javaBitmap = NULL;
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500188 sk_sp<SkColorSpace> prefColorSpace = GraphicsJNI::getNativeColorSpace(colorSpaceHandle);
Elliott Hughesa3804cf2011-04-11 16:50:19 -0700189
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500190 // Update with options supplied by the client.
Romain Guy7b2f8b82012-03-19 17:18:54 -0700191 if (options != NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 sampleSize = env->GetIntField(options, gOptions_sampleSizeFieldID);
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500193 // Correct a non-positive sampleSize. sampleSize defaults to zero within the
194 // options object, which is strange.
195 if (sampleSize <= 0) {
196 sampleSize = 1;
197 }
198
199 if (env->GetBooleanField(options, gOptions_justBoundsFieldID)) {
200 onlyDecodeSize = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 }
Romain Guy7b2f8b82012-03-19 17:18:54 -0700202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 // initialize these, in case we fail later on
204 env->SetIntField(options, gOptions_widthFieldID, -1);
205 env->SetIntField(options, gOptions_heightFieldID, -1);
206 env->SetObjectField(options, gOptions_mimeFieldID, 0);
Romain Guye8d2ebb2017-02-09 18:38:47 -0800207 env->SetObjectField(options, gOptions_outConfigFieldID, 0);
Romain Guy90fc43b2017-03-30 12:35:26 -0700208 env->SetObjectField(options, gOptions_outColorSpaceFieldID, 0);
Elliott Hughesa3804cf2011-04-11 16:50:19 -0700209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 jobject jconfig = env->GetObjectField(options, gOptions_configFieldID);
Mike Reed42a1d082014-07-07 18:06:18 -0400211 prefColorType = GraphicsJNI::getNativeBitmapColorType(env, jconfig);
sergeyvda6c8ffc2016-11-22 18:28:54 -0800212 isHardware = GraphicsJNI::isHardwareConfig(env, jconfig);
Romain Guy23610982011-01-17 12:51:55 -0800213 isMutable = env->GetBooleanField(options, gOptions_mutableFieldID);
Chris Craik1abf5d62013-08-16 12:47:03 -0700214 requireUnpremultiplied = !env->GetBooleanField(options, gOptions_premultipliedFieldID);
Chet Haase37f74ca2010-12-08 17:56:36 -0800215 javaBitmap = env->GetObjectField(options, gOptions_bitmapFieldID);
Chris Craik905e8242013-06-05 09:59:05 -0700216
217 if (env->GetBooleanField(options, gOptions_scaledFieldID)) {
218 const int density = env->GetIntField(options, gOptions_densityFieldID);
219 const int targetDensity = env->GetIntField(options, gOptions_targetDensityFieldID);
220 const int screenDensity = env->GetIntField(options, gOptions_screenDensityFieldID);
221 if (density != 0 && targetDensity != 0 && density != screenDensity) {
222 scale = (float) targetDensity / density;
223 }
224 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 }
Romain Guy7b2f8b82012-03-19 17:18:54 -0700226
sergeyv9fbb0b52016-11-23 10:27:33 -0800227 if (isMutable && isHardware) {
Hans Boehme5b337d2019-01-07 17:42:05 -0800228 doThrowIAE(env, "Bitmaps with Config.HARDWARE are always immutable");
sergeyv9fbb0b52016-11-23 10:27:33 -0800229 return nullObjectReturn("Cannot create mutable hardware bitmap");
230 }
231
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500232 // Create the codec.
233 NinePatchPeeker peeker;
Leon Scroggins IIIc782ad82018-01-14 10:50:45 -0500234 std::unique_ptr<SkAndroidCodec> codec;
235 {
236 SkCodec::Result result;
237 std::unique_ptr<SkCodec> c = SkCodec::MakeFromStream(std::move(stream), &result,
238 &peeker);
239 if (!c) {
240 SkString msg;
241 msg.printf("Failed to create image decoder with message '%s'",
242 SkCodec::ResultToString(result));
243 return nullObjectReturn(msg.c_str());
244 }
245
246 codec = SkAndroidCodec::MakeFromCodec(std::move(c));
247 if (!codec) {
248 return nullObjectReturn("SkAndroidCodec::MakeFromCodec returned null");
249 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 }
Elliott Hughesa3804cf2011-04-11 16:50:19 -0700251
Matt Sarett3b1b68d2015-12-14 13:08:33 -0500252 // Do not allow ninepatch decodes to 565. In the past, decodes to 565
253 // would dither, and we do not want to pre-dither ninepatches, since we
254 // know that they will be stretched. We no longer dither 565 decodes,
255 // but we continue to prevent ninepatches from decoding to 565, in order
256 // to maintain the old behavior.
257 if (peeker.mPatch && kRGB_565_SkColorType == prefColorType) {
258 prefColorType = kN32_SkColorType;
259 }
260
Anton Daubert4e5ec342016-03-07 17:30:20 +0100261 // Determine the output size.
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500262 SkISize size = codec->getSampledDimensions(sampleSize);
Anton Daubert4e5ec342016-03-07 17:30:20 +0100263
264 int scaledWidth = size.width();
265 int scaledHeight = size.height();
266 bool willScale = false;
267
268 // Apply a fine scaling step if necessary.
269 if (needsFineScale(codec->getInfo().dimensions(), size, sampleSize)) {
270 willScale = true;
271 scaledWidth = codec->getInfo().width() / sampleSize;
272 scaledHeight = codec->getInfo().height() / sampleSize;
273 }
274
Romain Guye8d2ebb2017-02-09 18:38:47 -0800275 // Set the decode colorType
276 SkColorType decodeColorType = codec->computeOutputColorType(prefColorType);
Leon Scroggins IIIee3bfe72019-01-31 08:42:23 -0500277 if (decodeColorType == kRGBA_F16_SkColorType && isHardware &&
278 !uirenderer::HardwareBitmapUploader::hasFP16Support()) {
279 decodeColorType = kN32_SkColorType;
280 }
281
Romain Guy95648b82017-04-13 18:43:42 -0700282 sk_sp<SkColorSpace> decodeColorSpace = codec->computeOutputColorSpace(
283 decodeColorType, prefColorSpace);
Romain Guye8d2ebb2017-02-09 18:38:47 -0800284
Anton Daubert4e5ec342016-03-07 17:30:20 +0100285 // Set the options and return if the client only wants the size.
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500286 if (options != NULL) {
Leon Scroggins III407b5442019-11-22 17:10:20 -0500287 jstring mimeType = getMimeTypeAsJavaString(env, codec->getEncodedFormat());
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500288 if (env->ExceptionCheck()) {
Leon Scroggins III407b5442019-11-22 17:10:20 -0500289 return nullObjectReturn("OOM in getMimeTypeAsJavaString()");
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,
Derek Sollenbergerbf3e4642019-01-30 11:28:27 -0500304 GraphicsJNI::getColorSpace(env, decodeColorSpace.get(), 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;
Leon Scroggins III71fae622019-03-26 16:28:41 -0400320 if (javaBitmap != nullptr) {
321 reuseBitmap = &bitmap::toBitmap(inBitmapHandle);
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.");
Leon Scroggins IIIca8aef62019-03-26 12:11:27 -0400324 javaBitmap = nullptr;
John Reckf29ed282015-04-07 07:32:03 -0700325 reuseBitmap = nullptr;
Chris Craik7e8c03c2013-06-03 13:53:36 -0700326 } else {
Leon Scroggins IIIca8aef62019-03-26 12:11:27 -0400327 existingBufferSize = reuseBitmap->getAllocationByteCount();
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
Matt Sarett327c7202017-02-22 17:38:20 -0500357 SkImageInfo bitmapInfo = decodeInfo;
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500358 if (decodeColorType == kGray_8_SkColorType) {
359 // The legacy implementation of BitmapFactory used kAlpha8 for
360 // grayscale images (before kGray8 existed). While the codec
361 // recognizes kGray8, we need to decode into a kAlpha8 bitmap
362 // in order to avoid a behavior change.
Matt Sarettee80c4712016-06-03 10:23:38 -0400363 bitmapInfo =
364 bitmapInfo.makeColorType(kAlpha_8_SkColorType).makeAlphaType(kPremul_SkAlphaType);
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500365 }
Chris Craik7e8c03c2013-06-03 13:53:36 -0700366 SkBitmap decodingBitmap;
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500367 if (!decodingBitmap.setInfo(bitmapInfo) ||
Mike Reed81397c42017-07-18 17:04:16 -0400368 !decodingBitmap.tryAllocPixels(decodeAllocator)) {
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500369 // SkAndroidCodec should recommend a valid SkImageInfo, so setInfo()
370 // should only only fail if the calculated value for rowBytes is too
371 // large.
372 // tryAllocPixels() can fail due to OOM on the Java heap, OOM on the
373 // native heap, or the recycled javaBitmap being too small to reuse.
374 return nullptr;
Mike Reedc70e06b2009-04-24 11:09:12 -0400375 }
376
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500377 // Use SkAndroidCodec to perform the decode.
378 SkAndroidCodec::AndroidOptions codecOptions;
Romain Guy253f2c22016-09-28 17:34:42 -0700379 codecOptions.fZeroInitialized = decodeAllocator == &defaultAllocator ?
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500380 SkCodec::kYes_ZeroInitialized : SkCodec::kNo_ZeroInitialized;
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500381 codecOptions.fSampleSize = sampleSize;
382 SkCodec::Result result = codec->getAndroidPixels(decodeInfo, decodingBitmap.getPixels(),
383 decodingBitmap.rowBytes(), &codecOptions);
384 switch (result) {
385 case SkCodec::kSuccess:
386 case SkCodec::kIncompleteInput:
387 break;
388 default:
Matt Sarett3b1b68d2015-12-14 13:08:33 -0500389 return nullObjectReturn("codec->getAndroidPixels() failed.");
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500390 }
391
Stan Iliev7aedf6f2017-12-20 12:22:59 -0500392 // This is weird so let me explain: we could use the scale parameter
393 // directly, but for historical reasons this is how the corresponding
394 // Dalvik code has always behaved. We simply recreate the behavior here.
395 // The result is slightly different from simply using scale because of
396 // the 0.5f rounding bias applied when computing the target image size
397 const float scaleX = scaledWidth / float(decodingBitmap.width());
398 const float scaleY = scaledHeight / float(decodingBitmap.height());
399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 jbyteArray ninePatchChunk = NULL;
Chris Craik47cd8e92014-07-08 17:13:08 -0700401 if (peeker.mPatch != NULL) {
Romain Guy7b2f8b82012-03-19 17:18:54 -0700402 if (willScale) {
Stan Iliev7aedf6f2017-12-20 12:22:59 -0500403 peeker.scale(scaleX, scaleY, scaledWidth, scaledHeight);
Romain Guy7b2f8b82012-03-19 17:18:54 -0700404 }
405
Chris Craik47cd8e92014-07-08 17:13:08 -0700406 size_t ninePatchArraySize = peeker.mPatch->serializedSize();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 ninePatchChunk = env->NewByteArray(ninePatchArraySize);
Romain Guy7b2f8b82012-03-19 17:18:54 -0700408 if (ninePatchChunk == NULL) {
Mike Reedc70e06b2009-04-24 11:09:12 -0400409 return nullObjectReturn("ninePatchChunk == null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 }
Romain Guy7b2f8b82012-03-19 17:18:54 -0700411
412 jbyte* array = (jbyte*) env->GetPrimitiveArrayCritical(ninePatchChunk, NULL);
413 if (array == NULL) {
Mike Reedc70e06b2009-04-24 11:09:12 -0400414 return nullObjectReturn("primitive array == null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 }
Romain Guy7b2f8b82012-03-19 17:18:54 -0700416
Chris Craik47cd8e92014-07-08 17:13:08 -0700417 memcpy(array, peeker.mPatch, peeker.mPatchSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 env->ReleasePrimitiveArrayCritical(ninePatchChunk, array, 0);
419 }
420
Chris Craik47cd8e92014-07-08 17:13:08 -0700421 jobject ninePatchInsets = NULL;
422 if (peeker.mHasInsets) {
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400423 ninePatchInsets = peeker.createNinePatchInsets(env, scale);
Mathieu Chartiera08d10f2014-08-29 16:55:55 -0700424 if (ninePatchInsets == NULL) {
425 return nullObjectReturn("nine patch insets == null");
426 }
Amith Yamasaniec4a5042012-04-04 10:27:15 -0700427 if (javaBitmap != NULL) {
Chris Craik47cd8e92014-07-08 17:13:08 -0700428 env->SetObjectField(javaBitmap, gBitmap_ninePatchInsetsFieldID, ninePatchInsets);
Amith Yamasaniec4a5042012-04-04 10:27:15 -0700429 }
430 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431
John Reckf29ed282015-04-07 07:32:03 -0700432 SkBitmap outputBitmap;
Romain Guy7b2f8b82012-03-19 17:18:54 -0700433 if (willScale) {
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500434 // Set the allocator for the outputBitmap.
435 SkBitmap::Allocator* outputAllocator;
436 if (javaBitmap != nullptr) {
437 outputAllocator = &recyclingAllocator;
438 } else {
sergeyv45082182016-09-29 18:25:40 -0700439 outputAllocator = &defaultAllocator;
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500440 }
441
Leon Scroggins IIIef691a3d2017-07-10 17:03:55 -0400442 SkColorType scaledColorType = decodingBitmap.colorType();
Leon Scroggins III8790be62013-12-03 16:26:51 -0500443 // FIXME: If the alphaType is kUnpremul and the image has alpha, the
444 // colors may not be correct, since Skia does not yet support drawing
445 // to/from unpremultiplied bitmaps.
Matt Sarettee80c4712016-06-03 10:23:38 -0400446 outputBitmap.setInfo(
447 bitmapInfo.makeWH(scaledWidth, scaledHeight).makeColorType(scaledColorType));
Mike Reed81397c42017-07-18 17:04:16 -0400448 if (!outputBitmap.tryAllocPixels(outputAllocator)) {
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500449 // This should only fail on OOM. The recyclingAllocator should have
450 // enough memory since we check this before decoding using the
451 // scaleCheckingAllocator.
Raph Levien005bfc62012-09-20 22:51:47 -0700452 return nullObjectReturn("allocation failed for scaled bitmap");
453 }
Leon Scroggins III1ffe7272013-09-19 11:34:06 -0400454
Romain Guy7b2f8b82012-03-19 17:18:54 -0700455 SkPaint paint;
Romain Guy253f2c22016-09-28 17:34:42 -0700456 // kSrc_Mode instructs us to overwrite the uninitialized pixels in
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500457 // outputBitmap. Otherwise we would blend by default, which is not
458 // what we want.
Mike Reed260ab722016-10-07 15:59:20 -0400459 paint.setBlendMode(SkBlendMode::kSrc);
Romain Guy253f2c22016-09-28 17:34:42 -0700460 paint.setFilterQuality(kLow_SkFilterQuality); // bilinear filtering
Romain Guy7b2f8b82012-03-19 17:18:54 -0700461
Matt Sarettca9b7032017-04-13 12:18:47 -0400462 SkCanvas canvas(outputBitmap, SkCanvas::ColorBehavior::kLegacy);
Stan Iliev7aedf6f2017-12-20 12:22:59 -0500463 canvas.scale(scaleX, scaleY);
Chris Craik7e8c03c2013-06-03 13:53:36 -0700464 canvas.drawBitmap(decodingBitmap, 0.0f, 0.0f, &paint);
465 } else {
John Reckf29ed282015-04-07 07:32:03 -0700466 outputBitmap.swap(decodingBitmap);
Romain Guy7b2f8b82012-03-19 17:18:54 -0700467 }
468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 if (padding) {
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400470 peeker.getPadding(env, padding);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500473 // If we get here, the outputBitmap should have an installed pixelref.
John Reckf29ed282015-04-07 07:32:03 -0700474 if (outputBitmap.pixelRef() == NULL) {
Marco Nelissenb2fe3be2012-05-07 11:24:13 -0700475 return nullObjectReturn("Got null SkPixelRef");
476 }
Romain Guy23610982011-01-17 12:51:55 -0800477
Chris Craik7e8c03c2013-06-03 13:53:36 -0700478 if (!isMutable && javaBitmap == NULL) {
Romain Guy23610982011-01-17 12:51:55 -0800479 // promise we will never change our pixels (great for sharing and pictures)
John Reckf29ed282015-04-07 07:32:03 -0700480 outputBitmap.setImmutable();
Romain Guy23610982011-01-17 12:51:55 -0800481 }
Chet Haase37f74ca2010-12-08 17:56:36 -0800482
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500483 bool isPremultiplied = !requireUnpremultiplied;
484 if (javaBitmap != nullptr) {
sergeyvc69853c2016-10-07 14:14:09 -0700485 bitmap::reinitBitmap(env, javaBitmap, outputBitmap.info(), isPremultiplied);
John Reckf29ed282015-04-07 07:32:03 -0700486 outputBitmap.notifyPixelsChanged();
Chet Haase37f74ca2010-12-08 17:56:36 -0800487 // If a java bitmap was passed in for reuse, pass it back
488 return javaBitmap;
489 }
Chris Craik1abf5d62013-08-16 12:47:03 -0700490
491 int bitmapCreateFlags = 0x0;
sergeyvc69853c2016-10-07 14:14:09 -0700492 if (isMutable) bitmapCreateFlags |= android::bitmap::kBitmapCreateFlag_Mutable;
493 if (isPremultiplied) bitmapCreateFlags |= android::bitmap::kBitmapCreateFlag_Premultiplied;
Chris Craik1abf5d62013-08-16 12:47:03 -0700494
sergeyvda6c8ffc2016-11-22 18:28:54 -0800495 if (isHardware) {
496 sk_sp<Bitmap> hardwareBitmap = Bitmap::allocateHardwareBitmap(outputBitmap);
sergeyvd67bb1e2017-06-28 12:44:03 -0700497 if (!hardwareBitmap.get()) {
498 return nullObjectReturn("Failed to allocate a hardware bitmap");
499 }
sergeyvda6c8ffc2016-11-22 18:28:54 -0800500 return bitmap::createBitmap(env, hardwareBitmap.release(), bitmapCreateFlags,
501 ninePatchChunk, ninePatchInsets, -1);
502 }
503
Mike Reedc70e06b2009-04-24 11:09:12 -0400504 // now create the java bitmap
Leon Scroggins III300af262019-03-14 08:07:15 -0400505 return bitmap::createBitmap(env, defaultAllocator.getStorageObjAndReset(),
506 bitmapCreateFlags, ninePatchChunk, ninePatchInsets, -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507}
508
Chris Craik905e8242013-06-05 09:59:05 -0700509static jobject nativeDecodeStream(JNIEnv* env, jobject clazz, jobject is, jbyteArray storage,
Leon Scroggins III71fae622019-03-26 16:28:41 -0400510 jobject padding, jobject options, jlong inBitmapHandle, jlong colorSpaceHandle) {
Romain Guy7b2f8b82012-03-19 17:18:54 -0700511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 jobject bitmap = NULL;
Ben Wagner60126ef2015-08-07 12:13:48 -0400513 std::unique_ptr<SkStream> stream(CreateJavaInputStreamAdaptor(env, is, storage));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514
Leon Scroggins IIIca320212013-08-20 17:59:39 -0400515 if (stream.get()) {
Leon Scroggins III2bcdf6f2020-04-21 14:08:32 -0400516 std::unique_ptr<SkStreamRewindable> bufferedStream(skia::FrontBufferedStream::Make(
517 std::move(stream), SkCodec::MinBufferedBytesNeeded()));
Leon Scroggins III7315f1b2013-09-10 20:26:05 -0400518 SkASSERT(bufferedStream.get() != NULL);
Leon Scroggins III71fae622019-03-26 16:28:41 -0400519 bitmap = doDecode(env, std::move(bufferedStream), padding, options, inBitmapHandle,
520 colorSpaceHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 }
522 return bitmap;
523}
524
Romain Guy7b2f8b82012-03-19 17:18:54 -0700525static jobject nativeDecodeFileDescriptor(JNIEnv* env, jobject clazz, jobject fileDescriptor,
Leon Scroggins III71fae622019-03-26 16:28:41 -0400526 jobject padding, jobject bitmapFactoryOptions, jlong inBitmapHandle, jlong colorSpaceHandle) {
Derek Sollenbergerc5882c42019-10-25 11:11:32 -0400527#ifndef __ANDROID__ // LayoutLib for Windows does not support F_DUPFD_CLOEXEC
528 return nullObjectReturn("Not supported on Windows");
529#else
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 NPE_CHECK_RETURN_ZERO(env, fileDescriptor);
531
Derek Sollenberger5cb769d2014-09-24 09:20:09 -0400532 int descriptor = jniGetFDFromFileDescriptor(env, fileDescriptor);
Mike Reedc70e06b2009-04-24 11:09:12 -0400533
Derek Sollenberger5827cb52013-07-26 14:58:06 -0400534 struct stat fdStat;
535 if (fstat(descriptor, &fdStat) == -1) {
536 doThrowIOE(env, "broken file descriptor");
537 return nullObjectReturn("fstat return -1");
538 }
539
Derek Sollenberger5cb769d2014-09-24 09:20:09 -0400540 // Restore the descriptor's offset on exiting this function. Even though
541 // we dup the descriptor, both the original and dup refer to the same open
542 // file description and changes to the file offset in one impact the other.
Jérôme Poichetd29c9022014-09-26 18:59:11 +0000543 AutoFDSeek autoRestore(descriptor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544
Derek Sollenberger5cb769d2014-09-24 09:20:09 -0400545 // Duplicate the descriptor here to prevent leaking memory. A leak occurs
546 // if we only close the file descriptor and not the file object it is used to
547 // create. If we don't explicitly clean up the file (which in turn closes the
548 // descriptor) the buffers allocated internally by fseek will be leaked.
Nick Kralevich4b3a08c2019-01-28 10:39:10 -0800549 int dupDescriptor = fcntl(descriptor, F_DUPFD_CLOEXEC, 0);
Derek Sollenberger5cb769d2014-09-24 09:20:09 -0400550
551 FILE* file = fdopen(dupDescriptor, "r");
Leon Scroggins III0102f8a2014-01-14 15:14:57 -0500552 if (file == NULL) {
Derek Sollenberger5cb769d2014-09-24 09:20:09 -0400553 // cleanup the duplicated descriptor since it will not be closed when the
554 // file is cleaned up (fclose).
555 close(dupDescriptor);
Leon Scroggins III0102f8a2014-01-14 15:14:57 -0500556 return nullObjectReturn("Could not open file");
Leon Scroggins IIIf65183f2013-10-07 16:32:14 -0400557 }
Leon Scroggins III0102f8a2014-01-14 15:14:57 -0500558
Leon Scroggins IIIdd3c06c2017-03-10 10:50:33 -0500559 std::unique_ptr<SkFILEStream> fileStream(new SkFILEStream(file));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560
Yujie Qinc1d7b7f2016-02-29 14:00:29 +0100561 // If there is no offset for the file descriptor, we use SkFILEStream directly.
562 if (::lseek(descriptor, 0, SEEK_CUR) == 0) {
563 assert(isSeekable(dupDescriptor));
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500564 return doDecode(env, std::move(fileStream), padding, bitmapFactoryOptions,
Leon Scroggins III71fae622019-03-26 16:28:41 -0400565 inBitmapHandle, colorSpaceHandle);
Yujie Qinc1d7b7f2016-02-29 14:00:29 +0100566 }
567
Leon Scroggins III0aa39dc2014-06-03 12:19:32 -0400568 // Use a buffered stream. Although an SkFILEStream can be rewound, this
569 // ensures that SkImageDecoder::Factory never rewinds beyond the
570 // current position of the file descriptor.
Leon Scroggins III2bcdf6f2020-04-21 14:08:32 -0400571 std::unique_ptr<SkStreamRewindable> stream(skia::FrontBufferedStream::Make(
572 std::move(fileStream), SkCodec::MinBufferedBytesNeeded()));
Leon Scroggins III2826e5f2014-02-05 19:46:02 -0500573
Leon Scroggins III71fae622019-03-26 16:28:41 -0400574 return doDecode(env, std::move(stream), padding, bitmapFactoryOptions, inBitmapHandle,
575 colorSpaceHandle);
Derek Sollenbergerc5882c42019-10-25 11:11:32 -0400576#endif
Mike Reedc70e06b2009-04-24 11:09:12 -0400577}
578
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000579static jobject nativeDecodeAsset(JNIEnv* env, jobject clazz, jlong native_asset,
Leon Scroggins III71fae622019-03-26 16:28:41 -0400580 jobject padding, jobject options, jlong inBitmapHandle, jlong colorSpaceHandle) {
Romain Guy7b2f8b82012-03-19 17:18:54 -0700581
Mike Reedc70e06b2009-04-24 11:09:12 -0400582 Asset* asset = reinterpret_cast<Asset*>(native_asset);
Leon Scroggins III0aa39dc2014-06-03 12:19:32 -0400583 // since we know we'll be done with the asset when we return, we can
584 // just use a simple wrapper
Mike Reed3cf42822019-12-12 11:59:49 -0500585 return doDecode(env, std::make_unique<AssetStreamAdaptor>(asset), padding, options,
Leon Scroggins III71fae622019-03-26 16:28:41 -0400586 inBitmapHandle, colorSpaceHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587}
588
589static jobject nativeDecodeByteArray(JNIEnv* env, jobject, jbyteArray byteArray,
Leon Scroggins III71fae622019-03-26 16:28:41 -0400590 jint offset, jint length, jobject options, jlong inBitmapHandle, jlong colorSpaceHandle) {
Romain Guy7b2f8b82012-03-19 17:18:54 -0700591
Mike Reedc70e06b2009-04-24 11:09:12 -0400592 AutoJavaByteArray ar(env, byteArray);
Mike Reed3cf42822019-12-12 11:59:49 -0500593 return doDecode(env, std::make_unique<SkMemoryStream>(ar.ptr() + offset, length, false),
Leon Scroggins III71fae622019-03-26 16:28:41 -0400594 nullptr, options, inBitmapHandle, colorSpaceHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595}
596
Owen Lina9d0d472011-01-18 17:39:15 +0800597static jboolean nativeIsSeekable(JNIEnv* env, jobject, jobject fileDescriptor) {
Elliott Hughesa3804cf2011-04-11 16:50:19 -0700598 jint descriptor = jniGetFDFromFileDescriptor(env, fileDescriptor);
Yujie Qinc1d7b7f2016-02-29 14:00:29 +0100599 return isSeekable(descriptor) ? JNI_TRUE : JNI_FALSE;
Owen Lina9d0d472011-01-18 17:39:15 +0800600}
601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602///////////////////////////////////////////////////////////////////////////////
603
Daniel Micay76f6a862015-09-19 17:31:01 -0400604static const JNINativeMethod gMethods[] = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 { "nativeDecodeStream",
Leon Scroggins III71fae622019-03-26 16:28:41 -0400606 "(Ljava/io/InputStream;[BLandroid/graphics/Rect;Landroid/graphics/BitmapFactory$Options;JJ)Landroid/graphics/Bitmap;",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 (void*)nativeDecodeStream
608 },
609
610 { "nativeDecodeFileDescriptor",
Leon Scroggins III71fae622019-03-26 16:28:41 -0400611 "(Ljava/io/FileDescriptor;Landroid/graphics/Rect;Landroid/graphics/BitmapFactory$Options;JJ)Landroid/graphics/Bitmap;",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 (void*)nativeDecodeFileDescriptor
613 },
614
615 { "nativeDecodeAsset",
Leon Scroggins III71fae622019-03-26 16:28:41 -0400616 "(JLandroid/graphics/Rect;Landroid/graphics/BitmapFactory$Options;JJ)Landroid/graphics/Bitmap;",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 (void*)nativeDecodeAsset
618 },
619
620 { "nativeDecodeByteArray",
Leon Scroggins III71fae622019-03-26 16:28:41 -0400621 "([BIILandroid/graphics/BitmapFactory$Options;JJ)Landroid/graphics/Bitmap;",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 (void*)nativeDecodeByteArray
623 },
624
Owen Lina9d0d472011-01-18 17:39:15 +0800625 { "nativeIsSeekable",
626 "(Ljava/io/FileDescriptor;)Z",
627 (void*)nativeIsSeekable
628 },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629};
630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631int register_android_graphics_BitmapFactory(JNIEnv* env) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800632 jclass options_class = FindClassOrDie(env, "android/graphics/BitmapFactory$Options");
633 gOptions_bitmapFieldID = GetFieldIDOrDie(env, options_class, "inBitmap",
Chris Craik47cd8e92014-07-08 17:13:08 -0700634 "Landroid/graphics/Bitmap;");
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800635 gOptions_justBoundsFieldID = GetFieldIDOrDie(env, options_class, "inJustDecodeBounds", "Z");
636 gOptions_sampleSizeFieldID = GetFieldIDOrDie(env, options_class, "inSampleSize", "I");
637 gOptions_configFieldID = GetFieldIDOrDie(env, options_class, "inPreferredConfig",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 "Landroid/graphics/Bitmap$Config;");
Romain Guy95648b82017-04-13 18:43:42 -0700639 gOptions_colorSpaceFieldID = GetFieldIDOrDie(env, options_class, "inPreferredColorSpace",
640 "Landroid/graphics/ColorSpace;");
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800641 gOptions_premultipliedFieldID = GetFieldIDOrDie(env, options_class, "inPremultiplied", "Z");
642 gOptions_mutableFieldID = GetFieldIDOrDie(env, options_class, "inMutable", "Z");
643 gOptions_ditherFieldID = GetFieldIDOrDie(env, options_class, "inDither", "Z");
644 gOptions_preferQualityOverSpeedFieldID = GetFieldIDOrDie(env, options_class,
Wei-Ta Chen953f9092010-12-03 14:06:18 -0800645 "inPreferQualityOverSpeed", "Z");
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800646 gOptions_scaledFieldID = GetFieldIDOrDie(env, options_class, "inScaled", "Z");
647 gOptions_densityFieldID = GetFieldIDOrDie(env, options_class, "inDensity", "I");
648 gOptions_screenDensityFieldID = GetFieldIDOrDie(env, options_class, "inScreenDensity", "I");
649 gOptions_targetDensityFieldID = GetFieldIDOrDie(env, options_class, "inTargetDensity", "I");
650 gOptions_widthFieldID = GetFieldIDOrDie(env, options_class, "outWidth", "I");
651 gOptions_heightFieldID = GetFieldIDOrDie(env, options_class, "outHeight", "I");
652 gOptions_mimeFieldID = GetFieldIDOrDie(env, options_class, "outMimeType", "Ljava/lang/String;");
Romain Guye8d2ebb2017-02-09 18:38:47 -0800653 gOptions_outConfigFieldID = GetFieldIDOrDie(env, options_class, "outConfig",
654 "Landroid/graphics/Bitmap$Config;");
Romain Guy90fc43b2017-03-30 12:35:26 -0700655 gOptions_outColorSpaceFieldID = GetFieldIDOrDie(env, options_class, "outColorSpace",
656 "Landroid/graphics/ColorSpace;");
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800657 gOptions_mCancelID = GetFieldIDOrDie(env, options_class, "mCancel", "Z");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800659 jclass bitmap_class = FindClassOrDie(env, "android/graphics/Bitmap");
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800660 gBitmap_ninePatchInsetsFieldID = GetFieldIDOrDie(env, bitmap_class, "mNinePatchInsets",
Chris Craik47cd8e92014-07-08 17:13:08 -0700661 "Landroid/graphics/NinePatch$InsetStruct;");
662
Romain Guye8d2ebb2017-02-09 18:38:47 -0800663 gBitmapConfig_class = MakeGlobalRefOrDie(env, FindClassOrDie(env,
664 "android/graphics/Bitmap$Config"));
665 gBitmapConfig_nativeToConfigMethodID = GetStaticMethodIDOrDie(env, gBitmapConfig_class,
666 "nativeToConfig", "(I)Landroid/graphics/Bitmap$Config;");
667
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800668 return android::RegisterMethodsOrDie(env, "android/graphics/BitmapFactory",
669 gMethods, NELEM(gMethods));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670}