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 | |
Chris Craik | 5e00c7c | 2016-07-06 16:10:09 -0700 | [diff] [blame] | 17 | #pragma once |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 18 | |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 19 | #include "Debug.h" |
John Reck | 82f5e0c | 2015-10-22 17:07:45 -0700 | [diff] [blame] | 20 | #include "Matrix.h" |
| 21 | #include "Rect.h" |
| 22 | #include "Vector.h" |
Chris Craik | d8165e8 | 2016-02-03 15:52:25 -0800 | [diff] [blame] | 23 | #include "VertexBuffer.h" |
John Reck | 82f5e0c | 2015-10-22 17:07:45 -0700 | [diff] [blame] | 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> |
Chris Craik | 6e068c01 | 2016-01-15 16:15:30 -0800 | [diff] [blame] | 29 | #include <SkPath.h> |
John Reck | 82f5e0c | 2015-10-22 17:07:45 -0700 | [diff] [blame] | 30 | |
| 31 | #include <utils/LruCache.h> |
| 32 | #include <utils/Mutex.h> |
| 33 | #include <utils/StrongPointer.h> |
| 34 | |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 35 | class SkBitmap; |
| 36 | class SkCanvas; |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 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 { |
sergeyv | 7224e2b | 2016-04-07 18:06:53 -0700 | [diff] [blame] | 54 | HASHABLE_TYPE(Description); |
| 55 | enum class Type { |
| 56 | None, |
| 57 | RoundRect, |
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 | 6ac174b | 2014-06-17 13:47:05 -0700 | [diff] [blame] | 78 | void setupMatrixAndPaint(Matrix4* matrix, SkPaint* paint) const; |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | struct ShadowDescription { |
sergeyv | 7224e2b | 2016-04-07 18:06:53 -0700 | [diff] [blame] | 82 | HASHABLE_TYPE(ShadowDescription); |
| 83 | const SkPath* nodeKey; |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 84 | float matrixData[16]; |
| 85 | |
| 86 | ShadowDescription(); |
sergeyv | 7224e2b | 2016-04-07 18:06:53 -0700 | [diff] [blame] | 87 | ShadowDescription(const SkPath* nodeKey, const Matrix4* drawTransform); |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 88 | }; |
| 89 | |
Chris Craik | d8165e8 | 2016-02-03 15:52:25 -0800 | [diff] [blame] | 90 | class ShadowTask : public Task<vertexBuffer_pair_t> { |
Chris Craik | 6e068c01 | 2016-01-15 16:15:30 -0800 | [diff] [blame] | 91 | public: |
| 92 | ShadowTask(const Matrix4* drawTransform, const Rect& localClip, bool opaque, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 93 | const SkPath* casterPerimeter, const Matrix4* transformXY, |
| 94 | const Matrix4* transformZ, const Vector3& lightCenter, float lightRadius) |
| 95 | : drawTransform(*drawTransform) |
| 96 | , localClip(localClip) |
| 97 | , opaque(opaque) |
| 98 | , casterPerimeter(*casterPerimeter) |
| 99 | , transformXY(*transformXY) |
| 100 | , transformZ(*transformZ) |
| 101 | , lightCenter(lightCenter) |
| 102 | , lightRadius(lightRadius) {} |
Chris Craik | 6e068c01 | 2016-01-15 16:15:30 -0800 | [diff] [blame] | 103 | |
Chris Craik | 6e068c01 | 2016-01-15 16:15:30 -0800 | [diff] [blame] | 104 | /* Note - we deep copy all task parameters, because *even though* pointers into Allocator |
| 105 | * controlled objects (like the SkPath and Matrix4s) should be safe for the entire frame, |
| 106 | * certain Allocators are destroyed before trim() is called to flush incomplete tasks. |
| 107 | * |
Chris Craik | d8165e8 | 2016-02-03 15:52:25 -0800 | [diff] [blame] | 108 | * 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] | 109 | * tasks before tearing down single-frame LinearAllocators. |
| 110 | */ |
| 111 | const Matrix4 drawTransform; |
| 112 | const Rect localClip; |
| 113 | bool opaque; |
| 114 | const SkPath casterPerimeter; |
| 115 | const Matrix4 transformXY; |
| 116 | const Matrix4 transformZ; |
| 117 | const Vector3 lightCenter; |
| 118 | const float lightRadius; |
Chris Craik | d8165e8 | 2016-02-03 15:52:25 -0800 | [diff] [blame] | 119 | VertexBuffer ambientBuffer; |
| 120 | VertexBuffer spotBuffer; |
Chris Craik | 6e068c01 | 2016-01-15 16:15:30 -0800 | [diff] [blame] | 121 | }; |
| 122 | |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 123 | TessellationCache(); |
| 124 | ~TessellationCache(); |
| 125 | |
| 126 | /** |
| 127 | * Clears the cache. This causes all TessellationBuffers to be deleted. |
| 128 | */ |
| 129 | void clear(); |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 130 | /** |
| 131 | * Returns the maximum size of the cache in bytes. |
| 132 | */ |
| 133 | uint32_t getMaxSize(); |
| 134 | /** |
| 135 | * Returns the current size of the cache in bytes. |
| 136 | */ |
| 137 | uint32_t getSize(); |
| 138 | |
| 139 | /** |
| 140 | * Trims the contents of the cache, removing items until it's under its |
| 141 | * specified limit. |
| 142 | * |
| 143 | * Trimming is used for caches that support pre-caching from a worker |
| 144 | * thread. During pre-caching the maximum limit of the cache can be |
| 145 | * exceeded for the duration of the frame. It is therefore required to |
| 146 | * trim the cache at the end of the frame to keep the total amount of |
| 147 | * memory used under control. |
| 148 | * |
| 149 | * Also removes transient Shadow VertexBuffers, which aren't cached between frames. |
| 150 | */ |
| 151 | void trim(); |
| 152 | |
| 153 | // TODO: precache/get for Oval, Lines, Points, etc. |
| 154 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 155 | void precacheRoundRect(const Matrix4& transform, const SkPaint& paint, float width, |
| 156 | float height, float rx, float ry) { |
Chris Craik | 6ac174b | 2014-06-17 13:47:05 -0700 | [diff] [blame] | 157 | getRoundRectBuffer(transform, paint, width, height, rx, ry); |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 158 | } |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 159 | const VertexBuffer* getRoundRect(const Matrix4& transform, const SkPaint& paint, float width, |
| 160 | float height, float rx, float ry); |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 161 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 162 | sp<ShadowTask> getShadowTask(const Matrix4* drawTransform, const Rect& localClip, bool opaque, |
| 163 | const SkPath* casterPerimeter, const Matrix4* transformXY, |
| 164 | const Matrix4* transformZ, const Vector3& lightCenter, |
| 165 | float lightRadius); |
Chris Craik | 6e068c01 | 2016-01-15 16:15:30 -0800 | [diff] [blame] | 166 | |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 167 | private: |
| 168 | class Buffer; |
| 169 | class TessellationTask; |
| 170 | class TessellationProcessor; |
| 171 | |
Chris Craik | 6ac174b | 2014-06-17 13:47:05 -0700 | [diff] [blame] | 172 | typedef VertexBuffer* (*Tessellator)(const Description&); |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 173 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 174 | void precacheShadows(const Matrix4* drawTransform, const Rect& localClip, bool opaque, |
| 175 | const SkPath* casterPerimeter, const Matrix4* transformXY, |
| 176 | const Matrix4* transformZ, const Vector3& lightCenter, float lightRadius); |
Chris Craik | 5e00c7c | 2016-07-06 16:10:09 -0700 | [diff] [blame] | 177 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 178 | Buffer* getRectBuffer(const Matrix4& transform, const SkPaint& paint, float width, |
| 179 | float height); |
| 180 | Buffer* getRoundRectBuffer(const Matrix4& transform, const SkPaint& paint, float width, |
| 181 | float height, float rx, float ry); |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 182 | |
Chris Craik | 6ac174b | 2014-06-17 13:47:05 -0700 | [diff] [blame] | 183 | Buffer* getOrCreateBuffer(const Description& entry, Tessellator tessellator); |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 184 | |
Chris Craik | 48a8f43 | 2016-02-05 15:59:29 -0800 | [diff] [blame] | 185 | const uint32_t mMaxSize; |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 186 | |
| 187 | bool mDebugEnabled; |
| 188 | |
| 189 | mutable Mutex mLock; |
| 190 | |
| 191 | /////////////////////////////////////////////////////////////////////////////// |
| 192 | // General tessellation caching |
| 193 | /////////////////////////////////////////////////////////////////////////////// |
| 194 | sp<TaskProcessor<VertexBuffer*> > mProcessor; |
| 195 | LruCache<Description, Buffer*> mCache; |
| 196 | class BufferRemovedListener : public OnEntryRemoved<Description, Buffer*> { |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame] | 197 | void operator()(Description& description, Buffer*& buffer) override; |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 198 | }; |
| 199 | BufferRemovedListener mBufferRemovedListener; |
| 200 | |
| 201 | /////////////////////////////////////////////////////////////////////////////// |
| 202 | // Shadow tessellation caching |
| 203 | /////////////////////////////////////////////////////////////////////////////// |
Chris Craik | d8165e8 | 2016-02-03 15:52:25 -0800 | [diff] [blame] | 204 | sp<TaskProcessor<vertexBuffer_pair_t> > mShadowProcessor; |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 205 | |
| 206 | // 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] | 207 | LruCache<ShadowDescription, Task<vertexBuffer_pair_t>*> mShadowCache; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 208 | class BufferPairRemovedListener |
| 209 | : public OnEntryRemoved<ShadowDescription, Task<vertexBuffer_pair_t>*> { |
| 210 | void operator()(ShadowDescription& description, |
| 211 | Task<vertexBuffer_pair_t>*& bufferPairTask) override { |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame] | 212 | bufferPairTask->decStrong(nullptr); |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 213 | } |
| 214 | }; |
| 215 | BufferPairRemovedListener mBufferPairRemovedListener; |
| 216 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 217 | }; // class TessellationCache |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 218 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 219 | void tessellateShadows(const Matrix4* drawTransform, const Rect* localClip, bool isCasterOpaque, |
| 220 | const SkPath* casterPerimeter, const Matrix4* casterTransformXY, |
| 221 | const Matrix4* casterTransformZ, const Vector3& lightCenter, |
| 222 | float lightRadius, VertexBuffer& ambientBuffer, VertexBuffer& spotBuffer); |
John Reck | 82f5e0c | 2015-10-22 17:07:45 -0700 | [diff] [blame] | 223 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 224 | }; // namespace uirenderer |
| 225 | }; // namespace android |