blob: e148ebdf1dd7efb6dda8058351c7ae124528f61a [file] [log] [blame]
Chris Craik9fded232015-11-11 16:42:34 -08001/*
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"
Chris Craik9fded232015-11-11 16:42:34 -080020#include "renderstate/RenderState.h"
21#include "utils/FatVector.h"
Chris Craikaff230f2016-05-04 16:27:28 -070022#include "utils/TraceUtils.h"
Chris Craik9fded232015-11-11 16:42:34 -080023
Romain Guy253f2c22016-09-28 17:34:42 -070024#include <utils/Color.h>
Chris Craik9fded232015-11-11 16:42:34 -080025#include <utils/Log.h>
26
27#include <GLES2/gl2.h>
28
29namespace android {
30namespace uirenderer {
31
32////////////////////////////////////////////////////////////////////////////////
33// OffscreenBuffer
34////////////////////////////////////////////////////////////////////////////////
35
John Reck1bcacfd2017-11-03 10:12:19 -070036OffscreenBuffer::OffscreenBuffer(RenderState& renderState, Caches& caches, uint32_t viewportWidth,
37 uint32_t viewportHeight, bool wideColorGamut)
John Reck38e0c322015-11-10 12:19:17 -080038 : GpuMemoryTracker(GpuObjectType::OffscreenBuffer)
39 , renderState(renderState)
Chris Craik9fded232015-11-11 16:42:34 -080040 , viewportWidth(viewportWidth)
41 , viewportHeight(viewportHeight)
Romain Guy07ae5052017-06-13 18:25:32 -070042 , texture(caches)
43 , wideColorGamut(wideColorGamut) {
John Reck38e0c322015-11-10 12:19:17 -080044 uint32_t width = computeIdealDimension(viewportWidth);
45 uint32_t height = computeIdealDimension(viewportHeight);
Chris Craikaff230f2016-05-04 16:27:28 -070046 ATRACE_FORMAT("Allocate %ux%u HW Layer", width, height);
John Reckbd41ded2016-01-22 09:31:28 -080047 caches.textureState().activateTexture(0);
John Reck1bcacfd2017-11-03 10:12:19 -070048 texture.resize(width, height, wideColorGamut ? GL_RGBA16F : caches.rgbaInternalFormat(),
49 GL_RGBA);
Chris Craik9fded232015-11-11 16:42:34 -080050 texture.blend = true;
John Reck38e0c322015-11-10 12:19:17 -080051 texture.setWrap(GL_CLAMP_TO_EDGE);
Chris Craik9fded232015-11-11 16:42:34 -080052 // not setting filter on texture, since it's set when drawing, based on transform
Chris Craik9fded232015-11-11 16:42:34 -080053}
54
Chris Craik7435eb12016-01-07 17:41:40 -080055Rect OffscreenBuffer::getTextureCoordinates() {
John Reck38e0c322015-11-10 12:19:17 -080056 const float texX = 1.0f / static_cast<float>(texture.width());
57 const float texY = 1.0f / static_cast<float>(texture.height());
Chris Craik7435eb12016-01-07 17:41:40 -080058 return Rect(0, viewportHeight * texY, viewportWidth * texX, 0);
59}
60
Chris Craik64db2bf2016-02-26 15:01:24 -080061void OffscreenBuffer::dirty(Rect dirtyArea) {
62 dirtyArea.doIntersect(0, 0, viewportWidth, viewportHeight);
63 if (!dirtyArea.isEmpty()) {
John Reck1bcacfd2017-11-03 10:12:19 -070064 region.orSelf(
65 android::Rect(dirtyArea.left, dirtyArea.top, dirtyArea.right, dirtyArea.bottom));
Chris Craik64db2bf2016-02-26 15:01:24 -080066 }
67}
68
Chris Craik9fded232015-11-11 16:42:34 -080069void OffscreenBuffer::updateMeshFromRegion() {
Mike Reede7f688b2018-05-04 15:21:43 -040070 // DEAD CODE
Chris Craik9fded232015-11-11 16:42:34 -080071}
72
73uint32_t OffscreenBuffer::computeIdealDimension(uint32_t dimension) {
74 return uint32_t(ceilf(dimension / float(LAYER_SIZE)) * LAYER_SIZE);
75}
76
77OffscreenBuffer::~OffscreenBuffer() {
Mike Reede7f688b2018-05-04 15:21:43 -040078 // DEAD CODE
Chris Craik9fded232015-11-11 16:42:34 -080079}
80
81///////////////////////////////////////////////////////////////////////////////
82// OffscreenBufferPool
83///////////////////////////////////////////////////////////////////////////////
84
85OffscreenBufferPool::OffscreenBufferPool()
John Reck1bcacfd2017-11-03 10:12:19 -070086 // 4 screen-sized RGBA_8888 textures
87 : mMaxSize(DeviceInfo::multiplyByResolution(4 * 4)) {}
Chris Craik9fded232015-11-11 16:42:34 -080088
89OffscreenBufferPool::~OffscreenBufferPool() {
John Reck1bcacfd2017-11-03 10:12:19 -070090 clear(); // TODO: unique_ptr?
Chris Craik9fded232015-11-11 16:42:34 -080091}
92
93int OffscreenBufferPool::Entry::compare(const Entry& lhs, const Entry& rhs) {
94 int deltaInt = int(lhs.width) - int(rhs.width);
95 if (deltaInt != 0) return deltaInt;
96
Romain Guy07ae5052017-06-13 18:25:32 -070097 deltaInt = int(lhs.height) - int(rhs.height);
98 if (deltaInt != 0) return deltaInt;
99
100 return int(lhs.wideColorGamut) - int(rhs.wideColorGamut);
Chris Craik9fded232015-11-11 16:42:34 -0800101}
102
103void OffscreenBufferPool::clear() {
Chris Craik74af6e22016-04-05 13:18:56 -0700104 for (auto& entry : mPool) {
Chris Craik9fded232015-11-11 16:42:34 -0800105 delete entry.layer;
106 }
107 mPool.clear();
108 mSize = 0;
109}
110
John Reck1bcacfd2017-11-03 10:12:19 -0700111OffscreenBuffer* OffscreenBufferPool::get(RenderState& renderState, const uint32_t width,
112 const uint32_t height, bool wideColorGamut) {
Chris Craik9fded232015-11-11 16:42:34 -0800113 OffscreenBuffer* layer = nullptr;
114
Romain Guy07ae5052017-06-13 18:25:32 -0700115 Entry entry(width, height, wideColorGamut);
Chris Craik9fded232015-11-11 16:42:34 -0800116 auto iter = mPool.find(entry);
117
118 if (iter != mPool.end()) {
119 entry = *iter;
120 mPool.erase(iter);
121
122 layer = entry.layer;
123 layer->viewportWidth = width;
124 layer->viewportHeight = height;
125 mSize -= layer->getSizeInBytes();
126 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700127 layer = new OffscreenBuffer(renderState, Caches::getInstance(), width, height,
128 wideColorGamut);
Chris Craik9fded232015-11-11 16:42:34 -0800129 }
130
131 return layer;
132}
133
John Reck1bcacfd2017-11-03 10:12:19 -0700134OffscreenBuffer* OffscreenBufferPool::resize(OffscreenBuffer* layer, const uint32_t width,
135 const uint32_t height) {
Chris Craik9fded232015-11-11 16:42:34 -0800136 RenderState& renderState = layer->renderState;
John Reck1bcacfd2017-11-03 10:12:19 -0700137 if (layer->texture.width() == OffscreenBuffer::computeIdealDimension(width) &&
138 layer->texture.height() == OffscreenBuffer::computeIdealDimension(height)) {
Chris Craik9fded232015-11-11 16:42:34 -0800139 // resize in place
140 layer->viewportWidth = width;
141 layer->viewportHeight = height;
Chris Craik02806282016-03-11 19:16:21 -0800142
143 // entire area will be repainted (and may be smaller) so clear usage region
144 layer->region.clear();
Chris Craik9fded232015-11-11 16:42:34 -0800145 return layer;
146 }
George Burgess IV09c00f22017-07-18 15:59:47 -0700147 bool wideColorGamut = layer->wideColorGamut;
Chris Craik9fded232015-11-11 16:42:34 -0800148 putOrDelete(layer);
George Burgess IV09c00f22017-07-18 15:59:47 -0700149 return get(renderState, width, height, wideColorGamut);
Chris Craik9fded232015-11-11 16:42:34 -0800150}
151
152void OffscreenBufferPool::dump() {
153 for (auto entry : mPool) {
154 ALOGD(" Layer size %dx%d", entry.width, entry.height);
155 }
156}
157
158void OffscreenBufferPool::putOrDelete(OffscreenBuffer* layer) {
159 const uint32_t size = layer->getSizeInBytes();
160 // Don't even try to cache a layer that's bigger than the cache
161 if (size < mMaxSize) {
162 // TODO: Use an LRU
163 while (mSize + size > mMaxSize) {
164 OffscreenBuffer* victim = mPool.begin()->layer;
165 mSize -= victim->getSizeInBytes();
166 delete victim;
167 mPool.erase(mPool.begin());
168 }
169
170 // clear region, since it's no longer valid
171 layer->region.clear();
172
173 Entry entry(layer);
174
175 mPool.insert(entry);
176 mSize += size;
177 } else {
178 delete layer;
179 }
180}
181
John Reck1bcacfd2017-11-03 10:12:19 -0700182}; // namespace uirenderer
183}; // namespace android