blob: b50647adc0beb2e2f7323924786c6211a8a90217 [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 Liu1d8e1942016-03-02 15:16:28 -080036void Path::draw(SkCanvas* outCanvas, const SkMatrix& groupStackedMatrix, float scaleX, float scaleY,
37 bool useStagingData) {
Doris Liu4bbc2932015-12-01 17:59:40 -080038 float matrixScale = getMatrixScale(groupStackedMatrix);
39 if (matrixScale == 0) {
40 // When either x or y is scaled to 0, we don't need to draw anything.
41 return;
42 }
43
Doris Liu4bbc2932015-12-01 17:59:40 -080044 SkMatrix pathMatrix(groupStackedMatrix);
45 pathMatrix.postScale(scaleX, scaleY);
46
47 //TODO: try apply the path matrix to the canvas instead of creating a new path.
48 SkPath renderPath;
49 renderPath.reset();
Doris Liu1d8e1942016-03-02 15:16:28 -080050
51 if (useStagingData) {
52 SkPath tmpPath;
53 getStagingPath(&tmpPath);
54 renderPath.addPath(tmpPath, pathMatrix);
55 } else {
56 renderPath.addPath(getUpdatedPath(), pathMatrix);
57 }
Doris Liu4bbc2932015-12-01 17:59:40 -080058
59 float minScale = fmin(scaleX, scaleY);
60 float strokeScale = minScale * matrixScale;
Doris Liu1d8e1942016-03-02 15:16:28 -080061 drawPath(outCanvas, renderPath, strokeScale, pathMatrix, useStagingData);
Doris Liu4bbc2932015-12-01 17:59:40 -080062}
63
64void Path::dump() {
Doris Liu1d8e1942016-03-02 15:16:28 -080065 ALOGD("Path: %s has %zu points", mName.c_str(), mProperties.getData().points.size());
Doris Liu4bbc2932015-12-01 17:59:40 -080066}
67
68float Path::getMatrixScale(const SkMatrix& groupStackedMatrix) {
69 // Given unit vectors A = (0, 1) and B = (1, 0).
70 // After matrix mapping, we got A' and B'. Let theta = the angel b/t A' and B'.
71 // Therefore, the final scale we want is min(|A'| * sin(theta), |B'| * sin(theta)),
72 // which is (|A'| * |B'| * sin(theta)) / max (|A'|, |B'|);
73 // If max (|A'|, |B'|) = 0, that means either x or y has a scale of 0.
74 //
75 // For non-skew case, which is most of the cases, matrix scale is computing exactly the
76 // scale on x and y axis, and take the minimal of these two.
77 // For skew case, an unit square will mapped to a parallelogram. And this function will
78 // return the minimal height of the 2 bases.
79 SkVector skVectors[2];
80 skVectors[0].set(0, 1);
81 skVectors[1].set(1, 0);
82 groupStackedMatrix.mapVectors(skVectors, 2);
83 float scaleX = hypotf(skVectors[0].fX, skVectors[0].fY);
84 float scaleY = hypotf(skVectors[1].fX, skVectors[1].fY);
85 float crossProduct = skVectors[0].cross(skVectors[1]);
86 float maxScale = fmax(scaleX, scaleY);
87
88 float matrixScale = 0;
89 if (maxScale > 0) {
90 matrixScale = fabs(crossProduct) / maxScale;
91 }
92 return matrixScale;
93}
Doris Liu1d8e1942016-03-02 15:16:28 -080094
95// Called from UI thread during the initial setup/theme change.
Doris Liu4bbc2932015-12-01 17:59:40 -080096Path::Path(const char* pathStr, size_t strLength) {
97 PathParser::ParseResult result;
Doris Liu1d8e1942016-03-02 15:16:28 -080098 Data data;
Doris Liub35da392016-04-12 11:06:23 -070099 PathParser::getPathDataFromAsciiString(&data, &result, pathStr, strLength);
Doris Liu1d8e1942016-03-02 15:16:28 -0800100 mStagingProperties.setData(data);
Doris Liu4bbc2932015-12-01 17:59:40 -0800101}
102
103Path::Path(const Path& path) : Node(path) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800104 mStagingProperties.syncProperties(path.mStagingProperties);
Doris Liu4bbc2932015-12-01 17:59:40 -0800105}
106
107const SkPath& Path::getUpdatedPath() {
108 if (mSkPathDirty) {
109 mSkPath.reset();
Doris Liu1d8e1942016-03-02 15:16:28 -0800110 VectorDrawableUtils::verbsToPath(&mSkPath, mProperties.getData());
Doris Liu4bbc2932015-12-01 17:59:40 -0800111 mSkPathDirty = false;
112 }
113 return mSkPath;
114}
115
Doris Liu1d8e1942016-03-02 15:16:28 -0800116void Path::getStagingPath(SkPath* outPath) {
117 outPath->reset();
118 VectorDrawableUtils::verbsToPath(outPath, mStagingProperties.getData());
119}
120
121void Path::syncProperties() {
122 if (mStagingPropertiesDirty) {
123 mProperties.syncProperties(mStagingProperties);
124 } else {
125 mStagingProperties.syncProperties(mProperties);
126 }
127 mStagingPropertiesDirty = false;
Doris Liu4bbc2932015-12-01 17:59:40 -0800128}
129
130FullPath::FullPath(const FullPath& path) : Path(path) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800131 mStagingProperties.syncProperties(path.mStagingProperties);
132}
133
134static void applyTrim(SkPath* outPath, const SkPath& inPath, float trimPathStart, float trimPathEnd,
135 float trimPathOffset) {
136 if (trimPathStart == 0.0f && trimPathEnd == 1.0f) {
137 *outPath = inPath;
138 return;
139 }
140 outPath->reset();
141 if (trimPathStart == trimPathEnd) {
142 // Trimmed path should be empty.
143 return;
144 }
145 SkPathMeasure measure(inPath, false);
146 float len = SkScalarToFloat(measure.getLength());
147 float start = len * fmod((trimPathStart + trimPathOffset), 1.0f);
148 float end = len * fmod((trimPathEnd + trimPathOffset), 1.0f);
149
150 if (start > end) {
151 measure.getSegment(start, len, outPath, true);
152 if (end > 0) {
153 measure.getSegment(0, end, outPath, true);
154 }
155 } else {
156 measure.getSegment(start, end, outPath, true);
157 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800158}
159
160const SkPath& FullPath::getUpdatedPath() {
Doris Liu1d8e1942016-03-02 15:16:28 -0800161 if (!mSkPathDirty && !mProperties.mTrimDirty) {
Doris Liu4bbc2932015-12-01 17:59:40 -0800162 return mTrimmedSkPath;
163 }
164 Path::getUpdatedPath();
Doris Liu1d8e1942016-03-02 15:16:28 -0800165 if (mProperties.getTrimPathStart() != 0.0f || mProperties.getTrimPathEnd() != 1.0f) {
166 mProperties.mTrimDirty = false;
167 applyTrim(&mTrimmedSkPath, mSkPath, mProperties.getTrimPathStart(),
168 mProperties.getTrimPathEnd(), mProperties.getTrimPathOffset());
Doris Liu4bbc2932015-12-01 17:59:40 -0800169 return mTrimmedSkPath;
170 } else {
171 return mSkPath;
172 }
173}
174
Doris Liu1d8e1942016-03-02 15:16:28 -0800175void FullPath::getStagingPath(SkPath* outPath) {
176 Path::getStagingPath(outPath);
177 SkPath inPath = *outPath;
178 applyTrim(outPath, inPath, mStagingProperties.getTrimPathStart(),
179 mStagingProperties.getTrimPathEnd(), mStagingProperties.getTrimPathOffset());
Doris Liu4bbc2932015-12-01 17:59:40 -0800180}
181
Doris Liu1d8e1942016-03-02 15:16:28 -0800182void FullPath::dump() {
183 Path::dump();
184 ALOGD("stroke width, color, alpha: %f, %d, %f, fill color, alpha: %d, %f",
185 mProperties.getStrokeWidth(), mProperties.getStrokeColor(), mProperties.getStrokeAlpha(),
186 mProperties.getFillColor(), mProperties.getFillAlpha());
187}
188
189
Doris Liu4bbc2932015-12-01 17:59:40 -0800190inline SkColor applyAlpha(SkColor color, float alpha) {
191 int alphaBytes = SkColorGetA(color);
192 return SkColorSetA(color, alphaBytes * alpha);
193}
194
Teng-Hui Zhu46591f42016-03-15 14:32:16 -0700195void FullPath::drawPath(SkCanvas* outCanvas, SkPath& renderPath, float strokeScale,
Doris Liu1d8e1942016-03-02 15:16:28 -0800196 const SkMatrix& matrix, bool useStagingData){
197 const FullPathProperties& properties = useStagingData ? mStagingProperties : mProperties;
198
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800199 // Draw path's fill, if fill color or gradient is valid
200 bool needsFill = false;
Doris Liu1d8e1942016-03-02 15:16:28 -0800201 SkPaint paint;
202 if (properties.getFillGradient() != nullptr) {
203 paint.setColor(applyAlpha(SK_ColorBLACK, properties.getFillAlpha()));
Mike Reed260ab722016-10-07 15:59:20 -0400204 paint.setShader(properties.getFillGradient()->makeWithLocalMatrix(matrix));
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800205 needsFill = true;
Doris Liu1d8e1942016-03-02 15:16:28 -0800206 } else if (properties.getFillColor() != SK_ColorTRANSPARENT) {
207 paint.setColor(applyAlpha(properties.getFillColor(), properties.getFillAlpha()));
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800208 needsFill = true;
Doris Liu4bbc2932015-12-01 17:59:40 -0800209 }
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800210
211 if (needsFill) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800212 paint.setStyle(SkPaint::Style::kFill_Style);
213 paint.setAntiAlias(true);
214 SkPath::FillType ft = static_cast<SkPath::FillType>(properties.getFillType());
Teng-Hui Zhu46591f42016-03-15 14:32:16 -0700215 renderPath.setFillType(ft);
Doris Liu1d8e1942016-03-02 15:16:28 -0800216 outCanvas->drawPath(renderPath, paint);
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800217 }
218
Doris Liu1d8e1942016-03-02 15:16:28 -0800219 // Draw path's stroke, if stroke color or Gradient is valid
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800220 bool needsStroke = false;
Doris Liu1d8e1942016-03-02 15:16:28 -0800221 if (properties.getStrokeGradient() != nullptr) {
222 paint.setColor(applyAlpha(SK_ColorBLACK, properties.getStrokeAlpha()));
Mike Reed260ab722016-10-07 15:59:20 -0400223 paint.setShader(properties.getStrokeGradient()->makeWithLocalMatrix(matrix));
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800224 needsStroke = true;
Doris Liu1d8e1942016-03-02 15:16:28 -0800225 } else if (properties.getStrokeColor() != SK_ColorTRANSPARENT) {
226 paint.setColor(applyAlpha(properties.getStrokeColor(), properties.getStrokeAlpha()));
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800227 needsStroke = true;
228 }
229 if (needsStroke) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800230 paint.setStyle(SkPaint::Style::kStroke_Style);
231 paint.setAntiAlias(true);
232 paint.setStrokeJoin(SkPaint::Join(properties.getStrokeLineJoin()));
233 paint.setStrokeCap(SkPaint::Cap(properties.getStrokeLineCap()));
234 paint.setStrokeMiter(properties.getStrokeMiterLimit());
235 paint.setStrokeWidth(properties.getStrokeWidth() * strokeScale);
236 outCanvas->drawPath(renderPath, paint);
Doris Liu4bbc2932015-12-01 17:59:40 -0800237 }
238}
239
Doris Liu1d8e1942016-03-02 15:16:28 -0800240void FullPath::syncProperties() {
241 Path::syncProperties();
Doris Liu4bbc2932015-12-01 17:59:40 -0800242
Doris Liu1d8e1942016-03-02 15:16:28 -0800243 if (mStagingPropertiesDirty) {
244 mProperties.syncProperties(mStagingProperties);
Doris Liu4bbc2932015-12-01 17:59:40 -0800245 } else {
Doris Liu1d8e1942016-03-02 15:16:28 -0800246 // Update staging property with property values from animation.
247 mStagingProperties.syncProperties(mProperties);
Doris Liu4bbc2932015-12-01 17:59:40 -0800248 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800249 mStagingPropertiesDirty = false;
Doris Liu4bbc2932015-12-01 17:59:40 -0800250}
251
Doris Liu1d8e1942016-03-02 15:16:28 -0800252REQUIRE_COMPATIBLE_LAYOUT(FullPath::FullPathProperties::PrimitiveFields);
Doris Liu4bbc2932015-12-01 17:59:40 -0800253
254static_assert(sizeof(float) == sizeof(int32_t), "float is not the same size as int32_t");
255static_assert(sizeof(SkColor) == sizeof(int32_t), "SkColor is not the same size as int32_t");
256
Doris Liu1d8e1942016-03-02 15:16:28 -0800257bool FullPath::FullPathProperties::copyProperties(int8_t* outProperties, int length) const {
258 int propertyDataSize = sizeof(FullPathProperties::PrimitiveFields);
Doris Liu4bbc2932015-12-01 17:59:40 -0800259 if (length != propertyDataSize) {
260 LOG_ALWAYS_FATAL("Properties needs exactly %d bytes, a byte array of size %d is provided",
261 propertyDataSize, length);
262 return false;
263 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800264
265 PrimitiveFields* out = reinterpret_cast<PrimitiveFields*>(outProperties);
266 *out = mPrimitiveFields;
Doris Liu4bbc2932015-12-01 17:59:40 -0800267 return true;
268}
269
Doris Liu1d8e1942016-03-02 15:16:28 -0800270void FullPath::FullPathProperties::setColorPropertyValue(int propertyId, int32_t value) {
Doris Liu766431a2016-02-04 22:17:11 +0000271 Property currentProperty = static_cast<Property>(propertyId);
Doris Liu1d8e1942016-03-02 15:16:28 -0800272 if (currentProperty == Property::strokeColor) {
273 setStrokeColor(value);
274 } else if (currentProperty == Property::fillColor) {
275 setFillColor(value);
Doris Liu766431a2016-02-04 22:17:11 +0000276 } else {
Doris Liu1d8e1942016-03-02 15:16:28 -0800277 LOG_ALWAYS_FATAL("Error setting color property on FullPath: No valid property"
278 " with id: %d", propertyId);
Doris Liu766431a2016-02-04 22:17:11 +0000279 }
280}
281
Doris Liu1d8e1942016-03-02 15:16:28 -0800282void FullPath::FullPathProperties::setPropertyValue(int propertyId, float value) {
Doris Liu766431a2016-02-04 22:17:11 +0000283 Property property = static_cast<Property>(propertyId);
284 switch (property) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800285 case Property::strokeWidth:
Doris Liu766431a2016-02-04 22:17:11 +0000286 setStrokeWidth(value);
287 break;
Doris Liu1d8e1942016-03-02 15:16:28 -0800288 case Property::strokeAlpha:
Doris Liu766431a2016-02-04 22:17:11 +0000289 setStrokeAlpha(value);
290 break;
Doris Liu1d8e1942016-03-02 15:16:28 -0800291 case Property::fillAlpha:
Doris Liu766431a2016-02-04 22:17:11 +0000292 setFillAlpha(value);
293 break;
Doris Liu1d8e1942016-03-02 15:16:28 -0800294 case Property::trimPathStart:
Doris Liu766431a2016-02-04 22:17:11 +0000295 setTrimPathStart(value);
296 break;
Doris Liu1d8e1942016-03-02 15:16:28 -0800297 case Property::trimPathEnd:
Doris Liu766431a2016-02-04 22:17:11 +0000298 setTrimPathEnd(value);
299 break;
Doris Liu1d8e1942016-03-02 15:16:28 -0800300 case Property::trimPathOffset:
Doris Liu766431a2016-02-04 22:17:11 +0000301 setTrimPathOffset(value);
302 break;
303 default:
304 LOG_ALWAYS_FATAL("Invalid property id: %d for animation", propertyId);
305 break;
306 }
307}
308
Teng-Hui Zhu46591f42016-03-15 14:32:16 -0700309void ClipPath::drawPath(SkCanvas* outCanvas, SkPath& renderPath,
Doris Liu1d8e1942016-03-02 15:16:28 -0800310 float strokeScale, const SkMatrix& matrix, bool useStagingData){
Doris Liuc2de46f2016-01-21 12:55:54 -0800311 outCanvas->clipPath(renderPath, SkRegion::kIntersect_Op);
Doris Liu4bbc2932015-12-01 17:59:40 -0800312}
313
314Group::Group(const Group& group) : Node(group) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800315 mStagingProperties.syncProperties(group.mStagingProperties);
Doris Liu4bbc2932015-12-01 17:59:40 -0800316}
317
Doris Liuc2de46f2016-01-21 12:55:54 -0800318void Group::draw(SkCanvas* outCanvas, const SkMatrix& currentMatrix, float scaleX,
Doris Liu1d8e1942016-03-02 15:16:28 -0800319 float scaleY, bool useStagingData) {
Doris Liu4bbc2932015-12-01 17:59:40 -0800320 // TODO: Try apply the matrix to the canvas instead of passing it down the tree
321
322 // Calculate current group's matrix by preConcat the parent's and
323 // and the current one on the top of the stack.
324 // Basically the Mfinal = Mviewport * M0 * M1 * M2;
325 // Mi the local matrix at level i of the group tree.
326 SkMatrix stackedMatrix;
Doris Liu1d8e1942016-03-02 15:16:28 -0800327 const GroupProperties& prop = useStagingData ? mStagingProperties : mProperties;
328 getLocalMatrix(&stackedMatrix, prop);
Doris Liu4bbc2932015-12-01 17:59:40 -0800329 stackedMatrix.postConcat(currentMatrix);
330
331 // Save the current clip information, which is local to this group.
Doris Liuc2de46f2016-01-21 12:55:54 -0800332 outCanvas->save();
Doris Liu4bbc2932015-12-01 17:59:40 -0800333 // Draw the group tree in the same order as the XML file.
Doris Liuef062eb2016-02-04 16:16:27 -0800334 for (auto& child : mChildren) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800335 child->draw(outCanvas, stackedMatrix, scaleX, scaleY, useStagingData);
Doris Liu4bbc2932015-12-01 17:59:40 -0800336 }
337 // Restore the previous clip information.
338 outCanvas->restore();
339}
340
341void Group::dump() {
342 ALOGD("Group %s has %zu children: ", mName.c_str(), mChildren.size());
Doris Liu1d8e1942016-03-02 15:16:28 -0800343 ALOGD("Group translateX, Y : %f, %f, scaleX, Y: %f, %f", mProperties.getTranslateX(),
344 mProperties.getTranslateY(), mProperties.getScaleX(), mProperties.getScaleY());
Doris Liu4bbc2932015-12-01 17:59:40 -0800345 for (size_t i = 0; i < mChildren.size(); i++) {
346 mChildren[i]->dump();
347 }
348}
349
Doris Liu1d8e1942016-03-02 15:16:28 -0800350void Group::syncProperties() {
351 // Copy over the dirty staging properties
352 if (mStagingPropertiesDirty) {
353 mProperties.syncProperties(mStagingProperties);
354 } else {
355 mStagingProperties.syncProperties(mProperties);
356 }
357 mStagingPropertiesDirty = false;
358 for (auto& child : mChildren) {
359 child->syncProperties();
360 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800361}
362
Doris Liu1d8e1942016-03-02 15:16:28 -0800363void Group::getLocalMatrix(SkMatrix* outMatrix, const GroupProperties& properties) {
Doris Liu4bbc2932015-12-01 17:59:40 -0800364 outMatrix->reset();
365 // TODO: use rotate(mRotate, mPivotX, mPivotY) and scale with pivot point, instead of
366 // translating to pivot for rotating and scaling, then translating back.
Doris Liu1d8e1942016-03-02 15:16:28 -0800367 outMatrix->postTranslate(-properties.getPivotX(), -properties.getPivotY());
368 outMatrix->postScale(properties.getScaleX(), properties.getScaleY());
369 outMatrix->postRotate(properties.getRotation(), 0, 0);
370 outMatrix->postTranslate(properties.getTranslateX() + properties.getPivotX(),
371 properties.getTranslateY() + properties.getPivotY());
Doris Liu4bbc2932015-12-01 17:59:40 -0800372}
373
374void Group::addChild(Node* child) {
Doris Liuef062eb2016-02-04 16:16:27 -0800375 mChildren.emplace_back(child);
Doris Liu1d8e1942016-03-02 15:16:28 -0800376 if (mPropertyChangedListener != nullptr) {
377 child->setPropertyChangedListener(mPropertyChangedListener);
378 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800379}
380
Doris Liu1d8e1942016-03-02 15:16:28 -0800381bool Group::GroupProperties::copyProperties(float* outProperties, int length) const {
382 int propertyCount = static_cast<int>(Property::count);
Doris Liu4bbc2932015-12-01 17:59:40 -0800383 if (length != propertyCount) {
384 LOG_ALWAYS_FATAL("Properties needs exactly %d bytes, a byte array of size %d is provided",
385 propertyCount, length);
386 return false;
387 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800388
389 PrimitiveFields* out = reinterpret_cast<PrimitiveFields*>(outProperties);
390 *out = mPrimitiveFields;
Doris Liu4bbc2932015-12-01 17:59:40 -0800391 return true;
392}
393
Doris Liu766431a2016-02-04 22:17:11 +0000394// TODO: Consider animating the properties as float pointers
Doris Liu1d8e1942016-03-02 15:16:28 -0800395// Called on render thread
396float Group::GroupProperties::getPropertyValue(int propertyId) const {
Doris Liu766431a2016-02-04 22:17:11 +0000397 Property currentProperty = static_cast<Property>(propertyId);
398 switch (currentProperty) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800399 case Property::rotate:
400 return getRotation();
401 case Property::pivotX:
402 return getPivotX();
403 case Property::pivotY:
404 return getPivotY();
405 case Property::scaleX:
406 return getScaleX();
407 case Property::scaleY:
408 return getScaleY();
409 case Property::translateX:
410 return getTranslateX();
411 case Property::translateY:
412 return getTranslateY();
Doris Liu766431a2016-02-04 22:17:11 +0000413 default:
414 LOG_ALWAYS_FATAL("Invalid property index: %d", propertyId);
415 return 0;
416 }
417}
418
Doris Liu1d8e1942016-03-02 15:16:28 -0800419// Called on render thread
420void Group::GroupProperties::setPropertyValue(int propertyId, float value) {
Doris Liu766431a2016-02-04 22:17:11 +0000421 Property currentProperty = static_cast<Property>(propertyId);
422 switch (currentProperty) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800423 case Property::rotate:
424 setRotation(value);
Doris Liu766431a2016-02-04 22:17:11 +0000425 break;
Doris Liu1d8e1942016-03-02 15:16:28 -0800426 case Property::pivotX:
427 setPivotX(value);
Doris Liu766431a2016-02-04 22:17:11 +0000428 break;
Doris Liu1d8e1942016-03-02 15:16:28 -0800429 case Property::pivotY:
430 setPivotY(value);
Doris Liu766431a2016-02-04 22:17:11 +0000431 break;
Doris Liu1d8e1942016-03-02 15:16:28 -0800432 case Property::scaleX:
433 setScaleX(value);
Doris Liu766431a2016-02-04 22:17:11 +0000434 break;
Doris Liu1d8e1942016-03-02 15:16:28 -0800435 case Property::scaleY:
436 setScaleY(value);
Doris Liu766431a2016-02-04 22:17:11 +0000437 break;
Doris Liu1d8e1942016-03-02 15:16:28 -0800438 case Property::translateX:
439 setTranslateX(value);
Doris Liu766431a2016-02-04 22:17:11 +0000440 break;
Doris Liu1d8e1942016-03-02 15:16:28 -0800441 case Property::translateY:
442 setTranslateY(value);
Doris Liu766431a2016-02-04 22:17:11 +0000443 break;
444 default:
445 LOG_ALWAYS_FATAL("Invalid property index: %d", propertyId);
446 }
447}
448
449bool Group::isValidProperty(int propertyId) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800450 return GroupProperties::isValidProperty(propertyId);
451}
452
453bool Group::GroupProperties::isValidProperty(int propertyId) {
454 return propertyId >= 0 && propertyId < static_cast<int>(Property::count);
Doris Liu766431a2016-02-04 22:17:11 +0000455}
456
Doris Liuf8d131c2016-04-29 18:41:29 -0700457int Tree::draw(Canvas* outCanvas, SkColorFilter* colorFilter,
Doris Liu4bbc2932015-12-01 17:59:40 -0800458 const SkRect& bounds, bool needsMirroring, bool canReuseCache) {
459 // The imageView can scale the canvas in different ways, in order to
460 // avoid blurry scaling, we have to draw into a bitmap with exact pixel
461 // size first. This bitmap size is determined by the bounds and the
462 // canvas scale.
Doris Liu1d8e1942016-03-02 15:16:28 -0800463 SkMatrix canvasMatrix;
464 outCanvas->getMatrix(&canvasMatrix);
Doris Liua0e61572015-12-29 14:57:49 -0800465 float canvasScaleX = 1.0f;
466 float canvasScaleY = 1.0f;
Doris Liu1d8e1942016-03-02 15:16:28 -0800467 if (canvasMatrix.getSkewX() == 0 && canvasMatrix.getSkewY() == 0) {
Doris Liua0e61572015-12-29 14:57:49 -0800468 // Only use the scale value when there's no skew or rotation in the canvas matrix.
Doris Liue410a352016-01-13 17:23:33 -0800469 // TODO: Add a cts test for drawing VD on a canvas with negative scaling factors.
Doris Liu1d8e1942016-03-02 15:16:28 -0800470 canvasScaleX = fabs(canvasMatrix.getScaleX());
471 canvasScaleY = fabs(canvasMatrix.getScaleY());
Doris Liua0e61572015-12-29 14:57:49 -0800472 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800473 int scaledWidth = (int) (bounds.width() * canvasScaleX);
474 int scaledHeight = (int) (bounds.height() * canvasScaleY);
Doris Liu4bbc2932015-12-01 17:59:40 -0800475 scaledWidth = std::min(Tree::MAX_CACHED_BITMAP_SIZE, scaledWidth);
476 scaledHeight = std::min(Tree::MAX_CACHED_BITMAP_SIZE, scaledHeight);
477
478 if (scaledWidth <= 0 || scaledHeight <= 0) {
Doris Liuf8d131c2016-04-29 18:41:29 -0700479 return 0;
Doris Liu4bbc2932015-12-01 17:59:40 -0800480 }
481
Doris Liu1d8e1942016-03-02 15:16:28 -0800482 mStagingProperties.setScaledSize(scaledWidth, scaledHeight);
Florin Malita777bf852016-02-03 10:48:55 -0500483 int saveCount = outCanvas->save(SaveFlags::MatrixClip);
Doris Liu1d8e1942016-03-02 15:16:28 -0800484 outCanvas->translate(bounds.fLeft, bounds.fTop);
Doris Liu4bbc2932015-12-01 17:59:40 -0800485
486 // Handle RTL mirroring.
487 if (needsMirroring) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800488 outCanvas->translate(bounds.width(), 0);
Doris Liu4bbc2932015-12-01 17:59:40 -0800489 outCanvas->scale(-1.0f, 1.0f);
490 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800491 mStagingProperties.setColorFilter(colorFilter);
Doris Liu4bbc2932015-12-01 17:59:40 -0800492
493 // At this point, canvas has been translated to the right position.
494 // And we use this bound for the destination rect for the drawBitmap, so
495 // we offset to (0, 0);
Doris Liu1d8e1942016-03-02 15:16:28 -0800496 SkRect tmpBounds = bounds;
497 tmpBounds.offsetTo(0, 0);
498 mStagingProperties.setBounds(tmpBounds);
Doris Liu766431a2016-02-04 22:17:11 +0000499 outCanvas->drawVectorDrawable(this);
Doris Liu4bbc2932015-12-01 17:59:40 -0800500 outCanvas->restoreToCount(saveCount);
Doris Liuf8d131c2016-04-29 18:41:29 -0700501 return scaledWidth * scaledHeight;
Doris Liu4bbc2932015-12-01 17:59:40 -0800502}
503
Doris Liu1d8e1942016-03-02 15:16:28 -0800504void Tree::drawStaging(Canvas* outCanvas) {
sergeyvfc9999502016-10-17 13:07:38 -0700505 bool redrawNeeded = allocateBitmapIfNeeded(mStagingCache,
Doris Liu1d8e1942016-03-02 15:16:28 -0800506 mStagingProperties.getScaledWidth(), mStagingProperties.getScaledHeight());
507 // draw bitmap cache
508 if (redrawNeeded || mStagingCache.dirty) {
sergeyvfc9999502016-10-17 13:07:38 -0700509 updateBitmapCache(*mStagingCache.bitmap, true);
Doris Liu1d8e1942016-03-02 15:16:28 -0800510 mStagingCache.dirty = false;
Doris Liu4bbc2932015-12-01 17:59:40 -0800511 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800512
513 SkPaint tmpPaint;
514 SkPaint* paint = updatePaint(&tmpPaint, &mStagingProperties);
sergeyvfc9999502016-10-17 13:07:38 -0700515 outCanvas->drawBitmap(*mStagingCache.bitmap, 0, 0,
516 mStagingCache.bitmap->width(), mStagingCache.bitmap->height(),
Doris Liu1d8e1942016-03-02 15:16:28 -0800517 mStagingProperties.getBounds().left(), mStagingProperties.getBounds().top(),
518 mStagingProperties.getBounds().right(), mStagingProperties.getBounds().bottom(), paint);
519}
520
521SkPaint* Tree::getPaint() {
522 return updatePaint(&mPaint, &mProperties);
523}
524
525// Update the given paint with alpha and color filter. Return nullptr if no color filter is
526// specified and root alpha is 1. Otherwise, return updated paint.
527SkPaint* Tree::updatePaint(SkPaint* outPaint, TreeProperties* prop) {
528 if (prop->getRootAlpha() == 1.0f && prop->getColorFilter() == nullptr) {
529 return nullptr;
530 } else {
Mike Reed260ab722016-10-07 15:59:20 -0400531 outPaint->setColorFilter(sk_ref_sp(prop->getColorFilter()));
Doris Liu1d8e1942016-03-02 15:16:28 -0800532 outPaint->setFilterQuality(kLow_SkFilterQuality);
533 outPaint->setAlpha(prop->getRootAlpha() * 255);
534 return outPaint;
535 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800536}
537
sergeyvfc9999502016-10-17 13:07:38 -0700538Bitmap& Tree::getBitmapUpdateIfDirty() {
539 bool redrawNeeded = allocateBitmapIfNeeded(mCache, mProperties.getScaledWidth(),
Doris Liu1d8e1942016-03-02 15:16:28 -0800540 mProperties.getScaledHeight());
541 if (redrawNeeded || mCache.dirty) {
sergeyvfc9999502016-10-17 13:07:38 -0700542 updateBitmapCache(*mCache.bitmap, false);
Doris Liu1d8e1942016-03-02 15:16:28 -0800543 mCache.dirty = false;
544 }
sergeyvfc9999502016-10-17 13:07:38 -0700545 return *mCache.bitmap;
Doris Liu4bbc2932015-12-01 17:59:40 -0800546}
547
sergeyvfc9999502016-10-17 13:07:38 -0700548void Tree::updateBitmapCache(Bitmap& bitmap, bool useStagingData) {
549 SkBitmap outCache;
550 bitmap.getSkBitmap(&outCache);
551 outCache.eraseColor(SK_ColorTRANSPARENT);
552 SkCanvas outCanvas(outCache);
Doris Liu1d8e1942016-03-02 15:16:28 -0800553 float viewportWidth = useStagingData ?
554 mStagingProperties.getViewportWidth() : mProperties.getViewportWidth();
555 float viewportHeight = useStagingData ?
556 mStagingProperties.getViewportHeight() : mProperties.getViewportHeight();
sergeyvfc9999502016-10-17 13:07:38 -0700557 float scaleX = outCache.width() / viewportWidth;
558 float scaleY = outCache.height() / viewportHeight;
Doris Liu1d8e1942016-03-02 15:16:28 -0800559 mRootNode->draw(&outCanvas, SkMatrix::I(), scaleX, scaleY, useStagingData);
560}
561
sergeyvfc9999502016-10-17 13:07:38 -0700562bool Tree::allocateBitmapIfNeeded(Cache& cache, int width, int height) {
563 if (!canReuseBitmap(cache.bitmap.get(), width, height)) {
Romain Guy253f2c22016-09-28 17:34:42 -0700564#ifndef ANDROID_ENABLE_LINEAR_BLENDING
565 sk_sp<SkColorSpace> colorSpace = nullptr;
566#else
567 sk_sp<SkColorSpace> colorSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named);
568#endif
569 SkImageInfo info = SkImageInfo::MakeN32(width, height, kPremul_SkAlphaType, colorSpace);
sergeyvfc9999502016-10-17 13:07:38 -0700570 cache.bitmap = Bitmap::allocateHeapBitmap(info);
Doris Liu1d8e1942016-03-02 15:16:28 -0800571 return true;
Doris Liu4bbc2932015-12-01 17:59:40 -0800572 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800573 return false;
Doris Liu4bbc2932015-12-01 17:59:40 -0800574}
575
sergeyvfc9999502016-10-17 13:07:38 -0700576bool Tree::canReuseBitmap(Bitmap* bitmap, int width, int height) {
577 return bitmap && width == bitmap->width() && height == bitmap->height();
Doris Liu1d8e1942016-03-02 15:16:28 -0800578}
579
580void Tree::onPropertyChanged(TreeProperties* prop) {
581 if (prop == &mStagingProperties) {
582 mStagingCache.dirty = true;
583 } else {
584 mCache.dirty = true;
585 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800586}
587
588}; // namespace VectorDrawable
589
590}; // namespace uirenderer
591}; // namespace android