blob: 61403aa93c97513ceff86c0b25658eb2aa89391e [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) {
138 SkPath::FillType ft = static_cast<SkPath::FillType>(properties.getFillType());
139 outPath->setFillType(ft);
140 }
141 return *outPath;
Doris Liu4bbc2932015-12-01 17:59:40 -0800142}
143
Doris Liu1d8e1942016-03-02 15:16:28 -0800144void FullPath::dump() {
145 Path::dump();
146 ALOGD("stroke width, color, alpha: %f, %d, %f, fill color, alpha: %d, %f",
John Reck1bcacfd2017-11-03 10:12:19 -0700147 mProperties.getStrokeWidth(), mProperties.getStrokeColor(), mProperties.getStrokeAlpha(),
148 mProperties.getFillColor(), mProperties.getFillAlpha());
Doris Liu1d8e1942016-03-02 15:16:28 -0800149}
150
Doris Liu4bbc2932015-12-01 17:59:40 -0800151inline SkColor applyAlpha(SkColor color, float alpha) {
152 int alphaBytes = SkColorGetA(color);
153 return SkColorSetA(color, alphaBytes * alpha);
154}
155
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400156void FullPath::draw(SkCanvas* outCanvas, bool useStagingData) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800157 const FullPathProperties& properties = useStagingData ? mStagingProperties : mProperties;
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400158 SkPath tempStagingPath;
159 const SkPath& renderPath = getUpdatedPath(useStagingData, &tempStagingPath);
Doris Liu1d8e1942016-03-02 15:16:28 -0800160
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800161 // Draw path's fill, if fill color or gradient is valid
162 bool needsFill = false;
Doris Liu1d8e1942016-03-02 15:16:28 -0800163 SkPaint paint;
164 if (properties.getFillGradient() != nullptr) {
165 paint.setColor(applyAlpha(SK_ColorBLACK, properties.getFillAlpha()));
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400166 paint.setShader(sk_sp<SkShader>(SkSafeRef(properties.getFillGradient())));
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800167 needsFill = true;
Doris Liu1d8e1942016-03-02 15:16:28 -0800168 } else if (properties.getFillColor() != SK_ColorTRANSPARENT) {
169 paint.setColor(applyAlpha(properties.getFillColor(), properties.getFillAlpha()));
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800170 needsFill = true;
Doris Liu4bbc2932015-12-01 17:59:40 -0800171 }
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800172
173 if (needsFill) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800174 paint.setStyle(SkPaint::Style::kFill_Style);
Doris Liu6b184d72017-12-04 16:31:07 -0800175 paint.setAntiAlias(mAntiAlias);
Doris Liu1d8e1942016-03-02 15:16:28 -0800176 outCanvas->drawPath(renderPath, paint);
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800177 }
178
Doris Liu1d8e1942016-03-02 15:16:28 -0800179 // Draw path's stroke, if stroke color or Gradient is valid
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800180 bool needsStroke = false;
Doris Liu1d8e1942016-03-02 15:16:28 -0800181 if (properties.getStrokeGradient() != nullptr) {
182 paint.setColor(applyAlpha(SK_ColorBLACK, properties.getStrokeAlpha()));
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400183 paint.setShader(sk_sp<SkShader>(SkSafeRef(properties.getStrokeGradient())));
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800184 needsStroke = true;
Doris Liu1d8e1942016-03-02 15:16:28 -0800185 } else if (properties.getStrokeColor() != SK_ColorTRANSPARENT) {
186 paint.setColor(applyAlpha(properties.getStrokeColor(), properties.getStrokeAlpha()));
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800187 needsStroke = true;
188 }
189 if (needsStroke) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800190 paint.setStyle(SkPaint::Style::kStroke_Style);
Doris Liu6b184d72017-12-04 16:31:07 -0800191 paint.setAntiAlias(mAntiAlias);
Doris Liu1d8e1942016-03-02 15:16:28 -0800192 paint.setStrokeJoin(SkPaint::Join(properties.getStrokeLineJoin()));
193 paint.setStrokeCap(SkPaint::Cap(properties.getStrokeLineCap()));
194 paint.setStrokeMiter(properties.getStrokeMiterLimit());
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400195 paint.setStrokeWidth(properties.getStrokeWidth());
Doris Liu1d8e1942016-03-02 15:16:28 -0800196 outCanvas->drawPath(renderPath, paint);
Doris Liu4bbc2932015-12-01 17:59:40 -0800197 }
198}
199
Doris Liu1d8e1942016-03-02 15:16:28 -0800200void FullPath::syncProperties() {
201 Path::syncProperties();
Doris Liu4bbc2932015-12-01 17:59:40 -0800202
Doris Liu1d8e1942016-03-02 15:16:28 -0800203 if (mStagingPropertiesDirty) {
204 mProperties.syncProperties(mStagingProperties);
Doris Liu4bbc2932015-12-01 17:59:40 -0800205 } else {
Doris Liu1d8e1942016-03-02 15:16:28 -0800206 // Update staging property with property values from animation.
207 mStagingProperties.syncProperties(mProperties);
Doris Liu4bbc2932015-12-01 17:59:40 -0800208 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800209 mStagingPropertiesDirty = false;
Doris Liu4bbc2932015-12-01 17:59:40 -0800210}
211
Doris Liu1d8e1942016-03-02 15:16:28 -0800212REQUIRE_COMPATIBLE_LAYOUT(FullPath::FullPathProperties::PrimitiveFields);
Doris Liu4bbc2932015-12-01 17:59:40 -0800213
214static_assert(sizeof(float) == sizeof(int32_t), "float is not the same size as int32_t");
215static_assert(sizeof(SkColor) == sizeof(int32_t), "SkColor is not the same size as int32_t");
216
Doris Liu1d8e1942016-03-02 15:16:28 -0800217bool FullPath::FullPathProperties::copyProperties(int8_t* outProperties, int length) const {
218 int propertyDataSize = sizeof(FullPathProperties::PrimitiveFields);
Doris Liu4bbc2932015-12-01 17:59:40 -0800219 if (length != propertyDataSize) {
220 LOG_ALWAYS_FATAL("Properties needs exactly %d bytes, a byte array of size %d is provided",
John Reck1bcacfd2017-11-03 10:12:19 -0700221 propertyDataSize, length);
Doris Liu4bbc2932015-12-01 17:59:40 -0800222 return false;
223 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800224
225 PrimitiveFields* out = reinterpret_cast<PrimitiveFields*>(outProperties);
226 *out = mPrimitiveFields;
Doris Liu4bbc2932015-12-01 17:59:40 -0800227 return true;
228}
229
Doris Liu1d8e1942016-03-02 15:16:28 -0800230void FullPath::FullPathProperties::setColorPropertyValue(int propertyId, int32_t value) {
Doris Liu766431a2016-02-04 22:17:11 +0000231 Property currentProperty = static_cast<Property>(propertyId);
Doris Liu1d8e1942016-03-02 15:16:28 -0800232 if (currentProperty == Property::strokeColor) {
233 setStrokeColor(value);
234 } else if (currentProperty == Property::fillColor) {
235 setFillColor(value);
Doris Liu766431a2016-02-04 22:17:11 +0000236 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700237 LOG_ALWAYS_FATAL(
238 "Error setting color property on FullPath: No valid property"
239 " with id: %d",
240 propertyId);
Doris Liu766431a2016-02-04 22:17:11 +0000241 }
242}
243
Doris Liu1d8e1942016-03-02 15:16:28 -0800244void FullPath::FullPathProperties::setPropertyValue(int propertyId, float value) {
Doris Liu766431a2016-02-04 22:17:11 +0000245 Property property = static_cast<Property>(propertyId);
246 switch (property) {
John Reck1bcacfd2017-11-03 10:12:19 -0700247 case Property::strokeWidth:
248 setStrokeWidth(value);
249 break;
250 case Property::strokeAlpha:
251 setStrokeAlpha(value);
252 break;
253 case Property::fillAlpha:
254 setFillAlpha(value);
255 break;
256 case Property::trimPathStart:
257 setTrimPathStart(value);
258 break;
259 case Property::trimPathEnd:
260 setTrimPathEnd(value);
261 break;
262 case Property::trimPathOffset:
263 setTrimPathOffset(value);
264 break;
265 default:
266 LOG_ALWAYS_FATAL("Invalid property id: %d for animation", propertyId);
267 break;
Doris Liu766431a2016-02-04 22:17:11 +0000268 }
269}
270
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400271void ClipPath::draw(SkCanvas* outCanvas, bool useStagingData) {
272 SkPath tempStagingPath;
273 outCanvas->clipPath(getUpdatedPath(useStagingData, &tempStagingPath));
Doris Liu4bbc2932015-12-01 17:59:40 -0800274}
275
276Group::Group(const Group& group) : Node(group) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800277 mStagingProperties.syncProperties(group.mStagingProperties);
Doris Liu4bbc2932015-12-01 17:59:40 -0800278}
279
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400280void Group::draw(SkCanvas* outCanvas, bool useStagingData) {
281 // Save the current clip and matrix information, which is local to this group.
282 SkAutoCanvasRestore saver(outCanvas, true);
283 // apply the current group's matrix to the canvas
Doris Liu4bbc2932015-12-01 17:59:40 -0800284 SkMatrix stackedMatrix;
Doris Liu1d8e1942016-03-02 15:16:28 -0800285 const GroupProperties& prop = useStagingData ? mStagingProperties : mProperties;
286 getLocalMatrix(&stackedMatrix, prop);
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400287 outCanvas->concat(stackedMatrix);
Doris Liu4bbc2932015-12-01 17:59:40 -0800288 // Draw the group tree in the same order as the XML file.
Doris Liuef062eb2016-02-04 16:16:27 -0800289 for (auto& child : mChildren) {
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400290 child->draw(outCanvas, useStagingData);
Doris Liu4bbc2932015-12-01 17:59:40 -0800291 }
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400292 // Restore the previous clip and matrix information.
Doris Liu4bbc2932015-12-01 17:59:40 -0800293}
294
295void Group::dump() {
296 ALOGD("Group %s has %zu children: ", mName.c_str(), mChildren.size());
Doris Liu1d8e1942016-03-02 15:16:28 -0800297 ALOGD("Group translateX, Y : %f, %f, scaleX, Y: %f, %f", mProperties.getTranslateX(),
John Reck1bcacfd2017-11-03 10:12:19 -0700298 mProperties.getTranslateY(), mProperties.getScaleX(), mProperties.getScaleY());
Doris Liu4bbc2932015-12-01 17:59:40 -0800299 for (size_t i = 0; i < mChildren.size(); i++) {
300 mChildren[i]->dump();
301 }
302}
303
Doris Liu1d8e1942016-03-02 15:16:28 -0800304void Group::syncProperties() {
305 // Copy over the dirty staging properties
306 if (mStagingPropertiesDirty) {
307 mProperties.syncProperties(mStagingProperties);
308 } else {
309 mStagingProperties.syncProperties(mProperties);
310 }
311 mStagingPropertiesDirty = false;
312 for (auto& child : mChildren) {
313 child->syncProperties();
314 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800315}
316
Doris Liu1d8e1942016-03-02 15:16:28 -0800317void Group::getLocalMatrix(SkMatrix* outMatrix, const GroupProperties& properties) {
Doris Liu4bbc2932015-12-01 17:59:40 -0800318 outMatrix->reset();
319 // TODO: use rotate(mRotate, mPivotX, mPivotY) and scale with pivot point, instead of
320 // translating to pivot for rotating and scaling, then translating back.
Doris Liu1d8e1942016-03-02 15:16:28 -0800321 outMatrix->postTranslate(-properties.getPivotX(), -properties.getPivotY());
322 outMatrix->postScale(properties.getScaleX(), properties.getScaleY());
323 outMatrix->postRotate(properties.getRotation(), 0, 0);
324 outMatrix->postTranslate(properties.getTranslateX() + properties.getPivotX(),
John Reck1bcacfd2017-11-03 10:12:19 -0700325 properties.getTranslateY() + properties.getPivotY());
Doris Liu4bbc2932015-12-01 17:59:40 -0800326}
327
328void Group::addChild(Node* child) {
Doris Liuef062eb2016-02-04 16:16:27 -0800329 mChildren.emplace_back(child);
Doris Liu1d8e1942016-03-02 15:16:28 -0800330 if (mPropertyChangedListener != nullptr) {
331 child->setPropertyChangedListener(mPropertyChangedListener);
332 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800333}
334
Doris Liu1d8e1942016-03-02 15:16:28 -0800335bool Group::GroupProperties::copyProperties(float* outProperties, int length) const {
336 int propertyCount = static_cast<int>(Property::count);
Doris Liu4bbc2932015-12-01 17:59:40 -0800337 if (length != propertyCount) {
338 LOG_ALWAYS_FATAL("Properties needs exactly %d bytes, a byte array of size %d is provided",
John Reck1bcacfd2017-11-03 10:12:19 -0700339 propertyCount, length);
Doris Liu4bbc2932015-12-01 17:59:40 -0800340 return false;
341 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800342
343 PrimitiveFields* out = reinterpret_cast<PrimitiveFields*>(outProperties);
344 *out = mPrimitiveFields;
Doris Liu4bbc2932015-12-01 17:59:40 -0800345 return true;
346}
347
Doris Liu766431a2016-02-04 22:17:11 +0000348// TODO: Consider animating the properties as float pointers
Doris Liu1d8e1942016-03-02 15:16:28 -0800349// Called on render thread
350float Group::GroupProperties::getPropertyValue(int propertyId) const {
Doris Liu766431a2016-02-04 22:17:11 +0000351 Property currentProperty = static_cast<Property>(propertyId);
352 switch (currentProperty) {
John Reck1bcacfd2017-11-03 10:12:19 -0700353 case Property::rotate:
354 return getRotation();
355 case Property::pivotX:
356 return getPivotX();
357 case Property::pivotY:
358 return getPivotY();
359 case Property::scaleX:
360 return getScaleX();
361 case Property::scaleY:
362 return getScaleY();
363 case Property::translateX:
364 return getTranslateX();
365 case Property::translateY:
366 return getTranslateY();
367 default:
368 LOG_ALWAYS_FATAL("Invalid property index: %d", propertyId);
369 return 0;
Doris Liu766431a2016-02-04 22:17:11 +0000370 }
371}
372
Doris Liu1d8e1942016-03-02 15:16:28 -0800373// Called on render thread
374void Group::GroupProperties::setPropertyValue(int propertyId, float value) {
Doris Liu766431a2016-02-04 22:17:11 +0000375 Property currentProperty = static_cast<Property>(propertyId);
376 switch (currentProperty) {
John Reck1bcacfd2017-11-03 10:12:19 -0700377 case Property::rotate:
378 setRotation(value);
379 break;
380 case Property::pivotX:
381 setPivotX(value);
382 break;
383 case Property::pivotY:
384 setPivotY(value);
385 break;
386 case Property::scaleX:
387 setScaleX(value);
388 break;
389 case Property::scaleY:
390 setScaleY(value);
391 break;
392 case Property::translateX:
393 setTranslateX(value);
394 break;
395 case Property::translateY:
396 setTranslateY(value);
397 break;
398 default:
399 LOG_ALWAYS_FATAL("Invalid property index: %d", propertyId);
Doris Liu766431a2016-02-04 22:17:11 +0000400 }
401}
402
403bool Group::isValidProperty(int propertyId) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800404 return GroupProperties::isValidProperty(propertyId);
405}
406
407bool Group::GroupProperties::isValidProperty(int propertyId) {
408 return propertyId >= 0 && propertyId < static_cast<int>(Property::count);
Doris Liu766431a2016-02-04 22:17:11 +0000409}
410
John Reck1bcacfd2017-11-03 10:12:19 -0700411int Tree::draw(Canvas* outCanvas, SkColorFilter* colorFilter, const SkRect& bounds,
412 bool needsMirroring, bool canReuseCache) {
Doris Liu4bbc2932015-12-01 17:59:40 -0800413 // The imageView can scale the canvas in different ways, in order to
414 // avoid blurry scaling, we have to draw into a bitmap with exact pixel
415 // size first. This bitmap size is determined by the bounds and the
416 // canvas scale.
Doris Liu1d8e1942016-03-02 15:16:28 -0800417 SkMatrix canvasMatrix;
418 outCanvas->getMatrix(&canvasMatrix);
Doris Liua0e61572015-12-29 14:57:49 -0800419 float canvasScaleX = 1.0f;
420 float canvasScaleY = 1.0f;
Doris Liu1d8e1942016-03-02 15:16:28 -0800421 if (canvasMatrix.getSkewX() == 0 && canvasMatrix.getSkewY() == 0) {
Doris Liua0e61572015-12-29 14:57:49 -0800422 // Only use the scale value when there's no skew or rotation in the canvas matrix.
Doris Liue410a352016-01-13 17:23:33 -0800423 // TODO: Add a cts test for drawing VD on a canvas with negative scaling factors.
Doris Liu1d8e1942016-03-02 15:16:28 -0800424 canvasScaleX = fabs(canvasMatrix.getScaleX());
425 canvasScaleY = fabs(canvasMatrix.getScaleY());
Doris Liua0e61572015-12-29 14:57:49 -0800426 }
John Reck1bcacfd2017-11-03 10:12:19 -0700427 int scaledWidth = (int)(bounds.width() * canvasScaleX);
428 int scaledHeight = (int)(bounds.height() * canvasScaleY);
Doris Liu4bbc2932015-12-01 17:59:40 -0800429 scaledWidth = std::min(Tree::MAX_CACHED_BITMAP_SIZE, scaledWidth);
430 scaledHeight = std::min(Tree::MAX_CACHED_BITMAP_SIZE, scaledHeight);
431
432 if (scaledWidth <= 0 || scaledHeight <= 0) {
Doris Liuf8d131c2016-04-29 18:41:29 -0700433 return 0;
Doris Liu4bbc2932015-12-01 17:59:40 -0800434 }
435
Doris Liu1d8e1942016-03-02 15:16:28 -0800436 mStagingProperties.setScaledSize(scaledWidth, scaledHeight);
Florin Malita777bf852016-02-03 10:48:55 -0500437 int saveCount = outCanvas->save(SaveFlags::MatrixClip);
Doris Liu1d8e1942016-03-02 15:16:28 -0800438 outCanvas->translate(bounds.fLeft, bounds.fTop);
Doris Liu4bbc2932015-12-01 17:59:40 -0800439
440 // Handle RTL mirroring.
441 if (needsMirroring) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800442 outCanvas->translate(bounds.width(), 0);
Doris Liu4bbc2932015-12-01 17:59:40 -0800443 outCanvas->scale(-1.0f, 1.0f);
444 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800445 mStagingProperties.setColorFilter(colorFilter);
Doris Liu4bbc2932015-12-01 17:59:40 -0800446
447 // At this point, canvas has been translated to the right position.
448 // And we use this bound for the destination rect for the drawBitmap, so
449 // we offset to (0, 0);
Doris Liu1d8e1942016-03-02 15:16:28 -0800450 SkRect tmpBounds = bounds;
451 tmpBounds.offsetTo(0, 0);
452 mStagingProperties.setBounds(tmpBounds);
Doris Liu766431a2016-02-04 22:17:11 +0000453 outCanvas->drawVectorDrawable(this);
Doris Liu4bbc2932015-12-01 17:59:40 -0800454 outCanvas->restoreToCount(saveCount);
Doris Liuf8d131c2016-04-29 18:41:29 -0700455 return scaledWidth * scaledHeight;
Doris Liu4bbc2932015-12-01 17:59:40 -0800456}
457
Doris Liu1d8e1942016-03-02 15:16:28 -0800458void Tree::drawStaging(Canvas* outCanvas) {
John Reck1bcacfd2017-11-03 10:12:19 -0700459 bool redrawNeeded = allocateBitmapIfNeeded(mStagingCache, mStagingProperties.getScaledWidth(),
460 mStagingProperties.getScaledHeight());
Doris Liu1d8e1942016-03-02 15:16:28 -0800461 // draw bitmap cache
462 if (redrawNeeded || mStagingCache.dirty) {
sergeyvfc9999502016-10-17 13:07:38 -0700463 updateBitmapCache(*mStagingCache.bitmap, true);
Doris Liu1d8e1942016-03-02 15:16:28 -0800464 mStagingCache.dirty = false;
Doris Liu4bbc2932015-12-01 17:59:40 -0800465 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800466
Mike Reedc2dbc032019-07-25 12:28:29 -0400467 SkPaint skp;
468 getPaintFor(&skp, mStagingProperties);
469 Paint paint;
470 paint.setFilterQuality(skp.getFilterQuality());
471 paint.setColorFilter(skp.refColorFilter());
472 paint.setAlpha(skp.getAlpha());
John Reck1bcacfd2017-11-03 10:12:19 -0700473 outCanvas->drawBitmap(*mStagingCache.bitmap, 0, 0, mStagingCache.bitmap->width(),
474 mStagingCache.bitmap->height(), mStagingProperties.getBounds().left(),
475 mStagingProperties.getBounds().top(),
476 mStagingProperties.getBounds().right(),
John Reck08ee8152018-09-20 16:27:46 -0700477 mStagingProperties.getBounds().bottom(), &paint);
Doris Liu1d8e1942016-03-02 15:16:28 -0800478}
479
Stan Iliev52e43922019-08-06 15:59:44 -0400480void Tree::getPaintFor(SkPaint* outPaint, const TreeProperties& prop) const {
Stan Iliev038fc372018-07-30 18:31:46 -0400481 // HWUI always draws VD with bilinear filtering.
482 outPaint->setFilterQuality(kLow_SkFilterQuality);
Doris Liu7cc6ec22018-10-02 16:15:57 -0700483 if (prop.getColorFilter() != nullptr) {
John Reck08ee8152018-09-20 16:27:46 -0700484 outPaint->setColorFilter(sk_ref_sp(prop.getColorFilter()));
Doris Liu1d8e1942016-03-02 15:16:28 -0800485 }
Doris Liu7cc6ec22018-10-02 16:15:57 -0700486 outPaint->setAlpha(prop.getRootAlpha() * 255);
Doris Liu4bbc2932015-12-01 17:59:40 -0800487}
488
sergeyvfc9999502016-10-17 13:07:38 -0700489Bitmap& Tree::getBitmapUpdateIfDirty() {
490 bool redrawNeeded = allocateBitmapIfNeeded(mCache, mProperties.getScaledWidth(),
John Reck1bcacfd2017-11-03 10:12:19 -0700491 mProperties.getScaledHeight());
Doris Liu1d8e1942016-03-02 15:16:28 -0800492 if (redrawNeeded || mCache.dirty) {
sergeyvfc9999502016-10-17 13:07:38 -0700493 updateBitmapCache(*mCache.bitmap, false);
Doris Liu1d8e1942016-03-02 15:16:28 -0800494 mCache.dirty = false;
495 }
sergeyvfc9999502016-10-17 13:07:38 -0700496 return *mCache.bitmap;
Doris Liu4bbc2932015-12-01 17:59:40 -0800497}
498
Stan Iliev3310fb12017-03-23 16:56:51 -0400499void Tree::updateCache(sp<skiapipeline::VectorDrawableAtlas>& atlas, GrContext* context) {
Stan Iliev52e43922019-08-06 15:59:44 -0400500#ifdef __ANDROID__ // Layoutlib does not support hardware acceleration
Stan Iliev3310fb12017-03-23 16:56:51 -0400501 SkRect dst;
502 sk_sp<SkSurface> surface = mCache.getSurface(&dst);
John Reck1bcacfd2017-11-03 10:12:19 -0700503 bool canReuseSurface = surface && dst.width() >= mProperties.getScaledWidth() &&
504 dst.height() >= mProperties.getScaledHeight();
Stan Iliev3310fb12017-03-23 16:56:51 -0400505 if (!canReuseSurface) {
506 int scaledWidth = SkScalarCeilToInt(mProperties.getScaledWidth());
507 int scaledHeight = SkScalarCeilToInt(mProperties.getScaledHeight());
508 auto atlasEntry = atlas->requestNewEntry(scaledWidth, scaledHeight, context);
509 if (INVALID_ATLAS_KEY != atlasEntry.key) {
510 dst = atlasEntry.rect;
511 surface = atlasEntry.surface;
512 mCache.setAtlas(atlas, atlasEntry.key);
513 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700514 // don't draw, if we failed to allocate an offscreen buffer
Stan Iliev3310fb12017-03-23 16:56:51 -0400515 mCache.clear();
516 surface.reset();
517 }
Stan Iliev23c38a92017-03-23 00:12:50 -0400518 }
Stan Iliev3310fb12017-03-23 16:56:51 -0400519 if (!canReuseSurface || mCache.dirty) {
Derek Sollenberger7fe53c12017-08-31 15:40:12 -0400520 if (surface) {
521 Bitmap& bitmap = getBitmapUpdateIfDirty();
522 SkBitmap skiaBitmap;
523 bitmap.getSkBitmap(&skiaBitmap);
Mike Reed6b6e66d2018-02-08 16:28:53 -0500524 surface->writePixels(skiaBitmap, dst.fLeft, dst.fTop);
Derek Sollenberger7fe53c12017-08-31 15:40:12 -0400525 }
Stan Iliev23c38a92017-03-23 00:12:50 -0400526 mCache.dirty = false;
Stan Iliev23c38a92017-03-23 00:12:50 -0400527 }
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100528#endif
Stan Iliev23c38a92017-03-23 00:12:50 -0400529}
530
Stan Iliev3310fb12017-03-23 16:56:51 -0400531void Tree::Cache::setAtlas(sp<skiapipeline::VectorDrawableAtlas> newAtlas,
John Reck1bcacfd2017-11-03 10:12:19 -0700532 skiapipeline::AtlasKey newAtlasKey) {
Stan Iliev3310fb12017-03-23 16:56:51 -0400533 LOG_ALWAYS_FATAL_IF(newAtlasKey == INVALID_ATLAS_KEY);
534 clear();
535 mAtlas = newAtlas;
536 mAtlasKey = newAtlasKey;
537}
538
539sk_sp<SkSurface> Tree::Cache::getSurface(SkRect* bounds) {
540 sk_sp<SkSurface> surface;
Stan Iliev52e43922019-08-06 15:59:44 -0400541#ifdef __ANDROID__ // Layoutlib does not support hardware acceleration
Stan Iliev3310fb12017-03-23 16:56:51 -0400542 sp<skiapipeline::VectorDrawableAtlas> atlas = mAtlas.promote();
543 if (atlas.get() && mAtlasKey != INVALID_ATLAS_KEY) {
544 auto atlasEntry = atlas->getEntry(mAtlasKey);
545 *bounds = atlasEntry.rect;
546 surface = atlasEntry.surface;
547 mAtlasKey = atlasEntry.key;
548 }
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100549#endif
Stan Iliev3310fb12017-03-23 16:56:51 -0400550
551 return surface;
552}
553
554void Tree::Cache::clear() {
Stan Iliev52e43922019-08-06 15:59:44 -0400555#ifdef __ANDROID__ // Layoutlib does not support hardware acceleration
556 if (mAtlasKey != INVALID_ATLAS_KEY) {
557 if (renderthread::RenderThread::isCurrent()) {
558 sp<skiapipeline::VectorDrawableAtlas> lockAtlas = mAtlas.promote();
559 if (lockAtlas.get()) {
560 lockAtlas->releaseEntry(mAtlasKey);
561 }
562 } else {
563 // VectorDrawableAtlas can be accessed only on RenderThread.
564 // Use by-copy capture of the current Cache variables, because "this" may not be valid
565 // by the time the lambda is evaluated on RenderThread.
566 renderthread::RenderThread::getInstance().queue().post(
567 [atlas = mAtlas, atlasKey = mAtlasKey]() {
568 sp<skiapipeline::VectorDrawableAtlas> lockAtlas = atlas.promote();
569 if (lockAtlas.get()) {
570 lockAtlas->releaseEntry(atlasKey);
571 }
572 });
573 }
574 mAtlasKey = INVALID_ATLAS_KEY;
Stan Iliev3310fb12017-03-23 16:56:51 -0400575 }
576 mAtlas = nullptr;
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100577#endif
Stan Iliev3310fb12017-03-23 16:56:51 -0400578}
579
John Reck08ee8152018-09-20 16:27:46 -0700580void Tree::draw(SkCanvas* canvas, const SkRect& bounds, const SkPaint& inPaint) {
Leon Scroggins III6c5864c2019-04-03 15:09:25 -0400581 if (canvas->quickReject(bounds)) {
582 // The RenderNode is on screen, but the AVD is not.
583 return;
584 }
585
John Reck08ee8152018-09-20 16:27:46 -0700586 // Update the paint for any animatable properties
587 SkPaint paint = inPaint;
588 paint.setAlpha(mProperties.getRootAlpha() * 255);
589
John Reck5cca8f22018-12-10 17:06:22 -0800590 if (canvas->getGrContext() == nullptr) {
591 // Recording to picture, don't use the SkSurface which won't work off of renderthread.
592 Bitmap& bitmap = getBitmapUpdateIfDirty();
593 SkBitmap skiaBitmap;
594 bitmap.getSkBitmap(&skiaBitmap);
595
596 int scaledWidth = SkScalarCeilToInt(mProperties.getScaledWidth());
597 int scaledHeight = SkScalarCeilToInt(mProperties.getScaledHeight());
598 canvas->drawBitmapRect(skiaBitmap, SkRect::MakeWH(scaledWidth, scaledHeight), bounds,
599 &paint, SkCanvas::kFast_SrcRectConstraint);
600 return;
601 }
602
Stan Iliev3310fb12017-03-23 16:56:51 -0400603 SkRect src;
604 sk_sp<SkSurface> vdSurface = mCache.getSurface(&src);
605 if (vdSurface) {
John Reck08ee8152018-09-20 16:27:46 -0700606 canvas->drawImageRect(vdSurface->makeImageSnapshot().get(), src, bounds, &paint,
John Recke170fb62018-05-07 08:12:07 -0700607 SkCanvas::kFast_SrcRectConstraint);
Stan Iliev3310fb12017-03-23 16:56:51 -0400608 } else {
609 // Handle the case when VectorDrawableAtlas has been destroyed, because of memory pressure.
610 // We render the VD into a temporary standalone buffer and mark the frame as dirty. Next
611 // frame will be cached into the atlas.
Derek Sollenberger7fe53c12017-08-31 15:40:12 -0400612 Bitmap& bitmap = getBitmapUpdateIfDirty();
613 SkBitmap skiaBitmap;
614 bitmap.getSkBitmap(&skiaBitmap);
615
Stan Iliev3310fb12017-03-23 16:56:51 -0400616 int scaledWidth = SkScalarCeilToInt(mProperties.getScaledWidth());
617 int scaledHeight = SkScalarCeilToInt(mProperties.getScaledHeight());
John Recke170fb62018-05-07 08:12:07 -0700618 canvas->drawBitmapRect(skiaBitmap, SkRect::MakeWH(scaledWidth, scaledHeight), bounds,
John Reck08ee8152018-09-20 16:27:46 -0700619 &paint, SkCanvas::kFast_SrcRectConstraint);
Stan Iliev3310fb12017-03-23 16:56:51 -0400620 mCache.clear();
Stan Iliev3310fb12017-03-23 16:56:51 -0400621 markDirty();
622 }
Stan Iliev23c38a92017-03-23 00:12:50 -0400623}
624
sergeyvfc9999502016-10-17 13:07:38 -0700625void Tree::updateBitmapCache(Bitmap& bitmap, bool useStagingData) {
626 SkBitmap outCache;
627 bitmap.getSkBitmap(&outCache);
ztenghuicf0c41d2017-09-13 10:32:50 -0700628 int cacheWidth = outCache.width();
629 int cacheHeight = outCache.height();
630 ATRACE_FORMAT("VectorDrawable repaint %dx%d", cacheWidth, cacheHeight);
sergeyvfc9999502016-10-17 13:07:38 -0700631 outCache.eraseColor(SK_ColorTRANSPARENT);
632 SkCanvas outCanvas(outCache);
John Reck1bcacfd2017-11-03 10:12:19 -0700633 float viewportWidth =
634 useStagingData ? mStagingProperties.getViewportWidth() : mProperties.getViewportWidth();
635 float viewportHeight = useStagingData ? mStagingProperties.getViewportHeight()
636 : mProperties.getViewportHeight();
ztenghuicf0c41d2017-09-13 10:32:50 -0700637 float scaleX = cacheWidth / viewportWidth;
638 float scaleY = cacheHeight / viewportHeight;
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400639 outCanvas.scale(scaleX, scaleY);
640 mRootNode->draw(&outCanvas, useStagingData);
Doris Liu1d8e1942016-03-02 15:16:28 -0800641}
642
sergeyvfc9999502016-10-17 13:07:38 -0700643bool Tree::allocateBitmapIfNeeded(Cache& cache, int width, int height) {
644 if (!canReuseBitmap(cache.bitmap.get(), width, height)) {
Leon Scroggins III12497572019-01-31 10:06:12 -0500645 SkImageInfo info = SkImageInfo::MakeN32(width, height, kPremul_SkAlphaType);
sergeyvfc9999502016-10-17 13:07:38 -0700646 cache.bitmap = Bitmap::allocateHeapBitmap(info);
Doris Liu1d8e1942016-03-02 15:16:28 -0800647 return true;
Doris Liu4bbc2932015-12-01 17:59:40 -0800648 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800649 return false;
Doris Liu4bbc2932015-12-01 17:59:40 -0800650}
651
sergeyvfc9999502016-10-17 13:07:38 -0700652bool Tree::canReuseBitmap(Bitmap* bitmap, int width, int height) {
Teng-Hui Zhu037fc182016-11-16 10:29:39 -0800653 return bitmap && width <= bitmap->width() && height <= bitmap->height();
Doris Liu1d8e1942016-03-02 15:16:28 -0800654}
655
656void Tree::onPropertyChanged(TreeProperties* prop) {
657 if (prop == &mStagingProperties) {
658 mStagingCache.dirty = true;
659 } else {
660 mCache.dirty = true;
661 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800662}
663
John Reck08ee8152018-09-20 16:27:46 -0700664class MinMaxAverage {
665public:
666 void add(float sample) {
667 if (mCount == 0) {
668 mMin = sample;
669 mMax = sample;
670 } else {
671 mMin = std::min(mMin, sample);
672 mMax = std::max(mMax, sample);
673 }
674 mTotal += sample;
675 mCount++;
676 }
677
678 float average() { return mTotal / mCount; }
679
680 float min() { return mMin; }
681
682 float max() { return mMax; }
683
684 float delta() { return mMax - mMin; }
685
686private:
687 float mMin = 0.0f;
688 float mMax = 0.0f;
689 float mTotal = 0.0f;
690 int mCount = 0;
691};
692
693BitmapPalette Tree::computePalette() {
694 // TODO Cache this and share the code with Bitmap.cpp
695
696 ATRACE_CALL();
697
698 // TODO: This calculation of converting to HSV & tracking min/max is probably overkill
699 // Experiment with something simpler since we just want to figure out if it's "color-ful"
700 // and then the average perceptual lightness.
701
702 MinMaxAverage hue, saturation, value;
703 int sampledCount = 0;
704
705 // Sample a grid of 100 pixels to get an overall estimation of the colors in play
706 mRootNode->forEachFillColor([&](SkColor color) {
707 if (SkColorGetA(color) < 75) {
708 return;
709 }
710 sampledCount++;
711 float hsv[3];
712 SkColorToHSV(color, hsv);
713 hue.add(hsv[0]);
714 saturation.add(hsv[1]);
715 value.add(hsv[2]);
716 });
717
718 if (sampledCount == 0) {
719 ALOGV("VectorDrawable is mostly translucent");
720 return BitmapPalette::Unknown;
721 }
722
723 ALOGV("samples = %d, hue [min = %f, max = %f, avg = %f]; saturation [min = %f, max = %f, avg = "
724 "%f]; value [min = %f, max = %f, avg = %f]",
725 sampledCount, hue.min(), hue.max(), hue.average(), saturation.min(), saturation.max(),
726 saturation.average(), value.min(), value.max(), value.average());
727
728 if (hue.delta() <= 20 && saturation.delta() <= .1f) {
729 if (value.average() >= .5f) {
730 return BitmapPalette::Light;
731 } else {
732 return BitmapPalette::Dark;
733 }
734 }
735 return BitmapPalette::Unknown;
736}
737
Chris Blume7b8a8082018-11-30 15:51:58 -0800738} // namespace VectorDrawable
Doris Liu4bbc2932015-12-01 17:59:40 -0800739
Chris Blume7b8a8082018-11-30 15:51:58 -0800740} // namespace uirenderer
741} // namespace android