blob: 0bd6365db60f2ff3b67470eccb89bbd6076f30d1 [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();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700134 /**
135 * Returns the maximum size of the cache in bytes.
136 */
137 uint32_t getMaxSize();
138 /**
139 * Returns the current size of the cache in bytes.
140 */
141 uint32_t getSize();
142
143 /**
144 * Trims the contents of the cache, removing items until it's under its
145 * specified limit.
146 *
147 * Trimming is used for caches that support pre-caching from a worker
148 * thread. During pre-caching the maximum limit of the cache can be
149 * exceeded for the duration of the frame. It is therefore required to
150 * trim the cache at the end of the frame to keep the total amount of
151 * memory used under control.
152 *
153 * Also removes transient Shadow VertexBuffers, which aren't cached between frames.
154 */
155 void trim();
156
157 // TODO: precache/get for Oval, Lines, Points, etc.
158
Chris Craik6ac174b2014-06-17 13:47:05 -0700159 void precacheRoundRect(const Matrix4& transform, const SkPaint& paint,
160 float width, float height, float rx, float ry) {
161 getRoundRectBuffer(transform, paint, width, height, rx, ry);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700162 }
Chris Craik6ac174b2014-06-17 13:47:05 -0700163 const VertexBuffer* getRoundRect(const Matrix4& transform, const SkPaint& paint,
164 float width, float height, float rx, float ry);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700165
Chris Craik6e068c012016-01-15 16:15:30 -0800166 // TODO: delete these when switching to HWUI_NEW_OPS
Chris Craik05f3d6e2014-06-02 16:27:04 -0700167 void precacheShadows(const Matrix4* drawTransform, const Rect& localClip,
168 bool opaque, const SkPath* casterPerimeter,
169 const Matrix4* transformXY, const Matrix4* transformZ,
170 const Vector3& lightCenter, float lightRadius);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700171 void getShadowBuffers(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,
175 vertexBuffer_pair_t& outBuffers);
176
Chris Craik6e068c012016-01-15 16:15:30 -0800177 sp<ShadowTask> getShadowTask(const Matrix4* drawTransform, const Rect& localClip,
178 bool opaque, const SkPath* casterPerimeter,
179 const Matrix4* transformXY, const Matrix4* transformZ,
180 const Vector3& lightCenter, float lightRadius);
181
Chris Craik05f3d6e2014-06-02 16:27:04 -0700182private:
183 class Buffer;
184 class TessellationTask;
185 class TessellationProcessor;
186
Chris Craik6ac174b2014-06-17 13:47:05 -0700187 typedef VertexBuffer* (*Tessellator)(const Description&);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700188
Chris Craik6ac174b2014-06-17 13:47:05 -0700189 Buffer* getRectBuffer(const Matrix4& transform, const SkPaint& paint,
190 float width, float height);
191 Buffer* getRoundRectBuffer(const Matrix4& transform, const SkPaint& paint,
192 float width, float height, float rx, float ry);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700193
Chris Craik6ac174b2014-06-17 13:47:05 -0700194 Buffer* getOrCreateBuffer(const Description& entry, Tessellator tessellator);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700195
Chris Craik48a8f432016-02-05 15:59:29 -0800196 const uint32_t mMaxSize;
Chris Craik05f3d6e2014-06-02 16:27:04 -0700197
198 bool mDebugEnabled;
199
200 mutable Mutex mLock;
201
202 ///////////////////////////////////////////////////////////////////////////////
203 // General tessellation caching
204 ///////////////////////////////////////////////////////////////////////////////
205 sp<TaskProcessor<VertexBuffer*> > mProcessor;
206 LruCache<Description, Buffer*> mCache;
207 class BufferRemovedListener : public OnEntryRemoved<Description, Buffer*> {
Chris Craike84a2082014-12-22 14:28:49 -0800208 void operator()(Description& description, Buffer*& buffer) override;
Chris Craik05f3d6e2014-06-02 16:27:04 -0700209 };
210 BufferRemovedListener mBufferRemovedListener;
211
212 ///////////////////////////////////////////////////////////////////////////////
213 // Shadow tessellation caching
214 ///////////////////////////////////////////////////////////////////////////////
Chris Craikd8165e82016-02-03 15:52:25 -0800215 sp<TaskProcessor<vertexBuffer_pair_t> > mShadowProcessor;
Chris Craik05f3d6e2014-06-02 16:27:04 -0700216
217 // holds a pointer, and implicit strong ref to each shadow task of the frame
Chris Craikd8165e82016-02-03 15:52:25 -0800218 LruCache<ShadowDescription, Task<vertexBuffer_pair_t>*> mShadowCache;
219 class BufferPairRemovedListener : public OnEntryRemoved<ShadowDescription, Task<vertexBuffer_pair_t>*> {
220 void operator()(ShadowDescription& description, Task<vertexBuffer_pair_t>*& bufferPairTask) override {
Chris Craike84a2082014-12-22 14:28:49 -0800221 bufferPairTask->decStrong(nullptr);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700222 }
223 };
224 BufferPairRemovedListener mBufferPairRemovedListener;
225
226}; // class TessellationCache
227
John Reck82f5e0c2015-10-22 17:07:45 -0700228void tessellateShadows(
229 const Matrix4* drawTransform, const Rect* localClip,
230 bool isCasterOpaque, const SkPath* casterPerimeter,
231 const Matrix4* casterTransformXY, const Matrix4* casterTransformZ,
232 const Vector3& lightCenter, float lightRadius,
233 VertexBuffer& ambientBuffer, VertexBuffer& spotBuffer);
234
Chris Craik05f3d6e2014-06-02 16:27:04 -0700235}; // namespace uirenderer
236}; // namespace android
237
238#endif // ANDROID_HWUI_PATH_CACHE_H