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> |
Mike Reed | c2dbc03 | 2019-07-25 12:28:29 -0400 | [diff] [blame^] | 20 | #include "hwui/Paint.h" |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 21 | #include "PathParser.h" |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 22 | #include "SkColorFilter.h" |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 23 | #include "SkImageInfo.h" |
Teng-Hui Zhu | dbee9bb | 2015-12-15 11:01:27 -0800 | [diff] [blame] | 24 | #include "SkShader.h" |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 25 | #include "utils/Macros.h" |
ztenghui | cf0c41d | 2017-09-13 10:32:50 -0700 | [diff] [blame] | 26 | #include "utils/TraceUtils.h" |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 27 | #include "utils/VectorDrawableUtils.h" |
| 28 | |
| 29 | #include <math.h> |
| 30 | #include <string.h> |
| 31 | |
| 32 | namespace android { |
| 33 | namespace uirenderer { |
| 34 | namespace VectorDrawable { |
| 35 | |
| 36 | const int Tree::MAX_CACHED_BITMAP_SIZE = 2048; |
| 37 | |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 38 | void Path::dump() { |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 39 | 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] | 40 | } |
| 41 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 42 | // Called from UI thread during the initial setup/theme change. |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 43 | Path::Path(const char* pathStr, size_t strLength) { |
| 44 | PathParser::ParseResult result; |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 45 | Data data; |
Doris Liu | b35da39 | 2016-04-12 11:06:23 -0700 | [diff] [blame] | 46 | PathParser::getPathDataFromAsciiString(&data, &result, pathStr, strLength); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 47 | mStagingProperties.setData(data); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | Path::Path(const Path& path) : Node(path) { |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 51 | mStagingProperties.syncProperties(path.mStagingProperties); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 52 | } |
| 53 | |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame] | 54 | const SkPath& Path::getUpdatedPath(bool useStagingData, SkPath* tempStagingPath) { |
| 55 | if (useStagingData) { |
| 56 | tempStagingPath->reset(); |
| 57 | VectorDrawableUtils::verbsToPath(tempStagingPath, mStagingProperties.getData()); |
| 58 | return *tempStagingPath; |
| 59 | } else { |
| 60 | if (mSkPathDirty) { |
| 61 | mSkPath.reset(); |
| 62 | VectorDrawableUtils::verbsToPath(&mSkPath, mProperties.getData()); |
| 63 | mSkPathDirty = false; |
| 64 | } |
| 65 | return mSkPath; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 66 | } |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void Path::syncProperties() { |
| 70 | if (mStagingPropertiesDirty) { |
| 71 | mProperties.syncProperties(mStagingProperties); |
| 72 | } else { |
| 73 | mStagingProperties.syncProperties(mProperties); |
| 74 | } |
| 75 | mStagingPropertiesDirty = false; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | FullPath::FullPath(const FullPath& path) : Path(path) { |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 79 | mStagingProperties.syncProperties(path.mStagingProperties); |
| 80 | } |
| 81 | |
| 82 | static void applyTrim(SkPath* outPath, const SkPath& inPath, float trimPathStart, float trimPathEnd, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 83 | float trimPathOffset) { |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 84 | if (trimPathStart == 0.0f && trimPathEnd == 1.0f) { |
| 85 | *outPath = inPath; |
| 86 | return; |
| 87 | } |
| 88 | outPath->reset(); |
| 89 | if (trimPathStart == trimPathEnd) { |
| 90 | // Trimmed path should be empty. |
| 91 | return; |
| 92 | } |
| 93 | SkPathMeasure measure(inPath, false); |
| 94 | float len = SkScalarToFloat(measure.getLength()); |
| 95 | float start = len * fmod((trimPathStart + trimPathOffset), 1.0f); |
| 96 | float end = len * fmod((trimPathEnd + trimPathOffset), 1.0f); |
| 97 | |
| 98 | if (start > end) { |
| 99 | measure.getSegment(start, len, outPath, true); |
| 100 | if (end > 0) { |
| 101 | measure.getSegment(0, end, outPath, true); |
| 102 | } |
| 103 | } else { |
| 104 | measure.getSegment(start, end, outPath, true); |
| 105 | } |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 106 | } |
| 107 | |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame] | 108 | const SkPath& FullPath::getUpdatedPath(bool useStagingData, SkPath* tempStagingPath) { |
| 109 | if (!useStagingData && !mSkPathDirty && !mProperties.mTrimDirty) { |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 110 | return mTrimmedSkPath; |
| 111 | } |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame] | 112 | Path::getUpdatedPath(useStagingData, tempStagingPath); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 113 | SkPath* outPath; |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame] | 114 | if (useStagingData) { |
| 115 | SkPath inPath = *tempStagingPath; |
| 116 | applyTrim(tempStagingPath, inPath, mStagingProperties.getTrimPathStart(), |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 117 | mStagingProperties.getTrimPathEnd(), mStagingProperties.getTrimPathOffset()); |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame] | 118 | outPath = tempStagingPath; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 119 | } else { |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame] | 120 | if (mProperties.getTrimPathStart() != 0.0f || mProperties.getTrimPathEnd() != 1.0f) { |
| 121 | mProperties.mTrimDirty = false; |
| 122 | applyTrim(&mTrimmedSkPath, mSkPath, mProperties.getTrimPathStart(), |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 123 | mProperties.getTrimPathEnd(), mProperties.getTrimPathOffset()); |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame] | 124 | outPath = &mTrimmedSkPath; |
| 125 | } else { |
| 126 | outPath = &mSkPath; |
| 127 | } |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 128 | } |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame] | 129 | const FullPathProperties& properties = useStagingData ? mStagingProperties : mProperties; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 130 | bool setFillPath = properties.getFillGradient() != nullptr || |
| 131 | properties.getFillColor() != SK_ColorTRANSPARENT; |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame] | 132 | if (setFillPath) { |
| 133 | SkPath::FillType ft = static_cast<SkPath::FillType>(properties.getFillType()); |
| 134 | outPath->setFillType(ft); |
| 135 | } |
| 136 | return *outPath; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 139 | void FullPath::dump() { |
| 140 | Path::dump(); |
| 141 | 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] | 142 | mProperties.getStrokeWidth(), mProperties.getStrokeColor(), mProperties.getStrokeAlpha(), |
| 143 | mProperties.getFillColor(), mProperties.getFillAlpha()); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 146 | inline SkColor applyAlpha(SkColor color, float alpha) { |
| 147 | int alphaBytes = SkColorGetA(color); |
| 148 | return SkColorSetA(color, alphaBytes * alpha); |
| 149 | } |
| 150 | |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame] | 151 | void FullPath::draw(SkCanvas* outCanvas, bool useStagingData) { |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 152 | const FullPathProperties& properties = useStagingData ? mStagingProperties : mProperties; |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame] | 153 | SkPath tempStagingPath; |
| 154 | const SkPath& renderPath = getUpdatedPath(useStagingData, &tempStagingPath); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 155 | |
Teng-Hui Zhu | dbee9bb | 2015-12-15 11:01:27 -0800 | [diff] [blame] | 156 | // Draw path's fill, if fill color or gradient is valid |
| 157 | bool needsFill = false; |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 158 | SkPaint paint; |
| 159 | if (properties.getFillGradient() != nullptr) { |
| 160 | paint.setColor(applyAlpha(SK_ColorBLACK, properties.getFillAlpha())); |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame] | 161 | paint.setShader(sk_sp<SkShader>(SkSafeRef(properties.getFillGradient()))); |
Teng-Hui Zhu | dbee9bb | 2015-12-15 11:01:27 -0800 | [diff] [blame] | 162 | needsFill = true; |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 163 | } else if (properties.getFillColor() != SK_ColorTRANSPARENT) { |
| 164 | paint.setColor(applyAlpha(properties.getFillColor(), properties.getFillAlpha())); |
Teng-Hui Zhu | dbee9bb | 2015-12-15 11:01:27 -0800 | [diff] [blame] | 165 | needsFill = true; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 166 | } |
Teng-Hui Zhu | dbee9bb | 2015-12-15 11:01:27 -0800 | [diff] [blame] | 167 | |
| 168 | if (needsFill) { |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 169 | paint.setStyle(SkPaint::Style::kFill_Style); |
Doris Liu | 6b184d7 | 2017-12-04 16:31:07 -0800 | [diff] [blame] | 170 | paint.setAntiAlias(mAntiAlias); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 171 | outCanvas->drawPath(renderPath, paint); |
Teng-Hui Zhu | dbee9bb | 2015-12-15 11:01:27 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 174 | // Draw path's stroke, if stroke color or Gradient is valid |
Teng-Hui Zhu | dbee9bb | 2015-12-15 11:01:27 -0800 | [diff] [blame] | 175 | bool needsStroke = false; |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 176 | if (properties.getStrokeGradient() != nullptr) { |
| 177 | paint.setColor(applyAlpha(SK_ColorBLACK, properties.getStrokeAlpha())); |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame] | 178 | paint.setShader(sk_sp<SkShader>(SkSafeRef(properties.getStrokeGradient()))); |
Teng-Hui Zhu | dbee9bb | 2015-12-15 11:01:27 -0800 | [diff] [blame] | 179 | needsStroke = true; |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 180 | } else if (properties.getStrokeColor() != SK_ColorTRANSPARENT) { |
| 181 | paint.setColor(applyAlpha(properties.getStrokeColor(), properties.getStrokeAlpha())); |
Teng-Hui Zhu | dbee9bb | 2015-12-15 11:01:27 -0800 | [diff] [blame] | 182 | needsStroke = true; |
| 183 | } |
| 184 | if (needsStroke) { |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 185 | paint.setStyle(SkPaint::Style::kStroke_Style); |
Doris Liu | 6b184d7 | 2017-12-04 16:31:07 -0800 | [diff] [blame] | 186 | paint.setAntiAlias(mAntiAlias); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 187 | paint.setStrokeJoin(SkPaint::Join(properties.getStrokeLineJoin())); |
| 188 | paint.setStrokeCap(SkPaint::Cap(properties.getStrokeLineCap())); |
| 189 | paint.setStrokeMiter(properties.getStrokeMiterLimit()); |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame] | 190 | paint.setStrokeWidth(properties.getStrokeWidth()); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 191 | outCanvas->drawPath(renderPath, paint); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 195 | void FullPath::syncProperties() { |
| 196 | Path::syncProperties(); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 197 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 198 | if (mStagingPropertiesDirty) { |
| 199 | mProperties.syncProperties(mStagingProperties); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 200 | } else { |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 201 | // Update staging property with property values from animation. |
| 202 | mStagingProperties.syncProperties(mProperties); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 203 | } |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 204 | mStagingPropertiesDirty = false; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 205 | } |
| 206 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 207 | REQUIRE_COMPATIBLE_LAYOUT(FullPath::FullPathProperties::PrimitiveFields); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 208 | |
| 209 | static_assert(sizeof(float) == sizeof(int32_t), "float is not the same size as int32_t"); |
| 210 | static_assert(sizeof(SkColor) == sizeof(int32_t), "SkColor is not the same size as int32_t"); |
| 211 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 212 | bool FullPath::FullPathProperties::copyProperties(int8_t* outProperties, int length) const { |
| 213 | int propertyDataSize = sizeof(FullPathProperties::PrimitiveFields); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 214 | if (length != propertyDataSize) { |
| 215 | 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] | 216 | propertyDataSize, length); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 217 | return false; |
| 218 | } |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 219 | |
| 220 | PrimitiveFields* out = reinterpret_cast<PrimitiveFields*>(outProperties); |
| 221 | *out = mPrimitiveFields; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 222 | return true; |
| 223 | } |
| 224 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 225 | void FullPath::FullPathProperties::setColorPropertyValue(int propertyId, int32_t value) { |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 226 | Property currentProperty = static_cast<Property>(propertyId); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 227 | if (currentProperty == Property::strokeColor) { |
| 228 | setStrokeColor(value); |
| 229 | } else if (currentProperty == Property::fillColor) { |
| 230 | setFillColor(value); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 231 | } else { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 232 | LOG_ALWAYS_FATAL( |
| 233 | "Error setting color property on FullPath: No valid property" |
| 234 | " with id: %d", |
| 235 | propertyId); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 239 | void FullPath::FullPathProperties::setPropertyValue(int propertyId, float value) { |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 240 | Property property = static_cast<Property>(propertyId); |
| 241 | switch (property) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 242 | case Property::strokeWidth: |
| 243 | setStrokeWidth(value); |
| 244 | break; |
| 245 | case Property::strokeAlpha: |
| 246 | setStrokeAlpha(value); |
| 247 | break; |
| 248 | case Property::fillAlpha: |
| 249 | setFillAlpha(value); |
| 250 | break; |
| 251 | case Property::trimPathStart: |
| 252 | setTrimPathStart(value); |
| 253 | break; |
| 254 | case Property::trimPathEnd: |
| 255 | setTrimPathEnd(value); |
| 256 | break; |
| 257 | case Property::trimPathOffset: |
| 258 | setTrimPathOffset(value); |
| 259 | break; |
| 260 | default: |
| 261 | LOG_ALWAYS_FATAL("Invalid property id: %d for animation", propertyId); |
| 262 | break; |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame] | 266 | void ClipPath::draw(SkCanvas* outCanvas, bool useStagingData) { |
| 267 | SkPath tempStagingPath; |
| 268 | outCanvas->clipPath(getUpdatedPath(useStagingData, &tempStagingPath)); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | Group::Group(const Group& group) : Node(group) { |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 272 | mStagingProperties.syncProperties(group.mStagingProperties); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 273 | } |
| 274 | |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame] | 275 | void Group::draw(SkCanvas* outCanvas, bool useStagingData) { |
| 276 | // Save the current clip and matrix information, which is local to this group. |
| 277 | SkAutoCanvasRestore saver(outCanvas, true); |
| 278 | // apply the current group's matrix to the canvas |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 279 | SkMatrix stackedMatrix; |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 280 | const GroupProperties& prop = useStagingData ? mStagingProperties : mProperties; |
| 281 | getLocalMatrix(&stackedMatrix, prop); |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame] | 282 | outCanvas->concat(stackedMatrix); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 283 | // Draw the group tree in the same order as the XML file. |
Doris Liu | ef062eb | 2016-02-04 16:16:27 -0800 | [diff] [blame] | 284 | for (auto& child : mChildren) { |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame] | 285 | child->draw(outCanvas, useStagingData); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 286 | } |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame] | 287 | // Restore the previous clip and matrix information. |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | void Group::dump() { |
| 291 | ALOGD("Group %s has %zu children: ", mName.c_str(), mChildren.size()); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 292 | ALOGD("Group translateX, Y : %f, %f, scaleX, Y: %f, %f", mProperties.getTranslateX(), |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 293 | mProperties.getTranslateY(), mProperties.getScaleX(), mProperties.getScaleY()); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 294 | for (size_t i = 0; i < mChildren.size(); i++) { |
| 295 | mChildren[i]->dump(); |
| 296 | } |
| 297 | } |
| 298 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 299 | void Group::syncProperties() { |
| 300 | // Copy over the dirty staging properties |
| 301 | if (mStagingPropertiesDirty) { |
| 302 | mProperties.syncProperties(mStagingProperties); |
| 303 | } else { |
| 304 | mStagingProperties.syncProperties(mProperties); |
| 305 | } |
| 306 | mStagingPropertiesDirty = false; |
| 307 | for (auto& child : mChildren) { |
| 308 | child->syncProperties(); |
| 309 | } |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 310 | } |
| 311 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 312 | void Group::getLocalMatrix(SkMatrix* outMatrix, const GroupProperties& properties) { |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 313 | outMatrix->reset(); |
| 314 | // TODO: use rotate(mRotate, mPivotX, mPivotY) and scale with pivot point, instead of |
| 315 | // translating to pivot for rotating and scaling, then translating back. |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 316 | outMatrix->postTranslate(-properties.getPivotX(), -properties.getPivotY()); |
| 317 | outMatrix->postScale(properties.getScaleX(), properties.getScaleY()); |
| 318 | outMatrix->postRotate(properties.getRotation(), 0, 0); |
| 319 | outMatrix->postTranslate(properties.getTranslateX() + properties.getPivotX(), |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 320 | properties.getTranslateY() + properties.getPivotY()); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | void Group::addChild(Node* child) { |
Doris Liu | ef062eb | 2016-02-04 16:16:27 -0800 | [diff] [blame] | 324 | mChildren.emplace_back(child); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 325 | if (mPropertyChangedListener != nullptr) { |
| 326 | child->setPropertyChangedListener(mPropertyChangedListener); |
| 327 | } |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 328 | } |
| 329 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 330 | bool Group::GroupProperties::copyProperties(float* outProperties, int length) const { |
| 331 | int propertyCount = static_cast<int>(Property::count); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 332 | if (length != propertyCount) { |
| 333 | 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] | 334 | propertyCount, length); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 335 | return false; |
| 336 | } |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 337 | |
| 338 | PrimitiveFields* out = reinterpret_cast<PrimitiveFields*>(outProperties); |
| 339 | *out = mPrimitiveFields; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 340 | return true; |
| 341 | } |
| 342 | |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 343 | // TODO: Consider animating the properties as float pointers |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 344 | // Called on render thread |
| 345 | float Group::GroupProperties::getPropertyValue(int propertyId) const { |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 346 | Property currentProperty = static_cast<Property>(propertyId); |
| 347 | switch (currentProperty) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 348 | case Property::rotate: |
| 349 | return getRotation(); |
| 350 | case Property::pivotX: |
| 351 | return getPivotX(); |
| 352 | case Property::pivotY: |
| 353 | return getPivotY(); |
| 354 | case Property::scaleX: |
| 355 | return getScaleX(); |
| 356 | case Property::scaleY: |
| 357 | return getScaleY(); |
| 358 | case Property::translateX: |
| 359 | return getTranslateX(); |
| 360 | case Property::translateY: |
| 361 | return getTranslateY(); |
| 362 | default: |
| 363 | LOG_ALWAYS_FATAL("Invalid property index: %d", propertyId); |
| 364 | return 0; |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 368 | // Called on render thread |
| 369 | void Group::GroupProperties::setPropertyValue(int propertyId, float value) { |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 370 | Property currentProperty = static_cast<Property>(propertyId); |
| 371 | switch (currentProperty) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 372 | case Property::rotate: |
| 373 | setRotation(value); |
| 374 | break; |
| 375 | case Property::pivotX: |
| 376 | setPivotX(value); |
| 377 | break; |
| 378 | case Property::pivotY: |
| 379 | setPivotY(value); |
| 380 | break; |
| 381 | case Property::scaleX: |
| 382 | setScaleX(value); |
| 383 | break; |
| 384 | case Property::scaleY: |
| 385 | setScaleY(value); |
| 386 | break; |
| 387 | case Property::translateX: |
| 388 | setTranslateX(value); |
| 389 | break; |
| 390 | case Property::translateY: |
| 391 | setTranslateY(value); |
| 392 | break; |
| 393 | default: |
| 394 | LOG_ALWAYS_FATAL("Invalid property index: %d", propertyId); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 395 | } |
| 396 | } |
| 397 | |
| 398 | bool Group::isValidProperty(int propertyId) { |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 399 | return GroupProperties::isValidProperty(propertyId); |
| 400 | } |
| 401 | |
| 402 | bool Group::GroupProperties::isValidProperty(int propertyId) { |
| 403 | return propertyId >= 0 && propertyId < static_cast<int>(Property::count); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 404 | } |
| 405 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 406 | int Tree::draw(Canvas* outCanvas, SkColorFilter* colorFilter, const SkRect& bounds, |
| 407 | bool needsMirroring, bool canReuseCache) { |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 408 | // The imageView can scale the canvas in different ways, in order to |
| 409 | // avoid blurry scaling, we have to draw into a bitmap with exact pixel |
| 410 | // size first. This bitmap size is determined by the bounds and the |
| 411 | // canvas scale. |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 412 | SkMatrix canvasMatrix; |
| 413 | outCanvas->getMatrix(&canvasMatrix); |
Doris Liu | a0e6157 | 2015-12-29 14:57:49 -0800 | [diff] [blame] | 414 | float canvasScaleX = 1.0f; |
| 415 | float canvasScaleY = 1.0f; |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 416 | if (canvasMatrix.getSkewX() == 0 && canvasMatrix.getSkewY() == 0) { |
Doris Liu | a0e6157 | 2015-12-29 14:57:49 -0800 | [diff] [blame] | 417 | // 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] | 418 | // 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] | 419 | canvasScaleX = fabs(canvasMatrix.getScaleX()); |
| 420 | canvasScaleY = fabs(canvasMatrix.getScaleY()); |
Doris Liu | a0e6157 | 2015-12-29 14:57:49 -0800 | [diff] [blame] | 421 | } |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 422 | int scaledWidth = (int)(bounds.width() * canvasScaleX); |
| 423 | int scaledHeight = (int)(bounds.height() * canvasScaleY); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 424 | scaledWidth = std::min(Tree::MAX_CACHED_BITMAP_SIZE, scaledWidth); |
| 425 | scaledHeight = std::min(Tree::MAX_CACHED_BITMAP_SIZE, scaledHeight); |
| 426 | |
| 427 | if (scaledWidth <= 0 || scaledHeight <= 0) { |
Doris Liu | f8d131c | 2016-04-29 18:41:29 -0700 | [diff] [blame] | 428 | return 0; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 429 | } |
| 430 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 431 | mStagingProperties.setScaledSize(scaledWidth, scaledHeight); |
Florin Malita | 777bf85 | 2016-02-03 10:48:55 -0500 | [diff] [blame] | 432 | int saveCount = outCanvas->save(SaveFlags::MatrixClip); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 433 | outCanvas->translate(bounds.fLeft, bounds.fTop); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 434 | |
| 435 | // Handle RTL mirroring. |
| 436 | if (needsMirroring) { |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 437 | outCanvas->translate(bounds.width(), 0); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 438 | outCanvas->scale(-1.0f, 1.0f); |
| 439 | } |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 440 | mStagingProperties.setColorFilter(colorFilter); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 441 | |
| 442 | // At this point, canvas has been translated to the right position. |
| 443 | // And we use this bound for the destination rect for the drawBitmap, so |
| 444 | // we offset to (0, 0); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 445 | SkRect tmpBounds = bounds; |
| 446 | tmpBounds.offsetTo(0, 0); |
| 447 | mStagingProperties.setBounds(tmpBounds); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 448 | outCanvas->drawVectorDrawable(this); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 449 | outCanvas->restoreToCount(saveCount); |
Doris Liu | f8d131c | 2016-04-29 18:41:29 -0700 | [diff] [blame] | 450 | return scaledWidth * scaledHeight; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 451 | } |
| 452 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 453 | void Tree::drawStaging(Canvas* outCanvas) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 454 | bool redrawNeeded = allocateBitmapIfNeeded(mStagingCache, mStagingProperties.getScaledWidth(), |
| 455 | mStagingProperties.getScaledHeight()); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 456 | // draw bitmap cache |
| 457 | if (redrawNeeded || mStagingCache.dirty) { |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 458 | updateBitmapCache(*mStagingCache.bitmap, true); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 459 | mStagingCache.dirty = false; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 460 | } |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 461 | |
Mike Reed | c2dbc03 | 2019-07-25 12:28:29 -0400 | [diff] [blame^] | 462 | SkPaint skp; |
| 463 | getPaintFor(&skp, mStagingProperties); |
| 464 | Paint paint; |
| 465 | paint.setFilterQuality(skp.getFilterQuality()); |
| 466 | paint.setColorFilter(skp.refColorFilter()); |
| 467 | paint.setAlpha(skp.getAlpha()); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 468 | outCanvas->drawBitmap(*mStagingCache.bitmap, 0, 0, mStagingCache.bitmap->width(), |
| 469 | mStagingCache.bitmap->height(), mStagingProperties.getBounds().left(), |
| 470 | mStagingProperties.getBounds().top(), |
| 471 | mStagingProperties.getBounds().right(), |
John Reck | 08ee815 | 2018-09-20 16:27:46 -0700 | [diff] [blame] | 472 | mStagingProperties.getBounds().bottom(), &paint); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 473 | } |
| 474 | |
John Reck | 08ee815 | 2018-09-20 16:27:46 -0700 | [diff] [blame] | 475 | void Tree::getPaintFor(SkPaint* outPaint, const TreeProperties &prop) const { |
Stan Iliev | 038fc37 | 2018-07-30 18:31:46 -0400 | [diff] [blame] | 476 | // HWUI always draws VD with bilinear filtering. |
| 477 | outPaint->setFilterQuality(kLow_SkFilterQuality); |
Doris Liu | 7cc6ec2 | 2018-10-02 16:15:57 -0700 | [diff] [blame] | 478 | if (prop.getColorFilter() != nullptr) { |
John Reck | 08ee815 | 2018-09-20 16:27:46 -0700 | [diff] [blame] | 479 | outPaint->setColorFilter(sk_ref_sp(prop.getColorFilter())); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 480 | } |
Doris Liu | 7cc6ec2 | 2018-10-02 16:15:57 -0700 | [diff] [blame] | 481 | outPaint->setAlpha(prop.getRootAlpha() * 255); |
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(), |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 486 | mProperties.getScaledHeight()); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 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 | |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 494 | void Tree::updateCache(sp<skiapipeline::VectorDrawableAtlas>& atlas, GrContext* context) { |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 495 | #ifdef __ANDROID__ // Layoutlib does not support hardware acceleration |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 496 | SkRect dst; |
| 497 | sk_sp<SkSurface> surface = mCache.getSurface(&dst); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 498 | bool canReuseSurface = surface && dst.width() >= mProperties.getScaledWidth() && |
| 499 | dst.height() >= mProperties.getScaledHeight(); |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 500 | if (!canReuseSurface) { |
| 501 | int scaledWidth = SkScalarCeilToInt(mProperties.getScaledWidth()); |
| 502 | int scaledHeight = SkScalarCeilToInt(mProperties.getScaledHeight()); |
| 503 | auto atlasEntry = atlas->requestNewEntry(scaledWidth, scaledHeight, context); |
| 504 | if (INVALID_ATLAS_KEY != atlasEntry.key) { |
| 505 | dst = atlasEntry.rect; |
| 506 | surface = atlasEntry.surface; |
| 507 | mCache.setAtlas(atlas, atlasEntry.key); |
| 508 | } else { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 509 | // don't draw, if we failed to allocate an offscreen buffer |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 510 | mCache.clear(); |
| 511 | surface.reset(); |
| 512 | } |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 513 | } |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 514 | if (!canReuseSurface || mCache.dirty) { |
Derek Sollenberger | 7fe53c1 | 2017-08-31 15:40:12 -0400 | [diff] [blame] | 515 | if (surface) { |
| 516 | Bitmap& bitmap = getBitmapUpdateIfDirty(); |
| 517 | SkBitmap skiaBitmap; |
| 518 | bitmap.getSkBitmap(&skiaBitmap); |
Mike Reed | 6b6e66d | 2018-02-08 16:28:53 -0500 | [diff] [blame] | 519 | surface->writePixels(skiaBitmap, dst.fLeft, dst.fTop); |
Derek Sollenberger | 7fe53c1 | 2017-08-31 15:40:12 -0400 | [diff] [blame] | 520 | } |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 521 | mCache.dirty = false; |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 522 | } |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 523 | #endif |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 524 | } |
| 525 | |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 526 | void Tree::Cache::setAtlas(sp<skiapipeline::VectorDrawableAtlas> newAtlas, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 527 | skiapipeline::AtlasKey newAtlasKey) { |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 528 | LOG_ALWAYS_FATAL_IF(newAtlasKey == INVALID_ATLAS_KEY); |
| 529 | clear(); |
| 530 | mAtlas = newAtlas; |
| 531 | mAtlasKey = newAtlasKey; |
| 532 | } |
| 533 | |
| 534 | sk_sp<SkSurface> Tree::Cache::getSurface(SkRect* bounds) { |
| 535 | sk_sp<SkSurface> surface; |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 536 | #ifdef __ANDROID__ // Layoutlib does not support hardware acceleration |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 537 | sp<skiapipeline::VectorDrawableAtlas> atlas = mAtlas.promote(); |
| 538 | if (atlas.get() && mAtlasKey != INVALID_ATLAS_KEY) { |
| 539 | auto atlasEntry = atlas->getEntry(mAtlasKey); |
| 540 | *bounds = atlasEntry.rect; |
| 541 | surface = atlasEntry.surface; |
| 542 | mAtlasKey = atlasEntry.key; |
| 543 | } |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 544 | #endif |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 545 | |
| 546 | return surface; |
| 547 | } |
| 548 | |
| 549 | void Tree::Cache::clear() { |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 550 | #ifdef __ANDROID__ // Layoutlib does not support hardware acceleration |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 551 | sp<skiapipeline::VectorDrawableAtlas> lockAtlas = mAtlas.promote(); |
| 552 | if (lockAtlas.get()) { |
| 553 | lockAtlas->releaseEntry(mAtlasKey); |
| 554 | } |
| 555 | mAtlas = nullptr; |
| 556 | mAtlasKey = INVALID_ATLAS_KEY; |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 557 | #endif |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 558 | } |
| 559 | |
John Reck | 08ee815 | 2018-09-20 16:27:46 -0700 | [diff] [blame] | 560 | void Tree::draw(SkCanvas* canvas, const SkRect& bounds, const SkPaint& inPaint) { |
Leon Scroggins III | 6c5864c | 2019-04-03 15:09:25 -0400 | [diff] [blame] | 561 | if (canvas->quickReject(bounds)) { |
| 562 | // The RenderNode is on screen, but the AVD is not. |
| 563 | return; |
| 564 | } |
| 565 | |
John Reck | 08ee815 | 2018-09-20 16:27:46 -0700 | [diff] [blame] | 566 | // Update the paint for any animatable properties |
| 567 | SkPaint paint = inPaint; |
| 568 | paint.setAlpha(mProperties.getRootAlpha() * 255); |
| 569 | |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 570 | if (canvas->getGrContext() == nullptr) { |
| 571 | // Recording to picture, don't use the SkSurface which won't work off of renderthread. |
| 572 | Bitmap& bitmap = getBitmapUpdateIfDirty(); |
| 573 | SkBitmap skiaBitmap; |
| 574 | bitmap.getSkBitmap(&skiaBitmap); |
| 575 | |
| 576 | int scaledWidth = SkScalarCeilToInt(mProperties.getScaledWidth()); |
| 577 | int scaledHeight = SkScalarCeilToInt(mProperties.getScaledHeight()); |
| 578 | canvas->drawBitmapRect(skiaBitmap, SkRect::MakeWH(scaledWidth, scaledHeight), bounds, |
| 579 | &paint, SkCanvas::kFast_SrcRectConstraint); |
| 580 | return; |
| 581 | } |
| 582 | |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 583 | SkRect src; |
| 584 | sk_sp<SkSurface> vdSurface = mCache.getSurface(&src); |
| 585 | if (vdSurface) { |
John Reck | 08ee815 | 2018-09-20 16:27:46 -0700 | [diff] [blame] | 586 | canvas->drawImageRect(vdSurface->makeImageSnapshot().get(), src, bounds, &paint, |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 587 | SkCanvas::kFast_SrcRectConstraint); |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 588 | } else { |
| 589 | // Handle the case when VectorDrawableAtlas has been destroyed, because of memory pressure. |
| 590 | // We render the VD into a temporary standalone buffer and mark the frame as dirty. Next |
| 591 | // frame will be cached into the atlas. |
Derek Sollenberger | 7fe53c1 | 2017-08-31 15:40:12 -0400 | [diff] [blame] | 592 | Bitmap& bitmap = getBitmapUpdateIfDirty(); |
| 593 | SkBitmap skiaBitmap; |
| 594 | bitmap.getSkBitmap(&skiaBitmap); |
| 595 | |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 596 | int scaledWidth = SkScalarCeilToInt(mProperties.getScaledWidth()); |
| 597 | int scaledHeight = SkScalarCeilToInt(mProperties.getScaledHeight()); |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 598 | canvas->drawBitmapRect(skiaBitmap, SkRect::MakeWH(scaledWidth, scaledHeight), bounds, |
John Reck | 08ee815 | 2018-09-20 16:27:46 -0700 | [diff] [blame] | 599 | &paint, SkCanvas::kFast_SrcRectConstraint); |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 600 | mCache.clear(); |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 601 | markDirty(); |
| 602 | } |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 603 | } |
| 604 | |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 605 | void Tree::updateBitmapCache(Bitmap& bitmap, bool useStagingData) { |
| 606 | SkBitmap outCache; |
| 607 | bitmap.getSkBitmap(&outCache); |
ztenghui | cf0c41d | 2017-09-13 10:32:50 -0700 | [diff] [blame] | 608 | int cacheWidth = outCache.width(); |
| 609 | int cacheHeight = outCache.height(); |
| 610 | ATRACE_FORMAT("VectorDrawable repaint %dx%d", cacheWidth, cacheHeight); |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 611 | outCache.eraseColor(SK_ColorTRANSPARENT); |
| 612 | SkCanvas outCanvas(outCache); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 613 | float viewportWidth = |
| 614 | useStagingData ? mStagingProperties.getViewportWidth() : mProperties.getViewportWidth(); |
| 615 | float viewportHeight = useStagingData ? mStagingProperties.getViewportHeight() |
| 616 | : mProperties.getViewportHeight(); |
ztenghui | cf0c41d | 2017-09-13 10:32:50 -0700 | [diff] [blame] | 617 | float scaleX = cacheWidth / viewportWidth; |
| 618 | float scaleY = cacheHeight / viewportHeight; |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame] | 619 | outCanvas.scale(scaleX, scaleY); |
| 620 | mRootNode->draw(&outCanvas, useStagingData); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 621 | } |
| 622 | |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 623 | bool Tree::allocateBitmapIfNeeded(Cache& cache, int width, int height) { |
| 624 | if (!canReuseBitmap(cache.bitmap.get(), width, height)) { |
Leon Scroggins III | 1249757 | 2019-01-31 10:06:12 -0500 | [diff] [blame] | 625 | SkImageInfo info = SkImageInfo::MakeN32(width, height, kPremul_SkAlphaType); |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 626 | cache.bitmap = Bitmap::allocateHeapBitmap(info); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 627 | return true; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 628 | } |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 629 | return false; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 630 | } |
| 631 | |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 632 | bool Tree::canReuseBitmap(Bitmap* bitmap, int width, int height) { |
Teng-Hui Zhu | 037fc18 | 2016-11-16 10:29:39 -0800 | [diff] [blame] | 633 | return bitmap && width <= bitmap->width() && height <= bitmap->height(); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | void Tree::onPropertyChanged(TreeProperties* prop) { |
| 637 | if (prop == &mStagingProperties) { |
| 638 | mStagingCache.dirty = true; |
| 639 | } else { |
| 640 | mCache.dirty = true; |
| 641 | } |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 642 | } |
| 643 | |
John Reck | 08ee815 | 2018-09-20 16:27:46 -0700 | [diff] [blame] | 644 | class MinMaxAverage { |
| 645 | public: |
| 646 | void add(float sample) { |
| 647 | if (mCount == 0) { |
| 648 | mMin = sample; |
| 649 | mMax = sample; |
| 650 | } else { |
| 651 | mMin = std::min(mMin, sample); |
| 652 | mMax = std::max(mMax, sample); |
| 653 | } |
| 654 | mTotal += sample; |
| 655 | mCount++; |
| 656 | } |
| 657 | |
| 658 | float average() { return mTotal / mCount; } |
| 659 | |
| 660 | float min() { return mMin; } |
| 661 | |
| 662 | float max() { return mMax; } |
| 663 | |
| 664 | float delta() { return mMax - mMin; } |
| 665 | |
| 666 | private: |
| 667 | float mMin = 0.0f; |
| 668 | float mMax = 0.0f; |
| 669 | float mTotal = 0.0f; |
| 670 | int mCount = 0; |
| 671 | }; |
| 672 | |
| 673 | BitmapPalette Tree::computePalette() { |
| 674 | // TODO Cache this and share the code with Bitmap.cpp |
| 675 | |
| 676 | ATRACE_CALL(); |
| 677 | |
| 678 | // TODO: This calculation of converting to HSV & tracking min/max is probably overkill |
| 679 | // Experiment with something simpler since we just want to figure out if it's "color-ful" |
| 680 | // and then the average perceptual lightness. |
| 681 | |
| 682 | MinMaxAverage hue, saturation, value; |
| 683 | int sampledCount = 0; |
| 684 | |
| 685 | // Sample a grid of 100 pixels to get an overall estimation of the colors in play |
| 686 | mRootNode->forEachFillColor([&](SkColor color) { |
| 687 | if (SkColorGetA(color) < 75) { |
| 688 | return; |
| 689 | } |
| 690 | sampledCount++; |
| 691 | float hsv[3]; |
| 692 | SkColorToHSV(color, hsv); |
| 693 | hue.add(hsv[0]); |
| 694 | saturation.add(hsv[1]); |
| 695 | value.add(hsv[2]); |
| 696 | }); |
| 697 | |
| 698 | if (sampledCount == 0) { |
| 699 | ALOGV("VectorDrawable is mostly translucent"); |
| 700 | return BitmapPalette::Unknown; |
| 701 | } |
| 702 | |
| 703 | ALOGV("samples = %d, hue [min = %f, max = %f, avg = %f]; saturation [min = %f, max = %f, avg = " |
| 704 | "%f]; value [min = %f, max = %f, avg = %f]", |
| 705 | sampledCount, hue.min(), hue.max(), hue.average(), saturation.min(), saturation.max(), |
| 706 | saturation.average(), value.min(), value.max(), value.average()); |
| 707 | |
| 708 | if (hue.delta() <= 20 && saturation.delta() <= .1f) { |
| 709 | if (value.average() >= .5f) { |
| 710 | return BitmapPalette::Light; |
| 711 | } else { |
| 712 | return BitmapPalette::Dark; |
| 713 | } |
| 714 | } |
| 715 | return BitmapPalette::Unknown; |
| 716 | } |
| 717 | |
Chris Blume | 7b8a808 | 2018-11-30 15:51:58 -0800 | [diff] [blame] | 718 | } // namespace VectorDrawable |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 719 | |
Chris Blume | 7b8a808 | 2018-11-30 15:51:58 -0800 | [diff] [blame] | 720 | } // namespace uirenderer |
| 721 | } // namespace android |