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