blob: 73a4da5086d5868df13f880c114d3b9337a6cad2 [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"
John Reck3b202512014-06-23 13:13:08 -070023#include "RenderState.h"
Chet Haased15ebf22012-09-05 11:40:29 -070024#include "Layer.h"
Chet Haase98d3a642012-09-26 10:27:40 -070025#include "LayerRenderer.h"
Chet Haased15ebf22012-09-05 11:40:29 -070026#include "OpenGLRenderer.h"
John Reck113e0822014-03-18 09:22:59 -070027#include "RenderNode.h"
Chet Haased15ebf22012-09-05 11:40:29 -070028
29namespace android {
30namespace uirenderer {
31
John Reck3b202512014-06-23 13:13:08 -070032Layer::Layer(RenderState& renderState, const uint32_t layerWidth, const uint32_t layerHeight)
33 : caches(Caches::getInstance())
34 , renderState(renderState)
35 , texture(caches) {
Chet Haase603f6de2012-09-14 15:31:25 -070036 mesh = NULL;
Chet Haase603f6de2012-09-14 15:31:25 -070037 meshElementCount = 0;
38 cacheable = true;
Romain Guy7c25aab2012-10-18 15:05:02 -070039 dirty = false;
Chet Haase603f6de2012-09-14 15:31:25 -070040 textureLayer = false;
41 renderTarget = GL_TEXTURE_2D;
42 texture.width = layerWidth;
43 texture.height = layerHeight;
44 colorFilter = NULL;
45 deferredUpdateScheduled = false;
46 renderer = NULL;
Chris Craika7090e02014-06-20 16:01:00 -070047 renderNode = NULL;
Chet Haase603f6de2012-09-14 15:31:25 -070048 fbo = 0;
Romain Guy3bbacf22013-02-06 16:51:04 -080049 stencil = NULL;
Romain Guy5bb3c732012-11-29 17:52:58 -080050 debugDrawUpdate = false;
Chris Craik34416ea2013-04-15 16:08:28 -070051 hasDrawnSinceUpdate = false;
Chris Craik9757ac02014-02-25 18:50:17 -080052 forceFilter = false;
Romain Guy96885eb2013-03-26 15:05:58 -070053 deferredList = NULL;
Chris Craik3f0854292014-04-15 16:18:08 -070054 convexMask = NULL;
Romain Guy8aa195d2013-06-04 18:00:09 -070055 caches.resourceCache.incrementRefcount(this);
Chet Haase603f6de2012-09-14 15:31:25 -070056}
57
Chet Haased15ebf22012-09-05 11:40:29 -070058Layer::~Layer() {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -050059 SkSafeUnref(colorFilter);
Chet Haase98d3a642012-09-26 10:27:40 -070060 removeFbo();
Romain Guy8a137492012-09-25 15:49:03 -070061 deleteTexture();
Romain Guy96885eb2013-03-26 15:05:58 -070062
63 delete[] mesh;
Romain Guy96885eb2013-03-26 15:05:58 -070064 delete deferredList;
John Reck668f0e32014-03-26 15:10:40 -070065 delete renderer;
Romain Guy97dc9172012-09-23 17:46:45 -070066}
67
Romain Guy2055aba2013-01-18 16:42:51 -080068uint32_t Layer::computeIdealWidth(uint32_t layerWidth) {
69 return uint32_t(ceilf(layerWidth / float(LAYER_SIZE)) * LAYER_SIZE);
70}
71
72uint32_t Layer::computeIdealHeight(uint32_t layerHeight) {
73 return uint32_t(ceilf(layerHeight / float(LAYER_SIZE)) * LAYER_SIZE);
74}
75
John Reck668f0e32014-03-26 15:10:40 -070076void Layer::requireRenderer() {
77 if (!renderer) {
John Reck3b202512014-06-23 13:13:08 -070078 renderer = new LayerRenderer(renderState, this);
John Reck668f0e32014-03-26 15:10:40 -070079 renderer->initProperties();
80 }
81}
82
Romain Guy2055aba2013-01-18 16:42:51 -080083bool Layer::resize(const uint32_t width, const uint32_t height) {
84 uint32_t desiredWidth = computeIdealWidth(width);
85 uint32_t desiredHeight = computeIdealWidth(height);
86
87 if (desiredWidth <= getWidth() && desiredHeight <= getHeight()) {
88 return true;
89 }
90
Romain Guy8aa195d2013-06-04 18:00:09 -070091 const uint32_t maxTextureSize = caches.maxTextureSize;
Romain Guyce4a7df2013-03-28 11:32:33 -070092 if (desiredWidth > maxTextureSize || desiredHeight > maxTextureSize) {
93 ALOGW("Layer exceeds max. dimensions supported by the GPU (%dx%d, max=%dx%d)",
94 desiredWidth, desiredHeight, maxTextureSize, maxTextureSize);
95 return false;
96 }
97
Romain Guy2055aba2013-01-18 16:42:51 -080098 uint32_t oldWidth = getWidth();
99 uint32_t oldHeight = getHeight();
100
101 setSize(desiredWidth, desiredHeight);
102
103 if (fbo) {
Romain Guy8aa195d2013-06-04 18:00:09 -0700104 caches.activeTexture(0);
Romain Guy2055aba2013-01-18 16:42:51 -0800105 bindTexture();
Romain Guy09087642013-04-04 12:27:54 -0700106 allocateTexture();
Romain Guy2055aba2013-01-18 16:42:51 -0800107
108 if (glGetError() != GL_NO_ERROR) {
109 setSize(oldWidth, oldHeight);
110 return false;
111 }
112 }
113
114 if (stencil) {
Romain Guy3bbacf22013-02-06 16:51:04 -0800115 stencil->bind();
116 stencil->resize(desiredWidth, desiredHeight);
Romain Guy2055aba2013-01-18 16:42:51 -0800117
118 if (glGetError() != GL_NO_ERROR) {
119 setSize(oldWidth, oldHeight);
120 return false;
121 }
122 }
123
124 return true;
125}
126
Romain Guy8ce00302013-01-15 18:51:42 -0800127void Layer::removeFbo(bool flush) {
128 if (stencil) {
John Reck3b202512014-06-23 13:13:08 -0700129 GLuint previousFbo = renderState.getFramebuffer();
130 renderState.bindFramebuffer(fbo);
Romain Guy8ce00302013-01-15 18:51:42 -0800131 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, 0);
John Reck3b202512014-06-23 13:13:08 -0700132 renderState.bindFramebuffer(previousFbo);
Romain Guy8ce00302013-01-15 18:51:42 -0800133
Romain Guy8aa195d2013-06-04 18:00:09 -0700134 caches.renderBufferCache.put(stencil);
Romain Guy3bbacf22013-02-06 16:51:04 -0800135 stencil = NULL;
Romain Guy8ce00302013-01-15 18:51:42 -0800136 }
137
Chet Haase98d3a642012-09-26 10:27:40 -0700138 if (fbo) {
John Reck3b202512014-06-23 13:13:08 -0700139 if (flush) LayerRenderer::flushLayer(renderState, this);
Romain Guy8ce00302013-01-15 18:51:42 -0800140 // If put fails the cache will delete the FBO
Romain Guy8aa195d2013-06-04 18:00:09 -0700141 caches.fboCache.put(fbo);
Chet Haase98d3a642012-09-26 10:27:40 -0700142 fbo = 0;
Dave Burke56257af2012-09-25 20:30:09 -0700143 }
144}
145
Chris Craika7090e02014-06-20 16:01:00 -0700146void Layer::updateDeferred(RenderNode* renderNode, int left, int top, int right, int bottom) {
John Reck087bc0c2014-04-04 16:20:08 -0700147 requireRenderer();
Chris Craika7090e02014-06-20 16:01:00 -0700148 this->renderNode = renderNode;
John Reck087bc0c2014-04-04 16:20:08 -0700149 const Rect r(left, top, right, bottom);
150 dirtyRect.unionWith(r);
151 deferredUpdateScheduled = true;
152}
153
Derek Sollenberger674554f2014-02-19 16:47:32 +0000154void Layer::setPaint(const SkPaint* paint) {
Chet Haased15ebf22012-09-05 11:40:29 -0700155 OpenGLRenderer::getAlphaAndModeDirect(paint, &alpha, &mode);
Derek Sollenberger674554f2014-02-19 16:47:32 +0000156 setColorFilter((paint) ? paint->getColorFilter() : NULL);
Chet Haased15ebf22012-09-05 11:40:29 -0700157}
158
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500159void Layer::setColorFilter(SkColorFilter* filter) {
160 SkRefCnt_SafeAssign(colorFilter, filter);
Romain Guy8aa195d2013-06-04 18:00:09 -0700161}
162
163void Layer::bindTexture() const {
164 if (texture.id) {
165 caches.bindTexture(renderTarget, texture.id);
166 }
167}
168
169void Layer::bindStencilRenderBuffer() const {
170 if (stencil) {
171 stencil->bind();
172 }
173}
174
175void Layer::generateTexture() {
176 if (!texture.id) {
177 glGenTextures(1, &texture.id);
178 }
179}
180
181void Layer::deleteTexture() {
182 if (texture.id) {
Romain Guybe1b1272013-06-06 14:02:54 -0700183 texture.deleteTexture();
Romain Guy8aa195d2013-06-04 18:00:09 -0700184 texture.id = 0;
185 }
186}
187
188void Layer::clearTexture() {
jiayuanr4a473c7d2014-06-10 17:41:49 +0800189 caches.unbindTexture(texture.id);
Romain Guy8aa195d2013-06-04 18:00:09 -0700190 texture.id = 0;
191}
192
193void Layer::allocateTexture() {
194#if DEBUG_LAYERS
195 ALOGD(" Allocate layer: %dx%d", getWidth(), getHeight());
196#endif
197 if (texture.id) {
198 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
199 glTexImage2D(renderTarget, 0, GL_RGBA, getWidth(), getHeight(), 0,
200 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
Chet Haased15ebf22012-09-05 11:40:29 -0700201 }
202}
203
Romain Guy96885eb2013-03-26 15:05:58 -0700204void Layer::defer() {
Romain Guy96885eb2013-03-26 15:05:58 -0700205 const float width = layer.getWidth();
206 const float height = layer.getHeight();
207
208 if (dirtyRect.isEmpty() || (dirtyRect.left <= 0 && dirtyRect.top <= 0 &&
209 dirtyRect.right >= width && dirtyRect.bottom >= height)) {
210 dirtyRect.set(0, 0, width, height);
211 }
212
Chris Craikf57776b2013-10-25 18:30:17 -0700213 delete deferredList;
214 deferredList = new DeferredDisplayList(dirtyRect);
215
Chris Craik28ce94a2013-05-31 11:38:03 -0700216 DeferStateStruct deferredState(*deferredList, *renderer,
John Recke18264b2014-03-12 13:56:30 -0700217 RenderNode::kReplayFlag_ClipChildren);
Chris Craik28ce94a2013-05-31 11:38:03 -0700218
Chris Craik797b95b2014-05-20 18:10:25 -0700219 renderer->setViewport(width, height);
Romain Guy96885eb2013-03-26 15:05:58 -0700220 renderer->setupFrameState(dirtyRect.left, dirtyRect.top,
221 dirtyRect.right, dirtyRect.bottom, !isBlend());
222
Chris Craika7090e02014-06-20 16:01:00 -0700223 renderNode->computeOrdering();
224 renderNode->defer(deferredState, 0);
Romain Guy02b49b72013-03-29 12:37:16 -0700225
226 deferredUpdateScheduled = false;
Romain Guy96885eb2013-03-26 15:05:58 -0700227}
228
Romain Guye93482f2013-06-17 13:14:51 -0700229void Layer::cancelDefer() {
Chris Craika7090e02014-06-20 16:01:00 -0700230 renderNode = NULL;
Romain Guye93482f2013-06-17 13:14:51 -0700231 deferredUpdateScheduled = false;
232 if (deferredList) {
233 delete deferredList;
234 deferredList = NULL;
235 }
236}
237
Romain Guy96885eb2013-03-26 15:05:58 -0700238void Layer::flush() {
Chris Craik8c6e17c2013-06-17 13:02:12 -0700239 // renderer is checked as layer may be destroyed/put in layer cache with flush scheduled
240 if (deferredList && renderer) {
Romain Guy96885eb2013-03-26 15:05:58 -0700241 renderer->setViewport(layer.getWidth(), layer.getHeight());
242 renderer->prepareDirty(dirtyRect.left, dirtyRect.top, dirtyRect.right, dirtyRect.bottom,
243 !isBlend());
244
245 deferredList->flush(*renderer, dirtyRect);
246
247 renderer->finish();
Romain Guy96885eb2013-03-26 15:05:58 -0700248
249 dirtyRect.setEmpty();
Chris Craika7090e02014-06-20 16:01:00 -0700250 renderNode = NULL;
Romain Guy96885eb2013-03-26 15:05:58 -0700251 }
252}
253
Romain Guy02b49b72013-03-29 12:37:16 -0700254void Layer::render() {
255 renderer->setViewport(layer.getWidth(), layer.getHeight());
256 renderer->prepareDirty(dirtyRect.left, dirtyRect.top, dirtyRect.right, dirtyRect.bottom,
257 !isBlend());
258
Chris Craika7090e02014-06-20 16:01:00 -0700259 renderer->drawRenderNode(renderNode.get(), dirtyRect, RenderNode::kReplayFlag_ClipChildren);
Romain Guy02b49b72013-03-29 12:37:16 -0700260
261 renderer->finish();
Romain Guy02b49b72013-03-29 12:37:16 -0700262
263 dirtyRect.setEmpty();
264
265 deferredUpdateScheduled = false;
Chris Craika7090e02014-06-20 16:01:00 -0700266 renderNode = NULL;
Romain Guy02b49b72013-03-29 12:37:16 -0700267}
268
Chet Haased15ebf22012-09-05 11:40:29 -0700269}; // namespace uirenderer
270}; // namespace android