blob: 18bcc566296fdab1803a4c95eeb8e5194f2c482f [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>
sergeyv7224e2b2016-04-07 18:06:53 -070028#include <SkPaint.h>
Chris Craik96a5c4c2015-01-27 15:46:35 -080029#include <SkPath.h>
Romain Guyc46d07a2013-03-15 19:06:39 -070030#include <utils/LruCache.h>
31#include <utils/Mutex.h>
John Reck272a6852015-07-29 16:48:58 -070032
33#include <vector>
Romain Guyfe48f652010-11-11 15:36:56 -080034
Romain Guyc46d07a2013-03-15 19:06:39 -070035class SkBitmap;
36class SkCanvas;
Romain Guyca89e2a2013-03-08 17:44:20 -080037class SkPaint;
Chris Craik564acf72014-01-02 16:46:18 -080038struct SkRect;
Romain Guyff26a0c2011-01-20 11:35:46 -080039
Romain Guy7fbcc042010-08-04 15:40:07 -070040namespace android {
41namespace uirenderer {
42
Romain Guyca89e2a2013-03-08 17:44:20 -080043class Caches;
44
Romain Guy9e108412010-11-09 14:35:20 -080045///////////////////////////////////////////////////////////////////////////////
Romain Guyc46d07a2013-03-15 19:06:39 -070046// Defines
47///////////////////////////////////////////////////////////////////////////////
48
49// Debug
50#if DEBUG_PATHS
51 #define PATH_LOGD(...) ALOGD(__VA_ARGS__)
52#else
53 #define PATH_LOGD(...)
54#endif
55
56///////////////////////////////////////////////////////////////////////////////
Romain Guy9e108412010-11-09 14:35:20 -080057// Classes
58///////////////////////////////////////////////////////////////////////////////
59
Romain Guyc46d07a2013-03-15 19:06:39 -070060/**
61 * Alpha texture used to represent a path.
62 */
63struct PathTexture: public Texture {
Chris Craike2bb3802015-03-13 15:07:52 -070064 PathTexture(Caches& caches, int generation)
65 : Texture(caches) {
66 this->generation = generation;
Romain Guyff26a0c2011-01-20 11:35:46 -080067 }
68
Romain Guyc46d07a2013-03-15 19:06:39 -070069 ~PathTexture() {
70 clearTask();
Romain Guy7fbcc042010-08-04 15:40:07 -070071 }
72
Romain Guyc46d07a2013-03-15 19:06:39 -070073 /**
74 * Left coordinate of the path bounds.
75 */
Chris Craike2bb3802015-03-13 15:07:52 -070076 float left = 0;
Romain Guyc46d07a2013-03-15 19:06:39 -070077 /**
78 * Top coordinate of the path bounds.
79 */
Chris Craike2bb3802015-03-13 15:07:52 -070080 float top = 0;
Romain Guyc46d07a2013-03-15 19:06:39 -070081 /**
82 * Offset to draw the path at the correct origin.
83 */
Chris Craike2bb3802015-03-13 15:07:52 -070084 float offset = 0;
Romain Guyc46d07a2013-03-15 19:06:39 -070085
86 sp<Task<SkBitmap*> > task() const {
87 return mTask;
Romain Guy059e12c2012-11-28 17:35:51 -080088 }
89
Romain Guyc46d07a2013-03-15 19:06:39 -070090 void setTask(const sp<Task<SkBitmap*> >& task) {
91 mTask = task;
Romain Guy7fbcc042010-08-04 15:40:07 -070092 }
Romain Guy7fbcc042010-08-04 15:40:07 -070093
Romain Guyc46d07a2013-03-15 19:06:39 -070094 void clearTask() {
Chris Craike84a2082014-12-22 14:28:49 -080095 if (mTask != nullptr) {
Romain Guyc46d07a2013-03-15 19:06:39 -070096 mTask.clear();
97 }
98 }
Romain Guyb29cfbf2011-03-18 16:24:19 -070099
Romain Guyc46d07a2013-03-15 19:06:39 -0700100private:
101 sp<Task<SkBitmap*> > mTask;
102}; // struct PathTexture
Romain Guy7fbcc042010-08-04 15:40:07 -0700103
sergeyv7224e2b2016-04-07 18:06:53 -0700104enum class ShapeType {
105 None,
106 Rect,
107 RoundRect,
108 Circle,
109 Oval,
110 Arc,
111 Path
Romain Guyc46d07a2013-03-15 19:06:39 -0700112};
113
114struct PathDescription {
sergeyv7224e2b2016-04-07 18:06:53 -0700115 HASHABLE_TYPE(PathDescription);
Romain Guyc46d07a2013-03-15 19:06:39 -0700116 ShapeType type;
117 SkPaint::Join join;
118 SkPaint::Cap cap;
119 SkPaint::Style style;
120 float miter;
121 float strokeWidth;
122 SkPathEffect* pathEffect;
123 union Shape {
124 struct Path {
Derek Sollenbergeree248592015-02-12 14:10:21 -0500125 uint32_t mGenerationID;
Romain Guyc46d07a2013-03-15 19:06:39 -0700126 } path;
127 struct RoundRect {
128 float mWidth;
129 float mHeight;
130 float mRx;
131 float mRy;
132 } roundRect;
133 struct Circle {
134 float mRadius;
135 } circle;
136 struct Oval {
137 float mWidth;
138 float mHeight;
139 } oval;
140 struct Rect {
141 float mWidth;
142 float mHeight;
143 } rect;
144 struct Arc {
145 float mWidth;
146 float mHeight;
147 float mStartAngle;
148 float mSweepAngle;
149 bool mUseCenter;
150 } arc;
151 } shape;
152
153 PathDescription();
Chris Craikd218a922014-01-02 17:13:34 -0800154 PathDescription(ShapeType shapeType, const SkPaint* paint);
Romain Guyc46d07a2013-03-15 19:06:39 -0700155};
Romain Guy059e12c2012-11-28 17:35:51 -0800156
Romain Guy7fbcc042010-08-04 15:40:07 -0700157/**
Romain Guyc46d07a2013-03-15 19:06:39 -0700158 * A simple LRU shape cache. The cache has a maximum size expressed in bytes.
Romain Guy7fbcc042010-08-04 15:40:07 -0700159 * Any texture added to the cache causing the cache to grow beyond the maximum
160 * allowed size will also cause the oldest texture to be kicked out.
161 */
Romain Guyc46d07a2013-03-15 19:06:39 -0700162class PathCache: public OnEntryRemoved<PathDescription, PathTexture*> {
Romain Guy7fbcc042010-08-04 15:40:07 -0700163public:
Romain Guyfb8b7632010-08-23 21:05:08 -0700164 PathCache();
Romain Guyca89e2a2013-03-08 17:44:20 -0800165 ~PathCache();
Romain Guy7fbcc042010-08-04 15:40:07 -0700166
167 /**
Romain Guyc46d07a2013-03-15 19:06:39 -0700168 * Used as a callback when an entry is removed from the cache.
169 * Do not invoke directly.
Romain Guy7fbcc042010-08-04 15:40:07 -0700170 */
Chris Craike84a2082014-12-22 14:28:49 -0800171 void operator()(PathDescription& path, PathTexture*& texture) override;
Romain Guyc46d07a2013-03-15 19:06:39 -0700172
173 /**
174 * Clears the cache. This causes all textures to be deleted.
175 */
176 void clear();
177
178 /**
Romain Guyc46d07a2013-03-15 19:06:39 -0700179 * Returns the maximum size of the cache in bytes.
180 */
181 uint32_t getMaxSize();
182 /**
183 * Returns the current size of the cache in bytes.
184 */
185 uint32_t getSize();
186
Chris Craikd218a922014-01-02 17:13:34 -0800187 PathTexture* getRoundRect(float width, float height, float rx, float ry, const SkPaint* paint);
188 PathTexture* getCircle(float radius, const SkPaint* paint);
189 PathTexture* getOval(float width, float height, const SkPaint* paint);
190 PathTexture* getRect(float width, float height, const SkPaint* paint);
Romain Guyc46d07a2013-03-15 19:06:39 -0700191 PathTexture* getArc(float width, float height, float startAngle, float sweepAngle,
Chris Craikd218a922014-01-02 17:13:34 -0800192 bool useCenter, const SkPaint* paint);
193 PathTexture* get(const SkPath* path, const SkPaint* paint);
Digish Pandya2e4f67c2015-11-04 11:00:28 +0530194 void remove(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 */
Derek Sollenbergeree248592015-02-12 14:10:21 -0500200 ANDROID_API void removeDeferred(const SkPath* path);
Romain Guyfe48f652010-11-11 15:36:56 -0800201 /**
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
Romain Guy7fbcc042010-08-04 15:40:07 -0700222private:
Romain Guyc46d07a2013-03-15 19:06:39 -0700223 PathTexture* addTexture(const PathDescription& entry,
224 const SkPath *path, const SkPaint* paint);
225 PathTexture* addTexture(const PathDescription& entry, SkBitmap* bitmap);
Romain Guy4500a8d2013-03-26 17:29:51 -0700226
227 /**
228 * Generates the texture from a bitmap into the specified texture structure.
229 */
230 void generateTexture(SkBitmap& bitmap, Texture* texture);
231 void generateTexture(const PathDescription& entry, SkBitmap* bitmap, PathTexture* texture,
232 bool addToCache = true);
Romain Guyc46d07a2013-03-15 19:06:39 -0700233
234 PathTexture* get(const PathDescription& entry) {
235 return mCache.get(entry);
236 }
237
238 /**
239 * Ensures there is enough space in the cache for a texture of the specified
240 * dimensions.
241 */
242 void purgeCache(uint32_t width, uint32_t height);
243
244 void removeTexture(PathTexture* texture);
245
Romain Guyc46d07a2013-03-15 19:06:39 -0700246 void init();
247
Romain Guy5dc7fa72013-03-11 20:48:31 -0700248 class PathTask: public Task<SkBitmap*> {
Romain Guyca89e2a2013-03-08 17:44:20 -0800249 public:
Chris Craikd218a922014-01-02 17:13:34 -0800250 PathTask(const SkPath* path, const SkPaint* paint, PathTexture* texture):
Chris Craik906d47f2014-06-27 18:30:23 -0700251 path(*path), paint(*paint), texture(texture) {
Romain Guy5dc7fa72013-03-11 20:48:31 -0700252 }
Romain Guyca89e2a2013-03-08 17:44:20 -0800253
Romain Guy5dc7fa72013-03-11 20:48:31 -0700254 ~PathTask() {
255 delete future()->get();
256 }
Romain Guyca89e2a2013-03-08 17:44:20 -0800257
Derek Sollenbergeree248592015-02-12 14:10:21 -0500258 // copied, since input path not guaranteed to survive for duration of task
Chris Craik906d47f2014-06-27 18:30:23 -0700259 const SkPath path;
260
261 // copied, since input paint may not be immutable
Kenny Root25e40de2014-05-30 11:51:20 -0700262 const SkPaint paint;
Romain Guy5dc7fa72013-03-11 20:48:31 -0700263 PathTexture* texture;
Romain Guyca89e2a2013-03-08 17:44:20 -0800264 };
265
Romain Guy5dc7fa72013-03-11 20:48:31 -0700266 class PathProcessor: public TaskProcessor<SkBitmap*> {
267 public:
Chih-Hung Hsiehfaecb782016-07-21 11:23:06 -0700268 explicit PathProcessor(Caches& caches);
Romain Guy5dc7fa72013-03-11 20:48:31 -0700269 ~PathProcessor() { }
270
Chris Craike84a2082014-12-22 14:28:49 -0800271 virtual void onProcess(const sp<Task<SkBitmap*> >& task) override;
Romain Guy5dc7fa72013-03-11 20:48:31 -0700272
273 private:
274 uint32_t mMaxTextureSize;
275 };
276
Romain Guyc46d07a2013-03-15 19:06:39 -0700277 LruCache<PathDescription, PathTexture*> mCache;
278 uint32_t mSize;
Chris Craik48a8f432016-02-05 15:59:29 -0800279 const uint32_t mMaxSize;
Romain Guyc46d07a2013-03-15 19:06:39 -0700280 GLuint mMaxTextureSize;
281
282 bool mDebugEnabled;
283
Romain Guy5dc7fa72013-03-11 20:48:31 -0700284 sp<PathProcessor> mProcessor;
Romain Guyc5cbee72013-03-20 19:15:02 -0700285
John Reck272a6852015-07-29 16:48:58 -0700286 std::vector<uint32_t> mGarbage;
Romain Guya2341a92010-09-08 18:04:33 -0700287 mutable Mutex mLock;
Romain Guy7fbcc042010-08-04 15:40:07 -0700288}; // class PathCache
289
290}; // namespace uirenderer
291}; // namespace android
292
Romain Guy5b3b3522010-10-27 18:57:51 -0700293#endif // ANDROID_HWUI_PATH_CACHE_H