blob: af169f4bc4cd7be7f05f9e9b51e141d27fbdaa5e [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
Jerome Gaillard358a3602024-02-26 17:14:54 +000019#include <gui/TraceUtils.h>
Stan Iliev52e43922019-08-06 15:59:44 -040020#include <math.h>
21#include <string.h>
John Reck1bcacfd2017-11-03 10:12:19 -070022#include <utils/Log.h>
Stan Iliev52e43922019-08-06 15:59:44 -040023
Doris Liu4bbc2932015-12-01 17:59:40 -080024#include "PathParser.h"
Kevin Lubick1175dc02022-02-28 12:41:27 -050025#include "SkImage.h"
Doris Liu4bbc2932015-12-01 17:59:40 -080026#include "SkImageInfo.h"
Kevin Lubick1175dc02022-02-28 12:41:27 -050027#include "SkSamplingOptions.h"
28#include "SkScalar.h"
Stan Iliev52e43922019-08-06 15:59:44 -040029#include "hwui/Paint.h"
Stan Iliev52e43922019-08-06 15:59:44 -040030#include "renderthread/RenderThread.h"
Doris Liu4bbc2932015-12-01 17:59:40 -080031#include "utils/Macros.h"
32#include "utils/VectorDrawableUtils.h"
33
Doris Liu4bbc2932015-12-01 17:59:40 -080034namespace android {
35namespace uirenderer {
36namespace VectorDrawable {
37
38const int Tree::MAX_CACHED_BITMAP_SIZE = 2048;
39
Doris Liu4bbc2932015-12-01 17:59:40 -080040void Path::dump() {
Doris Liu1d8e1942016-03-02 15:16:28 -080041 ALOGD("Path: %s has %zu points", mName.c_str(), mProperties.getData().points.size());
Doris Liu4bbc2932015-12-01 17:59:40 -080042}
43
Doris Liu1d8e1942016-03-02 15:16:28 -080044// Called from UI thread during the initial setup/theme change.
Doris Liu4bbc2932015-12-01 17:59:40 -080045Path::Path(const char* pathStr, size_t strLength) {
46 PathParser::ParseResult result;
Doris Liu1d8e1942016-03-02 15:16:28 -080047 Data data;
Doris Liub35da392016-04-12 11:06:23 -070048 PathParser::getPathDataFromAsciiString(&data, &result, pathStr, strLength);
Doris Liu1d8e1942016-03-02 15:16:28 -080049 mStagingProperties.setData(data);
Doris Liu4bbc2932015-12-01 17:59:40 -080050}
51
52Path::Path(const Path& path) : Node(path) {
Doris Liu1d8e1942016-03-02 15:16:28 -080053 mStagingProperties.syncProperties(path.mStagingProperties);
Doris Liu4bbc2932015-12-01 17:59:40 -080054}
55
Stan Ilievcc29a5d2017-03-15 16:37:10 -040056const SkPath& Path::getUpdatedPath(bool useStagingData, SkPath* tempStagingPath) {
57 if (useStagingData) {
58 tempStagingPath->reset();
59 VectorDrawableUtils::verbsToPath(tempStagingPath, mStagingProperties.getData());
60 return *tempStagingPath;
61 } else {
62 if (mSkPathDirty) {
63 mSkPath.reset();
64 VectorDrawableUtils::verbsToPath(&mSkPath, mProperties.getData());
65 mSkPathDirty = false;
66 }
67 return mSkPath;
Doris Liu4bbc2932015-12-01 17:59:40 -080068 }
Doris Liu1d8e1942016-03-02 15:16:28 -080069}
70
71void Path::syncProperties() {
72 if (mStagingPropertiesDirty) {
73 mProperties.syncProperties(mStagingProperties);
74 } else {
75 mStagingProperties.syncProperties(mProperties);
76 }
77 mStagingPropertiesDirty = false;
Doris Liu4bbc2932015-12-01 17:59:40 -080078}
79
80FullPath::FullPath(const FullPath& path) : Path(path) {
Doris Liu1d8e1942016-03-02 15:16:28 -080081 mStagingProperties.syncProperties(path.mStagingProperties);
82}
83
84static void applyTrim(SkPath* outPath, const SkPath& inPath, float trimPathStart, float trimPathEnd,
John Reck1bcacfd2017-11-03 10:12:19 -070085 float trimPathOffset) {
Doris Liu1d8e1942016-03-02 15:16:28 -080086 if (trimPathStart == 0.0f && trimPathEnd == 1.0f) {
87 *outPath = inPath;
88 return;
89 }
90 outPath->reset();
91 if (trimPathStart == trimPathEnd) {
92 // Trimmed path should be empty.
93 return;
94 }
95 SkPathMeasure measure(inPath, false);
96 float len = SkScalarToFloat(measure.getLength());
97 float start = len * fmod((trimPathStart + trimPathOffset), 1.0f);
98 float end = len * fmod((trimPathEnd + trimPathOffset), 1.0f);
99
100 if (start > end) {
101 measure.getSegment(start, len, outPath, true);
102 if (end > 0) {
103 measure.getSegment(0, end, outPath, true);
104 }
105 } else {
106 measure.getSegment(start, end, outPath, true);
107 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800108}
109
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400110const SkPath& FullPath::getUpdatedPath(bool useStagingData, SkPath* tempStagingPath) {
111 if (!useStagingData && !mSkPathDirty && !mProperties.mTrimDirty) {
Doris Liu4bbc2932015-12-01 17:59:40 -0800112 return mTrimmedSkPath;
113 }
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400114 Path::getUpdatedPath(useStagingData, tempStagingPath);
John Reck1bcacfd2017-11-03 10:12:19 -0700115 SkPath* outPath;
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400116 if (useStagingData) {
117 SkPath inPath = *tempStagingPath;
118 applyTrim(tempStagingPath, inPath, mStagingProperties.getTrimPathStart(),
John Reck1bcacfd2017-11-03 10:12:19 -0700119 mStagingProperties.getTrimPathEnd(), mStagingProperties.getTrimPathOffset());
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400120 outPath = tempStagingPath;
Doris Liu4bbc2932015-12-01 17:59:40 -0800121 } else {
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400122 if (mProperties.getTrimPathStart() != 0.0f || mProperties.getTrimPathEnd() != 1.0f) {
123 mProperties.mTrimDirty = false;
124 applyTrim(&mTrimmedSkPath, mSkPath, mProperties.getTrimPathStart(),
John Reck1bcacfd2017-11-03 10:12:19 -0700125 mProperties.getTrimPathEnd(), mProperties.getTrimPathOffset());
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400126 outPath = &mTrimmedSkPath;
127 } else {
128 outPath = &mSkPath;
129 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800130 }
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400131 const FullPathProperties& properties = useStagingData ? mStagingProperties : mProperties;
John Reck1bcacfd2017-11-03 10:12:19 -0700132 bool setFillPath = properties.getFillGradient() != nullptr ||
133 properties.getFillColor() != SK_ColorTRANSPARENT;
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400134 if (setFillPath) {
Mike Reed6a8bf8e2019-12-03 13:01:07 -0500135 outPath->setFillType(static_cast<SkPathFillType>(properties.getFillType()));
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400136 }
137 return *outPath;
Doris Liu4bbc2932015-12-01 17:59:40 -0800138}
139
Doris Liu1d8e1942016-03-02 15:16:28 -0800140void FullPath::dump() {
141 Path::dump();
142 ALOGD("stroke width, color, alpha: %f, %d, %f, fill color, alpha: %d, %f",
John Reck1bcacfd2017-11-03 10:12:19 -0700143 mProperties.getStrokeWidth(), mProperties.getStrokeColor(), mProperties.getStrokeAlpha(),
144 mProperties.getFillColor(), mProperties.getFillAlpha());
Doris Liu1d8e1942016-03-02 15:16:28 -0800145}
146
Doris Liu4bbc2932015-12-01 17:59:40 -0800147inline SkColor applyAlpha(SkColor color, float alpha) {
148 int alphaBytes = SkColorGetA(color);
149 return SkColorSetA(color, alphaBytes * alpha);
150}
151
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400152void FullPath::draw(SkCanvas* outCanvas, bool useStagingData) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800153 const FullPathProperties& properties = useStagingData ? mStagingProperties : mProperties;
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400154 SkPath tempStagingPath;
155 const SkPath& renderPath = getUpdatedPath(useStagingData, &tempStagingPath);
Doris Liu1d8e1942016-03-02 15:16:28 -0800156
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800157 // Draw path's fill, if fill color or gradient is valid
158 bool needsFill = false;
Nader Jawad5bed1f52020-09-25 00:27:29 -0700159 SkPaint paint;
Doris Liu1d8e1942016-03-02 15:16:28 -0800160 if (properties.getFillGradient() != nullptr) {
161 paint.setColor(applyAlpha(SK_ColorBLACK, properties.getFillAlpha()));
Nader Jawad5bed1f52020-09-25 00:27:29 -0700162 paint.setShader(sk_sp<SkShader>(SkSafeRef(properties.getFillGradient())));
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800163 needsFill = true;
Doris Liu1d8e1942016-03-02 15:16:28 -0800164 } else if (properties.getFillColor() != SK_ColorTRANSPARENT) {
165 paint.setColor(applyAlpha(properties.getFillColor(), properties.getFillAlpha()));
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800166 needsFill = true;
Doris Liu4bbc2932015-12-01 17:59:40 -0800167 }
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800168
169 if (needsFill) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800170 paint.setStyle(SkPaint::Style::kFill_Style);
Doris Liu6b184d72017-12-04 16:31:07 -0800171 paint.setAntiAlias(mAntiAlias);
Doris Liu1d8e1942016-03-02 15:16:28 -0800172 outCanvas->drawPath(renderPath, paint);
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800173 }
174
Doris Liu1d8e1942016-03-02 15:16:28 -0800175 // Draw path's stroke, if stroke color or Gradient is valid
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800176 bool needsStroke = false;
Doris Liu1d8e1942016-03-02 15:16:28 -0800177 if (properties.getStrokeGradient() != nullptr) {
178 paint.setColor(applyAlpha(SK_ColorBLACK, properties.getStrokeAlpha()));
Nader Jawad5bed1f52020-09-25 00:27:29 -0700179 paint.setShader(sk_sp<SkShader>(SkSafeRef(properties.getStrokeGradient())));
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800180 needsStroke = true;
Doris Liu1d8e1942016-03-02 15:16:28 -0800181 } else if (properties.getStrokeColor() != SK_ColorTRANSPARENT) {
182 paint.setColor(applyAlpha(properties.getStrokeColor(), properties.getStrokeAlpha()));
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800183 needsStroke = true;
184 }
185 if (needsStroke) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800186 paint.setStyle(SkPaint::Style::kStroke_Style);
Doris Liu6b184d72017-12-04 16:31:07 -0800187 paint.setAntiAlias(mAntiAlias);
Doris Liu1d8e1942016-03-02 15:16:28 -0800188 paint.setStrokeJoin(SkPaint::Join(properties.getStrokeLineJoin()));
189 paint.setStrokeCap(SkPaint::Cap(properties.getStrokeLineCap()));
190 paint.setStrokeMiter(properties.getStrokeMiterLimit());
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400191 paint.setStrokeWidth(properties.getStrokeWidth());
Doris Liu1d8e1942016-03-02 15:16:28 -0800192 outCanvas->drawPath(renderPath, paint);
Doris Liu4bbc2932015-12-01 17:59:40 -0800193 }
194}
195
Doris Liu1d8e1942016-03-02 15:16:28 -0800196void FullPath::syncProperties() {
197 Path::syncProperties();
Doris Liu4bbc2932015-12-01 17:59:40 -0800198
Doris Liu1d8e1942016-03-02 15:16:28 -0800199 if (mStagingPropertiesDirty) {
200 mProperties.syncProperties(mStagingProperties);
Doris Liu4bbc2932015-12-01 17:59:40 -0800201 } else {
Doris Liu1d8e1942016-03-02 15:16:28 -0800202 // Update staging property with property values from animation.
203 mStagingProperties.syncProperties(mProperties);
Doris Liu4bbc2932015-12-01 17:59:40 -0800204 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800205 mStagingPropertiesDirty = false;
Doris Liu4bbc2932015-12-01 17:59:40 -0800206}
207
Doris Liu1d8e1942016-03-02 15:16:28 -0800208REQUIRE_COMPATIBLE_LAYOUT(FullPath::FullPathProperties::PrimitiveFields);
Doris Liu4bbc2932015-12-01 17:59:40 -0800209
210static_assert(sizeof(float) == sizeof(int32_t), "float is not the same size as int32_t");
211static_assert(sizeof(SkColor) == sizeof(int32_t), "SkColor is not the same size as int32_t");
212
Doris Liu1d8e1942016-03-02 15:16:28 -0800213bool FullPath::FullPathProperties::copyProperties(int8_t* outProperties, int length) const {
214 int propertyDataSize = sizeof(FullPathProperties::PrimitiveFields);
Doris Liu4bbc2932015-12-01 17:59:40 -0800215 if (length != propertyDataSize) {
216 LOG_ALWAYS_FATAL("Properties needs exactly %d bytes, a byte array of size %d is provided",
John Reck1bcacfd2017-11-03 10:12:19 -0700217 propertyDataSize, length);
Doris Liu4bbc2932015-12-01 17:59:40 -0800218 return false;
219 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800220
221 PrimitiveFields* out = reinterpret_cast<PrimitiveFields*>(outProperties);
222 *out = mPrimitiveFields;
Doris Liu4bbc2932015-12-01 17:59:40 -0800223 return true;
224}
225
Doris Liu1d8e1942016-03-02 15:16:28 -0800226void FullPath::FullPathProperties::setColorPropertyValue(int propertyId, int32_t value) {
Doris Liu766431a2016-02-04 22:17:11 +0000227 Property currentProperty = static_cast<Property>(propertyId);
Doris Liu1d8e1942016-03-02 15:16:28 -0800228 if (currentProperty == Property::strokeColor) {
229 setStrokeColor(value);
230 } else if (currentProperty == Property::fillColor) {
231 setFillColor(value);
Doris Liu766431a2016-02-04 22:17:11 +0000232 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700233 LOG_ALWAYS_FATAL(
234 "Error setting color property on FullPath: No valid property"
235 " with id: %d",
236 propertyId);
Doris Liu766431a2016-02-04 22:17:11 +0000237 }
238}
239
Doris Liu1d8e1942016-03-02 15:16:28 -0800240void FullPath::FullPathProperties::setPropertyValue(int propertyId, float value) {
Doris Liu766431a2016-02-04 22:17:11 +0000241 Property property = static_cast<Property>(propertyId);
242 switch (property) {
John Reck1bcacfd2017-11-03 10:12:19 -0700243 case Property::strokeWidth:
244 setStrokeWidth(value);
245 break;
246 case Property::strokeAlpha:
247 setStrokeAlpha(value);
248 break;
249 case Property::fillAlpha:
250 setFillAlpha(value);
251 break;
252 case Property::trimPathStart:
253 setTrimPathStart(value);
254 break;
255 case Property::trimPathEnd:
256 setTrimPathEnd(value);
257 break;
258 case Property::trimPathOffset:
259 setTrimPathOffset(value);
260 break;
261 default:
262 LOG_ALWAYS_FATAL("Invalid property id: %d for animation", propertyId);
263 break;
Doris Liu766431a2016-02-04 22:17:11 +0000264 }
265}
266
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400267void ClipPath::draw(SkCanvas* outCanvas, bool useStagingData) {
268 SkPath tempStagingPath;
Bo Brinkmanba248e22021-09-17 22:30:07 +0000269 outCanvas->clipPath(getUpdatedPath(useStagingData, &tempStagingPath), true);
Doris Liu4bbc2932015-12-01 17:59:40 -0800270}
271
272Group::Group(const Group& group) : Node(group) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800273 mStagingProperties.syncProperties(group.mStagingProperties);
Doris Liu4bbc2932015-12-01 17:59:40 -0800274}
275
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400276void Group::draw(SkCanvas* outCanvas, bool useStagingData) {
277 // Save the current clip and matrix information, which is local to this group.
278 SkAutoCanvasRestore saver(outCanvas, true);
279 // apply the current group's matrix to the canvas
Doris Liu4bbc2932015-12-01 17:59:40 -0800280 SkMatrix stackedMatrix;
Doris Liu1d8e1942016-03-02 15:16:28 -0800281 const GroupProperties& prop = useStagingData ? mStagingProperties : mProperties;
282 getLocalMatrix(&stackedMatrix, prop);
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400283 outCanvas->concat(stackedMatrix);
Doris Liu4bbc2932015-12-01 17:59:40 -0800284 // Draw the group tree in the same order as the XML file.
Doris Liuef062eb2016-02-04 16:16:27 -0800285 for (auto& child : mChildren) {
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400286 child->draw(outCanvas, useStagingData);
Doris Liu4bbc2932015-12-01 17:59:40 -0800287 }
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400288 // Restore the previous clip and matrix information.
Doris Liu4bbc2932015-12-01 17:59:40 -0800289}
290
291void Group::dump() {
292 ALOGD("Group %s has %zu children: ", mName.c_str(), mChildren.size());
Doris Liu1d8e1942016-03-02 15:16:28 -0800293 ALOGD("Group translateX, Y : %f, %f, scaleX, Y: %f, %f", mProperties.getTranslateX(),
John Reck1bcacfd2017-11-03 10:12:19 -0700294 mProperties.getTranslateY(), mProperties.getScaleX(), mProperties.getScaleY());
Doris Liu4bbc2932015-12-01 17:59:40 -0800295 for (size_t i = 0; i < mChildren.size(); i++) {
296 mChildren[i]->dump();
297 }
298}
299
Doris Liu1d8e1942016-03-02 15:16:28 -0800300void Group::syncProperties() {
301 // Copy over the dirty staging properties
302 if (mStagingPropertiesDirty) {
303 mProperties.syncProperties(mStagingProperties);
304 } else {
305 mStagingProperties.syncProperties(mProperties);
306 }
307 mStagingPropertiesDirty = false;
308 for (auto& child : mChildren) {
309 child->syncProperties();
310 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800311}
312
Doris Liu1d8e1942016-03-02 15:16:28 -0800313void Group::getLocalMatrix(SkMatrix* outMatrix, const GroupProperties& properties) {
Doris Liu4bbc2932015-12-01 17:59:40 -0800314 outMatrix->reset();
315 // TODO: use rotate(mRotate, mPivotX, mPivotY) and scale with pivot point, instead of
316 // translating to pivot for rotating and scaling, then translating back.
Doris Liu1d8e1942016-03-02 15:16:28 -0800317 outMatrix->postTranslate(-properties.getPivotX(), -properties.getPivotY());
318 outMatrix->postScale(properties.getScaleX(), properties.getScaleY());
319 outMatrix->postRotate(properties.getRotation(), 0, 0);
320 outMatrix->postTranslate(properties.getTranslateX() + properties.getPivotX(),
John Reck1bcacfd2017-11-03 10:12:19 -0700321 properties.getTranslateY() + properties.getPivotY());
Doris Liu4bbc2932015-12-01 17:59:40 -0800322}
323
324void Group::addChild(Node* child) {
Doris Liuef062eb2016-02-04 16:16:27 -0800325 mChildren.emplace_back(child);
Doris Liu1d8e1942016-03-02 15:16:28 -0800326 if (mPropertyChangedListener != nullptr) {
327 child->setPropertyChangedListener(mPropertyChangedListener);
328 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800329}
330
Doris Liu1d8e1942016-03-02 15:16:28 -0800331bool Group::GroupProperties::copyProperties(float* outProperties, int length) const {
332 int propertyCount = static_cast<int>(Property::count);
Doris Liu4bbc2932015-12-01 17:59:40 -0800333 if (length != propertyCount) {
334 LOG_ALWAYS_FATAL("Properties needs exactly %d bytes, a byte array of size %d is provided",
John Reck1bcacfd2017-11-03 10:12:19 -0700335 propertyCount, length);
Doris Liu4bbc2932015-12-01 17:59:40 -0800336 return false;
337 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800338
339 PrimitiveFields* out = reinterpret_cast<PrimitiveFields*>(outProperties);
340 *out = mPrimitiveFields;
Doris Liu4bbc2932015-12-01 17:59:40 -0800341 return true;
342}
343
Doris Liu766431a2016-02-04 22:17:11 +0000344// TODO: Consider animating the properties as float pointers
Doris Liu1d8e1942016-03-02 15:16:28 -0800345// Called on render thread
346float Group::GroupProperties::getPropertyValue(int propertyId) const {
Doris Liu766431a2016-02-04 22:17:11 +0000347 Property currentProperty = static_cast<Property>(propertyId);
348 switch (currentProperty) {
John Reck1bcacfd2017-11-03 10:12:19 -0700349 case Property::rotate:
350 return getRotation();
351 case Property::pivotX:
352 return getPivotX();
353 case Property::pivotY:
354 return getPivotY();
355 case Property::scaleX:
356 return getScaleX();
357 case Property::scaleY:
358 return getScaleY();
359 case Property::translateX:
360 return getTranslateX();
361 case Property::translateY:
362 return getTranslateY();
363 default:
364 LOG_ALWAYS_FATAL("Invalid property index: %d", propertyId);
365 return 0;
Doris Liu766431a2016-02-04 22:17:11 +0000366 }
367}
368
Doris Liu1d8e1942016-03-02 15:16:28 -0800369// Called on render thread
370void Group::GroupProperties::setPropertyValue(int propertyId, float value) {
Doris Liu766431a2016-02-04 22:17:11 +0000371 Property currentProperty = static_cast<Property>(propertyId);
372 switch (currentProperty) {
John Reck1bcacfd2017-11-03 10:12:19 -0700373 case Property::rotate:
374 setRotation(value);
375 break;
376 case Property::pivotX:
377 setPivotX(value);
378 break;
379 case Property::pivotY:
380 setPivotY(value);
381 break;
382 case Property::scaleX:
383 setScaleX(value);
384 break;
385 case Property::scaleY:
386 setScaleY(value);
387 break;
388 case Property::translateX:
389 setTranslateX(value);
390 break;
391 case Property::translateY:
392 setTranslateY(value);
393 break;
394 default:
395 LOG_ALWAYS_FATAL("Invalid property index: %d", propertyId);
Doris Liu766431a2016-02-04 22:17:11 +0000396 }
397}
398
399bool Group::isValidProperty(int propertyId) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800400 return GroupProperties::isValidProperty(propertyId);
401}
402
403bool Group::GroupProperties::isValidProperty(int propertyId) {
404 return propertyId >= 0 && propertyId < static_cast<int>(Property::count);
Doris Liu766431a2016-02-04 22:17:11 +0000405}
406
John Reck1bcacfd2017-11-03 10:12:19 -0700407int Tree::draw(Canvas* outCanvas, SkColorFilter* colorFilter, const SkRect& bounds,
408 bool needsMirroring, bool canReuseCache) {
Doris Liu4bbc2932015-12-01 17:59:40 -0800409 // The imageView can scale the canvas in different ways, in order to
410 // avoid blurry scaling, we have to draw into a bitmap with exact pixel
411 // size first. This bitmap size is determined by the bounds and the
412 // canvas scale.
Doris Liu1d8e1942016-03-02 15:16:28 -0800413 SkMatrix canvasMatrix;
414 outCanvas->getMatrix(&canvasMatrix);
Doris Liua0e61572015-12-29 14:57:49 -0800415 float canvasScaleX = 1.0f;
416 float canvasScaleY = 1.0f;
Doris Liu1d8e1942016-03-02 15:16:28 -0800417 if (canvasMatrix.getSkewX() == 0 && canvasMatrix.getSkewY() == 0) {
Doris Liua0e61572015-12-29 14:57:49 -0800418 // Only use the scale value when there's no skew or rotation in the canvas matrix.
Doris Liue410a352016-01-13 17:23:33 -0800419 // TODO: Add a cts test for drawing VD on a canvas with negative scaling factors.
Doris Liu1d8e1942016-03-02 15:16:28 -0800420 canvasScaleX = fabs(canvasMatrix.getScaleX());
421 canvasScaleY = fabs(canvasMatrix.getScaleY());
Doris Liua0e61572015-12-29 14:57:49 -0800422 }
John Reck1bcacfd2017-11-03 10:12:19 -0700423 int scaledWidth = (int)(bounds.width() * canvasScaleX);
424 int scaledHeight = (int)(bounds.height() * canvasScaleY);
Doris Liu4bbc2932015-12-01 17:59:40 -0800425 scaledWidth = std::min(Tree::MAX_CACHED_BITMAP_SIZE, scaledWidth);
426 scaledHeight = std::min(Tree::MAX_CACHED_BITMAP_SIZE, scaledHeight);
427
428 if (scaledWidth <= 0 || scaledHeight <= 0) {
Doris Liuf8d131c2016-04-29 18:41:29 -0700429 return 0;
Doris Liu4bbc2932015-12-01 17:59:40 -0800430 }
431
Doris Liu1d8e1942016-03-02 15:16:28 -0800432 mStagingProperties.setScaledSize(scaledWidth, scaledHeight);
Florin Malita777bf852016-02-03 10:48:55 -0500433 int saveCount = outCanvas->save(SaveFlags::MatrixClip);
Doris Liu1d8e1942016-03-02 15:16:28 -0800434 outCanvas->translate(bounds.fLeft, bounds.fTop);
Doris Liu4bbc2932015-12-01 17:59:40 -0800435
436 // Handle RTL mirroring.
437 if (needsMirroring) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800438 outCanvas->translate(bounds.width(), 0);
Doris Liu4bbc2932015-12-01 17:59:40 -0800439 outCanvas->scale(-1.0f, 1.0f);
440 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800441 mStagingProperties.setColorFilter(colorFilter);
Doris Liu4bbc2932015-12-01 17:59:40 -0800442
443 // At this point, canvas has been translated to the right position.
444 // And we use this bound for the destination rect for the drawBitmap, so
445 // we offset to (0, 0);
Doris Liu1d8e1942016-03-02 15:16:28 -0800446 SkRect tmpBounds = bounds;
447 tmpBounds.offsetTo(0, 0);
448 mStagingProperties.setBounds(tmpBounds);
Doris Liu766431a2016-02-04 22:17:11 +0000449 outCanvas->drawVectorDrawable(this);
Doris Liu4bbc2932015-12-01 17:59:40 -0800450 outCanvas->restoreToCount(saveCount);
Doris Liuf8d131c2016-04-29 18:41:29 -0700451 return scaledWidth * scaledHeight;
Doris Liu4bbc2932015-12-01 17:59:40 -0800452}
453
Doris Liu1d8e1942016-03-02 15:16:28 -0800454void Tree::drawStaging(Canvas* outCanvas) {
John Reck1bcacfd2017-11-03 10:12:19 -0700455 bool redrawNeeded = allocateBitmapIfNeeded(mStagingCache, mStagingProperties.getScaledWidth(),
456 mStagingProperties.getScaledHeight());
Doris Liu1d8e1942016-03-02 15:16:28 -0800457 // draw bitmap cache
458 if (redrawNeeded || mStagingCache.dirty) {
sergeyvfc9999502016-10-17 13:07:38 -0700459 updateBitmapCache(*mStagingCache.bitmap, true);
Doris Liu1d8e1942016-03-02 15:16:28 -0800460 mStagingCache.dirty = false;
Doris Liu4bbc2932015-12-01 17:59:40 -0800461 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800462
Mike Reeda6cb58a2021-07-10 13:31:34 -0400463 Paint skp;
Mike Reedc2dbc032019-07-25 12:28:29 -0400464 getPaintFor(&skp, mStagingProperties);
465 Paint paint;
Mike Reeda6cb58a2021-07-10 13:31:34 -0400466 paint.setFilterBitmap(skp.isFilterBitmap());
Mike Reedc2dbc032019-07-25 12:28:29 -0400467 paint.setColorFilter(skp.refColorFilter());
468 paint.setAlpha(skp.getAlpha());
John Reck1bcacfd2017-11-03 10:12:19 -0700469 outCanvas->drawBitmap(*mStagingCache.bitmap, 0, 0, mStagingCache.bitmap->width(),
470 mStagingCache.bitmap->height(), mStagingProperties.getBounds().left(),
471 mStagingProperties.getBounds().top(),
472 mStagingProperties.getBounds().right(),
John Reck08ee8152018-09-20 16:27:46 -0700473 mStagingProperties.getBounds().bottom(), &paint);
Doris Liu1d8e1942016-03-02 15:16:28 -0800474}
475
Mike Reeda6cb58a2021-07-10 13:31:34 -0400476void Tree::getPaintFor(Paint* outPaint, const TreeProperties& prop) const {
Stan Iliev038fc372018-07-30 18:31:46 -0400477 // HWUI always draws VD with bilinear filtering.
Mike Reeda6cb58a2021-07-10 13:31:34 -0400478 outPaint->setFilterBitmap(true);
Doris Liu7cc6ec22018-10-02 16:15:57 -0700479 if (prop.getColorFilter() != nullptr) {
John Reck08ee8152018-09-20 16:27:46 -0700480 outPaint->setColorFilter(sk_ref_sp(prop.getColorFilter()));
Doris Liu1d8e1942016-03-02 15:16:28 -0800481 }
Doris Liu7cc6ec22018-10-02 16:15:57 -0700482 outPaint->setAlpha(prop.getRootAlpha() * 255);
Doris Liu4bbc2932015-12-01 17:59:40 -0800483}
484
sergeyvfc9999502016-10-17 13:07:38 -0700485Bitmap& Tree::getBitmapUpdateIfDirty() {
486 bool redrawNeeded = allocateBitmapIfNeeded(mCache, mProperties.getScaledWidth(),
John Reck1bcacfd2017-11-03 10:12:19 -0700487 mProperties.getScaledHeight());
Doris Liu1d8e1942016-03-02 15:16:28 -0800488 if (redrawNeeded || mCache.dirty) {
sergeyvfc9999502016-10-17 13:07:38 -0700489 updateBitmapCache(*mCache.bitmap, false);
Doris Liu1d8e1942016-03-02 15:16:28 -0800490 mCache.dirty = false;
491 }
sergeyvfc9999502016-10-17 13:07:38 -0700492 return *mCache.bitmap;
Doris Liu4bbc2932015-12-01 17:59:40 -0800493}
494
John Reck08ee8152018-09-20 16:27:46 -0700495void Tree::draw(SkCanvas* canvas, const SkRect& bounds, const SkPaint& inPaint) {
Leon Scroggins III6c5864c2019-04-03 15:09:25 -0400496 if (canvas->quickReject(bounds)) {
497 // The RenderNode is on screen, but the AVD is not.
498 return;
499 }
500
John Reck08ee8152018-09-20 16:27:46 -0700501 // Update the paint for any animatable properties
502 SkPaint paint = inPaint;
503 paint.setAlpha(mProperties.getRootAlpha() * 255);
504
Derek Sollenberger2960d162020-11-25 11:49:22 -0500505 sk_sp<SkImage> cachedBitmap = getBitmapUpdateIfDirty().makeImage();
John Reck5cca8f22018-12-10 17:06:22 -0800506
Mike Reed7994a312021-01-28 18:06:26 -0500507 // HWUI always draws VD with bilinear filtering.
508 auto sampling = SkSamplingOptions(SkFilterMode::kLinear);
John Reck83161dc2019-10-04 14:48:27 -0700509 int scaledWidth = SkScalarCeilToInt(mProperties.getScaledWidth());
510 int scaledHeight = SkScalarCeilToInt(mProperties.getScaledHeight());
Derek Sollenberger2960d162020-11-25 11:49:22 -0500511 canvas->drawImageRect(cachedBitmap, SkRect::MakeWH(scaledWidth, scaledHeight), bounds,
Mike Reed7994a312021-01-28 18:06:26 -0500512 sampling, &paint, SkCanvas::kFast_SrcRectConstraint);
Stan Iliev23c38a92017-03-23 00:12:50 -0400513}
514
sergeyvfc9999502016-10-17 13:07:38 -0700515void Tree::updateBitmapCache(Bitmap& bitmap, bool useStagingData) {
516 SkBitmap outCache;
517 bitmap.getSkBitmap(&outCache);
ztenghuicf0c41d2017-09-13 10:32:50 -0700518 int cacheWidth = outCache.width();
519 int cacheHeight = outCache.height();
520 ATRACE_FORMAT("VectorDrawable repaint %dx%d", cacheWidth, cacheHeight);
sergeyvfc9999502016-10-17 13:07:38 -0700521 outCache.eraseColor(SK_ColorTRANSPARENT);
522 SkCanvas outCanvas(outCache);
John Reck1bcacfd2017-11-03 10:12:19 -0700523 float viewportWidth =
524 useStagingData ? mStagingProperties.getViewportWidth() : mProperties.getViewportWidth();
525 float viewportHeight = useStagingData ? mStagingProperties.getViewportHeight()
526 : mProperties.getViewportHeight();
ztenghuicf0c41d2017-09-13 10:32:50 -0700527 float scaleX = cacheWidth / viewportWidth;
528 float scaleY = cacheHeight / viewportHeight;
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400529 outCanvas.scale(scaleX, scaleY);
530 mRootNode->draw(&outCanvas, useStagingData);
Doris Liu1d8e1942016-03-02 15:16:28 -0800531}
532
sergeyvfc9999502016-10-17 13:07:38 -0700533bool Tree::allocateBitmapIfNeeded(Cache& cache, int width, int height) {
534 if (!canReuseBitmap(cache.bitmap.get(), width, height)) {
Leon Scroggins III12497572019-01-31 10:06:12 -0500535 SkImageInfo info = SkImageInfo::MakeN32(width, height, kPremul_SkAlphaType);
sergeyvfc9999502016-10-17 13:07:38 -0700536 cache.bitmap = Bitmap::allocateHeapBitmap(info);
Doris Liu1d8e1942016-03-02 15:16:28 -0800537 return true;
Doris Liu4bbc2932015-12-01 17:59:40 -0800538 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800539 return false;
Doris Liu4bbc2932015-12-01 17:59:40 -0800540}
541
sergeyvfc9999502016-10-17 13:07:38 -0700542bool Tree::canReuseBitmap(Bitmap* bitmap, int width, int height) {
John Reck904174c32024-03-05 13:26:17 -0500543 return bitmap && width == bitmap->width() && height == bitmap->height();
Doris Liu1d8e1942016-03-02 15:16:28 -0800544}
545
546void Tree::onPropertyChanged(TreeProperties* prop) {
547 if (prop == &mStagingProperties) {
548 mStagingCache.dirty = true;
549 } else {
550 mCache.dirty = true;
551 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800552}
553
John Reck08ee8152018-09-20 16:27:46 -0700554class MinMaxAverage {
555public:
556 void add(float sample) {
557 if (mCount == 0) {
558 mMin = sample;
559 mMax = sample;
560 } else {
561 mMin = std::min(mMin, sample);
562 mMax = std::max(mMax, sample);
563 }
564 mTotal += sample;
565 mCount++;
566 }
567
568 float average() { return mTotal / mCount; }
569
570 float min() { return mMin; }
571
572 float max() { return mMax; }
573
574 float delta() { return mMax - mMin; }
575
576private:
577 float mMin = 0.0f;
578 float mMax = 0.0f;
579 float mTotal = 0.0f;
580 int mCount = 0;
581};
582
583BitmapPalette Tree::computePalette() {
584 // TODO Cache this and share the code with Bitmap.cpp
585
586 ATRACE_CALL();
587
588 // TODO: This calculation of converting to HSV & tracking min/max is probably overkill
589 // Experiment with something simpler since we just want to figure out if it's "color-ful"
590 // and then the average perceptual lightness.
591
592 MinMaxAverage hue, saturation, value;
593 int sampledCount = 0;
594
595 // Sample a grid of 100 pixels to get an overall estimation of the colors in play
596 mRootNode->forEachFillColor([&](SkColor color) {
597 if (SkColorGetA(color) < 75) {
598 return;
599 }
600 sampledCount++;
601 float hsv[3];
602 SkColorToHSV(color, hsv);
603 hue.add(hsv[0]);
604 saturation.add(hsv[1]);
605 value.add(hsv[2]);
606 });
607
608 if (sampledCount == 0) {
609 ALOGV("VectorDrawable is mostly translucent");
610 return BitmapPalette::Unknown;
611 }
612
613 ALOGV("samples = %d, hue [min = %f, max = %f, avg = %f]; saturation [min = %f, max = %f, avg = "
614 "%f]; value [min = %f, max = %f, avg = %f]",
615 sampledCount, hue.min(), hue.max(), hue.average(), saturation.min(), saturation.max(),
616 saturation.average(), value.min(), value.max(), value.average());
617
618 if (hue.delta() <= 20 && saturation.delta() <= .1f) {
619 if (value.average() >= .5f) {
620 return BitmapPalette::Light;
621 } else {
622 return BitmapPalette::Dark;
623 }
624 }
625 return BitmapPalette::Unknown;
626}
627
Chris Blume7b8a8082018-11-30 15:51:58 -0800628} // namespace VectorDrawable
Doris Liu4bbc2932015-12-01 17:59:40 -0800629
Chris Blume7b8a8082018-11-30 15:51:58 -0800630} // namespace uirenderer
631} // namespace android