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