blob: 0fe20ad1b662cd8b6969786e9f748a9561fe1006 [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
Chris Craik65fe5ee2015-01-26 18:06:29 -080017#include "Layer.h"
Chet Haased15ebf22012-09-05 11:40:29 -070018
John Reck113e0822014-03-18 09:22:59 -070019#include "Caches.h"
Romain Guy96885eb2013-03-26 15:05:58 -070020#include "DeferredDisplayList.h"
Chet Haase98d3a642012-09-26 10:27:40 -070021#include "LayerRenderer.h"
Chet Haased15ebf22012-09-05 11:40:29 -070022#include "OpenGLRenderer.h"
John Reck113e0822014-03-18 09:22:59 -070023#include "RenderNode.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080024#include "renderstate/RenderState.h"
Chris Craik70850ea2014-11-18 10:49:23 -080025#include "utils/TraceUtils.h"
26
Chris Craik65fe5ee2015-01-26 18:06:29 -080027#include <utils/Log.h>
28
Chris Craik70850ea2014-11-18 10:49:23 -080029#define ATRACE_LAYER_WORK(label) \
30 ATRACE_FORMAT("%s HW Layer DisplayList %s %ux%u", \
31 label, \
32 (renderNode.get() != NULL) ? renderNode->getName() : "", \
33 getWidth(), getHeight())
Chet Haased15ebf22012-09-05 11:40:29 -070034
35namespace android {
36namespace uirenderer {
37
Chris Craike5c65842015-03-02 17:50:26 -080038Layer::Layer(Type layerType, RenderState& renderState, uint32_t layerWidth, uint32_t layerHeight)
Chris Craikb9ce116d2015-08-20 15:14:06 -070039 : state(State::Uncached)
Chris Craikbfd1cd62014-09-10 13:04:31 -070040 , caches(Caches::getInstance())
John Reck3b202512014-06-23 13:13:08 -070041 , renderState(renderState)
Chris Craik8a226d22014-09-08 16:40:21 -070042 , texture(caches)
43 , type(layerType) {
John Reck0e89e2b2014-10-31 14:49:06 -070044 // TODO: This is a violation of Android's typical ref counting, but it
45 // preserves the old inc/dec ref locations. This should be changed...
Chris Craikd41c4d82015-01-05 15:51:13 -080046 incStrong(nullptr);
Chet Haase603f6de2012-09-14 15:31:25 -070047 renderTarget = GL_TEXTURE_2D;
48 texture.width = layerWidth;
49 texture.height = layerHeight;
John Reck0e89e2b2014-10-31 14:49:06 -070050 renderState.registerLayer(this);
Chet Haase603f6de2012-09-14 15:31:25 -070051}
52
Chet Haased15ebf22012-09-05 11:40:29 -070053Layer::~Layer() {
John Reck0e89e2b2014-10-31 14:49:06 -070054 renderState.unregisterLayer(this);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -050055 SkSafeUnref(colorFilter);
John Reck57998012015-01-29 10:17:57 -080056
57 if (stencil || fbo || texture.id) {
58 renderState.requireGLContext();
59 removeFbo();
60 deleteTexture();
61 }
Romain Guy96885eb2013-03-26 15:05:58 -070062
63 delete[] mesh;
Romain Guy97dc9172012-09-23 17:46:45 -070064}
65
John Reck57998012015-01-29 10:17:57 -080066void Layer::onGlContextLost() {
67 removeFbo();
68 deleteTexture();
69}
70
Romain Guy2055aba2013-01-18 16:42:51 -080071uint32_t Layer::computeIdealWidth(uint32_t layerWidth) {
72 return uint32_t(ceilf(layerWidth / float(LAYER_SIZE)) * LAYER_SIZE);
73}
74
75uint32_t Layer::computeIdealHeight(uint32_t layerHeight) {
76 return uint32_t(ceilf(layerHeight / float(LAYER_SIZE)) * LAYER_SIZE);
77}
78
John Reck668f0e32014-03-26 15:10:40 -070079void Layer::requireRenderer() {
80 if (!renderer) {
Chris Craik51d6a3d2014-12-22 17:16:56 -080081 renderer.reset(new LayerRenderer(renderState, this));
John Reck668f0e32014-03-26 15:10:40 -070082 renderer->initProperties();
83 }
84}
85
Chris Craik69e5adf2014-08-14 13:34:01 -070086void Layer::updateLightPosFromRenderer(const OpenGLRenderer& rootRenderer) {
87 if (renderer && rendererLightPosDirty) {
88 // re-init renderer's light position, based upon last cached location in window
89 Vector3 lightPos = rootRenderer.getLightCenter();
90 cachedInvTransformInWindow.mapPoint3d(lightPos);
Alan Viverette50210d92015-05-14 18:05:36 -070091 renderer->initLight(rootRenderer.getLightRadius(),
92 rootRenderer.getAmbientShadowAlpha(),
93 rootRenderer.getSpotShadowAlpha());
94 renderer->setLightCenter(lightPos);
Chris Craik69e5adf2014-08-14 13:34:01 -070095 rendererLightPosDirty = false;
96 }
97}
98
Romain Guy2055aba2013-01-18 16:42:51 -080099bool Layer::resize(const uint32_t width, const uint32_t height) {
100 uint32_t desiredWidth = computeIdealWidth(width);
101 uint32_t desiredHeight = computeIdealWidth(height);
102
103 if (desiredWidth <= getWidth() && desiredHeight <= getHeight()) {
104 return true;
105 }
106
John Reckec4cefc2014-07-29 09:49:13 -0700107 ATRACE_NAME("resizeLayer");
108
Romain Guy8aa195d2013-06-04 18:00:09 -0700109 const uint32_t maxTextureSize = caches.maxTextureSize;
Romain Guyce4a7df2013-03-28 11:32:33 -0700110 if (desiredWidth > maxTextureSize || desiredHeight > maxTextureSize) {
111 ALOGW("Layer exceeds max. dimensions supported by the GPU (%dx%d, max=%dx%d)",
112 desiredWidth, desiredHeight, maxTextureSize, maxTextureSize);
113 return false;
114 }
115
Romain Guy2055aba2013-01-18 16:42:51 -0800116 uint32_t oldWidth = getWidth();
117 uint32_t oldHeight = getHeight();
118
119 setSize(desiredWidth, desiredHeight);
120
121 if (fbo) {
Chris Craik44eb2c02015-01-29 09:45:09 -0800122 caches.textureState().activateTexture(0);
Romain Guy2055aba2013-01-18 16:42:51 -0800123 bindTexture();
Romain Guy09087642013-04-04 12:27:54 -0700124 allocateTexture();
Romain Guy2055aba2013-01-18 16:42:51 -0800125
126 if (glGetError() != GL_NO_ERROR) {
127 setSize(oldWidth, oldHeight);
128 return false;
129 }
130 }
131
132 if (stencil) {
Romain Guy3bbacf22013-02-06 16:51:04 -0800133 stencil->bind();
134 stencil->resize(desiredWidth, desiredHeight);
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 return true;
143}
144
Romain Guy8ce00302013-01-15 18:51:42 -0800145void Layer::removeFbo(bool flush) {
146 if (stencil) {
John Reck3b202512014-06-23 13:13:08 -0700147 GLuint previousFbo = renderState.getFramebuffer();
148 renderState.bindFramebuffer(fbo);
Romain Guy8ce00302013-01-15 18:51:42 -0800149 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, 0);
John Reck3b202512014-06-23 13:13:08 -0700150 renderState.bindFramebuffer(previousFbo);
Romain Guy8ce00302013-01-15 18:51:42 -0800151
Romain Guy8aa195d2013-06-04 18:00:09 -0700152 caches.renderBufferCache.put(stencil);
Chris Craikd41c4d82015-01-05 15:51:13 -0800153 stencil = nullptr;
Romain Guy8ce00302013-01-15 18:51:42 -0800154 }
155
Chet Haase98d3a642012-09-26 10:27:40 -0700156 if (fbo) {
John Reck3b202512014-06-23 13:13:08 -0700157 if (flush) LayerRenderer::flushLayer(renderState, this);
Chris Craik818c9fb2015-10-23 14:33:42 -0700158 renderState.deleteFramebuffer(fbo);
Chet Haase98d3a642012-09-26 10:27:40 -0700159 fbo = 0;
Dave Burke56257af2012-09-25 20:30:09 -0700160 }
161}
162
Chris Craika7090e02014-06-20 16:01:00 -0700163void Layer::updateDeferred(RenderNode* renderNode, int left, int top, int right, int bottom) {
John Reck087bc0c2014-04-04 16:20:08 -0700164 requireRenderer();
Chris Craika7090e02014-06-20 16:01:00 -0700165 this->renderNode = renderNode;
John Reck087bc0c2014-04-04 16:20:08 -0700166 const Rect r(left, top, right, bottom);
167 dirtyRect.unionWith(r);
168 deferredUpdateScheduled = true;
169}
170
Derek Sollenberger674554f2014-02-19 16:47:32 +0000171void Layer::setPaint(const SkPaint* paint) {
Chris Craikbf6f0f22015-10-01 12:36:07 -0700172 alpha = PaintUtils::getAlphaDirect(paint);
173 mode = PaintUtils::getXfermodeDirect(paint);
Chris Craikd41c4d82015-01-05 15:51:13 -0800174 setColorFilter((paint) ? paint->getColorFilter() : nullptr);
Chet Haased15ebf22012-09-05 11:40:29 -0700175}
176
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500177void Layer::setColorFilter(SkColorFilter* filter) {
178 SkRefCnt_SafeAssign(colorFilter, filter);
Romain Guy8aa195d2013-06-04 18:00:09 -0700179}
180
181void Layer::bindTexture() const {
182 if (texture.id) {
Chris Craik44eb2c02015-01-29 09:45:09 -0800183 caches.textureState().bindTexture(renderTarget, texture.id);
Romain Guy8aa195d2013-06-04 18:00:09 -0700184 }
185}
186
187void Layer::bindStencilRenderBuffer() const {
188 if (stencil) {
189 stencil->bind();
190 }
191}
192
193void Layer::generateTexture() {
194 if (!texture.id) {
195 glGenTextures(1, &texture.id);
196 }
197}
198
199void Layer::deleteTexture() {
200 if (texture.id) {
Romain Guybe1b1272013-06-06 14:02:54 -0700201 texture.deleteTexture();
Romain Guy8aa195d2013-06-04 18:00:09 -0700202 texture.id = 0;
203 }
204}
205
206void Layer::clearTexture() {
Chris Craik44eb2c02015-01-29 09:45:09 -0800207 caches.textureState().unbindTexture(texture.id);
Romain Guy8aa195d2013-06-04 18:00:09 -0700208 texture.id = 0;
209}
210
211void Layer::allocateTexture() {
212#if DEBUG_LAYERS
213 ALOGD(" Allocate layer: %dx%d", getWidth(), getHeight());
214#endif
215 if (texture.id) {
216 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
217 glTexImage2D(renderTarget, 0, GL_RGBA, getWidth(), getHeight(), 0,
Chris Craikd41c4d82015-01-05 15:51:13 -0800218 GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
Chet Haased15ebf22012-09-05 11:40:29 -0700219 }
220}
221
Chris Craik69e5adf2014-08-14 13:34:01 -0700222void Layer::defer(const OpenGLRenderer& rootRenderer) {
Chris Craik70850ea2014-11-18 10:49:23 -0800223 ATRACE_LAYER_WORK("Optimize");
224
Chris Craik69e5adf2014-08-14 13:34:01 -0700225 updateLightPosFromRenderer(rootRenderer);
Romain Guy96885eb2013-03-26 15:05:58 -0700226 const float width = layer.getWidth();
227 const float height = layer.getHeight();
228
229 if (dirtyRect.isEmpty() || (dirtyRect.left <= 0 && dirtyRect.top <= 0 &&
230 dirtyRect.right >= width && dirtyRect.bottom >= height)) {
231 dirtyRect.set(0, 0, width, height);
232 }
233
Chris Craik51d6a3d2014-12-22 17:16:56 -0800234 deferredList.reset(new DeferredDisplayList(dirtyRect));
Chris Craikf57776b2013-10-25 18:30:17 -0700235
Chris Craik28ce94a2013-05-31 11:38:03 -0700236 DeferStateStruct deferredState(*deferredList, *renderer,
John Recke18264b2014-03-12 13:56:30 -0700237 RenderNode::kReplayFlag_ClipChildren);
Chris Craik28ce94a2013-05-31 11:38:03 -0700238
Chris Craik64e445b2015-09-02 14:23:49 -0700239 renderer->setupFrameState(width, height, dirtyRect.left, dirtyRect.top,
Romain Guy96885eb2013-03-26 15:05:58 -0700240 dirtyRect.right, dirtyRect.bottom, !isBlend());
241
Chris Craika7090e02014-06-20 16:01:00 -0700242 renderNode->computeOrdering();
243 renderNode->defer(deferredState, 0);
Romain Guy02b49b72013-03-29 12:37:16 -0700244
245 deferredUpdateScheduled = false;
Romain Guy96885eb2013-03-26 15:05:58 -0700246}
247
Romain Guye93482f2013-06-17 13:14:51 -0700248void Layer::cancelDefer() {
Chris Craikd41c4d82015-01-05 15:51:13 -0800249 renderNode = nullptr;
Romain Guye93482f2013-06-17 13:14:51 -0700250 deferredUpdateScheduled = false;
Sangkyu Lee441cc422015-12-11 16:05:10 +0900251 deferredList.reset(nullptr);
Romain Guye93482f2013-06-17 13:14:51 -0700252}
253
Romain Guy96885eb2013-03-26 15:05:58 -0700254void Layer::flush() {
Chris Craik8c6e17c2013-06-17 13:02:12 -0700255 // renderer is checked as layer may be destroyed/put in layer cache with flush scheduled
256 if (deferredList && renderer) {
Chris Craik70850ea2014-11-18 10:49:23 -0800257 ATRACE_LAYER_WORK("Issue");
Chris Craikd41c4d82015-01-05 15:51:13 -0800258 renderer->startMark((renderNode.get() != nullptr) ? renderNode->getName() : "Layer");
Chris Craik70850ea2014-11-18 10:49:23 -0800259
Chris Craik64e445b2015-09-02 14:23:49 -0700260 renderer->prepareDirty(layer.getWidth(), layer.getHeight(),
261 dirtyRect.left, dirtyRect.top, dirtyRect.right, dirtyRect.bottom, !isBlend());
Romain Guy96885eb2013-03-26 15:05:58 -0700262
263 deferredList->flush(*renderer, dirtyRect);
264
265 renderer->finish();
Romain Guy96885eb2013-03-26 15:05:58 -0700266
267 dirtyRect.setEmpty();
Chris Craikd41c4d82015-01-05 15:51:13 -0800268 renderNode = nullptr;
Chris Craik70850ea2014-11-18 10:49:23 -0800269
270 renderer->endMark();
Romain Guy96885eb2013-03-26 15:05:58 -0700271 }
272}
273
Chris Craik69e5adf2014-08-14 13:34:01 -0700274void Layer::render(const OpenGLRenderer& rootRenderer) {
Chris Craik70850ea2014-11-18 10:49:23 -0800275 ATRACE_LAYER_WORK("Direct-Issue");
276
Chris Craik69e5adf2014-08-14 13:34:01 -0700277 updateLightPosFromRenderer(rootRenderer);
Chris Craik64e445b2015-09-02 14:23:49 -0700278 renderer->prepareDirty(layer.getWidth(), layer.getHeight(),
279 dirtyRect.left, dirtyRect.top, dirtyRect.right, dirtyRect.bottom, !isBlend());
Romain Guy02b49b72013-03-29 12:37:16 -0700280
Chris Craika7090e02014-06-20 16:01:00 -0700281 renderer->drawRenderNode(renderNode.get(), dirtyRect, RenderNode::kReplayFlag_ClipChildren);
Romain Guy02b49b72013-03-29 12:37:16 -0700282
283 renderer->finish();
Romain Guy02b49b72013-03-29 12:37:16 -0700284
285 dirtyRect.setEmpty();
286
287 deferredUpdateScheduled = false;
Chris Craikd41c4d82015-01-05 15:51:13 -0800288 renderNode = nullptr;
Romain Guy02b49b72013-03-29 12:37:16 -0700289}
290
John Reck0e89e2b2014-10-31 14:49:06 -0700291void Layer::postDecStrong() {
292 renderState.postDecStrong(this);
293}
294
Chet Haased15ebf22012-09-05 11:40:29 -0700295}; // namespace uirenderer
296}; // namespace android