blob: 79acb6cc35e5a894402b9c6c5b6007316a0e53ff [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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018#include "GraphicsJNI.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019
Kevin Lubick361b9f772024-01-22 13:57:07 +000020#include "SkMatrix.h"
21#include "SkPath.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022#include "SkPathMeasure.h"
Kevin Lubick361b9f772024-01-22 13:57:07 +000023#include "SkPoint.h"
24#include "SkScalar.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025
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*/
43struct PathMeasurePair {
44 PathMeasurePair() {}
45 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
52namespace android {
53
54class SkPathMeasureGlue {
55public:
56
Ashok Bhat0c10cc62014-01-10 18:38:39 +000057 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);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 }
Ashok Bhat0c10cc62014-01-10 18:38:39 +000068
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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 if (NULL == path) {
76 pair->fPath.reset();
77 } else {
78 pair->fPath = *path;
79 }
80 pair->fMeasure.setPath(&pair->fPath, forceClosed);
81 }
Ashok Bhat0c10cc62014-01-10 18:38:39 +000082
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()));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 }
Ashok Bhat0c10cc62014-01-10 18:38:39 +000087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 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
Ashok Bhat0c10cc62014-01-10 18:38:39 +000095 static jboolean getPosTan(JNIEnv* env, jobject clazz, jlong pairHandle, jfloat dist, jfloatArray pos, jfloatArray tan) {
96 PathMeasurePair* pair = reinterpret_cast<PathMeasurePair*>(pairHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 SkScalar tmpPos[2], tmpTan[2];
98 SkScalar* posPtr = pos ? tmpPos : NULL;
99 SkScalar* tanPtr = tan ? tmpTan : NULL;
100
Leon Scroggins III2e0103e2014-04-04 17:05:24 -0400101 if (!pair->fMeasure.getPosTan(dist, (SkPoint*)posPtr, (SkVector*)tanPtr)) {
Ashok Bhat0c10cc62014-01-10 18:38:39 +0000102 return JNI_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 }
104
105 if (pos) {
106 convertTwoElemFloatArray(env, pos, tmpPos);
107 }
108 if (tan) {
109 convertTwoElemFloatArray(env, tan, tmpTan);
110 }
Ashok Bhat0c10cc62014-01-10 18:38:39 +0000111 return JNI_TRUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 }
Ashok Bhat0c10cc62014-01-10 18:38:39 +0000113
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);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -0400118 bool result = pair->fMeasure.getMatrix(dist, matrix, (SkPathMeasure::MatrixFlags)flags);
Ashok Bhat0c10cc62014-01-10 18:38:39 +0000119 return result ? JNI_TRUE : JNI_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 }
Ashok Bhat0c10cc62014-01-10 18:38:39 +0000121
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);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -0400126 bool result = pair->fMeasure.getSegment(startF, stopF, dst, startWithMoveTo);
Ashok Bhat0c10cc62014-01-10 18:38:39 +0000127 return result ? JNI_TRUE : JNI_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 }
Ashok Bhat0c10cc62014-01-10 18:38:39 +0000129
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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 }
Ashok Bhat0c10cc62014-01-10 18:38:39 +0000135
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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 }
Ashok Bhat0c10cc62014-01-10 18:38:39 +0000141
142 static void destroy(JNIEnv* env, jobject clazz, jlong pairHandle) {
143 PathMeasurePair* pair = reinterpret_cast<PathMeasurePair*>(pairHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 delete pair;
145 }
146};
147
Daniel Micay76f6a862015-09-19 17:31:01 -0400148static const JNINativeMethod methods[] = {
Ashok Bhat0c10cc62014-01-10 18:38:39 +0000149 {"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 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158};
159
160int register_android_graphics_PathMeasure(JNIEnv* env) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800161 return RegisterMethodsOrDie(env, "android/graphics/PathMeasure", methods, NELEM(methods));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162}
163
164}