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 | #ifndef ANDROID_HWUI_VPATH_H |
| 18 | #define ANDROID_HWUI_VPATH_H |
| 19 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 20 | #include "DisplayList.h" |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 21 | #include "hwui/Bitmap.h" |
| 22 | #include "hwui/Canvas.h" |
| 23 | #include "renderthread/CacheManager.h" |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 24 | |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 25 | #include <SkBitmap.h> |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 26 | #include <SkCanvas.h> |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 27 | #include <SkColor.h> |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 28 | #include <SkColorFilter.h> |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 29 | #include <SkMatrix.h> |
| 30 | #include <SkPaint.h> |
| 31 | #include <SkPath.h> |
| 32 | #include <SkPathMeasure.h> |
| 33 | #include <SkRect.h> |
Teng-Hui Zhu | dbee9bb | 2015-12-15 11:01:27 -0800 | [diff] [blame] | 34 | #include <SkShader.h> |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 35 | #include <SkSurface.h> |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 36 | |
| 37 | #include <cutils/compiler.h> |
| 38 | #include <stddef.h> |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 39 | #include <string> |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 40 | #include <vector> |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 41 | |
| 42 | namespace android { |
| 43 | namespace uirenderer { |
| 44 | |
Teng-Hui Zhu | 85d9952 | 2016-04-25 14:23:40 -0700 | [diff] [blame] | 45 | // Debug |
| 46 | #if DEBUG_VECTOR_DRAWABLE |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 47 | #define VECTOR_DRAWABLE_LOGD(...) ALOGD(__VA_ARGS__) |
Teng-Hui Zhu | 85d9952 | 2016-04-25 14:23:40 -0700 | [diff] [blame] | 48 | #else |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 49 | #define VECTOR_DRAWABLE_LOGD(...) |
Teng-Hui Zhu | 85d9952 | 2016-04-25 14:23:40 -0700 | [diff] [blame] | 50 | #endif |
| 51 | |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 52 | namespace VectorDrawable { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 53 | #define VD_SET_PRIMITIVE_FIELD_WITH_FLAG(field, value, flag) \ |
| 54 | (VD_SET_PRIMITIVE_FIELD_AND_NOTIFY(field, (value)) ? ((flag) = true, true) : false) |
Doris Liu | 32d7cda | 2016-04-08 13:48:47 -0700 | [diff] [blame] | 55 | #define VD_SET_PROP(field, value) ((value) != (field) ? ((field) = (value), true) : false) |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 56 | #define VD_SET_PRIMITIVE_FIELD_AND_NOTIFY(field, value) \ |
| 57 | ({ \ |
| 58 | bool retVal = VD_SET_PROP((mPrimitiveFields.field), (value)); \ |
| 59 | onPropertyChanged(); \ |
| 60 | retVal; \ |
| 61 | }) |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 62 | |
| 63 | /* A VectorDrawable is composed of a tree of nodes. |
| 64 | * Each node can be a group node, or a path. |
| 65 | * A group node can have groups or paths as children, but a path node has |
| 66 | * no children. |
| 67 | * One example can be: |
| 68 | * Root Group |
| 69 | * / | \ |
| 70 | * Group Path Group |
| 71 | * / \ | |
| 72 | * Path Path Path |
| 73 | * |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 74 | * VectorDrawables are drawn into bitmap caches first, then the caches are drawn to the given |
| 75 | * canvas with root alpha applied. Two caches are maintained for VD, one in UI thread, the other in |
| 76 | * Render Thread. A generation id is used to keep track of changes in the vector drawable tree. |
| 77 | * Each cache has their own generation id to track whether they are up to date with the latest |
| 78 | * change in the tree. |
| 79 | * |
| 80 | * Any property change to the vector drawable coming from UI thread (such as bulk setters to update |
| 81 | * all the properties, and viewport change, etc.) are only modifying the staging properties. The |
| 82 | * staging properties will then be marked dirty and will be pushed over to render thread properties |
| 83 | * at sync point. If staging properties are not dirty at sync point, we sync backwards by updating |
| 84 | * staging properties with render thread properties to reflect the latest animation value. |
| 85 | * |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 86 | */ |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 87 | |
| 88 | class PropertyChangedListener { |
| 89 | public: |
| 90 | PropertyChangedListener(bool* dirty, bool* stagingDirty) |
| 91 | : mDirty(dirty), mStagingDirty(stagingDirty) {} |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 92 | void onPropertyChanged() { *mDirty = true; } |
| 93 | void onStagingPropertyChanged() { *mStagingDirty = true; } |
| 94 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 95 | private: |
| 96 | bool* mDirty; |
| 97 | bool* mStagingDirty; |
| 98 | }; |
| 99 | |
Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame^] | 100 | class Node { |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 101 | public: |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 102 | class Properties { |
| 103 | public: |
Chih-Hung Hsieh | a619ec7 | 2016-08-29 14:52:43 -0700 | [diff] [blame] | 104 | explicit Properties(Node* node) : mNode(node) {} |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 105 | inline void onPropertyChanged() { mNode->onPropertyChanged(this); } |
| 106 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 107 | private: |
| 108 | Node* mNode; |
| 109 | }; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 110 | Node(const Node& node) { mName = node.mName; } |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 111 | Node() {} |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame] | 112 | virtual void draw(SkCanvas* outCanvas, bool useStagingData) = 0; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 113 | virtual void dump() = 0; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 114 | void setName(const char* name) { mName = name; } |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 115 | virtual void setPropertyChangedListener(PropertyChangedListener* listener) { |
| 116 | mPropertyChangedListener = listener; |
| 117 | } |
| 118 | virtual void onPropertyChanged(Properties* properties) = 0; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 119 | virtual ~Node() {} |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 120 | virtual void syncProperties() = 0; |
Doris Liu | 6b184d7 | 2017-12-04 16:31:07 -0800 | [diff] [blame] | 121 | virtual void setAntiAlias(bool aa) = 0; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 122 | |
John Reck | 08ee815 | 2018-09-20 16:27:46 -0700 | [diff] [blame] | 123 | virtual void forEachFillColor(const std::function<void(SkColor)>& func) const { } |
| 124 | |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 125 | protected: |
| 126 | std::string mName; |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 127 | PropertyChangedListener* mPropertyChangedListener = nullptr; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 128 | }; |
| 129 | |
Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame^] | 130 | class Path : public Node { |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 131 | public: |
Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame^] | 132 | struct Data { |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 133 | std::vector<char> verbs; |
| 134 | std::vector<size_t> verbSizes; |
| 135 | std::vector<float> points; |
| 136 | bool operator==(const Data& data) const { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 137 | return verbs == data.verbs && verbSizes == data.verbSizes && points == data.points; |
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 | |
| 141 | class PathProperties : public Properties { |
| 142 | public: |
Chih-Hung Hsieh | a619ec7 | 2016-08-29 14:52:43 -0700 | [diff] [blame] | 143 | explicit PathProperties(Node* node) : Properties(node) {} |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 144 | void syncProperties(const PathProperties& prop) { |
| 145 | mData = prop.mData; |
| 146 | onPropertyChanged(); |
| 147 | } |
| 148 | void setData(const Data& data) { |
| 149 | // Updates the path data. Note that we don't generate a new Skia path right away |
| 150 | // because there are cases where the animation is changing the path data, but the view |
| 151 | // that hosts the VD has gone off screen, in which case we won't even draw. So we |
| 152 | // postpone the Skia path generation to the draw time. |
| 153 | if (data == mData) { |
| 154 | return; |
| 155 | } |
| 156 | mData = data; |
| 157 | onPropertyChanged(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 158 | } |
| 159 | const Data& getData() const { return mData; } |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 160 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 161 | private: |
| 162 | Data mData; |
| 163 | }; |
| 164 | |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 165 | Path(const Path& path); |
| 166 | Path(const char* path, size_t strLength); |
| 167 | Path() {} |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 168 | |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 169 | void dump() override; |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 170 | virtual void syncProperties() override; |
| 171 | virtual void onPropertyChanged(Properties* prop) override { |
| 172 | if (prop == &mStagingProperties) { |
| 173 | mStagingPropertiesDirty = true; |
| 174 | if (mPropertyChangedListener) { |
| 175 | mPropertyChangedListener->onStagingPropertyChanged(); |
| 176 | } |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 177 | } else if (prop == &mProperties) { |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 178 | mSkPathDirty = true; |
| 179 | if (mPropertyChangedListener) { |
| 180 | mPropertyChangedListener->onPropertyChanged(); |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | PathProperties* mutateStagingProperties() { return &mStagingProperties; } |
| 185 | const PathProperties* stagingProperties() { return &mStagingProperties; } |
| 186 | |
| 187 | // This should only be called from animations on RT |
| 188 | PathProperties* mutateProperties() { return &mProperties; } |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 189 | |
| 190 | protected: |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame] | 191 | virtual const SkPath& getUpdatedPath(bool useStagingData, SkPath* tempStagingPath); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 192 | |
| 193 | // Internal data, render thread only. |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 194 | bool mSkPathDirty = true; |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 195 | SkPath mSkPath; |
| 196 | |
| 197 | private: |
| 198 | PathProperties mProperties = PathProperties(this); |
| 199 | PathProperties mStagingProperties = PathProperties(this); |
| 200 | bool mStagingPropertiesDirty = true; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 201 | }; |
| 202 | |
Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame^] | 203 | class FullPath : public Path { |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 204 | public: |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 205 | class FullPathProperties : public Properties { |
| 206 | public: |
| 207 | struct PrimitiveFields { |
| 208 | float strokeWidth = 0; |
| 209 | SkColor strokeColor = SK_ColorTRANSPARENT; |
| 210 | float strokeAlpha = 1; |
| 211 | SkColor fillColor = SK_ColorTRANSPARENT; |
| 212 | float fillAlpha = 1; |
| 213 | float trimPathStart = 0; |
| 214 | float trimPathEnd = 1; |
| 215 | float trimPathOffset = 0; |
| 216 | int32_t strokeLineCap = SkPaint::Cap::kButt_Cap; |
| 217 | int32_t strokeLineJoin = SkPaint::Join::kMiter_Join; |
| 218 | float strokeMiterLimit = 4; |
| 219 | int fillType = 0; /* non-zero or kWinding_FillType in Skia */ |
| 220 | }; |
Chih-Hung Hsieh | a619ec7 | 2016-08-29 14:52:43 -0700 | [diff] [blame] | 221 | explicit FullPathProperties(Node* mNode) : Properties(mNode), mTrimDirty(false) {} |
Ben Wagner | c1a8a46 | 2018-07-12 12:41:28 -0400 | [diff] [blame] | 222 | ~FullPathProperties() {} |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 223 | void syncProperties(const FullPathProperties& prop) { |
| 224 | mPrimitiveFields = prop.mPrimitiveFields; |
| 225 | mTrimDirty = true; |
Ben Wagner | c1a8a46 | 2018-07-12 12:41:28 -0400 | [diff] [blame] | 226 | fillGradient = prop.fillGradient; |
| 227 | strokeGradient = prop.strokeGradient; |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 228 | onPropertyChanged(); |
| 229 | } |
| 230 | void setFillGradient(SkShader* gradient) { |
Ben Wagner | c1a8a46 | 2018-07-12 12:41:28 -0400 | [diff] [blame] | 231 | if (fillGradient.get() != gradient) { |
| 232 | fillGradient = sk_ref_sp(gradient); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 233 | onPropertyChanged(); |
| 234 | } |
| 235 | } |
| 236 | void setStrokeGradient(SkShader* gradient) { |
Ben Wagner | c1a8a46 | 2018-07-12 12:41:28 -0400 | [diff] [blame] | 237 | if (strokeGradient.get() != gradient) { |
| 238 | strokeGradient = sk_ref_sp(gradient); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 239 | onPropertyChanged(); |
| 240 | } |
| 241 | } |
Ben Wagner | c1a8a46 | 2018-07-12 12:41:28 -0400 | [diff] [blame] | 242 | SkShader* getFillGradient() const { return fillGradient.get(); } |
| 243 | SkShader* getStrokeGradient() const { return strokeGradient.get(); } |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 244 | float getStrokeWidth() const { return mPrimitiveFields.strokeWidth; } |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 245 | void setStrokeWidth(float strokeWidth) { |
Doris Liu | 32d7cda | 2016-04-08 13:48:47 -0700 | [diff] [blame] | 246 | VD_SET_PRIMITIVE_FIELD_AND_NOTIFY(strokeWidth, strokeWidth); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 247 | } |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 248 | SkColor getStrokeColor() const { return mPrimitiveFields.strokeColor; } |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 249 | void setStrokeColor(SkColor strokeColor) { |
Doris Liu | 32d7cda | 2016-04-08 13:48:47 -0700 | [diff] [blame] | 250 | VD_SET_PRIMITIVE_FIELD_AND_NOTIFY(strokeColor, strokeColor); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 251 | } |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 252 | float getStrokeAlpha() const { return mPrimitiveFields.strokeAlpha; } |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 253 | void setStrokeAlpha(float strokeAlpha) { |
Doris Liu | 32d7cda | 2016-04-08 13:48:47 -0700 | [diff] [blame] | 254 | VD_SET_PRIMITIVE_FIELD_AND_NOTIFY(strokeAlpha, strokeAlpha); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 255 | } |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 256 | SkColor getFillColor() const { return mPrimitiveFields.fillColor; } |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 257 | void setFillColor(SkColor fillColor) { |
Doris Liu | 32d7cda | 2016-04-08 13:48:47 -0700 | [diff] [blame] | 258 | VD_SET_PRIMITIVE_FIELD_AND_NOTIFY(fillColor, fillColor); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 259 | } |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 260 | float getFillAlpha() const { return mPrimitiveFields.fillAlpha; } |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 261 | void setFillAlpha(float fillAlpha) { |
Doris Liu | 32d7cda | 2016-04-08 13:48:47 -0700 | [diff] [blame] | 262 | VD_SET_PRIMITIVE_FIELD_AND_NOTIFY(fillAlpha, fillAlpha); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 263 | } |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 264 | float getTrimPathStart() const { return mPrimitiveFields.trimPathStart; } |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 265 | void setTrimPathStart(float trimPathStart) { |
Doris Liu | 32d7cda | 2016-04-08 13:48:47 -0700 | [diff] [blame] | 266 | VD_SET_PRIMITIVE_FIELD_WITH_FLAG(trimPathStart, trimPathStart, mTrimDirty); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 267 | } |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 268 | float getTrimPathEnd() const { return mPrimitiveFields.trimPathEnd; } |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 269 | void setTrimPathEnd(float trimPathEnd) { |
Doris Liu | 32d7cda | 2016-04-08 13:48:47 -0700 | [diff] [blame] | 270 | VD_SET_PRIMITIVE_FIELD_WITH_FLAG(trimPathEnd, trimPathEnd, mTrimDirty); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 271 | } |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 272 | float getTrimPathOffset() const { return mPrimitiveFields.trimPathOffset; } |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 273 | void setTrimPathOffset(float trimPathOffset) { |
Doris Liu | 32d7cda | 2016-04-08 13:48:47 -0700 | [diff] [blame] | 274 | VD_SET_PRIMITIVE_FIELD_WITH_FLAG(trimPathOffset, trimPathOffset, mTrimDirty); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 275 | } |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 276 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 277 | float getStrokeMiterLimit() const { return mPrimitiveFields.strokeMiterLimit; } |
| 278 | float getStrokeLineCap() const { return mPrimitiveFields.strokeLineCap; } |
| 279 | float getStrokeLineJoin() const { return mPrimitiveFields.strokeLineJoin; } |
| 280 | float getFillType() const { return mPrimitiveFields.fillType; } |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 281 | bool copyProperties(int8_t* outProperties, int length) const; |
| 282 | void updateProperties(float strokeWidth, SkColor strokeColor, float strokeAlpha, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 283 | SkColor fillColor, float fillAlpha, float trimPathStart, |
| 284 | float trimPathEnd, float trimPathOffset, float strokeMiterLimit, |
| 285 | int strokeLineCap, int strokeLineJoin, int fillType) { |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 286 | mPrimitiveFields.strokeWidth = strokeWidth; |
| 287 | mPrimitiveFields.strokeColor = strokeColor; |
| 288 | mPrimitiveFields.strokeAlpha = strokeAlpha; |
| 289 | mPrimitiveFields.fillColor = fillColor; |
| 290 | mPrimitiveFields.fillAlpha = fillAlpha; |
| 291 | mPrimitiveFields.trimPathStart = trimPathStart; |
| 292 | mPrimitiveFields.trimPathEnd = trimPathEnd; |
| 293 | mPrimitiveFields.trimPathOffset = trimPathOffset; |
| 294 | mPrimitiveFields.strokeMiterLimit = strokeMiterLimit; |
| 295 | mPrimitiveFields.strokeLineCap = strokeLineCap; |
| 296 | mPrimitiveFields.strokeLineJoin = strokeLineJoin; |
| 297 | mPrimitiveFields.fillType = fillType; |
| 298 | mTrimDirty = true; |
| 299 | onPropertyChanged(); |
| 300 | } |
| 301 | // Set property values during animation |
| 302 | void setColorPropertyValue(int propertyId, int32_t value); |
| 303 | void setPropertyValue(int propertyId, float value); |
| 304 | bool mTrimDirty; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 305 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 306 | private: |
| 307 | enum class Property { |
| 308 | strokeWidth = 0, |
| 309 | strokeColor, |
| 310 | strokeAlpha, |
| 311 | fillColor, |
| 312 | fillAlpha, |
| 313 | trimPathStart, |
| 314 | trimPathEnd, |
| 315 | trimPathOffset, |
| 316 | strokeLineCap, |
| 317 | strokeLineJoin, |
| 318 | strokeMiterLimit, |
| 319 | fillType, |
| 320 | count, |
| 321 | }; |
| 322 | PrimitiveFields mPrimitiveFields; |
Ben Wagner | c1a8a46 | 2018-07-12 12:41:28 -0400 | [diff] [blame] | 323 | sk_sp<SkShader> fillGradient; |
| 324 | sk_sp<SkShader> strokeGradient; |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 325 | }; |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 326 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 327 | // Called from UI thread |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 328 | FullPath(const FullPath& path); // for cloning |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 329 | FullPath(const char* path, size_t strLength) : Path(path, strLength) {} |
| 330 | FullPath() : Path() {} |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame] | 331 | void draw(SkCanvas* outCanvas, bool useStagingData) override; |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 332 | void dump() override; |
| 333 | FullPathProperties* mutateStagingProperties() { return &mStagingProperties; } |
| 334 | const FullPathProperties* stagingProperties() { return &mStagingProperties; } |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 335 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 336 | // This should only be called from animations on RT |
| 337 | FullPathProperties* mutateProperties() { return &mProperties; } |
Teng-Hui Zhu | dbee9bb | 2015-12-15 11:01:27 -0800 | [diff] [blame] | 338 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 339 | virtual void syncProperties() override; |
| 340 | virtual void onPropertyChanged(Properties* properties) override { |
| 341 | Path::onPropertyChanged(properties); |
| 342 | if (properties == &mStagingProperties) { |
| 343 | mStagingPropertiesDirty = true; |
| 344 | if (mPropertyChangedListener) { |
| 345 | mPropertyChangedListener->onStagingPropertyChanged(); |
| 346 | } |
| 347 | } else if (properties == &mProperties) { |
| 348 | if (mPropertyChangedListener) { |
| 349 | mPropertyChangedListener->onPropertyChanged(); |
| 350 | } |
| 351 | } |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 352 | } |
Doris Liu | 6b184d7 | 2017-12-04 16:31:07 -0800 | [diff] [blame] | 353 | virtual void setAntiAlias(bool aa) { mAntiAlias = aa; } |
John Reck | 08ee815 | 2018-09-20 16:27:46 -0700 | [diff] [blame] | 354 | void forEachFillColor(const std::function<void(SkColor)>& func) const override { |
| 355 | func(mStagingProperties.getFillColor()); |
| 356 | } |
Teng-Hui Zhu | dbee9bb | 2015-12-15 11:01:27 -0800 | [diff] [blame] | 357 | |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 358 | protected: |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame] | 359 | const SkPath& getUpdatedPath(bool useStagingData, SkPath* tempStagingPath) override; |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 360 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 361 | private: |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 362 | FullPathProperties mProperties = FullPathProperties(this); |
| 363 | FullPathProperties mStagingProperties = FullPathProperties(this); |
| 364 | bool mStagingPropertiesDirty = true; |
| 365 | |
| 366 | // Intermediate data for drawing, render thread only |
Doris Liu | 5a11e8d | 2016-02-04 20:04:10 +0000 | [diff] [blame] | 367 | SkPath mTrimmedSkPath; |
Doris Liu | 6b184d7 | 2017-12-04 16:31:07 -0800 | [diff] [blame] | 368 | // Default to use AntiAlias |
| 369 | bool mAntiAlias = true; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 370 | }; |
| 371 | |
Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame^] | 372 | class ClipPath : public Path { |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 373 | public: |
| 374 | ClipPath(const ClipPath& path) : Path(path) {} |
| 375 | ClipPath(const char* path, size_t strLength) : Path(path, strLength) {} |
| 376 | ClipPath() : Path() {} |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame] | 377 | void draw(SkCanvas* outCanvas, bool useStagingData) override; |
Doris Liu | 6b184d7 | 2017-12-04 16:31:07 -0800 | [diff] [blame] | 378 | virtual void setAntiAlias(bool aa) {} |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 379 | }; |
| 380 | |
Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame^] | 381 | class Group : public Node { |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 382 | public: |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 383 | class GroupProperties : public Properties { |
| 384 | public: |
Chih-Hung Hsieh | a619ec7 | 2016-08-29 14:52:43 -0700 | [diff] [blame] | 385 | explicit GroupProperties(Node* mNode) : Properties(mNode) {} |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 386 | struct PrimitiveFields { |
| 387 | float rotate = 0; |
| 388 | float pivotX = 0; |
| 389 | float pivotY = 0; |
| 390 | float scaleX = 1; |
| 391 | float scaleY = 1; |
| 392 | float translateX = 0; |
| 393 | float translateY = 0; |
| 394 | } mPrimitiveFields; |
| 395 | void syncProperties(const GroupProperties& prop) { |
| 396 | mPrimitiveFields = prop.mPrimitiveFields; |
| 397 | onPropertyChanged(); |
| 398 | } |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 399 | float getRotation() const { return mPrimitiveFields.rotate; } |
| 400 | void setRotation(float rotation) { VD_SET_PRIMITIVE_FIELD_AND_NOTIFY(rotate, rotation); } |
| 401 | float getPivotX() const { return mPrimitiveFields.pivotX; } |
| 402 | void setPivotX(float pivotX) { VD_SET_PRIMITIVE_FIELD_AND_NOTIFY(pivotX, pivotX); } |
| 403 | float getPivotY() const { return mPrimitiveFields.pivotY; } |
| 404 | void setPivotY(float pivotY) { VD_SET_PRIMITIVE_FIELD_AND_NOTIFY(pivotY, pivotY); } |
| 405 | float getScaleX() const { return mPrimitiveFields.scaleX; } |
| 406 | void setScaleX(float scaleX) { VD_SET_PRIMITIVE_FIELD_AND_NOTIFY(scaleX, scaleX); } |
| 407 | float getScaleY() const { return mPrimitiveFields.scaleY; } |
| 408 | void setScaleY(float scaleY) { VD_SET_PRIMITIVE_FIELD_AND_NOTIFY(scaleY, scaleY); } |
| 409 | float getTranslateX() const { return mPrimitiveFields.translateX; } |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 410 | void setTranslateX(float translateX) { |
Doris Liu | 32d7cda | 2016-04-08 13:48:47 -0700 | [diff] [blame] | 411 | VD_SET_PRIMITIVE_FIELD_AND_NOTIFY(translateX, translateX); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 412 | } |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 413 | float getTranslateY() const { return mPrimitiveFields.translateY; } |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 414 | void setTranslateY(float translateY) { |
Doris Liu | 32d7cda | 2016-04-08 13:48:47 -0700 | [diff] [blame] | 415 | VD_SET_PRIMITIVE_FIELD_AND_NOTIFY(translateY, translateY); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 416 | } |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 417 | void updateProperties(float rotate, float pivotX, float pivotY, float scaleX, float scaleY, |
| 418 | float translateX, float translateY) { |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 419 | mPrimitiveFields.rotate = rotate; |
| 420 | mPrimitiveFields.pivotX = pivotX; |
| 421 | mPrimitiveFields.pivotY = pivotY; |
| 422 | mPrimitiveFields.scaleX = scaleX; |
| 423 | mPrimitiveFields.scaleY = scaleY; |
| 424 | mPrimitiveFields.translateX = translateX; |
| 425 | mPrimitiveFields.translateY = translateY; |
| 426 | onPropertyChanged(); |
| 427 | } |
| 428 | void setPropertyValue(int propertyId, float value); |
| 429 | float getPropertyValue(int propertyId) const; |
| 430 | bool copyProperties(float* outProperties, int length) const; |
| 431 | static bool isValidProperty(int propertyId); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 432 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 433 | private: |
| 434 | enum class Property { |
| 435 | rotate = 0, |
| 436 | pivotX, |
| 437 | pivotY, |
| 438 | scaleX, |
| 439 | scaleY, |
| 440 | translateX, |
| 441 | translateY, |
| 442 | // Count of the properties, must be at the end. |
| 443 | count, |
| 444 | }; |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 445 | }; |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 446 | |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 447 | Group(const Group& group); |
| 448 | Group() {} |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 449 | void addChild(Node* child); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 450 | virtual void setPropertyChangedListener(PropertyChangedListener* listener) override { |
| 451 | Node::setPropertyChangedListener(listener); |
| 452 | for (auto& child : mChildren) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 453 | child->setPropertyChangedListener(listener); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | virtual void syncProperties() override; |
| 457 | GroupProperties* mutateStagingProperties() { return &mStagingProperties; } |
| 458 | const GroupProperties* stagingProperties() { return &mStagingProperties; } |
| 459 | |
| 460 | // This should only be called from animations on RT |
| 461 | GroupProperties* mutateProperties() { return &mProperties; } |
| 462 | |
| 463 | // Methods below could be called from either UI thread or Render Thread. |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame] | 464 | virtual void draw(SkCanvas* outCanvas, bool useStagingData) override; |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 465 | void getLocalMatrix(SkMatrix* outMatrix, const GroupProperties& properties); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 466 | void dump() override; |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 467 | static bool isValidProperty(int propertyId); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 468 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 469 | virtual void onPropertyChanged(Properties* properties) override { |
| 470 | if (properties == &mStagingProperties) { |
| 471 | mStagingPropertiesDirty = true; |
| 472 | if (mPropertyChangedListener) { |
| 473 | mPropertyChangedListener->onStagingPropertyChanged(); |
| 474 | } |
| 475 | } else { |
| 476 | if (mPropertyChangedListener) { |
| 477 | mPropertyChangedListener->onPropertyChanged(); |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | |
Doris Liu | 6b184d7 | 2017-12-04 16:31:07 -0800 | [diff] [blame] | 482 | virtual void setAntiAlias(bool aa) { |
| 483 | for (auto& child : mChildren) { |
| 484 | child->setAntiAlias(aa); |
| 485 | } |
| 486 | } |
| 487 | |
John Reck | 08ee815 | 2018-09-20 16:27:46 -0700 | [diff] [blame] | 488 | void forEachFillColor(const std::function<void(SkColor)>& func) const override { |
| 489 | for (auto& child : mChildren) { |
| 490 | child->forEachFillColor(func); |
| 491 | } |
| 492 | } |
| 493 | |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 494 | private: |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 495 | GroupProperties mProperties = GroupProperties(this); |
| 496 | GroupProperties mStagingProperties = GroupProperties(this); |
| 497 | bool mStagingPropertiesDirty = true; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 498 | std::vector<std::unique_ptr<Node> > mChildren; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 499 | }; |
| 500 | |
Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame^] | 501 | class Tree : public VirtualLightRefBase { |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 502 | public: |
Chih-Hung Hsieh | a619ec7 | 2016-08-29 14:52:43 -0700 | [diff] [blame] | 503 | explicit Tree(Group* rootNode) : mRootNode(rootNode) { |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 504 | mRootNode->setPropertyChangedListener(&mPropertyChangedListener); |
| 505 | } |
Doris Liu | 335d7d1 | 2016-05-26 15:19:15 -0700 | [diff] [blame] | 506 | |
| 507 | // Copy properties from the tree and use the give node as the root node |
| 508 | Tree(const Tree* copy, Group* rootNode) : Tree(rootNode) { |
John Reck | 08ee815 | 2018-09-20 16:27:46 -0700 | [diff] [blame] | 509 | mStagingProperties.syncAnimatableProperties(copy->stagingProperties()); |
| 510 | mStagingProperties.syncNonAnimatableProperties(copy->stagingProperties()); |
Doris Liu | 335d7d1 | 2016-05-26 15:19:15 -0700 | [diff] [blame] | 511 | } |
Doris Liu | f8d131c | 2016-04-29 18:41:29 -0700 | [diff] [blame] | 512 | // Draws the VD onto a bitmap cache, then the bitmap cache will be rendered onto the input |
| 513 | // canvas. Returns the number of pixels needed for the bitmap cache. |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 514 | int draw(Canvas* outCanvas, SkColorFilter* colorFilter, const SkRect& bounds, |
| 515 | bool needsMirroring, bool canReuseCache); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 516 | void drawStaging(Canvas* canvas); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 517 | |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 518 | Bitmap& getBitmapUpdateIfDirty(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 519 | void setAllowCaching(bool allowCaching) { mAllowCaching = allowCaching; } |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 520 | void syncProperties() { |
| 521 | if (mStagingProperties.mNonAnimatablePropertiesDirty) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 522 | mCache.dirty |= (mProperties.mNonAnimatableProperties.viewportWidth != |
| 523 | mStagingProperties.mNonAnimatableProperties.viewportWidth) || |
| 524 | (mProperties.mNonAnimatableProperties.viewportHeight != |
| 525 | mStagingProperties.mNonAnimatableProperties.viewportHeight) || |
| 526 | (mProperties.mNonAnimatableProperties.scaledWidth != |
| 527 | mStagingProperties.mNonAnimatableProperties.scaledWidth) || |
| 528 | (mProperties.mNonAnimatableProperties.scaledHeight != |
| 529 | mStagingProperties.mNonAnimatableProperties.scaledHeight) || |
| 530 | (mProperties.mNonAnimatableProperties.bounds != |
| 531 | mStagingProperties.mNonAnimatableProperties.bounds); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 532 | mProperties.syncNonAnimatableProperties(mStagingProperties); |
| 533 | mStagingProperties.mNonAnimatablePropertiesDirty = false; |
| 534 | } |
| 535 | |
| 536 | if (mStagingProperties.mAnimatablePropertiesDirty) { |
| 537 | mProperties.syncAnimatableProperties(mStagingProperties); |
| 538 | } else { |
| 539 | mStagingProperties.syncAnimatableProperties(mProperties); |
| 540 | } |
| 541 | mStagingProperties.mAnimatablePropertiesDirty = false; |
| 542 | mRootNode->syncProperties(); |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 543 | } |
| 544 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 545 | class TreeProperties { |
| 546 | public: |
Chih-Hung Hsieh | a619ec7 | 2016-08-29 14:52:43 -0700 | [diff] [blame] | 547 | explicit TreeProperties(Tree* tree) : mTree(tree) {} |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 548 | // Properties that can only be modified by UI thread, therefore sync should |
| 549 | // only go from UI to RT |
| 550 | struct NonAnimatableProperties { |
| 551 | float viewportWidth = 0; |
| 552 | float viewportHeight = 0; |
| 553 | SkRect bounds; |
| 554 | int scaledWidth = 0; |
| 555 | int scaledHeight = 0; |
Ben Wagner | c1a8a46 | 2018-07-12 12:41:28 -0400 | [diff] [blame] | 556 | sk_sp<SkColorFilter> colorFilter; |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 557 | } mNonAnimatableProperties; |
| 558 | bool mNonAnimatablePropertiesDirty = true; |
| 559 | |
| 560 | float mRootAlpha = 1.0f; |
| 561 | bool mAnimatablePropertiesDirty = true; |
| 562 | |
| 563 | void syncNonAnimatableProperties(const TreeProperties& prop) { |
| 564 | // Copy over the data that can only be changed in UI thread |
| 565 | if (mNonAnimatableProperties.colorFilter != prop.mNonAnimatableProperties.colorFilter) { |
Ben Wagner | c1a8a46 | 2018-07-12 12:41:28 -0400 | [diff] [blame] | 566 | mNonAnimatableProperties.colorFilter = prop.mNonAnimatableProperties.colorFilter; |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 567 | } |
| 568 | mNonAnimatableProperties = prop.mNonAnimatableProperties; |
| 569 | } |
| 570 | |
| 571 | void setViewportSize(float width, float height) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 572 | if (mNonAnimatableProperties.viewportWidth != width || |
| 573 | mNonAnimatableProperties.viewportHeight != height) { |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 574 | mNonAnimatablePropertiesDirty = true; |
| 575 | mNonAnimatableProperties.viewportWidth = width; |
| 576 | mNonAnimatableProperties.viewportHeight = height; |
| 577 | mTree->onPropertyChanged(this); |
| 578 | } |
| 579 | } |
| 580 | void setBounds(const SkRect& bounds) { |
| 581 | if (mNonAnimatableProperties.bounds != bounds) { |
| 582 | mNonAnimatableProperties.bounds = bounds; |
| 583 | mNonAnimatablePropertiesDirty = true; |
| 584 | mTree->onPropertyChanged(this); |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | void setScaledSize(int width, int height) { |
Teng-Hui Zhu | 037fc18 | 2016-11-16 10:29:39 -0800 | [diff] [blame] | 589 | // If the requested size is bigger than what the bitmap was, then |
| 590 | // we increase the bitmap size to match. The width and height |
| 591 | // are bound by MAX_CACHED_BITMAP_SIZE. |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 592 | if (mNonAnimatableProperties.scaledWidth < width || |
| 593 | mNonAnimatableProperties.scaledHeight < height) { |
| 594 | mNonAnimatableProperties.scaledWidth = |
| 595 | std::max(width, mNonAnimatableProperties.scaledWidth); |
| 596 | mNonAnimatableProperties.scaledHeight = |
| 597 | std::max(height, mNonAnimatableProperties.scaledHeight); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 598 | mNonAnimatablePropertiesDirty = true; |
| 599 | mTree->onPropertyChanged(this); |
| 600 | } |
| 601 | } |
| 602 | void setColorFilter(SkColorFilter* filter) { |
Ben Wagner | c1a8a46 | 2018-07-12 12:41:28 -0400 | [diff] [blame] | 603 | if (mNonAnimatableProperties.colorFilter.get() != filter) { |
| 604 | mNonAnimatableProperties.colorFilter = sk_ref_sp(filter); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 605 | mNonAnimatablePropertiesDirty = true; |
| 606 | mTree->onPropertyChanged(this); |
| 607 | } |
| 608 | } |
Ben Wagner | c1a8a46 | 2018-07-12 12:41:28 -0400 | [diff] [blame] | 609 | SkColorFilter* getColorFilter() const { return mNonAnimatableProperties.colorFilter.get(); } |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 610 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 611 | float getViewportWidth() const { return mNonAnimatableProperties.viewportWidth; } |
| 612 | float getViewportHeight() const { return mNonAnimatableProperties.viewportHeight; } |
| 613 | float getScaledWidth() const { return mNonAnimatableProperties.scaledWidth; } |
| 614 | float getScaledHeight() const { return mNonAnimatableProperties.scaledHeight; } |
| 615 | void syncAnimatableProperties(const TreeProperties& prop) { mRootAlpha = prop.mRootAlpha; } |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 616 | bool setRootAlpha(float rootAlpha) { |
| 617 | if (rootAlpha != mRootAlpha) { |
| 618 | mAnimatablePropertiesDirty = true; |
| 619 | mRootAlpha = rootAlpha; |
| 620 | mTree->onPropertyChanged(this); |
| 621 | return true; |
| 622 | } |
| 623 | return false; |
| 624 | } |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 625 | float getRootAlpha() const { return mRootAlpha; } |
| 626 | const SkRect& getBounds() const { return mNonAnimatableProperties.bounds; } |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 627 | Tree* mTree; |
| 628 | }; |
| 629 | void onPropertyChanged(TreeProperties* prop); |
| 630 | TreeProperties* mutateStagingProperties() { return &mStagingProperties; } |
John Reck | 08ee815 | 2018-09-20 16:27:46 -0700 | [diff] [blame] | 631 | const TreeProperties& stagingProperties() const { return mStagingProperties; } |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 632 | |
| 633 | // This should only be called from animations on RT |
| 634 | TreeProperties* mutateProperties() { return &mProperties; } |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 635 | |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 636 | // called from RT only |
| 637 | const TreeProperties& properties() const { return mProperties; } |
| 638 | |
Doris Liu | 67ce99b | 2016-05-17 16:50:31 -0700 | [diff] [blame] | 639 | // This should always be called from RT. |
Doris Liu | 7c7052d | 2016-07-25 17:19:24 -0700 | [diff] [blame] | 640 | void markDirty() { mCache.dirty = true; } |
Doris Liu | 67ce99b | 2016-05-17 16:50:31 -0700 | [diff] [blame] | 641 | bool isDirty() const { return mCache.dirty; } |
| 642 | bool getPropertyChangeWillBeConsumed() const { return mWillBeConsumed; } |
| 643 | void setPropertyChangeWillBeConsumed(bool willBeConsumed) { mWillBeConsumed = willBeConsumed; } |
| 644 | |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 645 | /** |
| 646 | * Draws VD cache into a canvas. This should always be called from RT and it works with Skia |
| 647 | * pipelines only. |
| 648 | */ |
John Reck | 08ee815 | 2018-09-20 16:27:46 -0700 | [diff] [blame] | 649 | void draw(SkCanvas* canvas, const SkRect& bounds, const SkPaint& paint); |
| 650 | |
| 651 | void getPaintFor(SkPaint* outPaint, const TreeProperties &props) const; |
| 652 | BitmapPalette computePalette(); |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 653 | |
Doris Liu | 6b184d7 | 2017-12-04 16:31:07 -0800 | [diff] [blame] | 654 | void setAntiAlias(bool aa) { mRootNode->setAntiAlias(aa); } |
| 655 | |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 656 | private: |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 657 | class Cache { |
| 658 | public: |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 659 | sk_sp<Bitmap> bitmap; // used by HWUI pipeline and software |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 660 | bool dirty = true; |
| 661 | }; |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 662 | |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 663 | bool allocateBitmapIfNeeded(Cache& cache, int width, int height); |
| 664 | bool canReuseBitmap(Bitmap*, int width, int height); |
| 665 | void updateBitmapCache(Bitmap& outCache, bool useStagingData); |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 666 | |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 667 | // Cap the bitmap size, such that it won't hurt the performance too much |
| 668 | // and it won't crash due to a very large scale. |
| 669 | // The drawable will look blurry above this size. |
| 670 | const static int MAX_CACHED_BITMAP_SIZE; |
| 671 | |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 672 | bool mAllowCaching = true; |
Doris Liu | ef062eb | 2016-02-04 16:16:27 -0800 | [diff] [blame] | 673 | std::unique_ptr<Group> mRootNode; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 674 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 675 | TreeProperties mProperties = TreeProperties(this); |
| 676 | TreeProperties mStagingProperties = TreeProperties(this); |
| 677 | |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 678 | Cache mStagingCache; |
| 679 | Cache mCache; |
| 680 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 681 | PropertyChangedListener mPropertyChangedListener = |
| 682 | PropertyChangedListener(&mCache.dirty, &mStagingCache.dirty); |
Doris Liu | 67ce99b | 2016-05-17 16:50:31 -0700 | [diff] [blame] | 683 | |
| 684 | mutable bool mWillBeConsumed = false; |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 685 | }; |
| 686 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 687 | } // namespace VectorDrawable |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 688 | |
| 689 | typedef VectorDrawable::Path::Data PathData; |
John Reck | d9d7f12 | 2018-05-03 14:40:56 -0700 | [diff] [blame] | 690 | typedef uirenderer::VectorDrawable::Tree VectorDrawableRoot; |
| 691 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 692 | } // namespace uirenderer |
| 693 | } // namespace android |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 694 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 695 | #endif // ANDROID_HWUI_VPATH_H |