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