blob: 4b80542ad0d0cf1cefc58cc26a71cce950d9dec6 [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"
Stan Iliev3310fb12017-03-23 16:56:51 -040022#include "renderthread/CacheManager.h"
Doris Liu1d8e1942016-03-02 15:16:28 -080023#include "DisplayList.h"
Doris Liu766431a2016-02-04 22:17:11 +000024
Doris Liu4bbc2932015-12-01 17:59:40 -080025#include <SkBitmap.h>
26#include <SkColor.h>
Doris Liu1d8e1942016-03-02 15:16:28 -080027#include <SkColorFilter.h>
Doris Liuc2de46f2016-01-21 12:55:54 -080028#include <SkCanvas.h>
Doris Liu4bbc2932015-12-01 17:59:40 -080029#include <SkMatrix.h>
30#include <SkPaint.h>
31#include <SkPath.h>
32#include <SkPathMeasure.h>
33#include <SkRect.h>
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -080034#include <SkShader.h>
Stan Iliev23c38a92017-03-23 00:12:50 -040035#include <SkSurface.h>
Doris Liu4bbc2932015-12-01 17:59:40 -080036
37#include <cutils/compiler.h>
38#include <stddef.h>
39#include <vector>
40#include <string>
41
42namespace android {
43namespace uirenderer {
44
Teng-Hui Zhu85d99522016-04-25 14:23:40 -070045// Debug
46#if DEBUG_VECTOR_DRAWABLE
47 #define VECTOR_DRAWABLE_LOGD(...) ALOGD(__VA_ARGS__)
48#else
49 #define VECTOR_DRAWABLE_LOGD(...)
50#endif
51
Doris Liu4bbc2932015-12-01 17:59:40 -080052namespace VectorDrawable {
Doris Liu32d7cda2016-04-08 13:48:47 -070053#define VD_SET_PRIMITIVE_FIELD_WITH_FLAG(field, value, flag) (VD_SET_PRIMITIVE_FIELD_AND_NOTIFY(field, (value)) ? ((flag) = true, true) : false)
54#define VD_SET_PROP(field, value) ((value) != (field) ? ((field) = (value), true) : false)
55#define VD_SET_PRIMITIVE_FIELD_AND_NOTIFY(field, value) ({ bool retVal = VD_SET_PROP((mPrimitiveFields.field), (value));\
Doris Liu1d8e1942016-03-02 15:16:28 -080056 onPropertyChanged(); retVal;})
Doris Liu32d7cda2016-04-08 13:48:47 -070057#define UPDATE_SKPROP(field, value) ({bool retVal = ((field) != (value)); if ((field) != (value)) SkRefCnt_SafeAssign((field), (value)); retVal;})
Doris Liu4bbc2932015-12-01 17:59:40 -080058
59/* A VectorDrawable is composed of a tree of nodes.
60 * Each node can be a group node, or a path.
61 * A group node can have groups or paths as children, but a path node has
62 * no children.
63 * One example can be:
64 * Root Group
65 * / | \
66 * Group Path Group
67 * / \ |
68 * Path Path Path
69 *
Doris Liu1d8e1942016-03-02 15:16:28 -080070 * VectorDrawables are drawn into bitmap caches first, then the caches are drawn to the given
71 * canvas with root alpha applied. Two caches are maintained for VD, one in UI thread, the other in
72 * Render Thread. A generation id is used to keep track of changes in the vector drawable tree.
73 * Each cache has their own generation id to track whether they are up to date with the latest
74 * change in the tree.
75 *
76 * Any property change to the vector drawable coming from UI thread (such as bulk setters to update
77 * all the properties, and viewport change, etc.) are only modifying the staging properties. The
78 * staging properties will then be marked dirty and will be pushed over to render thread properties
79 * at sync point. If staging properties are not dirty at sync point, we sync backwards by updating
80 * staging properties with render thread properties to reflect the latest animation value.
81 *
Doris Liu4bbc2932015-12-01 17:59:40 -080082 */
Doris Liu1d8e1942016-03-02 15:16:28 -080083
84class PropertyChangedListener {
85public:
86 PropertyChangedListener(bool* dirty, bool* stagingDirty)
87 : mDirty(dirty), mStagingDirty(stagingDirty) {}
88 void onPropertyChanged() {
89 *mDirty = true;
90 }
91 void onStagingPropertyChanged() {
92 *mStagingDirty = true;
93 }
94private:
95 bool* mDirty;
96 bool* mStagingDirty;
97};
98
Doris Liu4bbc2932015-12-01 17:59:40 -080099class ANDROID_API Node {
100public:
Doris Liu1d8e1942016-03-02 15:16:28 -0800101 class Properties {
102 public:
Chih-Hung Hsieha619ec72016-08-29 14:52:43 -0700103 explicit Properties(Node* node) : mNode(node) {}
Doris Liu1d8e1942016-03-02 15:16:28 -0800104 inline void onPropertyChanged() {
105 mNode->onPropertyChanged(this);
106 }
107 private:
108 Node* mNode;
109 };
Doris Liu4bbc2932015-12-01 17:59:40 -0800110 Node(const Node& node) {
111 mName = node.mName;
112 }
113 Node() {}
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400114 virtual void draw(SkCanvas* outCanvas, bool useStagingData) = 0;
Doris Liu4bbc2932015-12-01 17:59:40 -0800115 virtual void dump() = 0;
116 void setName(const char* name) {
117 mName = name;
118 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800119 virtual void setPropertyChangedListener(PropertyChangedListener* listener) {
120 mPropertyChangedListener = listener;
121 }
122 virtual void onPropertyChanged(Properties* properties) = 0;
Doris Liu4bbc2932015-12-01 17:59:40 -0800123 virtual ~Node(){}
Doris Liu1d8e1942016-03-02 15:16:28 -0800124 virtual void syncProperties() = 0;
Doris Liu4bbc2932015-12-01 17:59:40 -0800125protected:
126 std::string mName;
Doris Liu1d8e1942016-03-02 15:16:28 -0800127 PropertyChangedListener* mPropertyChangedListener = nullptr;
Doris Liu4bbc2932015-12-01 17:59:40 -0800128};
129
130class ANDROID_API Path : public Node {
131public:
132 struct ANDROID_API Data {
133 std::vector<char> verbs;
134 std::vector<size_t> verbSizes;
135 std::vector<float> points;
136 bool operator==(const Data& data) const {
137 return verbs == data.verbs && verbSizes == data.verbSizes
138 && points == data.points;
139 }
140 };
Doris Liu1d8e1942016-03-02 15:16:28 -0800141
142 class PathProperties : public Properties {
143 public:
Chih-Hung Hsieha619ec72016-08-29 14:52:43 -0700144 explicit PathProperties(Node* node) : Properties(node) {}
Doris Liu1d8e1942016-03-02 15:16:28 -0800145 void syncProperties(const PathProperties& prop) {
146 mData = prop.mData;
147 onPropertyChanged();
148 }
149 void setData(const Data& data) {
150 // Updates the path data. Note that we don't generate a new Skia path right away
151 // because there are cases where the animation is changing the path data, but the view
152 // that hosts the VD has gone off screen, in which case we won't even draw. So we
153 // postpone the Skia path generation to the draw time.
154 if (data == mData) {
155 return;
156 }
157 mData = data;
158 onPropertyChanged();
159
160 }
161 const Data& getData() const {
162 return mData;
163 }
164 private:
165 Data mData;
166 };
167
Doris Liu4bbc2932015-12-01 17:59:40 -0800168 Path(const Path& path);
169 Path(const char* path, size_t strLength);
170 Path() {}
Doris Liu1d8e1942016-03-02 15:16:28 -0800171
Doris Liu4bbc2932015-12-01 17:59:40 -0800172 void dump() override;
Doris Liu1d8e1942016-03-02 15:16:28 -0800173 virtual void syncProperties() override;
174 virtual void onPropertyChanged(Properties* prop) override {
175 if (prop == &mStagingProperties) {
176 mStagingPropertiesDirty = true;
177 if (mPropertyChangedListener) {
178 mPropertyChangedListener->onStagingPropertyChanged();
179 }
180 } else if (prop == &mProperties){
181 mSkPathDirty = true;
182 if (mPropertyChangedListener) {
183 mPropertyChangedListener->onPropertyChanged();
184 }
185 }
186 }
187 PathProperties* mutateStagingProperties() { return &mStagingProperties; }
188 const PathProperties* stagingProperties() { return &mStagingProperties; }
189
190 // This should only be called from animations on RT
191 PathProperties* mutateProperties() { return &mProperties; }
Doris Liu4bbc2932015-12-01 17:59:40 -0800192
193protected:
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400194 virtual const SkPath& getUpdatedPath(bool useStagingData, SkPath* tempStagingPath);
Doris Liu1d8e1942016-03-02 15:16:28 -0800195
196 // Internal data, render thread only.
Doris Liu4bbc2932015-12-01 17:59:40 -0800197 bool mSkPathDirty = true;
Doris Liu1d8e1942016-03-02 15:16:28 -0800198 SkPath mSkPath;
199
200private:
201 PathProperties mProperties = PathProperties(this);
202 PathProperties mStagingProperties = PathProperties(this);
203 bool mStagingPropertiesDirty = true;
Doris Liu4bbc2932015-12-01 17:59:40 -0800204};
205
206class ANDROID_API FullPath: public Path {
207public:
Doris Liu1d8e1942016-03-02 15:16:28 -0800208 class FullPathProperties : public Properties {
209 public:
210 struct PrimitiveFields {
211 float strokeWidth = 0;
212 SkColor strokeColor = SK_ColorTRANSPARENT;
213 float strokeAlpha = 1;
214 SkColor fillColor = SK_ColorTRANSPARENT;
215 float fillAlpha = 1;
216 float trimPathStart = 0;
217 float trimPathEnd = 1;
218 float trimPathOffset = 0;
219 int32_t strokeLineCap = SkPaint::Cap::kButt_Cap;
220 int32_t strokeLineJoin = SkPaint::Join::kMiter_Join;
221 float strokeMiterLimit = 4;
222 int fillType = 0; /* non-zero or kWinding_FillType in Skia */
223 };
Chih-Hung Hsieha619ec72016-08-29 14:52:43 -0700224 explicit FullPathProperties(Node* mNode) : Properties(mNode), mTrimDirty(false) {}
Doris Liuad21fe272016-04-14 18:13:36 -0700225 ~FullPathProperties() {
226 SkSafeUnref(fillGradient);
227 SkSafeUnref(strokeGradient);
228 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800229 void syncProperties(const FullPathProperties& prop) {
230 mPrimitiveFields = prop.mPrimitiveFields;
231 mTrimDirty = true;
Doris Liuad21fe272016-04-14 18:13:36 -0700232 UPDATE_SKPROP(fillGradient, prop.fillGradient);
233 UPDATE_SKPROP(strokeGradient, prop.strokeGradient);
Doris Liu1d8e1942016-03-02 15:16:28 -0800234 onPropertyChanged();
235 }
236 void setFillGradient(SkShader* gradient) {
Doris Liuad21fe272016-04-14 18:13:36 -0700237 if(UPDATE_SKPROP(fillGradient, gradient)) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800238 onPropertyChanged();
239 }
240 }
241 void setStrokeGradient(SkShader* gradient) {
Doris Liuad21fe272016-04-14 18:13:36 -0700242 if(UPDATE_SKPROP(strokeGradient, gradient)) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800243 onPropertyChanged();
244 }
245 }
246 SkShader* getFillGradient() const {
247 return fillGradient;
248 }
249 SkShader* getStrokeGradient() const {
250 return strokeGradient;
251 }
252 float getStrokeWidth() const{
253 return mPrimitiveFields.strokeWidth;
254 }
255 void setStrokeWidth(float strokeWidth) {
Doris Liu32d7cda2016-04-08 13:48:47 -0700256 VD_SET_PRIMITIVE_FIELD_AND_NOTIFY(strokeWidth, strokeWidth);
Doris Liu1d8e1942016-03-02 15:16:28 -0800257 }
258 SkColor getStrokeColor() const{
259 return mPrimitiveFields.strokeColor;
260 }
261 void setStrokeColor(SkColor strokeColor) {
Doris Liu32d7cda2016-04-08 13:48:47 -0700262 VD_SET_PRIMITIVE_FIELD_AND_NOTIFY(strokeColor, strokeColor);
Doris Liu1d8e1942016-03-02 15:16:28 -0800263 }
264 float getStrokeAlpha() const{
265 return mPrimitiveFields.strokeAlpha;
266 }
267 void setStrokeAlpha(float strokeAlpha) {
Doris Liu32d7cda2016-04-08 13:48:47 -0700268 VD_SET_PRIMITIVE_FIELD_AND_NOTIFY(strokeAlpha, strokeAlpha);
Doris Liu1d8e1942016-03-02 15:16:28 -0800269 }
270 SkColor getFillColor() const {
271 return mPrimitiveFields.fillColor;
272 }
273 void setFillColor(SkColor fillColor) {
Doris Liu32d7cda2016-04-08 13:48:47 -0700274 VD_SET_PRIMITIVE_FIELD_AND_NOTIFY(fillColor, fillColor);
Doris Liu1d8e1942016-03-02 15:16:28 -0800275 }
276 float getFillAlpha() const{
277 return mPrimitiveFields.fillAlpha;
278 }
279 void setFillAlpha(float fillAlpha) {
Doris Liu32d7cda2016-04-08 13:48:47 -0700280 VD_SET_PRIMITIVE_FIELD_AND_NOTIFY(fillAlpha, fillAlpha);
Doris Liu1d8e1942016-03-02 15:16:28 -0800281 }
282 float getTrimPathStart() const{
283 return mPrimitiveFields.trimPathStart;
284 }
285 void setTrimPathStart(float trimPathStart) {
Doris Liu32d7cda2016-04-08 13:48:47 -0700286 VD_SET_PRIMITIVE_FIELD_WITH_FLAG(trimPathStart, trimPathStart, mTrimDirty);
Doris Liu1d8e1942016-03-02 15:16:28 -0800287 }
288 float getTrimPathEnd() const{
289 return mPrimitiveFields.trimPathEnd;
290 }
291 void setTrimPathEnd(float trimPathEnd) {
Doris Liu32d7cda2016-04-08 13:48:47 -0700292 VD_SET_PRIMITIVE_FIELD_WITH_FLAG(trimPathEnd, trimPathEnd, mTrimDirty);
Doris Liu1d8e1942016-03-02 15:16:28 -0800293 }
294 float getTrimPathOffset() const{
295 return mPrimitiveFields.trimPathOffset;
296 }
297 void setTrimPathOffset(float trimPathOffset) {
Doris Liu32d7cda2016-04-08 13:48:47 -0700298 VD_SET_PRIMITIVE_FIELD_WITH_FLAG(trimPathOffset, trimPathOffset, mTrimDirty);
Doris Liu1d8e1942016-03-02 15:16:28 -0800299 }
Doris Liu766431a2016-02-04 22:17:11 +0000300
Doris Liu1d8e1942016-03-02 15:16:28 -0800301 float getStrokeMiterLimit() const {
302 return mPrimitiveFields.strokeMiterLimit;
303 }
304 float getStrokeLineCap() const {
305 return mPrimitiveFields.strokeLineCap;
306 }
307 float getStrokeLineJoin() const {
308 return mPrimitiveFields.strokeLineJoin;
309 }
310 float getFillType() const {
311 return mPrimitiveFields.fillType;
312 }
313 bool copyProperties(int8_t* outProperties, int length) const;
314 void updateProperties(float strokeWidth, SkColor strokeColor, float strokeAlpha,
315 SkColor fillColor, float fillAlpha, float trimPathStart, float trimPathEnd,
316 float trimPathOffset, float strokeMiterLimit, int strokeLineCap, int strokeLineJoin,
317 int fillType) {
318 mPrimitiveFields.strokeWidth = strokeWidth;
319 mPrimitiveFields.strokeColor = strokeColor;
320 mPrimitiveFields.strokeAlpha = strokeAlpha;
321 mPrimitiveFields.fillColor = fillColor;
322 mPrimitiveFields.fillAlpha = fillAlpha;
323 mPrimitiveFields.trimPathStart = trimPathStart;
324 mPrimitiveFields.trimPathEnd = trimPathEnd;
325 mPrimitiveFields.trimPathOffset = trimPathOffset;
326 mPrimitiveFields.strokeMiterLimit = strokeMiterLimit;
327 mPrimitiveFields.strokeLineCap = strokeLineCap;
328 mPrimitiveFields.strokeLineJoin = strokeLineJoin;
329 mPrimitiveFields.fillType = fillType;
330 mTrimDirty = true;
331 onPropertyChanged();
332 }
333 // Set property values during animation
334 void setColorPropertyValue(int propertyId, int32_t value);
335 void setPropertyValue(int propertyId, float value);
336 bool mTrimDirty;
337 private:
338 enum class Property {
339 strokeWidth = 0,
340 strokeColor,
341 strokeAlpha,
342 fillColor,
343 fillAlpha,
344 trimPathStart,
345 trimPathEnd,
346 trimPathOffset,
347 strokeLineCap,
348 strokeLineJoin,
349 strokeMiterLimit,
350 fillType,
351 count,
352 };
353 PrimitiveFields mPrimitiveFields;
Doris Liuad21fe272016-04-14 18:13:36 -0700354 SkShader* fillGradient = nullptr;
355 SkShader* strokeGradient = nullptr;
Doris Liu1d8e1942016-03-02 15:16:28 -0800356 };
Doris Liu766431a2016-02-04 22:17:11 +0000357
Doris Liu1d8e1942016-03-02 15:16:28 -0800358 // Called from UI thread
Doris Liu4bbc2932015-12-01 17:59:40 -0800359 FullPath(const FullPath& path); // for cloning
360 FullPath(const char* path, size_t strLength) : Path(path, strLength) {}
361 FullPath() : Path() {}
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400362 void draw(SkCanvas* outCanvas, bool useStagingData) override;
Doris Liu1d8e1942016-03-02 15:16:28 -0800363 void dump() override;
364 FullPathProperties* mutateStagingProperties() { return &mStagingProperties; }
365 const FullPathProperties* stagingProperties() { return &mStagingProperties; }
Doris Liu4bbc2932015-12-01 17:59:40 -0800366
Doris Liu1d8e1942016-03-02 15:16:28 -0800367 // This should only be called from animations on RT
368 FullPathProperties* mutateProperties() { return &mProperties; }
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800369
Doris Liu1d8e1942016-03-02 15:16:28 -0800370 virtual void syncProperties() override;
371 virtual void onPropertyChanged(Properties* properties) override {
372 Path::onPropertyChanged(properties);
373 if (properties == &mStagingProperties) {
374 mStagingPropertiesDirty = true;
375 if (mPropertyChangedListener) {
376 mPropertyChangedListener->onStagingPropertyChanged();
377 }
378 } else if (properties == &mProperties) {
379 if (mPropertyChangedListener) {
380 mPropertyChangedListener->onPropertyChanged();
381 }
382 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800383 }
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800384
Doris Liu4bbc2932015-12-01 17:59:40 -0800385protected:
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400386 const SkPath& getUpdatedPath(bool useStagingData, SkPath* tempStagingPath) override;
Doris Liu4bbc2932015-12-01 17:59:40 -0800387private:
Doris Liu1d8e1942016-03-02 15:16:28 -0800388
389 FullPathProperties mProperties = FullPathProperties(this);
390 FullPathProperties mStagingProperties = FullPathProperties(this);
391 bool mStagingPropertiesDirty = true;
392
393 // Intermediate data for drawing, render thread only
Doris Liu5a11e8d2016-02-04 20:04:10 +0000394 SkPath mTrimmedSkPath;
Doris Liu1d8e1942016-03-02 15:16:28 -0800395
Doris Liu4bbc2932015-12-01 17:59:40 -0800396};
397
398class ANDROID_API ClipPath: public Path {
399public:
400 ClipPath(const ClipPath& path) : Path(path) {}
401 ClipPath(const char* path, size_t strLength) : Path(path, strLength) {}
402 ClipPath() : Path() {}
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400403 void draw(SkCanvas* outCanvas, bool useStagingData) override;
Doris Liu4bbc2932015-12-01 17:59:40 -0800404};
405
406class ANDROID_API Group: public Node {
407public:
Doris Liu1d8e1942016-03-02 15:16:28 -0800408 class GroupProperties : public Properties {
409 public:
Chih-Hung Hsieha619ec72016-08-29 14:52:43 -0700410 explicit GroupProperties(Node* mNode) : Properties(mNode) {}
Doris Liu1d8e1942016-03-02 15:16:28 -0800411 struct PrimitiveFields {
412 float rotate = 0;
413 float pivotX = 0;
414 float pivotY = 0;
415 float scaleX = 1;
416 float scaleY = 1;
417 float translateX = 0;
418 float translateY = 0;
419 } mPrimitiveFields;
420 void syncProperties(const GroupProperties& prop) {
421 mPrimitiveFields = prop.mPrimitiveFields;
422 onPropertyChanged();
423 }
424 float getRotation() const {
425 return mPrimitiveFields.rotate;
426 }
427 void setRotation(float rotation) {
Doris Liu32d7cda2016-04-08 13:48:47 -0700428 VD_SET_PRIMITIVE_FIELD_AND_NOTIFY(rotate, rotation);
Doris Liu1d8e1942016-03-02 15:16:28 -0800429 }
430 float getPivotX() const {
431 return mPrimitiveFields.pivotX;
432 }
433 void setPivotX(float pivotX) {
Doris Liu32d7cda2016-04-08 13:48:47 -0700434 VD_SET_PRIMITIVE_FIELD_AND_NOTIFY(pivotX, pivotX);
Doris Liu1d8e1942016-03-02 15:16:28 -0800435 }
436 float getPivotY() const {
437 return mPrimitiveFields.pivotY;
438 }
439 void setPivotY(float pivotY) {
Doris Liu32d7cda2016-04-08 13:48:47 -0700440 VD_SET_PRIMITIVE_FIELD_AND_NOTIFY(pivotY, pivotY);
Doris Liu1d8e1942016-03-02 15:16:28 -0800441 }
442 float getScaleX() const {
443 return mPrimitiveFields.scaleX;
444 }
445 void setScaleX(float scaleX) {
Doris Liu32d7cda2016-04-08 13:48:47 -0700446 VD_SET_PRIMITIVE_FIELD_AND_NOTIFY(scaleX, scaleX);
Doris Liu1d8e1942016-03-02 15:16:28 -0800447 }
448 float getScaleY() const {
449 return mPrimitiveFields.scaleY;
450 }
451 void setScaleY(float scaleY) {
Doris Liu32d7cda2016-04-08 13:48:47 -0700452 VD_SET_PRIMITIVE_FIELD_AND_NOTIFY(scaleY, scaleY);
Doris Liu1d8e1942016-03-02 15:16:28 -0800453 }
454 float getTranslateX() const {
455 return mPrimitiveFields.translateX;
456 }
457 void setTranslateX(float translateX) {
Doris Liu32d7cda2016-04-08 13:48:47 -0700458 VD_SET_PRIMITIVE_FIELD_AND_NOTIFY(translateX, translateX);
Doris Liu1d8e1942016-03-02 15:16:28 -0800459 }
460 float getTranslateY() const {
461 return mPrimitiveFields.translateY;
462 }
463 void setTranslateY(float translateY) {
Doris Liu32d7cda2016-04-08 13:48:47 -0700464 VD_SET_PRIMITIVE_FIELD_AND_NOTIFY(translateY, translateY);
Doris Liu1d8e1942016-03-02 15:16:28 -0800465 }
466 void updateProperties(float rotate, float pivotX, float pivotY,
467 float scaleX, float scaleY, float translateX, float translateY) {
468 mPrimitiveFields.rotate = rotate;
469 mPrimitiveFields.pivotX = pivotX;
470 mPrimitiveFields.pivotY = pivotY;
471 mPrimitiveFields.scaleX = scaleX;
472 mPrimitiveFields.scaleY = scaleY;
473 mPrimitiveFields.translateX = translateX;
474 mPrimitiveFields.translateY = translateY;
475 onPropertyChanged();
476 }
477 void setPropertyValue(int propertyId, float value);
478 float getPropertyValue(int propertyId) const;
479 bool copyProperties(float* outProperties, int length) const;
480 static bool isValidProperty(int propertyId);
481 private:
482 enum class Property {
483 rotate = 0,
484 pivotX,
485 pivotY,
486 scaleX,
487 scaleY,
488 translateX,
489 translateY,
490 // Count of the properties, must be at the end.
491 count,
492 };
Doris Liu766431a2016-02-04 22:17:11 +0000493 };
Doris Liu1d8e1942016-03-02 15:16:28 -0800494
Doris Liu4bbc2932015-12-01 17:59:40 -0800495 Group(const Group& group);
496 Group() {}
Doris Liu4bbc2932015-12-01 17:59:40 -0800497 void addChild(Node* child);
Doris Liu1d8e1942016-03-02 15:16:28 -0800498 virtual void setPropertyChangedListener(PropertyChangedListener* listener) override {
499 Node::setPropertyChangedListener(listener);
500 for (auto& child : mChildren) {
501 child->setPropertyChangedListener(listener);
502 }
503 }
504 virtual void syncProperties() override;
505 GroupProperties* mutateStagingProperties() { return &mStagingProperties; }
506 const GroupProperties* stagingProperties() { return &mStagingProperties; }
507
508 // This should only be called from animations on RT
509 GroupProperties* mutateProperties() { return &mProperties; }
510
511 // Methods below could be called from either UI thread or Render Thread.
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400512 virtual void draw(SkCanvas* outCanvas, bool useStagingData) override;
Doris Liu1d8e1942016-03-02 15:16:28 -0800513 void getLocalMatrix(SkMatrix* outMatrix, const GroupProperties& properties);
Doris Liu4bbc2932015-12-01 17:59:40 -0800514 void dump() override;
Doris Liu766431a2016-02-04 22:17:11 +0000515 static bool isValidProperty(int propertyId);
Doris Liu4bbc2932015-12-01 17:59:40 -0800516
Doris Liu1d8e1942016-03-02 15:16:28 -0800517 virtual void onPropertyChanged(Properties* properties) override {
518 if (properties == &mStagingProperties) {
519 mStagingPropertiesDirty = true;
520 if (mPropertyChangedListener) {
521 mPropertyChangedListener->onStagingPropertyChanged();
522 }
523 } else {
524 if (mPropertyChangedListener) {
525 mPropertyChangedListener->onPropertyChanged();
526 }
527 }
528 }
529
Doris Liu4bbc2932015-12-01 17:59:40 -0800530private:
Doris Liu1d8e1942016-03-02 15:16:28 -0800531 GroupProperties mProperties = GroupProperties(this);
532 GroupProperties mStagingProperties = GroupProperties(this);
533 bool mStagingPropertiesDirty = true;
Doris Liuef062eb2016-02-04 16:16:27 -0800534 std::vector< std::unique_ptr<Node> > mChildren;
Doris Liu4bbc2932015-12-01 17:59:40 -0800535};
536
Doris Liu766431a2016-02-04 22:17:11 +0000537class ANDROID_API Tree : public VirtualLightRefBase {
Doris Liu4bbc2932015-12-01 17:59:40 -0800538public:
Chih-Hung Hsieha619ec72016-08-29 14:52:43 -0700539 explicit Tree(Group* rootNode) : mRootNode(rootNode) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800540 mRootNode->setPropertyChangedListener(&mPropertyChangedListener);
541 }
Doris Liu335d7d12016-05-26 15:19:15 -0700542
543 // Copy properties from the tree and use the give node as the root node
544 Tree(const Tree* copy, Group* rootNode) : Tree(rootNode) {
545 mStagingProperties.syncAnimatableProperties(*copy->stagingProperties());
546 mStagingProperties.syncNonAnimatableProperties(*copy->stagingProperties());
547 }
Doris Liuf8d131c2016-04-29 18:41:29 -0700548 // Draws the VD onto a bitmap cache, then the bitmap cache will be rendered onto the input
549 // canvas. Returns the number of pixels needed for the bitmap cache.
550 int draw(Canvas* outCanvas, SkColorFilter* colorFilter,
Doris Liu4bbc2932015-12-01 17:59:40 -0800551 const SkRect& bounds, bool needsMirroring, bool canReuseCache);
Doris Liu1d8e1942016-03-02 15:16:28 -0800552 void drawStaging(Canvas* canvas);
Doris Liu4bbc2932015-12-01 17:59:40 -0800553
sergeyvfc9999502016-10-17 13:07:38 -0700554 Bitmap& getBitmapUpdateIfDirty();
Doris Liu4bbc2932015-12-01 17:59:40 -0800555 void setAllowCaching(bool allowCaching) {
556 mAllowCaching = allowCaching;
557 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800558 SkPaint* getPaint();
559 void syncProperties() {
560 if (mStagingProperties.mNonAnimatablePropertiesDirty) {
Stan Ilievd7b8af12017-09-14 10:58:26 -0400561 mCache.dirty |= (mProperties.mNonAnimatableProperties.viewportWidth
562 != mStagingProperties.mNonAnimatableProperties.viewportWidth)
563 || (mProperties.mNonAnimatableProperties.viewportHeight
564 != mStagingProperties.mNonAnimatableProperties.viewportHeight)
565 || (mProperties.mNonAnimatableProperties.scaledWidth
566 != mStagingProperties.mNonAnimatableProperties.scaledWidth)
567 || (mProperties.mNonAnimatableProperties.scaledHeight
568 != mStagingProperties.mNonAnimatableProperties.scaledHeight)
569 || (mProperties.mNonAnimatableProperties.bounds
570 != mStagingProperties.mNonAnimatableProperties.bounds);
Doris Liu1d8e1942016-03-02 15:16:28 -0800571 mProperties.syncNonAnimatableProperties(mStagingProperties);
572 mStagingProperties.mNonAnimatablePropertiesDirty = false;
573 }
574
575 if (mStagingProperties.mAnimatablePropertiesDirty) {
576 mProperties.syncAnimatableProperties(mStagingProperties);
577 } else {
578 mStagingProperties.syncAnimatableProperties(mProperties);
579 }
580 mStagingProperties.mAnimatablePropertiesDirty = false;
581 mRootNode->syncProperties();
Doris Liu4bbc2932015-12-01 17:59:40 -0800582 }
583
Doris Liu1d8e1942016-03-02 15:16:28 -0800584 class TreeProperties {
585 public:
Chih-Hung Hsieha619ec72016-08-29 14:52:43 -0700586 explicit TreeProperties(Tree* tree) : mTree(tree) {}
Doris Liu1d8e1942016-03-02 15:16:28 -0800587 // Properties that can only be modified by UI thread, therefore sync should
588 // only go from UI to RT
589 struct NonAnimatableProperties {
590 float viewportWidth = 0;
591 float viewportHeight = 0;
592 SkRect bounds;
593 int scaledWidth = 0;
594 int scaledHeight = 0;
595 SkColorFilter* colorFilter = nullptr;
596 ~NonAnimatableProperties() {
597 SkSafeUnref(colorFilter);
598 }
599 } mNonAnimatableProperties;
600 bool mNonAnimatablePropertiesDirty = true;
601
602 float mRootAlpha = 1.0f;
603 bool mAnimatablePropertiesDirty = true;
604
605 void syncNonAnimatableProperties(const TreeProperties& prop) {
606 // Copy over the data that can only be changed in UI thread
607 if (mNonAnimatableProperties.colorFilter != prop.mNonAnimatableProperties.colorFilter) {
608 SkRefCnt_SafeAssign(mNonAnimatableProperties.colorFilter,
609 prop.mNonAnimatableProperties.colorFilter);
610 }
611 mNonAnimatableProperties = prop.mNonAnimatableProperties;
612 }
613
614 void setViewportSize(float width, float height) {
615 if (mNonAnimatableProperties.viewportWidth != width
616 || mNonAnimatableProperties.viewportHeight != height) {
617 mNonAnimatablePropertiesDirty = true;
618 mNonAnimatableProperties.viewportWidth = width;
619 mNonAnimatableProperties.viewportHeight = height;
620 mTree->onPropertyChanged(this);
621 }
622 }
623 void setBounds(const SkRect& bounds) {
624 if (mNonAnimatableProperties.bounds != bounds) {
625 mNonAnimatableProperties.bounds = bounds;
626 mNonAnimatablePropertiesDirty = true;
627 mTree->onPropertyChanged(this);
628 }
629 }
630
631 void setScaledSize(int width, int height) {
Teng-Hui Zhu037fc182016-11-16 10:29:39 -0800632 // If the requested size is bigger than what the bitmap was, then
633 // we increase the bitmap size to match. The width and height
634 // are bound by MAX_CACHED_BITMAP_SIZE.
635 if (mNonAnimatableProperties.scaledWidth < width
636 || mNonAnimatableProperties.scaledHeight < height) {
637 mNonAnimatableProperties.scaledWidth = std::max(width,
638 mNonAnimatableProperties.scaledWidth);
639 mNonAnimatableProperties.scaledHeight = std::max(height,
640 mNonAnimatableProperties.scaledHeight);
Doris Liu1d8e1942016-03-02 15:16:28 -0800641 mNonAnimatablePropertiesDirty = true;
642 mTree->onPropertyChanged(this);
643 }
644 }
645 void setColorFilter(SkColorFilter* filter) {
646 if (UPDATE_SKPROP(mNonAnimatableProperties.colorFilter, filter)) {
647 mNonAnimatablePropertiesDirty = true;
648 mTree->onPropertyChanged(this);
649 }
650 }
651 SkColorFilter* getColorFilter() const{
652 return mNonAnimatableProperties.colorFilter;
653 }
654
655 float getViewportWidth() const {
656 return mNonAnimatableProperties.viewportWidth;
657 }
658 float getViewportHeight() const {
659 return mNonAnimatableProperties.viewportHeight;
660 }
661 float getScaledWidth() const {
662 return mNonAnimatableProperties.scaledWidth;
663 }
664 float getScaledHeight() const {
665 return mNonAnimatableProperties.scaledHeight;
666 }
667 void syncAnimatableProperties(const TreeProperties& prop) {
668 mRootAlpha = prop.mRootAlpha;
669 }
670 bool setRootAlpha(float rootAlpha) {
671 if (rootAlpha != mRootAlpha) {
672 mAnimatablePropertiesDirty = true;
673 mRootAlpha = rootAlpha;
674 mTree->onPropertyChanged(this);
675 return true;
676 }
677 return false;
678 }
679 float getRootAlpha() const { return mRootAlpha;}
680 const SkRect& getBounds() const {
681 return mNonAnimatableProperties.bounds;
682 }
683 Tree* mTree;
684 };
685 void onPropertyChanged(TreeProperties* prop);
686 TreeProperties* mutateStagingProperties() { return &mStagingProperties; }
Doris Liu335d7d12016-05-26 15:19:15 -0700687 const TreeProperties* stagingProperties() const { return &mStagingProperties; }
Doris Liu1d8e1942016-03-02 15:16:28 -0800688
689 // This should only be called from animations on RT
690 TreeProperties* mutateProperties() { return &mProperties; }
Doris Liu4bbc2932015-12-01 17:59:40 -0800691
Stan Iliev23c38a92017-03-23 00:12:50 -0400692 // called from RT only
693 const TreeProperties& properties() const { return mProperties; }
694
Doris Liu67ce99b2016-05-17 16:50:31 -0700695 // This should always be called from RT.
Doris Liu7c7052d2016-07-25 17:19:24 -0700696 void markDirty() { mCache.dirty = true; }
Doris Liu67ce99b2016-05-17 16:50:31 -0700697 bool isDirty() const { return mCache.dirty; }
698 bool getPropertyChangeWillBeConsumed() const { return mWillBeConsumed; }
699 void setPropertyChangeWillBeConsumed(bool willBeConsumed) { mWillBeConsumed = willBeConsumed; }
700
Stan Iliev3310fb12017-03-23 16:56:51 -0400701 /**
702 * Draws VD cache into a canvas. This should always be called from RT and it works with Skia
703 * pipelines only.
704 */
Stan Iliev23c38a92017-03-23 00:12:50 -0400705 void draw(SkCanvas* canvas);
706
Stan Iliev3310fb12017-03-23 16:56:51 -0400707 /**
708 * Draws VD into a GPU backed surface.
709 * This should always be called from RT and it works with Skia pipeline only.
710 */
711 void updateCache(sp<skiapipeline::VectorDrawableAtlas>& atlas, GrContext* context);
Stan Iliev23c38a92017-03-23 00:12:50 -0400712
Doris Liu4bbc2932015-12-01 17:59:40 -0800713private:
Stan Iliev3310fb12017-03-23 16:56:51 -0400714 class Cache {
715 public:
Stan Iliev23c38a92017-03-23 00:12:50 -0400716 sk_sp<Bitmap> bitmap; //used by HWUI pipeline and software
717 //TODO: use surface instead of bitmap when drawing in software canvas
sergeyvfc9999502016-10-17 13:07:38 -0700718 bool dirty = true;
Stan Iliev3310fb12017-03-23 16:56:51 -0400719
720 // the rest of the code in Cache is used by Skia pipelines only
721
722 ~Cache() { clear(); }
723
724 /**
725 * Stores a weak pointer to the atlas and a key.
726 */
727 void setAtlas(sp<skiapipeline::VectorDrawableAtlas> atlas,
728 skiapipeline::AtlasKey newAtlasKey);
729
730 /**
731 * Gets a surface and bounds from the atlas.
732 *
733 * @return nullptr if the altas has been deleted.
734 */
735 sk_sp<SkSurface> getSurface(SkRect* bounds);
736
737 /**
738 * Releases atlas key from the atlas, which makes it available for reuse.
739 */
740 void clear();
741 private:
742 wp<skiapipeline::VectorDrawableAtlas> mAtlas;
743 skiapipeline::AtlasKey mAtlasKey = INVALID_ATLAS_KEY;
sergeyvfc9999502016-10-17 13:07:38 -0700744 };
Doris Liu1d8e1942016-03-02 15:16:28 -0800745
746 SkPaint* updatePaint(SkPaint* outPaint, TreeProperties* prop);
sergeyvfc9999502016-10-17 13:07:38 -0700747 bool allocateBitmapIfNeeded(Cache& cache, int width, int height);
748 bool canReuseBitmap(Bitmap*, int width, int height);
749 void updateBitmapCache(Bitmap& outCache, bool useStagingData);
Stan Iliev3310fb12017-03-23 16:56:51 -0400750
Doris Liu4bbc2932015-12-01 17:59:40 -0800751 // Cap the bitmap size, such that it won't hurt the performance too much
752 // and it won't crash due to a very large scale.
753 // The drawable will look blurry above this size.
754 const static int MAX_CACHED_BITMAP_SIZE;
755
Doris Liu4bbc2932015-12-01 17:59:40 -0800756 bool mAllowCaching = true;
Doris Liuef062eb2016-02-04 16:16:27 -0800757 std::unique_ptr<Group> mRootNode;
Doris Liu4bbc2932015-12-01 17:59:40 -0800758
Doris Liu1d8e1942016-03-02 15:16:28 -0800759 TreeProperties mProperties = TreeProperties(this);
760 TreeProperties mStagingProperties = TreeProperties(this);
761
Doris Liu1d8e1942016-03-02 15:16:28 -0800762 SkPaint mPaint;
Doris Liu1d8e1942016-03-02 15:16:28 -0800763
764 Cache mStagingCache;
765 Cache mCache;
766
767 PropertyChangedListener mPropertyChangedListener
768 = PropertyChangedListener(&mCache.dirty, &mStagingCache.dirty);
Doris Liu67ce99b2016-05-17 16:50:31 -0700769
770 mutable bool mWillBeConsumed = false;
Doris Liu4bbc2932015-12-01 17:59:40 -0800771};
772
773} // namespace VectorDrawable
774
775typedef VectorDrawable::Path::Data PathData;
776} // namespace uirenderer
777} // namespace android
778
779#endif // ANDROID_HWUI_VPATH_H