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(); |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 134 | /** |
| 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 Craik | 6ac174b | 2014-06-17 13:47:05 -0700 | [diff] [blame] | 159 | 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 Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 162 | } |
Chris Craik | 6ac174b | 2014-06-17 13:47:05 -0700 | [diff] [blame] | 163 | const VertexBuffer* getRoundRect(const Matrix4& transform, const SkPaint& paint, |
| 164 | float width, float height, float rx, float ry); |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 165 | |
Chris Craik | 6e068c01 | 2016-01-15 16:15:30 -0800 | [diff] [blame] | 166 | // TODO: delete these when switching to HWUI_NEW_OPS |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 167 | 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 Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 171 | 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 Craik | 6e068c01 | 2016-01-15 16:15:30 -0800 | [diff] [blame] | 177 | 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 Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 182 | private: |
| 183 | class Buffer; |
| 184 | class TessellationTask; |
| 185 | class TessellationProcessor; |
| 186 | |
Chris Craik | 6ac174b | 2014-06-17 13:47:05 -0700 | [diff] [blame] | 187 | typedef VertexBuffer* (*Tessellator)(const Description&); |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 188 | |
Chris Craik | 6ac174b | 2014-06-17 13:47:05 -0700 | [diff] [blame] | 189 | 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 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* getOrCreateBuffer(const Description& entry, Tessellator tessellator); |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 195 | |
Chris Craik | 48a8f43 | 2016-02-05 15:59:29 -0800 | [diff] [blame] | 196 | const uint32_t mMaxSize; |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 197 | |
| 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 Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame] | 208 | void operator()(Description& description, Buffer*& buffer) override; |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 209 | }; |
| 210 | BufferRemovedListener mBufferRemovedListener; |
| 211 | |
| 212 | /////////////////////////////////////////////////////////////////////////////// |
| 213 | // Shadow tessellation caching |
| 214 | /////////////////////////////////////////////////////////////////////////////// |
Chris Craik | d8165e8 | 2016-02-03 15:52:25 -0800 | [diff] [blame] | 215 | sp<TaskProcessor<vertexBuffer_pair_t> > mShadowProcessor; |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 216 | |
| 217 | // 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] | 218 | 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 Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame] | 221 | bufferPairTask->decStrong(nullptr); |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 222 | } |
| 223 | }; |
| 224 | BufferPairRemovedListener mBufferPairRemovedListener; |
| 225 | |
| 226 | }; // class TessellationCache |
| 227 | |
John Reck | 82f5e0c | 2015-10-22 17:07:45 -0700 | [diff] [blame] | 228 | void 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 Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 235 | }; // namespace uirenderer |
| 236 | }; // namespace android |
| 237 | |
| 238 | #endif // ANDROID_HWUI_PATH_CACHE_H |