blob: 987bf03bb863a800b53eec782b80e62b7d1a337d [file] [log] [blame]
Chet Haased15ebf22012-09-05 11:40:29 -07001/*
2 * Copyright (C) 2012 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#define LOG_TAG "OpenGLRenderer"
18
19#include <utils/Log.h>
20
Romain Guy96885eb2013-03-26 15:05:58 -070021#include "DisplayList.h"
22#include "DeferredDisplayList.h"
Chet Haased15ebf22012-09-05 11:40:29 -070023#include "Layer.h"
Chet Haase98d3a642012-09-26 10:27:40 -070024#include "LayerRenderer.h"
Chet Haased15ebf22012-09-05 11:40:29 -070025#include "OpenGLRenderer.h"
26#include "Caches.h"
27
28namespace android {
29namespace uirenderer {
30
Romain Guy8aa195d2013-06-04 18:00:09 -070031Layer::Layer(const uint32_t layerWidth, const uint32_t layerHeight):
32 caches(Caches::getInstance()), texture(caches) {
Chet Haase603f6de2012-09-14 15:31:25 -070033 mesh = NULL;
Chet Haase603f6de2012-09-14 15:31:25 -070034 meshElementCount = 0;
35 cacheable = true;
Romain Guy7c25aab2012-10-18 15:05:02 -070036 dirty = false;
Chet Haase603f6de2012-09-14 15:31:25 -070037 textureLayer = false;
38 renderTarget = GL_TEXTURE_2D;
39 texture.width = layerWidth;
40 texture.height = layerHeight;
41 colorFilter = NULL;
42 deferredUpdateScheduled = false;
43 renderer = NULL;
44 displayList = NULL;
45 fbo = 0;
Romain Guy3bbacf22013-02-06 16:51:04 -080046 stencil = NULL;
Romain Guy5bb3c732012-11-29 17:52:58 -080047 debugDrawUpdate = false;
Chris Craik34416ea2013-04-15 16:08:28 -070048 hasDrawnSinceUpdate = false;
Romain Guy96885eb2013-03-26 15:05:58 -070049 deferredList = NULL;
Romain Guy8aa195d2013-06-04 18:00:09 -070050 caches.resourceCache.incrementRefcount(this);
Chet Haase603f6de2012-09-14 15:31:25 -070051}
52
Chet Haased15ebf22012-09-05 11:40:29 -070053Layer::~Layer() {
Romain Guy8aa195d2013-06-04 18:00:09 -070054 if (colorFilter) caches.resourceCache.decrementRefcount(colorFilter);
Chet Haase98d3a642012-09-26 10:27:40 -070055 removeFbo();
Romain Guy8a137492012-09-25 15:49:03 -070056 deleteTexture();
Romain Guy96885eb2013-03-26 15:05:58 -070057
58 delete[] mesh;
Romain Guy96885eb2013-03-26 15:05:58 -070059 delete deferredList;
Romain Guy97dc9172012-09-23 17:46:45 -070060}
61
Romain Guy2055aba2013-01-18 16:42:51 -080062uint32_t Layer::computeIdealWidth(uint32_t layerWidth) {
63 return uint32_t(ceilf(layerWidth / float(LAYER_SIZE)) * LAYER_SIZE);
64}
65
66uint32_t Layer::computeIdealHeight(uint32_t layerHeight) {
67 return uint32_t(ceilf(layerHeight / float(LAYER_SIZE)) * LAYER_SIZE);
68}
69
70bool Layer::resize(const uint32_t width, const uint32_t height) {
71 uint32_t desiredWidth = computeIdealWidth(width);
72 uint32_t desiredHeight = computeIdealWidth(height);
73
74 if (desiredWidth <= getWidth() && desiredHeight <= getHeight()) {
75 return true;
76 }
77
Romain Guy8aa195d2013-06-04 18:00:09 -070078 const uint32_t maxTextureSize = caches.maxTextureSize;
Romain Guyce4a7df2013-03-28 11:32:33 -070079 if (desiredWidth > maxTextureSize || desiredHeight > maxTextureSize) {
80 ALOGW("Layer exceeds max. dimensions supported by the GPU (%dx%d, max=%dx%d)",
81 desiredWidth, desiredHeight, maxTextureSize, maxTextureSize);
82 return false;
83 }
84
Romain Guy2055aba2013-01-18 16:42:51 -080085 uint32_t oldWidth = getWidth();
86 uint32_t oldHeight = getHeight();
87
88 setSize(desiredWidth, desiredHeight);
89
90 if (fbo) {
Romain Guy8aa195d2013-06-04 18:00:09 -070091 caches.activeTexture(0);
Romain Guy2055aba2013-01-18 16:42:51 -080092 bindTexture();
Romain Guy09087642013-04-04 12:27:54 -070093 allocateTexture();
Romain Guy2055aba2013-01-18 16:42:51 -080094
95 if (glGetError() != GL_NO_ERROR) {
96 setSize(oldWidth, oldHeight);
97 return false;
98 }
99 }
100
101 if (stencil) {
Romain Guy3bbacf22013-02-06 16:51:04 -0800102 stencil->bind();
103 stencil->resize(desiredWidth, desiredHeight);
Romain Guy2055aba2013-01-18 16:42:51 -0800104
105 if (glGetError() != GL_NO_ERROR) {
106 setSize(oldWidth, oldHeight);
107 return false;
108 }
109 }
110
111 return true;
112}
113
Romain Guy8ce00302013-01-15 18:51:42 -0800114void Layer::removeFbo(bool flush) {
115 if (stencil) {
Romain Guy8ce00302013-01-15 18:51:42 -0800116 GLuint previousFbo;
117 glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &previousFbo);
118 if (fbo != previousFbo) glBindFramebuffer(GL_FRAMEBUFFER, fbo);
119 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, 0);
120 if (fbo != previousFbo) glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
121
Romain Guy8aa195d2013-06-04 18:00:09 -0700122 caches.renderBufferCache.put(stencil);
Romain Guy3bbacf22013-02-06 16:51:04 -0800123 stencil = NULL;
Romain Guy8ce00302013-01-15 18:51:42 -0800124 }
125
Chet Haase98d3a642012-09-26 10:27:40 -0700126 if (fbo) {
Romain Guy8ce00302013-01-15 18:51:42 -0800127 if (flush) LayerRenderer::flushLayer(this);
128 // If put fails the cache will delete the FBO
Romain Guy8aa195d2013-06-04 18:00:09 -0700129 caches.fboCache.put(fbo);
Chet Haase98d3a642012-09-26 10:27:40 -0700130 fbo = 0;
Dave Burke56257af2012-09-25 20:30:09 -0700131 }
132}
133
Chet Haased15ebf22012-09-05 11:40:29 -0700134void Layer::setPaint(SkPaint* paint) {
135 OpenGLRenderer::getAlphaAndModeDirect(paint, &alpha, &mode);
136}
137
138void Layer::setColorFilter(SkiaColorFilter* filter) {
139 if (colorFilter) {
Romain Guy8aa195d2013-06-04 18:00:09 -0700140 caches.resourceCache.decrementRefcount(colorFilter);
Chet Haased15ebf22012-09-05 11:40:29 -0700141 }
142 colorFilter = filter;
143 if (colorFilter) {
Romain Guy8aa195d2013-06-04 18:00:09 -0700144 caches.resourceCache.incrementRefcount(colorFilter);
145 }
146}
147
148void Layer::bindTexture() const {
149 if (texture.id) {
150 caches.bindTexture(renderTarget, texture.id);
151 }
152}
153
154void Layer::bindStencilRenderBuffer() const {
155 if (stencil) {
156 stencil->bind();
157 }
158}
159
160void Layer::generateTexture() {
161 if (!texture.id) {
162 glGenTextures(1, &texture.id);
163 }
164}
165
166void Layer::deleteTexture() {
167 if (texture.id) {
Romain Guybe1b1272013-06-06 14:02:54 -0700168 texture.deleteTexture();
Romain Guy8aa195d2013-06-04 18:00:09 -0700169 texture.id = 0;
170 }
171}
172
173void Layer::clearTexture() {
jiayuanr4a473c7d2014-06-10 17:41:49 +0800174 caches.unbindTexture(texture.id);
Romain Guy8aa195d2013-06-04 18:00:09 -0700175 texture.id = 0;
176}
177
178void Layer::allocateTexture() {
179#if DEBUG_LAYERS
180 ALOGD(" Allocate layer: %dx%d", getWidth(), getHeight());
181#endif
182 if (texture.id) {
183 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
184 glTexImage2D(renderTarget, 0, GL_RGBA, getWidth(), getHeight(), 0,
185 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
Chet Haased15ebf22012-09-05 11:40:29 -0700186 }
187}
188
Romain Guy96885eb2013-03-26 15:05:58 -0700189void Layer::defer() {
Romain Guy96885eb2013-03-26 15:05:58 -0700190 const float width = layer.getWidth();
191 const float height = layer.getHeight();
192
193 if (dirtyRect.isEmpty() || (dirtyRect.left <= 0 && dirtyRect.top <= 0 &&
194 dirtyRect.right >= width && dirtyRect.bottom >= height)) {
195 dirtyRect.set(0, 0, width, height);
196 }
197
Chris Craik28ce94a2013-05-31 11:38:03 -0700198 if (deferredList) {
199 deferredList->reset(dirtyRect);
200 } else {
201 deferredList = new DeferredDisplayList(dirtyRect);
202 }
203 DeferStateStruct deferredState(*deferredList, *renderer,
204 DisplayList::kReplayFlag_ClipChildren);
205
Romain Guy96885eb2013-03-26 15:05:58 -0700206 renderer->initViewport(width, height);
207 renderer->setupFrameState(dirtyRect.left, dirtyRect.top,
208 dirtyRect.right, dirtyRect.bottom, !isBlend());
209
210 displayList->defer(deferredState, 0);
Romain Guy02b49b72013-03-29 12:37:16 -0700211
212 deferredUpdateScheduled = false;
Romain Guy96885eb2013-03-26 15:05:58 -0700213}
214
Romain Guye93482f2013-06-17 13:14:51 -0700215void Layer::cancelDefer() {
216 renderer = NULL;
217 displayList = NULL;
218 deferredUpdateScheduled = false;
219 if (deferredList) {
220 delete deferredList;
221 deferredList = NULL;
222 }
223}
224
Romain Guy96885eb2013-03-26 15:05:58 -0700225void Layer::flush() {
Chris Craik8c6e17c2013-06-17 13:02:12 -0700226 // renderer is checked as layer may be destroyed/put in layer cache with flush scheduled
227 if (deferredList && renderer) {
Romain Guy96885eb2013-03-26 15:05:58 -0700228 renderer->setViewport(layer.getWidth(), layer.getHeight());
229 renderer->prepareDirty(dirtyRect.left, dirtyRect.top, dirtyRect.right, dirtyRect.bottom,
230 !isBlend());
231
232 deferredList->flush(*renderer, dirtyRect);
233
234 renderer->finish();
235 renderer = NULL;
236
237 dirtyRect.setEmpty();
Chris Craik1206b9b2013-04-04 14:46:24 -0700238 displayList = NULL;
Romain Guy96885eb2013-03-26 15:05:58 -0700239 }
240}
241
Romain Guy02b49b72013-03-29 12:37:16 -0700242void Layer::render() {
243 renderer->setViewport(layer.getWidth(), layer.getHeight());
244 renderer->prepareDirty(dirtyRect.left, dirtyRect.top, dirtyRect.right, dirtyRect.bottom,
245 !isBlend());
246
247 renderer->drawDisplayList(displayList, dirtyRect, DisplayList::kReplayFlag_ClipChildren);
248
249 renderer->finish();
250 renderer = NULL;
251
252 dirtyRect.setEmpty();
253
254 deferredUpdateScheduled = false;
255 displayList = NULL;
256}
257
Chet Haased15ebf22012-09-05 11:40:29 -0700258}; // namespace uirenderer
259}; // namespace android