android_graphics_drawable_VectorDrawable.cpp revision afbd0f1fef46ef0ddf633dfde0de724db3da1405
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#include "core_jni_helpers.h"
20#include "log/log.h"
21
22#include "Paint.h"
23#include "VectorDrawable.h"
24
25namespace android {
26using namespace uirenderer;
27using namespace uirenderer::VectorDrawable;
28
29static jlong createTree(JNIEnv*, jobject, jlong groupPtr) {
30    VectorDrawable::Group* rootGroup = reinterpret_cast<VectorDrawable::Group*>(groupPtr);
31    VectorDrawable::Tree* tree = new VectorDrawable::Tree(rootGroup);
32    return reinterpret_cast<jlong>(tree);
33}
34
35static void setTreeViewportSize(JNIEnv*, jobject, jlong treePtr,
36        jfloat viewportWidth, jfloat viewportHeight) {
37    VectorDrawable::Tree* tree = reinterpret_cast<VectorDrawable::Tree*>(treePtr);
38    tree->setViewportSize(viewportWidth, viewportHeight);
39}
40
41static jboolean setRootAlpha(JNIEnv*, jobject, jlong treePtr, jfloat alpha) {
42    VectorDrawable::Tree* tree = reinterpret_cast<VectorDrawable::Tree*>(treePtr);
43    return tree->setRootAlpha(alpha);
44}
45
46static jfloat getRootAlpha(JNIEnv*, jobject, jlong treePtr) {
47    VectorDrawable::Tree* tree = reinterpret_cast<VectorDrawable::Tree*>(treePtr);
48    return tree->getRootAlpha();
49}
50
51static void setAllowCaching(JNIEnv*, jobject, jlong treePtr, jboolean allowCaching) {
52    VectorDrawable::Tree* tree = reinterpret_cast<VectorDrawable::Tree*>(treePtr);
53    tree->setAllowCaching(allowCaching);
54}
55
56static void draw(JNIEnv* env, jobject, jlong treePtr, jlong canvasPtr,
57        jlong colorFilterPtr, jobject jrect, jboolean needsMirroring, jboolean canReuseCache) {
58    VectorDrawable::Tree* tree = reinterpret_cast<VectorDrawable::Tree*>(treePtr);
59    Canvas* canvas = reinterpret_cast<Canvas*>(canvasPtr);
60    SkRect rect;
61    GraphicsJNI::jrect_to_rect(env, jrect, &rect);
62    SkColorFilter* colorFilter = reinterpret_cast<SkColorFilter*>(colorFilterPtr);
63    tree->draw(canvas, colorFilter, rect, needsMirroring, canReuseCache);
64}
65
66static jlong createEmptyFullPath(JNIEnv*, jobject) {
67    VectorDrawable::FullPath* newPath = new VectorDrawable::FullPath();
68    return reinterpret_cast<jlong>(newPath);
69}
70
71static jlong createFullPath(JNIEnv*, jobject, jlong srcFullPathPtr) {
72    VectorDrawable::FullPath* srcFullPath =
73            reinterpret_cast<VectorDrawable::FullPath*>(srcFullPathPtr);
74    VectorDrawable::FullPath* newPath = new VectorDrawable::FullPath(*srcFullPath);
75    return reinterpret_cast<jlong>(newPath);
76}
77
78static void updateFullPathPropertiesAndStrokeStyles(JNIEnv*, jobject, jlong fullPathPtr,
79        jfloat strokeWidth, jint strokeColor, jfloat strokeAlpha, jint fillColor, jfloat fillAlpha,
80        jfloat trimPathStart, jfloat trimPathEnd, jfloat trimPathOffset, jfloat strokeMiterLimit,
81        jint strokeLineCap, jint strokeLineJoin, jint fillType) {
82    VectorDrawable::FullPath* fullPath = reinterpret_cast<VectorDrawable::FullPath*>(fullPathPtr);
83    fullPath->updateProperties(strokeWidth, strokeColor, strokeAlpha, fillColor, fillAlpha,
84            trimPathStart, trimPathEnd, trimPathOffset, strokeMiterLimit, strokeLineCap,
85            strokeLineJoin, fillType);
86}
87
88static void updateFullPathFillGradient(JNIEnv*, jobject, jlong pathPtr, jlong fillGradientPtr) {
89    VectorDrawable::FullPath* path = reinterpret_cast<VectorDrawable::FullPath*>(pathPtr);
90    SkShader* fillShader = reinterpret_cast<SkShader*>(fillGradientPtr);
91    path->setFillGradient(fillShader);
92}
93
94static void updateFullPathStrokeGradient(JNIEnv*, jobject, jlong pathPtr, jlong strokeGradientPtr) {
95    VectorDrawable::FullPath* path = reinterpret_cast<VectorDrawable::FullPath*>(pathPtr);
96    SkShader* strokeShader = reinterpret_cast<SkShader*>(strokeGradientPtr);
97    path->setStrokeGradient(strokeShader);
98}
99
100static jboolean getFullPathProperties(JNIEnv* env, jobject, jlong fullPathPtr,
101        jbyteArray outProperties, jint length) {
102    VectorDrawable::FullPath* fullPath = reinterpret_cast<VectorDrawable::FullPath*>(fullPathPtr);
103    int8_t pathProperties[length];
104    bool success = fullPath->getProperties(pathProperties, length);
105    env->SetByteArrayRegion(outProperties, 0, length, reinterpret_cast<int8_t*>(&pathProperties));
106    return success;
107}
108
109static jboolean getGroupProperties(JNIEnv* env, jobject, jlong groupPtr,
110        jfloatArray outProperties, jint length) {
111    VectorDrawable::Group* group = reinterpret_cast<VectorDrawable::Group*>(groupPtr);
112    float groupProperties[length];
113    bool success = group->getProperties(groupProperties, length);
114    env->SetFloatArrayRegion(outProperties, 0, length, reinterpret_cast<float*>(&groupProperties));
115    return success;
116}
117
118static jlong createEmptyClipPath(JNIEnv*, jobject) {
119    VectorDrawable::ClipPath* newPath = new VectorDrawable::ClipPath();
120    return reinterpret_cast<jlong>(newPath);
121}
122
123static jlong createClipPath(JNIEnv*, jobject, jlong srcClipPathPtr) {
124    VectorDrawable::ClipPath* srcClipPath =
125            reinterpret_cast<VectorDrawable::ClipPath*>(srcClipPathPtr);
126    VectorDrawable::ClipPath* newPath = new VectorDrawable::ClipPath(*srcClipPath);
127    return reinterpret_cast<jlong>(newPath);
128}
129
130static jlong createEmptyGroup(JNIEnv*, jobject) {
131    VectorDrawable::Group* newGroup = new VectorDrawable::Group();
132    return reinterpret_cast<jlong>(newGroup);
133}
134
135static jlong createGroup(JNIEnv*, jobject, jlong srcGroupPtr) {
136    VectorDrawable::Group* srcGroup = reinterpret_cast<VectorDrawable::Group*>(srcGroupPtr);
137    VectorDrawable::Group* newGroup = new VectorDrawable::Group(*srcGroup);
138    return reinterpret_cast<jlong>(newGroup);
139}
140
141static void setNodeName(JNIEnv* env, jobject, jlong nodePtr, jstring nameStr) {
142    VectorDrawable::Node* node = reinterpret_cast<VectorDrawable::Node*>(nodePtr);
143    const char* nodeName = env->GetStringUTFChars(nameStr, NULL);
144    node->setName(nodeName);
145    env->ReleaseStringUTFChars(nameStr, nodeName);
146}
147
148static void updateGroupProperties(JNIEnv*, jobject, jlong groupPtr, jfloat rotate, jfloat pivotX,
149        jfloat pivotY, jfloat scaleX, jfloat scaleY, jfloat translateX, jfloat translateY) {
150    VectorDrawable::Group* group = reinterpret_cast<VectorDrawable::Group*>(groupPtr);
151    group->updateLocalMatrix(rotate, pivotX, pivotY, scaleX, scaleY, translateX, translateY);
152}
153
154static void addChild(JNIEnv*, jobject, jlong groupPtr, jlong childPtr) {
155    VectorDrawable::Group* group = reinterpret_cast<VectorDrawable::Group*>(groupPtr);
156    VectorDrawable::Node* child = reinterpret_cast<VectorDrawable::Node*>(childPtr);
157    group->addChild(child);
158}
159
160static void setPathString(JNIEnv* env, jobject, jlong pathPtr, jstring inputStr,
161        jint stringLength) {
162    VectorDrawable::Path* path = reinterpret_cast<VectorDrawable::Path*>(pathPtr);
163    const char* pathString = env->GetStringUTFChars(inputStr, NULL);
164    path->setPath(pathString, stringLength);
165    env->ReleaseStringUTFChars(inputStr, pathString);
166}
167
168static jfloat getRotation(JNIEnv*, jobject, jlong groupPtr) {
169    VectorDrawable::Group* group = reinterpret_cast<VectorDrawable::Group*>(groupPtr);
170    return group->getRotation();
171}
172
173static void setRotation(JNIEnv*, jobject, jlong groupPtr, jfloat rotation) {
174    VectorDrawable::Group* group = reinterpret_cast<VectorDrawable::Group*>(groupPtr);
175    group->setRotation(rotation);
176}
177
178static jfloat getPivotX(JNIEnv*, jobject, jlong groupPtr) {
179    VectorDrawable::Group* group = reinterpret_cast<VectorDrawable::Group*>(groupPtr);
180    return group->getPivotX();
181}
182
183static void setPivotX(JNIEnv*, jobject, jlong groupPtr, jfloat pivotX) {
184    VectorDrawable::Group* group = reinterpret_cast<VectorDrawable::Group*>(groupPtr);
185    group->setPivotX(pivotX);
186}
187
188static jfloat getPivotY(JNIEnv*, jobject, jlong groupPtr) {
189    VectorDrawable::Group* group = reinterpret_cast<VectorDrawable::Group*>(groupPtr);
190    return group->getPivotY();
191}
192
193static void setPivotY(JNIEnv*, jobject, jlong groupPtr, jfloat pivotY) {
194    VectorDrawable::Group* group = reinterpret_cast<VectorDrawable::Group*>(groupPtr);
195    group->setPivotY(pivotY);
196}
197
198static jfloat getScaleX(JNIEnv*, jobject, jlong groupPtr) {
199    VectorDrawable::Group* group = reinterpret_cast<VectorDrawable::Group*>(groupPtr);
200    return group->getScaleX();
201}
202
203static void setScaleX(JNIEnv*, jobject, jlong groupPtr, jfloat scaleX) {
204    VectorDrawable::Group* group = reinterpret_cast<VectorDrawable::Group*>(groupPtr);
205    group->setScaleX(scaleX);
206}
207
208static jfloat getScaleY(JNIEnv*, jobject, jlong groupPtr) {
209    VectorDrawable::Group* group = reinterpret_cast<VectorDrawable::Group*>(groupPtr);
210    return group->getScaleY();
211}
212
213static void setScaleY(JNIEnv*, jobject, jlong groupPtr, jfloat scaleY) {
214    VectorDrawable::Group* group = reinterpret_cast<VectorDrawable::Group*>(groupPtr);
215    group->setScaleY(scaleY);
216}
217
218static jfloat getTranslateX(JNIEnv*, jobject, jlong groupPtr) {
219    VectorDrawable::Group* group = reinterpret_cast<VectorDrawable::Group*>(groupPtr);
220    return group->getTranslateX();
221}
222
223static void setTranslateX(JNIEnv*, jobject, jlong groupPtr, jfloat translateX) {
224    VectorDrawable::Group* group = reinterpret_cast<VectorDrawable::Group*>(groupPtr);
225    group->setTranslateX(translateX);
226}
227
228static jfloat getTranslateY(JNIEnv*, jobject, jlong groupPtr) {
229    VectorDrawable::Group* group = reinterpret_cast<VectorDrawable::Group*>(groupPtr);
230    return group->getTranslateY();
231}
232
233static void setTranslateY(JNIEnv*, jobject, jlong groupPtr, jfloat translateY) {
234    VectorDrawable::Group* group = reinterpret_cast<VectorDrawable::Group*>(groupPtr);
235    group->setTranslateY(translateY);
236}
237
238static void setPathData(JNIEnv*, jobject, jlong pathPtr, jlong pathDataPtr) {
239    VectorDrawable::Path* path = reinterpret_cast<VectorDrawable::Path*>(pathPtr);
240    PathData* pathData = reinterpret_cast<PathData*>(pathDataPtr);
241    path->setPathData(*pathData);
242}
243
244static jfloat getStrokeWidth(JNIEnv*, jobject, jlong fullPathPtr) {
245    VectorDrawable::FullPath* fullPath = reinterpret_cast<VectorDrawable::FullPath*>(fullPathPtr);
246    return fullPath->getStrokeWidth();
247}
248
249static void setStrokeWidth(JNIEnv*, jobject, jlong fullPathPtr, jfloat strokeWidth) {
250    VectorDrawable::FullPath* fullPath = reinterpret_cast<VectorDrawable::FullPath*>(fullPathPtr);
251    fullPath->setStrokeWidth(strokeWidth);
252}
253
254static jint getStrokeColor(JNIEnv*, jobject, jlong fullPathPtr) {
255    VectorDrawable::FullPath* fullPath = reinterpret_cast<VectorDrawable::FullPath*>(fullPathPtr);
256    return fullPath->getStrokeColor();
257}
258
259static void setStrokeColor(JNIEnv*, jobject, jlong fullPathPtr, jint strokeColor) {
260    VectorDrawable::FullPath* fullPath = reinterpret_cast<VectorDrawable::FullPath*>(fullPathPtr);
261    fullPath->setStrokeColor(strokeColor);
262}
263
264static jfloat getStrokeAlpha(JNIEnv*, jobject, jlong fullPathPtr) {
265    VectorDrawable::FullPath* fullPath = reinterpret_cast<VectorDrawable::FullPath*>(fullPathPtr);
266    return fullPath->getStrokeAlpha();
267}
268
269static void setStrokeAlpha(JNIEnv*, jobject, jlong fullPathPtr, jfloat strokeAlpha) {
270    VectorDrawable::FullPath* fullPath = reinterpret_cast<VectorDrawable::FullPath*>(fullPathPtr);
271    fullPath->setStrokeAlpha(strokeAlpha);
272}
273
274static jint getFillColor(JNIEnv*, jobject, jlong fullPathPtr) {
275    VectorDrawable::FullPath* fullPath = reinterpret_cast<VectorDrawable::FullPath*>(fullPathPtr);
276    return fullPath->getFillColor();
277}
278
279static void setFillColor(JNIEnv*, jobject, jlong fullPathPtr, jint fillColor) {
280    VectorDrawable::FullPath* fullPath = reinterpret_cast<VectorDrawable::FullPath*>(fullPathPtr);
281    fullPath->setFillColor(fillColor);
282}
283
284static jfloat getFillAlpha(JNIEnv*, jobject, jlong fullPathPtr) {
285    VectorDrawable::FullPath* fullPath = reinterpret_cast<VectorDrawable::FullPath*>(fullPathPtr);
286    return fullPath->getFillAlpha();
287}
288
289static void setFillAlpha(JNIEnv*, jobject, jlong fullPathPtr, jfloat fillAlpha) {
290    VectorDrawable::FullPath* fullPath = reinterpret_cast<VectorDrawable::FullPath*>(fullPathPtr);
291    fullPath->setFillAlpha(fillAlpha);
292}
293
294static jfloat getTrimPathStart(JNIEnv*, jobject, jlong fullPathPtr) {
295    VectorDrawable::FullPath* fullPath = reinterpret_cast<VectorDrawable::FullPath*>(fullPathPtr);
296    return fullPath->getTrimPathStart();
297}
298
299static void setTrimPathStart(JNIEnv*, jobject, jlong fullPathPtr, jfloat trimPathStart) {
300    VectorDrawable::FullPath* fullPath = reinterpret_cast<VectorDrawable::FullPath*>(fullPathPtr);
301    fullPath->setTrimPathStart(trimPathStart);
302}
303
304static jfloat getTrimPathEnd(JNIEnv*, jobject, jlong fullPathPtr) {
305    VectorDrawable::FullPath* fullPath = reinterpret_cast<VectorDrawable::FullPath*>(fullPathPtr);
306    return fullPath->getTrimPathEnd();
307}
308
309static void setTrimPathEnd(JNIEnv*, jobject, jlong fullPathPtr, jfloat trimPathEnd) {
310    VectorDrawable::FullPath* fullPath = reinterpret_cast<VectorDrawable::FullPath*>(fullPathPtr);
311    fullPath->setTrimPathEnd(trimPathEnd);
312}
313
314static jfloat getTrimPathOffset(JNIEnv*, jobject, jlong fullPathPtr) {
315    VectorDrawable::FullPath* fullPath = reinterpret_cast<VectorDrawable::FullPath*>(fullPathPtr);
316    return fullPath->getTrimPathOffset();
317}
318
319static void setTrimPathOffset(JNIEnv*, jobject, jlong fullPathPtr, jfloat trimPathOffset) {
320    VectorDrawable::FullPath* fullPath = reinterpret_cast<VectorDrawable::FullPath*>(fullPathPtr);
321    fullPath->setTrimPathOffset(trimPathOffset);
322}
323
324static const JNINativeMethod gMethods[] = {
325        {"nCreateRenderer", "!(J)J", (void*)createTree},
326        {"nSetRendererViewportSize", "!(JFF)V", (void*)setTreeViewportSize},
327        {"nSetRootAlpha", "!(JF)Z", (void*)setRootAlpha},
328        {"nGetRootAlpha", "!(J)F", (void*)getRootAlpha},
329        {"nSetAllowCaching", "!(JZ)V", (void*)setAllowCaching},
330
331        {"nDraw", "(JJJLandroid/graphics/Rect;ZZ)V", (void*)draw},
332        {"nCreateFullPath", "!()J", (void*)createEmptyFullPath},
333        {"nCreateFullPath", "!(J)J", (void*)createFullPath},
334        {"nUpdateFullPathProperties", "!(JFIFIFFFFFIII)V", (void*)updateFullPathPropertiesAndStrokeStyles},
335        {"nUpdateFullPathFillGradient", "!(JJ)V", (void*)updateFullPathFillGradient},
336        {"nUpdateFullPathStrokeGradient", "!(JJ)V", (void*)updateFullPathStrokeGradient},
337        {"nGetFullPathProperties", "(J[BI)Z", (void*)getFullPathProperties},
338        {"nGetGroupProperties", "(J[FI)Z", (void*)getGroupProperties},
339
340        {"nCreateClipPath", "!()J", (void*)createEmptyClipPath},
341        {"nCreateClipPath", "!(J)J", (void*)createClipPath},
342        {"nCreateGroup", "!()J", (void*)createEmptyGroup},
343        {"nCreateGroup", "!(J)J", (void*)createGroup},
344        {"nSetName", "(JLjava/lang/String;)V", (void*)setNodeName},
345        {"nUpdateGroupProperties", "!(JFFFFFFF)V", (void*)updateGroupProperties},
346
347        {"nAddChild", "!(JJ)V", (void*)addChild},
348        {"nSetPathString", "(JLjava/lang/String;I)V", (void*)setPathString},
349
350        {"nGetRotation", "!(J)F", (void*)getRotation},
351        {"nSetRotation", "!(JF)V", (void*)setRotation},
352        {"nGetPivotX", "!(J)F", (void*)getPivotX},
353        {"nSetPivotX", "!(JF)V", (void*)setPivotX},
354        {"nGetPivotY", "!(J)F", (void*)getPivotY},
355        {"nSetPivotY", "!(JF)V", (void*)setPivotY},
356        {"nGetScaleX", "!(J)F", (void*)getScaleX},
357        {"nSetScaleX", "!(JF)V", (void*)setScaleX},
358        {"nGetScaleY", "!(J)F", (void*)getScaleY},
359        {"nSetScaleY", "!(JF)V", (void*)setScaleY},
360        {"nGetTranslateX", "!(J)F", (void*)getTranslateX},
361        {"nSetTranslateX", "!(JF)V", (void*)setTranslateX},
362        {"nGetTranslateY", "!(J)F", (void*)getTranslateY},
363        {"nSetTranslateY", "!(JF)V", (void*)setTranslateY},
364
365        {"nSetPathData", "!(JJ)V", (void*)setPathData},
366        {"nGetStrokeWidth", "!(J)F", (void*)getStrokeWidth},
367        {"nSetStrokeWidth", "!(JF)V", (void*)setStrokeWidth},
368        {"nGetStrokeColor", "!(J)I", (void*)getStrokeColor},
369        {"nSetStrokeColor", "!(JI)V", (void*)setStrokeColor},
370        {"nGetStrokeAlpha", "!(J)F", (void*)getStrokeAlpha},
371        {"nSetStrokeAlpha", "!(JF)V", (void*)setStrokeAlpha},
372        {"nGetFillColor", "!(J)I", (void*)getFillColor},
373        {"nSetFillColor", "!(JI)V", (void*)setFillColor},
374        {"nGetFillAlpha", "!(J)F", (void*)getFillAlpha},
375        {"nSetFillAlpha", "!(JF)V", (void*)setFillAlpha},
376        {"nGetTrimPathStart", "!(J)F", (void*)getTrimPathStart},
377        {"nSetTrimPathStart", "!(JF)V", (void*)setTrimPathStart},
378        {"nGetTrimPathEnd", "!(J)F", (void*)getTrimPathEnd},
379        {"nSetTrimPathEnd", "!(JF)V", (void*)setTrimPathEnd},
380        {"nGetTrimPathOffset", "!(J)F", (void*)getTrimPathOffset},
381        {"nSetTrimPathOffset", "!(JF)V", (void*)setTrimPathOffset},
382};
383
384int register_android_graphics_drawable_VectorDrawable(JNIEnv* env) {
385    return RegisterMethodsOrDie(env, "android/graphics/drawable/VectorDrawable", gMethods, NELEM(gMethods));
386}
387
388}; // namespace android
389