Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 1 | /* |
| 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" |
| 20 | #include "SkImageInfo.h" |
Teng-Hui Zhu | dbee9bb | 2015-12-15 11:01:27 -0800 | [diff] [blame] | 21 | #include "SkShader.h" |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 22 | #include <utils/Log.h> |
| 23 | #include "utils/Macros.h" |
| 24 | #include "utils/VectorDrawableUtils.h" |
| 25 | |
| 26 | #include <math.h> |
| 27 | #include <string.h> |
| 28 | |
| 29 | namespace android { |
| 30 | namespace uirenderer { |
| 31 | namespace VectorDrawable { |
| 32 | |
| 33 | const int Tree::MAX_CACHED_BITMAP_SIZE = 2048; |
| 34 | |
Doris Liu | c2de46f | 2016-01-21 12:55:54 -0800 | [diff] [blame] | 35 | void Path::draw(SkCanvas* outCanvas, const SkMatrix& groupStackedMatrix, float scaleX, float scaleY) { |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 36 | float matrixScale = getMatrixScale(groupStackedMatrix); |
| 37 | if (matrixScale == 0) { |
| 38 | // When either x or y is scaled to 0, we don't need to draw anything. |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | const SkPath updatedPath = getUpdatedPath(); |
| 43 | SkMatrix pathMatrix(groupStackedMatrix); |
| 44 | pathMatrix.postScale(scaleX, scaleY); |
| 45 | |
| 46 | //TODO: try apply the path matrix to the canvas instead of creating a new path. |
| 47 | SkPath renderPath; |
| 48 | renderPath.reset(); |
| 49 | renderPath.addPath(updatedPath, pathMatrix); |
| 50 | |
| 51 | float minScale = fmin(scaleX, scaleY); |
| 52 | float strokeScale = minScale * matrixScale; |
Teng-Hui Zhu | dbee9bb | 2015-12-15 11:01:27 -0800 | [diff] [blame] | 53 | drawPath(outCanvas, renderPath, strokeScale, pathMatrix); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void Path::setPathData(const Data& data) { |
| 57 | if (mData == data) { |
| 58 | return; |
| 59 | } |
| 60 | // Updates the path data. Note that we don't generate a new Skia path right away |
| 61 | // because there are cases where the animation is changing the path data, but the view |
| 62 | // that hosts the VD has gone off screen, in which case we won't even draw. So we |
| 63 | // postpone the Skia path generation to the draw time. |
| 64 | mData = data; |
| 65 | mSkPathDirty = true; |
| 66 | } |
| 67 | |
| 68 | void Path::dump() { |
| 69 | ALOGD("Path: %s has %zu points", mName.c_str(), mData.points.size()); |
| 70 | } |
| 71 | |
| 72 | float Path::getMatrixScale(const SkMatrix& groupStackedMatrix) { |
| 73 | // Given unit vectors A = (0, 1) and B = (1, 0). |
| 74 | // After matrix mapping, we got A' and B'. Let theta = the angel b/t A' and B'. |
| 75 | // Therefore, the final scale we want is min(|A'| * sin(theta), |B'| * sin(theta)), |
| 76 | // which is (|A'| * |B'| * sin(theta)) / max (|A'|, |B'|); |
| 77 | // If max (|A'|, |B'|) = 0, that means either x or y has a scale of 0. |
| 78 | // |
| 79 | // For non-skew case, which is most of the cases, matrix scale is computing exactly the |
| 80 | // scale on x and y axis, and take the minimal of these two. |
| 81 | // For skew case, an unit square will mapped to a parallelogram. And this function will |
| 82 | // return the minimal height of the 2 bases. |
| 83 | SkVector skVectors[2]; |
| 84 | skVectors[0].set(0, 1); |
| 85 | skVectors[1].set(1, 0); |
| 86 | groupStackedMatrix.mapVectors(skVectors, 2); |
| 87 | float scaleX = hypotf(skVectors[0].fX, skVectors[0].fY); |
| 88 | float scaleY = hypotf(skVectors[1].fX, skVectors[1].fY); |
| 89 | float crossProduct = skVectors[0].cross(skVectors[1]); |
| 90 | float maxScale = fmax(scaleX, scaleY); |
| 91 | |
| 92 | float matrixScale = 0; |
| 93 | if (maxScale > 0) { |
| 94 | matrixScale = fabs(crossProduct) / maxScale; |
| 95 | } |
| 96 | return matrixScale; |
| 97 | } |
| 98 | Path::Path(const char* pathStr, size_t strLength) { |
| 99 | PathParser::ParseResult result; |
| 100 | PathParser::getPathDataFromString(&mData, &result, pathStr, strLength); |
| 101 | if (!result.failureOccurred) { |
| 102 | VectorDrawableUtils::verbsToPath(&mSkPath, mData); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | Path::Path(const Data& data) { |
| 107 | mData = data; |
| 108 | // Now we need to construct a path |
| 109 | VectorDrawableUtils::verbsToPath(&mSkPath, data); |
| 110 | } |
| 111 | |
| 112 | Path::Path(const Path& path) : Node(path) { |
| 113 | mData = path.mData; |
| 114 | VectorDrawableUtils::verbsToPath(&mSkPath, mData); |
| 115 | } |
| 116 | |
| 117 | bool Path::canMorph(const Data& morphTo) { |
| 118 | return VectorDrawableUtils::canMorph(mData, morphTo); |
| 119 | } |
| 120 | |
| 121 | bool Path::canMorph(const Path& path) { |
| 122 | return canMorph(path.mData); |
| 123 | } |
| 124 | |
| 125 | const SkPath& Path::getUpdatedPath() { |
| 126 | if (mSkPathDirty) { |
| 127 | mSkPath.reset(); |
| 128 | VectorDrawableUtils::verbsToPath(&mSkPath, mData); |
| 129 | mSkPathDirty = false; |
| 130 | } |
| 131 | return mSkPath; |
| 132 | } |
| 133 | |
| 134 | void Path::setPath(const char* pathStr, size_t strLength) { |
| 135 | PathParser::ParseResult result; |
| 136 | mSkPathDirty = true; |
| 137 | PathParser::getPathDataFromString(&mData, &result, pathStr, strLength); |
| 138 | } |
| 139 | |
| 140 | FullPath::FullPath(const FullPath& path) : Path(path) { |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 141 | mProperties = path.mProperties; |
Teng-Hui Zhu | dbee9bb | 2015-12-15 11:01:27 -0800 | [diff] [blame] | 142 | SkRefCnt_SafeAssign(mStrokeGradient, path.mStrokeGradient); |
| 143 | SkRefCnt_SafeAssign(mFillGradient, path.mFillGradient); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | const SkPath& FullPath::getUpdatedPath() { |
| 147 | if (!mSkPathDirty && !mTrimDirty) { |
| 148 | return mTrimmedSkPath; |
| 149 | } |
| 150 | Path::getUpdatedPath(); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 151 | if (mProperties.trimPathStart != 0.0f || mProperties.trimPathEnd != 1.0f) { |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 152 | applyTrim(); |
| 153 | return mTrimmedSkPath; |
| 154 | } else { |
| 155 | return mSkPath; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | void FullPath::updateProperties(float strokeWidth, SkColor strokeColor, float strokeAlpha, |
| 160 | SkColor fillColor, float fillAlpha, float trimPathStart, float trimPathEnd, |
Teng-Hui Zhu | 46591f4 | 2016-03-15 14:32:16 -0700 | [diff] [blame^] | 161 | float trimPathOffset, float strokeMiterLimit, int strokeLineCap, int strokeLineJoin, |
| 162 | int fillType) { |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 163 | mProperties.strokeWidth = strokeWidth; |
| 164 | mProperties.strokeColor = strokeColor; |
| 165 | mProperties.strokeAlpha = strokeAlpha; |
| 166 | mProperties.fillColor = fillColor; |
| 167 | mProperties.fillAlpha = fillAlpha; |
| 168 | mProperties.strokeMiterLimit = strokeMiterLimit; |
| 169 | mProperties.strokeLineCap = strokeLineCap; |
| 170 | mProperties.strokeLineJoin = strokeLineJoin; |
Teng-Hui Zhu | 46591f4 | 2016-03-15 14:32:16 -0700 | [diff] [blame^] | 171 | mProperties.fillType = fillType; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 172 | |
| 173 | // If any trim property changes, mark trim dirty and update the trim path |
| 174 | setTrimPathStart(trimPathStart); |
| 175 | setTrimPathEnd(trimPathEnd); |
| 176 | setTrimPathOffset(trimPathOffset); |
| 177 | } |
| 178 | |
| 179 | inline SkColor applyAlpha(SkColor color, float alpha) { |
| 180 | int alphaBytes = SkColorGetA(color); |
| 181 | return SkColorSetA(color, alphaBytes * alpha); |
| 182 | } |
| 183 | |
Teng-Hui Zhu | 46591f4 | 2016-03-15 14:32:16 -0700 | [diff] [blame^] | 184 | void FullPath::drawPath(SkCanvas* outCanvas, SkPath& renderPath, float strokeScale, |
Teng-Hui Zhu | dbee9bb | 2015-12-15 11:01:27 -0800 | [diff] [blame] | 185 | const SkMatrix& matrix){ |
| 186 | // Draw path's fill, if fill color or gradient is valid |
| 187 | bool needsFill = false; |
| 188 | if (mFillGradient != nullptr) { |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 189 | mPaint.setColor(applyAlpha(SK_ColorBLACK, mProperties.fillAlpha)); |
Teng-Hui Zhu | dbee9bb | 2015-12-15 11:01:27 -0800 | [diff] [blame] | 190 | SkShader* newShader = mFillGradient->newWithLocalMatrix(matrix); |
| 191 | mPaint.setShader(newShader); |
| 192 | needsFill = true; |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 193 | } else if (mProperties.fillColor != SK_ColorTRANSPARENT) { |
| 194 | mPaint.setColor(applyAlpha(mProperties.fillColor, mProperties.fillAlpha)); |
Teng-Hui Zhu | dbee9bb | 2015-12-15 11:01:27 -0800 | [diff] [blame] | 195 | needsFill = true; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 196 | } |
Teng-Hui Zhu | dbee9bb | 2015-12-15 11:01:27 -0800 | [diff] [blame] | 197 | |
| 198 | if (needsFill) { |
| 199 | mPaint.setStyle(SkPaint::Style::kFill_Style); |
| 200 | mPaint.setAntiAlias(true); |
Teng-Hui Zhu | 46591f4 | 2016-03-15 14:32:16 -0700 | [diff] [blame^] | 201 | SkPath::FillType ft = static_cast<SkPath::FillType>(mProperties.fillType); |
| 202 | renderPath.setFillType(ft); |
Teng-Hui Zhu | dbee9bb | 2015-12-15 11:01:27 -0800 | [diff] [blame] | 203 | outCanvas->drawPath(renderPath, mPaint); |
| 204 | } |
| 205 | |
| 206 | // Draw path's stroke, if stroke color or gradient is valid |
| 207 | bool needsStroke = false; |
| 208 | if (mStrokeGradient != nullptr) { |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 209 | mPaint.setColor(applyAlpha(SK_ColorBLACK, mProperties.strokeAlpha)); |
Teng-Hui Zhu | dbee9bb | 2015-12-15 11:01:27 -0800 | [diff] [blame] | 210 | SkShader* newShader = mStrokeGradient->newWithLocalMatrix(matrix); |
| 211 | mPaint.setShader(newShader); |
| 212 | needsStroke = true; |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 213 | } else if (mProperties.strokeColor != SK_ColorTRANSPARENT) { |
| 214 | mPaint.setColor(applyAlpha(mProperties.strokeColor, mProperties.strokeAlpha)); |
Teng-Hui Zhu | dbee9bb | 2015-12-15 11:01:27 -0800 | [diff] [blame] | 215 | needsStroke = true; |
| 216 | } |
| 217 | if (needsStroke) { |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 218 | mPaint.setStyle(SkPaint::Style::kStroke_Style); |
| 219 | mPaint.setAntiAlias(true); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 220 | mPaint.setStrokeJoin(SkPaint::Join(mProperties.strokeLineJoin)); |
| 221 | mPaint.setStrokeCap(SkPaint::Cap(mProperties.strokeLineCap)); |
| 222 | mPaint.setStrokeMiter(mProperties.strokeMiterLimit); |
| 223 | mPaint.setStrokeWidth(mProperties.strokeWidth * strokeScale); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 224 | outCanvas->drawPath(renderPath, mPaint); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Applies trimming to the specified path. |
| 230 | */ |
| 231 | void FullPath::applyTrim() { |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 232 | if (mProperties.trimPathStart == 0.0f && mProperties.trimPathEnd == 1.0f) { |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 233 | // No trimming necessary. |
| 234 | return; |
| 235 | } |
| 236 | SkPathMeasure measure(mSkPath, false); |
| 237 | float len = SkScalarToFloat(measure.getLength()); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 238 | float start = len * fmod((mProperties.trimPathStart + mProperties.trimPathOffset), 1.0f); |
| 239 | float end = len * fmod((mProperties.trimPathEnd + mProperties.trimPathOffset), 1.0f); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 240 | |
| 241 | mTrimmedSkPath.reset(); |
| 242 | if (start > end) { |
| 243 | measure.getSegment(start, len, &mTrimmedSkPath, true); |
| 244 | measure.getSegment(0, end, &mTrimmedSkPath, true); |
| 245 | } else { |
| 246 | measure.getSegment(start, end, &mTrimmedSkPath, true); |
| 247 | } |
| 248 | mTrimDirty = false; |
| 249 | } |
| 250 | |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 251 | REQUIRE_COMPATIBLE_LAYOUT(FullPath::Properties); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 252 | |
| 253 | static_assert(sizeof(float) == sizeof(int32_t), "float is not the same size as int32_t"); |
| 254 | static_assert(sizeof(SkColor) == sizeof(int32_t), "SkColor is not the same size as int32_t"); |
| 255 | |
| 256 | bool FullPath::getProperties(int8_t* outProperties, int length) { |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 257 | int propertyDataSize = sizeof(Properties); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 258 | if (length != propertyDataSize) { |
| 259 | LOG_ALWAYS_FATAL("Properties needs exactly %d bytes, a byte array of size %d is provided", |
| 260 | propertyDataSize, length); |
| 261 | return false; |
| 262 | } |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 263 | Properties* out = reinterpret_cast<Properties*>(outProperties); |
| 264 | *out = mProperties; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 265 | return true; |
| 266 | } |
| 267 | |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 268 | void FullPath::setColorPropertyValue(int propertyId, int32_t value) { |
| 269 | Property currentProperty = static_cast<Property>(propertyId); |
| 270 | if (currentProperty == Property::StrokeColor) { |
| 271 | mProperties.strokeColor = value; |
| 272 | } else if (currentProperty == Property::FillColor) { |
| 273 | mProperties.fillColor = value; |
| 274 | } else { |
| 275 | LOG_ALWAYS_FATAL("Error setting color property on FullPath: No valid property with id: %d", |
| 276 | propertyId); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | void FullPath::setPropertyValue(int propertyId, float value) { |
| 281 | Property property = static_cast<Property>(propertyId); |
| 282 | switch (property) { |
| 283 | case Property::StrokeWidth: |
| 284 | setStrokeWidth(value); |
| 285 | break; |
| 286 | case Property::StrokeAlpha: |
| 287 | setStrokeAlpha(value); |
| 288 | break; |
| 289 | case Property::FillAlpha: |
| 290 | setFillAlpha(value); |
| 291 | break; |
| 292 | case Property::TrimPathStart: |
| 293 | setTrimPathStart(value); |
| 294 | break; |
| 295 | case Property::TrimPathEnd: |
| 296 | setTrimPathEnd(value); |
| 297 | break; |
| 298 | case Property::TrimPathOffset: |
| 299 | setTrimPathOffset(value); |
| 300 | break; |
| 301 | default: |
| 302 | LOG_ALWAYS_FATAL("Invalid property id: %d for animation", propertyId); |
| 303 | break; |
| 304 | } |
| 305 | } |
| 306 | |
Teng-Hui Zhu | 46591f4 | 2016-03-15 14:32:16 -0700 | [diff] [blame^] | 307 | void ClipPath::drawPath(SkCanvas* outCanvas, SkPath& renderPath, |
Teng-Hui Zhu | dbee9bb | 2015-12-15 11:01:27 -0800 | [diff] [blame] | 308 | float strokeScale, const SkMatrix& matrix){ |
Doris Liu | c2de46f | 2016-01-21 12:55:54 -0800 | [diff] [blame] | 309 | outCanvas->clipPath(renderPath, SkRegion::kIntersect_Op); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | Group::Group(const Group& group) : Node(group) { |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 313 | mProperties = group.mProperties; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 314 | } |
| 315 | |
Doris Liu | c2de46f | 2016-01-21 12:55:54 -0800 | [diff] [blame] | 316 | void Group::draw(SkCanvas* outCanvas, const SkMatrix& currentMatrix, float scaleX, |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 317 | float scaleY) { |
| 318 | // TODO: Try apply the matrix to the canvas instead of passing it down the tree |
| 319 | |
| 320 | // Calculate current group's matrix by preConcat the parent's and |
| 321 | // and the current one on the top of the stack. |
| 322 | // Basically the Mfinal = Mviewport * M0 * M1 * M2; |
| 323 | // Mi the local matrix at level i of the group tree. |
| 324 | SkMatrix stackedMatrix; |
| 325 | getLocalMatrix(&stackedMatrix); |
| 326 | stackedMatrix.postConcat(currentMatrix); |
| 327 | |
| 328 | // Save the current clip information, which is local to this group. |
Doris Liu | c2de46f | 2016-01-21 12:55:54 -0800 | [diff] [blame] | 329 | outCanvas->save(); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 330 | // Draw the group tree in the same order as the XML file. |
Doris Liu | ef062eb | 2016-02-04 16:16:27 -0800 | [diff] [blame] | 331 | for (auto& child : mChildren) { |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 332 | child->draw(outCanvas, stackedMatrix, scaleX, scaleY); |
| 333 | } |
| 334 | // Restore the previous clip information. |
| 335 | outCanvas->restore(); |
| 336 | } |
| 337 | |
| 338 | void Group::dump() { |
| 339 | ALOGD("Group %s has %zu children: ", mName.c_str(), mChildren.size()); |
| 340 | for (size_t i = 0; i < mChildren.size(); i++) { |
| 341 | mChildren[i]->dump(); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | void Group::updateLocalMatrix(float rotate, float pivotX, float pivotY, |
| 346 | float scaleX, float scaleY, float translateX, float translateY) { |
| 347 | setRotation(rotate); |
| 348 | setPivotX(pivotX); |
| 349 | setPivotY(pivotY); |
| 350 | setScaleX(scaleX); |
| 351 | setScaleY(scaleY); |
| 352 | setTranslateX(translateX); |
| 353 | setTranslateY(translateY); |
| 354 | } |
| 355 | |
| 356 | void Group::getLocalMatrix(SkMatrix* outMatrix) { |
| 357 | outMatrix->reset(); |
| 358 | // TODO: use rotate(mRotate, mPivotX, mPivotY) and scale with pivot point, instead of |
| 359 | // translating to pivot for rotating and scaling, then translating back. |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 360 | outMatrix->postTranslate(-mProperties.pivotX, -mProperties.pivotY); |
| 361 | outMatrix->postScale(mProperties.scaleX, mProperties.scaleY); |
| 362 | outMatrix->postRotate(mProperties.rotate, 0, 0); |
| 363 | outMatrix->postTranslate(mProperties.translateX + mProperties.pivotX, |
| 364 | mProperties.translateY + mProperties.pivotY); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | void Group::addChild(Node* child) { |
Doris Liu | ef062eb | 2016-02-04 16:16:27 -0800 | [diff] [blame] | 368 | mChildren.emplace_back(child); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | bool Group::getProperties(float* outProperties, int length) { |
| 372 | int propertyCount = static_cast<int>(Property::Count); |
| 373 | if (length != propertyCount) { |
| 374 | LOG_ALWAYS_FATAL("Properties needs exactly %d bytes, a byte array of size %d is provided", |
| 375 | propertyCount, length); |
| 376 | return false; |
| 377 | } |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 378 | Properties* out = reinterpret_cast<Properties*>(outProperties); |
| 379 | *out = mProperties; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 380 | return true; |
| 381 | } |
| 382 | |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 383 | // TODO: Consider animating the properties as float pointers |
| 384 | float Group::getPropertyValue(int propertyId) const { |
| 385 | Property currentProperty = static_cast<Property>(propertyId); |
| 386 | switch (currentProperty) { |
| 387 | case Property::Rotate: |
| 388 | return mProperties.rotate; |
| 389 | case Property::PivotX: |
| 390 | return mProperties.pivotX; |
| 391 | case Property::PivotY: |
| 392 | return mProperties.pivotY; |
| 393 | case Property::ScaleX: |
| 394 | return mProperties.scaleX; |
| 395 | case Property::ScaleY: |
| 396 | return mProperties.scaleY; |
| 397 | case Property::TranslateX: |
| 398 | return mProperties.translateX; |
| 399 | case Property::TranslateY: |
| 400 | return mProperties.translateY; |
| 401 | default: |
| 402 | LOG_ALWAYS_FATAL("Invalid property index: %d", propertyId); |
| 403 | return 0; |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | void Group::setPropertyValue(int propertyId, float value) { |
| 408 | Property currentProperty = static_cast<Property>(propertyId); |
| 409 | switch (currentProperty) { |
| 410 | case Property::Rotate: |
| 411 | mProperties.rotate = value; |
| 412 | break; |
| 413 | case Property::PivotX: |
| 414 | mProperties.pivotX = value; |
| 415 | break; |
| 416 | case Property::PivotY: |
| 417 | mProperties.pivotY = value; |
| 418 | break; |
| 419 | case Property::ScaleX: |
| 420 | mProperties.scaleX = value; |
| 421 | break; |
| 422 | case Property::ScaleY: |
| 423 | mProperties.scaleY = value; |
| 424 | break; |
| 425 | case Property::TranslateX: |
| 426 | mProperties.translateX = value; |
| 427 | break; |
| 428 | case Property::TranslateY: |
| 429 | mProperties.translateY = value; |
| 430 | break; |
| 431 | default: |
| 432 | LOG_ALWAYS_FATAL("Invalid property index: %d", propertyId); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | bool Group::isValidProperty(int propertyId) { |
| 437 | return propertyId >= 0 && propertyId < static_cast<int>(Property::Count); |
| 438 | } |
| 439 | |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 440 | void Tree::draw(Canvas* outCanvas, SkColorFilter* colorFilter, |
| 441 | const SkRect& bounds, bool needsMirroring, bool canReuseCache) { |
| 442 | // The imageView can scale the canvas in different ways, in order to |
| 443 | // avoid blurry scaling, we have to draw into a bitmap with exact pixel |
| 444 | // size first. This bitmap size is determined by the bounds and the |
| 445 | // canvas scale. |
| 446 | outCanvas->getMatrix(&mCanvasMatrix); |
| 447 | mBounds = bounds; |
Doris Liu | a0e6157 | 2015-12-29 14:57:49 -0800 | [diff] [blame] | 448 | float canvasScaleX = 1.0f; |
| 449 | float canvasScaleY = 1.0f; |
| 450 | if (mCanvasMatrix.getSkewX() == 0 && mCanvasMatrix.getSkewY() == 0) { |
| 451 | // Only use the scale value when there's no skew or rotation in the canvas matrix. |
Doris Liu | e410a35 | 2016-01-13 17:23:33 -0800 | [diff] [blame] | 452 | // TODO: Add a cts test for drawing VD on a canvas with negative scaling factors. |
| 453 | canvasScaleX = fabs(mCanvasMatrix.getScaleX()); |
| 454 | canvasScaleY = fabs(mCanvasMatrix.getScaleY()); |
Doris Liu | a0e6157 | 2015-12-29 14:57:49 -0800 | [diff] [blame] | 455 | } |
| 456 | int scaledWidth = (int) (mBounds.width() * canvasScaleX); |
| 457 | int scaledHeight = (int) (mBounds.height() * canvasScaleY); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 458 | scaledWidth = std::min(Tree::MAX_CACHED_BITMAP_SIZE, scaledWidth); |
| 459 | scaledHeight = std::min(Tree::MAX_CACHED_BITMAP_SIZE, scaledHeight); |
| 460 | |
| 461 | if (scaledWidth <= 0 || scaledHeight <= 0) { |
| 462 | return; |
| 463 | } |
| 464 | |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 465 | mPaint.setColorFilter(colorFilter); |
| 466 | |
Florin Malita | 777bf85 | 2016-02-03 10:48:55 -0500 | [diff] [blame] | 467 | int saveCount = outCanvas->save(SaveFlags::MatrixClip); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 468 | outCanvas->translate(mBounds.fLeft, mBounds.fTop); |
| 469 | |
| 470 | // Handle RTL mirroring. |
| 471 | if (needsMirroring) { |
| 472 | outCanvas->translate(mBounds.width(), 0); |
| 473 | outCanvas->scale(-1.0f, 1.0f); |
| 474 | } |
| 475 | |
| 476 | // At this point, canvas has been translated to the right position. |
| 477 | // And we use this bound for the destination rect for the drawBitmap, so |
| 478 | // we offset to (0, 0); |
| 479 | mBounds.offsetTo(0, 0); |
Doris Liu | 5a11e8d | 2016-02-04 20:04:10 +0000 | [diff] [blame] | 480 | createCachedBitmapIfNeeded(scaledWidth, scaledHeight); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 481 | |
| 482 | outCanvas->drawVectorDrawable(this); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 483 | |
| 484 | outCanvas->restoreToCount(saveCount); |
| 485 | } |
| 486 | |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 487 | SkPaint* Tree::getPaint() { |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 488 | SkPaint* paint; |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 489 | if (mRootAlpha == 1.0f && mPaint.getColorFilter() == NULL) { |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 490 | paint = NULL; |
| 491 | } else { |
| 492 | mPaint.setFilterQuality(kLow_SkFilterQuality); |
| 493 | mPaint.setAlpha(mRootAlpha * 255); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 494 | paint = &mPaint; |
| 495 | } |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 496 | return paint; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 497 | } |
| 498 | |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 499 | const SkBitmap& Tree::getBitmapUpdateIfDirty() { |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 500 | mCachedBitmap.eraseColor(SK_ColorTRANSPARENT); |
Doris Liu | c2de46f | 2016-01-21 12:55:54 -0800 | [diff] [blame] | 501 | SkCanvas outCanvas(mCachedBitmap); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 502 | float scaleX = (float) mCachedBitmap.width() / mViewportWidth; |
| 503 | float scaleY = (float) mCachedBitmap.height() / mViewportHeight; |
Doris Liu | c2de46f | 2016-01-21 12:55:54 -0800 | [diff] [blame] | 504 | mRootNode->draw(&outCanvas, SkMatrix::I(), scaleX, scaleY); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 505 | mCacheDirty = false; |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 506 | return mCachedBitmap; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | void Tree::createCachedBitmapIfNeeded(int width, int height) { |
| 510 | if (!canReuseBitmap(width, height)) { |
| 511 | SkImageInfo info = SkImageInfo::Make(width, height, |
| 512 | kN32_SkColorType, kPremul_SkAlphaType); |
| 513 | mCachedBitmap.setInfo(info); |
| 514 | // TODO: Count the bitmap cache against app's java heap |
| 515 | mCachedBitmap.allocPixels(info); |
| 516 | mCacheDirty = true; |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | bool Tree::canReuseBitmap(int width, int height) { |
| 521 | return width == mCachedBitmap.width() && height == mCachedBitmap.height(); |
| 522 | } |
| 523 | |
| 524 | }; // namespace VectorDrawable |
| 525 | |
| 526 | }; // namespace uirenderer |
| 527 | }; // namespace android |