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