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