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;
155
156static jclass   gBitmapConfig_class;
157static jfieldID gBitmapConfig_nativeInstanceID;
158
159static jclass   gBitmapRegionDecoder_class;
160static jmethodID gBitmapRegionDecoder_constructorMethodID;
161
162static jclass   gCanvas_class;
163static jfieldID gCanvas_nativeInstanceID;
164
165static jclass   gPaint_class;
166static jfieldID gPaint_nativeInstanceID;
167
168static jclass   gPicture_class;
169static jfieldID gPicture_nativeInstanceID;
170
171static jclass   gRegion_class;
172static jfieldID gRegion_nativeInstanceID;
173static jmethodID gRegion_constructorMethodID;
174
175///////////////////////////////////////////////////////////////////////////////
176
177void GraphicsJNI::get_jrect(JNIEnv* env, jobject obj, int* L, int* T, int* R, int* B)
178{
179    SkASSERT(env->IsInstanceOf(obj, gRect_class));
180
181    *L = env->GetIntField(obj, gRect_leftFieldID);
182    *T = env->GetIntField(obj, gRect_topFieldID);
183    *R = env->GetIntField(obj, gRect_rightFieldID);
184    *B = env->GetIntField(obj, gRect_bottomFieldID);
185}
186
187void GraphicsJNI::set_jrect(JNIEnv* env, jobject obj, int L, int T, int R, int B)
188{
189    SkASSERT(env->IsInstanceOf(obj, gRect_class));
190
191    env->SetIntField(obj, gRect_leftFieldID, L);
192    env->SetIntField(obj, gRect_topFieldID, T);
193    env->SetIntField(obj, gRect_rightFieldID, R);
194    env->SetIntField(obj, gRect_bottomFieldID, B);
195}
196
197SkIRect* GraphicsJNI::jrect_to_irect(JNIEnv* env, jobject obj, SkIRect* ir)
198{
199    SkASSERT(env->IsInstanceOf(obj, gRect_class));
200
201    ir->set(env->GetIntField(obj, gRect_leftFieldID),
202            env->GetIntField(obj, gRect_topFieldID),
203            env->GetIntField(obj, gRect_rightFieldID),
204            env->GetIntField(obj, gRect_bottomFieldID));
205    return ir;
206}
207
208void GraphicsJNI::irect_to_jrect(const SkIRect& ir, JNIEnv* env, jobject obj)
209{
210    SkASSERT(env->IsInstanceOf(obj, gRect_class));
211
212    env->SetIntField(obj, gRect_leftFieldID, ir.fLeft);
213    env->SetIntField(obj, gRect_topFieldID, ir.fTop);
214    env->SetIntField(obj, gRect_rightFieldID, ir.fRight);
215    env->SetIntField(obj, gRect_bottomFieldID, ir.fBottom);
216}
217
218SkRect* GraphicsJNI::jrectf_to_rect(JNIEnv* env, jobject obj, SkRect* r)
219{
220    SkASSERT(env->IsInstanceOf(obj, gRectF_class));
221
222    r->set(SkFloatToScalar(env->GetFloatField(obj, gRectF_leftFieldID)),
223           SkFloatToScalar(env->GetFloatField(obj, gRectF_topFieldID)),
224           SkFloatToScalar(env->GetFloatField(obj, gRectF_rightFieldID)),
225           SkFloatToScalar(env->GetFloatField(obj, gRectF_bottomFieldID)));
226    return r;
227}
228
229SkRect* GraphicsJNI::jrect_to_rect(JNIEnv* env, jobject obj, SkRect* r)
230{
231    SkASSERT(env->IsInstanceOf(obj, gRect_class));
232
233    r->set(SkIntToScalar(env->GetIntField(obj, gRect_leftFieldID)),
234           SkIntToScalar(env->GetIntField(obj, gRect_topFieldID)),
235           SkIntToScalar(env->GetIntField(obj, gRect_rightFieldID)),
236           SkIntToScalar(env->GetIntField(obj, gRect_bottomFieldID)));
237    return r;
238}
239
240void GraphicsJNI::rect_to_jrectf(const SkRect& r, JNIEnv* env, jobject obj)
241{
242    SkASSERT(env->IsInstanceOf(obj, gRectF_class));
243
244    env->SetFloatField(obj, gRectF_leftFieldID, SkScalarToFloat(r.fLeft));
245    env->SetFloatField(obj, gRectF_topFieldID, SkScalarToFloat(r.fTop));
246    env->SetFloatField(obj, gRectF_rightFieldID, SkScalarToFloat(r.fRight));
247    env->SetFloatField(obj, gRectF_bottomFieldID, SkScalarToFloat(r.fBottom));
248}
249
250SkIPoint* GraphicsJNI::jpoint_to_ipoint(JNIEnv* env, jobject obj, SkIPoint* point)
251{
252    SkASSERT(env->IsInstanceOf(obj, gPoint_class));
253
254    point->set(env->GetIntField(obj, gPoint_xFieldID),
255               env->GetIntField(obj, gPoint_yFieldID));
256    return point;
257}
258
259void GraphicsJNI::ipoint_to_jpoint(const SkIPoint& ir, JNIEnv* env, jobject obj)
260{
261    SkASSERT(env->IsInstanceOf(obj, gPoint_class));
262
263    env->SetIntField(obj, gPoint_xFieldID, ir.fX);
264    env->SetIntField(obj, gPoint_yFieldID, ir.fY);
265}
266
267SkPoint* GraphicsJNI::jpointf_to_point(JNIEnv* env, jobject obj, SkPoint* point)
268{
269    SkASSERT(env->IsInstanceOf(obj, gPointF_class));
270
271    point->set(SkFloatToScalar(env->GetIntField(obj, gPointF_xFieldID)),
272               SkFloatToScalar(env->GetIntField(obj, gPointF_yFieldID)));
273    return point;
274}
275
276void GraphicsJNI::point_to_jpointf(const SkPoint& r, JNIEnv* env, jobject obj)
277{
278    SkASSERT(env->IsInstanceOf(obj, gPointF_class));
279
280    env->SetFloatField(obj, gPointF_xFieldID, SkScalarToFloat(r.fX));
281    env->SetFloatField(obj, gPointF_yFieldID, SkScalarToFloat(r.fY));
282}
283
284SkBitmap* GraphicsJNI::getNativeBitmap(JNIEnv* env, jobject bitmap) {
285    SkASSERT(env);
286    SkASSERT(bitmap);
287    SkASSERT(env->IsInstanceOf(bitmap, gBitmap_class));
288    SkBitmap* b = (SkBitmap*)env->GetIntField(bitmap, gBitmap_nativeInstanceID);
289    SkASSERT(b);
290    return b;
291}
292
293SkBitmap::Config GraphicsJNI::getNativeBitmapConfig(JNIEnv* env,
294                                                    jobject jconfig) {
295    SkASSERT(env);
296    if (NULL == jconfig) {
297        return SkBitmap::kNo_Config;
298    }
299    SkASSERT(env->IsInstanceOf(jconfig, gBitmapConfig_class));
300    int c = env->GetIntField(jconfig, gBitmapConfig_nativeInstanceID);
301    if (c < 0 || c >= SkBitmap::kConfigCount) {
302        c = SkBitmap::kNo_Config;
303    }
304    return static_cast<SkBitmap::Config>(c);
305}
306
307SkCanvas* GraphicsJNI::getNativeCanvas(JNIEnv* env, jobject canvas) {
308    SkASSERT(env);
309    SkASSERT(canvas);
310    SkASSERT(env->IsInstanceOf(canvas, gCanvas_class));
311    SkCanvas* c = (SkCanvas*)env->GetIntField(canvas, gCanvas_nativeInstanceID);
312    SkASSERT(c);
313    return c;
314}
315
316SkPaint* GraphicsJNI::getNativePaint(JNIEnv* env, jobject paint) {
317    SkASSERT(env);
318    SkASSERT(paint);
319    SkASSERT(env->IsInstanceOf(paint, gPaint_class));
320    SkPaint* p = (SkPaint*)env->GetIntField(paint, gPaint_nativeInstanceID);
321    SkASSERT(p);
322    return p;
323}
324
325SkPicture* GraphicsJNI::getNativePicture(JNIEnv* env, jobject picture)
326{
327    SkASSERT(env);
328    SkASSERT(picture);
329    SkASSERT(env->IsInstanceOf(picture, gPicture_class));
330    SkPicture* p = (SkPicture*)env->GetIntField(picture, gPicture_nativeInstanceID);
331    SkASSERT(p);
332    return p;
333}
334
335SkRegion* GraphicsJNI::getNativeRegion(JNIEnv* env, jobject region)
336{
337    SkASSERT(env);
338    SkASSERT(region);
339    SkASSERT(env->IsInstanceOf(region, gRegion_class));
340    SkRegion* r = (SkRegion*)env->GetIntField(region, gRegion_nativeInstanceID);
341    SkASSERT(r);
342    return r;
343}
344
345///////////////////////////////////////////////////////////////////////////////////////////
346
347jobject GraphicsJNI::createBitmap(JNIEnv* env, SkBitmap* bitmap, jbyteArray buffer,
348                                  bool isMutable, jbyteArray ninepatch, jintArray layoutbounds,
349                                  int density)
350{
351    SkASSERT(bitmap);
352    SkASSERT(bitmap->pixelRef());
353    jobject obj = env->NewObject(gBitmap_class, gBitmap_constructorMethodID,
354            static_cast<jint>(reinterpret_cast<uintptr_t>(bitmap)),
355            buffer, isMutable, ninepatch, layoutbounds, density);
356    hasException(env); // For the side effect of logging.
357    return obj;
358}
359
360jobject GraphicsJNI::createBitmap(JNIEnv* env, SkBitmap* bitmap, bool isMutable,
361                            jbyteArray ninepatch, int density)
362{
363    return createBitmap(env, bitmap, NULL, isMutable, ninepatch, NULL, density);
364}
365
366
367jobject GraphicsJNI::createBitmapRegionDecoder(JNIEnv* env, SkBitmapRegionDecoder* bitmap)
368{
369    SkASSERT(bitmap != NULL);
370
371    jobject obj = env->NewObject(gBitmapRegionDecoder_class,
372            gBitmapRegionDecoder_constructorMethodID,
373            static_cast<jint>(reinterpret_cast<uintptr_t>(bitmap)));
374    hasException(env); // For the side effect of logging.
375    return obj;
376}
377
378jobject GraphicsJNI::createRegion(JNIEnv* env, SkRegion* region)
379{
380    SkASSERT(region != NULL);
381    jobject obj = env->NewObject(gRegion_class, gRegion_constructorMethodID,
382            static_cast<jint>(reinterpret_cast<uintptr_t>(region)), 0);
383    hasException(env); // For the side effect of logging.
384    return obj;
385}
386
387static JNIEnv* vm2env(JavaVM* vm)
388{
389    JNIEnv* env = NULL;
390    if (vm->GetEnv((void**)&env, JNI_VERSION_1_4) != JNI_OK || NULL == env)
391    {
392        SkDebugf("------- [%p] vm->GetEnv() failed\n", vm);
393        sk_throw();
394    }
395    return env;
396}
397
398///////////////////////////////////////////////////////////////////////////////
399
400AndroidPixelRef::AndroidPixelRef(JNIEnv* env, void* storage, size_t size, jbyteArray storageObj,
401        SkColorTable* ctable) : SkMallocPixelRef(storage, size, ctable) {
402    SkASSERT(storage);
403    SkASSERT(env);
404
405    if (env->GetJavaVM(&fVM) != JNI_OK) {
406        SkDebugf("------ [%p] env->GetJavaVM failed\n", env);
407        sk_throw();
408    }
409    fStorageObj = storageObj;
410    fHasGlobalRef = false;
411    fGlobalRefCnt = 0;
412
413    // If storageObj is NULL, the memory was NOT allocated on the Java heap
414    fOnJavaHeap = (storageObj != NULL);
415
416}
417
418AndroidPixelRef::~AndroidPixelRef() {
419    if (fOnJavaHeap) {
420        JNIEnv* env = vm2env(fVM);
421
422        if (fStorageObj && fHasGlobalRef) {
423            env->DeleteGlobalRef(fStorageObj);
424        }
425        fStorageObj = NULL;
426
427        // Set this to NULL to prevent the SkMallocPixelRef destructor
428        // from freeing the memory.
429        fStorage = NULL;
430    }
431}
432
433void AndroidPixelRef::setLocalJNIRef(jbyteArray arr) {
434    if (!fHasGlobalRef) {
435        fStorageObj = arr;
436    }
437}
438
439void AndroidPixelRef::globalRef(void* localref) {
440    if (fOnJavaHeap && sk_atomic_inc(&fGlobalRefCnt) == 0) {
441        JNIEnv *env = vm2env(fVM);
442
443        // If JNI ref was passed, it is always used
444        if (localref) fStorageObj = (jbyteArray) localref;
445
446        if (fStorageObj == NULL) {
447            SkDebugf("No valid local ref to create a JNI global ref\n");
448            sk_throw();
449        }
450        if (fHasGlobalRef) {
451            // This should never happen
452            SkDebugf("Already holding a JNI global ref");
453            sk_throw();
454        }
455
456        fStorageObj = (jbyteArray) env->NewGlobalRef(fStorageObj);
457        // TODO: Check for failure here
458        fHasGlobalRef = true;
459    }
460    ref();
461}
462
463void AndroidPixelRef::globalUnref() {
464    if (fOnJavaHeap && sk_atomic_dec(&fGlobalRefCnt) == 1) {
465        JNIEnv *env = vm2env(fVM);
466        if (!fHasGlobalRef) {
467            SkDebugf("We don't have a global ref!");
468            sk_throw();
469        }
470        env->DeleteGlobalRef(fStorageObj);
471        fStorageObj = NULL;
472        fHasGlobalRef = false;
473    }
474    unref();
475}
476
477///////////////////////////////////////////////////////////////////////////////
478
479extern "C" jbyte* jniGetNonMovableArrayElements(C_JNIEnv* env, jarray arrayObj);
480
481jbyteArray GraphicsJNI::allocateJavaPixelRef(JNIEnv* env, SkBitmap* bitmap,
482                                             SkColorTable* ctable) {
483    Sk64 size64 = bitmap->getSize64();
484    if (size64.isNeg() || !size64.is32()) {
485        jniThrowException(env, "java/lang/IllegalArgumentException",
486                          "bitmap size exceeds 32bits");
487        return NULL;
488    }
489
490    size_t size = size64.get32();
491    jbyteArray arrayObj = env->NewByteArray(size);
492    if (arrayObj) {
493        // TODO: make this work without jniGetNonMovableArrayElements
494        jbyte* addr = jniGetNonMovableArrayElements(&env->functions, arrayObj);
495        if (addr) {
496            SkPixelRef* pr = new AndroidPixelRef(env, (void*) addr, size, arrayObj, ctable);
497            bitmap->setPixelRef(pr)->unref();
498            // since we're already allocated, we lockPixels right away
499            // HeapAllocator behaves this way too
500            bitmap->lockPixels();
501        }
502    }
503
504    return arrayObj;
505}
506
507///////////////////////////////////////////////////////////////////////////////
508
509JavaPixelAllocator::JavaPixelAllocator(JNIEnv* env)
510    : fStorageObj(NULL),
511      fAllocCount(0) {
512    if (env->GetJavaVM(&fVM) != JNI_OK) {
513        SkDebugf("------ [%p] env->GetJavaVM failed\n", env);
514        sk_throw();
515    }
516}
517
518bool JavaPixelAllocator::allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable) {
519    JNIEnv* env = vm2env(fVM);
520
521    fStorageObj = GraphicsJNI::allocateJavaPixelRef(env, bitmap, ctable);
522    fAllocCount += 1;
523    return fStorageObj != NULL;
524}
525
526////////////////////////////////////////////////////////////////////////////////
527
528JavaHeapBitmapRef::JavaHeapBitmapRef(JNIEnv* env, SkBitmap* nativeBitmap, jbyteArray buffer) {
529    fEnv = env;
530    fNativeBitmap = nativeBitmap;
531    fBuffer = buffer;
532
533    // If the buffer is NULL, the backing memory wasn't allocated on the Java heap
534    if (fBuffer) {
535        ((AndroidPixelRef*) fNativeBitmap->pixelRef())->setLocalJNIRef(fBuffer);
536    }
537}
538
539JavaHeapBitmapRef::~JavaHeapBitmapRef() {
540    if (fBuffer) {
541        ((AndroidPixelRef*) fNativeBitmap->pixelRef())->setLocalJNIRef(NULL);
542    }
543}
544
545////////////////////////////////////////////////////////////////////////////////
546
547static jclass make_globalref(JNIEnv* env, const char classname[])
548{
549    jclass c = env->FindClass(classname);
550    SkASSERT(c);
551    return (jclass)env->NewGlobalRef(c);
552}
553
554static jfieldID getFieldIDCheck(JNIEnv* env, jclass clazz,
555                                const char fieldname[], const char type[])
556{
557    jfieldID id = env->GetFieldID(clazz, fieldname, type);
558    SkASSERT(id);
559    return id;
560}
561
562int register_android_graphics_Graphics(JNIEnv* env)
563{
564    jmethodID m;
565    jclass c;
566
567    gRect_class = make_globalref(env, "android/graphics/Rect");
568    gRect_leftFieldID = getFieldIDCheck(env, gRect_class, "left", "I");
569    gRect_topFieldID = getFieldIDCheck(env, gRect_class, "top", "I");
570    gRect_rightFieldID = getFieldIDCheck(env, gRect_class, "right", "I");
571    gRect_bottomFieldID = getFieldIDCheck(env, gRect_class, "bottom", "I");
572
573    gRectF_class = make_globalref(env, "android/graphics/RectF");
574    gRectF_leftFieldID = getFieldIDCheck(env, gRectF_class, "left", "F");
575    gRectF_topFieldID = getFieldIDCheck(env, gRectF_class, "top", "F");
576    gRectF_rightFieldID = getFieldIDCheck(env, gRectF_class, "right", "F");
577    gRectF_bottomFieldID = getFieldIDCheck(env, gRectF_class, "bottom", "F");
578
579    gPoint_class = make_globalref(env, "android/graphics/Point");
580    gPoint_xFieldID = getFieldIDCheck(env, gPoint_class, "x", "I");
581    gPoint_yFieldID = getFieldIDCheck(env, gPoint_class, "y", "I");
582
583    gPointF_class = make_globalref(env, "android/graphics/PointF");
584    gPointF_xFieldID = getFieldIDCheck(env, gPointF_class, "x", "F");
585    gPointF_yFieldID = getFieldIDCheck(env, gPointF_class, "y", "F");
586
587    gBitmap_class = make_globalref(env, "android/graphics/Bitmap");
588    gBitmap_nativeInstanceID = getFieldIDCheck(env, gBitmap_class, "mNativeBitmap", "I");
589    gBitmap_constructorMethodID = env->GetMethodID(gBitmap_class, "<init>",
590                                            "(I[BZ[B[II)V");
591    gBitmapRegionDecoder_class = make_globalref(env, "android/graphics/BitmapRegionDecoder");
592    gBitmapRegionDecoder_constructorMethodID = env->GetMethodID(gBitmapRegionDecoder_class, "<init>", "(I)V");
593
594    gBitmapConfig_class = make_globalref(env, "android/graphics/Bitmap$Config");
595    gBitmapConfig_nativeInstanceID = getFieldIDCheck(env, gBitmapConfig_class,
596                                                     "nativeInt", "I");
597
598    gCanvas_class = make_globalref(env, "android/graphics/Canvas");
599    gCanvas_nativeInstanceID = getFieldIDCheck(env, gCanvas_class, "mNativeCanvas", "I");
600
601    gPaint_class = make_globalref(env, "android/graphics/Paint");
602    gPaint_nativeInstanceID = getFieldIDCheck(env, gPaint_class, "mNativePaint", "I");
603
604    gPicture_class = make_globalref(env, "android/graphics/Picture");
605    gPicture_nativeInstanceID = getFieldIDCheck(env, gPicture_class, "mNativePicture", "I");
606
607    gRegion_class = make_globalref(env, "android/graphics/Region");
608    gRegion_nativeInstanceID = getFieldIDCheck(env, gRegion_class, "mNativeRegion", "I");
609    gRegion_constructorMethodID = env->GetMethodID(gRegion_class, "<init>",
610        "(II)V");
611
612    return 0;
613}
614