blob: e925848a25b8e233dee51d4f6fc3fbe40c2f98db [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, float left, float top,
John Reck38e0c322015-11-10 12:19:17 -080065 float offset, int generation)
Chris Craike2bb3802015-03-13 15:07:52 -070066 : Texture(caches)
67 , left(left)
68 , top(top)
69 , offset(offset) {
Chris Craike2bb3802015-03-13 15:07:52 -070070 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
sergeyv7224e2b2016-04-07 18:06:53 -0700112enum class ShapeType {
113 None,
114 Rect,
115 RoundRect,
116 Circle,
117 Oval,
118 Arc,
119 Path
Romain Guyc46d07a2013-03-15 19:06:39 -0700120};
121
122struct PathDescription {
sergeyv7224e2b2016-04-07 18:06:53 -0700123 HASHABLE_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};
Romain Guy059e12c2012-11-28 17:35:51 -0800164
Romain Guy7fbcc042010-08-04 15:40:07 -0700165/**
Romain Guyc46d07a2013-03-15 19:06:39 -0700166 * A simple LRU shape cache. The cache has a maximum size expressed in bytes.
Romain Guy7fbcc042010-08-04 15:40:07 -0700167 * Any texture added to the cache causing the cache to grow beyond the maximum
168 * allowed size will also cause the oldest texture to be kicked out.
169 */
Romain Guyc46d07a2013-03-15 19:06:39 -0700170class PathCache: public OnEntryRemoved<PathDescription, PathTexture*> {
Romain Guy7fbcc042010-08-04 15:40:07 -0700171public:
Romain Guyfb8b7632010-08-23 21:05:08 -0700172 PathCache();
Romain Guyca89e2a2013-03-08 17:44:20 -0800173 ~PathCache();
Romain Guy7fbcc042010-08-04 15:40:07 -0700174
175 /**
Romain Guyc46d07a2013-03-15 19:06:39 -0700176 * Used as a callback when an entry is removed from the cache.
177 * Do not invoke directly.
Romain Guy7fbcc042010-08-04 15:40:07 -0700178 */
Chris Craike84a2082014-12-22 14:28:49 -0800179 void operator()(PathDescription& path, PathTexture*& texture) override;
Romain Guyc46d07a2013-03-15 19:06:39 -0700180
181 /**
182 * Clears the cache. This causes all textures to be deleted.
183 */
184 void clear();
185
186 /**
Romain Guyc46d07a2013-03-15 19:06:39 -0700187 * Returns the maximum size of the cache in bytes.
188 */
189 uint32_t getMaxSize();
190 /**
191 * Returns the current size of the cache in bytes.
192 */
193 uint32_t getSize();
194
Chris Craikd218a922014-01-02 17:13:34 -0800195 PathTexture* getRoundRect(float width, float height, float rx, float ry, const SkPaint* paint);
196 PathTexture* getCircle(float radius, const SkPaint* paint);
197 PathTexture* getOval(float width, float height, const SkPaint* paint);
198 PathTexture* getRect(float width, float height, const SkPaint* paint);
Romain Guyc46d07a2013-03-15 19:06:39 -0700199 PathTexture* getArc(float width, float height, float startAngle, float sweepAngle,
Chris Craikd218a922014-01-02 17:13:34 -0800200 bool useCenter, const SkPaint* paint);
201 PathTexture* get(const SkPath* path, const SkPaint* paint);
Digish Pandya2e4f67c2015-11-04 11:00:28 +0530202 void remove(const SkPath* path, const SkPaint* paint);
Romain Guyc46d07a2013-03-15 19:06:39 -0700203
Romain Guy7fbcc042010-08-04 15:40:07 -0700204 /**
Romain Guyfe48f652010-11-11 15:36:56 -0800205 * Removes the specified path. This is meant to be called from threads
206 * that are not the EGL context thread.
207 */
Derek Sollenbergeree248592015-02-12 14:10:21 -0500208 ANDROID_API void removeDeferred(const SkPath* path);
Romain Guyfe48f652010-11-11 15:36:56 -0800209 /**
210 * Process deferred removals.
211 */
212 void clearGarbage();
Romain Guyc46d07a2013-03-15 19:06:39 -0700213 /**
214 * Trims the contents of the cache, removing items until it's under its
215 * specified limit.
216 *
217 * Trimming is used for caches that support pre-caching from a worker
218 * thread. During pre-caching the maximum limit of the cache can be
219 * exceeded for the duration of the frame. It is therefore required to
220 * trim the cache at the end of the frame to keep the total amount of
221 * memory used under control.
222 */
223 void trim();
Romain Guy7fbcc042010-08-04 15:40:07 -0700224
Romain Guyc46d07a2013-03-15 19:06:39 -0700225 /**
226 * Precaches the specified path using background threads.
227 */
Chris Craikd218a922014-01-02 17:13:34 -0800228 void precache(const SkPath* path, const SkPaint* paint);
Romain Guyca89e2a2013-03-08 17:44:20 -0800229
Chris Craikd218a922014-01-02 17:13:34 -0800230 static bool canDrawAsConvexPath(SkPath* path, const SkPaint* paint);
Romain Guyc46d07a2013-03-15 19:06:39 -0700231 static void computePathBounds(const SkPath* path, const SkPaint* paint,
232 float& left, float& top, float& offset, uint32_t& width, uint32_t& height);
233 static void computeBounds(const SkRect& bounds, const SkPaint* paint,
234 float& left, float& top, float& offset, uint32_t& width, uint32_t& height);
235
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);
239 PathTexture* addTexture(const PathDescription& entry, SkBitmap* bitmap);
Romain Guy4500a8d2013-03-26 17:29:51 -0700240
241 /**
242 * Generates the texture from a bitmap into the specified texture structure.
243 */
244 void generateTexture(SkBitmap& bitmap, Texture* texture);
245 void generateTexture(const PathDescription& entry, SkBitmap* bitmap, PathTexture* texture,
246 bool addToCache = true);
Romain Guyc46d07a2013-03-15 19:06:39 -0700247
248 PathTexture* get(const PathDescription& entry) {
249 return mCache.get(entry);
250 }
251
252 /**
253 * 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
Derek Sollenbergeree248592015-02-12 14:10:21 -0500281 // copied, since input path not guaranteed to survive for duration of task
Chris Craik906d47f2014-06-27 18:30:23 -0700282 const SkPath path;
283
284 // copied, since input paint may not be immutable
Kenny Root25e40de2014-05-30 11:51:20 -0700285 const SkPaint paint;
Romain Guy5dc7fa72013-03-11 20:48:31 -0700286 PathTexture* texture;
Romain Guyca89e2a2013-03-08 17:44:20 -0800287 };
288
Romain Guy5dc7fa72013-03-11 20:48:31 -0700289 class PathProcessor: public TaskProcessor<SkBitmap*> {
290 public:
Chih-Hung Hsiehfaecb782016-07-21 11:23:06 -0700291 explicit PathProcessor(Caches& caches);
Romain Guy5dc7fa72013-03-11 20:48:31 -0700292 ~PathProcessor() { }
293
Chris Craike84a2082014-12-22 14:28:49 -0800294 virtual void onProcess(const sp<Task<SkBitmap*> >& task) override;
Romain Guy5dc7fa72013-03-11 20:48:31 -0700295
296 private:
297 uint32_t mMaxTextureSize;
298 };
299
Romain Guyc46d07a2013-03-15 19:06:39 -0700300 LruCache<PathDescription, PathTexture*> mCache;
301 uint32_t mSize;
Chris Craik48a8f432016-02-05 15:59:29 -0800302 const uint32_t mMaxSize;
Romain Guyc46d07a2013-03-15 19:06:39 -0700303 GLuint mMaxTextureSize;
304
305 bool mDebugEnabled;
306
caiqinl4b505372016-06-24 13:37:46 +0800307 /**
308 * Driver allocated 4k/8k/16k memory for small path cache,
309 * limit the number of PathTexture in case occupy too much memory in hardware.
310 */
311 uint32_t mTexNum;
312
Romain Guy5dc7fa72013-03-11 20:48:31 -0700313 sp<PathProcessor> mProcessor;
Romain Guyc5cbee72013-03-20 19:15:02 -0700314
John Reck272a6852015-07-29 16:48:58 -0700315 std::vector<uint32_t> mGarbage;
Romain Guya2341a92010-09-08 18:04:33 -0700316 mutable Mutex mLock;
Romain Guy7fbcc042010-08-04 15:40:07 -0700317}; // class PathCache
318
319}; // namespace uirenderer
320}; // namespace android
321
Romain Guy5b3b3522010-10-27 18:57:51 -0700322#endif // ANDROID_HWUI_PATH_CACHE_H