Graphics.cpp revision 5e49b497ae2019586937aae0e8159292363728b5
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
13void doThrowNPE(JNIEnv* env) {
14    jniThrowNullPointerException(env, NULL);
15}
16
17void doThrowAIOOBE(JNIEnv* env) {
18    jniThrowException(env, "java/lang/ArrayIndexOutOfBoundsException", NULL);
19}
20
21void doThrowRE(JNIEnv* env, const char* msg) {
22    jniThrowRuntimeException(env, msg);
23}
24
25void doThrowIAE(JNIEnv* env, const char* msg) {
26    jniThrowException(env, "java/lang/IllegalArgumentException", msg);
27}
28
29void doThrowISE(JNIEnv* env, const char* msg) {
30    jniThrowException(env, "java/lang/IllegalStateException", msg);
31}
32
33void doThrowOOME(JNIEnv* env, const char* msg) {
34    jniThrowException(env, "java/lang/OutOfMemoryError", msg);
35}
36
37void doThrowIOE(JNIEnv* env, const char* msg) {
38    jniThrowException(env, "java/io/IOException", msg);
39}
40
41bool 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
52AutoJavaFloatArray::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
66AutoJavaFloatArray::~AutoJavaFloatArray() {
67    if (fPtr) {
68        fEnv->ReleaseFloatArrayElements(fArray, fPtr, fReleaseMode);
69    }
70}
71
72AutoJavaIntArray::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
85AutoJavaIntArray::~AutoJavaIntArray() {
86    if (fPtr) {
87        fEnv->ReleaseIntArrayElements(fArray, fPtr, 0);
88    }
89}
90
91AutoJavaShortArray::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
105AutoJavaShortArray::~AutoJavaShortArray() {
106    if (fPtr) {
107        fEnv->ReleaseShortArrayElements(fArray, fPtr, fReleaseMode);
108    }
109}
110
111AutoJavaByteArray::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
124AutoJavaByteArray::~AutoJavaByteArray() {
125    if (fPtr) {
126        fEnv->ReleaseByteArrayElements(fArray, fPtr, 0);
127    }
128}
129
130///////////////////////////////////////////////////////////////////////////////
131
132static jclass   gRect_class;
133static jfieldID gRect_leftFieldID;
134static jfieldID gRect_topFieldID;
135static jfieldID gRect_rightFieldID;
136static jfieldID gRect_bottomFieldID;
137
138static jclass   gRectF_class;
139static jfieldID gRectF_leftFieldID;
140static jfieldID gRectF_topFieldID;
141static jfieldID gRectF_rightFieldID;
142static jfieldID gRectF_bottomFieldID;
143
144static jclass   gPoint_class;
145static jfieldID gPoint_xFieldID;
146static jfieldID gPoint_yFieldID;
147
148static jclass   gPointF_class;
149static jfieldID gPointF_xFieldID;
150static jfieldID gPointF_yFieldID;
151
152static jclass   gBitmap_class;
153static jfieldID gBitmap_nativeInstanceID;
154static jmethodID gBitmap_constructorMethodID;
155static jmethodID gBitmap_reinitMethodID;
156static jmethodID gBitmap_getAllocationByteCountMethodID;
157
158static jclass   gBitmapConfig_class;
159static jfieldID gBitmapConfig_nativeInstanceID;
160
161static jclass   gBitmapRegionDecoder_class;
162static jmethodID gBitmapRegionDecoder_constructorMethodID;
163
164static jclass   gCanvas_class;
165static jfieldID gCanvas_nativeInstanceID;
166
167static jclass   gPaint_class;
168static jfieldID gPaint_nativeInstanceID;
169
170static jclass   gPicture_class;
171static jfieldID gPicture_nativeInstanceID;
172
173static jclass   gRegion_class;
174static jfieldID gRegion_nativeInstanceID;
175static jmethodID gRegion_constructorMethodID;
176
177///////////////////////////////////////////////////////////////////////////////
178
179void 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
189void 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
199SkIRect* 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
210void 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
220SkRect* 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
231SkRect* 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
242void 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
252SkIPoint* 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
261void 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
269SkPoint* 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
278void 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
286SkBitmap* 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
295SkBitmap::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
309SkCanvas* 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
318SkPaint* 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
327SkPicture* 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
337SkRegion* 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.
350static 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
361jobject 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
381jobject 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
387void 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
398int GraphicsJNI::getBitmapAllocationByteCount(JNIEnv* env, jobject javaBitmap)
399{
400    return env->CallIntMethod(javaBitmap, gBitmap_getAllocationByteCountMethodID);
401}
402
403jobject 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
414jobject 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
423static 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
436AndroidPixelRef::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
455AndroidPixelRef::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
471AndroidPixelRef::~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}
483jbyteArray AndroidPixelRef::getStorageObj() {
484    if (fWrappedPixelRef) {
485        return fWrappedPixelRef->fStorageObj;
486    }
487    return fStorageObj;
488}
489
490void 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
499void 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
532void 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
553extern "C" jbyte* jniGetNonMovableArrayElements(C_JNIEnv* env, jarray arrayObj);
554
555jbyteArray 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
591JavaPixelAllocator::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
600bool 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
610JavaHeapBitmapRef::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
621JavaHeapBitmapRef::~JavaHeapBitmapRef() {
622    if (fBuffer) {
623        ((AndroidPixelRef*) fNativeBitmap->pixelRef())->setLocalJNIRef(NULL);
624    }
625}
626
627////////////////////////////////////////////////////////////////////////////////
628
629static 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
636static 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
644int 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