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