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