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