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