BitmapFactory.cpp revision 14494262d39964ccd7f0fbac43f45ac2ea1eb224
1#define LOG_TAG "BitmapFactory" 2 3#include "BitmapFactory.h" 4#include "NinePatchPeeker.h" 5#include "SkFrontBufferedStream.h" 6#include "SkImageDecoder.h" 7#include "SkMath.h" 8#include "SkPixelRef.h" 9#include "SkStream.h" 10#include "SkTemplates.h" 11#include "SkUtils.h" 12#include "CreateJavaOutputStreamAdaptor.h" 13#include "AutoDecodeCancel.h" 14#include "Utils.h" 15#include "JNIHelp.h" 16#include "GraphicsJNI.h" 17 18#include <android_runtime/AndroidRuntime.h> 19#include <androidfw/Asset.h> 20#include <androidfw/ResourceTypes.h> 21#include <cutils/compiler.h> 22#include <netinet/in.h> 23#include <stdio.h> 24#include <sys/mman.h> 25#include <sys/stat.h> 26 27jfieldID gOptions_justBoundsFieldID; 28jfieldID gOptions_sampleSizeFieldID; 29jfieldID gOptions_configFieldID; 30jfieldID gOptions_premultipliedFieldID; 31jfieldID gOptions_mutableFieldID; 32jfieldID gOptions_ditherFieldID; 33jfieldID gOptions_preferQualityOverSpeedFieldID; 34jfieldID gOptions_scaledFieldID; 35jfieldID gOptions_densityFieldID; 36jfieldID gOptions_screenDensityFieldID; 37jfieldID gOptions_targetDensityFieldID; 38jfieldID gOptions_widthFieldID; 39jfieldID gOptions_heightFieldID; 40jfieldID gOptions_mimeFieldID; 41jfieldID gOptions_mCancelID; 42jfieldID gOptions_bitmapFieldID; 43 44jfieldID gBitmap_nativeBitmapFieldID; 45jfieldID gBitmap_ninePatchInsetsFieldID; 46 47jclass gInsetStruct_class; 48jmethodID gInsetStruct_constructorMethodID; 49 50using namespace android; 51 52static inline int32_t validOrNeg1(bool isValid, int32_t value) { 53// return isValid ? value : -1; 54 SkASSERT((int)isValid == 0 || (int)isValid == 1); 55 return ((int32_t)isValid - 1) | value; 56} 57 58jstring getMimeTypeString(JNIEnv* env, SkImageDecoder::Format format) { 59 static const struct { 60 SkImageDecoder::Format fFormat; 61 const char* fMimeType; 62 } gMimeTypes[] = { 63 { SkImageDecoder::kBMP_Format, "image/bmp" }, 64 { SkImageDecoder::kGIF_Format, "image/gif" }, 65 { SkImageDecoder::kICO_Format, "image/x-ico" }, 66 { SkImageDecoder::kJPEG_Format, "image/jpeg" }, 67 { SkImageDecoder::kPNG_Format, "image/png" }, 68 { SkImageDecoder::kWEBP_Format, "image/webp" }, 69 { SkImageDecoder::kWBMP_Format, "image/vnd.wap.wbmp" } 70 }; 71 72 const char* cstr = NULL; 73 for (size_t i = 0; i < SK_ARRAY_COUNT(gMimeTypes); i++) { 74 if (gMimeTypes[i].fFormat == format) { 75 cstr = gMimeTypes[i].fMimeType; 76 break; 77 } 78 } 79 80 jstring jstr = 0; 81 if (NULL != cstr) { 82 jstr = env->NewStringUTF(cstr); 83 } 84 return jstr; 85} 86 87static bool optionsJustBounds(JNIEnv* env, jobject options) { 88 return options != NULL && env->GetBooleanField(options, gOptions_justBoundsFieldID); 89} 90 91static void scaleDivRange(int32_t* divs, int count, float scale, int maxValue) { 92 for (int i = 0; i < count; i++) { 93 divs[i] = int32_t(divs[i] * scale + 0.5f); 94 if (i > 0 && divs[i] == divs[i - 1]) { 95 divs[i]++; // avoid collisions 96 } 97 } 98 99 if (CC_UNLIKELY(divs[count - 1] > maxValue)) { 100 // if the collision avoidance above put some divs outside the bounds of the bitmap, 101 // slide outer stretchable divs inward to stay within bounds 102 int highestAvailable = maxValue; 103 for (int i = count - 1; i >= 0; i--) { 104 divs[i] = highestAvailable; 105 if (i > 0 && divs[i] <= divs[i-1]){ 106 // keep shifting 107 highestAvailable = divs[i] - 1; 108 } else { 109 break; 110 } 111 } 112 } 113} 114 115static void scaleNinePatchChunk(android::Res_png_9patch* chunk, float scale, 116 int scaledWidth, int scaledHeight) { 117 chunk->paddingLeft = int(chunk->paddingLeft * scale + 0.5f); 118 chunk->paddingTop = int(chunk->paddingTop * scale + 0.5f); 119 chunk->paddingRight = int(chunk->paddingRight * scale + 0.5f); 120 chunk->paddingBottom = int(chunk->paddingBottom * scale + 0.5f); 121 122 scaleDivRange(chunk->getXDivs(), chunk->numXDivs, scale, scaledWidth); 123 scaleDivRange(chunk->getYDivs(), chunk->numYDivs, scale, scaledHeight); 124} 125 126static SkColorType colorTypeForScaledOutput(SkColorType colorType) { 127 switch (colorType) { 128 case kUnknown_SkColorType: 129 case kIndex_8_SkColorType: 130 return kN32_SkColorType; 131 default: 132 break; 133 } 134 return colorType; 135} 136 137class ScaleCheckingAllocator : public SkBitmap::HeapAllocator { 138public: 139 ScaleCheckingAllocator(float scale, int size) 140 : mScale(scale), mSize(size) { 141 } 142 143 virtual bool allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable) { 144 // accounts for scale in final allocation, using eventual size and config 145 const int bytesPerPixel = SkColorTypeBytesPerPixel( 146 colorTypeForScaledOutput(bitmap->colorType())); 147 const int requestedSize = bytesPerPixel * 148 int(bitmap->width() * mScale + 0.5f) * 149 int(bitmap->height() * mScale + 0.5f); 150 if (requestedSize > mSize) { 151 ALOGW("bitmap for alloc reuse (%d bytes) can't fit scaled bitmap (%d bytes)", 152 mSize, requestedSize); 153 return false; 154 } 155 return SkBitmap::HeapAllocator::allocPixelRef(bitmap, ctable); 156 } 157private: 158 const float mScale; 159 const int mSize; 160}; 161 162class RecyclingPixelAllocator : public SkBitmap::Allocator { 163public: 164 RecyclingPixelAllocator(SkPixelRef* pixelRef, unsigned int size) 165 : mPixelRef(pixelRef), mSize(size) { 166 SkSafeRef(mPixelRef); 167 } 168 169 ~RecyclingPixelAllocator() { 170 SkSafeUnref(mPixelRef); 171 } 172 173 virtual bool allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable) { 174 const SkImageInfo& info = bitmap->info(); 175 if (info.fColorType == kUnknown_SkColorType) { 176 ALOGW("unable to reuse a bitmap as the target has an unknown bitmap configuration"); 177 return false; 178 } 179 180 const int64_t size64 = info.getSafeSize64(bitmap->rowBytes()); 181 if (!sk_64_isS32(size64)) { 182 ALOGW("bitmap is too large"); 183 return false; 184 } 185 186 const size_t size = sk_64_asS32(size64); 187 if (size > mSize) { 188 ALOGW("bitmap marked for reuse (%d bytes) can't fit new bitmap (%d bytes)", 189 mSize, size); 190 return false; 191 } 192 193 // Create a new pixelref with the new ctable that wraps the previous pixelref 194 SkPixelRef* pr = new AndroidPixelRef(*static_cast<AndroidPixelRef*>(mPixelRef), 195 info, bitmap->rowBytes(), ctable); 196 197 bitmap->setPixelRef(pr)->unref(); 198 // since we're already allocated, we lockPixels right away 199 // HeapAllocator/JavaPixelAllocator behaves this way too 200 bitmap->lockPixels(); 201 return true; 202 } 203 204private: 205 SkPixelRef* const mPixelRef; 206 const unsigned int mSize; 207}; 208 209static jobject doDecode(JNIEnv* env, SkStreamRewindable* stream, jobject padding, jobject options) { 210 211 int sampleSize = 1; 212 213 SkImageDecoder::Mode decodeMode = SkImageDecoder::kDecodePixels_Mode; 214 SkColorType prefColorType = kN32_SkColorType; 215 216 bool doDither = true; 217 bool isMutable = false; 218 float scale = 1.0f; 219 bool preferQualityOverSpeed = false; 220 bool requireUnpremultiplied = false; 221 222 jobject javaBitmap = NULL; 223 224 if (options != NULL) { 225 sampleSize = env->GetIntField(options, gOptions_sampleSizeFieldID); 226 if (optionsJustBounds(env, options)) { 227 decodeMode = SkImageDecoder::kDecodeBounds_Mode; 228 } 229 230 // initialize these, in case we fail later on 231 env->SetIntField(options, gOptions_widthFieldID, -1); 232 env->SetIntField(options, gOptions_heightFieldID, -1); 233 env->SetObjectField(options, gOptions_mimeFieldID, 0); 234 235 jobject jconfig = env->GetObjectField(options, gOptions_configFieldID); 236 prefColorType = GraphicsJNI::getNativeBitmapColorType(env, jconfig); 237 isMutable = env->GetBooleanField(options, gOptions_mutableFieldID); 238 doDither = env->GetBooleanField(options, gOptions_ditherFieldID); 239 preferQualityOverSpeed = env->GetBooleanField(options, 240 gOptions_preferQualityOverSpeedFieldID); 241 requireUnpremultiplied = !env->GetBooleanField(options, gOptions_premultipliedFieldID); 242 javaBitmap = env->GetObjectField(options, gOptions_bitmapFieldID); 243 244 if (env->GetBooleanField(options, gOptions_scaledFieldID)) { 245 const int density = env->GetIntField(options, gOptions_densityFieldID); 246 const int targetDensity = env->GetIntField(options, gOptions_targetDensityFieldID); 247 const int screenDensity = env->GetIntField(options, gOptions_screenDensityFieldID); 248 if (density != 0 && targetDensity != 0 && density != screenDensity) { 249 scale = (float) targetDensity / density; 250 } 251 } 252 } 253 254 const bool willScale = scale != 1.0f; 255 256 SkImageDecoder* decoder = SkImageDecoder::Factory(stream); 257 if (decoder == NULL) { 258 return nullObjectReturn("SkImageDecoder::Factory returned null"); 259 } 260 261 decoder->setSampleSize(sampleSize); 262 decoder->setDitherImage(doDither); 263 decoder->setPreferQualityOverSpeed(preferQualityOverSpeed); 264 decoder->setRequireUnpremultipliedColors(requireUnpremultiplied); 265 266 SkBitmap* outputBitmap = NULL; 267 unsigned int existingBufferSize = 0; 268 if (javaBitmap != NULL) { 269 outputBitmap = (SkBitmap*) env->GetLongField(javaBitmap, gBitmap_nativeBitmapFieldID); 270 if (outputBitmap->isImmutable()) { 271 ALOGW("Unable to reuse an immutable bitmap as an image decoder target."); 272 javaBitmap = NULL; 273 outputBitmap = NULL; 274 } else { 275 existingBufferSize = GraphicsJNI::getBitmapAllocationByteCount(env, javaBitmap); 276 } 277 } 278 279 SkAutoTDelete<SkBitmap> adb(outputBitmap == NULL ? new SkBitmap : NULL); 280 if (outputBitmap == NULL) outputBitmap = adb.get(); 281 282 NinePatchPeeker peeker(decoder); 283 decoder->setPeeker(&peeker); 284 285 JavaPixelAllocator javaAllocator(env); 286 RecyclingPixelAllocator recyclingAllocator(outputBitmap->pixelRef(), existingBufferSize); 287 ScaleCheckingAllocator scaleCheckingAllocator(scale, existingBufferSize); 288 SkBitmap::Allocator* outputAllocator = (javaBitmap != NULL) ? 289 (SkBitmap::Allocator*)&recyclingAllocator : (SkBitmap::Allocator*)&javaAllocator; 290 if (decodeMode != SkImageDecoder::kDecodeBounds_Mode) { 291 if (!willScale) { 292 // If the java allocator is being used to allocate the pixel memory, the decoder 293 // need not write zeroes, since the memory is initialized to 0. 294 decoder->setSkipWritingZeroes(outputAllocator == &javaAllocator); 295 decoder->setAllocator(outputAllocator); 296 } else if (javaBitmap != NULL) { 297 // check for eventual scaled bounds at allocation time, so we don't decode the bitmap 298 // only to find the scaled result too large to fit in the allocation 299 decoder->setAllocator(&scaleCheckingAllocator); 300 } 301 } 302 303 // Only setup the decoder to be deleted after its stack-based, refcounted 304 // components (allocators, peekers, etc) are declared. This prevents RefCnt 305 // asserts from firing due to the order objects are deleted from the stack. 306 SkAutoTDelete<SkImageDecoder> add(decoder); 307 308 AutoDecoderCancel adc(options, decoder); 309 310 // To fix the race condition in case "requestCancelDecode" 311 // happens earlier than AutoDecoderCancel object is added 312 // to the gAutoDecoderCancelMutex linked list. 313 if (options != NULL && env->GetBooleanField(options, gOptions_mCancelID)) { 314 return nullObjectReturn("gOptions_mCancelID"); 315 } 316 317 SkBitmap decodingBitmap; 318 if (decoder->decode(stream, &decodingBitmap, prefColorType, decodeMode) 319 != SkImageDecoder::kSuccess) { 320 return nullObjectReturn("decoder->decode returned false"); 321 } 322 323 int scaledWidth = decodingBitmap.width(); 324 int scaledHeight = decodingBitmap.height(); 325 326 if (willScale && decodeMode != SkImageDecoder::kDecodeBounds_Mode) { 327 scaledWidth = int(scaledWidth * scale + 0.5f); 328 scaledHeight = int(scaledHeight * scale + 0.5f); 329 } 330 331 // update options (if any) 332 if (options != NULL) { 333 env->SetIntField(options, gOptions_widthFieldID, scaledWidth); 334 env->SetIntField(options, gOptions_heightFieldID, scaledHeight); 335 env->SetObjectField(options, gOptions_mimeFieldID, 336 getMimeTypeString(env, decoder->getFormat())); 337 } 338 339 // if we're in justBounds mode, return now (skip the java bitmap) 340 if (decodeMode == SkImageDecoder::kDecodeBounds_Mode) { 341 return NULL; 342 } 343 344 jbyteArray ninePatchChunk = NULL; 345 if (peeker.mPatch != NULL) { 346 if (willScale) { 347 scaleNinePatchChunk(peeker.mPatch, scale, scaledWidth, scaledHeight); 348 } 349 350 size_t ninePatchArraySize = peeker.mPatch->serializedSize(); 351 ninePatchChunk = env->NewByteArray(ninePatchArraySize); 352 if (ninePatchChunk == NULL) { 353 return nullObjectReturn("ninePatchChunk == null"); 354 } 355 356 jbyte* array = (jbyte*) env->GetPrimitiveArrayCritical(ninePatchChunk, NULL); 357 if (array == NULL) { 358 return nullObjectReturn("primitive array == null"); 359 } 360 361 memcpy(array, peeker.mPatch, peeker.mPatchSize); 362 env->ReleasePrimitiveArrayCritical(ninePatchChunk, array, 0); 363 } 364 365 jobject ninePatchInsets = NULL; 366 if (peeker.mHasInsets) { 367 ninePatchInsets = env->NewObject(gInsetStruct_class, gInsetStruct_constructorMethodID, 368 peeker.mOpticalInsets[0], peeker.mOpticalInsets[1], peeker.mOpticalInsets[2], peeker.mOpticalInsets[3], 369 peeker.mOutlineInsets[0], peeker.mOutlineInsets[1], peeker.mOutlineInsets[2], peeker.mOutlineInsets[3], 370 peeker.mOutlineRadius, peeker.mOutlineAlpha, scale); 371 if (ninePatchInsets == NULL) { 372 return nullObjectReturn("nine patch insets == null"); 373 } 374 if (javaBitmap != NULL) { 375 env->SetObjectField(javaBitmap, gBitmap_ninePatchInsetsFieldID, ninePatchInsets); 376 } 377 } 378 379 if (willScale) { 380 // This is weird so let me explain: we could use the scale parameter 381 // directly, but for historical reasons this is how the corresponding 382 // Dalvik code has always behaved. We simply recreate the behavior here. 383 // The result is slightly different from simply using scale because of 384 // the 0.5f rounding bias applied when computing the target image size 385 const float sx = scaledWidth / float(decodingBitmap.width()); 386 const float sy = scaledHeight / float(decodingBitmap.height()); 387 388 // TODO: avoid copying when scaled size equals decodingBitmap size 389 SkColorType colorType = colorTypeForScaledOutput(decodingBitmap.colorType()); 390 // FIXME: If the alphaType is kUnpremul and the image has alpha, the 391 // colors may not be correct, since Skia does not yet support drawing 392 // to/from unpremultiplied bitmaps. 393 outputBitmap->setInfo(SkImageInfo::Make(scaledWidth, scaledHeight, 394 colorType, decodingBitmap.alphaType())); 395 if (!outputBitmap->allocPixels(outputAllocator, NULL)) { 396 return nullObjectReturn("allocation failed for scaled bitmap"); 397 } 398 399 // If outputBitmap's pixels are newly allocated by Java, there is no need 400 // to erase to 0, since the pixels were initialized to 0. 401 if (outputAllocator != &javaAllocator) { 402 outputBitmap->eraseColor(0); 403 } 404 405 SkPaint paint; 406 paint.setFilterLevel(SkPaint::kLow_FilterLevel); 407 408 SkCanvas canvas(*outputBitmap); 409 canvas.scale(sx, sy); 410 canvas.drawBitmap(decodingBitmap, 0.0f, 0.0f, &paint); 411 } else { 412 outputBitmap->swap(decodingBitmap); 413 } 414 415 if (padding) { 416 if (peeker.mPatch != NULL) { 417 GraphicsJNI::set_jrect(env, padding, 418 peeker.mPatch->paddingLeft, peeker.mPatch->paddingTop, 419 peeker.mPatch->paddingRight, peeker.mPatch->paddingBottom); 420 } else { 421 GraphicsJNI::set_jrect(env, padding, -1, -1, -1, -1); 422 } 423 } 424 425 // if we get here, we're in kDecodePixels_Mode and will therefore 426 // already have a pixelref installed. 427 if (outputBitmap->pixelRef() == NULL) { 428 return nullObjectReturn("Got null SkPixelRef"); 429 } 430 431 if (!isMutable && javaBitmap == NULL) { 432 // promise we will never change our pixels (great for sharing and pictures) 433 outputBitmap->setImmutable(); 434 } 435 436 // detach bitmap from its autodeleter, since we want to own it now 437 adb.detach(); 438 439 if (javaBitmap != NULL) { 440 bool isPremultiplied = !requireUnpremultiplied; 441 GraphicsJNI::reinitBitmap(env, javaBitmap, outputBitmap, isPremultiplied); 442 outputBitmap->notifyPixelsChanged(); 443 // If a java bitmap was passed in for reuse, pass it back 444 return javaBitmap; 445 } 446 447 int bitmapCreateFlags = 0x0; 448 if (isMutable) bitmapCreateFlags |= GraphicsJNI::kBitmapCreateFlag_Mutable; 449 if (!requireUnpremultiplied) bitmapCreateFlags |= GraphicsJNI::kBitmapCreateFlag_Premultiplied; 450 451 // now create the java bitmap 452 return GraphicsJNI::createBitmap(env, outputBitmap, javaAllocator.getStorageObj(), 453 bitmapCreateFlags, ninePatchChunk, ninePatchInsets, -1); 454} 455 456// Need to buffer enough input to be able to rewind as much as might be read by a decoder 457// trying to determine the stream's format. Currently the most is 64, read by 458// SkImageDecoder_libwebp. 459// FIXME: Get this number from SkImageDecoder 460#define BYTES_TO_BUFFER 64 461 462static jobject nativeDecodeStream(JNIEnv* env, jobject clazz, jobject is, jbyteArray storage, 463 jobject padding, jobject options) { 464 465 jobject bitmap = NULL; 466 SkAutoTUnref<SkStream> stream(CreateJavaInputStreamAdaptor(env, is, storage)); 467 468 if (stream.get()) { 469 SkAutoTUnref<SkStreamRewindable> bufferedStream( 470 SkFrontBufferedStream::Create(stream, BYTES_TO_BUFFER)); 471 SkASSERT(bufferedStream.get() != NULL); 472 bitmap = doDecode(env, bufferedStream, padding, options); 473 } 474 return bitmap; 475} 476 477static jobject nativeDecodeFileDescriptor(JNIEnv* env, jobject clazz, jobject fileDescriptor, 478 jobject padding, jobject bitmapFactoryOptions) { 479 480 NPE_CHECK_RETURN_ZERO(env, fileDescriptor); 481 482 int descriptor = jniGetFDFromFileDescriptor(env, fileDescriptor); 483 484 struct stat fdStat; 485 if (fstat(descriptor, &fdStat) == -1) { 486 doThrowIOE(env, "broken file descriptor"); 487 return nullObjectReturn("fstat return -1"); 488 } 489 490 // Restore the descriptor's offset on exiting this function. Even though 491 // we dup the descriptor, both the original and dup refer to the same open 492 // file description and changes to the file offset in one impact the other. 493 AutoFDSeek autoRestore(descriptor); 494 495 // Duplicate the descriptor here to prevent leaking memory. A leak occurs 496 // if we only close the file descriptor and not the file object it is used to 497 // create. If we don't explicitly clean up the file (which in turn closes the 498 // descriptor) the buffers allocated internally by fseek will be leaked. 499 int dupDescriptor = dup(descriptor); 500 501 FILE* file = fdopen(dupDescriptor, "r"); 502 if (file == NULL) { 503 // cleanup the duplicated descriptor since it will not be closed when the 504 // file is cleaned up (fclose). 505 close(dupDescriptor); 506 return nullObjectReturn("Could not open file"); 507 } 508 509 SkAutoTUnref<SkFILEStream> fileStream(new SkFILEStream(file, 510 SkFILEStream::kCallerPasses_Ownership)); 511 512 // Use a buffered stream. Although an SkFILEStream can be rewound, this 513 // ensures that SkImageDecoder::Factory never rewinds beyond the 514 // current position of the file descriptor. 515 SkAutoTUnref<SkStreamRewindable> stream(SkFrontBufferedStream::Create(fileStream, 516 BYTES_TO_BUFFER)); 517 518 return doDecode(env, stream, padding, bitmapFactoryOptions); 519} 520 521static jobject nativeDecodeAsset(JNIEnv* env, jobject clazz, jlong native_asset, 522 jobject padding, jobject options) { 523 524 Asset* asset = reinterpret_cast<Asset*>(native_asset); 525 // since we know we'll be done with the asset when we return, we can 526 // just use a simple wrapper 527 SkAutoTUnref<SkStreamRewindable> stream(new AssetStreamAdaptor(asset, 528 AssetStreamAdaptor::kNo_OwnAsset, AssetStreamAdaptor::kNo_HasMemoryBase)); 529 return doDecode(env, stream, padding, options); 530} 531 532static jobject nativeDecodeByteArray(JNIEnv* env, jobject, jbyteArray byteArray, 533 jint offset, jint length, jobject options) { 534 535 AutoJavaByteArray ar(env, byteArray); 536 SkMemoryStream* stream = new SkMemoryStream(ar.ptr() + offset, length, false); 537 SkAutoUnref aur(stream); 538 return doDecode(env, stream, NULL, options); 539} 540 541static void nativeRequestCancel(JNIEnv*, jobject joptions) { 542 (void)AutoDecoderCancel::RequestCancel(joptions); 543} 544 545static jboolean nativeIsSeekable(JNIEnv* env, jobject, jobject fileDescriptor) { 546 jint descriptor = jniGetFDFromFileDescriptor(env, fileDescriptor); 547 return ::lseek64(descriptor, 0, SEEK_CUR) != -1 ? JNI_TRUE : JNI_FALSE; 548} 549 550/////////////////////////////////////////////////////////////////////////////// 551 552static JNINativeMethod gMethods[] = { 553 { "nativeDecodeStream", 554 "(Ljava/io/InputStream;[BLandroid/graphics/Rect;Landroid/graphics/BitmapFactory$Options;)Landroid/graphics/Bitmap;", 555 (void*)nativeDecodeStream 556 }, 557 558 { "nativeDecodeFileDescriptor", 559 "(Ljava/io/FileDescriptor;Landroid/graphics/Rect;Landroid/graphics/BitmapFactory$Options;)Landroid/graphics/Bitmap;", 560 (void*)nativeDecodeFileDescriptor 561 }, 562 563 { "nativeDecodeAsset", 564 "(JLandroid/graphics/Rect;Landroid/graphics/BitmapFactory$Options;)Landroid/graphics/Bitmap;", 565 (void*)nativeDecodeAsset 566 }, 567 568 { "nativeDecodeByteArray", 569 "([BIILandroid/graphics/BitmapFactory$Options;)Landroid/graphics/Bitmap;", 570 (void*)nativeDecodeByteArray 571 }, 572 573 { "nativeIsSeekable", 574 "(Ljava/io/FileDescriptor;)Z", 575 (void*)nativeIsSeekable 576 }, 577}; 578 579static JNINativeMethod gOptionsMethods[] = { 580 { "requestCancel", "()V", (void*)nativeRequestCancel } 581}; 582 583static jfieldID getFieldIDCheck(JNIEnv* env, jclass clazz, 584 const char fieldname[], const char type[]) { 585 jfieldID id = env->GetFieldID(clazz, fieldname, type); 586 SkASSERT(id); 587 return id; 588} 589 590int register_android_graphics_BitmapFactory(JNIEnv* env) { 591 jclass options_class = env->FindClass("android/graphics/BitmapFactory$Options"); 592 SkASSERT(options_class); 593 gOptions_bitmapFieldID = getFieldIDCheck(env, options_class, "inBitmap", 594 "Landroid/graphics/Bitmap;"); 595 gOptions_justBoundsFieldID = getFieldIDCheck(env, options_class, "inJustDecodeBounds", "Z"); 596 gOptions_sampleSizeFieldID = getFieldIDCheck(env, options_class, "inSampleSize", "I"); 597 gOptions_configFieldID = getFieldIDCheck(env, options_class, "inPreferredConfig", 598 "Landroid/graphics/Bitmap$Config;"); 599 gOptions_premultipliedFieldID = getFieldIDCheck(env, options_class, "inPremultiplied", "Z"); 600 gOptions_mutableFieldID = getFieldIDCheck(env, options_class, "inMutable", "Z"); 601 gOptions_ditherFieldID = getFieldIDCheck(env, options_class, "inDither", "Z"); 602 gOptions_preferQualityOverSpeedFieldID = getFieldIDCheck(env, options_class, 603 "inPreferQualityOverSpeed", "Z"); 604 gOptions_scaledFieldID = getFieldIDCheck(env, options_class, "inScaled", "Z"); 605 gOptions_densityFieldID = getFieldIDCheck(env, options_class, "inDensity", "I"); 606 gOptions_screenDensityFieldID = getFieldIDCheck(env, options_class, "inScreenDensity", "I"); 607 gOptions_targetDensityFieldID = getFieldIDCheck(env, options_class, "inTargetDensity", "I"); 608 gOptions_widthFieldID = getFieldIDCheck(env, options_class, "outWidth", "I"); 609 gOptions_heightFieldID = getFieldIDCheck(env, options_class, "outHeight", "I"); 610 gOptions_mimeFieldID = getFieldIDCheck(env, options_class, "outMimeType", "Ljava/lang/String;"); 611 gOptions_mCancelID = getFieldIDCheck(env, options_class, "mCancel", "Z"); 612 613 jclass bitmap_class = env->FindClass("android/graphics/Bitmap"); 614 SkASSERT(bitmap_class); 615 gBitmap_nativeBitmapFieldID = getFieldIDCheck(env, bitmap_class, "mNativeBitmap", "J"); 616 gBitmap_ninePatchInsetsFieldID = getFieldIDCheck(env, bitmap_class, "mNinePatchInsets", 617 "Landroid/graphics/NinePatch$InsetStruct;"); 618 619 gInsetStruct_class = (jclass) env->NewGlobalRef(env->FindClass("android/graphics/NinePatch$InsetStruct")); 620 gInsetStruct_constructorMethodID = env->GetMethodID(gInsetStruct_class, "<init>", "(IIIIIIIIFIF)V"); 621 622 int ret = AndroidRuntime::registerNativeMethods(env, 623 "android/graphics/BitmapFactory$Options", 624 gOptionsMethods, 625 SK_ARRAY_COUNT(gOptionsMethods)); 626 if (ret) { 627 return ret; 628 } 629 return android::AndroidRuntime::registerNativeMethods(env, "android/graphics/BitmapFactory", 630 gMethods, SK_ARRAY_COUNT(gMethods)); 631} 632