1 #define LOG_TAG "GraphicsJNI" 2 3 #include "jni.h" 4 #include "JNIHelp.h" 5 #include "GraphicsJNI.h" 6 7 #include "SkCanvas.h" 8 #include "SkDevice.h" 9 #include "SkPicture.h" 10 #include "SkRegion.h" 11 #include <android_runtime/AndroidRuntime.h> 12 13 void doThrowNPE(JNIEnv* env) { 14 jniThrowNullPointerException(env, NULL); 15 } 16 17 void doThrowAIOOBE(JNIEnv* env) { 18 jniThrowException(env, "java/lang/ArrayIndexOutOfBoundsException", NULL); 19 } 20 21 void doThrowRE(JNIEnv* env, const char* msg) { 22 jniThrowRuntimeException(env, msg); 23 } 24 25 void doThrowIAE(JNIEnv* env, const char* msg) { 26 jniThrowException(env, "java/lang/IllegalArgumentException", msg); 27 } 28 29 void doThrowISE(JNIEnv* env, const char* msg) { 30 jniThrowException(env, "java/lang/IllegalStateException", msg); 31 } 32 33 void doThrowOOME(JNIEnv* env, const char* msg) { 34 jniThrowException(env, "java/lang/OutOfMemoryError", msg); 35 } 36 37 void doThrowIOE(JNIEnv* env, const char* msg) { 38 jniThrowException(env, "java/io/IOException", msg); 39 } 40 41 bool GraphicsJNI::hasException(JNIEnv *env) { 42 if (env->ExceptionCheck() != 0) { 43 ALOGE("*** Uncaught exception returned from Java call!\n"); 44 env->ExceptionDescribe(); 45 return true; 46 } 47 return false; 48 } 49 50 /////////////////////////////////////////////////////////////////////////////// 51 52 AutoJavaFloatArray::AutoJavaFloatArray(JNIEnv* env, jfloatArray array, 53 int minLength, JNIAccess access) 54 : fEnv(env), fArray(array), fPtr(NULL), fLen(0) { 55 SkASSERT(env); 56 if (array) { 57 fLen = env->GetArrayLength(array); 58 if (fLen < minLength) { 59 sk_throw(); 60 } 61 fPtr = env->GetFloatArrayElements(array, NULL); 62 } 63 fReleaseMode = (access == kRO_JNIAccess) ? JNI_ABORT : 0; 64 } 65 66 AutoJavaFloatArray::~AutoJavaFloatArray() { 67 if (fPtr) { 68 fEnv->ReleaseFloatArrayElements(fArray, fPtr, fReleaseMode); 69 } 70 } 71 72 AutoJavaIntArray::AutoJavaIntArray(JNIEnv* env, jintArray array, 73 int minLength) 74 : fEnv(env), fArray(array), fPtr(NULL), fLen(0) { 75 SkASSERT(env); 76 if (array) { 77 fLen = env->GetArrayLength(array); 78 if (fLen < minLength) { 79 sk_throw(); 80 } 81 fPtr = env->GetIntArrayElements(array, NULL); 82 } 83 } 84 85 AutoJavaIntArray::~AutoJavaIntArray() { 86 if (fPtr) { 87 fEnv->ReleaseIntArrayElements(fArray, fPtr, 0); 88 } 89 } 90 91 AutoJavaShortArray::AutoJavaShortArray(JNIEnv* env, jshortArray array, 92 int minLength, JNIAccess access) 93 : fEnv(env), fArray(array), fPtr(NULL), fLen(0) { 94 SkASSERT(env); 95 if (array) { 96 fLen = env->GetArrayLength(array); 97 if (fLen < minLength) { 98 sk_throw(); 99 } 100 fPtr = env->GetShortArrayElements(array, NULL); 101 } 102 fReleaseMode = (access == kRO_JNIAccess) ? JNI_ABORT : 0; 103 } 104 105 AutoJavaShortArray::~AutoJavaShortArray() { 106 if (fPtr) { 107 fEnv->ReleaseShortArrayElements(fArray, fPtr, fReleaseMode); 108 } 109 } 110 111 AutoJavaByteArray::AutoJavaByteArray(JNIEnv* env, jbyteArray array, 112 int minLength) 113 : fEnv(env), fArray(array), fPtr(NULL), fLen(0) { 114 SkASSERT(env); 115 if (array) { 116 fLen = env->GetArrayLength(array); 117 if (fLen < minLength) { 118 sk_throw(); 119 } 120 fPtr = env->GetByteArrayElements(array, NULL); 121 } 122 } 123 124 AutoJavaByteArray::~AutoJavaByteArray() { 125 if (fPtr) { 126 fEnv->ReleaseByteArrayElements(fArray, fPtr, 0); 127 } 128 } 129 130 /////////////////////////////////////////////////////////////////////////////// 131 132 static jclass gRect_class; 133 static jfieldID gRect_leftFieldID; 134 static jfieldID gRect_topFieldID; 135 static jfieldID gRect_rightFieldID; 136 static jfieldID gRect_bottomFieldID; 137 138 static jclass gRectF_class; 139 static jfieldID gRectF_leftFieldID; 140 static jfieldID gRectF_topFieldID; 141 static jfieldID gRectF_rightFieldID; 142 static jfieldID gRectF_bottomFieldID; 143 144 static jclass gPoint_class; 145 static jfieldID gPoint_xFieldID; 146 static jfieldID gPoint_yFieldID; 147 148 static jclass gPointF_class; 149 static jfieldID gPointF_xFieldID; 150 static jfieldID gPointF_yFieldID; 151 152 static jclass gBitmap_class; 153 static jfieldID gBitmap_nativeInstanceID; 154 static jmethodID gBitmap_constructorMethodID; 155 static jmethodID gBitmap_reinitMethodID; 156 static jmethodID gBitmap_getAllocationByteCountMethodID; 157 158 static jclass gBitmapConfig_class; 159 static jfieldID gBitmapConfig_nativeInstanceID; 160 161 static jclass gBitmapRegionDecoder_class; 162 static jmethodID gBitmapRegionDecoder_constructorMethodID; 163 164 static jclass gCanvas_class; 165 static jfieldID gCanvas_nativeInstanceID; 166 167 static jclass gPaint_class; 168 static jfieldID gPaint_nativeInstanceID; 169 170 static jclass gPicture_class; 171 static jfieldID gPicture_nativeInstanceID; 172 173 static jclass gRegion_class; 174 static jfieldID gRegion_nativeInstanceID; 175 static jmethodID gRegion_constructorMethodID; 176 177 /////////////////////////////////////////////////////////////////////////////// 178 179 void GraphicsJNI::get_jrect(JNIEnv* env, jobject obj, int* L, int* T, int* R, int* B) 180 { 181 SkASSERT(env->IsInstanceOf(obj, gRect_class)); 182 183 *L = env->GetIntField(obj, gRect_leftFieldID); 184 *T = env->GetIntField(obj, gRect_topFieldID); 185 *R = env->GetIntField(obj, gRect_rightFieldID); 186 *B = env->GetIntField(obj, gRect_bottomFieldID); 187 } 188 189 void GraphicsJNI::set_jrect(JNIEnv* env, jobject obj, int L, int T, int R, int B) 190 { 191 SkASSERT(env->IsInstanceOf(obj, gRect_class)); 192 193 env->SetIntField(obj, gRect_leftFieldID, L); 194 env->SetIntField(obj, gRect_topFieldID, T); 195 env->SetIntField(obj, gRect_rightFieldID, R); 196 env->SetIntField(obj, gRect_bottomFieldID, B); 197 } 198 199 SkIRect* GraphicsJNI::jrect_to_irect(JNIEnv* env, jobject obj, SkIRect* ir) 200 { 201 SkASSERT(env->IsInstanceOf(obj, gRect_class)); 202 203 ir->set(env->GetIntField(obj, gRect_leftFieldID), 204 env->GetIntField(obj, gRect_topFieldID), 205 env->GetIntField(obj, gRect_rightFieldID), 206 env->GetIntField(obj, gRect_bottomFieldID)); 207 return ir; 208 } 209 210 void GraphicsJNI::irect_to_jrect(const SkIRect& ir, JNIEnv* env, jobject obj) 211 { 212 SkASSERT(env->IsInstanceOf(obj, gRect_class)); 213 214 env->SetIntField(obj, gRect_leftFieldID, ir.fLeft); 215 env->SetIntField(obj, gRect_topFieldID, ir.fTop); 216 env->SetIntField(obj, gRect_rightFieldID, ir.fRight); 217 env->SetIntField(obj, gRect_bottomFieldID, ir.fBottom); 218 } 219 220 SkRect* GraphicsJNI::jrectf_to_rect(JNIEnv* env, jobject obj, SkRect* r) 221 { 222 SkASSERT(env->IsInstanceOf(obj, gRectF_class)); 223 224 r->set(SkFloatToScalar(env->GetFloatField(obj, gRectF_leftFieldID)), 225 SkFloatToScalar(env->GetFloatField(obj, gRectF_topFieldID)), 226 SkFloatToScalar(env->GetFloatField(obj, gRectF_rightFieldID)), 227 SkFloatToScalar(env->GetFloatField(obj, gRectF_bottomFieldID))); 228 return r; 229 } 230 231 SkRect* GraphicsJNI::jrect_to_rect(JNIEnv* env, jobject obj, SkRect* r) 232 { 233 SkASSERT(env->IsInstanceOf(obj, gRect_class)); 234 235 r->set(SkIntToScalar(env->GetIntField(obj, gRect_leftFieldID)), 236 SkIntToScalar(env->GetIntField(obj, gRect_topFieldID)), 237 SkIntToScalar(env->GetIntField(obj, gRect_rightFieldID)), 238 SkIntToScalar(env->GetIntField(obj, gRect_bottomFieldID))); 239 return r; 240 } 241 242 void GraphicsJNI::rect_to_jrectf(const SkRect& r, JNIEnv* env, jobject obj) 243 { 244 SkASSERT(env->IsInstanceOf(obj, gRectF_class)); 245 246 env->SetFloatField(obj, gRectF_leftFieldID, SkScalarToFloat(r.fLeft)); 247 env->SetFloatField(obj, gRectF_topFieldID, SkScalarToFloat(r.fTop)); 248 env->SetFloatField(obj, gRectF_rightFieldID, SkScalarToFloat(r.fRight)); 249 env->SetFloatField(obj, gRectF_bottomFieldID, SkScalarToFloat(r.fBottom)); 250 } 251 252 SkIPoint* GraphicsJNI::jpoint_to_ipoint(JNIEnv* env, jobject obj, SkIPoint* point) 253 { 254 SkASSERT(env->IsInstanceOf(obj, gPoint_class)); 255 256 point->set(env->GetIntField(obj, gPoint_xFieldID), 257 env->GetIntField(obj, gPoint_yFieldID)); 258 return point; 259 } 260 261 void GraphicsJNI::ipoint_to_jpoint(const SkIPoint& ir, JNIEnv* env, jobject obj) 262 { 263 SkASSERT(env->IsInstanceOf(obj, gPoint_class)); 264 265 env->SetIntField(obj, gPoint_xFieldID, ir.fX); 266 env->SetIntField(obj, gPoint_yFieldID, ir.fY); 267 } 268 269 SkPoint* GraphicsJNI::jpointf_to_point(JNIEnv* env, jobject obj, SkPoint* point) 270 { 271 SkASSERT(env->IsInstanceOf(obj, gPointF_class)); 272 273 point->set(SkFloatToScalar(env->GetIntField(obj, gPointF_xFieldID)), 274 SkFloatToScalar(env->GetIntField(obj, gPointF_yFieldID))); 275 return point; 276 } 277 278 void GraphicsJNI::point_to_jpointf(const SkPoint& r, JNIEnv* env, jobject obj) 279 { 280 SkASSERT(env->IsInstanceOf(obj, gPointF_class)); 281 282 env->SetFloatField(obj, gPointF_xFieldID, SkScalarToFloat(r.fX)); 283 env->SetFloatField(obj, gPointF_yFieldID, SkScalarToFloat(r.fY)); 284 } 285 286 SkBitmap* GraphicsJNI::getNativeBitmap(JNIEnv* env, jobject bitmap) { 287 SkASSERT(env); 288 SkASSERT(bitmap); 289 SkASSERT(env->IsInstanceOf(bitmap, gBitmap_class)); 290 SkBitmap* b = (SkBitmap*)env->GetIntField(bitmap, gBitmap_nativeInstanceID); 291 SkASSERT(b); 292 return b; 293 } 294 295 SkBitmap::Config GraphicsJNI::getNativeBitmapConfig(JNIEnv* env, 296 jobject jconfig) { 297 SkASSERT(env); 298 if (NULL == jconfig) { 299 return SkBitmap::kNo_Config; 300 } 301 SkASSERT(env->IsInstanceOf(jconfig, gBitmapConfig_class)); 302 int c = env->GetIntField(jconfig, gBitmapConfig_nativeInstanceID); 303 if (c < 0 || c >= SkBitmap::kConfigCount) { 304 c = SkBitmap::kNo_Config; 305 } 306 return static_cast<SkBitmap::Config>(c); 307 } 308 309 SkCanvas* GraphicsJNI::getNativeCanvas(JNIEnv* env, jobject canvas) { 310 SkASSERT(env); 311 SkASSERT(canvas); 312 SkASSERT(env->IsInstanceOf(canvas, gCanvas_class)); 313 SkCanvas* c = (SkCanvas*)env->GetIntField(canvas, gCanvas_nativeInstanceID); 314 SkASSERT(c); 315 return c; 316 } 317 318 SkPaint* GraphicsJNI::getNativePaint(JNIEnv* env, jobject paint) { 319 SkASSERT(env); 320 SkASSERT(paint); 321 SkASSERT(env->IsInstanceOf(paint, gPaint_class)); 322 SkPaint* p = (SkPaint*)env->GetIntField(paint, gPaint_nativeInstanceID); 323 SkASSERT(p); 324 return p; 325 } 326 327 SkPicture* GraphicsJNI::getNativePicture(JNIEnv* env, jobject picture) 328 { 329 SkASSERT(env); 330 SkASSERT(picture); 331 SkASSERT(env->IsInstanceOf(picture, gPicture_class)); 332 SkPicture* p = (SkPicture*)env->GetIntField(picture, gPicture_nativeInstanceID); 333 SkASSERT(p); 334 return p; 335 } 336 337 SkRegion* GraphicsJNI::getNativeRegion(JNIEnv* env, jobject region) 338 { 339 SkASSERT(env); 340 SkASSERT(region); 341 SkASSERT(env->IsInstanceOf(region, gRegion_class)); 342 SkRegion* r = (SkRegion*)env->GetIntField(region, gRegion_nativeInstanceID); 343 SkASSERT(r); 344 return r; 345 } 346 347 /////////////////////////////////////////////////////////////////////////////////////////// 348 349 jobject GraphicsJNI::createBitmap(JNIEnv* env, SkBitmap* bitmap, jbyteArray buffer, 350 int bitmapCreateFlags, jbyteArray ninepatch, jintArray layoutbounds, int density) 351 { 352 SkASSERT(bitmap); 353 SkASSERT(bitmap->pixelRef()); 354 bool isMutable = bitmapCreateFlags & kBitmapCreateFlag_Mutable; 355 bool isPremultiplied = bitmapCreateFlags & kBitmapCreateFlag_Premultiplied; 356 357 jobject obj = env->NewObject(gBitmap_class, gBitmap_constructorMethodID, 358 static_cast<jint>(reinterpret_cast<uintptr_t>(bitmap)), buffer, 359 bitmap->width(), bitmap->height(), density, isMutable, isPremultiplied, 360 ninepatch, layoutbounds); 361 hasException(env); // For the side effect of logging. 362 return obj; 363 } 364 365 jobject GraphicsJNI::createBitmap(JNIEnv* env, SkBitmap* bitmap, int bitmapCreateFlags, 366 jbyteArray ninepatch, int density) 367 { 368 return createBitmap(env, bitmap, NULL, bitmapCreateFlags, ninepatch, NULL, density); 369 } 370 371 void GraphicsJNI::reinitBitmap(JNIEnv* env, jobject javaBitmap, SkBitmap* bitmap, 372 bool isPremultiplied) 373 { 374 env->CallVoidMethod(javaBitmap, gBitmap_reinitMethodID, 375 bitmap->width(), bitmap->height(), isPremultiplied); 376 } 377 378 int GraphicsJNI::getBitmapAllocationByteCount(JNIEnv* env, jobject javaBitmap) 379 { 380 return env->CallIntMethod(javaBitmap, gBitmap_getAllocationByteCountMethodID); 381 } 382 383 jobject GraphicsJNI::createBitmapRegionDecoder(JNIEnv* env, SkBitmapRegionDecoder* bitmap) 384 { 385 SkASSERT(bitmap != NULL); 386 387 jobject obj = env->NewObject(gBitmapRegionDecoder_class, 388 gBitmapRegionDecoder_constructorMethodID, 389 static_cast<jint>(reinterpret_cast<uintptr_t>(bitmap))); 390 hasException(env); // For the side effect of logging. 391 return obj; 392 } 393 394 jobject GraphicsJNI::createRegion(JNIEnv* env, SkRegion* region) 395 { 396 SkASSERT(region != NULL); 397 jobject obj = env->NewObject(gRegion_class, gRegion_constructorMethodID, 398 static_cast<jint>(reinterpret_cast<uintptr_t>(region)), 0); 399 hasException(env); // For the side effect of logging. 400 return obj; 401 } 402 403 static JNIEnv* vm2env(JavaVM* vm) 404 { 405 JNIEnv* env = NULL; 406 if (vm->GetEnv((void**)&env, JNI_VERSION_1_4) != JNI_OK || NULL == env) 407 { 408 SkDebugf("------- [%p] vm->GetEnv() failed\n", vm); 409 sk_throw(); 410 } 411 return env; 412 } 413 414 /////////////////////////////////////////////////////////////////////////////// 415 416 AndroidPixelRef::AndroidPixelRef(JNIEnv* env, void* storage, size_t size, jbyteArray storageObj, 417 SkColorTable* ctable) : SkMallocPixelRef(storage, size, ctable, (storageObj == NULL)), 418 fWrappedPixelRef(NULL) { 419 SkASSERT(storage); 420 SkASSERT(env); 421 422 if (env->GetJavaVM(&fVM) != JNI_OK) { 423 SkDebugf("------ [%p] env->GetJavaVM failed\n", env); 424 sk_throw(); 425 } 426 fStorageObj = storageObj; 427 fHasGlobalRef = false; 428 fGlobalRefCnt = 0; 429 430 // If storageObj is NULL, the memory was NOT allocated on the Java heap 431 fOnJavaHeap = (storageObj != NULL); 432 433 } 434 435 AndroidPixelRef::AndroidPixelRef(AndroidPixelRef& wrappedPixelRef, SkColorTable* ctable) : 436 SkMallocPixelRef(wrappedPixelRef.getAddr(), wrappedPixelRef.getSize(), ctable, false), 437 fWrappedPixelRef(wrappedPixelRef.fWrappedPixelRef ? 438 wrappedPixelRef.fWrappedPixelRef : &wrappedPixelRef) 439 { 440 SkASSERT(fWrappedPixelRef); 441 SkSafeRef(fWrappedPixelRef); 442 443 // don't need to initialize these, as all the relevant logic delegates to the wrapped ref 444 fStorageObj = NULL; 445 fHasGlobalRef = false; 446 fGlobalRefCnt = 0; 447 fOnJavaHeap = false; 448 } 449 450 AndroidPixelRef::~AndroidPixelRef() { 451 if (fWrappedPixelRef) { 452 SkSafeUnref(fWrappedPixelRef); 453 } else if (fOnJavaHeap) { 454 JNIEnv* env = vm2env(fVM); 455 456 if (fStorageObj && fHasGlobalRef) { 457 env->DeleteGlobalRef(fStorageObj); 458 } 459 fStorageObj = NULL; 460 } 461 } 462 jbyteArray AndroidPixelRef::getStorageObj() { 463 if (fWrappedPixelRef) { 464 return fWrappedPixelRef->fStorageObj; 465 } 466 return fStorageObj; 467 } 468 469 void AndroidPixelRef::setLocalJNIRef(jbyteArray arr) { 470 if (fWrappedPixelRef) { 471 // delegate java obj management to the wrapped ref 472 fWrappedPixelRef->setLocalJNIRef(arr); 473 } else if (!fHasGlobalRef) { 474 fStorageObj = arr; 475 } 476 } 477 478 void AndroidPixelRef::globalRef(void* localref) { 479 if (fWrappedPixelRef) { 480 // delegate java obj management to the wrapped ref 481 fWrappedPixelRef->globalRef(localref); 482 483 // Note: we only ref and unref the wrapped AndroidPixelRef so that 484 // bitmap->pixelRef()->globalRef() and globalUnref() can be used in a pair, even if 485 // the bitmap has its underlying AndroidPixelRef swapped out/wrapped 486 return; 487 } 488 if (fOnJavaHeap && sk_atomic_inc(&fGlobalRefCnt) == 0) { 489 JNIEnv *env = vm2env(fVM); 490 491 // If JNI ref was passed, it is always used 492 if (localref) fStorageObj = (jbyteArray) localref; 493 494 if (fStorageObj == NULL) { 495 SkDebugf("No valid local ref to create a JNI global ref\n"); 496 sk_throw(); 497 } 498 if (fHasGlobalRef) { 499 // This should never happen 500 SkDebugf("Already holding a JNI global ref"); 501 sk_throw(); 502 } 503 504 fStorageObj = (jbyteArray) env->NewGlobalRef(fStorageObj); 505 // TODO: Check for failure here 506 fHasGlobalRef = true; 507 } 508 ref(); 509 } 510 511 void AndroidPixelRef::globalUnref() { 512 if (fWrappedPixelRef) { 513 // delegate java obj management to the wrapped ref 514 fWrappedPixelRef->globalUnref(); 515 return; 516 } 517 if (fOnJavaHeap && sk_atomic_dec(&fGlobalRefCnt) == 1) { 518 JNIEnv *env = vm2env(fVM); 519 if (!fHasGlobalRef) { 520 SkDebugf("We don't have a global ref!"); 521 sk_throw(); 522 } 523 env->DeleteGlobalRef(fStorageObj); 524 fStorageObj = NULL; 525 fHasGlobalRef = false; 526 } 527 unref(); 528 } 529 530 /////////////////////////////////////////////////////////////////////////////// 531 532 extern "C" jbyte* jniGetNonMovableArrayElements(C_JNIEnv* env, jarray arrayObj); 533 534 jbyteArray GraphicsJNI::allocateJavaPixelRef(JNIEnv* env, SkBitmap* bitmap, 535 SkColorTable* ctable) { 536 Sk64 size64 = bitmap->getSize64(); 537 if (size64.isNeg() || !size64.is32()) { 538 jniThrowException(env, "java/lang/IllegalArgumentException", 539 "bitmap size exceeds 32bits"); 540 return NULL; 541 } 542 543 size_t size = size64.get32(); 544 jbyteArray arrayObj = env->NewByteArray(size); 545 if (arrayObj) { 546 // TODO: make this work without jniGetNonMovableArrayElements 547 jbyte* addr = jniGetNonMovableArrayElements(&env->functions, arrayObj); 548 if (addr) { 549 SkPixelRef* pr = new AndroidPixelRef(env, (void*) addr, size, arrayObj, ctable); 550 bitmap->setPixelRef(pr)->unref(); 551 // since we're already allocated, we lockPixels right away 552 // HeapAllocator behaves this way too 553 bitmap->lockPixels(); 554 } 555 } 556 557 return arrayObj; 558 } 559 560 /////////////////////////////////////////////////////////////////////////////// 561 562 JavaPixelAllocator::JavaPixelAllocator(JNIEnv* env) 563 : fStorageObj(NULL), 564 fAllocCount(0) { 565 if (env->GetJavaVM(&fVM) != JNI_OK) { 566 SkDebugf("------ [%p] env->GetJavaVM failed\n", env); 567 sk_throw(); 568 } 569 } 570 571 bool JavaPixelAllocator::allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable) { 572 JNIEnv* env = vm2env(fVM); 573 574 fStorageObj = GraphicsJNI::allocateJavaPixelRef(env, bitmap, ctable); 575 fAllocCount += 1; 576 return fStorageObj != NULL; 577 } 578 579 //////////////////////////////////////////////////////////////////////////////// 580 581 JavaHeapBitmapRef::JavaHeapBitmapRef(JNIEnv* env, SkBitmap* nativeBitmap, jbyteArray buffer) { 582 fEnv = env; 583 fNativeBitmap = nativeBitmap; 584 fBuffer = buffer; 585 586 // If the buffer is NULL, the backing memory wasn't allocated on the Java heap 587 if (fBuffer) { 588 ((AndroidPixelRef*) fNativeBitmap->pixelRef())->setLocalJNIRef(fBuffer); 589 } 590 } 591 592 JavaHeapBitmapRef::~JavaHeapBitmapRef() { 593 if (fBuffer) { 594 ((AndroidPixelRef*) fNativeBitmap->pixelRef())->setLocalJNIRef(NULL); 595 } 596 } 597 598 //////////////////////////////////////////////////////////////////////////////// 599 600 static jclass make_globalref(JNIEnv* env, const char classname[]) 601 { 602 jclass c = env->FindClass(classname); 603 SkASSERT(c); 604 return (jclass)env->NewGlobalRef(c); 605 } 606 607 static jfieldID getFieldIDCheck(JNIEnv* env, jclass clazz, 608 const char fieldname[], const char type[]) 609 { 610 jfieldID id = env->GetFieldID(clazz, fieldname, type); 611 SkASSERT(id); 612 return id; 613 } 614 615 int register_android_graphics_Graphics(JNIEnv* env) 616 { 617 jmethodID m; 618 jclass c; 619 620 gRect_class = make_globalref(env, "android/graphics/Rect"); 621 gRect_leftFieldID = getFieldIDCheck(env, gRect_class, "left", "I"); 622 gRect_topFieldID = getFieldIDCheck(env, gRect_class, "top", "I"); 623 gRect_rightFieldID = getFieldIDCheck(env, gRect_class, "right", "I"); 624 gRect_bottomFieldID = getFieldIDCheck(env, gRect_class, "bottom", "I"); 625 626 gRectF_class = make_globalref(env, "android/graphics/RectF"); 627 gRectF_leftFieldID = getFieldIDCheck(env, gRectF_class, "left", "F"); 628 gRectF_topFieldID = getFieldIDCheck(env, gRectF_class, "top", "F"); 629 gRectF_rightFieldID = getFieldIDCheck(env, gRectF_class, "right", "F"); 630 gRectF_bottomFieldID = getFieldIDCheck(env, gRectF_class, "bottom", "F"); 631 632 gPoint_class = make_globalref(env, "android/graphics/Point"); 633 gPoint_xFieldID = getFieldIDCheck(env, gPoint_class, "x", "I"); 634 gPoint_yFieldID = getFieldIDCheck(env, gPoint_class, "y", "I"); 635 636 gPointF_class = make_globalref(env, "android/graphics/PointF"); 637 gPointF_xFieldID = getFieldIDCheck(env, gPointF_class, "x", "F"); 638 gPointF_yFieldID = getFieldIDCheck(env, gPointF_class, "y", "F"); 639 640 gBitmap_class = make_globalref(env, "android/graphics/Bitmap"); 641 gBitmap_nativeInstanceID = getFieldIDCheck(env, gBitmap_class, "mNativeBitmap", "I"); 642 gBitmap_constructorMethodID = env->GetMethodID(gBitmap_class, "<init>", "(I[BIIIZZ[B[I)V"); 643 gBitmap_reinitMethodID = env->GetMethodID(gBitmap_class, "reinit", "(IIZ)V"); 644 gBitmap_getAllocationByteCountMethodID = env->GetMethodID(gBitmap_class, "getAllocationByteCount", "()I"); 645 gBitmapRegionDecoder_class = make_globalref(env, "android/graphics/BitmapRegionDecoder"); 646 gBitmapRegionDecoder_constructorMethodID = env->GetMethodID(gBitmapRegionDecoder_class, "<init>", "(I)V"); 647 648 gBitmapConfig_class = make_globalref(env, "android/graphics/Bitmap$Config"); 649 gBitmapConfig_nativeInstanceID = getFieldIDCheck(env, gBitmapConfig_class, 650 "nativeInt", "I"); 651 652 gCanvas_class = make_globalref(env, "android/graphics/Canvas"); 653 gCanvas_nativeInstanceID = getFieldIDCheck(env, gCanvas_class, "mNativeCanvas", "I"); 654 655 gPaint_class = make_globalref(env, "android/graphics/Paint"); 656 gPaint_nativeInstanceID = getFieldIDCheck(env, gPaint_class, "mNativePaint", "I"); 657 658 gPicture_class = make_globalref(env, "android/graphics/Picture"); 659 gPicture_nativeInstanceID = getFieldIDCheck(env, gPicture_class, "mNativePicture", "I"); 660 661 gRegion_class = make_globalref(env, "android/graphics/Region"); 662 gRegion_nativeInstanceID = getFieldIDCheck(env, gRegion_class, "mNativeRegion", "I"); 663 gRegion_constructorMethodID = env->GetMethodID(gRegion_class, "<init>", 664 "(II)V"); 665 666 return 0; 667 } 668