Doris Liu | cdd23f9 | 2015-11-11 14:31:13 -0800 | [diff] [blame] | 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" |
Doris Liu | 0a1a516 | 2016-04-07 15:03:11 -0700 | [diff] [blame] | 18 | #include "GraphicsJNI.h" |
Doris Liu | cdd23f9 | 2015-11-11 14:31:13 -0800 | [diff] [blame] | 19 | |
| 20 | #include <PathParser.h> |
| 21 | #include <SkPath.h> |
Doris Liu | 804618d | 2015-11-16 22:48:34 -0800 | [diff] [blame] | 22 | #include <utils/VectorDrawableUtils.h> |
Doris Liu | cdd23f9 | 2015-11-11 14:31:13 -0800 | [diff] [blame] | 23 | |
Doris Liu | 1e67f08 | 2015-11-12 15:57:45 -0800 | [diff] [blame] | 24 | #include <android/log.h> |
Doris Liu | cdd23f9 | 2015-11-11 14:31:13 -0800 | [diff] [blame] | 25 | #include "core_jni_helpers.h" |
| 26 | |
| 27 | namespace android { |
| 28 | |
Doris Liu | 804618d | 2015-11-16 22:48:34 -0800 | [diff] [blame] | 29 | using namespace uirenderer; |
| 30 | |
Doris Liu | 0a1a516 | 2016-04-07 15:03:11 -0700 | [diff] [blame] | 31 | static void parseStringForPath(JNIEnv* env, jobject, jlong skPathHandle, jstring inputPathStr, |
Doris Liu | cdd23f9 | 2015-11-11 14:31:13 -0800 | [diff] [blame] | 32 | jint strLength) { |
| 33 | const char* pathString = env->GetStringUTFChars(inputPathStr, NULL); |
| 34 | SkPath* skPath = reinterpret_cast<SkPath*>(skPathHandle); |
Doris Liu | 1e67f08 | 2015-11-12 15:57:45 -0800 | [diff] [blame] | 35 | |
Doris Liu | 804618d | 2015-11-16 22:48:34 -0800 | [diff] [blame] | 36 | PathParser::ParseResult result; |
Doris Liu | b35da39 | 2016-04-12 11:06:23 -0700 | [diff] [blame] | 37 | PathParser::parseAsciiStringForSkPath(skPath, &result, pathString, strLength); |
Doris Liu | cdd23f9 | 2015-11-11 14:31:13 -0800 | [diff] [blame] | 38 | env->ReleaseStringUTFChars(inputPathStr, pathString); |
Doris Liu | 1e67f08 | 2015-11-12 15:57:45 -0800 | [diff] [blame] | 39 | if (result.failureOccurred) { |
Doris Liu | 0a1a516 | 2016-04-07 15:03:11 -0700 | [diff] [blame] | 40 | doThrowIAE(env, result.failureMessage.c_str()); |
Doris Liu | 1e67f08 | 2015-11-12 15:57:45 -0800 | [diff] [blame] | 41 | } |
Doris Liu | cdd23f9 | 2015-11-11 14:31:13 -0800 | [diff] [blame] | 42 | } |
| 43 | |
Doris Liu | 804618d | 2015-11-16 22:48:34 -0800 | [diff] [blame] | 44 | static long createEmptyPathData(JNIEnv*, jobject) { |
| 45 | PathData* pathData = new PathData(); |
| 46 | return reinterpret_cast<jlong>(pathData); |
| 47 | } |
| 48 | |
| 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); |
| 53 | } |
| 54 | |
| 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; |
Doris Liu | b35da39 | 2016-04-12 11:06:23 -0700 | [diff] [blame] | 59 | PathParser::getPathDataFromAsciiString(pathData, &result, pathString, strLength); |
Doris Liu | 804618d | 2015-11-16 22:48:34 -0800 | [diff] [blame] | 60 | env->ReleaseStringUTFChars(inputStr, pathString); |
| 61 | if (!result.failureOccurred) { |
| 62 | return reinterpret_cast<jlong>(pathData); |
| 63 | } else { |
| 64 | delete pathData; |
Doris Liu | 0a1a516 | 2016-04-07 15:03:11 -0700 | [diff] [blame] | 65 | doThrowIAE(env, result.failureMessage.c_str()); |
Doris Liu | 804618d | 2015-11-16 22:48:34 -0800 | [diff] [blame] | 66 | return NULL; |
| 67 | } |
| 68 | } |
| 69 | |
| 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); |
| 77 | } |
| 78 | |
| 79 | static void deletePathData(JNIEnv*, jobject, jlong pathDataHandle) { |
| 80 | PathData* pathData = reinterpret_cast<PathData*>(pathDataHandle); |
| 81 | delete pathData; |
| 82 | } |
| 83 | |
| 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); |
| 88 | } |
| 89 | |
| 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; |
| 94 | } |
| 95 | |
| 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); |
| 100 | } |
| 101 | |
Doris Liu | cdd23f9 | 2015-11-11 14:31:13 -0800 | [diff] [blame] | 102 | static const JNINativeMethod gMethods[] = { |
Doris Liu | 0a1a516 | 2016-04-07 15:03:11 -0700 | [diff] [blame] | 103 | {"nParseStringForPath", "(JLjava/lang/String;I)V", (void*)parseStringForPath}, |
Doris Liu | 804618d | 2015-11-16 22:48:34 -0800 | [diff] [blame] | 104 | {"nCreatePathDataFromString", "(Ljava/lang/String;I)J", (void*)createPathDataFromStringPath}, |
John Reck | 3299522 | 2016-10-07 11:02:20 -0700 | [diff] [blame] | 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}, |
Doris Liu | cdd23f9 | 2015-11-11 14:31:13 -0800 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | int register_android_util_PathParser(JNIEnv* env) { |
| 118 | return RegisterMethodsOrDie(env, "android/util/PathParser", gMethods, NELEM(gMethods)); |
| 119 | } |
| 120 | }; |