blob: 8478a02f23ff999b3f2d736d37f5726174b9cad2 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/* 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*/
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
Ashok Bhat0c10cc62014-01-10 18:38:39 +000055 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);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 }
Ashok Bhat0c10cc62014-01-10 18:38:39 +000066
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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 if (NULL == path) {
74 pair->fPath.reset();
75 } else {
76 pair->fPath = *path;
77 }
78 pair->fMeasure.setPath(&pair->fPath, forceClosed);
79 }
Ashok Bhat0c10cc62014-01-10 18:38:39 +000080
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()));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 }
Ashok Bhat0c10cc62014-01-10 18:38:39 +000085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 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
Ashok Bhat0c10cc62014-01-10 18:38:39 +000093 static jboolean getPosTan(JNIEnv* env, jobject clazz, jlong pairHandle, jfloat dist, jfloatArray pos, jfloatArray tan) {
94 PathMeasurePair* pair = reinterpret_cast<PathMeasurePair*>(pairHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 SkScalar tmpPos[2], tmpTan[2];
96 SkScalar* posPtr = pos ? tmpPos : NULL;
97 SkScalar* tanPtr = tan ? tmpTan : NULL;
98
99 if (!pair->fMeasure.getPosTan(SkFloatToScalar(dist), (SkPoint*)posPtr, (SkVector*)tanPtr)) {
Ashok Bhat0c10cc62014-01-10 18:38:39 +0000100 return JNI_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 }
102
103 if (pos) {
104 convertTwoElemFloatArray(env, pos, tmpPos);
105 }
106 if (tan) {
107 convertTwoElemFloatArray(env, tan, tmpTan);
108 }
Ashok Bhat0c10cc62014-01-10 18:38:39 +0000109 return JNI_TRUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 }
Ashok Bhat0c10cc62014-01-10 18:38:39 +0000111
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(SkFloatToScalar(dist), matrix, (SkPathMeasure::MatrixFlags)flags);
117 return result ? JNI_TRUE : JNI_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 }
Ashok Bhat0c10cc62014-01-10 18:38:39 +0000119
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(SkFloatToScalar(startF), SkFloatToScalar(stopF), dst, startWithMoveTo);
125 return result ? JNI_TRUE : JNI_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 }
Ashok Bhat0c10cc62014-01-10 18:38:39 +0000127
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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 }
Ashok Bhat0c10cc62014-01-10 18:38:39 +0000133
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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 }
Ashok Bhat0c10cc62014-01-10 18:38:39 +0000139
140 static void destroy(JNIEnv* env, jobject clazz, jlong pairHandle) {
141 PathMeasurePair* pair = reinterpret_cast<PathMeasurePair*>(pairHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 delete pair;
143 }
144};
145
146static JNINativeMethod methods[] = {
Ashok Bhat0c10cc62014-01-10 18:38:39 +0000147 {"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 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156};
157
158int register_android_graphics_PathMeasure(JNIEnv* env) {
159 int result = AndroidRuntime::registerNativeMethods(env, "android/graphics/PathMeasure", methods,
160 sizeof(methods) / sizeof(methods[0]));
161 return result;
162}
163
164}