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 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 19 | #include <utils/Log.h> |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 20 | #include "PathParser.h" |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 21 | #include "SkColorFilter.h" |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 22 | #include "SkImageInfo.h" |
Teng-Hui Zhu | dbee9bb | 2015-12-15 11:01:27 -0800 | [diff] [blame] | 23 | #include "SkShader.h" |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 24 | #include "utils/Macros.h" |
ztenghui | cf0c41d | 2017-09-13 10:32:50 -0700 | [diff] [blame] | 25 | #include "utils/TraceUtils.h" |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 26 | #include "utils/VectorDrawableUtils.h" |
| 27 | |
| 28 | #include <math.h> |
| 29 | #include <string.h> |
| 30 | |
| 31 | namespace android { |
| 32 | namespace uirenderer { |
| 33 | namespace VectorDrawable { |
| 34 | |
| 35 | const int Tree::MAX_CACHED_BITMAP_SIZE = 2048; |
| 36 | |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 37 | void Path::dump() { |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 38 | 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] | 39 | } |
| 40 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 41 | // Called from UI thread during the initial setup/theme change. |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 42 | Path::Path(const char* pathStr, size_t strLength) { |
| 43 | PathParser::ParseResult result; |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 44 | Data data; |
Doris Liu | b35da39 | 2016-04-12 11:06:23 -0700 | [diff] [blame] | 45 | PathParser::getPathDataFromAsciiString(&data, &result, pathStr, strLength); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 46 | mStagingProperties.setData(data); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | Path::Path(const Path& path) : Node(path) { |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 50 | mStagingProperties.syncProperties(path.mStagingProperties); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame] | 53 | const SkPath& Path::getUpdatedPath(bool useStagingData, SkPath* tempStagingPath) { |
| 54 | if (useStagingData) { |
| 55 | tempStagingPath->reset(); |
| 56 | VectorDrawableUtils::verbsToPath(tempStagingPath, mStagingProperties.getData()); |
| 57 | return *tempStagingPath; |
| 58 | } else { |
| 59 | if (mSkPathDirty) { |
| 60 | mSkPath.reset(); |
| 61 | VectorDrawableUtils::verbsToPath(&mSkPath, mProperties.getData()); |
| 62 | mSkPathDirty = false; |
| 63 | } |
| 64 | return mSkPath; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 65 | } |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | void Path::syncProperties() { |
| 69 | if (mStagingPropertiesDirty) { |
| 70 | mProperties.syncProperties(mStagingProperties); |
| 71 | } else { |
| 72 | mStagingProperties.syncProperties(mProperties); |
| 73 | } |
| 74 | mStagingPropertiesDirty = false; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | FullPath::FullPath(const FullPath& path) : Path(path) { |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 78 | mStagingProperties.syncProperties(path.mStagingProperties); |
| 79 | } |
| 80 | |
| 81 | static void applyTrim(SkPath* outPath, const SkPath& inPath, float trimPathStart, float trimPathEnd, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 82 | float trimPathOffset) { |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 83 | if (trimPathStart == 0.0f && trimPathEnd == 1.0f) { |
| 84 | *outPath = inPath; |
| 85 | return; |
| 86 | } |
| 87 | outPath->reset(); |
| 88 | if (trimPathStart == trimPathEnd) { |
| 89 | // Trimmed path should be empty. |
| 90 | return; |
| 91 | } |
| 92 | SkPathMeasure measure(inPath, false); |
| 93 | float len = SkScalarToFloat(measure.getLength()); |
| 94 | float start = len * fmod((trimPathStart + trimPathOffset), 1.0f); |
| 95 | float end = len * fmod((trimPathEnd + trimPathOffset), 1.0f); |
| 96 | |
| 97 | if (start > end) { |
| 98 | measure.getSegment(start, len, outPath, true); |
| 99 | if (end > 0) { |
| 100 | measure.getSegment(0, end, outPath, true); |
| 101 | } |
| 102 | } else { |
| 103 | measure.getSegment(start, end, outPath, true); |
| 104 | } |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame] | 107 | const SkPath& FullPath::getUpdatedPath(bool useStagingData, SkPath* tempStagingPath) { |
| 108 | if (!useStagingData && !mSkPathDirty && !mProperties.mTrimDirty) { |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 109 | return mTrimmedSkPath; |
| 110 | } |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame] | 111 | Path::getUpdatedPath(useStagingData, tempStagingPath); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 112 | SkPath* outPath; |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame] | 113 | if (useStagingData) { |
| 114 | SkPath inPath = *tempStagingPath; |
| 115 | applyTrim(tempStagingPath, inPath, mStagingProperties.getTrimPathStart(), |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 116 | mStagingProperties.getTrimPathEnd(), mStagingProperties.getTrimPathOffset()); |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame] | 117 | outPath = tempStagingPath; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 118 | } else { |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame] | 119 | if (mProperties.getTrimPathStart() != 0.0f || mProperties.getTrimPathEnd() != 1.0f) { |
| 120 | mProperties.mTrimDirty = false; |
| 121 | applyTrim(&mTrimmedSkPath, mSkPath, mProperties.getTrimPathStart(), |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 122 | mProperties.getTrimPathEnd(), mProperties.getTrimPathOffset()); |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame] | 123 | outPath = &mTrimmedSkPath; |
| 124 | } else { |
| 125 | outPath = &mSkPath; |
| 126 | } |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 127 | } |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame] | 128 | const FullPathProperties& properties = useStagingData ? mStagingProperties : mProperties; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 129 | bool setFillPath = properties.getFillGradient() != nullptr || |
| 130 | properties.getFillColor() != SK_ColorTRANSPARENT; |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame] | 131 | if (setFillPath) { |
| 132 | SkPath::FillType ft = static_cast<SkPath::FillType>(properties.getFillType()); |
| 133 | outPath->setFillType(ft); |
| 134 | } |
| 135 | return *outPath; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 138 | void FullPath::dump() { |
| 139 | Path::dump(); |
| 140 | ALOGD("stroke width, color, alpha: %f, %d, %f, fill color, alpha: %d, %f", |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 141 | mProperties.getStrokeWidth(), mProperties.getStrokeColor(), mProperties.getStrokeAlpha(), |
| 142 | mProperties.getFillColor(), mProperties.getFillAlpha()); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 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); |
Doris Liu | 6b184d7 | 2017-12-04 16:31:07 -0800 | [diff] [blame] | 169 | paint.setAntiAlias(mAntiAlias); |
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); |
Doris Liu | 6b184d7 | 2017-12-04 16:31:07 -0800 | [diff] [blame] | 185 | paint.setAntiAlias(mAntiAlias); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 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", |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 215 | propertyDataSize, length); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 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 { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 231 | LOG_ALWAYS_FATAL( |
| 232 | "Error setting color property on FullPath: No valid property" |
| 233 | " with id: %d", |
| 234 | propertyId); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 238 | void FullPath::FullPathProperties::setPropertyValue(int propertyId, float value) { |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 239 | Property property = static_cast<Property>(propertyId); |
| 240 | switch (property) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 241 | case Property::strokeWidth: |
| 242 | setStrokeWidth(value); |
| 243 | break; |
| 244 | case Property::strokeAlpha: |
| 245 | setStrokeAlpha(value); |
| 246 | break; |
| 247 | case Property::fillAlpha: |
| 248 | setFillAlpha(value); |
| 249 | break; |
| 250 | case Property::trimPathStart: |
| 251 | setTrimPathStart(value); |
| 252 | break; |
| 253 | case Property::trimPathEnd: |
| 254 | setTrimPathEnd(value); |
| 255 | break; |
| 256 | case Property::trimPathOffset: |
| 257 | setTrimPathOffset(value); |
| 258 | break; |
| 259 | default: |
| 260 | LOG_ALWAYS_FATAL("Invalid property id: %d for animation", propertyId); |
| 261 | break; |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame] | 265 | void ClipPath::draw(SkCanvas* outCanvas, bool useStagingData) { |
| 266 | SkPath tempStagingPath; |
| 267 | outCanvas->clipPath(getUpdatedPath(useStagingData, &tempStagingPath)); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | Group::Group(const Group& group) : Node(group) { |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 271 | mStagingProperties.syncProperties(group.mStagingProperties); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 272 | } |
| 273 | |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame] | 274 | void Group::draw(SkCanvas* outCanvas, bool useStagingData) { |
| 275 | // Save the current clip and matrix information, which is local to this group. |
| 276 | SkAutoCanvasRestore saver(outCanvas, true); |
| 277 | // apply the current group's matrix to the canvas |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 278 | SkMatrix stackedMatrix; |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 279 | const GroupProperties& prop = useStagingData ? mStagingProperties : mProperties; |
| 280 | getLocalMatrix(&stackedMatrix, prop); |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame] | 281 | outCanvas->concat(stackedMatrix); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 282 | // Draw the group tree in the same order as the XML file. |
Doris Liu | ef062eb | 2016-02-04 16:16:27 -0800 | [diff] [blame] | 283 | for (auto& child : mChildren) { |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame] | 284 | child->draw(outCanvas, useStagingData); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 285 | } |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame] | 286 | // Restore the previous clip and matrix information. |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | void Group::dump() { |
| 290 | ALOGD("Group %s has %zu children: ", mName.c_str(), mChildren.size()); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 291 | ALOGD("Group translateX, Y : %f, %f, scaleX, Y: %f, %f", mProperties.getTranslateX(), |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 292 | mProperties.getTranslateY(), mProperties.getScaleX(), mProperties.getScaleY()); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 293 | for (size_t i = 0; i < mChildren.size(); i++) { |
| 294 | mChildren[i]->dump(); |
| 295 | } |
| 296 | } |
| 297 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 298 | void Group::syncProperties() { |
| 299 | // Copy over the dirty staging properties |
| 300 | if (mStagingPropertiesDirty) { |
| 301 | mProperties.syncProperties(mStagingProperties); |
| 302 | } else { |
| 303 | mStagingProperties.syncProperties(mProperties); |
| 304 | } |
| 305 | mStagingPropertiesDirty = false; |
| 306 | for (auto& child : mChildren) { |
| 307 | child->syncProperties(); |
| 308 | } |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 309 | } |
| 310 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 311 | void Group::getLocalMatrix(SkMatrix* outMatrix, const GroupProperties& properties) { |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 312 | outMatrix->reset(); |
| 313 | // TODO: use rotate(mRotate, mPivotX, mPivotY) and scale with pivot point, instead of |
| 314 | // translating to pivot for rotating and scaling, then translating back. |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 315 | outMatrix->postTranslate(-properties.getPivotX(), -properties.getPivotY()); |
| 316 | outMatrix->postScale(properties.getScaleX(), properties.getScaleY()); |
| 317 | outMatrix->postRotate(properties.getRotation(), 0, 0); |
| 318 | outMatrix->postTranslate(properties.getTranslateX() + properties.getPivotX(), |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 319 | properties.getTranslateY() + properties.getPivotY()); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | void Group::addChild(Node* child) { |
Doris Liu | ef062eb | 2016-02-04 16:16:27 -0800 | [diff] [blame] | 323 | mChildren.emplace_back(child); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 324 | if (mPropertyChangedListener != nullptr) { |
| 325 | child->setPropertyChangedListener(mPropertyChangedListener); |
| 326 | } |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 327 | } |
| 328 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 329 | bool Group::GroupProperties::copyProperties(float* outProperties, int length) const { |
| 330 | int propertyCount = static_cast<int>(Property::count); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 331 | if (length != propertyCount) { |
| 332 | LOG_ALWAYS_FATAL("Properties needs exactly %d bytes, a byte array of size %d is provided", |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 333 | propertyCount, length); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 334 | return false; |
| 335 | } |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 336 | |
| 337 | PrimitiveFields* out = reinterpret_cast<PrimitiveFields*>(outProperties); |
| 338 | *out = mPrimitiveFields; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 339 | return true; |
| 340 | } |
| 341 | |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 342 | // TODO: Consider animating the properties as float pointers |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 343 | // Called on render thread |
| 344 | float Group::GroupProperties::getPropertyValue(int propertyId) const { |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 345 | Property currentProperty = static_cast<Property>(propertyId); |
| 346 | switch (currentProperty) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 347 | case Property::rotate: |
| 348 | return getRotation(); |
| 349 | case Property::pivotX: |
| 350 | return getPivotX(); |
| 351 | case Property::pivotY: |
| 352 | return getPivotY(); |
| 353 | case Property::scaleX: |
| 354 | return getScaleX(); |
| 355 | case Property::scaleY: |
| 356 | return getScaleY(); |
| 357 | case Property::translateX: |
| 358 | return getTranslateX(); |
| 359 | case Property::translateY: |
| 360 | return getTranslateY(); |
| 361 | default: |
| 362 | LOG_ALWAYS_FATAL("Invalid property index: %d", propertyId); |
| 363 | return 0; |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 367 | // Called on render thread |
| 368 | void Group::GroupProperties::setPropertyValue(int propertyId, float value) { |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 369 | Property currentProperty = static_cast<Property>(propertyId); |
| 370 | switch (currentProperty) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 371 | case Property::rotate: |
| 372 | setRotation(value); |
| 373 | break; |
| 374 | case Property::pivotX: |
| 375 | setPivotX(value); |
| 376 | break; |
| 377 | case Property::pivotY: |
| 378 | setPivotY(value); |
| 379 | break; |
| 380 | case Property::scaleX: |
| 381 | setScaleX(value); |
| 382 | break; |
| 383 | case Property::scaleY: |
| 384 | setScaleY(value); |
| 385 | break; |
| 386 | case Property::translateX: |
| 387 | setTranslateX(value); |
| 388 | break; |
| 389 | case Property::translateY: |
| 390 | setTranslateY(value); |
| 391 | break; |
| 392 | default: |
| 393 | LOG_ALWAYS_FATAL("Invalid property index: %d", propertyId); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 394 | } |
| 395 | } |
| 396 | |
| 397 | bool Group::isValidProperty(int propertyId) { |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 398 | return GroupProperties::isValidProperty(propertyId); |
| 399 | } |
| 400 | |
| 401 | bool Group::GroupProperties::isValidProperty(int propertyId) { |
| 402 | return propertyId >= 0 && propertyId < static_cast<int>(Property::count); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 403 | } |
| 404 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 405 | int Tree::draw(Canvas* outCanvas, SkColorFilter* colorFilter, const SkRect& bounds, |
| 406 | bool needsMirroring, bool canReuseCache) { |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 407 | // The imageView can scale the canvas in different ways, in order to |
| 408 | // avoid blurry scaling, we have to draw into a bitmap with exact pixel |
| 409 | // size first. This bitmap size is determined by the bounds and the |
| 410 | // canvas scale. |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 411 | SkMatrix canvasMatrix; |
| 412 | outCanvas->getMatrix(&canvasMatrix); |
Doris Liu | a0e6157 | 2015-12-29 14:57:49 -0800 | [diff] [blame] | 413 | float canvasScaleX = 1.0f; |
| 414 | float canvasScaleY = 1.0f; |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 415 | if (canvasMatrix.getSkewX() == 0 && canvasMatrix.getSkewY() == 0) { |
Doris Liu | a0e6157 | 2015-12-29 14:57:49 -0800 | [diff] [blame] | 416 | // 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] | 417 | // 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] | 418 | canvasScaleX = fabs(canvasMatrix.getScaleX()); |
| 419 | canvasScaleY = fabs(canvasMatrix.getScaleY()); |
Doris Liu | a0e6157 | 2015-12-29 14:57:49 -0800 | [diff] [blame] | 420 | } |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 421 | int scaledWidth = (int)(bounds.width() * canvasScaleX); |
| 422 | int scaledHeight = (int)(bounds.height() * canvasScaleY); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 423 | scaledWidth = std::min(Tree::MAX_CACHED_BITMAP_SIZE, scaledWidth); |
| 424 | scaledHeight = std::min(Tree::MAX_CACHED_BITMAP_SIZE, scaledHeight); |
| 425 | |
| 426 | if (scaledWidth <= 0 || scaledHeight <= 0) { |
Doris Liu | f8d131c | 2016-04-29 18:41:29 -0700 | [diff] [blame] | 427 | return 0; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 428 | } |
| 429 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 430 | mStagingProperties.setScaledSize(scaledWidth, scaledHeight); |
Florin Malita | 777bf85 | 2016-02-03 10:48:55 -0500 | [diff] [blame] | 431 | int saveCount = outCanvas->save(SaveFlags::MatrixClip); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 432 | outCanvas->translate(bounds.fLeft, bounds.fTop); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 433 | |
| 434 | // Handle RTL mirroring. |
| 435 | if (needsMirroring) { |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 436 | outCanvas->translate(bounds.width(), 0); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 437 | outCanvas->scale(-1.0f, 1.0f); |
| 438 | } |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 439 | mStagingProperties.setColorFilter(colorFilter); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 440 | |
| 441 | // At this point, canvas has been translated to the right position. |
| 442 | // And we use this bound for the destination rect for the drawBitmap, so |
| 443 | // we offset to (0, 0); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 444 | SkRect tmpBounds = bounds; |
| 445 | tmpBounds.offsetTo(0, 0); |
| 446 | mStagingProperties.setBounds(tmpBounds); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 447 | outCanvas->drawVectorDrawable(this); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 448 | outCanvas->restoreToCount(saveCount); |
Doris Liu | f8d131c | 2016-04-29 18:41:29 -0700 | [diff] [blame] | 449 | return scaledWidth * scaledHeight; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 450 | } |
| 451 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 452 | void Tree::drawStaging(Canvas* outCanvas) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 453 | bool redrawNeeded = allocateBitmapIfNeeded(mStagingCache, mStagingProperties.getScaledWidth(), |
| 454 | mStagingProperties.getScaledHeight()); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 455 | // draw bitmap cache |
| 456 | if (redrawNeeded || mStagingCache.dirty) { |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 457 | updateBitmapCache(*mStagingCache.bitmap, true); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 458 | mStagingCache.dirty = false; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 459 | } |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 460 | |
| 461 | SkPaint tmpPaint; |
| 462 | SkPaint* paint = updatePaint(&tmpPaint, &mStagingProperties); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 463 | outCanvas->drawBitmap(*mStagingCache.bitmap, 0, 0, mStagingCache.bitmap->width(), |
| 464 | mStagingCache.bitmap->height(), mStagingProperties.getBounds().left(), |
| 465 | mStagingProperties.getBounds().top(), |
| 466 | mStagingProperties.getBounds().right(), |
| 467 | mStagingProperties.getBounds().bottom(), paint); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | SkPaint* Tree::getPaint() { |
| 471 | return updatePaint(&mPaint, &mProperties); |
| 472 | } |
| 473 | |
| 474 | // Update the given paint with alpha and color filter. Return nullptr if no color filter is |
| 475 | // specified and root alpha is 1. Otherwise, return updated paint. |
| 476 | SkPaint* Tree::updatePaint(SkPaint* outPaint, TreeProperties* prop) { |
| 477 | if (prop->getRootAlpha() == 1.0f && prop->getColorFilter() == nullptr) { |
| 478 | return nullptr; |
| 479 | } else { |
Mike Reed | 260ab72 | 2016-10-07 15:59:20 -0400 | [diff] [blame] | 480 | outPaint->setColorFilter(sk_ref_sp(prop->getColorFilter())); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 481 | outPaint->setFilterQuality(kLow_SkFilterQuality); |
| 482 | outPaint->setAlpha(prop->getRootAlpha() * 255); |
| 483 | return outPaint; |
| 484 | } |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 485 | } |
| 486 | |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 487 | Bitmap& Tree::getBitmapUpdateIfDirty() { |
| 488 | bool redrawNeeded = allocateBitmapIfNeeded(mCache, mProperties.getScaledWidth(), |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 489 | mProperties.getScaledHeight()); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 490 | if (redrawNeeded || mCache.dirty) { |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 491 | updateBitmapCache(*mCache.bitmap, false); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 492 | mCache.dirty = false; |
| 493 | } |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 494 | return *mCache.bitmap; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 495 | } |
| 496 | |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 497 | void Tree::updateCache(sp<skiapipeline::VectorDrawableAtlas>& atlas, GrContext* context) { |
| 498 | SkRect dst; |
| 499 | sk_sp<SkSurface> surface = mCache.getSurface(&dst); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 500 | bool canReuseSurface = surface && dst.width() >= mProperties.getScaledWidth() && |
| 501 | dst.height() >= mProperties.getScaledHeight(); |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 502 | if (!canReuseSurface) { |
| 503 | int scaledWidth = SkScalarCeilToInt(mProperties.getScaledWidth()); |
| 504 | int scaledHeight = SkScalarCeilToInt(mProperties.getScaledHeight()); |
| 505 | auto atlasEntry = atlas->requestNewEntry(scaledWidth, scaledHeight, context); |
| 506 | if (INVALID_ATLAS_KEY != atlasEntry.key) { |
| 507 | dst = atlasEntry.rect; |
| 508 | surface = atlasEntry.surface; |
| 509 | mCache.setAtlas(atlas, atlasEntry.key); |
| 510 | } else { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 511 | // don't draw, if we failed to allocate an offscreen buffer |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 512 | mCache.clear(); |
| 513 | surface.reset(); |
| 514 | } |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 515 | } |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 516 | if (!canReuseSurface || mCache.dirty) { |
Derek Sollenberger | 7fe53c1 | 2017-08-31 15:40:12 -0400 | [diff] [blame] | 517 | if (surface) { |
| 518 | Bitmap& bitmap = getBitmapUpdateIfDirty(); |
| 519 | SkBitmap skiaBitmap; |
| 520 | bitmap.getSkBitmap(&skiaBitmap); |
| 521 | if (!surface->getCanvas()->writePixels(skiaBitmap, dst.fLeft, dst.fTop)) { |
| 522 | ALOGD("VectorDrawable caching failed to efficiently upload"); |
| 523 | surface->getCanvas()->drawBitmap(skiaBitmap, dst.fLeft, dst.fTop); |
| 524 | } |
| 525 | } |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 526 | mCache.dirty = false; |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 530 | void Tree::Cache::setAtlas(sp<skiapipeline::VectorDrawableAtlas> newAtlas, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 531 | skiapipeline::AtlasKey newAtlasKey) { |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 532 | LOG_ALWAYS_FATAL_IF(newAtlasKey == INVALID_ATLAS_KEY); |
| 533 | clear(); |
| 534 | mAtlas = newAtlas; |
| 535 | mAtlasKey = newAtlasKey; |
| 536 | } |
| 537 | |
| 538 | sk_sp<SkSurface> Tree::Cache::getSurface(SkRect* bounds) { |
| 539 | sk_sp<SkSurface> surface; |
| 540 | sp<skiapipeline::VectorDrawableAtlas> atlas = mAtlas.promote(); |
| 541 | if (atlas.get() && mAtlasKey != INVALID_ATLAS_KEY) { |
| 542 | auto atlasEntry = atlas->getEntry(mAtlasKey); |
| 543 | *bounds = atlasEntry.rect; |
| 544 | surface = atlasEntry.surface; |
| 545 | mAtlasKey = atlasEntry.key; |
| 546 | } |
| 547 | |
| 548 | return surface; |
| 549 | } |
| 550 | |
| 551 | void Tree::Cache::clear() { |
| 552 | sp<skiapipeline::VectorDrawableAtlas> lockAtlas = mAtlas.promote(); |
| 553 | if (lockAtlas.get()) { |
| 554 | lockAtlas->releaseEntry(mAtlasKey); |
| 555 | } |
| 556 | mAtlas = nullptr; |
| 557 | mAtlasKey = INVALID_ATLAS_KEY; |
| 558 | } |
| 559 | |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 560 | void Tree::draw(SkCanvas* canvas) { |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 561 | SkRect src; |
| 562 | sk_sp<SkSurface> vdSurface = mCache.getSurface(&src); |
| 563 | if (vdSurface) { |
| 564 | canvas->drawImageRect(vdSurface->makeImageSnapshot().get(), src, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 565 | mutateProperties()->getBounds(), getPaint(), |
| 566 | SkCanvas::kFast_SrcRectConstraint); |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 567 | } else { |
| 568 | // Handle the case when VectorDrawableAtlas has been destroyed, because of memory pressure. |
| 569 | // We render the VD into a temporary standalone buffer and mark the frame as dirty. Next |
| 570 | // frame will be cached into the atlas. |
Derek Sollenberger | 7fe53c1 | 2017-08-31 15:40:12 -0400 | [diff] [blame] | 571 | Bitmap& bitmap = getBitmapUpdateIfDirty(); |
| 572 | SkBitmap skiaBitmap; |
| 573 | bitmap.getSkBitmap(&skiaBitmap); |
| 574 | |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 575 | int scaledWidth = SkScalarCeilToInt(mProperties.getScaledWidth()); |
| 576 | int scaledHeight = SkScalarCeilToInt(mProperties.getScaledHeight()); |
Derek Sollenberger | 7fe53c1 | 2017-08-31 15:40:12 -0400 | [diff] [blame] | 577 | canvas->drawBitmapRect(skiaBitmap, SkRect::MakeWH(scaledWidth, scaledHeight), |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 578 | mutateProperties()->getBounds(), getPaint(), |
| 579 | SkCanvas::kFast_SrcRectConstraint); |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 580 | mCache.clear(); |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 581 | markDirty(); |
| 582 | } |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 583 | } |
| 584 | |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 585 | void Tree::updateBitmapCache(Bitmap& bitmap, bool useStagingData) { |
| 586 | SkBitmap outCache; |
| 587 | bitmap.getSkBitmap(&outCache); |
ztenghui | cf0c41d | 2017-09-13 10:32:50 -0700 | [diff] [blame] | 588 | int cacheWidth = outCache.width(); |
| 589 | int cacheHeight = outCache.height(); |
| 590 | ATRACE_FORMAT("VectorDrawable repaint %dx%d", cacheWidth, cacheHeight); |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 591 | outCache.eraseColor(SK_ColorTRANSPARENT); |
| 592 | SkCanvas outCanvas(outCache); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 593 | float viewportWidth = |
| 594 | useStagingData ? mStagingProperties.getViewportWidth() : mProperties.getViewportWidth(); |
| 595 | float viewportHeight = useStagingData ? mStagingProperties.getViewportHeight() |
| 596 | : mProperties.getViewportHeight(); |
ztenghui | cf0c41d | 2017-09-13 10:32:50 -0700 | [diff] [blame] | 597 | float scaleX = cacheWidth / viewportWidth; |
| 598 | float scaleY = cacheHeight / viewportHeight; |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame] | 599 | outCanvas.scale(scaleX, scaleY); |
| 600 | mRootNode->draw(&outCanvas, useStagingData); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 601 | } |
| 602 | |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 603 | bool Tree::allocateBitmapIfNeeded(Cache& cache, int width, int height) { |
| 604 | if (!canReuseBitmap(cache.bitmap.get(), width, height)) { |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 605 | #ifndef ANDROID_ENABLE_LINEAR_BLENDING |
| 606 | sk_sp<SkColorSpace> colorSpace = nullptr; |
| 607 | #else |
Matt Sarett | 89ddb1f | 2017-02-10 13:31:56 -0500 | [diff] [blame] | 608 | sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeSRGB(); |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 609 | #endif |
| 610 | SkImageInfo info = SkImageInfo::MakeN32(width, height, kPremul_SkAlphaType, colorSpace); |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 611 | cache.bitmap = Bitmap::allocateHeapBitmap(info); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 612 | return true; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 613 | } |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 614 | return false; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 615 | } |
| 616 | |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 617 | bool Tree::canReuseBitmap(Bitmap* bitmap, int width, int height) { |
Teng-Hui Zhu | 037fc18 | 2016-11-16 10:29:39 -0800 | [diff] [blame] | 618 | return bitmap && width <= bitmap->width() && height <= bitmap->height(); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | void Tree::onPropertyChanged(TreeProperties* prop) { |
| 622 | if (prop == &mStagingProperties) { |
| 623 | mStagingCache.dirty = true; |
| 624 | } else { |
| 625 | mCache.dirty = true; |
| 626 | } |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 627 | } |
| 628 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 629 | }; // namespace VectorDrawable |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 630 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 631 | }; // namespace uirenderer |
| 632 | }; // namespace android |