1 #define LOG_TAG "GraphicsJNI"
2
3 #include <unistd.h>
4 #include <sys/mman.h>
5
6 #include "jni.h"
7 #include "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 kIndex_8_SkColorType:
331 return kIndex8_LegacyBitmapConfig;
332 case kAlpha_8_SkColorType:
333 return kA8_LegacyBitmapConfig;
334 case kUnknown_SkColorType:
335 default:
336 break;
337 }
338 return kNo_LegacyBitmapConfig;
339 }
340
legacyBitmapConfigToColorType(jint legacyConfig)341 SkColorType GraphicsJNI::legacyBitmapConfigToColorType(jint legacyConfig) {
342 const uint8_t gConfig2ColorType[] = {
343 kUnknown_SkColorType,
344 kAlpha_8_SkColorType,
345 kIndex_8_SkColorType,
346 kRGB_565_SkColorType,
347 kARGB_4444_SkColorType,
348 kN32_SkColorType,
349 kRGBA_F16_SkColorType,
350 kN32_SkColorType
351 };
352
353 if (legacyConfig < 0 || legacyConfig > kLastEnum_LegacyBitmapConfig) {
354 legacyConfig = kNo_LegacyBitmapConfig;
355 }
356 return static_cast<SkColorType>(gConfig2ColorType[legacyConfig]);
357 }
358
getSkBitmap(JNIEnv * env,jobject bitmap,SkBitmap * outBitmap)359 void GraphicsJNI::getSkBitmap(JNIEnv* env, jobject bitmap, SkBitmap* outBitmap) {
360 bitmap::toBitmap(env, bitmap).getSkBitmap(outBitmap);
361 }
362
refSkPixelRef(JNIEnv * env,jobject jbitmap)363 SkPixelRef* GraphicsJNI::refSkPixelRef(JNIEnv* env, jobject jbitmap) {
364 android::Bitmap& bitmap = android::bitmap::toBitmap(env, jbitmap);
365 bitmap.ref();
366 return &bitmap;
367 }
getNativeBitmapColorType(JNIEnv * env,jobject jconfig)368 SkColorType GraphicsJNI::getNativeBitmapColorType(JNIEnv* env, jobject jconfig) {
369 SkASSERT(env);
370 if (NULL == jconfig) {
371 return kUnknown_SkColorType;
372 }
373 SkASSERT(env->IsInstanceOf(jconfig, gBitmapConfig_class));
374 int c = env->GetIntField(jconfig, gBitmapConfig_nativeInstanceID);
375 return legacyBitmapConfigToColorType(c);
376 }
377
isHardwareConfig(JNIEnv * env,jobject jconfig)378 bool GraphicsJNI::isHardwareConfig(JNIEnv* env, jobject jconfig) {
379 SkASSERT(env);
380 if (NULL == jconfig) {
381 return false;
382 }
383 int c = env->GetIntField(jconfig, gBitmapConfig_nativeInstanceID);
384 return c == kHardware_LegacyBitmapConfig;
385 }
386
hardwareLegacyBitmapConfig()387 jint GraphicsJNI::hardwareLegacyBitmapConfig() {
388 return kHardware_LegacyBitmapConfig;
389 }
390
getNativeCanvas(JNIEnv * env,jobject canvas)391 android::Canvas* GraphicsJNI::getNativeCanvas(JNIEnv* env, jobject canvas) {
392 SkASSERT(env);
393 SkASSERT(canvas);
394 SkASSERT(env->IsInstanceOf(canvas, gCanvas_class));
395 jlong canvasHandle = env->GetLongField(canvas, gCanvas_nativeInstanceID);
396 if (!canvasHandle) {
397 return NULL;
398 }
399 return reinterpret_cast<android::Canvas*>(canvasHandle);
400 }
401
getNativeRegion(JNIEnv * env,jobject region)402 SkRegion* GraphicsJNI::getNativeRegion(JNIEnv* env, jobject region)
403 {
404 SkASSERT(env);
405 SkASSERT(region);
406 SkASSERT(env->IsInstanceOf(region, gRegion_class));
407 jlong regionHandle = env->GetLongField(region, gRegion_nativeInstanceID);
408 SkRegion* r = reinterpret_cast<SkRegion*>(regionHandle);
409 SkASSERT(r);
410 return r;
411 }
412
413 ///////////////////////////////////////////////////////////////////////////////////////////
414
createBitmapRegionDecoder(JNIEnv * env,SkBitmapRegionDecoder * bitmap)415 jobject GraphicsJNI::createBitmapRegionDecoder(JNIEnv* env, SkBitmapRegionDecoder* bitmap)
416 {
417 SkASSERT(bitmap != NULL);
418
419 jobject obj = env->NewObject(gBitmapRegionDecoder_class,
420 gBitmapRegionDecoder_constructorMethodID,
421 reinterpret_cast<jlong>(bitmap));
422 hasException(env); // For the side effect of logging.
423 return obj;
424 }
425
createRegion(JNIEnv * env,SkRegion * region)426 jobject GraphicsJNI::createRegion(JNIEnv* env, SkRegion* region)
427 {
428 SkASSERT(region != NULL);
429 jobject obj = env->NewObject(gRegion_class, gRegion_constructorMethodID,
430 reinterpret_cast<jlong>(region), 0);
431 hasException(env); // For the side effect of logging.
432 return obj;
433 }
434
435 ///////////////////////////////////////////////////////////////////////////////
436
mapAshmemBitmap(JNIEnv * env,SkBitmap * bitmap,SkColorTable * ctable,int fd,void * addr,size_t size,bool readOnly)437 android::Bitmap* GraphicsJNI::mapAshmemBitmap(JNIEnv* env, SkBitmap* bitmap,
438 SkColorTable* ctable, int fd, void* addr, size_t size, bool readOnly) {
439 const SkImageInfo& info = bitmap->info();
440 if (info.colorType() == kUnknown_SkColorType) {
441 doThrowIAE(env, "unknown bitmap configuration");
442 return nullptr;
443 }
444
445 if (!addr) {
446 // Map existing ashmem region if not already mapped.
447 int flags = readOnly ? (PROT_READ) : (PROT_READ | PROT_WRITE);
448 size = ashmem_get_size_region(fd);
449 addr = mmap(NULL, size, flags, MAP_SHARED, fd, 0);
450 if (addr == MAP_FAILED) {
451 return nullptr;
452 }
453 }
454
455 // we must respect the rowBytes value already set on the bitmap instead of
456 // attempting to compute our own.
457 const size_t rowBytes = bitmap->rowBytes();
458
459 auto wrapper = new android::Bitmap(addr, fd, size, info, rowBytes, ctable);
460 wrapper->getSkBitmap(bitmap);
461 if (readOnly) {
462 bitmap->pixelRef()->setImmutable();
463 }
464 // since we're already allocated, we lockPixels right away
465 // HeapAllocator behaves this way too
466 bitmap->lockPixels();
467
468 return wrapper;
469 }
470
defaultColorSpace()471 sk_sp<SkColorSpace> GraphicsJNI::defaultColorSpace() {
472 #ifdef ANDROID_ENABLE_LINEAR_BLENDING
473 return SkColorSpace::MakeSRGB();
474 #else
475 return nullptr;
476 #endif
477 }
478
linearColorSpace()479 sk_sp<SkColorSpace> GraphicsJNI::linearColorSpace() {
480 return SkColorSpace::MakeSRGBLinear();
481 }
482
colorSpaceForType(SkColorType type)483 sk_sp<SkColorSpace> GraphicsJNI::colorSpaceForType(SkColorType type) {
484 switch (type) {
485 case kRGBA_F16_SkColorType:
486 return linearColorSpace();
487 default:
488 return defaultColorSpace();
489 }
490 }
491
isColorSpaceSRGB(SkColorSpace * colorSpace)492 bool GraphicsJNI::isColorSpaceSRGB(SkColorSpace* colorSpace) {
493 return colorSpace == nullptr || colorSpace->isSRGB();
494 }
495
getNativeTransferParameters(JNIEnv * env,jobject transferParams)496 SkColorSpaceTransferFn GraphicsJNI::getNativeTransferParameters(JNIEnv* env, jobject transferParams) {
497 SkColorSpaceTransferFn p;
498 p.fA = (float) env->GetDoubleField(transferParams, gTransferParams_aFieldID);
499 p.fB = (float) env->GetDoubleField(transferParams, gTransferParams_bFieldID);
500 p.fC = (float) env->GetDoubleField(transferParams, gTransferParams_cFieldID);
501 p.fD = (float) env->GetDoubleField(transferParams, gTransferParams_dFieldID);
502 p.fE = (float) env->GetDoubleField(transferParams, gTransferParams_eFieldID);
503 p.fF = (float) env->GetDoubleField(transferParams, gTransferParams_fFieldID);
504 p.fG = (float) env->GetDoubleField(transferParams, gTransferParams_gFieldID);
505 return p;
506 }
507
getNativeXYZMatrix(JNIEnv * env,jfloatArray xyzD50)508 SkMatrix44 GraphicsJNI::getNativeXYZMatrix(JNIEnv* env, jfloatArray xyzD50) {
509 SkMatrix44 xyzMatrix(SkMatrix44::kIdentity_Constructor);
510 jfloat* array = env->GetFloatArrayElements(xyzD50, NULL);
511 xyzMatrix.setFloat(0, 0, array[0]);
512 xyzMatrix.setFloat(1, 0, array[1]);
513 xyzMatrix.setFloat(2, 0, array[2]);
514 xyzMatrix.setFloat(0, 1, array[3]);
515 xyzMatrix.setFloat(1, 1, array[4]);
516 xyzMatrix.setFloat(2, 1, array[5]);
517 xyzMatrix.setFloat(0, 2, array[6]);
518 xyzMatrix.setFloat(1, 2, array[7]);
519 xyzMatrix.setFloat(2, 2, array[8]);
520 env->ReleaseFloatArrayElements(xyzD50, array, 0);
521 return xyzMatrix;
522 }
523
getNativeColorSpace(JNIEnv * env,jobject colorSpace)524 sk_sp<SkColorSpace> GraphicsJNI::getNativeColorSpace(JNIEnv* env, jobject colorSpace) {
525 if (colorSpace == nullptr) return nullptr;
526 if (!env->IsInstanceOf(colorSpace, gColorSpaceRGB_class)) {
527 doThrowIAE(env, "The color space must be an RGB color space");
528 }
529
530 jobject transferParams = env->CallObjectMethod(colorSpace,
531 gColorSpaceRGB_getTransferParametersMethodID);
532 if (transferParams == nullptr) {
533 doThrowIAE(env, "The color space must use an ICC parametric transfer function");
534 }
535
536 jfloatArray illuminantD50 = (jfloatArray) env->GetStaticObjectField(gColorSpace_class,
537 gColorSpace_IlluminantD50FieldID);
538 jobject colorSpaceD50 = env->CallStaticObjectMethod(gColorSpace_class,
539 gColorSpace_adaptMethodID, colorSpace, illuminantD50);
540
541 jfloatArray xyzD50 = (jfloatArray) env->CallObjectMethod(colorSpaceD50,
542 gColorSpaceRGB_getTransformMethodID);
543
544 SkMatrix44 xyzMatrix = getNativeXYZMatrix(env, xyzD50);
545 SkColorSpaceTransferFn transferFunction = getNativeTransferParameters(env, transferParams);
546
547 return SkColorSpace::MakeRGB(transferFunction, xyzMatrix);
548 }
549
550
getColorSpace(JNIEnv * env,sk_sp<SkColorSpace> & decodeColorSpace,SkColorType decodeColorType)551 jobject GraphicsJNI::getColorSpace(JNIEnv* env, sk_sp<SkColorSpace>& decodeColorSpace,
552 SkColorType decodeColorType) {
553 jobject colorSpace = nullptr;
554
555 // No need to match, we know what the output color space will be
556 if (decodeColorType == kRGBA_F16_SkColorType) {
557 jobject linearExtendedSRGB = env->GetStaticObjectField(
558 gColorSpace_Named_class, gColorSpace_Named_LinearExtendedSRGBFieldID);
559 colorSpace = env->CallStaticObjectMethod(gColorSpace_class,
560 gColorSpace_getMethodID, linearExtendedSRGB);
561 } else {
562 // Same here, no need to match
563 if (decodeColorSpace->isSRGB()) {
564 jobject sRGB = env->GetStaticObjectField(
565 gColorSpace_Named_class, gColorSpace_Named_sRGBFieldID);
566 colorSpace = env->CallStaticObjectMethod(gColorSpace_class,
567 gColorSpace_getMethodID, sRGB);
568 } else if (decodeColorSpace.get() != nullptr) {
569 // Try to match against known RGB color spaces using the CIE XYZ D50
570 // conversion matrix and numerical transfer function parameters
571 SkMatrix44 xyzMatrix(SkMatrix44::kUninitialized_Constructor);
572 LOG_ALWAYS_FATAL_IF(!decodeColorSpace->toXYZD50(&xyzMatrix));
573
574 SkColorSpaceTransferFn transferParams;
575 // We can only handle numerical transfer functions at the moment
576 LOG_ALWAYS_FATAL_IF(!decodeColorSpace->isNumericalTransferFn(&transferParams));
577
578 jobject params = env->NewObject(gTransferParameters_class,
579 gTransferParameters_constructorMethodID,
580 transferParams.fA, transferParams.fB, transferParams.fC,
581 transferParams.fD, transferParams.fE, transferParams.fF,
582 transferParams.fG);
583
584 jfloatArray xyzArray = env->NewFloatArray(9);
585 jfloat xyz[9] = {
586 xyzMatrix.getFloat(0, 0),
587 xyzMatrix.getFloat(1, 0),
588 xyzMatrix.getFloat(2, 0),
589 xyzMatrix.getFloat(0, 1),
590 xyzMatrix.getFloat(1, 1),
591 xyzMatrix.getFloat(2, 1),
592 xyzMatrix.getFloat(0, 2),
593 xyzMatrix.getFloat(1, 2),
594 xyzMatrix.getFloat(2, 2)
595 };
596 env->SetFloatArrayRegion(xyzArray, 0, 9, xyz);
597
598 colorSpace = env->CallStaticObjectMethod(gColorSpace_class,
599 gColorSpace_matchMethodID, xyzArray, params);
600
601 if (colorSpace == nullptr) {
602 // We couldn't find an exact match, let's create a new color space
603 // instance with the 3x3 conversion matrix and transfer function
604 colorSpace = env->NewObject(gColorSpaceRGB_class,
605 gColorSpaceRGB_constructorMethodID,
606 env->NewStringUTF("Unknown"), xyzArray, params);
607 }
608
609 env->DeleteLocalRef(xyzArray);
610 }
611 }
612 return colorSpace;
613 }
614
615 ///////////////////////////////////////////////////////////////////////////////
allocPixelRef(SkBitmap * bitmap,SkColorTable * ctable)616 bool HeapAllocator::allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable) {
617 mStorage = android::Bitmap::allocateHeapBitmap(bitmap, ctable);
618 return !!mStorage;
619 }
620
621 ////////////////////////////////////////////////////////////////////////////////
622
RecyclingClippingPixelAllocator(android::Bitmap * recycledBitmap,size_t recycledBytes)623 RecyclingClippingPixelAllocator::RecyclingClippingPixelAllocator(
624 android::Bitmap* recycledBitmap, size_t recycledBytes)
625 : mRecycledBitmap(recycledBitmap)
626 , mRecycledBytes(recycledBytes)
627 , mSkiaBitmap(nullptr)
628 , mNeedsCopy(false)
629 {}
630
~RecyclingClippingPixelAllocator()631 RecyclingClippingPixelAllocator::~RecyclingClippingPixelAllocator() {}
632
allocPixelRef(SkBitmap * bitmap,SkColorTable * ctable)633 bool RecyclingClippingPixelAllocator::allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable) {
634 // Ensure that the caller did not pass in a NULL bitmap to the constructor or this
635 // function.
636 LOG_ALWAYS_FATAL_IF(!mRecycledBitmap);
637 LOG_ALWAYS_FATAL_IF(!bitmap);
638 mSkiaBitmap = bitmap;
639
640 // This behaves differently than the RecyclingPixelAllocator. For backwards
641 // compatibility, the original color type of the recycled bitmap must be maintained.
642 if (mRecycledBitmap->info().colorType() != bitmap->colorType()) {
643 return false;
644 }
645
646 // The Skia bitmap specifies the width and height needed by the decoder.
647 // mRecycledBitmap specifies the width and height of the bitmap that we
648 // want to reuse. Neither can be changed. We will try to find a way
649 // to reuse the memory.
650 const int maxWidth = SkTMax(bitmap->width(), mRecycledBitmap->info().width());
651 const int maxHeight = SkTMax(bitmap->height(), mRecycledBitmap->info().height());
652 const SkImageInfo maxInfo = bitmap->info().makeWH(maxWidth, maxHeight);
653 const size_t rowBytes = maxInfo.minRowBytes();
654 const size_t bytesNeeded = maxInfo.getSafeSize(rowBytes);
655 if (bytesNeeded <= mRecycledBytes) {
656 // Here we take advantage of reconfigure() to reset the rowBytes and ctable
657 // of mRecycledBitmap. It is very important that we pass in
658 // mRecycledBitmap->info() for the SkImageInfo. According to the
659 // specification for BitmapRegionDecoder, we are not allowed to change
660 // the SkImageInfo.
661 // We can (must) preserve the color space since it doesn't affect the
662 // storage needs
663 mRecycledBitmap->reconfigure(
664 mRecycledBitmap->info().makeColorSpace(bitmap->refColorSpace()),
665 rowBytes, ctable);
666
667 // Give the bitmap the same pixelRef as mRecycledBitmap.
668 // skbug.com/4538: We also need to make sure that the rowBytes on the pixel ref
669 // match the rowBytes on the bitmap.
670 bitmap->setInfo(bitmap->info(), rowBytes);
671 mRecycledBitmap->ref();
672 bitmap->setPixelRef(mRecycledBitmap)->unref();
673
674 // Make sure that the recycled bitmap has the correct alpha type.
675 mRecycledBitmap->setAlphaType(bitmap->alphaType());
676
677 bitmap->notifyPixelsChanged();
678 bitmap->lockPixels();
679 mNeedsCopy = false;
680
681 // TODO: If the dimensions of the SkBitmap are smaller than those of
682 // mRecycledBitmap, should we zero the memory in mRecycledBitmap?
683 return true;
684 }
685
686 // In the event that mRecycledBitmap is not large enough, allocate new memory
687 // on the heap.
688 SkBitmap::HeapAllocator heapAllocator;
689
690 // We will need to copy from heap memory to mRecycledBitmap's memory after the
691 // decode is complete.
692 mNeedsCopy = true;
693
694 return heapAllocator.allocPixelRef(bitmap, ctable);
695 }
696
copyIfNecessary()697 void RecyclingClippingPixelAllocator::copyIfNecessary() {
698 if (mNeedsCopy) {
699 mRecycledBitmap->ref();
700 SkPixelRef* recycledPixels = mRecycledBitmap;
701 void* dst = recycledPixels->pixels();
702 const size_t dstRowBytes = mRecycledBitmap->rowBytes();
703 const size_t bytesToCopy = std::min(mRecycledBitmap->info().minRowBytes(),
704 mSkiaBitmap->info().minRowBytes());
705 const int rowsToCopy = std::min(mRecycledBitmap->info().height(),
706 mSkiaBitmap->info().height());
707 for (int y = 0; y < rowsToCopy; y++) {
708 memcpy(dst, mSkiaBitmap->getAddr(0, y), bytesToCopy);
709 dst = SkTAddOffset<void>(dst, dstRowBytes);
710 }
711 recycledPixels->notifyPixelsChanged();
712 recycledPixels->unref();
713 }
714 mRecycledBitmap = nullptr;
715 mSkiaBitmap = nullptr;
716 }
717
718 ////////////////////////////////////////////////////////////////////////////////
719
AshmemPixelAllocator(JNIEnv * env)720 AshmemPixelAllocator::AshmemPixelAllocator(JNIEnv *env) {
721 LOG_ALWAYS_FATAL_IF(env->GetJavaVM(&mJavaVM) != JNI_OK,
722 "env->GetJavaVM failed");
723 }
724
allocPixelRef(SkBitmap * bitmap,SkColorTable * ctable)725 bool AshmemPixelAllocator::allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable) {
726 mStorage = android::Bitmap::allocateAshmemBitmap(bitmap, ctable);
727 return !!mStorage;
728 }
729
730 ////////////////////////////////////////////////////////////////////////////////
731
register_android_graphics_Graphics(JNIEnv * env)732 int register_android_graphics_Graphics(JNIEnv* env)
733 {
734 jmethodID m;
735 jclass c;
736
737 gRect_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/Rect"));
738 gRect_leftFieldID = GetFieldIDOrDie(env, gRect_class, "left", "I");
739 gRect_topFieldID = GetFieldIDOrDie(env, gRect_class, "top", "I");
740 gRect_rightFieldID = GetFieldIDOrDie(env, gRect_class, "right", "I");
741 gRect_bottomFieldID = GetFieldIDOrDie(env, gRect_class, "bottom", "I");
742
743 gRectF_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/RectF"));
744 gRectF_leftFieldID = GetFieldIDOrDie(env, gRectF_class, "left", "F");
745 gRectF_topFieldID = GetFieldIDOrDie(env, gRectF_class, "top", "F");
746 gRectF_rightFieldID = GetFieldIDOrDie(env, gRectF_class, "right", "F");
747 gRectF_bottomFieldID = GetFieldIDOrDie(env, gRectF_class, "bottom", "F");
748
749 gPoint_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/Point"));
750 gPoint_xFieldID = GetFieldIDOrDie(env, gPoint_class, "x", "I");
751 gPoint_yFieldID = GetFieldIDOrDie(env, gPoint_class, "y", "I");
752
753 gPointF_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/PointF"));
754 gPointF_xFieldID = GetFieldIDOrDie(env, gPointF_class, "x", "F");
755 gPointF_yFieldID = GetFieldIDOrDie(env, gPointF_class, "y", "F");
756
757 gBitmapRegionDecoder_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/BitmapRegionDecoder"));
758 gBitmapRegionDecoder_constructorMethodID = GetMethodIDOrDie(env, gBitmapRegionDecoder_class, "<init>", "(J)V");
759
760 gBitmapConfig_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/Bitmap$Config"));
761 gBitmapConfig_nativeInstanceID = GetFieldIDOrDie(env, gBitmapConfig_class, "nativeInt", "I");
762
763 gCanvas_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/Canvas"));
764 gCanvas_nativeInstanceID = GetFieldIDOrDie(env, gCanvas_class, "mNativeCanvasWrapper", "J");
765
766 gPicture_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/Picture"));
767 gPicture_nativeInstanceID = GetFieldIDOrDie(env, gPicture_class, "mNativePicture", "J");
768
769 gRegion_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/Region"));
770 gRegion_nativeInstanceID = GetFieldIDOrDie(env, gRegion_class, "mNativeRegion", "J");
771 gRegion_constructorMethodID = GetMethodIDOrDie(env, gRegion_class, "<init>", "(JI)V");
772
773 c = env->FindClass("java/lang/Byte");
774 gByte_class = (jclass) env->NewGlobalRef(
775 env->GetStaticObjectField(c, env->GetStaticFieldID(c, "TYPE", "Ljava/lang/Class;")));
776
777 gVMRuntime_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "dalvik/system/VMRuntime"));
778 m = env->GetStaticMethodID(gVMRuntime_class, "getRuntime", "()Ldalvik/system/VMRuntime;");
779 gVMRuntime = env->NewGlobalRef(env->CallStaticObjectMethod(gVMRuntime_class, m));
780 gVMRuntime_newNonMovableArray = GetMethodIDOrDie(env, gVMRuntime_class, "newNonMovableArray",
781 "(Ljava/lang/Class;I)Ljava/lang/Object;");
782 gVMRuntime_addressOf = GetMethodIDOrDie(env, gVMRuntime_class, "addressOf", "(Ljava/lang/Object;)J");
783
784 jclass transfer_params_class = FindClassOrDie(env, "android/graphics/ColorSpace$Rgb$TransferParameters");
785 gTransferParams_aFieldID = GetFieldIDOrDie(env, transfer_params_class, "a", "D");
786 gTransferParams_bFieldID = GetFieldIDOrDie(env, transfer_params_class, "b", "D");
787 gTransferParams_cFieldID = GetFieldIDOrDie(env, transfer_params_class, "c", "D");
788 gTransferParams_dFieldID = GetFieldIDOrDie(env, transfer_params_class, "d", "D");
789 gTransferParams_eFieldID = GetFieldIDOrDie(env, transfer_params_class, "e", "D");
790 gTransferParams_fFieldID = GetFieldIDOrDie(env, transfer_params_class, "f", "D");
791 gTransferParams_gFieldID = GetFieldIDOrDie(env, transfer_params_class, "g", "D");
792
793 gColorSpace_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/ColorSpace"));
794 gColorSpace_IlluminantD50FieldID = GetStaticFieldIDOrDie(env,
795 gColorSpace_class, "ILLUMINANT_D50", "[F");
796 gColorSpace_adaptMethodID = GetStaticMethodIDOrDie(env, gColorSpace_class, "adapt",
797 "(Landroid/graphics/ColorSpace;[F)Landroid/graphics/ColorSpace;");
798 gColorSpace_getMethodID = GetStaticMethodIDOrDie(env, gColorSpace_class,
799 "get", "(Landroid/graphics/ColorSpace$Named;)Landroid/graphics/ColorSpace;");
800 gColorSpace_matchMethodID = GetStaticMethodIDOrDie(env, gColorSpace_class, "match",
801 "([FLandroid/graphics/ColorSpace$Rgb$TransferParameters;)Landroid/graphics/ColorSpace;");
802
803 gColorSpaceRGB_class = MakeGlobalRefOrDie(env,
804 FindClassOrDie(env, "android/graphics/ColorSpace$Rgb"));
805 gColorSpaceRGB_constructorMethodID = GetMethodIDOrDie(env, gColorSpaceRGB_class,
806 "<init>", "(Ljava/lang/String;[FLandroid/graphics/ColorSpace$Rgb$TransferParameters;)V");
807 gColorSpaceRGB_getTransferParametersMethodID = GetMethodIDOrDie(env, gColorSpaceRGB_class,
808 "getTransferParameters", "()Landroid/graphics/ColorSpace$Rgb$TransferParameters;");
809 gColorSpaceRGB_getTransformMethodID = GetMethodIDOrDie(env, gColorSpaceRGB_class,
810 "getTransform", "()[F");
811
812 gColorSpace_Named_class = MakeGlobalRefOrDie(env,
813 FindClassOrDie(env, "android/graphics/ColorSpace$Named"));
814 gColorSpace_Named_sRGBFieldID = GetStaticFieldIDOrDie(env,
815 gColorSpace_Named_class, "SRGB", "Landroid/graphics/ColorSpace$Named;");
816 gColorSpace_Named_LinearExtendedSRGBFieldID = GetStaticFieldIDOrDie(env,
817 gColorSpace_Named_class, "LINEAR_EXTENDED_SRGB", "Landroid/graphics/ColorSpace$Named;");
818
819 gTransferParameters_class = MakeGlobalRefOrDie(env, FindClassOrDie(env,
820 "android/graphics/ColorSpace$Rgb$TransferParameters"));
821 gTransferParameters_constructorMethodID = GetMethodIDOrDie(env, gTransferParameters_class,
822 "<init>", "(DDDDDDD)V");
823
824 return 0;
825 }
826