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> |
| 28 | #include <SkPath.h> |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 29 | #include <utils/LruCache.h> |
| 30 | #include <utils/Mutex.h> |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 31 | #include <utils/Vector.h> |
| 32 | |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 33 | class SkBitmap; |
| 34 | class SkCanvas; |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 35 | class SkPaint; |
Chris Craik | 564acf7 | 2014-01-02 16:46:18 -0800 | [diff] [blame] | 36 | struct SkRect; |
Romain Guy | ff26a0c | 2011-01-20 11:35:46 -0800 | [diff] [blame] | 37 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 38 | namespace android { |
| 39 | namespace uirenderer { |
| 40 | |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 41 | class Caches; |
| 42 | |
Romain Guy | 9e10841 | 2010-11-09 14:35:20 -0800 | [diff] [blame] | 43 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 44 | // Defines |
| 45 | /////////////////////////////////////////////////////////////////////////////// |
| 46 | |
| 47 | // Debug |
| 48 | #if DEBUG_PATHS |
| 49 | #define PATH_LOGD(...) ALOGD(__VA_ARGS__) |
| 50 | #else |
| 51 | #define PATH_LOGD(...) |
| 52 | #endif |
| 53 | |
| 54 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 9e10841 | 2010-11-09 14:35:20 -0800 | [diff] [blame] | 55 | // Classes |
| 56 | /////////////////////////////////////////////////////////////////////////////// |
| 57 | |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 58 | /** |
| 59 | * Alpha texture used to represent a path. |
| 60 | */ |
| 61 | struct PathTexture: public Texture { |
Chris Craik | e2bb380 | 2015-03-13 15:07:52 -0700 | [diff] [blame^] | 62 | PathTexture(Caches& caches, float left, float top, |
| 63 | float offset, int width, int height, int generation) |
| 64 | : Texture(caches) |
| 65 | , left(left) |
| 66 | , top(top) |
| 67 | , offset(offset) { |
| 68 | this->width = width; |
| 69 | this->height = height; |
| 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 | |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 112 | enum ShapeType { |
| 113 | kShapeNone, |
| 114 | kShapeRect, |
| 115 | kShapeRoundRect, |
| 116 | kShapeCircle, |
| 117 | kShapeOval, |
| 118 | kShapeArc, |
| 119 | kShapePath |
| 120 | }; |
| 121 | |
| 122 | struct PathDescription { |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 123 | DESCRIPTION_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 | |
| 164 | hash_t hash() const; |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 165 | }; |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 166 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 167 | /** |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 168 | * 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] | 169 | * Any texture added to the cache causing the cache to grow beyond the maximum |
| 170 | * allowed size will also cause the oldest texture to be kicked out. |
| 171 | */ |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 172 | class PathCache: public OnEntryRemoved<PathDescription, PathTexture*> { |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 173 | public: |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 174 | PathCache(); |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 175 | ~PathCache(); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 176 | |
| 177 | /** |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 178 | * Used as a callback when an entry is removed from the cache. |
| 179 | * Do not invoke directly. |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 180 | */ |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame] | 181 | void operator()(PathDescription& path, PathTexture*& texture) override; |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 182 | |
| 183 | /** |
| 184 | * Clears the cache. This causes all textures to be deleted. |
| 185 | */ |
| 186 | void clear(); |
| 187 | |
| 188 | /** |
| 189 | * Sets the maximum size of the cache in bytes. |
| 190 | */ |
| 191 | void setMaxSize(uint32_t maxSize); |
| 192 | /** |
| 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); |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 208 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 209 | /** |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 210 | * Removes the specified path. This is meant to be called from threads |
| 211 | * that are not the EGL context thread. |
| 212 | */ |
Derek Sollenberger | ee24859 | 2015-02-12 14:10:21 -0500 | [diff] [blame] | 213 | ANDROID_API void removeDeferred(const SkPath* path); |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 214 | /** |
| 215 | * Process deferred removals. |
| 216 | */ |
| 217 | void clearGarbage(); |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 218 | /** |
| 219 | * Trims the contents of the cache, removing items until it's under its |
| 220 | * specified limit. |
| 221 | * |
| 222 | * Trimming is used for caches that support pre-caching from a worker |
| 223 | * thread. During pre-caching the maximum limit of the cache can be |
| 224 | * exceeded for the duration of the frame. It is therefore required to |
| 225 | * trim the cache at the end of the frame to keep the total amount of |
| 226 | * memory used under control. |
| 227 | */ |
| 228 | void trim(); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 229 | |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 230 | /** |
| 231 | * Precaches the specified path using background threads. |
| 232 | */ |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 233 | void precache(const SkPath* path, const SkPaint* paint); |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 234 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 235 | static bool canDrawAsConvexPath(SkPath* path, const SkPaint* paint); |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 236 | static void computePathBounds(const SkPath* path, const SkPaint* paint, |
| 237 | float& left, float& top, float& offset, uint32_t& width, uint32_t& height); |
| 238 | static void computeBounds(const SkRect& bounds, const SkPaint* paint, |
| 239 | float& left, float& top, float& offset, uint32_t& width, uint32_t& height); |
| 240 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 241 | private: |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 242 | PathTexture* addTexture(const PathDescription& entry, |
| 243 | const SkPath *path, const SkPaint* paint); |
| 244 | PathTexture* addTexture(const PathDescription& entry, SkBitmap* bitmap); |
Romain Guy | 4500a8d | 2013-03-26 17:29:51 -0700 | [diff] [blame] | 245 | |
| 246 | /** |
| 247 | * Generates the texture from a bitmap into the specified texture structure. |
| 248 | */ |
| 249 | void generateTexture(SkBitmap& bitmap, Texture* texture); |
| 250 | void generateTexture(const PathDescription& entry, SkBitmap* bitmap, PathTexture* texture, |
| 251 | bool addToCache = true); |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 252 | |
| 253 | PathTexture* get(const PathDescription& entry) { |
| 254 | return mCache.get(entry); |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Ensures there is enough space in the cache for a texture of the specified |
| 259 | * dimensions. |
| 260 | */ |
| 261 | void purgeCache(uint32_t width, uint32_t height); |
| 262 | |
| 263 | void removeTexture(PathTexture* texture); |
| 264 | |
| 265 | bool checkTextureSize(uint32_t width, uint32_t height) { |
| 266 | if (width > mMaxTextureSize || height > mMaxTextureSize) { |
| 267 | ALOGW("Shape too large to be rendered into a texture (%dx%d, max=%dx%d)", |
| 268 | width, height, mMaxTextureSize, mMaxTextureSize); |
| 269 | return false; |
| 270 | } |
| 271 | return true; |
| 272 | } |
| 273 | |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 274 | void init(); |
| 275 | |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 276 | class PathTask: public Task<SkBitmap*> { |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 277 | public: |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 278 | PathTask(const SkPath* path, const SkPaint* paint, PathTexture* texture): |
Chris Craik | 906d47f | 2014-06-27 18:30:23 -0700 | [diff] [blame] | 279 | path(*path), paint(*paint), texture(texture) { |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 280 | } |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 281 | |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 282 | ~PathTask() { |
| 283 | delete future()->get(); |
| 284 | } |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 285 | |
Derek Sollenberger | ee24859 | 2015-02-12 14:10:21 -0500 | [diff] [blame] | 286 | // copied, since input path not guaranteed to survive for duration of task |
Chris Craik | 906d47f | 2014-06-27 18:30:23 -0700 | [diff] [blame] | 287 | const SkPath path; |
| 288 | |
| 289 | // copied, since input paint may not be immutable |
Kenny Root | 25e40de | 2014-05-30 11:51:20 -0700 | [diff] [blame] | 290 | const SkPaint paint; |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 291 | PathTexture* texture; |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 292 | }; |
| 293 | |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 294 | class PathProcessor: public TaskProcessor<SkBitmap*> { |
| 295 | public: |
| 296 | PathProcessor(Caches& caches); |
| 297 | ~PathProcessor() { } |
| 298 | |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame] | 299 | virtual void onProcess(const sp<Task<SkBitmap*> >& task) override; |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 300 | |
| 301 | private: |
| 302 | uint32_t mMaxTextureSize; |
| 303 | }; |
| 304 | |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 305 | LruCache<PathDescription, PathTexture*> mCache; |
| 306 | uint32_t mSize; |
| 307 | uint32_t mMaxSize; |
| 308 | GLuint mMaxTextureSize; |
| 309 | |
| 310 | bool mDebugEnabled; |
| 311 | |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 312 | sp<PathProcessor> mProcessor; |
Romain Guy | c5cbee7 | 2013-03-20 19:15:02 -0700 | [diff] [blame] | 313 | |
Derek Sollenberger | ee24859 | 2015-02-12 14:10:21 -0500 | [diff] [blame] | 314 | Vector<uint32_t> mGarbage; |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 315 | mutable Mutex mLock; |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 316 | }; // class PathCache |
| 317 | |
| 318 | }; // namespace uirenderer |
| 319 | }; // namespace android |
| 320 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 321 | #endif // ANDROID_HWUI_PATH_CACHE_H |