PathEffect.cpp revision cc11f15f76a62ded3e403cb2bc818c6aa5bf261c
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        float*  values = autoInterval.ptr();
40
41        SkAutoSTMalloc<32, SkScalar>    storage(count);
42        SkScalar*                       intervals = storage.get();
43        for (int i = 0; i < count; i++) {
44            intervals[i] = SkFloatToScalar(values[i]);
45        }
46        SkPathEffect* effect = SkDashPathEffect::Create(intervals, count, SkFloatToScalar(phase));
47        return reinterpret_cast<jlong>(effect);
48    }
49
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, SkFloatToScalar(advance),
55                     SkFloatToScalar(phase), (SkPath1DPathEffect::Style)style);
56        return reinterpret_cast<jlong>(effect);
57    }
58
59    static jlong Corner_constructor(JNIEnv* env, jobject, jfloat radius){
60        SkPathEffect* effect = SkCornerPathEffect::Create(SkFloatToScalar(radius));
61        return reinterpret_cast<jlong>(effect);
62    }
63
64    static jlong Discrete_constructor(JNIEnv* env, jobject,
65                                      jfloat length, jfloat deviation) {
66        SkPathEffect* effect = SkDiscretePathEffect::Create(SkFloatToScalar(length),
67                                        SkFloatToScalar(deviation));
68        return reinterpret_cast<jlong>(effect);
69    }
70
71};
72
73////////////////////////////////////////////////////////////////////////////////////////////////////////
74
75static JNINativeMethod gPathEffectMethods[] = {
76    { "nativeDestructor", "(J)V", (void*)SkPathEffectGlue::destructor }
77};
78
79static JNINativeMethod gComposePathEffectMethods[] = {
80    { "nativeCreate", "(JJ)J", (void*)SkPathEffectGlue::Compose_constructor }
81};
82
83static JNINativeMethod gSumPathEffectMethods[] = {
84    { "nativeCreate", "(JJ)J", (void*)SkPathEffectGlue::Sum_constructor }
85};
86
87static JNINativeMethod gDashPathEffectMethods[] = {
88    { "nativeCreate", "([FF)J", (void*)SkPathEffectGlue::Dash_constructor }
89};
90
91static JNINativeMethod gPathDashPathEffectMethods[] = {
92    { "nativeCreate", "(JFFI)J", (void*)SkPathEffectGlue::OneD_constructor }
93};
94
95static JNINativeMethod gCornerPathEffectMethods[] = {
96    { "nativeCreate", "(F)J", (void*)SkPathEffectGlue::Corner_constructor }
97};
98
99static JNINativeMethod gDiscretePathEffectMethods[] = {
100    { "nativeCreate", "(FF)J", (void*)SkPathEffectGlue::Discrete_constructor }
101};
102
103#include <android_runtime/AndroidRuntime.h>
104
105#define REG(env, name, array)                                              \
106    result = android::AndroidRuntime::registerNativeMethods(env, name, array, \
107                                                  SK_ARRAY_COUNT(array));  \
108    if (result < 0) return result
109
110int register_android_graphics_PathEffect(JNIEnv* env)
111{
112    int result;
113
114    REG(env, "android/graphics/PathEffect", gPathEffectMethods);
115    REG(env, "android/graphics/ComposePathEffect", gComposePathEffectMethods);
116    REG(env, "android/graphics/SumPathEffect", gSumPathEffectMethods);
117    REG(env, "android/graphics/DashPathEffect", gDashPathEffectMethods);
118    REG(env, "android/graphics/PathDashPathEffect", gPathDashPathEffectMethods);
119    REG(env, "android/graphics/CornerPathEffect", gCornerPathEffectMethods);
120    REG(env, "android/graphics/DiscretePathEffect", gDiscretePathEffectMethods);
121
122    return 0;
123}
124