Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 1 | /* |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 2 | * Copyright (C) 2013 The Android Open Source Project |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 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 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_HWUI_PATH_CACHE_H |
| 18 | #define ANDROID_HWUI_PATH_CACHE_H |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 19 | |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 20 | #include "Debug.h" |
| 21 | #include "Texture.h" |
sergeyv | 98fa4f9 | 2016-10-24 15:35:21 -0700 | [diff] [blame] | 22 | #include "hwui/Bitmap.h" |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 23 | #include "thread/Task.h" |
| 24 | #include "thread/TaskProcessor.h" |
| 25 | #include "utils/Macros.h" |
| 26 | #include "utils/Pair.h" |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 27 | |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 28 | #include <GLES2/gl2.h> |
sergeyv | 7224e2b | 2016-04-07 18:06:53 -0700 | [diff] [blame] | 29 | #include <SkPaint.h> |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 30 | #include <SkPath.h> |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 31 | #include <utils/LruCache.h> |
| 32 | #include <utils/Mutex.h> |
John Reck | 272a685 | 2015-07-29 16:48:58 -0700 | [diff] [blame] | 33 | |
| 34 | #include <vector> |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 35 | |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 36 | class SkCanvas; |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 37 | class SkPaint; |
Chris Craik | 564acf7 | 2014-01-02 16:46:18 -0800 | [diff] [blame] | 38 | struct SkRect; |
Romain Guy | ff26a0c | 2011-01-20 11:35:46 -0800 | [diff] [blame] | 39 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 40 | namespace android { |
| 41 | namespace uirenderer { |
| 42 | |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 43 | class Caches; |
Romain Guy | 9e10841 | 2010-11-09 14:35:20 -0800 | [diff] [blame] | 44 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 45 | // Defines |
| 46 | /////////////////////////////////////////////////////////////////////////////// |
| 47 | |
| 48 | // Debug |
| 49 | #if DEBUG_PATHS |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 50 | #define PATH_LOGD(...) ALOGD(__VA_ARGS__) |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 51 | #else |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 52 | #define PATH_LOGD(...) |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 53 | #endif |
| 54 | |
| 55 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 9e10841 | 2010-11-09 14:35:20 -0800 | [diff] [blame] | 56 | // Classes |
| 57 | /////////////////////////////////////////////////////////////////////////////// |
| 58 | |
sergeyv | 98fa4f9 | 2016-10-24 15:35:21 -0700 | [diff] [blame] | 59 | struct PathTexture; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 60 | class PathTask : public Task<sk_sp<Bitmap>> { |
sergeyv | 98fa4f9 | 2016-10-24 15:35:21 -0700 | [diff] [blame] | 61 | public: |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 62 | PathTask(const SkPath* path, const SkPaint* paint, PathTexture* texture) |
| 63 | : path(*path), paint(*paint), texture(texture) {} |
sergeyv | 98fa4f9 | 2016-10-24 15:35:21 -0700 | [diff] [blame] | 64 | |
| 65 | // copied, since input path not guaranteed to survive for duration of task |
| 66 | const SkPath path; |
| 67 | |
| 68 | // copied, since input paint may not be immutable |
| 69 | const SkPaint paint; |
| 70 | PathTexture* texture; |
| 71 | }; |
| 72 | |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 73 | /** |
| 74 | * Alpha texture used to represent a path. |
| 75 | */ |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 76 | struct PathTexture : public Texture { |
| 77 | PathTexture(Caches& caches, int generation) : Texture(caches) { this->generation = generation; } |
Romain Guy | ff26a0c | 2011-01-20 11:35:46 -0800 | [diff] [blame] | 78 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 79 | ~PathTexture() { clearTask(); } |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 80 | |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 81 | /** |
| 82 | * Left coordinate of the path bounds. |
| 83 | */ |
Chris Craik | e2bb380 | 2015-03-13 15:07:52 -0700 | [diff] [blame] | 84 | float left = 0; |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 85 | /** |
| 86 | * Top coordinate of the path bounds. |
| 87 | */ |
Chris Craik | e2bb380 | 2015-03-13 15:07:52 -0700 | [diff] [blame] | 88 | float top = 0; |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 89 | /** |
| 90 | * Offset to draw the path at the correct origin. |
| 91 | */ |
Chris Craik | e2bb380 | 2015-03-13 15:07:52 -0700 | [diff] [blame] | 92 | float offset = 0; |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 93 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 94 | sp<PathTask> task() const { return mTask; } |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 95 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 96 | void setTask(const sp<PathTask>& task) { mTask = task; } |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 97 | |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 98 | void clearTask() { |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame] | 99 | if (mTask != nullptr) { |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 100 | mTask.clear(); |
| 101 | } |
| 102 | } |
Romain Guy | b29cfbf | 2011-03-18 16:24:19 -0700 | [diff] [blame] | 103 | |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 104 | private: |
sergeyv | 98fa4f9 | 2016-10-24 15:35:21 -0700 | [diff] [blame] | 105 | sp<PathTask> mTask; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 106 | }; // struct PathTexture |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 107 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 108 | enum class ShapeType { None, Rect, RoundRect, Circle, Oval, Arc, Path }; |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 109 | |
| 110 | struct PathDescription { |
sergeyv | 7224e2b | 2016-04-07 18:06:53 -0700 | [diff] [blame] | 111 | HASHABLE_TYPE(PathDescription); |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 112 | ShapeType type; |
| 113 | SkPaint::Join join; |
| 114 | SkPaint::Cap cap; |
| 115 | SkPaint::Style style; |
| 116 | float miter; |
| 117 | float strokeWidth; |
| 118 | SkPathEffect* pathEffect; |
| 119 | union Shape { |
| 120 | struct Path { |
Derek Sollenberger | ee24859 | 2015-02-12 14:10:21 -0500 | [diff] [blame] | 121 | uint32_t mGenerationID; |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 122 | } path; |
| 123 | struct RoundRect { |
| 124 | float mWidth; |
| 125 | float mHeight; |
| 126 | float mRx; |
| 127 | float mRy; |
| 128 | } roundRect; |
| 129 | struct Circle { |
| 130 | float mRadius; |
| 131 | } circle; |
| 132 | struct Oval { |
| 133 | float mWidth; |
| 134 | float mHeight; |
| 135 | } oval; |
| 136 | struct Rect { |
| 137 | float mWidth; |
| 138 | float mHeight; |
| 139 | } rect; |
| 140 | struct Arc { |
| 141 | float mWidth; |
| 142 | float mHeight; |
| 143 | float mStartAngle; |
| 144 | float mSweepAngle; |
| 145 | bool mUseCenter; |
| 146 | } arc; |
| 147 | } shape; |
| 148 | |
| 149 | PathDescription(); |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 150 | PathDescription(ShapeType shapeType, const SkPaint* paint); |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 151 | }; |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 152 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 153 | /** |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 154 | * A simple LRU shape cache. The cache has a maximum size expressed in bytes. |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 155 | * Any texture added to the cache causing the cache to grow beyond the maximum |
| 156 | * allowed size will also cause the oldest texture to be kicked out. |
| 157 | */ |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 158 | class PathCache : public OnEntryRemoved<PathDescription, PathTexture*> { |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 159 | public: |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 160 | PathCache(); |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 161 | ~PathCache(); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 162 | |
| 163 | /** |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 164 | * Used as a callback when an entry is removed from the cache. |
| 165 | * Do not invoke directly. |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 166 | */ |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame] | 167 | void operator()(PathDescription& path, PathTexture*& texture) override; |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 168 | |
| 169 | /** |
| 170 | * Clears the cache. This causes all textures to be deleted. |
| 171 | */ |
| 172 | void clear(); |
| 173 | |
| 174 | /** |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 175 | * Returns the maximum size of the cache in bytes. |
| 176 | */ |
| 177 | uint32_t getMaxSize(); |
| 178 | /** |
| 179 | * Returns the current size of the cache in bytes. |
| 180 | */ |
| 181 | uint32_t getSize(); |
| 182 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 183 | PathTexture* getRoundRect(float width, float height, float rx, float ry, const SkPaint* paint); |
| 184 | PathTexture* getCircle(float radius, const SkPaint* paint); |
| 185 | PathTexture* getOval(float width, float height, const SkPaint* paint); |
| 186 | PathTexture* getRect(float width, float height, const SkPaint* paint); |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 187 | PathTexture* getArc(float width, float height, float startAngle, float sweepAngle, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 188 | bool useCenter, const SkPaint* paint); |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 189 | PathTexture* get(const SkPath* path, const SkPaint* paint); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 190 | void remove(const SkPath* path, const SkPaint* paint); |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 191 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 192 | /** |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 193 | * Removes the specified path. This is meant to be called from threads |
| 194 | * that are not the EGL context thread. |
| 195 | */ |
Derek Sollenberger | ee24859 | 2015-02-12 14:10:21 -0500 | [diff] [blame] | 196 | ANDROID_API void removeDeferred(const SkPath* path); |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 197 | /** |
| 198 | * Process deferred removals. |
| 199 | */ |
| 200 | void clearGarbage(); |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 201 | /** |
| 202 | * Trims the contents of the cache, removing items until it's under its |
| 203 | * specified limit. |
| 204 | * |
| 205 | * Trimming is used for caches that support pre-caching from a worker |
| 206 | * thread. During pre-caching the maximum limit of the cache can be |
| 207 | * exceeded for the duration of the frame. It is therefore required to |
| 208 | * trim the cache at the end of the frame to keep the total amount of |
| 209 | * memory used under control. |
| 210 | */ |
| 211 | void trim(); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 212 | |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 213 | /** |
| 214 | * Precaches the specified path using background threads. |
| 215 | */ |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 216 | void precache(const SkPath* path, const SkPaint* paint); |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 217 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 218 | private: |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 219 | PathTexture* addTexture(const PathDescription& entry, const SkPath* path, const SkPaint* paint); |
Romain Guy | 4500a8d | 2013-03-26 17:29:51 -0700 | [diff] [blame] | 220 | |
| 221 | /** |
| 222 | * Generates the texture from a bitmap into the specified texture structure. |
| 223 | */ |
sergeyv | 98fa4f9 | 2016-10-24 15:35:21 -0700 | [diff] [blame] | 224 | void generateTexture(Bitmap& bitmap, Texture* texture); |
| 225 | void generateTexture(const PathDescription& entry, Bitmap& bitmap, PathTexture* texture, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 226 | bool addToCache = true); |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 227 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 228 | PathTexture* get(const PathDescription& entry) { return mCache.get(entry); } |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 229 | |
| 230 | /** |
| 231 | * Ensures there is enough space in the cache for a texture of the specified |
| 232 | * dimensions. |
| 233 | */ |
| 234 | void purgeCache(uint32_t width, uint32_t height); |
| 235 | |
| 236 | void removeTexture(PathTexture* texture); |
| 237 | |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 238 | void init(); |
| 239 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 240 | class PathProcessor : public TaskProcessor<sk_sp<Bitmap>> { |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 241 | public: |
Chih-Hung Hsieh | faecb78 | 2016-07-21 11:23:06 -0700 | [diff] [blame] | 242 | explicit PathProcessor(Caches& caches); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 243 | ~PathProcessor() {} |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 244 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 245 | virtual void onProcess(const sp<Task<sk_sp<Bitmap>>>& task) override; |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 246 | |
| 247 | private: |
| 248 | uint32_t mMaxTextureSize; |
| 249 | }; |
| 250 | |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 251 | LruCache<PathDescription, PathTexture*> mCache; |
| 252 | uint32_t mSize; |
Chris Craik | 48a8f43 | 2016-02-05 15:59:29 -0800 | [diff] [blame] | 253 | const uint32_t mMaxSize; |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 254 | GLuint mMaxTextureSize; |
| 255 | |
| 256 | bool mDebugEnabled; |
| 257 | |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 258 | sp<PathProcessor> mProcessor; |
Romain Guy | c5cbee7 | 2013-03-20 19:15:02 -0700 | [diff] [blame] | 259 | |
John Reck | 272a685 | 2015-07-29 16:48:58 -0700 | [diff] [blame] | 260 | std::vector<uint32_t> mGarbage; |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 261 | mutable Mutex mLock; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 262 | }; // class PathCache |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 263 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 264 | }; // namespace uirenderer |
| 265 | }; // namespace android |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 266 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 267 | #endif // ANDROID_HWUI_PATH_CACHE_H |