2 * Copyright (C) 2015 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "GraphicsJNI.h"
20 #include <PathParser.h>
22 #include <utils/VectorDrawableUtils.h>
24 #include <android/log.h>
25 #include "core_jni_helpers.h"
29 using namespace uirenderer;
31 static void parseStringForPath(JNIEnv* env, jobject, jlong skPathHandle, jstring inputPathStr,
33 const char* pathString = env->GetStringUTFChars(inputPathStr, NULL);
34 SkPath* skPath = reinterpret_cast<SkPath*>(skPathHandle);
36 PathParser::ParseResult result;
37 PathParser::parseAsciiStringForSkPath(skPath, &result, pathString, strLength);
38 env->ReleaseStringUTFChars(inputPathStr, pathString);
39 if (result.failureOccurred) {
40 doThrowIAE(env, result.failureMessage.c_str());
44 static long createEmptyPathData(JNIEnv*, jobject) {
45 PathData* pathData = new PathData();
46 return reinterpret_cast<jlong>(pathData);
49 static long createPathData(JNIEnv*, jobject, jlong pathDataPtr) {
50 PathData* pathData = reinterpret_cast<PathData*>(pathDataPtr);
51 PathData* newPathData = new PathData(*pathData);
52 return reinterpret_cast<jlong>(newPathData);
55 static long createPathDataFromStringPath(JNIEnv* env, jobject, jstring inputStr, jint strLength) {
56 const char* pathString = env->GetStringUTFChars(inputStr, NULL);
57 PathData* pathData = new PathData();
58 PathParser::ParseResult result;
59 PathParser::getPathDataFromAsciiString(pathData, &result, pathString, strLength);
60 env->ReleaseStringUTFChars(inputStr, pathString);
61 if (!result.failureOccurred) {
62 return reinterpret_cast<jlong>(pathData);
65 doThrowIAE(env, result.failureMessage.c_str());
70 static bool interpolatePathData(JNIEnv*, jobject, jlong outPathDataPtr, jlong fromPathDataPtr,
71 jlong toPathDataPtr, jfloat fraction) {
72 PathData* outPathData = reinterpret_cast<PathData*>(outPathDataPtr);
73 PathData* fromPathData = reinterpret_cast<PathData*>(fromPathDataPtr);
74 PathData* toPathData = reinterpret_cast<PathData*>(toPathDataPtr);
75 return VectorDrawableUtils::interpolatePathData(outPathData, *fromPathData,
76 *toPathData, fraction);
79 static void deletePathData(JNIEnv*, jobject, jlong pathDataHandle) {
80 PathData* pathData = reinterpret_cast<PathData*>(pathDataHandle);
84 static bool canMorphPathData(JNIEnv*, jobject, jlong fromPathDataPtr, jlong toPathDataPtr) {
85 PathData* fromPathData = reinterpret_cast<PathData*>(fromPathDataPtr);
86 PathData* toPathData = reinterpret_cast<PathData*>(toPathDataPtr);
87 return VectorDrawableUtils::canMorph(*fromPathData, *toPathData);
90 static void setPathData(JNIEnv*, jobject, jlong outPathDataPtr, jlong fromPathDataPtr) {
91 PathData* fromPathData = reinterpret_cast<PathData*>(fromPathDataPtr);
92 PathData* outPathData = reinterpret_cast<PathData*>(outPathDataPtr);
93 *outPathData = *fromPathData;
96 static void setSkPathFromPathData(JNIEnv*, jobject, jlong outPathPtr, jlong pathDataPtr) {
97 PathData* pathData = reinterpret_cast<PathData*>(pathDataPtr);
98 SkPath* skPath = reinterpret_cast<SkPath*>(outPathPtr);
99 VectorDrawableUtils::verbsToPath(skPath, *pathData);
102 static const JNINativeMethod gMethods[] = {
103 {"nParseStringForPath", "(JLjava/lang/String;I)V", (void*)parseStringForPath},
104 {"nCreatePathDataFromString", "(Ljava/lang/String;I)J", (void*)createPathDataFromStringPath},
106 // ---------------- @FastNative -----------------
108 {"nCreateEmptyPathData", "()J", (void*)createEmptyPathData},
109 {"nCreatePathData", "(J)J", (void*)createPathData},
110 {"nInterpolatePathData", "(JJJF)Z", (void*)interpolatePathData},
111 {"nFinalize", "(J)V", (void*)deletePathData},
112 {"nCanMorph", "(JJ)Z", (void*)canMorphPathData},
113 {"nSetPathData", "(JJ)V", (void*)setPathData},
114 {"nCreatePathFromPathData", "(JJ)V", (void*)setSkPathFromPathData},
117 int register_android_util_PathParser(JNIEnv* env) {
118 return RegisterMethodsOrDie(env, "android/util/PathParser", gMethods, NELEM(gMethods));