blob: 7378018b61ea81160a005e5ee400190d20f5cb4e [file] [log] [blame]
Romain Guy7fbcc042010-08-04 15:40:07 -07001/*
Romain Guyc46d07a2013-03-15 19:06:39 -07002 * Copyright (C) 2013 The Android Open Source Project
Romain Guy7fbcc042010-08-04 15:40:07 -07003 *
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 Guy5b3b3522010-10-27 18:57:51 -070017#ifndef ANDROID_HWUI_PATH_CACHE_H
18#define ANDROID_HWUI_PATH_CACHE_H
Romain Guy7fbcc042010-08-04 15:40:07 -070019
Chris Craik96a5c4c2015-01-27 15:46:35 -080020#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 Guyc46d07a2013-03-15 19:06:39 -070026
Chris Craik96a5c4c2015-01-27 15:46:35 -080027#include <GLES2/gl2.h>
28#include <SkPath.h>
Romain Guyc46d07a2013-03-15 19:06:39 -070029#include <utils/LruCache.h>
30#include <utils/Mutex.h>
Romain Guyfe48f652010-11-11 15:36:56 -080031#include <utils/Vector.h>
32
Romain Guyc46d07a2013-03-15 19:06:39 -070033class SkBitmap;
34class SkCanvas;
Romain Guyca89e2a2013-03-08 17:44:20 -080035class SkPaint;
Chris Craik564acf72014-01-02 16:46:18 -080036struct SkRect;
Romain Guyff26a0c2011-01-20 11:35:46 -080037
Romain Guy7fbcc042010-08-04 15:40:07 -070038namespace android {
39namespace uirenderer {
40
Romain Guyca89e2a2013-03-08 17:44:20 -080041class Caches;
42
Romain Guy9e108412010-11-09 14:35:20 -080043///////////////////////////////////////////////////////////////////////////////
Romain Guyc46d07a2013-03-15 19:06:39 -070044// 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 Guy9e108412010-11-09 14:35:20 -080055// Classes
56///////////////////////////////////////////////////////////////////////////////
57
Romain Guyc46d07a2013-03-15 19:06:39 -070058/**
59 * Alpha texture used to represent a path.
60 */
61struct PathTexture: public Texture {
Romain Guy8aa195d2013-06-04 18:00:09 -070062 PathTexture(Caches& caches): Texture(caches) {
Romain Guyff26a0c2011-01-20 11:35:46 -080063 }
64
Romain Guyc46d07a2013-03-15 19:06:39 -070065 ~PathTexture() {
66 clearTask();
Romain Guy7fbcc042010-08-04 15:40:07 -070067 }
68
Romain Guyc46d07a2013-03-15 19:06:39 -070069 /**
70 * Left coordinate of the path bounds.
71 */
72 float left;
73 /**
74 * Top coordinate of the path bounds.
75 */
76 float top;
77 /**
78 * Offset to draw the path at the correct origin.
79 */
80 float offset;
81
82 sp<Task<SkBitmap*> > task() const {
83 return mTask;
Romain Guy059e12c2012-11-28 17:35:51 -080084 }
85
Romain Guyc46d07a2013-03-15 19:06:39 -070086 void setTask(const sp<Task<SkBitmap*> >& task) {
87 mTask = task;
Romain Guy7fbcc042010-08-04 15:40:07 -070088 }
Romain Guy7fbcc042010-08-04 15:40:07 -070089
Romain Guyc46d07a2013-03-15 19:06:39 -070090 void clearTask() {
Chris Craike84a2082014-12-22 14:28:49 -080091 if (mTask != nullptr) {
Romain Guyc46d07a2013-03-15 19:06:39 -070092 mTask.clear();
93 }
94 }
Romain Guyb29cfbf2011-03-18 16:24:19 -070095
Romain Guyc46d07a2013-03-15 19:06:39 -070096private:
97 sp<Task<SkBitmap*> > mTask;
98}; // struct PathTexture
Romain Guy7fbcc042010-08-04 15:40:07 -070099
Romain Guyc46d07a2013-03-15 19:06:39 -0700100enum ShapeType {
101 kShapeNone,
102 kShapeRect,
103 kShapeRoundRect,
104 kShapeCircle,
105 kShapeOval,
106 kShapeArc,
107 kShapePath
108};
109
110struct PathDescription {
Chris Craik05f3d6e2014-06-02 16:27:04 -0700111 DESCRIPTION_TYPE(PathDescription);
Romain Guyc46d07a2013-03-15 19:06:39 -0700112 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 {
Chris Craikd218a922014-01-02 17:13:34 -0800121 const SkPath* mPath;
Romain Guyc46d07a2013-03-15 19:06:39 -0700122 } 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 Craikd218a922014-01-02 17:13:34 -0800150 PathDescription(ShapeType shapeType, const SkPaint* paint);
Romain Guyc46d07a2013-03-15 19:06:39 -0700151
152 hash_t hash() const;
Romain Guyc46d07a2013-03-15 19:06:39 -0700153};
Romain Guy059e12c2012-11-28 17:35:51 -0800154
Romain Guy7fbcc042010-08-04 15:40:07 -0700155/**
Romain Guyc46d07a2013-03-15 19:06:39 -0700156 * A simple LRU shape cache. The cache has a maximum size expressed in bytes.
Romain Guy7fbcc042010-08-04 15:40:07 -0700157 * Any texture added to the cache causing the cache to grow beyond the maximum
158 * allowed size will also cause the oldest texture to be kicked out.
159 */
Romain Guyc46d07a2013-03-15 19:06:39 -0700160class PathCache: public OnEntryRemoved<PathDescription, PathTexture*> {
Romain Guy7fbcc042010-08-04 15:40:07 -0700161public:
Romain Guyfb8b7632010-08-23 21:05:08 -0700162 PathCache();
Romain Guyca89e2a2013-03-08 17:44:20 -0800163 ~PathCache();
Romain Guy7fbcc042010-08-04 15:40:07 -0700164
165 /**
Romain Guyc46d07a2013-03-15 19:06:39 -0700166 * Used as a callback when an entry is removed from the cache.
167 * Do not invoke directly.
Romain Guy7fbcc042010-08-04 15:40:07 -0700168 */
Chris Craike84a2082014-12-22 14:28:49 -0800169 void operator()(PathDescription& path, PathTexture*& texture) override;
Romain Guyc46d07a2013-03-15 19:06:39 -0700170
171 /**
172 * Clears the cache. This causes all textures to be deleted.
173 */
174 void clear();
175
176 /**
177 * Sets the maximum size of the cache in bytes.
178 */
179 void setMaxSize(uint32_t maxSize);
180 /**
181 * Returns the maximum size of the cache in bytes.
182 */
183 uint32_t getMaxSize();
184 /**
185 * Returns the current size of the cache in bytes.
186 */
187 uint32_t getSize();
188
Chris Craikd218a922014-01-02 17:13:34 -0800189 PathTexture* getRoundRect(float width, float height, float rx, float ry, const SkPaint* paint);
190 PathTexture* getCircle(float radius, const SkPaint* paint);
191 PathTexture* getOval(float width, float height, const SkPaint* paint);
192 PathTexture* getRect(float width, float height, const SkPaint* paint);
Romain Guyc46d07a2013-03-15 19:06:39 -0700193 PathTexture* getArc(float width, float height, float startAngle, float sweepAngle,
Chris Craikd218a922014-01-02 17:13:34 -0800194 bool useCenter, const SkPaint* paint);
195 PathTexture* get(const SkPath* path, const SkPaint* paint);
Romain Guyc46d07a2013-03-15 19:06:39 -0700196
Romain Guy7fbcc042010-08-04 15:40:07 -0700197 /**
Romain Guyfe48f652010-11-11 15:36:56 -0800198 * Removes the specified path. This is meant to be called from threads
199 * that are not the EGL context thread.
200 */
201 void removeDeferred(SkPath* path);
202 /**
203 * Process deferred removals.
204 */
205 void clearGarbage();
Romain Guyc46d07a2013-03-15 19:06:39 -0700206 /**
207 * Trims the contents of the cache, removing items until it's under its
208 * specified limit.
209 *
210 * Trimming is used for caches that support pre-caching from a worker
211 * thread. During pre-caching the maximum limit of the cache can be
212 * exceeded for the duration of the frame. It is therefore required to
213 * trim the cache at the end of the frame to keep the total amount of
214 * memory used under control.
215 */
216 void trim();
Romain Guy7fbcc042010-08-04 15:40:07 -0700217
Romain Guyc46d07a2013-03-15 19:06:39 -0700218 /**
219 * Precaches the specified path using background threads.
220 */
Chris Craikd218a922014-01-02 17:13:34 -0800221 void precache(const SkPath* path, const SkPaint* paint);
Romain Guyca89e2a2013-03-08 17:44:20 -0800222
Chris Craikd218a922014-01-02 17:13:34 -0800223 static bool canDrawAsConvexPath(SkPath* path, const SkPaint* paint);
Romain Guyc46d07a2013-03-15 19:06:39 -0700224 static void computePathBounds(const SkPath* path, const SkPaint* paint,
225 float& left, float& top, float& offset, uint32_t& width, uint32_t& height);
226 static void computeBounds(const SkRect& bounds, const SkPaint* paint,
227 float& left, float& top, float& offset, uint32_t& width, uint32_t& height);
228
Romain Guy7fbcc042010-08-04 15:40:07 -0700229private:
Romain Guyc5cbee72013-03-20 19:15:02 -0700230 typedef Pair<SkPath*, SkPath*> path_pair_t;
231
Romain Guyc46d07a2013-03-15 19:06:39 -0700232 PathTexture* addTexture(const PathDescription& entry,
233 const SkPath *path, const SkPaint* paint);
234 PathTexture* addTexture(const PathDescription& entry, SkBitmap* bitmap);
Romain Guy4500a8d2013-03-26 17:29:51 -0700235
236 /**
237 * Generates the texture from a bitmap into the specified texture structure.
238 */
239 void generateTexture(SkBitmap& bitmap, Texture* texture);
240 void generateTexture(const PathDescription& entry, SkBitmap* bitmap, PathTexture* texture,
241 bool addToCache = true);
Romain Guyc46d07a2013-03-15 19:06:39 -0700242
243 PathTexture* get(const PathDescription& entry) {
244 return mCache.get(entry);
245 }
246
247 /**
Romain Guyc5cbee72013-03-20 19:15:02 -0700248 * Removes an entry.
249 * The pair must define first=path, second=sourcePath
250 */
Romain Guye3b0a012013-06-26 15:45:41 -0700251 void remove(Vector<PathDescription>& pathsToRemove, const path_pair_t& pair);
Romain Guyc5cbee72013-03-20 19:15:02 -0700252
253 /**
Romain Guyc46d07a2013-03-15 19:06:39 -0700254 * Ensures there is enough space in the cache for a texture of the specified
255 * dimensions.
256 */
257 void purgeCache(uint32_t width, uint32_t height);
258
259 void removeTexture(PathTexture* texture);
260
261 bool checkTextureSize(uint32_t width, uint32_t height) {
262 if (width > mMaxTextureSize || height > mMaxTextureSize) {
263 ALOGW("Shape too large to be rendered into a texture (%dx%d, max=%dx%d)",
264 width, height, mMaxTextureSize, mMaxTextureSize);
265 return false;
266 }
267 return true;
268 }
269
Romain Guyc46d07a2013-03-15 19:06:39 -0700270 void init();
271
Romain Guy5dc7fa72013-03-11 20:48:31 -0700272 class PathTask: public Task<SkBitmap*> {
Romain Guyca89e2a2013-03-08 17:44:20 -0800273 public:
Chris Craikd218a922014-01-02 17:13:34 -0800274 PathTask(const SkPath* path, const SkPaint* paint, PathTexture* texture):
Chris Craik906d47f2014-06-27 18:30:23 -0700275 path(*path), paint(*paint), texture(texture) {
Romain Guy5dc7fa72013-03-11 20:48:31 -0700276 }
Romain Guyca89e2a2013-03-08 17:44:20 -0800277
Romain Guy5dc7fa72013-03-11 20:48:31 -0700278 ~PathTask() {
279 delete future()->get();
280 }
Romain Guyca89e2a2013-03-08 17:44:20 -0800281
Chris Craik906d47f2014-06-27 18:30:23 -0700282 // copied, since input path not refcounted / guaranteed to survive for duration of task
283 // TODO: avoid deep copy with refcounting
284 const SkPath path;
285
286 // copied, since input paint may not be immutable
Kenny Root25e40de2014-05-30 11:51:20 -0700287 const SkPaint paint;
Romain Guy5dc7fa72013-03-11 20:48:31 -0700288 PathTexture* texture;
Romain Guyca89e2a2013-03-08 17:44:20 -0800289 };
290
Romain Guy5dc7fa72013-03-11 20:48:31 -0700291 class PathProcessor: public TaskProcessor<SkBitmap*> {
292 public:
293 PathProcessor(Caches& caches);
294 ~PathProcessor() { }
295
Chris Craike84a2082014-12-22 14:28:49 -0800296 virtual void onProcess(const sp<Task<SkBitmap*> >& task) override;
Romain Guy5dc7fa72013-03-11 20:48:31 -0700297
298 private:
299 uint32_t mMaxTextureSize;
300 };
301
Romain Guyc46d07a2013-03-15 19:06:39 -0700302 LruCache<PathDescription, PathTexture*> mCache;
303 uint32_t mSize;
304 uint32_t mMaxSize;
305 GLuint mMaxTextureSize;
306
307 bool mDebugEnabled;
308
Romain Guy5dc7fa72013-03-11 20:48:31 -0700309 sp<PathProcessor> mProcessor;
Romain Guyc5cbee72013-03-20 19:15:02 -0700310
311 Vector<path_pair_t> mGarbage;
Romain Guya2341a92010-09-08 18:04:33 -0700312 mutable Mutex mLock;
Romain Guy7fbcc042010-08-04 15:40:07 -0700313}; // class PathCache
314
315}; // namespace uirenderer
316}; // namespace android
317
Romain Guy5b3b3522010-10-27 18:57:51 -0700318#endif // ANDROID_HWUI_PATH_CACHE_H