BitmapFactory.cpp revision cd0ba71aa942f35fcdb26808b86f20073b8aff92
1#define LOG_TAG "BitmapFactory" 2 3#include "BitmapFactory.h" 4#include "NinePatchPeeker.h" 5#include "SkData.h" 6#include "SkImageDecoder.h" 7#include "SkImageRef_ashmem.h" 8#include "SkImageRef_GlobalPool.h" 9#include "SkPixelRef.h" 10#include "SkStream.h" 11#include "SkTemplates.h" 12#include "SkUtils.h" 13#include "CreateJavaOutputStreamAdaptor.h" 14#include "AutoDecodeCancel.h" 15#include "Utils.h" 16#include "JNIHelp.h" 17#include "GraphicsJNI.h" 18 19#include <android_runtime/AndroidRuntime.h> 20#include <androidfw/Asset.h> 21#include <androidfw/ResourceTypes.h> 22#include <netinet/in.h> 23#include <sys/mman.h> 24#include <sys/stat.h> 25 26jfieldID gOptions_justBoundsFieldID; 27jfieldID gOptions_sampleSizeFieldID; 28jfieldID gOptions_configFieldID; 29jfieldID gOptions_premultipliedFieldID; 30jfieldID gOptions_mutableFieldID; 31jfieldID gOptions_ditherFieldID; 32jfieldID gOptions_purgeableFieldID; 33jfieldID gOptions_shareableFieldID; 34jfieldID gOptions_preferQualityOverSpeedFieldID; 35jfieldID gOptions_scaledFieldID; 36jfieldID gOptions_densityFieldID; 37jfieldID gOptions_screenDensityFieldID; 38jfieldID gOptions_targetDensityFieldID; 39jfieldID gOptions_widthFieldID; 40jfieldID gOptions_heightFieldID; 41jfieldID gOptions_mimeFieldID; 42jfieldID gOptions_mCancelID; 43jfieldID gOptions_bitmapFieldID; 44jfieldID gBitmap_nativeBitmapFieldID; 45jfieldID gBitmap_layoutBoundsFieldID; 46 47#if 0 48 #define TRACE_BITMAP(code) code 49#else 50 #define TRACE_BITMAP(code) 51#endif 52 53using namespace android; 54 55static inline int32_t validOrNeg1(bool isValid, int32_t value) { 56// return isValid ? value : -1; 57 SkASSERT((int)isValid == 0 || (int)isValid == 1); 58 return ((int32_t)isValid - 1) | value; 59} 60 61jstring getMimeTypeString(JNIEnv* env, SkImageDecoder::Format format) { 62 static const struct { 63 SkImageDecoder::Format fFormat; 64 const char* fMimeType; 65 } gMimeTypes[] = { 66 { SkImageDecoder::kBMP_Format, "image/bmp" }, 67 { SkImageDecoder::kGIF_Format, "image/gif" }, 68 { SkImageDecoder::kICO_Format, "image/x-ico" }, 69 { SkImageDecoder::kJPEG_Format, "image/jpeg" }, 70 { SkImageDecoder::kPNG_Format, "image/png" }, 71 { SkImageDecoder::kWEBP_Format, "image/webp" }, 72 { SkImageDecoder::kWBMP_Format, "image/vnd.wap.wbmp" } 73 }; 74 75 const char* cstr = NULL; 76 for (size_t i = 0; i < SK_ARRAY_COUNT(gMimeTypes); i++) { 77 if (gMimeTypes[i].fFormat == format) { 78 cstr = gMimeTypes[i].fMimeType; 79 break; 80 } 81 } 82 83 jstring jstr = 0; 84 if (NULL != cstr) { 85 jstr = env->NewStringUTF(cstr); 86 } 87 return jstr; 88} 89 90static bool optionsPurgeable(JNIEnv* env, jobject options) { 91 return options != NULL && env->GetBooleanField(options, gOptions_purgeableFieldID); 92} 93 94static bool optionsShareable(JNIEnv* env, jobject options) { 95 return options != NULL && env->GetBooleanField(options, gOptions_shareableFieldID); 96} 97 98static bool optionsJustBounds(JNIEnv* env, jobject options) { 99 return options != NULL && env->GetBooleanField(options, gOptions_justBoundsFieldID); 100} 101 102static void scaleNinePatchChunk(android::Res_png_9patch* chunk, float scale) { 103 chunk->paddingLeft = int(chunk->paddingLeft * scale + 0.5f); 104 chunk->paddingTop = int(chunk->paddingTop * scale + 0.5f); 105 chunk->paddingRight = int(chunk->paddingRight * scale + 0.5f); 106 chunk->paddingBottom = int(chunk->paddingBottom * scale + 0.5f); 107 108 for (int i = 0; i < chunk->numXDivs; i++) { 109 chunk->xDivs[i] = int(chunk->xDivs[i] * scale + 0.5f); 110 if (i > 0 && chunk->xDivs[i] == chunk->xDivs[i - 1]) { 111 chunk->xDivs[i]++; 112 } 113 } 114 115 for (int i = 0; i < chunk->numYDivs; i++) { 116 chunk->yDivs[i] = int(chunk->yDivs[i] * scale + 0.5f); 117 if (i > 0 && chunk->yDivs[i] == chunk->yDivs[i - 1]) { 118 chunk->yDivs[i]++; 119 } 120 } 121} 122 123static SkPixelRef* installPixelRef(SkBitmap* bitmap, SkStream* stream, 124 int sampleSize, bool ditherImage) { 125 126 SkImageRef* pr; 127 // only use ashmem for large images, since mmaps come at a price 128 if (bitmap->getSize() >= 32 * 1024) { 129 pr = new SkImageRef_ashmem(stream, bitmap->config(), sampleSize); 130 } else { 131 pr = new SkImageRef_GlobalPool(stream, bitmap->config(), sampleSize); 132 } 133 pr->setDitherImage(ditherImage); 134 bitmap->setPixelRef(pr)->unref(); 135 pr->isOpaque(bitmap); 136 return pr; 137} 138 139static SkBitmap::Config configForScaledOutput(SkBitmap::Config config) { 140 switch (config) { 141 case SkBitmap::kNo_Config: 142 case SkBitmap::kIndex8_Config: 143 return SkBitmap::kARGB_8888_Config; 144 default: 145 break; 146 } 147 return config; 148} 149 150class ScaleCheckingAllocator : public SkBitmap::HeapAllocator { 151public: 152 ScaleCheckingAllocator(float scale, int size) 153 : mScale(scale), mSize(size) { 154 } 155 156 virtual bool allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable) { 157 // accounts for scale in final allocation, using eventual size and config 158 const int bytesPerPixel = SkBitmap::ComputeBytesPerPixel( 159 configForScaledOutput(bitmap->getConfig())); 160 const int requestedSize = bytesPerPixel * 161 int(bitmap->width() * mScale + 0.5f) * 162 int(bitmap->height() * mScale + 0.5f); 163 if (requestedSize > mSize) { 164 ALOGW("bitmap for alloc reuse (%d bytes) can't fit scaled bitmap (%d bytes)", 165 mSize, requestedSize); 166 return false; 167 } 168 return SkBitmap::HeapAllocator::allocPixelRef(bitmap, ctable); 169 } 170private: 171 const float mScale; 172 const int mSize; 173}; 174 175class RecyclingPixelAllocator : public SkBitmap::Allocator { 176public: 177 RecyclingPixelAllocator(SkPixelRef* pixelRef, unsigned int size) 178 : mPixelRef(pixelRef), mSize(size) { 179 SkSafeRef(mPixelRef); 180 } 181 182 ~RecyclingPixelAllocator() { 183 SkSafeUnref(mPixelRef); 184 } 185 186 virtual bool allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable) { 187 if (!bitmap->getSize64().is32() || bitmap->getSize() > mSize) { 188 ALOGW("bitmap marked for reuse (%d bytes) can't fit new bitmap (%d bytes)", 189 mSize, bitmap->getSize()); 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), ctable); 195 196 bitmap->setPixelRef(pr)->unref(); 197 // since we're already allocated, we lockPixels right away 198 // HeapAllocator/JavaPixelAllocator behaves this way too 199 bitmap->lockPixels(); 200 return true; 201 } 202 203private: 204 SkPixelRef* const mPixelRef; 205 const unsigned int mSize; 206}; 207 208// since we "may" create a purgeable imageref, we require the stream be ref'able 209// i.e. dynamically allocated, since its lifetime may exceed the current stack 210// frame. 211static jobject doDecode(JNIEnv* env, SkStreamRewindable* stream, jobject padding, 212 jobject options, bool allowPurgeable, bool forcePurgeable = false) { 213 214 int sampleSize = 1; 215 216 SkImageDecoder::Mode mode = SkImageDecoder::kDecodePixels_Mode; 217 SkBitmap::Config prefConfig = SkBitmap::kARGB_8888_Config; 218 219 bool doDither = true; 220 bool isMutable = false; 221 float scale = 1.0f; 222 bool isPurgeable = forcePurgeable || (allowPurgeable && optionsPurgeable(env, options)); 223 bool preferQualityOverSpeed = false; 224 bool requireUnpremultiplied = false; 225 226 jobject javaBitmap = NULL; 227 228 if (options != NULL) { 229 sampleSize = env->GetIntField(options, gOptions_sampleSizeFieldID); 230 if (optionsJustBounds(env, options)) { 231 mode = SkImageDecoder::kDecodeBounds_Mode; 232 } 233 234 // initialize these, in case we fail later on 235 env->SetIntField(options, gOptions_widthFieldID, -1); 236 env->SetIntField(options, gOptions_heightFieldID, -1); 237 env->SetObjectField(options, gOptions_mimeFieldID, 0); 238 239 jobject jconfig = env->GetObjectField(options, gOptions_configFieldID); 240 prefConfig = GraphicsJNI::getNativeBitmapConfig(env, jconfig); 241 isMutable = env->GetBooleanField(options, gOptions_mutableFieldID); 242 doDither = env->GetBooleanField(options, gOptions_ditherFieldID); 243 preferQualityOverSpeed = env->GetBooleanField(options, 244 gOptions_preferQualityOverSpeedFieldID); 245 requireUnpremultiplied = !env->GetBooleanField(options, gOptions_premultipliedFieldID); 246 javaBitmap = env->GetObjectField(options, gOptions_bitmapFieldID); 247 248 if (env->GetBooleanField(options, gOptions_scaledFieldID)) { 249 const int density = env->GetIntField(options, gOptions_densityFieldID); 250 const int targetDensity = env->GetIntField(options, gOptions_targetDensityFieldID); 251 const int screenDensity = env->GetIntField(options, gOptions_screenDensityFieldID); 252 if (density != 0 && targetDensity != 0 && density != screenDensity) { 253 scale = (float) targetDensity / density; 254 } 255 } 256 } 257 258 const bool willScale = scale != 1.0f; 259 isPurgeable &= !willScale; 260 261 SkImageDecoder* decoder = SkImageDecoder::Factory(stream); 262 if (decoder == NULL) { 263 return nullObjectReturn("SkImageDecoder::Factory returned null"); 264 } 265 266 decoder->setSampleSize(sampleSize); 267 decoder->setDitherImage(doDither); 268 decoder->setPreferQualityOverSpeed(preferQualityOverSpeed); 269 decoder->setRequireUnpremultipliedColors(requireUnpremultiplied); 270 271 SkBitmap* outputBitmap = NULL; 272 unsigned int existingBufferSize = 0; 273 if (javaBitmap != NULL) { 274 outputBitmap = (SkBitmap*) env->GetIntField(javaBitmap, gBitmap_nativeBitmapFieldID); 275 if (outputBitmap->isImmutable()) { 276 ALOGW("Unable to reuse an immutable bitmap as an image decoder target."); 277 javaBitmap = NULL; 278 outputBitmap = NULL; 279 } else { 280 existingBufferSize = GraphicsJNI::getBitmapAllocationByteCount(env, javaBitmap); 281 } 282 } 283 284 SkAutoTDelete<SkBitmap> adb(outputBitmap == NULL ? new SkBitmap : NULL); 285 if (outputBitmap == NULL) outputBitmap = adb.get(); 286 287 NinePatchPeeker peeker(decoder); 288 decoder->setPeeker(&peeker); 289 290 SkImageDecoder::Mode decodeMode = isPurgeable ? SkImageDecoder::kDecodeBounds_Mode : mode; 291 292 JavaPixelAllocator javaAllocator(env); 293 RecyclingPixelAllocator recyclingAllocator(outputBitmap->pixelRef(), existingBufferSize); 294 ScaleCheckingAllocator scaleCheckingAllocator(scale, existingBufferSize); 295 SkBitmap::Allocator* outputAllocator = (javaBitmap != NULL) ? 296 (SkBitmap::Allocator*)&recyclingAllocator : (SkBitmap::Allocator*)&javaAllocator; 297 if (decodeMode != SkImageDecoder::kDecodeBounds_Mode) { 298 if (!willScale) { 299 decoder->setAllocator(outputAllocator); 300 } else if (javaBitmap != NULL) { 301 // check for eventual scaled bounds at allocation time, so we don't decode the bitmap 302 // only to find the scaled result too large to fit in the allocation 303 decoder->setAllocator(&scaleCheckingAllocator); 304 } 305 } 306 307 // Only setup the decoder to be deleted after its stack-based, refcounted 308 // components (allocators, peekers, etc) are declared. This prevents RefCnt 309 // asserts from firing due to the order objects are deleted from the stack. 310 SkAutoTDelete<SkImageDecoder> add(decoder); 311 312 AutoDecoderCancel adc(options, decoder); 313 314 // To fix the race condition in case "requestCancelDecode" 315 // happens earlier than AutoDecoderCancel object is added 316 // to the gAutoDecoderCancelMutex linked list. 317 if (options != NULL && env->GetBooleanField(options, gOptions_mCancelID)) { 318 return nullObjectReturn("gOptions_mCancelID"); 319 } 320 321 SkBitmap decodingBitmap; 322 if (!decoder->decode(stream, &decodingBitmap, prefConfig, decodeMode)) { 323 return nullObjectReturn("decoder->decode returned false"); 324 } 325 326 int scaledWidth = decodingBitmap.width(); 327 int scaledHeight = decodingBitmap.height(); 328 329 if (willScale && mode != SkImageDecoder::kDecodeBounds_Mode) { 330 scaledWidth = int(scaledWidth * scale + 0.5f); 331 scaledHeight = int(scaledHeight * scale + 0.5f); 332 } 333 334 // update options (if any) 335 if (options != NULL) { 336 env->SetIntField(options, gOptions_widthFieldID, scaledWidth); 337 env->SetIntField(options, gOptions_heightFieldID, scaledHeight); 338 env->SetObjectField(options, gOptions_mimeFieldID, 339 getMimeTypeString(env, decoder->getFormat())); 340 } 341 342 // if we're in justBounds mode, return now (skip the java bitmap) 343 if (mode == SkImageDecoder::kDecodeBounds_Mode) { 344 return NULL; 345 } 346 347 jbyteArray ninePatchChunk = NULL; 348 if (peeker.fPatch != NULL) { 349 if (willScale) { 350 scaleNinePatchChunk(peeker.fPatch, scale); 351 } 352 353 size_t ninePatchArraySize = peeker.fPatch->serializedSize(); 354 ninePatchChunk = env->NewByteArray(ninePatchArraySize); 355 if (ninePatchChunk == NULL) { 356 return nullObjectReturn("ninePatchChunk == null"); 357 } 358 359 jbyte* array = (jbyte*) env->GetPrimitiveArrayCritical(ninePatchChunk, NULL); 360 if (array == NULL) { 361 return nullObjectReturn("primitive array == null"); 362 } 363 364 peeker.fPatch->serialize(array); 365 env->ReleasePrimitiveArrayCritical(ninePatchChunk, array, 0); 366 } 367 368 jintArray layoutBounds = NULL; 369 if (peeker.fLayoutBounds != NULL) { 370 layoutBounds = env->NewIntArray(4); 371 if (layoutBounds == NULL) { 372 return nullObjectReturn("layoutBounds == null"); 373 } 374 375 jint scaledBounds[4]; 376 if (willScale) { 377 for (int i=0; i<4; i++) { 378 scaledBounds[i] = (jint)((((jint*)peeker.fLayoutBounds)[i]*scale) + .5f); 379 } 380 } else { 381 memcpy(scaledBounds, (jint*)peeker.fLayoutBounds, sizeof(scaledBounds)); 382 } 383 env->SetIntArrayRegion(layoutBounds, 0, 4, scaledBounds); 384 if (javaBitmap != NULL) { 385 env->SetObjectField(javaBitmap, gBitmap_layoutBoundsFieldID, layoutBounds); 386 } 387 } 388 389 if (willScale) { 390 // This is weird so let me explain: we could use the scale parameter 391 // directly, but for historical reasons this is how the corresponding 392 // Dalvik code has always behaved. We simply recreate the behavior here. 393 // The result is slightly different from simply using scale because of 394 // the 0.5f rounding bias applied when computing the target image size 395 const float sx = scaledWidth / float(decodingBitmap.width()); 396 const float sy = scaledHeight / float(decodingBitmap.height()); 397 398 // TODO: avoid copying when scaled size equals decodingBitmap size 399 SkBitmap::Config config = configForScaledOutput(decodingBitmap.config()); 400 outputBitmap->setConfig(config, scaledWidth, scaledHeight); 401 outputBitmap->setIsOpaque(decodingBitmap.isOpaque()); 402 if (!outputBitmap->allocPixels(outputAllocator, NULL)) { 403 return nullObjectReturn("allocation failed for scaled bitmap"); 404 } 405 outputBitmap->eraseColor(0); 406 407 SkPaint paint; 408 paint.setFilterBitmap(true); 409 410 SkCanvas canvas(*outputBitmap); 411 canvas.scale(sx, sy); 412 canvas.drawBitmap(decodingBitmap, 0.0f, 0.0f, &paint); 413 } else { 414 outputBitmap->swap(decodingBitmap); 415 } 416 417 if (padding) { 418 if (peeker.fPatch != NULL) { 419 GraphicsJNI::set_jrect(env, padding, 420 peeker.fPatch->paddingLeft, peeker.fPatch->paddingTop, 421 peeker.fPatch->paddingRight, peeker.fPatch->paddingBottom); 422 } else { 423 GraphicsJNI::set_jrect(env, padding, -1, -1, -1, -1); 424 } 425 } 426 427 SkPixelRef* pr; 428 if (isPurgeable) { 429 pr = installPixelRef(outputBitmap, stream, sampleSize, doDither); 430 } else { 431 // if we get here, we're in kDecodePixels_Mode and will therefore 432 // already have a pixelref installed. 433 pr = outputBitmap->pixelRef(); 434 } 435 if (pr == NULL) { 436 return nullObjectReturn("Got null SkPixelRef"); 437 } 438 439 if (!isMutable && javaBitmap == NULL) { 440 // promise we will never change our pixels (great for sharing and pictures) 441 pr->setImmutable(); 442 } 443 444 // detach bitmap from its autodeleter, since we want to own it now 445 adb.detach(); 446 447 if (javaBitmap != NULL) { 448 bool isPremultiplied = !requireUnpremultiplied; 449 GraphicsJNI::reinitBitmap(env, javaBitmap, outputBitmap, isPremultiplied); 450 outputBitmap->notifyPixelsChanged(); 451 // If a java bitmap was passed in for reuse, pass it back 452 return javaBitmap; 453 } 454 455 int bitmapCreateFlags = 0x0; 456 if (isMutable) bitmapCreateFlags |= GraphicsJNI::kBitmapCreateFlag_Mutable; 457 if (!requireUnpremultiplied) bitmapCreateFlags |= GraphicsJNI::kBitmapCreateFlag_Premultiplied; 458 459 // now create the java bitmap 460 return GraphicsJNI::createBitmap(env, outputBitmap, javaAllocator.getStorageObj(), 461 bitmapCreateFlags, ninePatchChunk, layoutBounds, -1); 462} 463 464static jobject nativeDecodeStream(JNIEnv* env, jobject clazz, jobject is, jbyteArray storage, 465 jobject padding, jobject options) { 466 467 jobject bitmap = NULL; 468 SkAutoTUnref<SkStreamRewindable> stream(GetRewindableStream(env, is, storage)); 469 470 if (stream.get()) { 471 // for now we don't allow purgeable with java inputstreams 472 // FIXME: GetRewindableStream may have made a copy, in which case 473 // purgeable should be allowed. 474 bitmap = doDecode(env, stream, padding, options, false, false); 475 } 476 return bitmap; 477} 478 479static jobject nativeDecodeFileDescriptor(JNIEnv* env, jobject clazz, jobject fileDescriptor, 480 jobject padding, jobject bitmapFactoryOptions) { 481 482 NPE_CHECK_RETURN_ZERO(env, fileDescriptor); 483 484 jint descriptor = jniGetFDFromFileDescriptor(env, fileDescriptor); 485 486 struct stat fdStat; 487 if (fstat(descriptor, &fdStat) == -1) { 488 doThrowIOE(env, "broken file descriptor"); 489 return nullObjectReturn("fstat return -1"); 490 } 491 492 bool isPurgeable = optionsPurgeable(env, bitmapFactoryOptions); 493 bool isShareable = optionsShareable(env, bitmapFactoryOptions); 494 bool weOwnTheFD = false; 495 if (isPurgeable && isShareable) { 496 int newFD = ::dup(descriptor); 497 if (-1 != newFD) { 498 weOwnTheFD = true; 499 descriptor = newFD; 500 } 501 } 502 503 SkAutoTUnref<SkData> data(SkData::NewFromFD(descriptor)); 504 SkAutoTUnref<SkMemoryStream> stream(new SkMemoryStream(data)); 505 506 /* Allow purgeable iff we own the FD, i.e., in the puregeable and 507 shareable case. 508 */ 509 return doDecode(env, stream, padding, bitmapFactoryOptions, weOwnTheFD); 510} 511 512static jobject nativeDecodeAsset(JNIEnv* env, jobject clazz, jint native_asset, 513 jobject padding, jobject options) { 514 515 SkStreamRewindable* stream; 516 Asset* asset = reinterpret_cast<Asset*>(native_asset); 517 bool forcePurgeable = optionsPurgeable(env, options); 518 if (forcePurgeable) { 519 // if we could "ref/reopen" the asset, we may not need to copy it here 520 // and we could assume optionsShareable, since assets are always RO 521 stream = CopyAssetToStream(asset); 522 if (stream == NULL) { 523 return NULL; 524 } 525 } else { 526 // since we know we'll be done with the asset when we return, we can 527 // just use a simple wrapper 528 stream = new AssetStreamAdaptor(asset); 529 } 530 SkAutoUnref aur(stream); 531 return doDecode(env, stream, padding, options, forcePurgeable, forcePurgeable); 532} 533 534static jobject nativeDecodeByteArray(JNIEnv* env, jobject, jbyteArray byteArray, 535 int offset, int length, jobject options) { 536 537 /* If optionsShareable() we could decide to just wrap the java array and 538 share it, but that means adding a globalref to the java array object 539 and managing its lifetime. For now we just always copy the array's data 540 if optionsPurgeable(), unless we're just decoding bounds. 541 */ 542 bool purgeable = optionsPurgeable(env, options) && !optionsJustBounds(env, options); 543 AutoJavaByteArray ar(env, byteArray); 544 SkMemoryStream* stream = new SkMemoryStream(ar.ptr() + offset, length, purgeable); 545 SkAutoUnref aur(stream); 546 return doDecode(env, stream, NULL, options, purgeable); 547} 548 549static void nativeRequestCancel(JNIEnv*, jobject joptions) { 550 (void)AutoDecoderCancel::RequestCancel(joptions); 551} 552 553static jboolean nativeIsSeekable(JNIEnv* env, jobject, jobject fileDescriptor) { 554 jint descriptor = jniGetFDFromFileDescriptor(env, fileDescriptor); 555 return ::lseek64(descriptor, 0, SEEK_CUR) != -1 ? JNI_TRUE : JNI_FALSE; 556} 557 558/////////////////////////////////////////////////////////////////////////////// 559 560static JNINativeMethod gMethods[] = { 561 { "nativeDecodeStream", 562 "(Ljava/io/InputStream;[BLandroid/graphics/Rect;Landroid/graphics/BitmapFactory$Options;)Landroid/graphics/Bitmap;", 563 (void*)nativeDecodeStream 564 }, 565 566 { "nativeDecodeFileDescriptor", 567 "(Ljava/io/FileDescriptor;Landroid/graphics/Rect;Landroid/graphics/BitmapFactory$Options;)Landroid/graphics/Bitmap;", 568 (void*)nativeDecodeFileDescriptor 569 }, 570 571 { "nativeDecodeAsset", 572 "(ILandroid/graphics/Rect;Landroid/graphics/BitmapFactory$Options;)Landroid/graphics/Bitmap;", 573 (void*)nativeDecodeAsset 574 }, 575 576 { "nativeDecodeByteArray", 577 "([BIILandroid/graphics/BitmapFactory$Options;)Landroid/graphics/Bitmap;", 578 (void*)nativeDecodeByteArray 579 }, 580 581 { "nativeIsSeekable", 582 "(Ljava/io/FileDescriptor;)Z", 583 (void*)nativeIsSeekable 584 }, 585}; 586 587static JNINativeMethod gOptionsMethods[] = { 588 { "requestCancel", "()V", (void*)nativeRequestCancel } 589}; 590 591static jfieldID getFieldIDCheck(JNIEnv* env, jclass clazz, 592 const char fieldname[], const char type[]) { 593 jfieldID id = env->GetFieldID(clazz, fieldname, type); 594 SkASSERT(id); 595 return id; 596} 597 598int register_android_graphics_BitmapFactory(JNIEnv* env) { 599 jclass options_class = env->FindClass("android/graphics/BitmapFactory$Options"); 600 SkASSERT(options_class); 601 gOptions_bitmapFieldID = getFieldIDCheck(env, options_class, "inBitmap", 602 "Landroid/graphics/Bitmap;"); 603 gOptions_justBoundsFieldID = getFieldIDCheck(env, options_class, "inJustDecodeBounds", "Z"); 604 gOptions_sampleSizeFieldID = getFieldIDCheck(env, options_class, "inSampleSize", "I"); 605 gOptions_configFieldID = getFieldIDCheck(env, options_class, "inPreferredConfig", 606 "Landroid/graphics/Bitmap$Config;"); 607 gOptions_premultipliedFieldID = getFieldIDCheck(env, options_class, "inPremultiplied", "Z"); 608 gOptions_mutableFieldID = getFieldIDCheck(env, options_class, "inMutable", "Z"); 609 gOptions_ditherFieldID = getFieldIDCheck(env, options_class, "inDither", "Z"); 610 gOptions_purgeableFieldID = getFieldIDCheck(env, options_class, "inPurgeable", "Z"); 611 gOptions_shareableFieldID = getFieldIDCheck(env, options_class, "inInputShareable", "Z"); 612 gOptions_preferQualityOverSpeedFieldID = getFieldIDCheck(env, options_class, 613 "inPreferQualityOverSpeed", "Z"); 614 gOptions_scaledFieldID = getFieldIDCheck(env, options_class, "inScaled", "Z"); 615 gOptions_densityFieldID = getFieldIDCheck(env, options_class, "inDensity", "I"); 616 gOptions_screenDensityFieldID = getFieldIDCheck(env, options_class, "inScreenDensity", "I"); 617 gOptions_targetDensityFieldID = getFieldIDCheck(env, options_class, "inTargetDensity", "I"); 618 gOptions_widthFieldID = getFieldIDCheck(env, options_class, "outWidth", "I"); 619 gOptions_heightFieldID = getFieldIDCheck(env, options_class, "outHeight", "I"); 620 gOptions_mimeFieldID = getFieldIDCheck(env, options_class, "outMimeType", "Ljava/lang/String;"); 621 gOptions_mCancelID = getFieldIDCheck(env, options_class, "mCancel", "Z"); 622 623 jclass bitmap_class = env->FindClass("android/graphics/Bitmap"); 624 SkASSERT(bitmap_class); 625 gBitmap_nativeBitmapFieldID = getFieldIDCheck(env, bitmap_class, "mNativeBitmap", "I"); 626 gBitmap_layoutBoundsFieldID = getFieldIDCheck(env, bitmap_class, "mLayoutBounds", "[I"); 627 int ret = AndroidRuntime::registerNativeMethods(env, 628 "android/graphics/BitmapFactory$Options", 629 gOptionsMethods, 630 SK_ARRAY_COUNT(gOptionsMethods)); 631 if (ret) { 632 return ret; 633 } 634 return android::AndroidRuntime::registerNativeMethods(env, "android/graphics/BitmapFactory", 635 gMethods, SK_ARRAY_COUNT(gMethods)); 636} 637