blob: ac17ed2eb735ec2ec094e595c36af93513a3bd68 [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()));
204 SkShader* newShader = properties.getFillGradient()->newWithLocalMatrix(matrix);
205 paint.setShader(newShader);
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800206 needsFill = true;
Doris Liu1d8e1942016-03-02 15:16:28 -0800207 } else if (properties.getFillColor() != SK_ColorTRANSPARENT) {
208 paint.setColor(applyAlpha(properties.getFillColor(), properties.getFillAlpha()));
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800209 needsFill = true;
Doris Liu4bbc2932015-12-01 17:59:40 -0800210 }
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800211
212 if (needsFill) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800213 paint.setStyle(SkPaint::Style::kFill_Style);
214 paint.setAntiAlias(true);
215 SkPath::FillType ft = static_cast<SkPath::FillType>(properties.getFillType());
Teng-Hui Zhu46591f42016-03-15 14:32:16 -0700216 renderPath.setFillType(ft);
Doris Liu1d8e1942016-03-02 15:16:28 -0800217 outCanvas->drawPath(renderPath, paint);
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800218 }
219
Doris Liu1d8e1942016-03-02 15:16:28 -0800220 // Draw path's stroke, if stroke color or Gradient is valid
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800221 bool needsStroke = false;
Doris Liu1d8e1942016-03-02 15:16:28 -0800222 if (properties.getStrokeGradient() != nullptr) {
223 paint.setColor(applyAlpha(SK_ColorBLACK, properties.getStrokeAlpha()));
224 SkShader* newShader = properties.getStrokeGradient()->newWithLocalMatrix(matrix);
225 paint.setShader(newShader);
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800226 needsStroke = true;
Doris Liu1d8e1942016-03-02 15:16:28 -0800227 } else if (properties.getStrokeColor() != SK_ColorTRANSPARENT) {
228 paint.setColor(applyAlpha(properties.getStrokeColor(), properties.getStrokeAlpha()));
Teng-Hui Zhudbee9bb2015-12-15 11:01:27 -0800229 needsStroke = true;
230 }
231 if (needsStroke) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800232 paint.setStyle(SkPaint::Style::kStroke_Style);
233 paint.setAntiAlias(true);
234 paint.setStrokeJoin(SkPaint::Join(properties.getStrokeLineJoin()));
235 paint.setStrokeCap(SkPaint::Cap(properties.getStrokeLineCap()));
236 paint.setStrokeMiter(properties.getStrokeMiterLimit());
237 paint.setStrokeWidth(properties.getStrokeWidth() * strokeScale);
238 outCanvas->drawPath(renderPath, paint);
Doris Liu4bbc2932015-12-01 17:59:40 -0800239 }
240}
241
Doris Liu1d8e1942016-03-02 15:16:28 -0800242void FullPath::syncProperties() {
243 Path::syncProperties();
Doris Liu4bbc2932015-12-01 17:59:40 -0800244
Doris Liu1d8e1942016-03-02 15:16:28 -0800245 if (mStagingPropertiesDirty) {
246 mProperties.syncProperties(mStagingProperties);
Doris Liu4bbc2932015-12-01 17:59:40 -0800247 } else {
Doris Liu1d8e1942016-03-02 15:16:28 -0800248 // Update staging property with property values from animation.
249 mStagingProperties.syncProperties(mProperties);
Doris Liu4bbc2932015-12-01 17:59:40 -0800250 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800251 mStagingPropertiesDirty = false;
Doris Liu4bbc2932015-12-01 17:59:40 -0800252}
253
Doris Liu1d8e1942016-03-02 15:16:28 -0800254REQUIRE_COMPATIBLE_LAYOUT(FullPath::FullPathProperties::PrimitiveFields);
Doris Liu4bbc2932015-12-01 17:59:40 -0800255
256static_assert(sizeof(float) == sizeof(int32_t), "float is not the same size as int32_t");
257static_assert(sizeof(SkColor) == sizeof(int32_t), "SkColor is not the same size as int32_t");
258
Doris Liu1d8e1942016-03-02 15:16:28 -0800259bool FullPath::FullPathProperties::copyProperties(int8_t* outProperties, int length) const {
260 int propertyDataSize = sizeof(FullPathProperties::PrimitiveFields);
Doris Liu4bbc2932015-12-01 17:59:40 -0800261 if (length != propertyDataSize) {
262 LOG_ALWAYS_FATAL("Properties needs exactly %d bytes, a byte array of size %d is provided",
263 propertyDataSize, length);
264 return false;
265 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800266
267 PrimitiveFields* out = reinterpret_cast<PrimitiveFields*>(outProperties);
268 *out = mPrimitiveFields;
Doris Liu4bbc2932015-12-01 17:59:40 -0800269 return true;
270}
271
Doris Liu1d8e1942016-03-02 15:16:28 -0800272void FullPath::FullPathProperties::setColorPropertyValue(int propertyId, int32_t value) {
Doris Liu766431a2016-02-04 22:17:11 +0000273 Property currentProperty = static_cast<Property>(propertyId);
Doris Liu1d8e1942016-03-02 15:16:28 -0800274 if (currentProperty == Property::strokeColor) {
275 setStrokeColor(value);
276 } else if (currentProperty == Property::fillColor) {
277 setFillColor(value);
Doris Liu766431a2016-02-04 22:17:11 +0000278 } else {
Doris Liu1d8e1942016-03-02 15:16:28 -0800279 LOG_ALWAYS_FATAL("Error setting color property on FullPath: No valid property"
280 " with id: %d", propertyId);
Doris Liu766431a2016-02-04 22:17:11 +0000281 }
282}
283
Doris Liu1d8e1942016-03-02 15:16:28 -0800284void FullPath::FullPathProperties::setPropertyValue(int propertyId, float value) {
Doris Liu766431a2016-02-04 22:17:11 +0000285 Property property = static_cast<Property>(propertyId);
286 switch (property) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800287 case Property::strokeWidth:
Doris Liu766431a2016-02-04 22:17:11 +0000288 setStrokeWidth(value);
289 break;
Doris Liu1d8e1942016-03-02 15:16:28 -0800290 case Property::strokeAlpha:
Doris Liu766431a2016-02-04 22:17:11 +0000291 setStrokeAlpha(value);
292 break;
Doris Liu1d8e1942016-03-02 15:16:28 -0800293 case Property::fillAlpha:
Doris Liu766431a2016-02-04 22:17:11 +0000294 setFillAlpha(value);
295 break;
Doris Liu1d8e1942016-03-02 15:16:28 -0800296 case Property::trimPathStart:
Doris Liu766431a2016-02-04 22:17:11 +0000297 setTrimPathStart(value);
298 break;
Doris Liu1d8e1942016-03-02 15:16:28 -0800299 case Property::trimPathEnd:
Doris Liu766431a2016-02-04 22:17:11 +0000300 setTrimPathEnd(value);
301 break;
Doris Liu1d8e1942016-03-02 15:16:28 -0800302 case Property::trimPathOffset:
Doris Liu766431a2016-02-04 22:17:11 +0000303 setTrimPathOffset(value);
304 break;
305 default:
306 LOG_ALWAYS_FATAL("Invalid property id: %d for animation", propertyId);
307 break;
308 }
309}
310
Teng-Hui Zhu46591f42016-03-15 14:32:16 -0700311void ClipPath::drawPath(SkCanvas* outCanvas, SkPath& renderPath,
Doris Liu1d8e1942016-03-02 15:16:28 -0800312 float strokeScale, const SkMatrix& matrix, bool useStagingData){
Doris Liuc2de46f2016-01-21 12:55:54 -0800313 outCanvas->clipPath(renderPath, SkRegion::kIntersect_Op);
Doris Liu4bbc2932015-12-01 17:59:40 -0800314}
315
316Group::Group(const Group& group) : Node(group) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800317 mStagingProperties.syncProperties(group.mStagingProperties);
Doris Liu4bbc2932015-12-01 17:59:40 -0800318}
319
Doris Liuc2de46f2016-01-21 12:55:54 -0800320void Group::draw(SkCanvas* outCanvas, const SkMatrix& currentMatrix, float scaleX,
Doris Liu1d8e1942016-03-02 15:16:28 -0800321 float scaleY, bool useStagingData) {
Doris Liu4bbc2932015-12-01 17:59:40 -0800322 // TODO: Try apply the matrix to the canvas instead of passing it down the tree
323
324 // Calculate current group's matrix by preConcat the parent's and
325 // and the current one on the top of the stack.
326 // Basically the Mfinal = Mviewport * M0 * M1 * M2;
327 // Mi the local matrix at level i of the group tree.
328 SkMatrix stackedMatrix;
Doris Liu1d8e1942016-03-02 15:16:28 -0800329 const GroupProperties& prop = useStagingData ? mStagingProperties : mProperties;
330 getLocalMatrix(&stackedMatrix, prop);
Doris Liu4bbc2932015-12-01 17:59:40 -0800331 stackedMatrix.postConcat(currentMatrix);
332
333 // Save the current clip information, which is local to this group.
Doris Liuc2de46f2016-01-21 12:55:54 -0800334 outCanvas->save();
Doris Liu4bbc2932015-12-01 17:59:40 -0800335 // Draw the group tree in the same order as the XML file.
Doris Liuef062eb2016-02-04 16:16:27 -0800336 for (auto& child : mChildren) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800337 child->draw(outCanvas, stackedMatrix, scaleX, scaleY, useStagingData);
Doris Liu4bbc2932015-12-01 17:59:40 -0800338 }
339 // Restore the previous clip information.
340 outCanvas->restore();
341}
342
343void Group::dump() {
344 ALOGD("Group %s has %zu children: ", mName.c_str(), mChildren.size());
Doris Liu1d8e1942016-03-02 15:16:28 -0800345 ALOGD("Group translateX, Y : %f, %f, scaleX, Y: %f, %f", mProperties.getTranslateX(),
346 mProperties.getTranslateY(), mProperties.getScaleX(), mProperties.getScaleY());
Doris Liu4bbc2932015-12-01 17:59:40 -0800347 for (size_t i = 0; i < mChildren.size(); i++) {
348 mChildren[i]->dump();
349 }
350}
351
Doris Liu1d8e1942016-03-02 15:16:28 -0800352void Group::syncProperties() {
353 // Copy over the dirty staging properties
354 if (mStagingPropertiesDirty) {
355 mProperties.syncProperties(mStagingProperties);
356 } else {
357 mStagingProperties.syncProperties(mProperties);
358 }
359 mStagingPropertiesDirty = false;
360 for (auto& child : mChildren) {
361 child->syncProperties();
362 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800363}
364
Doris Liu1d8e1942016-03-02 15:16:28 -0800365void Group::getLocalMatrix(SkMatrix* outMatrix, const GroupProperties& properties) {
Doris Liu4bbc2932015-12-01 17:59:40 -0800366 outMatrix->reset();
367 // TODO: use rotate(mRotate, mPivotX, mPivotY) and scale with pivot point, instead of
368 // translating to pivot for rotating and scaling, then translating back.
Doris Liu1d8e1942016-03-02 15:16:28 -0800369 outMatrix->postTranslate(-properties.getPivotX(), -properties.getPivotY());
370 outMatrix->postScale(properties.getScaleX(), properties.getScaleY());
371 outMatrix->postRotate(properties.getRotation(), 0, 0);
372 outMatrix->postTranslate(properties.getTranslateX() + properties.getPivotX(),
373 properties.getTranslateY() + properties.getPivotY());
Doris Liu4bbc2932015-12-01 17:59:40 -0800374}
375
376void Group::addChild(Node* child) {
Doris Liuef062eb2016-02-04 16:16:27 -0800377 mChildren.emplace_back(child);
Doris Liu1d8e1942016-03-02 15:16:28 -0800378 if (mPropertyChangedListener != nullptr) {
379 child->setPropertyChangedListener(mPropertyChangedListener);
380 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800381}
382
Doris Liu1d8e1942016-03-02 15:16:28 -0800383bool Group::GroupProperties::copyProperties(float* outProperties, int length) const {
384 int propertyCount = static_cast<int>(Property::count);
Doris Liu4bbc2932015-12-01 17:59:40 -0800385 if (length != propertyCount) {
386 LOG_ALWAYS_FATAL("Properties needs exactly %d bytes, a byte array of size %d is provided",
387 propertyCount, length);
388 return false;
389 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800390
391 PrimitiveFields* out = reinterpret_cast<PrimitiveFields*>(outProperties);
392 *out = mPrimitiveFields;
Doris Liu4bbc2932015-12-01 17:59:40 -0800393 return true;
394}
395
Doris Liu766431a2016-02-04 22:17:11 +0000396// TODO: Consider animating the properties as float pointers
Doris Liu1d8e1942016-03-02 15:16:28 -0800397// Called on render thread
398float Group::GroupProperties::getPropertyValue(int propertyId) const {
Doris Liu766431a2016-02-04 22:17:11 +0000399 Property currentProperty = static_cast<Property>(propertyId);
400 switch (currentProperty) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800401 case Property::rotate:
402 return getRotation();
403 case Property::pivotX:
404 return getPivotX();
405 case Property::pivotY:
406 return getPivotY();
407 case Property::scaleX:
408 return getScaleX();
409 case Property::scaleY:
410 return getScaleY();
411 case Property::translateX:
412 return getTranslateX();
413 case Property::translateY:
414 return getTranslateY();
Doris Liu766431a2016-02-04 22:17:11 +0000415 default:
416 LOG_ALWAYS_FATAL("Invalid property index: %d", propertyId);
417 return 0;
418 }
419}
420
Doris Liu1d8e1942016-03-02 15:16:28 -0800421// Called on render thread
422void Group::GroupProperties::setPropertyValue(int propertyId, float value) {
Doris Liu766431a2016-02-04 22:17:11 +0000423 Property currentProperty = static_cast<Property>(propertyId);
424 switch (currentProperty) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800425 case Property::rotate:
426 setRotation(value);
Doris Liu766431a2016-02-04 22:17:11 +0000427 break;
Doris Liu1d8e1942016-03-02 15:16:28 -0800428 case Property::pivotX:
429 setPivotX(value);
Doris Liu766431a2016-02-04 22:17:11 +0000430 break;
Doris Liu1d8e1942016-03-02 15:16:28 -0800431 case Property::pivotY:
432 setPivotY(value);
Doris Liu766431a2016-02-04 22:17:11 +0000433 break;
Doris Liu1d8e1942016-03-02 15:16:28 -0800434 case Property::scaleX:
435 setScaleX(value);
Doris Liu766431a2016-02-04 22:17:11 +0000436 break;
Doris Liu1d8e1942016-03-02 15:16:28 -0800437 case Property::scaleY:
438 setScaleY(value);
Doris Liu766431a2016-02-04 22:17:11 +0000439 break;
Doris Liu1d8e1942016-03-02 15:16:28 -0800440 case Property::translateX:
441 setTranslateX(value);
Doris Liu766431a2016-02-04 22:17:11 +0000442 break;
Doris Liu1d8e1942016-03-02 15:16:28 -0800443 case Property::translateY:
444 setTranslateY(value);
Doris Liu766431a2016-02-04 22:17:11 +0000445 break;
446 default:
447 LOG_ALWAYS_FATAL("Invalid property index: %d", propertyId);
448 }
449}
450
451bool Group::isValidProperty(int propertyId) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800452 return GroupProperties::isValidProperty(propertyId);
453}
454
455bool Group::GroupProperties::isValidProperty(int propertyId) {
456 return propertyId >= 0 && propertyId < static_cast<int>(Property::count);
Doris Liu766431a2016-02-04 22:17:11 +0000457}
458
Doris Liu4bbc2932015-12-01 17:59:40 -0800459void Tree::draw(Canvas* outCanvas, SkColorFilter* colorFilter,
460 const SkRect& bounds, bool needsMirroring, bool canReuseCache) {
461 // The imageView can scale the canvas in different ways, in order to
462 // avoid blurry scaling, we have to draw into a bitmap with exact pixel
463 // size first. This bitmap size is determined by the bounds and the
464 // canvas scale.
Doris Liu1d8e1942016-03-02 15:16:28 -0800465 SkMatrix canvasMatrix;
466 outCanvas->getMatrix(&canvasMatrix);
Doris Liua0e61572015-12-29 14:57:49 -0800467 float canvasScaleX = 1.0f;
468 float canvasScaleY = 1.0f;
Doris Liu1d8e1942016-03-02 15:16:28 -0800469 if (canvasMatrix.getSkewX() == 0 && canvasMatrix.getSkewY() == 0) {
Doris Liua0e61572015-12-29 14:57:49 -0800470 // Only use the scale value when there's no skew or rotation in the canvas matrix.
Doris Liue410a352016-01-13 17:23:33 -0800471 // TODO: Add a cts test for drawing VD on a canvas with negative scaling factors.
Doris Liu1d8e1942016-03-02 15:16:28 -0800472 canvasScaleX = fabs(canvasMatrix.getScaleX());
473 canvasScaleY = fabs(canvasMatrix.getScaleY());
Doris Liua0e61572015-12-29 14:57:49 -0800474 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800475 int scaledWidth = (int) (bounds.width() * canvasScaleX);
476 int scaledHeight = (int) (bounds.height() * canvasScaleY);
Doris Liu4bbc2932015-12-01 17:59:40 -0800477 scaledWidth = std::min(Tree::MAX_CACHED_BITMAP_SIZE, scaledWidth);
478 scaledHeight = std::min(Tree::MAX_CACHED_BITMAP_SIZE, scaledHeight);
479
480 if (scaledWidth <= 0 || scaledHeight <= 0) {
481 return;
482 }
483
Doris Liu1d8e1942016-03-02 15:16:28 -0800484 mStagingProperties.setScaledSize(scaledWidth, scaledHeight);
Florin Malita777bf852016-02-03 10:48:55 -0500485 int saveCount = outCanvas->save(SaveFlags::MatrixClip);
Doris Liu1d8e1942016-03-02 15:16:28 -0800486 outCanvas->translate(bounds.fLeft, bounds.fTop);
Doris Liu4bbc2932015-12-01 17:59:40 -0800487
488 // Handle RTL mirroring.
489 if (needsMirroring) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800490 outCanvas->translate(bounds.width(), 0);
Doris Liu4bbc2932015-12-01 17:59:40 -0800491 outCanvas->scale(-1.0f, 1.0f);
492 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800493 mStagingProperties.setColorFilter(colorFilter);
Doris Liu4bbc2932015-12-01 17:59:40 -0800494
495 // At this point, canvas has been translated to the right position.
496 // And we use this bound for the destination rect for the drawBitmap, so
497 // we offset to (0, 0);
Doris Liu1d8e1942016-03-02 15:16:28 -0800498 SkRect tmpBounds = bounds;
499 tmpBounds.offsetTo(0, 0);
500 mStagingProperties.setBounds(tmpBounds);
Doris Liu766431a2016-02-04 22:17:11 +0000501 outCanvas->drawVectorDrawable(this);
Doris Liu4bbc2932015-12-01 17:59:40 -0800502 outCanvas->restoreToCount(saveCount);
503}
504
Doris Liu1d8e1942016-03-02 15:16:28 -0800505void Tree::drawStaging(Canvas* outCanvas) {
506 bool redrawNeeded = allocateBitmapIfNeeded(&mStagingCache.bitmap,
507 mStagingProperties.getScaledWidth(), mStagingProperties.getScaledHeight());
508 // draw bitmap cache
509 if (redrawNeeded || mStagingCache.dirty) {
510 updateBitmapCache(&mStagingCache.bitmap, true);
511 mStagingCache.dirty = false;
Doris Liu4bbc2932015-12-01 17:59:40 -0800512 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800513
514 SkPaint tmpPaint;
515 SkPaint* paint = updatePaint(&tmpPaint, &mStagingProperties);
516 outCanvas->drawBitmap(mStagingCache.bitmap, 0, 0,
517 mStagingCache.bitmap.width(), mStagingCache.bitmap.height(),
518 mStagingProperties.getBounds().left(), mStagingProperties.getBounds().top(),
519 mStagingProperties.getBounds().right(), mStagingProperties.getBounds().bottom(), paint);
520}
521
522SkPaint* Tree::getPaint() {
523 return updatePaint(&mPaint, &mProperties);
524}
525
526// Update the given paint with alpha and color filter. Return nullptr if no color filter is
527// specified and root alpha is 1. Otherwise, return updated paint.
528SkPaint* Tree::updatePaint(SkPaint* outPaint, TreeProperties* prop) {
529 if (prop->getRootAlpha() == 1.0f && prop->getColorFilter() == nullptr) {
530 return nullptr;
531 } else {
532 outPaint->setColorFilter(mStagingProperties.getColorFilter());
533 outPaint->setFilterQuality(kLow_SkFilterQuality);
534 outPaint->setAlpha(prop->getRootAlpha() * 255);
535 return outPaint;
536 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800537}
538
Doris Liu766431a2016-02-04 22:17:11 +0000539const SkBitmap& Tree::getBitmapUpdateIfDirty() {
Doris Liu1d8e1942016-03-02 15:16:28 -0800540 bool redrawNeeded = allocateBitmapIfNeeded(&mCache.bitmap, mProperties.getScaledWidth(),
541 mProperties.getScaledHeight());
542 if (redrawNeeded || mCache.dirty) {
543 updateBitmapCache(&mCache.bitmap, false);
544 mCache.dirty = false;
545 }
546 return mCache.bitmap;
Doris Liu4bbc2932015-12-01 17:59:40 -0800547}
548
Doris Liu1d8e1942016-03-02 15:16:28 -0800549void Tree::updateBitmapCache(SkBitmap* outCache, bool useStagingData) {
550 outCache->eraseColor(SK_ColorTRANSPARENT);
551 SkCanvas outCanvas(*outCache);
552 float viewportWidth = useStagingData ?
553 mStagingProperties.getViewportWidth() : mProperties.getViewportWidth();
554 float viewportHeight = useStagingData ?
555 mStagingProperties.getViewportHeight() : mProperties.getViewportHeight();
556 float scaleX = outCache->width() / viewportWidth;
557 float scaleY = outCache->height() / viewportHeight;
558 mRootNode->draw(&outCanvas, SkMatrix::I(), scaleX, scaleY, useStagingData);
559}
560
561bool Tree::allocateBitmapIfNeeded(SkBitmap* outCache, int width, int height) {
562 if (!canReuseBitmap(*outCache, width, height)) {
Doris Liu4bbc2932015-12-01 17:59:40 -0800563 SkImageInfo info = SkImageInfo::Make(width, height,
564 kN32_SkColorType, kPremul_SkAlphaType);
Doris Liu1d8e1942016-03-02 15:16:28 -0800565 outCache->setInfo(info);
Doris Liu4bbc2932015-12-01 17:59:40 -0800566 // TODO: Count the bitmap cache against app's java heap
Doris Liu1d8e1942016-03-02 15:16:28 -0800567 outCache->allocPixels(info);
568 return true;
Doris Liu4bbc2932015-12-01 17:59:40 -0800569 }
Doris Liu1d8e1942016-03-02 15:16:28 -0800570 return false;
Doris Liu4bbc2932015-12-01 17:59:40 -0800571}
572
Doris Liu1d8e1942016-03-02 15:16:28 -0800573bool Tree::canReuseBitmap(const SkBitmap& bitmap, int width, int height) {
574 return width == bitmap.width() && height == bitmap.height();
575}
576
577void Tree::onPropertyChanged(TreeProperties* prop) {
578 if (prop == &mStagingProperties) {
579 mStagingCache.dirty = true;
580 } else {
581 mCache.dirty = true;
582 }
Doris Liu4bbc2932015-12-01 17:59:40 -0800583}
584
585}; // namespace VectorDrawable
586
587}; // namespace uirenderer
588}; // namespace android