blob: cd908354aea5f376b4ccdcda6a728bd045f1f95c [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"
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -080026#include "SkShader.h"
Stan Iliev52e43922019-08-06 15:59:44 -040027#include "hwui/Paint.h"
28
29#ifdef __ANDROID__
30#include "renderthread/RenderThread.h"
31#endif
32
Doris Liu4bbc2932015-12-01 17:59:40 -080033#include "utils/Macros.h"
ztenghuicf0c41d2017-09-13 10:32:50 -070034#include "utils/TraceUtils.h"
Doris Liu4bbc2932015-12-01 17:59:40 -080035#include "utils/VectorDrawableUtils.h"
36
Doris Liu4bbc2932015-12-01 17:59:40 -080037namespace android {
38namespace uirenderer {
39namespace VectorDrawable {
40
41const int Tree::MAX_CACHED_BITMAP_SIZE = 2048;
42
Doris Liu4bbc2932015-12-01 17:59:40 -080043void Path::dump() {
Doris Liu1d8e1942016-03-02 15:16:28 -080044 ALOGD("Path: %s has %zu points", mName.c_str(), mProperties.getData().points.size());
Doris Liu4bbc2932015-12-01 17:59:40 -080045}
46
Doris Liu1d8e1942016-03-02 15:16:28 -080047// Called from UI thread during the initial setup/theme change.
Doris Liu4bbc2932015-12-01 17:59:40 -080048Path::Path(const char* pathStr, size_t strLength) {
49 PathParser::ParseResult result;
Doris Liu1d8e1942016-03-02 15:16:28 -080050 Data data;
Doris Liub35da392016-04-12 11:06:23 -070051 PathParser::getPathDataFromAsciiString(&data, &result, pathStr, strLength);
Doris Liu1d8e1942016-03-02 15:16:28 -080052 mStagingProperties.setData(data);
Doris Liu4bbc2932015-12-01 17:59:40 -080053}
54
55Path::Path(const Path& path) : Node(path) {
Doris Liu1d8e1942016-03-02 15:16:28 -080056 mStagingProperties.syncProperties(path.mStagingProperties);
Doris Liu4bbc2932015-12-01 17:59:40 -080057}
58
Stan Ilievcc29a5d2017-03-15 16:37:10 -040059const SkPath& Path::getUpdatedPath(bool useStagingData, SkPath* tempStagingPath) {
60 if (useStagingData) {
61 tempStagingPath->reset();
62 VectorDrawableUtils::verbsToPath(tempStagingPath, mStagingProperties.getData());
63 return *tempStagingPath;
64 } else {
65 if (mSkPathDirty) {
66 mSkPath.reset();
67 VectorDrawableUtils::verbsToPath(&mSkPath, mProperties.getData());
68 mSkPathDirty = false;
69 }
70 return mSkPath;
Doris Liu4bbc2932015-12-01 17:59:40 -080071 }
Doris Liu1d8e1942016-03-02 15:16:28 -080072}
73
74void Path::syncProperties() {
75 if (mStagingPropertiesDirty) {
76 mProperties.syncProperties(mStagingProperties);
77 } else {
78 mStagingProperties.syncProperties(mProperties);
79 }
80 mStagingPropertiesDirty = false;
Doris Liu4bbc2932015-12-01 17:59:40 -080081}
82
83FullPath::FullPath(const FullPath& path) : Path(path) {
Doris Liu1d8e1942016-03-02 15:16:28 -080084 mStagingProperties.syncProperties(path.mStagingProperties);
85}
86
87static void applyTrim(SkPath* outPath, const SkPath& inPath, float trimPathStart, float trimPathEnd,
John Reck1bcacfd2017-11-03 10:12:19 -070088 float trimPathOffset) {
Doris Liu1d8e1942016-03-02 15:16:28 -080089 if (trimPathStart == 0.0f && trimPathEnd == 1.0f) {
90 *outPath = inPath;
91 return;
92 }
93 outPath->reset();
94 if (trimPathStart == trimPathEnd) {
95 // Trimmed path should be empty.
96 return;
97 }
98 SkPathMeasure measure(inPath, false);
99 float len = SkScalarToFloat(measure.getLength());
100 float start = len * fmod((trimPathStart + trimPathOffset), 1.0f);
101 float end = len * fmod((trimPathEnd + trimPathOffset), 1.0f);
102
103 if (start > end) {
104 measure.getSegment(start, len, outPath, true);
105 if (end > 0) {
106 measure.getSegment(0, end, outPath, true);
107 }
108 } else {
109 measure.getSegment(start, end, outPath, true);
110 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800111}
112
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400113const SkPath& FullPath::getUpdatedPath(bool useStagingData, SkPath* tempStagingPath) {
114 if (!useStagingData && !mSkPathDirty && !mProperties.mTrimDirty) {
Doris Liu4bbc2932015-12-01 17:59:40 -0800115 return mTrimmedSkPath;
116 }
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400117 Path::getUpdatedPath(useStagingData, tempStagingPath);
John Reck1bcacfd2017-11-03 10:12:19 -0700118 SkPath* outPath;
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400119 if (useStagingData) {
120 SkPath inPath = *tempStagingPath;
121 applyTrim(tempStagingPath, inPath, mStagingProperties.getTrimPathStart(),
John Reck1bcacfd2017-11-03 10:12:19 -0700122 mStagingProperties.getTrimPathEnd(), mStagingProperties.getTrimPathOffset());
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400123 outPath = tempStagingPath;
Doris Liu4bbc2932015-12-01 17:59:40 -0800124 } else {
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400125 if (mProperties.getTrimPathStart() != 0.0f || mProperties.getTrimPathEnd() != 1.0f) {
126 mProperties.mTrimDirty = false;
127 applyTrim(&mTrimmedSkPath, mSkPath, mProperties.getTrimPathStart(),
John Reck1bcacfd2017-11-03 10:12:19 -0700128 mProperties.getTrimPathEnd(), mProperties.getTrimPathOffset());
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400129 outPath = &mTrimmedSkPath;
130 } else {
131 outPath = &mSkPath;
132 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800133 }
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400134 const FullPathProperties& properties = useStagingData ? mStagingProperties : mProperties;
John Reck1bcacfd2017-11-03 10:12:19 -0700135 bool setFillPath = properties.getFillGradient() != nullptr ||
136 properties.getFillColor() != SK_ColorTRANSPARENT;
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400137 if (setFillPath) {
Mike Reed6a8bf8e2019-12-03 13:01:07 -0500138 outPath->setFillType(static_cast<SkPathFillType>(properties.getFillType()));
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400139 }
140 return *outPath;
Doris Liu4bbc2932015-12-01 17:59:40 -0800141}
142
Doris Liu1d8e1942016-03-02 15:16:28 -0800143void FullPath::dump() {
144 Path::dump();
145 ALOGD("stroke width, color, alpha: %f, %d, %f, fill color, alpha: %d, %f",
John Reck1bcacfd2017-11-03 10:12:19 -0700146 mProperties.getStrokeWidth(), mProperties.getStrokeColor(), mProperties.getStrokeAlpha(),
147 mProperties.getFillColor(), mProperties.getFillAlpha());
Doris Liu1d8e1942016-03-02 15:16:28 -0800148}
149
Doris Liu4bbc2932015-12-01 17:59:40 -0800150inline SkColor applyAlpha(SkColor color, float alpha) {
151 int alphaBytes = SkColorGetA(color);
152 return SkColorSetA(color, alphaBytes * alpha);
153}
154
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400155void FullPath::draw(SkCanvas* outCanvas, bool useStagingData) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800156 const FullPathProperties& properties = useStagingData ? mStagingProperties : mProperties;
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400157 SkPath tempStagingPath;
158 const SkPath& renderPath = getUpdatedPath(useStagingData, &tempStagingPath);
Doris Liu1d8e1942016-03-02 15:16:28 -0800159
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800160 // Draw path's fill, if fill color or gradient is valid
161 bool needsFill = false;
Doris Liu1d8e1942016-03-02 15:16:28 -0800162 SkPaint paint;
163 if (properties.getFillGradient() != nullptr) {
164 paint.setColor(applyAlpha(SK_ColorBLACK, properties.getFillAlpha()));
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400165 paint.setShader(sk_sp<SkShader>(SkSafeRef(properties.getFillGradient())));
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800166 needsFill = true;
Doris Liu1d8e1942016-03-02 15:16:28 -0800167 } else if (properties.getFillColor() != SK_ColorTRANSPARENT) {
168 paint.setColor(applyAlpha(properties.getFillColor(), properties.getFillAlpha()));
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800169 needsFill = true;
Doris Liu4bbc2932015-12-01 17:59:40 -0800170 }
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800171
172 if (needsFill) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800173 paint.setStyle(SkPaint::Style::kFill_Style);
Doris Liu6b184d72017-12-04 16:31:07 -0800174 paint.setAntiAlias(mAntiAlias);
Doris Liu1d8e1942016-03-02 15:16:28 -0800175 outCanvas->drawPath(renderPath, paint);
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800176 }
177
Doris Liu1d8e1942016-03-02 15:16:28 -0800178 // Draw path's stroke, if stroke color or Gradient is valid
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800179 bool needsStroke = false;
Doris Liu1d8e1942016-03-02 15:16:28 -0800180 if (properties.getStrokeGradient() != nullptr) {
181 paint.setColor(applyAlpha(SK_ColorBLACK, properties.getStrokeAlpha()));
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400182 paint.setShader(sk_sp<SkShader>(SkSafeRef(properties.getStrokeGradient())));
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800183 needsStroke = true;
Doris Liu1d8e1942016-03-02 15:16:28 -0800184 } else if (properties.getStrokeColor() != SK_ColorTRANSPARENT) {
185 paint.setColor(applyAlpha(properties.getStrokeColor(), properties.getStrokeAlpha()));
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800186 needsStroke = true;
187 }
188 if (needsStroke) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800189 paint.setStyle(SkPaint::Style::kStroke_Style);
Doris Liu6b184d72017-12-04 16:31:07 -0800190 paint.setAntiAlias(mAntiAlias);
Doris Liu1d8e1942016-03-02 15:16:28 -0800191 paint.setStrokeJoin(SkPaint::Join(properties.getStrokeLineJoin()));
192 paint.setStrokeCap(SkPaint::Cap(properties.getStrokeLineCap()));
193 paint.setStrokeMiter(properties.getStrokeMiterLimit());
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400194 paint.setStrokeWidth(properties.getStrokeWidth());
Doris Liu1d8e1942016-03-02 15:16:28 -0800195 outCanvas->drawPath(renderPath, paint);
Doris Liu4bbc2932015-12-01 17:59:40 -0800196 }
197}
198
Doris Liu1d8e1942016-03-02 15:16:28 -0800199void FullPath::syncProperties() {
200 Path::syncProperties();
Doris Liu4bbc2932015-12-01 17:59:40 -0800201
Doris Liu1d8e1942016-03-02 15:16:28 -0800202 if (mStagingPropertiesDirty) {
203 mProperties.syncProperties(mStagingProperties);
Doris Liu4bbc2932015-12-01 17:59:40 -0800204 } else {
Doris Liu1d8e1942016-03-02 15:16:28 -0800205 // Update staging property with property values from animation.
206 mStagingProperties.syncProperties(mProperties);
Doris Liu4bbc2932015-12-01 17:59:40 -0800207 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800208 mStagingPropertiesDirty = false;
Doris Liu4bbc2932015-12-01 17:59:40 -0800209}
210
Doris Liu1d8e1942016-03-02 15:16:28 -0800211REQUIRE_COMPATIBLE_LAYOUT(FullPath::FullPathProperties::PrimitiveFields);
Doris Liu4bbc2932015-12-01 17:59:40 -0800212
213static_assert(sizeof(float) == sizeof(int32_t), "float is not the same size as int32_t");
214static_assert(sizeof(SkColor) == sizeof(int32_t), "SkColor is not the same size as int32_t");
215
Doris Liu1d8e1942016-03-02 15:16:28 -0800216bool FullPath::FullPathProperties::copyProperties(int8_t* outProperties, int length) const {
217 int propertyDataSize = sizeof(FullPathProperties::PrimitiveFields);
Doris Liu4bbc2932015-12-01 17:59:40 -0800218 if (length != propertyDataSize) {
219 LOG_ALWAYS_FATAL("Properties needs exactly %d bytes, a byte array of size %d is provided",
John Reck1bcacfd2017-11-03 10:12:19 -0700220 propertyDataSize, length);
Doris Liu4bbc2932015-12-01 17:59:40 -0800221 return false;
222 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800223
224 PrimitiveFields* out = reinterpret_cast<PrimitiveFields*>(outProperties);
225 *out = mPrimitiveFields;
Doris Liu4bbc2932015-12-01 17:59:40 -0800226 return true;
227}
228
Doris Liu1d8e1942016-03-02 15:16:28 -0800229void FullPath::FullPathProperties::setColorPropertyValue(int propertyId, int32_t value) {
Doris Liu766431a2016-02-04 22:17:11 +0000230 Property currentProperty = static_cast<Property>(propertyId);
Doris Liu1d8e1942016-03-02 15:16:28 -0800231 if (currentProperty == Property::strokeColor) {
232 setStrokeColor(value);
233 } else if (currentProperty == Property::fillColor) {
234 setFillColor(value);
Doris Liu766431a2016-02-04 22:17:11 +0000235 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700236 LOG_ALWAYS_FATAL(
237 "Error setting color property on FullPath: No valid property"
238 " with id: %d",
239 propertyId);
Doris Liu766431a2016-02-04 22:17:11 +0000240 }
241}
242
Doris Liu1d8e1942016-03-02 15:16:28 -0800243void FullPath::FullPathProperties::setPropertyValue(int propertyId, float value) {
Doris Liu766431a2016-02-04 22:17:11 +0000244 Property property = static_cast<Property>(propertyId);
245 switch (property) {
John Reck1bcacfd2017-11-03 10:12:19 -0700246 case Property::strokeWidth:
247 setStrokeWidth(value);
248 break;
249 case Property::strokeAlpha:
250 setStrokeAlpha(value);
251 break;
252 case Property::fillAlpha:
253 setFillAlpha(value);
254 break;
255 case Property::trimPathStart:
256 setTrimPathStart(value);
257 break;
258 case Property::trimPathEnd:
259 setTrimPathEnd(value);
260 break;
261 case Property::trimPathOffset:
262 setTrimPathOffset(value);
263 break;
264 default:
265 LOG_ALWAYS_FATAL("Invalid property id: %d for animation", propertyId);
266 break;
Doris Liu766431a2016-02-04 22:17:11 +0000267 }
268}
269
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400270void ClipPath::draw(SkCanvas* outCanvas, bool useStagingData) {
271 SkPath tempStagingPath;
272 outCanvas->clipPath(getUpdatedPath(useStagingData, &tempStagingPath));
Doris Liu4bbc2932015-12-01 17:59:40 -0800273}
274
275Group::Group(const Group& group) : Node(group) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800276 mStagingProperties.syncProperties(group.mStagingProperties);
Doris Liu4bbc2932015-12-01 17:59:40 -0800277}
278
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400279void Group::draw(SkCanvas* outCanvas, bool useStagingData) {
280 // Save the current clip and matrix information, which is local to this group.
281 SkAutoCanvasRestore saver(outCanvas, true);
282 // apply the current group's matrix to the canvas
Doris Liu4bbc2932015-12-01 17:59:40 -0800283 SkMatrix stackedMatrix;
Doris Liu1d8e1942016-03-02 15:16:28 -0800284 const GroupProperties& prop = useStagingData ? mStagingProperties : mProperties;
285 getLocalMatrix(&stackedMatrix, prop);
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400286 outCanvas->concat(stackedMatrix);
Doris Liu4bbc2932015-12-01 17:59:40 -0800287 // Draw the group tree in the same order as the XML file.
Doris Liuef062eb2016-02-04 16:16:27 -0800288 for (auto& child : mChildren) {
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400289 child->draw(outCanvas, useStagingData);
Doris Liu4bbc2932015-12-01 17:59:40 -0800290 }
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400291 // Restore the previous clip and matrix information.
Doris Liu4bbc2932015-12-01 17:59:40 -0800292}
293
294void Group::dump() {
295 ALOGD("Group %s has %zu children: ", mName.c_str(), mChildren.size());
Doris Liu1d8e1942016-03-02 15:16:28 -0800296 ALOGD("Group translateX, Y : %f, %f, scaleX, Y: %f, %f", mProperties.getTranslateX(),
John Reck1bcacfd2017-11-03 10:12:19 -0700297 mProperties.getTranslateY(), mProperties.getScaleX(), mProperties.getScaleY());
Doris Liu4bbc2932015-12-01 17:59:40 -0800298 for (size_t i = 0; i < mChildren.size(); i++) {
299 mChildren[i]->dump();
300 }
301}
302
Doris Liu1d8e1942016-03-02 15:16:28 -0800303void Group::syncProperties() {
304 // Copy over the dirty staging properties
305 if (mStagingPropertiesDirty) {
306 mProperties.syncProperties(mStagingProperties);
307 } else {
308 mStagingProperties.syncProperties(mProperties);
309 }
310 mStagingPropertiesDirty = false;
311 for (auto& child : mChildren) {
312 child->syncProperties();
313 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800314}
315
Doris Liu1d8e1942016-03-02 15:16:28 -0800316void Group::getLocalMatrix(SkMatrix* outMatrix, const GroupProperties& properties) {
Doris Liu4bbc2932015-12-01 17:59:40 -0800317 outMatrix->reset();
318 // TODO: use rotate(mRotate, mPivotX, mPivotY) and scale with pivot point, instead of
319 // translating to pivot for rotating and scaling, then translating back.
Doris Liu1d8e1942016-03-02 15:16:28 -0800320 outMatrix->postTranslate(-properties.getPivotX(), -properties.getPivotY());
321 outMatrix->postScale(properties.getScaleX(), properties.getScaleY());
322 outMatrix->postRotate(properties.getRotation(), 0, 0);
323 outMatrix->postTranslate(properties.getTranslateX() + properties.getPivotX(),
John Reck1bcacfd2017-11-03 10:12:19 -0700324 properties.getTranslateY() + properties.getPivotY());
Doris Liu4bbc2932015-12-01 17:59:40 -0800325}
326
327void Group::addChild(Node* child) {
Doris Liuef062eb2016-02-04 16:16:27 -0800328 mChildren.emplace_back(child);
Doris Liu1d8e1942016-03-02 15:16:28 -0800329 if (mPropertyChangedListener != nullptr) {
330 child->setPropertyChangedListener(mPropertyChangedListener);
331 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800332}
333
Doris Liu1d8e1942016-03-02 15:16:28 -0800334bool Group::GroupProperties::copyProperties(float* outProperties, int length) const {
335 int propertyCount = static_cast<int>(Property::count);
Doris Liu4bbc2932015-12-01 17:59:40 -0800336 if (length != propertyCount) {
337 LOG_ALWAYS_FATAL("Properties needs exactly %d bytes, a byte array of size %d is provided",
John Reck1bcacfd2017-11-03 10:12:19 -0700338 propertyCount, length);
Doris Liu4bbc2932015-12-01 17:59:40 -0800339 return false;
340 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800341
342 PrimitiveFields* out = reinterpret_cast<PrimitiveFields*>(outProperties);
343 *out = mPrimitiveFields;
Doris Liu4bbc2932015-12-01 17:59:40 -0800344 return true;
345}
346
Doris Liu766431a2016-02-04 22:17:11 +0000347// TODO: Consider animating the properties as float pointers
Doris Liu1d8e1942016-03-02 15:16:28 -0800348// Called on render thread
349float Group::GroupProperties::getPropertyValue(int propertyId) const {
Doris Liu766431a2016-02-04 22:17:11 +0000350 Property currentProperty = static_cast<Property>(propertyId);
351 switch (currentProperty) {
John Reck1bcacfd2017-11-03 10:12:19 -0700352 case Property::rotate:
353 return getRotation();
354 case Property::pivotX:
355 return getPivotX();
356 case Property::pivotY:
357 return getPivotY();
358 case Property::scaleX:
359 return getScaleX();
360 case Property::scaleY:
361 return getScaleY();
362 case Property::translateX:
363 return getTranslateX();
364 case Property::translateY:
365 return getTranslateY();
366 default:
367 LOG_ALWAYS_FATAL("Invalid property index: %d", propertyId);
368 return 0;
Doris Liu766431a2016-02-04 22:17:11 +0000369 }
370}
371
Doris Liu1d8e1942016-03-02 15:16:28 -0800372// Called on render thread
373void Group::GroupProperties::setPropertyValue(int propertyId, float value) {
Doris Liu766431a2016-02-04 22:17:11 +0000374 Property currentProperty = static_cast<Property>(propertyId);
375 switch (currentProperty) {
John Reck1bcacfd2017-11-03 10:12:19 -0700376 case Property::rotate:
377 setRotation(value);
378 break;
379 case Property::pivotX:
380 setPivotX(value);
381 break;
382 case Property::pivotY:
383 setPivotY(value);
384 break;
385 case Property::scaleX:
386 setScaleX(value);
387 break;
388 case Property::scaleY:
389 setScaleY(value);
390 break;
391 case Property::translateX:
392 setTranslateX(value);
393 break;
394 case Property::translateY:
395 setTranslateY(value);
396 break;
397 default:
398 LOG_ALWAYS_FATAL("Invalid property index: %d", propertyId);
Doris Liu766431a2016-02-04 22:17:11 +0000399 }
400}
401
402bool Group::isValidProperty(int propertyId) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800403 return GroupProperties::isValidProperty(propertyId);
404}
405
406bool Group::GroupProperties::isValidProperty(int propertyId) {
407 return propertyId >= 0 && propertyId < static_cast<int>(Property::count);
Doris Liu766431a2016-02-04 22:17:11 +0000408}
409
John Reck1bcacfd2017-11-03 10:12:19 -0700410int Tree::draw(Canvas* outCanvas, SkColorFilter* colorFilter, const SkRect& bounds,
411 bool needsMirroring, bool canReuseCache) {
Doris Liu4bbc2932015-12-01 17:59:40 -0800412 // The imageView can scale the canvas in different ways, in order to
413 // avoid blurry scaling, we have to draw into a bitmap with exact pixel
414 // size first. This bitmap size is determined by the bounds and the
415 // canvas scale.
Doris Liu1d8e1942016-03-02 15:16:28 -0800416 SkMatrix canvasMatrix;
417 outCanvas->getMatrix(&canvasMatrix);
Doris Liua0e61572015-12-29 14:57:49 -0800418 float canvasScaleX = 1.0f;
419 float canvasScaleY = 1.0f;
Doris Liu1d8e1942016-03-02 15:16:28 -0800420 if (canvasMatrix.getSkewX() == 0 && canvasMatrix.getSkewY() == 0) {
Doris Liua0e61572015-12-29 14:57:49 -0800421 // Only use the scale value when there's no skew or rotation in the canvas matrix.
Doris Liue410a352016-01-13 17:23:33 -0800422 // TODO: Add a cts test for drawing VD on a canvas with negative scaling factors.
Doris Liu1d8e1942016-03-02 15:16:28 -0800423 canvasScaleX = fabs(canvasMatrix.getScaleX());
424 canvasScaleY = fabs(canvasMatrix.getScaleY());
Doris Liua0e61572015-12-29 14:57:49 -0800425 }
John Reck1bcacfd2017-11-03 10:12:19 -0700426 int scaledWidth = (int)(bounds.width() * canvasScaleX);
427 int scaledHeight = (int)(bounds.height() * canvasScaleY);
Doris Liu4bbc2932015-12-01 17:59:40 -0800428 scaledWidth = std::min(Tree::MAX_CACHED_BITMAP_SIZE, scaledWidth);
429 scaledHeight = std::min(Tree::MAX_CACHED_BITMAP_SIZE, scaledHeight);
430
431 if (scaledWidth <= 0 || scaledHeight <= 0) {
Doris Liuf8d131c2016-04-29 18:41:29 -0700432 return 0;
Doris Liu4bbc2932015-12-01 17:59:40 -0800433 }
434
Doris Liu1d8e1942016-03-02 15:16:28 -0800435 mStagingProperties.setScaledSize(scaledWidth, scaledHeight);
Florin Malita777bf852016-02-03 10:48:55 -0500436 int saveCount = outCanvas->save(SaveFlags::MatrixClip);
Doris Liu1d8e1942016-03-02 15:16:28 -0800437 outCanvas->translate(bounds.fLeft, bounds.fTop);
Doris Liu4bbc2932015-12-01 17:59:40 -0800438
439 // Handle RTL mirroring.
440 if (needsMirroring) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800441 outCanvas->translate(bounds.width(), 0);
Doris Liu4bbc2932015-12-01 17:59:40 -0800442 outCanvas->scale(-1.0f, 1.0f);
443 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800444 mStagingProperties.setColorFilter(colorFilter);
Doris Liu4bbc2932015-12-01 17:59:40 -0800445
446 // At this point, canvas has been translated to the right position.
447 // And we use this bound for the destination rect for the drawBitmap, so
448 // we offset to (0, 0);
Doris Liu1d8e1942016-03-02 15:16:28 -0800449 SkRect tmpBounds = bounds;
450 tmpBounds.offsetTo(0, 0);
451 mStagingProperties.setBounds(tmpBounds);
Doris Liu766431a2016-02-04 22:17:11 +0000452 outCanvas->drawVectorDrawable(this);
Doris Liu4bbc2932015-12-01 17:59:40 -0800453 outCanvas->restoreToCount(saveCount);
Doris Liuf8d131c2016-04-29 18:41:29 -0700454 return scaledWidth * scaledHeight;
Doris Liu4bbc2932015-12-01 17:59:40 -0800455}
456
Doris Liu1d8e1942016-03-02 15:16:28 -0800457void Tree::drawStaging(Canvas* outCanvas) {
John Reck1bcacfd2017-11-03 10:12:19 -0700458 bool redrawNeeded = allocateBitmapIfNeeded(mStagingCache, mStagingProperties.getScaledWidth(),
459 mStagingProperties.getScaledHeight());
Doris Liu1d8e1942016-03-02 15:16:28 -0800460 // draw bitmap cache
461 if (redrawNeeded || mStagingCache.dirty) {
sergeyvfc9999502016-10-17 13:07:38 -0700462 updateBitmapCache(*mStagingCache.bitmap, true);
Doris Liu1d8e1942016-03-02 15:16:28 -0800463 mStagingCache.dirty = false;
Doris Liu4bbc2932015-12-01 17:59:40 -0800464 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800465
Mike Reedc2dbc032019-07-25 12:28:29 -0400466 SkPaint skp;
467 getPaintFor(&skp, mStagingProperties);
468 Paint paint;
469 paint.setFilterQuality(skp.getFilterQuality());
470 paint.setColorFilter(skp.refColorFilter());
471 paint.setAlpha(skp.getAlpha());
John Reck1bcacfd2017-11-03 10:12:19 -0700472 outCanvas->drawBitmap(*mStagingCache.bitmap, 0, 0, mStagingCache.bitmap->width(),
473 mStagingCache.bitmap->height(), mStagingProperties.getBounds().left(),
474 mStagingProperties.getBounds().top(),
475 mStagingProperties.getBounds().right(),
John Reck08ee8152018-09-20 16:27:46 -0700476 mStagingProperties.getBounds().bottom(), &paint);
Doris Liu1d8e1942016-03-02 15:16:28 -0800477}
478
Stan Iliev52e43922019-08-06 15:59:44 -0400479void Tree::getPaintFor(SkPaint* outPaint, const TreeProperties& prop) const {
Stan Iliev038fc372018-07-30 18:31:46 -0400480 // HWUI always draws VD with bilinear filtering.
481 outPaint->setFilterQuality(kLow_SkFilterQuality);
Doris Liu7cc6ec22018-10-02 16:15:57 -0700482 if (prop.getColorFilter() != nullptr) {
John Reck08ee8152018-09-20 16:27:46 -0700483 outPaint->setColorFilter(sk_ref_sp(prop.getColorFilter()));
Doris Liu1d8e1942016-03-02 15:16:28 -0800484 }
Doris Liu7cc6ec22018-10-02 16:15:57 -0700485 outPaint->setAlpha(prop.getRootAlpha() * 255);
Doris Liu4bbc2932015-12-01 17:59:40 -0800486}
487
sergeyvfc9999502016-10-17 13:07:38 -0700488Bitmap& Tree::getBitmapUpdateIfDirty() {
489 bool redrawNeeded = allocateBitmapIfNeeded(mCache, mProperties.getScaledWidth(),
John Reck1bcacfd2017-11-03 10:12:19 -0700490 mProperties.getScaledHeight());
Doris Liu1d8e1942016-03-02 15:16:28 -0800491 if (redrawNeeded || mCache.dirty) {
sergeyvfc9999502016-10-17 13:07:38 -0700492 updateBitmapCache(*mCache.bitmap, false);
Doris Liu1d8e1942016-03-02 15:16:28 -0800493 mCache.dirty = false;
494 }
sergeyvfc9999502016-10-17 13:07:38 -0700495 return *mCache.bitmap;
Doris Liu4bbc2932015-12-01 17:59:40 -0800496}
497
John Reck08ee8152018-09-20 16:27:46 -0700498void Tree::draw(SkCanvas* canvas, const SkRect& bounds, const SkPaint& inPaint) {
Leon Scroggins III6c5864c2019-04-03 15:09:25 -0400499 if (canvas->quickReject(bounds)) {
500 // The RenderNode is on screen, but the AVD is not.
501 return;
502 }
503
John Reck08ee8152018-09-20 16:27:46 -0700504 // Update the paint for any animatable properties
505 SkPaint paint = inPaint;
506 paint.setAlpha(mProperties.getRootAlpha() * 255);
507
John Reck83161dc2019-10-04 14:48:27 -0700508 Bitmap& bitmap = getBitmapUpdateIfDirty();
509 SkBitmap skiaBitmap;
510 bitmap.getSkBitmap(&skiaBitmap);
John Reck5cca8f22018-12-10 17:06:22 -0800511
John Reck83161dc2019-10-04 14:48:27 -0700512 int scaledWidth = SkScalarCeilToInt(mProperties.getScaledWidth());
513 int scaledHeight = SkScalarCeilToInt(mProperties.getScaledHeight());
514 canvas->drawBitmapRect(skiaBitmap, SkRect::MakeWH(scaledWidth, scaledHeight), bounds,
515 &paint, SkCanvas::kFast_SrcRectConstraint);
Stan Iliev23c38a92017-03-23 00:12:50 -0400516}
517
sergeyvfc9999502016-10-17 13:07:38 -0700518void Tree::updateBitmapCache(Bitmap& bitmap, bool useStagingData) {
519 SkBitmap outCache;
520 bitmap.getSkBitmap(&outCache);
ztenghuicf0c41d2017-09-13 10:32:50 -0700521 int cacheWidth = outCache.width();
522 int cacheHeight = outCache.height();
523 ATRACE_FORMAT("VectorDrawable repaint %dx%d", cacheWidth, cacheHeight);
sergeyvfc9999502016-10-17 13:07:38 -0700524 outCache.eraseColor(SK_ColorTRANSPARENT);
525 SkCanvas outCanvas(outCache);
John Reck1bcacfd2017-11-03 10:12:19 -0700526 float viewportWidth =
527 useStagingData ? mStagingProperties.getViewportWidth() : mProperties.getViewportWidth();
528 float viewportHeight = useStagingData ? mStagingProperties.getViewportHeight()
529 : mProperties.getViewportHeight();
ztenghuicf0c41d2017-09-13 10:32:50 -0700530 float scaleX = cacheWidth / viewportWidth;
531 float scaleY = cacheHeight / viewportHeight;
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400532 outCanvas.scale(scaleX, scaleY);
533 mRootNode->draw(&outCanvas, useStagingData);
Doris Liu1d8e1942016-03-02 15:16:28 -0800534}
535
sergeyvfc9999502016-10-17 13:07:38 -0700536bool Tree::allocateBitmapIfNeeded(Cache& cache, int width, int height) {
537 if (!canReuseBitmap(cache.bitmap.get(), width, height)) {
Leon Scroggins III12497572019-01-31 10:06:12 -0500538 SkImageInfo info = SkImageInfo::MakeN32(width, height, kPremul_SkAlphaType);
sergeyvfc9999502016-10-17 13:07:38 -0700539 cache.bitmap = Bitmap::allocateHeapBitmap(info);
Doris Liu1d8e1942016-03-02 15:16:28 -0800540 return true;
Doris Liu4bbc2932015-12-01 17:59:40 -0800541 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800542 return false;
Doris Liu4bbc2932015-12-01 17:59:40 -0800543}
544
sergeyvfc9999502016-10-17 13:07:38 -0700545bool Tree::canReuseBitmap(Bitmap* bitmap, int width, int height) {
Teng-Hui Zhu037fc182016-11-16 10:29:39 -0800546 return bitmap && width <= bitmap->width() && height <= bitmap->height();
Doris Liu1d8e1942016-03-02 15:16:28 -0800547}
548
549void Tree::onPropertyChanged(TreeProperties* prop) {
550 if (prop == &mStagingProperties) {
551 mStagingCache.dirty = true;
552 } else {
553 mCache.dirty = true;
554 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800555}
556
John Reck08ee8152018-09-20 16:27:46 -0700557class MinMaxAverage {
558public:
559 void add(float sample) {
560 if (mCount == 0) {
561 mMin = sample;
562 mMax = sample;
563 } else {
564 mMin = std::min(mMin, sample);
565 mMax = std::max(mMax, sample);
566 }
567 mTotal += sample;
568 mCount++;
569 }
570
571 float average() { return mTotal / mCount; }
572
573 float min() { return mMin; }
574
575 float max() { return mMax; }
576
577 float delta() { return mMax - mMin; }
578
579private:
580 float mMin = 0.0f;
581 float mMax = 0.0f;
582 float mTotal = 0.0f;
583 int mCount = 0;
584};
585
586BitmapPalette Tree::computePalette() {
587 // TODO Cache this and share the code with Bitmap.cpp
588
589 ATRACE_CALL();
590
591 // TODO: This calculation of converting to HSV & tracking min/max is probably overkill
592 // Experiment with something simpler since we just want to figure out if it's "color-ful"
593 // and then the average perceptual lightness.
594
595 MinMaxAverage hue, saturation, value;
596 int sampledCount = 0;
597
598 // Sample a grid of 100 pixels to get an overall estimation of the colors in play
599 mRootNode->forEachFillColor([&](SkColor color) {
600 if (SkColorGetA(color) < 75) {
601 return;
602 }
603 sampledCount++;
604 float hsv[3];
605 SkColorToHSV(color, hsv);
606 hue.add(hsv[0]);
607 saturation.add(hsv[1]);
608 value.add(hsv[2]);
609 });
610
611 if (sampledCount == 0) {
612 ALOGV("VectorDrawable is mostly translucent");
613 return BitmapPalette::Unknown;
614 }
615
616 ALOGV("samples = %d, hue [min = %f, max = %f, avg = %f]; saturation [min = %f, max = %f, avg = "
617 "%f]; value [min = %f, max = %f, avg = %f]",
618 sampledCount, hue.min(), hue.max(), hue.average(), saturation.min(), saturation.max(),
619 saturation.average(), value.min(), value.max(), value.average());
620
621 if (hue.delta() <= 20 && saturation.delta() <= .1f) {
622 if (value.average() >= .5f) {
623 return BitmapPalette::Light;
624 } else {
625 return BitmapPalette::Dark;
626 }
627 }
628 return BitmapPalette::Unknown;
629}
630
Chris Blume7b8a8082018-11-30 15:51:58 -0800631} // namespace VectorDrawable
Doris Liu4bbc2932015-12-01 17:59:40 -0800632
Chris Blume7b8a8082018-11-30 15:51:58 -0800633} // namespace uirenderer
634} // namespace android