blob: 376371de80dce4b25ee01628c2284f6fa7ca17a8 [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
19#include "PathParser.h"
Doris Liu1d8e1942016-03-02 15:16:28 -080020#include "SkColorFilter.h"
Doris Liu4bbc2932015-12-01 17:59:40 -080021#include "SkImageInfo.h"
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -080022#include "SkShader.h"
Doris Liu4bbc2932015-12-01 17:59:40 -080023#include <utils/Log.h>
24#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,
82 float trimPathOffset) {
83 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);
112 SkPath *outPath;
113 if (useStagingData) {
114 SkPath inPath = *tempStagingPath;
115 applyTrim(tempStagingPath, inPath, mStagingProperties.getTrimPathStart(),
116 mStagingProperties.getTrimPathEnd(), mStagingProperties.getTrimPathOffset());
117 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(),
122 mProperties.getTrimPathEnd(), mProperties.getTrimPathOffset());
123 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;
129 bool setFillPath = properties.getFillGradient() != nullptr
130 || properties.getFillColor() != SK_ColorTRANSPARENT;
131 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",
141 mProperties.getStrokeWidth(), mProperties.getStrokeColor(), mProperties.getStrokeAlpha(),
142 mProperties.getFillColor(), mProperties.getFillAlpha());
143}
144
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);
170 paint.setAntiAlias(true);
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);
186 paint.setAntiAlias(true);
187 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",
216 propertyDataSize, length);
217 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 {
Doris Liu1d8e1942016-03-02 15:16:28 -0800232 LOG_ALWAYS_FATAL("Error setting color property on FullPath: No valid property"
233 " with id: %d", propertyId);
Doris Liu766431a2016-02-04 22:17:11 +0000234 }
235}
236
Doris Liu1d8e1942016-03-02 15:16:28 -0800237void FullPath::FullPathProperties::setPropertyValue(int propertyId, float value) {
Doris Liu766431a2016-02-04 22:17:11 +0000238 Property property = static_cast<Property>(propertyId);
239 switch (property) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800240 case Property::strokeWidth:
Doris Liu766431a2016-02-04 22:17:11 +0000241 setStrokeWidth(value);
242 break;
Doris Liu1d8e1942016-03-02 15:16:28 -0800243 case Property::strokeAlpha:
Doris Liu766431a2016-02-04 22:17:11 +0000244 setStrokeAlpha(value);
245 break;
Doris Liu1d8e1942016-03-02 15:16:28 -0800246 case Property::fillAlpha:
Doris Liu766431a2016-02-04 22:17:11 +0000247 setFillAlpha(value);
248 break;
Doris Liu1d8e1942016-03-02 15:16:28 -0800249 case Property::trimPathStart:
Doris Liu766431a2016-02-04 22:17:11 +0000250 setTrimPathStart(value);
251 break;
Doris Liu1d8e1942016-03-02 15:16:28 -0800252 case Property::trimPathEnd:
Doris Liu766431a2016-02-04 22:17:11 +0000253 setTrimPathEnd(value);
254 break;
Doris Liu1d8e1942016-03-02 15:16:28 -0800255 case Property::trimPathOffset:
Doris Liu766431a2016-02-04 22:17:11 +0000256 setTrimPathOffset(value);
257 break;
258 default:
259 LOG_ALWAYS_FATAL("Invalid property id: %d for animation", propertyId);
260 break;
261 }
262}
263
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400264void ClipPath::draw(SkCanvas* outCanvas, bool useStagingData) {
265 SkPath tempStagingPath;
266 outCanvas->clipPath(getUpdatedPath(useStagingData, &tempStagingPath));
Doris Liu4bbc2932015-12-01 17:59:40 -0800267}
268
269Group::Group(const Group& group) : Node(group) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800270 mStagingProperties.syncProperties(group.mStagingProperties);
Doris Liu4bbc2932015-12-01 17:59:40 -0800271}
272
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400273void Group::draw(SkCanvas* outCanvas, bool useStagingData) {
274 // Save the current clip and matrix information, which is local to this group.
275 SkAutoCanvasRestore saver(outCanvas, true);
276 // apply the current group's matrix to the canvas
Doris Liu4bbc2932015-12-01 17:59:40 -0800277 SkMatrix stackedMatrix;
Doris Liu1d8e1942016-03-02 15:16:28 -0800278 const GroupProperties& prop = useStagingData ? mStagingProperties : mProperties;
279 getLocalMatrix(&stackedMatrix, prop);
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400280 outCanvas->concat(stackedMatrix);
Doris Liu4bbc2932015-12-01 17:59:40 -0800281 // Draw the group tree in the same order as the XML file.
Doris Liuef062eb2016-02-04 16:16:27 -0800282 for (auto& child : mChildren) {
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400283 child->draw(outCanvas, useStagingData);
Doris Liu4bbc2932015-12-01 17:59:40 -0800284 }
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400285 // Restore the previous clip and matrix information.
Doris Liu4bbc2932015-12-01 17:59:40 -0800286}
287
288void Group::dump() {
289 ALOGD("Group %s has %zu children: ", mName.c_str(), mChildren.size());
Doris Liu1d8e1942016-03-02 15:16:28 -0800290 ALOGD("Group translateX, Y : %f, %f, scaleX, Y: %f, %f", mProperties.getTranslateX(),
291 mProperties.getTranslateY(), mProperties.getScaleX(), mProperties.getScaleY());
Doris Liu4bbc2932015-12-01 17:59:40 -0800292 for (size_t i = 0; i < mChildren.size(); i++) {
293 mChildren[i]->dump();
294 }
295}
296
Doris Liu1d8e1942016-03-02 15:16:28 -0800297void Group::syncProperties() {
298 // Copy over the dirty staging properties
299 if (mStagingPropertiesDirty) {
300 mProperties.syncProperties(mStagingProperties);
301 } else {
302 mStagingProperties.syncProperties(mProperties);
303 }
304 mStagingPropertiesDirty = false;
305 for (auto& child : mChildren) {
306 child->syncProperties();
307 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800308}
309
Doris Liu1d8e1942016-03-02 15:16:28 -0800310void Group::getLocalMatrix(SkMatrix* outMatrix, const GroupProperties& properties) {
Doris Liu4bbc2932015-12-01 17:59:40 -0800311 outMatrix->reset();
312 // TODO: use rotate(mRotate, mPivotX, mPivotY) and scale with pivot point, instead of
313 // translating to pivot for rotating and scaling, then translating back.
Doris Liu1d8e1942016-03-02 15:16:28 -0800314 outMatrix->postTranslate(-properties.getPivotX(), -properties.getPivotY());
315 outMatrix->postScale(properties.getScaleX(), properties.getScaleY());
316 outMatrix->postRotate(properties.getRotation(), 0, 0);
317 outMatrix->postTranslate(properties.getTranslateX() + properties.getPivotX(),
318 properties.getTranslateY() + properties.getPivotY());
Doris Liu4bbc2932015-12-01 17:59:40 -0800319}
320
321void Group::addChild(Node* child) {
Doris Liuef062eb2016-02-04 16:16:27 -0800322 mChildren.emplace_back(child);
Doris Liu1d8e1942016-03-02 15:16:28 -0800323 if (mPropertyChangedListener != nullptr) {
324 child->setPropertyChangedListener(mPropertyChangedListener);
325 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800326}
327
Doris Liu1d8e1942016-03-02 15:16:28 -0800328bool Group::GroupProperties::copyProperties(float* outProperties, int length) const {
329 int propertyCount = static_cast<int>(Property::count);
Doris Liu4bbc2932015-12-01 17:59:40 -0800330 if (length != propertyCount) {
331 LOG_ALWAYS_FATAL("Properties needs exactly %d bytes, a byte array of size %d is provided",
332 propertyCount, length);
333 return false;
334 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800335
336 PrimitiveFields* out = reinterpret_cast<PrimitiveFields*>(outProperties);
337 *out = mPrimitiveFields;
Doris Liu4bbc2932015-12-01 17:59:40 -0800338 return true;
339}
340
Doris Liu766431a2016-02-04 22:17:11 +0000341// TODO: Consider animating the properties as float pointers
Doris Liu1d8e1942016-03-02 15:16:28 -0800342// Called on render thread
343float Group::GroupProperties::getPropertyValue(int propertyId) const {
Doris Liu766431a2016-02-04 22:17:11 +0000344 Property currentProperty = static_cast<Property>(propertyId);
345 switch (currentProperty) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800346 case Property::rotate:
347 return getRotation();
348 case Property::pivotX:
349 return getPivotX();
350 case Property::pivotY:
351 return getPivotY();
352 case Property::scaleX:
353 return getScaleX();
354 case Property::scaleY:
355 return getScaleY();
356 case Property::translateX:
357 return getTranslateX();
358 case Property::translateY:
359 return getTranslateY();
Doris Liu766431a2016-02-04 22:17:11 +0000360 default:
361 LOG_ALWAYS_FATAL("Invalid property index: %d", propertyId);
362 return 0;
363 }
364}
365
Doris Liu1d8e1942016-03-02 15:16:28 -0800366// Called on render thread
367void Group::GroupProperties::setPropertyValue(int propertyId, float value) {
Doris Liu766431a2016-02-04 22:17:11 +0000368 Property currentProperty = static_cast<Property>(propertyId);
369 switch (currentProperty) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800370 case Property::rotate:
371 setRotation(value);
Doris Liu766431a2016-02-04 22:17:11 +0000372 break;
Doris Liu1d8e1942016-03-02 15:16:28 -0800373 case Property::pivotX:
374 setPivotX(value);
Doris Liu766431a2016-02-04 22:17:11 +0000375 break;
Doris Liu1d8e1942016-03-02 15:16:28 -0800376 case Property::pivotY:
377 setPivotY(value);
Doris Liu766431a2016-02-04 22:17:11 +0000378 break;
Doris Liu1d8e1942016-03-02 15:16:28 -0800379 case Property::scaleX:
380 setScaleX(value);
Doris Liu766431a2016-02-04 22:17:11 +0000381 break;
Doris Liu1d8e1942016-03-02 15:16:28 -0800382 case Property::scaleY:
383 setScaleY(value);
Doris Liu766431a2016-02-04 22:17:11 +0000384 break;
Doris Liu1d8e1942016-03-02 15:16:28 -0800385 case Property::translateX:
386 setTranslateX(value);
Doris Liu766431a2016-02-04 22:17:11 +0000387 break;
Doris Liu1d8e1942016-03-02 15:16:28 -0800388 case Property::translateY:
389 setTranslateY(value);
Doris Liu766431a2016-02-04 22:17:11 +0000390 break;
391 default:
392 LOG_ALWAYS_FATAL("Invalid property index: %d", propertyId);
393 }
394}
395
396bool Group::isValidProperty(int propertyId) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800397 return GroupProperties::isValidProperty(propertyId);
398}
399
400bool Group::GroupProperties::isValidProperty(int propertyId) {
401 return propertyId >= 0 && propertyId < static_cast<int>(Property::count);
Doris Liu766431a2016-02-04 22:17:11 +0000402}
403
Doris Liuf8d131c2016-04-29 18:41:29 -0700404int Tree::draw(Canvas* outCanvas, SkColorFilter* colorFilter,
Doris Liu4bbc2932015-12-01 17:59:40 -0800405 const SkRect& bounds, bool needsMirroring, bool canReuseCache) {
406 // The imageView can scale the canvas in different ways, in order to
407 // avoid blurry scaling, we have to draw into a bitmap with exact pixel
408 // size first. This bitmap size is determined by the bounds and the
409 // canvas scale.
Doris Liu1d8e1942016-03-02 15:16:28 -0800410 SkMatrix canvasMatrix;
411 outCanvas->getMatrix(&canvasMatrix);
Doris Liua0e61572015-12-29 14:57:49 -0800412 float canvasScaleX = 1.0f;
413 float canvasScaleY = 1.0f;
Doris Liu1d8e1942016-03-02 15:16:28 -0800414 if (canvasMatrix.getSkewX() == 0 && canvasMatrix.getSkewY() == 0) {
Doris Liua0e61572015-12-29 14:57:49 -0800415 // Only use the scale value when there's no skew or rotation in the canvas matrix.
Doris Liue410a352016-01-13 17:23:33 -0800416 // TODO: Add a cts test for drawing VD on a canvas with negative scaling factors.
Doris Liu1d8e1942016-03-02 15:16:28 -0800417 canvasScaleX = fabs(canvasMatrix.getScaleX());
418 canvasScaleY = fabs(canvasMatrix.getScaleY());
Doris Liua0e61572015-12-29 14:57:49 -0800419 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800420 int scaledWidth = (int) (bounds.width() * canvasScaleX);
421 int scaledHeight = (int) (bounds.height() * canvasScaleY);
Doris Liu4bbc2932015-12-01 17:59:40 -0800422 scaledWidth = std::min(Tree::MAX_CACHED_BITMAP_SIZE, scaledWidth);
423 scaledHeight = std::min(Tree::MAX_CACHED_BITMAP_SIZE, scaledHeight);
424
425 if (scaledWidth <= 0 || scaledHeight <= 0) {
Doris Liuf8d131c2016-04-29 18:41:29 -0700426 return 0;
Doris Liu4bbc2932015-12-01 17:59:40 -0800427 }
428
Doris Liu1d8e1942016-03-02 15:16:28 -0800429 mStagingProperties.setScaledSize(scaledWidth, scaledHeight);
Florin Malita777bf852016-02-03 10:48:55 -0500430 int saveCount = outCanvas->save(SaveFlags::MatrixClip);
Doris Liu1d8e1942016-03-02 15:16:28 -0800431 outCanvas->translate(bounds.fLeft, bounds.fTop);
Doris Liu4bbc2932015-12-01 17:59:40 -0800432
433 // Handle RTL mirroring.
434 if (needsMirroring) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800435 outCanvas->translate(bounds.width(), 0);
Doris Liu4bbc2932015-12-01 17:59:40 -0800436 outCanvas->scale(-1.0f, 1.0f);
437 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800438 mStagingProperties.setColorFilter(colorFilter);
Doris Liu4bbc2932015-12-01 17:59:40 -0800439
440 // At this point, canvas has been translated to the right position.
441 // And we use this bound for the destination rect for the drawBitmap, so
442 // we offset to (0, 0);
Doris Liu1d8e1942016-03-02 15:16:28 -0800443 SkRect tmpBounds = bounds;
444 tmpBounds.offsetTo(0, 0);
445 mStagingProperties.setBounds(tmpBounds);
Doris Liu766431a2016-02-04 22:17:11 +0000446 outCanvas->drawVectorDrawable(this);
Doris Liu4bbc2932015-12-01 17:59:40 -0800447 outCanvas->restoreToCount(saveCount);
Doris Liuf8d131c2016-04-29 18:41:29 -0700448 return scaledWidth * scaledHeight;
Doris Liu4bbc2932015-12-01 17:59:40 -0800449}
450
Doris Liu1d8e1942016-03-02 15:16:28 -0800451void Tree::drawStaging(Canvas* outCanvas) {
sergeyvfc9999502016-10-17 13:07:38 -0700452 bool redrawNeeded = allocateBitmapIfNeeded(mStagingCache,
Doris Liu1d8e1942016-03-02 15:16:28 -0800453 mStagingProperties.getScaledWidth(), mStagingProperties.getScaledHeight());
454 // draw bitmap cache
455 if (redrawNeeded || mStagingCache.dirty) {
sergeyvfc9999502016-10-17 13:07:38 -0700456 updateBitmapCache(*mStagingCache.bitmap, true);
Doris Liu1d8e1942016-03-02 15:16:28 -0800457 mStagingCache.dirty = false;
Doris Liu4bbc2932015-12-01 17:59:40 -0800458 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800459
460 SkPaint tmpPaint;
461 SkPaint* paint = updatePaint(&tmpPaint, &mStagingProperties);
sergeyvfc9999502016-10-17 13:07:38 -0700462 outCanvas->drawBitmap(*mStagingCache.bitmap, 0, 0,
463 mStagingCache.bitmap->width(), mStagingCache.bitmap->height(),
Doris Liu1d8e1942016-03-02 15:16:28 -0800464 mStagingProperties.getBounds().left(), mStagingProperties.getBounds().top(),
465 mStagingProperties.getBounds().right(), mStagingProperties.getBounds().bottom(), paint);
466}
467
468SkPaint* Tree::getPaint() {
469 return updatePaint(&mPaint, &mProperties);
470}
471
472// Update the given paint with alpha and color filter. Return nullptr if no color filter is
473// specified and root alpha is 1. Otherwise, return updated paint.
474SkPaint* Tree::updatePaint(SkPaint* outPaint, TreeProperties* prop) {
475 if (prop->getRootAlpha() == 1.0f && prop->getColorFilter() == nullptr) {
476 return nullptr;
477 } else {
Mike Reed260ab722016-10-07 15:59:20 -0400478 outPaint->setColorFilter(sk_ref_sp(prop->getColorFilter()));
Doris Liu1d8e1942016-03-02 15:16:28 -0800479 outPaint->setFilterQuality(kLow_SkFilterQuality);
480 outPaint->setAlpha(prop->getRootAlpha() * 255);
481 return outPaint;
482 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800483}
484
sergeyvfc9999502016-10-17 13:07:38 -0700485Bitmap& Tree::getBitmapUpdateIfDirty() {
486 bool redrawNeeded = allocateBitmapIfNeeded(mCache, mProperties.getScaledWidth(),
Doris Liu1d8e1942016-03-02 15:16:28 -0800487 mProperties.getScaledHeight());
488 if (redrawNeeded || mCache.dirty) {
sergeyvfc9999502016-10-17 13:07:38 -0700489 updateBitmapCache(*mCache.bitmap, false);
Doris Liu1d8e1942016-03-02 15:16:28 -0800490 mCache.dirty = false;
491 }
sergeyvfc9999502016-10-17 13:07:38 -0700492 return *mCache.bitmap;
Doris Liu4bbc2932015-12-01 17:59:40 -0800493}
494
Stan Iliev3310fb12017-03-23 16:56:51 -0400495void Tree::updateCache(sp<skiapipeline::VectorDrawableAtlas>& atlas, GrContext* context) {
496 SkRect dst;
497 sk_sp<SkSurface> surface = mCache.getSurface(&dst);
498 bool canReuseSurface = surface && dst.width() >= mProperties.getScaledWidth()
499 && dst.height() >= mProperties.getScaledHeight();
500 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 {
509 //don't draw, if we failed to allocate an offscreen buffer
510 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);
519 if (!surface->getCanvas()->writePixels(skiaBitmap, dst.fLeft, dst.fTop)) {
520 ALOGD("VectorDrawable caching failed to efficiently upload");
521 surface->getCanvas()->drawBitmap(skiaBitmap, dst.fLeft, dst.fTop);
522 }
523 }
Stan Iliev23c38a92017-03-23 00:12:50 -0400524 mCache.dirty = false;
Stan Iliev23c38a92017-03-23 00:12:50 -0400525 }
526}
527
Stan Iliev3310fb12017-03-23 16:56:51 -0400528void Tree::Cache::setAtlas(sp<skiapipeline::VectorDrawableAtlas> newAtlas,
529 skiapipeline::AtlasKey newAtlasKey) {
530 LOG_ALWAYS_FATAL_IF(newAtlasKey == INVALID_ATLAS_KEY);
531 clear();
532 mAtlas = newAtlas;
533 mAtlasKey = newAtlasKey;
534}
535
536sk_sp<SkSurface> Tree::Cache::getSurface(SkRect* bounds) {
537 sk_sp<SkSurface> surface;
538 sp<skiapipeline::VectorDrawableAtlas> atlas = mAtlas.promote();
539 if (atlas.get() && mAtlasKey != INVALID_ATLAS_KEY) {
540 auto atlasEntry = atlas->getEntry(mAtlasKey);
541 *bounds = atlasEntry.rect;
542 surface = atlasEntry.surface;
543 mAtlasKey = atlasEntry.key;
544 }
545
546 return surface;
547}
548
549void Tree::Cache::clear() {
550 sp<skiapipeline::VectorDrawableAtlas> lockAtlas = mAtlas.promote();
551 if (lockAtlas.get()) {
552 lockAtlas->releaseEntry(mAtlasKey);
553 }
554 mAtlas = nullptr;
555 mAtlasKey = INVALID_ATLAS_KEY;
556}
557
Stan Iliev23c38a92017-03-23 00:12:50 -0400558void Tree::draw(SkCanvas* canvas) {
Stan Iliev3310fb12017-03-23 16:56:51 -0400559 SkRect src;
560 sk_sp<SkSurface> vdSurface = mCache.getSurface(&src);
561 if (vdSurface) {
562 canvas->drawImageRect(vdSurface->makeImageSnapshot().get(), src,
Derek Sollenberger6c2a9e22017-08-15 16:23:01 -0400563 mutateProperties()->getBounds(), getPaint(), SkCanvas::kFast_SrcRectConstraint);
Stan Iliev3310fb12017-03-23 16:56:51 -0400564 } else {
565 // Handle the case when VectorDrawableAtlas has been destroyed, because of memory pressure.
566 // We render the VD into a temporary standalone buffer and mark the frame as dirty. Next
567 // frame will be cached into the atlas.
Derek Sollenberger7fe53c12017-08-31 15:40:12 -0400568 Bitmap& bitmap = getBitmapUpdateIfDirty();
569 SkBitmap skiaBitmap;
570 bitmap.getSkBitmap(&skiaBitmap);
571
Stan Iliev3310fb12017-03-23 16:56:51 -0400572 int scaledWidth = SkScalarCeilToInt(mProperties.getScaledWidth());
573 int scaledHeight = SkScalarCeilToInt(mProperties.getScaledHeight());
Derek Sollenberger7fe53c12017-08-31 15:40:12 -0400574 canvas->drawBitmapRect(skiaBitmap, SkRect::MakeWH(scaledWidth, scaledHeight),
575 mutateProperties()->getBounds(), getPaint(), SkCanvas::kFast_SrcRectConstraint);
Stan Iliev3310fb12017-03-23 16:56:51 -0400576 mCache.clear();
Stan Iliev3310fb12017-03-23 16:56:51 -0400577 markDirty();
578 }
Stan Iliev23c38a92017-03-23 00:12:50 -0400579}
580
sergeyvfc9999502016-10-17 13:07:38 -0700581void Tree::updateBitmapCache(Bitmap& bitmap, bool useStagingData) {
582 SkBitmap outCache;
583 bitmap.getSkBitmap(&outCache);
ztenghuicf0c41d2017-09-13 10:32:50 -0700584 int cacheWidth = outCache.width();
585 int cacheHeight = outCache.height();
586 ATRACE_FORMAT("VectorDrawable repaint %dx%d", cacheWidth, cacheHeight);
sergeyvfc9999502016-10-17 13:07:38 -0700587 outCache.eraseColor(SK_ColorTRANSPARENT);
588 SkCanvas outCanvas(outCache);
Doris Liu1d8e1942016-03-02 15:16:28 -0800589 float viewportWidth = useStagingData ?
590 mStagingProperties.getViewportWidth() : mProperties.getViewportWidth();
591 float viewportHeight = useStagingData ?
592 mStagingProperties.getViewportHeight() : mProperties.getViewportHeight();
ztenghuicf0c41d2017-09-13 10:32:50 -0700593 float scaleX = cacheWidth / viewportWidth;
594 float scaleY = cacheHeight / viewportHeight;
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400595 outCanvas.scale(scaleX, scaleY);
596 mRootNode->draw(&outCanvas, useStagingData);
Doris Liu1d8e1942016-03-02 15:16:28 -0800597}
598
sergeyvfc9999502016-10-17 13:07:38 -0700599bool Tree::allocateBitmapIfNeeded(Cache& cache, int width, int height) {
600 if (!canReuseBitmap(cache.bitmap.get(), width, height)) {
Romain Guy253f2c22016-09-28 17:34:42 -0700601#ifndef ANDROID_ENABLE_LINEAR_BLENDING
602 sk_sp<SkColorSpace> colorSpace = nullptr;
603#else
Matt Sarett89ddb1f2017-02-10 13:31:56 -0500604 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeSRGB();
Romain Guy253f2c22016-09-28 17:34:42 -0700605#endif
606 SkImageInfo info = SkImageInfo::MakeN32(width, height, kPremul_SkAlphaType, colorSpace);
sergeyvfc9999502016-10-17 13:07:38 -0700607 cache.bitmap = Bitmap::allocateHeapBitmap(info);
Doris Liu1d8e1942016-03-02 15:16:28 -0800608 return true;
Doris Liu4bbc2932015-12-01 17:59:40 -0800609 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800610 return false;
Doris Liu4bbc2932015-12-01 17:59:40 -0800611}
612
sergeyvfc9999502016-10-17 13:07:38 -0700613bool Tree::canReuseBitmap(Bitmap* bitmap, int width, int height) {
Teng-Hui Zhu037fc182016-11-16 10:29:39 -0800614 return bitmap && width <= bitmap->width() && height <= bitmap->height();
Doris Liu1d8e1942016-03-02 15:16:28 -0800615}
616
617void Tree::onPropertyChanged(TreeProperties* prop) {
618 if (prop == &mStagingProperties) {
619 mStagingCache.dirty = true;
620 } else {
621 mCache.dirty = true;
622 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800623}
624
625}; // namespace VectorDrawable
626
627}; // namespace uirenderer
628}; // namespace android