blob: 4457c61bd736bc348164c63864e1a63cef8d065a [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
John Reck113e0822014-03-18 09:22:59 -070021#include "Caches.h"
Romain Guy96885eb2013-03-26 15:05:58 -070022#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"
John Reck113e0822014-03-18 09:22:59 -070026#include "RenderNode.h"
Chet Haased15ebf22012-09-05 11:40:29 -070027
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;
Chris Craik9757ac02014-02-25 18:50:17 -080049 forceFilter = false;
Romain Guy96885eb2013-03-26 15:05:58 -070050 deferredList = NULL;
Romain Guy8aa195d2013-06-04 18:00:09 -070051 caches.resourceCache.incrementRefcount(this);
Chet Haase603f6de2012-09-14 15:31:25 -070052}
53
Chet Haased15ebf22012-09-05 11:40:29 -070054Layer::~Layer() {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -050055 SkSafeUnref(colorFilter);
Chet Haase98d3a642012-09-26 10:27:40 -070056 removeFbo();
Romain Guy8a137492012-09-25 15:49:03 -070057 deleteTexture();
Romain Guy96885eb2013-03-26 15:05:58 -070058
59 delete[] mesh;
Romain Guy96885eb2013-03-26 15:05:58 -070060 delete deferredList;
John Reck668f0e32014-03-26 15:10:40 -070061 delete renderer;
Romain Guy97dc9172012-09-23 17:46:45 -070062}
63
Romain Guy2055aba2013-01-18 16:42:51 -080064uint32_t Layer::computeIdealWidth(uint32_t layerWidth) {
65 return uint32_t(ceilf(layerWidth / float(LAYER_SIZE)) * LAYER_SIZE);
66}
67
68uint32_t Layer::computeIdealHeight(uint32_t layerHeight) {
69 return uint32_t(ceilf(layerHeight / float(LAYER_SIZE)) * LAYER_SIZE);
70}
71
John Reck668f0e32014-03-26 15:10:40 -070072void Layer::requireRenderer() {
73 if (!renderer) {
74 renderer = new LayerRenderer(this);
75 renderer->initProperties();
76 }
77}
78
Romain Guy2055aba2013-01-18 16:42:51 -080079bool Layer::resize(const uint32_t width, const uint32_t height) {
80 uint32_t desiredWidth = computeIdealWidth(width);
81 uint32_t desiredHeight = computeIdealWidth(height);
82
83 if (desiredWidth <= getWidth() && desiredHeight <= getHeight()) {
84 return true;
85 }
86
Romain Guy8aa195d2013-06-04 18:00:09 -070087 const uint32_t maxTextureSize = caches.maxTextureSize;
Romain Guyce4a7df2013-03-28 11:32:33 -070088 if (desiredWidth > maxTextureSize || desiredHeight > maxTextureSize) {
89 ALOGW("Layer exceeds max. dimensions supported by the GPU (%dx%d, max=%dx%d)",
90 desiredWidth, desiredHeight, maxTextureSize, maxTextureSize);
91 return false;
92 }
93
Romain Guy2055aba2013-01-18 16:42:51 -080094 uint32_t oldWidth = getWidth();
95 uint32_t oldHeight = getHeight();
96
97 setSize(desiredWidth, desiredHeight);
98
99 if (fbo) {
Romain Guy8aa195d2013-06-04 18:00:09 -0700100 caches.activeTexture(0);
Romain Guy2055aba2013-01-18 16:42:51 -0800101 bindTexture();
Romain Guy09087642013-04-04 12:27:54 -0700102 allocateTexture();
Romain Guy2055aba2013-01-18 16:42:51 -0800103
104 if (glGetError() != GL_NO_ERROR) {
105 setSize(oldWidth, oldHeight);
106 return false;
107 }
108 }
109
110 if (stencil) {
Romain Guy3bbacf22013-02-06 16:51:04 -0800111 stencil->bind();
112 stencil->resize(desiredWidth, desiredHeight);
Romain Guy2055aba2013-01-18 16:42:51 -0800113
114 if (glGetError() != GL_NO_ERROR) {
115 setSize(oldWidth, oldHeight);
116 return false;
117 }
118 }
119
120 return true;
121}
122
Romain Guy8ce00302013-01-15 18:51:42 -0800123void Layer::removeFbo(bool flush) {
124 if (stencil) {
Romain Guy8ce00302013-01-15 18:51:42 -0800125 GLuint previousFbo;
126 glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &previousFbo);
127 if (fbo != previousFbo) glBindFramebuffer(GL_FRAMEBUFFER, fbo);
128 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, 0);
129 if (fbo != previousFbo) glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
130
Romain Guy8aa195d2013-06-04 18:00:09 -0700131 caches.renderBufferCache.put(stencil);
Romain Guy3bbacf22013-02-06 16:51:04 -0800132 stencil = NULL;
Romain Guy8ce00302013-01-15 18:51:42 -0800133 }
134
Chet Haase98d3a642012-09-26 10:27:40 -0700135 if (fbo) {
Romain Guy8ce00302013-01-15 18:51:42 -0800136 if (flush) LayerRenderer::flushLayer(this);
137 // If put fails the cache will delete the FBO
Romain Guy8aa195d2013-06-04 18:00:09 -0700138 caches.fboCache.put(fbo);
Chet Haase98d3a642012-09-26 10:27:40 -0700139 fbo = 0;
Dave Burke56257af2012-09-25 20:30:09 -0700140 }
141}
142
Derek Sollenberger674554f2014-02-19 16:47:32 +0000143void Layer::setPaint(const SkPaint* paint) {
Chet Haased15ebf22012-09-05 11:40:29 -0700144 OpenGLRenderer::getAlphaAndModeDirect(paint, &alpha, &mode);
Derek Sollenberger674554f2014-02-19 16:47:32 +0000145 setColorFilter((paint) ? paint->getColorFilter() : NULL);
Chet Haased15ebf22012-09-05 11:40:29 -0700146}
147
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500148void Layer::setColorFilter(SkColorFilter* filter) {
149 SkRefCnt_SafeAssign(colorFilter, filter);
Romain Guy8aa195d2013-06-04 18:00:09 -0700150}
151
152void Layer::bindTexture() const {
153 if (texture.id) {
154 caches.bindTexture(renderTarget, texture.id);
155 }
156}
157
158void Layer::bindStencilRenderBuffer() const {
159 if (stencil) {
160 stencil->bind();
161 }
162}
163
164void Layer::generateTexture() {
165 if (!texture.id) {
166 glGenTextures(1, &texture.id);
167 }
168}
169
170void Layer::deleteTexture() {
171 if (texture.id) {
Romain Guybe1b1272013-06-06 14:02:54 -0700172 texture.deleteTexture();
Romain Guy8aa195d2013-06-04 18:00:09 -0700173 texture.id = 0;
174 }
175}
176
177void Layer::clearTexture() {
178 texture.id = 0;
179}
180
181void Layer::allocateTexture() {
182#if DEBUG_LAYERS
183 ALOGD(" Allocate layer: %dx%d", getWidth(), getHeight());
184#endif
185 if (texture.id) {
186 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
187 glTexImage2D(renderTarget, 0, GL_RGBA, getWidth(), getHeight(), 0,
188 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
Chet Haased15ebf22012-09-05 11:40:29 -0700189 }
190}
191
Romain Guy96885eb2013-03-26 15:05:58 -0700192void Layer::defer() {
Romain Guy96885eb2013-03-26 15:05:58 -0700193 const float width = layer.getWidth();
194 const float height = layer.getHeight();
195
196 if (dirtyRect.isEmpty() || (dirtyRect.left <= 0 && dirtyRect.top <= 0 &&
197 dirtyRect.right >= width && dirtyRect.bottom >= height)) {
198 dirtyRect.set(0, 0, width, height);
199 }
200
Chris Craikf57776b2013-10-25 18:30:17 -0700201 delete deferredList;
202 deferredList = new DeferredDisplayList(dirtyRect);
203
Chris Craik28ce94a2013-05-31 11:38:03 -0700204 DeferStateStruct deferredState(*deferredList, *renderer,
John Recke18264b2014-03-12 13:56:30 -0700205 RenderNode::kReplayFlag_ClipChildren);
Chris Craik28ce94a2013-05-31 11:38:03 -0700206
Romain Guy96885eb2013-03-26 15:05:58 -0700207 renderer->initViewport(width, height);
208 renderer->setupFrameState(dirtyRect.left, dirtyRect.top,
209 dirtyRect.right, dirtyRect.bottom, !isBlend());
210
Chris Craikf57776b2013-10-25 18:30:17 -0700211 displayList->computeOrdering();
Romain Guy96885eb2013-03-26 15:05:58 -0700212 displayList->defer(deferredState, 0);
Romain Guy02b49b72013-03-29 12:37:16 -0700213
214 deferredUpdateScheduled = false;
Romain Guy96885eb2013-03-26 15:05:58 -0700215}
216
Romain Guye93482f2013-06-17 13:14:51 -0700217void Layer::cancelDefer() {
Romain Guye93482f2013-06-17 13:14:51 -0700218 displayList = NULL;
219 deferredUpdateScheduled = false;
220 if (deferredList) {
221 delete deferredList;
222 deferredList = NULL;
223 }
224}
225
Romain Guy96885eb2013-03-26 15:05:58 -0700226void Layer::flush() {
Chris Craik8c6e17c2013-06-17 13:02:12 -0700227 // renderer is checked as layer may be destroyed/put in layer cache with flush scheduled
228 if (deferredList && renderer) {
Romain Guy96885eb2013-03-26 15:05:58 -0700229 renderer->setViewport(layer.getWidth(), layer.getHeight());
230 renderer->prepareDirty(dirtyRect.left, dirtyRect.top, dirtyRect.right, dirtyRect.bottom,
231 !isBlend());
232
233 deferredList->flush(*renderer, dirtyRect);
234
235 renderer->finish();
Romain Guy96885eb2013-03-26 15:05:58 -0700236
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
John Recke18264b2014-03-12 13:56:30 -0700247 renderer->drawDisplayList(displayList, dirtyRect, RenderNode::kReplayFlag_ClipChildren);
Romain Guy02b49b72013-03-29 12:37:16 -0700248
249 renderer->finish();
Romain Guy02b49b72013-03-29 12:37:16 -0700250
251 dirtyRect.setEmpty();
252
253 deferredUpdateScheduled = false;
254 displayList = NULL;
255}
256
Chet Haased15ebf22012-09-05 11:40:29 -0700257}; // namespace uirenderer
258}; // namespace android