From 03b3e236a058b878ec467918610df8f94e83301c Mon Sep 17 00:00:00 2001 From: Leon Scroggins III Date: Tue, 12 Mar 2019 10:15:46 -0400 Subject: Restore Paint#nSetColor(@ColorInt) Bug: 127580253 Test: CtsGraphicsTestCases Perf: systrace Restored with a partial revert of "Add Paint#get(ShadowLayer)ColorLong" (commit 6ee411010ea270351d495bf357fc294304286a70). The original CL combined the @ColorInt and @ColorLong version for simplicity, but required doing extra work for the @ColorInt version. Separating them back out speeds it up at the cost of more code. Using systrace I see the following stats: Duration: (w/o this change) (w/ this change) avg: 0.020 ms 0.001 ms max: 9.141 ms 0.072 ms min: 0.005 ms 0.001 ms std: 0.074 ms 0.001 ms This change shows a significant speed improvement. It does not do the same for setShadowLayer, which is likely used less frequently. Change-Id: I9021864fcad7d0149b93674f09339f805c272994 --- graphics/java/android/graphics/Paint.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'graphics/java/android') diff --git a/graphics/java/android/graphics/Paint.java b/graphics/java/android/graphics/Paint.java index 452f7c93f8aa..966e171a9c04 100644 --- a/graphics/java/android/graphics/Paint.java +++ b/graphics/java/android/graphics/Paint.java @@ -1048,7 +1048,8 @@ public class Paint { * @param color The new color (including alpha) to set in the paint. */ public void setColor(@ColorInt int color) { - setColor(Color.pack(color)); + nSetColor(mNativePaint, color); + mColor = Color.pack(color); } /** @@ -3192,6 +3193,8 @@ public class Paint { private static native void nSetColor(long paintPtr, long colorSpaceHandle, float r, float g, float b, float a); @CriticalNative + private static native void nSetColor(long paintPtr, @ColorInt int color); + @CriticalNative private static native void nSetStrikeThruText(long paintPtr, boolean strikeThruText); @CriticalNative private static native boolean nIsElegantTextHeight(long paintPtr); -- cgit v1.2.3-59-g8ed1b From 10965f3922380581b7bf11dbc7b1bf4ef4ffdd2e Mon Sep 17 00:00:00 2001 From: Leon Scroggins III Date: Tue, 12 Mar 2019 11:13:27 -0400 Subject: Convert ColorLong in native code in Paint Bug: 127580253 Test: CtsGraphicsTestCases Perf: systrace Follow on to I0b42c17159b290868a6bed7b90da096995504c4d, which did this for other classes. I expect this to be more efficient due to passing fewer JNI parameters, which appears to be the case based on the systrace data for setColor(@ColorLong): Duration: (w/o this change) (w/ this change) avg: 0.010 ms 0.005 ms max: 9.130 ms 0.239 ms min: 0.002 ms 0.002 ms std: 0.052 ms 0.004 ms The average is twice as fast, the max is significantly shorter, and the standard deviation is much smaller as well. Change-Id: I241824bf2a934746df93da1063b14f3080486cb5 --- core/jni/android/graphics/Paint.cpp | 12 ++++++------ graphics/java/android/graphics/Paint.java | 19 +++++++------------ 2 files changed, 13 insertions(+), 18 deletions(-) (limited to 'graphics/java/android') diff --git a/core/jni/android/graphics/Paint.cpp b/core/jni/android/graphics/Paint.cpp index 376d6f331d72..cd5c734afc4d 100644 --- a/core/jni/android/graphics/Paint.cpp +++ b/core/jni/android/graphics/Paint.cpp @@ -724,9 +724,9 @@ namespace PaintGlue { } static void setColorLong(jlong paintHandle, jlong colorSpaceHandle, - jfloat r, jfloat g, jfloat b, jfloat a) { + jlong colorLong) { + SkColor4f color = GraphicsJNI::convertColorLong(colorLong); sk_sp cs = GraphicsJNI::getNativeColorSpace(colorSpaceHandle); - SkColor4f color = SkColor4f{r, g, b, a}; reinterpret_cast(paintHandle)->setColor4f(color, cs.get()); } @@ -995,9 +995,9 @@ namespace PaintGlue { static void setShadowLayer(jlong paintHandle, jfloat radius, jfloat dx, jfloat dy, jlong colorSpaceHandle, - jfloat r, jfloat g, jfloat b, jfloat a) { + jlong colorLong) { + SkColor4f color = GraphicsJNI::convertColorLong(colorLong); sk_sp cs = GraphicsJNI::getNativeColorSpace(colorSpaceHandle); - SkColor4f color = SkColor4f{r, g, b, a}; Paint* paint = reinterpret_cast(paintHandle); if (radius <= 0) { @@ -1087,7 +1087,7 @@ static const JNINativeMethod methods[] = { {"nGetStyle","(J)I", (void*) PaintGlue::getStyle}, {"nSetStyle","(JI)V", (void*) PaintGlue::setStyle}, {"nSetColor","(JI)V", (void*) PaintGlue::setColor}, - {"nSetColor","(JJFFFF)V", (void*) PaintGlue::setColorLong}, + {"nSetColor","(JJJ)V", (void*) PaintGlue::setColorLong}, {"nSetAlpha","(JI)V", (void*) PaintGlue::setAlpha}, {"nGetStrokeWidth","(J)F", (void*) PaintGlue::getStrokeWidth}, {"nSetStrokeWidth","(JF)V", (void*) PaintGlue::setStrokeWidth}, @@ -1130,7 +1130,7 @@ static const JNINativeMethod methods[] = { {"nGetUnderlineThickness","(J)F", (void*) PaintGlue::getUnderlineThickness}, {"nGetStrikeThruPosition","(J)F", (void*) PaintGlue::getStrikeThruPosition}, {"nGetStrikeThruThickness","(J)F", (void*) PaintGlue::getStrikeThruThickness}, - {"nSetShadowLayer", "(JFFFJFFFF)V", (void*)PaintGlue::setShadowLayer}, + {"nSetShadowLayer", "(JFFFJJ)V", (void*)PaintGlue::setShadowLayer}, {"nHasShadowLayer", "(J)Z", (void*)PaintGlue::hasShadowLayer}, {"nEqualsForTextMeasurement", "(JJ)Z", (void*)PaintGlue::equalsForTextMeasurement}, }; diff --git a/graphics/java/android/graphics/Paint.java b/graphics/java/android/graphics/Paint.java index 966e171a9c04..04385c628c35 100644 --- a/graphics/java/android/graphics/Paint.java +++ b/graphics/java/android/graphics/Paint.java @@ -1066,12 +1066,8 @@ public class Paint { */ public void setColor(@ColorLong long color) { ColorSpace cs = Color.colorSpace(color); - float r = Color.red(color); - float g = Color.green(color); - float b = Color.blue(color); - float a = Color.alpha(color); - nSetColor(mNativePaint, cs.getNativeInstance(), r, g, b, a); + nSetColor(mNativePaint, cs.getNativeInstance(), color); mColor = color; } @@ -1501,11 +1497,7 @@ public class Paint { */ public void setShadowLayer(float radius, float dx, float dy, @ColorLong long shadowColor) { ColorSpace cs = Color.colorSpace(shadowColor); - float r = Color.red(shadowColor); - float g = Color.green(shadowColor); - float b = Color.blue(shadowColor); - float a = Color.alpha(shadowColor); - nSetShadowLayer(mNativePaint, radius, dx, dy, cs.getNativeInstance(), r, g, b, a); + nSetShadowLayer(mNativePaint, radius, dx, dy, cs.getNativeInstance(), shadowColor); mShadowLayerRadius = radius; mShadowLayerDx = dx; @@ -1533,6 +1525,7 @@ public class Paint { /** * Returns the blur radius of the shadow layer. * @see #setShadowLayer(float,float,float,int) + * @see #setShadowLayer(float,float,float,long) */ public float getShadowLayerRadius() { return mShadowLayerRadius; @@ -1541,6 +1534,7 @@ public class Paint { /** * Returns the x offset of the shadow layer. * @see #setShadowLayer(float,float,float,int) + * @see #setShadowLayer(float,float,float,long) */ public float getShadowLayerDx() { return mShadowLayerDx; @@ -1549,6 +1543,7 @@ public class Paint { /** * Returns the y offset of the shadow layer. * @see #setShadowLayer(float,float,float,int) + * @see #setShadowLayer(float,float,float,long) */ public float getShadowLayerDy() { return mShadowLayerDy; @@ -3138,7 +3133,7 @@ public class Paint { @CriticalNative private static native void nSetShadowLayer(long paintPtr, float radius, float dx, float dy, long colorSpaceHandle, - float r, float g, float b, float a); + @ColorLong long shadowColor); @CriticalNative private static native boolean nHasShadowLayer(long paintPtr); @CriticalNative @@ -3191,7 +3186,7 @@ public class Paint { private static native void nSetFilterBitmap(long paintPtr, boolean filter); @CriticalNative private static native void nSetColor(long paintPtr, long colorSpaceHandle, - float r, float g, float b, float a); + @ColorLong long color); @CriticalNative private static native void nSetColor(long paintPtr, @ColorInt int color); @CriticalNative -- cgit v1.2.3-59-g8ed1b