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