blob: cdbbbab7730d7e23b7348f0702af062c24c2c04c [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 Reckdb009172016-03-17 11:02:07 -0700200 // There's a rare possibility that Caches could have been destroyed already
201 // since this method is queued up as a task.
202 // Since this is a reset method, treat this as non-fatal.
203 if (caches.isInitialized()) {
204 caches.textureState().unbindTexture(texture.mId);
205 }
John Reck38e0c322015-11-10 12:19:17 -0800206 texture.mId = 0;
Romain Guy8aa195d2013-06-04 18:00:09 -0700207}
208
209void Layer::allocateTexture() {
210#if DEBUG_LAYERS
211 ALOGD(" Allocate layer: %dx%d", getWidth(), getHeight());
212#endif
John Reck38e0c322015-11-10 12:19:17 -0800213 if (texture.mId) {
214 texture.updateSize(getWidth(), getHeight(), GL_RGBA);
Romain Guy8aa195d2013-06-04 18:00:09 -0700215 glTexImage2D(renderTarget, 0, GL_RGBA, getWidth(), getHeight(), 0,
Chris Craikd41c4d82015-01-05 15:51:13 -0800216 GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
Chet Haased15ebf22012-09-05 11:40:29 -0700217 }
218}
219
Chris Craik69e5adf2014-08-14 13:34:01 -0700220void Layer::defer(const OpenGLRenderer& rootRenderer) {
Chris Craik70850ea2014-11-18 10:49:23 -0800221 ATRACE_LAYER_WORK("Optimize");
222
Chris Craik69e5adf2014-08-14 13:34:01 -0700223 updateLightPosFromRenderer(rootRenderer);
Romain Guy96885eb2013-03-26 15:05:58 -0700224 const float width = layer.getWidth();
225 const float height = layer.getHeight();
226
227 if (dirtyRect.isEmpty() || (dirtyRect.left <= 0 && dirtyRect.top <= 0 &&
228 dirtyRect.right >= width && dirtyRect.bottom >= height)) {
229 dirtyRect.set(0, 0, width, height);
230 }
231
Chris Craik51d6a3d2014-12-22 17:16:56 -0800232 deferredList.reset(new DeferredDisplayList(dirtyRect));
Chris Craikf57776b2013-10-25 18:30:17 -0700233
Chris Craik28ce94a2013-05-31 11:38:03 -0700234 DeferStateStruct deferredState(*deferredList, *renderer,
John Recke18264b2014-03-12 13:56:30 -0700235 RenderNode::kReplayFlag_ClipChildren);
Chris Craik28ce94a2013-05-31 11:38:03 -0700236
Chris Craik64e445b2015-09-02 14:23:49 -0700237 renderer->setupFrameState(width, height, dirtyRect.left, dirtyRect.top,
Romain Guy96885eb2013-03-26 15:05:58 -0700238 dirtyRect.right, dirtyRect.bottom, !isBlend());
239
Chris Craika7090e02014-06-20 16:01:00 -0700240 renderNode->computeOrdering();
241 renderNode->defer(deferredState, 0);
Romain Guy02b49b72013-03-29 12:37:16 -0700242
243 deferredUpdateScheduled = false;
Romain Guy96885eb2013-03-26 15:05:58 -0700244}
245
Romain Guye93482f2013-06-17 13:14:51 -0700246void Layer::cancelDefer() {
Chris Craikd41c4d82015-01-05 15:51:13 -0800247 renderNode = nullptr;
Romain Guye93482f2013-06-17 13:14:51 -0700248 deferredUpdateScheduled = false;
Sangkyu Lee441cc422015-12-11 16:05:10 +0900249 deferredList.reset(nullptr);
Romain Guye93482f2013-06-17 13:14:51 -0700250}
251
Romain Guy96885eb2013-03-26 15:05:58 -0700252void Layer::flush() {
Chris Craik8c6e17c2013-06-17 13:02:12 -0700253 // renderer is checked as layer may be destroyed/put in layer cache with flush scheduled
254 if (deferredList && renderer) {
Chris Craik70850ea2014-11-18 10:49:23 -0800255 ATRACE_LAYER_WORK("Issue");
Chris Craikd41c4d82015-01-05 15:51:13 -0800256 renderer->startMark((renderNode.get() != nullptr) ? renderNode->getName() : "Layer");
Chris Craik70850ea2014-11-18 10:49:23 -0800257
Chris Craik64e445b2015-09-02 14:23:49 -0700258 renderer->prepareDirty(layer.getWidth(), layer.getHeight(),
259 dirtyRect.left, dirtyRect.top, dirtyRect.right, dirtyRect.bottom, !isBlend());
Romain Guy96885eb2013-03-26 15:05:58 -0700260
261 deferredList->flush(*renderer, dirtyRect);
262
263 renderer->finish();
Romain Guy96885eb2013-03-26 15:05:58 -0700264
265 dirtyRect.setEmpty();
Chris Craikd41c4d82015-01-05 15:51:13 -0800266 renderNode = nullptr;
Chris Craik70850ea2014-11-18 10:49:23 -0800267
268 renderer->endMark();
Romain Guy96885eb2013-03-26 15:05:58 -0700269 }
270}
271
Chris Craik69e5adf2014-08-14 13:34:01 -0700272void Layer::render(const OpenGLRenderer& rootRenderer) {
Chris Craik70850ea2014-11-18 10:49:23 -0800273 ATRACE_LAYER_WORK("Direct-Issue");
274
Chris Craik69e5adf2014-08-14 13:34:01 -0700275 updateLightPosFromRenderer(rootRenderer);
Chris Craik64e445b2015-09-02 14:23:49 -0700276 renderer->prepareDirty(layer.getWidth(), layer.getHeight(),
277 dirtyRect.left, dirtyRect.top, dirtyRect.right, dirtyRect.bottom, !isBlend());
Romain Guy02b49b72013-03-29 12:37:16 -0700278
Chris Craika7090e02014-06-20 16:01:00 -0700279 renderer->drawRenderNode(renderNode.get(), dirtyRect, RenderNode::kReplayFlag_ClipChildren);
Romain Guy02b49b72013-03-29 12:37:16 -0700280
281 renderer->finish();
Romain Guy02b49b72013-03-29 12:37:16 -0700282
283 dirtyRect.setEmpty();
284
285 deferredUpdateScheduled = false;
Chris Craikd41c4d82015-01-05 15:51:13 -0800286 renderNode = nullptr;
Romain Guy02b49b72013-03-29 12:37:16 -0700287}
288
John Reck0e89e2b2014-10-31 14:49:06 -0700289void Layer::postDecStrong() {
290 renderState.postDecStrong(this);
291}
292
Chet Haased15ebf22012-09-05 11:40:29 -0700293}; // namespace uirenderer
294}; // namespace android