Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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_OFFSCREEN_BUFFER_POOL_H |
| 18 | #define ANDROID_HWUI_OFFSCREEN_BUFFER_POOL_H |
| 19 | |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 20 | #include <GpuMemoryTracker.h> |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 21 | #include "Caches.h" |
| 22 | #include "Texture.h" |
| 23 | #include "utils/Macros.h" |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 24 | #include <ui/Region.h> |
| 25 | |
| 26 | #include <set> |
| 27 | |
| 28 | namespace android { |
| 29 | namespace uirenderer { |
| 30 | |
| 31 | class RenderState; |
| 32 | |
| 33 | /** |
| 34 | * Lightweight alternative to Layer. Owns the persistent state of an offscreen render target, and |
| 35 | * encompasses enough information to draw it back on screen (minus paint properties, which are held |
| 36 | * by LayerOp). |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 37 | * |
| 38 | * Has two distinct sizes - viewportWidth/viewportHeight describe content area, |
| 39 | * texture.width/.height are actual allocated texture size. Texture will tend to be larger than the |
| 40 | * viewport bounds, since textures are always allocated with width / height as a multiple of 64, for |
| 41 | * the purpose of improving reuse. |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 42 | */ |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 43 | class OffscreenBuffer : GpuMemoryTracker { |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 44 | public: |
| 45 | OffscreenBuffer(RenderState& renderState, Caches& caches, |
| 46 | uint32_t viewportWidth, uint32_t viewportHeight); |
| 47 | ~OffscreenBuffer(); |
| 48 | |
Chris Craik | 7435eb1 | 2016-01-07 17:41:40 -0800 | [diff] [blame] | 49 | Rect getTextureCoordinates(); |
| 50 | |
Chris Craik | 64db2bf | 2016-02-26 15:01:24 -0800 | [diff] [blame^] | 51 | void dirty(Rect dirtyArea); |
| 52 | |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 53 | // must be called prior to rendering, to construct/update vertex buffer |
| 54 | void updateMeshFromRegion(); |
| 55 | |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 56 | // Set by RenderNode for HW layers, TODO for clipped saveLayers |
| 57 | void setWindowTransform(const Matrix4& transform) { |
| 58 | inverseTransformInWindow.loadInverse(transform); |
| 59 | } |
| 60 | |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 61 | static uint32_t computeIdealDimension(uint32_t dimension); |
| 62 | |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 63 | uint32_t getSizeInBytes() { return texture.objectSize(); } |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 64 | |
| 65 | RenderState& renderState; |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 66 | |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 67 | uint32_t viewportWidth; |
| 68 | uint32_t viewportHeight; |
| 69 | Texture texture; |
| 70 | |
| 71 | // Portion of layer that has been drawn to. Used to minimize drawing area when |
| 72 | // drawing back to screen / parent FBO. |
| 73 | Region region; |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 74 | |
| 75 | Matrix4 inverseTransformInWindow; |
| 76 | |
| 77 | // vbo / size of mesh |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 78 | GLsizei elementCount = 0; |
| 79 | GLuint vbo = 0; |
| 80 | }; |
| 81 | |
| 82 | /** |
| 83 | * Pool of OffscreenBuffers allocated, but not currently in use. |
| 84 | */ |
| 85 | class OffscreenBufferPool { |
| 86 | public: |
| 87 | OffscreenBufferPool(); |
| 88 | ~OffscreenBufferPool(); |
| 89 | |
| 90 | WARN_UNUSED_RESULT OffscreenBuffer* get(RenderState& renderState, |
| 91 | const uint32_t width, const uint32_t height); |
| 92 | |
| 93 | WARN_UNUSED_RESULT OffscreenBuffer* resize(OffscreenBuffer* layer, |
| 94 | const uint32_t width, const uint32_t height); |
| 95 | |
| 96 | void putOrDelete(OffscreenBuffer* layer); |
| 97 | |
| 98 | /** |
| 99 | * Clears the pool. This causes all layers to be deleted. |
| 100 | */ |
| 101 | void clear(); |
| 102 | |
| 103 | /** |
| 104 | * Returns the maximum size of the pool in bytes. |
| 105 | */ |
| 106 | uint32_t getMaxSize() { return mMaxSize; } |
| 107 | |
| 108 | /** |
| 109 | * Returns the current size of the pool in bytes. |
| 110 | */ |
| 111 | uint32_t getSize() { return mSize; } |
| 112 | |
| 113 | size_t getCount() { return mPool.size(); } |
| 114 | |
| 115 | /** |
| 116 | * Prints out the content of the pool. |
| 117 | */ |
| 118 | void dump(); |
| 119 | private: |
| 120 | struct Entry { |
| 121 | Entry() {} |
| 122 | |
| 123 | Entry(const uint32_t layerWidth, const uint32_t layerHeight) |
| 124 | : width(OffscreenBuffer::computeIdealDimension(layerWidth)) |
| 125 | , height(OffscreenBuffer::computeIdealDimension(layerHeight)) {} |
| 126 | |
| 127 | Entry(OffscreenBuffer* layer) |
| 128 | : layer(layer) |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 129 | , width(layer->texture.width()) |
| 130 | , height(layer->texture.height()) { |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | static int compare(const Entry& lhs, const Entry& rhs); |
| 134 | |
| 135 | bool operator==(const Entry& other) const { |
| 136 | return compare(*this, other) == 0; |
| 137 | } |
| 138 | |
| 139 | bool operator!=(const Entry& other) const { |
| 140 | return compare(*this, other) != 0; |
| 141 | } |
| 142 | |
| 143 | bool operator<(const Entry& other) const { |
| 144 | return Entry::compare(*this, other) < 0; |
| 145 | } |
| 146 | |
| 147 | OffscreenBuffer* layer = nullptr; |
| 148 | uint32_t width = 0; |
| 149 | uint32_t height = 0; |
| 150 | }; // struct Entry |
| 151 | |
| 152 | std::multiset<Entry> mPool; |
| 153 | |
| 154 | uint32_t mSize = 0; |
| 155 | uint32_t mMaxSize; |
| 156 | }; // class OffscreenBufferCache |
| 157 | |
| 158 | }; // namespace uirenderer |
| 159 | }; // namespace android |
| 160 | |
| 161 | #endif // ANDROID_HWUI_OFFSCREEN_BUFFER_POOL_H |