1d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haase/*
2d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haase * Copyright (C) 2012 The Android Open Source Project
3d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haase *
4d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haase * Licensed under the Apache License, Version 2.0 (the "License");
5d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haase * you may not use this file except in compliance with the License.
6d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haase * You may obtain a copy of the License at
7d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haase *
8d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haase *      http://www.apache.org/licenses/LICENSE-2.0
9d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haase *
10d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haase * Unless required by applicable law or agreed to in writing, software
11d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haase * distributed under the License is distributed on an "AS IS" BASIS,
12d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haase * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haase * See the License for the specific language governing permissions and
14d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haase * limitations under the License.
15d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haase */
16d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haase
1765fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik#include "Layer.h"
18d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haase
19113e0824d6bddf4376240681f9cf6a2deded9498John Reck#include "Caches.h"
2096885eb480c5e0526fe2f77d30f6e551f3f3ceabRomain Guy#include "DeferredDisplayList.h"
2198d3a64ffa13596e3ea9125bbff40c51ec96bd8dChet Haase#include "LayerRenderer.h"
22d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haase#include "OpenGLRenderer.h"
23113e0824d6bddf4376240681f9cf6a2deded9498John Reck#include "RenderNode.h"
2465fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik#include "renderstate/RenderState.h"
2570850ea258cbf91477efa57a1f1a23cc0044cc93Chris Craik#include "utils/TraceUtils.h"
2670850ea258cbf91477efa57a1f1a23cc0044cc93Chris Craik
2765fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik#include <utils/Log.h>
2865fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik
2970850ea258cbf91477efa57a1f1a23cc0044cc93Chris Craik#define ATRACE_LAYER_WORK(label) \
3070850ea258cbf91477efa57a1f1a23cc0044cc93Chris Craik    ATRACE_FORMAT("%s HW Layer DisplayList %s %ux%u", \
3170850ea258cbf91477efa57a1f1a23cc0044cc93Chris Craik            label, \
3270850ea258cbf91477efa57a1f1a23cc0044cc93Chris Craik            (renderNode.get() != NULL) ? renderNode->getName() : "", \
3370850ea258cbf91477efa57a1f1a23cc0044cc93Chris Craik            getWidth(), getHeight())
34d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haase
35d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haasenamespace android {
36d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haasenamespace uirenderer {
37d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haase
38e5c6584a402fb3b1fe0507e4e00e601bec8f1bbcChris CraikLayer::Layer(Type layerType, RenderState& renderState, uint32_t layerWidth, uint32_t layerHeight)
3938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck        : GpuMemoryTracker(GpuObjectType::Layer)
4038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck        , state(State::Uncached)
41bfd1cd620991ac2fa9202fdce6c00ec47d071935Chris Craik        , caches(Caches::getInstance())
423b20251a355c88193c439f928a84ae69483fb488John Reck        , renderState(renderState)
438a226d24b8b2fde4c855d0051cb7bfc5b5813c36Chris Craik        , texture(caches)
448a226d24b8b2fde4c855d0051cb7bfc5b5813c36Chris Craik        , type(layerType) {
450e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck    // TODO: This is a violation of Android's typical ref counting, but it
460e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck    // preserves the old inc/dec ref locations. This should be changed...
47d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    incStrong(nullptr);
48603f6de35f21d74ae242d52d501f4f5c25ff4f4cChet Haase    renderTarget = GL_TEXTURE_2D;
4938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck    texture.mWidth = layerWidth;
5038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck    texture.mHeight = layerHeight;
510e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck    renderState.registerLayer(this);
52603f6de35f21d74ae242d52d501f4f5c25ff4f4cChet Haase}
53603f6de35f21d74ae242d52d501f4f5c25ff4f4cChet Haase
54d15ebf25c595b855f6978d0600218e3ea5f31e92Chet HaaseLayer::~Layer() {
550e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck    renderState.unregisterLayer(this);
5676d3a1b8d035d27bc80b0f2fc480a903bd001514Derek Sollenberger    SkSafeUnref(colorFilter);
5757998017ff137f7d4ec33df21b6596141f8c4547John Reck
5838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck    if (stencil || fbo || texture.mId) {
5957998017ff137f7d4ec33df21b6596141f8c4547John Reck        removeFbo();
6038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck        texture.deleteTexture();
6157998017ff137f7d4ec33df21b6596141f8c4547John Reck    }
6296885eb480c5e0526fe2f77d30f6e551f3f3ceabRomain Guy
6396885eb480c5e0526fe2f77d30f6e551f3f3ceabRomain Guy    delete[] mesh;
6497dc9172b0e58979c63de0dedbab656399a62281Romain Guy}
6597dc9172b0e58979c63de0dedbab656399a62281Romain Guy
6657998017ff137f7d4ec33df21b6596141f8c4547John Reckvoid Layer::onGlContextLost() {
6757998017ff137f7d4ec33df21b6596141f8c4547John Reck    removeFbo();
6838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck    texture.deleteTexture();
6957998017ff137f7d4ec33df21b6596141f8c4547John Reck}
7057998017ff137f7d4ec33df21b6596141f8c4547John Reck
712055abaa0a590c35e27e1ae2e7d7cfccdfb98b59Romain Guyuint32_t Layer::computeIdealWidth(uint32_t layerWidth) {
722055abaa0a590c35e27e1ae2e7d7cfccdfb98b59Romain Guy    return uint32_t(ceilf(layerWidth / float(LAYER_SIZE)) * LAYER_SIZE);
732055abaa0a590c35e27e1ae2e7d7cfccdfb98b59Romain Guy}
742055abaa0a590c35e27e1ae2e7d7cfccdfb98b59Romain Guy
752055abaa0a590c35e27e1ae2e7d7cfccdfb98b59Romain Guyuint32_t Layer::computeIdealHeight(uint32_t layerHeight) {
762055abaa0a590c35e27e1ae2e7d7cfccdfb98b59Romain Guy    return uint32_t(ceilf(layerHeight / float(LAYER_SIZE)) * LAYER_SIZE);
772055abaa0a590c35e27e1ae2e7d7cfccdfb98b59Romain Guy}
782055abaa0a590c35e27e1ae2e7d7cfccdfb98b59Romain Guy
79668f0e38ef0277d55d3118af37e17b8c435df85cJohn Reckvoid Layer::requireRenderer() {
80668f0e38ef0277d55d3118af37e17b8c435df85cJohn Reck    if (!renderer) {
8151d6a3db97bdd5315f1a17a4b447d10a92217b98Chris Craik        renderer.reset(new LayerRenderer(renderState, this));
82668f0e38ef0277d55d3118af37e17b8c435df85cJohn Reck        renderer->initProperties();
83668f0e38ef0277d55d3118af37e17b8c435df85cJohn Reck    }
84668f0e38ef0277d55d3118af37e17b8c435df85cJohn Reck}
85668f0e38ef0277d55d3118af37e17b8c435df85cJohn Reck
8669e5adffb19135d51bde8e458f4907d7265f3e23Chris Craikvoid Layer::updateLightPosFromRenderer(const OpenGLRenderer& rootRenderer) {
8769e5adffb19135d51bde8e458f4907d7265f3e23Chris Craik    if (renderer && rendererLightPosDirty) {
8869e5adffb19135d51bde8e458f4907d7265f3e23Chris Craik        // re-init renderer's light position, based upon last cached location in window
8969e5adffb19135d51bde8e458f4907d7265f3e23Chris Craik        Vector3 lightPos = rootRenderer.getLightCenter();
9069e5adffb19135d51bde8e458f4907d7265f3e23Chris Craik        cachedInvTransformInWindow.mapPoint3d(lightPos);
9150210d912925aef14e4ce69be82e4949122a3cd9Alan Viverette        renderer->initLight(rootRenderer.getLightRadius(),
9250210d912925aef14e4ce69be82e4949122a3cd9Alan Viverette                rootRenderer.getAmbientShadowAlpha(),
9350210d912925aef14e4ce69be82e4949122a3cd9Alan Viverette                rootRenderer.getSpotShadowAlpha());
9450210d912925aef14e4ce69be82e4949122a3cd9Alan Viverette        renderer->setLightCenter(lightPos);
9569e5adffb19135d51bde8e458f4907d7265f3e23Chris Craik        rendererLightPosDirty = false;
9669e5adffb19135d51bde8e458f4907d7265f3e23Chris Craik    }
9769e5adffb19135d51bde8e458f4907d7265f3e23Chris Craik}
9869e5adffb19135d51bde8e458f4907d7265f3e23Chris Craik
992055abaa0a590c35e27e1ae2e7d7cfccdfb98b59Romain Guybool Layer::resize(const uint32_t width, const uint32_t height) {
1002055abaa0a590c35e27e1ae2e7d7cfccdfb98b59Romain Guy    uint32_t desiredWidth = computeIdealWidth(width);
1012055abaa0a590c35e27e1ae2e7d7cfccdfb98b59Romain Guy    uint32_t desiredHeight = computeIdealWidth(height);
1022055abaa0a590c35e27e1ae2e7d7cfccdfb98b59Romain Guy
1032055abaa0a590c35e27e1ae2e7d7cfccdfb98b59Romain Guy    if (desiredWidth <= getWidth() && desiredHeight <= getHeight()) {
1042055abaa0a590c35e27e1ae2e7d7cfccdfb98b59Romain Guy        return true;
1052055abaa0a590c35e27e1ae2e7d7cfccdfb98b59Romain Guy    }
1062055abaa0a590c35e27e1ae2e7d7cfccdfb98b59Romain Guy
107ec4cefc152749f9805d3a971a4a395687b5a3831John Reck    ATRACE_NAME("resizeLayer");
108ec4cefc152749f9805d3a971a4a395687b5a3831John Reck
1098aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy    const uint32_t maxTextureSize = caches.maxTextureSize;
110ce4a7dfc516ee61301e9af91fad17ca1320efaabRomain Guy    if (desiredWidth > maxTextureSize || desiredHeight > maxTextureSize) {
111ce4a7dfc516ee61301e9af91fad17ca1320efaabRomain Guy        ALOGW("Layer exceeds max. dimensions supported by the GPU (%dx%d, max=%dx%d)",
112ce4a7dfc516ee61301e9af91fad17ca1320efaabRomain Guy                desiredWidth, desiredHeight, maxTextureSize, maxTextureSize);
113ce4a7dfc516ee61301e9af91fad17ca1320efaabRomain Guy        return false;
114ce4a7dfc516ee61301e9af91fad17ca1320efaabRomain Guy    }
115ce4a7dfc516ee61301e9af91fad17ca1320efaabRomain Guy
1162055abaa0a590c35e27e1ae2e7d7cfccdfb98b59Romain Guy    uint32_t oldWidth = getWidth();
1172055abaa0a590c35e27e1ae2e7d7cfccdfb98b59Romain Guy    uint32_t oldHeight = getHeight();
1182055abaa0a590c35e27e1ae2e7d7cfccdfb98b59Romain Guy
1192055abaa0a590c35e27e1ae2e7d7cfccdfb98b59Romain Guy    setSize(desiredWidth, desiredHeight);
1202055abaa0a590c35e27e1ae2e7d7cfccdfb98b59Romain Guy
1212055abaa0a590c35e27e1ae2e7d7cfccdfb98b59Romain Guy    if (fbo) {
12244eb2c00861098dd3e2950d923646814b4cc57c2Chris Craik        caches.textureState().activateTexture(0);
1232055abaa0a590c35e27e1ae2e7d7cfccdfb98b59Romain Guy        bindTexture();
1240908764b2b3cf5075df4178a5f0a8547dcb7b317Romain Guy        allocateTexture();
1252055abaa0a590c35e27e1ae2e7d7cfccdfb98b59Romain Guy
1262055abaa0a590c35e27e1ae2e7d7cfccdfb98b59Romain Guy        if (glGetError() != GL_NO_ERROR) {
1272055abaa0a590c35e27e1ae2e7d7cfccdfb98b59Romain Guy            setSize(oldWidth, oldHeight);
1282055abaa0a590c35e27e1ae2e7d7cfccdfb98b59Romain Guy            return false;
1292055abaa0a590c35e27e1ae2e7d7cfccdfb98b59Romain Guy        }
1302055abaa0a590c35e27e1ae2e7d7cfccdfb98b59Romain Guy    }
1312055abaa0a590c35e27e1ae2e7d7cfccdfb98b59Romain Guy
1322055abaa0a590c35e27e1ae2e7d7cfccdfb98b59Romain Guy    if (stencil) {
1333bbacf27c0be1bae4e4483577fc89ae3113abe5dRomain Guy        stencil->bind();
1343bbacf27c0be1bae4e4483577fc89ae3113abe5dRomain Guy        stencil->resize(desiredWidth, desiredHeight);
1352055abaa0a590c35e27e1ae2e7d7cfccdfb98b59Romain Guy
1362055abaa0a590c35e27e1ae2e7d7cfccdfb98b59Romain Guy        if (glGetError() != GL_NO_ERROR) {
1372055abaa0a590c35e27e1ae2e7d7cfccdfb98b59Romain Guy            setSize(oldWidth, oldHeight);
1382055abaa0a590c35e27e1ae2e7d7cfccdfb98b59Romain Guy            return false;
1392055abaa0a590c35e27e1ae2e7d7cfccdfb98b59Romain Guy        }
1402055abaa0a590c35e27e1ae2e7d7cfccdfb98b59Romain Guy    }
1412055abaa0a590c35e27e1ae2e7d7cfccdfb98b59Romain Guy
1422055abaa0a590c35e27e1ae2e7d7cfccdfb98b59Romain Guy    return true;
1432055abaa0a590c35e27e1ae2e7d7cfccdfb98b59Romain Guy}
1442055abaa0a590c35e27e1ae2e7d7cfccdfb98b59Romain Guy
1458ce00301a023eecaeb8891ce906f67b513ebb42aRomain Guyvoid Layer::removeFbo(bool flush) {
1468ce00301a023eecaeb8891ce906f67b513ebb42aRomain Guy    if (stencil) {
1473b20251a355c88193c439f928a84ae69483fb488John Reck        GLuint previousFbo = renderState.getFramebuffer();
1483b20251a355c88193c439f928a84ae69483fb488John Reck        renderState.bindFramebuffer(fbo);
1498ce00301a023eecaeb8891ce906f67b513ebb42aRomain Guy        glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, 0);
1503b20251a355c88193c439f928a84ae69483fb488John Reck        renderState.bindFramebuffer(previousFbo);
1518ce00301a023eecaeb8891ce906f67b513ebb42aRomain Guy
1528aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy        caches.renderBufferCache.put(stencil);
153d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik        stencil = nullptr;
1548ce00301a023eecaeb8891ce906f67b513ebb42aRomain Guy    }
1558ce00301a023eecaeb8891ce906f67b513ebb42aRomain Guy
15698d3a64ffa13596e3ea9125bbff40c51ec96bd8dChet Haase    if (fbo) {
1573b20251a355c88193c439f928a84ae69483fb488John Reck        if (flush) LayerRenderer::flushLayer(renderState, this);
158818c9fbf1d76d5df19253ba4eb964efa939ec9ecChris Craik        renderState.deleteFramebuffer(fbo);
15998d3a64ffa13596e3ea9125bbff40c51ec96bd8dChet Haase        fbo = 0;
16056257aff8a55c847be72be9924c392033fd8151dDave Burke    }
16156257aff8a55c847be72be9924c392033fd8151dDave Burke}
16256257aff8a55c847be72be9924c392033fd8151dDave Burke
163a7090e0cfd7c719a6d4c03aae34f5db98754cbddChris Craikvoid Layer::updateDeferred(RenderNode* renderNode, int left, int top, int right, int bottom) {
164087bc0c14bdccf7c258dce0cdef46a69a839b427John Reck    requireRenderer();
165a7090e0cfd7c719a6d4c03aae34f5db98754cbddChris Craik    this->renderNode = renderNode;
166087bc0c14bdccf7c258dce0cdef46a69a839b427John Reck    const Rect r(left, top, right, bottom);
167087bc0c14bdccf7c258dce0cdef46a69a839b427John Reck    dirtyRect.unionWith(r);
168087bc0c14bdccf7c258dce0cdef46a69a839b427John Reck    deferredUpdateScheduled = true;
169087bc0c14bdccf7c258dce0cdef46a69a839b427John Reck}
170087bc0c14bdccf7c258dce0cdef46a69a839b427John Reck
171674554fc36932ca50b15bba41ac6f650254d4e72Derek Sollenbergervoid Layer::setPaint(const SkPaint* paint) {
172bf6f0f260886a04a1680c7f9917124a751322ca4Chris Craik    alpha = PaintUtils::getAlphaDirect(paint);
173bf6f0f260886a04a1680c7f9917124a751322ca4Chris Craik    mode = PaintUtils::getXfermodeDirect(paint);
174d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    setColorFilter((paint) ? paint->getColorFilter() : nullptr);
175d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haase}
176d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haase
17776d3a1b8d035d27bc80b0f2fc480a903bd001514Derek Sollenbergervoid Layer::setColorFilter(SkColorFilter* filter) {
17876d3a1b8d035d27bc80b0f2fc480a903bd001514Derek Sollenberger    SkRefCnt_SafeAssign(colorFilter, filter);
1798aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy}
1808aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy
1818aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guyvoid Layer::bindTexture() const {
18238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck    if (texture.mId) {
18338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck        caches.textureState().bindTexture(renderTarget, texture.mId);
1848aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy    }
1858aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy}
1868aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy
1878aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guyvoid Layer::bindStencilRenderBuffer() const {
1888aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy    if (stencil) {
1898aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy        stencil->bind();
1908aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy    }
1918aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy}
1928aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy
1938aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guyvoid Layer::generateTexture() {
19438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck    if (!texture.mId) {
19538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck        glGenTextures(1, &texture.mId);
1968aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy    }
1978aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy}
1988aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy
1998aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guyvoid Layer::clearTexture() {
200db009173f800bb0d626c42786b5bd0f57cc6545aJohn Reck    // There's a rare possibility that Caches could have been destroyed already
201db009173f800bb0d626c42786b5bd0f57cc6545aJohn Reck    // since this method is queued up as a task.
202db009173f800bb0d626c42786b5bd0f57cc6545aJohn Reck    // Since this is a reset method, treat this as non-fatal.
203db009173f800bb0d626c42786b5bd0f57cc6545aJohn Reck    if (caches.isInitialized()) {
204db009173f800bb0d626c42786b5bd0f57cc6545aJohn Reck        caches.textureState().unbindTexture(texture.mId);
205db009173f800bb0d626c42786b5bd0f57cc6545aJohn Reck    }
20638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck    texture.mId = 0;
2078aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy}
2088aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy
2098aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guyvoid Layer::allocateTexture() {
2108aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy#if DEBUG_LAYERS
2118aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy    ALOGD("  Allocate layer: %dx%d", getWidth(), getHeight());
2128aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy#endif
21338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck    if (texture.mId) {
21438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck        texture.updateSize(getWidth(), getHeight(), GL_RGBA);
2158aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy        glTexImage2D(renderTarget, 0, GL_RGBA, getWidth(), getHeight(), 0,
216d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik                GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
217d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haase    }
218d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haase}
219d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haase
22069e5adffb19135d51bde8e458f4907d7265f3e23Chris Craikvoid Layer::defer(const OpenGLRenderer& rootRenderer) {
22170850ea258cbf91477efa57a1f1a23cc0044cc93Chris Craik    ATRACE_LAYER_WORK("Optimize");
22270850ea258cbf91477efa57a1f1a23cc0044cc93Chris Craik
22369e5adffb19135d51bde8e458f4907d7265f3e23Chris Craik    updateLightPosFromRenderer(rootRenderer);
22496885eb480c5e0526fe2f77d30f6e551f3f3ceabRomain Guy    const float width = layer.getWidth();
22596885eb480c5e0526fe2f77d30f6e551f3f3ceabRomain Guy    const float height = layer.getHeight();
22696885eb480c5e0526fe2f77d30f6e551f3f3ceabRomain Guy
22796885eb480c5e0526fe2f77d30f6e551f3f3ceabRomain Guy    if (dirtyRect.isEmpty() || (dirtyRect.left <= 0 && dirtyRect.top <= 0 &&
22896885eb480c5e0526fe2f77d30f6e551f3f3ceabRomain Guy            dirtyRect.right >= width && dirtyRect.bottom >= height)) {
22996885eb480c5e0526fe2f77d30f6e551f3f3ceabRomain Guy        dirtyRect.set(0, 0, width, height);
23096885eb480c5e0526fe2f77d30f6e551f3f3ceabRomain Guy    }
23196885eb480c5e0526fe2f77d30f6e551f3f3ceabRomain Guy
23251d6a3db97bdd5315f1a17a4b447d10a92217b98Chris Craik    deferredList.reset(new DeferredDisplayList(dirtyRect));
233f57776b2d195f0937906eb88b777bb55ccc36967Chris Craik
23428ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik    DeferStateStruct deferredState(*deferredList, *renderer,
235e18264b079481a244b30e3f71012c53bbd861f92John Reck            RenderNode::kReplayFlag_ClipChildren);
23628ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik
23764e445bf74bee2098781d608cedfd723d8cc88d3Chris Craik    renderer->setupFrameState(width, height, dirtyRect.left, dirtyRect.top,
23896885eb480c5e0526fe2f77d30f6e551f3f3ceabRomain Guy            dirtyRect.right, dirtyRect.bottom, !isBlend());
23996885eb480c5e0526fe2f77d30f6e551f3f3ceabRomain Guy
240a7090e0cfd7c719a6d4c03aae34f5db98754cbddChris Craik    renderNode->computeOrdering();
241a7090e0cfd7c719a6d4c03aae34f5db98754cbddChris Craik    renderNode->defer(deferredState, 0);
24202b49b70ede0b9eb760ff334823aee1d9520ed85Romain Guy
24302b49b70ede0b9eb760ff334823aee1d9520ed85Romain Guy    deferredUpdateScheduled = false;
24496885eb480c5e0526fe2f77d30f6e551f3f3ceabRomain Guy}
24596885eb480c5e0526fe2f77d30f6e551f3f3ceabRomain Guy
246e93482f5eac3df581d57e64c2a771a96aa868585Romain Guyvoid Layer::cancelDefer() {
247d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    renderNode = nullptr;
248e93482f5eac3df581d57e64c2a771a96aa868585Romain Guy    deferredUpdateScheduled = false;
249441cc421b51377ec27bf97ed690c63aa5509ae90Sangkyu Lee    deferredList.reset(nullptr);
250e93482f5eac3df581d57e64c2a771a96aa868585Romain Guy}
251e93482f5eac3df581d57e64c2a771a96aa868585Romain Guy
25296885eb480c5e0526fe2f77d30f6e551f3f3ceabRomain Guyvoid Layer::flush() {
2538c6e17c2a9b0ad7864a261cc9a30b9623e20bdcbChris Craik    // renderer is checked as layer may be destroyed/put in layer cache with flush scheduled
2548c6e17c2a9b0ad7864a261cc9a30b9623e20bdcbChris Craik    if (deferredList && renderer) {
25570850ea258cbf91477efa57a1f1a23cc0044cc93Chris Craik        ATRACE_LAYER_WORK("Issue");
256d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik        renderer->startMark((renderNode.get() != nullptr) ? renderNode->getName() : "Layer");
25770850ea258cbf91477efa57a1f1a23cc0044cc93Chris Craik
25864e445bf74bee2098781d608cedfd723d8cc88d3Chris Craik        renderer->prepareDirty(layer.getWidth(), layer.getHeight(),
25964e445bf74bee2098781d608cedfd723d8cc88d3Chris Craik                dirtyRect.left, dirtyRect.top, dirtyRect.right, dirtyRect.bottom, !isBlend());
26096885eb480c5e0526fe2f77d30f6e551f3f3ceabRomain Guy
26196885eb480c5e0526fe2f77d30f6e551f3f3ceabRomain Guy        deferredList->flush(*renderer, dirtyRect);
26296885eb480c5e0526fe2f77d30f6e551f3f3ceabRomain Guy
26396885eb480c5e0526fe2f77d30f6e551f3f3ceabRomain Guy        renderer->finish();
26496885eb480c5e0526fe2f77d30f6e551f3f3ceabRomain Guy
26596885eb480c5e0526fe2f77d30f6e551f3f3ceabRomain Guy        dirtyRect.setEmpty();
266d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik        renderNode = nullptr;
26770850ea258cbf91477efa57a1f1a23cc0044cc93Chris Craik
26870850ea258cbf91477efa57a1f1a23cc0044cc93Chris Craik        renderer->endMark();
26996885eb480c5e0526fe2f77d30f6e551f3f3ceabRomain Guy    }
27096885eb480c5e0526fe2f77d30f6e551f3f3ceabRomain Guy}
27196885eb480c5e0526fe2f77d30f6e551f3f3ceabRomain Guy
27269e5adffb19135d51bde8e458f4907d7265f3e23Chris Craikvoid Layer::render(const OpenGLRenderer& rootRenderer) {
27370850ea258cbf91477efa57a1f1a23cc0044cc93Chris Craik    ATRACE_LAYER_WORK("Direct-Issue");
27470850ea258cbf91477efa57a1f1a23cc0044cc93Chris Craik
27569e5adffb19135d51bde8e458f4907d7265f3e23Chris Craik    updateLightPosFromRenderer(rootRenderer);
27664e445bf74bee2098781d608cedfd723d8cc88d3Chris Craik    renderer->prepareDirty(layer.getWidth(), layer.getHeight(),
27764e445bf74bee2098781d608cedfd723d8cc88d3Chris Craik            dirtyRect.left, dirtyRect.top, dirtyRect.right, dirtyRect.bottom, !isBlend());
27802b49b70ede0b9eb760ff334823aee1d9520ed85Romain Guy
279a7090e0cfd7c719a6d4c03aae34f5db98754cbddChris Craik    renderer->drawRenderNode(renderNode.get(), dirtyRect, RenderNode::kReplayFlag_ClipChildren);
28002b49b70ede0b9eb760ff334823aee1d9520ed85Romain Guy
28102b49b70ede0b9eb760ff334823aee1d9520ed85Romain Guy    renderer->finish();
28202b49b70ede0b9eb760ff334823aee1d9520ed85Romain Guy
28302b49b70ede0b9eb760ff334823aee1d9520ed85Romain Guy    dirtyRect.setEmpty();
28402b49b70ede0b9eb760ff334823aee1d9520ed85Romain Guy
28502b49b70ede0b9eb760ff334823aee1d9520ed85Romain Guy    deferredUpdateScheduled = false;
286d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    renderNode = nullptr;
28702b49b70ede0b9eb760ff334823aee1d9520ed85Romain Guy}
28802b49b70ede0b9eb760ff334823aee1d9520ed85Romain Guy
2890e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reckvoid Layer::postDecStrong() {
2900e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck    renderState.postDecStrong(this);
2910e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck}
2920e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck
293d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haase}; // namespace uirenderer
294d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haase}; // namespace android
295