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