blob: 7a74b9825d80f614d2ae126fcfc0d83bf6d96331 [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 renderState.requireGLContext();
60 removeFbo();
John Reck38e0c322015-11-10 12:19:17 -080061 texture.deleteTexture();
John Reck57998012015-01-29 10:17:57 -080062 }
Romain Guy96885eb2013-03-26 15:05:58 -070063
64 delete[] mesh;
Romain Guy97dc9172012-09-23 17:46:45 -070065}
66
John Reck57998012015-01-29 10:17:57 -080067void Layer::onGlContextLost() {
68 removeFbo();
John Reck38e0c322015-11-10 12:19:17 -080069 texture.deleteTexture();
John Reck57998012015-01-29 10:17:57 -080070}
71
Romain Guy2055aba2013-01-18 16:42:51 -080072uint32_t Layer::computeIdealWidth(uint32_t layerWidth) {
73 return uint32_t(ceilf(layerWidth / float(LAYER_SIZE)) * LAYER_SIZE);
74}
75
76uint32_t Layer::computeIdealHeight(uint32_t layerHeight) {
77 return uint32_t(ceilf(layerHeight / float(LAYER_SIZE)) * LAYER_SIZE);
78}
79
John Reck668f0e32014-03-26 15:10:40 -070080void Layer::requireRenderer() {
81 if (!renderer) {
Chris Craik51d6a3d2014-12-22 17:16:56 -080082 renderer.reset(new LayerRenderer(renderState, this));
John Reck668f0e32014-03-26 15:10:40 -070083 renderer->initProperties();
84 }
85}
86
Chris Craik69e5adf2014-08-14 13:34:01 -070087void Layer::updateLightPosFromRenderer(const OpenGLRenderer& rootRenderer) {
88 if (renderer && rendererLightPosDirty) {
89 // re-init renderer's light position, based upon last cached location in window
90 Vector3 lightPos = rootRenderer.getLightCenter();
91 cachedInvTransformInWindow.mapPoint3d(lightPos);
Alan Viverette50210d92015-05-14 18:05:36 -070092 renderer->initLight(rootRenderer.getLightRadius(),
93 rootRenderer.getAmbientShadowAlpha(),
94 rootRenderer.getSpotShadowAlpha());
95 renderer->setLightCenter(lightPos);
Chris Craik69e5adf2014-08-14 13:34:01 -070096 rendererLightPosDirty = false;
97 }
98}
99
Romain Guy2055aba2013-01-18 16:42:51 -0800100bool Layer::resize(const uint32_t width, const uint32_t height) {
101 uint32_t desiredWidth = computeIdealWidth(width);
102 uint32_t desiredHeight = computeIdealWidth(height);
103
104 if (desiredWidth <= getWidth() && desiredHeight <= getHeight()) {
105 return true;
106 }
107
John Reckec4cefc2014-07-29 09:49:13 -0700108 ATRACE_NAME("resizeLayer");
109
Romain Guy8aa195d2013-06-04 18:00:09 -0700110 const uint32_t maxTextureSize = caches.maxTextureSize;
Romain Guyce4a7df2013-03-28 11:32:33 -0700111 if (desiredWidth > maxTextureSize || desiredHeight > maxTextureSize) {
112 ALOGW("Layer exceeds max. dimensions supported by the GPU (%dx%d, max=%dx%d)",
113 desiredWidth, desiredHeight, maxTextureSize, maxTextureSize);
114 return false;
115 }
116
Romain Guy2055aba2013-01-18 16:42:51 -0800117 uint32_t oldWidth = getWidth();
118 uint32_t oldHeight = getHeight();
119
120 setSize(desiredWidth, desiredHeight);
121
122 if (fbo) {
Chris Craik44eb2c02015-01-29 09:45:09 -0800123 caches.textureState().activateTexture(0);
Romain Guy2055aba2013-01-18 16:42:51 -0800124 bindTexture();
Romain Guy09087642013-04-04 12:27:54 -0700125 allocateTexture();
Romain Guy2055aba2013-01-18 16:42:51 -0800126
127 if (glGetError() != GL_NO_ERROR) {
128 setSize(oldWidth, oldHeight);
129 return false;
130 }
131 }
132
133 if (stencil) {
Romain Guy3bbacf22013-02-06 16:51:04 -0800134 stencil->bind();
135 stencil->resize(desiredWidth, desiredHeight);
Romain Guy2055aba2013-01-18 16:42:51 -0800136
137 if (glGetError() != GL_NO_ERROR) {
138 setSize(oldWidth, oldHeight);
139 return false;
140 }
141 }
142
143 return true;
144}
145
Romain Guy8ce00302013-01-15 18:51:42 -0800146void Layer::removeFbo(bool flush) {
147 if (stencil) {
John Reck3b202512014-06-23 13:13:08 -0700148 GLuint previousFbo = renderState.getFramebuffer();
149 renderState.bindFramebuffer(fbo);
Romain Guy8ce00302013-01-15 18:51:42 -0800150 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, 0);
John Reck3b202512014-06-23 13:13:08 -0700151 renderState.bindFramebuffer(previousFbo);
Romain Guy8ce00302013-01-15 18:51:42 -0800152
Romain Guy8aa195d2013-06-04 18:00:09 -0700153 caches.renderBufferCache.put(stencil);
Chris Craikd41c4d82015-01-05 15:51:13 -0800154 stencil = nullptr;
Romain Guy8ce00302013-01-15 18:51:42 -0800155 }
156
Chet Haase98d3a642012-09-26 10:27:40 -0700157 if (fbo) {
John Reck3b202512014-06-23 13:13:08 -0700158 if (flush) LayerRenderer::flushLayer(renderState, this);
Chris Craik818c9fb2015-10-23 14:33:42 -0700159 renderState.deleteFramebuffer(fbo);
Chet Haase98d3a642012-09-26 10:27:40 -0700160 fbo = 0;
Dave Burke56257af2012-09-25 20:30:09 -0700161 }
162}
163
Chris Craika7090e02014-06-20 16:01:00 -0700164void Layer::updateDeferred(RenderNode* renderNode, int left, int top, int right, int bottom) {
John Reck087bc0c2014-04-04 16:20:08 -0700165 requireRenderer();
Chris Craika7090e02014-06-20 16:01:00 -0700166 this->renderNode = renderNode;
John Reck087bc0c2014-04-04 16:20:08 -0700167 const Rect r(left, top, right, bottom);
168 dirtyRect.unionWith(r);
169 deferredUpdateScheduled = true;
170}
171
Derek Sollenberger674554f2014-02-19 16:47:32 +0000172void Layer::setPaint(const SkPaint* paint) {
Chris Craikbf6f0f22015-10-01 12:36:07 -0700173 alpha = PaintUtils::getAlphaDirect(paint);
174 mode = PaintUtils::getXfermodeDirect(paint);
Chris Craikd41c4d82015-01-05 15:51:13 -0800175 setColorFilter((paint) ? paint->getColorFilter() : nullptr);
Chet Haased15ebf22012-09-05 11:40:29 -0700176}
177
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500178void Layer::setColorFilter(SkColorFilter* filter) {
179 SkRefCnt_SafeAssign(colorFilter, filter);
Romain Guy8aa195d2013-06-04 18:00:09 -0700180}
181
182void Layer::bindTexture() const {
John Reck38e0c322015-11-10 12:19:17 -0800183 if (texture.mId) {
184 caches.textureState().bindTexture(renderTarget, texture.mId);
Romain Guy8aa195d2013-06-04 18:00:09 -0700185 }
186}
187
188void Layer::bindStencilRenderBuffer() const {
189 if (stencil) {
190 stencil->bind();
191 }
192}
193
194void Layer::generateTexture() {
John Reck38e0c322015-11-10 12:19:17 -0800195 if (!texture.mId) {
196 glGenTextures(1, &texture.mId);
Romain Guy8aa195d2013-06-04 18:00:09 -0700197 }
198}
199
200void Layer::clearTexture() {
John Reck38e0c322015-11-10 12:19:17 -0800201 caches.textureState().unbindTexture(texture.mId);
202 texture.mId = 0;
Romain Guy8aa195d2013-06-04 18:00:09 -0700203}
204
205void Layer::allocateTexture() {
206#if DEBUG_LAYERS
207 ALOGD(" Allocate layer: %dx%d", getWidth(), getHeight());
208#endif
John Reck38e0c322015-11-10 12:19:17 -0800209 if (texture.mId) {
210 texture.updateSize(getWidth(), getHeight(), GL_RGBA);
Romain Guy8aa195d2013-06-04 18:00:09 -0700211 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
212 glTexImage2D(renderTarget, 0, GL_RGBA, getWidth(), getHeight(), 0,
Chris Craikd41c4d82015-01-05 15:51:13 -0800213 GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
Chet Haased15ebf22012-09-05 11:40:29 -0700214 }
215}
216
Chris Craik69e5adf2014-08-14 13:34:01 -0700217void Layer::defer(const OpenGLRenderer& rootRenderer) {
Chris Craik70850ea2014-11-18 10:49:23 -0800218 ATRACE_LAYER_WORK("Optimize");
219
Chris Craik69e5adf2014-08-14 13:34:01 -0700220 updateLightPosFromRenderer(rootRenderer);
Romain Guy96885eb2013-03-26 15:05:58 -0700221 const float width = layer.getWidth();
222 const float height = layer.getHeight();
223
224 if (dirtyRect.isEmpty() || (dirtyRect.left <= 0 && dirtyRect.top <= 0 &&
225 dirtyRect.right >= width && dirtyRect.bottom >= height)) {
226 dirtyRect.set(0, 0, width, height);
227 }
228
Chris Craik51d6a3d2014-12-22 17:16:56 -0800229 deferredList.reset(new DeferredDisplayList(dirtyRect));
Chris Craikf57776b2013-10-25 18:30:17 -0700230
Chris Craik28ce94a2013-05-31 11:38:03 -0700231 DeferStateStruct deferredState(*deferredList, *renderer,
John Recke18264b2014-03-12 13:56:30 -0700232 RenderNode::kReplayFlag_ClipChildren);
Chris Craik28ce94a2013-05-31 11:38:03 -0700233
Chris Craik64e445b2015-09-02 14:23:49 -0700234 renderer->setupFrameState(width, height, dirtyRect.left, dirtyRect.top,
Romain Guy96885eb2013-03-26 15:05:58 -0700235 dirtyRect.right, dirtyRect.bottom, !isBlend());
236
Chris Craika7090e02014-06-20 16:01:00 -0700237 renderNode->computeOrdering();
238 renderNode->defer(deferredState, 0);
Romain Guy02b49b72013-03-29 12:37:16 -0700239
240 deferredUpdateScheduled = false;
Romain Guy96885eb2013-03-26 15:05:58 -0700241}
242
Romain Guye93482f2013-06-17 13:14:51 -0700243void Layer::cancelDefer() {
Chris Craikd41c4d82015-01-05 15:51:13 -0800244 renderNode = nullptr;
Romain Guye93482f2013-06-17 13:14:51 -0700245 deferredUpdateScheduled = false;
Sangkyu Lee441cc422015-12-11 16:05:10 +0900246 deferredList.reset(nullptr);
Romain Guye93482f2013-06-17 13:14:51 -0700247}
248
Romain Guy96885eb2013-03-26 15:05:58 -0700249void Layer::flush() {
Chris Craik8c6e17c2013-06-17 13:02:12 -0700250 // renderer is checked as layer may be destroyed/put in layer cache with flush scheduled
251 if (deferredList && renderer) {
Chris Craik70850ea2014-11-18 10:49:23 -0800252 ATRACE_LAYER_WORK("Issue");
Chris Craikd41c4d82015-01-05 15:51:13 -0800253 renderer->startMark((renderNode.get() != nullptr) ? renderNode->getName() : "Layer");
Chris Craik70850ea2014-11-18 10:49:23 -0800254
Chris Craik64e445b2015-09-02 14:23:49 -0700255 renderer->prepareDirty(layer.getWidth(), layer.getHeight(),
256 dirtyRect.left, dirtyRect.top, dirtyRect.right, dirtyRect.bottom, !isBlend());
Romain Guy96885eb2013-03-26 15:05:58 -0700257
258 deferredList->flush(*renderer, dirtyRect);
259
260 renderer->finish();
Romain Guy96885eb2013-03-26 15:05:58 -0700261
262 dirtyRect.setEmpty();
Chris Craikd41c4d82015-01-05 15:51:13 -0800263 renderNode = nullptr;
Chris Craik70850ea2014-11-18 10:49:23 -0800264
265 renderer->endMark();
Romain Guy96885eb2013-03-26 15:05:58 -0700266 }
267}
268
Chris Craik69e5adf2014-08-14 13:34:01 -0700269void Layer::render(const OpenGLRenderer& rootRenderer) {
Chris Craik70850ea2014-11-18 10:49:23 -0800270 ATRACE_LAYER_WORK("Direct-Issue");
271
Chris Craik69e5adf2014-08-14 13:34:01 -0700272 updateLightPosFromRenderer(rootRenderer);
Chris Craik64e445b2015-09-02 14:23:49 -0700273 renderer->prepareDirty(layer.getWidth(), layer.getHeight(),
274 dirtyRect.left, dirtyRect.top, dirtyRect.right, dirtyRect.bottom, !isBlend());
Romain Guy02b49b72013-03-29 12:37:16 -0700275
Chris Craika7090e02014-06-20 16:01:00 -0700276 renderer->drawRenderNode(renderNode.get(), dirtyRect, RenderNode::kReplayFlag_ClipChildren);
Romain Guy02b49b72013-03-29 12:37:16 -0700277
278 renderer->finish();
Romain Guy02b49b72013-03-29 12:37:16 -0700279
280 dirtyRect.setEmpty();
281
282 deferredUpdateScheduled = false;
Chris Craikd41c4d82015-01-05 15:51:13 -0800283 renderNode = nullptr;
Romain Guy02b49b72013-03-29 12:37:16 -0700284}
285
John Reck0e89e2b2014-10-31 14:49:06 -0700286void Layer::postDecStrong() {
287 renderState.postDecStrong(this);
288}
289
Chet Haased15ebf22012-09-05 11:40:29 -0700290}; // namespace uirenderer
291}; // namespace android