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" |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 20 | #include "SkColorFilter.h" |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 21 | #include "SkImageInfo.h" |
Teng-Hui Zhu | dbee9bb | 2015-12-15 11:01:27 -0800 | [diff] [blame] | 22 | #include "SkShader.h" |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 23 | #include <utils/Log.h> |
| 24 | #include "utils/Macros.h" |
| 25 | #include "utils/VectorDrawableUtils.h" |
| 26 | |
| 27 | #include <math.h> |
| 28 | #include <string.h> |
| 29 | |
| 30 | namespace android { |
| 31 | namespace uirenderer { |
| 32 | namespace VectorDrawable { |
| 33 | |
| 34 | const int Tree::MAX_CACHED_BITMAP_SIZE = 2048; |
| 35 | |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 36 | void Path::dump() { |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 37 | ALOGD("Path: %s has %zu points", mName.c_str(), mProperties.getData().points.size()); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 40 | // Called from UI thread during the initial setup/theme change. |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 41 | Path::Path(const char* pathStr, size_t strLength) { |
| 42 | PathParser::ParseResult result; |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 43 | Data data; |
Doris Liu | b35da39 | 2016-04-12 11:06:23 -0700 | [diff] [blame] | 44 | PathParser::getPathDataFromAsciiString(&data, &result, pathStr, strLength); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 45 | mStagingProperties.setData(data); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | Path::Path(const Path& path) : Node(path) { |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 49 | mStagingProperties.syncProperties(path.mStagingProperties); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame^] | 52 | const 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 Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 64 | } |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void Path::syncProperties() { |
| 68 | if (mStagingPropertiesDirty) { |
| 69 | mProperties.syncProperties(mStagingProperties); |
| 70 | } else { |
| 71 | mStagingProperties.syncProperties(mProperties); |
| 72 | } |
| 73 | mStagingPropertiesDirty = false; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | FullPath::FullPath(const FullPath& path) : Path(path) { |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 77 | mStagingProperties.syncProperties(path.mStagingProperties); |
| 78 | } |
| 79 | |
| 80 | static 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 Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame^] | 106 | const SkPath& FullPath::getUpdatedPath(bool useStagingData, SkPath* tempStagingPath) { |
| 107 | if (!useStagingData && !mSkPathDirty && !mProperties.mTrimDirty) { |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 108 | return mTrimmedSkPath; |
| 109 | } |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame^] | 110 | 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 Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 117 | } else { |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame^] | 118 | 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 Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 126 | } |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame^] | 127 | 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 Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 137 | void 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 Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 145 | inline SkColor applyAlpha(SkColor color, float alpha) { |
| 146 | int alphaBytes = SkColorGetA(color); |
| 147 | return SkColorSetA(color, alphaBytes * alpha); |
| 148 | } |
| 149 | |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame^] | 150 | void FullPath::draw(SkCanvas* outCanvas, bool useStagingData) { |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 151 | const FullPathProperties& properties = useStagingData ? mStagingProperties : mProperties; |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame^] | 152 | SkPath tempStagingPath; |
| 153 | const SkPath& renderPath = getUpdatedPath(useStagingData, &tempStagingPath); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 154 | |
Teng-Hui Zhu | dbee9bb | 2015-12-15 11:01:27 -0800 | [diff] [blame] | 155 | // Draw path's fill, if fill color or gradient is valid |
| 156 | bool needsFill = false; |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 157 | SkPaint paint; |
| 158 | if (properties.getFillGradient() != nullptr) { |
| 159 | paint.setColor(applyAlpha(SK_ColorBLACK, properties.getFillAlpha())); |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame^] | 160 | paint.setShader(sk_sp<SkShader>(SkSafeRef(properties.getFillGradient()))); |
Teng-Hui Zhu | dbee9bb | 2015-12-15 11:01:27 -0800 | [diff] [blame] | 161 | needsFill = true; |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 162 | } else if (properties.getFillColor() != SK_ColorTRANSPARENT) { |
| 163 | paint.setColor(applyAlpha(properties.getFillColor(), properties.getFillAlpha())); |
Teng-Hui Zhu | dbee9bb | 2015-12-15 11:01:27 -0800 | [diff] [blame] | 164 | needsFill = true; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 165 | } |
Teng-Hui Zhu | dbee9bb | 2015-12-15 11:01:27 -0800 | [diff] [blame] | 166 | |
| 167 | if (needsFill) { |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 168 | paint.setStyle(SkPaint::Style::kFill_Style); |
| 169 | paint.setAntiAlias(true); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 170 | outCanvas->drawPath(renderPath, paint); |
Teng-Hui Zhu | dbee9bb | 2015-12-15 11:01:27 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 173 | // Draw path's stroke, if stroke color or Gradient is valid |
Teng-Hui Zhu | dbee9bb | 2015-12-15 11:01:27 -0800 | [diff] [blame] | 174 | bool needsStroke = false; |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 175 | if (properties.getStrokeGradient() != nullptr) { |
| 176 | paint.setColor(applyAlpha(SK_ColorBLACK, properties.getStrokeAlpha())); |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame^] | 177 | paint.setShader(sk_sp<SkShader>(SkSafeRef(properties.getStrokeGradient()))); |
Teng-Hui Zhu | dbee9bb | 2015-12-15 11:01:27 -0800 | [diff] [blame] | 178 | needsStroke = true; |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 179 | } else if (properties.getStrokeColor() != SK_ColorTRANSPARENT) { |
| 180 | paint.setColor(applyAlpha(properties.getStrokeColor(), properties.getStrokeAlpha())); |
Teng-Hui Zhu | dbee9bb | 2015-12-15 11:01:27 -0800 | [diff] [blame] | 181 | needsStroke = true; |
| 182 | } |
| 183 | if (needsStroke) { |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 184 | 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 Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame^] | 189 | paint.setStrokeWidth(properties.getStrokeWidth()); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 190 | outCanvas->drawPath(renderPath, paint); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 194 | void FullPath::syncProperties() { |
| 195 | Path::syncProperties(); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 196 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 197 | if (mStagingPropertiesDirty) { |
| 198 | mProperties.syncProperties(mStagingProperties); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 199 | } else { |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 200 | // Update staging property with property values from animation. |
| 201 | mStagingProperties.syncProperties(mProperties); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 202 | } |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 203 | mStagingPropertiesDirty = false; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 206 | REQUIRE_COMPATIBLE_LAYOUT(FullPath::FullPathProperties::PrimitiveFields); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 207 | |
| 208 | static_assert(sizeof(float) == sizeof(int32_t), "float is not the same size as int32_t"); |
| 209 | static_assert(sizeof(SkColor) == sizeof(int32_t), "SkColor is not the same size as int32_t"); |
| 210 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 211 | bool FullPath::FullPathProperties::copyProperties(int8_t* outProperties, int length) const { |
| 212 | int propertyDataSize = sizeof(FullPathProperties::PrimitiveFields); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 213 | 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 Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 218 | |
| 219 | PrimitiveFields* out = reinterpret_cast<PrimitiveFields*>(outProperties); |
| 220 | *out = mPrimitiveFields; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 221 | return true; |
| 222 | } |
| 223 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 224 | void FullPath::FullPathProperties::setColorPropertyValue(int propertyId, int32_t value) { |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 225 | Property currentProperty = static_cast<Property>(propertyId); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 226 | if (currentProperty == Property::strokeColor) { |
| 227 | setStrokeColor(value); |
| 228 | } else if (currentProperty == Property::fillColor) { |
| 229 | setFillColor(value); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 230 | } else { |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 231 | LOG_ALWAYS_FATAL("Error setting color property on FullPath: No valid property" |
| 232 | " with id: %d", propertyId); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 236 | void FullPath::FullPathProperties::setPropertyValue(int propertyId, float value) { |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 237 | Property property = static_cast<Property>(propertyId); |
| 238 | switch (property) { |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 239 | case Property::strokeWidth: |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 240 | setStrokeWidth(value); |
| 241 | break; |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 242 | case Property::strokeAlpha: |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 243 | setStrokeAlpha(value); |
| 244 | break; |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 245 | case Property::fillAlpha: |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 246 | setFillAlpha(value); |
| 247 | break; |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 248 | case Property::trimPathStart: |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 249 | setTrimPathStart(value); |
| 250 | break; |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 251 | case Property::trimPathEnd: |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 252 | setTrimPathEnd(value); |
| 253 | break; |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 254 | case Property::trimPathOffset: |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 255 | setTrimPathOffset(value); |
| 256 | break; |
| 257 | default: |
| 258 | LOG_ALWAYS_FATAL("Invalid property id: %d for animation", propertyId); |
| 259 | break; |
| 260 | } |
| 261 | } |
| 262 | |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame^] | 263 | void ClipPath::draw(SkCanvas* outCanvas, bool useStagingData) { |
| 264 | SkPath tempStagingPath; |
| 265 | outCanvas->clipPath(getUpdatedPath(useStagingData, &tempStagingPath)); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | Group::Group(const Group& group) : Node(group) { |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 269 | mStagingProperties.syncProperties(group.mStagingProperties); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 270 | } |
| 271 | |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame^] | 272 | void 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 Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 276 | SkMatrix stackedMatrix; |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 277 | const GroupProperties& prop = useStagingData ? mStagingProperties : mProperties; |
| 278 | getLocalMatrix(&stackedMatrix, prop); |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame^] | 279 | outCanvas->concat(stackedMatrix); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 280 | // Draw the group tree in the same order as the XML file. |
Doris Liu | ef062eb | 2016-02-04 16:16:27 -0800 | [diff] [blame] | 281 | for (auto& child : mChildren) { |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame^] | 282 | child->draw(outCanvas, useStagingData); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 283 | } |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame^] | 284 | // Restore the previous clip and matrix information. |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | void Group::dump() { |
| 288 | ALOGD("Group %s has %zu children: ", mName.c_str(), mChildren.size()); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 289 | ALOGD("Group translateX, Y : %f, %f, scaleX, Y: %f, %f", mProperties.getTranslateX(), |
| 290 | mProperties.getTranslateY(), mProperties.getScaleX(), mProperties.getScaleY()); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 291 | for (size_t i = 0; i < mChildren.size(); i++) { |
| 292 | mChildren[i]->dump(); |
| 293 | } |
| 294 | } |
| 295 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 296 | void 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 Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 307 | } |
| 308 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 309 | void Group::getLocalMatrix(SkMatrix* outMatrix, const GroupProperties& properties) { |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 310 | 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 Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 313 | 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 Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | void Group::addChild(Node* child) { |
Doris Liu | ef062eb | 2016-02-04 16:16:27 -0800 | [diff] [blame] | 321 | mChildren.emplace_back(child); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 322 | if (mPropertyChangedListener != nullptr) { |
| 323 | child->setPropertyChangedListener(mPropertyChangedListener); |
| 324 | } |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 325 | } |
| 326 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 327 | bool Group::GroupProperties::copyProperties(float* outProperties, int length) const { |
| 328 | int propertyCount = static_cast<int>(Property::count); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 329 | 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 Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 334 | |
| 335 | PrimitiveFields* out = reinterpret_cast<PrimitiveFields*>(outProperties); |
| 336 | *out = mPrimitiveFields; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 337 | return true; |
| 338 | } |
| 339 | |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 340 | // TODO: Consider animating the properties as float pointers |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 341 | // Called on render thread |
| 342 | float Group::GroupProperties::getPropertyValue(int propertyId) const { |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 343 | Property currentProperty = static_cast<Property>(propertyId); |
| 344 | switch (currentProperty) { |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 345 | 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 Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 359 | default: |
| 360 | LOG_ALWAYS_FATAL("Invalid property index: %d", propertyId); |
| 361 | return 0; |
| 362 | } |
| 363 | } |
| 364 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 365 | // Called on render thread |
| 366 | void Group::GroupProperties::setPropertyValue(int propertyId, float value) { |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 367 | Property currentProperty = static_cast<Property>(propertyId); |
| 368 | switch (currentProperty) { |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 369 | case Property::rotate: |
| 370 | setRotation(value); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 371 | break; |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 372 | case Property::pivotX: |
| 373 | setPivotX(value); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 374 | break; |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 375 | case Property::pivotY: |
| 376 | setPivotY(value); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 377 | break; |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 378 | case Property::scaleX: |
| 379 | setScaleX(value); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 380 | break; |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 381 | case Property::scaleY: |
| 382 | setScaleY(value); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 383 | break; |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 384 | case Property::translateX: |
| 385 | setTranslateX(value); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 386 | break; |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 387 | case Property::translateY: |
| 388 | setTranslateY(value); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 389 | break; |
| 390 | default: |
| 391 | LOG_ALWAYS_FATAL("Invalid property index: %d", propertyId); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | bool Group::isValidProperty(int propertyId) { |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 396 | return GroupProperties::isValidProperty(propertyId); |
| 397 | } |
| 398 | |
| 399 | bool Group::GroupProperties::isValidProperty(int propertyId) { |
| 400 | return propertyId >= 0 && propertyId < static_cast<int>(Property::count); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Doris Liu | f8d131c | 2016-04-29 18:41:29 -0700 | [diff] [blame] | 403 | int Tree::draw(Canvas* outCanvas, SkColorFilter* colorFilter, |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 404 | 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 Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 409 | SkMatrix canvasMatrix; |
| 410 | outCanvas->getMatrix(&canvasMatrix); |
Doris Liu | a0e6157 | 2015-12-29 14:57:49 -0800 | [diff] [blame] | 411 | float canvasScaleX = 1.0f; |
| 412 | float canvasScaleY = 1.0f; |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 413 | if (canvasMatrix.getSkewX() == 0 && canvasMatrix.getSkewY() == 0) { |
Doris Liu | a0e6157 | 2015-12-29 14:57:49 -0800 | [diff] [blame] | 414 | // 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] | 415 | // TODO: Add a cts test for drawing VD on a canvas with negative scaling factors. |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 416 | canvasScaleX = fabs(canvasMatrix.getScaleX()); |
| 417 | canvasScaleY = fabs(canvasMatrix.getScaleY()); |
Doris Liu | a0e6157 | 2015-12-29 14:57:49 -0800 | [diff] [blame] | 418 | } |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 419 | int scaledWidth = (int) (bounds.width() * canvasScaleX); |
| 420 | int scaledHeight = (int) (bounds.height() * canvasScaleY); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 421 | 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 Liu | f8d131c | 2016-04-29 18:41:29 -0700 | [diff] [blame] | 425 | return 0; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 426 | } |
| 427 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 428 | mStagingProperties.setScaledSize(scaledWidth, scaledHeight); |
Florin Malita | 777bf85 | 2016-02-03 10:48:55 -0500 | [diff] [blame] | 429 | int saveCount = outCanvas->save(SaveFlags::MatrixClip); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 430 | outCanvas->translate(bounds.fLeft, bounds.fTop); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 431 | |
| 432 | // Handle RTL mirroring. |
| 433 | if (needsMirroring) { |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 434 | outCanvas->translate(bounds.width(), 0); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 435 | outCanvas->scale(-1.0f, 1.0f); |
| 436 | } |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 437 | mStagingProperties.setColorFilter(colorFilter); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 438 | |
| 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 Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 442 | SkRect tmpBounds = bounds; |
| 443 | tmpBounds.offsetTo(0, 0); |
| 444 | mStagingProperties.setBounds(tmpBounds); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 445 | outCanvas->drawVectorDrawable(this); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 446 | outCanvas->restoreToCount(saveCount); |
Doris Liu | f8d131c | 2016-04-29 18:41:29 -0700 | [diff] [blame] | 447 | return scaledWidth * scaledHeight; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 448 | } |
| 449 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 450 | void Tree::drawStaging(Canvas* outCanvas) { |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 451 | bool redrawNeeded = allocateBitmapIfNeeded(mStagingCache, |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 452 | mStagingProperties.getScaledWidth(), mStagingProperties.getScaledHeight()); |
| 453 | // draw bitmap cache |
| 454 | if (redrawNeeded || mStagingCache.dirty) { |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 455 | updateBitmapCache(*mStagingCache.bitmap, true); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 456 | mStagingCache.dirty = false; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 457 | } |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 458 | |
| 459 | SkPaint tmpPaint; |
| 460 | SkPaint* paint = updatePaint(&tmpPaint, &mStagingProperties); |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 461 | outCanvas->drawBitmap(*mStagingCache.bitmap, 0, 0, |
| 462 | mStagingCache.bitmap->width(), mStagingCache.bitmap->height(), |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 463 | mStagingProperties.getBounds().left(), mStagingProperties.getBounds().top(), |
| 464 | mStagingProperties.getBounds().right(), mStagingProperties.getBounds().bottom(), paint); |
| 465 | } |
| 466 | |
| 467 | SkPaint* 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. |
| 473 | SkPaint* Tree::updatePaint(SkPaint* outPaint, TreeProperties* prop) { |
| 474 | if (prop->getRootAlpha() == 1.0f && prop->getColorFilter() == nullptr) { |
| 475 | return nullptr; |
| 476 | } else { |
Mike Reed | 260ab72 | 2016-10-07 15:59:20 -0400 | [diff] [blame] | 477 | outPaint->setColorFilter(sk_ref_sp(prop->getColorFilter())); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 478 | outPaint->setFilterQuality(kLow_SkFilterQuality); |
| 479 | outPaint->setAlpha(prop->getRootAlpha() * 255); |
| 480 | return outPaint; |
| 481 | } |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 482 | } |
| 483 | |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 484 | Bitmap& Tree::getBitmapUpdateIfDirty() { |
| 485 | bool redrawNeeded = allocateBitmapIfNeeded(mCache, mProperties.getScaledWidth(), |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 486 | mProperties.getScaledHeight()); |
| 487 | if (redrawNeeded || mCache.dirty) { |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 488 | updateBitmapCache(*mCache.bitmap, false); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 489 | mCache.dirty = false; |
| 490 | } |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 491 | return *mCache.bitmap; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 492 | } |
| 493 | |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 494 | void Tree::updateBitmapCache(Bitmap& bitmap, bool useStagingData) { |
| 495 | SkBitmap outCache; |
| 496 | bitmap.getSkBitmap(&outCache); |
| 497 | outCache.eraseColor(SK_ColorTRANSPARENT); |
| 498 | SkCanvas outCanvas(outCache); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 499 | float viewportWidth = useStagingData ? |
| 500 | mStagingProperties.getViewportWidth() : mProperties.getViewportWidth(); |
| 501 | float viewportHeight = useStagingData ? |
| 502 | mStagingProperties.getViewportHeight() : mProperties.getViewportHeight(); |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 503 | float scaleX = outCache.width() / viewportWidth; |
| 504 | float scaleY = outCache.height() / viewportHeight; |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame^] | 505 | outCanvas.scale(scaleX, scaleY); |
| 506 | mRootNode->draw(&outCanvas, useStagingData); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 507 | } |
| 508 | |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 509 | bool Tree::allocateBitmapIfNeeded(Cache& cache, int width, int height) { |
| 510 | if (!canReuseBitmap(cache.bitmap.get(), width, height)) { |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 511 | #ifndef ANDROID_ENABLE_LINEAR_BLENDING |
| 512 | sk_sp<SkColorSpace> colorSpace = nullptr; |
| 513 | #else |
Matt Sarett | 89ddb1f | 2017-02-10 13:31:56 -0500 | [diff] [blame] | 514 | sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeSRGB(); |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 515 | #endif |
| 516 | SkImageInfo info = SkImageInfo::MakeN32(width, height, kPremul_SkAlphaType, colorSpace); |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 517 | cache.bitmap = Bitmap::allocateHeapBitmap(info); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 518 | return true; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 519 | } |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 520 | return false; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 521 | } |
| 522 | |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 523 | bool Tree::canReuseBitmap(Bitmap* bitmap, int width, int height) { |
Teng-Hui Zhu | 037fc18 | 2016-11-16 10:29:39 -0800 | [diff] [blame] | 524 | return bitmap && width <= bitmap->width() && height <= bitmap->height(); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | void Tree::onPropertyChanged(TreeProperties* prop) { |
| 528 | if (prop == &mStagingProperties) { |
| 529 | mStagingCache.dirty = true; |
| 530 | } else { |
| 531 | mCache.dirty = true; |
| 532 | } |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | }; // namespace VectorDrawable |
| 536 | |
| 537 | }; // namespace uirenderer |
| 538 | }; // namespace android |