blob: f662406bdc386d10b87c985f483d43a831c1d239 [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
461 SkPaint tmpPaint;
462 SkPaint* paint = updatePaint(&tmpPaint, &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(),
467 mStagingProperties.getBounds().bottom(), paint);
Doris Liu1d8e1942016-03-02 15:16:28 -0800468}
469
470SkPaint* Tree::getPaint() {
471 return updatePaint(&mPaint, &mProperties);
472}
473
474// Update the given paint with alpha and color filter. Return nullptr if no color filter is
475// specified and root alpha is 1. Otherwise, return updated paint.
476SkPaint* Tree::updatePaint(SkPaint* outPaint, TreeProperties* prop) {
477 if (prop->getRootAlpha() == 1.0f && prop->getColorFilter() == nullptr) {
478 return nullptr;
479 } else {
Mike Reed260ab722016-10-07 15:59:20 -0400480 outPaint->setColorFilter(sk_ref_sp(prop->getColorFilter()));
Doris Liu1d8e1942016-03-02 15:16:28 -0800481 outPaint->setFilterQuality(kLow_SkFilterQuality);
482 outPaint->setAlpha(prop->getRootAlpha() * 255);
483 return outPaint;
484 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800485}
486
sergeyvfc9999502016-10-17 13:07:38 -0700487Bitmap& Tree::getBitmapUpdateIfDirty() {
488 bool redrawNeeded = allocateBitmapIfNeeded(mCache, mProperties.getScaledWidth(),
John Reck1bcacfd2017-11-03 10:12:19 -0700489 mProperties.getScaledHeight());
Doris Liu1d8e1942016-03-02 15:16:28 -0800490 if (redrawNeeded || mCache.dirty) {
sergeyvfc9999502016-10-17 13:07:38 -0700491 updateBitmapCache(*mCache.bitmap, false);
Doris Liu1d8e1942016-03-02 15:16:28 -0800492 mCache.dirty = false;
493 }
sergeyvfc9999502016-10-17 13:07:38 -0700494 return *mCache.bitmap;
Doris Liu4bbc2932015-12-01 17:59:40 -0800495}
496
Stan Iliev3310fb12017-03-23 16:56:51 -0400497void Tree::updateCache(sp<skiapipeline::VectorDrawableAtlas>& atlas, GrContext* context) {
498 SkRect dst;
499 sk_sp<SkSurface> surface = mCache.getSurface(&dst);
John Reck1bcacfd2017-11-03 10:12:19 -0700500 bool canReuseSurface = surface && dst.width() >= mProperties.getScaledWidth() &&
501 dst.height() >= mProperties.getScaledHeight();
Stan Iliev3310fb12017-03-23 16:56:51 -0400502 if (!canReuseSurface) {
503 int scaledWidth = SkScalarCeilToInt(mProperties.getScaledWidth());
504 int scaledHeight = SkScalarCeilToInt(mProperties.getScaledHeight());
505 auto atlasEntry = atlas->requestNewEntry(scaledWidth, scaledHeight, context);
506 if (INVALID_ATLAS_KEY != atlasEntry.key) {
507 dst = atlasEntry.rect;
508 surface = atlasEntry.surface;
509 mCache.setAtlas(atlas, atlasEntry.key);
510 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700511 // don't draw, if we failed to allocate an offscreen buffer
Stan Iliev3310fb12017-03-23 16:56:51 -0400512 mCache.clear();
513 surface.reset();
514 }
Stan Iliev23c38a92017-03-23 00:12:50 -0400515 }
Stan Iliev3310fb12017-03-23 16:56:51 -0400516 if (!canReuseSurface || mCache.dirty) {
Derek Sollenberger7fe53c12017-08-31 15:40:12 -0400517 if (surface) {
518 Bitmap& bitmap = getBitmapUpdateIfDirty();
519 SkBitmap skiaBitmap;
520 bitmap.getSkBitmap(&skiaBitmap);
521 if (!surface->getCanvas()->writePixels(skiaBitmap, dst.fLeft, dst.fTop)) {
522 ALOGD("VectorDrawable caching failed to efficiently upload");
523 surface->getCanvas()->drawBitmap(skiaBitmap, dst.fLeft, dst.fTop);
524 }
525 }
Stan Iliev23c38a92017-03-23 00:12:50 -0400526 mCache.dirty = false;
Stan Iliev23c38a92017-03-23 00:12:50 -0400527 }
528}
529
Stan Iliev3310fb12017-03-23 16:56:51 -0400530void Tree::Cache::setAtlas(sp<skiapipeline::VectorDrawableAtlas> newAtlas,
John Reck1bcacfd2017-11-03 10:12:19 -0700531 skiapipeline::AtlasKey newAtlasKey) {
Stan Iliev3310fb12017-03-23 16:56:51 -0400532 LOG_ALWAYS_FATAL_IF(newAtlasKey == INVALID_ATLAS_KEY);
533 clear();
534 mAtlas = newAtlas;
535 mAtlasKey = newAtlasKey;
536}
537
538sk_sp<SkSurface> Tree::Cache::getSurface(SkRect* bounds) {
539 sk_sp<SkSurface> surface;
540 sp<skiapipeline::VectorDrawableAtlas> atlas = mAtlas.promote();
541 if (atlas.get() && mAtlasKey != INVALID_ATLAS_KEY) {
542 auto atlasEntry = atlas->getEntry(mAtlasKey);
543 *bounds = atlasEntry.rect;
544 surface = atlasEntry.surface;
545 mAtlasKey = atlasEntry.key;
546 }
547
548 return surface;
549}
550
551void Tree::Cache::clear() {
552 sp<skiapipeline::VectorDrawableAtlas> lockAtlas = mAtlas.promote();
553 if (lockAtlas.get()) {
554 lockAtlas->releaseEntry(mAtlasKey);
555 }
556 mAtlas = nullptr;
557 mAtlasKey = INVALID_ATLAS_KEY;
558}
559
Stan Iliev65e678f2018-02-07 14:07:30 -0500560void Tree::draw(SkCanvas* canvas, const SkRect& bounds) {
Stan Iliev3310fb12017-03-23 16:56:51 -0400561 SkRect src;
562 sk_sp<SkSurface> vdSurface = mCache.getSurface(&src);
563 if (vdSurface) {
564 canvas->drawImageRect(vdSurface->makeImageSnapshot().get(), src,
Stan Iliev65e678f2018-02-07 14:07:30 -0500565 bounds, getPaint(), SkCanvas::kFast_SrcRectConstraint);
Stan Iliev3310fb12017-03-23 16:56:51 -0400566 } else {
567 // Handle the case when VectorDrawableAtlas has been destroyed, because of memory pressure.
568 // We render the VD into a temporary standalone buffer and mark the frame as dirty. Next
569 // frame will be cached into the atlas.
Derek Sollenberger7fe53c12017-08-31 15:40:12 -0400570 Bitmap& bitmap = getBitmapUpdateIfDirty();
571 SkBitmap skiaBitmap;
572 bitmap.getSkBitmap(&skiaBitmap);
573
Stan Iliev3310fb12017-03-23 16:56:51 -0400574 int scaledWidth = SkScalarCeilToInt(mProperties.getScaledWidth());
575 int scaledHeight = SkScalarCeilToInt(mProperties.getScaledHeight());
Derek Sollenberger7fe53c12017-08-31 15:40:12 -0400576 canvas->drawBitmapRect(skiaBitmap, SkRect::MakeWH(scaledWidth, scaledHeight),
Stan Iliev65e678f2018-02-07 14:07:30 -0500577 bounds, getPaint(), SkCanvas::kFast_SrcRectConstraint);
Stan Iliev3310fb12017-03-23 16:56:51 -0400578 mCache.clear();
Stan Iliev3310fb12017-03-23 16:56:51 -0400579 markDirty();
580 }
Stan Iliev23c38a92017-03-23 00:12:50 -0400581}
582
sergeyvfc9999502016-10-17 13:07:38 -0700583void Tree::updateBitmapCache(Bitmap& bitmap, bool useStagingData) {
584 SkBitmap outCache;
585 bitmap.getSkBitmap(&outCache);
ztenghuicf0c41d2017-09-13 10:32:50 -0700586 int cacheWidth = outCache.width();
587 int cacheHeight = outCache.height();
588 ATRACE_FORMAT("VectorDrawable repaint %dx%d", cacheWidth, cacheHeight);
sergeyvfc9999502016-10-17 13:07:38 -0700589 outCache.eraseColor(SK_ColorTRANSPARENT);
590 SkCanvas outCanvas(outCache);
John Reck1bcacfd2017-11-03 10:12:19 -0700591 float viewportWidth =
592 useStagingData ? mStagingProperties.getViewportWidth() : mProperties.getViewportWidth();
593 float viewportHeight = useStagingData ? mStagingProperties.getViewportHeight()
594 : mProperties.getViewportHeight();
ztenghuicf0c41d2017-09-13 10:32:50 -0700595 float scaleX = cacheWidth / viewportWidth;
596 float scaleY = cacheHeight / viewportHeight;
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400597 outCanvas.scale(scaleX, scaleY);
598 mRootNode->draw(&outCanvas, useStagingData);
Doris Liu1d8e1942016-03-02 15:16:28 -0800599}
600
sergeyvfc9999502016-10-17 13:07:38 -0700601bool Tree::allocateBitmapIfNeeded(Cache& cache, int width, int height) {
602 if (!canReuseBitmap(cache.bitmap.get(), width, height)) {
Romain Guy253f2c22016-09-28 17:34:42 -0700603#ifndef ANDROID_ENABLE_LINEAR_BLENDING
604 sk_sp<SkColorSpace> colorSpace = nullptr;
605#else
Matt Sarett89ddb1f2017-02-10 13:31:56 -0500606 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeSRGB();
Romain Guy253f2c22016-09-28 17:34:42 -0700607#endif
608 SkImageInfo info = SkImageInfo::MakeN32(width, height, kPremul_SkAlphaType, colorSpace);
sergeyvfc9999502016-10-17 13:07:38 -0700609 cache.bitmap = Bitmap::allocateHeapBitmap(info);
Doris Liu1d8e1942016-03-02 15:16:28 -0800610 return true;
Doris Liu4bbc2932015-12-01 17:59:40 -0800611 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800612 return false;
Doris Liu4bbc2932015-12-01 17:59:40 -0800613}
614
sergeyvfc9999502016-10-17 13:07:38 -0700615bool Tree::canReuseBitmap(Bitmap* bitmap, int width, int height) {
Teng-Hui Zhu037fc182016-11-16 10:29:39 -0800616 return bitmap && width <= bitmap->width() && height <= bitmap->height();
Doris Liu1d8e1942016-03-02 15:16:28 -0800617}
618
619void Tree::onPropertyChanged(TreeProperties* prop) {
620 if (prop == &mStagingProperties) {
621 mStagingCache.dirty = true;
622 } else {
623 mCache.dirty = true;
624 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800625}
626
John Reck1bcacfd2017-11-03 10:12:19 -0700627}; // namespace VectorDrawable
Doris Liu4bbc2932015-12-01 17:59:40 -0800628
John Reck1bcacfd2017-11-03 10:12:19 -0700629}; // namespace uirenderer
630}; // namespace android