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