blob: b5089aa79ff3d536f0e1ff37c8deec0a1ea4a525 [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
Chris Craik8a226d22014-09-08 16:40:21 -070032Layer::Layer(Type layerType, RenderState& renderState, const uint32_t layerWidth, const uint32_t layerHeight)
Chris Craikbfd1cd62014-09-10 13:04:31 -070033 : state(kState_Uncached)
34 , caches(Caches::getInstance())
John Reck3b202512014-06-23 13:13:08 -070035 , renderState(renderState)
Chris Craik8a226d22014-09-08 16:40:21 -070036 , texture(caches)
37 , type(layerType) {
Chet Haase603f6de2012-09-14 15:31:25 -070038 mesh = NULL;
Chet Haase603f6de2012-09-14 15:31:25 -070039 meshElementCount = 0;
40 cacheable = true;
Romain Guy7c25aab2012-10-18 15:05:02 -070041 dirty = false;
Chet Haase603f6de2012-09-14 15:31:25 -070042 renderTarget = GL_TEXTURE_2D;
43 texture.width = layerWidth;
44 texture.height = layerHeight;
45 colorFilter = NULL;
46 deferredUpdateScheduled = false;
47 renderer = NULL;
Chris Craika7090e02014-06-20 16:01:00 -070048 renderNode = NULL;
Chet Haase603f6de2012-09-14 15:31:25 -070049 fbo = 0;
Romain Guy3bbacf22013-02-06 16:51:04 -080050 stencil = NULL;
Romain Guy5bb3c732012-11-29 17:52:58 -080051 debugDrawUpdate = false;
Chris Craik34416ea2013-04-15 16:08:28 -070052 hasDrawnSinceUpdate = false;
Chris Craik9757ac02014-02-25 18:50:17 -080053 forceFilter = false;
Romain Guy96885eb2013-03-26 15:05:58 -070054 deferredList = NULL;
Chris Craik3f0854292014-04-15 16:18:08 -070055 convexMask = NULL;
Romain Guy8aa195d2013-06-04 18:00:09 -070056 caches.resourceCache.incrementRefcount(this);
Chris Craik69e5adf2014-08-14 13:34:01 -070057 rendererLightPosDirty = true;
John Reck443a7142014-09-04 17:40:05 -070058 wasBuildLayered = false;
Chris Craik8a226d22014-09-08 16:40:21 -070059 if (!isTextureLayer()) {
60 // track only non-texture layer lifecycles in renderstate,
61 // because texture layers are destroyed via finalizer
62 renderState.registerLayer(this);
63 }
Chet Haase603f6de2012-09-14 15:31:25 -070064}
65
Chet Haased15ebf22012-09-05 11:40:29 -070066Layer::~Layer() {
Chris Craik8a226d22014-09-08 16:40:21 -070067 if (!isTextureLayer()) {
68 renderState.unregisterLayer(this);
69 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -050070 SkSafeUnref(colorFilter);
Chet Haase98d3a642012-09-26 10:27:40 -070071 removeFbo();
Romain Guy8a137492012-09-25 15:49:03 -070072 deleteTexture();
Romain Guy96885eb2013-03-26 15:05:58 -070073
74 delete[] mesh;
Romain Guy96885eb2013-03-26 15:05:58 -070075 delete deferredList;
John Reck668f0e32014-03-26 15:10:40 -070076 delete renderer;
Romain Guy97dc9172012-09-23 17:46:45 -070077}
78
Romain Guy2055aba2013-01-18 16:42:51 -080079uint32_t Layer::computeIdealWidth(uint32_t layerWidth) {
80 return uint32_t(ceilf(layerWidth / float(LAYER_SIZE)) * LAYER_SIZE);
81}
82
83uint32_t Layer::computeIdealHeight(uint32_t layerHeight) {
84 return uint32_t(ceilf(layerHeight / float(LAYER_SIZE)) * LAYER_SIZE);
85}
86
John Reck668f0e32014-03-26 15:10:40 -070087void Layer::requireRenderer() {
88 if (!renderer) {
John Reck3b202512014-06-23 13:13:08 -070089 renderer = new LayerRenderer(renderState, this);
John Reck668f0e32014-03-26 15:10:40 -070090 renderer->initProperties();
91 }
92}
93
Chris Craik69e5adf2014-08-14 13:34:01 -070094void Layer::updateLightPosFromRenderer(const OpenGLRenderer& rootRenderer) {
95 if (renderer && rendererLightPosDirty) {
96 // re-init renderer's light position, based upon last cached location in window
97 Vector3 lightPos = rootRenderer.getLightCenter();
98 cachedInvTransformInWindow.mapPoint3d(lightPos);
99 renderer->initLight(lightPos, rootRenderer.getLightRadius(),
100 rootRenderer.getAmbientShadowAlpha(), rootRenderer.getSpotShadowAlpha());
101 rendererLightPosDirty = false;
102 }
103}
104
Romain Guy2055aba2013-01-18 16:42:51 -0800105bool Layer::resize(const uint32_t width, const uint32_t height) {
106 uint32_t desiredWidth = computeIdealWidth(width);
107 uint32_t desiredHeight = computeIdealWidth(height);
108
109 if (desiredWidth <= getWidth() && desiredHeight <= getHeight()) {
110 return true;
111 }
112
John Reckec4cefc2014-07-29 09:49:13 -0700113 ATRACE_NAME("resizeLayer");
114
Romain Guy8aa195d2013-06-04 18:00:09 -0700115 const uint32_t maxTextureSize = caches.maxTextureSize;
Romain Guyce4a7df2013-03-28 11:32:33 -0700116 if (desiredWidth > maxTextureSize || desiredHeight > maxTextureSize) {
117 ALOGW("Layer exceeds max. dimensions supported by the GPU (%dx%d, max=%dx%d)",
118 desiredWidth, desiredHeight, maxTextureSize, maxTextureSize);
119 return false;
120 }
121
Romain Guy2055aba2013-01-18 16:42:51 -0800122 uint32_t oldWidth = getWidth();
123 uint32_t oldHeight = getHeight();
124
125 setSize(desiredWidth, desiredHeight);
126
127 if (fbo) {
Romain Guy8aa195d2013-06-04 18:00:09 -0700128 caches.activeTexture(0);
Romain Guy2055aba2013-01-18 16:42:51 -0800129 bindTexture();
Romain Guy09087642013-04-04 12:27:54 -0700130 allocateTexture();
Romain Guy2055aba2013-01-18 16:42:51 -0800131
132 if (glGetError() != GL_NO_ERROR) {
133 setSize(oldWidth, oldHeight);
134 return false;
135 }
136 }
137
138 if (stencil) {
Romain Guy3bbacf22013-02-06 16:51:04 -0800139 stencil->bind();
140 stencil->resize(desiredWidth, desiredHeight);
Romain Guy2055aba2013-01-18 16:42:51 -0800141
142 if (glGetError() != GL_NO_ERROR) {
143 setSize(oldWidth, oldHeight);
144 return false;
145 }
146 }
147
148 return true;
149}
150
Romain Guy8ce00302013-01-15 18:51:42 -0800151void Layer::removeFbo(bool flush) {
152 if (stencil) {
John Reck3b202512014-06-23 13:13:08 -0700153 GLuint previousFbo = renderState.getFramebuffer();
154 renderState.bindFramebuffer(fbo);
Romain Guy8ce00302013-01-15 18:51:42 -0800155 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, 0);
John Reck3b202512014-06-23 13:13:08 -0700156 renderState.bindFramebuffer(previousFbo);
Romain Guy8ce00302013-01-15 18:51:42 -0800157
Romain Guy8aa195d2013-06-04 18:00:09 -0700158 caches.renderBufferCache.put(stencil);
Romain Guy3bbacf22013-02-06 16:51:04 -0800159 stencil = NULL;
Romain Guy8ce00302013-01-15 18:51:42 -0800160 }
161
Chet Haase98d3a642012-09-26 10:27:40 -0700162 if (fbo) {
John Reck3b202512014-06-23 13:13:08 -0700163 if (flush) LayerRenderer::flushLayer(renderState, this);
Romain Guy8ce00302013-01-15 18:51:42 -0800164 // If put fails the cache will delete the FBO
Romain Guy8aa195d2013-06-04 18:00:09 -0700165 caches.fboCache.put(fbo);
Chet Haase98d3a642012-09-26 10:27:40 -0700166 fbo = 0;
Dave Burke56257af2012-09-25 20:30:09 -0700167 }
168}
169
Chris Craika7090e02014-06-20 16:01:00 -0700170void Layer::updateDeferred(RenderNode* renderNode, int left, int top, int right, int bottom) {
John Reck087bc0c2014-04-04 16:20:08 -0700171 requireRenderer();
Chris Craika7090e02014-06-20 16:01:00 -0700172 this->renderNode = renderNode;
John Reck087bc0c2014-04-04 16:20:08 -0700173 const Rect r(left, top, right, bottom);
174 dirtyRect.unionWith(r);
175 deferredUpdateScheduled = true;
176}
177
Derek Sollenberger674554f2014-02-19 16:47:32 +0000178void Layer::setPaint(const SkPaint* paint) {
Chet Haased15ebf22012-09-05 11:40:29 -0700179 OpenGLRenderer::getAlphaAndModeDirect(paint, &alpha, &mode);
Derek Sollenberger674554f2014-02-19 16:47:32 +0000180 setColorFilter((paint) ? paint->getColorFilter() : NULL);
Chet Haased15ebf22012-09-05 11:40:29 -0700181}
182
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500183void Layer::setColorFilter(SkColorFilter* filter) {
184 SkRefCnt_SafeAssign(colorFilter, filter);
Romain Guy8aa195d2013-06-04 18:00:09 -0700185}
186
187void Layer::bindTexture() const {
188 if (texture.id) {
189 caches.bindTexture(renderTarget, texture.id);
190 }
191}
192
193void Layer::bindStencilRenderBuffer() const {
194 if (stencil) {
195 stencil->bind();
196 }
197}
198
199void Layer::generateTexture() {
200 if (!texture.id) {
201 glGenTextures(1, &texture.id);
202 }
203}
204
205void Layer::deleteTexture() {
206 if (texture.id) {
Romain Guybe1b1272013-06-06 14:02:54 -0700207 texture.deleteTexture();
Romain Guy8aa195d2013-06-04 18:00:09 -0700208 texture.id = 0;
209 }
210}
211
212void Layer::clearTexture() {
jiayuanr4a473c7d2014-06-10 17:41:49 +0800213 caches.unbindTexture(texture.id);
Romain Guy8aa195d2013-06-04 18:00:09 -0700214 texture.id = 0;
215}
216
217void Layer::allocateTexture() {
218#if DEBUG_LAYERS
219 ALOGD(" Allocate layer: %dx%d", getWidth(), getHeight());
220#endif
221 if (texture.id) {
222 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
223 glTexImage2D(renderTarget, 0, GL_RGBA, getWidth(), getHeight(), 0,
224 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
Chet Haased15ebf22012-09-05 11:40:29 -0700225 }
226}
227
Chris Craik69e5adf2014-08-14 13:34:01 -0700228void Layer::defer(const OpenGLRenderer& rootRenderer) {
229 updateLightPosFromRenderer(rootRenderer);
Romain Guy96885eb2013-03-26 15:05:58 -0700230 const float width = layer.getWidth();
231 const float height = layer.getHeight();
232
233 if (dirtyRect.isEmpty() || (dirtyRect.left <= 0 && dirtyRect.top <= 0 &&
234 dirtyRect.right >= width && dirtyRect.bottom >= height)) {
235 dirtyRect.set(0, 0, width, height);
236 }
237
Chris Craikf57776b2013-10-25 18:30:17 -0700238 delete deferredList;
239 deferredList = new DeferredDisplayList(dirtyRect);
240
Chris Craik28ce94a2013-05-31 11:38:03 -0700241 DeferStateStruct deferredState(*deferredList, *renderer,
John Recke18264b2014-03-12 13:56:30 -0700242 RenderNode::kReplayFlag_ClipChildren);
Chris Craik28ce94a2013-05-31 11:38:03 -0700243
Chris Craik797b95b2014-05-20 18:10:25 -0700244 renderer->setViewport(width, height);
Romain Guy96885eb2013-03-26 15:05:58 -0700245 renderer->setupFrameState(dirtyRect.left, dirtyRect.top,
246 dirtyRect.right, dirtyRect.bottom, !isBlend());
247
Chris Craika7090e02014-06-20 16:01:00 -0700248 renderNode->computeOrdering();
249 renderNode->defer(deferredState, 0);
Romain Guy02b49b72013-03-29 12:37:16 -0700250
251 deferredUpdateScheduled = false;
Romain Guy96885eb2013-03-26 15:05:58 -0700252}
253
Romain Guye93482f2013-06-17 13:14:51 -0700254void Layer::cancelDefer() {
Chris Craika7090e02014-06-20 16:01:00 -0700255 renderNode = NULL;
Romain Guye93482f2013-06-17 13:14:51 -0700256 deferredUpdateScheduled = false;
257 if (deferredList) {
258 delete deferredList;
259 deferredList = NULL;
260 }
261}
262
Romain Guy96885eb2013-03-26 15:05:58 -0700263void Layer::flush() {
Chris Craik8c6e17c2013-06-17 13:02:12 -0700264 // renderer is checked as layer may be destroyed/put in layer cache with flush scheduled
265 if (deferredList && renderer) {
Romain Guy96885eb2013-03-26 15:05:58 -0700266 renderer->setViewport(layer.getWidth(), layer.getHeight());
267 renderer->prepareDirty(dirtyRect.left, dirtyRect.top, dirtyRect.right, dirtyRect.bottom,
268 !isBlend());
269
270 deferredList->flush(*renderer, dirtyRect);
271
272 renderer->finish();
Romain Guy96885eb2013-03-26 15:05:58 -0700273
274 dirtyRect.setEmpty();
Chris Craika7090e02014-06-20 16:01:00 -0700275 renderNode = NULL;
Romain Guy96885eb2013-03-26 15:05:58 -0700276 }
277}
278
Chris Craik69e5adf2014-08-14 13:34:01 -0700279void Layer::render(const OpenGLRenderer& rootRenderer) {
280 updateLightPosFromRenderer(rootRenderer);
Romain Guy02b49b72013-03-29 12:37:16 -0700281 renderer->setViewport(layer.getWidth(), layer.getHeight());
282 renderer->prepareDirty(dirtyRect.left, dirtyRect.top, dirtyRect.right, dirtyRect.bottom,
283 !isBlend());
284
Chris Craika7090e02014-06-20 16:01:00 -0700285 renderer->drawRenderNode(renderNode.get(), dirtyRect, RenderNode::kReplayFlag_ClipChildren);
Romain Guy02b49b72013-03-29 12:37:16 -0700286
287 renderer->finish();
Romain Guy02b49b72013-03-29 12:37:16 -0700288
289 dirtyRect.setEmpty();
290
291 deferredUpdateScheduled = false;
Chris Craika7090e02014-06-20 16:01:00 -0700292 renderNode = NULL;
Romain Guy02b49b72013-03-29 12:37:16 -0700293}
294
Chet Haased15ebf22012-09-05 11:40:29 -0700295}; // namespace uirenderer
296}; // namespace android