blob: ecd3712ce6eb59e6c3e213bc773bdeffeec2f545 [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
Romain Guyc46d07a2013-03-15 19:06:39 -070020#include <GLES2/gl2.h>
21
22#include <utils/LruCache.h>
23#include <utils/Mutex.h>
Romain Guyfe48f652010-11-11 15:36:56 -080024#include <utils/Vector.h>
25
Romain Guyc15008e2010-11-10 11:59:15 -080026#include "Debug.h"
Romain Guyc46d07a2013-03-15 19:06:39 -070027#include "Texture.h"
Chris Craik05f3d6e2014-06-02 16:27:04 -070028#include "utils/Macros.h"
Romain Guyc5cbee72013-03-20 19:15:02 -070029#include "utils/Pair.h"
Romain Guyca89e2a2013-03-08 17:44:20 -080030
Romain Guyc46d07a2013-03-15 19:06:39 -070031class SkBitmap;
32class SkCanvas;
Romain Guyca89e2a2013-03-08 17:44:20 -080033class SkPaint;
34class SkPath;
Chris Craik564acf72014-01-02 16:46:18 -080035struct SkRect;
Romain Guyff26a0c2011-01-20 11:35:46 -080036
Romain Guy7fbcc042010-08-04 15:40:07 -070037namespace android {
38namespace uirenderer {
39
Romain Guyca89e2a2013-03-08 17:44:20 -080040class Caches;
41
Romain Guy9e108412010-11-09 14:35:20 -080042///////////////////////////////////////////////////////////////////////////////
Romain Guyc46d07a2013-03-15 19:06:39 -070043// Defines
44///////////////////////////////////////////////////////////////////////////////
45
46// Debug
47#if DEBUG_PATHS
48 #define PATH_LOGD(...) ALOGD(__VA_ARGS__)
49#else
50 #define PATH_LOGD(...)
51#endif
52
53///////////////////////////////////////////////////////////////////////////////
Romain Guy9e108412010-11-09 14:35:20 -080054// Classes
55///////////////////////////////////////////////////////////////////////////////
56
Romain Guyc46d07a2013-03-15 19:06:39 -070057/**
58 * Alpha texture used to represent a path.
59 */
60struct PathTexture: public Texture {
Romain Guy8aa195d2013-06-04 18:00:09 -070061 PathTexture(Caches& caches): Texture(caches) {
Romain Guyff26a0c2011-01-20 11:35:46 -080062 }
63
Romain Guyc46d07a2013-03-15 19:06:39 -070064 ~PathTexture() {
65 clearTask();
Romain Guy7fbcc042010-08-04 15:40:07 -070066 }
67
Romain Guyc46d07a2013-03-15 19:06:39 -070068 /**
69 * Left coordinate of the path bounds.
70 */
71 float left;
72 /**
73 * Top coordinate of the path bounds.
74 */
75 float top;
76 /**
77 * Offset to draw the path at the correct origin.
78 */
79 float offset;
80
81 sp<Task<SkBitmap*> > task() const {
82 return mTask;
Romain Guy059e12c2012-11-28 17:35:51 -080083 }
84
Romain Guyc46d07a2013-03-15 19:06:39 -070085 void setTask(const sp<Task<SkBitmap*> >& task) {
86 mTask = task;
Romain Guy7fbcc042010-08-04 15:40:07 -070087 }
Romain Guy7fbcc042010-08-04 15:40:07 -070088
Romain Guyc46d07a2013-03-15 19:06:39 -070089 void clearTask() {
Chris Craike84a2082014-12-22 14:28:49 -080090 if (mTask != nullptr) {
Romain Guyc46d07a2013-03-15 19:06:39 -070091 mTask.clear();
92 }
93 }
Romain Guyb29cfbf2011-03-18 16:24:19 -070094
Romain Guyc46d07a2013-03-15 19:06:39 -070095private:
96 sp<Task<SkBitmap*> > mTask;
97}; // struct PathTexture
Romain Guy7fbcc042010-08-04 15:40:07 -070098
Romain Guyc46d07a2013-03-15 19:06:39 -070099enum ShapeType {
100 kShapeNone,
101 kShapeRect,
102 kShapeRoundRect,
103 kShapeCircle,
104 kShapeOval,
105 kShapeArc,
106 kShapePath
107};
108
109struct PathDescription {
Chris Craik05f3d6e2014-06-02 16:27:04 -0700110 DESCRIPTION_TYPE(PathDescription);
Romain Guyc46d07a2013-03-15 19:06:39 -0700111 ShapeType type;
112 SkPaint::Join join;
113 SkPaint::Cap cap;
114 SkPaint::Style style;
115 float miter;
116 float strokeWidth;
117 SkPathEffect* pathEffect;
118 union Shape {
119 struct Path {
Chris Craikd218a922014-01-02 17:13:34 -0800120 const SkPath* mPath;
Romain Guyc46d07a2013-03-15 19:06:39 -0700121 } path;
122 struct RoundRect {
123 float mWidth;
124 float mHeight;
125 float mRx;
126 float mRy;
127 } roundRect;
128 struct Circle {
129 float mRadius;
130 } circle;
131 struct Oval {
132 float mWidth;
133 float mHeight;
134 } oval;
135 struct Rect {
136 float mWidth;
137 float mHeight;
138 } rect;
139 struct Arc {
140 float mWidth;
141 float mHeight;
142 float mStartAngle;
143 float mSweepAngle;
144 bool mUseCenter;
145 } arc;
146 } shape;
147
148 PathDescription();
Chris Craikd218a922014-01-02 17:13:34 -0800149 PathDescription(ShapeType shapeType, const SkPaint* paint);
Romain Guyc46d07a2013-03-15 19:06:39 -0700150
151 hash_t hash() const;
Romain Guyc46d07a2013-03-15 19:06:39 -0700152};
Romain Guy059e12c2012-11-28 17:35:51 -0800153
Romain Guy7fbcc042010-08-04 15:40:07 -0700154/**
Romain Guyc46d07a2013-03-15 19:06:39 -0700155 * A simple LRU shape cache. The cache has a maximum size expressed in bytes.
Romain Guy7fbcc042010-08-04 15:40:07 -0700156 * Any texture added to the cache causing the cache to grow beyond the maximum
157 * allowed size will also cause the oldest texture to be kicked out.
158 */
Romain Guyc46d07a2013-03-15 19:06:39 -0700159class PathCache: public OnEntryRemoved<PathDescription, PathTexture*> {
Romain Guy7fbcc042010-08-04 15:40:07 -0700160public:
Romain Guyfb8b7632010-08-23 21:05:08 -0700161 PathCache();
Romain Guyca89e2a2013-03-08 17:44:20 -0800162 ~PathCache();
Romain Guy7fbcc042010-08-04 15:40:07 -0700163
164 /**
Romain Guyc46d07a2013-03-15 19:06:39 -0700165 * Used as a callback when an entry is removed from the cache.
166 * Do not invoke directly.
Romain Guy7fbcc042010-08-04 15:40:07 -0700167 */
Chris Craike84a2082014-12-22 14:28:49 -0800168 void operator()(PathDescription& path, PathTexture*& texture) override;
Romain Guyc46d07a2013-03-15 19:06:39 -0700169
170 /**
171 * Clears the cache. This causes all textures to be deleted.
172 */
173 void clear();
174
175 /**
176 * Sets the maximum size of the cache in bytes.
177 */
178 void setMaxSize(uint32_t maxSize);
179 /**
180 * Returns the maximum size of the cache in bytes.
181 */
182 uint32_t getMaxSize();
183 /**
184 * Returns the current size of the cache in bytes.
185 */
186 uint32_t getSize();
187
Chris Craikd218a922014-01-02 17:13:34 -0800188 PathTexture* getRoundRect(float width, float height, float rx, float ry, const SkPaint* paint);
189 PathTexture* getCircle(float radius, const SkPaint* paint);
190 PathTexture* getOval(float width, float height, const SkPaint* paint);
191 PathTexture* getRect(float width, float height, const SkPaint* paint);
Romain Guyc46d07a2013-03-15 19:06:39 -0700192 PathTexture* getArc(float width, float height, float startAngle, float sweepAngle,
Chris Craikd218a922014-01-02 17:13:34 -0800193 bool useCenter, const SkPaint* paint);
194 PathTexture* get(const SkPath* path, const SkPaint* paint);
Romain Guyc46d07a2013-03-15 19:06:39 -0700195
Romain Guy7fbcc042010-08-04 15:40:07 -0700196 /**
Romain Guyfe48f652010-11-11 15:36:56 -0800197 * Removes the specified path. This is meant to be called from threads
198 * that are not the EGL context thread.
199 */
200 void removeDeferred(SkPath* path);
201 /**
202 * Process deferred removals.
203 */
204 void clearGarbage();
Romain Guyc46d07a2013-03-15 19:06:39 -0700205 /**
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 Guy7fbcc042010-08-04 15:40:07 -0700216
Romain Guyc46d07a2013-03-15 19:06:39 -0700217 /**
218 * Precaches the specified path using background threads.
219 */
Chris Craikd218a922014-01-02 17:13:34 -0800220 void precache(const SkPath* path, const SkPaint* paint);
Romain Guyca89e2a2013-03-08 17:44:20 -0800221
Chris Craikd218a922014-01-02 17:13:34 -0800222 static bool canDrawAsConvexPath(SkPath* path, const SkPaint* paint);
Romain Guyc46d07a2013-03-15 19:06:39 -0700223 static void computePathBounds(const SkPath* path, const SkPaint* paint,
224 float& left, float& top, float& offset, uint32_t& width, uint32_t& height);
225 static void computeBounds(const SkRect& bounds, const SkPaint* paint,
226 float& left, float& top, float& offset, uint32_t& width, uint32_t& height);
227
Romain Guy7fbcc042010-08-04 15:40:07 -0700228private:
Romain Guyc5cbee72013-03-20 19:15:02 -0700229 typedef Pair<SkPath*, SkPath*> path_pair_t;
230
Romain Guyc46d07a2013-03-15 19:06:39 -0700231 PathTexture* addTexture(const PathDescription& entry,
232 const SkPath *path, const SkPaint* paint);
233 PathTexture* addTexture(const PathDescription& entry, SkBitmap* bitmap);
Romain Guy4500a8d2013-03-26 17:29:51 -0700234
235 /**
236 * Generates the texture from a bitmap into the specified texture structure.
237 */
238 void generateTexture(SkBitmap& bitmap, Texture* texture);
239 void generateTexture(const PathDescription& entry, SkBitmap* bitmap, PathTexture* texture,
240 bool addToCache = true);
Romain Guyc46d07a2013-03-15 19:06:39 -0700241
242 PathTexture* get(const PathDescription& entry) {
243 return mCache.get(entry);
244 }
245
246 /**
Romain Guyc5cbee72013-03-20 19:15:02 -0700247 * Removes an entry.
248 * The pair must define first=path, second=sourcePath
249 */
Romain Guye3b0a012013-06-26 15:45:41 -0700250 void remove(Vector<PathDescription>& pathsToRemove, const path_pair_t& pair);
Romain Guyc5cbee72013-03-20 19:15:02 -0700251
252 /**
Romain Guyc46d07a2013-03-15 19:06:39 -0700253 * 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 Guyc46d07a2013-03-15 19:06:39 -0700269 void init();
270
Romain Guy5dc7fa72013-03-11 20:48:31 -0700271 class PathTask: public Task<SkBitmap*> {
Romain Guyca89e2a2013-03-08 17:44:20 -0800272 public:
Chris Craikd218a922014-01-02 17:13:34 -0800273 PathTask(const SkPath* path, const SkPaint* paint, PathTexture* texture):
Chris Craik906d47f2014-06-27 18:30:23 -0700274 path(*path), paint(*paint), texture(texture) {
Romain Guy5dc7fa72013-03-11 20:48:31 -0700275 }
Romain Guyca89e2a2013-03-08 17:44:20 -0800276
Romain Guy5dc7fa72013-03-11 20:48:31 -0700277 ~PathTask() {
278 delete future()->get();
279 }
Romain Guyca89e2a2013-03-08 17:44:20 -0800280
Chris Craik906d47f2014-06-27 18:30:23 -0700281 // copied, since input path not refcounted / guaranteed to survive for duration of task
282 // TODO: avoid deep copy with refcounting
283 const SkPath path;
284
285 // copied, since input paint may not be immutable
Kenny Root25e40de2014-05-30 11:51:20 -0700286 const SkPaint paint;
Romain Guy5dc7fa72013-03-11 20:48:31 -0700287 PathTexture* texture;
Romain Guyca89e2a2013-03-08 17:44:20 -0800288 };
289
Romain Guy5dc7fa72013-03-11 20:48:31 -0700290 class PathProcessor: public TaskProcessor<SkBitmap*> {
291 public:
292 PathProcessor(Caches& caches);
293 ~PathProcessor() { }
294
Chris Craike84a2082014-12-22 14:28:49 -0800295 virtual void onProcess(const sp<Task<SkBitmap*> >& task) override;
Romain Guy5dc7fa72013-03-11 20:48:31 -0700296
297 private:
298 uint32_t mMaxTextureSize;
299 };
300
Romain Guyc46d07a2013-03-15 19:06:39 -0700301 LruCache<PathDescription, PathTexture*> mCache;
302 uint32_t mSize;
303 uint32_t mMaxSize;
304 GLuint mMaxTextureSize;
305
306 bool mDebugEnabled;
307
Romain Guy5dc7fa72013-03-11 20:48:31 -0700308 sp<PathProcessor> mProcessor;
Romain Guyc5cbee72013-03-20 19:15:02 -0700309
310 Vector<path_pair_t> mGarbage;
Romain Guya2341a92010-09-08 18:04:33 -0700311 mutable Mutex mLock;
Romain Guy7fbcc042010-08-04 15:40:07 -0700312}; // class PathCache
313
314}; // namespace uirenderer
315}; // namespace android
316
Romain Guy5b3b3522010-10-27 18:57:51 -0700317#endif // ANDROID_HWUI_PATH_CACHE_H