| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 18 | #include "GraphicsJNI.h" |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | |
| Kevin Lubick | 361b9f77 | 2024-01-22 13:57:07 +0000 | [diff] [blame] | 20 | #include "SkMatrix.h" |
| 21 | #include "SkPath.h" |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | #include "SkPathMeasure.h" |
| Kevin Lubick | 361b9f77 | 2024-01-22 13:57:07 +0000 | [diff] [blame] | 23 | #include "SkPoint.h" |
| 24 | #include "SkScalar.h" |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 { |
| 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 | |
| 52 | namespace android { |
| 53 | |
| 54 | class SkPathMeasureGlue { |
| 55 | public: |
| 56 | |
| Ashok Bhat | 0c10cc6 | 2014-01-10 18:38:39 +0000 | [diff] [blame] | 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); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | } |
| Ashok Bhat | 0c10cc6 | 2014-01-10 18:38:39 +0000 | [diff] [blame] | 68 | |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | if (NULL == path) { |
| 76 | pair->fPath.reset(); |
| 77 | } else { |
| 78 | pair->fPath = *path; |
| 79 | } |
| 80 | pair->fMeasure.setPath(&pair->fPath, forceClosed); |
| 81 | } |
| Ashok Bhat | 0c10cc6 | 2014-01-10 18:38:39 +0000 | [diff] [blame] | 82 | |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | } |
| Ashok Bhat | 0c10cc6 | 2014-01-10 18:38:39 +0000 | [diff] [blame] | 87 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
| Ashok Bhat | 0c10cc6 | 2014-01-10 18:38:39 +0000 | [diff] [blame] | 95 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | SkScalar tmpPos[2], tmpTan[2]; |
| 98 | SkScalar* posPtr = pos ? tmpPos : NULL; |
| 99 | SkScalar* tanPtr = tan ? tmpTan : NULL; |
| 100 | |
| Leon Scroggins III | 2e0103e | 2014-04-04 17:05:24 -0400 | [diff] [blame] | 101 | if (!pair->fMeasure.getPosTan(dist, (SkPoint*)posPtr, (SkVector*)tanPtr)) { |
| Ashok Bhat | 0c10cc6 | 2014-01-10 18:38:39 +0000 | [diff] [blame] | 102 | return JNI_FALSE; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | if (pos) { |
| 106 | convertTwoElemFloatArray(env, pos, tmpPos); |
| 107 | } |
| 108 | if (tan) { |
| 109 | convertTwoElemFloatArray(env, tan, tmpTan); |
| 110 | } |
| Ashok Bhat | 0c10cc6 | 2014-01-10 18:38:39 +0000 | [diff] [blame] | 111 | return JNI_TRUE; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | } |
| Ashok Bhat | 0c10cc6 | 2014-01-10 18:38:39 +0000 | [diff] [blame] | 113 | |
| 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 III | 2e0103e | 2014-04-04 17:05:24 -0400 | [diff] [blame] | 118 | bool result = pair->fMeasure.getMatrix(dist, matrix, (SkPathMeasure::MatrixFlags)flags); |
| Ashok Bhat | 0c10cc6 | 2014-01-10 18:38:39 +0000 | [diff] [blame] | 119 | return result ? JNI_TRUE : JNI_FALSE; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | } |
| Ashok Bhat | 0c10cc6 | 2014-01-10 18:38:39 +0000 | [diff] [blame] | 121 | |
| 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 III | 2e0103e | 2014-04-04 17:05:24 -0400 | [diff] [blame] | 126 | bool result = pair->fMeasure.getSegment(startF, stopF, dst, startWithMoveTo); |
| Ashok Bhat | 0c10cc6 | 2014-01-10 18:38:39 +0000 | [diff] [blame] | 127 | return result ? JNI_TRUE : JNI_FALSE; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | } |
| Ashok Bhat | 0c10cc6 | 2014-01-10 18:38:39 +0000 | [diff] [blame] | 129 | |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 134 | } |
| Ashok Bhat | 0c10cc6 | 2014-01-10 18:38:39 +0000 | [diff] [blame] | 135 | |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | } |
| Ashok Bhat | 0c10cc6 | 2014-01-10 18:38:39 +0000 | [diff] [blame] | 141 | |
| 142 | static void destroy(JNIEnv* env, jobject clazz, jlong pairHandle) { |
| 143 | PathMeasurePair* pair = reinterpret_cast<PathMeasurePair*>(pairHandle); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | delete pair; |
| 145 | } |
| 146 | }; |
| 147 | |
| Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 148 | static const JNINativeMethod methods[] = { |
| Ashok Bhat | 0c10cc6 | 2014-01-10 18:38:39 +0000 | [diff] [blame] | 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 } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | }; |
| 159 | |
| 160 | int register_android_graphics_PathMeasure(JNIEnv* env) { |
| Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 161 | return RegisterMethodsOrDie(env, "android/graphics/PathMeasure", methods, NELEM(methods)); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | } |