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 <log/log.h>
19
20 #include "GraphicsJNI.h"
21 #include "SkPath.h"
22 #include "SkPoint.h"
23 #include "graphics_jni_helpers.h"
24
25 namespace android {
26
27 class SkPathIteratorGlue {
28 public:
finalizer(SkPath::RawIter * obj)29 static void finalizer(SkPath::RawIter* obj) { delete obj; }
30
getFinalizer(JNIEnv * env,jclass clazz)31 static jlong getFinalizer(JNIEnv* env, jclass clazz) {
32 return static_cast<jlong>(reinterpret_cast<uintptr_t>(&finalizer));
33 }
34
create(JNIEnv * env,jobject clazz,jlong pathHandle)35 static jlong create(JNIEnv* env, jobject clazz, jlong pathHandle) {
36 const SkPath* path = reinterpret_cast<SkPath*>(pathHandle);
37 return reinterpret_cast<jlong>(new SkPath::RawIter(*path));
38 }
39
40 // A variant of 'next' (below) that is compatible with the host JVM.
nextHost(JNIEnv * env,jclass clazz,jlong iteratorHandle,jfloatArray pointsArray)41 static jint nextHost(JNIEnv* env, jclass clazz, jlong iteratorHandle, jfloatArray pointsArray) {
42 jfloat* points = env->GetFloatArrayElements(pointsArray, 0);
43 #ifdef __ANDROID__
44 jint result = next(iteratorHandle, reinterpret_cast<jlong>(points));
45 #else
46 jint result = next(env, clazz, iteratorHandle, reinterpret_cast<jlong>(points));
47 #endif
48 env->ReleaseFloatArrayElements(pointsArray, points, 0);
49 return result;
50 }
51
52 // ---------------- @CriticalNative -------------------------
53
peek(CRITICAL_JNI_PARAMS_COMMA jlong iteratorHandle)54 static jint peek(CRITICAL_JNI_PARAMS_COMMA jlong iteratorHandle) {
55 SkPath::RawIter* iterator = reinterpret_cast<SkPath::RawIter*>(iteratorHandle);
56 return iterator->peek();
57 }
58
next(CRITICAL_JNI_PARAMS_COMMA jlong iteratorHandle,jlong pointsArray)59 static jint next(CRITICAL_JNI_PARAMS_COMMA jlong iteratorHandle, jlong pointsArray) {
60 static_assert(SkPath::kMove_Verb == 0, "SkPath::Verb unexpected index");
61 static_assert(SkPath::kLine_Verb == 1, "SkPath::Verb unexpected index");
62 static_assert(SkPath::kQuad_Verb == 2, "SkPath::Verb unexpected index");
63 static_assert(SkPath::kConic_Verb == 3, "SkPath::Verb unexpected index");
64 static_assert(SkPath::kCubic_Verb == 4, "SkPath::Verb unexpected index");
65 static_assert(SkPath::kClose_Verb == 5, "SkPath::Verb unexpected index");
66 static_assert(SkPath::kDone_Verb == 6, "SkPath::Verb unexpected index");
67
68 SkPath::RawIter* iterator = reinterpret_cast<SkPath::RawIter*>(iteratorHandle);
69 float* points = reinterpret_cast<float*>(pointsArray);
70 SkPath::Verb verb =
71 static_cast<SkPath::Verb>(iterator->next(reinterpret_cast<SkPoint*>(points)));
72 if (verb == SkPath::kConic_Verb) {
73 float weight = iterator->conicWeight();
74 points[6] = weight;
75 }
76 return static_cast<int>(verb);
77 }
78 };
79
80 static const JNINativeMethod methods[] = {
nCreate(J)81 {"nCreate", "(J)J", (void*)SkPathIteratorGlue::create},
nGetFinalizer()82 {"nGetFinalizer", "()J", (void*)SkPathIteratorGlue::getFinalizer},
83
84 // ------- @CriticalNative below here ------------------
85
nPeek(J)86 {"nPeek", "(J)I", (void*)SkPathIteratorGlue::peek},
nNext(JJ)87 {"nNext", "(JJ)I", (void*)SkPathIteratorGlue::next},
nNextHost(J[F)88 {"nNextHost", "(J[F)I", (void*)SkPathIteratorGlue::nextHost},
89 };
90
register_android_graphics_PathIterator(JNIEnv * env)91 int register_android_graphics_PathIterator(JNIEnv* env) {
92 return RegisterMethodsOrDie(env, "android/graphics/PathIterator", methods, NELEM(methods));
93 }
94
95 } // namespace android
96