blob: a0f5cb9d4e09122fd046d3f51efd5212ea9ce522 [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() {
70 // avoid T-junctions as they cause artifacts in between the resultant
71 // geometry when complex transforms occur.
72 // TODO: generate the safeRegion only if necessary based on drawing transform
73 Region safeRegion = Region::createTJunctionFreeRegion(region);
74
75 size_t count;
76 const android::Rect* rects = safeRegion.getArray(&count);
77
John Reck38e0c322015-11-10 12:19:17 -080078 const float texX = 1.0f / float(texture.width());
79 const float texY = 1.0f / float(texture.height());
Chris Craik9fded232015-11-11 16:42:34 -080080
John Reck1bcacfd2017-11-03 10:12:19 -070081 FatVector<TextureVertex, 64> meshVector(count *
82 4); // uses heap if more than 64 vertices needed
Chris Craik9fded232015-11-11 16:42:34 -080083 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;
John Reck1bcacfd2017-11-03 10:12:19 -070098 renderState.meshState().genOrUpdateMeshBuffer(
99 &vbo, sizeof(TextureVertex) * count * 4, &meshVector[0],
100 GL_DYNAMIC_DRAW); // TODO: GL_STATIC_DRAW if savelayer
Chris Craik9fded232015-11-11 16:42:34 -0800101}
102
103uint32_t OffscreenBuffer::computeIdealDimension(uint32_t dimension) {
104 return uint32_t(ceilf(dimension / float(LAYER_SIZE)) * LAYER_SIZE);
105}
106
107OffscreenBuffer::~OffscreenBuffer() {
Chris Craikaff230f2016-05-04 16:27:28 -0700108 ATRACE_FORMAT("Destroy %ux%u HW Layer", texture.width(), texture.height());
Chris Craik9fded232015-11-11 16:42:34 -0800109 texture.deleteTexture();
110 renderState.meshState().deleteMeshBuffer(vbo);
111 elementCount = 0;
112 vbo = 0;
113}
114
115///////////////////////////////////////////////////////////////////////////////
116// OffscreenBufferPool
117///////////////////////////////////////////////////////////////////////////////
118
119OffscreenBufferPool::OffscreenBufferPool()
John Reck1bcacfd2017-11-03 10:12:19 -0700120 // 4 screen-sized RGBA_8888 textures
121 : mMaxSize(DeviceInfo::multiplyByResolution(4 * 4)) {}
Chris Craik9fded232015-11-11 16:42:34 -0800122
123OffscreenBufferPool::~OffscreenBufferPool() {
John Reck1bcacfd2017-11-03 10:12:19 -0700124 clear(); // TODO: unique_ptr?
Chris Craik9fded232015-11-11 16:42:34 -0800125}
126
127int OffscreenBufferPool::Entry::compare(const Entry& lhs, const Entry& rhs) {
128 int deltaInt = int(lhs.width) - int(rhs.width);
129 if (deltaInt != 0) return deltaInt;
130
Romain Guy07ae5052017-06-13 18:25:32 -0700131 deltaInt = int(lhs.height) - int(rhs.height);
132 if (deltaInt != 0) return deltaInt;
133
134 return int(lhs.wideColorGamut) - int(rhs.wideColorGamut);
Chris Craik9fded232015-11-11 16:42:34 -0800135}
136
137void OffscreenBufferPool::clear() {
Chris Craik74af6e22016-04-05 13:18:56 -0700138 for (auto& entry : mPool) {
Chris Craik9fded232015-11-11 16:42:34 -0800139 delete entry.layer;
140 }
141 mPool.clear();
142 mSize = 0;
143}
144
John Reck1bcacfd2017-11-03 10:12:19 -0700145OffscreenBuffer* OffscreenBufferPool::get(RenderState& renderState, const uint32_t width,
146 const uint32_t height, bool wideColorGamut) {
Chris Craik9fded232015-11-11 16:42:34 -0800147 OffscreenBuffer* layer = nullptr;
148
Romain Guy07ae5052017-06-13 18:25:32 -0700149 Entry entry(width, height, wideColorGamut);
Chris Craik9fded232015-11-11 16:42:34 -0800150 auto iter = mPool.find(entry);
151
152 if (iter != mPool.end()) {
153 entry = *iter;
154 mPool.erase(iter);
155
156 layer = entry.layer;
157 layer->viewportWidth = width;
158 layer->viewportHeight = height;
159 mSize -= layer->getSizeInBytes();
160 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700161 layer = new OffscreenBuffer(renderState, Caches::getInstance(), width, height,
162 wideColorGamut);
Chris Craik9fded232015-11-11 16:42:34 -0800163 }
164
165 return layer;
166}
167
John Reck1bcacfd2017-11-03 10:12:19 -0700168OffscreenBuffer* OffscreenBufferPool::resize(OffscreenBuffer* layer, const uint32_t width,
169 const uint32_t height) {
Chris Craik9fded232015-11-11 16:42:34 -0800170 RenderState& renderState = layer->renderState;
John Reck1bcacfd2017-11-03 10:12:19 -0700171 if (layer->texture.width() == OffscreenBuffer::computeIdealDimension(width) &&
172 layer->texture.height() == OffscreenBuffer::computeIdealDimension(height)) {
Chris Craik9fded232015-11-11 16:42:34 -0800173 // resize in place
174 layer->viewportWidth = width;
175 layer->viewportHeight = height;
Chris Craik02806282016-03-11 19:16:21 -0800176
177 // entire area will be repainted (and may be smaller) so clear usage region
178 layer->region.clear();
Chris Craik9fded232015-11-11 16:42:34 -0800179 return layer;
180 }
George Burgess IV09c00f22017-07-18 15:59:47 -0700181 bool wideColorGamut = layer->wideColorGamut;
Chris Craik9fded232015-11-11 16:42:34 -0800182 putOrDelete(layer);
George Burgess IV09c00f22017-07-18 15:59:47 -0700183 return get(renderState, width, height, wideColorGamut);
Chris Craik9fded232015-11-11 16:42:34 -0800184}
185
186void OffscreenBufferPool::dump() {
187 for (auto entry : mPool) {
188 ALOGD(" Layer size %dx%d", entry.width, entry.height);
189 }
190}
191
192void 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
John Reck1bcacfd2017-11-03 10:12:19 -0700216}; // namespace uirenderer
217}; // namespace android