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