blob: 2883be9c80adfccbfd9e1c289f3e302cb737e3af [file] [log] [blame]
Ficus Kirkpatrick1a9c27c2010-03-05 17:05:08 -08001#define LOG_TAG "GraphicsJNI"
2
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003#include "jni.h"
Elliott Hughes8451b252011-04-07 19:17:57 -07004#include "JNIHelp.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005#include "GraphicsJNI.h"
Patrick Dubroye4ac2d62010-12-01 11:23:13 -08006
7#include "SkCanvas.h"
8#include "SkDevice.h"
Leon Scroggins46cb9bd2014-03-06 15:36:39 -05009#include "SkMath.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010#include "SkPicture.h"
11#include "SkRegion.h"
12#include <android_runtime/AndroidRuntime.h>
13
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014void doThrowNPE(JNIEnv* env) {
Elliott Hughes69a017b2011-04-08 14:10:28 -070015 jniThrowNullPointerException(env, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016}
17
18void doThrowAIOOBE(JNIEnv* env) {
Elliott Hughes8451b252011-04-07 19:17:57 -070019 jniThrowException(env, "java/lang/ArrayIndexOutOfBoundsException", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020}
21
22void doThrowRE(JNIEnv* env, const char* msg) {
Elliott Hughes69a017b2011-04-08 14:10:28 -070023 jniThrowRuntimeException(env, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024}
25
26void doThrowIAE(JNIEnv* env, const char* msg) {
Elliott Hughes8451b252011-04-07 19:17:57 -070027 jniThrowException(env, "java/lang/IllegalArgumentException", msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028}
29
30void doThrowISE(JNIEnv* env, const char* msg) {
Elliott Hughes8451b252011-04-07 19:17:57 -070031 jniThrowException(env, "java/lang/IllegalStateException", msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032}
33
34void doThrowOOME(JNIEnv* env, const char* msg) {
Elliott Hughes8451b252011-04-07 19:17:57 -070035 jniThrowException(env, "java/lang/OutOfMemoryError", msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036}
37
Joseph Wenf1f48bc2010-07-19 16:59:51 +080038void doThrowIOE(JNIEnv* env, const char* msg) {
Elliott Hughes8451b252011-04-07 19:17:57 -070039 jniThrowException(env, "java/io/IOException", msg);
Joseph Wenf1f48bc2010-07-19 16:59:51 +080040}
41
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042bool GraphicsJNI::hasException(JNIEnv *env) {
43 if (env->ExceptionCheck() != 0) {
Steve Block3762c312012-01-06 19:20:56 +000044 ALOGE("*** Uncaught exception returned from Java call!\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045 env->ExceptionDescribe();
46 return true;
47 }
48 return false;
49}
50
51///////////////////////////////////////////////////////////////////////////////
52
53AutoJavaFloatArray::AutoJavaFloatArray(JNIEnv* env, jfloatArray array,
Mike Reedc04851f2009-10-28 15:09:45 -040054 int minLength, JNIAccess access)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055: fEnv(env), fArray(array), fPtr(NULL), fLen(0) {
56 SkASSERT(env);
57 if (array) {
58 fLen = env->GetArrayLength(array);
59 if (fLen < minLength) {
60 sk_throw();
61 }
62 fPtr = env->GetFloatArrayElements(array, NULL);
63 }
Mike Reedc04851f2009-10-28 15:09:45 -040064 fReleaseMode = (access == kRO_JNIAccess) ? JNI_ABORT : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065}
66
67AutoJavaFloatArray::~AutoJavaFloatArray() {
68 if (fPtr) {
Mike Reedc04851f2009-10-28 15:09:45 -040069 fEnv->ReleaseFloatArrayElements(fArray, fPtr, fReleaseMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070 }
71}
72
73AutoJavaIntArray::AutoJavaIntArray(JNIEnv* env, jintArray array,
74 int minLength)
75: fEnv(env), fArray(array), fPtr(NULL), fLen(0) {
76 SkASSERT(env);
77 if (array) {
78 fLen = env->GetArrayLength(array);
79 if (fLen < minLength) {
80 sk_throw();
81 }
82 fPtr = env->GetIntArrayElements(array, NULL);
83 }
84}
85
86AutoJavaIntArray::~AutoJavaIntArray() {
87 if (fPtr) {
88 fEnv->ReleaseIntArrayElements(fArray, fPtr, 0);
89 }
90}
91
92AutoJavaShortArray::AutoJavaShortArray(JNIEnv* env, jshortArray array,
Mike Reedc04851f2009-10-28 15:09:45 -040093 int minLength, JNIAccess access)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094: fEnv(env), fArray(array), fPtr(NULL), fLen(0) {
95 SkASSERT(env);
96 if (array) {
97 fLen = env->GetArrayLength(array);
98 if (fLen < minLength) {
99 sk_throw();
100 }
101 fPtr = env->GetShortArrayElements(array, NULL);
102 }
Mike Reedc04851f2009-10-28 15:09:45 -0400103 fReleaseMode = (access == kRO_JNIAccess) ? JNI_ABORT : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104}
105
106AutoJavaShortArray::~AutoJavaShortArray() {
107 if (fPtr) {
Mike Reedc04851f2009-10-28 15:09:45 -0400108 fEnv->ReleaseShortArrayElements(fArray, fPtr, fReleaseMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 }
110}
111
112AutoJavaByteArray::AutoJavaByteArray(JNIEnv* env, jbyteArray array,
113 int minLength)
114: fEnv(env), fArray(array), fPtr(NULL), fLen(0) {
115 SkASSERT(env);
116 if (array) {
117 fLen = env->GetArrayLength(array);
118 if (fLen < minLength) {
119 sk_throw();
120 }
121 fPtr = env->GetByteArrayElements(array, NULL);
122 }
123}
124
125AutoJavaByteArray::~AutoJavaByteArray() {
126 if (fPtr) {
127 fEnv->ReleaseByteArrayElements(fArray, fPtr, 0);
128 }
129}
130
131///////////////////////////////////////////////////////////////////////////////
132
133static jclass gRect_class;
134static jfieldID gRect_leftFieldID;
135static jfieldID gRect_topFieldID;
136static jfieldID gRect_rightFieldID;
137static jfieldID gRect_bottomFieldID;
138
139static jclass gRectF_class;
140static jfieldID gRectF_leftFieldID;
141static jfieldID gRectF_topFieldID;
142static jfieldID gRectF_rightFieldID;
143static jfieldID gRectF_bottomFieldID;
144
145static jclass gPoint_class;
146static jfieldID gPoint_xFieldID;
147static jfieldID gPoint_yFieldID;
148
149static jclass gPointF_class;
150static jfieldID gPointF_xFieldID;
151static jfieldID gPointF_yFieldID;
152
153static jclass gBitmap_class;
154static jfieldID gBitmap_nativeInstanceID;
155static jmethodID gBitmap_constructorMethodID;
Chris Craik9f583612013-05-20 18:13:47 -0700156static jmethodID gBitmap_reinitMethodID;
157static jmethodID gBitmap_getAllocationByteCountMethodID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158
159static jclass gBitmapConfig_class;
160static jfieldID gBitmapConfig_nativeInstanceID;
161
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800162static jclass gBitmapRegionDecoder_class;
163static jmethodID gBitmapRegionDecoder_constructorMethodID;
Joseph Wenf1f48bc2010-07-19 16:59:51 +0800164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165static jclass gCanvas_class;
166static jfieldID gCanvas_nativeInstanceID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167
168static jclass gPaint_class;
169static jfieldID gPaint_nativeInstanceID;
170
171static jclass gPicture_class;
172static jfieldID gPicture_nativeInstanceID;
173
174static jclass gRegion_class;
175static jfieldID gRegion_nativeInstanceID;
176static jmethodID gRegion_constructorMethodID;
177
Mathieu Chartier7384b422013-10-17 18:16:42 -0700178static jclass gByte_class;
179static jobject gVMRuntime;
180static jclass gVMRuntime_class;
181static jmethodID gVMRuntime_newNonMovableArray;
182static jmethodID gVMRuntime_addressOf;
183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184///////////////////////////////////////////////////////////////////////////////
185
186void GraphicsJNI::get_jrect(JNIEnv* env, jobject obj, int* L, int* T, int* R, int* B)
187{
188 SkASSERT(env->IsInstanceOf(obj, gRect_class));
189
190 *L = env->GetIntField(obj, gRect_leftFieldID);
191 *T = env->GetIntField(obj, gRect_topFieldID);
192 *R = env->GetIntField(obj, gRect_rightFieldID);
193 *B = env->GetIntField(obj, gRect_bottomFieldID);
194}
195
196void GraphicsJNI::set_jrect(JNIEnv* env, jobject obj, int L, int T, int R, int B)
197{
198 SkASSERT(env->IsInstanceOf(obj, gRect_class));
199
200 env->SetIntField(obj, gRect_leftFieldID, L);
201 env->SetIntField(obj, gRect_topFieldID, T);
202 env->SetIntField(obj, gRect_rightFieldID, R);
203 env->SetIntField(obj, gRect_bottomFieldID, B);
204}
205
206SkIRect* GraphicsJNI::jrect_to_irect(JNIEnv* env, jobject obj, SkIRect* ir)
207{
208 SkASSERT(env->IsInstanceOf(obj, gRect_class));
209
210 ir->set(env->GetIntField(obj, gRect_leftFieldID),
211 env->GetIntField(obj, gRect_topFieldID),
212 env->GetIntField(obj, gRect_rightFieldID),
213 env->GetIntField(obj, gRect_bottomFieldID));
214 return ir;
215}
216
217void GraphicsJNI::irect_to_jrect(const SkIRect& ir, JNIEnv* env, jobject obj)
218{
219 SkASSERT(env->IsInstanceOf(obj, gRect_class));
220
221 env->SetIntField(obj, gRect_leftFieldID, ir.fLeft);
222 env->SetIntField(obj, gRect_topFieldID, ir.fTop);
223 env->SetIntField(obj, gRect_rightFieldID, ir.fRight);
224 env->SetIntField(obj, gRect_bottomFieldID, ir.fBottom);
225}
226
227SkRect* GraphicsJNI::jrectf_to_rect(JNIEnv* env, jobject obj, SkRect* r)
228{
229 SkASSERT(env->IsInstanceOf(obj, gRectF_class));
Elliott Hughes8451b252011-04-07 19:17:57 -0700230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 r->set(SkFloatToScalar(env->GetFloatField(obj, gRectF_leftFieldID)),
232 SkFloatToScalar(env->GetFloatField(obj, gRectF_topFieldID)),
233 SkFloatToScalar(env->GetFloatField(obj, gRectF_rightFieldID)),
234 SkFloatToScalar(env->GetFloatField(obj, gRectF_bottomFieldID)));
235 return r;
236}
237
238SkRect* GraphicsJNI::jrect_to_rect(JNIEnv* env, jobject obj, SkRect* r)
239{
240 SkASSERT(env->IsInstanceOf(obj, gRect_class));
Elliott Hughes8451b252011-04-07 19:17:57 -0700241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 r->set(SkIntToScalar(env->GetIntField(obj, gRect_leftFieldID)),
243 SkIntToScalar(env->GetIntField(obj, gRect_topFieldID)),
244 SkIntToScalar(env->GetIntField(obj, gRect_rightFieldID)),
245 SkIntToScalar(env->GetIntField(obj, gRect_bottomFieldID)));
246 return r;
247}
248
249void GraphicsJNI::rect_to_jrectf(const SkRect& r, JNIEnv* env, jobject obj)
250{
251 SkASSERT(env->IsInstanceOf(obj, gRectF_class));
252
253 env->SetFloatField(obj, gRectF_leftFieldID, SkScalarToFloat(r.fLeft));
254 env->SetFloatField(obj, gRectF_topFieldID, SkScalarToFloat(r.fTop));
255 env->SetFloatField(obj, gRectF_rightFieldID, SkScalarToFloat(r.fRight));
256 env->SetFloatField(obj, gRectF_bottomFieldID, SkScalarToFloat(r.fBottom));
257}
258
259SkIPoint* GraphicsJNI::jpoint_to_ipoint(JNIEnv* env, jobject obj, SkIPoint* point)
260{
261 SkASSERT(env->IsInstanceOf(obj, gPoint_class));
Elliott Hughes8451b252011-04-07 19:17:57 -0700262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 point->set(env->GetIntField(obj, gPoint_xFieldID),
264 env->GetIntField(obj, gPoint_yFieldID));
265 return point;
266}
267
268void GraphicsJNI::ipoint_to_jpoint(const SkIPoint& ir, JNIEnv* env, jobject obj)
269{
270 SkASSERT(env->IsInstanceOf(obj, gPoint_class));
271
272 env->SetIntField(obj, gPoint_xFieldID, ir.fX);
273 env->SetIntField(obj, gPoint_yFieldID, ir.fY);
274}
275
276SkPoint* GraphicsJNI::jpointf_to_point(JNIEnv* env, jobject obj, SkPoint* point)
277{
278 SkASSERT(env->IsInstanceOf(obj, gPointF_class));
Elliott Hughes8451b252011-04-07 19:17:57 -0700279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 point->set(SkFloatToScalar(env->GetIntField(obj, gPointF_xFieldID)),
281 SkFloatToScalar(env->GetIntField(obj, gPointF_yFieldID)));
282 return point;
283}
284
285void GraphicsJNI::point_to_jpointf(const SkPoint& r, JNIEnv* env, jobject obj)
286{
287 SkASSERT(env->IsInstanceOf(obj, gPointF_class));
288
289 env->SetFloatField(obj, gPointF_xFieldID, SkScalarToFloat(r.fX));
290 env->SetFloatField(obj, gPointF_yFieldID, SkScalarToFloat(r.fY));
291}
292
293SkBitmap* GraphicsJNI::getNativeBitmap(JNIEnv* env, jobject bitmap) {
294 SkASSERT(env);
295 SkASSERT(bitmap);
296 SkASSERT(env->IsInstanceOf(bitmap, gBitmap_class));
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000297 jlong bitmapHandle = env->GetLongField(bitmap, gBitmap_nativeInstanceID);
298 SkBitmap* b = reinterpret_cast<SkBitmap*>(bitmapHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 SkASSERT(b);
300 return b;
301}
302
303SkBitmap::Config GraphicsJNI::getNativeBitmapConfig(JNIEnv* env,
304 jobject jconfig) {
305 SkASSERT(env);
306 if (NULL == jconfig) {
307 return SkBitmap::kNo_Config;
308 }
309 SkASSERT(env->IsInstanceOf(jconfig, gBitmapConfig_class));
310 int c = env->GetIntField(jconfig, gBitmapConfig_nativeInstanceID);
311 if (c < 0 || c >= SkBitmap::kConfigCount) {
312 c = SkBitmap::kNo_Config;
313 }
314 return static_cast<SkBitmap::Config>(c);
315}
316
317SkCanvas* GraphicsJNI::getNativeCanvas(JNIEnv* env, jobject canvas) {
318 SkASSERT(env);
319 SkASSERT(canvas);
320 SkASSERT(env->IsInstanceOf(canvas, gCanvas_class));
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000321 jlong canvasHandle = env->GetLongField(canvas, gCanvas_nativeInstanceID);
322 SkCanvas* c = reinterpret_cast<SkCanvas*>(canvasHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 SkASSERT(c);
324 return c;
325}
326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327SkPaint* GraphicsJNI::getNativePaint(JNIEnv* env, jobject paint) {
328 SkASSERT(env);
329 SkASSERT(paint);
330 SkASSERT(env->IsInstanceOf(paint, gPaint_class));
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000331 jlong paintHandle = env->GetLongField(paint, gPaint_nativeInstanceID);
332 SkPaint* p = reinterpret_cast<SkPaint*>(paintHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 SkASSERT(p);
334 return p;
335}
336
337SkPicture* GraphicsJNI::getNativePicture(JNIEnv* env, jobject picture)
338{
339 SkASSERT(env);
340 SkASSERT(picture);
341 SkASSERT(env->IsInstanceOf(picture, gPicture_class));
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000342 jlong pictureHandle = env->GetLongField(picture, gPicture_nativeInstanceID);
343 SkPicture* p = reinterpret_cast<SkPicture*>(pictureHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 SkASSERT(p);
345 return p;
346}
347
348SkRegion* GraphicsJNI::getNativeRegion(JNIEnv* env, jobject region)
349{
350 SkASSERT(env);
351 SkASSERT(region);
352 SkASSERT(env->IsInstanceOf(region, gRegion_class));
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000353 jlong regionHandle = env->GetLongField(region, gRegion_nativeInstanceID);
354 SkRegion* r = reinterpret_cast<SkRegion*>(regionHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 SkASSERT(r);
356 return r;
357}
358
359///////////////////////////////////////////////////////////////////////////////////////////
360
Leon Scroggins III8790be62013-12-03 16:26:51 -0500361// Assert that bitmap's SkAlphaType is consistent with isPremultiplied.
362static void assert_premultiplied(const SkBitmap& bitmap, bool isPremultiplied) {
363 // kOpaque_SkAlphaType and kIgnore_SkAlphaType mean that isPremultiplied is
364 // irrelevant. This just tests to ensure that the SkAlphaType is not
365 // opposite of isPremultiplied.
366 if (isPremultiplied) {
367 SkASSERT(bitmap.alphaType() != kUnpremul_SkAlphaType);
368 } else {
369 SkASSERT(bitmap.alphaType() != kPremul_SkAlphaType);
370 }
371}
372
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800373jobject GraphicsJNI::createBitmap(JNIEnv* env, SkBitmap* bitmap, jbyteArray buffer,
Chris Craik1abf5d62013-08-16 12:47:03 -0700374 int bitmapCreateFlags, jbyteArray ninepatch, jintArray layoutbounds, int density)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375{
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800376 SkASSERT(bitmap);
377 SkASSERT(bitmap->pixelRef());
Chris Craik1abf5d62013-08-16 12:47:03 -0700378 bool isMutable = bitmapCreateFlags & kBitmapCreateFlag_Mutable;
379 bool isPremultiplied = bitmapCreateFlags & kBitmapCreateFlag_Premultiplied;
380
Leon Scroggins III8790be62013-12-03 16:26:51 -0500381 // The caller needs to have already set the alpha type properly, so the
382 // native SkBitmap stays in sync with the Java Bitmap.
383 assert_premultiplied(*bitmap, isPremultiplied);
384
Elliott Hughescf6f7a02011-04-12 17:50:45 -0700385 jobject obj = env->NewObject(gBitmap_class, gBitmap_constructorMethodID,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000386 reinterpret_cast<jlong>(bitmap), buffer,
Chris Craik1abf5d62013-08-16 12:47:03 -0700387 bitmap->width(), bitmap->height(), density, isMutable, isPremultiplied,
388 ninepatch, layoutbounds);
Elliott Hughescf6f7a02011-04-12 17:50:45 -0700389 hasException(env); // For the side effect of logging.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 return obj;
391}
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800392
Chris Craik1abf5d62013-08-16 12:47:03 -0700393jobject GraphicsJNI::createBitmap(JNIEnv* env, SkBitmap* bitmap, int bitmapCreateFlags,
394 jbyteArray ninepatch, int density)
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800395{
Chris Craik1abf5d62013-08-16 12:47:03 -0700396 return createBitmap(env, bitmap, NULL, bitmapCreateFlags, ninepatch, NULL, density);
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800397}
398
Chris Craik1abf5d62013-08-16 12:47:03 -0700399void GraphicsJNI::reinitBitmap(JNIEnv* env, jobject javaBitmap, SkBitmap* bitmap,
400 bool isPremultiplied)
Chris Craik9f583612013-05-20 18:13:47 -0700401{
Leon Scroggins III8790be62013-12-03 16:26:51 -0500402 // The caller needs to have already set the alpha type properly, so the
403 // native SkBitmap stays in sync with the Java Bitmap.
404 assert_premultiplied(*bitmap, isPremultiplied);
405
Chris Craik1abf5d62013-08-16 12:47:03 -0700406 env->CallVoidMethod(javaBitmap, gBitmap_reinitMethodID,
407 bitmap->width(), bitmap->height(), isPremultiplied);
Chris Craik9f583612013-05-20 18:13:47 -0700408}
409
410int GraphicsJNI::getBitmapAllocationByteCount(JNIEnv* env, jobject javaBitmap)
411{
412 return env->CallIntMethod(javaBitmap, gBitmap_getAllocationByteCountMethodID);
413}
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800414
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800415jobject GraphicsJNI::createBitmapRegionDecoder(JNIEnv* env, SkBitmapRegionDecoder* bitmap)
Joseph Wenf1f48bc2010-07-19 16:59:51 +0800416{
417 SkASSERT(bitmap != NULL);
418
Elliott Hughescf6f7a02011-04-12 17:50:45 -0700419 jobject obj = env->NewObject(gBitmapRegionDecoder_class,
420 gBitmapRegionDecoder_constructorMethodID,
Ashok Bhatb091d472014-01-08 14:32:49 +0000421 reinterpret_cast<jlong>(bitmap));
Elliott Hughescf6f7a02011-04-12 17:50:45 -0700422 hasException(env); // For the side effect of logging.
Joseph Wenf1f48bc2010-07-19 16:59:51 +0800423 return obj;
424}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425
426jobject GraphicsJNI::createRegion(JNIEnv* env, SkRegion* region)
427{
428 SkASSERT(region != NULL);
Elliott Hughescf6f7a02011-04-12 17:50:45 -0700429 jobject obj = env->NewObject(gRegion_class, gRegion_constructorMethodID,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000430 reinterpret_cast<jlong>(region), 0);
Elliott Hughescf6f7a02011-04-12 17:50:45 -0700431 hasException(env); // For the side effect of logging.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 return obj;
433}
434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435static JNIEnv* vm2env(JavaVM* vm)
436{
437 JNIEnv* env = NULL;
438 if (vm->GetEnv((void**)&env, JNI_VERSION_1_4) != JNI_OK || NULL == env)
439 {
440 SkDebugf("------- [%p] vm->GetEnv() failed\n", vm);
441 sk_throw();
442 }
443 return env;
444}
445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446///////////////////////////////////////////////////////////////////////////////
447
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500448AndroidPixelRef::AndroidPixelRef(JNIEnv* env, const SkImageInfo& info, void* storage,
449 size_t rowBytes, jbyteArray storageObj, SkColorTable* ctable) :
450 SkMallocPixelRef(info, storage, rowBytes, ctable, (storageObj == NULL)),
Chris Craikcd0ba712013-09-06 14:40:30 -0700451 fWrappedPixelRef(NULL) {
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800452 SkASSERT(storage);
453 SkASSERT(env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800455 if (env->GetJavaVM(&fVM) != JNI_OK) {
456 SkDebugf("------ [%p] env->GetJavaVM failed\n", env);
457 sk_throw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 }
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800459 fStorageObj = storageObj;
460 fHasGlobalRef = false;
461 fGlobalRefCnt = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800463 // If storageObj is NULL, the memory was NOT allocated on the Java heap
464 fOnJavaHeap = (storageObj != NULL);
Elliott Hughes8451b252011-04-07 19:17:57 -0700465
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800466}
467
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500468AndroidPixelRef::AndroidPixelRef(AndroidPixelRef& wrappedPixelRef, const SkImageInfo& info,
469 size_t rowBytes, SkColorTable* ctable) :
470 SkMallocPixelRef(info, wrappedPixelRef.getAddr(), rowBytes, ctable, false),
Chris Craikcd0ba712013-09-06 14:40:30 -0700471 fWrappedPixelRef(wrappedPixelRef.fWrappedPixelRef ?
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500472 wrappedPixelRef.fWrappedPixelRef : &wrappedPixelRef)
Chris Craikcd0ba712013-09-06 14:40:30 -0700473{
474 SkASSERT(fWrappedPixelRef);
475 SkSafeRef(fWrappedPixelRef);
476
477 // don't need to initialize these, as all the relevant logic delegates to the wrapped ref
478 fStorageObj = NULL;
479 fHasGlobalRef = false;
480 fGlobalRefCnt = 0;
481 fOnJavaHeap = false;
482}
483
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800484AndroidPixelRef::~AndroidPixelRef() {
Chris Craikcd0ba712013-09-06 14:40:30 -0700485 if (fWrappedPixelRef) {
486 SkSafeUnref(fWrappedPixelRef);
487 } else if (fOnJavaHeap) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 JNIEnv* env = vm2env(fVM);
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800489
490 if (fStorageObj && fHasGlobalRef) {
491 env->DeleteGlobalRef(fStorageObj);
492 }
493 fStorageObj = NULL;
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800494 }
495}
Chris Craikcd0ba712013-09-06 14:40:30 -0700496jbyteArray AndroidPixelRef::getStorageObj() {
497 if (fWrappedPixelRef) {
498 return fWrappedPixelRef->fStorageObj;
499 }
500 return fStorageObj;
501}
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800502
503void AndroidPixelRef::setLocalJNIRef(jbyteArray arr) {
Chris Craikcd0ba712013-09-06 14:40:30 -0700504 if (fWrappedPixelRef) {
505 // delegate java obj management to the wrapped ref
506 fWrappedPixelRef->setLocalJNIRef(arr);
507 } else if (!fHasGlobalRef) {
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800508 fStorageObj = arr;
509 }
510}
511
Patrick Dubroya2f0e2d2010-12-15 15:03:47 -0800512void AndroidPixelRef::globalRef(void* localref) {
Chris Craikcd0ba712013-09-06 14:40:30 -0700513 if (fWrappedPixelRef) {
514 // delegate java obj management to the wrapped ref
515 fWrappedPixelRef->globalRef(localref);
Chris Craik28a12222013-09-10 17:18:04 -0700516
517 // Note: we only ref and unref the wrapped AndroidPixelRef so that
518 // bitmap->pixelRef()->globalRef() and globalUnref() can be used in a pair, even if
519 // the bitmap has its underlying AndroidPixelRef swapped out/wrapped
520 return;
521 }
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800522 if (fOnJavaHeap && sk_atomic_inc(&fGlobalRefCnt) == 0) {
523 JNIEnv *env = vm2env(fVM);
Patrick Dubroya2f0e2d2010-12-15 15:03:47 -0800524
525 // If JNI ref was passed, it is always used
526 if (localref) fStorageObj = (jbyteArray) localref;
527
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800528 if (fStorageObj == NULL) {
Patrick Dubroya2f0e2d2010-12-15 15:03:47 -0800529 SkDebugf("No valid local ref to create a JNI global ref\n");
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800530 sk_throw();
531 }
532 if (fHasGlobalRef) {
533 // This should never happen
Patrick Dubroya2f0e2d2010-12-15 15:03:47 -0800534 SkDebugf("Already holding a JNI global ref");
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800535 sk_throw();
536 }
537
538 fStorageObj = (jbyteArray) env->NewGlobalRef(fStorageObj);
539 // TODO: Check for failure here
540 fHasGlobalRef = true;
541 }
542 ref();
543}
544
545void AndroidPixelRef::globalUnref() {
Chris Craikcd0ba712013-09-06 14:40:30 -0700546 if (fWrappedPixelRef) {
547 // delegate java obj management to the wrapped ref
548 fWrappedPixelRef->globalUnref();
Chris Craik28a12222013-09-10 17:18:04 -0700549 return;
550 }
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800551 if (fOnJavaHeap && sk_atomic_dec(&fGlobalRefCnt) == 1) {
552 JNIEnv *env = vm2env(fVM);
553 if (!fHasGlobalRef) {
554 SkDebugf("We don't have a global ref!");
555 sk_throw();
556 }
557 env->DeleteGlobalRef(fStorageObj);
558 fStorageObj = NULL;
559 fHasGlobalRef = false;
560 }
561 unref();
562}
563
564///////////////////////////////////////////////////////////////////////////////
565
566jbyteArray GraphicsJNI::allocateJavaPixelRef(JNIEnv* env, SkBitmap* bitmap,
567 SkColorTable* ctable) {
Leon Scroggins46cb9bd2014-03-06 15:36:39 -0500568 const SkImageInfo& info = bitmap->info();
569 if (info.fColorType == kUnknown_SkColorType) {
570 doThrowIAE(env, "unknown bitmap configuration");
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800571 return NULL;
572 }
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500573
Leon Scroggins46cb9bd2014-03-06 15:36:39 -0500574 const int64_t size64 = info.getSafeSize64(bitmap->rowBytes());
575 if (!sk_64_isS32(size64)) {
576 doThrowIAE(env, "bitmap size exceeds 32bits");
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500577 return NULL;
578 }
Leon Scroggins46cb9bd2014-03-06 15:36:39 -0500579 const size_t size = sk_64_asS32(size64);
580 SkASSERT(size == info.getSafeSize(bitmap->rowBytes()));
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500581
Mathieu Chartier7384b422013-10-17 18:16:42 -0700582 jbyteArray arrayObj = (jbyteArray) env->CallObjectMethod(gVMRuntime,
583 gVMRuntime_newNonMovableArray,
584 gByte_class, size);
Mathieu Chartiera1a19d22013-12-04 17:32:43 -0800585 if (env->ExceptionCheck() != 0) {
586 return NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 }
Mathieu Chartiera1a19d22013-12-04 17:32:43 -0800588 SkASSERT(arrayObj);
589 jbyte* addr = (jbyte*) env->CallLongMethod(gVMRuntime, gVMRuntime_addressOf, arrayObj);
590 if (env->ExceptionCheck() != 0) {
591 return NULL;
592 }
593 SkASSERT(addr);
Leon Scroggins46cb9bd2014-03-06 15:36:39 -0500594 SkPixelRef* pr = new AndroidPixelRef(env, info, (void*) addr,
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500595 bitmap->rowBytes(), arrayObj, ctable);
Mathieu Chartiera1a19d22013-12-04 17:32:43 -0800596 bitmap->setPixelRef(pr)->unref();
597 // since we're already allocated, we lockPixels right away
598 // HeapAllocator behaves this way too
599 bitmap->lockPixels();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800601 return arrayObj;
602}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604///////////////////////////////////////////////////////////////////////////////
605
Carl Shapiro2118b252010-12-17 18:03:38 -0800606JavaPixelAllocator::JavaPixelAllocator(JNIEnv* env)
607 : fStorageObj(NULL),
Patrick Dubroyafde46e2010-12-15 11:52:01 -0800608 fAllocCount(0) {
Wei-Ta Chen291303b2010-08-18 15:40:29 +0800609 if (env->GetJavaVM(&fVM) != JNI_OK) {
610 SkDebugf("------ [%p] env->GetJavaVM failed\n", env);
611 sk_throw();
612 }
613}
Elliott Hughes8451b252011-04-07 19:17:57 -0700614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615bool JavaPixelAllocator::allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable) {
Wei-Ta Chen291303b2010-08-18 15:40:29 +0800616 JNIEnv* env = vm2env(fVM);
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800617
Carl Shapiro2118b252010-12-17 18:03:38 -0800618 fStorageObj = GraphicsJNI::allocateJavaPixelRef(env, bitmap, ctable);
619 fAllocCount += 1;
620 return fStorageObj != NULL;
Joseph Wenf1f48bc2010-07-19 16:59:51 +0800621}
622
623////////////////////////////////////////////////////////////////////////////////
624
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800625JavaHeapBitmapRef::JavaHeapBitmapRef(JNIEnv* env, SkBitmap* nativeBitmap, jbyteArray buffer) {
626 fEnv = env;
627 fNativeBitmap = nativeBitmap;
628 fBuffer = buffer;
629
630 // If the buffer is NULL, the backing memory wasn't allocated on the Java heap
631 if (fBuffer) {
632 ((AndroidPixelRef*) fNativeBitmap->pixelRef())->setLocalJNIRef(fBuffer);
633 }
634}
635
636JavaHeapBitmapRef::~JavaHeapBitmapRef() {
637 if (fBuffer) {
638 ((AndroidPixelRef*) fNativeBitmap->pixelRef())->setLocalJNIRef(NULL);
639 }
640}
641
642////////////////////////////////////////////////////////////////////////////////
643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644static jclass make_globalref(JNIEnv* env, const char classname[])
645{
646 jclass c = env->FindClass(classname);
647 SkASSERT(c);
Mathieu Chartier6ecb7a92013-10-18 11:04:11 -0700648 return (jclass) env->NewGlobalRef(c);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649}
650
651static jfieldID getFieldIDCheck(JNIEnv* env, jclass clazz,
652 const char fieldname[], const char type[])
653{
654 jfieldID id = env->GetFieldID(clazz, fieldname, type);
655 SkASSERT(id);
656 return id;
657}
658
659int register_android_graphics_Graphics(JNIEnv* env)
660{
661 jmethodID m;
662 jclass c;
663
664 gRect_class = make_globalref(env, "android/graphics/Rect");
665 gRect_leftFieldID = getFieldIDCheck(env, gRect_class, "left", "I");
666 gRect_topFieldID = getFieldIDCheck(env, gRect_class, "top", "I");
667 gRect_rightFieldID = getFieldIDCheck(env, gRect_class, "right", "I");
668 gRect_bottomFieldID = getFieldIDCheck(env, gRect_class, "bottom", "I");
669
670 gRectF_class = make_globalref(env, "android/graphics/RectF");
671 gRectF_leftFieldID = getFieldIDCheck(env, gRectF_class, "left", "F");
672 gRectF_topFieldID = getFieldIDCheck(env, gRectF_class, "top", "F");
673 gRectF_rightFieldID = getFieldIDCheck(env, gRectF_class, "right", "F");
674 gRectF_bottomFieldID = getFieldIDCheck(env, gRectF_class, "bottom", "F");
675
676 gPoint_class = make_globalref(env, "android/graphics/Point");
677 gPoint_xFieldID = getFieldIDCheck(env, gPoint_class, "x", "I");
678 gPoint_yFieldID = getFieldIDCheck(env, gPoint_class, "y", "I");
679
680 gPointF_class = make_globalref(env, "android/graphics/PointF");
681 gPointF_xFieldID = getFieldIDCheck(env, gPointF_class, "x", "F");
682 gPointF_yFieldID = getFieldIDCheck(env, gPointF_class, "y", "F");
683
684 gBitmap_class = make_globalref(env, "android/graphics/Bitmap");
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000685 gBitmap_nativeInstanceID = getFieldIDCheck(env, gBitmap_class, "mNativeBitmap", "J");
686 gBitmap_constructorMethodID = env->GetMethodID(gBitmap_class, "<init>", "(J[BIIIZZ[B[I)V");
Chris Craik1abf5d62013-08-16 12:47:03 -0700687 gBitmap_reinitMethodID = env->GetMethodID(gBitmap_class, "reinit", "(IIZ)V");
Chris Craik9f583612013-05-20 18:13:47 -0700688 gBitmap_getAllocationByteCountMethodID = env->GetMethodID(gBitmap_class, "getAllocationByteCount", "()I");
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800689 gBitmapRegionDecoder_class = make_globalref(env, "android/graphics/BitmapRegionDecoder");
Ashok Bhatb091d472014-01-08 14:32:49 +0000690 gBitmapRegionDecoder_constructorMethodID = env->GetMethodID(gBitmapRegionDecoder_class, "<init>", "(J)V");
Joseph Wenf1f48bc2010-07-19 16:59:51 +0800691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 gBitmapConfig_class = make_globalref(env, "android/graphics/Bitmap$Config");
693 gBitmapConfig_nativeInstanceID = getFieldIDCheck(env, gBitmapConfig_class,
Elliott Hughes8451b252011-04-07 19:17:57 -0700694 "nativeInt", "I");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695
696 gCanvas_class = make_globalref(env, "android/graphics/Canvas");
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000697 gCanvas_nativeInstanceID = getFieldIDCheck(env, gCanvas_class, "mNativeCanvas", "J");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698
699 gPaint_class = make_globalref(env, "android/graphics/Paint");
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000700 gPaint_nativeInstanceID = getFieldIDCheck(env, gPaint_class, "mNativePaint", "J");
Elliott Hughes8451b252011-04-07 19:17:57 -0700701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 gPicture_class = make_globalref(env, "android/graphics/Picture");
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000703 gPicture_nativeInstanceID = getFieldIDCheck(env, gPicture_class, "mNativePicture", "J");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704
705 gRegion_class = make_globalref(env, "android/graphics/Region");
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000706 gRegion_nativeInstanceID = getFieldIDCheck(env, gRegion_class, "mNativeRegion", "J");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 gRegion_constructorMethodID = env->GetMethodID(gRegion_class, "<init>",
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000708 "(JI)V");
Elliott Hughes8451b252011-04-07 19:17:57 -0700709
Mathieu Chartier7384b422013-10-17 18:16:42 -0700710 c = env->FindClass("java/lang/Byte");
Mathieu Chartier6ecb7a92013-10-18 11:04:11 -0700711 gByte_class = (jclass) env->NewGlobalRef(
Mathieu Chartier7384b422013-10-17 18:16:42 -0700712 env->GetStaticObjectField(c, env->GetStaticFieldID(c, "TYPE", "Ljava/lang/Class;")));
713
714 gVMRuntime_class = make_globalref(env, "dalvik/system/VMRuntime");
715 m = env->GetStaticMethodID(gVMRuntime_class, "getRuntime", "()Ldalvik/system/VMRuntime;");
716 gVMRuntime = env->NewGlobalRef(env->CallStaticObjectMethod(gVMRuntime_class, m));
717 gVMRuntime_newNonMovableArray = env->GetMethodID(gVMRuntime_class, "newNonMovableArray",
718 "(Ljava/lang/Class;I)Ljava/lang/Object;");
719 gVMRuntime_addressOf = env->GetMethodID(gVMRuntime_class, "addressOf", "(Ljava/lang/Object;)J");
720
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721 return 0;
722}