blob: dd62bbbdc84f72e5b88bfc60ed4e7d2e58a45ba9 [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>
Doris Liu4bbc2932015-12-01 17:59:40 -080020#include "PathParser.h"
Doris Liu1d8e1942016-03-02 15:16:28 -080021#include "SkColorFilter.h"
Doris Liu4bbc2932015-12-01 17:59:40 -080022#include "SkImageInfo.h"
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -080023#include "SkShader.h"
Doris Liu4bbc2932015-12-01 17:59:40 -080024#include "utils/Macros.h"
ztenghuicf0c41d2017-09-13 10:32:50 -070025#include "utils/TraceUtils.h"
Doris Liu4bbc2932015-12-01 17:59:40 -080026#include "utils/VectorDrawableUtils.h"
27
28#include <math.h>
29#include <string.h>
30
31namespace android {
32namespace uirenderer {
33namespace VectorDrawable {
34
35const int Tree::MAX_CACHED_BITMAP_SIZE = 2048;
36
Doris Liu4bbc2932015-12-01 17:59:40 -080037void Path::dump() {
Doris Liu1d8e1942016-03-02 15:16:28 -080038 ALOGD("Path: %s has %zu points", mName.c_str(), mProperties.getData().points.size());
Doris Liu4bbc2932015-12-01 17:59:40 -080039}
40
Doris Liu1d8e1942016-03-02 15:16:28 -080041// Called from UI thread during the initial setup/theme change.
Doris Liu4bbc2932015-12-01 17:59:40 -080042Path::Path(const char* pathStr, size_t strLength) {
43 PathParser::ParseResult result;
Doris Liu1d8e1942016-03-02 15:16:28 -080044 Data data;
Doris Liub35da392016-04-12 11:06:23 -070045 PathParser::getPathDataFromAsciiString(&data, &result, pathStr, strLength);
Doris Liu1d8e1942016-03-02 15:16:28 -080046 mStagingProperties.setData(data);
Doris Liu4bbc2932015-12-01 17:59:40 -080047}
48
49Path::Path(const Path& path) : Node(path) {
Doris Liu1d8e1942016-03-02 15:16:28 -080050 mStagingProperties.syncProperties(path.mStagingProperties);
Doris Liu4bbc2932015-12-01 17:59:40 -080051}
52
Stan Ilievcc29a5d2017-03-15 16:37:10 -040053const SkPath& Path::getUpdatedPath(bool useStagingData, SkPath* tempStagingPath) {
54 if (useStagingData) {
55 tempStagingPath->reset();
56 VectorDrawableUtils::verbsToPath(tempStagingPath, mStagingProperties.getData());
57 return *tempStagingPath;
58 } else {
59 if (mSkPathDirty) {
60 mSkPath.reset();
61 VectorDrawableUtils::verbsToPath(&mSkPath, mProperties.getData());
62 mSkPathDirty = false;
63 }
64 return mSkPath;
Doris Liu4bbc2932015-12-01 17:59:40 -080065 }
Doris Liu1d8e1942016-03-02 15:16:28 -080066}
67
68void Path::syncProperties() {
69 if (mStagingPropertiesDirty) {
70 mProperties.syncProperties(mStagingProperties);
71 } else {
72 mStagingProperties.syncProperties(mProperties);
73 }
74 mStagingPropertiesDirty = false;
Doris Liu4bbc2932015-12-01 17:59:40 -080075}
76
77FullPath::FullPath(const FullPath& path) : Path(path) {
Doris Liu1d8e1942016-03-02 15:16:28 -080078 mStagingProperties.syncProperties(path.mStagingProperties);
79}
80
81static void applyTrim(SkPath* outPath, const SkPath& inPath, float trimPathStart, float trimPathEnd,
John Reck1bcacfd2017-11-03 10:12:19 -070082 float trimPathOffset) {
Doris Liu1d8e1942016-03-02 15:16:28 -080083 if (trimPathStart == 0.0f && trimPathEnd == 1.0f) {
84 *outPath = inPath;
85 return;
86 }
87 outPath->reset();
88 if (trimPathStart == trimPathEnd) {
89 // Trimmed path should be empty.
90 return;
91 }
92 SkPathMeasure measure(inPath, false);
93 float len = SkScalarToFloat(measure.getLength());
94 float start = len * fmod((trimPathStart + trimPathOffset), 1.0f);
95 float end = len * fmod((trimPathEnd + trimPathOffset), 1.0f);
96
97 if (start > end) {
98 measure.getSegment(start, len, outPath, true);
99 if (end > 0) {
100 measure.getSegment(0, end, outPath, true);
101 }
102 } else {
103 measure.getSegment(start, end, outPath, true);
104 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800105}
106
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400107const SkPath& FullPath::getUpdatedPath(bool useStagingData, SkPath* tempStagingPath) {
108 if (!useStagingData && !mSkPathDirty && !mProperties.mTrimDirty) {
Doris Liu4bbc2932015-12-01 17:59:40 -0800109 return mTrimmedSkPath;
110 }
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400111 Path::getUpdatedPath(useStagingData, tempStagingPath);
John Reck1bcacfd2017-11-03 10:12:19 -0700112 SkPath* outPath;
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400113 if (useStagingData) {
114 SkPath inPath = *tempStagingPath;
115 applyTrim(tempStagingPath, inPath, mStagingProperties.getTrimPathStart(),
John Reck1bcacfd2017-11-03 10:12:19 -0700116 mStagingProperties.getTrimPathEnd(), mStagingProperties.getTrimPathOffset());
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400117 outPath = tempStagingPath;
Doris Liu4bbc2932015-12-01 17:59:40 -0800118 } else {
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400119 if (mProperties.getTrimPathStart() != 0.0f || mProperties.getTrimPathEnd() != 1.0f) {
120 mProperties.mTrimDirty = false;
121 applyTrim(&mTrimmedSkPath, mSkPath, mProperties.getTrimPathStart(),
John Reck1bcacfd2017-11-03 10:12:19 -0700122 mProperties.getTrimPathEnd(), mProperties.getTrimPathOffset());
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400123 outPath = &mTrimmedSkPath;
124 } else {
125 outPath = &mSkPath;
126 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800127 }
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400128 const FullPathProperties& properties = useStagingData ? mStagingProperties : mProperties;
John Reck1bcacfd2017-11-03 10:12:19 -0700129 bool setFillPath = properties.getFillGradient() != nullptr ||
130 properties.getFillColor() != SK_ColorTRANSPARENT;
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400131 if (setFillPath) {
132 SkPath::FillType ft = static_cast<SkPath::FillType>(properties.getFillType());
133 outPath->setFillType(ft);
134 }
135 return *outPath;
Doris Liu4bbc2932015-12-01 17:59:40 -0800136}
137
Doris Liu1d8e1942016-03-02 15:16:28 -0800138void FullPath::dump() {
139 Path::dump();
140 ALOGD("stroke width, color, alpha: %f, %d, %f, fill color, alpha: %d, %f",
John Reck1bcacfd2017-11-03 10:12:19 -0700141 mProperties.getStrokeWidth(), mProperties.getStrokeColor(), mProperties.getStrokeAlpha(),
142 mProperties.getFillColor(), mProperties.getFillAlpha());
Doris Liu1d8e1942016-03-02 15:16:28 -0800143}
144
Doris Liu4bbc2932015-12-01 17:59:40 -0800145inline SkColor applyAlpha(SkColor color, float alpha) {
146 int alphaBytes = SkColorGetA(color);
147 return SkColorSetA(color, alphaBytes * alpha);
148}
149
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400150void FullPath::draw(SkCanvas* outCanvas, bool useStagingData) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800151 const FullPathProperties& properties = useStagingData ? mStagingProperties : mProperties;
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400152 SkPath tempStagingPath;
153 const SkPath& renderPath = getUpdatedPath(useStagingData, &tempStagingPath);
Doris Liu1d8e1942016-03-02 15:16:28 -0800154
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800155 // Draw path's fill, if fill color or gradient is valid
156 bool needsFill = false;
Doris Liu1d8e1942016-03-02 15:16:28 -0800157 SkPaint paint;
158 if (properties.getFillGradient() != nullptr) {
159 paint.setColor(applyAlpha(SK_ColorBLACK, properties.getFillAlpha()));
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400160 paint.setShader(sk_sp<SkShader>(SkSafeRef(properties.getFillGradient())));
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800161 needsFill = true;
Doris Liu1d8e1942016-03-02 15:16:28 -0800162 } else if (properties.getFillColor() != SK_ColorTRANSPARENT) {
163 paint.setColor(applyAlpha(properties.getFillColor(), properties.getFillAlpha()));
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800164 needsFill = true;
Doris Liu4bbc2932015-12-01 17:59:40 -0800165 }
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800166
167 if (needsFill) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800168 paint.setStyle(SkPaint::Style::kFill_Style);
Doris Liu6b184d72017-12-04 16:31:07 -0800169 paint.setAntiAlias(mAntiAlias);
Doris Liu1d8e1942016-03-02 15:16:28 -0800170 outCanvas->drawPath(renderPath, paint);
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800171 }
172
Doris Liu1d8e1942016-03-02 15:16:28 -0800173 // Draw path's stroke, if stroke color or Gradient is valid
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800174 bool needsStroke = false;
Doris Liu1d8e1942016-03-02 15:16:28 -0800175 if (properties.getStrokeGradient() != nullptr) {
176 paint.setColor(applyAlpha(SK_ColorBLACK, properties.getStrokeAlpha()));
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400177 paint.setShader(sk_sp<SkShader>(SkSafeRef(properties.getStrokeGradient())));
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800178 needsStroke = true;
Doris Liu1d8e1942016-03-02 15:16:28 -0800179 } else if (properties.getStrokeColor() != SK_ColorTRANSPARENT) {
180 paint.setColor(applyAlpha(properties.getStrokeColor(), properties.getStrokeAlpha()));
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800181 needsStroke = true;
182 }
183 if (needsStroke) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800184 paint.setStyle(SkPaint::Style::kStroke_Style);
Doris Liu6b184d72017-12-04 16:31:07 -0800185 paint.setAntiAlias(mAntiAlias);
Doris Liu1d8e1942016-03-02 15:16:28 -0800186 paint.setStrokeJoin(SkPaint::Join(properties.getStrokeLineJoin()));
187 paint.setStrokeCap(SkPaint::Cap(properties.getStrokeLineCap()));
188 paint.setStrokeMiter(properties.getStrokeMiterLimit());
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400189 paint.setStrokeWidth(properties.getStrokeWidth());
Doris Liu1d8e1942016-03-02 15:16:28 -0800190 outCanvas->drawPath(renderPath, paint);
Doris Liu4bbc2932015-12-01 17:59:40 -0800191 }
192}
193
Doris Liu1d8e1942016-03-02 15:16:28 -0800194void FullPath::syncProperties() {
195 Path::syncProperties();
Doris Liu4bbc2932015-12-01 17:59:40 -0800196
Doris Liu1d8e1942016-03-02 15:16:28 -0800197 if (mStagingPropertiesDirty) {
198 mProperties.syncProperties(mStagingProperties);
Doris Liu4bbc2932015-12-01 17:59:40 -0800199 } else {
Doris Liu1d8e1942016-03-02 15:16:28 -0800200 // Update staging property with property values from animation.
201 mStagingProperties.syncProperties(mProperties);
Doris Liu4bbc2932015-12-01 17:59:40 -0800202 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800203 mStagingPropertiesDirty = false;
Doris Liu4bbc2932015-12-01 17:59:40 -0800204}
205
Doris Liu1d8e1942016-03-02 15:16:28 -0800206REQUIRE_COMPATIBLE_LAYOUT(FullPath::FullPathProperties::PrimitiveFields);
Doris Liu4bbc2932015-12-01 17:59:40 -0800207
208static_assert(sizeof(float) == sizeof(int32_t), "float is not the same size as int32_t");
209static_assert(sizeof(SkColor) == sizeof(int32_t), "SkColor is not the same size as int32_t");
210
Doris Liu1d8e1942016-03-02 15:16:28 -0800211bool FullPath::FullPathProperties::copyProperties(int8_t* outProperties, int length) const {
212 int propertyDataSize = sizeof(FullPathProperties::PrimitiveFields);
Doris Liu4bbc2932015-12-01 17:59:40 -0800213 if (length != propertyDataSize) {
214 LOG_ALWAYS_FATAL("Properties needs exactly %d bytes, a byte array of size %d is provided",
John Reck1bcacfd2017-11-03 10:12:19 -0700215 propertyDataSize, length);
Doris Liu4bbc2932015-12-01 17:59:40 -0800216 return false;
217 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800218
219 PrimitiveFields* out = reinterpret_cast<PrimitiveFields*>(outProperties);
220 *out = mPrimitiveFields;
Doris Liu4bbc2932015-12-01 17:59:40 -0800221 return true;
222}
223
Doris Liu1d8e1942016-03-02 15:16:28 -0800224void FullPath::FullPathProperties::setColorPropertyValue(int propertyId, int32_t value) {
Doris Liu766431a2016-02-04 22:17:11 +0000225 Property currentProperty = static_cast<Property>(propertyId);
Doris Liu1d8e1942016-03-02 15:16:28 -0800226 if (currentProperty == Property::strokeColor) {
227 setStrokeColor(value);
228 } else if (currentProperty == Property::fillColor) {
229 setFillColor(value);
Doris Liu766431a2016-02-04 22:17:11 +0000230 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700231 LOG_ALWAYS_FATAL(
232 "Error setting color property on FullPath: No valid property"
233 " with id: %d",
234 propertyId);
Doris Liu766431a2016-02-04 22:17:11 +0000235 }
236}
237
Doris Liu1d8e1942016-03-02 15:16:28 -0800238void FullPath::FullPathProperties::setPropertyValue(int propertyId, float value) {
Doris Liu766431a2016-02-04 22:17:11 +0000239 Property property = static_cast<Property>(propertyId);
240 switch (property) {
John Reck1bcacfd2017-11-03 10:12:19 -0700241 case Property::strokeWidth:
242 setStrokeWidth(value);
243 break;
244 case Property::strokeAlpha:
245 setStrokeAlpha(value);
246 break;
247 case Property::fillAlpha:
248 setFillAlpha(value);
249 break;
250 case Property::trimPathStart:
251 setTrimPathStart(value);
252 break;
253 case Property::trimPathEnd:
254 setTrimPathEnd(value);
255 break;
256 case Property::trimPathOffset:
257 setTrimPathOffset(value);
258 break;
259 default:
260 LOG_ALWAYS_FATAL("Invalid property id: %d for animation", propertyId);
261 break;
Doris Liu766431a2016-02-04 22:17:11 +0000262 }
263}
264
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400265void ClipPath::draw(SkCanvas* outCanvas, bool useStagingData) {
266 SkPath tempStagingPath;
267 outCanvas->clipPath(getUpdatedPath(useStagingData, &tempStagingPath));
Doris Liu4bbc2932015-12-01 17:59:40 -0800268}
269
270Group::Group(const Group& group) : Node(group) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800271 mStagingProperties.syncProperties(group.mStagingProperties);
Doris Liu4bbc2932015-12-01 17:59:40 -0800272}
273
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400274void Group::draw(SkCanvas* outCanvas, bool useStagingData) {
275 // Save the current clip and matrix information, which is local to this group.
276 SkAutoCanvasRestore saver(outCanvas, true);
277 // apply the current group's matrix to the canvas
Doris Liu4bbc2932015-12-01 17:59:40 -0800278 SkMatrix stackedMatrix;
Doris Liu1d8e1942016-03-02 15:16:28 -0800279 const GroupProperties& prop = useStagingData ? mStagingProperties : mProperties;
280 getLocalMatrix(&stackedMatrix, prop);
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400281 outCanvas->concat(stackedMatrix);
Doris Liu4bbc2932015-12-01 17:59:40 -0800282 // Draw the group tree in the same order as the XML file.
Doris Liuef062eb2016-02-04 16:16:27 -0800283 for (auto& child : mChildren) {
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400284 child->draw(outCanvas, useStagingData);
Doris Liu4bbc2932015-12-01 17:59:40 -0800285 }
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400286 // Restore the previous clip and matrix information.
Doris Liu4bbc2932015-12-01 17:59:40 -0800287}
288
289void Group::dump() {
290 ALOGD("Group %s has %zu children: ", mName.c_str(), mChildren.size());
Doris Liu1d8e1942016-03-02 15:16:28 -0800291 ALOGD("Group translateX, Y : %f, %f, scaleX, Y: %f, %f", mProperties.getTranslateX(),
John Reck1bcacfd2017-11-03 10:12:19 -0700292 mProperties.getTranslateY(), mProperties.getScaleX(), mProperties.getScaleY());
Doris Liu4bbc2932015-12-01 17:59:40 -0800293 for (size_t i = 0; i < mChildren.size(); i++) {
294 mChildren[i]->dump();
295 }
296}
297
Doris Liu1d8e1942016-03-02 15:16:28 -0800298void Group::syncProperties() {
299 // Copy over the dirty staging properties
300 if (mStagingPropertiesDirty) {
301 mProperties.syncProperties(mStagingProperties);
302 } else {
303 mStagingProperties.syncProperties(mProperties);
304 }
305 mStagingPropertiesDirty = false;
306 for (auto& child : mChildren) {
307 child->syncProperties();
308 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800309}
310
Doris Liu1d8e1942016-03-02 15:16:28 -0800311void Group::getLocalMatrix(SkMatrix* outMatrix, const GroupProperties& properties) {
Doris Liu4bbc2932015-12-01 17:59:40 -0800312 outMatrix->reset();
313 // TODO: use rotate(mRotate, mPivotX, mPivotY) and scale with pivot point, instead of
314 // translating to pivot for rotating and scaling, then translating back.
Doris Liu1d8e1942016-03-02 15:16:28 -0800315 outMatrix->postTranslate(-properties.getPivotX(), -properties.getPivotY());
316 outMatrix->postScale(properties.getScaleX(), properties.getScaleY());
317 outMatrix->postRotate(properties.getRotation(), 0, 0);
318 outMatrix->postTranslate(properties.getTranslateX() + properties.getPivotX(),
John Reck1bcacfd2017-11-03 10:12:19 -0700319 properties.getTranslateY() + properties.getPivotY());
Doris Liu4bbc2932015-12-01 17:59:40 -0800320}
321
322void Group::addChild(Node* child) {
Doris Liuef062eb2016-02-04 16:16:27 -0800323 mChildren.emplace_back(child);
Doris Liu1d8e1942016-03-02 15:16:28 -0800324 if (mPropertyChangedListener != nullptr) {
325 child->setPropertyChangedListener(mPropertyChangedListener);
326 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800327}
328
Doris Liu1d8e1942016-03-02 15:16:28 -0800329bool Group::GroupProperties::copyProperties(float* outProperties, int length) const {
330 int propertyCount = static_cast<int>(Property::count);
Doris Liu4bbc2932015-12-01 17:59:40 -0800331 if (length != propertyCount) {
332 LOG_ALWAYS_FATAL("Properties needs exactly %d bytes, a byte array of size %d is provided",
John Reck1bcacfd2017-11-03 10:12:19 -0700333 propertyCount, length);
Doris Liu4bbc2932015-12-01 17:59:40 -0800334 return false;
335 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800336
337 PrimitiveFields* out = reinterpret_cast<PrimitiveFields*>(outProperties);
338 *out = mPrimitiveFields;
Doris Liu4bbc2932015-12-01 17:59:40 -0800339 return true;
340}
341
Doris Liu766431a2016-02-04 22:17:11 +0000342// TODO: Consider animating the properties as float pointers
Doris Liu1d8e1942016-03-02 15:16:28 -0800343// Called on render thread
344float Group::GroupProperties::getPropertyValue(int propertyId) const {
Doris Liu766431a2016-02-04 22:17:11 +0000345 Property currentProperty = static_cast<Property>(propertyId);
346 switch (currentProperty) {
John Reck1bcacfd2017-11-03 10:12:19 -0700347 case Property::rotate:
348 return getRotation();
349 case Property::pivotX:
350 return getPivotX();
351 case Property::pivotY:
352 return getPivotY();
353 case Property::scaleX:
354 return getScaleX();
355 case Property::scaleY:
356 return getScaleY();
357 case Property::translateX:
358 return getTranslateX();
359 case Property::translateY:
360 return getTranslateY();
361 default:
362 LOG_ALWAYS_FATAL("Invalid property index: %d", propertyId);
363 return 0;
Doris Liu766431a2016-02-04 22:17:11 +0000364 }
365}
366
Doris Liu1d8e1942016-03-02 15:16:28 -0800367// Called on render thread
368void Group::GroupProperties::setPropertyValue(int propertyId, float value) {
Doris Liu766431a2016-02-04 22:17:11 +0000369 Property currentProperty = static_cast<Property>(propertyId);
370 switch (currentProperty) {
John Reck1bcacfd2017-11-03 10:12:19 -0700371 case Property::rotate:
372 setRotation(value);
373 break;
374 case Property::pivotX:
375 setPivotX(value);
376 break;
377 case Property::pivotY:
378 setPivotY(value);
379 break;
380 case Property::scaleX:
381 setScaleX(value);
382 break;
383 case Property::scaleY:
384 setScaleY(value);
385 break;
386 case Property::translateX:
387 setTranslateX(value);
388 break;
389 case Property::translateY:
390 setTranslateY(value);
391 break;
392 default:
393 LOG_ALWAYS_FATAL("Invalid property index: %d", propertyId);
Doris Liu766431a2016-02-04 22:17:11 +0000394 }
395}
396
397bool Group::isValidProperty(int propertyId) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800398 return GroupProperties::isValidProperty(propertyId);
399}
400
401bool Group::GroupProperties::isValidProperty(int propertyId) {
402 return propertyId >= 0 && propertyId < static_cast<int>(Property::count);
Doris Liu766431a2016-02-04 22:17:11 +0000403}
404
John Reck1bcacfd2017-11-03 10:12:19 -0700405int Tree::draw(Canvas* outCanvas, SkColorFilter* colorFilter, const SkRect& bounds,
406 bool needsMirroring, bool canReuseCache) {
Doris Liu4bbc2932015-12-01 17:59:40 -0800407 // The imageView can scale the canvas in different ways, in order to
408 // avoid blurry scaling, we have to draw into a bitmap with exact pixel
409 // size first. This bitmap size is determined by the bounds and the
410 // canvas scale.
Doris Liu1d8e1942016-03-02 15:16:28 -0800411 SkMatrix canvasMatrix;
412 outCanvas->getMatrix(&canvasMatrix);
Doris Liua0e61572015-12-29 14:57:49 -0800413 float canvasScaleX = 1.0f;
414 float canvasScaleY = 1.0f;
Doris Liu1d8e1942016-03-02 15:16:28 -0800415 if (canvasMatrix.getSkewX() == 0 && canvasMatrix.getSkewY() == 0) {
Doris Liua0e61572015-12-29 14:57:49 -0800416 // Only use the scale value when there's no skew or rotation in the canvas matrix.
Doris Liue410a352016-01-13 17:23:33 -0800417 // TODO: Add a cts test for drawing VD on a canvas with negative scaling factors.
Doris Liu1d8e1942016-03-02 15:16:28 -0800418 canvasScaleX = fabs(canvasMatrix.getScaleX());
419 canvasScaleY = fabs(canvasMatrix.getScaleY());
Doris Liua0e61572015-12-29 14:57:49 -0800420 }
John Reck1bcacfd2017-11-03 10:12:19 -0700421 int scaledWidth = (int)(bounds.width() * canvasScaleX);
422 int scaledHeight = (int)(bounds.height() * canvasScaleY);
Doris Liu4bbc2932015-12-01 17:59:40 -0800423 scaledWidth = std::min(Tree::MAX_CACHED_BITMAP_SIZE, scaledWidth);
424 scaledHeight = std::min(Tree::MAX_CACHED_BITMAP_SIZE, scaledHeight);
425
426 if (scaledWidth <= 0 || scaledHeight <= 0) {
Doris Liuf8d131c2016-04-29 18:41:29 -0700427 return 0;
Doris Liu4bbc2932015-12-01 17:59:40 -0800428 }
429
Doris Liu1d8e1942016-03-02 15:16:28 -0800430 mStagingProperties.setScaledSize(scaledWidth, scaledHeight);
Florin Malita777bf852016-02-03 10:48:55 -0500431 int saveCount = outCanvas->save(SaveFlags::MatrixClip);
Doris Liu1d8e1942016-03-02 15:16:28 -0800432 outCanvas->translate(bounds.fLeft, bounds.fTop);
Doris Liu4bbc2932015-12-01 17:59:40 -0800433
434 // Handle RTL mirroring.
435 if (needsMirroring) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800436 outCanvas->translate(bounds.width(), 0);
Doris Liu4bbc2932015-12-01 17:59:40 -0800437 outCanvas->scale(-1.0f, 1.0f);
438 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800439 mStagingProperties.setColorFilter(colorFilter);
Doris Liu4bbc2932015-12-01 17:59:40 -0800440
441 // At this point, canvas has been translated to the right position.
442 // And we use this bound for the destination rect for the drawBitmap, so
443 // we offset to (0, 0);
Doris Liu1d8e1942016-03-02 15:16:28 -0800444 SkRect tmpBounds = bounds;
445 tmpBounds.offsetTo(0, 0);
446 mStagingProperties.setBounds(tmpBounds);
Doris Liu766431a2016-02-04 22:17:11 +0000447 outCanvas->drawVectorDrawable(this);
Doris Liu4bbc2932015-12-01 17:59:40 -0800448 outCanvas->restoreToCount(saveCount);
Doris Liuf8d131c2016-04-29 18:41:29 -0700449 return scaledWidth * scaledHeight;
Doris Liu4bbc2932015-12-01 17:59:40 -0800450}
451
Doris Liu1d8e1942016-03-02 15:16:28 -0800452void Tree::drawStaging(Canvas* outCanvas) {
John Reck1bcacfd2017-11-03 10:12:19 -0700453 bool redrawNeeded = allocateBitmapIfNeeded(mStagingCache, mStagingProperties.getScaledWidth(),
454 mStagingProperties.getScaledHeight());
Doris Liu1d8e1942016-03-02 15:16:28 -0800455 // draw bitmap cache
456 if (redrawNeeded || mStagingCache.dirty) {
sergeyvfc9999502016-10-17 13:07:38 -0700457 updateBitmapCache(*mStagingCache.bitmap, true);
Doris Liu1d8e1942016-03-02 15:16:28 -0800458 mStagingCache.dirty = false;
Doris Liu4bbc2932015-12-01 17:59:40 -0800459 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800460
John Reck08ee8152018-09-20 16:27:46 -0700461 SkPaint paint;
462 getPaintFor(&paint, mStagingProperties);
John Reck1bcacfd2017-11-03 10:12:19 -0700463 outCanvas->drawBitmap(*mStagingCache.bitmap, 0, 0, mStagingCache.bitmap->width(),
464 mStagingCache.bitmap->height(), mStagingProperties.getBounds().left(),
465 mStagingProperties.getBounds().top(),
466 mStagingProperties.getBounds().right(),
John Reck08ee8152018-09-20 16:27:46 -0700467 mStagingProperties.getBounds().bottom(), &paint);
Doris Liu1d8e1942016-03-02 15:16:28 -0800468}
469
John Reck08ee8152018-09-20 16:27:46 -0700470void Tree::getPaintFor(SkPaint* outPaint, const TreeProperties &prop) const {
Stan Iliev038fc372018-07-30 18:31:46 -0400471 // HWUI always draws VD with bilinear filtering.
472 outPaint->setFilterQuality(kLow_SkFilterQuality);
Doris Liu7cc6ec22018-10-02 16:15:57 -0700473 if (prop.getColorFilter() != nullptr) {
John Reck08ee8152018-09-20 16:27:46 -0700474 outPaint->setColorFilter(sk_ref_sp(prop.getColorFilter()));
Doris Liu1d8e1942016-03-02 15:16:28 -0800475 }
Doris Liu7cc6ec22018-10-02 16:15:57 -0700476 outPaint->setAlpha(prop.getRootAlpha() * 255);
Doris Liu4bbc2932015-12-01 17:59:40 -0800477}
478
sergeyvfc9999502016-10-17 13:07:38 -0700479Bitmap& Tree::getBitmapUpdateIfDirty() {
480 bool redrawNeeded = allocateBitmapIfNeeded(mCache, mProperties.getScaledWidth(),
John Reck1bcacfd2017-11-03 10:12:19 -0700481 mProperties.getScaledHeight());
Doris Liu1d8e1942016-03-02 15:16:28 -0800482 if (redrawNeeded || mCache.dirty) {
sergeyvfc9999502016-10-17 13:07:38 -0700483 updateBitmapCache(*mCache.bitmap, false);
Doris Liu1d8e1942016-03-02 15:16:28 -0800484 mCache.dirty = false;
485 }
sergeyvfc9999502016-10-17 13:07:38 -0700486 return *mCache.bitmap;
Doris Liu4bbc2932015-12-01 17:59:40 -0800487}
488
Stan Iliev3310fb12017-03-23 16:56:51 -0400489void Tree::updateCache(sp<skiapipeline::VectorDrawableAtlas>& atlas, GrContext* context) {
490 SkRect dst;
491 sk_sp<SkSurface> surface = mCache.getSurface(&dst);
John Reck1bcacfd2017-11-03 10:12:19 -0700492 bool canReuseSurface = surface && dst.width() >= mProperties.getScaledWidth() &&
493 dst.height() >= mProperties.getScaledHeight();
Stan Iliev3310fb12017-03-23 16:56:51 -0400494 if (!canReuseSurface) {
495 int scaledWidth = SkScalarCeilToInt(mProperties.getScaledWidth());
496 int scaledHeight = SkScalarCeilToInt(mProperties.getScaledHeight());
497 auto atlasEntry = atlas->requestNewEntry(scaledWidth, scaledHeight, context);
498 if (INVALID_ATLAS_KEY != atlasEntry.key) {
499 dst = atlasEntry.rect;
500 surface = atlasEntry.surface;
501 mCache.setAtlas(atlas, atlasEntry.key);
502 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700503 // don't draw, if we failed to allocate an offscreen buffer
Stan Iliev3310fb12017-03-23 16:56:51 -0400504 mCache.clear();
505 surface.reset();
506 }
Stan Iliev23c38a92017-03-23 00:12:50 -0400507 }
Stan Iliev3310fb12017-03-23 16:56:51 -0400508 if (!canReuseSurface || mCache.dirty) {
Derek Sollenberger7fe53c12017-08-31 15:40:12 -0400509 if (surface) {
510 Bitmap& bitmap = getBitmapUpdateIfDirty();
511 SkBitmap skiaBitmap;
512 bitmap.getSkBitmap(&skiaBitmap);
Mike Reed6b6e66d2018-02-08 16:28:53 -0500513 surface->writePixels(skiaBitmap, dst.fLeft, dst.fTop);
Derek Sollenberger7fe53c12017-08-31 15:40:12 -0400514 }
Stan Iliev23c38a92017-03-23 00:12:50 -0400515 mCache.dirty = false;
Stan Iliev23c38a92017-03-23 00:12:50 -0400516 }
517}
518
Stan Iliev3310fb12017-03-23 16:56:51 -0400519void Tree::Cache::setAtlas(sp<skiapipeline::VectorDrawableAtlas> newAtlas,
John Reck1bcacfd2017-11-03 10:12:19 -0700520 skiapipeline::AtlasKey newAtlasKey) {
Stan Iliev3310fb12017-03-23 16:56:51 -0400521 LOG_ALWAYS_FATAL_IF(newAtlasKey == INVALID_ATLAS_KEY);
522 clear();
523 mAtlas = newAtlas;
524 mAtlasKey = newAtlasKey;
525}
526
527sk_sp<SkSurface> Tree::Cache::getSurface(SkRect* bounds) {
528 sk_sp<SkSurface> surface;
529 sp<skiapipeline::VectorDrawableAtlas> atlas = mAtlas.promote();
530 if (atlas.get() && mAtlasKey != INVALID_ATLAS_KEY) {
531 auto atlasEntry = atlas->getEntry(mAtlasKey);
532 *bounds = atlasEntry.rect;
533 surface = atlasEntry.surface;
534 mAtlasKey = atlasEntry.key;
535 }
536
537 return surface;
538}
539
540void Tree::Cache::clear() {
541 sp<skiapipeline::VectorDrawableAtlas> lockAtlas = mAtlas.promote();
542 if (lockAtlas.get()) {
543 lockAtlas->releaseEntry(mAtlasKey);
544 }
545 mAtlas = nullptr;
546 mAtlasKey = INVALID_ATLAS_KEY;
547}
548
John Reck08ee8152018-09-20 16:27:46 -0700549void Tree::draw(SkCanvas* canvas, const SkRect& bounds, const SkPaint& inPaint) {
550 // Update the paint for any animatable properties
551 SkPaint paint = inPaint;
552 paint.setAlpha(mProperties.getRootAlpha() * 255);
553
Stan Iliev3310fb12017-03-23 16:56:51 -0400554 SkRect src;
555 sk_sp<SkSurface> vdSurface = mCache.getSurface(&src);
556 if (vdSurface) {
John Reck08ee8152018-09-20 16:27:46 -0700557 canvas->drawImageRect(vdSurface->makeImageSnapshot().get(), src, bounds, &paint,
John Recke170fb62018-05-07 08:12:07 -0700558 SkCanvas::kFast_SrcRectConstraint);
Stan Iliev3310fb12017-03-23 16:56:51 -0400559 } else {
560 // Handle the case when VectorDrawableAtlas has been destroyed, because of memory pressure.
561 // We render the VD into a temporary standalone buffer and mark the frame as dirty. Next
562 // frame will be cached into the atlas.
Derek Sollenberger7fe53c12017-08-31 15:40:12 -0400563 Bitmap& bitmap = getBitmapUpdateIfDirty();
564 SkBitmap skiaBitmap;
565 bitmap.getSkBitmap(&skiaBitmap);
566
Stan Iliev3310fb12017-03-23 16:56:51 -0400567 int scaledWidth = SkScalarCeilToInt(mProperties.getScaledWidth());
568 int scaledHeight = SkScalarCeilToInt(mProperties.getScaledHeight());
John Recke170fb62018-05-07 08:12:07 -0700569 canvas->drawBitmapRect(skiaBitmap, SkRect::MakeWH(scaledWidth, scaledHeight), bounds,
John Reck08ee8152018-09-20 16:27:46 -0700570 &paint, SkCanvas::kFast_SrcRectConstraint);
Stan Iliev3310fb12017-03-23 16:56:51 -0400571 mCache.clear();
Stan Iliev3310fb12017-03-23 16:56:51 -0400572 markDirty();
573 }
Stan Iliev23c38a92017-03-23 00:12:50 -0400574}
575
sergeyvfc9999502016-10-17 13:07:38 -0700576void Tree::updateBitmapCache(Bitmap& bitmap, bool useStagingData) {
577 SkBitmap outCache;
578 bitmap.getSkBitmap(&outCache);
ztenghuicf0c41d2017-09-13 10:32:50 -0700579 int cacheWidth = outCache.width();
580 int cacheHeight = outCache.height();
581 ATRACE_FORMAT("VectorDrawable repaint %dx%d", cacheWidth, cacheHeight);
sergeyvfc9999502016-10-17 13:07:38 -0700582 outCache.eraseColor(SK_ColorTRANSPARENT);
583 SkCanvas outCanvas(outCache);
John Reck1bcacfd2017-11-03 10:12:19 -0700584 float viewportWidth =
585 useStagingData ? mStagingProperties.getViewportWidth() : mProperties.getViewportWidth();
586 float viewportHeight = useStagingData ? mStagingProperties.getViewportHeight()
587 : mProperties.getViewportHeight();
ztenghuicf0c41d2017-09-13 10:32:50 -0700588 float scaleX = cacheWidth / viewportWidth;
589 float scaleY = cacheHeight / viewportHeight;
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400590 outCanvas.scale(scaleX, scaleY);
591 mRootNode->draw(&outCanvas, useStagingData);
Doris Liu1d8e1942016-03-02 15:16:28 -0800592}
593
sergeyvfc9999502016-10-17 13:07:38 -0700594bool Tree::allocateBitmapIfNeeded(Cache& cache, int width, int height) {
595 if (!canReuseBitmap(cache.bitmap.get(), width, height)) {
Romain Guy253f2c22016-09-28 17:34:42 -0700596#ifndef ANDROID_ENABLE_LINEAR_BLENDING
597 sk_sp<SkColorSpace> colorSpace = nullptr;
598#else
Matt Sarett89ddb1f2017-02-10 13:31:56 -0500599 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeSRGB();
Romain Guy253f2c22016-09-28 17:34:42 -0700600#endif
601 SkImageInfo info = SkImageInfo::MakeN32(width, height, kPremul_SkAlphaType, colorSpace);
sergeyvfc9999502016-10-17 13:07:38 -0700602 cache.bitmap = Bitmap::allocateHeapBitmap(info);
Doris Liu1d8e1942016-03-02 15:16:28 -0800603 return true;
Doris Liu4bbc2932015-12-01 17:59:40 -0800604 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800605 return false;
Doris Liu4bbc2932015-12-01 17:59:40 -0800606}
607
sergeyvfc9999502016-10-17 13:07:38 -0700608bool Tree::canReuseBitmap(Bitmap* bitmap, int width, int height) {
Teng-Hui Zhu037fc182016-11-16 10:29:39 -0800609 return bitmap && width <= bitmap->width() && height <= bitmap->height();
Doris Liu1d8e1942016-03-02 15:16:28 -0800610}
611
612void Tree::onPropertyChanged(TreeProperties* prop) {
613 if (prop == &mStagingProperties) {
614 mStagingCache.dirty = true;
615 } else {
616 mCache.dirty = true;
617 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800618}
619
John Reck08ee8152018-09-20 16:27:46 -0700620class MinMaxAverage {
621public:
622 void add(float sample) {
623 if (mCount == 0) {
624 mMin = sample;
625 mMax = sample;
626 } else {
627 mMin = std::min(mMin, sample);
628 mMax = std::max(mMax, sample);
629 }
630 mTotal += sample;
631 mCount++;
632 }
633
634 float average() { return mTotal / mCount; }
635
636 float min() { return mMin; }
637
638 float max() { return mMax; }
639
640 float delta() { return mMax - mMin; }
641
642private:
643 float mMin = 0.0f;
644 float mMax = 0.0f;
645 float mTotal = 0.0f;
646 int mCount = 0;
647};
648
649BitmapPalette Tree::computePalette() {
650 // TODO Cache this and share the code with Bitmap.cpp
651
652 ATRACE_CALL();
653
654 // TODO: This calculation of converting to HSV & tracking min/max is probably overkill
655 // Experiment with something simpler since we just want to figure out if it's "color-ful"
656 // and then the average perceptual lightness.
657
658 MinMaxAverage hue, saturation, value;
659 int sampledCount = 0;
660
661 // Sample a grid of 100 pixels to get an overall estimation of the colors in play
662 mRootNode->forEachFillColor([&](SkColor color) {
663 if (SkColorGetA(color) < 75) {
664 return;
665 }
666 sampledCount++;
667 float hsv[3];
668 SkColorToHSV(color, hsv);
669 hue.add(hsv[0]);
670 saturation.add(hsv[1]);
671 value.add(hsv[2]);
672 });
673
674 if (sampledCount == 0) {
675 ALOGV("VectorDrawable is mostly translucent");
676 return BitmapPalette::Unknown;
677 }
678
679 ALOGV("samples = %d, hue [min = %f, max = %f, avg = %f]; saturation [min = %f, max = %f, avg = "
680 "%f]; value [min = %f, max = %f, avg = %f]",
681 sampledCount, hue.min(), hue.max(), hue.average(), saturation.min(), saturation.max(),
682 saturation.average(), value.min(), value.max(), value.average());
683
684 if (hue.delta() <= 20 && saturation.delta() <= .1f) {
685 if (value.average() >= .5f) {
686 return BitmapPalette::Light;
687 } else {
688 return BitmapPalette::Dark;
689 }
690 }
691 return BitmapPalette::Unknown;
692}
693
Chris Blume7b8a8082018-11-30 15:51:58 -0800694} // namespace VectorDrawable
Doris Liu4bbc2932015-12-01 17:59:40 -0800695
Chris Blume7b8a8082018-11-30 15:51:58 -0800696} // namespace uirenderer
697} // namespace android