blob: 9aa29ca184d0a045d9c4e98265bac557160ab05b [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"
Chris Craik70850ea2014-11-18 10:49:23 -080027#include "RenderState.h"
28#include "utils/TraceUtils.h"
29
30#define ATRACE_LAYER_WORK(label) \
31 ATRACE_FORMAT("%s HW Layer DisplayList %s %ux%u", \
32 label, \
33 (renderNode.get() != NULL) ? renderNode->getName() : "", \
34 getWidth(), getHeight())
Chet Haased15ebf22012-09-05 11:40:29 -070035
36namespace android {
37namespace uirenderer {
38
Chris Craik8a226d22014-09-08 16:40:21 -070039Layer::Layer(Type layerType, RenderState& renderState, const uint32_t layerWidth, const uint32_t layerHeight)
Chris Craikbfd1cd62014-09-10 13:04:31 -070040 : state(kState_Uncached)
41 , caches(Caches::getInstance())
John Reck3b202512014-06-23 13:13:08 -070042 , renderState(renderState)
Chris Craik8a226d22014-09-08 16:40:21 -070043 , texture(caches)
44 , type(layerType) {
John Reck0e89e2b2014-10-31 14:49:06 -070045 // TODO: This is a violation of Android's typical ref counting, but it
46 // preserves the old inc/dec ref locations. This should be changed...
47 incStrong(0);
Chet Haase603f6de2012-09-14 15:31:25 -070048 mesh = NULL;
Chet Haase603f6de2012-09-14 15:31:25 -070049 meshElementCount = 0;
50 cacheable = true;
Romain Guy7c25aab2012-10-18 15:05:02 -070051 dirty = false;
Chet Haase603f6de2012-09-14 15:31:25 -070052 renderTarget = GL_TEXTURE_2D;
53 texture.width = layerWidth;
54 texture.height = layerHeight;
55 colorFilter = NULL;
56 deferredUpdateScheduled = false;
57 renderer = NULL;
Chris Craika7090e02014-06-20 16:01:00 -070058 renderNode = NULL;
Chet Haase603f6de2012-09-14 15:31:25 -070059 fbo = 0;
Romain Guy3bbacf22013-02-06 16:51:04 -080060 stencil = NULL;
Romain Guy5bb3c732012-11-29 17:52:58 -080061 debugDrawUpdate = false;
Chris Craik34416ea2013-04-15 16:08:28 -070062 hasDrawnSinceUpdate = false;
Chris Craik9757ac02014-02-25 18:50:17 -080063 forceFilter = false;
Romain Guy96885eb2013-03-26 15:05:58 -070064 deferredList = NULL;
Chris Craik3f0854292014-04-15 16:18:08 -070065 convexMask = NULL;
Chris Craik69e5adf2014-08-14 13:34:01 -070066 rendererLightPosDirty = true;
John Reck443a7142014-09-04 17:40:05 -070067 wasBuildLayered = false;
John Reck0e89e2b2014-10-31 14:49:06 -070068 renderState.registerLayer(this);
Chet Haase603f6de2012-09-14 15:31:25 -070069}
70
Chet Haased15ebf22012-09-05 11:40:29 -070071Layer::~Layer() {
John Reck0e89e2b2014-10-31 14:49:06 -070072 renderState.requireGLContext();
73 renderState.unregisterLayer(this);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -050074 SkSafeUnref(colorFilter);
Chet Haase98d3a642012-09-26 10:27:40 -070075 removeFbo();
Romain Guy8a137492012-09-25 15:49:03 -070076 deleteTexture();
Romain Guy96885eb2013-03-26 15:05:58 -070077
78 delete[] mesh;
Romain Guy96885eb2013-03-26 15:05:58 -070079 delete deferredList;
John Reck668f0e32014-03-26 15:10:40 -070080 delete renderer;
Romain Guy97dc9172012-09-23 17:46:45 -070081}
82
Romain Guy2055aba2013-01-18 16:42:51 -080083uint32_t Layer::computeIdealWidth(uint32_t layerWidth) {
84 return uint32_t(ceilf(layerWidth / float(LAYER_SIZE)) * LAYER_SIZE);
85}
86
87uint32_t Layer::computeIdealHeight(uint32_t layerHeight) {
88 return uint32_t(ceilf(layerHeight / float(LAYER_SIZE)) * LAYER_SIZE);
89}
90
John Reck668f0e32014-03-26 15:10:40 -070091void Layer::requireRenderer() {
92 if (!renderer) {
John Reck3b202512014-06-23 13:13:08 -070093 renderer = new LayerRenderer(renderState, this);
John Reck668f0e32014-03-26 15:10:40 -070094 renderer->initProperties();
95 }
96}
97
Chris Craik69e5adf2014-08-14 13:34:01 -070098void Layer::updateLightPosFromRenderer(const OpenGLRenderer& rootRenderer) {
99 if (renderer && rendererLightPosDirty) {
100 // re-init renderer's light position, based upon last cached location in window
101 Vector3 lightPos = rootRenderer.getLightCenter();
102 cachedInvTransformInWindow.mapPoint3d(lightPos);
103 renderer->initLight(lightPos, rootRenderer.getLightRadius(),
104 rootRenderer.getAmbientShadowAlpha(), rootRenderer.getSpotShadowAlpha());
105 rendererLightPosDirty = false;
106 }
107}
108
Romain Guy2055aba2013-01-18 16:42:51 -0800109bool Layer::resize(const uint32_t width, const uint32_t height) {
110 uint32_t desiredWidth = computeIdealWidth(width);
111 uint32_t desiredHeight = computeIdealWidth(height);
112
113 if (desiredWidth <= getWidth() && desiredHeight <= getHeight()) {
114 return true;
115 }
116
John Reckec4cefc2014-07-29 09:49:13 -0700117 ATRACE_NAME("resizeLayer");
118
Romain Guy8aa195d2013-06-04 18:00:09 -0700119 const uint32_t maxTextureSize = caches.maxTextureSize;
Romain Guyce4a7df2013-03-28 11:32:33 -0700120 if (desiredWidth > maxTextureSize || desiredHeight > maxTextureSize) {
121 ALOGW("Layer exceeds max. dimensions supported by the GPU (%dx%d, max=%dx%d)",
122 desiredWidth, desiredHeight, maxTextureSize, maxTextureSize);
123 return false;
124 }
125
Romain Guy2055aba2013-01-18 16:42:51 -0800126 uint32_t oldWidth = getWidth();
127 uint32_t oldHeight = getHeight();
128
129 setSize(desiredWidth, desiredHeight);
130
131 if (fbo) {
Romain Guy8aa195d2013-06-04 18:00:09 -0700132 caches.activeTexture(0);
Romain Guy2055aba2013-01-18 16:42:51 -0800133 bindTexture();
Romain Guy09087642013-04-04 12:27:54 -0700134 allocateTexture();
Romain Guy2055aba2013-01-18 16:42:51 -0800135
136 if (glGetError() != GL_NO_ERROR) {
137 setSize(oldWidth, oldHeight);
138 return false;
139 }
140 }
141
142 if (stencil) {
Romain Guy3bbacf22013-02-06 16:51:04 -0800143 stencil->bind();
144 stencil->resize(desiredWidth, desiredHeight);
Romain Guy2055aba2013-01-18 16:42:51 -0800145
146 if (glGetError() != GL_NO_ERROR) {
147 setSize(oldWidth, oldHeight);
148 return false;
149 }
150 }
151
152 return true;
153}
154
Romain Guy8ce00302013-01-15 18:51:42 -0800155void Layer::removeFbo(bool flush) {
156 if (stencil) {
John Reck3b202512014-06-23 13:13:08 -0700157 GLuint previousFbo = renderState.getFramebuffer();
158 renderState.bindFramebuffer(fbo);
Romain Guy8ce00302013-01-15 18:51:42 -0800159 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, 0);
John Reck3b202512014-06-23 13:13:08 -0700160 renderState.bindFramebuffer(previousFbo);
Romain Guy8ce00302013-01-15 18:51:42 -0800161
Romain Guy8aa195d2013-06-04 18:00:09 -0700162 caches.renderBufferCache.put(stencil);
Romain Guy3bbacf22013-02-06 16:51:04 -0800163 stencil = NULL;
Romain Guy8ce00302013-01-15 18:51:42 -0800164 }
165
Chet Haase98d3a642012-09-26 10:27:40 -0700166 if (fbo) {
John Reck3b202512014-06-23 13:13:08 -0700167 if (flush) LayerRenderer::flushLayer(renderState, this);
Romain Guy8ce00302013-01-15 18:51:42 -0800168 // If put fails the cache will delete the FBO
Romain Guy8aa195d2013-06-04 18:00:09 -0700169 caches.fboCache.put(fbo);
Chet Haase98d3a642012-09-26 10:27:40 -0700170 fbo = 0;
Dave Burke56257af2012-09-25 20:30:09 -0700171 }
172}
173
Chris Craika7090e02014-06-20 16:01:00 -0700174void Layer::updateDeferred(RenderNode* renderNode, int left, int top, int right, int bottom) {
John Reck087bc0c2014-04-04 16:20:08 -0700175 requireRenderer();
Chris Craika7090e02014-06-20 16:01:00 -0700176 this->renderNode = renderNode;
John Reck087bc0c2014-04-04 16:20:08 -0700177 const Rect r(left, top, right, bottom);
178 dirtyRect.unionWith(r);
179 deferredUpdateScheduled = true;
180}
181
Derek Sollenberger674554f2014-02-19 16:47:32 +0000182void Layer::setPaint(const SkPaint* paint) {
Chet Haased15ebf22012-09-05 11:40:29 -0700183 OpenGLRenderer::getAlphaAndModeDirect(paint, &alpha, &mode);
Derek Sollenberger674554f2014-02-19 16:47:32 +0000184 setColorFilter((paint) ? paint->getColorFilter() : NULL);
Chet Haased15ebf22012-09-05 11:40:29 -0700185}
186
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500187void Layer::setColorFilter(SkColorFilter* filter) {
188 SkRefCnt_SafeAssign(colorFilter, filter);
Romain Guy8aa195d2013-06-04 18:00:09 -0700189}
190
191void Layer::bindTexture() const {
192 if (texture.id) {
193 caches.bindTexture(renderTarget, texture.id);
194 }
195}
196
197void Layer::bindStencilRenderBuffer() const {
198 if (stencil) {
199 stencil->bind();
200 }
201}
202
203void Layer::generateTexture() {
204 if (!texture.id) {
205 glGenTextures(1, &texture.id);
206 }
207}
208
209void Layer::deleteTexture() {
210 if (texture.id) {
Romain Guybe1b1272013-06-06 14:02:54 -0700211 texture.deleteTexture();
Romain Guy8aa195d2013-06-04 18:00:09 -0700212 texture.id = 0;
213 }
214}
215
216void Layer::clearTexture() {
jiayuanr4a473c7d2014-06-10 17:41:49 +0800217 caches.unbindTexture(texture.id);
Romain Guy8aa195d2013-06-04 18:00:09 -0700218 texture.id = 0;
219}
220
221void Layer::allocateTexture() {
222#if DEBUG_LAYERS
223 ALOGD(" Allocate layer: %dx%d", getWidth(), getHeight());
224#endif
225 if (texture.id) {
226 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
227 glTexImage2D(renderTarget, 0, GL_RGBA, getWidth(), getHeight(), 0,
228 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
Chet Haased15ebf22012-09-05 11:40:29 -0700229 }
230}
231
Chris Craik69e5adf2014-08-14 13:34:01 -0700232void Layer::defer(const OpenGLRenderer& rootRenderer) {
Chris Craik70850ea2014-11-18 10:49:23 -0800233 ATRACE_LAYER_WORK("Optimize");
234
Chris Craik69e5adf2014-08-14 13:34:01 -0700235 updateLightPosFromRenderer(rootRenderer);
Romain Guy96885eb2013-03-26 15:05:58 -0700236 const float width = layer.getWidth();
237 const float height = layer.getHeight();
238
239 if (dirtyRect.isEmpty() || (dirtyRect.left <= 0 && dirtyRect.top <= 0 &&
240 dirtyRect.right >= width && dirtyRect.bottom >= height)) {
241 dirtyRect.set(0, 0, width, height);
242 }
243
Chris Craikf57776b2013-10-25 18:30:17 -0700244 delete deferredList;
245 deferredList = new DeferredDisplayList(dirtyRect);
246
Chris Craik28ce94a2013-05-31 11:38:03 -0700247 DeferStateStruct deferredState(*deferredList, *renderer,
John Recke18264b2014-03-12 13:56:30 -0700248 RenderNode::kReplayFlag_ClipChildren);
Chris Craik28ce94a2013-05-31 11:38:03 -0700249
Chris Craik797b95b2014-05-20 18:10:25 -0700250 renderer->setViewport(width, height);
Romain Guy96885eb2013-03-26 15:05:58 -0700251 renderer->setupFrameState(dirtyRect.left, dirtyRect.top,
252 dirtyRect.right, dirtyRect.bottom, !isBlend());
253
Chris Craika7090e02014-06-20 16:01:00 -0700254 renderNode->computeOrdering();
255 renderNode->defer(deferredState, 0);
Romain Guy02b49b72013-03-29 12:37:16 -0700256
257 deferredUpdateScheduled = false;
Romain Guy96885eb2013-03-26 15:05:58 -0700258}
259
Romain Guye93482f2013-06-17 13:14:51 -0700260void Layer::cancelDefer() {
Chris Craika7090e02014-06-20 16:01:00 -0700261 renderNode = NULL;
Romain Guye93482f2013-06-17 13:14:51 -0700262 deferredUpdateScheduled = false;
263 if (deferredList) {
264 delete deferredList;
265 deferredList = NULL;
266 }
267}
268
Romain Guy96885eb2013-03-26 15:05:58 -0700269void Layer::flush() {
Chris Craik8c6e17c2013-06-17 13:02:12 -0700270 // renderer is checked as layer may be destroyed/put in layer cache with flush scheduled
271 if (deferredList && renderer) {
Chris Craik70850ea2014-11-18 10:49:23 -0800272 ATRACE_LAYER_WORK("Issue");
273 renderer->startMark((renderNode.get() != NULL) ? renderNode->getName() : "Layer");
274
Romain Guy96885eb2013-03-26 15:05:58 -0700275 renderer->setViewport(layer.getWidth(), layer.getHeight());
276 renderer->prepareDirty(dirtyRect.left, dirtyRect.top, dirtyRect.right, dirtyRect.bottom,
277 !isBlend());
278
279 deferredList->flush(*renderer, dirtyRect);
280
281 renderer->finish();
Romain Guy96885eb2013-03-26 15:05:58 -0700282
283 dirtyRect.setEmpty();
Chris Craika7090e02014-06-20 16:01:00 -0700284 renderNode = NULL;
Chris Craik70850ea2014-11-18 10:49:23 -0800285
286 renderer->endMark();
Romain Guy96885eb2013-03-26 15:05:58 -0700287 }
288}
289
Chris Craik69e5adf2014-08-14 13:34:01 -0700290void Layer::render(const OpenGLRenderer& rootRenderer) {
Chris Craik70850ea2014-11-18 10:49:23 -0800291 ATRACE_LAYER_WORK("Direct-Issue");
292
Chris Craik69e5adf2014-08-14 13:34:01 -0700293 updateLightPosFromRenderer(rootRenderer);
Romain Guy02b49b72013-03-29 12:37:16 -0700294 renderer->setViewport(layer.getWidth(), layer.getHeight());
295 renderer->prepareDirty(dirtyRect.left, dirtyRect.top, dirtyRect.right, dirtyRect.bottom,
296 !isBlend());
297
Chris Craika7090e02014-06-20 16:01:00 -0700298 renderer->drawRenderNode(renderNode.get(), dirtyRect, RenderNode::kReplayFlag_ClipChildren);
Romain Guy02b49b72013-03-29 12:37:16 -0700299
300 renderer->finish();
Romain Guy02b49b72013-03-29 12:37:16 -0700301
302 dirtyRect.setEmpty();
303
304 deferredUpdateScheduled = false;
Chris Craika7090e02014-06-20 16:01:00 -0700305 renderNode = NULL;
Romain Guy02b49b72013-03-29 12:37:16 -0700306}
307
John Reck0e89e2b2014-10-31 14:49:06 -0700308void Layer::postDecStrong() {
309 renderState.postDecStrong(this);
310}
311
Chet Haased15ebf22012-09-05 11:40:29 -0700312}; // namespace uirenderer
313}; // namespace android