blob: 8f37230d7eaf26f816ac919cec287e75e76dfd94 [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>
22#include <utils/Vector.h>
23
24#include "Debug.h"
25#include "utils/Macros.h"
26#include "utils/Pair.h"
27#include "VertexBuffer.h"
28
29class SkBitmap;
30class SkCanvas;
31class SkPaint;
32class SkPath;
33struct SkRect;
34
35namespace android {
36namespace uirenderer {
37
38class Caches;
39
40///////////////////////////////////////////////////////////////////////////////
41// Classes
42///////////////////////////////////////////////////////////////////////////////
43
44class TessellationCache {
45public:
46 typedef Pair<VertexBuffer*, VertexBuffer*> vertexBuffer_pair_t;
47
48 struct Description {
49 DESCRIPTION_TYPE(Description);
50 enum Type {
51 kNone,
52 kRoundRect,
53 kAmbientShadow,
54 kSpotShadow
55 };
56
57 Type type;
58 SkPaint::Cap cap;
59 SkPaint::Style style;
60 float strokeWidth;
61 union Shape {
62 struct RoundRect {
63 float mScaleX;
64 float mScaleY;
65 float mWidth;
66 float mHeight;
67 float mRx;
68 float mRy;
69 } roundRect;
70 } shape;
71
72 Description();
73 Description(Type type);
74 Description(Type type, const SkPaint* paint);
75 hash_t hash() const;
76 };
77
78 struct ShadowDescription {
79 DESCRIPTION_TYPE(ShadowDescription);
80 const void* nodeKey;
81 float matrixData[16];
82
83 ShadowDescription();
84 ShadowDescription(const void* nodeKey, const Matrix4* drawTransform);
85 hash_t hash() const;
86 };
87
88 TessellationCache();
89 ~TessellationCache();
90
91 /**
92 * Clears the cache. This causes all TessellationBuffers to be deleted.
93 */
94 void clear();
95
96 /**
97 * Sets the maximum size of the cache in bytes.
98 */
99 void setMaxSize(uint32_t maxSize);
100 /**
101 * Returns the maximum size of the cache in bytes.
102 */
103 uint32_t getMaxSize();
104 /**
105 * Returns the current size of the cache in bytes.
106 */
107 uint32_t getSize();
108
109 /**
110 * Trims the contents of the cache, removing items until it's under its
111 * specified limit.
112 *
113 * Trimming is used for caches that support pre-caching from a worker
114 * thread. During pre-caching the maximum limit of the cache can be
115 * exceeded for the duration of the frame. It is therefore required to
116 * trim the cache at the end of the frame to keep the total amount of
117 * memory used under control.
118 *
119 * Also removes transient Shadow VertexBuffers, which aren't cached between frames.
120 */
121 void trim();
122
123 // TODO: precache/get for Oval, Lines, Points, etc.
124
125 void precacheRoundRect(const Matrix4& transform,
126 float width, float height, float rx, float ry, const SkPaint* paint) {
127 getRoundRectBuffer(transform, width, height, rx, ry, paint);
128 }
129 const VertexBuffer* getRoundRect(const Matrix4& transform,
130 float width, float height, float rx, float ry, const SkPaint* paint);
131
132 void precacheShadows(const Matrix4* drawTransform, const Rect& localClip,
133 bool opaque, const SkPath* casterPerimeter,
134 const Matrix4* transformXY, const Matrix4* transformZ,
135 const Vector3& lightCenter, float lightRadius);
136
137 void getShadowBuffers(const Matrix4* drawTransform, const Rect& localClip,
138 bool opaque, const SkPath* casterPerimeter,
139 const Matrix4* transformXY, const Matrix4* transformZ,
140 const Vector3& lightCenter, float lightRadius,
141 vertexBuffer_pair_t& outBuffers);
142
143private:
144 class Buffer;
145 class TessellationTask;
146 class TessellationProcessor;
147
148
149 typedef VertexBuffer* (*Tessellator)(const Description&, const SkPaint&);
150
151 Buffer* getRoundRectBuffer(const Matrix4& transform,
152 float width, float height, float rx, float ry, const SkPaint* paint);
153
154 Buffer* getOrCreateBuffer(const Description& entry,
155 Tessellator tessellator, const SkPaint* paint);
156
157 uint32_t mSize;
158 uint32_t mMaxSize;
159
160 bool mDebugEnabled;
161
162 mutable Mutex mLock;
163
164 ///////////////////////////////////////////////////////////////////////////////
165 // General tessellation caching
166 ///////////////////////////////////////////////////////////////////////////////
167 sp<TaskProcessor<VertexBuffer*> > mProcessor;
168 LruCache<Description, Buffer*> mCache;
169 class BufferRemovedListener : public OnEntryRemoved<Description, Buffer*> {
170 void operator()(Description& description, Buffer*& buffer);
171 };
172 BufferRemovedListener mBufferRemovedListener;
173
174 ///////////////////////////////////////////////////////////////////////////////
175 // Shadow tessellation caching
176 ///////////////////////////////////////////////////////////////////////////////
177 sp<TaskProcessor<vertexBuffer_pair_t*> > mShadowProcessor;
178
179 // holds a pointer, and implicit strong ref to each shadow task of the frame
180 LruCache<ShadowDescription, Task<vertexBuffer_pair_t*>*> mShadowCache;
181 class BufferPairRemovedListener : public OnEntryRemoved<ShadowDescription, Task<vertexBuffer_pair_t*>*> {
182 void operator()(ShadowDescription& description, Task<vertexBuffer_pair_t*>*& bufferPairTask) {
183 bufferPairTask->decStrong(NULL);
184 }
185 };
186 BufferPairRemovedListener mBufferPairRemovedListener;
187
188}; // class TessellationCache
189
190}; // namespace uirenderer
191}; // namespace android
192
193#endif // ANDROID_HWUI_PATH_CACHE_H