1 #include <jni.h>
2 #include "GraphicsJNI.h"
3
4 #include "SkPathEffect.h"
5 #include "SkCornerPathEffect.h"
6 #include "SkDashPathEffect.h"
7 #include "SkDiscretePathEffect.h"
8 #include "Sk1DPathEffect.h"
9 #include "SkTemplates.h"
10
11 class SkPathEffectGlue {
12 public:
13
destructor(JNIEnv * env,jobject,SkPathEffect * effect)14 static void destructor(JNIEnv* env, jobject, SkPathEffect* effect) {
15 SkSafeUnref(effect);
16 }
17
Compose_constructor(JNIEnv * env,jobject,SkPathEffect * outer,SkPathEffect * inner)18 static SkPathEffect* Compose_constructor(JNIEnv* env, jobject,
19 SkPathEffect* outer, SkPathEffect* inner) {
20 return new SkComposePathEffect(outer, inner);
21 }
22
Sum_constructor(JNIEnv * env,jobject,SkPathEffect * first,SkPathEffect * second)23 static SkPathEffect* Sum_constructor(JNIEnv* env, jobject,
24 SkPathEffect* first, SkPathEffect* second) {
25 return new SkSumPathEffect(first, second);
26 }
27
Dash_constructor(JNIEnv * env,jobject,jfloatArray intervalArray,float phase)28 static SkPathEffect* Dash_constructor(JNIEnv* env, jobject,
29 jfloatArray intervalArray, float phase) {
30 AutoJavaFloatArray autoInterval(env, intervalArray);
31 int count = autoInterval.length() & ~1; // even number
32 float* values = autoInterval.ptr();
33
34 SkAutoSTMalloc<32, SkScalar> storage(count);
35 SkScalar* intervals = storage.get();
36 for (int i = 0; i < count; i++) {
37 intervals[i] = SkFloatToScalar(values[i]);
38 }
39 return new SkDashPathEffect(intervals, count, SkFloatToScalar(phase));
40 }
41
OneD_constructor(JNIEnv * env,jobject,const SkPath * shape,float advance,float phase,int style)42 static SkPathEffect* OneD_constructor(JNIEnv* env, jobject,
43 const SkPath* shape, float advance, float phase, int style) {
44 SkASSERT(shape != NULL);
45 return new SkPath1DPathEffect(*shape, SkFloatToScalar(advance),
46 SkFloatToScalar(phase), (SkPath1DPathEffect::Style)style);
47 }
48
Corner_constructor(JNIEnv * env,jobject,float radius)49 static SkPathEffect* Corner_constructor(JNIEnv* env, jobject, float radius){
50 return new SkCornerPathEffect(SkFloatToScalar(radius));
51 }
52
Discrete_constructor(JNIEnv * env,jobject,float length,float deviation)53 static SkPathEffect* Discrete_constructor(JNIEnv* env, jobject,
54 float length, float deviation) {
55 return new SkDiscretePathEffect(SkFloatToScalar(length),
56 SkFloatToScalar(deviation));
57 }
58
59 };
60
61 ////////////////////////////////////////////////////////////////////////////////////////////////////////
62
63 static JNINativeMethod gPathEffectMethods[] = {
64 { "nativeDestructor", "(I)V", (void*)SkPathEffectGlue::destructor }
65 };
66
67 static JNINativeMethod gComposePathEffectMethods[] = {
68 { "nativeCreate", "(II)I", (void*)SkPathEffectGlue::Compose_constructor }
69 };
70
71 static JNINativeMethod gSumPathEffectMethods[] = {
72 { "nativeCreate", "(II)I", (void*)SkPathEffectGlue::Sum_constructor }
73 };
74
75 static JNINativeMethod gDashPathEffectMethods[] = {
76 { "nativeCreate", "([FF)I", (void*)SkPathEffectGlue::Dash_constructor }
77 };
78
79 static JNINativeMethod gPathDashPathEffectMethods[] = {
80 { "nativeCreate", "(IFFI)I", (void*)SkPathEffectGlue::OneD_constructor }
81 };
82
83 static JNINativeMethod gCornerPathEffectMethods[] = {
84 { "nativeCreate", "(F)I", (void*)SkPathEffectGlue::Corner_constructor }
85 };
86
87 static JNINativeMethod gDiscretePathEffectMethods[] = {
88 { "nativeCreate", "(FF)I", (void*)SkPathEffectGlue::Discrete_constructor }
89 };
90
91 #include <android_runtime/AndroidRuntime.h>
92
93 #define REG(env, name, array) \
94 result = android::AndroidRuntime::registerNativeMethods(env, name, array, \
95 SK_ARRAY_COUNT(array)); \
96 if (result < 0) return result
97
register_android_graphics_PathEffect(JNIEnv * env)98 int register_android_graphics_PathEffect(JNIEnv* env)
99 {
100 int result;
101
102 REG(env, "android/graphics/PathEffect", gPathEffectMethods);
103 REG(env, "android/graphics/ComposePathEffect", gComposePathEffectMethods);
104 REG(env, "android/graphics/SumPathEffect", gSumPathEffectMethods);
105 REG(env, "android/graphics/DashPathEffect", gDashPathEffectMethods);
106 REG(env, "android/graphics/PathDashPathEffect", gPathDashPathEffectMethods);
107 REG(env, "android/graphics/CornerPathEffect", gCornerPathEffectMethods);
108 REG(env, "android/graphics/DiscretePathEffect", gDiscretePathEffectMethods);
109
110 return 0;
111 }
112