blob: 42e9273c5ba26bc869f771d5073d4072855b90c4 [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
50jclass gInsetStruct_class;
51jmethodID gInsetStruct_constructorMethodID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052
Romain Guye8d2ebb2017-02-09 18:38:47 -080053jclass gBitmapConfig_class;
54jmethodID gBitmapConfig_nativeToConfigMethodID;
55
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056using namespace android;
57
Hal Canary10219fb2016-11-23 20:41:22 -050058jstring encodedFormatToString(JNIEnv* env, SkEncodedImageFormat format) {
Matt Sarettb8adc9a2015-12-02 13:35:22 -050059 const char* mimeType;
60 switch (format) {
Hal Canary10219fb2016-11-23 20:41:22 -050061 case SkEncodedImageFormat::kBMP:
Matt Sarettb8adc9a2015-12-02 13:35:22 -050062 mimeType = "image/bmp";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 break;
Hal Canary10219fb2016-11-23 20:41:22 -050064 case SkEncodedImageFormat::kGIF:
Matt Sarettb8adc9a2015-12-02 13:35:22 -050065 mimeType = "image/gif";
66 break;
Hal Canary10219fb2016-11-23 20:41:22 -050067 case SkEncodedImageFormat::kICO:
Matt Sarettb8adc9a2015-12-02 13:35:22 -050068 mimeType = "image/x-ico";
69 break;
Hal Canary10219fb2016-11-23 20:41:22 -050070 case SkEncodedImageFormat::kJPEG:
Matt Sarettb8adc9a2015-12-02 13:35:22 -050071 mimeType = "image/jpeg";
72 break;
Hal Canary10219fb2016-11-23 20:41:22 -050073 case SkEncodedImageFormat::kPNG:
Matt Sarettb8adc9a2015-12-02 13:35:22 -050074 mimeType = "image/png";
75 break;
Hal Canary10219fb2016-11-23 20:41:22 -050076 case SkEncodedImageFormat::kWEBP:
Matt Sarettb8adc9a2015-12-02 13:35:22 -050077 mimeType = "image/webp";
78 break;
Chong Zhang48fa8902017-08-16 11:57:02 -070079 case SkEncodedImageFormat::kHEIF:
80 mimeType = "image/heif";
81 break;
Hal Canary10219fb2016-11-23 20:41:22 -050082 case SkEncodedImageFormat::kWBMP:
Matt Sarettb8adc9a2015-12-02 13:35:22 -050083 mimeType = "image/vnd.wap.wbmp";
84 break;
Hal Canary10219fb2016-11-23 20:41:22 -050085 case SkEncodedImageFormat::kDNG:
Yujie Qin990ea132016-03-17 14:13:22 +010086 mimeType = "image/x-adobe-dng";
87 break;
Matt Sarettb8adc9a2015-12-02 13:35:22 -050088 default:
89 mimeType = nullptr;
90 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 }
92
Vladimir Marko7ab249a2015-01-06 18:17:52 +000093 jstring jstr = nullptr;
Matt Sarettb8adc9a2015-12-02 13:35:22 -050094 if (mimeType) {
Vladimir Marko7ab249a2015-01-06 18:17:52 +000095 // NOTE: Caller should env->ExceptionCheck() for OOM
96 // (can't check for nullptr as it's a valid return value)
Matt Sarettb8adc9a2015-12-02 13:35:22 -050097 jstr = env->NewStringUTF(mimeType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 }
99 return jstr;
100}
101
Chris Craikbd8db2e82014-08-20 16:31:57 -0700102static void scaleDivRange(int32_t* divs, int count, float scale, int maxValue) {
103 for (int i = 0; i < count; i++) {
104 divs[i] = int32_t(divs[i] * scale + 0.5f);
105 if (i > 0 && divs[i] == divs[i - 1]) {
106 divs[i]++; // avoid collisions
107 }
108 }
109
110 if (CC_UNLIKELY(divs[count - 1] > maxValue)) {
111 // if the collision avoidance above put some divs outside the bounds of the bitmap,
112 // slide outer stretchable divs inward to stay within bounds
113 int highestAvailable = maxValue;
114 for (int i = count - 1; i >= 0; i--) {
115 divs[i] = highestAvailable;
116 if (i > 0 && divs[i] <= divs[i-1]){
117 // keep shifting
118 highestAvailable = divs[i] - 1;
119 } else {
120 break;
121 }
122 }
123 }
124}
125
126static void scaleNinePatchChunk(android::Res_png_9patch* chunk, float scale,
127 int scaledWidth, int scaledHeight) {
Romain Guy7b2f8b82012-03-19 17:18:54 -0700128 chunk->paddingLeft = int(chunk->paddingLeft * scale + 0.5f);
129 chunk->paddingTop = int(chunk->paddingTop * scale + 0.5f);
130 chunk->paddingRight = int(chunk->paddingRight * scale + 0.5f);
131 chunk->paddingBottom = int(chunk->paddingBottom * scale + 0.5f);
132
Chris Craikbd8db2e82014-08-20 16:31:57 -0700133 scaleDivRange(chunk->getXDivs(), chunk->numXDivs, scale, scaledWidth);
134 scaleDivRange(chunk->getYDivs(), chunk->numYDivs, scale, scaledHeight);
Romain Guy7b2f8b82012-03-19 17:18:54 -0700135}
136
Chris Craik905e8242013-06-05 09:59:05 -0700137class ScaleCheckingAllocator : public SkBitmap::HeapAllocator {
138public:
139 ScaleCheckingAllocator(float scale, int size)
140 : mScale(scale), mSize(size) {
141 }
142
Mike Reed81397c42017-07-18 17:04:16 -0400143 virtual bool allocPixelRef(SkBitmap* bitmap) {
Chris Craik905e8242013-06-05 09:59:05 -0700144 // accounts for scale in final allocation, using eventual size and config
Leon Scroggins IIIef691a3d2017-07-10 17:03:55 -0400145 const int bytesPerPixel = SkColorTypeBytesPerPixel(bitmap->colorType());
Chris Craik905e8242013-06-05 09:59:05 -0700146 const int requestedSize = bytesPerPixel *
147 int(bitmap->width() * mScale + 0.5f) *
148 int(bitmap->height() * mScale + 0.5f);
149 if (requestedSize > mSize) {
150 ALOGW("bitmap for alloc reuse (%d bytes) can't fit scaled bitmap (%d bytes)",
151 mSize, requestedSize);
152 return false;
153 }
Mike Reed81397c42017-07-18 17:04:16 -0400154 return SkBitmap::HeapAllocator::allocPixelRef(bitmap);
Chris Craik905e8242013-06-05 09:59:05 -0700155 }
156private:
157 const float mScale;
158 const int mSize;
159};
160
Chris Craik7e8c03c2013-06-03 13:53:36 -0700161class RecyclingPixelAllocator : public SkBitmap::Allocator {
162public:
sergeyvc1c54062016-10-19 18:47:26 -0700163 RecyclingPixelAllocator(android::Bitmap* bitmap, unsigned int size)
John Reckf29ed282015-04-07 07:32:03 -0700164 : mBitmap(bitmap), mSize(size) {
Chris Craik7e8c03c2013-06-03 13:53:36 -0700165 }
166
167 ~RecyclingPixelAllocator() {
Chris Craik7e8c03c2013-06-03 13:53:36 -0700168 }
169
Mike Reed81397c42017-07-18 17:04:16 -0400170 virtual bool allocPixelRef(SkBitmap* bitmap) {
Leon Scroggins46cb9bd2014-03-06 15:36:39 -0500171 const SkImageInfo& info = bitmap->info();
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400172 if (info.colorType() == kUnknown_SkColorType) {
Leon Scroggins46cb9bd2014-03-06 15:36:39 -0500173 ALOGW("unable to reuse a bitmap as the target has an unknown bitmap configuration");
Chris Craik7e8c03c2013-06-03 13:53:36 -0700174 return false;
175 }
Chris Craikcd0ba712013-09-06 14:40:30 -0700176
Leon Scroggins46cb9bd2014-03-06 15:36:39 -0500177 const int64_t size64 = info.getSafeSize64(bitmap->rowBytes());
178 if (!sk_64_isS32(size64)) {
179 ALOGW("bitmap is too large");
180 return false;
181 }
182
183 const size_t size = sk_64_asS32(size64);
184 if (size > mSize) {
Dan Albert46d84442014-11-18 16:07:51 -0800185 ALOGW("bitmap marked for reuse (%u bytes) can't fit new bitmap "
186 "(%zu bytes)", mSize, size);
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500187 return false;
188 }
189
Leon Scroggins IIIf51a80d2017-07-12 10:46:35 -0400190 mBitmap->reconfigure(info, bitmap->rowBytes());
Mike Reed826deef2017-04-04 15:32:04 -0400191 bitmap->setPixelRef(sk_ref_sp(mBitmap), 0, 0);
Chris Craik7e8c03c2013-06-03 13:53:36 -0700192 return true;
193 }
194
195private:
sergeyvc1c54062016-10-19 18:47:26 -0700196 android::Bitmap* const mBitmap;
Chris Craik7e8c03c2013-06-03 13:53:36 -0700197 const unsigned int mSize;
198};
199
Anton Daubert4e5ec342016-03-07 17:30:20 +0100200// Necessary for decodes when the native decoder cannot scale to appropriately match the sampleSize
201// (for example, RAW). If the sampleSize divides evenly into the dimension, we require that the
202// scale matches exactly. If sampleSize does not divide evenly, we allow the decoder to choose how
203// best to round.
204static bool needsFineScale(const int fullSize, const int decodedSize, const int sampleSize) {
205 if (fullSize % sampleSize == 0 && fullSize / sampleSize != decodedSize) {
206 return true;
207 } else if ((fullSize / sampleSize + 1) != decodedSize &&
208 (fullSize / sampleSize) != decodedSize) {
209 return true;
210 }
211 return false;
212}
213
214static bool needsFineScale(const SkISize fullSize, const SkISize decodedSize,
215 const int sampleSize) {
216 return needsFineScale(fullSize.width(), decodedSize.width(), sampleSize) ||
217 needsFineScale(fullSize.height(), decodedSize.height(), sampleSize);
218}
219
Mike Reedc7c65602017-07-26 10:40:25 -0400220static jobject doDecode(JNIEnv* env, std::unique_ptr<SkStreamRewindable> stream,
221 jobject padding, jobject options) {
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500222 // Set default values for the options parameters.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 int sampleSize = 1;
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500224 bool onlyDecodeSize = false;
Mike Reed42a1d082014-07-07 18:06:18 -0400225 SkColorType prefColorType = kN32_SkColorType;
sergeyvda6c8ffc2016-11-22 18:28:54 -0800226 bool isHardware = false;
Romain Guy23610982011-01-17 12:51:55 -0800227 bool isMutable = false;
Chris Craik905e8242013-06-05 09:59:05 -0700228 float scale = 1.0f;
Chris Craik1abf5d62013-08-16 12:47:03 -0700229 bool requireUnpremultiplied = false;
Chet Haase37f74ca2010-12-08 17:56:36 -0800230 jobject javaBitmap = NULL;
Romain Guy95648b82017-04-13 18:43:42 -0700231 sk_sp<SkColorSpace> prefColorSpace = nullptr;
Elliott Hughesa3804cf2011-04-11 16:50:19 -0700232
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500233 // Update with options supplied by the client.
Romain Guy7b2f8b82012-03-19 17:18:54 -0700234 if (options != NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 sampleSize = env->GetIntField(options, gOptions_sampleSizeFieldID);
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500236 // Correct a non-positive sampleSize. sampleSize defaults to zero within the
237 // options object, which is strange.
238 if (sampleSize <= 0) {
239 sampleSize = 1;
240 }
241
242 if (env->GetBooleanField(options, gOptions_justBoundsFieldID)) {
243 onlyDecodeSize = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 }
Romain Guy7b2f8b82012-03-19 17:18:54 -0700245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 // initialize these, in case we fail later on
247 env->SetIntField(options, gOptions_widthFieldID, -1);
248 env->SetIntField(options, gOptions_heightFieldID, -1);
249 env->SetObjectField(options, gOptions_mimeFieldID, 0);
Romain Guye8d2ebb2017-02-09 18:38:47 -0800250 env->SetObjectField(options, gOptions_outConfigFieldID, 0);
Romain Guy90fc43b2017-03-30 12:35:26 -0700251 env->SetObjectField(options, gOptions_outColorSpaceFieldID, 0);
Elliott Hughesa3804cf2011-04-11 16:50:19 -0700252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 jobject jconfig = env->GetObjectField(options, gOptions_configFieldID);
Mike Reed42a1d082014-07-07 18:06:18 -0400254 prefColorType = GraphicsJNI::getNativeBitmapColorType(env, jconfig);
Romain Guy95648b82017-04-13 18:43:42 -0700255 jobject jcolorSpace = env->GetObjectField(options, gOptions_colorSpaceFieldID);
256 prefColorSpace = GraphicsJNI::getNativeColorSpace(env, jcolorSpace);
sergeyvda6c8ffc2016-11-22 18:28:54 -0800257 isHardware = GraphicsJNI::isHardwareConfig(env, jconfig);
Romain Guy23610982011-01-17 12:51:55 -0800258 isMutable = env->GetBooleanField(options, gOptions_mutableFieldID);
Chris Craik1abf5d62013-08-16 12:47:03 -0700259 requireUnpremultiplied = !env->GetBooleanField(options, gOptions_premultipliedFieldID);
Chet Haase37f74ca2010-12-08 17:56:36 -0800260 javaBitmap = env->GetObjectField(options, gOptions_bitmapFieldID);
Chris Craik905e8242013-06-05 09:59:05 -0700261
262 if (env->GetBooleanField(options, gOptions_scaledFieldID)) {
263 const int density = env->GetIntField(options, gOptions_densityFieldID);
264 const int targetDensity = env->GetIntField(options, gOptions_targetDensityFieldID);
265 const int screenDensity = env->GetIntField(options, gOptions_screenDensityFieldID);
266 if (density != 0 && targetDensity != 0 && density != screenDensity) {
267 scale = (float) targetDensity / density;
268 }
269 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 }
Romain Guy7b2f8b82012-03-19 17:18:54 -0700271
sergeyv9fbb0b52016-11-23 10:27:33 -0800272 if (isMutable && isHardware) {
273 doThrowIAE(env, "Bitmaps with Config.HARWARE are always immutable");
274 return nullObjectReturn("Cannot create mutable hardware bitmap");
275 }
276
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500277 // Create the codec.
278 NinePatchPeeker peeker;
Mike Reedc7c65602017-07-26 10:40:25 -0400279 std::unique_ptr<SkAndroidCodec> codec = SkAndroidCodec::MakeFromStream(
280 std::move(stream), &peeker);
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500281 if (!codec.get()) {
Mike Reedc7c65602017-07-26 10:40:25 -0400282 return nullObjectReturn("SkAndroidCodec::MakeFromStream returned null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 }
Elliott Hughesa3804cf2011-04-11 16:50:19 -0700284
Matt Sarett3b1b68d2015-12-14 13:08:33 -0500285 // Do not allow ninepatch decodes to 565. In the past, decodes to 565
286 // would dither, and we do not want to pre-dither ninepatches, since we
287 // know that they will be stretched. We no longer dither 565 decodes,
288 // but we continue to prevent ninepatches from decoding to 565, in order
289 // to maintain the old behavior.
290 if (peeker.mPatch && kRGB_565_SkColorType == prefColorType) {
291 prefColorType = kN32_SkColorType;
292 }
293
Anton Daubert4e5ec342016-03-07 17:30:20 +0100294 // Determine the output size.
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500295 SkISize size = codec->getSampledDimensions(sampleSize);
Anton Daubert4e5ec342016-03-07 17:30:20 +0100296
297 int scaledWidth = size.width();
298 int scaledHeight = size.height();
299 bool willScale = false;
300
301 // Apply a fine scaling step if necessary.
302 if (needsFineScale(codec->getInfo().dimensions(), size, sampleSize)) {
303 willScale = true;
304 scaledWidth = codec->getInfo().width() / sampleSize;
305 scaledHeight = codec->getInfo().height() / sampleSize;
306 }
307
Romain Guye8d2ebb2017-02-09 18:38:47 -0800308 // Set the decode colorType
309 SkColorType decodeColorType = codec->computeOutputColorType(prefColorType);
Romain Guy95648b82017-04-13 18:43:42 -0700310 sk_sp<SkColorSpace> decodeColorSpace = codec->computeOutputColorSpace(
311 decodeColorType, prefColorSpace);
Romain Guye8d2ebb2017-02-09 18:38:47 -0800312
Anton Daubert4e5ec342016-03-07 17:30:20 +0100313 // Set the options and return if the client only wants the size.
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500314 if (options != NULL) {
Hal Canary10219fb2016-11-23 20:41:22 -0500315 jstring mimeType = encodedFormatToString(
316 env, (SkEncodedImageFormat)codec->getEncodedFormat());
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500317 if (env->ExceptionCheck()) {
Matt Sarettd31512b2015-12-09 15:16:31 -0500318 return nullObjectReturn("OOM in encodedFormatToString()");
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500319 }
Anton Daubert4e5ec342016-03-07 17:30:20 +0100320 env->SetIntField(options, gOptions_widthFieldID, scaledWidth);
321 env->SetIntField(options, gOptions_heightFieldID, scaledHeight);
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500322 env->SetObjectField(options, gOptions_mimeFieldID, mimeType);
323
Leon Scroggins IIIef691a3d2017-07-10 17:03:55 -0400324 jint configID = GraphicsJNI::colorTypeToLegacyBitmapConfig(decodeColorType);
Romain Guye8d2ebb2017-02-09 18:38:47 -0800325 if (isHardware) {
326 configID = GraphicsJNI::kHardware_LegacyBitmapConfig;
327 }
328 jobject config = env->CallStaticObjectMethod(gBitmapConfig_class,
329 gBitmapConfig_nativeToConfigMethodID, configID);
330 env->SetObjectField(options, gOptions_outConfigFieldID, config);
331
Romain Guy90fc43b2017-03-30 12:35:26 -0700332 env->SetObjectField(options, gOptions_outColorSpaceFieldID,
Romain Guy95648b82017-04-13 18:43:42 -0700333 GraphicsJNI::getColorSpace(env, decodeColorSpace, decodeColorType));
Romain Guy90fc43b2017-03-30 12:35:26 -0700334
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500335 if (onlyDecodeSize) {
336 return nullptr;
337 }
338 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339
Anton Daubert4e5ec342016-03-07 17:30:20 +0100340 // Scale is necessary due to density differences.
341 if (scale != 1.0f) {
342 willScale = true;
343 scaledWidth = static_cast<int>(scaledWidth * scale + 0.5f);
344 scaledHeight = static_cast<int>(scaledHeight * scale + 0.5f);
345 }
346
sergeyvc1c54062016-10-19 18:47:26 -0700347 android::Bitmap* reuseBitmap = nullptr;
Chris Craik9f583612013-05-20 18:13:47 -0700348 unsigned int existingBufferSize = 0;
Chris Craik7e8c03c2013-06-03 13:53:36 -0700349 if (javaBitmap != NULL) {
sergeyvaed7f582016-10-14 16:30:21 -0700350 reuseBitmap = &bitmap::toBitmap(env, javaBitmap);
sergeyvc69853c2016-10-07 14:14:09 -0700351 if (reuseBitmap->isImmutable()) {
Derek Sollenberger2a6ecae2012-08-31 14:03:51 -0400352 ALOGW("Unable to reuse an immutable bitmap as an image decoder target.");
Chris Craik7e8c03c2013-06-03 13:53:36 -0700353 javaBitmap = NULL;
John Reckf29ed282015-04-07 07:32:03 -0700354 reuseBitmap = nullptr;
Chris Craik7e8c03c2013-06-03 13:53:36 -0700355 } else {
sergeyvc69853c2016-10-07 14:14:09 -0700356 existingBufferSize = bitmap::getBitmapAllocationByteCount(env, javaBitmap);
Derek Sollenberger2a6ecae2012-08-31 14:03:51 -0400357 }
Chet Haase37f74ca2010-12-08 17:56:36 -0800358 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359
sergeyv45082182016-09-29 18:25:40 -0700360 HeapAllocator defaultAllocator;
John Reckf29ed282015-04-07 07:32:03 -0700361 RecyclingPixelAllocator recyclingAllocator(reuseBitmap, existingBufferSize);
Chris Craik905e8242013-06-05 09:59:05 -0700362 ScaleCheckingAllocator scaleCheckingAllocator(scale, existingBufferSize);
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500363 SkBitmap::HeapAllocator heapAllocator;
364 SkBitmap::Allocator* decodeAllocator;
365 if (javaBitmap != nullptr && willScale) {
366 // This will allocate pixels using a HeapAllocator, since there will be an extra
367 // scaling step that copies these pixels into Java memory. This allocator
368 // also checks that the recycled javaBitmap is large enough.
369 decodeAllocator = &scaleCheckingAllocator;
370 } else if (javaBitmap != nullptr) {
371 decodeAllocator = &recyclingAllocator;
sergeyvda6c8ffc2016-11-22 18:28:54 -0800372 } else if (willScale || isHardware) {
373 // This will allocate pixels using a HeapAllocator,
374 // for scale case: there will be an extra scaling step.
375 // for hardware case: there will be extra swizzling & upload to gralloc step.
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500376 decodeAllocator = &heapAllocator;
377 } else {
sergeyv45082182016-09-29 18:25:40 -0700378 decodeAllocator = &defaultAllocator;
Chris Craik905e8242013-06-05 09:59:05 -0700379 }
380
Matt Sarett9e7cd632015-12-11 10:54:28 -0500381 SkAlphaType alphaType = codec->computeOutputAlphaType(requireUnpremultiplied);
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500382
Romain Guy253f2c22016-09-28 17:34:42 -0700383 const SkImageInfo decodeInfo = SkImageInfo::Make(size.width(), size.height(),
Romain Guy90fc43b2017-03-30 12:35:26 -0700384 decodeColorType, alphaType, decodeColorSpace);
Matt Sarett6c382572017-02-21 17:42:41 -0500385
386 // For wide gamut images, we will leave the color space on the SkBitmap. Otherwise,
387 // use the default.
Matt Sarett327c7202017-02-22 17:38:20 -0500388 SkImageInfo bitmapInfo = decodeInfo;
Matt Sarett2ecdfc22017-03-08 17:14:17 -0500389 if (decodeInfo.colorSpace() && decodeInfo.colorSpace()->isSRGB()) {
Matt Sarett6c382572017-02-21 17:42:41 -0500390 bitmapInfo = bitmapInfo.makeColorSpace(GraphicsJNI::colorSpaceForType(decodeColorType));
391 }
Matt Sarett33e37412016-12-21 12:17:17 -0500392
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500393 if (decodeColorType == kGray_8_SkColorType) {
394 // The legacy implementation of BitmapFactory used kAlpha8 for
395 // grayscale images (before kGray8 existed). While the codec
396 // recognizes kGray8, we need to decode into a kAlpha8 bitmap
397 // in order to avoid a behavior change.
Matt Sarettee80c472016-06-03 10:23:38 -0400398 bitmapInfo =
399 bitmapInfo.makeColorType(kAlpha_8_SkColorType).makeAlphaType(kPremul_SkAlphaType);
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500400 }
Chris Craik7e8c03c2013-06-03 13:53:36 -0700401 SkBitmap decodingBitmap;
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500402 if (!decodingBitmap.setInfo(bitmapInfo) ||
Mike Reed81397c42017-07-18 17:04:16 -0400403 !decodingBitmap.tryAllocPixels(decodeAllocator)) {
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500404 // SkAndroidCodec should recommend a valid SkImageInfo, so setInfo()
405 // should only only fail if the calculated value for rowBytes is too
406 // large.
407 // tryAllocPixels() can fail due to OOM on the Java heap, OOM on the
408 // native heap, or the recycled javaBitmap being too small to reuse.
409 return nullptr;
Mike Reedc70e06b2009-04-24 11:09:12 -0400410 }
411
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500412 // Use SkAndroidCodec to perform the decode.
413 SkAndroidCodec::AndroidOptions codecOptions;
Romain Guy253f2c22016-09-28 17:34:42 -0700414 codecOptions.fZeroInitialized = decodeAllocator == &defaultAllocator ?
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500415 SkCodec::kYes_ZeroInitialized : SkCodec::kNo_ZeroInitialized;
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500416 codecOptions.fSampleSize = sampleSize;
417 SkCodec::Result result = codec->getAndroidPixels(decodeInfo, decodingBitmap.getPixels(),
418 decodingBitmap.rowBytes(), &codecOptions);
419 switch (result) {
420 case SkCodec::kSuccess:
421 case SkCodec::kIncompleteInput:
422 break;
423 default:
Matt Sarett3b1b68d2015-12-14 13:08:33 -0500424 return nullObjectReturn("codec->getAndroidPixels() failed.");
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500425 }
426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 jbyteArray ninePatchChunk = NULL;
Chris Craik47cd8e92014-07-08 17:13:08 -0700428 if (peeker.mPatch != NULL) {
Romain Guy7b2f8b82012-03-19 17:18:54 -0700429 if (willScale) {
Chris Craikbd8db2e82014-08-20 16:31:57 -0700430 scaleNinePatchChunk(peeker.mPatch, scale, scaledWidth, scaledHeight);
Romain Guy7b2f8b82012-03-19 17:18:54 -0700431 }
432
Chris Craik47cd8e92014-07-08 17:13:08 -0700433 size_t ninePatchArraySize = peeker.mPatch->serializedSize();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 ninePatchChunk = env->NewByteArray(ninePatchArraySize);
Romain Guy7b2f8b82012-03-19 17:18:54 -0700435 if (ninePatchChunk == NULL) {
Mike Reedc70e06b2009-04-24 11:09:12 -0400436 return nullObjectReturn("ninePatchChunk == null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 }
Romain Guy7b2f8b82012-03-19 17:18:54 -0700438
439 jbyte* array = (jbyte*) env->GetPrimitiveArrayCritical(ninePatchChunk, NULL);
440 if (array == NULL) {
Mike Reedc70e06b2009-04-24 11:09:12 -0400441 return nullObjectReturn("primitive array == null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 }
Romain Guy7b2f8b82012-03-19 17:18:54 -0700443
Chris Craik47cd8e92014-07-08 17:13:08 -0700444 memcpy(array, peeker.mPatch, peeker.mPatchSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 env->ReleasePrimitiveArrayCritical(ninePatchChunk, array, 0);
446 }
447
Chris Craik47cd8e92014-07-08 17:13:08 -0700448 jobject ninePatchInsets = NULL;
449 if (peeker.mHasInsets) {
450 ninePatchInsets = env->NewObject(gInsetStruct_class, gInsetStruct_constructorMethodID,
Romain Guy253f2c22016-09-28 17:34:42 -0700451 peeker.mOpticalInsets[0], peeker.mOpticalInsets[1],
452 peeker.mOpticalInsets[2], peeker.mOpticalInsets[3],
453 peeker.mOutlineInsets[0], peeker.mOutlineInsets[1],
454 peeker.mOutlineInsets[2], peeker.mOutlineInsets[3],
Chris Craik77b5cad2014-07-30 18:23:07 -0700455 peeker.mOutlineRadius, peeker.mOutlineAlpha, scale);
Mathieu Chartiera08d10f2014-08-29 16:55:55 -0700456 if (ninePatchInsets == NULL) {
457 return nullObjectReturn("nine patch insets == null");
458 }
Amith Yamasaniec4a5042012-04-04 10:27:15 -0700459 if (javaBitmap != NULL) {
Chris Craik47cd8e92014-07-08 17:13:08 -0700460 env->SetObjectField(javaBitmap, gBitmap_ninePatchInsetsFieldID, ninePatchInsets);
Amith Yamasaniec4a5042012-04-04 10:27:15 -0700461 }
462 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463
John Reckf29ed282015-04-07 07:32:03 -0700464 SkBitmap outputBitmap;
Romain Guy7b2f8b82012-03-19 17:18:54 -0700465 if (willScale) {
466 // This is weird so let me explain: we could use the scale parameter
467 // directly, but for historical reasons this is how the corresponding
468 // Dalvik code has always behaved. We simply recreate the behavior here.
469 // The result is slightly different from simply using scale because of
470 // the 0.5f rounding bias applied when computing the target image size
Chris Craik7e8c03c2013-06-03 13:53:36 -0700471 const float sx = scaledWidth / float(decodingBitmap.width());
472 const float sy = scaledHeight / float(decodingBitmap.height());
Romain Guy7b2f8b82012-03-19 17:18:54 -0700473
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500474 // Set the allocator for the outputBitmap.
475 SkBitmap::Allocator* outputAllocator;
476 if (javaBitmap != nullptr) {
477 outputAllocator = &recyclingAllocator;
478 } else {
sergeyv45082182016-09-29 18:25:40 -0700479 outputAllocator = &defaultAllocator;
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500480 }
481
Leon Scroggins IIIef691a3d2017-07-10 17:03:55 -0400482 SkColorType scaledColorType = decodingBitmap.colorType();
Leon Scroggins III8790be62013-12-03 16:26:51 -0500483 // FIXME: If the alphaType is kUnpremul and the image has alpha, the
484 // colors may not be correct, since Skia does not yet support drawing
485 // to/from unpremultiplied bitmaps.
Matt Sarettee80c472016-06-03 10:23:38 -0400486 outputBitmap.setInfo(
487 bitmapInfo.makeWH(scaledWidth, scaledHeight).makeColorType(scaledColorType));
Mike Reed81397c42017-07-18 17:04:16 -0400488 if (!outputBitmap.tryAllocPixels(outputAllocator)) {
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500489 // This should only fail on OOM. The recyclingAllocator should have
490 // enough memory since we check this before decoding using the
491 // scaleCheckingAllocator.
Raph Levien005bfc62012-09-20 22:51:47 -0700492 return nullObjectReturn("allocation failed for scaled bitmap");
493 }
Leon Scroggins III1ffe7272013-09-19 11:34:06 -0400494
Romain Guy7b2f8b82012-03-19 17:18:54 -0700495 SkPaint paint;
Romain Guy253f2c22016-09-28 17:34:42 -0700496 // kSrc_Mode instructs us to overwrite the uninitialized pixels in
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500497 // outputBitmap. Otherwise we would blend by default, which is not
498 // what we want.
Mike Reed260ab722016-10-07 15:59:20 -0400499 paint.setBlendMode(SkBlendMode::kSrc);
Romain Guy253f2c22016-09-28 17:34:42 -0700500 paint.setFilterQuality(kLow_SkFilterQuality); // bilinear filtering
Romain Guy7b2f8b82012-03-19 17:18:54 -0700501
Matt Sarettca9b7032017-04-13 12:18:47 -0400502 SkCanvas canvas(outputBitmap, SkCanvas::ColorBehavior::kLegacy);
Romain Guy7b2f8b82012-03-19 17:18:54 -0700503 canvas.scale(sx, sy);
Chris Craik7e8c03c2013-06-03 13:53:36 -0700504 canvas.drawBitmap(decodingBitmap, 0.0f, 0.0f, &paint);
505 } else {
John Reckf29ed282015-04-07 07:32:03 -0700506 outputBitmap.swap(decodingBitmap);
Romain Guy7b2f8b82012-03-19 17:18:54 -0700507 }
508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 if (padding) {
Chris Craik47cd8e92014-07-08 17:13:08 -0700510 if (peeker.mPatch != NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 GraphicsJNI::set_jrect(env, padding,
Chris Craik47cd8e92014-07-08 17:13:08 -0700512 peeker.mPatch->paddingLeft, peeker.mPatch->paddingTop,
513 peeker.mPatch->paddingRight, peeker.mPatch->paddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 } else {
515 GraphicsJNI::set_jrect(env, padding, -1, -1, -1, -1);
516 }
517 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500519 // If we get here, the outputBitmap should have an installed pixelref.
John Reckf29ed282015-04-07 07:32:03 -0700520 if (outputBitmap.pixelRef() == NULL) {
Marco Nelissenb2fe3be2012-05-07 11:24:13 -0700521 return nullObjectReturn("Got null SkPixelRef");
522 }
Romain Guy23610982011-01-17 12:51:55 -0800523
Chris Craik7e8c03c2013-06-03 13:53:36 -0700524 if (!isMutable && javaBitmap == NULL) {
Romain Guy23610982011-01-17 12:51:55 -0800525 // promise we will never change our pixels (great for sharing and pictures)
John Reckf29ed282015-04-07 07:32:03 -0700526 outputBitmap.setImmutable();
Romain Guy23610982011-01-17 12:51:55 -0800527 }
Chet Haase37f74ca2010-12-08 17:56:36 -0800528
Matt Sarettb8adc9a2015-12-02 13:35:22 -0500529 bool isPremultiplied = !requireUnpremultiplied;
530 if (javaBitmap != nullptr) {
sergeyvc69853c2016-10-07 14:14:09 -0700531 bitmap::reinitBitmap(env, javaBitmap, outputBitmap.info(), isPremultiplied);
John Reckf29ed282015-04-07 07:32:03 -0700532 outputBitmap.notifyPixelsChanged();
Chet Haase37f74ca2010-12-08 17:56:36 -0800533 // If a java bitmap was passed in for reuse, pass it back
534 return javaBitmap;
535 }
Chris Craik1abf5d62013-08-16 12:47:03 -0700536
537 int bitmapCreateFlags = 0x0;
sergeyvc69853c2016-10-07 14:14:09 -0700538 if (isMutable) bitmapCreateFlags |= android::bitmap::kBitmapCreateFlag_Mutable;
539 if (isPremultiplied) bitmapCreateFlags |= android::bitmap::kBitmapCreateFlag_Premultiplied;
Chris Craik1abf5d62013-08-16 12:47:03 -0700540
sergeyvda6c8ffc2016-11-22 18:28:54 -0800541 if (isHardware) {
542 sk_sp<Bitmap> hardwareBitmap = Bitmap::allocateHardwareBitmap(outputBitmap);
sergeyvd67bb1e2017-06-28 12:44:03 -0700543 if (!hardwareBitmap.get()) {
544 return nullObjectReturn("Failed to allocate a hardware bitmap");
545 }
sergeyvda6c8ffc2016-11-22 18:28:54 -0800546 return bitmap::createBitmap(env, hardwareBitmap.release(), bitmapCreateFlags,
547 ninePatchChunk, ninePatchInsets, -1);
548 }
549
Mike Reedc70e06b2009-04-24 11:09:12 -0400550 // now create the java bitmap
sergeyvc69853c2016-10-07 14:14:09 -0700551 return bitmap::createBitmap(env, defaultAllocator.getStorageObjAndReset(),
Chris Craik47cd8e92014-07-08 17:13:08 -0700552 bitmapCreateFlags, ninePatchChunk, ninePatchInsets, -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553}
554
Chris Craik905e8242013-06-05 09:59:05 -0700555static jobject nativeDecodeStream(JNIEnv* env, jobject clazz, jobject is, jbyteArray storage,
556 jobject padding, jobject options) {
Romain Guy7b2f8b82012-03-19 17:18:54 -0700557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 jobject bitmap = NULL;
Ben Wagner60126ef2015-08-07 12:13:48 -0400559 std::unique_ptr<SkStream> stream(CreateJavaInputStreamAdaptor(env, is, storage));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560
Leon Scroggins IIIca320212013-08-20 17:59:39 -0400561 if (stream.get()) {
Ben Wagner60126ef2015-08-07 12:13:48 -0400562 std::unique_ptr<SkStreamRewindable> bufferedStream(
Matt Sarett96ffbdc2016-02-11 17:03:54 -0500563 SkFrontBufferedStream::Create(stream.release(), SkCodec::MinBufferedBytesNeeded()));
Leon Scroggins III7315f1b2013-09-10 20:26:05 -0400564 SkASSERT(bufferedStream.get() != NULL);
Mike Reedc7c65602017-07-26 10:40:25 -0400565 bitmap = doDecode(env, std::move(bufferedStream), padding, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 }
567 return bitmap;
568}
569
Romain Guy7b2f8b82012-03-19 17:18:54 -0700570static jobject nativeDecodeFileDescriptor(JNIEnv* env, jobject clazz, jobject fileDescriptor,
571 jobject padding, jobject bitmapFactoryOptions) {
572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 NPE_CHECK_RETURN_ZERO(env, fileDescriptor);
574
Derek Sollenberger5cb769d2014-09-24 09:20:09 -0400575 int descriptor = jniGetFDFromFileDescriptor(env, fileDescriptor);
Mike Reedc70e06b2009-04-24 11:09:12 -0400576
Derek Sollenberger5827cb52013-07-26 14:58:06 -0400577 struct stat fdStat;
578 if (fstat(descriptor, &fdStat) == -1) {
579 doThrowIOE(env, "broken file descriptor");
580 return nullObjectReturn("fstat return -1");
581 }
582
Derek Sollenberger5cb769d2014-09-24 09:20:09 -0400583 // Restore the descriptor's offset on exiting this function. Even though
584 // we dup the descriptor, both the original and dup refer to the same open
585 // file description and changes to the file offset in one impact the other.
Jérôme Poichetd29c9022014-09-26 18:59:11 +0000586 AutoFDSeek autoRestore(descriptor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587
Derek Sollenberger5cb769d2014-09-24 09:20:09 -0400588 // Duplicate the descriptor here to prevent leaking memory. A leak occurs
589 // if we only close the file descriptor and not the file object it is used to
590 // create. If we don't explicitly clean up the file (which in turn closes the
591 // descriptor) the buffers allocated internally by fseek will be leaked.
592 int dupDescriptor = dup(descriptor);
593
594 FILE* file = fdopen(dupDescriptor, "r");
Leon Scroggins III0102f8a2014-01-14 15:14:57 -0500595 if (file == NULL) {
Derek Sollenberger5cb769d2014-09-24 09:20:09 -0400596 // cleanup the duplicated descriptor since it will not be closed when the
597 // file is cleaned up (fclose).
598 close(dupDescriptor);
Leon Scroggins III0102f8a2014-01-14 15:14:57 -0500599 return nullObjectReturn("Could not open file");
Leon Scroggins IIIf65183f2013-10-07 16:32:14 -0400600 }
Leon Scroggins III0102f8a2014-01-14 15:14:57 -0500601
Leon Scroggins IIIdd3c06c2017-03-10 10:50:33 -0500602 std::unique_ptr<SkFILEStream> fileStream(new SkFILEStream(file));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603
Yujie Qinc1d7b7f2016-02-29 14:00:29 +0100604 // If there is no offset for the file descriptor, we use SkFILEStream directly.
605 if (::lseek(descriptor, 0, SEEK_CUR) == 0) {
606 assert(isSeekable(dupDescriptor));
Mike Reedc7c65602017-07-26 10:40:25 -0400607 return doDecode(env, std::move(fileStream), padding, bitmapFactoryOptions);
Yujie Qinc1d7b7f2016-02-29 14:00:29 +0100608 }
609
Leon Scroggins III0aa39dc2014-06-03 12:19:32 -0400610 // Use a buffered stream. Although an SkFILEStream can be rewound, this
611 // ensures that SkImageDecoder::Factory never rewinds beyond the
612 // current position of the file descriptor.
Ben Wagner60126ef2015-08-07 12:13:48 -0400613 std::unique_ptr<SkStreamRewindable> stream(SkFrontBufferedStream::Create(fileStream.release(),
Matt Sarett96ffbdc2016-02-11 17:03:54 -0500614 SkCodec::MinBufferedBytesNeeded()));
Leon Scroggins III2826e5f2014-02-05 19:46:02 -0500615
Mike Reedc7c65602017-07-26 10:40:25 -0400616 return doDecode(env, std::move(stream), padding, bitmapFactoryOptions);
Mike Reedc70e06b2009-04-24 11:09:12 -0400617}
618
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000619static jobject nativeDecodeAsset(JNIEnv* env, jobject clazz, jlong native_asset,
Chris Craik905e8242013-06-05 09:59:05 -0700620 jobject padding, jobject options) {
Romain Guy7b2f8b82012-03-19 17:18:54 -0700621
Mike Reedc70e06b2009-04-24 11:09:12 -0400622 Asset* asset = reinterpret_cast<Asset*>(native_asset);
Leon Scroggins III0aa39dc2014-06-03 12:19:32 -0400623 // since we know we'll be done with the asset when we return, we can
624 // just use a simple wrapper
Mike Reedc7c65602017-07-26 10:40:25 -0400625 return doDecode(env, skstd::make_unique<AssetStreamAdaptor>(asset), padding, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626}
627
628static jobject nativeDecodeByteArray(JNIEnv* env, jobject, jbyteArray byteArray,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000629 jint offset, jint length, jobject options) {
Romain Guy7b2f8b82012-03-19 17:18:54 -0700630
Mike Reedc70e06b2009-04-24 11:09:12 -0400631 AutoJavaByteArray ar(env, byteArray);
Mike Reedc7c65602017-07-26 10:40:25 -0400632 return doDecode(env, skstd::make_unique<SkMemoryStream>(ar.ptr() + offset, length, false),
633 nullptr, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634}
635
Owen Lina9d0d472011-01-18 17:39:15 +0800636static jboolean nativeIsSeekable(JNIEnv* env, jobject, jobject fileDescriptor) {
Elliott Hughesa3804cf2011-04-11 16:50:19 -0700637 jint descriptor = jniGetFDFromFileDescriptor(env, fileDescriptor);
Yujie Qinc1d7b7f2016-02-29 14:00:29 +0100638 return isSeekable(descriptor) ? JNI_TRUE : JNI_FALSE;
Owen Lina9d0d472011-01-18 17:39:15 +0800639}
640
John Reck41478772015-04-10 13:35:27 -0700641jobject decodeBitmap(JNIEnv* env, void* data, size_t size) {
Mike Reedc7c65602017-07-26 10:40:25 -0400642 return doDecode(env, skstd::make_unique<SkMemoryStream>(data, size),
643 nullptr, nullptr);
John Reck41478772015-04-10 13:35:27 -0700644}
645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646///////////////////////////////////////////////////////////////////////////////
647
Daniel Micay76f6a862015-09-19 17:31:01 -0400648static const JNINativeMethod gMethods[] = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 { "nativeDecodeStream",
650 "(Ljava/io/InputStream;[BLandroid/graphics/Rect;Landroid/graphics/BitmapFactory$Options;)Landroid/graphics/Bitmap;",
651 (void*)nativeDecodeStream
652 },
653
654 { "nativeDecodeFileDescriptor",
655 "(Ljava/io/FileDescriptor;Landroid/graphics/Rect;Landroid/graphics/BitmapFactory$Options;)Landroid/graphics/Bitmap;",
656 (void*)nativeDecodeFileDescriptor
657 },
658
659 { "nativeDecodeAsset",
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000660 "(JLandroid/graphics/Rect;Landroid/graphics/BitmapFactory$Options;)Landroid/graphics/Bitmap;",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 (void*)nativeDecodeAsset
662 },
663
664 { "nativeDecodeByteArray",
665 "([BIILandroid/graphics/BitmapFactory$Options;)Landroid/graphics/Bitmap;",
666 (void*)nativeDecodeByteArray
667 },
668
Owen Lina9d0d472011-01-18 17:39:15 +0800669 { "nativeIsSeekable",
670 "(Ljava/io/FileDescriptor;)Z",
671 (void*)nativeIsSeekable
672 },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673};
674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675int register_android_graphics_BitmapFactory(JNIEnv* env) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800676 jclass options_class = FindClassOrDie(env, "android/graphics/BitmapFactory$Options");
677 gOptions_bitmapFieldID = GetFieldIDOrDie(env, options_class, "inBitmap",
Chris Craik47cd8e92014-07-08 17:13:08 -0700678 "Landroid/graphics/Bitmap;");
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800679 gOptions_justBoundsFieldID = GetFieldIDOrDie(env, options_class, "inJustDecodeBounds", "Z");
680 gOptions_sampleSizeFieldID = GetFieldIDOrDie(env, options_class, "inSampleSize", "I");
681 gOptions_configFieldID = GetFieldIDOrDie(env, options_class, "inPreferredConfig",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 "Landroid/graphics/Bitmap$Config;");
Romain Guy95648b82017-04-13 18:43:42 -0700683 gOptions_colorSpaceFieldID = GetFieldIDOrDie(env, options_class, "inPreferredColorSpace",
684 "Landroid/graphics/ColorSpace;");
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800685 gOptions_premultipliedFieldID = GetFieldIDOrDie(env, options_class, "inPremultiplied", "Z");
686 gOptions_mutableFieldID = GetFieldIDOrDie(env, options_class, "inMutable", "Z");
687 gOptions_ditherFieldID = GetFieldIDOrDie(env, options_class, "inDither", "Z");
688 gOptions_preferQualityOverSpeedFieldID = GetFieldIDOrDie(env, options_class,
Wei-Ta Chen953f9092010-12-03 14:06:18 -0800689 "inPreferQualityOverSpeed", "Z");
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800690 gOptions_scaledFieldID = GetFieldIDOrDie(env, options_class, "inScaled", "Z");
691 gOptions_densityFieldID = GetFieldIDOrDie(env, options_class, "inDensity", "I");
692 gOptions_screenDensityFieldID = GetFieldIDOrDie(env, options_class, "inScreenDensity", "I");
693 gOptions_targetDensityFieldID = GetFieldIDOrDie(env, options_class, "inTargetDensity", "I");
694 gOptions_widthFieldID = GetFieldIDOrDie(env, options_class, "outWidth", "I");
695 gOptions_heightFieldID = GetFieldIDOrDie(env, options_class, "outHeight", "I");
696 gOptions_mimeFieldID = GetFieldIDOrDie(env, options_class, "outMimeType", "Ljava/lang/String;");
Romain Guye8d2ebb2017-02-09 18:38:47 -0800697 gOptions_outConfigFieldID = GetFieldIDOrDie(env, options_class, "outConfig",
698 "Landroid/graphics/Bitmap$Config;");
Romain Guy90fc43b2017-03-30 12:35:26 -0700699 gOptions_outColorSpaceFieldID = GetFieldIDOrDie(env, options_class, "outColorSpace",
700 "Landroid/graphics/ColorSpace;");
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800701 gOptions_mCancelID = GetFieldIDOrDie(env, options_class, "mCancel", "Z");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800703 jclass bitmap_class = FindClassOrDie(env, "android/graphics/Bitmap");
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800704 gBitmap_ninePatchInsetsFieldID = GetFieldIDOrDie(env, bitmap_class, "mNinePatchInsets",
Chris Craik47cd8e92014-07-08 17:13:08 -0700705 "Landroid/graphics/NinePatch$InsetStruct;");
706
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800707 gInsetStruct_class = MakeGlobalRefOrDie(env, FindClassOrDie(env,
708 "android/graphics/NinePatch$InsetStruct"));
709 gInsetStruct_constructorMethodID = GetMethodIDOrDie(env, gInsetStruct_class, "<init>",
710 "(IIIIIIIIFIF)V");
Chris Craik47cd8e92014-07-08 17:13:08 -0700711
Romain Guye8d2ebb2017-02-09 18:38:47 -0800712 gBitmapConfig_class = MakeGlobalRefOrDie(env, FindClassOrDie(env,
713 "android/graphics/Bitmap$Config"));
714 gBitmapConfig_nativeToConfigMethodID = GetStaticMethodIDOrDie(env, gBitmapConfig_class,
715 "nativeToConfig", "(I)Landroid/graphics/Bitmap$Config;");
716
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800717 return android::RegisterMethodsOrDie(env, "android/graphics/BitmapFactory",
718 gMethods, NELEM(gMethods));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719}