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