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