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