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