[androidkit] add gaussian blur filter to java ImageFilters

Change-Id: I63f3f3feaf590971633c88857105ed14dbab2bb8
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/420122
Commit-Queue: Jorge Betancourt <jmbetancourt@google.com>
Reviewed-by: Florin Malita <fmalita@chromium.org>
This commit is contained in:
Jorge Betancourt 2021-06-22 10:30:24 -04:00 committed by Skia Commit-Bot
parent 60cc3e171c
commit 7280b36687
5 changed files with 41 additions and 6 deletions

View File

@ -10,6 +10,7 @@
#include "include/core/SkImageFilter.h" #include "include/core/SkImageFilter.h"
#include "include/core/SkPoint3.h" #include "include/core/SkPoint3.h"
#include "include/effects/SkImageFilters.h" #include "include/effects/SkImageFilters.h"
#include "modules/androidkit/src/Utils.h"
namespace { namespace {
@ -30,12 +31,22 @@ static long ImageFilter_DistantLitDiffuse(JNIEnv* env, jobject, jfloat x, jfloat
return reinterpret_cast<jlong>(filter.release()); return reinterpret_cast<jlong>(filter.release());
} }
static long ImageFilter_Blur(JNIEnv* env, jobject, jfloat sigmaX, jfloat sigmaY,
jint jTileMode, jlong native_input) {
auto input = sk_ref_sp(reinterpret_cast<SkImageFilter*>(native_input));
auto filter = SkImageFilters::Blur(sigmaX, sigmaY,
androidkit::utils::TileMode(jTileMode),
std::move(input));
return reinterpret_cast<jlong>(filter.release());
}
} // namespace } // namespace
int register_androidkit_ImageFilter(JNIEnv* env) { int register_androidkit_ImageFilter(JNIEnv* env) {
static const JNINativeMethod methods[] = { static const JNINativeMethod methods[] = {
{"nRelease" , "(J)V" , reinterpret_cast<void*>(ImageFilter_Release)}, {"nRelease" , "(J)V" , reinterpret_cast<void*>(ImageFilter_Release)},
{"nDistantLitDiffuse", "(FFFFFFFFJ)J", reinterpret_cast<void*>(ImageFilter_DistantLitDiffuse)}, {"nDistantLitDiffuse", "(FFFFFFFFJ)J", reinterpret_cast<void*>(ImageFilter_DistantLitDiffuse)},
{"nBlur" , "(FFIJ)J" , reinterpret_cast<void*>(ImageFilter_Blur)},
}; };
const auto clazz = env->FindClass("org/skia/androidkit/ImageFilter"); const auto clazz = env->FindClass("org/skia/androidkit/ImageFilter");

View File

@ -56,7 +56,7 @@ public class Image {
public Shader makeShader(TileMode tmx, TileMode tmy, SamplingOptions sampling, public Shader makeShader(TileMode tmx, TileMode tmy, SamplingOptions sampling,
@Nullable Matrix localMatrix) { @Nullable Matrix localMatrix) {
long nativeMatrix = localMatrix != null ? localMatrix.getNativeInstance() : 0; long nativeMatrix = localMatrix != null ? localMatrix.getNativeInstance() : 0;
return new Shader(nMakeShader(mNativeInstance, tmx.ordinal(), tmy.ordinal(), return new Shader(nMakeShader(mNativeInstance, tmx.nativeInt, tmy.nativeInt,
sampling.getNativeDesc(), sampling.getNativeDesc(),
sampling.getCubicCoeffB(), sampling.getCubicCoeffC(), sampling.getCubicCoeffB(), sampling.getCubicCoeffC(),
nativeMatrix)); nativeMatrix));

View File

@ -36,6 +36,23 @@ public class ImageFilter {
return new ImageFilter(nDistantLitDiffuse(x, y, z, c.r(), c.g(), c.b(), return new ImageFilter(nDistantLitDiffuse(x, y, z, c.r(), c.g(), c.b(),
surfaceScale, kd, nativeInput)); surfaceScale, kd, nativeInput));
} }
/**
* Create a filter that calculates the diffuse illumination from a distant light source,
* interpreting the alpha channel of the input as the height profile of the surface (to
* approximate normal vectors).
* @param sigmaX The Gaussian sigma value for blurring along the X axis.
* @param sigmaY The Gaussian sigma value for blurring along the Y axis.
* @param tileMode The tile mode applied at edges
* @param input The input filter that is blurred, uses source bitmap if this is null.
*/
public static ImageFilter blur(float sigmaX, float sigmaY, TileMode tileMode, @Nullable ImageFilter input) {
long nativeInput = 0;
if (input != null) {
nativeInput = input.getNativeInstance();
}
return new ImageFilter(nBlur(sigmaX, sigmaY, tileMode.nativeInt, nativeInput));
}
/** /**
* Releases any resources associated with this Shader. * Releases any resources associated with this Shader.
*/ */
@ -57,4 +74,5 @@ public class ImageFilter {
float r, float g, float b, float r, float g, float b,
float surfaceScale, float kd, float surfaceScale, float kd,
long native_input); long native_input);
private static native long nBlur(float sigmaX, float sigmaY, int tileMode, long native_input);
} }

View File

@ -12,22 +12,27 @@ public enum TileMode {
* Replicate the edge color if the shader draws outside of its * Replicate the edge color if the shader draws outside of its
* original bounds. * original bounds.
*/ */
CLAMP, CLAMP(0),
/** /**
* Repeat the shader's image horizontally and vertically. * Repeat the shader's image horizontally and vertically.
*/ */
REPEAT, REPEAT(1),
/** /**
* Repeat the shader's image horizontally and vertically, alternating * Repeat the shader's image horizontally and vertically, alternating
* mirror images so that adjacent images always seam. * mirror images so that adjacent images always seam.
*/ */
MIRROR, MIRROR(2),
/** /**
* Only draw within the original domain, return transparent-black everywhere else. * Only draw within the original domain, return transparent-black everywhere else.
*/ */
DECAL, DECAL(3);
TileMode(int nativeInt) {
this.nativeInt = nativeInt;
}
final int nativeInt;
} }

View File

@ -94,7 +94,8 @@ public class MainActivity extends Activity {
threadedSurface = Surface.createThreadedSurface(holder.getSurface()); threadedSurface = Surface.createThreadedSurface(holder.getSurface());
Paint p = new Paint(); Paint p = new Paint();
p.setColor(new Color(1, 1, 0, 1)); p.setColor(new Color(1, 1, 0, 1));
ImageFilter filter = ImageFilter.distantLitDiffuse(.5f, .5f, .5f, new Color(1, 0, 0, 1), 1, 1, null); //ImageFilter filter = ImageFilter.distantLitDiffuse(.5f, .5f, .5f, new Color(1, 0, 0, 1), 1, 1, null);
ImageFilter filter = ImageFilter.blur(10, 10, TileMode.DECAL, null);
p.setImageFilter(filter); p.setImageFilter(filter);
PathBuilder pathBuilder = new PathBuilder(); PathBuilder pathBuilder = new PathBuilder();
pathBuilder.moveTo(20, 20); pathBuilder.moveTo(20, 20);