1 #include "GraphicsJNI.h"
2 #include "SkMaskFilter.h"
3 #include "SkBlurMask.h"
4 #include "SkBlurMaskFilter.h"
5 #include "SkTableMaskFilter.h"
6
ThrowIAE_IfNull(JNIEnv * env,void * ptr)7 static void ThrowIAE_IfNull(JNIEnv* env, void* ptr) {
8 if (NULL == ptr) {
9 doThrowIAE(env);
10 }
11 }
12
13 class SkMaskFilterGlue {
14 public:
destructor(JNIEnv * env,jobject,jlong filterHandle)15 static void destructor(JNIEnv* env, jobject, jlong filterHandle) {
16 SkMaskFilter* filter = reinterpret_cast<SkMaskFilter *>(filterHandle);
17 SkSafeUnref(filter);
18 }
19
createBlur(JNIEnv * env,jobject,jfloat radius,jint blurStyle)20 static jlong createBlur(JNIEnv* env, jobject, jfloat radius, jint blurStyle) {
21 SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(radius);
22 SkMaskFilter* filter = SkMaskFilter::MakeBlur((SkBlurStyle)blurStyle, sigma).release();
23 ThrowIAE_IfNull(env, filter);
24 return reinterpret_cast<jlong>(filter);
25 }
26
createEmboss(JNIEnv * env,jobject,jfloatArray dirArray,jfloat ambient,jfloat specular,jfloat radius)27 static jlong createEmboss(JNIEnv* env, jobject, jfloatArray dirArray, jfloat ambient, jfloat specular, jfloat radius) {
28 SkScalar direction[3];
29
30 AutoJavaFloatArray autoDir(env, dirArray, 3);
31 float* values = autoDir.ptr();
32 for (int i = 0; i < 3; i++) {
33 direction[i] = values[i];
34 }
35
36 SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(radius);
37 SkMaskFilter* filter = SkBlurMaskFilter::MakeEmboss(sigma,
38 direction, ambient, specular).release();
39 ThrowIAE_IfNull(env, filter);
40 return reinterpret_cast<jlong>(filter);
41 }
42
createTable(JNIEnv * env,jobject,jbyteArray jtable)43 static jlong createTable(JNIEnv* env, jobject, jbyteArray jtable) {
44 AutoJavaByteArray autoTable(env, jtable, 256);
45 SkMaskFilter* filter = SkTableMaskFilter::Create((const uint8_t*)autoTable.ptr());
46 return reinterpret_cast<jlong>(filter);
47 }
48
createClipTable(JNIEnv * env,jobject,jint min,jint max)49 static jlong createClipTable(JNIEnv* env, jobject, jint min, jint max) {
50 SkMaskFilter* filter = SkTableMaskFilter::CreateClip(min, max);
51 return reinterpret_cast<jlong>(filter);
52 }
53
createGammaTable(JNIEnv * env,jobject,jfloat gamma)54 static jlong createGammaTable(JNIEnv* env, jobject, jfloat gamma) {
55 SkMaskFilter* filter = SkTableMaskFilter::CreateGamma(gamma);
56 return reinterpret_cast<jlong>(filter);
57 }
58 };
59
60 static const JNINativeMethod gMaskFilterMethods[] = {
61 { "nativeDestructor", "(J)V", (void*)SkMaskFilterGlue::destructor }
62 };
63
64 static const JNINativeMethod gBlurMaskFilterMethods[] = {
65 { "nativeConstructor", "(FI)J", (void*)SkMaskFilterGlue::createBlur }
66 };
67
68 static const JNINativeMethod gEmbossMaskFilterMethods[] = {
69 { "nativeConstructor", "([FFFF)J", (void*)SkMaskFilterGlue::createEmboss }
70 };
71
72 static const JNINativeMethod gTableMaskFilterMethods[] = {
73 { "nativeNewTable", "([B)J", (void*)SkMaskFilterGlue::createTable },
74 { "nativeNewClip", "(II)J", (void*)SkMaskFilterGlue::createClipTable },
75 { "nativeNewGamma", "(F)J", (void*)SkMaskFilterGlue::createGammaTable }
76 };
77
register_android_graphics_MaskFilter(JNIEnv * env)78 int register_android_graphics_MaskFilter(JNIEnv* env)
79 {
80 android::RegisterMethodsOrDie(env, "android/graphics/MaskFilter", gMaskFilterMethods,
81 NELEM(gMaskFilterMethods));
82 android::RegisterMethodsOrDie(env, "android/graphics/BlurMaskFilter", gBlurMaskFilterMethods,
83 NELEM(gBlurMaskFilterMethods));
84 android::RegisterMethodsOrDie(env, "android/graphics/EmbossMaskFilter",
85 gEmbossMaskFilterMethods, NELEM(gEmbossMaskFilterMethods));
86 android::RegisterMethodsOrDie(env, "android/graphics/TableMaskFilter", gTableMaskFilterMethods,
87 NELEM(gTableMaskFilterMethods));
88
89 return 0;
90 }
91