1 #undef LOG_TAG
2 #define LOG_TAG "GraphicsJNI"
3
4 #include <assert.h>
5 #include <unistd.h>
6
7 #include "jni.h"
8 #include <nativehelper/JNIHelp.h>
9 #include "GraphicsJNI.h"
10
11 #include "SkCanvas.h"
12 #include "SkFontMetrics.h"
13 #include "SkMath.h"
14 #include "SkRegion.h"
15 #include <cutils/ashmem.h>
16 #include <hwui/Canvas.h>
17
18 using namespace android;
19
20 /*static*/ JavaVM* GraphicsJNI::mJavaVM = nullptr;
21
setJavaVM(JavaVM * javaVM)22 void GraphicsJNI::setJavaVM(JavaVM* javaVM) {
23 mJavaVM = javaVM;
24 }
25
26 /** return a pointer to the JNIEnv for this thread */
getJNIEnv()27 JNIEnv* GraphicsJNI::getJNIEnv() {
28 assert(mJavaVM != nullptr);
29 JNIEnv* env;
30 if (mJavaVM->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
31 return nullptr;
32 }
33 return env;
34 }
35
36 /** create a JNIEnv* for this thread or assert if one already exists */
attachJNIEnv(const char * envName)37 JNIEnv* GraphicsJNI::attachJNIEnv(const char* envName) {
38 assert(getJNIEnv() == nullptr);
39 JNIEnv* env = nullptr;
40 JavaVMAttachArgs args = { JNI_VERSION_1_4, envName, NULL };
41 int result = mJavaVM->AttachCurrentThread(&env, (void*) &args);
42 if (result != JNI_OK) {
43 ALOGE("thread attach failed: %#x", result);
44 }
45 return env;
46 }
47
48 /** detach the current thread from the JavaVM */
detachJNIEnv()49 void GraphicsJNI::detachJNIEnv() {
50 assert(mJavaVM != nullptr);
51 mJavaVM->DetachCurrentThread();
52 }
53
doThrowNPE(JNIEnv * env)54 void doThrowNPE(JNIEnv* env) {
55 jniThrowNullPointerException(env, NULL);
56 }
57
doThrowAIOOBE(JNIEnv * env)58 void doThrowAIOOBE(JNIEnv* env) {
59 jniThrowException(env, "java/lang/ArrayIndexOutOfBoundsException", NULL);
60 }
61
doThrowRE(JNIEnv * env,const char * msg)62 void doThrowRE(JNIEnv* env, const char* msg) {
63 jniThrowRuntimeException(env, msg);
64 }
65
doThrowIAE(JNIEnv * env,const char * msg)66 void doThrowIAE(JNIEnv* env, const char* msg) {
67 jniThrowException(env, "java/lang/IllegalArgumentException", msg);
68 }
69
doThrowISE(JNIEnv * env,const char * msg)70 void doThrowISE(JNIEnv* env, const char* msg) {
71 jniThrowException(env, "java/lang/IllegalStateException", msg);
72 }
73
doThrowOOME(JNIEnv * env,const char * msg)74 void doThrowOOME(JNIEnv* env, const char* msg) {
75 jniThrowException(env, "java/lang/OutOfMemoryError", msg);
76 }
77
doThrowIOE(JNIEnv * env,const char * msg)78 void doThrowIOE(JNIEnv* env, const char* msg) {
79 jniThrowException(env, "java/io/IOException", msg);
80 }
81
hasException(JNIEnv * env)82 bool GraphicsJNI::hasException(JNIEnv *env) {
83 if (env->ExceptionCheck() != 0) {
84 ALOGE("*** Uncaught exception returned from Java call!\n");
85 env->ExceptionDescribe();
86 return true;
87 }
88 return false;
89 }
90
91 ///////////////////////////////////////////////////////////////////////////////
92
AutoJavaFloatArray(JNIEnv * env,jfloatArray array,int minLength,JNIAccess access)93 AutoJavaFloatArray::AutoJavaFloatArray(JNIEnv* env, jfloatArray array,
94 int minLength, JNIAccess access)
95 : fEnv(env), fArray(array), fPtr(NULL), fLen(0) {
96 ALOG_ASSERT(env);
97 if (array) {
98 fLen = env->GetArrayLength(array);
99 if (fLen < minLength) {
100 LOG_ALWAYS_FATAL("bad length");
101 }
102 fPtr = env->GetFloatArrayElements(array, NULL);
103 }
104 fReleaseMode = (access == kRO_JNIAccess) ? JNI_ABORT : 0;
105 }
106
~AutoJavaFloatArray()107 AutoJavaFloatArray::~AutoJavaFloatArray() {
108 if (fPtr) {
109 fEnv->ReleaseFloatArrayElements(fArray, fPtr, fReleaseMode);
110 }
111 }
112
AutoJavaIntArray(JNIEnv * env,jintArray array,int minLength)113 AutoJavaIntArray::AutoJavaIntArray(JNIEnv* env, jintArray array,
114 int minLength)
115 : fEnv(env), fArray(array), fPtr(NULL), fLen(0) {
116 ALOG_ASSERT(env);
117 if (array) {
118 fLen = env->GetArrayLength(array);
119 if (fLen < minLength) {
120 LOG_ALWAYS_FATAL("bad length");
121 }
122 fPtr = env->GetIntArrayElements(array, NULL);
123 }
124 }
125
~AutoJavaIntArray()126 AutoJavaIntArray::~AutoJavaIntArray() {
127 if (fPtr) {
128 fEnv->ReleaseIntArrayElements(fArray, fPtr, 0);
129 }
130 }
131
AutoJavaShortArray(JNIEnv * env,jshortArray array,int minLength,JNIAccess access)132 AutoJavaShortArray::AutoJavaShortArray(JNIEnv* env, jshortArray array,
133 int minLength, JNIAccess access)
134 : fEnv(env), fArray(array), fPtr(NULL), fLen(0) {
135 ALOG_ASSERT(env);
136 if (array) {
137 fLen = env->GetArrayLength(array);
138 if (fLen < minLength) {
139 LOG_ALWAYS_FATAL("bad length");
140 }
141 fPtr = env->GetShortArrayElements(array, NULL);
142 }
143 fReleaseMode = (access == kRO_JNIAccess) ? JNI_ABORT : 0;
144 }
145
~AutoJavaShortArray()146 AutoJavaShortArray::~AutoJavaShortArray() {
147 if (fPtr) {
148 fEnv->ReleaseShortArrayElements(fArray, fPtr, fReleaseMode);
149 }
150 }
151
AutoJavaByteArray(JNIEnv * env,jbyteArray array,int minLength)152 AutoJavaByteArray::AutoJavaByteArray(JNIEnv* env, jbyteArray array,
153 int minLength)
154 : fEnv(env), fArray(array), fPtr(NULL), fLen(0) {
155 ALOG_ASSERT(env);
156 if (array) {
157 fLen = env->GetArrayLength(array);
158 if (fLen < minLength) {
159 LOG_ALWAYS_FATAL("bad length");
160 }
161 fPtr = env->GetByteArrayElements(array, NULL);
162 }
163 }
164
~AutoJavaByteArray()165 AutoJavaByteArray::~AutoJavaByteArray() {
166 if (fPtr) {
167 fEnv->ReleaseByteArrayElements(fArray, fPtr, 0);
168 }
169 }
170
171 ///////////////////////////////////////////////////////////////////////////////
172
173 static jclass gRect_class;
174 static jfieldID gRect_leftFieldID;
175 static jfieldID gRect_topFieldID;
176 static jfieldID gRect_rightFieldID;
177 static jfieldID gRect_bottomFieldID;
178
179 static jclass gRectF_class;
180 static jfieldID gRectF_leftFieldID;
181 static jfieldID gRectF_topFieldID;
182 static jfieldID gRectF_rightFieldID;
183 static jfieldID gRectF_bottomFieldID;
184
185 static jclass gPoint_class;
186 static jfieldID gPoint_xFieldID;
187 static jfieldID gPoint_yFieldID;
188
189 static jclass gPointF_class;
190 static jfieldID gPointF_xFieldID;
191 static jfieldID gPointF_yFieldID;
192
193 static jclass gBitmapConfig_class;
194 static jfieldID gBitmapConfig_nativeInstanceID;
195 static jmethodID gBitmapConfig_nativeToConfigMethodID;
196
197 static jclass gBitmapRegionDecoder_class;
198 static jmethodID gBitmapRegionDecoder_constructorMethodID;
199
200 static jclass gCanvas_class;
201 static jfieldID gCanvas_nativeInstanceID;
202
203 static jclass gPicture_class;
204 static jfieldID gPicture_nativeInstanceID;
205
206 static jclass gRegion_class;
207 static jfieldID gRegion_nativeInstanceID;
208 static jmethodID gRegion_constructorMethodID;
209
210 static jclass gByte_class;
211 static jobject gVMRuntime;
212 static jclass gVMRuntime_class;
213 static jmethodID gVMRuntime_newNonMovableArray;
214 static jmethodID gVMRuntime_addressOf;
215
216 static jclass gColorSpace_class;
217 static jmethodID gColorSpace_getMethodID;
218 static jmethodID gColorSpace_matchMethodID;
219
220 static jclass gColorSpaceRGB_class;
221 static jmethodID gColorSpaceRGB_constructorMethodID;
222
223 static jclass gColorSpace_Named_class;
224 static jfieldID gColorSpace_Named_sRGBFieldID;
225 static jfieldID gColorSpace_Named_ExtendedSRGBFieldID;
226 static jfieldID gColorSpace_Named_LinearSRGBFieldID;
227 static jfieldID gColorSpace_Named_LinearExtendedSRGBFieldID;
228
229 static jclass gTransferParameters_class;
230 static jmethodID gTransferParameters_constructorMethodID;
231
232 static jclass gFontMetrics_class;
233 static jfieldID gFontMetrics_top;
234 static jfieldID gFontMetrics_ascent;
235 static jfieldID gFontMetrics_descent;
236 static jfieldID gFontMetrics_bottom;
237 static jfieldID gFontMetrics_leading;
238
239 static jclass gFontMetricsInt_class;
240 static jfieldID gFontMetricsInt_top;
241 static jfieldID gFontMetricsInt_ascent;
242 static jfieldID gFontMetricsInt_descent;
243 static jfieldID gFontMetricsInt_bottom;
244 static jfieldID gFontMetricsInt_leading;
245
246 ///////////////////////////////////////////////////////////////////////////////
247
get_jrect(JNIEnv * env,jobject obj,int * L,int * T,int * R,int * B)248 void GraphicsJNI::get_jrect(JNIEnv* env, jobject obj, int* L, int* T, int* R, int* B)
249 {
250 ALOG_ASSERT(env->IsInstanceOf(obj, gRect_class));
251
252 *L = env->GetIntField(obj, gRect_leftFieldID);
253 *T = env->GetIntField(obj, gRect_topFieldID);
254 *R = env->GetIntField(obj, gRect_rightFieldID);
255 *B = env->GetIntField(obj, gRect_bottomFieldID);
256 }
257
set_jrect(JNIEnv * env,jobject obj,int L,int T,int R,int B)258 void GraphicsJNI::set_jrect(JNIEnv* env, jobject obj, int L, int T, int R, int B)
259 {
260 ALOG_ASSERT(env->IsInstanceOf(obj, gRect_class));
261
262 env->SetIntField(obj, gRect_leftFieldID, L);
263 env->SetIntField(obj, gRect_topFieldID, T);
264 env->SetIntField(obj, gRect_rightFieldID, R);
265 env->SetIntField(obj, gRect_bottomFieldID, B);
266 }
267
jrect_to_irect(JNIEnv * env,jobject obj,SkIRect * ir)268 SkIRect* GraphicsJNI::jrect_to_irect(JNIEnv* env, jobject obj, SkIRect* ir)
269 {
270 ALOG_ASSERT(env->IsInstanceOf(obj, gRect_class));
271
272 ir->setLTRB(env->GetIntField(obj, gRect_leftFieldID),
273 env->GetIntField(obj, gRect_topFieldID),
274 env->GetIntField(obj, gRect_rightFieldID),
275 env->GetIntField(obj, gRect_bottomFieldID));
276 return ir;
277 }
278
irect_to_jrect(const SkIRect & ir,JNIEnv * env,jobject obj)279 void GraphicsJNI::irect_to_jrect(const SkIRect& ir, JNIEnv* env, jobject obj)
280 {
281 ALOG_ASSERT(env->IsInstanceOf(obj, gRect_class));
282
283 env->SetIntField(obj, gRect_leftFieldID, ir.fLeft);
284 env->SetIntField(obj, gRect_topFieldID, ir.fTop);
285 env->SetIntField(obj, gRect_rightFieldID, ir.fRight);
286 env->SetIntField(obj, gRect_bottomFieldID, ir.fBottom);
287 }
288
jrectf_to_rect(JNIEnv * env,jobject obj,SkRect * r)289 SkRect* GraphicsJNI::jrectf_to_rect(JNIEnv* env, jobject obj, SkRect* r)
290 {
291 ALOG_ASSERT(env->IsInstanceOf(obj, gRectF_class));
292
293 r->setLTRB(env->GetFloatField(obj, gRectF_leftFieldID),
294 env->GetFloatField(obj, gRectF_topFieldID),
295 env->GetFloatField(obj, gRectF_rightFieldID),
296 env->GetFloatField(obj, gRectF_bottomFieldID));
297 return r;
298 }
299
jrect_to_rect(JNIEnv * env,jobject obj,SkRect * r)300 SkRect* GraphicsJNI::jrect_to_rect(JNIEnv* env, jobject obj, SkRect* r)
301 {
302 ALOG_ASSERT(env->IsInstanceOf(obj, gRect_class));
303
304 r->setLTRB(SkIntToScalar(env->GetIntField(obj, gRect_leftFieldID)),
305 SkIntToScalar(env->GetIntField(obj, gRect_topFieldID)),
306 SkIntToScalar(env->GetIntField(obj, gRect_rightFieldID)),
307 SkIntToScalar(env->GetIntField(obj, gRect_bottomFieldID)));
308 return r;
309 }
310
rect_to_jrectf(const SkRect & r,JNIEnv * env,jobject obj)311 void GraphicsJNI::rect_to_jrectf(const SkRect& r, JNIEnv* env, jobject obj)
312 {
313 ALOG_ASSERT(env->IsInstanceOf(obj, gRectF_class));
314
315 env->SetFloatField(obj, gRectF_leftFieldID, SkScalarToFloat(r.fLeft));
316 env->SetFloatField(obj, gRectF_topFieldID, SkScalarToFloat(r.fTop));
317 env->SetFloatField(obj, gRectF_rightFieldID, SkScalarToFloat(r.fRight));
318 env->SetFloatField(obj, gRectF_bottomFieldID, SkScalarToFloat(r.fBottom));
319 }
320
jpoint_to_ipoint(JNIEnv * env,jobject obj,SkIPoint * point)321 SkIPoint* GraphicsJNI::jpoint_to_ipoint(JNIEnv* env, jobject obj, SkIPoint* point)
322 {
323 ALOG_ASSERT(env->IsInstanceOf(obj, gPoint_class));
324
325 point->set(env->GetIntField(obj, gPoint_xFieldID),
326 env->GetIntField(obj, gPoint_yFieldID));
327 return point;
328 }
329
ipoint_to_jpoint(const SkIPoint & ir,JNIEnv * env,jobject obj)330 void GraphicsJNI::ipoint_to_jpoint(const SkIPoint& ir, JNIEnv* env, jobject obj)
331 {
332 ALOG_ASSERT(env->IsInstanceOf(obj, gPoint_class));
333
334 env->SetIntField(obj, gPoint_xFieldID, ir.fX);
335 env->SetIntField(obj, gPoint_yFieldID, ir.fY);
336 }
337
jpointf_to_point(JNIEnv * env,jobject obj,SkPoint * point)338 SkPoint* GraphicsJNI::jpointf_to_point(JNIEnv* env, jobject obj, SkPoint* point)
339 {
340 ALOG_ASSERT(env->IsInstanceOf(obj, gPointF_class));
341
342 point->set(env->GetIntField(obj, gPointF_xFieldID),
343 env->GetIntField(obj, gPointF_yFieldID));
344 return point;
345 }
346
point_to_jpointf(const SkPoint & r,JNIEnv * env,jobject obj)347 void GraphicsJNI::point_to_jpointf(const SkPoint& r, JNIEnv* env, jobject obj)
348 {
349 ALOG_ASSERT(env->IsInstanceOf(obj, gPointF_class));
350
351 env->SetFloatField(obj, gPointF_xFieldID, SkScalarToFloat(r.fX));
352 env->SetFloatField(obj, gPointF_yFieldID, SkScalarToFloat(r.fY));
353 }
354
355 // See enum values in GraphicsJNI.h
colorTypeToLegacyBitmapConfig(SkColorType colorType)356 jint GraphicsJNI::colorTypeToLegacyBitmapConfig(SkColorType colorType) {
357 switch (colorType) {
358 case kRGBA_F16_SkColorType:
359 return kRGBA_16F_LegacyBitmapConfig;
360 case kN32_SkColorType:
361 return kARGB_8888_LegacyBitmapConfig;
362 case kARGB_4444_SkColorType:
363 return kARGB_4444_LegacyBitmapConfig;
364 case kRGB_565_SkColorType:
365 return kRGB_565_LegacyBitmapConfig;
366 case kAlpha_8_SkColorType:
367 return kA8_LegacyBitmapConfig;
368 case kUnknown_SkColorType:
369 default:
370 break;
371 }
372 return kNo_LegacyBitmapConfig;
373 }
374
legacyBitmapConfigToColorType(jint legacyConfig)375 SkColorType GraphicsJNI::legacyBitmapConfigToColorType(jint legacyConfig) {
376 const uint8_t gConfig2ColorType[] = {
377 kUnknown_SkColorType,
378 kAlpha_8_SkColorType,
379 kUnknown_SkColorType, // Previously kIndex_8_SkColorType,
380 kRGB_565_SkColorType,
381 kARGB_4444_SkColorType,
382 kN32_SkColorType,
383 kRGBA_F16_SkColorType,
384 kN32_SkColorType
385 };
386
387 if (legacyConfig < 0 || legacyConfig > kLastEnum_LegacyBitmapConfig) {
388 legacyConfig = kNo_LegacyBitmapConfig;
389 }
390 return static_cast<SkColorType>(gConfig2ColorType[legacyConfig]);
391 }
392
getFormatFromConfig(JNIEnv * env,jobject jconfig)393 AndroidBitmapFormat GraphicsJNI::getFormatFromConfig(JNIEnv* env, jobject jconfig) {
394 ALOG_ASSERT(env);
395 if (NULL == jconfig) {
396 return ANDROID_BITMAP_FORMAT_NONE;
397 }
398 ALOG_ASSERT(env->IsInstanceOf(jconfig, gBitmapConfig_class));
399 jint javaConfigId = env->GetIntField(jconfig, gBitmapConfig_nativeInstanceID);
400
401 const AndroidBitmapFormat config2BitmapFormat[] = {
402 ANDROID_BITMAP_FORMAT_NONE,
403 ANDROID_BITMAP_FORMAT_A_8,
404 ANDROID_BITMAP_FORMAT_NONE, // Previously Config.Index_8
405 ANDROID_BITMAP_FORMAT_RGB_565,
406 ANDROID_BITMAP_FORMAT_RGBA_4444,
407 ANDROID_BITMAP_FORMAT_RGBA_8888,
408 ANDROID_BITMAP_FORMAT_RGBA_F16,
409 ANDROID_BITMAP_FORMAT_NONE // Congfig.HARDWARE
410 };
411 return config2BitmapFormat[javaConfigId];
412 }
413
getConfigFromFormat(JNIEnv * env,AndroidBitmapFormat format)414 jobject GraphicsJNI::getConfigFromFormat(JNIEnv* env, AndroidBitmapFormat format) {
415 ALOG_ASSERT(env);
416 jint configId = kNo_LegacyBitmapConfig;
417 switch (format) {
418 case ANDROID_BITMAP_FORMAT_A_8:
419 configId = kA8_LegacyBitmapConfig;
420 break;
421 case ANDROID_BITMAP_FORMAT_RGB_565:
422 configId = kRGB_565_LegacyBitmapConfig;
423 break;
424 case ANDROID_BITMAP_FORMAT_RGBA_4444:
425 configId = kARGB_4444_LegacyBitmapConfig;
426 break;
427 case ANDROID_BITMAP_FORMAT_RGBA_8888:
428 configId = kARGB_8888_LegacyBitmapConfig;
429 break;
430 case ANDROID_BITMAP_FORMAT_RGBA_F16:
431 configId = kRGBA_16F_LegacyBitmapConfig;
432 break;
433 default:
434 break;
435 }
436
437 return env->CallStaticObjectMethod(gBitmapConfig_class,
438 gBitmapConfig_nativeToConfigMethodID, configId);
439 }
440
getNativeBitmapColorType(JNIEnv * env,jobject jconfig)441 SkColorType GraphicsJNI::getNativeBitmapColorType(JNIEnv* env, jobject jconfig) {
442 ALOG_ASSERT(env);
443 if (NULL == jconfig) {
444 return kUnknown_SkColorType;
445 }
446 ALOG_ASSERT(env->IsInstanceOf(jconfig, gBitmapConfig_class));
447 int c = env->GetIntField(jconfig, gBitmapConfig_nativeInstanceID);
448 return legacyBitmapConfigToColorType(c);
449 }
450
isHardwareConfig(JNIEnv * env,jobject jconfig)451 bool GraphicsJNI::isHardwareConfig(JNIEnv* env, jobject jconfig) {
452 ALOG_ASSERT(env);
453 if (NULL == jconfig) {
454 return false;
455 }
456 int c = env->GetIntField(jconfig, gBitmapConfig_nativeInstanceID);
457 return c == kHardware_LegacyBitmapConfig;
458 }
459
hardwareLegacyBitmapConfig()460 jint GraphicsJNI::hardwareLegacyBitmapConfig() {
461 return kHardware_LegacyBitmapConfig;
462 }
463
getNativeCanvas(JNIEnv * env,jobject canvas)464 android::Canvas* GraphicsJNI::getNativeCanvas(JNIEnv* env, jobject canvas) {
465 ALOG_ASSERT(env);
466 ALOG_ASSERT(canvas);
467 ALOG_ASSERT(env->IsInstanceOf(canvas, gCanvas_class));
468 jlong canvasHandle = env->GetLongField(canvas, gCanvas_nativeInstanceID);
469 if (!canvasHandle) {
470 return NULL;
471 }
472 return reinterpret_cast<android::Canvas*>(canvasHandle);
473 }
474
getNativeRegion(JNIEnv * env,jobject region)475 SkRegion* GraphicsJNI::getNativeRegion(JNIEnv* env, jobject region)
476 {
477 ALOG_ASSERT(env);
478 ALOG_ASSERT(region);
479 ALOG_ASSERT(env->IsInstanceOf(region, gRegion_class));
480 jlong regionHandle = env->GetLongField(region, gRegion_nativeInstanceID);
481 SkRegion* r = reinterpret_cast<SkRegion*>(regionHandle);
482 ALOG_ASSERT(r);
483 return r;
484 }
485
set_metrics(JNIEnv * env,jobject metrics,const SkFontMetrics & skmetrics)486 void GraphicsJNI::set_metrics(JNIEnv* env, jobject metrics, const SkFontMetrics& skmetrics) {
487 if (metrics == nullptr) return;
488 SkASSERT(env->IsInstanceOf(metrics, gFontMetrics_class));
489 env->SetFloatField(metrics, gFontMetrics_top, SkScalarToFloat(skmetrics.fTop));
490 env->SetFloatField(metrics, gFontMetrics_ascent, SkScalarToFloat(skmetrics.fAscent));
491 env->SetFloatField(metrics, gFontMetrics_descent, SkScalarToFloat(skmetrics.fDescent));
492 env->SetFloatField(metrics, gFontMetrics_bottom, SkScalarToFloat(skmetrics.fBottom));
493 env->SetFloatField(metrics, gFontMetrics_leading, SkScalarToFloat(skmetrics.fLeading));
494 }
495
set_metrics_int(JNIEnv * env,jobject metrics,const SkFontMetrics & skmetrics)496 int GraphicsJNI::set_metrics_int(JNIEnv* env, jobject metrics, const SkFontMetrics& skmetrics) {
497 int ascent = SkScalarRoundToInt(skmetrics.fAscent);
498 int descent = SkScalarRoundToInt(skmetrics.fDescent);
499 int leading = SkScalarRoundToInt(skmetrics.fLeading);
500
501 if (metrics) {
502 SkASSERT(env->IsInstanceOf(metrics, gFontMetricsInt_class));
503 env->SetIntField(metrics, gFontMetricsInt_top, SkScalarFloorToInt(skmetrics.fTop));
504 env->SetIntField(metrics, gFontMetricsInt_ascent, ascent);
505 env->SetIntField(metrics, gFontMetricsInt_descent, descent);
506 env->SetIntField(metrics, gFontMetricsInt_bottom, SkScalarCeilToInt(skmetrics.fBottom));
507 env->SetIntField(metrics, gFontMetricsInt_leading, leading);
508 }
509 return descent - ascent + leading;
510 }
511
512 ///////////////////////////////////////////////////////////////////////////////////////////
513
createBitmapRegionDecoder(JNIEnv * env,skia::BitmapRegionDecoder * bitmap)514 jobject GraphicsJNI::createBitmapRegionDecoder(JNIEnv* env, skia::BitmapRegionDecoder* bitmap)
515 {
516 ALOG_ASSERT(bitmap != NULL);
517
518 jobject obj = env->NewObject(gBitmapRegionDecoder_class,
519 gBitmapRegionDecoder_constructorMethodID,
520 reinterpret_cast<jlong>(bitmap));
521 hasException(env); // For the side effect of logging.
522 return obj;
523 }
524
createRegion(JNIEnv * env,SkRegion * region)525 jobject GraphicsJNI::createRegion(JNIEnv* env, SkRegion* region)
526 {
527 ALOG_ASSERT(region != NULL);
528 jobject obj = env->NewObject(gRegion_class, gRegion_constructorMethodID,
529 reinterpret_cast<jlong>(region), 0);
530 hasException(env); // For the side effect of logging.
531 return obj;
532 }
533
534 ///////////////////////////////////////////////////////////////////////////////
535
getColorSpace(JNIEnv * env,SkColorSpace * decodeColorSpace,SkColorType decodeColorType)536 jobject GraphicsJNI::getColorSpace(JNIEnv* env, SkColorSpace* decodeColorSpace,
537 SkColorType decodeColorType) {
538 if (!decodeColorSpace || decodeColorType == kAlpha_8_SkColorType) {
539 return nullptr;
540 }
541
542 // Special checks for the common sRGB cases and their extended variants.
543 jobject namedCS = nullptr;
544 sk_sp<SkColorSpace> srgbLinear = SkColorSpace::MakeSRGBLinear();
545 if (decodeColorType == kRGBA_F16_SkColorType) {
546 // An F16 Bitmap will always report that it is EXTENDED if
547 // it matches a ColorSpace that has an EXTENDED variant.
548 if (decodeColorSpace->isSRGB()) {
549 namedCS = env->GetStaticObjectField(gColorSpace_Named_class,
550 gColorSpace_Named_ExtendedSRGBFieldID);
551 } else if (decodeColorSpace == srgbLinear.get()) {
552 namedCS = env->GetStaticObjectField(gColorSpace_Named_class,
553 gColorSpace_Named_LinearExtendedSRGBFieldID);
554 }
555 } else if (decodeColorSpace->isSRGB()) {
556 namedCS = env->GetStaticObjectField(gColorSpace_Named_class,
557 gColorSpace_Named_sRGBFieldID);
558 } else if (decodeColorSpace == srgbLinear.get()) {
559 namedCS = env->GetStaticObjectField(gColorSpace_Named_class,
560 gColorSpace_Named_LinearSRGBFieldID);
561 }
562
563 if (namedCS) {
564 return env->CallStaticObjectMethod(gColorSpace_class, gColorSpace_getMethodID, namedCS);
565 }
566
567 // Try to match against known RGB color spaces using the CIE XYZ D50
568 // conversion matrix and numerical transfer function parameters
569 skcms_Matrix3x3 xyzMatrix;
570 LOG_ALWAYS_FATAL_IF(!decodeColorSpace->toXYZD50(&xyzMatrix));
571
572 skcms_TransferFunction transferParams;
573 // We can only handle numerical transfer functions at the moment
574 LOG_ALWAYS_FATAL_IF(!decodeColorSpace->isNumericalTransferFn(&transferParams));
575
576 jobject params = env->NewObject(gTransferParameters_class,
577 gTransferParameters_constructorMethodID,
578 transferParams.a, transferParams.b, transferParams.c,
579 transferParams.d, transferParams.e, transferParams.f,
580 transferParams.g);
581
582 jfloatArray xyzArray = env->NewFloatArray(9);
583 jfloat xyz[9] = {
584 xyzMatrix.vals[0][0],
585 xyzMatrix.vals[1][0],
586 xyzMatrix.vals[2][0],
587 xyzMatrix.vals[0][1],
588 xyzMatrix.vals[1][1],
589 xyzMatrix.vals[2][1],
590 xyzMatrix.vals[0][2],
591 xyzMatrix.vals[1][2],
592 xyzMatrix.vals[2][2]
593 };
594 env->SetFloatArrayRegion(xyzArray, 0, 9, xyz);
595
596 jobject colorSpace = env->CallStaticObjectMethod(gColorSpace_class,
597 gColorSpace_matchMethodID, xyzArray, params);
598
599 if (colorSpace == nullptr) {
600 // We couldn't find an exact match, let's create a new color space
601 // instance with the 3x3 conversion matrix and transfer function
602 colorSpace = env->NewObject(gColorSpaceRGB_class,
603 gColorSpaceRGB_constructorMethodID,
604 env->NewStringUTF("Unknown"), xyzArray, params);
605 }
606
607 env->DeleteLocalRef(xyzArray);
608 return colorSpace;
609 }
610
611 ///////////////////////////////////////////////////////////////////////////////
allocPixelRef(SkBitmap * bitmap)612 bool HeapAllocator::allocPixelRef(SkBitmap* bitmap) {
613 mStorage = android::Bitmap::allocateHeapBitmap(bitmap);
614 return !!mStorage;
615 }
616
617 ////////////////////////////////////////////////////////////////////////////////
618
RecyclingClippingPixelAllocator(android::Bitmap * recycledBitmap,size_t recycledBytes)619 RecyclingClippingPixelAllocator::RecyclingClippingPixelAllocator(
620 android::Bitmap* recycledBitmap, size_t recycledBytes)
621 : mRecycledBitmap(recycledBitmap)
622 , mRecycledBytes(recycledBytes)
623 , mSkiaBitmap(nullptr)
624 , mNeedsCopy(false)
625 {}
626
~RecyclingClippingPixelAllocator()627 RecyclingClippingPixelAllocator::~RecyclingClippingPixelAllocator() {}
628
allocPixelRef(SkBitmap * bitmap)629 bool RecyclingClippingPixelAllocator::allocPixelRef(SkBitmap* bitmap) {
630 // Ensure that the caller did not pass in a NULL bitmap to the constructor or this
631 // function.
632 LOG_ALWAYS_FATAL_IF(!mRecycledBitmap);
633 LOG_ALWAYS_FATAL_IF(!bitmap);
634 mSkiaBitmap = bitmap;
635
636 // This behaves differently than the RecyclingPixelAllocator. For backwards
637 // compatibility, the original color type of the recycled bitmap must be maintained.
638 if (mRecycledBitmap->info().colorType() != bitmap->colorType()) {
639 return false;
640 }
641
642 // The Skia bitmap specifies the width and height needed by the decoder.
643 // mRecycledBitmap specifies the width and height of the bitmap that we
644 // want to reuse. Neither can be changed. We will try to find a way
645 // to reuse the memory.
646 const int maxWidth = std::max(bitmap->width(), mRecycledBitmap->info().width());
647 const int maxHeight = std::max(bitmap->height(), mRecycledBitmap->info().height());
648 const SkImageInfo maxInfo = bitmap->info().makeWH(maxWidth, maxHeight);
649 const size_t rowBytes = maxInfo.minRowBytes();
650 const size_t bytesNeeded = maxInfo.computeByteSize(rowBytes);
651 if (bytesNeeded <= mRecycledBytes) {
652 // Here we take advantage of reconfigure() to reset the rowBytes
653 // of mRecycledBitmap. It is very important that we pass in
654 // mRecycledBitmap->info() for the SkImageInfo. According to the
655 // specification for BitmapRegionDecoder, we are not allowed to change
656 // the SkImageInfo.
657 // We can (must) preserve the color space since it doesn't affect the
658 // storage needs
659 mRecycledBitmap->reconfigure(
660 mRecycledBitmap->info().makeColorSpace(bitmap->refColorSpace()),
661 rowBytes);
662
663 // Give the bitmap the same pixelRef as mRecycledBitmap.
664 // skbug.com/4538: We also need to make sure that the rowBytes on the pixel ref
665 // match the rowBytes on the bitmap.
666 bitmap->setInfo(bitmap->info(), rowBytes);
667 bitmap->setPixelRef(sk_ref_sp(mRecycledBitmap), 0, 0);
668
669 // Make sure that the recycled bitmap has the correct alpha type.
670 mRecycledBitmap->setAlphaType(bitmap->alphaType());
671
672 bitmap->notifyPixelsChanged();
673 mNeedsCopy = false;
674
675 // TODO: If the dimensions of the SkBitmap are smaller than those of
676 // mRecycledBitmap, should we zero the memory in mRecycledBitmap?
677 return true;
678 }
679
680 // In the event that mRecycledBitmap is not large enough, allocate new memory
681 // on the heap.
682 SkBitmap::HeapAllocator heapAllocator;
683
684 // We will need to copy from heap memory to mRecycledBitmap's memory after the
685 // decode is complete.
686 mNeedsCopy = true;
687
688 return heapAllocator.allocPixelRef(bitmap);
689 }
690
copyIfNecessary()691 void RecyclingClippingPixelAllocator::copyIfNecessary() {
692 if (mNeedsCopy) {
693 mRecycledBitmap->ref();
694 SkPixelRef* recycledPixels = mRecycledBitmap;
695 void* dst = recycledPixels->pixels();
696 const size_t dstRowBytes = mRecycledBitmap->rowBytes();
697 const size_t bytesToCopy = std::min(mRecycledBitmap->info().minRowBytes(),
698 mSkiaBitmap->info().minRowBytes());
699 const int rowsToCopy = std::min(mRecycledBitmap->info().height(),
700 mSkiaBitmap->info().height());
701 for (int y = 0; y < rowsToCopy; y++) {
702 memcpy(dst, mSkiaBitmap->getAddr(0, y), bytesToCopy);
703 dst = SkTAddOffset<void>(dst, dstRowBytes);
704 }
705 recycledPixels->notifyPixelsChanged();
706 recycledPixels->unref();
707 }
708 mRecycledBitmap = nullptr;
709 mSkiaBitmap = nullptr;
710 }
711
712 ////////////////////////////////////////////////////////////////////////////////
713
AshmemPixelAllocator(JNIEnv * env)714 AshmemPixelAllocator::AshmemPixelAllocator(JNIEnv *env) {
715 LOG_ALWAYS_FATAL_IF(env->GetJavaVM(&mJavaVM) != JNI_OK,
716 "env->GetJavaVM failed");
717 }
718
allocPixelRef(SkBitmap * bitmap)719 bool AshmemPixelAllocator::allocPixelRef(SkBitmap* bitmap) {
720 mStorage = android::Bitmap::allocateAshmemBitmap(bitmap);
721 return !!mStorage;
722 }
723
724 ////////////////////////////////////////////////////////////////////////////////
725
register_android_graphics_Graphics(JNIEnv * env)726 int register_android_graphics_Graphics(JNIEnv* env)
727 {
728 jmethodID m;
729 jclass c;
730
731 gRect_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/Rect"));
732 gRect_leftFieldID = GetFieldIDOrDie(env, gRect_class, "left", "I");
733 gRect_topFieldID = GetFieldIDOrDie(env, gRect_class, "top", "I");
734 gRect_rightFieldID = GetFieldIDOrDie(env, gRect_class, "right", "I");
735 gRect_bottomFieldID = GetFieldIDOrDie(env, gRect_class, "bottom", "I");
736
737 gRectF_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/RectF"));
738 gRectF_leftFieldID = GetFieldIDOrDie(env, gRectF_class, "left", "F");
739 gRectF_topFieldID = GetFieldIDOrDie(env, gRectF_class, "top", "F");
740 gRectF_rightFieldID = GetFieldIDOrDie(env, gRectF_class, "right", "F");
741 gRectF_bottomFieldID = GetFieldIDOrDie(env, gRectF_class, "bottom", "F");
742
743 gPoint_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/Point"));
744 gPoint_xFieldID = GetFieldIDOrDie(env, gPoint_class, "x", "I");
745 gPoint_yFieldID = GetFieldIDOrDie(env, gPoint_class, "y", "I");
746
747 gPointF_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/PointF"));
748 gPointF_xFieldID = GetFieldIDOrDie(env, gPointF_class, "x", "F");
749 gPointF_yFieldID = GetFieldIDOrDie(env, gPointF_class, "y", "F");
750
751 gBitmapRegionDecoder_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/BitmapRegionDecoder"));
752 gBitmapRegionDecoder_constructorMethodID = GetMethodIDOrDie(env, gBitmapRegionDecoder_class, "<init>", "(J)V");
753
754 gBitmapConfig_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/Bitmap$Config"));
755 gBitmapConfig_nativeInstanceID = GetFieldIDOrDie(env, gBitmapConfig_class, "nativeInt", "I");
756 gBitmapConfig_nativeToConfigMethodID = GetStaticMethodIDOrDie(env, gBitmapConfig_class,
757 "nativeToConfig",
758 "(I)Landroid/graphics/Bitmap$Config;");
759
760 gCanvas_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/Canvas"));
761 gCanvas_nativeInstanceID = GetFieldIDOrDie(env, gCanvas_class, "mNativeCanvasWrapper", "J");
762
763 gPicture_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/Picture"));
764 gPicture_nativeInstanceID = GetFieldIDOrDie(env, gPicture_class, "mNativePicture", "J");
765
766 gRegion_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/Region"));
767 gRegion_nativeInstanceID = GetFieldIDOrDie(env, gRegion_class, "mNativeRegion", "J");
768 gRegion_constructorMethodID = GetMethodIDOrDie(env, gRegion_class, "<init>", "(JI)V");
769
770 c = env->FindClass("java/lang/Byte");
771 gByte_class = (jclass) env->NewGlobalRef(
772 env->GetStaticObjectField(c, env->GetStaticFieldID(c, "TYPE", "Ljava/lang/Class;")));
773
774 gVMRuntime_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "dalvik/system/VMRuntime"));
775 m = env->GetStaticMethodID(gVMRuntime_class, "getRuntime", "()Ldalvik/system/VMRuntime;");
776 gVMRuntime = env->NewGlobalRef(env->CallStaticObjectMethod(gVMRuntime_class, m));
777 gVMRuntime_newNonMovableArray = GetMethodIDOrDie(env, gVMRuntime_class, "newNonMovableArray",
778 "(Ljava/lang/Class;I)Ljava/lang/Object;");
779 gVMRuntime_addressOf = GetMethodIDOrDie(env, gVMRuntime_class, "addressOf", "(Ljava/lang/Object;)J");
780
781 gColorSpace_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/ColorSpace"));
782 gColorSpace_getMethodID = GetStaticMethodIDOrDie(env, gColorSpace_class,
783 "get", "(Landroid/graphics/ColorSpace$Named;)Landroid/graphics/ColorSpace;");
784 gColorSpace_matchMethodID = GetStaticMethodIDOrDie(env, gColorSpace_class, "match",
785 "([FLandroid/graphics/ColorSpace$Rgb$TransferParameters;)Landroid/graphics/ColorSpace;");
786
787 gColorSpaceRGB_class = MakeGlobalRefOrDie(env,
788 FindClassOrDie(env, "android/graphics/ColorSpace$Rgb"));
789 gColorSpaceRGB_constructorMethodID = GetMethodIDOrDie(env, gColorSpaceRGB_class,
790 "<init>", "(Ljava/lang/String;[FLandroid/graphics/ColorSpace$Rgb$TransferParameters;)V");
791
792 gColorSpace_Named_class = MakeGlobalRefOrDie(env,
793 FindClassOrDie(env, "android/graphics/ColorSpace$Named"));
794 gColorSpace_Named_sRGBFieldID = GetStaticFieldIDOrDie(env,
795 gColorSpace_Named_class, "SRGB", "Landroid/graphics/ColorSpace$Named;");
796 gColorSpace_Named_ExtendedSRGBFieldID = GetStaticFieldIDOrDie(env,
797 gColorSpace_Named_class, "EXTENDED_SRGB", "Landroid/graphics/ColorSpace$Named;");
798 gColorSpace_Named_LinearSRGBFieldID = GetStaticFieldIDOrDie(env,
799 gColorSpace_Named_class, "LINEAR_SRGB", "Landroid/graphics/ColorSpace$Named;");
800 gColorSpace_Named_LinearExtendedSRGBFieldID = GetStaticFieldIDOrDie(env,
801 gColorSpace_Named_class, "LINEAR_EXTENDED_SRGB", "Landroid/graphics/ColorSpace$Named;");
802
803 gTransferParameters_class = MakeGlobalRefOrDie(env, FindClassOrDie(env,
804 "android/graphics/ColorSpace$Rgb$TransferParameters"));
805 gTransferParameters_constructorMethodID = GetMethodIDOrDie(env, gTransferParameters_class,
806 "<init>", "(DDDDDDD)V");
807
808 gFontMetrics_class = FindClassOrDie(env, "android/graphics/Paint$FontMetrics");
809 gFontMetrics_class = MakeGlobalRefOrDie(env, gFontMetrics_class);
810
811 gFontMetrics_top = GetFieldIDOrDie(env, gFontMetrics_class, "top", "F");
812 gFontMetrics_ascent = GetFieldIDOrDie(env, gFontMetrics_class, "ascent", "F");
813 gFontMetrics_descent = GetFieldIDOrDie(env, gFontMetrics_class, "descent", "F");
814 gFontMetrics_bottom = GetFieldIDOrDie(env, gFontMetrics_class, "bottom", "F");
815 gFontMetrics_leading = GetFieldIDOrDie(env, gFontMetrics_class, "leading", "F");
816
817 gFontMetricsInt_class = FindClassOrDie(env, "android/graphics/Paint$FontMetricsInt");
818 gFontMetricsInt_class = MakeGlobalRefOrDie(env, gFontMetricsInt_class);
819
820 gFontMetricsInt_top = GetFieldIDOrDie(env, gFontMetricsInt_class, "top", "I");
821 gFontMetricsInt_ascent = GetFieldIDOrDie(env, gFontMetricsInt_class, "ascent", "I");
822 gFontMetricsInt_descent = GetFieldIDOrDie(env, gFontMetricsInt_class, "descent", "I");
823 gFontMetricsInt_bottom = GetFieldIDOrDie(env, gFontMetricsInt_class, "bottom", "I");
824 gFontMetricsInt_leading = GetFieldIDOrDie(env, gFontMetricsInt_class, "leading", "I");
825
826 return 0;
827 }
828