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 // Assert that bitmap's SkAlphaType is consistent with isPremultiplied. 350 static void assert_premultiplied(const SkBitmap& bitmap, bool isPremultiplied) { 351 // kOpaque_SkAlphaType and kIgnore_SkAlphaType mean that isPremultiplied is 352 // irrelevant. This just tests to ensure that the SkAlphaType is not 353 // opposite of isPremultiplied. 354 if (isPremultiplied) { 355 SkASSERT(bitmap.alphaType() != kUnpremul_SkAlphaType); 356 } else { 357 SkASSERT(bitmap.alphaType() != kPremul_SkAlphaType); 358 } 359 } 360 361 jobject GraphicsJNI::createBitmap(JNIEnv* env, SkBitmap* bitmap, jbyteArray buffer, 362 int bitmapCreateFlags, jbyteArray ninepatch, jintArray layoutbounds, int density) 363 { 364 SkASSERT(bitmap); 365 SkASSERT(bitmap->pixelRef()); 366 bool isMutable = bitmapCreateFlags & kBitmapCreateFlag_Mutable; 367 bool isPremultiplied = bitmapCreateFlags & kBitmapCreateFlag_Premultiplied; 368 369 // The caller needs to have already set the alpha type properly, so the 370 // native SkBitmap stays in sync with the Java Bitmap. 371 assert_premultiplied(*bitmap, isPremultiplied); 372 373 jobject obj = env->NewObject(gBitmap_class, gBitmap_constructorMethodID, 374 static_cast<jint>(reinterpret_cast<uintptr_t>(bitmap)), buffer, 375 bitmap->width(), bitmap->height(), density, isMutable, isPremultiplied, 376 ninepatch, layoutbounds); 377 hasException(env); // For the side effect of logging. 378 return obj; 379 } 380 381 jobject GraphicsJNI::createBitmap(JNIEnv* env, SkBitmap* bitmap, int bitmapCreateFlags, 382 jbyteArray ninepatch, int density) 383 { 384 return createBitmap(env, bitmap, NULL, bitmapCreateFlags, ninepatch, NULL, density); 385 } 386 387 void GraphicsJNI::reinitBitmap(JNIEnv* env, jobject javaBitmap, SkBitmap* bitmap, 388 bool isPremultiplied) 389 { 390 // The caller needs to have already set the alpha type properly, so the 391 // native SkBitmap stays in sync with the Java Bitmap. 392 assert_premultiplied(*bitmap, isPremultiplied); 393 394 env->CallVoidMethod(javaBitmap, gBitmap_reinitMethodID, 395 bitmap->width(), bitmap->height(), isPremultiplied); 396 } 397 398 int GraphicsJNI::getBitmapAllocationByteCount(JNIEnv* env, jobject javaBitmap) 399 { 400 return env->CallIntMethod(javaBitmap, gBitmap_getAllocationByteCountMethodID); 401 } 402 403 jobject GraphicsJNI::createBitmapRegionDecoder(JNIEnv* env, SkBitmapRegionDecoder* bitmap) 404 { 405 SkASSERT(bitmap != NULL); 406 407 jobject obj = env->NewObject(gBitmapRegionDecoder_class, 408 gBitmapRegionDecoder_constructorMethodID, 409 static_cast<jint>(reinterpret_cast<uintptr_t>(bitmap))); 410 hasException(env); // For the side effect of logging. 411 return obj; 412 } 413 414 jobject GraphicsJNI::createRegion(JNIEnv* env, SkRegion* region) 415 { 416 SkASSERT(region != NULL); 417 jobject obj = env->NewObject(gRegion_class, gRegion_constructorMethodID, 418 static_cast<jint>(reinterpret_cast<uintptr_t>(region)), 0); 419 hasException(env); // For the side effect of logging. 420 return obj; 421 } 422 423 static JNIEnv* vm2env(JavaVM* vm) 424 { 425 JNIEnv* env = NULL; 426 if (vm->GetEnv((void**)&env, JNI_VERSION_1_4) != JNI_OK || NULL == env) 427 { 428 SkDebugf("------- [%p] vm->GetEnv() failed\n", vm); 429 sk_throw(); 430 } 431 return env; 432 } 433 434 /////////////////////////////////////////////////////////////////////////////// 435 436 AndroidPixelRef::AndroidPixelRef(JNIEnv* env, const SkImageInfo& info, void* storage, 437 size_t rowBytes, jbyteArray storageObj, SkColorTable* ctable) : 438 SkMallocPixelRef(info, storage, rowBytes, ctable, (storageObj == NULL)), 439 fWrappedPixelRef(NULL) { 440 SkASSERT(storage); 441 SkASSERT(env); 442 443 if (env->GetJavaVM(&fVM) != JNI_OK) { 444 SkDebugf("------ [%p] env->GetJavaVM failed\n", env); 445 sk_throw(); 446 } 447 fStorageObj = storageObj; 448 fHasGlobalRef = false; 449 fGlobalRefCnt = 0; 450 451 // If storageObj is NULL, the memory was NOT allocated on the Java heap 452 fOnJavaHeap = (storageObj != NULL); 453 } 454 455 AndroidPixelRef::AndroidPixelRef(AndroidPixelRef& wrappedPixelRef, const SkImageInfo& info, 456 size_t rowBytes, SkColorTable* ctable) : 457 SkMallocPixelRef(info, wrappedPixelRef.getAddr(), rowBytes, ctable, false), 458 fWrappedPixelRef(wrappedPixelRef.fWrappedPixelRef ? 459 wrappedPixelRef.fWrappedPixelRef : &wrappedPixelRef) 460 { 461 SkASSERT(fWrappedPixelRef); 462 SkSafeRef(fWrappedPixelRef); 463 464 // don't need to initialize these, as all the relevant logic delegates to the wrapped ref 465 fStorageObj = NULL; 466 fHasGlobalRef = false; 467 fGlobalRefCnt = 0; 468 fOnJavaHeap = false; 469 } 470 471 AndroidPixelRef::~AndroidPixelRef() { 472 if (fWrappedPixelRef) { 473 SkSafeUnref(fWrappedPixelRef); 474 } else if (fOnJavaHeap) { 475 JNIEnv* env = vm2env(fVM); 476 477 if (fStorageObj && fHasGlobalRef) { 478 env->DeleteGlobalRef(fStorageObj); 479 } 480 fStorageObj = NULL; 481 } 482 } 483 jbyteArray AndroidPixelRef::getStorageObj() { 484 if (fWrappedPixelRef) { 485 return fWrappedPixelRef->fStorageObj; 486 } 487 return fStorageObj; 488 } 489 490 void AndroidPixelRef::setLocalJNIRef(jbyteArray arr) { 491 if (fWrappedPixelRef) { 492 // delegate java obj management to the wrapped ref 493 fWrappedPixelRef->setLocalJNIRef(arr); 494 } else if (!fHasGlobalRef) { 495 fStorageObj = arr; 496 } 497 } 498 499 void AndroidPixelRef::globalRef(void* localref) { 500 if (fWrappedPixelRef) { 501 // delegate java obj management to the wrapped ref 502 fWrappedPixelRef->globalRef(localref); 503 504 // Note: we only ref and unref the wrapped AndroidPixelRef so that 505 // bitmap->pixelRef()->globalRef() and globalUnref() can be used in a pair, even if 506 // the bitmap has its underlying AndroidPixelRef swapped out/wrapped 507 return; 508 } 509 if (fOnJavaHeap && sk_atomic_inc(&fGlobalRefCnt) == 0) { 510 JNIEnv *env = vm2env(fVM); 511 512 // If JNI ref was passed, it is always used 513 if (localref) fStorageObj = (jbyteArray) localref; 514 515 if (fStorageObj == NULL) { 516 SkDebugf("No valid local ref to create a JNI global ref\n"); 517 sk_throw(); 518 } 519 if (fHasGlobalRef) { 520 // This should never happen 521 SkDebugf("Already holding a JNI global ref"); 522 sk_throw(); 523 } 524 525 fStorageObj = (jbyteArray) env->NewGlobalRef(fStorageObj); 526 // TODO: Check for failure here 527 fHasGlobalRef = true; 528 } 529 ref(); 530 } 531 532 void AndroidPixelRef::globalUnref() { 533 if (fWrappedPixelRef) { 534 // delegate java obj management to the wrapped ref 535 fWrappedPixelRef->globalUnref(); 536 return; 537 } 538 if (fOnJavaHeap && sk_atomic_dec(&fGlobalRefCnt) == 1) { 539 JNIEnv *env = vm2env(fVM); 540 if (!fHasGlobalRef) { 541 SkDebugf("We don't have a global ref!"); 542 sk_throw(); 543 } 544 env->DeleteGlobalRef(fStorageObj); 545 fStorageObj = NULL; 546 fHasGlobalRef = false; 547 } 548 unref(); 549 } 550 551 /////////////////////////////////////////////////////////////////////////////// 552 553 extern "C" jbyte* jniGetNonMovableArrayElements(C_JNIEnv* env, jarray arrayObj); 554 555 jbyteArray GraphicsJNI::allocateJavaPixelRef(JNIEnv* env, SkBitmap* bitmap, 556 SkColorTable* ctable) { 557 Sk64 size64 = bitmap->getSize64(); 558 if (size64.isNeg() || !size64.is32()) { 559 jniThrowException(env, "java/lang/IllegalArgumentException", 560 "bitmap size exceeds 32bits"); 561 return NULL; 562 } 563 564 SkImageInfo bitmapInfo; 565 if (!bitmap->asImageInfo(&bitmapInfo)) { 566 jniThrowException(env, "java/lang/IllegalArgumentException", 567 "unknown bitmap configuration"); 568 return NULL; 569 } 570 571 size_t size = size64.get32(); 572 jbyteArray arrayObj = env->NewByteArray(size); 573 if (arrayObj) { 574 // TODO: make this work without jniGetNonMovableArrayElements 575 jbyte* addr = jniGetNonMovableArrayElements(&env->functions, arrayObj); 576 if (addr) { 577 SkPixelRef* pr = new AndroidPixelRef(env, bitmapInfo, (void*) addr, 578 bitmap->rowBytes(), arrayObj, ctable); 579 bitmap->setPixelRef(pr)->unref(); 580 // since we're already allocated, we lockPixels right away 581 // HeapAllocator behaves this way too 582 bitmap->lockPixels(); 583 } 584 } 585 586 return arrayObj; 587 } 588 589 /////////////////////////////////////////////////////////////////////////////// 590 591 JavaPixelAllocator::JavaPixelAllocator(JNIEnv* env) 592 : fStorageObj(NULL), 593 fAllocCount(0) { 594 if (env->GetJavaVM(&fVM) != JNI_OK) { 595 SkDebugf("------ [%p] env->GetJavaVM failed\n", env); 596 sk_throw(); 597 } 598 } 599 600 bool JavaPixelAllocator::allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable) { 601 JNIEnv* env = vm2env(fVM); 602 603 fStorageObj = GraphicsJNI::allocateJavaPixelRef(env, bitmap, ctable); 604 fAllocCount += 1; 605 return fStorageObj != NULL; 606 } 607 608 //////////////////////////////////////////////////////////////////////////////// 609 610 JavaHeapBitmapRef::JavaHeapBitmapRef(JNIEnv* env, SkBitmap* nativeBitmap, jbyteArray buffer) { 611 fEnv = env; 612 fNativeBitmap = nativeBitmap; 613 fBuffer = buffer; 614 615 // If the buffer is NULL, the backing memory wasn't allocated on the Java heap 616 if (fBuffer) { 617 ((AndroidPixelRef*) fNativeBitmap->pixelRef())->setLocalJNIRef(fBuffer); 618 } 619 } 620 621 JavaHeapBitmapRef::~JavaHeapBitmapRef() { 622 if (fBuffer) { 623 ((AndroidPixelRef*) fNativeBitmap->pixelRef())->setLocalJNIRef(NULL); 624 } 625 } 626 627 //////////////////////////////////////////////////////////////////////////////// 628 629 static jclass make_globalref(JNIEnv* env, const char classname[]) 630 { 631 jclass c = env->FindClass(classname); 632 SkASSERT(c); 633 return (jclass)env->NewGlobalRef(c); 634 } 635 636 static jfieldID getFieldIDCheck(JNIEnv* env, jclass clazz, 637 const char fieldname[], const char type[]) 638 { 639 jfieldID id = env->GetFieldID(clazz, fieldname, type); 640 SkASSERT(id); 641 return id; 642 } 643 644 int register_android_graphics_Graphics(JNIEnv* env) 645 { 646 jmethodID m; 647 jclass c; 648 649 gRect_class = make_globalref(env, "android/graphics/Rect"); 650 gRect_leftFieldID = getFieldIDCheck(env, gRect_class, "left", "I"); 651 gRect_topFieldID = getFieldIDCheck(env, gRect_class, "top", "I"); 652 gRect_rightFieldID = getFieldIDCheck(env, gRect_class, "right", "I"); 653 gRect_bottomFieldID = getFieldIDCheck(env, gRect_class, "bottom", "I"); 654 655 gRectF_class = make_globalref(env, "android/graphics/RectF"); 656 gRectF_leftFieldID = getFieldIDCheck(env, gRectF_class, "left", "F"); 657 gRectF_topFieldID = getFieldIDCheck(env, gRectF_class, "top", "F"); 658 gRectF_rightFieldID = getFieldIDCheck(env, gRectF_class, "right", "F"); 659 gRectF_bottomFieldID = getFieldIDCheck(env, gRectF_class, "bottom", "F"); 660 661 gPoint_class = make_globalref(env, "android/graphics/Point"); 662 gPoint_xFieldID = getFieldIDCheck(env, gPoint_class, "x", "I"); 663 gPoint_yFieldID = getFieldIDCheck(env, gPoint_class, "y", "I"); 664 665 gPointF_class = make_globalref(env, "android/graphics/PointF"); 666 gPointF_xFieldID = getFieldIDCheck(env, gPointF_class, "x", "F"); 667 gPointF_yFieldID = getFieldIDCheck(env, gPointF_class, "y", "F"); 668 669 gBitmap_class = make_globalref(env, "android/graphics/Bitmap"); 670 gBitmap_nativeInstanceID = getFieldIDCheck(env, gBitmap_class, "mNativeBitmap", "I"); 671 gBitmap_constructorMethodID = env->GetMethodID(gBitmap_class, "<init>", "(I[BIIIZZ[B[I)V"); 672 gBitmap_reinitMethodID = env->GetMethodID(gBitmap_class, "reinit", "(IIZ)V"); 673 gBitmap_getAllocationByteCountMethodID = env->GetMethodID(gBitmap_class, "getAllocationByteCount", "()I"); 674 gBitmapRegionDecoder_class = make_globalref(env, "android/graphics/BitmapRegionDecoder"); 675 gBitmapRegionDecoder_constructorMethodID = env->GetMethodID(gBitmapRegionDecoder_class, "<init>", "(I)V"); 676 677 gBitmapConfig_class = make_globalref(env, "android/graphics/Bitmap$Config"); 678 gBitmapConfig_nativeInstanceID = getFieldIDCheck(env, gBitmapConfig_class, 679 "nativeInt", "I"); 680 681 gCanvas_class = make_globalref(env, "android/graphics/Canvas"); 682 gCanvas_nativeInstanceID = getFieldIDCheck(env, gCanvas_class, "mNativeCanvas", "I"); 683 684 gPaint_class = make_globalref(env, "android/graphics/Paint"); 685 gPaint_nativeInstanceID = getFieldIDCheck(env, gPaint_class, "mNativePaint", "I"); 686 687 gPicture_class = make_globalref(env, "android/graphics/Picture"); 688 gPicture_nativeInstanceID = getFieldIDCheck(env, gPicture_class, "mNativePicture", "I"); 689 690 gRegion_class = make_globalref(env, "android/graphics/Region"); 691 gRegion_nativeInstanceID = getFieldIDCheck(env, gRegion_class, "mNativeRegion", "I"); 692 gRegion_constructorMethodID = env->GetMethodID(gRegion_class, "<init>", 693 "(II)V"); 694 695 return 0; 696 } 697