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 | #include "OffscreenBufferPool.h" |
| 18 | |
| 19 | #include "Caches.h" |
| 20 | #include "Properties.h" |
| 21 | #include "renderstate/RenderState.h" |
| 22 | #include "utils/FatVector.h" |
Chris Craik | aff230f | 2016-05-04 16:27:28 -0700 | [diff] [blame] | 23 | #include "utils/TraceUtils.h" |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 24 | |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 25 | #include <utils/Color.h> |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 26 | #include <utils/Log.h> |
| 27 | |
| 28 | #include <GLES2/gl2.h> |
| 29 | |
| 30 | namespace android { |
| 31 | namespace uirenderer { |
| 32 | |
| 33 | //////////////////////////////////////////////////////////////////////////////// |
| 34 | // OffscreenBuffer |
| 35 | //////////////////////////////////////////////////////////////////////////////// |
| 36 | |
| 37 | OffscreenBuffer::OffscreenBuffer(RenderState& renderState, Caches& caches, |
Romain Guy | 07ae505 | 2017-06-13 18:25:32 -0700 | [diff] [blame^] | 38 | uint32_t viewportWidth, uint32_t viewportHeight, bool wideColorGamut) |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 39 | : GpuMemoryTracker(GpuObjectType::OffscreenBuffer) |
| 40 | , renderState(renderState) |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 41 | , viewportWidth(viewportWidth) |
| 42 | , viewportHeight(viewportHeight) |
Romain Guy | 07ae505 | 2017-06-13 18:25:32 -0700 | [diff] [blame^] | 43 | , texture(caches) |
| 44 | , wideColorGamut(wideColorGamut) { |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 45 | uint32_t width = computeIdealDimension(viewportWidth); |
| 46 | uint32_t height = computeIdealDimension(viewportHeight); |
Chris Craik | aff230f | 2016-05-04 16:27:28 -0700 | [diff] [blame] | 47 | ATRACE_FORMAT("Allocate %ux%u HW Layer", width, height); |
John Reck | bd41ded | 2016-01-22 09:31:28 -0800 | [diff] [blame] | 48 | caches.textureState().activateTexture(0); |
Romain Guy | 07ae505 | 2017-06-13 18:25:32 -0700 | [diff] [blame^] | 49 | texture.resize(width, height, |
| 50 | wideColorGamut ? GL_RGBA16F : caches.rgbaInternalFormat(), GL_RGBA); |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 51 | texture.blend = true; |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 52 | texture.setWrap(GL_CLAMP_TO_EDGE); |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 53 | // not setting filter on texture, since it's set when drawing, based on transform |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Chris Craik | 7435eb1 | 2016-01-07 17:41:40 -0800 | [diff] [blame] | 56 | Rect OffscreenBuffer::getTextureCoordinates() { |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 57 | const float texX = 1.0f / static_cast<float>(texture.width()); |
| 58 | const float texY = 1.0f / static_cast<float>(texture.height()); |
Chris Craik | 7435eb1 | 2016-01-07 17:41:40 -0800 | [diff] [blame] | 59 | return Rect(0, viewportHeight * texY, viewportWidth * texX, 0); |
| 60 | } |
| 61 | |
Chris Craik | 64db2bf | 2016-02-26 15:01:24 -0800 | [diff] [blame] | 62 | void OffscreenBuffer::dirty(Rect dirtyArea) { |
| 63 | dirtyArea.doIntersect(0, 0, viewportWidth, viewportHeight); |
| 64 | if (!dirtyArea.isEmpty()) { |
| 65 | region.orSelf(android::Rect(dirtyArea.left, dirtyArea.top, |
| 66 | dirtyArea.right, dirtyArea.bottom)); |
| 67 | } |
| 68 | } |
| 69 | |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 70 | void OffscreenBuffer::updateMeshFromRegion() { |
| 71 | // avoid T-junctions as they cause artifacts in between the resultant |
| 72 | // geometry when complex transforms occur. |
| 73 | // TODO: generate the safeRegion only if necessary based on drawing transform |
| 74 | Region safeRegion = Region::createTJunctionFreeRegion(region); |
| 75 | |
| 76 | size_t count; |
| 77 | const android::Rect* rects = safeRegion.getArray(&count); |
| 78 | |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 79 | const float texX = 1.0f / float(texture.width()); |
| 80 | const float texY = 1.0f / float(texture.height()); |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 81 | |
| 82 | FatVector<TextureVertex, 64> meshVector(count * 4); // uses heap if more than 64 vertices needed |
| 83 | TextureVertex* mesh = &meshVector[0]; |
| 84 | for (size_t i = 0; i < count; i++) { |
| 85 | const android::Rect* r = &rects[i]; |
| 86 | |
| 87 | const float u1 = r->left * texX; |
| 88 | const float v1 = (viewportHeight - r->top) * texY; |
| 89 | const float u2 = r->right * texX; |
| 90 | const float v2 = (viewportHeight - r->bottom) * texY; |
| 91 | |
| 92 | TextureVertex::set(mesh++, r->left, r->top, u1, v1); |
| 93 | TextureVertex::set(mesh++, r->right, r->top, u2, v1); |
| 94 | TextureVertex::set(mesh++, r->left, r->bottom, u1, v2); |
| 95 | TextureVertex::set(mesh++, r->right, r->bottom, u2, v2); |
| 96 | } |
| 97 | elementCount = count * 6; |
| 98 | renderState.meshState().genOrUpdateMeshBuffer(&vbo, |
| 99 | sizeof(TextureVertex) * count * 4, |
| 100 | &meshVector[0], |
| 101 | GL_DYNAMIC_DRAW); // TODO: GL_STATIC_DRAW if savelayer |
| 102 | } |
| 103 | |
| 104 | uint32_t OffscreenBuffer::computeIdealDimension(uint32_t dimension) { |
| 105 | return uint32_t(ceilf(dimension / float(LAYER_SIZE)) * LAYER_SIZE); |
| 106 | } |
| 107 | |
| 108 | OffscreenBuffer::~OffscreenBuffer() { |
Chris Craik | aff230f | 2016-05-04 16:27:28 -0700 | [diff] [blame] | 109 | ATRACE_FORMAT("Destroy %ux%u HW Layer", texture.width(), texture.height()); |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 110 | texture.deleteTexture(); |
| 111 | renderState.meshState().deleteMeshBuffer(vbo); |
| 112 | elementCount = 0; |
| 113 | vbo = 0; |
| 114 | } |
| 115 | |
| 116 | /////////////////////////////////////////////////////////////////////////////// |
| 117 | // OffscreenBufferPool |
| 118 | /////////////////////////////////////////////////////////////////////////////// |
| 119 | |
| 120 | OffscreenBufferPool::OffscreenBufferPool() |
| 121 | : mMaxSize(Properties::layerPoolSize) { |
| 122 | } |
| 123 | |
| 124 | OffscreenBufferPool::~OffscreenBufferPool() { |
| 125 | clear(); // TODO: unique_ptr? |
| 126 | } |
| 127 | |
| 128 | int OffscreenBufferPool::Entry::compare(const Entry& lhs, const Entry& rhs) { |
| 129 | int deltaInt = int(lhs.width) - int(rhs.width); |
| 130 | if (deltaInt != 0) return deltaInt; |
| 131 | |
Romain Guy | 07ae505 | 2017-06-13 18:25:32 -0700 | [diff] [blame^] | 132 | deltaInt = int(lhs.height) - int(rhs.height); |
| 133 | if (deltaInt != 0) return deltaInt; |
| 134 | |
| 135 | return int(lhs.wideColorGamut) - int(rhs.wideColorGamut); |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | void OffscreenBufferPool::clear() { |
Chris Craik | 74af6e2 | 2016-04-05 13:18:56 -0700 | [diff] [blame] | 139 | for (auto& entry : mPool) { |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 140 | delete entry.layer; |
| 141 | } |
| 142 | mPool.clear(); |
| 143 | mSize = 0; |
| 144 | } |
| 145 | |
| 146 | OffscreenBuffer* OffscreenBufferPool::get(RenderState& renderState, |
Romain Guy | 07ae505 | 2017-06-13 18:25:32 -0700 | [diff] [blame^] | 147 | const uint32_t width, const uint32_t height, bool wideColorGamut) { |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 148 | OffscreenBuffer* layer = nullptr; |
| 149 | |
Romain Guy | 07ae505 | 2017-06-13 18:25:32 -0700 | [diff] [blame^] | 150 | Entry entry(width, height, wideColorGamut); |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 151 | auto iter = mPool.find(entry); |
| 152 | |
| 153 | if (iter != mPool.end()) { |
| 154 | entry = *iter; |
| 155 | mPool.erase(iter); |
| 156 | |
| 157 | layer = entry.layer; |
| 158 | layer->viewportWidth = width; |
| 159 | layer->viewportHeight = height; |
| 160 | mSize -= layer->getSizeInBytes(); |
| 161 | } else { |
Romain Guy | 07ae505 | 2017-06-13 18:25:32 -0700 | [diff] [blame^] | 162 | layer = new OffscreenBuffer(renderState, Caches::getInstance(), |
| 163 | width, height, wideColorGamut); |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | return layer; |
| 167 | } |
| 168 | |
| 169 | OffscreenBuffer* OffscreenBufferPool::resize(OffscreenBuffer* layer, |
| 170 | const uint32_t width, const uint32_t height) { |
| 171 | RenderState& renderState = layer->renderState; |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 172 | if (layer->texture.width() == OffscreenBuffer::computeIdealDimension(width) |
| 173 | && layer->texture.height() == OffscreenBuffer::computeIdealDimension(height)) { |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 174 | // resize in place |
| 175 | layer->viewportWidth = width; |
| 176 | layer->viewportHeight = height; |
Chris Craik | 0280628 | 2016-03-11 19:16:21 -0800 | [diff] [blame] | 177 | |
| 178 | // entire area will be repainted (and may be smaller) so clear usage region |
| 179 | layer->region.clear(); |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 180 | return layer; |
| 181 | } |
| 182 | putOrDelete(layer); |
Romain Guy | 07ae505 | 2017-06-13 18:25:32 -0700 | [diff] [blame^] | 183 | return get(renderState, width, height, layer->wideColorGamut); |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | void OffscreenBufferPool::dump() { |
| 187 | for (auto entry : mPool) { |
| 188 | ALOGD(" Layer size %dx%d", entry.width, entry.height); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | void OffscreenBufferPool::putOrDelete(OffscreenBuffer* layer) { |
| 193 | const uint32_t size = layer->getSizeInBytes(); |
| 194 | // Don't even try to cache a layer that's bigger than the cache |
| 195 | if (size < mMaxSize) { |
| 196 | // TODO: Use an LRU |
| 197 | while (mSize + size > mMaxSize) { |
| 198 | OffscreenBuffer* victim = mPool.begin()->layer; |
| 199 | mSize -= victim->getSizeInBytes(); |
| 200 | delete victim; |
| 201 | mPool.erase(mPool.begin()); |
| 202 | } |
| 203 | |
| 204 | // clear region, since it's no longer valid |
| 205 | layer->region.clear(); |
| 206 | |
| 207 | Entry entry(layer); |
| 208 | |
| 209 | mPool.insert(entry); |
| 210 | mSize += size; |
| 211 | } else { |
| 212 | delete layer; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | }; // namespace uirenderer |
| 217 | }; // namespace android |