blob: 729a4dd4ba767079426680953bc62a7fddbcfbc2 [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#ifndef ANDROID_HWUI_VPATH_H
18#define ANDROID_HWUI_VPATH_H
19
sergeyvdccca442016-03-21 15:38:21 -070020#include "hwui/Canvas.h"
sergeyvfc9999502016-10-17 13:07:38 -070021#include "hwui/Bitmap.h"
Doris Liu1d8e1942016-03-02 15:16:28 -080022#include "DisplayList.h"
Doris Liu766431a2016-02-04 22:17:11 +000023
Doris Liu4bbc2932015-12-01 17:59:40 -080024#include <SkBitmap.h>
25#include <SkColor.h>
Doris Liu1d8e1942016-03-02 15:16:28 -080026#include <SkColorFilter.h>
Doris Liuc2de46f2016-01-21 12:55:54 -080027#include <SkCanvas.h>
Doris Liu4bbc2932015-12-01 17:59:40 -080028#include <SkMatrix.h>
29#include <SkPaint.h>
30#include <SkPath.h>
31#include <SkPathMeasure.h>
32#include <SkRect.h>
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -080033#include <SkShader.h>
Doris Liu4bbc2932015-12-01 17:59:40 -080034
35#include <cutils/compiler.h>
36#include <stddef.h>
37#include <vector>
38#include <string>
39
40namespace android {
41namespace uirenderer {
42
Teng-Hui Zhu85d99522016-04-25 14:23:40 -070043// Debug
44#if DEBUG_VECTOR_DRAWABLE
45 #define VECTOR_DRAWABLE_LOGD(...) ALOGD(__VA_ARGS__)
46#else
47 #define VECTOR_DRAWABLE_LOGD(...)
48#endif
49
Doris Liu4bbc2932015-12-01 17:59:40 -080050namespace VectorDrawable {
Doris Liu32d7cda2016-04-08 13:48:47 -070051#define VD_SET_PRIMITIVE_FIELD_WITH_FLAG(field, value, flag) (VD_SET_PRIMITIVE_FIELD_AND_NOTIFY(field, (value)) ? ((flag) = true, true) : false)
52#define VD_SET_PROP(field, value) ((value) != (field) ? ((field) = (value), true) : false)
53#define VD_SET_PRIMITIVE_FIELD_AND_NOTIFY(field, value) ({ bool retVal = VD_SET_PROP((mPrimitiveFields.field), (value));\
Doris Liu1d8e1942016-03-02 15:16:28 -080054 onPropertyChanged(); retVal;})
Doris Liu32d7cda2016-04-08 13:48:47 -070055#define UPDATE_SKPROP(field, value) ({bool retVal = ((field) != (value)); if ((field) != (value)) SkRefCnt_SafeAssign((field), (value)); retVal;})
Doris Liu4bbc2932015-12-01 17:59:40 -080056
57/* A VectorDrawable is composed of a tree of nodes.
58 * Each node can be a group node, or a path.
59 * A group node can have groups or paths as children, but a path node has
60 * no children.
61 * One example can be:
62 * Root Group
63 * / | \
64 * Group Path Group
65 * / \ |
66 * Path Path Path
67 *
Doris Liu1d8e1942016-03-02 15:16:28 -080068 * VectorDrawables are drawn into bitmap caches first, then the caches are drawn to the given
69 * canvas with root alpha applied. Two caches are maintained for VD, one in UI thread, the other in
70 * Render Thread. A generation id is used to keep track of changes in the vector drawable tree.
71 * Each cache has their own generation id to track whether they are up to date with the latest
72 * change in the tree.
73 *
74 * Any property change to the vector drawable coming from UI thread (such as bulk setters to update
75 * all the properties, and viewport change, etc.) are only modifying the staging properties. The
76 * staging properties will then be marked dirty and will be pushed over to render thread properties
77 * at sync point. If staging properties are not dirty at sync point, we sync backwards by updating
78 * staging properties with render thread properties to reflect the latest animation value.
79 *
Doris Liu4bbc2932015-12-01 17:59:40 -080080 */
Doris Liu1d8e1942016-03-02 15:16:28 -080081
82class PropertyChangedListener {
83public:
84 PropertyChangedListener(bool* dirty, bool* stagingDirty)
85 : mDirty(dirty), mStagingDirty(stagingDirty) {}
86 void onPropertyChanged() {
87 *mDirty = true;
88 }
89 void onStagingPropertyChanged() {
90 *mStagingDirty = true;
91 }
92private:
93 bool* mDirty;
94 bool* mStagingDirty;
95};
96
Doris Liu4bbc2932015-12-01 17:59:40 -080097class ANDROID_API Node {
98public:
Doris Liu1d8e1942016-03-02 15:16:28 -080099 class Properties {
100 public:
Chih-Hung Hsieha619ec72016-08-29 14:52:43 -0700101 explicit Properties(Node* node) : mNode(node) {}
Doris Liu1d8e1942016-03-02 15:16:28 -0800102 inline void onPropertyChanged() {
103 mNode->onPropertyChanged(this);
104 }
105 private:
106 Node* mNode;
107 };
Doris Liu4bbc2932015-12-01 17:59:40 -0800108 Node(const Node& node) {
109 mName = node.mName;
110 }
111 Node() {}
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400112 virtual void draw(SkCanvas* outCanvas, bool useStagingData) = 0;
Doris Liu4bbc2932015-12-01 17:59:40 -0800113 virtual void dump() = 0;
114 void setName(const char* name) {
115 mName = name;
116 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800117 virtual void setPropertyChangedListener(PropertyChangedListener* listener) {
118 mPropertyChangedListener = listener;
119 }
120 virtual void onPropertyChanged(Properties* properties) = 0;
Doris Liu4bbc2932015-12-01 17:59:40 -0800121 virtual ~Node(){}
Doris Liu1d8e1942016-03-02 15:16:28 -0800122 virtual void syncProperties() = 0;
Doris Liu4bbc2932015-12-01 17:59:40 -0800123protected:
124 std::string mName;
Doris Liu1d8e1942016-03-02 15:16:28 -0800125 PropertyChangedListener* mPropertyChangedListener = nullptr;
Doris Liu4bbc2932015-12-01 17:59:40 -0800126};
127
128class ANDROID_API Path : public Node {
129public:
130 struct ANDROID_API Data {
131 std::vector<char> verbs;
132 std::vector<size_t> verbSizes;
133 std::vector<float> points;
134 bool operator==(const Data& data) const {
135 return verbs == data.verbs && verbSizes == data.verbSizes
136 && points == data.points;
137 }
138 };
Doris Liu1d8e1942016-03-02 15:16:28 -0800139
140 class PathProperties : public Properties {
141 public:
Chih-Hung Hsieha619ec72016-08-29 14:52:43 -0700142 explicit PathProperties(Node* node) : Properties(node) {}
Doris Liu1d8e1942016-03-02 15:16:28 -0800143 void syncProperties(const PathProperties& prop) {
144 mData = prop.mData;
145 onPropertyChanged();
146 }
147 void setData(const Data& data) {
148 // Updates the path data. Note that we don't generate a new Skia path right away
149 // because there are cases where the animation is changing the path data, but the view
150 // that hosts the VD has gone off screen, in which case we won't even draw. So we
151 // postpone the Skia path generation to the draw time.
152 if (data == mData) {
153 return;
154 }
155 mData = data;
156 onPropertyChanged();
157
158 }
159 const Data& getData() const {
160 return mData;
161 }
162 private:
163 Data mData;
164 };
165
Doris Liu4bbc2932015-12-01 17:59:40 -0800166 Path(const Path& path);
167 Path(const char* path, size_t strLength);
168 Path() {}
Doris Liu1d8e1942016-03-02 15:16:28 -0800169
Doris Liu4bbc2932015-12-01 17:59:40 -0800170 void dump() override;
Doris Liu1d8e1942016-03-02 15:16:28 -0800171 virtual void syncProperties() override;
172 virtual void onPropertyChanged(Properties* prop) override {
173 if (prop == &mStagingProperties) {
174 mStagingPropertiesDirty = true;
175 if (mPropertyChangedListener) {
176 mPropertyChangedListener->onStagingPropertyChanged();
177 }
178 } else if (prop == &mProperties){
179 mSkPathDirty = true;
180 if (mPropertyChangedListener) {
181 mPropertyChangedListener->onPropertyChanged();
182 }
183 }
184 }
185 PathProperties* mutateStagingProperties() { return &mStagingProperties; }
186 const PathProperties* stagingProperties() { return &mStagingProperties; }
187
188 // This should only be called from animations on RT
189 PathProperties* mutateProperties() { return &mProperties; }
Doris Liu4bbc2932015-12-01 17:59:40 -0800190
191protected:
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400192 virtual const SkPath& getUpdatedPath(bool useStagingData, SkPath* tempStagingPath);
Doris Liu1d8e1942016-03-02 15:16:28 -0800193
194 // Internal data, render thread only.
Doris Liu4bbc2932015-12-01 17:59:40 -0800195 bool mSkPathDirty = true;
Doris Liu1d8e1942016-03-02 15:16:28 -0800196 SkPath mSkPath;
197
198private:
199 PathProperties mProperties = PathProperties(this);
200 PathProperties mStagingProperties = PathProperties(this);
201 bool mStagingPropertiesDirty = true;
Doris Liu4bbc2932015-12-01 17:59:40 -0800202};
203
204class ANDROID_API FullPath: public Path {
205public:
Doris Liu1d8e1942016-03-02 15:16:28 -0800206 class FullPathProperties : public Properties {
207 public:
208 struct PrimitiveFields {
209 float strokeWidth = 0;
210 SkColor strokeColor = SK_ColorTRANSPARENT;
211 float strokeAlpha = 1;
212 SkColor fillColor = SK_ColorTRANSPARENT;
213 float fillAlpha = 1;
214 float trimPathStart = 0;
215 float trimPathEnd = 1;
216 float trimPathOffset = 0;
217 int32_t strokeLineCap = SkPaint::Cap::kButt_Cap;
218 int32_t strokeLineJoin = SkPaint::Join::kMiter_Join;
219 float strokeMiterLimit = 4;
220 int fillType = 0; /* non-zero or kWinding_FillType in Skia */
221 };
Chih-Hung Hsieha619ec72016-08-29 14:52:43 -0700222 explicit FullPathProperties(Node* mNode) : Properties(mNode), mTrimDirty(false) {}
Doris Liuad21fe22016-04-14 18:13:36 -0700223 ~FullPathProperties() {
224 SkSafeUnref(fillGradient);
225 SkSafeUnref(strokeGradient);
226 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800227 void syncProperties(const FullPathProperties& prop) {
228 mPrimitiveFields = prop.mPrimitiveFields;
229 mTrimDirty = true;
Doris Liuad21fe22016-04-14 18:13:36 -0700230 UPDATE_SKPROP(fillGradient, prop.fillGradient);
231 UPDATE_SKPROP(strokeGradient, prop.strokeGradient);
Doris Liu1d8e1942016-03-02 15:16:28 -0800232 onPropertyChanged();
233 }
234 void setFillGradient(SkShader* gradient) {
Doris Liuad21fe22016-04-14 18:13:36 -0700235 if(UPDATE_SKPROP(fillGradient, gradient)) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800236 onPropertyChanged();
237 }
238 }
239 void setStrokeGradient(SkShader* gradient) {
Doris Liuad21fe22016-04-14 18:13:36 -0700240 if(UPDATE_SKPROP(strokeGradient, gradient)) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800241 onPropertyChanged();
242 }
243 }
244 SkShader* getFillGradient() const {
245 return fillGradient;
246 }
247 SkShader* getStrokeGradient() const {
248 return strokeGradient;
249 }
250 float getStrokeWidth() const{
251 return mPrimitiveFields.strokeWidth;
252 }
253 void setStrokeWidth(float strokeWidth) {
Doris Liu32d7cda2016-04-08 13:48:47 -0700254 VD_SET_PRIMITIVE_FIELD_AND_NOTIFY(strokeWidth, strokeWidth);
Doris Liu1d8e1942016-03-02 15:16:28 -0800255 }
256 SkColor getStrokeColor() const{
257 return mPrimitiveFields.strokeColor;
258 }
259 void setStrokeColor(SkColor strokeColor) {
Doris Liu32d7cda2016-04-08 13:48:47 -0700260 VD_SET_PRIMITIVE_FIELD_AND_NOTIFY(strokeColor, strokeColor);
Doris Liu1d8e1942016-03-02 15:16:28 -0800261 }
262 float getStrokeAlpha() const{
263 return mPrimitiveFields.strokeAlpha;
264 }
265 void setStrokeAlpha(float strokeAlpha) {
Doris Liu32d7cda2016-04-08 13:48:47 -0700266 VD_SET_PRIMITIVE_FIELD_AND_NOTIFY(strokeAlpha, strokeAlpha);
Doris Liu1d8e1942016-03-02 15:16:28 -0800267 }
268 SkColor getFillColor() const {
269 return mPrimitiveFields.fillColor;
270 }
271 void setFillColor(SkColor fillColor) {
Doris Liu32d7cda2016-04-08 13:48:47 -0700272 VD_SET_PRIMITIVE_FIELD_AND_NOTIFY(fillColor, fillColor);
Doris Liu1d8e1942016-03-02 15:16:28 -0800273 }
274 float getFillAlpha() const{
275 return mPrimitiveFields.fillAlpha;
276 }
277 void setFillAlpha(float fillAlpha) {
Doris Liu32d7cda2016-04-08 13:48:47 -0700278 VD_SET_PRIMITIVE_FIELD_AND_NOTIFY(fillAlpha, fillAlpha);
Doris Liu1d8e1942016-03-02 15:16:28 -0800279 }
280 float getTrimPathStart() const{
281 return mPrimitiveFields.trimPathStart;
282 }
283 void setTrimPathStart(float trimPathStart) {
Doris Liu32d7cda2016-04-08 13:48:47 -0700284 VD_SET_PRIMITIVE_FIELD_WITH_FLAG(trimPathStart, trimPathStart, mTrimDirty);
Doris Liu1d8e1942016-03-02 15:16:28 -0800285 }
286 float getTrimPathEnd() const{
287 return mPrimitiveFields.trimPathEnd;
288 }
289 void setTrimPathEnd(float trimPathEnd) {
Doris Liu32d7cda2016-04-08 13:48:47 -0700290 VD_SET_PRIMITIVE_FIELD_WITH_FLAG(trimPathEnd, trimPathEnd, mTrimDirty);
Doris Liu1d8e1942016-03-02 15:16:28 -0800291 }
292 float getTrimPathOffset() const{
293 return mPrimitiveFields.trimPathOffset;
294 }
295 void setTrimPathOffset(float trimPathOffset) {
Doris Liu32d7cda2016-04-08 13:48:47 -0700296 VD_SET_PRIMITIVE_FIELD_WITH_FLAG(trimPathOffset, trimPathOffset, mTrimDirty);
Doris Liu1d8e1942016-03-02 15:16:28 -0800297 }
Doris Liu766431a2016-02-04 22:17:11 +0000298
Doris Liu1d8e1942016-03-02 15:16:28 -0800299 float getStrokeMiterLimit() const {
300 return mPrimitiveFields.strokeMiterLimit;
301 }
302 float getStrokeLineCap() const {
303 return mPrimitiveFields.strokeLineCap;
304 }
305 float getStrokeLineJoin() const {
306 return mPrimitiveFields.strokeLineJoin;
307 }
308 float getFillType() const {
309 return mPrimitiveFields.fillType;
310 }
311 bool copyProperties(int8_t* outProperties, int length) const;
312 void updateProperties(float strokeWidth, SkColor strokeColor, float strokeAlpha,
313 SkColor fillColor, float fillAlpha, float trimPathStart, float trimPathEnd,
314 float trimPathOffset, float strokeMiterLimit, int strokeLineCap, int strokeLineJoin,
315 int fillType) {
316 mPrimitiveFields.strokeWidth = strokeWidth;
317 mPrimitiveFields.strokeColor = strokeColor;
318 mPrimitiveFields.strokeAlpha = strokeAlpha;
319 mPrimitiveFields.fillColor = fillColor;
320 mPrimitiveFields.fillAlpha = fillAlpha;
321 mPrimitiveFields.trimPathStart = trimPathStart;
322 mPrimitiveFields.trimPathEnd = trimPathEnd;
323 mPrimitiveFields.trimPathOffset = trimPathOffset;
324 mPrimitiveFields.strokeMiterLimit = strokeMiterLimit;
325 mPrimitiveFields.strokeLineCap = strokeLineCap;
326 mPrimitiveFields.strokeLineJoin = strokeLineJoin;
327 mPrimitiveFields.fillType = fillType;
328 mTrimDirty = true;
329 onPropertyChanged();
330 }
331 // Set property values during animation
332 void setColorPropertyValue(int propertyId, int32_t value);
333 void setPropertyValue(int propertyId, float value);
334 bool mTrimDirty;
335 private:
336 enum class Property {
337 strokeWidth = 0,
338 strokeColor,
339 strokeAlpha,
340 fillColor,
341 fillAlpha,
342 trimPathStart,
343 trimPathEnd,
344 trimPathOffset,
345 strokeLineCap,
346 strokeLineJoin,
347 strokeMiterLimit,
348 fillType,
349 count,
350 };
351 PrimitiveFields mPrimitiveFields;
Doris Liuad21fe22016-04-14 18:13:36 -0700352 SkShader* fillGradient = nullptr;
353 SkShader* strokeGradient = nullptr;
Doris Liu1d8e1942016-03-02 15:16:28 -0800354 };
Doris Liu766431a2016-02-04 22:17:11 +0000355
Doris Liu1d8e1942016-03-02 15:16:28 -0800356 // Called from UI thread
Doris Liu4bbc2932015-12-01 17:59:40 -0800357 FullPath(const FullPath& path); // for cloning
358 FullPath(const char* path, size_t strLength) : Path(path, strLength) {}
359 FullPath() : Path() {}
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400360 void draw(SkCanvas* outCanvas, bool useStagingData) override;
Doris Liu1d8e1942016-03-02 15:16:28 -0800361 void dump() override;
362 FullPathProperties* mutateStagingProperties() { return &mStagingProperties; }
363 const FullPathProperties* stagingProperties() { return &mStagingProperties; }
Doris Liu4bbc2932015-12-01 17:59:40 -0800364
Doris Liu1d8e1942016-03-02 15:16:28 -0800365 // This should only be called from animations on RT
366 FullPathProperties* mutateProperties() { return &mProperties; }
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800367
Doris Liu1d8e1942016-03-02 15:16:28 -0800368 virtual void syncProperties() override;
369 virtual void onPropertyChanged(Properties* properties) override {
370 Path::onPropertyChanged(properties);
371 if (properties == &mStagingProperties) {
372 mStagingPropertiesDirty = true;
373 if (mPropertyChangedListener) {
374 mPropertyChangedListener->onStagingPropertyChanged();
375 }
376 } else if (properties == &mProperties) {
377 if (mPropertyChangedListener) {
378 mPropertyChangedListener->onPropertyChanged();
379 }
380 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800381 }
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800382
Doris Liu4bbc2932015-12-01 17:59:40 -0800383protected:
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400384 const SkPath& getUpdatedPath(bool useStagingData, SkPath* tempStagingPath) override;
Doris Liu4bbc2932015-12-01 17:59:40 -0800385private:
Doris Liu1d8e1942016-03-02 15:16:28 -0800386
387 FullPathProperties mProperties = FullPathProperties(this);
388 FullPathProperties mStagingProperties = FullPathProperties(this);
389 bool mStagingPropertiesDirty = true;
390
391 // Intermediate data for drawing, render thread only
Doris Liu5a11e8d2016-02-04 20:04:10 +0000392 SkPath mTrimmedSkPath;
Doris Liu1d8e1942016-03-02 15:16:28 -0800393
Doris Liu4bbc2932015-12-01 17:59:40 -0800394};
395
396class ANDROID_API ClipPath: public Path {
397public:
398 ClipPath(const ClipPath& path) : Path(path) {}
399 ClipPath(const char* path, size_t strLength) : Path(path, strLength) {}
400 ClipPath() : Path() {}
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400401 void draw(SkCanvas* outCanvas, bool useStagingData) override;
Doris Liu4bbc2932015-12-01 17:59:40 -0800402};
403
404class ANDROID_API Group: public Node {
405public:
Doris Liu1d8e1942016-03-02 15:16:28 -0800406 class GroupProperties : public Properties {
407 public:
Chih-Hung Hsieha619ec72016-08-29 14:52:43 -0700408 explicit GroupProperties(Node* mNode) : Properties(mNode) {}
Doris Liu1d8e1942016-03-02 15:16:28 -0800409 struct PrimitiveFields {
410 float rotate = 0;
411 float pivotX = 0;
412 float pivotY = 0;
413 float scaleX = 1;
414 float scaleY = 1;
415 float translateX = 0;
416 float translateY = 0;
417 } mPrimitiveFields;
418 void syncProperties(const GroupProperties& prop) {
419 mPrimitiveFields = prop.mPrimitiveFields;
420 onPropertyChanged();
421 }
422 float getRotation() const {
423 return mPrimitiveFields.rotate;
424 }
425 void setRotation(float rotation) {
Doris Liu32d7cda2016-04-08 13:48:47 -0700426 VD_SET_PRIMITIVE_FIELD_AND_NOTIFY(rotate, rotation);
Doris Liu1d8e1942016-03-02 15:16:28 -0800427 }
428 float getPivotX() const {
429 return mPrimitiveFields.pivotX;
430 }
431 void setPivotX(float pivotX) {
Doris Liu32d7cda2016-04-08 13:48:47 -0700432 VD_SET_PRIMITIVE_FIELD_AND_NOTIFY(pivotX, pivotX);
Doris Liu1d8e1942016-03-02 15:16:28 -0800433 }
434 float getPivotY() const {
435 return mPrimitiveFields.pivotY;
436 }
437 void setPivotY(float pivotY) {
Doris Liu32d7cda2016-04-08 13:48:47 -0700438 VD_SET_PRIMITIVE_FIELD_AND_NOTIFY(pivotY, pivotY);
Doris Liu1d8e1942016-03-02 15:16:28 -0800439 }
440 float getScaleX() const {
441 return mPrimitiveFields.scaleX;
442 }
443 void setScaleX(float scaleX) {
Doris Liu32d7cda2016-04-08 13:48:47 -0700444 VD_SET_PRIMITIVE_FIELD_AND_NOTIFY(scaleX, scaleX);
Doris Liu1d8e1942016-03-02 15:16:28 -0800445 }
446 float getScaleY() const {
447 return mPrimitiveFields.scaleY;
448 }
449 void setScaleY(float scaleY) {
Doris Liu32d7cda2016-04-08 13:48:47 -0700450 VD_SET_PRIMITIVE_FIELD_AND_NOTIFY(scaleY, scaleY);
Doris Liu1d8e1942016-03-02 15:16:28 -0800451 }
452 float getTranslateX() const {
453 return mPrimitiveFields.translateX;
454 }
455 void setTranslateX(float translateX) {
Doris Liu32d7cda2016-04-08 13:48:47 -0700456 VD_SET_PRIMITIVE_FIELD_AND_NOTIFY(translateX, translateX);
Doris Liu1d8e1942016-03-02 15:16:28 -0800457 }
458 float getTranslateY() const {
459 return mPrimitiveFields.translateY;
460 }
461 void setTranslateY(float translateY) {
Doris Liu32d7cda2016-04-08 13:48:47 -0700462 VD_SET_PRIMITIVE_FIELD_AND_NOTIFY(translateY, translateY);
Doris Liu1d8e1942016-03-02 15:16:28 -0800463 }
464 void updateProperties(float rotate, float pivotX, float pivotY,
465 float scaleX, float scaleY, float translateX, float translateY) {
466 mPrimitiveFields.rotate = rotate;
467 mPrimitiveFields.pivotX = pivotX;
468 mPrimitiveFields.pivotY = pivotY;
469 mPrimitiveFields.scaleX = scaleX;
470 mPrimitiveFields.scaleY = scaleY;
471 mPrimitiveFields.translateX = translateX;
472 mPrimitiveFields.translateY = translateY;
473 onPropertyChanged();
474 }
475 void setPropertyValue(int propertyId, float value);
476 float getPropertyValue(int propertyId) const;
477 bool copyProperties(float* outProperties, int length) const;
478 static bool isValidProperty(int propertyId);
479 private:
480 enum class Property {
481 rotate = 0,
482 pivotX,
483 pivotY,
484 scaleX,
485 scaleY,
486 translateX,
487 translateY,
488 // Count of the properties, must be at the end.
489 count,
490 };
Doris Liu766431a2016-02-04 22:17:11 +0000491 };
Doris Liu1d8e1942016-03-02 15:16:28 -0800492
Doris Liu4bbc2932015-12-01 17:59:40 -0800493 Group(const Group& group);
494 Group() {}
Doris Liu4bbc2932015-12-01 17:59:40 -0800495 void addChild(Node* child);
Doris Liu1d8e1942016-03-02 15:16:28 -0800496 virtual void setPropertyChangedListener(PropertyChangedListener* listener) override {
497 Node::setPropertyChangedListener(listener);
498 for (auto& child : mChildren) {
499 child->setPropertyChangedListener(listener);
500 }
501 }
502 virtual void syncProperties() override;
503 GroupProperties* mutateStagingProperties() { return &mStagingProperties; }
504 const GroupProperties* stagingProperties() { return &mStagingProperties; }
505
506 // This should only be called from animations on RT
507 GroupProperties* mutateProperties() { return &mProperties; }
508
509 // Methods below could be called from either UI thread or Render Thread.
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400510 virtual void draw(SkCanvas* outCanvas, bool useStagingData) override;
Doris Liu1d8e1942016-03-02 15:16:28 -0800511 void getLocalMatrix(SkMatrix* outMatrix, const GroupProperties& properties);
Doris Liu4bbc2932015-12-01 17:59:40 -0800512 void dump() override;
Doris Liu766431a2016-02-04 22:17:11 +0000513 static bool isValidProperty(int propertyId);
Doris Liu4bbc2932015-12-01 17:59:40 -0800514
Doris Liu1d8e1942016-03-02 15:16:28 -0800515 virtual void onPropertyChanged(Properties* properties) override {
516 if (properties == &mStagingProperties) {
517 mStagingPropertiesDirty = true;
518 if (mPropertyChangedListener) {
519 mPropertyChangedListener->onStagingPropertyChanged();
520 }
521 } else {
522 if (mPropertyChangedListener) {
523 mPropertyChangedListener->onPropertyChanged();
524 }
525 }
526 }
527
Doris Liu4bbc2932015-12-01 17:59:40 -0800528private:
Doris Liu1d8e1942016-03-02 15:16:28 -0800529 GroupProperties mProperties = GroupProperties(this);
530 GroupProperties mStagingProperties = GroupProperties(this);
531 bool mStagingPropertiesDirty = true;
Doris Liuef062eb2016-02-04 16:16:27 -0800532 std::vector< std::unique_ptr<Node> > mChildren;
Doris Liu4bbc2932015-12-01 17:59:40 -0800533};
534
Doris Liu766431a2016-02-04 22:17:11 +0000535class ANDROID_API Tree : public VirtualLightRefBase {
Doris Liu4bbc2932015-12-01 17:59:40 -0800536public:
Chih-Hung Hsieha619ec72016-08-29 14:52:43 -0700537 explicit Tree(Group* rootNode) : mRootNode(rootNode) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800538 mRootNode->setPropertyChangedListener(&mPropertyChangedListener);
539 }
Doris Liu335d7d12016-05-26 15:19:15 -0700540
541 // Copy properties from the tree and use the give node as the root node
542 Tree(const Tree* copy, Group* rootNode) : Tree(rootNode) {
543 mStagingProperties.syncAnimatableProperties(*copy->stagingProperties());
544 mStagingProperties.syncNonAnimatableProperties(*copy->stagingProperties());
545 }
Doris Liuf8d131c2016-04-29 18:41:29 -0700546 // Draws the VD onto a bitmap cache, then the bitmap cache will be rendered onto the input
547 // canvas. Returns the number of pixels needed for the bitmap cache.
548 int draw(Canvas* outCanvas, SkColorFilter* colorFilter,
Doris Liu4bbc2932015-12-01 17:59:40 -0800549 const SkRect& bounds, bool needsMirroring, bool canReuseCache);
Doris Liu1d8e1942016-03-02 15:16:28 -0800550 void drawStaging(Canvas* canvas);
Doris Liu4bbc2932015-12-01 17:59:40 -0800551
sergeyvfc9999502016-10-17 13:07:38 -0700552 Bitmap& getBitmapUpdateIfDirty();
Doris Liu4bbc2932015-12-01 17:59:40 -0800553 void setAllowCaching(bool allowCaching) {
554 mAllowCaching = allowCaching;
555 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800556 SkPaint* getPaint();
557 void syncProperties() {
558 if (mStagingProperties.mNonAnimatablePropertiesDirty) {
559 mProperties.syncNonAnimatableProperties(mStagingProperties);
560 mStagingProperties.mNonAnimatablePropertiesDirty = false;
561 }
562
563 if (mStagingProperties.mAnimatablePropertiesDirty) {
564 mProperties.syncAnimatableProperties(mStagingProperties);
565 } else {
566 mStagingProperties.syncAnimatableProperties(mProperties);
567 }
568 mStagingProperties.mAnimatablePropertiesDirty = false;
569 mRootNode->syncProperties();
Doris Liu4bbc2932015-12-01 17:59:40 -0800570 }
571
Doris Liu1d8e1942016-03-02 15:16:28 -0800572 class TreeProperties {
573 public:
Chih-Hung Hsieha619ec72016-08-29 14:52:43 -0700574 explicit TreeProperties(Tree* tree) : mTree(tree) {}
Doris Liu1d8e1942016-03-02 15:16:28 -0800575 // Properties that can only be modified by UI thread, therefore sync should
576 // only go from UI to RT
577 struct NonAnimatableProperties {
578 float viewportWidth = 0;
579 float viewportHeight = 0;
580 SkRect bounds;
581 int scaledWidth = 0;
582 int scaledHeight = 0;
583 SkColorFilter* colorFilter = nullptr;
584 ~NonAnimatableProperties() {
585 SkSafeUnref(colorFilter);
586 }
587 } mNonAnimatableProperties;
588 bool mNonAnimatablePropertiesDirty = true;
589
590 float mRootAlpha = 1.0f;
591 bool mAnimatablePropertiesDirty = true;
592
593 void syncNonAnimatableProperties(const TreeProperties& prop) {
594 // Copy over the data that can only be changed in UI thread
595 if (mNonAnimatableProperties.colorFilter != prop.mNonAnimatableProperties.colorFilter) {
596 SkRefCnt_SafeAssign(mNonAnimatableProperties.colorFilter,
597 prop.mNonAnimatableProperties.colorFilter);
598 }
599 mNonAnimatableProperties = prop.mNonAnimatableProperties;
600 }
601
602 void setViewportSize(float width, float height) {
603 if (mNonAnimatableProperties.viewportWidth != width
604 || mNonAnimatableProperties.viewportHeight != height) {
605 mNonAnimatablePropertiesDirty = true;
606 mNonAnimatableProperties.viewportWidth = width;
607 mNonAnimatableProperties.viewportHeight = height;
608 mTree->onPropertyChanged(this);
609 }
610 }
611 void setBounds(const SkRect& bounds) {
612 if (mNonAnimatableProperties.bounds != bounds) {
613 mNonAnimatableProperties.bounds = bounds;
614 mNonAnimatablePropertiesDirty = true;
615 mTree->onPropertyChanged(this);
616 }
617 }
618
619 void setScaledSize(int width, int height) {
Teng-Hui Zhu037fc182016-11-16 10:29:39 -0800620 // If the requested size is bigger than what the bitmap was, then
621 // we increase the bitmap size to match. The width and height
622 // are bound by MAX_CACHED_BITMAP_SIZE.
623 if (mNonAnimatableProperties.scaledWidth < width
624 || mNonAnimatableProperties.scaledHeight < height) {
625 mNonAnimatableProperties.scaledWidth = std::max(width,
626 mNonAnimatableProperties.scaledWidth);
627 mNonAnimatableProperties.scaledHeight = std::max(height,
628 mNonAnimatableProperties.scaledHeight);
Doris Liu1d8e1942016-03-02 15:16:28 -0800629 mNonAnimatablePropertiesDirty = true;
630 mTree->onPropertyChanged(this);
631 }
632 }
633 void setColorFilter(SkColorFilter* filter) {
634 if (UPDATE_SKPROP(mNonAnimatableProperties.colorFilter, filter)) {
635 mNonAnimatablePropertiesDirty = true;
636 mTree->onPropertyChanged(this);
637 }
638 }
639 SkColorFilter* getColorFilter() const{
640 return mNonAnimatableProperties.colorFilter;
641 }
642
643 float getViewportWidth() const {
644 return mNonAnimatableProperties.viewportWidth;
645 }
646 float getViewportHeight() const {
647 return mNonAnimatableProperties.viewportHeight;
648 }
649 float getScaledWidth() const {
650 return mNonAnimatableProperties.scaledWidth;
651 }
652 float getScaledHeight() const {
653 return mNonAnimatableProperties.scaledHeight;
654 }
655 void syncAnimatableProperties(const TreeProperties& prop) {
656 mRootAlpha = prop.mRootAlpha;
657 }
658 bool setRootAlpha(float rootAlpha) {
659 if (rootAlpha != mRootAlpha) {
660 mAnimatablePropertiesDirty = true;
661 mRootAlpha = rootAlpha;
662 mTree->onPropertyChanged(this);
663 return true;
664 }
665 return false;
666 }
667 float getRootAlpha() const { return mRootAlpha;}
668 const SkRect& getBounds() const {
669 return mNonAnimatableProperties.bounds;
670 }
671 Tree* mTree;
672 };
673 void onPropertyChanged(TreeProperties* prop);
674 TreeProperties* mutateStagingProperties() { return &mStagingProperties; }
Doris Liu335d7d12016-05-26 15:19:15 -0700675 const TreeProperties* stagingProperties() const { return &mStagingProperties; }
Doris Liu1d8e1942016-03-02 15:16:28 -0800676
677 // This should only be called from animations on RT
678 TreeProperties* mutateProperties() { return &mProperties; }
Doris Liu4bbc2932015-12-01 17:59:40 -0800679
Doris Liu67ce99b2016-05-17 16:50:31 -0700680 // This should always be called from RT.
Doris Liu7c7052d2016-07-25 17:19:24 -0700681 void markDirty() { mCache.dirty = true; }
Doris Liu67ce99b2016-05-17 16:50:31 -0700682 bool isDirty() const { return mCache.dirty; }
683 bool getPropertyChangeWillBeConsumed() const { return mWillBeConsumed; }
684 void setPropertyChangeWillBeConsumed(bool willBeConsumed) { mWillBeConsumed = willBeConsumed; }
685
Doris Liu4bbc2932015-12-01 17:59:40 -0800686private:
sergeyvfc9999502016-10-17 13:07:38 -0700687 struct Cache {
688 sk_sp<Bitmap> bitmap;
689 bool dirty = true;
690 };
Doris Liu1d8e1942016-03-02 15:16:28 -0800691
692 SkPaint* updatePaint(SkPaint* outPaint, TreeProperties* prop);
sergeyvfc9999502016-10-17 13:07:38 -0700693 bool allocateBitmapIfNeeded(Cache& cache, int width, int height);
694 bool canReuseBitmap(Bitmap*, int width, int height);
695 void updateBitmapCache(Bitmap& outCache, bool useStagingData);
Doris Liu4bbc2932015-12-01 17:59:40 -0800696 // Cap the bitmap size, such that it won't hurt the performance too much
697 // and it won't crash due to a very large scale.
698 // The drawable will look blurry above this size.
699 const static int MAX_CACHED_BITMAP_SIZE;
700
Doris Liu4bbc2932015-12-01 17:59:40 -0800701 bool mAllowCaching = true;
Doris Liuef062eb2016-02-04 16:16:27 -0800702 std::unique_ptr<Group> mRootNode;
Doris Liu4bbc2932015-12-01 17:59:40 -0800703
Doris Liu1d8e1942016-03-02 15:16:28 -0800704 TreeProperties mProperties = TreeProperties(this);
705 TreeProperties mStagingProperties = TreeProperties(this);
706
Doris Liu1d8e1942016-03-02 15:16:28 -0800707 SkPaint mPaint;
Doris Liu1d8e1942016-03-02 15:16:28 -0800708
709 Cache mStagingCache;
710 Cache mCache;
711
712 PropertyChangedListener mPropertyChangedListener
713 = PropertyChangedListener(&mCache.dirty, &mStagingCache.dirty);
Doris Liu67ce99b2016-05-17 16:50:31 -0700714
715 mutable bool mWillBeConsumed = false;
Doris Liu4bbc2932015-12-01 17:59:40 -0800716};
717
718} // namespace VectorDrawable
719
720typedef VectorDrawable::Path::Data PathData;
721} // namespace uirenderer
722} // namespace android
723
724#endif // ANDROID_HWUI_VPATH_H