1/* libs/android_runtime/android/graphics/PathMeasure.cpp
2**
3** Copyright 2007, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9**     http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#include "jni.h"
19#include "GraphicsJNI.h"
20#include <core_jni_helpers.h>
21
22#include "SkPathMeasure.h"
23
24/*  We declare an explicit pair, so that we don't have to rely on the java
25    client to be sure not to edit the path while we have an active measure
26    object associated with it.
27
28    This costs us the copy of the path, for the sake of not allowing a bad
29    java client to randomly crash (since we can't detect the case where the
30    native path has been modified).
31
32    The C side does have this risk, but it chooses for speed over safety. If it
33    later changes this, and is internally safe from changes to the path, then
34    we can remove this explicit copy from our JNI code.
35
36    Note that we do not have a reference on the java side to the java path.
37    Were we to not need the native copy here, we would want to add a java
38    reference, so that the java path would not get GD'd while the measure object
39    was still alive.
40*/
41struct PathMeasurePair {
42    PathMeasurePair() {}
43    PathMeasurePair(const SkPath& path, bool forceClosed)
44        : fPath(path), fMeasure(fPath, forceClosed) {}
45
46    SkPath          fPath;      // copy of the user's path
47    SkPathMeasure   fMeasure;   // this guy points to fPath
48};
49
50namespace android {
51
52class SkPathMeasureGlue {
53public:
54
55    static jlong create(JNIEnv* env, jobject clazz, jlong pathHandle,
56                        jboolean forceClosedHandle) {
57        const SkPath* path = reinterpret_cast<SkPath*>(pathHandle);
58        bool forceClosed = (forceClosedHandle == JNI_TRUE);
59        PathMeasurePair* pair;
60        if(path)
61            pair = new PathMeasurePair(*path, forceClosed);
62        else
63            pair = new PathMeasurePair;
64        return reinterpret_cast<jlong>(pair);
65    }
66
67    static void setPath(JNIEnv* env, jobject clazz, jlong pairHandle,
68                        jlong pathHandle, jboolean forceClosedHandle) {
69        PathMeasurePair* pair = reinterpret_cast<PathMeasurePair*>(pairHandle);
70        const SkPath* path = reinterpret_cast<SkPath*>(pathHandle);
71        bool forceClosed = (forceClosedHandle == JNI_TRUE);
72
73        if (NULL == path) {
74            pair->fPath.reset();
75        } else {
76            pair->fPath = *path;
77        }
78        pair->fMeasure.setPath(&pair->fPath, forceClosed);
79    }
80
81    static jfloat getLength(JNIEnv* env, jobject clazz, jlong pairHandle) {
82        PathMeasurePair* pair = reinterpret_cast<PathMeasurePair*>(pairHandle);
83        return static_cast<jfloat>(SkScalarToFloat(pair->fMeasure.getLength()));
84    }
85
86    static void convertTwoElemFloatArray(JNIEnv* env, jfloatArray array, const SkScalar src[2]) {
87        AutoJavaFloatArray autoArray(env, array, 2);
88        jfloat* ptr = autoArray.ptr();
89        ptr[0] = SkScalarToFloat(src[0]);
90        ptr[1] = SkScalarToFloat(src[1]);
91    }
92
93    static jboolean getPosTan(JNIEnv* env, jobject clazz, jlong pairHandle, jfloat dist, jfloatArray pos, jfloatArray tan) {
94        PathMeasurePair* pair = reinterpret_cast<PathMeasurePair*>(pairHandle);
95        SkScalar    tmpPos[2], tmpTan[2];
96        SkScalar*   posPtr = pos ? tmpPos : NULL;
97        SkScalar*   tanPtr = tan ? tmpTan : NULL;
98
99        if (!pair->fMeasure.getPosTan(dist, (SkPoint*)posPtr, (SkVector*)tanPtr)) {
100            return JNI_FALSE;
101        }
102
103        if (pos) {
104            convertTwoElemFloatArray(env, pos, tmpPos);
105        }
106        if (tan) {
107            convertTwoElemFloatArray(env, tan, tmpTan);
108        }
109        return JNI_TRUE;
110    }
111
112    static jboolean getMatrix(JNIEnv* env, jobject clazz, jlong pairHandle, jfloat dist,
113                          jlong matrixHandle, jint flags) {
114        PathMeasurePair* pair = reinterpret_cast<PathMeasurePair*>(pairHandle);
115        SkMatrix* matrix = reinterpret_cast<SkMatrix*>(matrixHandle);
116        bool result = pair->fMeasure.getMatrix(dist, matrix, (SkPathMeasure::MatrixFlags)flags);
117        return result ? JNI_TRUE : JNI_FALSE;
118    }
119
120    static jboolean getSegment(JNIEnv* env, jobject clazz, jlong pairHandle, jfloat startF,
121                               jfloat stopF, jlong dstHandle, jboolean startWithMoveTo) {
122        PathMeasurePair* pair = reinterpret_cast<PathMeasurePair*>(pairHandle);
123        SkPath* dst = reinterpret_cast<SkPath*>(dstHandle);
124        bool result = pair->fMeasure.getSegment(startF, stopF, dst, startWithMoveTo);
125        return result ? JNI_TRUE : JNI_FALSE;
126    }
127
128    static jboolean isClosed(JNIEnv* env, jobject clazz, jlong pairHandle) {
129        PathMeasurePair* pair = reinterpret_cast<PathMeasurePair*>(pairHandle);
130        bool result = pair->fMeasure.isClosed();
131        return result ? JNI_TRUE : JNI_FALSE;
132    }
133
134    static jboolean nextContour(JNIEnv* env, jobject clazz, jlong pairHandle) {
135        PathMeasurePair* pair = reinterpret_cast<PathMeasurePair*>(pairHandle);
136        bool result = pair->fMeasure.nextContour();
137        return result ? JNI_TRUE : JNI_FALSE;
138    }
139
140    static void destroy(JNIEnv* env, jobject clazz, jlong pairHandle) {
141        PathMeasurePair* pair = reinterpret_cast<PathMeasurePair*>(pairHandle);
142        delete pair;
143    }
144};
145
146static const JNINativeMethod methods[] = {
147    {"native_create",       "(JZ)J",        (void*) SkPathMeasureGlue::create      },
148    {"native_setPath",      "(JJZ)V",       (void*) SkPathMeasureGlue::setPath     },
149    {"native_getLength",    "(J)F",         (void*) SkPathMeasureGlue::getLength   },
150    {"native_getPosTan",    "(JF[F[F)Z",    (void*) SkPathMeasureGlue::getPosTan   },
151    {"native_getMatrix",    "(JFJI)Z",      (void*) SkPathMeasureGlue::getMatrix   },
152    {"native_getSegment",   "(JFFJZ)Z",     (void*) SkPathMeasureGlue::getSegment  },
153    {"native_isClosed",     "(J)Z",         (void*) SkPathMeasureGlue::isClosed    },
154    {"native_nextContour",  "(J)Z",         (void*) SkPathMeasureGlue::nextContour },
155    {"native_destroy",      "(J)V",         (void*) SkPathMeasureGlue::destroy     }
156};
157
158int register_android_graphics_PathMeasure(JNIEnv* env) {
159    return RegisterMethodsOrDie(env, "android/graphics/PathMeasure", methods, NELEM(methods));
160}
161
162}
163