| 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 "GraphicsJNI.h" |
| 18 | |
| 19 | #include <PathParser.h> |
| 20 | #include <SkPath.h> |
| 21 | #include <utils/VectorDrawableUtils.h> |
| 22 | |
| 23 | #include <android/log.h> |
| 24 | |
| 25 | namespace android { |
| 26 | |
| 27 | using namespace uirenderer; |
| 28 | |
| 29 | static void parseStringForPath(JNIEnv* env, jobject, jlong skPathHandle, jstring inputPathStr, |
| 30 | jint strLength) { |
| 31 | const char* pathString = env->GetStringUTFChars(inputPathStr, NULL); |
| 32 | SkPath* skPath = reinterpret_cast<SkPath*>(skPathHandle); |
| 33 | |
| 34 | PathParser::ParseResult result; |
| 35 | PathParser::parseAsciiStringForSkPath(skPath, &result, pathString, strLength); |
| 36 | env->ReleaseStringUTFChars(inputPathStr, pathString); |
| 37 | if (result.failureOccurred) { |
| 38 | doThrowIAE(env, result.failureMessage.c_str()); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | static jlong createEmptyPathData(JNIEnv*, jobject) { |
| 43 | PathData* pathData = new PathData(); |
| 44 | return reinterpret_cast<jlong>(pathData); |
| 45 | } |
| 46 | |
| 47 | static jlong createPathData(JNIEnv*, jobject, jlong pathDataPtr) { |
| 48 | PathData* pathData = reinterpret_cast<PathData*>(pathDataPtr); |
| 49 | PathData* newPathData = new PathData(*pathData); |
| 50 | return reinterpret_cast<jlong>(newPathData); |
| 51 | } |
| 52 | |
| 53 | static jlong createPathDataFromStringPath(JNIEnv* env, jobject, jstring inputStr, jint strLength) { |
| 54 | const char* pathString = env->GetStringUTFChars(inputStr, NULL); |
| 55 | PathData* pathData = new PathData(); |
| 56 | PathParser::ParseResult result; |
| 57 | PathParser::getPathDataFromAsciiString(pathData, &result, pathString, strLength); |
| 58 | env->ReleaseStringUTFChars(inputStr, pathString); |
| 59 | if (!result.failureOccurred) { |
| 60 | return reinterpret_cast<jlong>(pathData); |
| 61 | } else { |
| 62 | delete pathData; |
| 63 | doThrowIAE(env, result.failureMessage.c_str()); |
| 64 | return 0; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | static jboolean interpolatePathData(JNIEnv*, jobject, jlong outPathDataPtr, jlong fromPathDataPtr, |
| 69 | jlong toPathDataPtr, jfloat fraction) { |
| 70 | PathData* outPathData = reinterpret_cast<PathData*>(outPathDataPtr); |
| 71 | PathData* fromPathData = reinterpret_cast<PathData*>(fromPathDataPtr); |
| 72 | PathData* toPathData = reinterpret_cast<PathData*>(toPathDataPtr); |
| 73 | return VectorDrawableUtils::interpolatePathData(outPathData, *fromPathData, |
| 74 | *toPathData, fraction); |
| 75 | } |
| 76 | |
| 77 | static void deletePathData(JNIEnv*, jobject, jlong pathDataHandle) { |
| 78 | PathData* pathData = reinterpret_cast<PathData*>(pathDataHandle); |
| 79 | delete pathData; |
| 80 | } |
| 81 | |
| 82 | static jboolean canMorphPathData(JNIEnv*, jobject, jlong fromPathDataPtr, jlong toPathDataPtr) { |
| 83 | PathData* fromPathData = reinterpret_cast<PathData*>(fromPathDataPtr); |
| 84 | PathData* toPathData = reinterpret_cast<PathData*>(toPathDataPtr); |
| 85 | return VectorDrawableUtils::canMorph(*fromPathData, *toPathData); |
| 86 | } |
| 87 | |
| 88 | static void setPathData(JNIEnv*, jobject, jlong outPathDataPtr, jlong fromPathDataPtr) { |
| 89 | PathData* fromPathData = reinterpret_cast<PathData*>(fromPathDataPtr); |
| 90 | PathData* outPathData = reinterpret_cast<PathData*>(outPathDataPtr); |
| 91 | *outPathData = *fromPathData; |
| 92 | } |
| 93 | |
| 94 | static void setSkPathFromPathData(JNIEnv*, jobject, jlong outPathPtr, jlong pathDataPtr) { |
| 95 | PathData* pathData = reinterpret_cast<PathData*>(pathDataPtr); |
| 96 | SkPath* skPath = reinterpret_cast<SkPath*>(outPathPtr); |
| 97 | VectorDrawableUtils::verbsToPath(skPath, *pathData); |
| 98 | } |
| 99 | |
| 100 | static const JNINativeMethod gMethods[] = { |
| 101 | {"nParseStringForPath", "(JLjava/lang/String;I)V", (void*)parseStringForPath}, |
| 102 | {"nCreatePathDataFromString", "(Ljava/lang/String;I)J", (void*)createPathDataFromStringPath}, |
| 103 | |
| 104 | // ---------------- @FastNative ----------------- |
| 105 | |
| 106 | {"nCreateEmptyPathData", "()J", (void*)createEmptyPathData}, |
| 107 | {"nCreatePathData", "(J)J", (void*)createPathData}, |
| 108 | {"nInterpolatePathData", "(JJJF)Z", (void*)interpolatePathData}, |
| 109 | {"nFinalize", "(J)V", (void*)deletePathData}, |
| 110 | {"nCanMorph", "(JJ)Z", (void*)canMorphPathData}, |
| 111 | {"nSetPathData", "(JJ)V", (void*)setPathData}, |
| 112 | {"nCreatePathFromPathData", "(JJ)V", (void*)setSkPathFromPathData}, |
| 113 | }; |
| 114 | |
| 115 | int register_android_util_PathParser(JNIEnv* env) { |
| 116 | return RegisterMethodsOrDie(env, "android/util/PathParser", gMethods, NELEM(gMethods)); |
| 117 | } |
| 118 | }; |