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