blob: d2633aa427c58bc82cb2aad199cd47d7fb7db59f [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>
John Reck272a6852015-07-29 16:48:58 -070031
32#include <vector>
Romain Guyfe48f652010-11-11 15:36:56 -080033
Romain Guyc46d07a2013-03-15 19:06:39 -070034class SkBitmap;
35class SkCanvas;
Romain Guyca89e2a2013-03-08 17:44:20 -080036class SkPaint;
Chris Craik564acf72014-01-02 16:46:18 -080037struct SkRect;
Romain Guyff26a0c2011-01-20 11:35:46 -080038
Romain Guy7fbcc042010-08-04 15:40:07 -070039namespace android {
40namespace uirenderer {
41
Romain Guyca89e2a2013-03-08 17:44:20 -080042class Caches;
43
Romain Guy9e108412010-11-09 14:35:20 -080044///////////////////////////////////////////////////////////////////////////////
Romain Guyc46d07a2013-03-15 19:06:39 -070045// Defines
46///////////////////////////////////////////////////////////////////////////////
47
48// Debug
49#if DEBUG_PATHS
50 #define PATH_LOGD(...) ALOGD(__VA_ARGS__)
51#else
52 #define PATH_LOGD(...)
53#endif
54
55///////////////////////////////////////////////////////////////////////////////
Romain Guy9e108412010-11-09 14:35:20 -080056// Classes
57///////////////////////////////////////////////////////////////////////////////
58
Romain Guyc46d07a2013-03-15 19:06:39 -070059/**
60 * Alpha texture used to represent a path.
61 */
62struct PathTexture: public Texture {
Chris Craike2bb3802015-03-13 15:07:52 -070063 PathTexture(Caches& caches, float left, float top,
John Reck38e0c322015-11-10 12:19:17 -080064 float offset, int generation)
Chris Craike2bb3802015-03-13 15:07:52 -070065 : Texture(caches)
66 , left(left)
67 , top(top)
68 , offset(offset) {
Chris Craike2bb3802015-03-13 15:07:52 -070069 this->generation = generation;
70 }
71 PathTexture(Caches& caches, int generation)
72 : Texture(caches) {
73 this->generation = generation;
Romain Guyff26a0c2011-01-20 11:35:46 -080074 }
75
Romain Guyc46d07a2013-03-15 19:06:39 -070076 ~PathTexture() {
77 clearTask();
Romain Guy7fbcc042010-08-04 15:40:07 -070078 }
79
Romain Guyc46d07a2013-03-15 19:06:39 -070080 /**
81 * Left coordinate of the path bounds.
82 */
Chris Craike2bb3802015-03-13 15:07:52 -070083 float left = 0;
Romain Guyc46d07a2013-03-15 19:06:39 -070084 /**
85 * Top coordinate of the path bounds.
86 */
Chris Craike2bb3802015-03-13 15:07:52 -070087 float top = 0;
Romain Guyc46d07a2013-03-15 19:06:39 -070088 /**
89 * Offset to draw the path at the correct origin.
90 */
Chris Craike2bb3802015-03-13 15:07:52 -070091 float offset = 0;
Romain Guyc46d07a2013-03-15 19:06:39 -070092
93 sp<Task<SkBitmap*> > task() const {
94 return mTask;
Romain Guy059e12c2012-11-28 17:35:51 -080095 }
96
Romain Guyc46d07a2013-03-15 19:06:39 -070097 void setTask(const sp<Task<SkBitmap*> >& task) {
98 mTask = task;
Romain Guy7fbcc042010-08-04 15:40:07 -070099 }
Romain Guy7fbcc042010-08-04 15:40:07 -0700100
Romain Guyc46d07a2013-03-15 19:06:39 -0700101 void clearTask() {
Chris Craike84a2082014-12-22 14:28:49 -0800102 if (mTask != nullptr) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700103 mTask.clear();
104 }
105 }
Romain Guyb29cfbf2011-03-18 16:24:19 -0700106
Romain Guyc46d07a2013-03-15 19:06:39 -0700107private:
108 sp<Task<SkBitmap*> > mTask;
109}; // struct PathTexture
Romain Guy7fbcc042010-08-04 15:40:07 -0700110
Romain Guyc46d07a2013-03-15 19:06:39 -0700111enum ShapeType {
112 kShapeNone,
113 kShapeRect,
114 kShapeRoundRect,
115 kShapeCircle,
116 kShapeOval,
117 kShapeArc,
118 kShapePath
119};
120
121struct PathDescription {
Chris Craik05f3d6e2014-06-02 16:27:04 -0700122 DESCRIPTION_TYPE(PathDescription);
Romain Guyc46d07a2013-03-15 19:06:39 -0700123 ShapeType type;
124 SkPaint::Join join;
125 SkPaint::Cap cap;
126 SkPaint::Style style;
127 float miter;
128 float strokeWidth;
129 SkPathEffect* pathEffect;
130 union Shape {
131 struct Path {
Derek Sollenbergeree248592015-02-12 14:10:21 -0500132 uint32_t mGenerationID;
Romain Guyc46d07a2013-03-15 19:06:39 -0700133 } path;
134 struct RoundRect {
135 float mWidth;
136 float mHeight;
137 float mRx;
138 float mRy;
139 } roundRect;
140 struct Circle {
141 float mRadius;
142 } circle;
143 struct Oval {
144 float mWidth;
145 float mHeight;
146 } oval;
147 struct Rect {
148 float mWidth;
149 float mHeight;
150 } rect;
151 struct Arc {
152 float mWidth;
153 float mHeight;
154 float mStartAngle;
155 float mSweepAngle;
156 bool mUseCenter;
157 } arc;
158 } shape;
159
160 PathDescription();
Chris Craikd218a922014-01-02 17:13:34 -0800161 PathDescription(ShapeType shapeType, const SkPaint* paint);
Romain Guyc46d07a2013-03-15 19:06:39 -0700162
163 hash_t hash() const;
Romain Guyc46d07a2013-03-15 19:06:39 -0700164};
Romain Guy059e12c2012-11-28 17:35:51 -0800165
Romain Guy7fbcc042010-08-04 15:40:07 -0700166/**
Romain Guyc46d07a2013-03-15 19:06:39 -0700167 * A simple LRU shape cache. The cache has a maximum size expressed in bytes.
Romain Guy7fbcc042010-08-04 15:40:07 -0700168 * Any texture added to the cache causing the cache to grow beyond the maximum
169 * allowed size will also cause the oldest texture to be kicked out.
170 */
Romain Guyc46d07a2013-03-15 19:06:39 -0700171class PathCache: public OnEntryRemoved<PathDescription, PathTexture*> {
Romain Guy7fbcc042010-08-04 15:40:07 -0700172public:
Romain Guyfb8b7632010-08-23 21:05:08 -0700173 PathCache();
Romain Guyca89e2a2013-03-08 17:44:20 -0800174 ~PathCache();
Romain Guy7fbcc042010-08-04 15:40:07 -0700175
176 /**
Romain Guyc46d07a2013-03-15 19:06:39 -0700177 * Used as a callback when an entry is removed from the cache.
178 * Do not invoke directly.
Romain Guy7fbcc042010-08-04 15:40:07 -0700179 */
Chris Craike84a2082014-12-22 14:28:49 -0800180 void operator()(PathDescription& path, PathTexture*& texture) override;
Romain Guyc46d07a2013-03-15 19:06:39 -0700181
182 /**
183 * Clears the cache. This causes all textures to be deleted.
184 */
185 void clear();
186
187 /**
Romain Guyc46d07a2013-03-15 19:06:39 -0700188 * Returns the maximum size of the cache in bytes.
189 */
190 uint32_t getMaxSize();
191 /**
192 * Returns the current size of the cache in bytes.
193 */
194 uint32_t getSize();
195
Chris Craikd218a922014-01-02 17:13:34 -0800196 PathTexture* getRoundRect(float width, float height, float rx, float ry, const SkPaint* paint);
197 PathTexture* getCircle(float radius, const SkPaint* paint);
198 PathTexture* getOval(float width, float height, const SkPaint* paint);
199 PathTexture* getRect(float width, float height, const SkPaint* paint);
Romain Guyc46d07a2013-03-15 19:06:39 -0700200 PathTexture* getArc(float width, float height, float startAngle, float sweepAngle,
Chris Craikd218a922014-01-02 17:13:34 -0800201 bool useCenter, const SkPaint* paint);
202 PathTexture* get(const SkPath* path, const SkPaint* paint);
Digish Pandya2e4f67c2015-11-04 11:00:28 +0530203 void remove(const SkPath* path, const SkPaint* paint);
Romain Guyc46d07a2013-03-15 19:06:39 -0700204
Romain Guy7fbcc042010-08-04 15:40:07 -0700205 /**
Romain Guyfe48f652010-11-11 15:36:56 -0800206 * Removes the specified path. This is meant to be called from threads
207 * that are not the EGL context thread.
208 */
Derek Sollenbergeree248592015-02-12 14:10:21 -0500209 ANDROID_API void removeDeferred(const SkPath* path);
Romain Guyfe48f652010-11-11 15:36:56 -0800210 /**
211 * Process deferred removals.
212 */
213 void clearGarbage();
Romain Guyc46d07a2013-03-15 19:06:39 -0700214 /**
215 * Trims the contents of the cache, removing items until it's under its
216 * specified limit.
217 *
218 * Trimming is used for caches that support pre-caching from a worker
219 * thread. During pre-caching the maximum limit of the cache can be
220 * exceeded for the duration of the frame. It is therefore required to
221 * trim the cache at the end of the frame to keep the total amount of
222 * memory used under control.
223 */
224 void trim();
Romain Guy7fbcc042010-08-04 15:40:07 -0700225
Romain Guyc46d07a2013-03-15 19:06:39 -0700226 /**
227 * Precaches the specified path using background threads.
228 */
Chris Craikd218a922014-01-02 17:13:34 -0800229 void precache(const SkPath* path, const SkPaint* paint);
Romain Guyca89e2a2013-03-08 17:44:20 -0800230
Chris Craikd218a922014-01-02 17:13:34 -0800231 static bool canDrawAsConvexPath(SkPath* path, const SkPaint* paint);
Romain Guyc46d07a2013-03-15 19:06:39 -0700232 static void computePathBounds(const SkPath* path, const SkPaint* paint,
233 float& left, float& top, float& offset, uint32_t& width, uint32_t& height);
234 static void computeBounds(const SkRect& bounds, const SkPaint* paint,
235 float& left, float& top, float& offset, uint32_t& width, uint32_t& height);
236
Romain Guy7fbcc042010-08-04 15:40:07 -0700237private:
Romain Guyc46d07a2013-03-15 19:06:39 -0700238 PathTexture* addTexture(const PathDescription& entry,
239 const SkPath *path, const SkPaint* paint);
240 PathTexture* addTexture(const PathDescription& entry, SkBitmap* bitmap);
Romain Guy4500a8d2013-03-26 17:29:51 -0700241
242 /**
243 * Generates the texture from a bitmap into the specified texture structure.
244 */
245 void generateTexture(SkBitmap& bitmap, Texture* texture);
246 void generateTexture(const PathDescription& entry, SkBitmap* bitmap, PathTexture* texture,
247 bool addToCache = true);
Romain Guyc46d07a2013-03-15 19:06:39 -0700248
249 PathTexture* get(const PathDescription& entry) {
250 return mCache.get(entry);
251 }
252
253 /**
254 * 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
Derek Sollenbergeree248592015-02-12 14:10:21 -0500282 // copied, since input path not guaranteed to survive for duration of task
Chris Craik906d47f2014-06-27 18:30:23 -0700283 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;
Chris Craik48a8f432016-02-05 15:59:29 -0800303 const uint32_t mMaxSize;
Romain Guyc46d07a2013-03-15 19:06:39 -0700304 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
John Reck272a6852015-07-29 16:48:58 -0700310 std::vector<uint32_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