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