Graphics.cpp revision 9505a6552764461c22ce48f1ac13d025d23e1579
1#define LOG_TAG "GraphicsJNI" 2 3#include <unistd.h> 4#include <sys/mman.h> 5 6#include "jni.h" 7#include "JNIHelp.h" 8#include "GraphicsJNI.h" 9 10#include "SkCanvas.h" 11#include "SkDevice.h" 12#include "SkMath.h" 13#include "SkRegion.h" 14#include <android_runtime/AndroidRuntime.h> 15#include <cutils/ashmem.h> 16#include <hwui/Canvas.h> 17 18#include <Caches.h> 19#include <TextureCache.h> 20 21void doThrowNPE(JNIEnv* env) { 22 jniThrowNullPointerException(env, NULL); 23} 24 25void doThrowAIOOBE(JNIEnv* env) { 26 jniThrowException(env, "java/lang/ArrayIndexOutOfBoundsException", NULL); 27} 28 29void doThrowRE(JNIEnv* env, const char* msg) { 30 jniThrowRuntimeException(env, msg); 31} 32 33void doThrowIAE(JNIEnv* env, const char* msg) { 34 jniThrowException(env, "java/lang/IllegalArgumentException", msg); 35} 36 37void doThrowISE(JNIEnv* env, const char* msg) { 38 jniThrowException(env, "java/lang/IllegalStateException", msg); 39} 40 41void doThrowOOME(JNIEnv* env, const char* msg) { 42 jniThrowException(env, "java/lang/OutOfMemoryError", msg); 43} 44 45void doThrowIOE(JNIEnv* env, const char* msg) { 46 jniThrowException(env, "java/io/IOException", msg); 47} 48 49bool GraphicsJNI::hasException(JNIEnv *env) { 50 if (env->ExceptionCheck() != 0) { 51 ALOGE("*** Uncaught exception returned from Java call!\n"); 52 env->ExceptionDescribe(); 53 return true; 54 } 55 return false; 56} 57 58/////////////////////////////////////////////////////////////////////////////// 59 60AutoJavaFloatArray::AutoJavaFloatArray(JNIEnv* env, jfloatArray array, 61 int minLength, JNIAccess access) 62: fEnv(env), fArray(array), fPtr(NULL), fLen(0) { 63 SkASSERT(env); 64 if (array) { 65 fLen = env->GetArrayLength(array); 66 if (fLen < minLength) { 67 sk_throw(); 68 } 69 fPtr = env->GetFloatArrayElements(array, NULL); 70 } 71 fReleaseMode = (access == kRO_JNIAccess) ? JNI_ABORT : 0; 72} 73 74AutoJavaFloatArray::~AutoJavaFloatArray() { 75 if (fPtr) { 76 fEnv->ReleaseFloatArrayElements(fArray, fPtr, fReleaseMode); 77 } 78} 79 80AutoJavaIntArray::AutoJavaIntArray(JNIEnv* env, jintArray array, 81 int minLength) 82: fEnv(env), fArray(array), fPtr(NULL), fLen(0) { 83 SkASSERT(env); 84 if (array) { 85 fLen = env->GetArrayLength(array); 86 if (fLen < minLength) { 87 sk_throw(); 88 } 89 fPtr = env->GetIntArrayElements(array, NULL); 90 } 91} 92 93AutoJavaIntArray::~AutoJavaIntArray() { 94 if (fPtr) { 95 fEnv->ReleaseIntArrayElements(fArray, fPtr, 0); 96 } 97} 98 99AutoJavaShortArray::AutoJavaShortArray(JNIEnv* env, jshortArray array, 100 int minLength, JNIAccess access) 101: fEnv(env), fArray(array), fPtr(NULL), fLen(0) { 102 SkASSERT(env); 103 if (array) { 104 fLen = env->GetArrayLength(array); 105 if (fLen < minLength) { 106 sk_throw(); 107 } 108 fPtr = env->GetShortArrayElements(array, NULL); 109 } 110 fReleaseMode = (access == kRO_JNIAccess) ? JNI_ABORT : 0; 111} 112 113AutoJavaShortArray::~AutoJavaShortArray() { 114 if (fPtr) { 115 fEnv->ReleaseShortArrayElements(fArray, fPtr, fReleaseMode); 116 } 117} 118 119AutoJavaByteArray::AutoJavaByteArray(JNIEnv* env, jbyteArray array, 120 int minLength) 121: fEnv(env), fArray(array), fPtr(NULL), fLen(0) { 122 SkASSERT(env); 123 if (array) { 124 fLen = env->GetArrayLength(array); 125 if (fLen < minLength) { 126 sk_throw(); 127 } 128 fPtr = env->GetByteArrayElements(array, NULL); 129 } 130} 131 132AutoJavaByteArray::~AutoJavaByteArray() { 133 if (fPtr) { 134 fEnv->ReleaseByteArrayElements(fArray, fPtr, 0); 135 } 136} 137 138/////////////////////////////////////////////////////////////////////////////// 139 140static jclass gRect_class; 141static jfieldID gRect_leftFieldID; 142static jfieldID gRect_topFieldID; 143static jfieldID gRect_rightFieldID; 144static jfieldID gRect_bottomFieldID; 145 146static jclass gRectF_class; 147static jfieldID gRectF_leftFieldID; 148static jfieldID gRectF_topFieldID; 149static jfieldID gRectF_rightFieldID; 150static jfieldID gRectF_bottomFieldID; 151 152static jclass gPoint_class; 153static jfieldID gPoint_xFieldID; 154static jfieldID gPoint_yFieldID; 155 156static jclass gPointF_class; 157static jfieldID gPointF_xFieldID; 158static jfieldID gPointF_yFieldID; 159 160static jclass gBitmapConfig_class; 161static jfieldID gBitmapConfig_nativeInstanceID; 162 163static jclass gBitmapRegionDecoder_class; 164static jmethodID gBitmapRegionDecoder_constructorMethodID; 165 166static jclass gCanvas_class; 167static jfieldID gCanvas_nativeInstanceID; 168 169static jclass gPicture_class; 170static jfieldID gPicture_nativeInstanceID; 171 172static jclass gRegion_class; 173static jfieldID gRegion_nativeInstanceID; 174static jmethodID gRegion_constructorMethodID; 175 176static jclass gByte_class; 177static jobject gVMRuntime; 178static jclass gVMRuntime_class; 179static jmethodID gVMRuntime_newNonMovableArray; 180static jmethodID gVMRuntime_addressOf; 181 182/////////////////////////////////////////////////////////////////////////////// 183 184void GraphicsJNI::get_jrect(JNIEnv* env, jobject obj, int* L, int* T, int* R, int* B) 185{ 186 SkASSERT(env->IsInstanceOf(obj, gRect_class)); 187 188 *L = env->GetIntField(obj, gRect_leftFieldID); 189 *T = env->GetIntField(obj, gRect_topFieldID); 190 *R = env->GetIntField(obj, gRect_rightFieldID); 191 *B = env->GetIntField(obj, gRect_bottomFieldID); 192} 193 194void GraphicsJNI::set_jrect(JNIEnv* env, jobject obj, int L, int T, int R, int B) 195{ 196 SkASSERT(env->IsInstanceOf(obj, gRect_class)); 197 198 env->SetIntField(obj, gRect_leftFieldID, L); 199 env->SetIntField(obj, gRect_topFieldID, T); 200 env->SetIntField(obj, gRect_rightFieldID, R); 201 env->SetIntField(obj, gRect_bottomFieldID, B); 202} 203 204SkIRect* GraphicsJNI::jrect_to_irect(JNIEnv* env, jobject obj, SkIRect* ir) 205{ 206 SkASSERT(env->IsInstanceOf(obj, gRect_class)); 207 208 ir->set(env->GetIntField(obj, gRect_leftFieldID), 209 env->GetIntField(obj, gRect_topFieldID), 210 env->GetIntField(obj, gRect_rightFieldID), 211 env->GetIntField(obj, gRect_bottomFieldID)); 212 return ir; 213} 214 215void GraphicsJNI::irect_to_jrect(const SkIRect& ir, JNIEnv* env, jobject obj) 216{ 217 SkASSERT(env->IsInstanceOf(obj, gRect_class)); 218 219 env->SetIntField(obj, gRect_leftFieldID, ir.fLeft); 220 env->SetIntField(obj, gRect_topFieldID, ir.fTop); 221 env->SetIntField(obj, gRect_rightFieldID, ir.fRight); 222 env->SetIntField(obj, gRect_bottomFieldID, ir.fBottom); 223} 224 225SkRect* GraphicsJNI::jrectf_to_rect(JNIEnv* env, jobject obj, SkRect* r) 226{ 227 SkASSERT(env->IsInstanceOf(obj, gRectF_class)); 228 229 r->set(env->GetFloatField(obj, gRectF_leftFieldID), 230 env->GetFloatField(obj, gRectF_topFieldID), 231 env->GetFloatField(obj, gRectF_rightFieldID), 232 env->GetFloatField(obj, gRectF_bottomFieldID)); 233 return r; 234} 235 236SkRect* GraphicsJNI::jrect_to_rect(JNIEnv* env, jobject obj, SkRect* r) 237{ 238 SkASSERT(env->IsInstanceOf(obj, gRect_class)); 239 240 r->set(SkIntToScalar(env->GetIntField(obj, gRect_leftFieldID)), 241 SkIntToScalar(env->GetIntField(obj, gRect_topFieldID)), 242 SkIntToScalar(env->GetIntField(obj, gRect_rightFieldID)), 243 SkIntToScalar(env->GetIntField(obj, gRect_bottomFieldID))); 244 return r; 245} 246 247void GraphicsJNI::rect_to_jrectf(const SkRect& r, JNIEnv* env, jobject obj) 248{ 249 SkASSERT(env->IsInstanceOf(obj, gRectF_class)); 250 251 env->SetFloatField(obj, gRectF_leftFieldID, SkScalarToFloat(r.fLeft)); 252 env->SetFloatField(obj, gRectF_topFieldID, SkScalarToFloat(r.fTop)); 253 env->SetFloatField(obj, gRectF_rightFieldID, SkScalarToFloat(r.fRight)); 254 env->SetFloatField(obj, gRectF_bottomFieldID, SkScalarToFloat(r.fBottom)); 255} 256 257SkIPoint* GraphicsJNI::jpoint_to_ipoint(JNIEnv* env, jobject obj, SkIPoint* point) 258{ 259 SkASSERT(env->IsInstanceOf(obj, gPoint_class)); 260 261 point->set(env->GetIntField(obj, gPoint_xFieldID), 262 env->GetIntField(obj, gPoint_yFieldID)); 263 return point; 264} 265 266void GraphicsJNI::ipoint_to_jpoint(const SkIPoint& ir, JNIEnv* env, jobject obj) 267{ 268 SkASSERT(env->IsInstanceOf(obj, gPoint_class)); 269 270 env->SetIntField(obj, gPoint_xFieldID, ir.fX); 271 env->SetIntField(obj, gPoint_yFieldID, ir.fY); 272} 273 274SkPoint* GraphicsJNI::jpointf_to_point(JNIEnv* env, jobject obj, SkPoint* point) 275{ 276 SkASSERT(env->IsInstanceOf(obj, gPointF_class)); 277 278 point->set(env->GetIntField(obj, gPointF_xFieldID), 279 env->GetIntField(obj, gPointF_yFieldID)); 280 return point; 281} 282 283void GraphicsJNI::point_to_jpointf(const SkPoint& r, JNIEnv* env, jobject obj) 284{ 285 SkASSERT(env->IsInstanceOf(obj, gPointF_class)); 286 287 env->SetFloatField(obj, gPointF_xFieldID, SkScalarToFloat(r.fX)); 288 env->SetFloatField(obj, gPointF_yFieldID, SkScalarToFloat(r.fY)); 289} 290 291// This enum must keep these int values, to match the int values 292// in the java Bitmap.Config enum. 293enum LegacyBitmapConfig { 294 kNo_LegacyBitmapConfig = 0, 295 kA8_LegacyBitmapConfig = 1, 296 kIndex8_LegacyBitmapConfig = 2, 297 kRGB_565_LegacyBitmapConfig = 3, 298 kARGB_4444_LegacyBitmapConfig = 4, 299 kARGB_8888_LegacyBitmapConfig = 5, 300 kRGBA_16F_LegacyBitmapConfig = 6, 301 kHardware_LegacyBitmapConfig = 7, 302 303 kLastEnum_LegacyBitmapConfig = kHardware_LegacyBitmapConfig 304}; 305 306jint GraphicsJNI::colorTypeToLegacyBitmapConfig(SkColorType colorType) { 307 switch (colorType) { 308 case kRGBA_F16_SkColorType: 309 return kRGBA_16F_LegacyBitmapConfig; 310 case kN32_SkColorType: 311 return kARGB_8888_LegacyBitmapConfig; 312 case kARGB_4444_SkColorType: 313 return kARGB_4444_LegacyBitmapConfig; 314 case kRGB_565_SkColorType: 315 return kRGB_565_LegacyBitmapConfig; 316 case kIndex_8_SkColorType: 317 return kIndex8_LegacyBitmapConfig; 318 case kAlpha_8_SkColorType: 319 return kA8_LegacyBitmapConfig; 320 case kUnknown_SkColorType: 321 default: 322 break; 323 } 324 return kNo_LegacyBitmapConfig; 325} 326 327SkColorType GraphicsJNI::legacyBitmapConfigToColorType(jint legacyConfig) { 328 const uint8_t gConfig2ColorType[] = { 329 kUnknown_SkColorType, 330 kAlpha_8_SkColorType, 331 kIndex_8_SkColorType, 332 kRGB_565_SkColorType, 333 kARGB_4444_SkColorType, 334 kN32_SkColorType, 335 kRGBA_F16_SkColorType, 336 kN32_SkColorType 337 }; 338 339 if (legacyConfig < 0 || legacyConfig > kLastEnum_LegacyBitmapConfig) { 340 legacyConfig = kNo_LegacyBitmapConfig; 341 } 342 return static_cast<SkColorType>(gConfig2ColorType[legacyConfig]); 343} 344 345void GraphicsJNI::getSkBitmap(JNIEnv* env, jobject bitmap, SkBitmap* outBitmap) { 346 android::bitmap::toBitmap(env, bitmap).getSkBitmap(outBitmap); 347} 348 349SkPixelRef* GraphicsJNI::refSkPixelRef(JNIEnv* env, jobject jbitmap) { 350 android::Bitmap& bitmap = android::bitmap::toBitmap(env, jbitmap); 351 bitmap.ref(); 352 return &bitmap; 353} 354SkColorType GraphicsJNI::getNativeBitmapColorType(JNIEnv* env, jobject jconfig) { 355 SkASSERT(env); 356 if (NULL == jconfig) { 357 return kUnknown_SkColorType; 358 } 359 SkASSERT(env->IsInstanceOf(jconfig, gBitmapConfig_class)); 360 int c = env->GetIntField(jconfig, gBitmapConfig_nativeInstanceID); 361 return legacyBitmapConfigToColorType(c); 362} 363 364bool GraphicsJNI::isHardwareConfig(JNIEnv* env, jobject jconfig) { 365 SkASSERT(env); 366 if (NULL == jconfig) { 367 return false; 368 } 369 int c = env->GetIntField(jconfig, gBitmapConfig_nativeInstanceID); 370 return c == kHardware_LegacyBitmapConfig; 371} 372 373jint GraphicsJNI::hardwareLegacyBitmapConfig() { 374 return kHardware_LegacyBitmapConfig; 375} 376 377android::Canvas* GraphicsJNI::getNativeCanvas(JNIEnv* env, jobject canvas) { 378 SkASSERT(env); 379 SkASSERT(canvas); 380 SkASSERT(env->IsInstanceOf(canvas, gCanvas_class)); 381 jlong canvasHandle = env->GetLongField(canvas, gCanvas_nativeInstanceID); 382 if (!canvasHandle) { 383 return NULL; 384 } 385 return reinterpret_cast<android::Canvas*>(canvasHandle); 386} 387 388SkRegion* GraphicsJNI::getNativeRegion(JNIEnv* env, jobject region) 389{ 390 SkASSERT(env); 391 SkASSERT(region); 392 SkASSERT(env->IsInstanceOf(region, gRegion_class)); 393 jlong regionHandle = env->GetLongField(region, gRegion_nativeInstanceID); 394 SkRegion* r = reinterpret_cast<SkRegion*>(regionHandle); 395 SkASSERT(r); 396 return r; 397} 398 399/////////////////////////////////////////////////////////////////////////////////////////// 400 401jobject GraphicsJNI::createBitmapRegionDecoder(JNIEnv* env, SkBitmapRegionDecoder* bitmap) 402{ 403 SkASSERT(bitmap != NULL); 404 405 jobject obj = env->NewObject(gBitmapRegionDecoder_class, 406 gBitmapRegionDecoder_constructorMethodID, 407 reinterpret_cast<jlong>(bitmap)); 408 hasException(env); // For the side effect of logging. 409 return obj; 410} 411 412jobject GraphicsJNI::createRegion(JNIEnv* env, SkRegion* region) 413{ 414 SkASSERT(region != NULL); 415 jobject obj = env->NewObject(gRegion_class, gRegion_constructorMethodID, 416 reinterpret_cast<jlong>(region), 0); 417 hasException(env); // For the side effect of logging. 418 return obj; 419} 420 421/////////////////////////////////////////////////////////////////////////////// 422 423android::Bitmap* GraphicsJNI::mapAshmemBitmap(JNIEnv* env, SkBitmap* bitmap, 424 SkColorTable* ctable, int fd, void* addr, size_t size, bool readOnly) { 425 const SkImageInfo& info = bitmap->info(); 426 if (info.colorType() == kUnknown_SkColorType) { 427 doThrowIAE(env, "unknown bitmap configuration"); 428 return nullptr; 429 } 430 431 if (!addr) { 432 // Map existing ashmem region if not already mapped. 433 int flags = readOnly ? (PROT_READ) : (PROT_READ | PROT_WRITE); 434 size = ashmem_get_size_region(fd); 435 addr = mmap(NULL, size, flags, MAP_SHARED, fd, 0); 436 if (addr == MAP_FAILED) { 437 return nullptr; 438 } 439 } 440 441 // we must respect the rowBytes value already set on the bitmap instead of 442 // attempting to compute our own. 443 const size_t rowBytes = bitmap->rowBytes(); 444 445 auto wrapper = new android::Bitmap(addr, fd, size, info, rowBytes, ctable); 446 wrapper->getSkBitmap(bitmap); 447 if (readOnly) { 448 bitmap->pixelRef()->setImmutable(); 449 } 450 // since we're already allocated, we lockPixels right away 451 // HeapAllocator behaves this way too 452 bitmap->lockPixels(); 453 454 return wrapper; 455} 456 457sk_sp<SkColorSpace> GraphicsJNI::defaultColorSpace() { 458#ifdef ANDROID_ENABLE_LINEAR_BLENDING 459 return SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named); 460#else 461 return nullptr; 462#endif 463} 464 465sk_sp<SkColorSpace> GraphicsJNI::linearColorSpace() { 466 return SkColorSpace::MakeNamed(SkColorSpace::kSRGBLinear_Named); 467} 468 469sk_sp<SkColorSpace> GraphicsJNI::colorSpaceForType(SkColorType type) { 470 switch (type) { 471 case kRGBA_F16_SkColorType: 472 return linearColorSpace(); 473 default: 474 return defaultColorSpace(); 475 } 476} 477 478/////////////////////////////////////////////////////////////////////////////// 479bool HeapAllocator::allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable) { 480 mStorage = android::Bitmap::allocateHeapBitmap(bitmap, ctable); 481 return !!mStorage; 482} 483 484//////////////////////////////////////////////////////////////////////////////// 485 486RecyclingClippingPixelAllocator::RecyclingClippingPixelAllocator( 487 android::Bitmap* recycledBitmap, size_t recycledBytes) 488 : mRecycledBitmap(recycledBitmap) 489 , mRecycledBytes(recycledBytes) 490 , mSkiaBitmap(nullptr) 491 , mNeedsCopy(false) 492{} 493 494RecyclingClippingPixelAllocator::~RecyclingClippingPixelAllocator() {} 495 496bool RecyclingClippingPixelAllocator::allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable) { 497 // Ensure that the caller did not pass in a NULL bitmap to the constructor or this 498 // function. 499 LOG_ALWAYS_FATAL_IF(!mRecycledBitmap); 500 LOG_ALWAYS_FATAL_IF(!bitmap); 501 mSkiaBitmap = bitmap; 502 503 // This behaves differently than the RecyclingPixelAllocator. For backwards 504 // compatibility, the original color type of the recycled bitmap must be maintained. 505 if (mRecycledBitmap->info().colorType() != bitmap->colorType()) { 506 return false; 507 } 508 509 // The Skia bitmap specifies the width and height needed by the decoder. 510 // mRecycledBitmap specifies the width and height of the bitmap that we 511 // want to reuse. Neither can be changed. We will try to find a way 512 // to reuse the memory. 513 const int maxWidth = SkTMax(bitmap->width(), mRecycledBitmap->info().width()); 514 const int maxHeight = SkTMax(bitmap->height(), mRecycledBitmap->info().height()); 515 const SkImageInfo maxInfo = bitmap->info().makeWH(maxWidth, maxHeight); 516 const size_t rowBytes = maxInfo.minRowBytes(); 517 const size_t bytesNeeded = maxInfo.getSafeSize(rowBytes); 518 if (bytesNeeded <= mRecycledBytes) { 519 // Here we take advantage of reconfigure() to reset the rowBytes and ctable 520 // of mRecycledBitmap. It is very important that we pass in 521 // mRecycledBitmap->info() for the SkImageInfo. According to the 522 // specification for BitmapRegionDecoder, we are not allowed to change 523 // the SkImageInfo. 524 mRecycledBitmap->reconfigure(mRecycledBitmap->info(), rowBytes, ctable); 525 526 // Give the bitmap the same pixelRef as mRecycledBitmap. 527 // skbug.com/4538: We also need to make sure that the rowBytes on the pixel ref 528 // match the rowBytes on the bitmap. 529 bitmap->setInfo(bitmap->info(), rowBytes); 530 mRecycledBitmap->ref(); 531 bitmap->setPixelRef(mRecycledBitmap)->unref(); 532 533 // Make sure that the recycled bitmap has the correct alpha type. 534 mRecycledBitmap->setAlphaType(bitmap->alphaType()); 535 536 bitmap->notifyPixelsChanged(); 537 bitmap->lockPixels(); 538 mNeedsCopy = false; 539 540 // TODO: If the dimensions of the SkBitmap are smaller than those of 541 // mRecycledBitmap, should we zero the memory in mRecycledBitmap? 542 return true; 543 } 544 545 // In the event that mRecycledBitmap is not large enough, allocate new memory 546 // on the heap. 547 SkBitmap::HeapAllocator heapAllocator; 548 549 // We will need to copy from heap memory to mRecycledBitmap's memory after the 550 // decode is complete. 551 mNeedsCopy = true; 552 553 return heapAllocator.allocPixelRef(bitmap, ctable); 554} 555 556void RecyclingClippingPixelAllocator::copyIfNecessary() { 557 if (mNeedsCopy) { 558 mRecycledBitmap->ref(); 559 SkPixelRef* recycledPixels = mRecycledBitmap; 560 void* dst = recycledPixels->pixels(); 561 const size_t dstRowBytes = mRecycledBitmap->rowBytes(); 562 const size_t bytesToCopy = std::min(mRecycledBitmap->info().minRowBytes(), 563 mSkiaBitmap->info().minRowBytes()); 564 const int rowsToCopy = std::min(mRecycledBitmap->info().height(), 565 mSkiaBitmap->info().height()); 566 for (int y = 0; y < rowsToCopy; y++) { 567 memcpy(dst, mSkiaBitmap->getAddr(0, y), bytesToCopy); 568 dst = SkTAddOffset<void>(dst, dstRowBytes); 569 } 570 recycledPixels->notifyPixelsChanged(); 571 recycledPixels->unref(); 572 } 573 mRecycledBitmap = nullptr; 574 mSkiaBitmap = nullptr; 575} 576 577//////////////////////////////////////////////////////////////////////////////// 578 579AshmemPixelAllocator::AshmemPixelAllocator(JNIEnv *env) { 580 LOG_ALWAYS_FATAL_IF(env->GetJavaVM(&mJavaVM) != JNI_OK, 581 "env->GetJavaVM failed"); 582} 583 584bool AshmemPixelAllocator::allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable) { 585 mStorage = android::Bitmap::allocateAshmemBitmap(bitmap, ctable); 586 return !!mStorage; 587} 588 589//////////////////////////////////////////////////////////////////////////////// 590 591static jclass make_globalref(JNIEnv* env, const char classname[]) 592{ 593 jclass c = env->FindClass(classname); 594 SkASSERT(c); 595 return (jclass) env->NewGlobalRef(c); 596} 597 598static jfieldID getFieldIDCheck(JNIEnv* env, jclass clazz, 599 const char fieldname[], const char type[]) 600{ 601 jfieldID id = env->GetFieldID(clazz, fieldname, type); 602 SkASSERT(id); 603 return id; 604} 605 606int register_android_graphics_Graphics(JNIEnv* env) 607{ 608 jmethodID m; 609 jclass c; 610 611 gRect_class = make_globalref(env, "android/graphics/Rect"); 612 gRect_leftFieldID = getFieldIDCheck(env, gRect_class, "left", "I"); 613 gRect_topFieldID = getFieldIDCheck(env, gRect_class, "top", "I"); 614 gRect_rightFieldID = getFieldIDCheck(env, gRect_class, "right", "I"); 615 gRect_bottomFieldID = getFieldIDCheck(env, gRect_class, "bottom", "I"); 616 617 gRectF_class = make_globalref(env, "android/graphics/RectF"); 618 gRectF_leftFieldID = getFieldIDCheck(env, gRectF_class, "left", "F"); 619 gRectF_topFieldID = getFieldIDCheck(env, gRectF_class, "top", "F"); 620 gRectF_rightFieldID = getFieldIDCheck(env, gRectF_class, "right", "F"); 621 gRectF_bottomFieldID = getFieldIDCheck(env, gRectF_class, "bottom", "F"); 622 623 gPoint_class = make_globalref(env, "android/graphics/Point"); 624 gPoint_xFieldID = getFieldIDCheck(env, gPoint_class, "x", "I"); 625 gPoint_yFieldID = getFieldIDCheck(env, gPoint_class, "y", "I"); 626 627 gPointF_class = make_globalref(env, "android/graphics/PointF"); 628 gPointF_xFieldID = getFieldIDCheck(env, gPointF_class, "x", "F"); 629 gPointF_yFieldID = getFieldIDCheck(env, gPointF_class, "y", "F"); 630 631 gBitmapRegionDecoder_class = make_globalref(env, "android/graphics/BitmapRegionDecoder"); 632 gBitmapRegionDecoder_constructorMethodID = env->GetMethodID(gBitmapRegionDecoder_class, "<init>", "(J)V"); 633 634 gBitmapConfig_class = make_globalref(env, "android/graphics/Bitmap$Config"); 635 gBitmapConfig_nativeInstanceID = getFieldIDCheck(env, gBitmapConfig_class, 636 "nativeInt", "I"); 637 638 gCanvas_class = make_globalref(env, "android/graphics/Canvas"); 639 gCanvas_nativeInstanceID = getFieldIDCheck(env, gCanvas_class, "mNativeCanvasWrapper", "J"); 640 641 gPicture_class = make_globalref(env, "android/graphics/Picture"); 642 gPicture_nativeInstanceID = getFieldIDCheck(env, gPicture_class, "mNativePicture", "J"); 643 644 gRegion_class = make_globalref(env, "android/graphics/Region"); 645 gRegion_nativeInstanceID = getFieldIDCheck(env, gRegion_class, "mNativeRegion", "J"); 646 gRegion_constructorMethodID = env->GetMethodID(gRegion_class, "<init>", 647 "(JI)V"); 648 649 c = env->FindClass("java/lang/Byte"); 650 gByte_class = (jclass) env->NewGlobalRef( 651 env->GetStaticObjectField(c, env->GetStaticFieldID(c, "TYPE", "Ljava/lang/Class;"))); 652 653 gVMRuntime_class = make_globalref(env, "dalvik/system/VMRuntime"); 654 m = env->GetStaticMethodID(gVMRuntime_class, "getRuntime", "()Ldalvik/system/VMRuntime;"); 655 gVMRuntime = env->NewGlobalRef(env->CallStaticObjectMethod(gVMRuntime_class, m)); 656 gVMRuntime_newNonMovableArray = env->GetMethodID(gVMRuntime_class, "newNonMovableArray", 657 "(Ljava/lang/Class;I)Ljava/lang/Object;"); 658 gVMRuntime_addressOf = env->GetMethodID(gVMRuntime_class, "addressOf", "(Ljava/lang/Object;)J"); 659 660 return 0; 661} 662