Revert "Create wrapper around Shader native implementation"

This reverts commit fc42a99ea5712883c9872d34a523d972c26c9d6f.

Because we are moving away from a wrapper class to delegate
between SkImageFilter and SkShader and instead adding
RenderEffect on RenderNode, this wrapper implementation
is no longer necessary. There are some behavioral
differences between SkShader and SkImageFilter that
would lead to additional complexity in conversions
between these objects and potential bugs.
To simplify this, RenderEffect will be added
directly to RenderNode to support better
caching of rasterization layers that is necessary
for various visual effects like blur.

Test: N/A
Change-Id: I65dc9f8bd26aed8761af13491ae3f6d583208377
diff --git a/libs/hwui/jni/Shader.cpp b/libs/hwui/jni/Shader.cpp
index 0a194f9..e76aace 100644
--- a/libs/hwui/jni/Shader.cpp
+++ b/libs/hwui/jni/Shader.cpp
@@ -5,14 +5,6 @@
 #include "SkShader.h"
 #include "SkBlendMode.h"
 #include "include/effects/SkRuntimeEffect.h"
-#include "shader/Shader.h"
-#include "shader/BitmapShader.h"
-#include "shader/BlurShader.h"
-#include "shader/ComposeShader.h"
-#include "shader/LinearGradientShader.h"
-#include "shader/RadialGradientShader.h"
-#include "shader/RuntimeShader.h"
-#include "shader/SweepGradientShader.h"
 
 #include <vector>
 
@@ -58,7 +50,7 @@
 
 ///////////////////////////////////////////////////////////////////////////////////////////////
 
-static void Shader_safeUnref(Shader* shader) {
+static void Shader_safeUnref(SkShader* shader) {
     SkSafeUnref(shader);
 }
 
@@ -82,15 +74,15 @@
         SkBitmap bitmap;
         image = SkMakeImageFromRasterBitmap(bitmap, kNever_SkCopyPixelsMode);
     }
+    sk_sp<SkShader> shader = image->makeShader(
+            (SkTileMode)tileModeX, (SkTileMode)tileModeY);
+    ThrowIAE_IfNull(env, shader.get());
 
-    auto* shader = new BitmapShader(
-            image,
-            static_cast<SkTileMode>(tileModeX),
-            static_cast<SkTileMode>(tileModeY),
-            matrix
-        );
+    if (matrix) {
+        shader = shader->makeWithLocalMatrix(*matrix);
+    }
 
-    return reinterpret_cast<jlong>(shader);
+    return reinterpret_cast<jlong>(shader.release());
 }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////
@@ -126,18 +118,17 @@
     #error Need to convert float array to SkScalar array before calling the following function.
 #endif
 
-    auto* matrix = reinterpret_cast<const SkMatrix*>(matrixPtr);
-    auto* shader = new LinearGradientShader(
-                pts,
-                colors,
-                GraphicsJNI::getNativeColorSpace(colorSpaceHandle),
-                pos,
-                static_cast<SkTileMode>(tileMode),
-                sGradientShaderFlags,
-                matrix
-            );
+    sk_sp<SkShader> shader(SkGradientShader::MakeLinear(pts, &colors[0],
+                GraphicsJNI::getNativeColorSpace(colorSpaceHandle), pos, colors.size(),
+                static_cast<SkTileMode>(tileMode), sGradientShaderFlags, nullptr));
+    ThrowIAE_IfNull(env, shader);
 
-    return reinterpret_cast<jlong>(shader);
+    const SkMatrix* matrix = reinterpret_cast<const SkMatrix*>(matrixPtr);
+    if (matrix) {
+        shader = shader->makeWithLocalMatrix(*matrix);
+    }
+
+    return reinterpret_cast<jlong>(shader.release());
 }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////
@@ -157,20 +148,17 @@
     #error Need to convert float array to SkScalar array before calling the following function.
 #endif
 
-    auto* matrix = reinterpret_cast<const SkMatrix*>(matrixPtr);
+    sk_sp<SkShader> shader = SkGradientShader::MakeRadial(center, radius, &colors[0],
+            GraphicsJNI::getNativeColorSpace(colorSpaceHandle), pos, colors.size(),
+            static_cast<SkTileMode>(tileMode), sGradientShaderFlags, nullptr);
+    ThrowIAE_IfNull(env, shader);
 
-    auto* shader = new RadialGradientShader(
-                center,
-                radius,
-                colors,
-                GraphicsJNI::getNativeColorSpace(colorSpaceHandle),
-                pos,
-                static_cast<SkTileMode>(tileMode),
-                sGradientShaderFlags,
-                matrix
-            );
+    const SkMatrix* matrix = reinterpret_cast<const SkMatrix*>(matrixPtr);
+    if (matrix) {
+        shader = shader->makeWithLocalMatrix(*matrix);
+    }
 
-    return reinterpret_cast<jlong>(shader);
+    return reinterpret_cast<jlong>(shader.release());
 }
 
 ///////////////////////////////////////////////////////////////////////////////
@@ -186,75 +174,54 @@
     #error Need to convert float array to SkScalar array before calling the following function.
 #endif
 
-    auto* matrix = reinterpret_cast<const SkMatrix*>(matrixPtr);
+    sk_sp<SkShader> shader = SkGradientShader::MakeSweep(x, y, &colors[0],
+            GraphicsJNI::getNativeColorSpace(colorSpaceHandle), pos, colors.size(),
+            sGradientShaderFlags, nullptr);
+    ThrowIAE_IfNull(env, shader);
 
-    auto* shader = new SweepGradientShader(
-                x,
-                y,
-                colors,
-                GraphicsJNI::getNativeColorSpace(colorSpaceHandle),
-                pos,
-                sGradientShaderFlags,
-                matrix
-            );
+    const SkMatrix* matrix = reinterpret_cast<const SkMatrix*>(matrixPtr);
+    if (matrix) {
+        shader = shader->makeWithLocalMatrix(*matrix);
+    }
 
-    return reinterpret_cast<jlong>(shader);
+    return reinterpret_cast<jlong>(shader.release());
 }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////
 
 static jlong ComposeShader_create(JNIEnv* env, jobject o, jlong matrixPtr,
         jlong shaderAHandle, jlong shaderBHandle, jint xfermodeHandle) {
-    auto* matrix = reinterpret_cast<const SkMatrix*>(matrixPtr);
-    auto* shaderA = reinterpret_cast<Shader*>(shaderAHandle);
-    auto* shaderB = reinterpret_cast<Shader*>(shaderBHandle);
+    const SkMatrix* matrix = reinterpret_cast<const SkMatrix*>(matrixPtr);
+    SkShader* shaderA = reinterpret_cast<SkShader *>(shaderAHandle);
+    SkShader* shaderB = reinterpret_cast<SkShader *>(shaderBHandle);
+    SkBlendMode mode = static_cast<SkBlendMode>(xfermodeHandle);
+    sk_sp<SkShader> baseShader(SkShaders::Blend(mode,
+            sk_ref_sp(shaderA), sk_ref_sp(shaderB)));
 
-    auto mode = static_cast<SkBlendMode>(xfermodeHandle);
+    SkShader* shader;
 
-    auto* composeShader = new ComposeShader(
-            *shaderA,
-            *shaderB,
-            mode,
-            matrix
-        );
-
-    return reinterpret_cast<jlong>(composeShader);
-}
-
-///////////////////////////////////////////////////////////////////////////////////////////////
-
-static jlong BlurShader_create(JNIEnv* env , jobject o, jlong matrixPtr, jfloat sigmaX,
-        jfloat sigmaY, jlong shaderHandle, jint edgeTreatment) {
-    auto* matrix = reinterpret_cast<const SkMatrix*>(matrixPtr);
-    auto* inputShader = reinterpret_cast<Shader*>(shaderHandle);
-
-    auto* blurShader = new BlurShader(
-                sigmaX,
-                sigmaY,
-                inputShader,
-                static_cast<SkTileMode>(edgeTreatment),
-                matrix
-            );
-    return reinterpret_cast<jlong>(blurShader);
+    if (matrix) {
+        shader = baseShader->makeWithLocalMatrix(*matrix).release();
+    } else {
+        shader = baseShader.release();
+    }
+    return reinterpret_cast<jlong>(shader);
 }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////
 
 static jlong RuntimeShader_create(JNIEnv* env, jobject, jlong shaderFactory, jlong matrixPtr,
         jbyteArray inputs, jlong colorSpaceHandle, jboolean isOpaque) {
-    auto* effect = reinterpret_cast<SkRuntimeEffect*>(shaderFactory);
+    SkRuntimeEffect* effect = reinterpret_cast<SkRuntimeEffect*>(shaderFactory);
     AutoJavaByteArray arInputs(env, inputs);
 
-    auto data = SkData::MakeWithCopy(arInputs.ptr(), arInputs.length());
-    auto* matrix = reinterpret_cast<const SkMatrix*>(matrixPtr);
+    sk_sp<SkData> fData;
+    fData = SkData::MakeWithCopy(arInputs.ptr(), arInputs.length());
+    const SkMatrix* matrix = reinterpret_cast<const SkMatrix*>(matrixPtr);
+    sk_sp<SkShader> shader = effect->makeShader(fData, nullptr, 0, matrix, isOpaque == JNI_TRUE);
+    ThrowIAE_IfNull(env, shader);
 
-    auto* shader = new RuntimeShader(
-            *effect,
-            std::move(data),
-            isOpaque == JNI_TRUE,
-            matrix
-        );
-    return reinterpret_cast<jlong>(shader);
+    return reinterpret_cast<jlong>(shader.release());
 }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////
@@ -272,8 +239,12 @@
 
 ///////////////////////////////////////////////////////////////////////////////////////////////
 
+static void Effect_safeUnref(SkRuntimeEffect* effect) {
+    SkSafeUnref(effect);
+}
+
 static jlong RuntimeShader_getNativeFinalizer(JNIEnv*, jobject) {
-    return static_cast<jlong>(reinterpret_cast<uintptr_t>(&Shader_safeUnref));
+    return static_cast<jlong>(reinterpret_cast<uintptr_t>(&Effect_safeUnref));
 }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////
@@ -291,10 +262,6 @@
     { "nativeCreate",      "(JJII)J",  (void*)BitmapShader_constructor },
 };
 
-static const JNINativeMethod gBlurShaderMethods[] = {
-    { "nativeCreate",      "(JFFJI)J", (void*)BlurShader_create }
-};
-
 static const JNINativeMethod gLinearGradientMethods[] = {
     { "nativeCreate",     "(JFFFF[J[FIJ)J",  (void*)LinearGradient_create     },
 };
@@ -326,8 +293,6 @@
                                   NELEM(gShaderMethods));
     android::RegisterMethodsOrDie(env, "android/graphics/BitmapShader", gBitmapShaderMethods,
                                   NELEM(gBitmapShaderMethods));
-    android::RegisterMethodsOrDie(env, "android/graphics/BlurShader", gBlurShaderMethods,
-                                  NELEM(gBlurShaderMethods));
     android::RegisterMethodsOrDie(env, "android/graphics/LinearGradient", gLinearGradientMethods,
                                   NELEM(gLinearGradientMethods));
     android::RegisterMethodsOrDie(env, "android/graphics/RadialGradient", gRadialGradientMethods,