1b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik/*
2b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik * Copyright (C) 2015 The Android Open Source Project
3b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik *
4b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik * Licensed under the Apache License, Version 2.0 (the "License");
5b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik * you may not use this file except in compliance with the License.
6b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik * You may obtain a copy of the License at
7b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik *
8b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik *      http://www.apache.org/licenses/LICENSE-2.0
9b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik *
10b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik * Unless required by applicable law or agreed to in writing, software
11b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik * distributed under the License is distributed on an "AS IS" BASIS,
12b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik * See the License for the specific language governing permissions and
14b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik * limitations under the License.
15b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik */
16b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
17b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik#include "BakedOpRenderer.h"
18b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
19b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik#include "Caches.h"
20b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik#include "Glop.h"
21b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik#include "GlopBuilder.h"
229fded232a9548a304e0145011df8849fba0dcda7Chris Craik#include "renderstate/OffscreenBufferPool.h"
23b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik#include "renderstate/RenderState.h"
24b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik#include "utils/GLUtils.h"
259fded232a9548a304e0145011df8849fba0dcda7Chris Craik#include "VertexBuffer.h"
26b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
27a1717271caac5e8ea3808c331d4141ac01a42134Chris Craik#include <algorithm>
28a1717271caac5e8ea3808c331d4141ac01a42134Chris Craik
29b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craiknamespace android {
30b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craiknamespace uirenderer {
31b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
32d3daa3198e2212c985c634821682d5819346b653Chris CraikOffscreenBuffer* BakedOpRenderer::startTemporaryLayer(uint32_t width, uint32_t height) {
339fded232a9548a304e0145011df8849fba0dcda7Chris Craik    LOG_ALWAYS_FATAL_IF(mRenderTarget.offscreenBuffer, "already has layer...");
349fded232a9548a304e0145011df8849fba0dcda7Chris Craik
359fded232a9548a304e0145011df8849fba0dcda7Chris Craik    OffscreenBuffer* buffer = mRenderState.layerPool().get(mRenderState, width, height);
3698787e6c9b2c10b1ab7820bdac168686025b924aChris Craik    startRepaintLayer(buffer, Rect(width, height));
370b7e8245db728d127ada698be63d78b33fc6e4daChris Craik    return buffer;
380b7e8245db728d127ada698be63d78b33fc6e4daChris Craik}
390b7e8245db728d127ada698be63d78b33fc6e4daChris Craik
4074af6e282f8a8f75928a071e8200039517cf5c12Chris Craikvoid BakedOpRenderer::recycleTemporaryLayer(OffscreenBuffer* offscreenBuffer) {
4174af6e282f8a8f75928a071e8200039517cf5c12Chris Craik    mRenderState.layerPool().putOrDelete(offscreenBuffer);
4274af6e282f8a8f75928a071e8200039517cf5c12Chris Craik}
4374af6e282f8a8f75928a071e8200039517cf5c12Chris Craik
4498787e6c9b2c10b1ab7820bdac168686025b924aChris Craikvoid BakedOpRenderer::startRepaintLayer(OffscreenBuffer* offscreenBuffer, const Rect& repaintRect) {
45d3daa3198e2212c985c634821682d5819346b653Chris Craik    LOG_ALWAYS_FATAL_IF(mRenderTarget.offscreenBuffer, "already has layer...");
46d3daa3198e2212c985c634821682d5819346b653Chris Craik
4702806288d1c56475413888a934c796e6e4eb11c5Chris Craik    // subtract repaintRect from region, since it will be regenerated
4802806288d1c56475413888a934c796e6e4eb11c5Chris Craik    if (repaintRect.contains(0, 0,
4902806288d1c56475413888a934c796e6e4eb11c5Chris Craik                offscreenBuffer->viewportWidth, offscreenBuffer->viewportHeight)) {
5002806288d1c56475413888a934c796e6e4eb11c5Chris Craik        // repaint full layer, so throw away entire region
5102806288d1c56475413888a934c796e6e4eb11c5Chris Craik        offscreenBuffer->region.clear();
5202806288d1c56475413888a934c796e6e4eb11c5Chris Craik    } else {
5302806288d1c56475413888a934c796e6e4eb11c5Chris Craik        offscreenBuffer->region.subtractSelf(android::Rect(repaintRect.left, repaintRect.top,
5402806288d1c56475413888a934c796e6e4eb11c5Chris Craik                repaintRect.right, repaintRect.bottom));
5502806288d1c56475413888a934c796e6e4eb11c5Chris Craik    }
5602806288d1c56475413888a934c796e6e4eb11c5Chris Craik
570b7e8245db728d127ada698be63d78b33fc6e4daChris Craik    mRenderTarget.offscreenBuffer = offscreenBuffer;
5837413289478a965336239c731ebfea37ac4dde28Chris Craik    mRenderTarget.offscreenBuffer->hasRenderedSinceRepaint = false;
59818c9fbf1d76d5df19253ba4eb964efa939ec9ecChris Craik
605854b34881b1a747ac80b5077869ef270a92b1f4Chris Craik    // create and bind framebuffer
610b8d0677be2289bbc9e0b48c0878fb67d1cc0ebdJohn Reck    mRenderTarget.frameBufferId = mRenderState.createFramebuffer();
625854b34881b1a747ac80b5077869ef270a92b1f4Chris Craik    mRenderState.bindFramebuffer(mRenderTarget.frameBufferId);
63818c9fbf1d76d5df19253ba4eb964efa939ec9ecChris Craik
64818c9fbf1d76d5df19253ba4eb964efa939ec9ecChris Craik    // attach the texture to the FBO
65818c9fbf1d76d5df19253ba4eb964efa939ec9ecChris Craik    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
6638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck            offscreenBuffer->texture.id(), 0);
67975591a7af883d866d86ab819e164c6004694744John Reck    GL_CHECKPOINT(LOW);
68975591a7af883d866d86ab819e164c6004694744John Reck
69d4fe4d3b30aaefcaaae6a6d1b8dc4bf59e034768Chris Craik    int status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
70d4fe4d3b30aaefcaaae6a6d1b8dc4bf59e034768Chris Craik    LOG_ALWAYS_FATAL_IF(status != GL_FRAMEBUFFER_COMPLETE,
71d4fe4d3b30aaefcaaae6a6d1b8dc4bf59e034768Chris Craik            "framebuffer incomplete, status %d, textureId %d, size %dx%d",
72d4fe4d3b30aaefcaaae6a6d1b8dc4bf59e034768Chris Craik            status,
73d4fe4d3b30aaefcaaae6a6d1b8dc4bf59e034768Chris Craik            offscreenBuffer->texture.id(),
74d4fe4d3b30aaefcaaae6a6d1b8dc4bf59e034768Chris Craik            offscreenBuffer->texture.width(),
75d4fe4d3b30aaefcaaae6a6d1b8dc4bf59e034768Chris Craik            offscreenBuffer->texture.height());
76818c9fbf1d76d5df19253ba4eb964efa939ec9ecChris Craik
77818c9fbf1d76d5df19253ba4eb964efa939ec9ecChris Craik    // Change the viewport & ortho projection
780b7e8245db728d127ada698be63d78b33fc6e4daChris Craik    setViewport(offscreenBuffer->viewportWidth, offscreenBuffer->viewportHeight);
7998787e6c9b2c10b1ab7820bdac168686025b924aChris Craik
8098787e6c9b2c10b1ab7820bdac168686025b924aChris Craik    clearColorBuffer(repaintRect);
81818c9fbf1d76d5df19253ba4eb964efa939ec9ecChris Craik}
82818c9fbf1d76d5df19253ba4eb964efa939ec9ecChris Craik
835854b34881b1a747ac80b5077869ef270a92b1f4Chris Craikvoid BakedOpRenderer::endLayer() {
84e5b50197e870aa6d22c3893f5d25f4279f06e5c3Chris Craik    if (mRenderTarget.stencil) {
85e5b50197e870aa6d22c3893f5d25f4279f06e5c3Chris Craik        // if stencil was used for clipping, detach it and return it to pool
86e5b50197e870aa6d22c3893f5d25f4279f06e5c3Chris Craik        glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, 0);
87975591a7af883d866d86ab819e164c6004694744John Reck        GL_CHECKPOINT(MODERATE);
88e5b50197e870aa6d22c3893f5d25f4279f06e5c3Chris Craik        mCaches.renderBufferCache.put(mRenderTarget.stencil);
89e5b50197e870aa6d22c3893f5d25f4279f06e5c3Chris Craik        mRenderTarget.stencil = nullptr;
90e5b50197e870aa6d22c3893f5d25f4279f06e5c3Chris Craik    }
91e4db79de127cfe961195f52907af8451026eaa20Chris Craik    mRenderTarget.lastStencilClip = nullptr;
92818c9fbf1d76d5df19253ba4eb964efa939ec9ecChris Craik
93e5b50197e870aa6d22c3893f5d25f4279f06e5c3Chris Craik    mRenderTarget.offscreenBuffer->updateMeshFromRegion();
94e5b50197e870aa6d22c3893f5d25f4279f06e5c3Chris Craik    mRenderTarget.offscreenBuffer = nullptr; // It's in drawLayerOp's hands now.
95e5b50197e870aa6d22c3893f5d25f4279f06e5c3Chris Craik
96818c9fbf1d76d5df19253ba4eb964efa939ec9ecChris Craik    // Detach the texture from the FBO
97818c9fbf1d76d5df19253ba4eb964efa939ec9ecChris Craik    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
98975591a7af883d866d86ab819e164c6004694744John Reck    GL_CHECKPOINT(LOW);
995854b34881b1a747ac80b5077869ef270a92b1f4Chris Craik    mRenderState.deleteFramebuffer(mRenderTarget.frameBufferId);
100e4db79de127cfe961195f52907af8451026eaa20Chris Craik    mRenderTarget.frameBufferId = 0;
101818c9fbf1d76d5df19253ba4eb964efa939ec9ecChris Craik}
102818c9fbf1d76d5df19253ba4eb964efa939ec9ecChris Craik
1037435eb148e72382126e9073183e881357bb38a8bChris CraikOffscreenBuffer* BakedOpRenderer::copyToLayer(const Rect& area) {
104ab3f8c2e3277af88935c8fa3bb7a36470d2b0116sergeyv    const uint32_t width = area.getWidth();
105ab3f8c2e3277af88935c8fa3bb7a36470d2b0116sergeyv    const uint32_t height = area.getHeight();
106ab3f8c2e3277af88935c8fa3bb7a36470d2b0116sergeyv    OffscreenBuffer* buffer = mRenderState.layerPool().get(mRenderState, width, height);
107ab3f8c2e3277af88935c8fa3bb7a36470d2b0116sergeyv    if (!area.isEmpty() && width != 0 && height != 0) {
1087435eb148e72382126e9073183e881357bb38a8bChris Craik        mCaches.textureState().activateTexture(0);
10938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck        mCaches.textureState().bindTexture(buffer->texture.id());
1107435eb148e72382126e9073183e881357bb38a8bChris Craik
1117435eb148e72382126e9073183e881357bb38a8bChris Craik        glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
112ab3f8c2e3277af88935c8fa3bb7a36470d2b0116sergeyv                area.left, mRenderTarget.viewportHeight - area.bottom, width, height);
1137435eb148e72382126e9073183e881357bb38a8bChris Craik    }
1147435eb148e72382126e9073183e881357bb38a8bChris Craik    return buffer;
1157435eb148e72382126e9073183e881357bb38a8bChris Craik}
1167435eb148e72382126e9073183e881357bb38a8bChris Craik
11798787e6c9b2c10b1ab7820bdac168686025b924aChris Craikvoid BakedOpRenderer::startFrame(uint32_t width, uint32_t height, const Rect& repaintRect) {
118e4db79de127cfe961195f52907af8451026eaa20Chris Craik    LOG_ALWAYS_FATAL_IF(mRenderTarget.frameBufferId != 0, "primary framebufferId must be 0");
1195854b34881b1a747ac80b5077869ef270a92b1f4Chris Craik    mRenderState.bindFramebuffer(0);
1205854b34881b1a747ac80b5077869ef270a92b1f4Chris Craik    setViewport(width, height);
121b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
1225854b34881b1a747ac80b5077869ef270a92b1f4Chris Craik    if (!mOpaque) {
12398787e6c9b2c10b1ab7820bdac168686025b924aChris Craik        clearColorBuffer(repaintRect);
124b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    }
125e4db79de127cfe961195f52907af8451026eaa20Chris Craik
126e4db79de127cfe961195f52907af8451026eaa20Chris Craik    mRenderState.debugOverdraw(true, true);
127b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
1285854b34881b1a747ac80b5077869ef270a92b1f4Chris Craik
129e4db79de127cfe961195f52907af8451026eaa20Chris Craikvoid BakedOpRenderer::endFrame(const Rect& repaintRect) {
130e4db79de127cfe961195f52907af8451026eaa20Chris Craik    if (CC_UNLIKELY(Properties::debugOverdraw)) {
131e4db79de127cfe961195f52907af8451026eaa20Chris Craik        ClipRect overdrawClip(repaintRect);
132e4db79de127cfe961195f52907af8451026eaa20Chris Craik        Rect viewportRect(mRenderTarget.viewportWidth, mRenderTarget.viewportHeight);
133e4db79de127cfe961195f52907af8451026eaa20Chris Craik        // overdraw visualization
134e4db79de127cfe961195f52907af8451026eaa20Chris Craik        for (int i = 1; i <= 4; i++) {
135e4db79de127cfe961195f52907af8451026eaa20Chris Craik            if (i < 4) {
136e4db79de127cfe961195f52907af8451026eaa20Chris Craik                // nth level of overdraw tests for n+1 draws per pixel
137e4db79de127cfe961195f52907af8451026eaa20Chris Craik                mRenderState.stencil().enableDebugTest(i + 1, false);
138e4db79de127cfe961195f52907af8451026eaa20Chris Craik            } else {
139e4db79de127cfe961195f52907af8451026eaa20Chris Craik                // 4th level tests for 4 or higher draws per pixel
140e4db79de127cfe961195f52907af8451026eaa20Chris Craik                mRenderState.stencil().enableDebugTest(4, true);
141e4db79de127cfe961195f52907af8451026eaa20Chris Craik            }
142e4db79de127cfe961195f52907af8451026eaa20Chris Craik
143e4db79de127cfe961195f52907af8451026eaa20Chris Craik            SkPaint paint;
144e4db79de127cfe961195f52907af8451026eaa20Chris Craik            paint.setColor(mCaches.getOverdrawColor(i));
145e4db79de127cfe961195f52907af8451026eaa20Chris Craik            Glop glop;
146e4db79de127cfe961195f52907af8451026eaa20Chris Craik            GlopBuilder(mRenderState, mCaches, &glop)
147e4db79de127cfe961195f52907af8451026eaa20Chris Craik                    .setRoundRectClipState(nullptr)
148e4db79de127cfe961195f52907af8451026eaa20Chris Craik                    .setMeshUnitQuad()
149e4db79de127cfe961195f52907af8451026eaa20Chris Craik                    .setFillPaint(paint, 1.0f)
150e4db79de127cfe961195f52907af8451026eaa20Chris Craik                    .setTransform(Matrix4::identity(), TransformFlags::None)
151e4db79de127cfe961195f52907af8451026eaa20Chris Craik                    .setModelViewMapUnitToRect(viewportRect)
152e4db79de127cfe961195f52907af8451026eaa20Chris Craik                    .build();
153e4db79de127cfe961195f52907af8451026eaa20Chris Craik            renderGlop(nullptr, &overdrawClip, glop);
154e4db79de127cfe961195f52907af8451026eaa20Chris Craik        }
155e4db79de127cfe961195f52907af8451026eaa20Chris Craik        mRenderState.stencil().disable();
156e4db79de127cfe961195f52907af8451026eaa20Chris Craik    }
157e4db79de127cfe961195f52907af8451026eaa20Chris Craik
1581dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik    // Note: we leave FBO 0 renderable here, for post-frame-content decoration
159b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
160b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
1615854b34881b1a747ac80b5077869ef270a92b1f4Chris Craikvoid BakedOpRenderer::setViewport(uint32_t width, uint32_t height) {
1625854b34881b1a747ac80b5077869ef270a92b1f4Chris Craik    mRenderTarget.viewportWidth = width;
1635854b34881b1a747ac80b5077869ef270a92b1f4Chris Craik    mRenderTarget.viewportHeight = height;
1645854b34881b1a747ac80b5077869ef270a92b1f4Chris Craik    mRenderTarget.orthoMatrix.loadOrtho(width, height);
1655854b34881b1a747ac80b5077869ef270a92b1f4Chris Craik
1665854b34881b1a747ac80b5077869ef270a92b1f4Chris Craik    mRenderState.setViewport(width, height);
1675854b34881b1a747ac80b5077869ef270a92b1f4Chris Craik    mRenderState.blend().syncEnabled();
1685854b34881b1a747ac80b5077869ef270a92b1f4Chris Craik}
1695854b34881b1a747ac80b5077869ef270a92b1f4Chris Craik
17098787e6c9b2c10b1ab7820bdac168686025b924aChris Craikvoid BakedOpRenderer::clearColorBuffer(const Rect& rect) {
17184ad6149db0e1dc98ed3778ee4fc2ab685765d9aChris Craik    if (rect.contains(Rect(mRenderTarget.viewportWidth, mRenderTarget.viewportHeight))) {
17298787e6c9b2c10b1ab7820bdac168686025b924aChris Craik        // Full viewport is being cleared - disable scissor
17398787e6c9b2c10b1ab7820bdac168686025b924aChris Craik        mRenderState.scissor().setEnabled(false);
17498787e6c9b2c10b1ab7820bdac168686025b924aChris Craik    } else {
17598787e6c9b2c10b1ab7820bdac168686025b924aChris Craik        // Requested rect is subset of viewport - scissor to it to avoid over-clearing
17698787e6c9b2c10b1ab7820bdac168686025b924aChris Craik        mRenderState.scissor().setEnabled(true);
17798787e6c9b2c10b1ab7820bdac168686025b924aChris Craik        mRenderState.scissor().set(rect.left, mRenderTarget.viewportHeight - rect.bottom,
17898787e6c9b2c10b1ab7820bdac168686025b924aChris Craik                rect.getWidth(), rect.getHeight());
17998787e6c9b2c10b1ab7820bdac168686025b924aChris Craik    }
18098787e6c9b2c10b1ab7820bdac168686025b924aChris Craik    glClear(GL_COLOR_BUFFER_BIT);
18198787e6c9b2c10b1ab7820bdac168686025b924aChris Craik    if (!mRenderTarget.frameBufferId) mHasDrawn = true;
18298787e6c9b2c10b1ab7820bdac168686025b924aChris Craik}
18398787e6c9b2c10b1ab7820bdac168686025b924aChris Craik
184ec4a4b13eae2241d1613890c1c1c096bed891845sergeyvTexture* BakedOpRenderer::getTexture(Bitmap* bitmap) {
185253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guy    return mCaches.textureCache.get(bitmap);
1865854b34881b1a747ac80b5077869ef270a92b1f4Chris Craik}
1875854b34881b1a747ac80b5077869ef270a92b1f4Chris Craik
1881dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craikvoid BakedOpRenderer::drawRects(const float* rects, int count, const SkPaint* paint) {
1891dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik    std::vector<Vertex> vertices;
1901dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik    vertices.reserve(count);
1911dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik    Vertex* vertex = vertices.data();
1921dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik
1931dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik    for (int index = 0; index < count; index += 4) {
1941dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik        float l = rects[index + 0];
1951dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik        float t = rects[index + 1];
1961dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik        float r = rects[index + 2];
1971dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik        float b = rects[index + 3];
1981dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik
1991dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik        Vertex::set(vertex++, l, t);
2001dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik        Vertex::set(vertex++, r, t);
2011dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik        Vertex::set(vertex++, l, b);
2021dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik        Vertex::set(vertex++, r, b);
2031dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik    }
2041dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik
2051dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik    LOG_ALWAYS_FATAL_IF(mRenderTarget.frameBufferId != 0, "decoration only supported for FBO 0");
2061dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik    // TODO: Currently assume full FBO damage, due to FrameInfoVisualizer::unionDirty.
20796bacd186e5381d27181cb3414003a74e2d2c3b7Chris Craik    // Should should scissor/set mHasDrawn safely.
2081dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik    mRenderState.scissor().setEnabled(false);
20996bacd186e5381d27181cb3414003a74e2d2c3b7Chris Craik    mHasDrawn = true;
2101dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik    Glop glop;
2111dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik    GlopBuilder(mRenderState, mCaches, &glop)
2121dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik            .setRoundRectClipState(nullptr)
2131dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik            .setMeshIndexedQuads(vertices.data(), count / 4)
2141dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik            .setFillPaint(*paint, 1.0f)
2151dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik            .setTransform(Matrix4::identity(), TransformFlags::None)
2161dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik            .setModelViewIdentityEmptyBounds()
2171dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik            .build();
2181dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik    mRenderState.render(glop, mRenderTarget.orthoMatrix);
2191dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik}
2201dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik
221e4db79de127cfe961195f52907af8451026eaa20Chris Craik// clears and re-fills stencil with provided rendertarget space quads,
222e4db79de127cfe961195f52907af8451026eaa20Chris Craik// and then put stencil into test mode
223e4db79de127cfe961195f52907af8451026eaa20Chris Craikvoid BakedOpRenderer::setupStencilQuads(std::vector<Vertex>& quadVertices,
224e4db79de127cfe961195f52907af8451026eaa20Chris Craik        int incrementThreshold) {
225e4db79de127cfe961195f52907af8451026eaa20Chris Craik    mRenderState.stencil().enableWrite(incrementThreshold);
226e4db79de127cfe961195f52907af8451026eaa20Chris Craik    mRenderState.stencil().clear();
227e4db79de127cfe961195f52907af8451026eaa20Chris Craik    Glop glop;
228e4db79de127cfe961195f52907af8451026eaa20Chris Craik    GlopBuilder(mRenderState, mCaches, &glop)
229e4db79de127cfe961195f52907af8451026eaa20Chris Craik            .setRoundRectClipState(nullptr)
230e4db79de127cfe961195f52907af8451026eaa20Chris Craik            .setMeshIndexedQuads(quadVertices.data(), quadVertices.size() / 4)
231e4db79de127cfe961195f52907af8451026eaa20Chris Craik            .setFillBlack()
232e4db79de127cfe961195f52907af8451026eaa20Chris Craik            .setTransform(Matrix4::identity(), TransformFlags::None)
233e4db79de127cfe961195f52907af8451026eaa20Chris Craik            .setModelViewIdentityEmptyBounds()
234e4db79de127cfe961195f52907af8451026eaa20Chris Craik            .build();
235e4db79de127cfe961195f52907af8451026eaa20Chris Craik    mRenderState.render(glop, mRenderTarget.orthoMatrix);
236e4db79de127cfe961195f52907af8451026eaa20Chris Craik    mRenderState.stencil().enableTest(incrementThreshold);
237e4db79de127cfe961195f52907af8451026eaa20Chris Craik}
238e4db79de127cfe961195f52907af8451026eaa20Chris Craik
239e4db79de127cfe961195f52907af8451026eaa20Chris Craikvoid BakedOpRenderer::setupStencilRectList(const ClipBase* clip) {
24015f046866cb650d78f55d03327cfa4a474fc9471Chris Craik    LOG_ALWAYS_FATAL_IF(clip->mode != ClipMode::RectangleList, "can't rectlist clip without rectlist");
241e4db79de127cfe961195f52907af8451026eaa20Chris Craik    auto&& rectList = reinterpret_cast<const ClipRectList*>(clip)->rectList;
242e4db79de127cfe961195f52907af8451026eaa20Chris Craik    int quadCount = rectList.getTransformedRectanglesCount();
243e4db79de127cfe961195f52907af8451026eaa20Chris Craik    std::vector<Vertex> rectangleVertices;
244e4db79de127cfe961195f52907af8451026eaa20Chris Craik    rectangleVertices.reserve(quadCount * 4);
245e4db79de127cfe961195f52907af8451026eaa20Chris Craik    for (int i = 0; i < quadCount; i++) {
246e4db79de127cfe961195f52907af8451026eaa20Chris Craik        const TransformedRectangle& tr(rectList.getTransformedRectangle(i));
247e4db79de127cfe961195f52907af8451026eaa20Chris Craik        const Matrix4& transform = tr.getTransform();
248e4db79de127cfe961195f52907af8451026eaa20Chris Craik        Rect bounds = tr.getBounds();
249e4db79de127cfe961195f52907af8451026eaa20Chris Craik        if (transform.rectToRect()) {
250e4db79de127cfe961195f52907af8451026eaa20Chris Craik            // If rectToRect, can simply map bounds before storing verts
251e4db79de127cfe961195f52907af8451026eaa20Chris Craik            transform.mapRect(bounds);
252e4db79de127cfe961195f52907af8451026eaa20Chris Craik            bounds.doIntersect(clip->rect);
253e4db79de127cfe961195f52907af8451026eaa20Chris Craik            if (bounds.isEmpty()) {
254e4db79de127cfe961195f52907af8451026eaa20Chris Craik                continue; // will be outside of scissor, skip
255e4db79de127cfe961195f52907af8451026eaa20Chris Craik            }
256e4db79de127cfe961195f52907af8451026eaa20Chris Craik        }
257e4db79de127cfe961195f52907af8451026eaa20Chris Craik
258e4db79de127cfe961195f52907af8451026eaa20Chris Craik        rectangleVertices.push_back(Vertex{bounds.left, bounds.top});
259e4db79de127cfe961195f52907af8451026eaa20Chris Craik        rectangleVertices.push_back(Vertex{bounds.right, bounds.top});
260e4db79de127cfe961195f52907af8451026eaa20Chris Craik        rectangleVertices.push_back(Vertex{bounds.left, bounds.bottom});
261e4db79de127cfe961195f52907af8451026eaa20Chris Craik        rectangleVertices.push_back(Vertex{bounds.right, bounds.bottom});
262e4db79de127cfe961195f52907af8451026eaa20Chris Craik
263e4db79de127cfe961195f52907af8451026eaa20Chris Craik        if (!transform.rectToRect()) {
264e4db79de127cfe961195f52907af8451026eaa20Chris Craik            // If not rectToRect, must map each point individually
265e4db79de127cfe961195f52907af8451026eaa20Chris Craik            for (auto cur = rectangleVertices.end() - 4; cur < rectangleVertices.end(); cur++) {
266e4db79de127cfe961195f52907af8451026eaa20Chris Craik                transform.mapPoint(cur->x, cur->y);
267e4db79de127cfe961195f52907af8451026eaa20Chris Craik            }
268e4db79de127cfe961195f52907af8451026eaa20Chris Craik        }
269e4db79de127cfe961195f52907af8451026eaa20Chris Craik    }
270e4db79de127cfe961195f52907af8451026eaa20Chris Craik    setupStencilQuads(rectangleVertices, rectList.getTransformedRectanglesCount());
271e4db79de127cfe961195f52907af8451026eaa20Chris Craik}
272e4db79de127cfe961195f52907af8451026eaa20Chris Craik
273e4db79de127cfe961195f52907af8451026eaa20Chris Craikvoid BakedOpRenderer::setupStencilRegion(const ClipBase* clip) {
27415f046866cb650d78f55d03327cfa4a474fc9471Chris Craik    LOG_ALWAYS_FATAL_IF(clip->mode != ClipMode::Region, "can't region clip without region");
275e4db79de127cfe961195f52907af8451026eaa20Chris Craik    auto&& region = reinterpret_cast<const ClipRegion*>(clip)->region;
276e4db79de127cfe961195f52907af8451026eaa20Chris Craik
277e4db79de127cfe961195f52907af8451026eaa20Chris Craik    std::vector<Vertex> regionVertices;
278e4db79de127cfe961195f52907af8451026eaa20Chris Craik    SkRegion::Cliperator it(region, clip->rect.toSkIRect());
279e4db79de127cfe961195f52907af8451026eaa20Chris Craik    while (!it.done()) {
280e4db79de127cfe961195f52907af8451026eaa20Chris Craik        const SkIRect& r = it.rect();
281e4db79de127cfe961195f52907af8451026eaa20Chris Craik        regionVertices.push_back(Vertex{(float)r.fLeft, (float)r.fTop});
282e4db79de127cfe961195f52907af8451026eaa20Chris Craik        regionVertices.push_back(Vertex{(float)r.fRight, (float)r.fTop});
283e4db79de127cfe961195f52907af8451026eaa20Chris Craik        regionVertices.push_back(Vertex{(float)r.fLeft, (float)r.fBottom});
284e4db79de127cfe961195f52907af8451026eaa20Chris Craik        regionVertices.push_back(Vertex{(float)r.fRight, (float)r.fBottom});
285e4db79de127cfe961195f52907af8451026eaa20Chris Craik        it.next();
286e4db79de127cfe961195f52907af8451026eaa20Chris Craik    }
287e4db79de127cfe961195f52907af8451026eaa20Chris Craik    setupStencilQuads(regionVertices, 0);
288e4db79de127cfe961195f52907af8451026eaa20Chris Craik}
289e4db79de127cfe961195f52907af8451026eaa20Chris Craik
290e4db79de127cfe961195f52907af8451026eaa20Chris Craikvoid BakedOpRenderer::prepareRender(const Rect* dirtyBounds, const ClipBase* clip) {
291b87eadda1818034ce03d85f30388384d1ac65916Chris Craik    // Prepare scissor (done before stencil, to simplify filling stencil)
29215c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    mRenderState.scissor().setEnabled(clip != nullptr);
29315c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    if (clip) {
294e4db79de127cfe961195f52907af8451026eaa20Chris Craik        mRenderState.scissor().set(mRenderTarget.viewportHeight, clip->rect);
295e5b50197e870aa6d22c3893f5d25f4279f06e5c3Chris Craik    }
296e5b50197e870aa6d22c3893f5d25f4279f06e5c3Chris Craik
297b87eadda1818034ce03d85f30388384d1ac65916Chris Craik    // If stencil may be used for clipping, enable it, fill it, or disable it as appropriate
298e5b50197e870aa6d22c3893f5d25f4279f06e5c3Chris Craik    if (CC_LIKELY(!Properties::debugOverdraw)) {
299e5b50197e870aa6d22c3893f5d25f4279f06e5c3Chris Craik        // only modify stencil mode and content when it's not used for overdraw visualization
300e5b50197e870aa6d22c3893f5d25f4279f06e5c3Chris Craik        if (CC_UNLIKELY(clip && clip->mode != ClipMode::Rectangle)) {
301e5b50197e870aa6d22c3893f5d25f4279f06e5c3Chris Craik            // NOTE: this pointer check is only safe for non-rect clips,
302e5b50197e870aa6d22c3893f5d25f4279f06e5c3Chris Craik            // since rect clips may be created on the stack
303e5b50197e870aa6d22c3893f5d25f4279f06e5c3Chris Craik            if (mRenderTarget.lastStencilClip != clip) {
304e5b50197e870aa6d22c3893f5d25f4279f06e5c3Chris Craik                // Stencil needed, but current stencil isn't up to date
305e5b50197e870aa6d22c3893f5d25f4279f06e5c3Chris Craik                mRenderTarget.lastStencilClip = clip;
306e5b50197e870aa6d22c3893f5d25f4279f06e5c3Chris Craik
307e5b50197e870aa6d22c3893f5d25f4279f06e5c3Chris Craik                if (mRenderTarget.frameBufferId != 0 && !mRenderTarget.stencil) {
308e5b50197e870aa6d22c3893f5d25f4279f06e5c3Chris Craik                    OffscreenBuffer* layer = mRenderTarget.offscreenBuffer;
309e5b50197e870aa6d22c3893f5d25f4279f06e5c3Chris Craik                    mRenderTarget.stencil = mCaches.renderBufferCache.get(
310e5b50197e870aa6d22c3893f5d25f4279f06e5c3Chris Craik                            Stencil::getLayerStencilFormat(),
31138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck                            layer->texture.width(), layer->texture.height());
312e5b50197e870aa6d22c3893f5d25f4279f06e5c3Chris Craik                    // stencil is bound + allocated - associate it with current FBO
313e5b50197e870aa6d22c3893f5d25f4279f06e5c3Chris Craik                    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
314e5b50197e870aa6d22c3893f5d25f4279f06e5c3Chris Craik                            GL_RENDERBUFFER, mRenderTarget.stencil->getName());
315e5b50197e870aa6d22c3893f5d25f4279f06e5c3Chris Craik                }
316e5b50197e870aa6d22c3893f5d25f4279f06e5c3Chris Craik
317e5b50197e870aa6d22c3893f5d25f4279f06e5c3Chris Craik                if (clip->mode == ClipMode::RectangleList) {
318e5b50197e870aa6d22c3893f5d25f4279f06e5c3Chris Craik                    setupStencilRectList(clip);
319e5b50197e870aa6d22c3893f5d25f4279f06e5c3Chris Craik                } else {
320e5b50197e870aa6d22c3893f5d25f4279f06e5c3Chris Craik                    setupStencilRegion(clip);
321e4db79de127cfe961195f52907af8451026eaa20Chris Craik                }
322e4db79de127cfe961195f52907af8451026eaa20Chris Craik            } else {
323e5b50197e870aa6d22c3893f5d25f4279f06e5c3Chris Craik                // stencil is up to date - just need to ensure it's enabled (since an unclipped
324e5b50197e870aa6d22c3893f5d25f4279f06e5c3Chris Craik                // or scissor-only clipped op may have been drawn, disabling the stencil)
325e5b50197e870aa6d22c3893f5d25f4279f06e5c3Chris Craik                int incrementThreshold = 0;
326e5b50197e870aa6d22c3893f5d25f4279f06e5c3Chris Craik                if (CC_LIKELY(clip->mode == ClipMode::RectangleList)) {
327e5b50197e870aa6d22c3893f5d25f4279f06e5c3Chris Craik                    auto&& rectList = reinterpret_cast<const ClipRectList*>(clip)->rectList;
328e5b50197e870aa6d22c3893f5d25f4279f06e5c3Chris Craik                    incrementThreshold = rectList.getTransformedRectanglesCount();
329e5b50197e870aa6d22c3893f5d25f4279f06e5c3Chris Craik                }
330e5b50197e870aa6d22c3893f5d25f4279f06e5c3Chris Craik                mRenderState.stencil().enableTest(incrementThreshold);
331e4db79de127cfe961195f52907af8451026eaa20Chris Craik            }
332e5b50197e870aa6d22c3893f5d25f4279f06e5c3Chris Craik        } else {
333e5b50197e870aa6d22c3893f5d25f4279f06e5c3Chris Craik            // either scissor or no clip, so disable stencil test
334e5b50197e870aa6d22c3893f5d25f4279f06e5c3Chris Craik            mRenderState.stencil().disable();
335e4db79de127cfe961195f52907af8451026eaa20Chris Craik        }
3365854b34881b1a747ac80b5077869ef270a92b1f4Chris Craik    }
337e4db79de127cfe961195f52907af8451026eaa20Chris Craik
33864db2bf1118db88c937e2b8c61b299bb2a80e3cbChris Craik    if (dirtyBounds) {
33964db2bf1118db88c937e2b8c61b299bb2a80e3cbChris Craik        // dirty offscreenbuffer if present
34064db2bf1118db88c937e2b8c61b299bb2a80e3cbChris Craik        dirtyRenderTarget(*dirtyBounds);
3418d2cf943d9c7292e54726399faefdec4a01c084bChris Craik    }
342e29ce6f51d681af7649c0a7cddee97c471e43eb5Chris Craik}
343e29ce6f51d681af7649c0a7cddee97c471e43eb5Chris Craik
344419a1e7ef53468e494d21c66ea7f63c0c522d208Chris Craikvoid BakedOpRenderer::renderGlopImpl(const Rect* dirtyBounds, const ClipBase* clip,
345e4db79de127cfe961195f52907af8451026eaa20Chris Craik        const Glop& glop) {
346e29ce6f51d681af7649c0a7cddee97c471e43eb5Chris Craik    prepareRender(dirtyBounds, clip);
3475854b34881b1a747ac80b5077869ef270a92b1f4Chris Craik    mRenderState.render(glop, mRenderTarget.orthoMatrix);
34898787e6c9b2c10b1ab7820bdac168686025b924aChris Craik    if (!mRenderTarget.frameBufferId) mHasDrawn = true;
3495854b34881b1a747ac80b5077869ef270a92b1f4Chris Craik}
3505854b34881b1a747ac80b5077869ef270a92b1f4Chris Craik
351e29ce6f51d681af7649c0a7cddee97c471e43eb5Chris Craikvoid BakedOpRenderer::renderFunctor(const FunctorOp& op, const BakedOpState& state) {
352e4db79de127cfe961195f52907af8451026eaa20Chris Craik    prepareRender(&state.computedState.clippedBounds, state.computedState.getClipIfNeeded());
353e29ce6f51d681af7649c0a7cddee97c471e43eb5Chris Craik
354e29ce6f51d681af7649c0a7cddee97c471e43eb5Chris Craik    DrawGlInfo info;
355e4db79de127cfe961195f52907af8451026eaa20Chris Craik    auto&& clip = state.computedState.clipRect();
356e29ce6f51d681af7649c0a7cddee97c471e43eb5Chris Craik    info.clipLeft = clip.left;
357e29ce6f51d681af7649c0a7cddee97c471e43eb5Chris Craik    info.clipTop = clip.top;
358e29ce6f51d681af7649c0a7cddee97c471e43eb5Chris Craik    info.clipRight = clip.right;
359e29ce6f51d681af7649c0a7cddee97c471e43eb5Chris Craik    info.clipBottom = clip.bottom;
360e29ce6f51d681af7649c0a7cddee97c471e43eb5Chris Craik    info.isLayer = offscreenRenderTarget();
361e29ce6f51d681af7649c0a7cddee97c471e43eb5Chris Craik    info.width = mRenderTarget.viewportWidth;
362e29ce6f51d681af7649c0a7cddee97c471e43eb5Chris Craik    info.height = mRenderTarget.viewportHeight;
363e29ce6f51d681af7649c0a7cddee97c471e43eb5Chris Craik    state.computedState.transform.copyTo(&info.transform[0]);
364e29ce6f51d681af7649c0a7cddee97c471e43eb5Chris Craik
365e29ce6f51d681af7649c0a7cddee97c471e43eb5Chris Craik    mRenderState.invokeFunctor(op.functor, DrawGlInfo::kModeDraw, &info);
36648f9bb6254324006a4595f4576c28498a4aaf5d2Chris Craik    if (!mRenderTarget.frameBufferId) mHasDrawn = true;
367e29ce6f51d681af7649c0a7cddee97c471e43eb5Chris Craik}
368e29ce6f51d681af7649c0a7cddee97c471e43eb5Chris Craik
36915c3f19a445b8df575911a16e8a6dba755a084b5Chris Craikvoid BakedOpRenderer::dirtyRenderTarget(const Rect& uiDirty) {
37015c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    if (mRenderTarget.offscreenBuffer) {
37164db2bf1118db88c937e2b8c61b299bb2a80e3cbChris Craik        mRenderTarget.offscreenBuffer->dirty(uiDirty);
37215c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    }
37315c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik}
37415c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik
375b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik} // namespace uirenderer
376b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik} // namespace android
377