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