1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
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
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#include "jni.h"
18#include "GraphicsJNI.h"
19
20#include <PathParser.h>
21#include <SkPath.h>
22#include <utils/VectorDrawableUtils.h>
23
24#include <android/log.h>
25#include "core_jni_helpers.h"
26
27namespace android {
28
29using namespace uirenderer;
30
31static void parseStringForPath(JNIEnv* env, jobject, jlong skPathHandle, jstring inputPathStr,
32        jint strLength) {
33    const char* pathString = env->GetStringUTFChars(inputPathStr, NULL);
34    SkPath* skPath = reinterpret_cast<SkPath*>(skPathHandle);
35
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());
41    }
42}
43
44static long createEmptyPathData(JNIEnv*, jobject) {
45    PathData* pathData = new PathData();
46    return reinterpret_cast<jlong>(pathData);
47}
48
49static 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);
53}
54
55static 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);
63    } else {
64        delete pathData;
65        doThrowIAE(env, result.failureMessage.c_str());
66        return NULL;
67    }
68}
69
70static 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);
77}
78
79static void deletePathData(JNIEnv*, jobject, jlong pathDataHandle) {
80    PathData* pathData = reinterpret_cast<PathData*>(pathDataHandle);
81    delete pathData;
82}
83
84static 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);
88}
89
90static 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;
94}
95
96static 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);
100}
101
102static const JNINativeMethod gMethods[] = {
103    {"nParseStringForPath", "(JLjava/lang/String;I)V", (void*)parseStringForPath},
104    {"nCreatePathDataFromString", "(Ljava/lang/String;I)J", (void*)createPathDataFromStringPath},
105
106    // ---------------- @FastNative -----------------
107
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},
115};
116
117int register_android_util_PathParser(JNIEnv* env) {
118    return RegisterMethodsOrDie(env, "android/util/PathParser", gMethods, NELEM(gMethods));
119}
120};
121