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