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