blob: 977c2d9e9f8aad5fe7166c3eb52ee5462984e9bb [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
Chris Craik05f3d6e2014-06-02 16:27:04 -070020#include "Debug.h"
John Reck82f5e0c2015-10-22 17:07:45 -070021#include "Matrix.h"
22#include "Rect.h"
23#include "Vector.h"
24#include "thread/TaskProcessor.h"
Chris Craik05f3d6e2014-06-02 16:27:04 -070025#include "utils/Macros.h"
26#include "utils/Pair.h"
Chris Craik05f3d6e2014-06-02 16:27:04 -070027
John Reck82f5e0c2015-10-22 17:07:45 -070028#include <SkPaint.h>
Chris Craik6e068c012016-01-15 16:15:30 -080029#include <SkPath.h>
John Reck82f5e0c2015-10-22 17:07:45 -070030
31#include <utils/LruCache.h>
32#include <utils/Mutex.h>
33#include <utils/StrongPointer.h>
34
Chris Craik05f3d6e2014-06-02 16:27:04 -070035class SkBitmap;
36class SkCanvas;
Chris Craik05f3d6e2014-06-02 16:27:04 -070037struct SkRect;
38
39namespace android {
40namespace uirenderer {
41
42class Caches;
Tom Hudson2dc236b2014-10-15 15:46:42 -040043class VertexBuffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -070044
45///////////////////////////////////////////////////////////////////////////////
46// Classes
47///////////////////////////////////////////////////////////////////////////////
48
49class TessellationCache {
50public:
51 typedef Pair<VertexBuffer*, VertexBuffer*> vertexBuffer_pair_t;
52
53 struct Description {
54 DESCRIPTION_TYPE(Description);
55 enum Type {
56 kNone,
57 kRoundRect,
Chris Craik05f3d6e2014-06-02 16:27:04 -070058 };
59
60 Type type;
Chris Craik6ac174b2014-06-17 13:47:05 -070061 float scaleX;
62 float scaleY;
Chris Craiked4ef0b2014-06-12 13:27:30 -070063 bool aa;
Chris Craik05f3d6e2014-06-02 16:27:04 -070064 SkPaint::Cap cap;
65 SkPaint::Style style;
66 float strokeWidth;
67 union Shape {
68 struct RoundRect {
Chris Craik6ac174b2014-06-17 13:47:05 -070069 float width;
70 float height;
71 float rx;
72 float ry;
Chris Craik05f3d6e2014-06-02 16:27:04 -070073 } roundRect;
74 } shape;
75
76 Description();
Chris Craik6ac174b2014-06-17 13:47:05 -070077 Description(Type type, const Matrix4& transform, const SkPaint& paint);
Chris Craik05f3d6e2014-06-02 16:27:04 -070078 hash_t hash() const;
Chris Craik6ac174b2014-06-17 13:47:05 -070079 void setupMatrixAndPaint(Matrix4* matrix, SkPaint* paint) const;
Chris Craik05f3d6e2014-06-02 16:27:04 -070080 };
81
82 struct ShadowDescription {
83 DESCRIPTION_TYPE(ShadowDescription);
84 const void* nodeKey;
85 float matrixData[16];
86
87 ShadowDescription();
88 ShadowDescription(const void* nodeKey, const Matrix4* drawTransform);
89 hash_t hash() const;
90 };
91
Chris Craik6e068c012016-01-15 16:15:30 -080092 class ShadowTask : public Task<TessellationCache::vertexBuffer_pair_t*> {
93 public:
94 ShadowTask(const Matrix4* drawTransform, const Rect& localClip, bool opaque,
95 const SkPath* casterPerimeter, const Matrix4* transformXY, const Matrix4* transformZ,
96 const Vector3& lightCenter, float lightRadius)
97 : drawTransform(*drawTransform)
98 , localClip(localClip)
99 , opaque(opaque)
100 , casterPerimeter(*casterPerimeter)
101 , transformXY(*transformXY)
102 , transformZ(*transformZ)
103 , lightCenter(lightCenter)
104 , lightRadius(lightRadius) {
105 }
106
107 ~ShadowTask();
108
109 /* Note - we deep copy all task parameters, because *even though* pointers into Allocator
110 * controlled objects (like the SkPath and Matrix4s) should be safe for the entire frame,
111 * certain Allocators are destroyed before trim() is called to flush incomplete tasks.
112 *
113 * These deep copies could be avoided, long term, by cancelling or flushing outstanding
114 * tasks before tearing down single-frame LinearAllocators.
115 */
116 const Matrix4 drawTransform;
117 const Rect localClip;
118 bool opaque;
119 const SkPath casterPerimeter;
120 const Matrix4 transformXY;
121 const Matrix4 transformZ;
122 const Vector3 lightCenter;
123 const float lightRadius;
124 };
125
Chris Craik05f3d6e2014-06-02 16:27:04 -0700126 TessellationCache();
127 ~TessellationCache();
128
129 /**
130 * Clears the cache. This causes all TessellationBuffers to be deleted.
131 */
132 void clear();
133
134 /**
135 * Sets the maximum size of the cache in bytes.
136 */
137 void setMaxSize(uint32_t maxSize);
138 /**
139 * Returns the maximum size of the cache in bytes.
140 */
141 uint32_t getMaxSize();
142 /**
143 * Returns the current size of the cache in bytes.
144 */
145 uint32_t getSize();
146
147 /**
148 * Trims the contents of the cache, removing items until it's under its
149 * specified limit.
150 *
151 * Trimming is used for caches that support pre-caching from a worker
152 * thread. During pre-caching the maximum limit of the cache can be
153 * exceeded for the duration of the frame. It is therefore required to
154 * trim the cache at the end of the frame to keep the total amount of
155 * memory used under control.
156 *
157 * Also removes transient Shadow VertexBuffers, which aren't cached between frames.
158 */
159 void trim();
160
161 // TODO: precache/get for Oval, Lines, Points, etc.
162
Chris Craik6ac174b2014-06-17 13:47:05 -0700163 void precacheRoundRect(const Matrix4& transform, const SkPaint& paint,
164 float width, float height, float rx, float ry) {
165 getRoundRectBuffer(transform, paint, width, height, rx, ry);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700166 }
Chris Craik6ac174b2014-06-17 13:47:05 -0700167 const VertexBuffer* getRoundRect(const Matrix4& transform, const SkPaint& paint,
168 float width, float height, float rx, float ry);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700169
Chris Craik6e068c012016-01-15 16:15:30 -0800170 // TODO: delete these when switching to HWUI_NEW_OPS
Chris Craik05f3d6e2014-06-02 16:27:04 -0700171 void precacheShadows(const Matrix4* drawTransform, const Rect& localClip,
172 bool opaque, const SkPath* casterPerimeter,
173 const Matrix4* transformXY, const Matrix4* transformZ,
174 const Vector3& lightCenter, float lightRadius);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700175 void getShadowBuffers(const Matrix4* drawTransform, const Rect& localClip,
176 bool opaque, const SkPath* casterPerimeter,
177 const Matrix4* transformXY, const Matrix4* transformZ,
178 const Vector3& lightCenter, float lightRadius,
179 vertexBuffer_pair_t& outBuffers);
180
Chris Craik6e068c012016-01-15 16:15:30 -0800181 sp<ShadowTask> getShadowTask(const Matrix4* drawTransform, const Rect& localClip,
182 bool opaque, const SkPath* casterPerimeter,
183 const Matrix4* transformXY, const Matrix4* transformZ,
184 const Vector3& lightCenter, float lightRadius);
185
Chris Craik05f3d6e2014-06-02 16:27:04 -0700186private:
187 class Buffer;
188 class TessellationTask;
189 class TessellationProcessor;
190
Chris Craik6ac174b2014-06-17 13:47:05 -0700191 typedef VertexBuffer* (*Tessellator)(const Description&);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700192
Chris Craik6ac174b2014-06-17 13:47:05 -0700193 Buffer* getRectBuffer(const Matrix4& transform, const SkPaint& paint,
194 float width, float height);
195 Buffer* getRoundRectBuffer(const Matrix4& transform, const SkPaint& paint,
196 float width, float height, float rx, float ry);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700197
Chris Craik6ac174b2014-06-17 13:47:05 -0700198 Buffer* getOrCreateBuffer(const Description& entry, Tessellator tessellator);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700199
200 uint32_t mSize;
201 uint32_t mMaxSize;
202
203 bool mDebugEnabled;
204
205 mutable Mutex mLock;
206
207 ///////////////////////////////////////////////////////////////////////////////
208 // General tessellation caching
209 ///////////////////////////////////////////////////////////////////////////////
210 sp<TaskProcessor<VertexBuffer*> > mProcessor;
211 LruCache<Description, Buffer*> mCache;
212 class BufferRemovedListener : public OnEntryRemoved<Description, Buffer*> {
Chris Craike84a2082014-12-22 14:28:49 -0800213 void operator()(Description& description, Buffer*& buffer) override;
Chris Craik05f3d6e2014-06-02 16:27:04 -0700214 };
215 BufferRemovedListener mBufferRemovedListener;
216
217 ///////////////////////////////////////////////////////////////////////////////
218 // Shadow tessellation caching
219 ///////////////////////////////////////////////////////////////////////////////
220 sp<TaskProcessor<vertexBuffer_pair_t*> > mShadowProcessor;
221
222 // holds a pointer, and implicit strong ref to each shadow task of the frame
223 LruCache<ShadowDescription, Task<vertexBuffer_pair_t*>*> mShadowCache;
224 class BufferPairRemovedListener : public OnEntryRemoved<ShadowDescription, Task<vertexBuffer_pair_t*>*> {
Chris Craike84a2082014-12-22 14:28:49 -0800225 void operator()(ShadowDescription& description, Task<vertexBuffer_pair_t*>*& bufferPairTask) override {
226 bufferPairTask->decStrong(nullptr);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700227 }
228 };
229 BufferPairRemovedListener mBufferPairRemovedListener;
230
231}; // class TessellationCache
232
John Reck82f5e0c2015-10-22 17:07:45 -0700233void tessellateShadows(
234 const Matrix4* drawTransform, const Rect* localClip,
235 bool isCasterOpaque, const SkPath* casterPerimeter,
236 const Matrix4* casterTransformXY, const Matrix4* casterTransformZ,
237 const Vector3& lightCenter, float lightRadius,
238 VertexBuffer& ambientBuffer, VertexBuffer& spotBuffer);
239
Chris Craik05f3d6e2014-06-02 16:27:04 -0700240}; // namespace uirenderer
241}; // namespace android
242
243#endif // ANDROID_HWUI_PATH_CACHE_H