• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
doThrowNPE(JNIEnv * env)13 void doThrowNPE(JNIEnv* env) {
14     jniThrowNullPointerException(env, NULL);
15 }
16 
doThrowAIOOBE(JNIEnv * env)17 void doThrowAIOOBE(JNIEnv* env) {
18     jniThrowException(env, "java/lang/ArrayIndexOutOfBoundsException", NULL);
19 }
20 
doThrowRE(JNIEnv * env,const char * msg)21 void doThrowRE(JNIEnv* env, const char* msg) {
22     jniThrowRuntimeException(env, msg);
23 }
24 
doThrowIAE(JNIEnv * env,const char * msg)25 void doThrowIAE(JNIEnv* env, const char* msg) {
26     jniThrowException(env, "java/lang/IllegalArgumentException", msg);
27 }
28 
doThrowISE(JNIEnv * env,const char * msg)29 void doThrowISE(JNIEnv* env, const char* msg) {
30     jniThrowException(env, "java/lang/IllegalStateException", msg);
31 }
32 
doThrowOOME(JNIEnv * env,const char * msg)33 void doThrowOOME(JNIEnv* env, const char* msg) {
34     jniThrowException(env, "java/lang/OutOfMemoryError", msg);
35 }
36 
doThrowIOE(JNIEnv * env,const char * msg)37 void doThrowIOE(JNIEnv* env, const char* msg) {
38     jniThrowException(env, "java/io/IOException", msg);
39 }
40 
hasException(JNIEnv * env)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 
AutoJavaFloatArray(JNIEnv * env,jfloatArray array,int minLength,JNIAccess access)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 
~AutoJavaFloatArray()66 AutoJavaFloatArray::~AutoJavaFloatArray() {
67     if (fPtr) {
68         fEnv->ReleaseFloatArrayElements(fArray, fPtr, fReleaseMode);
69     }
70 }
71 
AutoJavaIntArray(JNIEnv * env,jintArray array,int minLength)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 
~AutoJavaIntArray()85 AutoJavaIntArray::~AutoJavaIntArray() {
86     if (fPtr) {
87         fEnv->ReleaseIntArrayElements(fArray, fPtr, 0);
88     }
89 }
90 
AutoJavaShortArray(JNIEnv * env,jshortArray array,int minLength,JNIAccess access)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 
~AutoJavaShortArray()105 AutoJavaShortArray::~AutoJavaShortArray() {
106     if (fPtr) {
107         fEnv->ReleaseShortArrayElements(fArray, fPtr, fReleaseMode);
108     }
109 }
110 
AutoJavaByteArray(JNIEnv * env,jbyteArray array,int minLength)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 
~AutoJavaByteArray()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 
156 static jclass   gBitmapConfig_class;
157 static jfieldID gBitmapConfig_nativeInstanceID;
158 
159 static jclass   gBitmapRegionDecoder_class;
160 static jmethodID gBitmapRegionDecoder_constructorMethodID;
161 
162 static jclass   gCanvas_class;
163 static jfieldID gCanvas_nativeInstanceID;
164 
165 static jclass   gPaint_class;
166 static jfieldID gPaint_nativeInstanceID;
167 
168 static jclass   gPicture_class;
169 static jfieldID gPicture_nativeInstanceID;
170 
171 static jclass   gRegion_class;
172 static jfieldID gRegion_nativeInstanceID;
173 static jmethodID gRegion_constructorMethodID;
174 
175 ///////////////////////////////////////////////////////////////////////////////
176 
get_jrect(JNIEnv * env,jobject obj,int * L,int * T,int * R,int * B)177 void 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 
set_jrect(JNIEnv * env,jobject obj,int L,int T,int R,int B)187 void 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 
jrect_to_irect(JNIEnv * env,jobject obj,SkIRect * ir)197 SkIRect* 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 
irect_to_jrect(const SkIRect & ir,JNIEnv * env,jobject obj)208 void 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 
jrectf_to_rect(JNIEnv * env,jobject obj,SkRect * r)218 SkRect* 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 
jrect_to_rect(JNIEnv * env,jobject obj,SkRect * r)229 SkRect* 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 
rect_to_jrectf(const SkRect & r,JNIEnv * env,jobject obj)240 void 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 
jpoint_to_ipoint(JNIEnv * env,jobject obj,SkIPoint * point)250 SkIPoint* 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 
ipoint_to_jpoint(const SkIPoint & ir,JNIEnv * env,jobject obj)259 void 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 
jpointf_to_point(JNIEnv * env,jobject obj,SkPoint * point)267 SkPoint* 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 
point_to_jpointf(const SkPoint & r,JNIEnv * env,jobject obj)276 void 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 
getNativeBitmap(JNIEnv * env,jobject bitmap)284 SkBitmap* 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 
getNativeBitmapConfig(JNIEnv * env,jobject jconfig)293 SkBitmap::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 
getNativeCanvas(JNIEnv * env,jobject canvas)307 SkCanvas* 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 
getNativePaint(JNIEnv * env,jobject paint)316 SkPaint* 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 
getNativePicture(JNIEnv * env,jobject picture)325 SkPicture* 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 
getNativeRegion(JNIEnv * env,jobject region)335 SkRegion* 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 
createBitmap(JNIEnv * env,SkBitmap * bitmap,jbyteArray buffer,bool isMutable,jbyteArray ninepatch,jintArray layoutbounds,int density)347 jobject 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 
createBitmap(JNIEnv * env,SkBitmap * bitmap,bool isMutable,jbyteArray ninepatch,int density)360 jobject 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 
createBitmapRegionDecoder(JNIEnv * env,SkBitmapRegionDecoder * bitmap)367 jobject 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 
createRegion(JNIEnv * env,SkRegion * region)378 jobject 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 
vm2env(JavaVM * vm)387 static 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 
AndroidPixelRef(JNIEnv * env,void * storage,size_t size,jbyteArray storageObj,SkColorTable * ctable)400 AndroidPixelRef::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 
~AndroidPixelRef()418 AndroidPixelRef::~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 
setLocalJNIRef(jbyteArray arr)433 void AndroidPixelRef::setLocalJNIRef(jbyteArray arr) {
434     if (!fHasGlobalRef) {
435         fStorageObj = arr;
436     }
437 }
438 
globalRef(void * localref)439 void 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 
globalUnref()463 void 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 
479 extern "C" jbyte* jniGetNonMovableArrayElements(C_JNIEnv* env, jarray arrayObj);
480 
allocateJavaPixelRef(JNIEnv * env,SkBitmap * bitmap,SkColorTable * ctable)481 jbyteArray 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 
JavaPixelAllocator(JNIEnv * env)509 JavaPixelAllocator::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 
allocPixelRef(SkBitmap * bitmap,SkColorTable * ctable)518 bool 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 
JavaHeapBitmapRef(JNIEnv * env,SkBitmap * nativeBitmap,jbyteArray buffer)528 JavaHeapBitmapRef::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 
~JavaHeapBitmapRef()539 JavaHeapBitmapRef::~JavaHeapBitmapRef() {
540     if (fBuffer) {
541         ((AndroidPixelRef*) fNativeBitmap->pixelRef())->setLocalJNIRef(NULL);
542     }
543 }
544 
545 ////////////////////////////////////////////////////////////////////////////////
546 
make_globalref(JNIEnv * env,const char classname[])547 static 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 
getFieldIDCheck(JNIEnv * env,jclass clazz,const char fieldname[],const char type[])554 static 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 
register_android_graphics_Graphics(JNIEnv * env)562 int 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