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