blob: 86c59b532b287dc2274cd2d80d88d84afbd44faf [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 {
Chris Craike2bb3802015-03-13 15:07:52 -070062 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 Guyff26a0c2011-01-20 11:35:46 -080075 }
76
Romain Guyc46d07a2013-03-15 19:06:39 -070077 ~PathTexture() {
78 clearTask();
Romain Guy7fbcc042010-08-04 15:40:07 -070079 }
80
Romain Guyc46d07a2013-03-15 19:06:39 -070081 /**
82 * Left coordinate of the path bounds.
83 */
Chris Craike2bb3802015-03-13 15:07:52 -070084 float left = 0;
Romain Guyc46d07a2013-03-15 19:06:39 -070085 /**
86 * Top coordinate of the path bounds.
87 */
Chris Craike2bb3802015-03-13 15:07:52 -070088 float top = 0;
Romain Guyc46d07a2013-03-15 19:06:39 -070089 /**
90 * Offset to draw the path at the correct origin.
91 */
Chris Craike2bb3802015-03-13 15:07:52 -070092 float offset = 0;
Romain Guyc46d07a2013-03-15 19:06:39 -070093
94 sp<Task<SkBitmap*> > task() const {
95 return mTask;
Romain Guy059e12c2012-11-28 17:35:51 -080096 }
97
Romain Guyc46d07a2013-03-15 19:06:39 -070098 void setTask(const sp<Task<SkBitmap*> >& task) {
99 mTask = task;
Romain Guy7fbcc042010-08-04 15:40:07 -0700100 }
Romain Guy7fbcc042010-08-04 15:40:07 -0700101
Romain Guyc46d07a2013-03-15 19:06:39 -0700102 void clearTask() {
Chris Craike84a2082014-12-22 14:28:49 -0800103 if (mTask != nullptr) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700104 mTask.clear();
105 }
106 }
Romain Guyb29cfbf2011-03-18 16:24:19 -0700107
Romain Guyc46d07a2013-03-15 19:06:39 -0700108private:
109 sp<Task<SkBitmap*> > mTask;
110}; // struct PathTexture
Romain Guy7fbcc042010-08-04 15:40:07 -0700111
Romain Guyc46d07a2013-03-15 19:06:39 -0700112enum ShapeType {
113 kShapeNone,
114 kShapeRect,
115 kShapeRoundRect,
116 kShapeCircle,
117 kShapeOval,
118 kShapeArc,
119 kShapePath
120};
121
122struct PathDescription {
Chris Craik05f3d6e2014-06-02 16:27:04 -0700123 DESCRIPTION_TYPE(PathDescription);
Romain Guyc46d07a2013-03-15 19:06:39 -0700124 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 Sollenbergeree248592015-02-12 14:10:21 -0500133 uint32_t mGenerationID;
Romain Guyc46d07a2013-03-15 19:06:39 -0700134 } 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 Craikd218a922014-01-02 17:13:34 -0800162 PathDescription(ShapeType shapeType, const SkPaint* paint);
Romain Guyc46d07a2013-03-15 19:06:39 -0700163
164 hash_t hash() const;
Romain Guyc46d07a2013-03-15 19:06:39 -0700165};
Romain Guy059e12c2012-11-28 17:35:51 -0800166
Romain Guy7fbcc042010-08-04 15:40:07 -0700167/**
Romain Guyc46d07a2013-03-15 19:06:39 -0700168 * A simple LRU shape cache. The cache has a maximum size expressed in bytes.
Romain Guy7fbcc042010-08-04 15:40:07 -0700169 * 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 Guyc46d07a2013-03-15 19:06:39 -0700172class PathCache: public OnEntryRemoved<PathDescription, PathTexture*> {
Romain Guy7fbcc042010-08-04 15:40:07 -0700173public:
Romain Guyfb8b7632010-08-23 21:05:08 -0700174 PathCache();
Romain Guyca89e2a2013-03-08 17:44:20 -0800175 ~PathCache();
Romain Guy7fbcc042010-08-04 15:40:07 -0700176
177 /**
Romain Guyc46d07a2013-03-15 19:06:39 -0700178 * Used as a callback when an entry is removed from the cache.
179 * Do not invoke directly.
Romain Guy7fbcc042010-08-04 15:40:07 -0700180 */
Chris Craike84a2082014-12-22 14:28:49 -0800181 void operator()(PathDescription& path, PathTexture*& texture) override;
Romain Guyc46d07a2013-03-15 19:06:39 -0700182
183 /**
184 * Clears the cache. This causes all textures to be deleted.
185 */
186 void clear();
187
188 /**
Romain Guyc46d07a2013-03-15 19:06:39 -0700189 * Returns the maximum size of the cache in bytes.
190 */
191 uint32_t getMaxSize();
192 /**
193 * Returns the current size of the cache in bytes.
194 */
195 uint32_t getSize();
196
Chris Craikd218a922014-01-02 17:13:34 -0800197 PathTexture* getRoundRect(float width, float height, float rx, float ry, const SkPaint* paint);
198 PathTexture* getCircle(float radius, const SkPaint* paint);
199 PathTexture* getOval(float width, float height, const SkPaint* paint);
200 PathTexture* getRect(float width, float height, const SkPaint* paint);
Romain Guyc46d07a2013-03-15 19:06:39 -0700201 PathTexture* getArc(float width, float height, float startAngle, float sweepAngle,
Chris Craikd218a922014-01-02 17:13:34 -0800202 bool useCenter, const SkPaint* paint);
203 PathTexture* get(const SkPath* path, const SkPaint* paint);
Digish Pandya2e4f67c2015-11-04 11:00:28 +0530204 void remove(const SkPath* path, const SkPaint* paint);
Romain Guyc46d07a2013-03-15 19:06:39 -0700205
Romain Guy7fbcc042010-08-04 15:40:07 -0700206 /**
Romain Guyfe48f652010-11-11 15:36:56 -0800207 * Removes the specified path. This is meant to be called from threads
208 * that are not the EGL context thread.
209 */
Derek Sollenbergeree248592015-02-12 14:10:21 -0500210 ANDROID_API void removeDeferred(const SkPath* path);
Romain Guyfe48f652010-11-11 15:36:56 -0800211 /**
212 * Process deferred removals.
213 */
214 void clearGarbage();
Romain Guyc46d07a2013-03-15 19:06:39 -0700215 /**
216 * Trims the contents of the cache, removing items until it's under its
217 * specified limit.
218 *
219 * Trimming is used for caches that support pre-caching from a worker
220 * thread. During pre-caching the maximum limit of the cache can be
221 * exceeded for the duration of the frame. It is therefore required to
222 * trim the cache at the end of the frame to keep the total amount of
223 * memory used under control.
224 */
225 void trim();
Romain Guy7fbcc042010-08-04 15:40:07 -0700226
Romain Guyc46d07a2013-03-15 19:06:39 -0700227 /**
228 * Precaches the specified path using background threads.
229 */
Chris Craikd218a922014-01-02 17:13:34 -0800230 void precache(const SkPath* path, const SkPaint* paint);
Romain Guyca89e2a2013-03-08 17:44:20 -0800231
Chris Craikd218a922014-01-02 17:13:34 -0800232 static bool canDrawAsConvexPath(SkPath* path, const SkPaint* paint);
Romain Guyc46d07a2013-03-15 19:06:39 -0700233 static void computePathBounds(const SkPath* path, const SkPaint* paint,
234 float& left, float& top, float& offset, uint32_t& width, uint32_t& height);
235 static void computeBounds(const SkRect& bounds, const SkPaint* paint,
236 float& left, float& top, float& offset, uint32_t& width, uint32_t& height);
237
Romain Guy7fbcc042010-08-04 15:40:07 -0700238private:
Romain Guyc46d07a2013-03-15 19:06:39 -0700239 PathTexture* addTexture(const PathDescription& entry,
240 const SkPath *path, const SkPaint* paint);
241 PathTexture* addTexture(const PathDescription& entry, SkBitmap* bitmap);
Romain Guy4500a8d2013-03-26 17:29:51 -0700242
243 /**
244 * Generates the texture from a bitmap into the specified texture structure.
245 */
246 void generateTexture(SkBitmap& bitmap, Texture* texture);
247 void generateTexture(const PathDescription& entry, SkBitmap* bitmap, PathTexture* texture,
248 bool addToCache = true);
Romain Guyc46d07a2013-03-15 19:06:39 -0700249
250 PathTexture* get(const PathDescription& entry) {
251 return mCache.get(entry);
252 }
253
254 /**
255 * Ensures there is enough space in the cache for a texture of the specified
256 * dimensions.
257 */
258 void purgeCache(uint32_t width, uint32_t height);
259
260 void removeTexture(PathTexture* texture);
261
262 bool checkTextureSize(uint32_t width, uint32_t height) {
263 if (width > mMaxTextureSize || height > mMaxTextureSize) {
264 ALOGW("Shape too large to be rendered into a texture (%dx%d, max=%dx%d)",
265 width, height, mMaxTextureSize, mMaxTextureSize);
266 return false;
267 }
268 return true;
269 }
270
Romain Guyc46d07a2013-03-15 19:06:39 -0700271 void init();
272
Romain Guy5dc7fa72013-03-11 20:48:31 -0700273 class PathTask: public Task<SkBitmap*> {
Romain Guyca89e2a2013-03-08 17:44:20 -0800274 public:
Chris Craikd218a922014-01-02 17:13:34 -0800275 PathTask(const SkPath* path, const SkPaint* paint, PathTexture* texture):
Chris Craik906d47f2014-06-27 18:30:23 -0700276 path(*path), paint(*paint), texture(texture) {
Romain Guy5dc7fa72013-03-11 20:48:31 -0700277 }
Romain Guyca89e2a2013-03-08 17:44:20 -0800278
Romain Guy5dc7fa72013-03-11 20:48:31 -0700279 ~PathTask() {
280 delete future()->get();
281 }
Romain Guyca89e2a2013-03-08 17:44:20 -0800282
Derek Sollenbergeree248592015-02-12 14:10:21 -0500283 // copied, since input path not guaranteed to survive for duration of task
Chris Craik906d47f2014-06-27 18:30:23 -0700284 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:
Chih-Hung Hsiehfaecb782016-07-21 11:23:06 -0700293 explicit PathProcessor(Caches& caches);
Romain Guy5dc7fa72013-03-11 20:48:31 -0700294 ~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
Derek Sollenbergeree248592015-02-12 14:10:21 -0500311 Vector<uint32_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