blob: 98c94dfab1c59b295c6839ff462ede25cbcd5a76 [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"
23
24#include <utils/Log.h>
25
26#include <GLES2/gl2.h>
27
28namespace android {
29namespace uirenderer {
30
31////////////////////////////////////////////////////////////////////////////////
32// OffscreenBuffer
33////////////////////////////////////////////////////////////////////////////////
34
35OffscreenBuffer::OffscreenBuffer(RenderState& renderState, Caches& caches,
36 uint32_t viewportWidth, uint32_t viewportHeight)
John Reck38e0c322015-11-10 12:19:17 -080037 : GpuMemoryTracker(GpuObjectType::OffscreenBuffer)
38 , renderState(renderState)
Chris Craik9fded232015-11-11 16:42:34 -080039 , viewportWidth(viewportWidth)
40 , viewportHeight(viewportHeight)
41 , texture(caches) {
John Reck38e0c322015-11-10 12:19:17 -080042 uint32_t width = computeIdealDimension(viewportWidth);
43 uint32_t height = computeIdealDimension(viewportHeight);
44 texture.resize(width, height, GL_RGBA);
Chris Craik9fded232015-11-11 16:42:34 -080045 texture.blend = true;
John Reck38e0c322015-11-10 12:19:17 -080046 texture.setWrap(GL_CLAMP_TO_EDGE);
Chris Craik9fded232015-11-11 16:42:34 -080047 // not setting filter on texture, since it's set when drawing, based on transform
Chris Craik9fded232015-11-11 16:42:34 -080048}
49
Chris Craik7435eb12016-01-07 17:41:40 -080050Rect OffscreenBuffer::getTextureCoordinates() {
John Reck38e0c322015-11-10 12:19:17 -080051 const float texX = 1.0f / static_cast<float>(texture.width());
52 const float texY = 1.0f / static_cast<float>(texture.height());
Chris Craik7435eb12016-01-07 17:41:40 -080053 return Rect(0, viewportHeight * texY, viewportWidth * texX, 0);
54}
55
Chris Craik9fded232015-11-11 16:42:34 -080056void OffscreenBuffer::updateMeshFromRegion() {
57 // avoid T-junctions as they cause artifacts in between the resultant
58 // geometry when complex transforms occur.
59 // TODO: generate the safeRegion only if necessary based on drawing transform
60 Region safeRegion = Region::createTJunctionFreeRegion(region);
61
62 size_t count;
63 const android::Rect* rects = safeRegion.getArray(&count);
64
John Reck38e0c322015-11-10 12:19:17 -080065 const float texX = 1.0f / float(texture.width());
66 const float texY = 1.0f / float(texture.height());
Chris Craik9fded232015-11-11 16:42:34 -080067
68 FatVector<TextureVertex, 64> meshVector(count * 4); // uses heap if more than 64 vertices needed
69 TextureVertex* mesh = &meshVector[0];
70 for (size_t i = 0; i < count; i++) {
71 const android::Rect* r = &rects[i];
72
73 const float u1 = r->left * texX;
74 const float v1 = (viewportHeight - r->top) * texY;
75 const float u2 = r->right * texX;
76 const float v2 = (viewportHeight - r->bottom) * texY;
77
78 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
79 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
80 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
81 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
82 }
83 elementCount = count * 6;
84 renderState.meshState().genOrUpdateMeshBuffer(&vbo,
85 sizeof(TextureVertex) * count * 4,
86 &meshVector[0],
87 GL_DYNAMIC_DRAW); // TODO: GL_STATIC_DRAW if savelayer
88}
89
90uint32_t OffscreenBuffer::computeIdealDimension(uint32_t dimension) {
91 return uint32_t(ceilf(dimension / float(LAYER_SIZE)) * LAYER_SIZE);
92}
93
94OffscreenBuffer::~OffscreenBuffer() {
95 texture.deleteTexture();
96 renderState.meshState().deleteMeshBuffer(vbo);
97 elementCount = 0;
98 vbo = 0;
99}
100
101///////////////////////////////////////////////////////////////////////////////
102// OffscreenBufferPool
103///////////////////////////////////////////////////////////////////////////////
104
105OffscreenBufferPool::OffscreenBufferPool()
106 : mMaxSize(Properties::layerPoolSize) {
107}
108
109OffscreenBufferPool::~OffscreenBufferPool() {
110 clear(); // TODO: unique_ptr?
111}
112
113int OffscreenBufferPool::Entry::compare(const Entry& lhs, const Entry& rhs) {
114 int deltaInt = int(lhs.width) - int(rhs.width);
115 if (deltaInt != 0) return deltaInt;
116
117 return int(lhs.height) - int(rhs.height);
118}
119
120void OffscreenBufferPool::clear() {
121 for (auto entry : mPool) {
122 delete entry.layer;
123 }
124 mPool.clear();
125 mSize = 0;
126}
127
128OffscreenBuffer* OffscreenBufferPool::get(RenderState& renderState,
129 const uint32_t width, const uint32_t height) {
130 OffscreenBuffer* layer = nullptr;
131
132 Entry entry(width, height);
133 auto iter = mPool.find(entry);
134
135 if (iter != mPool.end()) {
136 entry = *iter;
137 mPool.erase(iter);
138
139 layer = entry.layer;
140 layer->viewportWidth = width;
141 layer->viewportHeight = height;
142 mSize -= layer->getSizeInBytes();
143 } else {
144 layer = new OffscreenBuffer(renderState, Caches::getInstance(), width, height);
145 }
146
147 return layer;
148}
149
150OffscreenBuffer* OffscreenBufferPool::resize(OffscreenBuffer* layer,
151 const uint32_t width, const uint32_t height) {
152 RenderState& renderState = layer->renderState;
John Reck38e0c322015-11-10 12:19:17 -0800153 if (layer->texture.width() == OffscreenBuffer::computeIdealDimension(width)
154 && layer->texture.height() == OffscreenBuffer::computeIdealDimension(height)) {
Chris Craik9fded232015-11-11 16:42:34 -0800155 // resize in place
156 layer->viewportWidth = width;
157 layer->viewportHeight = height;
158 return layer;
159 }
160 putOrDelete(layer);
161 return get(renderState, width, height);
162}
163
164void OffscreenBufferPool::dump() {
165 for (auto entry : mPool) {
166 ALOGD(" Layer size %dx%d", entry.width, entry.height);
167 }
168}
169
170void OffscreenBufferPool::putOrDelete(OffscreenBuffer* layer) {
171 const uint32_t size = layer->getSizeInBytes();
172 // Don't even try to cache a layer that's bigger than the cache
173 if (size < mMaxSize) {
174 // TODO: Use an LRU
175 while (mSize + size > mMaxSize) {
176 OffscreenBuffer* victim = mPool.begin()->layer;
177 mSize -= victim->getSizeInBytes();
178 delete victim;
179 mPool.erase(mPool.begin());
180 }
181
182 // clear region, since it's no longer valid
183 layer->region.clear();
184
185 Entry entry(layer);
186
187 mPool.insert(entry);
188 mSize += size;
189 } else {
190 delete layer;
191 }
192}
193
194}; // namespace uirenderer
195}; // namespace android