2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef ANDROID_HWUI_TESSELLATION_CACHE_H
18 #define ANDROID_HWUI_TESSELLATION_CACHE_H
20 #include <utils/LruCache.h>
21 #include <utils/Mutex.h>
22 #include <utils/Vector.h>
25 #include "utils/Macros.h"
26 #include "utils/Pair.h"
35 namespace uirenderer {
40 ///////////////////////////////////////////////////////////////////////////////
42 ///////////////////////////////////////////////////////////////////////////////
44 class TessellationCache {
46 typedef Pair<VertexBuffer*, VertexBuffer*> vertexBuffer_pair_t;
49 DESCRIPTION_TYPE(Description);
72 Description(Type type, const Matrix4& transform, const SkPaint& paint);
74 void setupMatrixAndPaint(Matrix4* matrix, SkPaint* paint) const;
77 struct ShadowDescription {
78 DESCRIPTION_TYPE(ShadowDescription);
83 ShadowDescription(const void* nodeKey, const Matrix4* drawTransform);
91 * Clears the cache. This causes all TessellationBuffers to be deleted.
96 * Sets the maximum size of the cache in bytes.
98 void setMaxSize(uint32_t maxSize);
100 * Returns the maximum size of the cache in bytes.
102 uint32_t getMaxSize();
104 * Returns the current size of the cache in bytes.
109 * Trims the contents of the cache, removing items until it's under its
112 * Trimming is used for caches that support pre-caching from a worker
113 * thread. During pre-caching the maximum limit of the cache can be
114 * exceeded for the duration of the frame. It is therefore required to
115 * trim the cache at the end of the frame to keep the total amount of
116 * memory used under control.
118 * Also removes transient Shadow VertexBuffers, which aren't cached between frames.
122 // TODO: precache/get for Oval, Lines, Points, etc.
124 void precacheRoundRect(const Matrix4& transform, const SkPaint& paint,
125 float width, float height, float rx, float ry) {
126 getRoundRectBuffer(transform, paint, width, height, rx, ry);
128 const VertexBuffer* getRoundRect(const Matrix4& transform, const SkPaint& paint,
129 float width, float height, float rx, float ry);
131 void precacheShadows(const Matrix4* drawTransform, const Rect& localClip,
132 bool opaque, const SkPath* casterPerimeter,
133 const Matrix4* transformXY, const Matrix4* transformZ,
134 const Vector3& lightCenter, float lightRadius);
136 void getShadowBuffers(const Matrix4* drawTransform, const Rect& localClip,
137 bool opaque, const SkPath* casterPerimeter,
138 const Matrix4* transformXY, const Matrix4* transformZ,
139 const Vector3& lightCenter, float lightRadius,
140 vertexBuffer_pair_t& outBuffers);
144 class TessellationTask;
145 class TessellationProcessor;
147 typedef VertexBuffer* (*Tessellator)(const Description&);
149 Buffer* getRectBuffer(const Matrix4& transform, const SkPaint& paint,
150 float width, float height);
151 Buffer* getRoundRectBuffer(const Matrix4& transform, const SkPaint& paint,
152 float width, float height, float rx, float ry);
154 Buffer* getOrCreateBuffer(const Description& entry, Tessellator tessellator);
163 ///////////////////////////////////////////////////////////////////////////////
164 // General tessellation caching
165 ///////////////////////////////////////////////////////////////////////////////
166 sp<TaskProcessor<VertexBuffer*> > mProcessor;
167 LruCache<Description, Buffer*> mCache;
168 class BufferRemovedListener : public OnEntryRemoved<Description, Buffer*> {
169 void operator()(Description& description, Buffer*& buffer) override;
171 BufferRemovedListener mBufferRemovedListener;
173 ///////////////////////////////////////////////////////////////////////////////
174 // Shadow tessellation caching
175 ///////////////////////////////////////////////////////////////////////////////
176 sp<TaskProcessor<vertexBuffer_pair_t*> > mShadowProcessor;
178 // holds a pointer, and implicit strong ref to each shadow task of the frame
179 LruCache<ShadowDescription, Task<vertexBuffer_pair_t*>*> mShadowCache;
180 class BufferPairRemovedListener : public OnEntryRemoved<ShadowDescription, Task<vertexBuffer_pair_t*>*> {
181 void operator()(ShadowDescription& description, Task<vertexBuffer_pair_t*>*& bufferPairTask) override {
182 bufferPairTask->decStrong(nullptr);
185 BufferPairRemovedListener mBufferPairRemovedListener;
187 }; // class TessellationCache
189 }; // namespace uirenderer
190 }; // namespace android
192 #endif // ANDROID_HWUI_PATH_CACHE_H