Ficus Kirkpatrick | 1a9c27c | 2010-03-05 17:05:08 -0800 | [diff] [blame] | 1 | #define LOG_TAG "GraphicsJNI" |
| 2 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3 | #include "jni.h" |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 4 | #include "JNIHelp.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5 | #include "GraphicsJNI.h" |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 6 | |
| 7 | #include "SkCanvas.h" |
| 8 | #include "SkDevice.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9 | #include "SkPicture.h" |
| 10 | #include "SkRegion.h" |
| 11 | #include <android_runtime/AndroidRuntime.h> |
| 12 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13 | void doThrowNPE(JNIEnv* env) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 14 | jniThrowNullPointerException(env, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 15 | } |
| 16 | |
| 17 | void doThrowAIOOBE(JNIEnv* env) { |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 18 | jniThrowException(env, "java/lang/ArrayIndexOutOfBoundsException", NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | void doThrowRE(JNIEnv* env, const char* msg) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 22 | jniThrowRuntimeException(env, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | void doThrowIAE(JNIEnv* env, const char* msg) { |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 26 | jniThrowException(env, "java/lang/IllegalArgumentException", msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | void doThrowISE(JNIEnv* env, const char* msg) { |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 30 | jniThrowException(env, "java/lang/IllegalStateException", msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | void doThrowOOME(JNIEnv* env, const char* msg) { |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 34 | jniThrowException(env, "java/lang/OutOfMemoryError", msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | } |
| 36 | |
Joseph Wen | f1f48bc | 2010-07-19 16:59:51 +0800 | [diff] [blame] | 37 | void doThrowIOE(JNIEnv* env, const char* msg) { |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 38 | jniThrowException(env, "java/io/IOException", msg); |
Joseph Wen | f1f48bc | 2010-07-19 16:59:51 +0800 | [diff] [blame] | 39 | } |
| 40 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | bool GraphicsJNI::hasException(JNIEnv *env) { |
| 42 | if (env->ExceptionCheck() != 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 43 | ALOGE("*** Uncaught exception returned from Java call!\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | env->ExceptionDescribe(); |
| 45 | return true; |
| 46 | } |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | /////////////////////////////////////////////////////////////////////////////// |
| 51 | |
| 52 | AutoJavaFloatArray::AutoJavaFloatArray(JNIEnv* env, jfloatArray array, |
Mike Reed | c04851f | 2009-10-28 15:09:45 -0400 | [diff] [blame] | 53 | int minLength, JNIAccess access) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | } |
Mike Reed | c04851f | 2009-10-28 15:09:45 -0400 | [diff] [blame] | 63 | fReleaseMode = (access == kRO_JNIAccess) ? JNI_ABORT : 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | AutoJavaFloatArray::~AutoJavaFloatArray() { |
| 67 | if (fPtr) { |
Mike Reed | c04851f | 2009-10-28 15:09:45 -0400 | [diff] [blame] | 68 | fEnv->ReleaseFloatArrayElements(fArray, fPtr, fReleaseMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
| 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 | |
| 85 | AutoJavaIntArray::~AutoJavaIntArray() { |
| 86 | if (fPtr) { |
| 87 | fEnv->ReleaseIntArrayElements(fArray, fPtr, 0); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | AutoJavaShortArray::AutoJavaShortArray(JNIEnv* env, jshortArray array, |
Mike Reed | c04851f | 2009-10-28 15:09:45 -0400 | [diff] [blame] | 92 | int minLength, JNIAccess access) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | } |
Mike Reed | c04851f | 2009-10-28 15:09:45 -0400 | [diff] [blame] | 102 | fReleaseMode = (access == kRO_JNIAccess) ? JNI_ABORT : 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | AutoJavaShortArray::~AutoJavaShortArray() { |
| 106 | if (fPtr) { |
Mike Reed | c04851f | 2009-10-28 15:09:45 -0400 | [diff] [blame] | 107 | fEnv->ReleaseShortArrayElements(fArray, fPtr, fReleaseMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
| 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 | |
| 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; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | |
| 156 | static jclass gBitmapConfig_class; |
| 157 | static jfieldID gBitmapConfig_nativeInstanceID; |
| 158 | |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 159 | static jclass gBitmapRegionDecoder_class; |
| 160 | static jmethodID gBitmapRegionDecoder_constructorMethodID; |
Joseph Wen | f1f48bc | 2010-07-19 16:59:51 +0800 | [diff] [blame] | 161 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | static jclass gCanvas_class; |
| 163 | static jfieldID gCanvas_nativeInstanceID; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Mathieu Chartier | 7384b42 | 2013-10-17 18:16:42 -0700 | [diff] [blame^] | 175 | static jclass gByte_class; |
| 176 | static jobject gVMRuntime; |
| 177 | static jclass gVMRuntime_class; |
| 178 | static jmethodID gVMRuntime_newNonMovableArray; |
| 179 | static jmethodID gVMRuntime_addressOf; |
| 180 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | /////////////////////////////////////////////////////////////////////////////// |
| 182 | |
| 183 | void GraphicsJNI::get_jrect(JNIEnv* env, jobject obj, int* L, int* T, int* R, int* B) |
| 184 | { |
| 185 | SkASSERT(env->IsInstanceOf(obj, gRect_class)); |
| 186 | |
| 187 | *L = env->GetIntField(obj, gRect_leftFieldID); |
| 188 | *T = env->GetIntField(obj, gRect_topFieldID); |
| 189 | *R = env->GetIntField(obj, gRect_rightFieldID); |
| 190 | *B = env->GetIntField(obj, gRect_bottomFieldID); |
| 191 | } |
| 192 | |
| 193 | void GraphicsJNI::set_jrect(JNIEnv* env, jobject obj, int L, int T, int R, int B) |
| 194 | { |
| 195 | SkASSERT(env->IsInstanceOf(obj, gRect_class)); |
| 196 | |
| 197 | env->SetIntField(obj, gRect_leftFieldID, L); |
| 198 | env->SetIntField(obj, gRect_topFieldID, T); |
| 199 | env->SetIntField(obj, gRect_rightFieldID, R); |
| 200 | env->SetIntField(obj, gRect_bottomFieldID, B); |
| 201 | } |
| 202 | |
| 203 | SkIRect* GraphicsJNI::jrect_to_irect(JNIEnv* env, jobject obj, SkIRect* ir) |
| 204 | { |
| 205 | SkASSERT(env->IsInstanceOf(obj, gRect_class)); |
| 206 | |
| 207 | ir->set(env->GetIntField(obj, gRect_leftFieldID), |
| 208 | env->GetIntField(obj, gRect_topFieldID), |
| 209 | env->GetIntField(obj, gRect_rightFieldID), |
| 210 | env->GetIntField(obj, gRect_bottomFieldID)); |
| 211 | return ir; |
| 212 | } |
| 213 | |
| 214 | void GraphicsJNI::irect_to_jrect(const SkIRect& ir, JNIEnv* env, jobject obj) |
| 215 | { |
| 216 | SkASSERT(env->IsInstanceOf(obj, gRect_class)); |
| 217 | |
| 218 | env->SetIntField(obj, gRect_leftFieldID, ir.fLeft); |
| 219 | env->SetIntField(obj, gRect_topFieldID, ir.fTop); |
| 220 | env->SetIntField(obj, gRect_rightFieldID, ir.fRight); |
| 221 | env->SetIntField(obj, gRect_bottomFieldID, ir.fBottom); |
| 222 | } |
| 223 | |
| 224 | SkRect* GraphicsJNI::jrectf_to_rect(JNIEnv* env, jobject obj, SkRect* r) |
| 225 | { |
| 226 | SkASSERT(env->IsInstanceOf(obj, gRectF_class)); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 227 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 228 | r->set(SkFloatToScalar(env->GetFloatField(obj, gRectF_leftFieldID)), |
| 229 | SkFloatToScalar(env->GetFloatField(obj, gRectF_topFieldID)), |
| 230 | SkFloatToScalar(env->GetFloatField(obj, gRectF_rightFieldID)), |
| 231 | SkFloatToScalar(env->GetFloatField(obj, gRectF_bottomFieldID))); |
| 232 | return r; |
| 233 | } |
| 234 | |
| 235 | SkRect* GraphicsJNI::jrect_to_rect(JNIEnv* env, jobject obj, SkRect* r) |
| 236 | { |
| 237 | SkASSERT(env->IsInstanceOf(obj, gRect_class)); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 238 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 239 | r->set(SkIntToScalar(env->GetIntField(obj, gRect_leftFieldID)), |
| 240 | SkIntToScalar(env->GetIntField(obj, gRect_topFieldID)), |
| 241 | SkIntToScalar(env->GetIntField(obj, gRect_rightFieldID)), |
| 242 | SkIntToScalar(env->GetIntField(obj, gRect_bottomFieldID))); |
| 243 | return r; |
| 244 | } |
| 245 | |
| 246 | void GraphicsJNI::rect_to_jrectf(const SkRect& r, JNIEnv* env, jobject obj) |
| 247 | { |
| 248 | SkASSERT(env->IsInstanceOf(obj, gRectF_class)); |
| 249 | |
| 250 | env->SetFloatField(obj, gRectF_leftFieldID, SkScalarToFloat(r.fLeft)); |
| 251 | env->SetFloatField(obj, gRectF_topFieldID, SkScalarToFloat(r.fTop)); |
| 252 | env->SetFloatField(obj, gRectF_rightFieldID, SkScalarToFloat(r.fRight)); |
| 253 | env->SetFloatField(obj, gRectF_bottomFieldID, SkScalarToFloat(r.fBottom)); |
| 254 | } |
| 255 | |
| 256 | SkIPoint* GraphicsJNI::jpoint_to_ipoint(JNIEnv* env, jobject obj, SkIPoint* point) |
| 257 | { |
| 258 | SkASSERT(env->IsInstanceOf(obj, gPoint_class)); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 259 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | point->set(env->GetIntField(obj, gPoint_xFieldID), |
| 261 | env->GetIntField(obj, gPoint_yFieldID)); |
| 262 | return point; |
| 263 | } |
| 264 | |
| 265 | void GraphicsJNI::ipoint_to_jpoint(const SkIPoint& ir, JNIEnv* env, jobject obj) |
| 266 | { |
| 267 | SkASSERT(env->IsInstanceOf(obj, gPoint_class)); |
| 268 | |
| 269 | env->SetIntField(obj, gPoint_xFieldID, ir.fX); |
| 270 | env->SetIntField(obj, gPoint_yFieldID, ir.fY); |
| 271 | } |
| 272 | |
| 273 | SkPoint* GraphicsJNI::jpointf_to_point(JNIEnv* env, jobject obj, SkPoint* point) |
| 274 | { |
| 275 | SkASSERT(env->IsInstanceOf(obj, gPointF_class)); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 276 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 277 | point->set(SkFloatToScalar(env->GetIntField(obj, gPointF_xFieldID)), |
| 278 | SkFloatToScalar(env->GetIntField(obj, gPointF_yFieldID))); |
| 279 | return point; |
| 280 | } |
| 281 | |
| 282 | void GraphicsJNI::point_to_jpointf(const SkPoint& r, JNIEnv* env, jobject obj) |
| 283 | { |
| 284 | SkASSERT(env->IsInstanceOf(obj, gPointF_class)); |
| 285 | |
| 286 | env->SetFloatField(obj, gPointF_xFieldID, SkScalarToFloat(r.fX)); |
| 287 | env->SetFloatField(obj, gPointF_yFieldID, SkScalarToFloat(r.fY)); |
| 288 | } |
| 289 | |
| 290 | SkBitmap* GraphicsJNI::getNativeBitmap(JNIEnv* env, jobject bitmap) { |
| 291 | SkASSERT(env); |
| 292 | SkASSERT(bitmap); |
| 293 | SkASSERT(env->IsInstanceOf(bitmap, gBitmap_class)); |
| 294 | SkBitmap* b = (SkBitmap*)env->GetIntField(bitmap, gBitmap_nativeInstanceID); |
| 295 | SkASSERT(b); |
| 296 | return b; |
| 297 | } |
| 298 | |
| 299 | SkBitmap::Config GraphicsJNI::getNativeBitmapConfig(JNIEnv* env, |
| 300 | jobject jconfig) { |
| 301 | SkASSERT(env); |
| 302 | if (NULL == jconfig) { |
| 303 | return SkBitmap::kNo_Config; |
| 304 | } |
| 305 | SkASSERT(env->IsInstanceOf(jconfig, gBitmapConfig_class)); |
| 306 | int c = env->GetIntField(jconfig, gBitmapConfig_nativeInstanceID); |
| 307 | if (c < 0 || c >= SkBitmap::kConfigCount) { |
| 308 | c = SkBitmap::kNo_Config; |
| 309 | } |
| 310 | return static_cast<SkBitmap::Config>(c); |
| 311 | } |
| 312 | |
| 313 | SkCanvas* GraphicsJNI::getNativeCanvas(JNIEnv* env, jobject canvas) { |
| 314 | SkASSERT(env); |
| 315 | SkASSERT(canvas); |
| 316 | SkASSERT(env->IsInstanceOf(canvas, gCanvas_class)); |
| 317 | SkCanvas* c = (SkCanvas*)env->GetIntField(canvas, gCanvas_nativeInstanceID); |
| 318 | SkASSERT(c); |
| 319 | return c; |
| 320 | } |
| 321 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 322 | SkPaint* GraphicsJNI::getNativePaint(JNIEnv* env, jobject paint) { |
| 323 | SkASSERT(env); |
| 324 | SkASSERT(paint); |
| 325 | SkASSERT(env->IsInstanceOf(paint, gPaint_class)); |
| 326 | SkPaint* p = (SkPaint*)env->GetIntField(paint, gPaint_nativeInstanceID); |
| 327 | SkASSERT(p); |
| 328 | return p; |
| 329 | } |
| 330 | |
| 331 | SkPicture* GraphicsJNI::getNativePicture(JNIEnv* env, jobject picture) |
| 332 | { |
| 333 | SkASSERT(env); |
| 334 | SkASSERT(picture); |
| 335 | SkASSERT(env->IsInstanceOf(picture, gPicture_class)); |
| 336 | SkPicture* p = (SkPicture*)env->GetIntField(picture, gPicture_nativeInstanceID); |
| 337 | SkASSERT(p); |
| 338 | return p; |
| 339 | } |
| 340 | |
| 341 | SkRegion* GraphicsJNI::getNativeRegion(JNIEnv* env, jobject region) |
| 342 | { |
| 343 | SkASSERT(env); |
| 344 | SkASSERT(region); |
| 345 | SkASSERT(env->IsInstanceOf(region, gRegion_class)); |
| 346 | SkRegion* r = (SkRegion*)env->GetIntField(region, gRegion_nativeInstanceID); |
| 347 | SkASSERT(r); |
| 348 | return r; |
| 349 | } |
| 350 | |
| 351 | /////////////////////////////////////////////////////////////////////////////////////////// |
| 352 | |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 353 | jobject GraphicsJNI::createBitmap(JNIEnv* env, SkBitmap* bitmap, jbyteArray buffer, |
Amith Yamasani | ec4a504 | 2012-04-04 10:27:15 -0700 | [diff] [blame] | 354 | bool isMutable, jbyteArray ninepatch, jintArray layoutbounds, |
| 355 | int density) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 356 | { |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 357 | SkASSERT(bitmap); |
| 358 | SkASSERT(bitmap->pixelRef()); |
Elliott Hughes | cf6f7a0 | 2011-04-12 17:50:45 -0700 | [diff] [blame] | 359 | jobject obj = env->NewObject(gBitmap_class, gBitmap_constructorMethodID, |
| 360 | static_cast<jint>(reinterpret_cast<uintptr_t>(bitmap)), |
Amith Yamasani | ec4a504 | 2012-04-04 10:27:15 -0700 | [diff] [blame] | 361 | buffer, isMutable, ninepatch, layoutbounds, density); |
Elliott Hughes | cf6f7a0 | 2011-04-12 17:50:45 -0700 | [diff] [blame] | 362 | hasException(env); // For the side effect of logging. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 363 | return obj; |
| 364 | } |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 365 | |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 366 | jobject GraphicsJNI::createBitmap(JNIEnv* env, SkBitmap* bitmap, bool isMutable, |
| 367 | jbyteArray ninepatch, int density) |
| 368 | { |
Amith Yamasani | ec4a504 | 2012-04-04 10:27:15 -0700 | [diff] [blame] | 369 | return createBitmap(env, bitmap, NULL, isMutable, ninepatch, NULL, density); |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 373 | jobject GraphicsJNI::createBitmapRegionDecoder(JNIEnv* env, SkBitmapRegionDecoder* bitmap) |
Joseph Wen | f1f48bc | 2010-07-19 16:59:51 +0800 | [diff] [blame] | 374 | { |
| 375 | SkASSERT(bitmap != NULL); |
| 376 | |
Elliott Hughes | cf6f7a0 | 2011-04-12 17:50:45 -0700 | [diff] [blame] | 377 | jobject obj = env->NewObject(gBitmapRegionDecoder_class, |
| 378 | gBitmapRegionDecoder_constructorMethodID, |
| 379 | static_cast<jint>(reinterpret_cast<uintptr_t>(bitmap))); |
| 380 | hasException(env); // For the side effect of logging. |
Joseph Wen | f1f48bc | 2010-07-19 16:59:51 +0800 | [diff] [blame] | 381 | return obj; |
| 382 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 383 | |
| 384 | jobject GraphicsJNI::createRegion(JNIEnv* env, SkRegion* region) |
| 385 | { |
| 386 | SkASSERT(region != NULL); |
Elliott Hughes | cf6f7a0 | 2011-04-12 17:50:45 -0700 | [diff] [blame] | 387 | jobject obj = env->NewObject(gRegion_class, gRegion_constructorMethodID, |
| 388 | static_cast<jint>(reinterpret_cast<uintptr_t>(region)), 0); |
| 389 | hasException(env); // For the side effect of logging. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 390 | return obj; |
| 391 | } |
| 392 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 393 | static JNIEnv* vm2env(JavaVM* vm) |
| 394 | { |
| 395 | JNIEnv* env = NULL; |
| 396 | if (vm->GetEnv((void**)&env, JNI_VERSION_1_4) != JNI_OK || NULL == env) |
| 397 | { |
| 398 | SkDebugf("------- [%p] vm->GetEnv() failed\n", vm); |
| 399 | sk_throw(); |
| 400 | } |
| 401 | return env; |
| 402 | } |
| 403 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 404 | /////////////////////////////////////////////////////////////////////////////// |
| 405 | |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 406 | AndroidPixelRef::AndroidPixelRef(JNIEnv* env, void* storage, size_t size, jbyteArray storageObj, |
| 407 | SkColorTable* ctable) : SkMallocPixelRef(storage, size, ctable) { |
| 408 | SkASSERT(storage); |
| 409 | SkASSERT(env); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 410 | |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 411 | if (env->GetJavaVM(&fVM) != JNI_OK) { |
| 412 | SkDebugf("------ [%p] env->GetJavaVM failed\n", env); |
| 413 | sk_throw(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 414 | } |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 415 | fStorageObj = storageObj; |
| 416 | fHasGlobalRef = false; |
| 417 | fGlobalRefCnt = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 418 | |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 419 | // If storageObj is NULL, the memory was NOT allocated on the Java heap |
| 420 | fOnJavaHeap = (storageObj != NULL); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 421 | |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | AndroidPixelRef::~AndroidPixelRef() { |
| 425 | if (fOnJavaHeap) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 426 | JNIEnv* env = vm2env(fVM); |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 427 | |
| 428 | if (fStorageObj && fHasGlobalRef) { |
| 429 | env->DeleteGlobalRef(fStorageObj); |
| 430 | } |
| 431 | fStorageObj = NULL; |
| 432 | |
| 433 | // Set this to NULL to prevent the SkMallocPixelRef destructor |
| 434 | // from freeing the memory. |
| 435 | fStorage = NULL; |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | void AndroidPixelRef::setLocalJNIRef(jbyteArray arr) { |
| 440 | if (!fHasGlobalRef) { |
| 441 | fStorageObj = arr; |
| 442 | } |
| 443 | } |
| 444 | |
Patrick Dubroy | a2f0e2d | 2010-12-15 15:03:47 -0800 | [diff] [blame] | 445 | void AndroidPixelRef::globalRef(void* localref) { |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 446 | if (fOnJavaHeap && sk_atomic_inc(&fGlobalRefCnt) == 0) { |
| 447 | JNIEnv *env = vm2env(fVM); |
Patrick Dubroy | a2f0e2d | 2010-12-15 15:03:47 -0800 | [diff] [blame] | 448 | |
| 449 | // If JNI ref was passed, it is always used |
| 450 | if (localref) fStorageObj = (jbyteArray) localref; |
| 451 | |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 452 | if (fStorageObj == NULL) { |
Patrick Dubroy | a2f0e2d | 2010-12-15 15:03:47 -0800 | [diff] [blame] | 453 | SkDebugf("No valid local ref to create a JNI global ref\n"); |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 454 | sk_throw(); |
| 455 | } |
| 456 | if (fHasGlobalRef) { |
| 457 | // This should never happen |
Patrick Dubroy | a2f0e2d | 2010-12-15 15:03:47 -0800 | [diff] [blame] | 458 | SkDebugf("Already holding a JNI global ref"); |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 459 | sk_throw(); |
| 460 | } |
| 461 | |
| 462 | fStorageObj = (jbyteArray) env->NewGlobalRef(fStorageObj); |
| 463 | // TODO: Check for failure here |
| 464 | fHasGlobalRef = true; |
| 465 | } |
| 466 | ref(); |
| 467 | } |
| 468 | |
| 469 | void AndroidPixelRef::globalUnref() { |
| 470 | if (fOnJavaHeap && sk_atomic_dec(&fGlobalRefCnt) == 1) { |
| 471 | JNIEnv *env = vm2env(fVM); |
| 472 | if (!fHasGlobalRef) { |
| 473 | SkDebugf("We don't have a global ref!"); |
| 474 | sk_throw(); |
| 475 | } |
| 476 | env->DeleteGlobalRef(fStorageObj); |
| 477 | fStorageObj = NULL; |
| 478 | fHasGlobalRef = false; |
| 479 | } |
| 480 | unref(); |
| 481 | } |
| 482 | |
| 483 | /////////////////////////////////////////////////////////////////////////////// |
| 484 | |
| 485 | jbyteArray GraphicsJNI::allocateJavaPixelRef(JNIEnv* env, SkBitmap* bitmap, |
| 486 | SkColorTable* ctable) { |
| 487 | Sk64 size64 = bitmap->getSize64(); |
| 488 | if (size64.isNeg() || !size64.is32()) { |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 489 | jniThrowException(env, "java/lang/IllegalArgumentException", |
| 490 | "bitmap size exceeds 32bits"); |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 491 | return NULL; |
| 492 | } |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 493 | size_t size = size64.get32(); |
Mathieu Chartier | 7384b42 | 2013-10-17 18:16:42 -0700 | [diff] [blame^] | 494 | jbyteArray arrayObj = (jbyteArray) env->CallObjectMethod(gVMRuntime, |
| 495 | gVMRuntime_newNonMovableArray, |
| 496 | gByte_class, size); |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 497 | if (arrayObj) { |
Mathieu Chartier | 7384b42 | 2013-10-17 18:16:42 -0700 | [diff] [blame^] | 498 | jbyte* addr = (jbyte*)env->CallLongMethod(gVMRuntime, gVMRuntime_addressOf, arrayObj); |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 499 | if (addr) { |
| 500 | SkPixelRef* pr = new AndroidPixelRef(env, (void*) addr, size, arrayObj, ctable); |
| 501 | bitmap->setPixelRef(pr)->unref(); |
| 502 | // since we're already allocated, we lockPixels right away |
| 503 | // HeapAllocator behaves this way too |
| 504 | bitmap->lockPixels(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 505 | } |
| 506 | } |
| 507 | |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 508 | return arrayObj; |
| 509 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 510 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 511 | /////////////////////////////////////////////////////////////////////////////// |
| 512 | |
Carl Shapiro | 2118b25 | 2010-12-17 18:03:38 -0800 | [diff] [blame] | 513 | JavaPixelAllocator::JavaPixelAllocator(JNIEnv* env) |
| 514 | : fStorageObj(NULL), |
Patrick Dubroy | afde46e | 2010-12-15 11:52:01 -0800 | [diff] [blame] | 515 | fAllocCount(0) { |
Wei-Ta Chen | 291303b | 2010-08-18 15:40:29 +0800 | [diff] [blame] | 516 | if (env->GetJavaVM(&fVM) != JNI_OK) { |
| 517 | SkDebugf("------ [%p] env->GetJavaVM failed\n", env); |
| 518 | sk_throw(); |
| 519 | } |
| 520 | } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 521 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 522 | bool JavaPixelAllocator::allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable) { |
Wei-Ta Chen | 291303b | 2010-08-18 15:40:29 +0800 | [diff] [blame] | 523 | JNIEnv* env = vm2env(fVM); |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 524 | |
Carl Shapiro | 2118b25 | 2010-12-17 18:03:38 -0800 | [diff] [blame] | 525 | fStorageObj = GraphicsJNI::allocateJavaPixelRef(env, bitmap, ctable); |
| 526 | fAllocCount += 1; |
| 527 | return fStorageObj != NULL; |
Joseph Wen | f1f48bc | 2010-07-19 16:59:51 +0800 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | //////////////////////////////////////////////////////////////////////////////// |
| 531 | |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 532 | JavaHeapBitmapRef::JavaHeapBitmapRef(JNIEnv* env, SkBitmap* nativeBitmap, jbyteArray buffer) { |
| 533 | fEnv = env; |
| 534 | fNativeBitmap = nativeBitmap; |
| 535 | fBuffer = buffer; |
| 536 | |
| 537 | // If the buffer is NULL, the backing memory wasn't allocated on the Java heap |
| 538 | if (fBuffer) { |
| 539 | ((AndroidPixelRef*) fNativeBitmap->pixelRef())->setLocalJNIRef(fBuffer); |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | JavaHeapBitmapRef::~JavaHeapBitmapRef() { |
| 544 | if (fBuffer) { |
| 545 | ((AndroidPixelRef*) fNativeBitmap->pixelRef())->setLocalJNIRef(NULL); |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | //////////////////////////////////////////////////////////////////////////////// |
| 550 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 551 | static jclass make_globalref(JNIEnv* env, const char classname[]) |
| 552 | { |
| 553 | jclass c = env->FindClass(classname); |
| 554 | SkASSERT(c); |
| 555 | return (jclass)env->NewGlobalRef(c); |
| 556 | } |
| 557 | |
| 558 | static jfieldID getFieldIDCheck(JNIEnv* env, jclass clazz, |
| 559 | const char fieldname[], const char type[]) |
| 560 | { |
| 561 | jfieldID id = env->GetFieldID(clazz, fieldname, type); |
| 562 | SkASSERT(id); |
| 563 | return id; |
| 564 | } |
| 565 | |
| 566 | int register_android_graphics_Graphics(JNIEnv* env) |
| 567 | { |
| 568 | jmethodID m; |
| 569 | jclass c; |
| 570 | |
| 571 | gRect_class = make_globalref(env, "android/graphics/Rect"); |
| 572 | gRect_leftFieldID = getFieldIDCheck(env, gRect_class, "left", "I"); |
| 573 | gRect_topFieldID = getFieldIDCheck(env, gRect_class, "top", "I"); |
| 574 | gRect_rightFieldID = getFieldIDCheck(env, gRect_class, "right", "I"); |
| 575 | gRect_bottomFieldID = getFieldIDCheck(env, gRect_class, "bottom", "I"); |
| 576 | |
| 577 | gRectF_class = make_globalref(env, "android/graphics/RectF"); |
| 578 | gRectF_leftFieldID = getFieldIDCheck(env, gRectF_class, "left", "F"); |
| 579 | gRectF_topFieldID = getFieldIDCheck(env, gRectF_class, "top", "F"); |
| 580 | gRectF_rightFieldID = getFieldIDCheck(env, gRectF_class, "right", "F"); |
| 581 | gRectF_bottomFieldID = getFieldIDCheck(env, gRectF_class, "bottom", "F"); |
| 582 | |
| 583 | gPoint_class = make_globalref(env, "android/graphics/Point"); |
| 584 | gPoint_xFieldID = getFieldIDCheck(env, gPoint_class, "x", "I"); |
| 585 | gPoint_yFieldID = getFieldIDCheck(env, gPoint_class, "y", "I"); |
| 586 | |
| 587 | gPointF_class = make_globalref(env, "android/graphics/PointF"); |
| 588 | gPointF_xFieldID = getFieldIDCheck(env, gPointF_class, "x", "F"); |
| 589 | gPointF_yFieldID = getFieldIDCheck(env, gPointF_class, "y", "F"); |
| 590 | |
| 591 | gBitmap_class = make_globalref(env, "android/graphics/Bitmap"); |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 592 | gBitmap_nativeInstanceID = getFieldIDCheck(env, gBitmap_class, "mNativeBitmap", "I"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 593 | gBitmap_constructorMethodID = env->GetMethodID(gBitmap_class, "<init>", |
Amith Yamasani | ec4a504 | 2012-04-04 10:27:15 -0700 | [diff] [blame] | 594 | "(I[BZ[B[II)V"); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 595 | gBitmapRegionDecoder_class = make_globalref(env, "android/graphics/BitmapRegionDecoder"); |
| 596 | gBitmapRegionDecoder_constructorMethodID = env->GetMethodID(gBitmapRegionDecoder_class, "<init>", "(I)V"); |
Joseph Wen | f1f48bc | 2010-07-19 16:59:51 +0800 | [diff] [blame] | 597 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 598 | gBitmapConfig_class = make_globalref(env, "android/graphics/Bitmap$Config"); |
| 599 | gBitmapConfig_nativeInstanceID = getFieldIDCheck(env, gBitmapConfig_class, |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 600 | "nativeInt", "I"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 601 | |
| 602 | gCanvas_class = make_globalref(env, "android/graphics/Canvas"); |
| 603 | gCanvas_nativeInstanceID = getFieldIDCheck(env, gCanvas_class, "mNativeCanvas", "I"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 604 | |
| 605 | gPaint_class = make_globalref(env, "android/graphics/Paint"); |
| 606 | gPaint_nativeInstanceID = getFieldIDCheck(env, gPaint_class, "mNativePaint", "I"); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 607 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 608 | gPicture_class = make_globalref(env, "android/graphics/Picture"); |
| 609 | gPicture_nativeInstanceID = getFieldIDCheck(env, gPicture_class, "mNativePicture", "I"); |
| 610 | |
| 611 | gRegion_class = make_globalref(env, "android/graphics/Region"); |
| 612 | gRegion_nativeInstanceID = getFieldIDCheck(env, gRegion_class, "mNativeRegion", "I"); |
| 613 | gRegion_constructorMethodID = env->GetMethodID(gRegion_class, "<init>", |
| 614 | "(II)V"); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 615 | |
Mathieu Chartier | 7384b42 | 2013-10-17 18:16:42 -0700 | [diff] [blame^] | 616 | c = env->FindClass("java/lang/Byte"); |
| 617 | gByte_class = (jclass)env->NewGlobalRef( |
| 618 | env->GetStaticObjectField(c, env->GetStaticFieldID(c, "TYPE", "Ljava/lang/Class;"))); |
| 619 | |
| 620 | gVMRuntime_class = make_globalref(env, "dalvik/system/VMRuntime"); |
| 621 | m = env->GetStaticMethodID(gVMRuntime_class, "getRuntime", "()Ldalvik/system/VMRuntime;"); |
| 622 | gVMRuntime = env->NewGlobalRef(env->CallStaticObjectMethod(gVMRuntime_class, m)); |
| 623 | gVMRuntime_newNonMovableArray = env->GetMethodID(gVMRuntime_class, "newNonMovableArray", |
| 624 | "(Ljava/lang/Class;I)Ljava/lang/Object;"); |
| 625 | gVMRuntime_addressOf = env->GetMethodID(gVMRuntime_class, "addressOf", "(Ljava/lang/Object;)J"); |
| 626 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 627 | return 0; |
| 628 | } |