From c6024cdd3553760aa99bfbabecc6a3d8443f5c47 Mon Sep 17 00:00:00 2001 From: John Reck Date: Fri, 10 Jul 2015 10:55:49 -0700 Subject: [PATCH] JNI optimization tweaks to Paint high-frequency methods Bug: 22378829 Use fast-jni for getFontMetrics, drops from 35us -> 30us Note the "heavy" part of the method, getMetricsInternal, is already called by other fast-jni methods. Use critical array access for getRunAdvance_* methods. This will avoid the copy and the access is appropriately scoped and fast enough to not significantly block the moving GC. Improves from 88us -> 79us on short text Change-Id: I7c1481c23f6dba3420fbcf48220f6335cf9f6d10 --- core/jni/android/graphics/Paint.cpp | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/core/jni/android/graphics/Paint.cpp b/core/jni/android/graphics/Paint.cpp index bff1885bc07b2..c66cdfec7d1c4 100644 --- a/core/jni/android/graphics/Paint.cpp +++ b/core/jni/android/graphics/Paint.cpp @@ -1065,12 +1065,11 @@ public: jint contextEnd, jboolean isRtl, jint offset) { const Paint* paint = reinterpret_cast(paintHandle); TypefaceImpl* typeface = reinterpret_cast(typefaceHandle); - // TODO performance: optimize JNI array access - jchar* textArray = env->GetCharArrayElements(text, NULL); + jchar* textArray = (jchar*) env->GetPrimitiveArrayCritical(text, NULL); jfloat result = doRunAdvance(paint, typeface, textArray + contextStart, start - contextStart, end - start, contextEnd - contextStart, isRtl, offset - contextStart); - env->ReleaseCharArrayElements(text, textArray, JNI_ABORT); + env->ReleasePrimitiveArrayCritical(text, textArray, JNI_ABORT); return result; } @@ -1086,12 +1085,11 @@ public: jint contextEnd, jboolean isRtl, jfloat advance) { const Paint* paint = reinterpret_cast(paintHandle); TypefaceImpl* typeface = reinterpret_cast(typefaceHandle); - // TODO performance: optimize JNI array access - jchar* textArray = env->GetCharArrayElements(text, NULL); + jchar* textArray = (jchar*) env->GetPrimitiveArrayCritical(text, NULL); jint result = doOffsetForAdvance(paint, typeface, textArray + contextStart, start - contextStart, end - start, contextEnd - contextStart, isRtl, advance); result += contextStart; - env->ReleaseCharArrayElements(text, textArray, JNI_ABORT); + env->ReleasePrimitiveArrayCritical(text, textArray, JNI_ABORT); return result; } @@ -1158,9 +1156,9 @@ static JNINativeMethod methods[] = { {"ascent","!()F", (void*) PaintGlue::ascent}, {"descent","!()F", (void*) PaintGlue::descent}, - {"getFontMetrics", "(Landroid/graphics/Paint$FontMetrics;)F", + {"getFontMetrics", "!(Landroid/graphics/Paint$FontMetrics;)F", (void*)PaintGlue::getFontMetrics}, - {"getFontMetricsInt", "(Landroid/graphics/Paint$FontMetricsInt;)I", + {"getFontMetricsInt", "!(Landroid/graphics/Paint$FontMetricsInt;)I", (void*)PaintGlue::getFontMetricsInt}, {"native_measureText","([CIII)F", (void*) PaintGlue::measureText_CIII}, {"native_measureText","(Ljava/lang/String;I)F", (void*) PaintGlue::measureText_StringI},