blob: b54666b7d595d2461817182b9963cac80690cd66 [file] [log] [blame]
Chris Craik05f3d6e2014-06-02 16:27:04 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
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
17#ifndef ANDROID_HWUI_TESSELLATION_CACHE_H
18#define ANDROID_HWUI_TESSELLATION_CACHE_H
19
20#include <utils/LruCache.h>
21#include <utils/Mutex.h>
Chris Craik05f3d6e2014-06-02 16:27:04 -070022
23#include "Debug.h"
24#include "utils/Macros.h"
25#include "utils/Pair.h"
Chris Craik05f3d6e2014-06-02 16:27:04 -070026
27class SkBitmap;
28class SkCanvas;
29class SkPaint;
30class SkPath;
31struct SkRect;
32
33namespace android {
34namespace uirenderer {
35
36class Caches;
Tom Hudson2dc236b2014-10-15 15:46:42 -040037class VertexBuffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -070038
39///////////////////////////////////////////////////////////////////////////////
40// Classes
41///////////////////////////////////////////////////////////////////////////////
42
43class TessellationCache {
44public:
45 typedef Pair<VertexBuffer*, VertexBuffer*> vertexBuffer_pair_t;
46
47 struct Description {
48 DESCRIPTION_TYPE(Description);
49 enum Type {
50 kNone,
51 kRoundRect,
Chris Craik05f3d6e2014-06-02 16:27:04 -070052 };
53
54 Type type;
Chris Craik6ac174b2014-06-17 13:47:05 -070055 float scaleX;
56 float scaleY;
Chris Craiked4ef0b2014-06-12 13:27:30 -070057 bool aa;
Chris Craik05f3d6e2014-06-02 16:27:04 -070058 SkPaint::Cap cap;
59 SkPaint::Style style;
60 float strokeWidth;
61 union Shape {
62 struct RoundRect {
Chris Craik6ac174b2014-06-17 13:47:05 -070063 float width;
64 float height;
65 float rx;
66 float ry;
Chris Craik05f3d6e2014-06-02 16:27:04 -070067 } roundRect;
68 } shape;
69
70 Description();
Chris Craik6ac174b2014-06-17 13:47:05 -070071 Description(Type type, const Matrix4& transform, const SkPaint& paint);
Chris Craik05f3d6e2014-06-02 16:27:04 -070072 hash_t hash() const;
Chris Craik6ac174b2014-06-17 13:47:05 -070073 void setupMatrixAndPaint(Matrix4* matrix, SkPaint* paint) const;
Chris Craik05f3d6e2014-06-02 16:27:04 -070074 };
75
76 struct ShadowDescription {
77 DESCRIPTION_TYPE(ShadowDescription);
78 const void* nodeKey;
79 float matrixData[16];
80
81 ShadowDescription();
82 ShadowDescription(const void* nodeKey, const Matrix4* drawTransform);
83 hash_t hash() const;
84 };
85
86 TessellationCache();
87 ~TessellationCache();
88
89 /**
90 * Clears the cache. This causes all TessellationBuffers to be deleted.
91 */
92 void clear();
93
94 /**
95 * Sets the maximum size of the cache in bytes.
96 */
97 void setMaxSize(uint32_t maxSize);
98 /**
99 * Returns the maximum size of the cache in bytes.
100 */
101 uint32_t getMaxSize();
102 /**
103 * Returns the current size of the cache in bytes.
104 */
105 uint32_t getSize();
106
107 /**
108 * Trims the contents of the cache, removing items until it's under its
109 * specified limit.
110 *
111 * Trimming is used for caches that support pre-caching from a worker
112 * thread. During pre-caching the maximum limit of the cache can be
113 * exceeded for the duration of the frame. It is therefore required to
114 * trim the cache at the end of the frame to keep the total amount of
115 * memory used under control.
116 *
117 * Also removes transient Shadow VertexBuffers, which aren't cached between frames.
118 */
119 void trim();
120
121 // TODO: precache/get for Oval, Lines, Points, etc.
122
Chris Craik6ac174b2014-06-17 13:47:05 -0700123 void precacheRoundRect(const Matrix4& transform, const SkPaint& paint,
124 float width, float height, float rx, float ry) {
125 getRoundRectBuffer(transform, paint, width, height, rx, ry);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700126 }
Chris Craik6ac174b2014-06-17 13:47:05 -0700127 const VertexBuffer* getRoundRect(const Matrix4& transform, const SkPaint& paint,
128 float width, float height, float rx, float ry);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700129
130 void precacheShadows(const Matrix4* drawTransform, const Rect& localClip,
131 bool opaque, const SkPath* casterPerimeter,
132 const Matrix4* transformXY, const Matrix4* transformZ,
133 const Vector3& lightCenter, float lightRadius);
134
135 void getShadowBuffers(const Matrix4* drawTransform, const Rect& localClip,
136 bool opaque, const SkPath* casterPerimeter,
137 const Matrix4* transformXY, const Matrix4* transformZ,
138 const Vector3& lightCenter, float lightRadius,
139 vertexBuffer_pair_t& outBuffers);
140
141private:
142 class Buffer;
143 class TessellationTask;
144 class TessellationProcessor;
145
Chris Craik6ac174b2014-06-17 13:47:05 -0700146 typedef VertexBuffer* (*Tessellator)(const Description&);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700147
Chris Craik6ac174b2014-06-17 13:47:05 -0700148 Buffer* getRectBuffer(const Matrix4& transform, const SkPaint& paint,
149 float width, float height);
150 Buffer* getRoundRectBuffer(const Matrix4& transform, const SkPaint& paint,
151 float width, float height, float rx, float ry);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700152
Chris Craik6ac174b2014-06-17 13:47:05 -0700153 Buffer* getOrCreateBuffer(const Description& entry, Tessellator tessellator);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700154
155 uint32_t mSize;
156 uint32_t mMaxSize;
157
158 bool mDebugEnabled;
159
160 mutable Mutex mLock;
161
162 ///////////////////////////////////////////////////////////////////////////////
163 // General tessellation caching
164 ///////////////////////////////////////////////////////////////////////////////
165 sp<TaskProcessor<VertexBuffer*> > mProcessor;
166 LruCache<Description, Buffer*> mCache;
167 class BufferRemovedListener : public OnEntryRemoved<Description, Buffer*> {
Chris Craike84a2082014-12-22 14:28:49 -0800168 void operator()(Description& description, Buffer*& buffer) override;
Chris Craik05f3d6e2014-06-02 16:27:04 -0700169 };
170 BufferRemovedListener mBufferRemovedListener;
171
172 ///////////////////////////////////////////////////////////////////////////////
173 // Shadow tessellation caching
174 ///////////////////////////////////////////////////////////////////////////////
175 sp<TaskProcessor<vertexBuffer_pair_t*> > mShadowProcessor;
176
177 // holds a pointer, and implicit strong ref to each shadow task of the frame
178 LruCache<ShadowDescription, Task<vertexBuffer_pair_t*>*> mShadowCache;
179 class BufferPairRemovedListener : public OnEntryRemoved<ShadowDescription, Task<vertexBuffer_pair_t*>*> {
Chris Craike84a2082014-12-22 14:28:49 -0800180 void operator()(ShadowDescription& description, Task<vertexBuffer_pair_t*>*& bufferPairTask) override {
181 bufferPairTask->decStrong(nullptr);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700182 }
183 };
184 BufferPairRemovedListener mBufferPairRemovedListener;
185
186}; // class TessellationCache
187
188}; // namespace uirenderer
189}; // namespace android
190
191#endif // ANDROID_HWUI_PATH_CACHE_H