1 #include <jni.h>
2 #include "GraphicsJNI.h"
3 
4 #include "core_jni_helpers.h"
5 
6 #include "SkPathEffect.h"
7 #include "SkCornerPathEffect.h"
8 #include "SkDashPathEffect.h"
9 #include "SkDiscretePathEffect.h"
10 #include "Sk1DPathEffect.h"
11 #include "SkTemplates.h"
12 
13 class SkPathEffectGlue {
14 public:
15 
destructor(JNIEnv * env,jobject,jlong effectHandle)16     static void destructor(JNIEnv* env, jobject, jlong effectHandle) {
17         SkPathEffect* effect = reinterpret_cast<SkPathEffect*>(effectHandle);
18         SkSafeUnref(effect);
19     }
20 
Compose_constructor(JNIEnv * env,jobject,jlong outerHandle,jlong innerHandle)21     static jlong Compose_constructor(JNIEnv* env, jobject,
22                                      jlong outerHandle, jlong innerHandle) {
23         SkPathEffect* outer = reinterpret_cast<SkPathEffect*>(outerHandle);
24         SkPathEffect* inner = reinterpret_cast<SkPathEffect*>(innerHandle);
25         SkPathEffect* effect = SkComposePathEffect::Create(outer, inner);
26         return reinterpret_cast<jlong>(effect);
27     }
28 
Sum_constructor(JNIEnv * env,jobject,jlong firstHandle,jlong secondHandle)29     static jlong Sum_constructor(JNIEnv* env, jobject,
30                                  jlong firstHandle, jlong secondHandle) {
31         SkPathEffect* first = reinterpret_cast<SkPathEffect*>(firstHandle);
32         SkPathEffect* second = reinterpret_cast<SkPathEffect*>(secondHandle);
33         SkPathEffect* effect = SkSumPathEffect::Create(first, second);
34         return reinterpret_cast<jlong>(effect);
35     }
36 
Dash_constructor(JNIEnv * env,jobject,jfloatArray intervalArray,jfloat phase)37     static jlong Dash_constructor(JNIEnv* env, jobject,
38                                       jfloatArray intervalArray, jfloat phase) {
39         AutoJavaFloatArray autoInterval(env, intervalArray);
40         int         count = autoInterval.length() & ~1;  // even number
41 #ifdef SK_SCALAR_IS_FLOAT
42         SkScalar*   intervals = autoInterval.ptr();
43 #else
44         #error Need to convert float array to SkScalar array before calling the following function.
45 #endif
46         SkPathEffect* effect = SkDashPathEffect::Create(intervals, count, phase);
47         return reinterpret_cast<jlong>(effect);
48     }
49 
OneD_constructor(JNIEnv * env,jobject,jlong shapeHandle,jfloat advance,jfloat phase,jint style)50     static jlong OneD_constructor(JNIEnv* env, jobject,
51                   jlong shapeHandle, jfloat advance, jfloat phase, jint style) {
52         const SkPath* shape = reinterpret_cast<SkPath*>(shapeHandle);
53         SkASSERT(shape != NULL);
54         SkPathEffect* effect = SkPath1DPathEffect::Create(*shape, advance, phase,
55                 (SkPath1DPathEffect::Style)style);
56         return reinterpret_cast<jlong>(effect);
57     }
58 
Corner_constructor(JNIEnv * env,jobject,jfloat radius)59     static jlong Corner_constructor(JNIEnv* env, jobject, jfloat radius){
60         SkPathEffect* effect = SkCornerPathEffect::Create(radius);
61         return reinterpret_cast<jlong>(effect);
62     }
63 
Discrete_constructor(JNIEnv * env,jobject,jfloat length,jfloat deviation)64     static jlong Discrete_constructor(JNIEnv* env, jobject,
65                                       jfloat length, jfloat deviation) {
66         SkPathEffect* effect = SkDiscretePathEffect::Create(length, deviation);
67         return reinterpret_cast<jlong>(effect);
68     }
69 
70 };
71 
72 ////////////////////////////////////////////////////////////////////////////////////////////////////////
73 
74 static JNINativeMethod gPathEffectMethods[] = {
75     { "nativeDestructor", "(J)V", (void*)SkPathEffectGlue::destructor }
76 };
77 
78 static JNINativeMethod gComposePathEffectMethods[] = {
79     { "nativeCreate", "(JJ)J", (void*)SkPathEffectGlue::Compose_constructor }
80 };
81 
82 static JNINativeMethod gSumPathEffectMethods[] = {
83     { "nativeCreate", "(JJ)J", (void*)SkPathEffectGlue::Sum_constructor }
84 };
85 
86 static JNINativeMethod gDashPathEffectMethods[] = {
87     { "nativeCreate", "([FF)J", (void*)SkPathEffectGlue::Dash_constructor }
88 };
89 
90 static JNINativeMethod gPathDashPathEffectMethods[] = {
91     { "nativeCreate", "(JFFI)J", (void*)SkPathEffectGlue::OneD_constructor }
92 };
93 
94 static JNINativeMethod gCornerPathEffectMethods[] = {
95     { "nativeCreate", "(F)J", (void*)SkPathEffectGlue::Corner_constructor }
96 };
97 
98 static JNINativeMethod gDiscretePathEffectMethods[] = {
99     { "nativeCreate", "(FF)J", (void*)SkPathEffectGlue::Discrete_constructor }
100 };
101 
register_android_graphics_PathEffect(JNIEnv * env)102 int register_android_graphics_PathEffect(JNIEnv* env)
103 {
104     android::RegisterMethodsOrDie(env, "android/graphics/PathEffect", gPathEffectMethods,
105                          NELEM(gPathEffectMethods));
106     android::RegisterMethodsOrDie(env, "android/graphics/ComposePathEffect",
107                                   gComposePathEffectMethods, NELEM(gComposePathEffectMethods));
108     android::RegisterMethodsOrDie(env, "android/graphics/SumPathEffect", gSumPathEffectMethods,
109                                   NELEM(gSumPathEffectMethods));
110     android::RegisterMethodsOrDie(env, "android/graphics/DashPathEffect", gDashPathEffectMethods,
111                                   NELEM(gDashPathEffectMethods));
112     android::RegisterMethodsOrDie(env, "android/graphics/PathDashPathEffect",
113                                   gPathDashPathEffectMethods, NELEM(gPathDashPathEffectMethods));
114     android::RegisterMethodsOrDie(env, "android/graphics/CornerPathEffect",
115                                   gCornerPathEffectMethods, NELEM(gCornerPathEffectMethods));
116     android::RegisterMethodsOrDie(env, "android/graphics/DiscretePathEffect",
117                                   gDiscretePathEffectMethods, NELEM(gDiscretePathEffectMethods));
118 
119     return 0;
120 }
121