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 | |
John Reck | 08ee815 | 2018-09-20 16:27:46 -0700 | [diff] [blame] | 461 | SkPaint paint; |
| 462 | getPaintFor(&paint, 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(), |
John Reck | 08ee815 | 2018-09-20 16:27:46 -0700 | [diff] [blame] | 467 | mStagingProperties.getBounds().bottom(), &paint); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 468 | } |
| 469 | |
John Reck | 08ee815 | 2018-09-20 16:27:46 -0700 | [diff] [blame] | 470 | void Tree::getPaintFor(SkPaint* outPaint, const TreeProperties &prop) const { |
Stan Iliev | 038fc37 | 2018-07-30 18:31:46 -0400 | [diff] [blame] | 471 | // HWUI always draws VD with bilinear filtering. |
| 472 | outPaint->setFilterQuality(kLow_SkFilterQuality); |
Doris Liu | 7cc6ec2 | 2018-10-02 16:15:57 -0700 | [diff] [blame] | 473 | if (prop.getColorFilter() != nullptr) { |
John Reck | 08ee815 | 2018-09-20 16:27:46 -0700 | [diff] [blame] | 474 | outPaint->setColorFilter(sk_ref_sp(prop.getColorFilter())); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 475 | } |
Doris Liu | 7cc6ec2 | 2018-10-02 16:15:57 -0700 | [diff] [blame] | 476 | outPaint->setAlpha(prop.getRootAlpha() * 255); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 477 | } |
| 478 | |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 479 | Bitmap& Tree::getBitmapUpdateIfDirty() { |
| 480 | bool redrawNeeded = allocateBitmapIfNeeded(mCache, mProperties.getScaledWidth(), |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 481 | mProperties.getScaledHeight()); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 482 | if (redrawNeeded || mCache.dirty) { |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 483 | updateBitmapCache(*mCache.bitmap, false); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 484 | mCache.dirty = false; |
| 485 | } |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 486 | return *mCache.bitmap; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 487 | } |
| 488 | |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 489 | void Tree::updateCache(sp<skiapipeline::VectorDrawableAtlas>& atlas, GrContext* context) { |
| 490 | SkRect dst; |
| 491 | sk_sp<SkSurface> surface = mCache.getSurface(&dst); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 492 | bool canReuseSurface = surface && dst.width() >= mProperties.getScaledWidth() && |
| 493 | dst.height() >= mProperties.getScaledHeight(); |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 494 | if (!canReuseSurface) { |
| 495 | int scaledWidth = SkScalarCeilToInt(mProperties.getScaledWidth()); |
| 496 | int scaledHeight = SkScalarCeilToInt(mProperties.getScaledHeight()); |
| 497 | auto atlasEntry = atlas->requestNewEntry(scaledWidth, scaledHeight, context); |
| 498 | if (INVALID_ATLAS_KEY != atlasEntry.key) { |
| 499 | dst = atlasEntry.rect; |
| 500 | surface = atlasEntry.surface; |
| 501 | mCache.setAtlas(atlas, atlasEntry.key); |
| 502 | } else { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 503 | // don't draw, if we failed to allocate an offscreen buffer |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 504 | mCache.clear(); |
| 505 | surface.reset(); |
| 506 | } |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 507 | } |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 508 | if (!canReuseSurface || mCache.dirty) { |
Derek Sollenberger | 7fe53c1 | 2017-08-31 15:40:12 -0400 | [diff] [blame] | 509 | if (surface) { |
| 510 | Bitmap& bitmap = getBitmapUpdateIfDirty(); |
| 511 | SkBitmap skiaBitmap; |
| 512 | bitmap.getSkBitmap(&skiaBitmap); |
Mike Reed | 6b6e66d | 2018-02-08 16:28:53 -0500 | [diff] [blame] | 513 | surface->writePixels(skiaBitmap, dst.fLeft, dst.fTop); |
Derek Sollenberger | 7fe53c1 | 2017-08-31 15:40:12 -0400 | [diff] [blame] | 514 | } |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 515 | mCache.dirty = false; |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 519 | void Tree::Cache::setAtlas(sp<skiapipeline::VectorDrawableAtlas> newAtlas, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 520 | skiapipeline::AtlasKey newAtlasKey) { |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 521 | LOG_ALWAYS_FATAL_IF(newAtlasKey == INVALID_ATLAS_KEY); |
| 522 | clear(); |
| 523 | mAtlas = newAtlas; |
| 524 | mAtlasKey = newAtlasKey; |
| 525 | } |
| 526 | |
| 527 | sk_sp<SkSurface> Tree::Cache::getSurface(SkRect* bounds) { |
| 528 | sk_sp<SkSurface> surface; |
| 529 | sp<skiapipeline::VectorDrawableAtlas> atlas = mAtlas.promote(); |
| 530 | if (atlas.get() && mAtlasKey != INVALID_ATLAS_KEY) { |
| 531 | auto atlasEntry = atlas->getEntry(mAtlasKey); |
| 532 | *bounds = atlasEntry.rect; |
| 533 | surface = atlasEntry.surface; |
| 534 | mAtlasKey = atlasEntry.key; |
| 535 | } |
| 536 | |
| 537 | return surface; |
| 538 | } |
| 539 | |
| 540 | void Tree::Cache::clear() { |
| 541 | sp<skiapipeline::VectorDrawableAtlas> lockAtlas = mAtlas.promote(); |
| 542 | if (lockAtlas.get()) { |
| 543 | lockAtlas->releaseEntry(mAtlasKey); |
| 544 | } |
| 545 | mAtlas = nullptr; |
| 546 | mAtlasKey = INVALID_ATLAS_KEY; |
| 547 | } |
| 548 | |
John Reck | 08ee815 | 2018-09-20 16:27:46 -0700 | [diff] [blame] | 549 | void Tree::draw(SkCanvas* canvas, const SkRect& bounds, const SkPaint& inPaint) { |
| 550 | // Update the paint for any animatable properties |
| 551 | SkPaint paint = inPaint; |
| 552 | paint.setAlpha(mProperties.getRootAlpha() * 255); |
| 553 | |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 554 | SkRect src; |
| 555 | sk_sp<SkSurface> vdSurface = mCache.getSurface(&src); |
| 556 | if (vdSurface) { |
John Reck | 08ee815 | 2018-09-20 16:27:46 -0700 | [diff] [blame] | 557 | canvas->drawImageRect(vdSurface->makeImageSnapshot().get(), src, bounds, &paint, |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 558 | SkCanvas::kFast_SrcRectConstraint); |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 559 | } else { |
| 560 | // Handle the case when VectorDrawableAtlas has been destroyed, because of memory pressure. |
| 561 | // We render the VD into a temporary standalone buffer and mark the frame as dirty. Next |
| 562 | // frame will be cached into the atlas. |
Derek Sollenberger | 7fe53c1 | 2017-08-31 15:40:12 -0400 | [diff] [blame] | 563 | Bitmap& bitmap = getBitmapUpdateIfDirty(); |
| 564 | SkBitmap skiaBitmap; |
| 565 | bitmap.getSkBitmap(&skiaBitmap); |
| 566 | |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 567 | int scaledWidth = SkScalarCeilToInt(mProperties.getScaledWidth()); |
| 568 | int scaledHeight = SkScalarCeilToInt(mProperties.getScaledHeight()); |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 569 | canvas->drawBitmapRect(skiaBitmap, SkRect::MakeWH(scaledWidth, scaledHeight), bounds, |
John Reck | 08ee815 | 2018-09-20 16:27:46 -0700 | [diff] [blame] | 570 | &paint, SkCanvas::kFast_SrcRectConstraint); |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 571 | mCache.clear(); |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 572 | markDirty(); |
| 573 | } |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 574 | } |
| 575 | |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 576 | void Tree::updateBitmapCache(Bitmap& bitmap, bool useStagingData) { |
| 577 | SkBitmap outCache; |
| 578 | bitmap.getSkBitmap(&outCache); |
ztenghui | cf0c41d | 2017-09-13 10:32:50 -0700 | [diff] [blame] | 579 | int cacheWidth = outCache.width(); |
| 580 | int cacheHeight = outCache.height(); |
| 581 | ATRACE_FORMAT("VectorDrawable repaint %dx%d", cacheWidth, cacheHeight); |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 582 | outCache.eraseColor(SK_ColorTRANSPARENT); |
| 583 | SkCanvas outCanvas(outCache); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 584 | float viewportWidth = |
| 585 | useStagingData ? mStagingProperties.getViewportWidth() : mProperties.getViewportWidth(); |
| 586 | float viewportHeight = useStagingData ? mStagingProperties.getViewportHeight() |
| 587 | : mProperties.getViewportHeight(); |
ztenghui | cf0c41d | 2017-09-13 10:32:50 -0700 | [diff] [blame] | 588 | float scaleX = cacheWidth / viewportWidth; |
| 589 | float scaleY = cacheHeight / viewportHeight; |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame] | 590 | outCanvas.scale(scaleX, scaleY); |
| 591 | mRootNode->draw(&outCanvas, useStagingData); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 592 | } |
| 593 | |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 594 | bool Tree::allocateBitmapIfNeeded(Cache& cache, int width, int height) { |
| 595 | if (!canReuseBitmap(cache.bitmap.get(), width, height)) { |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 596 | #ifndef ANDROID_ENABLE_LINEAR_BLENDING |
| 597 | sk_sp<SkColorSpace> colorSpace = nullptr; |
| 598 | #else |
Matt Sarett | 89ddb1f | 2017-02-10 13:31:56 -0500 | [diff] [blame] | 599 | sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeSRGB(); |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 600 | #endif |
| 601 | SkImageInfo info = SkImageInfo::MakeN32(width, height, kPremul_SkAlphaType, colorSpace); |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 602 | cache.bitmap = Bitmap::allocateHeapBitmap(info); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 603 | return true; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 604 | } |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 605 | return false; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 606 | } |
| 607 | |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 608 | bool Tree::canReuseBitmap(Bitmap* bitmap, int width, int height) { |
Teng-Hui Zhu | 037fc18 | 2016-11-16 10:29:39 -0800 | [diff] [blame] | 609 | return bitmap && width <= bitmap->width() && height <= bitmap->height(); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | void Tree::onPropertyChanged(TreeProperties* prop) { |
| 613 | if (prop == &mStagingProperties) { |
| 614 | mStagingCache.dirty = true; |
| 615 | } else { |
| 616 | mCache.dirty = true; |
| 617 | } |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 618 | } |
| 619 | |
John Reck | 08ee815 | 2018-09-20 16:27:46 -0700 | [diff] [blame] | 620 | class MinMaxAverage { |
| 621 | public: |
| 622 | void add(float sample) { |
| 623 | if (mCount == 0) { |
| 624 | mMin = sample; |
| 625 | mMax = sample; |
| 626 | } else { |
| 627 | mMin = std::min(mMin, sample); |
| 628 | mMax = std::max(mMax, sample); |
| 629 | } |
| 630 | mTotal += sample; |
| 631 | mCount++; |
| 632 | } |
| 633 | |
| 634 | float average() { return mTotal / mCount; } |
| 635 | |
| 636 | float min() { return mMin; } |
| 637 | |
| 638 | float max() { return mMax; } |
| 639 | |
| 640 | float delta() { return mMax - mMin; } |
| 641 | |
| 642 | private: |
| 643 | float mMin = 0.0f; |
| 644 | float mMax = 0.0f; |
| 645 | float mTotal = 0.0f; |
| 646 | int mCount = 0; |
| 647 | }; |
| 648 | |
| 649 | BitmapPalette Tree::computePalette() { |
| 650 | // TODO Cache this and share the code with Bitmap.cpp |
| 651 | |
| 652 | ATRACE_CALL(); |
| 653 | |
| 654 | // TODO: This calculation of converting to HSV & tracking min/max is probably overkill |
| 655 | // Experiment with something simpler since we just want to figure out if it's "color-ful" |
| 656 | // and then the average perceptual lightness. |
| 657 | |
| 658 | MinMaxAverage hue, saturation, value; |
| 659 | int sampledCount = 0; |
| 660 | |
| 661 | // Sample a grid of 100 pixels to get an overall estimation of the colors in play |
| 662 | mRootNode->forEachFillColor([&](SkColor color) { |
| 663 | if (SkColorGetA(color) < 75) { |
| 664 | return; |
| 665 | } |
| 666 | sampledCount++; |
| 667 | float hsv[3]; |
| 668 | SkColorToHSV(color, hsv); |
| 669 | hue.add(hsv[0]); |
| 670 | saturation.add(hsv[1]); |
| 671 | value.add(hsv[2]); |
| 672 | }); |
| 673 | |
| 674 | if (sampledCount == 0) { |
| 675 | ALOGV("VectorDrawable is mostly translucent"); |
| 676 | return BitmapPalette::Unknown; |
| 677 | } |
| 678 | |
| 679 | ALOGV("samples = %d, hue [min = %f, max = %f, avg = %f]; saturation [min = %f, max = %f, avg = " |
| 680 | "%f]; value [min = %f, max = %f, avg = %f]", |
| 681 | sampledCount, hue.min(), hue.max(), hue.average(), saturation.min(), saturation.max(), |
| 682 | saturation.average(), value.min(), value.max(), value.average()); |
| 683 | |
| 684 | if (hue.delta() <= 20 && saturation.delta() <= .1f) { |
| 685 | if (value.average() >= .5f) { |
| 686 | return BitmapPalette::Light; |
| 687 | } else { |
| 688 | return BitmapPalette::Dark; |
| 689 | } |
| 690 | } |
| 691 | return BitmapPalette::Unknown; |
| 692 | } |
| 693 | |
Chris Blume | 7b8a808 | 2018-11-30 15:51:58 -0800 | [diff] [blame] | 694 | } // namespace VectorDrawable |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 695 | |
Chris Blume | 7b8a808 | 2018-11-30 15:51:58 -0800 | [diff] [blame] | 696 | } // namespace uirenderer |
| 697 | } // namespace android |