blob: 7bd190df951b5d8a2e461c7b139dd083a9015e0e [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"
sergeyv98fa4f92016-10-24 15:35:21 -070022#include "hwui/Bitmap.h"
Chris Craik96a5c4c2015-01-27 15:46:35 -080023#include "thread/Task.h"
24#include "thread/TaskProcessor.h"
25#include "utils/Macros.h"
26#include "utils/Pair.h"
Romain Guyc46d07a2013-03-15 19:06:39 -070027
Chris Craik96a5c4c2015-01-27 15:46:35 -080028#include <GLES2/gl2.h>
sergeyv7224e2b2016-04-07 18:06:53 -070029#include <SkPaint.h>
Chris Craik96a5c4c2015-01-27 15:46:35 -080030#include <SkPath.h>
Romain Guyc46d07a2013-03-15 19:06:39 -070031#include <utils/LruCache.h>
32#include <utils/Mutex.h>
John Reck272a6852015-07-29 16:48:58 -070033
34#include <vector>
Romain Guyfe48f652010-11-11 15:36:56 -080035
Romain Guyc46d07a2013-03-15 19:06:39 -070036class 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;
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
sergeyv98fa4f92016-10-24 15:35:21 -070059struct PathTexture;
60class PathTask: public Task<sk_sp<Bitmap>> {
61public:
62 PathTask(const SkPath* path, const SkPaint* paint, PathTexture* texture):
63 path(*path), paint(*paint), texture(texture) {
64 }
65
66 // copied, since input path not guaranteed to survive for duration of task
67 const SkPath path;
68
69 // copied, since input paint may not be immutable
70 const SkPaint paint;
71 PathTexture* texture;
72};
73
Romain Guyc46d07a2013-03-15 19:06:39 -070074/**
75 * Alpha texture used to represent a path.
76 */
77struct PathTexture: public Texture {
Chris Craike2bb3802015-03-13 15:07:52 -070078 PathTexture(Caches& caches, int generation)
79 : Texture(caches) {
80 this->generation = generation;
Romain Guyff26a0c2011-01-20 11:35:46 -080081 }
82
Romain Guyc46d07a2013-03-15 19:06:39 -070083 ~PathTexture() {
84 clearTask();
Romain Guy7fbcc042010-08-04 15:40:07 -070085 }
86
Romain Guyc46d07a2013-03-15 19:06:39 -070087 /**
88 * Left coordinate of the path bounds.
89 */
Chris Craike2bb3802015-03-13 15:07:52 -070090 float left = 0;
Romain Guyc46d07a2013-03-15 19:06:39 -070091 /**
92 * Top coordinate of the path bounds.
93 */
Chris Craike2bb3802015-03-13 15:07:52 -070094 float top = 0;
Romain Guyc46d07a2013-03-15 19:06:39 -070095 /**
96 * Offset to draw the path at the correct origin.
97 */
Chris Craike2bb3802015-03-13 15:07:52 -070098 float offset = 0;
Romain Guyc46d07a2013-03-15 19:06:39 -070099
sergeyv98fa4f92016-10-24 15:35:21 -0700100 sp<PathTask> task() const {
Romain Guyc46d07a2013-03-15 19:06:39 -0700101 return mTask;
Romain Guy059e12c2012-11-28 17:35:51 -0800102 }
103
sergeyv98fa4f92016-10-24 15:35:21 -0700104 void setTask(const sp<PathTask>& task) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700105 mTask = task;
Romain Guy7fbcc042010-08-04 15:40:07 -0700106 }
Romain Guy7fbcc042010-08-04 15:40:07 -0700107
Romain Guyc46d07a2013-03-15 19:06:39 -0700108 void clearTask() {
Chris Craike84a2082014-12-22 14:28:49 -0800109 if (mTask != nullptr) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700110 mTask.clear();
111 }
112 }
Romain Guyb29cfbf2011-03-18 16:24:19 -0700113
Romain Guyc46d07a2013-03-15 19:06:39 -0700114private:
sergeyv98fa4f92016-10-24 15:35:21 -0700115 sp<PathTask> mTask;
Romain Guyc46d07a2013-03-15 19:06:39 -0700116}; // struct PathTexture
Romain Guy7fbcc042010-08-04 15:40:07 -0700117
sergeyv7224e2b2016-04-07 18:06:53 -0700118enum class ShapeType {
119 None,
120 Rect,
121 RoundRect,
122 Circle,
123 Oval,
124 Arc,
125 Path
Romain Guyc46d07a2013-03-15 19:06:39 -0700126};
127
128struct PathDescription {
sergeyv7224e2b2016-04-07 18:06:53 -0700129 HASHABLE_TYPE(PathDescription);
Romain Guyc46d07a2013-03-15 19:06:39 -0700130 ShapeType type;
131 SkPaint::Join join;
132 SkPaint::Cap cap;
133 SkPaint::Style style;
134 float miter;
135 float strokeWidth;
136 SkPathEffect* pathEffect;
137 union Shape {
138 struct Path {
Derek Sollenbergeree248592015-02-12 14:10:21 -0500139 uint32_t mGenerationID;
Romain Guyc46d07a2013-03-15 19:06:39 -0700140 } path;
141 struct RoundRect {
142 float mWidth;
143 float mHeight;
144 float mRx;
145 float mRy;
146 } roundRect;
147 struct Circle {
148 float mRadius;
149 } circle;
150 struct Oval {
151 float mWidth;
152 float mHeight;
153 } oval;
154 struct Rect {
155 float mWidth;
156 float mHeight;
157 } rect;
158 struct Arc {
159 float mWidth;
160 float mHeight;
161 float mStartAngle;
162 float mSweepAngle;
163 bool mUseCenter;
164 } arc;
165 } shape;
166
167 PathDescription();
Chris Craikd218a922014-01-02 17:13:34 -0800168 PathDescription(ShapeType shapeType, const SkPaint* paint);
Romain Guyc46d07a2013-03-15 19:06:39 -0700169};
Romain Guy059e12c2012-11-28 17:35:51 -0800170
Romain Guy7fbcc042010-08-04 15:40:07 -0700171/**
Romain Guyc46d07a2013-03-15 19:06:39 -0700172 * A simple LRU shape cache. The cache has a maximum size expressed in bytes.
Romain Guy7fbcc042010-08-04 15:40:07 -0700173 * Any texture added to the cache causing the cache to grow beyond the maximum
174 * allowed size will also cause the oldest texture to be kicked out.
175 */
Romain Guyc46d07a2013-03-15 19:06:39 -0700176class PathCache: public OnEntryRemoved<PathDescription, PathTexture*> {
Romain Guy7fbcc042010-08-04 15:40:07 -0700177public:
Romain Guyfb8b7632010-08-23 21:05:08 -0700178 PathCache();
Romain Guyca89e2a2013-03-08 17:44:20 -0800179 ~PathCache();
Romain Guy7fbcc042010-08-04 15:40:07 -0700180
181 /**
Romain Guyc46d07a2013-03-15 19:06:39 -0700182 * Used as a callback when an entry is removed from the cache.
183 * Do not invoke directly.
Romain Guy7fbcc042010-08-04 15:40:07 -0700184 */
Chris Craike84a2082014-12-22 14:28:49 -0800185 void operator()(PathDescription& path, PathTexture*& texture) override;
Romain Guyc46d07a2013-03-15 19:06:39 -0700186
187 /**
188 * Clears the cache. This causes all textures to be deleted.
189 */
190 void clear();
191
192 /**
Romain Guyc46d07a2013-03-15 19:06:39 -0700193 * Returns the maximum size of the cache in bytes.
194 */
195 uint32_t getMaxSize();
196 /**
197 * Returns the current size of the cache in bytes.
198 */
199 uint32_t getSize();
200
Chris Craikd218a922014-01-02 17:13:34 -0800201 PathTexture* getRoundRect(float width, float height, float rx, float ry, const SkPaint* paint);
202 PathTexture* getCircle(float radius, const SkPaint* paint);
203 PathTexture* getOval(float width, float height, const SkPaint* paint);
204 PathTexture* getRect(float width, float height, const SkPaint* paint);
Romain Guyc46d07a2013-03-15 19:06:39 -0700205 PathTexture* getArc(float width, float height, float startAngle, float sweepAngle,
Chris Craikd218a922014-01-02 17:13:34 -0800206 bool useCenter, const SkPaint* paint);
207 PathTexture* get(const SkPath* path, const SkPaint* paint);
Digish Pandya2e4f67c2015-11-04 11:00:28 +0530208 void remove(const SkPath* path, const SkPaint* paint);
Romain Guyc46d07a2013-03-15 19:06:39 -0700209
Romain Guy7fbcc042010-08-04 15:40:07 -0700210 /**
Romain Guyfe48f652010-11-11 15:36:56 -0800211 * Removes the specified path. This is meant to be called from threads
212 * that are not the EGL context thread.
213 */
Derek Sollenbergeree248592015-02-12 14:10:21 -0500214 ANDROID_API void removeDeferred(const SkPath* path);
Romain Guyfe48f652010-11-11 15:36:56 -0800215 /**
216 * Process deferred removals.
217 */
218 void clearGarbage();
Romain Guyc46d07a2013-03-15 19:06:39 -0700219 /**
220 * Trims the contents of the cache, removing items until it's under its
221 * specified limit.
222 *
223 * Trimming is used for caches that support pre-caching from a worker
224 * thread. During pre-caching the maximum limit of the cache can be
225 * exceeded for the duration of the frame. It is therefore required to
226 * trim the cache at the end of the frame to keep the total amount of
227 * memory used under control.
228 */
229 void trim();
Romain Guy7fbcc042010-08-04 15:40:07 -0700230
Romain Guyc46d07a2013-03-15 19:06:39 -0700231 /**
232 * Precaches the specified path using background threads.
233 */
Chris Craikd218a922014-01-02 17:13:34 -0800234 void precache(const SkPath* path, const SkPaint* paint);
Romain Guyca89e2a2013-03-08 17:44:20 -0800235
Romain Guy7fbcc042010-08-04 15:40:07 -0700236private:
Romain Guyc46d07a2013-03-15 19:06:39 -0700237 PathTexture* addTexture(const PathDescription& entry,
238 const SkPath *path, const SkPaint* paint);
Romain Guy4500a8d2013-03-26 17:29:51 -0700239
240 /**
241 * Generates the texture from a bitmap into the specified texture structure.
242 */
sergeyv98fa4f92016-10-24 15:35:21 -0700243 void generateTexture(Bitmap& bitmap, Texture* texture);
244 void generateTexture(const PathDescription& entry, Bitmap& bitmap, PathTexture* texture,
Romain Guy4500a8d2013-03-26 17:29:51 -0700245 bool addToCache = true);
Romain Guyc46d07a2013-03-15 19:06:39 -0700246
247 PathTexture* get(const PathDescription& entry) {
248 return mCache.get(entry);
249 }
250
251 /**
252 * Ensures there is enough space in the cache for a texture of the specified
253 * dimensions.
254 */
255 void purgeCache(uint32_t width, uint32_t height);
256
257 void removeTexture(PathTexture* texture);
258
Romain Guyc46d07a2013-03-15 19:06:39 -0700259 void init();
260
Romain Guyca89e2a2013-03-08 17:44:20 -0800261
sergeyv98fa4f92016-10-24 15:35:21 -0700262 class PathProcessor: public TaskProcessor<sk_sp<Bitmap> > {
Romain Guy5dc7fa72013-03-11 20:48:31 -0700263 public:
Chih-Hung Hsiehfaecb782016-07-21 11:23:06 -0700264 explicit PathProcessor(Caches& caches);
Romain Guy5dc7fa72013-03-11 20:48:31 -0700265 ~PathProcessor() { }
266
sergeyv98fa4f92016-10-24 15:35:21 -0700267 virtual void onProcess(const sp<Task<sk_sp<Bitmap> > >& task) override;
Romain Guy5dc7fa72013-03-11 20:48:31 -0700268
269 private:
270 uint32_t mMaxTextureSize;
271 };
272
Romain Guyc46d07a2013-03-15 19:06:39 -0700273 LruCache<PathDescription, PathTexture*> mCache;
274 uint32_t mSize;
Chris Craik48a8f432016-02-05 15:59:29 -0800275 const uint32_t mMaxSize;
Romain Guyc46d07a2013-03-15 19:06:39 -0700276 GLuint mMaxTextureSize;
277
278 bool mDebugEnabled;
279
Romain Guy5dc7fa72013-03-11 20:48:31 -0700280 sp<PathProcessor> mProcessor;
Romain Guyc5cbee72013-03-20 19:15:02 -0700281
John Reck272a6852015-07-29 16:48:58 -0700282 std::vector<uint32_t> mGarbage;
Romain Guya2341a92010-09-08 18:04:33 -0700283 mutable Mutex mLock;
Romain Guy7fbcc042010-08-04 15:40:07 -0700284}; // class PathCache
285
286}; // namespace uirenderer
287}; // namespace android
288
Romain Guy5b3b3522010-10-27 18:57:51 -0700289#endif // ANDROID_HWUI_PATH_CACHE_H