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