blob: 0f566e4b494a499c9f89a70d1afc38965d065df7 [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 "VectorDrawable.h"
18
Stan Iliev52e43922019-08-06 15:59:44 -040019#include <math.h>
20#include <string.h>
John Reck1bcacfd2017-11-03 10:12:19 -070021#include <utils/Log.h>
Stan Iliev52e43922019-08-06 15:59:44 -040022
Doris Liu4bbc2932015-12-01 17:59:40 -080023#include "PathParser.h"
Doris Liu1d8e1942016-03-02 15:16:28 -080024#include "SkColorFilter.h"
Doris Liu4bbc2932015-12-01 17:59:40 -080025#include "SkImageInfo.h"
Stan Iliev52e43922019-08-06 15:59:44 -040026#include "hwui/Paint.h"
27
28#ifdef __ANDROID__
29#include "renderthread/RenderThread.h"
30#endif
31
Doris Liu4bbc2932015-12-01 17:59:40 -080032#include "utils/Macros.h"
ztenghuicf0c41d2017-09-13 10:32:50 -070033#include "utils/TraceUtils.h"
Doris Liu4bbc2932015-12-01 17:59:40 -080034#include "utils/VectorDrawableUtils.h"
35
Doris Liu4bbc2932015-12-01 17:59:40 -080036namespace android {
37namespace uirenderer {
38namespace VectorDrawable {
39
40const int Tree::MAX_CACHED_BITMAP_SIZE = 2048;
41
Doris Liu4bbc2932015-12-01 17:59:40 -080042void Path::dump() {
Doris Liu1d8e1942016-03-02 15:16:28 -080043 ALOGD("Path: %s has %zu points", mName.c_str(), mProperties.getData().points.size());
Doris Liu4bbc2932015-12-01 17:59:40 -080044}
45
Doris Liu1d8e1942016-03-02 15:16:28 -080046// Called from UI thread during the initial setup/theme change.
Doris Liu4bbc2932015-12-01 17:59:40 -080047Path::Path(const char* pathStr, size_t strLength) {
48 PathParser::ParseResult result;
Doris Liu1d8e1942016-03-02 15:16:28 -080049 Data data;
Doris Liub35da392016-04-12 11:06:23 -070050 PathParser::getPathDataFromAsciiString(&data, &result, pathStr, strLength);
Doris Liu1d8e1942016-03-02 15:16:28 -080051 mStagingProperties.setData(data);
Doris Liu4bbc2932015-12-01 17:59:40 -080052}
53
54Path::Path(const Path& path) : Node(path) {
Doris Liu1d8e1942016-03-02 15:16:28 -080055 mStagingProperties.syncProperties(path.mStagingProperties);
Doris Liu4bbc2932015-12-01 17:59:40 -080056}
57
Stan Ilievcc29a5d2017-03-15 16:37:10 -040058const SkPath& Path::getUpdatedPath(bool useStagingData, SkPath* tempStagingPath) {
59 if (useStagingData) {
60 tempStagingPath->reset();
61 VectorDrawableUtils::verbsToPath(tempStagingPath, mStagingProperties.getData());
62 return *tempStagingPath;
63 } else {
64 if (mSkPathDirty) {
65 mSkPath.reset();
66 VectorDrawableUtils::verbsToPath(&mSkPath, mProperties.getData());
67 mSkPathDirty = false;
68 }
69 return mSkPath;
Doris Liu4bbc2932015-12-01 17:59:40 -080070 }
Doris Liu1d8e1942016-03-02 15:16:28 -080071}
72
73void Path::syncProperties() {
74 if (mStagingPropertiesDirty) {
75 mProperties.syncProperties(mStagingProperties);
76 } else {
77 mStagingProperties.syncProperties(mProperties);
78 }
79 mStagingPropertiesDirty = false;
Doris Liu4bbc2932015-12-01 17:59:40 -080080}
81
82FullPath::FullPath(const FullPath& path) : Path(path) {
Doris Liu1d8e1942016-03-02 15:16:28 -080083 mStagingProperties.syncProperties(path.mStagingProperties);
84}
85
86static void applyTrim(SkPath* outPath, const SkPath& inPath, float trimPathStart, float trimPathEnd,
John Reck1bcacfd2017-11-03 10:12:19 -070087 float trimPathOffset) {
Doris Liu1d8e1942016-03-02 15:16:28 -080088 if (trimPathStart == 0.0f && trimPathEnd == 1.0f) {
89 *outPath = inPath;
90 return;
91 }
92 outPath->reset();
93 if (trimPathStart == trimPathEnd) {
94 // Trimmed path should be empty.
95 return;
96 }
97 SkPathMeasure measure(inPath, false);
98 float len = SkScalarToFloat(measure.getLength());
99 float start = len * fmod((trimPathStart + trimPathOffset), 1.0f);
100 float end = len * fmod((trimPathEnd + trimPathOffset), 1.0f);
101
102 if (start > end) {
103 measure.getSegment(start, len, outPath, true);
104 if (end > 0) {
105 measure.getSegment(0, end, outPath, true);
106 }
107 } else {
108 measure.getSegment(start, end, outPath, true);
109 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800110}
111
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400112const SkPath& FullPath::getUpdatedPath(bool useStagingData, SkPath* tempStagingPath) {
113 if (!useStagingData && !mSkPathDirty && !mProperties.mTrimDirty) {
Doris Liu4bbc2932015-12-01 17:59:40 -0800114 return mTrimmedSkPath;
115 }
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400116 Path::getUpdatedPath(useStagingData, tempStagingPath);
John Reck1bcacfd2017-11-03 10:12:19 -0700117 SkPath* outPath;
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400118 if (useStagingData) {
119 SkPath inPath = *tempStagingPath;
120 applyTrim(tempStagingPath, inPath, mStagingProperties.getTrimPathStart(),
John Reck1bcacfd2017-11-03 10:12:19 -0700121 mStagingProperties.getTrimPathEnd(), mStagingProperties.getTrimPathOffset());
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400122 outPath = tempStagingPath;
Doris Liu4bbc2932015-12-01 17:59:40 -0800123 } else {
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400124 if (mProperties.getTrimPathStart() != 0.0f || mProperties.getTrimPathEnd() != 1.0f) {
125 mProperties.mTrimDirty = false;
126 applyTrim(&mTrimmedSkPath, mSkPath, mProperties.getTrimPathStart(),
John Reck1bcacfd2017-11-03 10:12:19 -0700127 mProperties.getTrimPathEnd(), mProperties.getTrimPathOffset());
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400128 outPath = &mTrimmedSkPath;
129 } else {
130 outPath = &mSkPath;
131 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800132 }
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400133 const FullPathProperties& properties = useStagingData ? mStagingProperties : mProperties;
John Reck1bcacfd2017-11-03 10:12:19 -0700134 bool setFillPath = properties.getFillGradient() != nullptr ||
135 properties.getFillColor() != SK_ColorTRANSPARENT;
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400136 if (setFillPath) {
Mike Reed6a8bf8e2019-12-03 13:01:07 -0500137 outPath->setFillType(static_cast<SkPathFillType>(properties.getFillType()));
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400138 }
139 return *outPath;
Doris Liu4bbc2932015-12-01 17:59:40 -0800140}
141
Doris Liu1d8e1942016-03-02 15:16:28 -0800142void FullPath::dump() {
143 Path::dump();
144 ALOGD("stroke width, color, alpha: %f, %d, %f, fill color, alpha: %d, %f",
John Reck1bcacfd2017-11-03 10:12:19 -0700145 mProperties.getStrokeWidth(), mProperties.getStrokeColor(), mProperties.getStrokeAlpha(),
146 mProperties.getFillColor(), mProperties.getFillAlpha());
Doris Liu1d8e1942016-03-02 15:16:28 -0800147}
148
Doris Liu4bbc2932015-12-01 17:59:40 -0800149inline SkColor applyAlpha(SkColor color, float alpha) {
150 int alphaBytes = SkColorGetA(color);
151 return SkColorSetA(color, alphaBytes * alpha);
152}
153
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400154void FullPath::draw(SkCanvas* outCanvas, bool useStagingData) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800155 const FullPathProperties& properties = useStagingData ? mStagingProperties : mProperties;
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400156 SkPath tempStagingPath;
157 const SkPath& renderPath = getUpdatedPath(useStagingData, &tempStagingPath);
Doris Liu1d8e1942016-03-02 15:16:28 -0800158
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800159 // Draw path's fill, if fill color or gradient is valid
160 bool needsFill = false;
Nader Jawadfc42a992020-07-29 22:48:59 -0700161 Paint paint;
Doris Liu1d8e1942016-03-02 15:16:28 -0800162 if (properties.getFillGradient() != nullptr) {
163 paint.setColor(applyAlpha(SK_ColorBLACK, properties.getFillAlpha()));
Nader Jawadfc42a992020-07-29 22:48:59 -0700164 paint.setShader(sk_sp<Shader>(SkSafeRef(properties.getFillGradient())));
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800165 needsFill = true;
Doris Liu1d8e1942016-03-02 15:16:28 -0800166 } else if (properties.getFillColor() != SK_ColorTRANSPARENT) {
167 paint.setColor(applyAlpha(properties.getFillColor(), properties.getFillAlpha()));
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800168 needsFill = true;
Doris Liu4bbc2932015-12-01 17:59:40 -0800169 }
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800170
171 if (needsFill) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800172 paint.setStyle(SkPaint::Style::kFill_Style);
Doris Liu6b184d72017-12-04 16:31:07 -0800173 paint.setAntiAlias(mAntiAlias);
Doris Liu1d8e1942016-03-02 15:16:28 -0800174 outCanvas->drawPath(renderPath, paint);
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800175 }
176
Doris Liu1d8e1942016-03-02 15:16:28 -0800177 // Draw path's stroke, if stroke color or Gradient is valid
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800178 bool needsStroke = false;
Doris Liu1d8e1942016-03-02 15:16:28 -0800179 if (properties.getStrokeGradient() != nullptr) {
180 paint.setColor(applyAlpha(SK_ColorBLACK, properties.getStrokeAlpha()));
Nader Jawadfc42a992020-07-29 22:48:59 -0700181 paint.setShader(sk_sp<Shader>(SkSafeRef(properties.getStrokeGradient())));
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800182 needsStroke = true;
Doris Liu1d8e1942016-03-02 15:16:28 -0800183 } else if (properties.getStrokeColor() != SK_ColorTRANSPARENT) {
184 paint.setColor(applyAlpha(properties.getStrokeColor(), properties.getStrokeAlpha()));
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800185 needsStroke = true;
186 }
187 if (needsStroke) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800188 paint.setStyle(SkPaint::Style::kStroke_Style);
Doris Liu6b184d72017-12-04 16:31:07 -0800189 paint.setAntiAlias(mAntiAlias);
Doris Liu1d8e1942016-03-02 15:16:28 -0800190 paint.setStrokeJoin(SkPaint::Join(properties.getStrokeLineJoin()));
191 paint.setStrokeCap(SkPaint::Cap(properties.getStrokeLineCap()));
192 paint.setStrokeMiter(properties.getStrokeMiterLimit());
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400193 paint.setStrokeWidth(properties.getStrokeWidth());
Doris Liu1d8e1942016-03-02 15:16:28 -0800194 outCanvas->drawPath(renderPath, paint);
Doris Liu4bbc2932015-12-01 17:59:40 -0800195 }
196}
197
Doris Liu1d8e1942016-03-02 15:16:28 -0800198void FullPath::syncProperties() {
199 Path::syncProperties();
Doris Liu4bbc2932015-12-01 17:59:40 -0800200
Doris Liu1d8e1942016-03-02 15:16:28 -0800201 if (mStagingPropertiesDirty) {
202 mProperties.syncProperties(mStagingProperties);
Doris Liu4bbc2932015-12-01 17:59:40 -0800203 } else {
Doris Liu1d8e1942016-03-02 15:16:28 -0800204 // Update staging property with property values from animation.
205 mStagingProperties.syncProperties(mProperties);
Doris Liu4bbc2932015-12-01 17:59:40 -0800206 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800207 mStagingPropertiesDirty = false;
Doris Liu4bbc2932015-12-01 17:59:40 -0800208}
209
Doris Liu1d8e1942016-03-02 15:16:28 -0800210REQUIRE_COMPATIBLE_LAYOUT(FullPath::FullPathProperties::PrimitiveFields);
Doris Liu4bbc2932015-12-01 17:59:40 -0800211
212static_assert(sizeof(float) == sizeof(int32_t), "float is not the same size as int32_t");
213static_assert(sizeof(SkColor) == sizeof(int32_t), "SkColor is not the same size as int32_t");
214
Doris Liu1d8e1942016-03-02 15:16:28 -0800215bool FullPath::FullPathProperties::copyProperties(int8_t* outProperties, int length) const {
216 int propertyDataSize = sizeof(FullPathProperties::PrimitiveFields);
Doris Liu4bbc2932015-12-01 17:59:40 -0800217 if (length != propertyDataSize) {
218 LOG_ALWAYS_FATAL("Properties needs exactly %d bytes, a byte array of size %d is provided",
John Reck1bcacfd2017-11-03 10:12:19 -0700219 propertyDataSize, length);
Doris Liu4bbc2932015-12-01 17:59:40 -0800220 return false;
221 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800222
223 PrimitiveFields* out = reinterpret_cast<PrimitiveFields*>(outProperties);
224 *out = mPrimitiveFields;
Doris Liu4bbc2932015-12-01 17:59:40 -0800225 return true;
226}
227
Doris Liu1d8e1942016-03-02 15:16:28 -0800228void FullPath::FullPathProperties::setColorPropertyValue(int propertyId, int32_t value) {
Doris Liu766431a2016-02-04 22:17:11 +0000229 Property currentProperty = static_cast<Property>(propertyId);
Doris Liu1d8e1942016-03-02 15:16:28 -0800230 if (currentProperty == Property::strokeColor) {
231 setStrokeColor(value);
232 } else if (currentProperty == Property::fillColor) {
233 setFillColor(value);
Doris Liu766431a2016-02-04 22:17:11 +0000234 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700235 LOG_ALWAYS_FATAL(
236 "Error setting color property on FullPath: No valid property"
237 " with id: %d",
238 propertyId);
Doris Liu766431a2016-02-04 22:17:11 +0000239 }
240}
241
Doris Liu1d8e1942016-03-02 15:16:28 -0800242void FullPath::FullPathProperties::setPropertyValue(int propertyId, float value) {
Doris Liu766431a2016-02-04 22:17:11 +0000243 Property property = static_cast<Property>(propertyId);
244 switch (property) {
John Reck1bcacfd2017-11-03 10:12:19 -0700245 case Property::strokeWidth:
246 setStrokeWidth(value);
247 break;
248 case Property::strokeAlpha:
249 setStrokeAlpha(value);
250 break;
251 case Property::fillAlpha:
252 setFillAlpha(value);
253 break;
254 case Property::trimPathStart:
255 setTrimPathStart(value);
256 break;
257 case Property::trimPathEnd:
258 setTrimPathEnd(value);
259 break;
260 case Property::trimPathOffset:
261 setTrimPathOffset(value);
262 break;
263 default:
264 LOG_ALWAYS_FATAL("Invalid property id: %d for animation", propertyId);
265 break;
Doris Liu766431a2016-02-04 22:17:11 +0000266 }
267}
268
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400269void ClipPath::draw(SkCanvas* outCanvas, bool useStagingData) {
270 SkPath tempStagingPath;
271 outCanvas->clipPath(getUpdatedPath(useStagingData, &tempStagingPath));
Doris Liu4bbc2932015-12-01 17:59:40 -0800272}
273
274Group::Group(const Group& group) : Node(group) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800275 mStagingProperties.syncProperties(group.mStagingProperties);
Doris Liu4bbc2932015-12-01 17:59:40 -0800276}
277
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400278void Group::draw(SkCanvas* outCanvas, bool useStagingData) {
279 // Save the current clip and matrix information, which is local to this group.
280 SkAutoCanvasRestore saver(outCanvas, true);
281 // apply the current group's matrix to the canvas
Doris Liu4bbc2932015-12-01 17:59:40 -0800282 SkMatrix stackedMatrix;
Doris Liu1d8e1942016-03-02 15:16:28 -0800283 const GroupProperties& prop = useStagingData ? mStagingProperties : mProperties;
284 getLocalMatrix(&stackedMatrix, prop);
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400285 outCanvas->concat(stackedMatrix);
Doris Liu4bbc2932015-12-01 17:59:40 -0800286 // Draw the group tree in the same order as the XML file.
Doris Liuef062eb2016-02-04 16:16:27 -0800287 for (auto& child : mChildren) {
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400288 child->draw(outCanvas, useStagingData);
Doris Liu4bbc2932015-12-01 17:59:40 -0800289 }
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400290 // Restore the previous clip and matrix information.
Doris Liu4bbc2932015-12-01 17:59:40 -0800291}
292
293void Group::dump() {
294 ALOGD("Group %s has %zu children: ", mName.c_str(), mChildren.size());
Doris Liu1d8e1942016-03-02 15:16:28 -0800295 ALOGD("Group translateX, Y : %f, %f, scaleX, Y: %f, %f", mProperties.getTranslateX(),
John Reck1bcacfd2017-11-03 10:12:19 -0700296 mProperties.getTranslateY(), mProperties.getScaleX(), mProperties.getScaleY());
Doris Liu4bbc2932015-12-01 17:59:40 -0800297 for (size_t i = 0; i < mChildren.size(); i++) {
298 mChildren[i]->dump();
299 }
300}
301
Doris Liu1d8e1942016-03-02 15:16:28 -0800302void Group::syncProperties() {
303 // Copy over the dirty staging properties
304 if (mStagingPropertiesDirty) {
305 mProperties.syncProperties(mStagingProperties);
306 } else {
307 mStagingProperties.syncProperties(mProperties);
308 }
309 mStagingPropertiesDirty = false;
310 for (auto& child : mChildren) {
311 child->syncProperties();
312 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800313}
314
Doris Liu1d8e1942016-03-02 15:16:28 -0800315void Group::getLocalMatrix(SkMatrix* outMatrix, const GroupProperties& properties) {
Doris Liu4bbc2932015-12-01 17:59:40 -0800316 outMatrix->reset();
317 // TODO: use rotate(mRotate, mPivotX, mPivotY) and scale with pivot point, instead of
318 // translating to pivot for rotating and scaling, then translating back.
Doris Liu1d8e1942016-03-02 15:16:28 -0800319 outMatrix->postTranslate(-properties.getPivotX(), -properties.getPivotY());
320 outMatrix->postScale(properties.getScaleX(), properties.getScaleY());
321 outMatrix->postRotate(properties.getRotation(), 0, 0);
322 outMatrix->postTranslate(properties.getTranslateX() + properties.getPivotX(),
John Reck1bcacfd2017-11-03 10:12:19 -0700323 properties.getTranslateY() + properties.getPivotY());
Doris Liu4bbc2932015-12-01 17:59:40 -0800324}
325
326void Group::addChild(Node* child) {
Doris Liuef062eb2016-02-04 16:16:27 -0800327 mChildren.emplace_back(child);
Doris Liu1d8e1942016-03-02 15:16:28 -0800328 if (mPropertyChangedListener != nullptr) {
329 child->setPropertyChangedListener(mPropertyChangedListener);
330 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800331}
332
Doris Liu1d8e1942016-03-02 15:16:28 -0800333bool Group::GroupProperties::copyProperties(float* outProperties, int length) const {
334 int propertyCount = static_cast<int>(Property::count);
Doris Liu4bbc2932015-12-01 17:59:40 -0800335 if (length != propertyCount) {
336 LOG_ALWAYS_FATAL("Properties needs exactly %d bytes, a byte array of size %d is provided",
John Reck1bcacfd2017-11-03 10:12:19 -0700337 propertyCount, length);
Doris Liu4bbc2932015-12-01 17:59:40 -0800338 return false;
339 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800340
341 PrimitiveFields* out = reinterpret_cast<PrimitiveFields*>(outProperties);
342 *out = mPrimitiveFields;
Doris Liu4bbc2932015-12-01 17:59:40 -0800343 return true;
344}
345
Doris Liu766431a2016-02-04 22:17:11 +0000346// TODO: Consider animating the properties as float pointers
Doris Liu1d8e1942016-03-02 15:16:28 -0800347// Called on render thread
348float Group::GroupProperties::getPropertyValue(int propertyId) const {
Doris Liu766431a2016-02-04 22:17:11 +0000349 Property currentProperty = static_cast<Property>(propertyId);
350 switch (currentProperty) {
John Reck1bcacfd2017-11-03 10:12:19 -0700351 case Property::rotate:
352 return getRotation();
353 case Property::pivotX:
354 return getPivotX();
355 case Property::pivotY:
356 return getPivotY();
357 case Property::scaleX:
358 return getScaleX();
359 case Property::scaleY:
360 return getScaleY();
361 case Property::translateX:
362 return getTranslateX();
363 case Property::translateY:
364 return getTranslateY();
365 default:
366 LOG_ALWAYS_FATAL("Invalid property index: %d", propertyId);
367 return 0;
Doris Liu766431a2016-02-04 22:17:11 +0000368 }
369}
370
Doris Liu1d8e1942016-03-02 15:16:28 -0800371// Called on render thread
372void Group::GroupProperties::setPropertyValue(int propertyId, float value) {
Doris Liu766431a2016-02-04 22:17:11 +0000373 Property currentProperty = static_cast<Property>(propertyId);
374 switch (currentProperty) {
John Reck1bcacfd2017-11-03 10:12:19 -0700375 case Property::rotate:
376 setRotation(value);
377 break;
378 case Property::pivotX:
379 setPivotX(value);
380 break;
381 case Property::pivotY:
382 setPivotY(value);
383 break;
384 case Property::scaleX:
385 setScaleX(value);
386 break;
387 case Property::scaleY:
388 setScaleY(value);
389 break;
390 case Property::translateX:
391 setTranslateX(value);
392 break;
393 case Property::translateY:
394 setTranslateY(value);
395 break;
396 default:
397 LOG_ALWAYS_FATAL("Invalid property index: %d", propertyId);
Doris Liu766431a2016-02-04 22:17:11 +0000398 }
399}
400
401bool Group::isValidProperty(int propertyId) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800402 return GroupProperties::isValidProperty(propertyId);
403}
404
405bool Group::GroupProperties::isValidProperty(int propertyId) {
406 return propertyId >= 0 && propertyId < static_cast<int>(Property::count);
Doris Liu766431a2016-02-04 22:17:11 +0000407}
408
John Reck1bcacfd2017-11-03 10:12:19 -0700409int Tree::draw(Canvas* outCanvas, SkColorFilter* colorFilter, const SkRect& bounds,
410 bool needsMirroring, bool canReuseCache) {
Doris Liu4bbc2932015-12-01 17:59:40 -0800411 // The imageView can scale the canvas in different ways, in order to
412 // avoid blurry scaling, we have to draw into a bitmap with exact pixel
413 // size first. This bitmap size is determined by the bounds and the
414 // canvas scale.
Doris Liu1d8e1942016-03-02 15:16:28 -0800415 SkMatrix canvasMatrix;
416 outCanvas->getMatrix(&canvasMatrix);
Doris Liua0e61572015-12-29 14:57:49 -0800417 float canvasScaleX = 1.0f;
418 float canvasScaleY = 1.0f;
Doris Liu1d8e1942016-03-02 15:16:28 -0800419 if (canvasMatrix.getSkewX() == 0 && canvasMatrix.getSkewY() == 0) {
Doris Liua0e61572015-12-29 14:57:49 -0800420 // Only use the scale value when there's no skew or rotation in the canvas matrix.
Doris Liue410a352016-01-13 17:23:33 -0800421 // TODO: Add a cts test for drawing VD on a canvas with negative scaling factors.
Doris Liu1d8e1942016-03-02 15:16:28 -0800422 canvasScaleX = fabs(canvasMatrix.getScaleX());
423 canvasScaleY = fabs(canvasMatrix.getScaleY());
Doris Liua0e61572015-12-29 14:57:49 -0800424 }
John Reck1bcacfd2017-11-03 10:12:19 -0700425 int scaledWidth = (int)(bounds.width() * canvasScaleX);
426 int scaledHeight = (int)(bounds.height() * canvasScaleY);
Doris Liu4bbc2932015-12-01 17:59:40 -0800427 scaledWidth = std::min(Tree::MAX_CACHED_BITMAP_SIZE, scaledWidth);
428 scaledHeight = std::min(Tree::MAX_CACHED_BITMAP_SIZE, scaledHeight);
429
430 if (scaledWidth <= 0 || scaledHeight <= 0) {
Doris Liuf8d131c2016-04-29 18:41:29 -0700431 return 0;
Doris Liu4bbc2932015-12-01 17:59:40 -0800432 }
433
Doris Liu1d8e1942016-03-02 15:16:28 -0800434 mStagingProperties.setScaledSize(scaledWidth, scaledHeight);
Florin Malita777bf852016-02-03 10:48:55 -0500435 int saveCount = outCanvas->save(SaveFlags::MatrixClip);
Doris Liu1d8e1942016-03-02 15:16:28 -0800436 outCanvas->translate(bounds.fLeft, bounds.fTop);
Doris Liu4bbc2932015-12-01 17:59:40 -0800437
438 // Handle RTL mirroring.
439 if (needsMirroring) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800440 outCanvas->translate(bounds.width(), 0);
Doris Liu4bbc2932015-12-01 17:59:40 -0800441 outCanvas->scale(-1.0f, 1.0f);
442 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800443 mStagingProperties.setColorFilter(colorFilter);
Doris Liu4bbc2932015-12-01 17:59:40 -0800444
445 // At this point, canvas has been translated to the right position.
446 // And we use this bound for the destination rect for the drawBitmap, so
447 // we offset to (0, 0);
Doris Liu1d8e1942016-03-02 15:16:28 -0800448 SkRect tmpBounds = bounds;
449 tmpBounds.offsetTo(0, 0);
450 mStagingProperties.setBounds(tmpBounds);
Doris Liu766431a2016-02-04 22:17:11 +0000451 outCanvas->drawVectorDrawable(this);
Doris Liu4bbc2932015-12-01 17:59:40 -0800452 outCanvas->restoreToCount(saveCount);
Doris Liuf8d131c2016-04-29 18:41:29 -0700453 return scaledWidth * scaledHeight;
Doris Liu4bbc2932015-12-01 17:59:40 -0800454}
455
Doris Liu1d8e1942016-03-02 15:16:28 -0800456void Tree::drawStaging(Canvas* outCanvas) {
John Reck1bcacfd2017-11-03 10:12:19 -0700457 bool redrawNeeded = allocateBitmapIfNeeded(mStagingCache, mStagingProperties.getScaledWidth(),
458 mStagingProperties.getScaledHeight());
Doris Liu1d8e1942016-03-02 15:16:28 -0800459 // draw bitmap cache
460 if (redrawNeeded || mStagingCache.dirty) {
sergeyvfc9999502016-10-17 13:07:38 -0700461 updateBitmapCache(*mStagingCache.bitmap, true);
Doris Liu1d8e1942016-03-02 15:16:28 -0800462 mStagingCache.dirty = false;
Doris Liu4bbc2932015-12-01 17:59:40 -0800463 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800464
Mike Reedc2dbc032019-07-25 12:28:29 -0400465 SkPaint skp;
466 getPaintFor(&skp, mStagingProperties);
467 Paint paint;
468 paint.setFilterQuality(skp.getFilterQuality());
469 paint.setColorFilter(skp.refColorFilter());
470 paint.setAlpha(skp.getAlpha());
John Reck1bcacfd2017-11-03 10:12:19 -0700471 outCanvas->drawBitmap(*mStagingCache.bitmap, 0, 0, mStagingCache.bitmap->width(),
472 mStagingCache.bitmap->height(), mStagingProperties.getBounds().left(),
473 mStagingProperties.getBounds().top(),
474 mStagingProperties.getBounds().right(),
John Reck08ee8152018-09-20 16:27:46 -0700475 mStagingProperties.getBounds().bottom(), &paint);
Doris Liu1d8e1942016-03-02 15:16:28 -0800476}
477
Stan Iliev52e43922019-08-06 15:59:44 -0400478void Tree::getPaintFor(SkPaint* outPaint, const TreeProperties& prop) const {
Stan Iliev038fc372018-07-30 18:31:46 -0400479 // HWUI always draws VD with bilinear filtering.
480 outPaint->setFilterQuality(kLow_SkFilterQuality);
Doris Liu7cc6ec22018-10-02 16:15:57 -0700481 if (prop.getColorFilter() != nullptr) {
John Reck08ee8152018-09-20 16:27:46 -0700482 outPaint->setColorFilter(sk_ref_sp(prop.getColorFilter()));
Doris Liu1d8e1942016-03-02 15:16:28 -0800483 }
Doris Liu7cc6ec22018-10-02 16:15:57 -0700484 outPaint->setAlpha(prop.getRootAlpha() * 255);
Doris Liu4bbc2932015-12-01 17:59:40 -0800485}
486
sergeyvfc9999502016-10-17 13:07:38 -0700487Bitmap& Tree::getBitmapUpdateIfDirty() {
488 bool redrawNeeded = allocateBitmapIfNeeded(mCache, mProperties.getScaledWidth(),
John Reck1bcacfd2017-11-03 10:12:19 -0700489 mProperties.getScaledHeight());
Doris Liu1d8e1942016-03-02 15:16:28 -0800490 if (redrawNeeded || mCache.dirty) {
sergeyvfc9999502016-10-17 13:07:38 -0700491 updateBitmapCache(*mCache.bitmap, false);
Doris Liu1d8e1942016-03-02 15:16:28 -0800492 mCache.dirty = false;
493 }
sergeyvfc9999502016-10-17 13:07:38 -0700494 return *mCache.bitmap;
Doris Liu4bbc2932015-12-01 17:59:40 -0800495}
496
John Reck08ee8152018-09-20 16:27:46 -0700497void Tree::draw(SkCanvas* canvas, const SkRect& bounds, const SkPaint& inPaint) {
Leon Scroggins III6c5864c2019-04-03 15:09:25 -0400498 if (canvas->quickReject(bounds)) {
499 // The RenderNode is on screen, but the AVD is not.
500 return;
501 }
502
John Reck08ee8152018-09-20 16:27:46 -0700503 // Update the paint for any animatable properties
504 SkPaint paint = inPaint;
505 paint.setAlpha(mProperties.getRootAlpha() * 255);
506
John Reck83161dc2019-10-04 14:48:27 -0700507 Bitmap& bitmap = getBitmapUpdateIfDirty();
508 SkBitmap skiaBitmap;
509 bitmap.getSkBitmap(&skiaBitmap);
John Reck5cca8f22018-12-10 17:06:22 -0800510
John Reck83161dc2019-10-04 14:48:27 -0700511 int scaledWidth = SkScalarCeilToInt(mProperties.getScaledWidth());
512 int scaledHeight = SkScalarCeilToInt(mProperties.getScaledHeight());
513 canvas->drawBitmapRect(skiaBitmap, SkRect::MakeWH(scaledWidth, scaledHeight), bounds,
514 &paint, SkCanvas::kFast_SrcRectConstraint);
Stan Iliev23c38a92017-03-23 00:12:50 -0400515}
516
sergeyvfc9999502016-10-17 13:07:38 -0700517void Tree::updateBitmapCache(Bitmap& bitmap, bool useStagingData) {
518 SkBitmap outCache;
519 bitmap.getSkBitmap(&outCache);
ztenghuicf0c41d2017-09-13 10:32:50 -0700520 int cacheWidth = outCache.width();
521 int cacheHeight = outCache.height();
522 ATRACE_FORMAT("VectorDrawable repaint %dx%d", cacheWidth, cacheHeight);
sergeyvfc9999502016-10-17 13:07:38 -0700523 outCache.eraseColor(SK_ColorTRANSPARENT);
524 SkCanvas outCanvas(outCache);
John Reck1bcacfd2017-11-03 10:12:19 -0700525 float viewportWidth =
526 useStagingData ? mStagingProperties.getViewportWidth() : mProperties.getViewportWidth();
527 float viewportHeight = useStagingData ? mStagingProperties.getViewportHeight()
528 : mProperties.getViewportHeight();
ztenghuicf0c41d2017-09-13 10:32:50 -0700529 float scaleX = cacheWidth / viewportWidth;
530 float scaleY = cacheHeight / viewportHeight;
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400531 outCanvas.scale(scaleX, scaleY);
532 mRootNode->draw(&outCanvas, useStagingData);
Doris Liu1d8e1942016-03-02 15:16:28 -0800533}
534
sergeyvfc9999502016-10-17 13:07:38 -0700535bool Tree::allocateBitmapIfNeeded(Cache& cache, int width, int height) {
536 if (!canReuseBitmap(cache.bitmap.get(), width, height)) {
Leon Scroggins III12497572019-01-31 10:06:12 -0500537 SkImageInfo info = SkImageInfo::MakeN32(width, height, kPremul_SkAlphaType);
sergeyvfc9999502016-10-17 13:07:38 -0700538 cache.bitmap = Bitmap::allocateHeapBitmap(info);
Doris Liu1d8e1942016-03-02 15:16:28 -0800539 return true;
Doris Liu4bbc2932015-12-01 17:59:40 -0800540 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800541 return false;
Doris Liu4bbc2932015-12-01 17:59:40 -0800542}
543
sergeyvfc9999502016-10-17 13:07:38 -0700544bool Tree::canReuseBitmap(Bitmap* bitmap, int width, int height) {
Teng-Hui Zhu037fc182016-11-16 10:29:39 -0800545 return bitmap && width <= bitmap->width() && height <= bitmap->height();
Doris Liu1d8e1942016-03-02 15:16:28 -0800546}
547
548void Tree::onPropertyChanged(TreeProperties* prop) {
549 if (prop == &mStagingProperties) {
550 mStagingCache.dirty = true;
551 } else {
552 mCache.dirty = true;
553 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800554}
555
John Reck08ee8152018-09-20 16:27:46 -0700556class MinMaxAverage {
557public:
558 void add(float sample) {
559 if (mCount == 0) {
560 mMin = sample;
561 mMax = sample;
562 } else {
563 mMin = std::min(mMin, sample);
564 mMax = std::max(mMax, sample);
565 }
566 mTotal += sample;
567 mCount++;
568 }
569
570 float average() { return mTotal / mCount; }
571
572 float min() { return mMin; }
573
574 float max() { return mMax; }
575
576 float delta() { return mMax - mMin; }
577
578private:
579 float mMin = 0.0f;
580 float mMax = 0.0f;
581 float mTotal = 0.0f;
582 int mCount = 0;
583};
584
585BitmapPalette Tree::computePalette() {
586 // TODO Cache this and share the code with Bitmap.cpp
587
588 ATRACE_CALL();
589
590 // TODO: This calculation of converting to HSV & tracking min/max is probably overkill
591 // Experiment with something simpler since we just want to figure out if it's "color-ful"
592 // and then the average perceptual lightness.
593
594 MinMaxAverage hue, saturation, value;
595 int sampledCount = 0;
596
597 // Sample a grid of 100 pixels to get an overall estimation of the colors in play
598 mRootNode->forEachFillColor([&](SkColor color) {
599 if (SkColorGetA(color) < 75) {
600 return;
601 }
602 sampledCount++;
603 float hsv[3];
604 SkColorToHSV(color, hsv);
605 hue.add(hsv[0]);
606 saturation.add(hsv[1]);
607 value.add(hsv[2]);
608 });
609
610 if (sampledCount == 0) {
611 ALOGV("VectorDrawable is mostly translucent");
612 return BitmapPalette::Unknown;
613 }
614
615 ALOGV("samples = %d, hue [min = %f, max = %f, avg = %f]; saturation [min = %f, max = %f, avg = "
616 "%f]; value [min = %f, max = %f, avg = %f]",
617 sampledCount, hue.min(), hue.max(), hue.average(), saturation.min(), saturation.max(),
618 saturation.average(), value.min(), value.max(), value.average());
619
620 if (hue.delta() <= 20 && saturation.delta() <= .1f) {
621 if (value.average() >= .5f) {
622 return BitmapPalette::Light;
623 } else {
624 return BitmapPalette::Dark;
625 }
626 }
627 return BitmapPalette::Unknown;
628}
629
Chris Blume7b8a8082018-11-30 15:51:58 -0800630} // namespace VectorDrawable
Doris Liu4bbc2932015-12-01 17:59:40 -0800631
Chris Blume7b8a8082018-11-30 15:51:58 -0800632} // namespace uirenderer
633} // namespace android