blob: 53669a8d4bd5d0f4914b17e7246bf459d06a6dd6 [file] [log] [blame]
Doris Liucdd23f92015-11-11 14:31:13 -08001/*
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"
Doris Liu0a1a5162016-04-07 15:03:11 -070018#include "GraphicsJNI.h"
Doris Liucdd23f92015-11-11 14:31:13 -080019
20#include <PathParser.h>
21#include <SkPath.h>
Doris Liu804618d2015-11-16 22:48:34 -080022#include <utils/VectorDrawableUtils.h>
Doris Liucdd23f92015-11-11 14:31:13 -080023
Doris Liu1e67f082015-11-12 15:57:45 -080024#include <android/log.h>
Doris Liucdd23f92015-11-11 14:31:13 -080025#include "core_jni_helpers.h"
26
27namespace android {
28
Doris Liu804618d2015-11-16 22:48:34 -080029using namespace uirenderer;
30
Doris Liu0a1a5162016-04-07 15:03:11 -070031static void parseStringForPath(JNIEnv* env, jobject, jlong skPathHandle, jstring inputPathStr,
Doris Liucdd23f92015-11-11 14:31:13 -080032 jint strLength) {
33 const char* pathString = env->GetStringUTFChars(inputPathStr, NULL);
34 SkPath* skPath = reinterpret_cast<SkPath*>(skPathHandle);
Doris Liu1e67f082015-11-12 15:57:45 -080035
Doris Liu804618d2015-11-16 22:48:34 -080036 PathParser::ParseResult result;
Doris Liub35da392016-04-12 11:06:23 -070037 PathParser::parseAsciiStringForSkPath(skPath, &result, pathString, strLength);
Doris Liucdd23f92015-11-11 14:31:13 -080038 env->ReleaseStringUTFChars(inputPathStr, pathString);
Doris Liu1e67f082015-11-12 15:57:45 -080039 if (result.failureOccurred) {
Doris Liu0a1a5162016-04-07 15:03:11 -070040 doThrowIAE(env, result.failureMessage.c_str());
Doris Liu1e67f082015-11-12 15:57:45 -080041 }
Doris Liucdd23f92015-11-11 14:31:13 -080042}
43
Doris Liu804618d2015-11-16 22:48:34 -080044static 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;
Doris Liub35da392016-04-12 11:06:23 -070059 PathParser::getPathDataFromAsciiString(pathData, &result, pathString, strLength);
Doris Liu804618d2015-11-16 22:48:34 -080060 env->ReleaseStringUTFChars(inputStr, pathString);
61 if (!result.failureOccurred) {
62 return reinterpret_cast<jlong>(pathData);
63 } else {
64 delete pathData;
Doris Liu0a1a5162016-04-07 15:03:11 -070065 doThrowIAE(env, result.failureMessage.c_str());
Doris Liu804618d2015-11-16 22:48:34 -080066 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
Doris Liucdd23f92015-11-11 14:31:13 -0800102static const JNINativeMethod gMethods[] = {
Doris Liu0a1a5162016-04-07 15:03:11 -0700103 {"nParseStringForPath", "(JLjava/lang/String;I)V", (void*)parseStringForPath},
Doris Liu804618d2015-11-16 22:48:34 -0800104 {"nCreateEmptyPathData", "!()J", (void*)createEmptyPathData},
105 {"nCreatePathData", "!(J)J", (void*)createPathData},
106 {"nCreatePathDataFromString", "(Ljava/lang/String;I)J", (void*)createPathDataFromStringPath},
107 {"nInterpolatePathData", "!(JJJF)Z", (void*)interpolatePathData},
108 {"nFinalize", "!(J)V", (void*)deletePathData},
109 {"nCanMorph", "!(JJ)Z", (void*)canMorphPathData},
110 {"nSetPathData", "!(JJ)V", (void*)setPathData},
111 {"nCreatePathFromPathData", "!(JJ)V", (void*)setSkPathFromPathData},
Doris Liucdd23f92015-11-11 14:31:13 -0800112};
113
114int register_android_util_PathParser(JNIEnv* env) {
115 return RegisterMethodsOrDie(env, "android/util/PathParser", gMethods, NELEM(gMethods));
116}
117};