blob: 114347d94357a93eb6c9784327a8eb9eecf13dd2 [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)
John Reck38e0c322015-11-10 12:19:17 -080039 : GpuMemoryTracker(GpuObjectType::Layer)
40 , state(State::Uncached)
Chris Craikbfd1cd62014-09-10 13:04:31 -070041 , 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...
Chris Craikd41c4d82015-01-05 15:51:13 -080047 incStrong(nullptr);
Chet Haase603f6de2012-09-14 15:31:25 -070048 renderTarget = GL_TEXTURE_2D;
John Reck38e0c322015-11-10 12:19:17 -080049 texture.mWidth = layerWidth;
50 texture.mHeight = layerHeight;
John Reck0e89e2b2014-10-31 14:49:06 -070051 renderState.registerLayer(this);
Chet Haase603f6de2012-09-14 15:31:25 -070052}
53
Chet Haased15ebf22012-09-05 11:40:29 -070054Layer::~Layer() {
John Reck0e89e2b2014-10-31 14:49:06 -070055 renderState.unregisterLayer(this);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -050056 SkSafeUnref(colorFilter);
John Reck57998012015-01-29 10:17:57 -080057
John Reck38e0c322015-11-10 12:19:17 -080058 if (stencil || fbo || texture.mId) {
John Reck57998012015-01-29 10:17:57 -080059 removeFbo();
John Reck38e0c322015-11-10 12:19:17 -080060 texture.deleteTexture();
John Reck57998012015-01-29 10:17:57 -080061 }
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();
John Reck38e0c322015-11-10 12:19:17 -080068 texture.deleteTexture();
John Reck57998012015-01-29 10:17:57 -080069}
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 {
John Reck38e0c322015-11-10 12:19:17 -0800182 if (texture.mId) {
183 caches.textureState().bindTexture(renderTarget, texture.mId);
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() {
John Reck38e0c322015-11-10 12:19:17 -0800194 if (!texture.mId) {
195 glGenTextures(1, &texture.mId);
Romain Guy8aa195d2013-06-04 18:00:09 -0700196 }
197}
198
199void Layer::clearTexture() {
John Reck38e0c322015-11-10 12:19:17 -0800200 caches.textureState().unbindTexture(texture.mId);
201 texture.mId = 0;
Romain Guy8aa195d2013-06-04 18:00:09 -0700202}
203
204void Layer::allocateTexture() {
205#if DEBUG_LAYERS
206 ALOGD(" Allocate layer: %dx%d", getWidth(), getHeight());
207#endif
John Reck38e0c322015-11-10 12:19:17 -0800208 if (texture.mId) {
209 texture.updateSize(getWidth(), getHeight(), GL_RGBA);
Romain Guy8aa195d2013-06-04 18:00:09 -0700210 glTexImage2D(renderTarget, 0, GL_RGBA, getWidth(), getHeight(), 0,
Chris Craikd41c4d82015-01-05 15:51:13 -0800211 GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
Chet Haased15ebf22012-09-05 11:40:29 -0700212 }
213}
214
Chris Craik69e5adf2014-08-14 13:34:01 -0700215void Layer::defer(const OpenGLRenderer& rootRenderer) {
Chris Craik70850ea2014-11-18 10:49:23 -0800216 ATRACE_LAYER_WORK("Optimize");
217
Chris Craik69e5adf2014-08-14 13:34:01 -0700218 updateLightPosFromRenderer(rootRenderer);
Romain Guy96885eb2013-03-26 15:05:58 -0700219 const float width = layer.getWidth();
220 const float height = layer.getHeight();
221
222 if (dirtyRect.isEmpty() || (dirtyRect.left <= 0 && dirtyRect.top <= 0 &&
223 dirtyRect.right >= width && dirtyRect.bottom >= height)) {
224 dirtyRect.set(0, 0, width, height);
225 }
226
Chris Craik51d6a3d2014-12-22 17:16:56 -0800227 deferredList.reset(new DeferredDisplayList(dirtyRect));
Chris Craikf57776b2013-10-25 18:30:17 -0700228
Chris Craik28ce94a2013-05-31 11:38:03 -0700229 DeferStateStruct deferredState(*deferredList, *renderer,
John Recke18264b2014-03-12 13:56:30 -0700230 RenderNode::kReplayFlag_ClipChildren);
Chris Craik28ce94a2013-05-31 11:38:03 -0700231
Chris Craik64e445b2015-09-02 14:23:49 -0700232 renderer->setupFrameState(width, height, dirtyRect.left, dirtyRect.top,
Romain Guy96885eb2013-03-26 15:05:58 -0700233 dirtyRect.right, dirtyRect.bottom, !isBlend());
234
Chris Craika7090e02014-06-20 16:01:00 -0700235 renderNode->computeOrdering();
236 renderNode->defer(deferredState, 0);
Romain Guy02b49b72013-03-29 12:37:16 -0700237
238 deferredUpdateScheduled = false;
Romain Guy96885eb2013-03-26 15:05:58 -0700239}
240
Romain Guye93482f2013-06-17 13:14:51 -0700241void Layer::cancelDefer() {
Chris Craikd41c4d82015-01-05 15:51:13 -0800242 renderNode = nullptr;
Romain Guye93482f2013-06-17 13:14:51 -0700243 deferredUpdateScheduled = false;
Sangkyu Lee441cc422015-12-11 16:05:10 +0900244 deferredList.reset(nullptr);
Romain Guye93482f2013-06-17 13:14:51 -0700245}
246
Romain Guy96885eb2013-03-26 15:05:58 -0700247void Layer::flush() {
Chris Craik8c6e17c2013-06-17 13:02:12 -0700248 // renderer is checked as layer may be destroyed/put in layer cache with flush scheduled
249 if (deferredList && renderer) {
Chris Craik70850ea2014-11-18 10:49:23 -0800250 ATRACE_LAYER_WORK("Issue");
Chris Craikd41c4d82015-01-05 15:51:13 -0800251 renderer->startMark((renderNode.get() != nullptr) ? renderNode->getName() : "Layer");
Chris Craik70850ea2014-11-18 10:49:23 -0800252
Chris Craik64e445b2015-09-02 14:23:49 -0700253 renderer->prepareDirty(layer.getWidth(), layer.getHeight(),
254 dirtyRect.left, dirtyRect.top, dirtyRect.right, dirtyRect.bottom, !isBlend());
Romain Guy96885eb2013-03-26 15:05:58 -0700255
256 deferredList->flush(*renderer, dirtyRect);
257
258 renderer->finish();
Romain Guy96885eb2013-03-26 15:05:58 -0700259
260 dirtyRect.setEmpty();
Chris Craikd41c4d82015-01-05 15:51:13 -0800261 renderNode = nullptr;
Chris Craik70850ea2014-11-18 10:49:23 -0800262
263 renderer->endMark();
Romain Guy96885eb2013-03-26 15:05:58 -0700264 }
265}
266
Chris Craik69e5adf2014-08-14 13:34:01 -0700267void Layer::render(const OpenGLRenderer& rootRenderer) {
Chris Craik70850ea2014-11-18 10:49:23 -0800268 ATRACE_LAYER_WORK("Direct-Issue");
269
Chris Craik69e5adf2014-08-14 13:34:01 -0700270 updateLightPosFromRenderer(rootRenderer);
Chris Craik64e445b2015-09-02 14:23:49 -0700271 renderer->prepareDirty(layer.getWidth(), layer.getHeight(),
272 dirtyRect.left, dirtyRect.top, dirtyRect.right, dirtyRect.bottom, !isBlend());
Romain Guy02b49b72013-03-29 12:37:16 -0700273
Chris Craika7090e02014-06-20 16:01:00 -0700274 renderer->drawRenderNode(renderNode.get(), dirtyRect, RenderNode::kReplayFlag_ClipChildren);
Romain Guy02b49b72013-03-29 12:37:16 -0700275
276 renderer->finish();
Romain Guy02b49b72013-03-29 12:37:16 -0700277
278 dirtyRect.setEmpty();
279
280 deferredUpdateScheduled = false;
Chris Craikd41c4d82015-01-05 15:51:13 -0800281 renderNode = nullptr;
Romain Guy02b49b72013-03-29 12:37:16 -0700282}
283
John Reck0e89e2b2014-10-31 14:49:06 -0700284void Layer::postDecStrong() {
285 renderState.postDecStrong(this);
286}
287
Chet Haased15ebf22012-09-05 11:40:29 -0700288}; // namespace uirenderer
289}; // namespace android