• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <android_runtime/AndroidRuntime.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 */
41 struct PathMeasurePair {
PathMeasurePairPathMeasurePair42     PathMeasurePair() {}
PathMeasurePairPathMeasurePair43     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 
50 namespace android {
51 
52 class SkPathMeasureGlue {
53 public:
54 
create(JNIEnv * env,jobject clazz,const SkPath * path,jboolean forceClosed)55     static PathMeasurePair* create(JNIEnv* env, jobject clazz, const SkPath* path, jboolean forceClosed) {
56         return path ? new PathMeasurePair(*path, forceClosed) : new PathMeasurePair;
57     }
58 
setPath(JNIEnv * env,jobject clazz,PathMeasurePair * pair,const SkPath * path,jboolean forceClosed)59     static void setPath(JNIEnv* env, jobject clazz, PathMeasurePair* pair, const SkPath* path, jboolean forceClosed) {
60         if (NULL == path) {
61             pair->fPath.reset();
62         } else {
63             pair->fPath = *path;
64         }
65         pair->fMeasure.setPath(&pair->fPath, forceClosed);
66     }
67 
getLength(JNIEnv * env,jobject clazz,PathMeasurePair * pair)68     static jfloat getLength(JNIEnv* env, jobject clazz, PathMeasurePair* pair) {
69         return SkScalarToFloat(pair->fMeasure.getLength());
70     }
71 
convertTwoElemFloatArray(JNIEnv * env,jfloatArray array,const SkScalar src[2])72     static void convertTwoElemFloatArray(JNIEnv* env, jfloatArray array, const SkScalar src[2]) {
73         AutoJavaFloatArray autoArray(env, array, 2);
74         jfloat* ptr = autoArray.ptr();
75         ptr[0] = SkScalarToFloat(src[0]);
76         ptr[1] = SkScalarToFloat(src[1]);
77     }
78 
getPosTan(JNIEnv * env,jobject clazz,PathMeasurePair * pair,jfloat dist,jfloatArray pos,jfloatArray tan)79     static jboolean getPosTan(JNIEnv* env, jobject clazz, PathMeasurePair* pair, jfloat dist, jfloatArray pos, jfloatArray tan) {
80         SkScalar    tmpPos[2], tmpTan[2];
81         SkScalar*   posPtr = pos ? tmpPos : NULL;
82         SkScalar*   tanPtr = tan ? tmpTan : NULL;
83 
84         if (!pair->fMeasure.getPosTan(SkFloatToScalar(dist), (SkPoint*)posPtr, (SkVector*)tanPtr)) {
85             return false;
86         }
87 
88         if (pos) {
89             convertTwoElemFloatArray(env, pos, tmpPos);
90         }
91         if (tan) {
92             convertTwoElemFloatArray(env, tan, tmpTan);
93         }
94         return true;
95     }
96 
getMatrix(JNIEnv * env,jobject clazz,PathMeasurePair * pair,jfloat dist,SkMatrix * matrix,int flags)97     static jboolean getMatrix(JNIEnv* env, jobject clazz, PathMeasurePair* pair, jfloat dist,
98                           SkMatrix* matrix, int flags) {
99         return pair->fMeasure.getMatrix(SkFloatToScalar(dist), matrix, (SkPathMeasure::MatrixFlags)flags);
100     }
101 
getSegment(JNIEnv * env,jobject clazz,PathMeasurePair * pair,jfloat startF,jfloat stopF,SkPath * dst,jboolean startWithMoveTo)102     static jboolean getSegment(JNIEnv* env, jobject clazz, PathMeasurePair* pair, jfloat startF,
103                                jfloat stopF, SkPath* dst, jboolean startWithMoveTo) {
104         return pair->fMeasure.getSegment(SkFloatToScalar(startF), SkFloatToScalar(stopF), dst, startWithMoveTo);
105     }
106 
isClosed(JNIEnv * env,jobject clazz,PathMeasurePair * pair)107     static jboolean isClosed(JNIEnv* env, jobject clazz, PathMeasurePair* pair) {
108         return pair->fMeasure.isClosed();
109     }
110 
nextContour(JNIEnv * env,jobject clazz,PathMeasurePair * pair)111     static jboolean nextContour(JNIEnv* env, jobject clazz, PathMeasurePair* pair) {
112         return pair->fMeasure.nextContour();
113     }
114 
destroy(JNIEnv * env,jobject clazz,PathMeasurePair * pair)115     static void destroy(JNIEnv* env, jobject clazz, PathMeasurePair* pair) {
116         delete pair;
117     }
118 };
119 
120 static JNINativeMethod methods[] = {
121     {"native_create",       "(IZ)I",        (void*) SkPathMeasureGlue::create      },
122     {"native_setPath",      "(IIZ)V",       (void*) SkPathMeasureGlue::setPath     },
123     {"native_getLength",    "(I)F",         (void*) SkPathMeasureGlue::getLength   },
124     {"native_getPosTan",    "(IF[F[F)Z",    (void*) SkPathMeasureGlue::getPosTan   },
125     {"native_getMatrix",    "(IFII)Z",      (void*) SkPathMeasureGlue::getMatrix   },
126     {"native_getSegment",   "(IFFIZ)Z",     (void*) SkPathMeasureGlue::getSegment  },
127     {"native_isClosed",     "(I)Z",         (void*) SkPathMeasureGlue::isClosed    },
128     {"native_nextContour",  "(I)Z",         (void*) SkPathMeasureGlue::nextContour },
129     {"native_destroy",      "(I)V",         (void*) SkPathMeasureGlue::destroy     }
130 };
131 
register_android_graphics_PathMeasure(JNIEnv * env)132 int register_android_graphics_PathMeasure(JNIEnv* env) {
133     int result = AndroidRuntime::registerNativeMethods(env, "android/graphics/PathMeasure", methods,
134         sizeof(methods) / sizeof(methods[0]));
135     return result;
136 }
137 
138 }
139