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