Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 1 | /* |
| 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 Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 20 | #include "Debug.h" |
John Reck | 82f5e0c | 2015-10-22 17:07:45 -0700 | [diff] [blame] | 21 | #include "Matrix.h" |
| 22 | #include "Rect.h" |
| 23 | #include "Vector.h" |
| 24 | #include "thread/TaskProcessor.h" |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 25 | #include "utils/Macros.h" |
| 26 | #include "utils/Pair.h" |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 27 | |
John Reck | 82f5e0c | 2015-10-22 17:07:45 -0700 | [diff] [blame] | 28 | #include <SkPaint.h> |
| 29 | |
| 30 | #include <utils/LruCache.h> |
| 31 | #include <utils/Mutex.h> |
| 32 | #include <utils/StrongPointer.h> |
| 33 | |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 34 | class SkBitmap; |
| 35 | class SkCanvas; |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 36 | class SkPath; |
| 37 | struct SkRect; |
| 38 | |
| 39 | namespace android { |
| 40 | namespace uirenderer { |
| 41 | |
| 42 | class Caches; |
Tom Hudson | 2dc236b | 2014-10-15 15:46:42 -0400 | [diff] [blame] | 43 | class VertexBuffer; |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 44 | |
| 45 | /////////////////////////////////////////////////////////////////////////////// |
| 46 | // Classes |
| 47 | /////////////////////////////////////////////////////////////////////////////// |
| 48 | |
| 49 | class TessellationCache { |
| 50 | public: |
| 51 | typedef Pair<VertexBuffer*, VertexBuffer*> vertexBuffer_pair_t; |
| 52 | |
| 53 | struct Description { |
| 54 | DESCRIPTION_TYPE(Description); |
| 55 | enum Type { |
| 56 | kNone, |
| 57 | kRoundRect, |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | Type type; |
Chris Craik | 6ac174b | 2014-06-17 13:47:05 -0700 | [diff] [blame] | 61 | float scaleX; |
| 62 | float scaleY; |
Chris Craik | ed4ef0b | 2014-06-12 13:27:30 -0700 | [diff] [blame] | 63 | bool aa; |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 64 | SkPaint::Cap cap; |
| 65 | SkPaint::Style style; |
| 66 | float strokeWidth; |
| 67 | union Shape { |
| 68 | struct RoundRect { |
Chris Craik | 6ac174b | 2014-06-17 13:47:05 -0700 | [diff] [blame] | 69 | float width; |
| 70 | float height; |
| 71 | float rx; |
| 72 | float ry; |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 73 | } roundRect; |
| 74 | } shape; |
| 75 | |
| 76 | Description(); |
Chris Craik | 6ac174b | 2014-06-17 13:47:05 -0700 | [diff] [blame] | 77 | Description(Type type, const Matrix4& transform, const SkPaint& paint); |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 78 | hash_t hash() const; |
Chris Craik | 6ac174b | 2014-06-17 13:47:05 -0700 | [diff] [blame] | 79 | void setupMatrixAndPaint(Matrix4* matrix, SkPaint* paint) const; |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 80 | }; |
| 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 | |
| 92 | TessellationCache(); |
| 93 | ~TessellationCache(); |
| 94 | |
| 95 | /** |
| 96 | * Clears the cache. This causes all TessellationBuffers to be deleted. |
| 97 | */ |
| 98 | void clear(); |
| 99 | |
| 100 | /** |
| 101 | * Sets the maximum size of the cache in bytes. |
| 102 | */ |
| 103 | void setMaxSize(uint32_t maxSize); |
| 104 | /** |
| 105 | * Returns the maximum size of the cache in bytes. |
| 106 | */ |
| 107 | uint32_t getMaxSize(); |
| 108 | /** |
| 109 | * Returns the current size of the cache in bytes. |
| 110 | */ |
| 111 | uint32_t getSize(); |
| 112 | |
| 113 | /** |
| 114 | * Trims the contents of the cache, removing items until it's under its |
| 115 | * specified limit. |
| 116 | * |
| 117 | * Trimming is used for caches that support pre-caching from a worker |
| 118 | * thread. During pre-caching the maximum limit of the cache can be |
| 119 | * exceeded for the duration of the frame. It is therefore required to |
| 120 | * trim the cache at the end of the frame to keep the total amount of |
| 121 | * memory used under control. |
| 122 | * |
| 123 | * Also removes transient Shadow VertexBuffers, which aren't cached between frames. |
| 124 | */ |
| 125 | void trim(); |
| 126 | |
| 127 | // TODO: precache/get for Oval, Lines, Points, etc. |
| 128 | |
Chris Craik | 6ac174b | 2014-06-17 13:47:05 -0700 | [diff] [blame] | 129 | void precacheRoundRect(const Matrix4& transform, const SkPaint& paint, |
| 130 | float width, float height, float rx, float ry) { |
| 131 | getRoundRectBuffer(transform, paint, width, height, rx, ry); |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 132 | } |
Chris Craik | 6ac174b | 2014-06-17 13:47:05 -0700 | [diff] [blame] | 133 | const VertexBuffer* getRoundRect(const Matrix4& transform, const SkPaint& paint, |
| 134 | float width, float height, float rx, float ry); |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 135 | |
| 136 | void precacheShadows(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 | |
| 141 | void getShadowBuffers(const Matrix4* drawTransform, const Rect& localClip, |
| 142 | bool opaque, const SkPath* casterPerimeter, |
| 143 | const Matrix4* transformXY, const Matrix4* transformZ, |
| 144 | const Vector3& lightCenter, float lightRadius, |
| 145 | vertexBuffer_pair_t& outBuffers); |
| 146 | |
| 147 | private: |
| 148 | class Buffer; |
| 149 | class TessellationTask; |
| 150 | class TessellationProcessor; |
| 151 | |
Chris Craik | 6ac174b | 2014-06-17 13:47:05 -0700 | [diff] [blame] | 152 | typedef VertexBuffer* (*Tessellator)(const Description&); |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 153 | |
Chris Craik | 6ac174b | 2014-06-17 13:47:05 -0700 | [diff] [blame] | 154 | Buffer* getRectBuffer(const Matrix4& transform, const SkPaint& paint, |
| 155 | float width, float height); |
| 156 | Buffer* getRoundRectBuffer(const Matrix4& transform, const SkPaint& paint, |
| 157 | float width, float height, float rx, float ry); |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 158 | |
Chris Craik | 6ac174b | 2014-06-17 13:47:05 -0700 | [diff] [blame] | 159 | Buffer* getOrCreateBuffer(const Description& entry, Tessellator tessellator); |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 160 | |
| 161 | uint32_t mSize; |
| 162 | uint32_t mMaxSize; |
| 163 | |
| 164 | bool mDebugEnabled; |
| 165 | |
| 166 | mutable Mutex mLock; |
| 167 | |
| 168 | /////////////////////////////////////////////////////////////////////////////// |
| 169 | // General tessellation caching |
| 170 | /////////////////////////////////////////////////////////////////////////////// |
| 171 | sp<TaskProcessor<VertexBuffer*> > mProcessor; |
| 172 | LruCache<Description, Buffer*> mCache; |
| 173 | class BufferRemovedListener : public OnEntryRemoved<Description, Buffer*> { |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame] | 174 | void operator()(Description& description, Buffer*& buffer) override; |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 175 | }; |
| 176 | BufferRemovedListener mBufferRemovedListener; |
| 177 | |
| 178 | /////////////////////////////////////////////////////////////////////////////// |
| 179 | // Shadow tessellation caching |
| 180 | /////////////////////////////////////////////////////////////////////////////// |
| 181 | sp<TaskProcessor<vertexBuffer_pair_t*> > mShadowProcessor; |
| 182 | |
| 183 | // holds a pointer, and implicit strong ref to each shadow task of the frame |
| 184 | LruCache<ShadowDescription, Task<vertexBuffer_pair_t*>*> mShadowCache; |
| 185 | class BufferPairRemovedListener : public OnEntryRemoved<ShadowDescription, Task<vertexBuffer_pair_t*>*> { |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame] | 186 | void operator()(ShadowDescription& description, Task<vertexBuffer_pair_t*>*& bufferPairTask) override { |
| 187 | bufferPairTask->decStrong(nullptr); |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 188 | } |
| 189 | }; |
| 190 | BufferPairRemovedListener mBufferPairRemovedListener; |
| 191 | |
| 192 | }; // class TessellationCache |
| 193 | |
John Reck | 82f5e0c | 2015-10-22 17:07:45 -0700 | [diff] [blame] | 194 | void tessellateShadows( |
| 195 | const Matrix4* drawTransform, const Rect* localClip, |
| 196 | bool isCasterOpaque, const SkPath* casterPerimeter, |
| 197 | const Matrix4* casterTransformXY, const Matrix4* casterTransformZ, |
| 198 | const Vector3& lightCenter, float lightRadius, |
| 199 | VertexBuffer& ambientBuffer, VertexBuffer& spotBuffer); |
| 200 | |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 201 | }; // namespace uirenderer |
| 202 | }; // namespace android |
| 203 | |
| 204 | #endif // ANDROID_HWUI_PATH_CACHE_H |