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" |
Chris Craik | d8165e8 | 2016-02-03 15:52:25 -0800 | [diff] [blame^] | 24 | #include "VertexBuffer.h" |
John Reck | 82f5e0c | 2015-10-22 17:07:45 -0700 | [diff] [blame] | 25 | #include "thread/TaskProcessor.h" |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 26 | #include "utils/Macros.h" |
| 27 | #include "utils/Pair.h" |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 28 | |
John Reck | 82f5e0c | 2015-10-22 17:07:45 -0700 | [diff] [blame] | 29 | #include <SkPaint.h> |
Chris Craik | 6e068c01 | 2016-01-15 16:15:30 -0800 | [diff] [blame] | 30 | #include <SkPath.h> |
John Reck | 82f5e0c | 2015-10-22 17:07:45 -0700 | [diff] [blame] | 31 | |
| 32 | #include <utils/LruCache.h> |
| 33 | #include <utils/Mutex.h> |
| 34 | #include <utils/StrongPointer.h> |
| 35 | |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 36 | class SkBitmap; |
| 37 | class SkCanvas; |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 38 | struct SkRect; |
| 39 | |
| 40 | namespace android { |
| 41 | namespace uirenderer { |
| 42 | |
| 43 | class Caches; |
Tom Hudson | 2dc236b | 2014-10-15 15:46:42 -0400 | [diff] [blame] | 44 | class VertexBuffer; |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 45 | |
| 46 | /////////////////////////////////////////////////////////////////////////////// |
| 47 | // Classes |
| 48 | /////////////////////////////////////////////////////////////////////////////// |
| 49 | |
| 50 | class TessellationCache { |
| 51 | public: |
| 52 | typedef Pair<VertexBuffer*, VertexBuffer*> vertexBuffer_pair_t; |
| 53 | |
| 54 | struct Description { |
| 55 | DESCRIPTION_TYPE(Description); |
| 56 | enum Type { |
| 57 | kNone, |
| 58 | kRoundRect, |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | Type type; |
Chris Craik | 6ac174b | 2014-06-17 13:47:05 -0700 | [diff] [blame] | 62 | float scaleX; |
| 63 | float scaleY; |
Chris Craik | ed4ef0b | 2014-06-12 13:27:30 -0700 | [diff] [blame] | 64 | bool aa; |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 65 | SkPaint::Cap cap; |
| 66 | SkPaint::Style style; |
| 67 | float strokeWidth; |
| 68 | union Shape { |
| 69 | struct RoundRect { |
Chris Craik | 6ac174b | 2014-06-17 13:47:05 -0700 | [diff] [blame] | 70 | float width; |
| 71 | float height; |
| 72 | float rx; |
| 73 | float ry; |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 74 | } roundRect; |
| 75 | } shape; |
| 76 | |
| 77 | Description(); |
Chris Craik | 6ac174b | 2014-06-17 13:47:05 -0700 | [diff] [blame] | 78 | Description(Type type, const Matrix4& transform, const SkPaint& paint); |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 79 | hash_t hash() const; |
Chris Craik | 6ac174b | 2014-06-17 13:47:05 -0700 | [diff] [blame] | 80 | void setupMatrixAndPaint(Matrix4* matrix, SkPaint* paint) const; |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 81 | }; |
| 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 Craik | d8165e8 | 2016-02-03 15:52:25 -0800 | [diff] [blame^] | 93 | class ShadowTask : public Task<vertexBuffer_pair_t> { |
Chris Craik | 6e068c01 | 2016-01-15 16:15:30 -0800 | [diff] [blame] | 94 | 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 Craik | 6e068c01 | 2016-01-15 16:15:30 -0800 | [diff] [blame] | 108 | /* 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 Craik | d8165e8 | 2016-02-03 15:52:25 -0800 | [diff] [blame^] | 112 | * These deep copies could be avoided, long term, by canceling or flushing outstanding |
Chris Craik | 6e068c01 | 2016-01-15 16:15:30 -0800 | [diff] [blame] | 113 | * 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 Craik | d8165e8 | 2016-02-03 15:52:25 -0800 | [diff] [blame^] | 123 | VertexBuffer ambientBuffer; |
| 124 | VertexBuffer spotBuffer; |
Chris Craik | 6e068c01 | 2016-01-15 16:15:30 -0800 | [diff] [blame] | 125 | }; |
| 126 | |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 127 | 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 Craik | 6ac174b | 2014-06-17 13:47:05 -0700 | [diff] [blame] | 164 | 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 Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 167 | } |
Chris Craik | 6ac174b | 2014-06-17 13:47:05 -0700 | [diff] [blame] | 168 | const VertexBuffer* getRoundRect(const Matrix4& transform, const SkPaint& paint, |
| 169 | float width, float height, float rx, float ry); |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 170 | |
Chris Craik | 6e068c01 | 2016-01-15 16:15:30 -0800 | [diff] [blame] | 171 | // TODO: delete these when switching to HWUI_NEW_OPS |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 172 | 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 Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 176 | 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 Craik | 6e068c01 | 2016-01-15 16:15:30 -0800 | [diff] [blame] | 182 | 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 Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 187 | private: |
| 188 | class Buffer; |
| 189 | class TessellationTask; |
| 190 | class TessellationProcessor; |
| 191 | |
Chris Craik | 6ac174b | 2014-06-17 13:47:05 -0700 | [diff] [blame] | 192 | typedef VertexBuffer* (*Tessellator)(const Description&); |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 193 | |
Chris Craik | 6ac174b | 2014-06-17 13:47:05 -0700 | [diff] [blame] | 194 | 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 Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 198 | |
Chris Craik | 6ac174b | 2014-06-17 13:47:05 -0700 | [diff] [blame] | 199 | Buffer* getOrCreateBuffer(const Description& entry, Tessellator tessellator); |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 200 | |
| 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 Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame] | 214 | void operator()(Description& description, Buffer*& buffer) override; |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 215 | }; |
| 216 | BufferRemovedListener mBufferRemovedListener; |
| 217 | |
| 218 | /////////////////////////////////////////////////////////////////////////////// |
| 219 | // Shadow tessellation caching |
| 220 | /////////////////////////////////////////////////////////////////////////////// |
Chris Craik | d8165e8 | 2016-02-03 15:52:25 -0800 | [diff] [blame^] | 221 | sp<TaskProcessor<vertexBuffer_pair_t> > mShadowProcessor; |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 222 | |
| 223 | // holds a pointer, and implicit strong ref to each shadow task of the frame |
Chris Craik | d8165e8 | 2016-02-03 15:52:25 -0800 | [diff] [blame^] | 224 | 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 Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame] | 227 | bufferPairTask->decStrong(nullptr); |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 228 | } |
| 229 | }; |
| 230 | BufferPairRemovedListener mBufferPairRemovedListener; |
| 231 | |
| 232 | }; // class TessellationCache |
| 233 | |
John Reck | 82f5e0c | 2015-10-22 17:07:45 -0700 | [diff] [blame] | 234 | void 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 Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 241 | }; // namespace uirenderer |
| 242 | }; // namespace android |
| 243 | |
| 244 | #endif // ANDROID_HWUI_PATH_CACHE_H |