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