blob: 6dcc8120cf481c372a62087e2d44dc209ebec99d [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"
Chris Craikd8165e82016-02-03 15:52:25 -080024#include "VertexBuffer.h"
John Reck82f5e0c2015-10-22 17:07:45 -070025#include "thread/TaskProcessor.h"
Chris Craik05f3d6e2014-06-02 16:27:04 -070026#include "utils/Macros.h"
27#include "utils/Pair.h"
Chris Craik05f3d6e2014-06-02 16:27:04 -070028
John Reck82f5e0c2015-10-22 17:07:45 -070029#include <SkPaint.h>
Chris Craik6e068c012016-01-15 16:15:30 -080030#include <SkPath.h>
John Reck82f5e0c2015-10-22 17:07:45 -070031
32#include <utils/LruCache.h>
33#include <utils/Mutex.h>
34#include <utils/StrongPointer.h>
35
Chris Craik05f3d6e2014-06-02 16:27:04 -070036class SkBitmap;
37class SkCanvas;
Chris Craik05f3d6e2014-06-02 16:27:04 -070038struct SkRect;
39
40namespace android {
41namespace uirenderer {
42
43class Caches;
Tom Hudson2dc236b2014-10-15 15:46:42 -040044class VertexBuffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -070045
46///////////////////////////////////////////////////////////////////////////////
47// Classes
48///////////////////////////////////////////////////////////////////////////////
49
50class TessellationCache {
51public:
52 typedef Pair<VertexBuffer*, VertexBuffer*> vertexBuffer_pair_t;
53
54 struct Description {
55 DESCRIPTION_TYPE(Description);
56 enum Type {
57 kNone,
58 kRoundRect,
Chris Craik05f3d6e2014-06-02 16:27:04 -070059 };
60
61 Type type;
Chris Craik6ac174b2014-06-17 13:47:05 -070062 float scaleX;
63 float scaleY;
Chris Craiked4ef0b2014-06-12 13:27:30 -070064 bool aa;
Chris Craik05f3d6e2014-06-02 16:27:04 -070065 SkPaint::Cap cap;
66 SkPaint::Style style;
67 float strokeWidth;
68 union Shape {
69 struct RoundRect {
Chris Craik6ac174b2014-06-17 13:47:05 -070070 float width;
71 float height;
72 float rx;
73 float ry;
Chris Craik05f3d6e2014-06-02 16:27:04 -070074 } roundRect;
75 } shape;
76
77 Description();
Chris Craik6ac174b2014-06-17 13:47:05 -070078 Description(Type type, const Matrix4& transform, const SkPaint& paint);
Chris Craik05f3d6e2014-06-02 16:27:04 -070079 hash_t hash() const;
Chris Craik6ac174b2014-06-17 13:47:05 -070080 void setupMatrixAndPaint(Matrix4* matrix, SkPaint* paint) const;
Chris Craik05f3d6e2014-06-02 16:27:04 -070081 };
82
83 struct ShadowDescription {
84 DESCRIPTION_TYPE(ShadowDescription);
85 const void* nodeKey;
86 float matrixData[16];
87
88 ShadowDescription();
89 ShadowDescription(const void* nodeKey, const Matrix4* drawTransform);
90 hash_t hash() const;
91 };
92
Chris Craikd8165e82016-02-03 15:52:25 -080093 class ShadowTask : public Task<vertexBuffer_pair_t> {
Chris Craik6e068c012016-01-15 16:15:30 -080094 public:
95 ShadowTask(const Matrix4* drawTransform, const Rect& localClip, bool opaque,
96 const SkPath* casterPerimeter, const Matrix4* transformXY, const Matrix4* transformZ,
97 const Vector3& lightCenter, float lightRadius)
98 : drawTransform(*drawTransform)
99 , localClip(localClip)
100 , opaque(opaque)
101 , casterPerimeter(*casterPerimeter)
102 , transformXY(*transformXY)
103 , transformZ(*transformZ)
104 , lightCenter(lightCenter)
105 , lightRadius(lightRadius) {
106 }
107
Chris Craik6e068c012016-01-15 16:15:30 -0800108 /* Note - we deep copy all task parameters, because *even though* pointers into Allocator
109 * controlled objects (like the SkPath and Matrix4s) should be safe for the entire frame,
110 * certain Allocators are destroyed before trim() is called to flush incomplete tasks.
111 *
Chris Craikd8165e82016-02-03 15:52:25 -0800112 * These deep copies could be avoided, long term, by canceling or flushing outstanding
Chris Craik6e068c012016-01-15 16:15:30 -0800113 * tasks before tearing down single-frame LinearAllocators.
114 */
115 const Matrix4 drawTransform;
116 const Rect localClip;
117 bool opaque;
118 const SkPath casterPerimeter;
119 const Matrix4 transformXY;
120 const Matrix4 transformZ;
121 const Vector3 lightCenter;
122 const float lightRadius;
Chris Craikd8165e82016-02-03 15:52:25 -0800123 VertexBuffer ambientBuffer;
124 VertexBuffer spotBuffer;
Chris Craik6e068c012016-01-15 16:15:30 -0800125 };
126
Chris Craik05f3d6e2014-06-02 16:27:04 -0700127 TessellationCache();
128 ~TessellationCache();
129
130 /**
131 * Clears the cache. This causes all TessellationBuffers to be deleted.
132 */
133 void clear();
134
135 /**
136 * Sets the maximum size of the cache in bytes.
137 */
138 void setMaxSize(uint32_t maxSize);
139 /**
140 * Returns the maximum size of the cache in bytes.
141 */
142 uint32_t getMaxSize();
143 /**
144 * Returns the current size of the cache in bytes.
145 */
146 uint32_t getSize();
147
148 /**
149 * Trims the contents of the cache, removing items until it's under its
150 * specified limit.
151 *
152 * Trimming is used for caches that support pre-caching from a worker
153 * thread. During pre-caching the maximum limit of the cache can be
154 * exceeded for the duration of the frame. It is therefore required to
155 * trim the cache at the end of the frame to keep the total amount of
156 * memory used under control.
157 *
158 * Also removes transient Shadow VertexBuffers, which aren't cached between frames.
159 */
160 void trim();
161
162 // TODO: precache/get for Oval, Lines, Points, etc.
163
Chris Craik6ac174b2014-06-17 13:47:05 -0700164 void precacheRoundRect(const Matrix4& transform, const SkPaint& paint,
165 float width, float height, float rx, float ry) {
166 getRoundRectBuffer(transform, paint, width, height, rx, ry);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700167 }
Chris Craik6ac174b2014-06-17 13:47:05 -0700168 const VertexBuffer* getRoundRect(const Matrix4& transform, const SkPaint& paint,
169 float width, float height, float rx, float ry);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700170
Chris Craik6e068c012016-01-15 16:15:30 -0800171 // TODO: delete these when switching to HWUI_NEW_OPS
Chris Craik05f3d6e2014-06-02 16:27:04 -0700172 void precacheShadows(const Matrix4* drawTransform, const Rect& localClip,
173 bool opaque, const SkPath* casterPerimeter,
174 const Matrix4* transformXY, const Matrix4* transformZ,
175 const Vector3& lightCenter, float lightRadius);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700176 void getShadowBuffers(const Matrix4* drawTransform, const Rect& localClip,
177 bool opaque, const SkPath* casterPerimeter,
178 const Matrix4* transformXY, const Matrix4* transformZ,
179 const Vector3& lightCenter, float lightRadius,
180 vertexBuffer_pair_t& outBuffers);
181
Chris Craik6e068c012016-01-15 16:15:30 -0800182 sp<ShadowTask> getShadowTask(const Matrix4* drawTransform, const Rect& localClip,
183 bool opaque, const SkPath* casterPerimeter,
184 const Matrix4* transformXY, const Matrix4* transformZ,
185 const Vector3& lightCenter, float lightRadius);
186
Chris Craik05f3d6e2014-06-02 16:27:04 -0700187private:
188 class Buffer;
189 class TessellationTask;
190 class TessellationProcessor;
191
Chris Craik6ac174b2014-06-17 13:47:05 -0700192 typedef VertexBuffer* (*Tessellator)(const Description&);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700193
Chris Craik6ac174b2014-06-17 13:47:05 -0700194 Buffer* getRectBuffer(const Matrix4& transform, const SkPaint& paint,
195 float width, float height);
196 Buffer* getRoundRectBuffer(const Matrix4& transform, const SkPaint& paint,
197 float width, float height, float rx, float ry);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700198
Chris Craik6ac174b2014-06-17 13:47:05 -0700199 Buffer* getOrCreateBuffer(const Description& entry, Tessellator tessellator);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700200
201 uint32_t mSize;
202 uint32_t mMaxSize;
203
204 bool mDebugEnabled;
205
206 mutable Mutex mLock;
207
208 ///////////////////////////////////////////////////////////////////////////////
209 // General tessellation caching
210 ///////////////////////////////////////////////////////////////////////////////
211 sp<TaskProcessor<VertexBuffer*> > mProcessor;
212 LruCache<Description, Buffer*> mCache;
213 class BufferRemovedListener : public OnEntryRemoved<Description, Buffer*> {
Chris Craike84a2082014-12-22 14:28:49 -0800214 void operator()(Description& description, Buffer*& buffer) override;
Chris Craik05f3d6e2014-06-02 16:27:04 -0700215 };
216 BufferRemovedListener mBufferRemovedListener;
217
218 ///////////////////////////////////////////////////////////////////////////////
219 // Shadow tessellation caching
220 ///////////////////////////////////////////////////////////////////////////////
Chris Craikd8165e82016-02-03 15:52:25 -0800221 sp<TaskProcessor<vertexBuffer_pair_t> > mShadowProcessor;
Chris Craik05f3d6e2014-06-02 16:27:04 -0700222
223 // holds a pointer, and implicit strong ref to each shadow task of the frame
Chris Craikd8165e82016-02-03 15:52:25 -0800224 LruCache<ShadowDescription, Task<vertexBuffer_pair_t>*> mShadowCache;
225 class BufferPairRemovedListener : public OnEntryRemoved<ShadowDescription, Task<vertexBuffer_pair_t>*> {
226 void operator()(ShadowDescription& description, Task<vertexBuffer_pair_t>*& bufferPairTask) override {
Chris Craike84a2082014-12-22 14:28:49 -0800227 bufferPairTask->decStrong(nullptr);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700228 }
229 };
230 BufferPairRemovedListener mBufferPairRemovedListener;
231
232}; // class TessellationCache
233
John Reck82f5e0c2015-10-22 17:07:45 -0700234void tessellateShadows(
235 const Matrix4* drawTransform, const Rect* localClip,
236 bool isCasterOpaque, const SkPath* casterPerimeter,
237 const Matrix4* casterTransformXY, const Matrix4* casterTransformZ,
238 const Vector3& lightCenter, float lightRadius,
239 VertexBuffer& ambientBuffer, VertexBuffer& spotBuffer);
240
Chris Craik05f3d6e2014-06-02 16:27:04 -0700241}; // namespace uirenderer
242}; // namespace android
243
244#endif // ANDROID_HWUI_PATH_CACHE_H