blob: f91d178f01f3456fc0c464c008027c64f469c7cd [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
John Reck1bcacfd2017-11-03 10:12:19 -070019#include <utils/Log.h>
Mike Reedc2dbc032019-07-25 12:28:29 -040020#include "hwui/Paint.h"
Doris Liu4bbc2932015-12-01 17:59:40 -080021#include "PathParser.h"
Doris Liu1d8e1942016-03-02 15:16:28 -080022#include "SkColorFilter.h"
Doris Liu4bbc2932015-12-01 17:59:40 -080023#include "SkImageInfo.h"
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -080024#include "SkShader.h"
Doris Liu4bbc2932015-12-01 17:59:40 -080025#include "utils/Macros.h"
ztenghuicf0c41d2017-09-13 10:32:50 -070026#include "utils/TraceUtils.h"
Doris Liu4bbc2932015-12-01 17:59:40 -080027#include "utils/VectorDrawableUtils.h"
28
29#include <math.h>
30#include <string.h>
31
32namespace android {
33namespace uirenderer {
34namespace VectorDrawable {
35
36const int Tree::MAX_CACHED_BITMAP_SIZE = 2048;
37
Doris Liu4bbc2932015-12-01 17:59:40 -080038void Path::dump() {
Doris Liu1d8e1942016-03-02 15:16:28 -080039 ALOGD("Path: %s has %zu points", mName.c_str(), mProperties.getData().points.size());
Doris Liu4bbc2932015-12-01 17:59:40 -080040}
41
Doris Liu1d8e1942016-03-02 15:16:28 -080042// Called from UI thread during the initial setup/theme change.
Doris Liu4bbc2932015-12-01 17:59:40 -080043Path::Path(const char* pathStr, size_t strLength) {
44 PathParser::ParseResult result;
Doris Liu1d8e1942016-03-02 15:16:28 -080045 Data data;
Doris Liub35da392016-04-12 11:06:23 -070046 PathParser::getPathDataFromAsciiString(&data, &result, pathStr, strLength);
Doris Liu1d8e1942016-03-02 15:16:28 -080047 mStagingProperties.setData(data);
Doris Liu4bbc2932015-12-01 17:59:40 -080048}
49
50Path::Path(const Path& path) : Node(path) {
Doris Liu1d8e1942016-03-02 15:16:28 -080051 mStagingProperties.syncProperties(path.mStagingProperties);
Doris Liu4bbc2932015-12-01 17:59:40 -080052}
53
Stan Ilievcc29a5d2017-03-15 16:37:10 -040054const SkPath& Path::getUpdatedPath(bool useStagingData, SkPath* tempStagingPath) {
55 if (useStagingData) {
56 tempStagingPath->reset();
57 VectorDrawableUtils::verbsToPath(tempStagingPath, mStagingProperties.getData());
58 return *tempStagingPath;
59 } else {
60 if (mSkPathDirty) {
61 mSkPath.reset();
62 VectorDrawableUtils::verbsToPath(&mSkPath, mProperties.getData());
63 mSkPathDirty = false;
64 }
65 return mSkPath;
Doris Liu4bbc2932015-12-01 17:59:40 -080066 }
Doris Liu1d8e1942016-03-02 15:16:28 -080067}
68
69void Path::syncProperties() {
70 if (mStagingPropertiesDirty) {
71 mProperties.syncProperties(mStagingProperties);
72 } else {
73 mStagingProperties.syncProperties(mProperties);
74 }
75 mStagingPropertiesDirty = false;
Doris Liu4bbc2932015-12-01 17:59:40 -080076}
77
78FullPath::FullPath(const FullPath& path) : Path(path) {
Doris Liu1d8e1942016-03-02 15:16:28 -080079 mStagingProperties.syncProperties(path.mStagingProperties);
80}
81
82static void applyTrim(SkPath* outPath, const SkPath& inPath, float trimPathStart, float trimPathEnd,
John Reck1bcacfd2017-11-03 10:12:19 -070083 float trimPathOffset) {
Doris Liu1d8e1942016-03-02 15:16:28 -080084 if (trimPathStart == 0.0f && trimPathEnd == 1.0f) {
85 *outPath = inPath;
86 return;
87 }
88 outPath->reset();
89 if (trimPathStart == trimPathEnd) {
90 // Trimmed path should be empty.
91 return;
92 }
93 SkPathMeasure measure(inPath, false);
94 float len = SkScalarToFloat(measure.getLength());
95 float start = len * fmod((trimPathStart + trimPathOffset), 1.0f);
96 float end = len * fmod((trimPathEnd + trimPathOffset), 1.0f);
97
98 if (start > end) {
99 measure.getSegment(start, len, outPath, true);
100 if (end > 0) {
101 measure.getSegment(0, end, outPath, true);
102 }
103 } else {
104 measure.getSegment(start, end, outPath, true);
105 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800106}
107
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400108const SkPath& FullPath::getUpdatedPath(bool useStagingData, SkPath* tempStagingPath) {
109 if (!useStagingData && !mSkPathDirty && !mProperties.mTrimDirty) {
Doris Liu4bbc2932015-12-01 17:59:40 -0800110 return mTrimmedSkPath;
111 }
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400112 Path::getUpdatedPath(useStagingData, tempStagingPath);
John Reck1bcacfd2017-11-03 10:12:19 -0700113 SkPath* outPath;
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400114 if (useStagingData) {
115 SkPath inPath = *tempStagingPath;
116 applyTrim(tempStagingPath, inPath, mStagingProperties.getTrimPathStart(),
John Reck1bcacfd2017-11-03 10:12:19 -0700117 mStagingProperties.getTrimPathEnd(), mStagingProperties.getTrimPathOffset());
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400118 outPath = tempStagingPath;
Doris Liu4bbc2932015-12-01 17:59:40 -0800119 } else {
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400120 if (mProperties.getTrimPathStart() != 0.0f || mProperties.getTrimPathEnd() != 1.0f) {
121 mProperties.mTrimDirty = false;
122 applyTrim(&mTrimmedSkPath, mSkPath, mProperties.getTrimPathStart(),
John Reck1bcacfd2017-11-03 10:12:19 -0700123 mProperties.getTrimPathEnd(), mProperties.getTrimPathOffset());
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400124 outPath = &mTrimmedSkPath;
125 } else {
126 outPath = &mSkPath;
127 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800128 }
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400129 const FullPathProperties& properties = useStagingData ? mStagingProperties : mProperties;
John Reck1bcacfd2017-11-03 10:12:19 -0700130 bool setFillPath = properties.getFillGradient() != nullptr ||
131 properties.getFillColor() != SK_ColorTRANSPARENT;
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400132 if (setFillPath) {
133 SkPath::FillType ft = static_cast<SkPath::FillType>(properties.getFillType());
134 outPath->setFillType(ft);
135 }
136 return *outPath;
Doris Liu4bbc2932015-12-01 17:59:40 -0800137}
138
Doris Liu1d8e1942016-03-02 15:16:28 -0800139void FullPath::dump() {
140 Path::dump();
141 ALOGD("stroke width, color, alpha: %f, %d, %f, fill color, alpha: %d, %f",
John Reck1bcacfd2017-11-03 10:12:19 -0700142 mProperties.getStrokeWidth(), mProperties.getStrokeColor(), mProperties.getStrokeAlpha(),
143 mProperties.getFillColor(), mProperties.getFillAlpha());
Doris Liu1d8e1942016-03-02 15:16:28 -0800144}
145
Doris Liu4bbc2932015-12-01 17:59:40 -0800146inline SkColor applyAlpha(SkColor color, float alpha) {
147 int alphaBytes = SkColorGetA(color);
148 return SkColorSetA(color, alphaBytes * alpha);
149}
150
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400151void FullPath::draw(SkCanvas* outCanvas, bool useStagingData) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800152 const FullPathProperties& properties = useStagingData ? mStagingProperties : mProperties;
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400153 SkPath tempStagingPath;
154 const SkPath& renderPath = getUpdatedPath(useStagingData, &tempStagingPath);
Doris Liu1d8e1942016-03-02 15:16:28 -0800155
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800156 // Draw path's fill, if fill color or gradient is valid
157 bool needsFill = false;
Doris Liu1d8e1942016-03-02 15:16:28 -0800158 SkPaint paint;
159 if (properties.getFillGradient() != nullptr) {
160 paint.setColor(applyAlpha(SK_ColorBLACK, properties.getFillAlpha()));
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400161 paint.setShader(sk_sp<SkShader>(SkSafeRef(properties.getFillGradient())));
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800162 needsFill = true;
Doris Liu1d8e1942016-03-02 15:16:28 -0800163 } else if (properties.getFillColor() != SK_ColorTRANSPARENT) {
164 paint.setColor(applyAlpha(properties.getFillColor(), properties.getFillAlpha()));
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800165 needsFill = true;
Doris Liu4bbc2932015-12-01 17:59:40 -0800166 }
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800167
168 if (needsFill) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800169 paint.setStyle(SkPaint::Style::kFill_Style);
Doris Liu6b184d72017-12-04 16:31:07 -0800170 paint.setAntiAlias(mAntiAlias);
Doris Liu1d8e1942016-03-02 15:16:28 -0800171 outCanvas->drawPath(renderPath, paint);
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800172 }
173
Doris Liu1d8e1942016-03-02 15:16:28 -0800174 // Draw path's stroke, if stroke color or Gradient is valid
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800175 bool needsStroke = false;
Doris Liu1d8e1942016-03-02 15:16:28 -0800176 if (properties.getStrokeGradient() != nullptr) {
177 paint.setColor(applyAlpha(SK_ColorBLACK, properties.getStrokeAlpha()));
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400178 paint.setShader(sk_sp<SkShader>(SkSafeRef(properties.getStrokeGradient())));
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800179 needsStroke = true;
Doris Liu1d8e1942016-03-02 15:16:28 -0800180 } else if (properties.getStrokeColor() != SK_ColorTRANSPARENT) {
181 paint.setColor(applyAlpha(properties.getStrokeColor(), properties.getStrokeAlpha()));
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800182 needsStroke = true;
183 }
184 if (needsStroke) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800185 paint.setStyle(SkPaint::Style::kStroke_Style);
Doris Liu6b184d72017-12-04 16:31:07 -0800186 paint.setAntiAlias(mAntiAlias);
Doris Liu1d8e1942016-03-02 15:16:28 -0800187 paint.setStrokeJoin(SkPaint::Join(properties.getStrokeLineJoin()));
188 paint.setStrokeCap(SkPaint::Cap(properties.getStrokeLineCap()));
189 paint.setStrokeMiter(properties.getStrokeMiterLimit());
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400190 paint.setStrokeWidth(properties.getStrokeWidth());
Doris Liu1d8e1942016-03-02 15:16:28 -0800191 outCanvas->drawPath(renderPath, paint);
Doris Liu4bbc2932015-12-01 17:59:40 -0800192 }
193}
194
Doris Liu1d8e1942016-03-02 15:16:28 -0800195void FullPath::syncProperties() {
196 Path::syncProperties();
Doris Liu4bbc2932015-12-01 17:59:40 -0800197
Doris Liu1d8e1942016-03-02 15:16:28 -0800198 if (mStagingPropertiesDirty) {
199 mProperties.syncProperties(mStagingProperties);
Doris Liu4bbc2932015-12-01 17:59:40 -0800200 } else {
Doris Liu1d8e1942016-03-02 15:16:28 -0800201 // Update staging property with property values from animation.
202 mStagingProperties.syncProperties(mProperties);
Doris Liu4bbc2932015-12-01 17:59:40 -0800203 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800204 mStagingPropertiesDirty = false;
Doris Liu4bbc2932015-12-01 17:59:40 -0800205}
206
Doris Liu1d8e1942016-03-02 15:16:28 -0800207REQUIRE_COMPATIBLE_LAYOUT(FullPath::FullPathProperties::PrimitiveFields);
Doris Liu4bbc2932015-12-01 17:59:40 -0800208
209static_assert(sizeof(float) == sizeof(int32_t), "float is not the same size as int32_t");
210static_assert(sizeof(SkColor) == sizeof(int32_t), "SkColor is not the same size as int32_t");
211
Doris Liu1d8e1942016-03-02 15:16:28 -0800212bool FullPath::FullPathProperties::copyProperties(int8_t* outProperties, int length) const {
213 int propertyDataSize = sizeof(FullPathProperties::PrimitiveFields);
Doris Liu4bbc2932015-12-01 17:59:40 -0800214 if (length != propertyDataSize) {
215 LOG_ALWAYS_FATAL("Properties needs exactly %d bytes, a byte array of size %d is provided",
John Reck1bcacfd2017-11-03 10:12:19 -0700216 propertyDataSize, length);
Doris Liu4bbc2932015-12-01 17:59:40 -0800217 return false;
218 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800219
220 PrimitiveFields* out = reinterpret_cast<PrimitiveFields*>(outProperties);
221 *out = mPrimitiveFields;
Doris Liu4bbc2932015-12-01 17:59:40 -0800222 return true;
223}
224
Doris Liu1d8e1942016-03-02 15:16:28 -0800225void FullPath::FullPathProperties::setColorPropertyValue(int propertyId, int32_t value) {
Doris Liu766431a2016-02-04 22:17:11 +0000226 Property currentProperty = static_cast<Property>(propertyId);
Doris Liu1d8e1942016-03-02 15:16:28 -0800227 if (currentProperty == Property::strokeColor) {
228 setStrokeColor(value);
229 } else if (currentProperty == Property::fillColor) {
230 setFillColor(value);
Doris Liu766431a2016-02-04 22:17:11 +0000231 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700232 LOG_ALWAYS_FATAL(
233 "Error setting color property on FullPath: No valid property"
234 " with id: %d",
235 propertyId);
Doris Liu766431a2016-02-04 22:17:11 +0000236 }
237}
238
Doris Liu1d8e1942016-03-02 15:16:28 -0800239void FullPath::FullPathProperties::setPropertyValue(int propertyId, float value) {
Doris Liu766431a2016-02-04 22:17:11 +0000240 Property property = static_cast<Property>(propertyId);
241 switch (property) {
John Reck1bcacfd2017-11-03 10:12:19 -0700242 case Property::strokeWidth:
243 setStrokeWidth(value);
244 break;
245 case Property::strokeAlpha:
246 setStrokeAlpha(value);
247 break;
248 case Property::fillAlpha:
249 setFillAlpha(value);
250 break;
251 case Property::trimPathStart:
252 setTrimPathStart(value);
253 break;
254 case Property::trimPathEnd:
255 setTrimPathEnd(value);
256 break;
257 case Property::trimPathOffset:
258 setTrimPathOffset(value);
259 break;
260 default:
261 LOG_ALWAYS_FATAL("Invalid property id: %d for animation", propertyId);
262 break;
Doris Liu766431a2016-02-04 22:17:11 +0000263 }
264}
265
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400266void ClipPath::draw(SkCanvas* outCanvas, bool useStagingData) {
267 SkPath tempStagingPath;
268 outCanvas->clipPath(getUpdatedPath(useStagingData, &tempStagingPath));
Doris Liu4bbc2932015-12-01 17:59:40 -0800269}
270
271Group::Group(const Group& group) : Node(group) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800272 mStagingProperties.syncProperties(group.mStagingProperties);
Doris Liu4bbc2932015-12-01 17:59:40 -0800273}
274
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400275void Group::draw(SkCanvas* outCanvas, bool useStagingData) {
276 // Save the current clip and matrix information, which is local to this group.
277 SkAutoCanvasRestore saver(outCanvas, true);
278 // apply the current group's matrix to the canvas
Doris Liu4bbc2932015-12-01 17:59:40 -0800279 SkMatrix stackedMatrix;
Doris Liu1d8e1942016-03-02 15:16:28 -0800280 const GroupProperties& prop = useStagingData ? mStagingProperties : mProperties;
281 getLocalMatrix(&stackedMatrix, prop);
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400282 outCanvas->concat(stackedMatrix);
Doris Liu4bbc2932015-12-01 17:59:40 -0800283 // Draw the group tree in the same order as the XML file.
Doris Liuef062eb2016-02-04 16:16:27 -0800284 for (auto& child : mChildren) {
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400285 child->draw(outCanvas, useStagingData);
Doris Liu4bbc2932015-12-01 17:59:40 -0800286 }
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400287 // Restore the previous clip and matrix information.
Doris Liu4bbc2932015-12-01 17:59:40 -0800288}
289
290void Group::dump() {
291 ALOGD("Group %s has %zu children: ", mName.c_str(), mChildren.size());
Doris Liu1d8e1942016-03-02 15:16:28 -0800292 ALOGD("Group translateX, Y : %f, %f, scaleX, Y: %f, %f", mProperties.getTranslateX(),
John Reck1bcacfd2017-11-03 10:12:19 -0700293 mProperties.getTranslateY(), mProperties.getScaleX(), mProperties.getScaleY());
Doris Liu4bbc2932015-12-01 17:59:40 -0800294 for (size_t i = 0; i < mChildren.size(); i++) {
295 mChildren[i]->dump();
296 }
297}
298
Doris Liu1d8e1942016-03-02 15:16:28 -0800299void Group::syncProperties() {
300 // Copy over the dirty staging properties
301 if (mStagingPropertiesDirty) {
302 mProperties.syncProperties(mStagingProperties);
303 } else {
304 mStagingProperties.syncProperties(mProperties);
305 }
306 mStagingPropertiesDirty = false;
307 for (auto& child : mChildren) {
308 child->syncProperties();
309 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800310}
311
Doris Liu1d8e1942016-03-02 15:16:28 -0800312void Group::getLocalMatrix(SkMatrix* outMatrix, const GroupProperties& properties) {
Doris Liu4bbc2932015-12-01 17:59:40 -0800313 outMatrix->reset();
314 // TODO: use rotate(mRotate, mPivotX, mPivotY) and scale with pivot point, instead of
315 // translating to pivot for rotating and scaling, then translating back.
Doris Liu1d8e1942016-03-02 15:16:28 -0800316 outMatrix->postTranslate(-properties.getPivotX(), -properties.getPivotY());
317 outMatrix->postScale(properties.getScaleX(), properties.getScaleY());
318 outMatrix->postRotate(properties.getRotation(), 0, 0);
319 outMatrix->postTranslate(properties.getTranslateX() + properties.getPivotX(),
John Reck1bcacfd2017-11-03 10:12:19 -0700320 properties.getTranslateY() + properties.getPivotY());
Doris Liu4bbc2932015-12-01 17:59:40 -0800321}
322
323void Group::addChild(Node* child) {
Doris Liuef062eb2016-02-04 16:16:27 -0800324 mChildren.emplace_back(child);
Doris Liu1d8e1942016-03-02 15:16:28 -0800325 if (mPropertyChangedListener != nullptr) {
326 child->setPropertyChangedListener(mPropertyChangedListener);
327 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800328}
329
Doris Liu1d8e1942016-03-02 15:16:28 -0800330bool Group::GroupProperties::copyProperties(float* outProperties, int length) const {
331 int propertyCount = static_cast<int>(Property::count);
Doris Liu4bbc2932015-12-01 17:59:40 -0800332 if (length != propertyCount) {
333 LOG_ALWAYS_FATAL("Properties needs exactly %d bytes, a byte array of size %d is provided",
John Reck1bcacfd2017-11-03 10:12:19 -0700334 propertyCount, length);
Doris Liu4bbc2932015-12-01 17:59:40 -0800335 return false;
336 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800337
338 PrimitiveFields* out = reinterpret_cast<PrimitiveFields*>(outProperties);
339 *out = mPrimitiveFields;
Doris Liu4bbc2932015-12-01 17:59:40 -0800340 return true;
341}
342
Doris Liu766431a2016-02-04 22:17:11 +0000343// TODO: Consider animating the properties as float pointers
Doris Liu1d8e1942016-03-02 15:16:28 -0800344// Called on render thread
345float Group::GroupProperties::getPropertyValue(int propertyId) const {
Doris Liu766431a2016-02-04 22:17:11 +0000346 Property currentProperty = static_cast<Property>(propertyId);
347 switch (currentProperty) {
John Reck1bcacfd2017-11-03 10:12:19 -0700348 case Property::rotate:
349 return getRotation();
350 case Property::pivotX:
351 return getPivotX();
352 case Property::pivotY:
353 return getPivotY();
354 case Property::scaleX:
355 return getScaleX();
356 case Property::scaleY:
357 return getScaleY();
358 case Property::translateX:
359 return getTranslateX();
360 case Property::translateY:
361 return getTranslateY();
362 default:
363 LOG_ALWAYS_FATAL("Invalid property index: %d", propertyId);
364 return 0;
Doris Liu766431a2016-02-04 22:17:11 +0000365 }
366}
367
Doris Liu1d8e1942016-03-02 15:16:28 -0800368// Called on render thread
369void Group::GroupProperties::setPropertyValue(int propertyId, float value) {
Doris Liu766431a2016-02-04 22:17:11 +0000370 Property currentProperty = static_cast<Property>(propertyId);
371 switch (currentProperty) {
John Reck1bcacfd2017-11-03 10:12:19 -0700372 case Property::rotate:
373 setRotation(value);
374 break;
375 case Property::pivotX:
376 setPivotX(value);
377 break;
378 case Property::pivotY:
379 setPivotY(value);
380 break;
381 case Property::scaleX:
382 setScaleX(value);
383 break;
384 case Property::scaleY:
385 setScaleY(value);
386 break;
387 case Property::translateX:
388 setTranslateX(value);
389 break;
390 case Property::translateY:
391 setTranslateY(value);
392 break;
393 default:
394 LOG_ALWAYS_FATAL("Invalid property index: %d", propertyId);
Doris Liu766431a2016-02-04 22:17:11 +0000395 }
396}
397
398bool Group::isValidProperty(int propertyId) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800399 return GroupProperties::isValidProperty(propertyId);
400}
401
402bool Group::GroupProperties::isValidProperty(int propertyId) {
403 return propertyId >= 0 && propertyId < static_cast<int>(Property::count);
Doris Liu766431a2016-02-04 22:17:11 +0000404}
405
John Reck1bcacfd2017-11-03 10:12:19 -0700406int Tree::draw(Canvas* outCanvas, SkColorFilter* colorFilter, const SkRect& bounds,
407 bool needsMirroring, bool canReuseCache) {
Doris Liu4bbc2932015-12-01 17:59:40 -0800408 // The imageView can scale the canvas in different ways, in order to
409 // avoid blurry scaling, we have to draw into a bitmap with exact pixel
410 // size first. This bitmap size is determined by the bounds and the
411 // canvas scale.
Doris Liu1d8e1942016-03-02 15:16:28 -0800412 SkMatrix canvasMatrix;
413 outCanvas->getMatrix(&canvasMatrix);
Doris Liua0e61572015-12-29 14:57:49 -0800414 float canvasScaleX = 1.0f;
415 float canvasScaleY = 1.0f;
Doris Liu1d8e1942016-03-02 15:16:28 -0800416 if (canvasMatrix.getSkewX() == 0 && canvasMatrix.getSkewY() == 0) {
Doris Liua0e61572015-12-29 14:57:49 -0800417 // Only use the scale value when there's no skew or rotation in the canvas matrix.
Doris Liue410a352016-01-13 17:23:33 -0800418 // TODO: Add a cts test for drawing VD on a canvas with negative scaling factors.
Doris Liu1d8e1942016-03-02 15:16:28 -0800419 canvasScaleX = fabs(canvasMatrix.getScaleX());
420 canvasScaleY = fabs(canvasMatrix.getScaleY());
Doris Liua0e61572015-12-29 14:57:49 -0800421 }
John Reck1bcacfd2017-11-03 10:12:19 -0700422 int scaledWidth = (int)(bounds.width() * canvasScaleX);
423 int scaledHeight = (int)(bounds.height() * canvasScaleY);
Doris Liu4bbc2932015-12-01 17:59:40 -0800424 scaledWidth = std::min(Tree::MAX_CACHED_BITMAP_SIZE, scaledWidth);
425 scaledHeight = std::min(Tree::MAX_CACHED_BITMAP_SIZE, scaledHeight);
426
427 if (scaledWidth <= 0 || scaledHeight <= 0) {
Doris Liuf8d131c2016-04-29 18:41:29 -0700428 return 0;
Doris Liu4bbc2932015-12-01 17:59:40 -0800429 }
430
Doris Liu1d8e1942016-03-02 15:16:28 -0800431 mStagingProperties.setScaledSize(scaledWidth, scaledHeight);
Florin Malita777bf852016-02-03 10:48:55 -0500432 int saveCount = outCanvas->save(SaveFlags::MatrixClip);
Doris Liu1d8e1942016-03-02 15:16:28 -0800433 outCanvas->translate(bounds.fLeft, bounds.fTop);
Doris Liu4bbc2932015-12-01 17:59:40 -0800434
435 // Handle RTL mirroring.
436 if (needsMirroring) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800437 outCanvas->translate(bounds.width(), 0);
Doris Liu4bbc2932015-12-01 17:59:40 -0800438 outCanvas->scale(-1.0f, 1.0f);
439 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800440 mStagingProperties.setColorFilter(colorFilter);
Doris Liu4bbc2932015-12-01 17:59:40 -0800441
442 // At this point, canvas has been translated to the right position.
443 // And we use this bound for the destination rect for the drawBitmap, so
444 // we offset to (0, 0);
Doris Liu1d8e1942016-03-02 15:16:28 -0800445 SkRect tmpBounds = bounds;
446 tmpBounds.offsetTo(0, 0);
447 mStagingProperties.setBounds(tmpBounds);
Doris Liu766431a2016-02-04 22:17:11 +0000448 outCanvas->drawVectorDrawable(this);
Doris Liu4bbc2932015-12-01 17:59:40 -0800449 outCanvas->restoreToCount(saveCount);
Doris Liuf8d131c2016-04-29 18:41:29 -0700450 return scaledWidth * scaledHeight;
Doris Liu4bbc2932015-12-01 17:59:40 -0800451}
452
Doris Liu1d8e1942016-03-02 15:16:28 -0800453void Tree::drawStaging(Canvas* outCanvas) {
John Reck1bcacfd2017-11-03 10:12:19 -0700454 bool redrawNeeded = allocateBitmapIfNeeded(mStagingCache, mStagingProperties.getScaledWidth(),
455 mStagingProperties.getScaledHeight());
Doris Liu1d8e1942016-03-02 15:16:28 -0800456 // draw bitmap cache
457 if (redrawNeeded || mStagingCache.dirty) {
sergeyvfc9999502016-10-17 13:07:38 -0700458 updateBitmapCache(*mStagingCache.bitmap, true);
Doris Liu1d8e1942016-03-02 15:16:28 -0800459 mStagingCache.dirty = false;
Doris Liu4bbc2932015-12-01 17:59:40 -0800460 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800461
Mike Reedc2dbc032019-07-25 12:28:29 -0400462 SkPaint skp;
463 getPaintFor(&skp, mStagingProperties);
464 Paint paint;
465 paint.setFilterQuality(skp.getFilterQuality());
466 paint.setColorFilter(skp.refColorFilter());
467 paint.setAlpha(skp.getAlpha());
John Reck1bcacfd2017-11-03 10:12:19 -0700468 outCanvas->drawBitmap(*mStagingCache.bitmap, 0, 0, mStagingCache.bitmap->width(),
469 mStagingCache.bitmap->height(), mStagingProperties.getBounds().left(),
470 mStagingProperties.getBounds().top(),
471 mStagingProperties.getBounds().right(),
John Reck08ee8152018-09-20 16:27:46 -0700472 mStagingProperties.getBounds().bottom(), &paint);
Doris Liu1d8e1942016-03-02 15:16:28 -0800473}
474
John Reck08ee8152018-09-20 16:27:46 -0700475void Tree::getPaintFor(SkPaint* outPaint, const TreeProperties &prop) const {
Stan Iliev038fc372018-07-30 18:31:46 -0400476 // HWUI always draws VD with bilinear filtering.
477 outPaint->setFilterQuality(kLow_SkFilterQuality);
Doris Liu7cc6ec22018-10-02 16:15:57 -0700478 if (prop.getColorFilter() != nullptr) {
John Reck08ee8152018-09-20 16:27:46 -0700479 outPaint->setColorFilter(sk_ref_sp(prop.getColorFilter()));
Doris Liu1d8e1942016-03-02 15:16:28 -0800480 }
Doris Liu7cc6ec22018-10-02 16:15:57 -0700481 outPaint->setAlpha(prop.getRootAlpha() * 255);
Doris Liu4bbc2932015-12-01 17:59:40 -0800482}
483
sergeyvfc9999502016-10-17 13:07:38 -0700484Bitmap& Tree::getBitmapUpdateIfDirty() {
485 bool redrawNeeded = allocateBitmapIfNeeded(mCache, mProperties.getScaledWidth(),
John Reck1bcacfd2017-11-03 10:12:19 -0700486 mProperties.getScaledHeight());
Doris Liu1d8e1942016-03-02 15:16:28 -0800487 if (redrawNeeded || mCache.dirty) {
sergeyvfc9999502016-10-17 13:07:38 -0700488 updateBitmapCache(*mCache.bitmap, false);
Doris Liu1d8e1942016-03-02 15:16:28 -0800489 mCache.dirty = false;
490 }
sergeyvfc9999502016-10-17 13:07:38 -0700491 return *mCache.bitmap;
Doris Liu4bbc2932015-12-01 17:59:40 -0800492}
493
Stan Iliev3310fb12017-03-23 16:56:51 -0400494void Tree::updateCache(sp<skiapipeline::VectorDrawableAtlas>& atlas, GrContext* context) {
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100495#ifdef __ANDROID__ // Layoutlib does not support hardware acceleration
Stan Iliev3310fb12017-03-23 16:56:51 -0400496 SkRect dst;
497 sk_sp<SkSurface> surface = mCache.getSurface(&dst);
John Reck1bcacfd2017-11-03 10:12:19 -0700498 bool canReuseSurface = surface && dst.width() >= mProperties.getScaledWidth() &&
499 dst.height() >= mProperties.getScaledHeight();
Stan Iliev3310fb12017-03-23 16:56:51 -0400500 if (!canReuseSurface) {
501 int scaledWidth = SkScalarCeilToInt(mProperties.getScaledWidth());
502 int scaledHeight = SkScalarCeilToInt(mProperties.getScaledHeight());
503 auto atlasEntry = atlas->requestNewEntry(scaledWidth, scaledHeight, context);
504 if (INVALID_ATLAS_KEY != atlasEntry.key) {
505 dst = atlasEntry.rect;
506 surface = atlasEntry.surface;
507 mCache.setAtlas(atlas, atlasEntry.key);
508 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700509 // don't draw, if we failed to allocate an offscreen buffer
Stan Iliev3310fb12017-03-23 16:56:51 -0400510 mCache.clear();
511 surface.reset();
512 }
Stan Iliev23c38a92017-03-23 00:12:50 -0400513 }
Stan Iliev3310fb12017-03-23 16:56:51 -0400514 if (!canReuseSurface || mCache.dirty) {
Derek Sollenberger7fe53c12017-08-31 15:40:12 -0400515 if (surface) {
516 Bitmap& bitmap = getBitmapUpdateIfDirty();
517 SkBitmap skiaBitmap;
518 bitmap.getSkBitmap(&skiaBitmap);
Mike Reed6b6e66d2018-02-08 16:28:53 -0500519 surface->writePixels(skiaBitmap, dst.fLeft, dst.fTop);
Derek Sollenberger7fe53c12017-08-31 15:40:12 -0400520 }
Stan Iliev23c38a92017-03-23 00:12:50 -0400521 mCache.dirty = false;
Stan Iliev23c38a92017-03-23 00:12:50 -0400522 }
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100523#endif
Stan Iliev23c38a92017-03-23 00:12:50 -0400524}
525
Stan Iliev3310fb12017-03-23 16:56:51 -0400526void Tree::Cache::setAtlas(sp<skiapipeline::VectorDrawableAtlas> newAtlas,
John Reck1bcacfd2017-11-03 10:12:19 -0700527 skiapipeline::AtlasKey newAtlasKey) {
Stan Iliev3310fb12017-03-23 16:56:51 -0400528 LOG_ALWAYS_FATAL_IF(newAtlasKey == INVALID_ATLAS_KEY);
529 clear();
530 mAtlas = newAtlas;
531 mAtlasKey = newAtlasKey;
532}
533
534sk_sp<SkSurface> Tree::Cache::getSurface(SkRect* bounds) {
535 sk_sp<SkSurface> surface;
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100536#ifdef __ANDROID__ // Layoutlib does not support hardware acceleration
Stan Iliev3310fb12017-03-23 16:56:51 -0400537 sp<skiapipeline::VectorDrawableAtlas> atlas = mAtlas.promote();
538 if (atlas.get() && mAtlasKey != INVALID_ATLAS_KEY) {
539 auto atlasEntry = atlas->getEntry(mAtlasKey);
540 *bounds = atlasEntry.rect;
541 surface = atlasEntry.surface;
542 mAtlasKey = atlasEntry.key;
543 }
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100544#endif
Stan Iliev3310fb12017-03-23 16:56:51 -0400545
546 return surface;
547}
548
549void Tree::Cache::clear() {
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100550#ifdef __ANDROID__ // Layoutlib does not support hardware acceleration
Stan Iliev3310fb12017-03-23 16:56:51 -0400551 sp<skiapipeline::VectorDrawableAtlas> lockAtlas = mAtlas.promote();
552 if (lockAtlas.get()) {
553 lockAtlas->releaseEntry(mAtlasKey);
554 }
555 mAtlas = nullptr;
556 mAtlasKey = INVALID_ATLAS_KEY;
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100557#endif
Stan Iliev3310fb12017-03-23 16:56:51 -0400558}
559
John Reck08ee8152018-09-20 16:27:46 -0700560void Tree::draw(SkCanvas* canvas, const SkRect& bounds, const SkPaint& inPaint) {
Leon Scroggins III6c5864c2019-04-03 15:09:25 -0400561 if (canvas->quickReject(bounds)) {
562 // The RenderNode is on screen, but the AVD is not.
563 return;
564 }
565
John Reck08ee8152018-09-20 16:27:46 -0700566 // Update the paint for any animatable properties
567 SkPaint paint = inPaint;
568 paint.setAlpha(mProperties.getRootAlpha() * 255);
569
John Reck5cca8f22018-12-10 17:06:22 -0800570 if (canvas->getGrContext() == nullptr) {
571 // Recording to picture, don't use the SkSurface which won't work off of renderthread.
572 Bitmap& bitmap = getBitmapUpdateIfDirty();
573 SkBitmap skiaBitmap;
574 bitmap.getSkBitmap(&skiaBitmap);
575
576 int scaledWidth = SkScalarCeilToInt(mProperties.getScaledWidth());
577 int scaledHeight = SkScalarCeilToInt(mProperties.getScaledHeight());
578 canvas->drawBitmapRect(skiaBitmap, SkRect::MakeWH(scaledWidth, scaledHeight), bounds,
579 &paint, SkCanvas::kFast_SrcRectConstraint);
580 return;
581 }
582
Stan Iliev3310fb12017-03-23 16:56:51 -0400583 SkRect src;
584 sk_sp<SkSurface> vdSurface = mCache.getSurface(&src);
585 if (vdSurface) {
John Reck08ee8152018-09-20 16:27:46 -0700586 canvas->drawImageRect(vdSurface->makeImageSnapshot().get(), src, bounds, &paint,
John Recke170fb62018-05-07 08:12:07 -0700587 SkCanvas::kFast_SrcRectConstraint);
Stan Iliev3310fb12017-03-23 16:56:51 -0400588 } else {
589 // Handle the case when VectorDrawableAtlas has been destroyed, because of memory pressure.
590 // We render the VD into a temporary standalone buffer and mark the frame as dirty. Next
591 // frame will be cached into the atlas.
Derek Sollenberger7fe53c12017-08-31 15:40:12 -0400592 Bitmap& bitmap = getBitmapUpdateIfDirty();
593 SkBitmap skiaBitmap;
594 bitmap.getSkBitmap(&skiaBitmap);
595
Stan Iliev3310fb12017-03-23 16:56:51 -0400596 int scaledWidth = SkScalarCeilToInt(mProperties.getScaledWidth());
597 int scaledHeight = SkScalarCeilToInt(mProperties.getScaledHeight());
John Recke170fb62018-05-07 08:12:07 -0700598 canvas->drawBitmapRect(skiaBitmap, SkRect::MakeWH(scaledWidth, scaledHeight), bounds,
John Reck08ee8152018-09-20 16:27:46 -0700599 &paint, SkCanvas::kFast_SrcRectConstraint);
Stan Iliev3310fb12017-03-23 16:56:51 -0400600 mCache.clear();
Stan Iliev3310fb12017-03-23 16:56:51 -0400601 markDirty();
602 }
Stan Iliev23c38a92017-03-23 00:12:50 -0400603}
604
sergeyvfc9999502016-10-17 13:07:38 -0700605void Tree::updateBitmapCache(Bitmap& bitmap, bool useStagingData) {
606 SkBitmap outCache;
607 bitmap.getSkBitmap(&outCache);
ztenghuicf0c41d2017-09-13 10:32:50 -0700608 int cacheWidth = outCache.width();
609 int cacheHeight = outCache.height();
610 ATRACE_FORMAT("VectorDrawable repaint %dx%d", cacheWidth, cacheHeight);
sergeyvfc9999502016-10-17 13:07:38 -0700611 outCache.eraseColor(SK_ColorTRANSPARENT);
612 SkCanvas outCanvas(outCache);
John Reck1bcacfd2017-11-03 10:12:19 -0700613 float viewportWidth =
614 useStagingData ? mStagingProperties.getViewportWidth() : mProperties.getViewportWidth();
615 float viewportHeight = useStagingData ? mStagingProperties.getViewportHeight()
616 : mProperties.getViewportHeight();
ztenghuicf0c41d2017-09-13 10:32:50 -0700617 float scaleX = cacheWidth / viewportWidth;
618 float scaleY = cacheHeight / viewportHeight;
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400619 outCanvas.scale(scaleX, scaleY);
620 mRootNode->draw(&outCanvas, useStagingData);
Doris Liu1d8e1942016-03-02 15:16:28 -0800621}
622
sergeyvfc9999502016-10-17 13:07:38 -0700623bool Tree::allocateBitmapIfNeeded(Cache& cache, int width, int height) {
624 if (!canReuseBitmap(cache.bitmap.get(), width, height)) {
Leon Scroggins III12497572019-01-31 10:06:12 -0500625 SkImageInfo info = SkImageInfo::MakeN32(width, height, kPremul_SkAlphaType);
sergeyvfc9999502016-10-17 13:07:38 -0700626 cache.bitmap = Bitmap::allocateHeapBitmap(info);
Doris Liu1d8e1942016-03-02 15:16:28 -0800627 return true;
Doris Liu4bbc2932015-12-01 17:59:40 -0800628 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800629 return false;
Doris Liu4bbc2932015-12-01 17:59:40 -0800630}
631
sergeyvfc9999502016-10-17 13:07:38 -0700632bool Tree::canReuseBitmap(Bitmap* bitmap, int width, int height) {
Teng-Hui Zhu037fc182016-11-16 10:29:39 -0800633 return bitmap && width <= bitmap->width() && height <= bitmap->height();
Doris Liu1d8e1942016-03-02 15:16:28 -0800634}
635
636void Tree::onPropertyChanged(TreeProperties* prop) {
637 if (prop == &mStagingProperties) {
638 mStagingCache.dirty = true;
639 } else {
640 mCache.dirty = true;
641 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800642}
643
John Reck08ee8152018-09-20 16:27:46 -0700644class MinMaxAverage {
645public:
646 void add(float sample) {
647 if (mCount == 0) {
648 mMin = sample;
649 mMax = sample;
650 } else {
651 mMin = std::min(mMin, sample);
652 mMax = std::max(mMax, sample);
653 }
654 mTotal += sample;
655 mCount++;
656 }
657
658 float average() { return mTotal / mCount; }
659
660 float min() { return mMin; }
661
662 float max() { return mMax; }
663
664 float delta() { return mMax - mMin; }
665
666private:
667 float mMin = 0.0f;
668 float mMax = 0.0f;
669 float mTotal = 0.0f;
670 int mCount = 0;
671};
672
673BitmapPalette Tree::computePalette() {
674 // TODO Cache this and share the code with Bitmap.cpp
675
676 ATRACE_CALL();
677
678 // TODO: This calculation of converting to HSV & tracking min/max is probably overkill
679 // Experiment with something simpler since we just want to figure out if it's "color-ful"
680 // and then the average perceptual lightness.
681
682 MinMaxAverage hue, saturation, value;
683 int sampledCount = 0;
684
685 // Sample a grid of 100 pixels to get an overall estimation of the colors in play
686 mRootNode->forEachFillColor([&](SkColor color) {
687 if (SkColorGetA(color) < 75) {
688 return;
689 }
690 sampledCount++;
691 float hsv[3];
692 SkColorToHSV(color, hsv);
693 hue.add(hsv[0]);
694 saturation.add(hsv[1]);
695 value.add(hsv[2]);
696 });
697
698 if (sampledCount == 0) {
699 ALOGV("VectorDrawable is mostly translucent");
700 return BitmapPalette::Unknown;
701 }
702
703 ALOGV("samples = %d, hue [min = %f, max = %f, avg = %f]; saturation [min = %f, max = %f, avg = "
704 "%f]; value [min = %f, max = %f, avg = %f]",
705 sampledCount, hue.min(), hue.max(), hue.average(), saturation.min(), saturation.max(),
706 saturation.average(), value.min(), value.max(), value.average());
707
708 if (hue.delta() <= 20 && saturation.delta() <= .1f) {
709 if (value.average() >= .5f) {
710 return BitmapPalette::Light;
711 } else {
712 return BitmapPalette::Dark;
713 }
714 }
715 return BitmapPalette::Unknown;
716}
717
Chris Blume7b8a8082018-11-30 15:51:58 -0800718} // namespace VectorDrawable
Doris Liu4bbc2932015-12-01 17:59:40 -0800719
Chris Blume7b8a8082018-11-30 15:51:58 -0800720} // namespace uirenderer
721} // namespace android