blob: f4ce864e83e1567d5961ffbca2261b05913cebfd [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"
25#include "utils/VectorDrawableUtils.h"
26
27#include <math.h>
28#include <string.h>
29
30namespace android {
31namespace uirenderer {
32namespace VectorDrawable {
33
34const int Tree::MAX_CACHED_BITMAP_SIZE = 2048;
35
Doris Liu4bbc2932015-12-01 17:59:40 -080036void Path::dump() {
Doris Liu1d8e1942016-03-02 15:16:28 -080037 ALOGD("Path: %s has %zu points", mName.c_str(), mProperties.getData().points.size());
Doris Liu4bbc2932015-12-01 17:59:40 -080038}
39
Doris Liu1d8e1942016-03-02 15:16:28 -080040// Called from UI thread during the initial setup/theme change.
Doris Liu4bbc2932015-12-01 17:59:40 -080041Path::Path(const char* pathStr, size_t strLength) {
42 PathParser::ParseResult result;
Doris Liu1d8e1942016-03-02 15:16:28 -080043 Data data;
Doris Liub35da392016-04-12 11:06:23 -070044 PathParser::getPathDataFromAsciiString(&data, &result, pathStr, strLength);
Doris Liu1d8e1942016-03-02 15:16:28 -080045 mStagingProperties.setData(data);
Doris Liu4bbc2932015-12-01 17:59:40 -080046}
47
48Path::Path(const Path& path) : Node(path) {
Doris Liu1d8e1942016-03-02 15:16:28 -080049 mStagingProperties.syncProperties(path.mStagingProperties);
Doris Liu4bbc2932015-12-01 17:59:40 -080050}
51
Stan Ilievcc29a5d2017-03-15 16:37:10 -040052const SkPath& Path::getUpdatedPath(bool useStagingData, SkPath* tempStagingPath) {
53 if (useStagingData) {
54 tempStagingPath->reset();
55 VectorDrawableUtils::verbsToPath(tempStagingPath, mStagingProperties.getData());
56 return *tempStagingPath;
57 } else {
58 if (mSkPathDirty) {
59 mSkPath.reset();
60 VectorDrawableUtils::verbsToPath(&mSkPath, mProperties.getData());
61 mSkPathDirty = false;
62 }
63 return mSkPath;
Doris Liu4bbc2932015-12-01 17:59:40 -080064 }
Doris Liu1d8e1942016-03-02 15:16:28 -080065}
66
67void Path::syncProperties() {
68 if (mStagingPropertiesDirty) {
69 mProperties.syncProperties(mStagingProperties);
70 } else {
71 mStagingProperties.syncProperties(mProperties);
72 }
73 mStagingPropertiesDirty = false;
Doris Liu4bbc2932015-12-01 17:59:40 -080074}
75
76FullPath::FullPath(const FullPath& path) : Path(path) {
Doris Liu1d8e1942016-03-02 15:16:28 -080077 mStagingProperties.syncProperties(path.mStagingProperties);
78}
79
80static void applyTrim(SkPath* outPath, const SkPath& inPath, float trimPathStart, float trimPathEnd,
81 float trimPathOffset) {
82 if (trimPathStart == 0.0f && trimPathEnd == 1.0f) {
83 *outPath = inPath;
84 return;
85 }
86 outPath->reset();
87 if (trimPathStart == trimPathEnd) {
88 // Trimmed path should be empty.
89 return;
90 }
91 SkPathMeasure measure(inPath, false);
92 float len = SkScalarToFloat(measure.getLength());
93 float start = len * fmod((trimPathStart + trimPathOffset), 1.0f);
94 float end = len * fmod((trimPathEnd + trimPathOffset), 1.0f);
95
96 if (start > end) {
97 measure.getSegment(start, len, outPath, true);
98 if (end > 0) {
99 measure.getSegment(0, end, outPath, true);
100 }
101 } else {
102 measure.getSegment(start, end, outPath, true);
103 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800104}
105
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400106const SkPath& FullPath::getUpdatedPath(bool useStagingData, SkPath* tempStagingPath) {
107 if (!useStagingData && !mSkPathDirty && !mProperties.mTrimDirty) {
Doris Liu4bbc2932015-12-01 17:59:40 -0800108 return mTrimmedSkPath;
109 }
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400110 Path::getUpdatedPath(useStagingData, tempStagingPath);
111 SkPath *outPath;
112 if (useStagingData) {
113 SkPath inPath = *tempStagingPath;
114 applyTrim(tempStagingPath, inPath, mStagingProperties.getTrimPathStart(),
115 mStagingProperties.getTrimPathEnd(), mStagingProperties.getTrimPathOffset());
116 outPath = tempStagingPath;
Doris Liu4bbc2932015-12-01 17:59:40 -0800117 } else {
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400118 if (mProperties.getTrimPathStart() != 0.0f || mProperties.getTrimPathEnd() != 1.0f) {
119 mProperties.mTrimDirty = false;
120 applyTrim(&mTrimmedSkPath, mSkPath, mProperties.getTrimPathStart(),
121 mProperties.getTrimPathEnd(), mProperties.getTrimPathOffset());
122 outPath = &mTrimmedSkPath;
123 } else {
124 outPath = &mSkPath;
125 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800126 }
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400127 const FullPathProperties& properties = useStagingData ? mStagingProperties : mProperties;
128 bool setFillPath = properties.getFillGradient() != nullptr
129 || properties.getFillColor() != SK_ColorTRANSPARENT;
130 if (setFillPath) {
131 SkPath::FillType ft = static_cast<SkPath::FillType>(properties.getFillType());
132 outPath->setFillType(ft);
133 }
134 return *outPath;
Doris Liu4bbc2932015-12-01 17:59:40 -0800135}
136
Doris Liu1d8e1942016-03-02 15:16:28 -0800137void FullPath::dump() {
138 Path::dump();
139 ALOGD("stroke width, color, alpha: %f, %d, %f, fill color, alpha: %d, %f",
140 mProperties.getStrokeWidth(), mProperties.getStrokeColor(), mProperties.getStrokeAlpha(),
141 mProperties.getFillColor(), mProperties.getFillAlpha());
142}
143
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);
169 paint.setAntiAlias(true);
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);
185 paint.setAntiAlias(true);
186 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",
215 propertyDataSize, length);
216 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 {
Doris Liu1d8e1942016-03-02 15:16:28 -0800231 LOG_ALWAYS_FATAL("Error setting color property on FullPath: No valid property"
232 " with id: %d", propertyId);
Doris Liu766431a2016-02-04 22:17:11 +0000233 }
234}
235
Doris Liu1d8e1942016-03-02 15:16:28 -0800236void FullPath::FullPathProperties::setPropertyValue(int propertyId, float value) {
Doris Liu766431a2016-02-04 22:17:11 +0000237 Property property = static_cast<Property>(propertyId);
238 switch (property) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800239 case Property::strokeWidth:
Doris Liu766431a2016-02-04 22:17:11 +0000240 setStrokeWidth(value);
241 break;
Doris Liu1d8e1942016-03-02 15:16:28 -0800242 case Property::strokeAlpha:
Doris Liu766431a2016-02-04 22:17:11 +0000243 setStrokeAlpha(value);
244 break;
Doris Liu1d8e1942016-03-02 15:16:28 -0800245 case Property::fillAlpha:
Doris Liu766431a2016-02-04 22:17:11 +0000246 setFillAlpha(value);
247 break;
Doris Liu1d8e1942016-03-02 15:16:28 -0800248 case Property::trimPathStart:
Doris Liu766431a2016-02-04 22:17:11 +0000249 setTrimPathStart(value);
250 break;
Doris Liu1d8e1942016-03-02 15:16:28 -0800251 case Property::trimPathEnd:
Doris Liu766431a2016-02-04 22:17:11 +0000252 setTrimPathEnd(value);
253 break;
Doris Liu1d8e1942016-03-02 15:16:28 -0800254 case Property::trimPathOffset:
Doris Liu766431a2016-02-04 22:17:11 +0000255 setTrimPathOffset(value);
256 break;
257 default:
258 LOG_ALWAYS_FATAL("Invalid property id: %d for animation", propertyId);
259 break;
260 }
261}
262
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400263void ClipPath::draw(SkCanvas* outCanvas, bool useStagingData) {
264 SkPath tempStagingPath;
265 outCanvas->clipPath(getUpdatedPath(useStagingData, &tempStagingPath));
Doris Liu4bbc2932015-12-01 17:59:40 -0800266}
267
268Group::Group(const Group& group) : Node(group) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800269 mStagingProperties.syncProperties(group.mStagingProperties);
Doris Liu4bbc2932015-12-01 17:59:40 -0800270}
271
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400272void Group::draw(SkCanvas* outCanvas, bool useStagingData) {
273 // Save the current clip and matrix information, which is local to this group.
274 SkAutoCanvasRestore saver(outCanvas, true);
275 // apply the current group's matrix to the canvas
Doris Liu4bbc2932015-12-01 17:59:40 -0800276 SkMatrix stackedMatrix;
Doris Liu1d8e1942016-03-02 15:16:28 -0800277 const GroupProperties& prop = useStagingData ? mStagingProperties : mProperties;
278 getLocalMatrix(&stackedMatrix, prop);
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400279 outCanvas->concat(stackedMatrix);
Doris Liu4bbc2932015-12-01 17:59:40 -0800280 // Draw the group tree in the same order as the XML file.
Doris Liuef062eb2016-02-04 16:16:27 -0800281 for (auto& child : mChildren) {
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400282 child->draw(outCanvas, useStagingData);
Doris Liu4bbc2932015-12-01 17:59:40 -0800283 }
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400284 // Restore the previous clip and matrix information.
Doris Liu4bbc2932015-12-01 17:59:40 -0800285}
286
287void Group::dump() {
288 ALOGD("Group %s has %zu children: ", mName.c_str(), mChildren.size());
Doris Liu1d8e1942016-03-02 15:16:28 -0800289 ALOGD("Group translateX, Y : %f, %f, scaleX, Y: %f, %f", mProperties.getTranslateX(),
290 mProperties.getTranslateY(), mProperties.getScaleX(), mProperties.getScaleY());
Doris Liu4bbc2932015-12-01 17:59:40 -0800291 for (size_t i = 0; i < mChildren.size(); i++) {
292 mChildren[i]->dump();
293 }
294}
295
Doris Liu1d8e1942016-03-02 15:16:28 -0800296void Group::syncProperties() {
297 // Copy over the dirty staging properties
298 if (mStagingPropertiesDirty) {
299 mProperties.syncProperties(mStagingProperties);
300 } else {
301 mStagingProperties.syncProperties(mProperties);
302 }
303 mStagingPropertiesDirty = false;
304 for (auto& child : mChildren) {
305 child->syncProperties();
306 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800307}
308
Doris Liu1d8e1942016-03-02 15:16:28 -0800309void Group::getLocalMatrix(SkMatrix* outMatrix, const GroupProperties& properties) {
Doris Liu4bbc2932015-12-01 17:59:40 -0800310 outMatrix->reset();
311 // TODO: use rotate(mRotate, mPivotX, mPivotY) and scale with pivot point, instead of
312 // translating to pivot for rotating and scaling, then translating back.
Doris Liu1d8e1942016-03-02 15:16:28 -0800313 outMatrix->postTranslate(-properties.getPivotX(), -properties.getPivotY());
314 outMatrix->postScale(properties.getScaleX(), properties.getScaleY());
315 outMatrix->postRotate(properties.getRotation(), 0, 0);
316 outMatrix->postTranslate(properties.getTranslateX() + properties.getPivotX(),
317 properties.getTranslateY() + properties.getPivotY());
Doris Liu4bbc2932015-12-01 17:59:40 -0800318}
319
320void Group::addChild(Node* child) {
Doris Liuef062eb2016-02-04 16:16:27 -0800321 mChildren.emplace_back(child);
Doris Liu1d8e1942016-03-02 15:16:28 -0800322 if (mPropertyChangedListener != nullptr) {
323 child->setPropertyChangedListener(mPropertyChangedListener);
324 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800325}
326
Doris Liu1d8e1942016-03-02 15:16:28 -0800327bool Group::GroupProperties::copyProperties(float* outProperties, int length) const {
328 int propertyCount = static_cast<int>(Property::count);
Doris Liu4bbc2932015-12-01 17:59:40 -0800329 if (length != propertyCount) {
330 LOG_ALWAYS_FATAL("Properties needs exactly %d bytes, a byte array of size %d is provided",
331 propertyCount, length);
332 return false;
333 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800334
335 PrimitiveFields* out = reinterpret_cast<PrimitiveFields*>(outProperties);
336 *out = mPrimitiveFields;
Doris Liu4bbc2932015-12-01 17:59:40 -0800337 return true;
338}
339
Doris Liu766431a2016-02-04 22:17:11 +0000340// TODO: Consider animating the properties as float pointers
Doris Liu1d8e1942016-03-02 15:16:28 -0800341// Called on render thread
342float Group::GroupProperties::getPropertyValue(int propertyId) const {
Doris Liu766431a2016-02-04 22:17:11 +0000343 Property currentProperty = static_cast<Property>(propertyId);
344 switch (currentProperty) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800345 case Property::rotate:
346 return getRotation();
347 case Property::pivotX:
348 return getPivotX();
349 case Property::pivotY:
350 return getPivotY();
351 case Property::scaleX:
352 return getScaleX();
353 case Property::scaleY:
354 return getScaleY();
355 case Property::translateX:
356 return getTranslateX();
357 case Property::translateY:
358 return getTranslateY();
Doris Liu766431a2016-02-04 22:17:11 +0000359 default:
360 LOG_ALWAYS_FATAL("Invalid property index: %d", propertyId);
361 return 0;
362 }
363}
364
Doris Liu1d8e1942016-03-02 15:16:28 -0800365// Called on render thread
366void Group::GroupProperties::setPropertyValue(int propertyId, float value) {
Doris Liu766431a2016-02-04 22:17:11 +0000367 Property currentProperty = static_cast<Property>(propertyId);
368 switch (currentProperty) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800369 case Property::rotate:
370 setRotation(value);
Doris Liu766431a2016-02-04 22:17:11 +0000371 break;
Doris Liu1d8e1942016-03-02 15:16:28 -0800372 case Property::pivotX:
373 setPivotX(value);
Doris Liu766431a2016-02-04 22:17:11 +0000374 break;
Doris Liu1d8e1942016-03-02 15:16:28 -0800375 case Property::pivotY:
376 setPivotY(value);
Doris Liu766431a2016-02-04 22:17:11 +0000377 break;
Doris Liu1d8e1942016-03-02 15:16:28 -0800378 case Property::scaleX:
379 setScaleX(value);
Doris Liu766431a2016-02-04 22:17:11 +0000380 break;
Doris Liu1d8e1942016-03-02 15:16:28 -0800381 case Property::scaleY:
382 setScaleY(value);
Doris Liu766431a2016-02-04 22:17:11 +0000383 break;
Doris Liu1d8e1942016-03-02 15:16:28 -0800384 case Property::translateX:
385 setTranslateX(value);
Doris Liu766431a2016-02-04 22:17:11 +0000386 break;
Doris Liu1d8e1942016-03-02 15:16:28 -0800387 case Property::translateY:
388 setTranslateY(value);
Doris Liu766431a2016-02-04 22:17:11 +0000389 break;
390 default:
391 LOG_ALWAYS_FATAL("Invalid property index: %d", propertyId);
392 }
393}
394
395bool Group::isValidProperty(int propertyId) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800396 return GroupProperties::isValidProperty(propertyId);
397}
398
399bool Group::GroupProperties::isValidProperty(int propertyId) {
400 return propertyId >= 0 && propertyId < static_cast<int>(Property::count);
Doris Liu766431a2016-02-04 22:17:11 +0000401}
402
Doris Liuf8d131c2016-04-29 18:41:29 -0700403int Tree::draw(Canvas* outCanvas, SkColorFilter* colorFilter,
Doris Liu4bbc2932015-12-01 17:59:40 -0800404 const SkRect& bounds, bool needsMirroring, bool canReuseCache) {
405 // The imageView can scale the canvas in different ways, in order to
406 // avoid blurry scaling, we have to draw into a bitmap with exact pixel
407 // size first. This bitmap size is determined by the bounds and the
408 // canvas scale.
Doris Liu1d8e1942016-03-02 15:16:28 -0800409 SkMatrix canvasMatrix;
410 outCanvas->getMatrix(&canvasMatrix);
Doris Liua0e61572015-12-29 14:57:49 -0800411 float canvasScaleX = 1.0f;
412 float canvasScaleY = 1.0f;
Doris Liu1d8e1942016-03-02 15:16:28 -0800413 if (canvasMatrix.getSkewX() == 0 && canvasMatrix.getSkewY() == 0) {
Doris Liua0e61572015-12-29 14:57:49 -0800414 // Only use the scale value when there's no skew or rotation in the canvas matrix.
Doris Liue410a352016-01-13 17:23:33 -0800415 // TODO: Add a cts test for drawing VD on a canvas with negative scaling factors.
Doris Liu1d8e1942016-03-02 15:16:28 -0800416 canvasScaleX = fabs(canvasMatrix.getScaleX());
417 canvasScaleY = fabs(canvasMatrix.getScaleY());
Doris Liua0e61572015-12-29 14:57:49 -0800418 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800419 int scaledWidth = (int) (bounds.width() * canvasScaleX);
420 int scaledHeight = (int) (bounds.height() * canvasScaleY);
Doris Liu4bbc2932015-12-01 17:59:40 -0800421 scaledWidth = std::min(Tree::MAX_CACHED_BITMAP_SIZE, scaledWidth);
422 scaledHeight = std::min(Tree::MAX_CACHED_BITMAP_SIZE, scaledHeight);
423
424 if (scaledWidth <= 0 || scaledHeight <= 0) {
Doris Liuf8d131c2016-04-29 18:41:29 -0700425 return 0;
Doris Liu4bbc2932015-12-01 17:59:40 -0800426 }
427
Doris Liu1d8e1942016-03-02 15:16:28 -0800428 mStagingProperties.setScaledSize(scaledWidth, scaledHeight);
Florin Malita777bf852016-02-03 10:48:55 -0500429 int saveCount = outCanvas->save(SaveFlags::MatrixClip);
Doris Liu1d8e1942016-03-02 15:16:28 -0800430 outCanvas->translate(bounds.fLeft, bounds.fTop);
Doris Liu4bbc2932015-12-01 17:59:40 -0800431
432 // Handle RTL mirroring.
433 if (needsMirroring) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800434 outCanvas->translate(bounds.width(), 0);
Doris Liu4bbc2932015-12-01 17:59:40 -0800435 outCanvas->scale(-1.0f, 1.0f);
436 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800437 mStagingProperties.setColorFilter(colorFilter);
Doris Liu4bbc2932015-12-01 17:59:40 -0800438
439 // At this point, canvas has been translated to the right position.
440 // And we use this bound for the destination rect for the drawBitmap, so
441 // we offset to (0, 0);
Doris Liu1d8e1942016-03-02 15:16:28 -0800442 SkRect tmpBounds = bounds;
443 tmpBounds.offsetTo(0, 0);
444 mStagingProperties.setBounds(tmpBounds);
Doris Liu766431a2016-02-04 22:17:11 +0000445 outCanvas->drawVectorDrawable(this);
Doris Liu4bbc2932015-12-01 17:59:40 -0800446 outCanvas->restoreToCount(saveCount);
Doris Liuf8d131c2016-04-29 18:41:29 -0700447 return scaledWidth * scaledHeight;
Doris Liu4bbc2932015-12-01 17:59:40 -0800448}
449
Doris Liu1d8e1942016-03-02 15:16:28 -0800450void Tree::drawStaging(Canvas* outCanvas) {
sergeyvfc9999502016-10-17 13:07:38 -0700451 bool redrawNeeded = allocateBitmapIfNeeded(mStagingCache,
Doris Liu1d8e1942016-03-02 15:16:28 -0800452 mStagingProperties.getScaledWidth(), mStagingProperties.getScaledHeight());
453 // draw bitmap cache
454 if (redrawNeeded || mStagingCache.dirty) {
sergeyvfc9999502016-10-17 13:07:38 -0700455 updateBitmapCache(*mStagingCache.bitmap, true);
Doris Liu1d8e1942016-03-02 15:16:28 -0800456 mStagingCache.dirty = false;
Doris Liu4bbc2932015-12-01 17:59:40 -0800457 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800458
459 SkPaint tmpPaint;
460 SkPaint* paint = updatePaint(&tmpPaint, &mStagingProperties);
sergeyvfc9999502016-10-17 13:07:38 -0700461 outCanvas->drawBitmap(*mStagingCache.bitmap, 0, 0,
462 mStagingCache.bitmap->width(), mStagingCache.bitmap->height(),
Doris Liu1d8e1942016-03-02 15:16:28 -0800463 mStagingProperties.getBounds().left(), mStagingProperties.getBounds().top(),
464 mStagingProperties.getBounds().right(), mStagingProperties.getBounds().bottom(), paint);
465}
466
467SkPaint* Tree::getPaint() {
468 return updatePaint(&mPaint, &mProperties);
469}
470
471// Update the given paint with alpha and color filter. Return nullptr if no color filter is
472// specified and root alpha is 1. Otherwise, return updated paint.
473SkPaint* Tree::updatePaint(SkPaint* outPaint, TreeProperties* prop) {
474 if (prop->getRootAlpha() == 1.0f && prop->getColorFilter() == nullptr) {
475 return nullptr;
476 } else {
Mike Reed260ab722016-10-07 15:59:20 -0400477 outPaint->setColorFilter(sk_ref_sp(prop->getColorFilter()));
Doris Liu1d8e1942016-03-02 15:16:28 -0800478 outPaint->setFilterQuality(kLow_SkFilterQuality);
479 outPaint->setAlpha(prop->getRootAlpha() * 255);
480 return outPaint;
481 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800482}
483
sergeyvfc9999502016-10-17 13:07:38 -0700484Bitmap& Tree::getBitmapUpdateIfDirty() {
485 bool redrawNeeded = allocateBitmapIfNeeded(mCache, mProperties.getScaledWidth(),
Doris Liu1d8e1942016-03-02 15:16:28 -0800486 mProperties.getScaledHeight());
487 if (redrawNeeded || mCache.dirty) {
sergeyvfc9999502016-10-17 13:07:38 -0700488 updateBitmapCache(*mCache.bitmap, false);
Doris Liu1d8e1942016-03-02 15:16:28 -0800489 mCache.dirty = false;
490 }
sergeyvfc9999502016-10-17 13:07:38 -0700491 return *mCache.bitmap;
Doris Liu4bbc2932015-12-01 17:59:40 -0800492}
493
Stan Iliev3310fb12017-03-23 16:56:51 -0400494void Tree::updateCache(sp<skiapipeline::VectorDrawableAtlas>& atlas, GrContext* context) {
495 SkRect dst;
496 sk_sp<SkSurface> surface = mCache.getSurface(&dst);
497 bool canReuseSurface = surface && dst.width() >= mProperties.getScaledWidth()
498 && dst.height() >= mProperties.getScaledHeight();
499 if (!canReuseSurface) {
500 int scaledWidth = SkScalarCeilToInt(mProperties.getScaledWidth());
501 int scaledHeight = SkScalarCeilToInt(mProperties.getScaledHeight());
502 auto atlasEntry = atlas->requestNewEntry(scaledWidth, scaledHeight, context);
503 if (INVALID_ATLAS_KEY != atlasEntry.key) {
504 dst = atlasEntry.rect;
505 surface = atlasEntry.surface;
506 mCache.setAtlas(atlas, atlasEntry.key);
507 } else {
508 //don't draw, if we failed to allocate an offscreen buffer
509 mCache.clear();
510 surface.reset();
511 }
Stan Iliev23c38a92017-03-23 00:12:50 -0400512 }
Stan Iliev3310fb12017-03-23 16:56:51 -0400513 if (!canReuseSurface || mCache.dirty) {
514 draw(surface.get(), dst);
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::draw(SkSurface* surface, const SkRect& dst) {
520 if (surface) {
521 SkCanvas* canvas = surface->getCanvas();
522 float scaleX = dst.width() / mProperties.getViewportWidth();
523 float scaleY = dst.height() / mProperties.getViewportHeight();
524 SkAutoCanvasRestore acr(canvas, true);
525 canvas->translate(dst.fLeft, dst.fTop);
526 canvas->clipRect(SkRect::MakeWH(dst.width(), dst.height()));
527 canvas->clear(SK_ColorTRANSPARENT);
528 canvas->scale(scaleX, scaleY);
529 mRootNode->draw(canvas, false);
530 }
531}
532
533void Tree::Cache::setAtlas(sp<skiapipeline::VectorDrawableAtlas> newAtlas,
534 skiapipeline::AtlasKey newAtlasKey) {
535 LOG_ALWAYS_FATAL_IF(newAtlasKey == INVALID_ATLAS_KEY);
536 clear();
537 mAtlas = newAtlas;
538 mAtlasKey = newAtlasKey;
539}
540
541sk_sp<SkSurface> Tree::Cache::getSurface(SkRect* bounds) {
542 sk_sp<SkSurface> surface;
543 sp<skiapipeline::VectorDrawableAtlas> atlas = mAtlas.promote();
544 if (atlas.get() && mAtlasKey != INVALID_ATLAS_KEY) {
545 auto atlasEntry = atlas->getEntry(mAtlasKey);
546 *bounds = atlasEntry.rect;
547 surface = atlasEntry.surface;
548 mAtlasKey = atlasEntry.key;
549 }
550
551 return surface;
552}
553
554void Tree::Cache::clear() {
555 sp<skiapipeline::VectorDrawableAtlas> lockAtlas = mAtlas.promote();
556 if (lockAtlas.get()) {
557 lockAtlas->releaseEntry(mAtlasKey);
558 }
559 mAtlas = nullptr;
560 mAtlasKey = INVALID_ATLAS_KEY;
561}
562
Stan Iliev23c38a92017-03-23 00:12:50 -0400563void Tree::draw(SkCanvas* canvas) {
Stan Iliev3310fb12017-03-23 16:56:51 -0400564 SkRect src;
565 sk_sp<SkSurface> vdSurface = mCache.getSurface(&src);
566 if (vdSurface) {
567 canvas->drawImageRect(vdSurface->makeImageSnapshot().get(), src,
Derek Sollenberger6c2a9e22017-08-15 16:23:01 -0400568 mutateProperties()->getBounds(), getPaint(), SkCanvas::kFast_SrcRectConstraint);
Stan Iliev3310fb12017-03-23 16:56:51 -0400569 } else {
570 // Handle the case when VectorDrawableAtlas has been destroyed, because of memory pressure.
571 // We render the VD into a temporary standalone buffer and mark the frame as dirty. Next
572 // frame will be cached into the atlas.
573 int scaledWidth = SkScalarCeilToInt(mProperties.getScaledWidth());
574 int scaledHeight = SkScalarCeilToInt(mProperties.getScaledHeight());
575 SkRect src = SkRect::MakeWH(scaledWidth, scaledHeight);
576#ifndef ANDROID_ENABLE_LINEAR_BLENDING
577 sk_sp<SkColorSpace> colorSpace = nullptr;
578#else
579 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeSRGB();
580#endif
581 SkImageInfo info = SkImageInfo::MakeN32(scaledWidth, scaledHeight, kPremul_SkAlphaType,
582 colorSpace);
583 sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(canvas->getGrContext(),
584 SkBudgeted::kYes, info);
585 draw(surface.get(), src);
586 mCache.clear();
587 canvas->drawImageRect(surface->makeImageSnapshot().get(), mutateProperties()->getBounds(),
Derek Sollenberger6c2a9e22017-08-15 16:23:01 -0400588 getPaint(), SkCanvas::kFast_SrcRectConstraint);
Stan Iliev3310fb12017-03-23 16:56:51 -0400589 markDirty();
590 }
Stan Iliev23c38a92017-03-23 00:12:50 -0400591}
592
sergeyvfc9999502016-10-17 13:07:38 -0700593void Tree::updateBitmapCache(Bitmap& bitmap, bool useStagingData) {
594 SkBitmap outCache;
595 bitmap.getSkBitmap(&outCache);
596 outCache.eraseColor(SK_ColorTRANSPARENT);
597 SkCanvas outCanvas(outCache);
Doris Liu1d8e1942016-03-02 15:16:28 -0800598 float viewportWidth = useStagingData ?
599 mStagingProperties.getViewportWidth() : mProperties.getViewportWidth();
600 float viewportHeight = useStagingData ?
601 mStagingProperties.getViewportHeight() : mProperties.getViewportHeight();
sergeyvfc9999502016-10-17 13:07:38 -0700602 float scaleX = outCache.width() / viewportWidth;
603 float scaleY = outCache.height() / viewportHeight;
Stan Ilievcc29a5d2017-03-15 16:37:10 -0400604 outCanvas.scale(scaleX, scaleY);
605 mRootNode->draw(&outCanvas, useStagingData);
Doris Liu1d8e1942016-03-02 15:16:28 -0800606}
607
sergeyvfc9999502016-10-17 13:07:38 -0700608bool Tree::allocateBitmapIfNeeded(Cache& cache, int width, int height) {
609 if (!canReuseBitmap(cache.bitmap.get(), width, height)) {
Romain Guy253f2c22016-09-28 17:34:42 -0700610#ifndef ANDROID_ENABLE_LINEAR_BLENDING
611 sk_sp<SkColorSpace> colorSpace = nullptr;
612#else
Matt Sarett89ddb1f2017-02-10 13:31:56 -0500613 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeSRGB();
Romain Guy253f2c22016-09-28 17:34:42 -0700614#endif
615 SkImageInfo info = SkImageInfo::MakeN32(width, height, kPremul_SkAlphaType, colorSpace);
sergeyvfc9999502016-10-17 13:07:38 -0700616 cache.bitmap = Bitmap::allocateHeapBitmap(info);
Doris Liu1d8e1942016-03-02 15:16:28 -0800617 return true;
Doris Liu4bbc2932015-12-01 17:59:40 -0800618 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800619 return false;
Doris Liu4bbc2932015-12-01 17:59:40 -0800620}
621
sergeyvfc9999502016-10-17 13:07:38 -0700622bool Tree::canReuseBitmap(Bitmap* bitmap, int width, int height) {
Teng-Hui Zhu037fc182016-11-16 10:29:39 -0800623 return bitmap && width <= bitmap->width() && height <= bitmap->height();
Doris Liu1d8e1942016-03-02 15:16:28 -0800624}
625
626void Tree::onPropertyChanged(TreeProperties* prop) {
627 if (prop == &mStagingProperties) {
628 mStagingCache.dirty = true;
629 } else {
630 mCache.dirty = true;
631 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800632}
633
634}; // namespace VectorDrawable
635
636}; // namespace uirenderer
637}; // namespace android