• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #undef LOG_TAG
2 #define LOG_TAG "GraphicsJNI"
3 
4 #include <assert.h>
5 #include <unistd.h>
6 
7 #include "jni.h"
8 #include <nativehelper/JNIHelp.h>
9 #include "GraphicsJNI.h"
10 
11 #include "SkCanvas.h"
12 #include "SkFontMetrics.h"
13 #include "SkMath.h"
14 #include "SkRegion.h"
15 #include <cutils/ashmem.h>
16 #include <hwui/Canvas.h>
17 
18 using namespace android;
19 
20 /*static*/ JavaVM* GraphicsJNI::mJavaVM = nullptr;
21 
setJavaVM(JavaVM * javaVM)22 void GraphicsJNI::setJavaVM(JavaVM* javaVM) {
23   mJavaVM = javaVM;
24 }
25 
26 /** return a pointer to the JNIEnv for this thread */
getJNIEnv()27 JNIEnv* GraphicsJNI::getJNIEnv() {
28   assert(mJavaVM != nullptr);
29   JNIEnv* env;
30   if (mJavaVM->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
31       return nullptr;
32   }
33   return env;
34 }
35 
36 /** create a JNIEnv* for this thread or assert if one already exists */
attachJNIEnv(const char * envName)37 JNIEnv* GraphicsJNI::attachJNIEnv(const char* envName) {
38   assert(getJNIEnv() == nullptr);
39   JNIEnv* env = nullptr;
40   JavaVMAttachArgs args = { JNI_VERSION_1_4, envName, NULL };
41   int result = mJavaVM->AttachCurrentThread(&env, (void*) &args);
42   if (result != JNI_OK) {
43       ALOGE("thread attach failed: %#x", result);
44   }
45   return env;
46 }
47 
48 /** detach the current thread from the JavaVM */
detachJNIEnv()49 void GraphicsJNI::detachJNIEnv() {
50   assert(mJavaVM != nullptr);
51   mJavaVM->DetachCurrentThread();
52 }
53 
doThrowNPE(JNIEnv * env)54 void doThrowNPE(JNIEnv* env) {
55     jniThrowNullPointerException(env, NULL);
56 }
57 
doThrowAIOOBE(JNIEnv * env)58 void doThrowAIOOBE(JNIEnv* env) {
59     jniThrowException(env, "java/lang/ArrayIndexOutOfBoundsException", NULL);
60 }
61 
doThrowRE(JNIEnv * env,const char * msg)62 void doThrowRE(JNIEnv* env, const char* msg) {
63     jniThrowRuntimeException(env, msg);
64 }
65 
doThrowIAE(JNIEnv * env,const char * msg)66 void doThrowIAE(JNIEnv* env, const char* msg) {
67     jniThrowException(env, "java/lang/IllegalArgumentException", msg);
68 }
69 
doThrowISE(JNIEnv * env,const char * msg)70 void doThrowISE(JNIEnv* env, const char* msg) {
71     jniThrowException(env, "java/lang/IllegalStateException", msg);
72 }
73 
doThrowOOME(JNIEnv * env,const char * msg)74 void doThrowOOME(JNIEnv* env, const char* msg) {
75     jniThrowException(env, "java/lang/OutOfMemoryError", msg);
76 }
77 
doThrowIOE(JNIEnv * env,const char * msg)78 void doThrowIOE(JNIEnv* env, const char* msg) {
79     jniThrowException(env, "java/io/IOException", msg);
80 }
81 
hasException(JNIEnv * env)82 bool GraphicsJNI::hasException(JNIEnv *env) {
83     if (env->ExceptionCheck() != 0) {
84         ALOGE("*** Uncaught exception returned from Java call!\n");
85         env->ExceptionDescribe();
86         return true;
87     }
88     return false;
89 }
90 
91 ///////////////////////////////////////////////////////////////////////////////
92 
AutoJavaFloatArray(JNIEnv * env,jfloatArray array,int minLength,JNIAccess access)93 AutoJavaFloatArray::AutoJavaFloatArray(JNIEnv* env, jfloatArray array,
94                                        int minLength, JNIAccess access)
95 : fEnv(env), fArray(array), fPtr(NULL), fLen(0) {
96     ALOG_ASSERT(env);
97     if (array) {
98         fLen = env->GetArrayLength(array);
99         if (fLen < minLength) {
100             LOG_ALWAYS_FATAL("bad length");
101         }
102         fPtr = env->GetFloatArrayElements(array, NULL);
103     }
104     fReleaseMode = (access == kRO_JNIAccess) ? JNI_ABORT : 0;
105 }
106 
~AutoJavaFloatArray()107 AutoJavaFloatArray::~AutoJavaFloatArray() {
108     if (fPtr) {
109         fEnv->ReleaseFloatArrayElements(fArray, fPtr, fReleaseMode);
110     }
111 }
112 
AutoJavaIntArray(JNIEnv * env,jintArray array,int minLength)113 AutoJavaIntArray::AutoJavaIntArray(JNIEnv* env, jintArray array,
114                                        int minLength)
115 : fEnv(env), fArray(array), fPtr(NULL), fLen(0) {
116     ALOG_ASSERT(env);
117     if (array) {
118         fLen = env->GetArrayLength(array);
119         if (fLen < minLength) {
120             LOG_ALWAYS_FATAL("bad length");
121         }
122         fPtr = env->GetIntArrayElements(array, NULL);
123     }
124 }
125 
~AutoJavaIntArray()126 AutoJavaIntArray::~AutoJavaIntArray() {
127     if (fPtr) {
128         fEnv->ReleaseIntArrayElements(fArray, fPtr, 0);
129     }
130 }
131 
AutoJavaShortArray(JNIEnv * env,jshortArray array,int minLength,JNIAccess access)132 AutoJavaShortArray::AutoJavaShortArray(JNIEnv* env, jshortArray array,
133                                        int minLength, JNIAccess access)
134 : fEnv(env), fArray(array), fPtr(NULL), fLen(0) {
135     ALOG_ASSERT(env);
136     if (array) {
137         fLen = env->GetArrayLength(array);
138         if (fLen < minLength) {
139             LOG_ALWAYS_FATAL("bad length");
140         }
141         fPtr = env->GetShortArrayElements(array, NULL);
142     }
143     fReleaseMode = (access == kRO_JNIAccess) ? JNI_ABORT : 0;
144 }
145 
~AutoJavaShortArray()146 AutoJavaShortArray::~AutoJavaShortArray() {
147     if (fPtr) {
148         fEnv->ReleaseShortArrayElements(fArray, fPtr, fReleaseMode);
149     }
150 }
151 
AutoJavaByteArray(JNIEnv * env,jbyteArray array,int minLength)152 AutoJavaByteArray::AutoJavaByteArray(JNIEnv* env, jbyteArray array,
153                                        int minLength)
154 : fEnv(env), fArray(array), fPtr(NULL), fLen(0) {
155     ALOG_ASSERT(env);
156     if (array) {
157         fLen = env->GetArrayLength(array);
158         if (fLen < minLength) {
159             LOG_ALWAYS_FATAL("bad length");
160         }
161         fPtr = env->GetByteArrayElements(array, NULL);
162     }
163 }
164 
~AutoJavaByteArray()165 AutoJavaByteArray::~AutoJavaByteArray() {
166     if (fPtr) {
167         fEnv->ReleaseByteArrayElements(fArray, fPtr, 0);
168     }
169 }
170 
171 ///////////////////////////////////////////////////////////////////////////////
172 
173 static jclass   gRect_class;
174 static jfieldID gRect_leftFieldID;
175 static jfieldID gRect_topFieldID;
176 static jfieldID gRect_rightFieldID;
177 static jfieldID gRect_bottomFieldID;
178 
179 static jclass   gRectF_class;
180 static jfieldID gRectF_leftFieldID;
181 static jfieldID gRectF_topFieldID;
182 static jfieldID gRectF_rightFieldID;
183 static jfieldID gRectF_bottomFieldID;
184 
185 static jclass   gPoint_class;
186 static jfieldID gPoint_xFieldID;
187 static jfieldID gPoint_yFieldID;
188 
189 static jclass   gPointF_class;
190 static jfieldID gPointF_xFieldID;
191 static jfieldID gPointF_yFieldID;
192 
193 static jclass   gBitmapConfig_class;
194 static jfieldID gBitmapConfig_nativeInstanceID;
195 static jmethodID gBitmapConfig_nativeToConfigMethodID;
196 
197 static jclass   gBitmapRegionDecoder_class;
198 static jmethodID gBitmapRegionDecoder_constructorMethodID;
199 
200 static jclass   gCanvas_class;
201 static jfieldID gCanvas_nativeInstanceID;
202 
203 static jclass   gPicture_class;
204 static jfieldID gPicture_nativeInstanceID;
205 
206 static jclass   gRegion_class;
207 static jfieldID gRegion_nativeInstanceID;
208 static jmethodID gRegion_constructorMethodID;
209 
210 static jclass    gByte_class;
211 static jobject   gVMRuntime;
212 static jclass    gVMRuntime_class;
213 static jmethodID gVMRuntime_newNonMovableArray;
214 static jmethodID gVMRuntime_addressOf;
215 
216 static jclass gColorSpace_class;
217 static jmethodID gColorSpace_getMethodID;
218 static jmethodID gColorSpace_matchMethodID;
219 
220 static jclass gColorSpaceRGB_class;
221 static jmethodID gColorSpaceRGB_constructorMethodID;
222 
223 static jclass gColorSpace_Named_class;
224 static jfieldID gColorSpace_Named_sRGBFieldID;
225 static jfieldID gColorSpace_Named_ExtendedSRGBFieldID;
226 static jfieldID gColorSpace_Named_LinearSRGBFieldID;
227 static jfieldID gColorSpace_Named_LinearExtendedSRGBFieldID;
228 
229 static jclass gTransferParameters_class;
230 static jmethodID gTransferParameters_constructorMethodID;
231 
232 static jclass   gFontMetrics_class;
233 static jfieldID gFontMetrics_top;
234 static jfieldID gFontMetrics_ascent;
235 static jfieldID gFontMetrics_descent;
236 static jfieldID gFontMetrics_bottom;
237 static jfieldID gFontMetrics_leading;
238 
239 static jclass   gFontMetricsInt_class;
240 static jfieldID gFontMetricsInt_top;
241 static jfieldID gFontMetricsInt_ascent;
242 static jfieldID gFontMetricsInt_descent;
243 static jfieldID gFontMetricsInt_bottom;
244 static jfieldID gFontMetricsInt_leading;
245 
246 ///////////////////////////////////////////////////////////////////////////////
247 
get_jrect(JNIEnv * env,jobject obj,int * L,int * T,int * R,int * B)248 void GraphicsJNI::get_jrect(JNIEnv* env, jobject obj, int* L, int* T, int* R, int* B)
249 {
250     ALOG_ASSERT(env->IsInstanceOf(obj, gRect_class));
251 
252     *L = env->GetIntField(obj, gRect_leftFieldID);
253     *T = env->GetIntField(obj, gRect_topFieldID);
254     *R = env->GetIntField(obj, gRect_rightFieldID);
255     *B = env->GetIntField(obj, gRect_bottomFieldID);
256 }
257 
set_jrect(JNIEnv * env,jobject obj,int L,int T,int R,int B)258 void GraphicsJNI::set_jrect(JNIEnv* env, jobject obj, int L, int T, int R, int B)
259 {
260     ALOG_ASSERT(env->IsInstanceOf(obj, gRect_class));
261 
262     env->SetIntField(obj, gRect_leftFieldID, L);
263     env->SetIntField(obj, gRect_topFieldID, T);
264     env->SetIntField(obj, gRect_rightFieldID, R);
265     env->SetIntField(obj, gRect_bottomFieldID, B);
266 }
267 
jrect_to_irect(JNIEnv * env,jobject obj,SkIRect * ir)268 SkIRect* GraphicsJNI::jrect_to_irect(JNIEnv* env, jobject obj, SkIRect* ir)
269 {
270     ALOG_ASSERT(env->IsInstanceOf(obj, gRect_class));
271 
272     ir->setLTRB(env->GetIntField(obj, gRect_leftFieldID),
273                 env->GetIntField(obj, gRect_topFieldID),
274                 env->GetIntField(obj, gRect_rightFieldID),
275                 env->GetIntField(obj, gRect_bottomFieldID));
276     return ir;
277 }
278 
irect_to_jrect(const SkIRect & ir,JNIEnv * env,jobject obj)279 void GraphicsJNI::irect_to_jrect(const SkIRect& ir, JNIEnv* env, jobject obj)
280 {
281     ALOG_ASSERT(env->IsInstanceOf(obj, gRect_class));
282 
283     env->SetIntField(obj, gRect_leftFieldID, ir.fLeft);
284     env->SetIntField(obj, gRect_topFieldID, ir.fTop);
285     env->SetIntField(obj, gRect_rightFieldID, ir.fRight);
286     env->SetIntField(obj, gRect_bottomFieldID, ir.fBottom);
287 }
288 
jrectf_to_rect(JNIEnv * env,jobject obj,SkRect * r)289 SkRect* GraphicsJNI::jrectf_to_rect(JNIEnv* env, jobject obj, SkRect* r)
290 {
291     ALOG_ASSERT(env->IsInstanceOf(obj, gRectF_class));
292 
293     r->setLTRB(env->GetFloatField(obj, gRectF_leftFieldID),
294                env->GetFloatField(obj, gRectF_topFieldID),
295                env->GetFloatField(obj, gRectF_rightFieldID),
296                env->GetFloatField(obj, gRectF_bottomFieldID));
297     return r;
298 }
299 
jrect_to_rect(JNIEnv * env,jobject obj,SkRect * r)300 SkRect* GraphicsJNI::jrect_to_rect(JNIEnv* env, jobject obj, SkRect* r)
301 {
302     ALOG_ASSERT(env->IsInstanceOf(obj, gRect_class));
303 
304     r->setLTRB(SkIntToScalar(env->GetIntField(obj, gRect_leftFieldID)),
305                SkIntToScalar(env->GetIntField(obj, gRect_topFieldID)),
306                SkIntToScalar(env->GetIntField(obj, gRect_rightFieldID)),
307                SkIntToScalar(env->GetIntField(obj, gRect_bottomFieldID)));
308     return r;
309 }
310 
rect_to_jrectf(const SkRect & r,JNIEnv * env,jobject obj)311 void GraphicsJNI::rect_to_jrectf(const SkRect& r, JNIEnv* env, jobject obj)
312 {
313     ALOG_ASSERT(env->IsInstanceOf(obj, gRectF_class));
314 
315     env->SetFloatField(obj, gRectF_leftFieldID, SkScalarToFloat(r.fLeft));
316     env->SetFloatField(obj, gRectF_topFieldID, SkScalarToFloat(r.fTop));
317     env->SetFloatField(obj, gRectF_rightFieldID, SkScalarToFloat(r.fRight));
318     env->SetFloatField(obj, gRectF_bottomFieldID, SkScalarToFloat(r.fBottom));
319 }
320 
jpoint_to_ipoint(JNIEnv * env,jobject obj,SkIPoint * point)321 SkIPoint* GraphicsJNI::jpoint_to_ipoint(JNIEnv* env, jobject obj, SkIPoint* point)
322 {
323     ALOG_ASSERT(env->IsInstanceOf(obj, gPoint_class));
324 
325     point->set(env->GetIntField(obj, gPoint_xFieldID),
326                env->GetIntField(obj, gPoint_yFieldID));
327     return point;
328 }
329 
ipoint_to_jpoint(const SkIPoint & ir,JNIEnv * env,jobject obj)330 void GraphicsJNI::ipoint_to_jpoint(const SkIPoint& ir, JNIEnv* env, jobject obj)
331 {
332     ALOG_ASSERT(env->IsInstanceOf(obj, gPoint_class));
333 
334     env->SetIntField(obj, gPoint_xFieldID, ir.fX);
335     env->SetIntField(obj, gPoint_yFieldID, ir.fY);
336 }
337 
jpointf_to_point(JNIEnv * env,jobject obj,SkPoint * point)338 SkPoint* GraphicsJNI::jpointf_to_point(JNIEnv* env, jobject obj, SkPoint* point)
339 {
340     ALOG_ASSERT(env->IsInstanceOf(obj, gPointF_class));
341 
342     point->set(env->GetIntField(obj, gPointF_xFieldID),
343                env->GetIntField(obj, gPointF_yFieldID));
344     return point;
345 }
346 
point_to_jpointf(const SkPoint & r,JNIEnv * env,jobject obj)347 void GraphicsJNI::point_to_jpointf(const SkPoint& r, JNIEnv* env, jobject obj)
348 {
349     ALOG_ASSERT(env->IsInstanceOf(obj, gPointF_class));
350 
351     env->SetFloatField(obj, gPointF_xFieldID, SkScalarToFloat(r.fX));
352     env->SetFloatField(obj, gPointF_yFieldID, SkScalarToFloat(r.fY));
353 }
354 
355 // See enum values in GraphicsJNI.h
colorTypeToLegacyBitmapConfig(SkColorType colorType)356 jint GraphicsJNI::colorTypeToLegacyBitmapConfig(SkColorType colorType) {
357     switch (colorType) {
358         case kRGBA_F16_SkColorType:
359             return kRGBA_16F_LegacyBitmapConfig;
360         case kN32_SkColorType:
361             return kARGB_8888_LegacyBitmapConfig;
362         case kARGB_4444_SkColorType:
363             return kARGB_4444_LegacyBitmapConfig;
364         case kRGB_565_SkColorType:
365             return kRGB_565_LegacyBitmapConfig;
366         case kAlpha_8_SkColorType:
367             return kA8_LegacyBitmapConfig;
368         case kRGBA_1010102_SkColorType:
369             return kRGBA_1010102_LegacyBitmapConfig;
370         case kUnknown_SkColorType:
371         default:
372             break;
373     }
374     return kNo_LegacyBitmapConfig;
375 }
376 
legacyBitmapConfigToColorType(jint legacyConfig)377 SkColorType GraphicsJNI::legacyBitmapConfigToColorType(jint legacyConfig) {
378     const uint8_t gConfig2ColorType[] = {
379             kUnknown_SkColorType,  kAlpha_8_SkColorType,
380             kUnknown_SkColorType,  // Previously kIndex_8_SkColorType,
381             kRGB_565_SkColorType,  kARGB_4444_SkColorType, kN32_SkColorType,
382             kRGBA_F16_SkColorType, kN32_SkColorType,       kRGBA_1010102_SkColorType,
383     };
384 
385     if (legacyConfig < 0 || legacyConfig > kLastEnum_LegacyBitmapConfig) {
386         legacyConfig = kNo_LegacyBitmapConfig;
387     }
388     return static_cast<SkColorType>(gConfig2ColorType[legacyConfig]);
389 }
390 
getFormatFromConfig(JNIEnv * env,jobject jconfig)391 AndroidBitmapFormat GraphicsJNI::getFormatFromConfig(JNIEnv* env, jobject jconfig) {
392     ALOG_ASSERT(env);
393     if (NULL == jconfig) {
394         return ANDROID_BITMAP_FORMAT_NONE;
395     }
396     ALOG_ASSERT(env->IsInstanceOf(jconfig, gBitmapConfig_class));
397     jint javaConfigId = env->GetIntField(jconfig, gBitmapConfig_nativeInstanceID);
398 
399     const AndroidBitmapFormat config2BitmapFormat[] = {
400             ANDROID_BITMAP_FORMAT_NONE,        ANDROID_BITMAP_FORMAT_A_8,
401             ANDROID_BITMAP_FORMAT_NONE,  // Previously Config.Index_8
402             ANDROID_BITMAP_FORMAT_RGB_565,     ANDROID_BITMAP_FORMAT_RGBA_4444,
403             ANDROID_BITMAP_FORMAT_RGBA_8888,   ANDROID_BITMAP_FORMAT_RGBA_F16,
404             ANDROID_BITMAP_FORMAT_NONE,  // Congfig.HARDWARE
405             ANDROID_BITMAP_FORMAT_RGBA_1010102};
406     return config2BitmapFormat[javaConfigId];
407 }
408 
getConfigFromFormat(JNIEnv * env,AndroidBitmapFormat format)409 jobject GraphicsJNI::getConfigFromFormat(JNIEnv* env, AndroidBitmapFormat format) {
410     ALOG_ASSERT(env);
411     jint configId = kNo_LegacyBitmapConfig;
412     switch (format) {
413       case ANDROID_BITMAP_FORMAT_A_8:
414         configId = kA8_LegacyBitmapConfig;
415         break;
416       case ANDROID_BITMAP_FORMAT_RGB_565:
417         configId = kRGB_565_LegacyBitmapConfig;
418         break;
419       case ANDROID_BITMAP_FORMAT_RGBA_4444:
420         configId = kARGB_4444_LegacyBitmapConfig;
421         break;
422       case ANDROID_BITMAP_FORMAT_RGBA_8888:
423         configId = kARGB_8888_LegacyBitmapConfig;
424         break;
425       case ANDROID_BITMAP_FORMAT_RGBA_F16:
426         configId = kRGBA_16F_LegacyBitmapConfig;
427         break;
428       case ANDROID_BITMAP_FORMAT_RGBA_1010102:
429           configId = kRGBA_1010102_LegacyBitmapConfig;
430           break;
431       default:
432         break;
433     }
434 
435     return env->CallStaticObjectMethod(gBitmapConfig_class,
436                                        gBitmapConfig_nativeToConfigMethodID, configId);
437 }
438 
getNativeBitmapColorType(JNIEnv * env,jobject jconfig)439 SkColorType GraphicsJNI::getNativeBitmapColorType(JNIEnv* env, jobject jconfig) {
440     ALOG_ASSERT(env);
441     if (NULL == jconfig) {
442         return kUnknown_SkColorType;
443     }
444     ALOG_ASSERT(env->IsInstanceOf(jconfig, gBitmapConfig_class));
445     int c = env->GetIntField(jconfig, gBitmapConfig_nativeInstanceID);
446     return legacyBitmapConfigToColorType(c);
447 }
448 
isHardwareConfig(JNIEnv * env,jobject jconfig)449 bool GraphicsJNI::isHardwareConfig(JNIEnv* env, jobject jconfig) {
450     ALOG_ASSERT(env);
451     if (NULL == jconfig) {
452         return false;
453     }
454     int c = env->GetIntField(jconfig, gBitmapConfig_nativeInstanceID);
455     return c == kHardware_LegacyBitmapConfig;
456 }
457 
hardwareLegacyBitmapConfig()458 jint GraphicsJNI::hardwareLegacyBitmapConfig() {
459     return kHardware_LegacyBitmapConfig;
460 }
461 
getNativeCanvas(JNIEnv * env,jobject canvas)462 android::Canvas* GraphicsJNI::getNativeCanvas(JNIEnv* env, jobject canvas) {
463     ALOG_ASSERT(env);
464     ALOG_ASSERT(canvas);
465     ALOG_ASSERT(env->IsInstanceOf(canvas, gCanvas_class));
466     jlong canvasHandle = env->GetLongField(canvas, gCanvas_nativeInstanceID);
467     if (!canvasHandle) {
468         return NULL;
469     }
470     return reinterpret_cast<android::Canvas*>(canvasHandle);
471 }
472 
getNativeRegion(JNIEnv * env,jobject region)473 SkRegion* GraphicsJNI::getNativeRegion(JNIEnv* env, jobject region)
474 {
475     ALOG_ASSERT(env);
476     ALOG_ASSERT(region);
477     ALOG_ASSERT(env->IsInstanceOf(region, gRegion_class));
478     jlong regionHandle = env->GetLongField(region, gRegion_nativeInstanceID);
479     SkRegion* r = reinterpret_cast<SkRegion*>(regionHandle);
480     ALOG_ASSERT(r);
481     return r;
482 }
483 
set_metrics(JNIEnv * env,jobject metrics,const SkFontMetrics & skmetrics)484 void GraphicsJNI::set_metrics(JNIEnv* env, jobject metrics, const SkFontMetrics& skmetrics) {
485     if (metrics == nullptr) return;
486     SkASSERT(env->IsInstanceOf(metrics, gFontMetrics_class));
487     env->SetFloatField(metrics, gFontMetrics_top, SkScalarToFloat(skmetrics.fTop));
488     env->SetFloatField(metrics, gFontMetrics_ascent, SkScalarToFloat(skmetrics.fAscent));
489     env->SetFloatField(metrics, gFontMetrics_descent, SkScalarToFloat(skmetrics.fDescent));
490     env->SetFloatField(metrics, gFontMetrics_bottom, SkScalarToFloat(skmetrics.fBottom));
491     env->SetFloatField(metrics, gFontMetrics_leading, SkScalarToFloat(skmetrics.fLeading));
492 }
493 
set_metrics_int(JNIEnv * env,jobject metrics,const SkFontMetrics & skmetrics)494 int GraphicsJNI::set_metrics_int(JNIEnv* env, jobject metrics, const SkFontMetrics& skmetrics) {
495     int ascent = SkScalarRoundToInt(skmetrics.fAscent);
496     int descent = SkScalarRoundToInt(skmetrics.fDescent);
497     int leading = SkScalarRoundToInt(skmetrics.fLeading);
498 
499     if (metrics) {
500         SkASSERT(env->IsInstanceOf(metrics, gFontMetricsInt_class));
501         env->SetIntField(metrics, gFontMetricsInt_top, SkScalarFloorToInt(skmetrics.fTop));
502         env->SetIntField(metrics, gFontMetricsInt_ascent, ascent);
503         env->SetIntField(metrics, gFontMetricsInt_descent, descent);
504         env->SetIntField(metrics, gFontMetricsInt_bottom, SkScalarCeilToInt(skmetrics.fBottom));
505         env->SetIntField(metrics, gFontMetricsInt_leading, leading);
506     }
507     return descent - ascent + leading;
508 }
509 
510 ///////////////////////////////////////////////////////////////////////////////////////////
511 
createBitmapRegionDecoder(JNIEnv * env,skia::BitmapRegionDecoder * bitmap)512 jobject GraphicsJNI::createBitmapRegionDecoder(JNIEnv* env, skia::BitmapRegionDecoder* bitmap)
513 {
514     ALOG_ASSERT(bitmap != NULL);
515 
516     jobject obj = env->NewObject(gBitmapRegionDecoder_class,
517             gBitmapRegionDecoder_constructorMethodID,
518             reinterpret_cast<jlong>(bitmap));
519     hasException(env); // For the side effect of logging.
520     return obj;
521 }
522 
createRegion(JNIEnv * env,SkRegion * region)523 jobject GraphicsJNI::createRegion(JNIEnv* env, SkRegion* region)
524 {
525     ALOG_ASSERT(region != NULL);
526     jobject obj = env->NewObject(gRegion_class, gRegion_constructorMethodID,
527                                  reinterpret_cast<jlong>(region), 0);
528     hasException(env); // For the side effect of logging.
529     return obj;
530 }
531 
532 ///////////////////////////////////////////////////////////////////////////////
533 
getColorSpace(JNIEnv * env,SkColorSpace * decodeColorSpace,SkColorType decodeColorType)534 jobject GraphicsJNI::getColorSpace(JNIEnv* env, SkColorSpace* decodeColorSpace,
535         SkColorType decodeColorType) {
536     if (!decodeColorSpace || decodeColorType == kAlpha_8_SkColorType) {
537         return nullptr;
538     }
539 
540     // Special checks for the common sRGB cases and their extended variants.
541     jobject namedCS = nullptr;
542     sk_sp<SkColorSpace> srgbLinear = SkColorSpace::MakeSRGBLinear();
543     if (decodeColorType == kRGBA_F16_SkColorType) {
544         // An F16 Bitmap will always report that it is EXTENDED if
545         // it matches a ColorSpace that has an EXTENDED variant.
546         if (decodeColorSpace->isSRGB()) {
547             namedCS = env->GetStaticObjectField(gColorSpace_Named_class,
548                                                 gColorSpace_Named_ExtendedSRGBFieldID);
549         } else if (decodeColorSpace == srgbLinear.get()) {
550             namedCS = env->GetStaticObjectField(gColorSpace_Named_class,
551                                                 gColorSpace_Named_LinearExtendedSRGBFieldID);
552         }
553     } else if (decodeColorSpace->isSRGB()) {
554         namedCS = env->GetStaticObjectField(gColorSpace_Named_class,
555                                             gColorSpace_Named_sRGBFieldID);
556     } else if (decodeColorSpace == srgbLinear.get()) {
557         namedCS = env->GetStaticObjectField(gColorSpace_Named_class,
558                                             gColorSpace_Named_LinearSRGBFieldID);
559     }
560 
561     if (namedCS) {
562         return env->CallStaticObjectMethod(gColorSpace_class, gColorSpace_getMethodID, namedCS);
563     }
564 
565     // Try to match against known RGB color spaces using the CIE XYZ D50
566     // conversion matrix and numerical transfer function parameters
567     skcms_Matrix3x3 xyzMatrix;
568     LOG_ALWAYS_FATAL_IF(!decodeColorSpace->toXYZD50(&xyzMatrix));
569 
570     skcms_TransferFunction transferParams;
571     // We can only handle numerical transfer functions at the moment
572     LOG_ALWAYS_FATAL_IF(!decodeColorSpace->isNumericalTransferFn(&transferParams));
573 
574     jobject params = env->NewObject(gTransferParameters_class,
575             gTransferParameters_constructorMethodID,
576             transferParams.a, transferParams.b, transferParams.c,
577             transferParams.d, transferParams.e, transferParams.f,
578             transferParams.g);
579 
580     jfloatArray xyzArray = env->NewFloatArray(9);
581     jfloat xyz[9] = {
582             xyzMatrix.vals[0][0],
583             xyzMatrix.vals[1][0],
584             xyzMatrix.vals[2][0],
585             xyzMatrix.vals[0][1],
586             xyzMatrix.vals[1][1],
587             xyzMatrix.vals[2][1],
588             xyzMatrix.vals[0][2],
589             xyzMatrix.vals[1][2],
590             xyzMatrix.vals[2][2]
591     };
592     env->SetFloatArrayRegion(xyzArray, 0, 9, xyz);
593 
594     jobject colorSpace = env->CallStaticObjectMethod(gColorSpace_class,
595             gColorSpace_matchMethodID, xyzArray, params);
596 
597     if (colorSpace == nullptr) {
598         // We couldn't find an exact match, let's create a new color space
599         // instance with the 3x3 conversion matrix and transfer function
600         colorSpace = env->NewObject(gColorSpaceRGB_class,
601                 gColorSpaceRGB_constructorMethodID,
602                 env->NewStringUTF("Unknown"), xyzArray, params);
603     }
604 
605     env->DeleteLocalRef(xyzArray);
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 = std::max(bitmap->width(), mRecycledBitmap->info().width());
645     const int maxHeight = std::max(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.computeByteSize(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     gBitmapConfig_nativeToConfigMethodID = GetStaticMethodIDOrDie(env, gBitmapConfig_class,
755                                                                   "nativeToConfig",
756                                                                   "(I)Landroid/graphics/Bitmap$Config;");
757 
758     gCanvas_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/Canvas"));
759     gCanvas_nativeInstanceID = GetFieldIDOrDie(env, gCanvas_class, "mNativeCanvasWrapper", "J");
760 
761     gPicture_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/Picture"));
762     gPicture_nativeInstanceID = GetFieldIDOrDie(env, gPicture_class, "mNativePicture", "J");
763 
764     gRegion_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/Region"));
765     gRegion_nativeInstanceID = GetFieldIDOrDie(env, gRegion_class, "mNativeRegion", "J");
766     gRegion_constructorMethodID = GetMethodIDOrDie(env, gRegion_class, "<init>", "(JI)V");
767 
768     c = env->FindClass("java/lang/Byte");
769     gByte_class = (jclass) env->NewGlobalRef(
770         env->GetStaticObjectField(c, env->GetStaticFieldID(c, "TYPE", "Ljava/lang/Class;")));
771 
772     gVMRuntime_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "dalvik/system/VMRuntime"));
773     m = env->GetStaticMethodID(gVMRuntime_class, "getRuntime", "()Ldalvik/system/VMRuntime;");
774     gVMRuntime = env->NewGlobalRef(env->CallStaticObjectMethod(gVMRuntime_class, m));
775     gVMRuntime_newNonMovableArray = GetMethodIDOrDie(env, gVMRuntime_class, "newNonMovableArray",
776                                                      "(Ljava/lang/Class;I)Ljava/lang/Object;");
777     gVMRuntime_addressOf = GetMethodIDOrDie(env, gVMRuntime_class, "addressOf", "(Ljava/lang/Object;)J");
778 
779     gColorSpace_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/ColorSpace"));
780     gColorSpace_getMethodID = GetStaticMethodIDOrDie(env, gColorSpace_class,
781             "get", "(Landroid/graphics/ColorSpace$Named;)Landroid/graphics/ColorSpace;");
782     gColorSpace_matchMethodID = GetStaticMethodIDOrDie(env, gColorSpace_class, "match",
783             "([FLandroid/graphics/ColorSpace$Rgb$TransferParameters;)Landroid/graphics/ColorSpace;");
784 
785     gColorSpaceRGB_class = MakeGlobalRefOrDie(env,
786             FindClassOrDie(env, "android/graphics/ColorSpace$Rgb"));
787     gColorSpaceRGB_constructorMethodID = GetMethodIDOrDie(env, gColorSpaceRGB_class,
788             "<init>", "(Ljava/lang/String;[FLandroid/graphics/ColorSpace$Rgb$TransferParameters;)V");
789 
790     gColorSpace_Named_class = MakeGlobalRefOrDie(env,
791             FindClassOrDie(env, "android/graphics/ColorSpace$Named"));
792     gColorSpace_Named_sRGBFieldID = GetStaticFieldIDOrDie(env,
793             gColorSpace_Named_class, "SRGB", "Landroid/graphics/ColorSpace$Named;");
794     gColorSpace_Named_ExtendedSRGBFieldID = GetStaticFieldIDOrDie(env,
795             gColorSpace_Named_class, "EXTENDED_SRGB", "Landroid/graphics/ColorSpace$Named;");
796     gColorSpace_Named_LinearSRGBFieldID = GetStaticFieldIDOrDie(env,
797             gColorSpace_Named_class, "LINEAR_SRGB", "Landroid/graphics/ColorSpace$Named;");
798     gColorSpace_Named_LinearExtendedSRGBFieldID = GetStaticFieldIDOrDie(env,
799             gColorSpace_Named_class, "LINEAR_EXTENDED_SRGB", "Landroid/graphics/ColorSpace$Named;");
800 
801     gTransferParameters_class = MakeGlobalRefOrDie(env, FindClassOrDie(env,
802             "android/graphics/ColorSpace$Rgb$TransferParameters"));
803     gTransferParameters_constructorMethodID = GetMethodIDOrDie(env, gTransferParameters_class,
804             "<init>", "(DDDDDDD)V");
805 
806     gFontMetrics_class = FindClassOrDie(env, "android/graphics/Paint$FontMetrics");
807     gFontMetrics_class = MakeGlobalRefOrDie(env, gFontMetrics_class);
808 
809     gFontMetrics_top = GetFieldIDOrDie(env, gFontMetrics_class, "top", "F");
810     gFontMetrics_ascent = GetFieldIDOrDie(env, gFontMetrics_class, "ascent", "F");
811     gFontMetrics_descent = GetFieldIDOrDie(env, gFontMetrics_class, "descent", "F");
812     gFontMetrics_bottom = GetFieldIDOrDie(env, gFontMetrics_class, "bottom", "F");
813     gFontMetrics_leading = GetFieldIDOrDie(env, gFontMetrics_class, "leading", "F");
814 
815     gFontMetricsInt_class = FindClassOrDie(env, "android/graphics/Paint$FontMetricsInt");
816     gFontMetricsInt_class = MakeGlobalRefOrDie(env, gFontMetricsInt_class);
817 
818     gFontMetricsInt_top = GetFieldIDOrDie(env, gFontMetricsInt_class, "top", "I");
819     gFontMetricsInt_ascent = GetFieldIDOrDie(env, gFontMetricsInt_class, "ascent", "I");
820     gFontMetricsInt_descent = GetFieldIDOrDie(env, gFontMetricsInt_class, "descent", "I");
821     gFontMetricsInt_bottom = GetFieldIDOrDie(env, gFontMetricsInt_class, "bottom", "I");
822     gFontMetricsInt_leading = GetFieldIDOrDie(env, gFontMetricsInt_class, "leading", "I");
823 
824     return 0;
825 }
826