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