From ab87983a11e0bd2e08d752d86d5e945ea7d39a04 Mon Sep 17 00:00:00 2001 From: Leon Scroggins III Date: Wed, 9 Jul 2014 16:44:35 -0400 Subject: [PATCH] Simplify Shader.setLocalMatrix. Previously, calling setLocalMatrix updated any Paint that had the Shader attached. This depended on deprecated behavior in Skia. Use new Skia APIs, and do not modify any Paints that use the Shader. In addition, update callers to call setShader (again) after modifying the Shader. Sample app at ag/499573 for testing. Depends on I673801444f0a8fd4f192b5b7effdde1aa83e702b in external/skia. BUG:14315916 Change-Id: I3c3316377874e89fccc85afb864bc038b0ef3890 --- core/java/android/view/View.java | 4 ++++ core/jni/android/graphics/Shader.cpp | 13 +++++++------ graphics/java/android/graphics/Shader.java | 11 ++++++++--- .../android/graphics/drawable/BitmapDrawable.java | 4 ++++ .../systemui/recent/FadedEdgeDrawHelper.java | 4 ++++ .../com/android/test/hwui/GradientsActivity.java | 9 +++++---- 6 files changed, 32 insertions(+), 13 deletions(-) diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java index 1e5f448beaa87..675b96dfdbed0 100644 --- a/core/java/android/view/View.java +++ b/core/java/android/view/View.java @@ -14947,6 +14947,7 @@ public class View implements Drawable.Callback, KeyEvent.Callback, matrix.setScale(1, fadeHeight * topFadeStrength); matrix.postTranslate(left, top); fade.setLocalMatrix(matrix); + p.setShader(fade); canvas.drawRect(left, top, right, top + length, p); } @@ -14955,6 +14956,7 @@ public class View implements Drawable.Callback, KeyEvent.Callback, matrix.postRotate(180); matrix.postTranslate(left, bottom); fade.setLocalMatrix(matrix); + p.setShader(fade); canvas.drawRect(left, bottom - length, right, bottom, p); } @@ -14963,6 +14965,7 @@ public class View implements Drawable.Callback, KeyEvent.Callback, matrix.postRotate(-90); matrix.postTranslate(left, top); fade.setLocalMatrix(matrix); + p.setShader(fade); canvas.drawRect(left, top, left + length, bottom, p); } @@ -14971,6 +14974,7 @@ public class View implements Drawable.Callback, KeyEvent.Callback, matrix.postRotate(90); matrix.postTranslate(right, top); fade.setLocalMatrix(matrix); + p.setShader(fade); canvas.drawRect(right - length, top, right, bottom, p); } diff --git a/core/jni/android/graphics/Shader.cpp b/core/jni/android/graphics/Shader.cpp index 0cfcaefb27373..c71f226193b6d 100644 --- a/core/jni/android/graphics/Shader.cpp +++ b/core/jni/android/graphics/Shader.cpp @@ -56,19 +56,20 @@ static void Shader_destructor(JNIEnv* env, jobject o, jlong shaderHandle) SkSafeUnref(shader); } -static void Shader_setLocalMatrix(JNIEnv* env, jobject o, jlong shaderHandle, +static jlong Shader_setLocalMatrix(JNIEnv* env, jobject o, jlong shaderHandle, jlong matrixHandle) { SkShader* shader = reinterpret_cast(shaderHandle); const SkMatrix* matrix = reinterpret_cast(matrixHandle); if (shader) { if (NULL == matrix) { - shader->resetLocalMatrix(); - } - else { - shader->setLocalMatrix(*matrix); + matrix = &SkMatrix::I(); } + SkShader* newShader = SkShader::CreateLocalMatrixShader(shader, *matrix); + shader->unref(); + shader = newShader; } + return reinterpret_cast(shader); } /////////////////////////////////////////////////////////////////////////////////////////////// @@ -239,7 +240,7 @@ static JNINativeMethod gColorMethods[] = { static JNINativeMethod gShaderMethods[] = { { "nativeDestructor", "(J)V", (void*)Shader_destructor }, - { "nativeSetLocalMatrix", "(JJ)V", (void*)Shader_setLocalMatrix } + { "nativeSetLocalMatrix", "(JJ)J", (void*)Shader_setLocalMatrix } }; static JNINativeMethod gBitmapShaderMethods[] = { diff --git a/graphics/java/android/graphics/Shader.java b/graphics/java/android/graphics/Shader.java index 6870ab46f007b..936fe0cf4442c 100644 --- a/graphics/java/android/graphics/Shader.java +++ b/graphics/java/android/graphics/Shader.java @@ -69,12 +69,17 @@ public class Shader { /** * Set the shader's local matrix. Passing null will reset the shader's - * matrix to identity + * matrix to identity. + * + * Starting with {@link android.os.Build.VERSION_CODES#L}, this does not + * modify any Paints which use this Shader. In order to modify the Paint, + * you need to call {@link Paint#setShader} again. + * * @param localM The shader's new local matrix, or null to specify identity */ public void setLocalMatrix(Matrix localM) { mLocalMatrix = localM; - nativeSetLocalMatrix(native_instance, + native_instance = nativeSetLocalMatrix(native_instance, localM == null ? 0 : localM.native_instance); } @@ -109,6 +114,6 @@ public class Shader { } private static native void nativeDestructor(long native_shader); - private static native void nativeSetLocalMatrix(long native_shader, + private static native long nativeSetLocalMatrix(long native_shader, long matrix_instance); } diff --git a/graphics/java/android/graphics/drawable/BitmapDrawable.java b/graphics/java/android/graphics/drawable/BitmapDrawable.java index 83a8ed58883b3..db5c8e3161c8b 100644 --- a/graphics/java/android/graphics/drawable/BitmapDrawable.java +++ b/graphics/java/android/graphics/drawable/BitmapDrawable.java @@ -467,10 +467,12 @@ public class BitmapDrawable extends Drawable { if (needMirroring()) { updateMirrorMatrix(bounds.right - bounds.left); shader.setLocalMatrix(mMirrorMatrix); + mBitmapState.mPaint.setShader(shader); } else { if (mMirrorMatrix != null) { mMirrorMatrix = null; shader.setLocalMatrix(Matrix.IDENTITY_MATRIX); + mBitmapState.mPaint.setShader(shader); } } } @@ -547,10 +549,12 @@ public class BitmapDrawable extends Drawable { // Mirror the bitmap updateMirrorMatrix(mDstRect.right - mDstRect.left); shader.setLocalMatrix(mMirrorMatrix); + paint.setShader(shader); } else { if (mMirrorMatrix != null) { mMirrorMatrix = null; shader.setLocalMatrix(Matrix.IDENTITY_MATRIX); + paint.setShader(shader); } } diff --git a/packages/SystemUI/src/com/android/systemui/recent/FadedEdgeDrawHelper.java b/packages/SystemUI/src/com/android/systemui/recent/FadedEdgeDrawHelper.java index 1cfc892b38249..59f7a800fe4cd 100644 --- a/packages/SystemUI/src/com/android/systemui/recent/FadedEdgeDrawHelper.java +++ b/packages/SystemUI/src/com/android/systemui/recent/FadedEdgeDrawHelper.java @@ -142,6 +142,7 @@ public class FadedEdgeDrawHelper { mFadeMatrix.setScale(1, fadeHeight * topFadeStrength); mFadeMatrix.postTranslate(left, top); mFade.setLocalMatrix(mFadeMatrix); + mFadePaint.setShader(mFade); canvas.drawRect(left, top, right, top + length, mFadePaint); if (mBlackPaint == null) { @@ -157,6 +158,7 @@ public class FadedEdgeDrawHelper { mFadeMatrix.postRotate(180); mFadeMatrix.postTranslate(left, bottom); mFade.setLocalMatrix(mFadeMatrix); + mFadePaint.setShader(mFade); canvas.drawRect(left, bottom - length, right, bottom, mFadePaint); } @@ -165,6 +167,7 @@ public class FadedEdgeDrawHelper { mFadeMatrix.postRotate(-90); mFadeMatrix.postTranslate(left, top); mFade.setLocalMatrix(mFadeMatrix); + mFadePaint.setShader(mFade); canvas.drawRect(left, top, left + length, bottom, mFadePaint); } @@ -173,6 +176,7 @@ public class FadedEdgeDrawHelper { mFadeMatrix.postRotate(90); mFadeMatrix.postTranslate(right, top); mFade.setLocalMatrix(mFadeMatrix); + mFadePaint.setShader(mFade); canvas.drawRect(right - length, top, right, bottom, mFadePaint); } } diff --git a/tests/HwAccelerationTest/src/com/android/test/hwui/GradientsActivity.java b/tests/HwAccelerationTest/src/com/android/test/hwui/GradientsActivity.java index 90db818a75c49..fbe7856f32ec8 100644 --- a/tests/HwAccelerationTest/src/com/android/test/hwui/GradientsActivity.java +++ b/tests/HwAccelerationTest/src/com/android/test/hwui/GradientsActivity.java @@ -223,12 +223,11 @@ public class GradientsActivity extends Activity { float left = 40.0f; float bottom = 40.0f + mDrawHeight; - mPaint.setShader(mGradient); - mMatrix.setScale(1, mDrawWidth); mMatrix.postRotate(90); mMatrix.postTranslate(right, top); mGradient.setLocalMatrix(mMatrix); + mPaint.setShader(mGradient); canvas.drawRect(right - mDrawWidth, top, right, top + mDrawHeight, mPaint); top += 40.0f + mDrawHeight; @@ -237,6 +236,7 @@ public class GradientsActivity extends Activity { mMatrix.setScale(1, mDrawHeight); mMatrix.postTranslate(left, top); mGradient.setLocalMatrix(mMatrix); + mPaint.setShader(mGradient); canvas.drawRect(left, top, right, top + mDrawHeight, mPaint); left += 40.0f + mDrawWidth; @@ -248,6 +248,7 @@ public class GradientsActivity extends Activity { mMatrix.postRotate(180); mMatrix.postTranslate(left, bottom); mGradient.setLocalMatrix(mMatrix); + mPaint.setShader(mGradient); canvas.drawRect(left, bottom - mDrawHeight, right, bottom, mPaint); top += 40.0f + mDrawHeight; @@ -257,6 +258,7 @@ public class GradientsActivity extends Activity { mMatrix.postRotate(-90); mMatrix.postTranslate(left, top); mGradient.setLocalMatrix(mMatrix); + mPaint.setShader(mGradient); canvas.drawRect(left, top, left + mDrawWidth, bottom, mPaint); right = left + mDrawWidth; @@ -264,12 +266,11 @@ public class GradientsActivity extends Activity { top = bottom + 20.0f; bottom = top + 50.0f; - mPaint.setShader(mGradientStops); - mMatrix.setScale(1, mDrawWidth); mMatrix.postRotate(90); mMatrix.postTranslate(right, top); mGradientStops.setLocalMatrix(mMatrix); + mPaint.setShader(mGradientStops); canvas.drawRect(left, top, right, bottom, mPaint); canvas.restore();