1b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik/*
25ea1724be4d3b6039818f91fc087e1216c1463d5Chris Craik * Copyright (C) 2016 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
17f158b49c888f722194afe5a80539a2b020c130bcChris Craik#include "FrameBuilder.h"
18b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
190b7e8245db728d127ada698be63d78b33fc6e4daChris Craik#include "LayerUpdateQueue.h"
20161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik#include "RenderNode.h"
21766431aa57c16ece8842287a92b2e7208e3b8ac3Doris Liu#include "VectorDrawable.h"
2298787e6c9b2c10b1ab7820bdac168686025b924aChris Craik#include "renderstate/OffscreenBufferPool.h"
23dccca44ffda4836b56a21da95a046c9708ffd49csergeyv#include "hwui/Canvas.h"
24161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik#include "utils/FatVector.h"
25161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik#include "utils/PaintUtils.h"
268ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik#include "utils/TraceUtils.h"
27b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
28d3daa3198e2212c985c634821682d5819346b653Chris Craik#include <SkPathOps.h>
29161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik#include <utils/TypeHelpers.h>
30b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
31b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craiknamespace android {
32b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craiknamespace uirenderer {
33b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
349cd1bbe5c9e14472e631d8cc10005613925f34afChris CraikFrameBuilder::FrameBuilder(const SkRect& clip,
350b7e8245db728d127ada698be63d78b33fc6e4daChris Craik        uint32_t viewportWidth, uint32_t viewportHeight,
369cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik        const LightGeometry& lightGeometry, Caches& caches)
379cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik        : mStdAllocator(mAllocator)
389cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik        , mLayerBuilders(mStdAllocator)
399cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik        , mLayerStack(mStdAllocator)
409cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik        , mCanvasState(*this)
416e068c0182f6f85bccb855a647510724d1c65a13Chris Craik        , mCaches(caches)
426246d27813f25b85f6e4b5cb1121fe8484bcce2dChris Craik        , mLightRadius(lightGeometry.radius)
439cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik        , mDrawFbo0(true) {
4498787e6c9b2c10b1ab7820bdac168686025b924aChris Craik
4598787e6c9b2c10b1ab7820bdac168686025b924aChris Craik    // Prepare to defer Fbo0
46f158b49c888f722194afe5a80539a2b020c130bcChris Craik    auto fbo0 = mAllocator.create<LayerBuilder>(viewportWidth, viewportHeight, Rect(clip));
47f158b49c888f722194afe5a80539a2b020c130bcChris Craik    mLayerBuilders.push_back(fbo0);
4898787e6c9b2c10b1ab7820bdac168686025b924aChris Craik    mLayerStack.push_back(0);
49b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    mCanvasState.initializeSaveStack(viewportWidth, viewportHeight,
50ddf2215d9807b641dbcb304779ef6b530f876ac7Chris Craik            clip.fLeft, clip.fTop, clip.fRight, clip.fBottom,
516e068c0182f6f85bccb855a647510724d1c65a13Chris Craik            lightGeometry.center);
529cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik}
539cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik
549cd1bbe5c9e14472e631d8cc10005613925f34afChris CraikFrameBuilder::FrameBuilder(const LayerUpdateQueue& layers,
559cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik        const LightGeometry& lightGeometry, Caches& caches)
569cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik        : mStdAllocator(mAllocator)
579cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik        , mLayerBuilders(mStdAllocator)
589cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik        , mLayerStack(mStdAllocator)
599cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik        , mCanvasState(*this)
609cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik        , mCaches(caches)
619cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik        , mLightRadius(lightGeometry.radius)
629cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik        , mDrawFbo0(false) {
639cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik    // TODO: remove, with each layer on its own save stack
640b7e8245db728d127ada698be63d78b33fc6e4daChris Craik
659cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik    // Prepare to defer Fbo0 (which will be empty)
669cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik    auto fbo0 = mAllocator.create<LayerBuilder>(1, 1, Rect(1, 1));
679cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik    mLayerBuilders.push_back(fbo0);
689cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik    mLayerStack.push_back(0);
699cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik    mCanvasState.initializeSaveStack(1, 1,
709cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik            0, 0, 1, 1,
719cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik            lightGeometry.center);
729cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik
739cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik    deferLayers(layers);
749cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik}
759cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik
769cd1bbe5c9e14472e631d8cc10005613925f34afChris Craikvoid FrameBuilder::deferLayers(const LayerUpdateQueue& layers) {
770b7e8245db728d127ada698be63d78b33fc6e4daChris Craik    // Render all layers to be updated, in order. Defer in reverse order, so that they'll be
78f158b49c888f722194afe5a80539a2b020c130bcChris Craik    // updated in the order they're passed in (mLayerBuilders are issued to Renderer in reverse)
790b7e8245db728d127ada698be63d78b33fc6e4daChris Craik    for (int i = layers.entries().size() - 1; i >= 0; i--) {
800b7e8245db728d127ada698be63d78b33fc6e4daChris Craik        RenderNode* layerNode = layers.entries()[i].renderNode;
81e9c5fd888b1b33d6651bc640450b4f62e67c99f4Chris Craik        // only schedule repaint if node still on layer - possible it may have been
82e9c5fd888b1b33d6651bc640450b4f62e67c99f4Chris Craik        // removed during a dropped frame, but layers may still remain scheduled so
83e9c5fd888b1b33d6651bc640450b4f62e67c99f4Chris Craik        // as not to lose info on what portion is damaged
8437413289478a965336239c731ebfea37ac4dde28Chris Craik        OffscreenBuffer* layer = layerNode->getLayer();
8537413289478a965336239c731ebfea37ac4dde28Chris Craik        if (CC_LIKELY(layer)) {
86aff230f6f9f9e44a5e111ba3f087d03f7a0e24f3Chris Craik            ATRACE_FORMAT("Optimize HW Layer DisplayList %s %ux%u",
87aff230f6f9f9e44a5e111ba3f087d03f7a0e24f3Chris Craik                    layerNode->getName(), layerNode->getWidth(), layerNode->getHeight());
88aff230f6f9f9e44a5e111ba3f087d03f7a0e24f3Chris Craik
89d4fe4d3b30aaefcaaae6a6d1b8dc4bf59e034768Chris Craik            Rect layerDamage = layers.entries()[i].damage;
90d4fe4d3b30aaefcaaae6a6d1b8dc4bf59e034768Chris Craik            // TODO: ensure layer damage can't be larger than layer
91d4fe4d3b30aaefcaaae6a6d1b8dc4bf59e034768Chris Craik            layerDamage.doIntersect(0, 0, layer->viewportWidth, layer->viewportHeight);
92e9c5fd888b1b33d6651bc640450b4f62e67c99f4Chris Craik            layerNode->computeOrdering();
930b7e8245db728d127ada698be63d78b33fc6e4daChris Craik
94e9c5fd888b1b33d6651bc640450b4f62e67c99f4Chris Craik            // map current light center into RenderNode's coordinate space
95e9c5fd888b1b33d6651bc640450b4f62e67c99f4Chris Craik            Vector3 lightCenter = mCanvasState.currentSnapshot()->getRelativeLightCenter();
9637413289478a965336239c731ebfea37ac4dde28Chris Craik            layer->inverseTransformInWindow.mapPoint3d(lightCenter);
978ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik
98e9c5fd888b1b33d6651bc640450b4f62e67c99f4Chris Craik            saveForLayer(layerNode->getWidth(), layerNode->getHeight(), 0, 0,
99e9c5fd888b1b33d6651bc640450b4f62e67c99f4Chris Craik                    layerDamage, lightCenter, nullptr, layerNode);
1000b7e8245db728d127ada698be63d78b33fc6e4daChris Craik
101e9c5fd888b1b33d6651bc640450b4f62e67c99f4Chris Craik            if (layerNode->getDisplayList()) {
102e9c5fd888b1b33d6651bc640450b4f62e67c99f4Chris Craik                deferNodeOps(*layerNode);
103e9c5fd888b1b33d6651bc640450b4f62e67c99f4Chris Craik            }
104e9c5fd888b1b33d6651bc640450b4f62e67c99f4Chris Craik            restoreForLayer();
1050b7e8245db728d127ada698be63d78b33fc6e4daChris Craik        }
1060b7e8245db728d127ada698be63d78b33fc6e4daChris Craik    }
1079cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik}
1089cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik
1099cd1bbe5c9e14472e631d8cc10005613925f34afChris Craikvoid FrameBuilder::deferRenderNode(RenderNode& renderNode) {
1109cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik    renderNode.computeOrdering();
1119cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik
1129cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik    mCanvasState.save(SaveFlags::MatrixClip);
1139cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik    deferNodePropsAndOps(renderNode);
1149cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik    mCanvasState.restore();
1159cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik}
1160b7e8245db728d127ada698be63d78b33fc6e4daChris Craik
1179cd1bbe5c9e14472e631d8cc10005613925f34afChris Craikvoid FrameBuilder::deferRenderNode(float tx, float ty, Rect clipRect, RenderNode& renderNode) {
1189cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik    renderNode.computeOrdering();
1199cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik
1209cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik    mCanvasState.save(SaveFlags::MatrixClip);
1219cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik    mCanvasState.translate(tx, ty);
1229cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik    mCanvasState.clipRect(clipRect.left, clipRect.top, clipRect.right, clipRect.bottom,
1239cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik            SkRegion::kIntersect_Op);
1249cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik    deferNodePropsAndOps(renderNode);
1259cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik    mCanvasState.restore();
1269cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik}
1279cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik
1289cd1bbe5c9e14472e631d8cc10005613925f34afChris Craikstatic Rect nodeBounds(RenderNode& node) {
1299cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik    auto& props = node.properties();
1309cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik    return Rect(props.getLeft(), props.getTop(),
1319cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik            props.getRight(), props.getBottom());
1329cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik}
1339cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik
1349cd1bbe5c9e14472e631d8cc10005613925f34afChris Craikvoid FrameBuilder::deferRenderNodeScene(const std::vector< sp<RenderNode> >& nodes,
1359cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik        const Rect& contentDrawBounds) {
1369cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik    if (nodes.size() < 1) return;
1379cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik    if (nodes.size() == 1) {
1389cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik        if (!nodes[0]->nothingToDraw()) {
1399cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik            deferRenderNode(*nodes[0]);
1409cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik        }
1419cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik        return;
1429cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik    }
143c3bd56811268a074ffb9513bde0d940199e7ad16Chong Zhang    // It there are multiple render nodes, they are laid out as follows:
144c3bd56811268a074ffb9513bde0d940199e7ad16Chong Zhang    // #0 - backdrop (content + caption)
1459cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik    // #1 - content (local bounds are at (0,0), will be translated and clipped to backdrop)
146c3bd56811268a074ffb9513bde0d940199e7ad16Chong Zhang    // #2 - additional overlay nodes
147c3bd56811268a074ffb9513bde0d940199e7ad16Chong Zhang    // Usually the backdrop cannot be seen since it will be entirely covered by the content. While
148c3bd56811268a074ffb9513bde0d940199e7ad16Chong Zhang    // resizing however it might become partially visible. The following render loop will crop the
149c3bd56811268a074ffb9513bde0d940199e7ad16Chong Zhang    // backdrop against the content and draw the remaining part of it. It will then draw the content
150c3bd56811268a074ffb9513bde0d940199e7ad16Chong Zhang    // cropped to the backdrop (since that indicates a shrinking of the window).
151c3bd56811268a074ffb9513bde0d940199e7ad16Chong Zhang    //
152c3bd56811268a074ffb9513bde0d940199e7ad16Chong Zhang    // Additional nodes will be drawn on top with no particular clipping semantics.
153c3bd56811268a074ffb9513bde0d940199e7ad16Chong Zhang
154c3bd56811268a074ffb9513bde0d940199e7ad16Chong Zhang    // Usually the contents bounds should be mContentDrawBounds - however - we will
155c3bd56811268a074ffb9513bde0d940199e7ad16Chong Zhang    // move it towards the fixed edge to give it a more stable appearance (for the moment).
156c3bd56811268a074ffb9513bde0d940199e7ad16Chong Zhang    // If there is no content bounds we ignore the layering as stated above and start with 2.
1579cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik
1589cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik    // Backdrop bounds in render target space
1599cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik    const Rect backdrop = nodeBounds(*nodes[0]);
1609cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik
1619cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik    // Bounds that content will fill in render target space (note content node bounds may be bigger)
1629cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik    Rect content(contentDrawBounds.getWidth(), contentDrawBounds.getHeight());
1639cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik    content.translate(backdrop.left, backdrop.top);
1649cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik    if (!content.contains(backdrop) && !nodes[0]->nothingToDraw()) {
1659cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik        // Content doesn't entirely overlap backdrop, so fill around content (right/bottom)
1669cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik
1679cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik        // Note: in the future, if content doesn't snap to backdrop's left/top, this may need to
1689cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik        // also fill left/top. Currently, both 2up and freeform position content at the top/left of
1699cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik        // the backdrop, so this isn't necessary.
1709cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik        if (content.right < backdrop.right) {
1719cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik            // draw backdrop to right side of content
1729cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik            deferRenderNode(0, 0, Rect(content.right, backdrop.top,
1739cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik                    backdrop.right, backdrop.bottom), *nodes[0]);
1749cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik        }
1759cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik        if (content.bottom < backdrop.bottom) {
1769cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik            // draw backdrop to bottom of content
1779cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik            // Note: bottom fill uses content left/right, to avoid overdrawing left/right fill
1789cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik            deferRenderNode(0, 0, Rect(content.left, content.bottom,
1799cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik                    content.right, backdrop.bottom), *nodes[0]);
180c3bd56811268a074ffb9513bde0d940199e7ad16Chong Zhang        }
1819cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik    }
1829cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik
1839cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik    if (!backdrop.isEmpty()) {
1849cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik        // content node translation to catch up with backdrop
1859cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik        float dx = contentDrawBounds.left - backdrop.left;
1869cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik        float dy = contentDrawBounds.top - backdrop.top;
187c3bd56811268a074ffb9513bde0d940199e7ad16Chong Zhang
1889cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik        Rect contentLocalClip = backdrop;
1899cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik        contentLocalClip.translate(dx, dy);
1909cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik        deferRenderNode(-dx, -dy, contentLocalClip, *nodes[1]);
1919cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik    } else {
1929cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik        deferRenderNode(*nodes[1]);
1939cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik    }
1949cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik
1959cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik    // remaining overlay nodes, simply defer
1969cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik    for (size_t index = 2; index < nodes.size(); index++) {
1979cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik        if (!nodes[index]->nothingToDraw()) {
1989cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik            deferRenderNode(*nodes[index]);
1999cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik        }
200b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    }
201b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
202b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
203f158b49c888f722194afe5a80539a2b020c130bcChris Craikvoid FrameBuilder::onViewportInitialized() {}
204818c9fbf1d76d5df19253ba4eb964efa939ec9ecChris Craik
205f158b49c888f722194afe5a80539a2b020c130bcChris Craikvoid FrameBuilder::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {}
206818c9fbf1d76d5df19253ba4eb964efa939ec9ecChris Craik
207f158b49c888f722194afe5a80539a2b020c130bcChris Craikvoid FrameBuilder::deferNodePropsAndOps(RenderNode& node) {
2088ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    const RenderProperties& properties = node.properties();
2098ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    const Outline& outline = properties.getOutline();
2108ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    if (properties.getAlpha() <= 0
2118ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik            || (outline.getShouldClip() && outline.isEmpty())
2128ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik            || properties.getScaleX() == 0
2138ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik            || properties.getScaleY() == 0) {
2148ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik        return; // rejected
2158ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    }
2168ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik
2178ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    if (properties.getLeft() != 0 || properties.getTop() != 0) {
2188ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik        mCanvasState.translate(properties.getLeft(), properties.getTop());
2198ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    }
2208ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    if (properties.getStaticMatrix()) {
2218ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik        mCanvasState.concatMatrix(*properties.getStaticMatrix());
2228ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    } else if (properties.getAnimationMatrix()) {
2238ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik        mCanvasState.concatMatrix(*properties.getAnimationMatrix());
2248ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    }
2258ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    if (properties.hasTransformMatrix()) {
2268ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik        if (properties.isTransformTranslateOnly()) {
2278ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik            mCanvasState.translate(properties.getTranslationX(), properties.getTranslationY());
2288ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik        } else {
2298ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik            mCanvasState.concatMatrix(*properties.getTransformMatrix());
2308ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik        }
2318ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    }
2328ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik
2338ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    const int width = properties.getWidth();
2348ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    const int height = properties.getHeight();
2358ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik
2368ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    Rect saveLayerBounds; // will be set to non-empty if saveLayer needed
2378ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    const bool isLayer = properties.effectiveLayerType() != LayerType::None;
2388ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    int clipFlags = properties.getClippingFlags();
2398ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    if (properties.getAlpha() < 1) {
2408ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik        if (isLayer) {
2418ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik            clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
2428ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik        }
2438ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik        if (CC_LIKELY(isLayer || !properties.getHasOverlappingRendering())) {
2448ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik            // simply scale rendering content's alpha
2458ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik            mCanvasState.scaleAlpha(properties.getAlpha());
2468ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik        } else {
2478ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik            // schedule saveLayer by initializing saveLayerBounds
2488ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik            saveLayerBounds.set(0, 0, width, height);
2498ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik            if (clipFlags) {
2508ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik                properties.getClippingRectForFlags(clipFlags, &saveLayerBounds);
2518ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik                clipFlags = 0; // all clipping done by savelayer
2528ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik            }
2538ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik        }
2548ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik
2558ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik        if (CC_UNLIKELY(ATRACE_ENABLED() && properties.promotedToLayer())) {
2568ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik            // pretend alpha always causes savelayer to warn about
2578ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik            // performance problem affecting old versions
2588ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik            ATRACE_FORMAT("%s alpha caused saveLayer %dx%d", node.getName(), width, height);
2598ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik        }
2608ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    }
2618ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    if (clipFlags) {
2628ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik        Rect clipRect;
2638ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik        properties.getClippingRectForFlags(clipFlags, &clipRect);
2648ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik        mCanvasState.clipRect(clipRect.left, clipRect.top, clipRect.right, clipRect.bottom,
2658ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik                SkRegion::kIntersect_Op);
2668ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    }
2678ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik
2688ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    if (properties.getRevealClip().willClip()) {
2698ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik        Rect bounds;
2708ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik        properties.getRevealClip().getBounds(&bounds);
2718ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik        mCanvasState.setClippingRoundRect(mAllocator,
2728ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik                bounds, properties.getRevealClip().getRadius());
2738ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    } else if (properties.getOutline().willClip()) {
2748ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik        mCanvasState.setClippingOutline(mAllocator, &(properties.getOutline()));
2758ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    }
2768ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik
2778913c8942fa15c2df6da747e42c5eda8454758ebChris Craik    bool quickRejected = mCanvasState.currentSnapshot()->getRenderTargetClip().isEmpty()
2788913c8942fa15c2df6da747e42c5eda8454758ebChris Craik            || (properties.getClipToBounds()
2798913c8942fa15c2df6da747e42c5eda8454758ebChris Craik                    && mCanvasState.quickRejectConservative(0, 0, width, height));
2807fc1b0349bc2ac8c880120dc5611f703faa7f06fChris Craik    if (!quickRejected) {
2818ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik        // not rejected, so defer render as either Layer, or direct (possibly wrapped in saveLayer)
2820b7e8245db728d127ada698be63d78b33fc6e4daChris Craik        if (node.getLayer()) {
2830b7e8245db728d127ada698be63d78b33fc6e4daChris Craik            // HW layer
2847df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck            LayerOp* drawLayerOp = mAllocator.create_trivial<LayerOp>(node);
2850b7e8245db728d127ada698be63d78b33fc6e4daChris Craik            BakedOpState* bakedOpState = tryBakeOpState(*drawLayerOp);
2860b7e8245db728d127ada698be63d78b33fc6e4daChris Craik            if (bakedOpState) {
2878ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik                // Node's layer already deferred, schedule it to render into parent layer
2880b7e8245db728d127ada698be63d78b33fc6e4daChris Craik                currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Bitmap);
2890b7e8245db728d127ada698be63d78b33fc6e4daChris Craik            }
2908ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik        } else if (CC_UNLIKELY(!saveLayerBounds.isEmpty())) {
2918ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik            // draw DisplayList contents within temporary, since persisted layer could not be used.
2928ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik            // (temp layers are clipped to viewport, since they don't persist offscreen content)
2938ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik            SkPaint saveLayerPaint;
2948ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik            saveLayerPaint.setAlpha(properties.getAlpha());
2957df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck            deferBeginLayerOp(*mAllocator.create_trivial<BeginLayerOp>(
2968ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik                    saveLayerBounds,
2978ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik                    Matrix4::identity(),
298e4db79de127cfe961195f52907af8451026eaa20Chris Craik                    nullptr, // no record-time clip - need only respect defer-time one
2998ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik                    &saveLayerPaint));
3008d1f2120fe80b23ab03c7168e3b6b2d13bafe2e7Chris Craik            deferNodeOps(node);
3017df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck            deferEndLayerOp(*mAllocator.create_trivial<EndLayerOp>());
3020b7e8245db728d127ada698be63d78b33fc6e4daChris Craik        } else {
3038d1f2120fe80b23ab03c7168e3b6b2d13bafe2e7Chris Craik            deferNodeOps(node);
3040b7e8245db728d127ada698be63d78b33fc6e4daChris Craik        }
3050b7e8245db728d127ada698be63d78b33fc6e4daChris Craik    }
3060b7e8245db728d127ada698be63d78b33fc6e4daChris Craik}
3070b7e8245db728d127ada698be63d78b33fc6e4daChris Craik
308161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craiktypedef key_value_pair_t<float, const RenderNodeOp*> ZRenderNodeOpPair;
309161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik
310161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craiktemplate <typename V>
311161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craikstatic void buildZSortedChildList(V* zTranslatedNodes,
312161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik        const DisplayList& displayList, const DisplayList::Chunk& chunk) {
313161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik    if (chunk.beginChildIndex == chunk.endChildIndex) return;
314161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik
315161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik    for (size_t i = chunk.beginChildIndex; i < chunk.endChildIndex; i++) {
316161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik        RenderNodeOp* childOp = displayList.getChildren()[i];
317161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik        RenderNode* child = childOp->renderNode;
318161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik        float childZ = child->properties().getZ();
319161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik
320161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik        if (!MathUtils::isZero(childZ) && chunk.reorderChildren) {
321161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik            zTranslatedNodes->push_back(ZRenderNodeOpPair(childZ, childOp));
322161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik            childOp->skipInOrderDraw = true;
323161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik        } else if (!child->properties().getProjectBackwards()) {
324161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik            // regular, in order drawing DisplayList
325161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik            childOp->skipInOrderDraw = false;
326161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik        }
327161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik    }
328161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik
329161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik    // Z sort any 3d children (stable-ness makes z compare fall back to standard drawing order)
330161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik    std::stable_sort(zTranslatedNodes->begin(), zTranslatedNodes->end());
331161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik}
332161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik
333161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craiktemplate <typename V>
334161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craikstatic size_t findNonNegativeIndex(const V& zTranslatedNodes) {
335161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik    for (size_t i = 0; i < zTranslatedNodes.size(); i++) {
336161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik        if (zTranslatedNodes[i].key >= 0.0f) return i;
337161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik    }
338161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik    return zTranslatedNodes.size();
339161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik}
340161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik
341161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craiktemplate <typename V>
342d645640180c25c2711e99aa82ec629155f8e91baChris Craikvoid FrameBuilder::defer3dChildren(const ClipBase* reorderClip, ChildrenSelectMode mode,
343d645640180c25c2711e99aa82ec629155f8e91baChris Craik        const V& zTranslatedNodes) {
344161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik    const int size = zTranslatedNodes.size();
345161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik    if (size == 0
346161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik            || (mode == ChildrenSelectMode::Negative&& zTranslatedNodes[0].key > 0.0f)
347161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik            || (mode == ChildrenSelectMode::Positive && zTranslatedNodes[size - 1].key < 0.0f)) {
348161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik        // no 3d children to draw
349161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik        return;
350161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik    }
351161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik
352161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik    /**
353161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik     * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
354161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik     * with very similar Z heights to draw together.
355161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik     *
356161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik     * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
357161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik     * underneath both, and neither's shadow is drawn on top of the other.
358161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik     */
359161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik    const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
360161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik    size_t drawIndex, shadowIndex, endIndex;
361161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik    if (mode == ChildrenSelectMode::Negative) {
362161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik        drawIndex = 0;
363161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik        endIndex = nonNegativeIndex;
364161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik        shadowIndex = endIndex; // draw no shadows
365161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik    } else {
366161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik        drawIndex = nonNegativeIndex;
367161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik        endIndex = size;
368161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik        shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
369161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik    }
370161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik
371161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik    float lastCasterZ = 0.0f;
372161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik    while (shadowIndex < endIndex || drawIndex < endIndex) {
373161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik        if (shadowIndex < endIndex) {
374161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik            const RenderNodeOp* casterNodeOp = zTranslatedNodes[shadowIndex].value;
375161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik            const float casterZ = zTranslatedNodes[shadowIndex].key;
376161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik            // attempt to render the shadow if the caster about to be drawn is its caster,
377161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik            // OR if its caster's Z value is similar to the previous potential caster
378161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik            if (shadowIndex == drawIndex || casterZ - lastCasterZ < 0.1f) {
379d645640180c25c2711e99aa82ec629155f8e91baChris Craik                deferShadow(reorderClip, *casterNodeOp);
380161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik
381161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik                lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
382161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik                shadowIndex++;
383161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik                continue;
384161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik            }
385161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik        }
386161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik
387161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik        const RenderNodeOp* childOp = zTranslatedNodes[drawIndex].value;
388268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik        deferRenderNodeOpImpl(*childOp);
389161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik        drawIndex++;
390161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik    }
391161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik}
392161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik
393d645640180c25c2711e99aa82ec629155f8e91baChris Craikvoid FrameBuilder::deferShadow(const ClipBase* reorderClip, const RenderNodeOp& casterNodeOp) {
394d3daa3198e2212c985c634821682d5819346b653Chris Craik    auto& node = *casterNodeOp.renderNode;
395d3daa3198e2212c985c634821682d5819346b653Chris Craik    auto& properties = node.properties();
396d3daa3198e2212c985c634821682d5819346b653Chris Craik
397d3daa3198e2212c985c634821682d5819346b653Chris Craik    if (properties.getAlpha() <= 0.0f
398d3daa3198e2212c985c634821682d5819346b653Chris Craik            || properties.getOutline().getAlpha() <= 0.0f
399d3daa3198e2212c985c634821682d5819346b653Chris Craik            || !properties.getOutline().getPath()
400d3daa3198e2212c985c634821682d5819346b653Chris Craik            || properties.getScaleX() == 0
401d3daa3198e2212c985c634821682d5819346b653Chris Craik            || properties.getScaleY() == 0) {
402d3daa3198e2212c985c634821682d5819346b653Chris Craik        // no shadow to draw
403d3daa3198e2212c985c634821682d5819346b653Chris Craik        return;
404d3daa3198e2212c985c634821682d5819346b653Chris Craik    }
405d3daa3198e2212c985c634821682d5819346b653Chris Craik
406d3daa3198e2212c985c634821682d5819346b653Chris Craik    const SkPath* casterOutlinePath = properties.getOutline().getPath();
407d3daa3198e2212c985c634821682d5819346b653Chris Craik    const SkPath* revealClipPath = properties.getRevealClip().getPath();
408d3daa3198e2212c985c634821682d5819346b653Chris Craik    if (revealClipPath && revealClipPath->isEmpty()) return;
409d3daa3198e2212c985c634821682d5819346b653Chris Craik
410d3daa3198e2212c985c634821682d5819346b653Chris Craik    float casterAlpha = properties.getAlpha() * properties.getOutline().getAlpha();
411d3daa3198e2212c985c634821682d5819346b653Chris Craik
412d3daa3198e2212c985c634821682d5819346b653Chris Craik    // holds temporary SkPath to store the result of intersections
413d3daa3198e2212c985c634821682d5819346b653Chris Craik    SkPath* frameAllocatedPath = nullptr;
414d3daa3198e2212c985c634821682d5819346b653Chris Craik    const SkPath* casterPath = casterOutlinePath;
415d3daa3198e2212c985c634821682d5819346b653Chris Craik
416d3daa3198e2212c985c634821682d5819346b653Chris Craik    // intersect the shadow-casting path with the reveal, if present
417d3daa3198e2212c985c634821682d5819346b653Chris Craik    if (revealClipPath) {
418d3daa3198e2212c985c634821682d5819346b653Chris Craik        frameAllocatedPath = createFrameAllocatedPath();
419d3daa3198e2212c985c634821682d5819346b653Chris Craik
420d3daa3198e2212c985c634821682d5819346b653Chris Craik        Op(*casterPath, *revealClipPath, kIntersect_SkPathOp, frameAllocatedPath);
421d3daa3198e2212c985c634821682d5819346b653Chris Craik        casterPath = frameAllocatedPath;
422d3daa3198e2212c985c634821682d5819346b653Chris Craik    }
423d3daa3198e2212c985c634821682d5819346b653Chris Craik
424d3daa3198e2212c985c634821682d5819346b653Chris Craik    // intersect the shadow-casting path with the clipBounds, if present
425d3daa3198e2212c985c634821682d5819346b653Chris Craik    if (properties.getClippingFlags() & CLIP_TO_CLIP_BOUNDS) {
426d3daa3198e2212c985c634821682d5819346b653Chris Craik        if (!frameAllocatedPath) {
427d3daa3198e2212c985c634821682d5819346b653Chris Craik            frameAllocatedPath = createFrameAllocatedPath();
428d3daa3198e2212c985c634821682d5819346b653Chris Craik        }
429d3daa3198e2212c985c634821682d5819346b653Chris Craik        Rect clipBounds;
430d3daa3198e2212c985c634821682d5819346b653Chris Craik        properties.getClippingRectForFlags(CLIP_TO_CLIP_BOUNDS, &clipBounds);
431d3daa3198e2212c985c634821682d5819346b653Chris Craik        SkPath clipBoundsPath;
432d3daa3198e2212c985c634821682d5819346b653Chris Craik        clipBoundsPath.addRect(clipBounds.left, clipBounds.top,
433d3daa3198e2212c985c634821682d5819346b653Chris Craik                clipBounds.right, clipBounds.bottom);
434d3daa3198e2212c985c634821682d5819346b653Chris Craik
435d3daa3198e2212c985c634821682d5819346b653Chris Craik        Op(*casterPath, clipBoundsPath, kIntersect_SkPathOp, frameAllocatedPath);
436d3daa3198e2212c985c634821682d5819346b653Chris Craik        casterPath = frameAllocatedPath;
437d3daa3198e2212c985c634821682d5819346b653Chris Craik    }
438d3daa3198e2212c985c634821682d5819346b653Chris Craik
439d645640180c25c2711e99aa82ec629155f8e91baChris Craik    // apply reorder clip to shadow, so it respects clip at beginning of reorderable chunk
440d645640180c25c2711e99aa82ec629155f8e91baChris Craik    int restoreTo = mCanvasState.save(SaveFlags::MatrixClip);
441d645640180c25c2711e99aa82ec629155f8e91baChris Craik    mCanvasState.writableSnapshot()->applyClip(reorderClip,
442d645640180c25c2711e99aa82ec629155f8e91baChris Craik            *mCanvasState.currentSnapshot()->transform);
4436e068c0182f6f85bccb855a647510724d1c65a13Chris Craik    if (CC_LIKELY(!mCanvasState.getRenderTargetClipBounds().isEmpty())) {
4446e068c0182f6f85bccb855a647510724d1c65a13Chris Craik        Matrix4 shadowMatrixXY(casterNodeOp.localMatrix);
4456e068c0182f6f85bccb855a647510724d1c65a13Chris Craik        Matrix4 shadowMatrixZ(casterNodeOp.localMatrix);
4466e068c0182f6f85bccb855a647510724d1c65a13Chris Craik        node.applyViewPropertyTransforms(shadowMatrixXY, false);
4476e068c0182f6f85bccb855a647510724d1c65a13Chris Craik        node.applyViewPropertyTransforms(shadowMatrixZ, true);
4486e068c0182f6f85bccb855a647510724d1c65a13Chris Craik
4493a5811b50157e7ba50854caf957e806aee794d39Chris Craik        sp<TessellationCache::ShadowTask> task = mCaches.tessellationCache.getShadowTask(
4506e068c0182f6f85bccb855a647510724d1c65a13Chris Craik                mCanvasState.currentTransform(),
4516e068c0182f6f85bccb855a647510724d1c65a13Chris Craik                mCanvasState.getLocalClipBounds(),
4526e068c0182f6f85bccb855a647510724d1c65a13Chris Craik                casterAlpha >= 1.0f,
4536e068c0182f6f85bccb855a647510724d1c65a13Chris Craik                casterPath,
4546e068c0182f6f85bccb855a647510724d1c65a13Chris Craik                &shadowMatrixXY, &shadowMatrixZ,
4556e068c0182f6f85bccb855a647510724d1c65a13Chris Craik                mCanvasState.currentSnapshot()->getRelativeLightCenter(),
4566e068c0182f6f85bccb855a647510724d1c65a13Chris Craik                mLightRadius);
4576e068c0182f6f85bccb855a647510724d1c65a13Chris Craik        ShadowOp* shadowOp = mAllocator.create<ShadowOp>(task, casterAlpha);
4586e068c0182f6f85bccb855a647510724d1c65a13Chris Craik        BakedOpState* bakedOpState = BakedOpState::tryShadowOpConstruct(
4596e068c0182f6f85bccb855a647510724d1c65a13Chris Craik                mAllocator, *mCanvasState.writableSnapshot(), shadowOp);
4606e068c0182f6f85bccb855a647510724d1c65a13Chris Craik        if (CC_LIKELY(bakedOpState)) {
4616e068c0182f6f85bccb855a647510724d1c65a13Chris Craik            currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Shadow);
4626e068c0182f6f85bccb855a647510724d1c65a13Chris Craik        }
463d3daa3198e2212c985c634821682d5819346b653Chris Craik    }
464d645640180c25c2711e99aa82ec629155f8e91baChris Craik    mCanvasState.restoreToCount(restoreTo);
465161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik}
466d3daa3198e2212c985c634821682d5819346b653Chris Craik
467f158b49c888f722194afe5a80539a2b020c130bcChris Craikvoid FrameBuilder::deferProjectedChildren(const RenderNode& renderNode) {
468eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita    int count = mCanvasState.save(SaveFlags::MatrixClip);
469678ff81105753656aa4822f4f675ef96dc9d2b83Chris Craik    const SkPath* projectionReceiverOutline = renderNode.properties().getOutline().getPath();
4708d1f2120fe80b23ab03c7168e3b6b2d13bafe2e7Chris Craik
471678ff81105753656aa4822f4f675ef96dc9d2b83Chris Craik    SkPath transformedMaskPath; // on stack, since BakedOpState makes a deep copy
472678ff81105753656aa4822f4f675ef96dc9d2b83Chris Craik    if (projectionReceiverOutline) {
473678ff81105753656aa4822f4f675ef96dc9d2b83Chris Craik        // transform the mask for this projector into render target space
474678ff81105753656aa4822f4f675ef96dc9d2b83Chris Craik        // TODO: consider combining both transforms by stashing transform instead of applying
475678ff81105753656aa4822f4f675ef96dc9d2b83Chris Craik        SkMatrix skCurrentTransform;
476678ff81105753656aa4822f4f675ef96dc9d2b83Chris Craik        mCanvasState.currentTransform()->copyTo(skCurrentTransform);
477678ff81105753656aa4822f4f675ef96dc9d2b83Chris Craik        projectionReceiverOutline->transform(
478678ff81105753656aa4822f4f675ef96dc9d2b83Chris Craik                skCurrentTransform,
479678ff81105753656aa4822f4f675ef96dc9d2b83Chris Craik                &transformedMaskPath);
480678ff81105753656aa4822f4f675ef96dc9d2b83Chris Craik        mCanvasState.setProjectionPathMask(mAllocator, &transformedMaskPath);
481678ff81105753656aa4822f4f675ef96dc9d2b83Chris Craik    }
4828d1f2120fe80b23ab03c7168e3b6b2d13bafe2e7Chris Craik
4838d1f2120fe80b23ab03c7168e3b6b2d13bafe2e7Chris Craik    for (size_t i = 0; i < renderNode.mProjectedNodes.size(); i++) {
4848d1f2120fe80b23ab03c7168e3b6b2d13bafe2e7Chris Craik        RenderNodeOp* childOp = renderNode.mProjectedNodes[i];
485a748c08241e43fc68c7c34767d819aef5183936eChris Craik        RenderNode& childNode = *childOp->renderNode;
486678ff81105753656aa4822f4f675ef96dc9d2b83Chris Craik
487a748c08241e43fc68c7c34767d819aef5183936eChris Craik        // Draw child if it has content, but ignore state in childOp - matrix already applied to
488a748c08241e43fc68c7c34767d819aef5183936eChris Craik        // transformFromCompositingAncestor, and record-time clip is ignored when projecting
489a748c08241e43fc68c7c34767d819aef5183936eChris Craik        if (!childNode.nothingToDraw()) {
490a748c08241e43fc68c7c34767d819aef5183936eChris Craik            int restoreTo = mCanvasState.save(SaveFlags::MatrixClip);
491678ff81105753656aa4822f4f675ef96dc9d2b83Chris Craik
492a748c08241e43fc68c7c34767d819aef5183936eChris Craik            // Apply transform between ancestor and projected descendant
493a748c08241e43fc68c7c34767d819aef5183936eChris Craik            mCanvasState.concatMatrix(childOp->transformFromCompositingAncestor);
494a748c08241e43fc68c7c34767d819aef5183936eChris Craik
495a748c08241e43fc68c7c34767d819aef5183936eChris Craik            deferNodePropsAndOps(childNode);
496a748c08241e43fc68c7c34767d819aef5183936eChris Craik
497a748c08241e43fc68c7c34767d819aef5183936eChris Craik            mCanvasState.restoreToCount(restoreTo);
498a748c08241e43fc68c7c34767d819aef5183936eChris Craik        }
4998d1f2120fe80b23ab03c7168e3b6b2d13bafe2e7Chris Craik    }
5008d1f2120fe80b23ab03c7168e3b6b2d13bafe2e7Chris Craik    mCanvasState.restoreToCount(count);
5018d1f2120fe80b23ab03c7168e3b6b2d13bafe2e7Chris Craik}
5028d1f2120fe80b23ab03c7168e3b6b2d13bafe2e7Chris Craik
503b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik/**
504f158b49c888f722194afe5a80539a2b020c130bcChris Craik * Used to define a list of lambdas referencing private FrameBuilder::onXX::defer() methods.
505b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik *
5068d1f2120fe80b23ab03c7168e3b6b2d13bafe2e7Chris Craik * This allows opIds embedded in the RecordedOps to be used for dispatching to these lambdas.
507f158b49c888f722194afe5a80539a2b020c130bcChris Craik * E.g. a BitmapOp op then would be dispatched to FrameBuilder::onBitmapOp(const BitmapOp&)
508b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik */
5096fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik#define OP_RECEIVER(Type) \
510f158b49c888f722194afe5a80539a2b020c130bcChris Craik        [](FrameBuilder& frameBuilder, const RecordedOp& op) { frameBuilder.defer##Type(static_cast<const Type&>(op)); },
511f158b49c888f722194afe5a80539a2b020c130bcChris Craikvoid FrameBuilder::deferNodeOps(const RenderNode& renderNode) {
512f158b49c888f722194afe5a80539a2b020c130bcChris Craik    typedef void (*OpDispatcher) (FrameBuilder& frameBuilder, const RecordedOp& op);
5137cbf63da4f29e5a6b131796eb3b67fd9ff1521b8Chris Craik    static OpDispatcher receivers[] = BUILD_DEFERRABLE_OP_LUT(OP_RECEIVER);
5148d1f2120fe80b23ab03c7168e3b6b2d13bafe2e7Chris Craik
5158d1f2120fe80b23ab03c7168e3b6b2d13bafe2e7Chris Craik    // can't be null, since DL=null node rejection happens before deferNodePropsAndOps
5168d1f2120fe80b23ab03c7168e3b6b2d13bafe2e7Chris Craik    const DisplayList& displayList = *(renderNode.getDisplayList());
517d645640180c25c2711e99aa82ec629155f8e91baChris Craik    for (auto& chunk : displayList.getChunks()) {
518161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik        FatVector<ZRenderNodeOpPair, 16> zTranslatedNodes;
519161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik        buildZSortedChildList(&zTranslatedNodes, displayList, chunk);
520161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik
521d645640180c25c2711e99aa82ec629155f8e91baChris Craik        defer3dChildren(chunk.reorderClip, ChildrenSelectMode::Negative, zTranslatedNodes);
522b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        for (size_t opIndex = chunk.beginOpIndex; opIndex < chunk.endOpIndex; opIndex++) {
523b36af87f8275f4b982906f88193ec27600f2746aChris Craik            const RecordedOp* op = displayList.getOps()[opIndex];
524b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            receivers[op->opId](*this, *op);
5258d1f2120fe80b23ab03c7168e3b6b2d13bafe2e7Chris Craik
5268d1f2120fe80b23ab03c7168e3b6b2d13bafe2e7Chris Craik            if (CC_UNLIKELY(!renderNode.mProjectedNodes.empty()
5278d1f2120fe80b23ab03c7168e3b6b2d13bafe2e7Chris Craik                    && displayList.projectionReceiveIndex >= 0
5288d1f2120fe80b23ab03c7168e3b6b2d13bafe2e7Chris Craik                    && static_cast<int>(opIndex) == displayList.projectionReceiveIndex)) {
5298d1f2120fe80b23ab03c7168e3b6b2d13bafe2e7Chris Craik                deferProjectedChildren(renderNode);
5308d1f2120fe80b23ab03c7168e3b6b2d13bafe2e7Chris Craik            }
531b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        }
532d645640180c25c2711e99aa82ec629155f8e91baChris Craik        defer3dChildren(chunk.reorderClip, ChildrenSelectMode::Positive, zTranslatedNodes);
533b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    }
534b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
535b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
536f158b49c888f722194afe5a80539a2b020c130bcChris Craikvoid FrameBuilder::deferRenderNodeOpImpl(const RenderNodeOp& op) {
537161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik    if (op.renderNode->nothingToDraw()) return;
538eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita    int count = mCanvasState.save(SaveFlags::MatrixClip);
539b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
540e4db79de127cfe961195f52907af8451026eaa20Chris Craik    // apply state from RecordedOp (clip first, since op's clip is transformed by current matrix)
54104d46eb69fb4f4c4c332c36c6ae845da3b2ae848Chris Craik    mCanvasState.writableSnapshot()->applyClip(op.localClip,
542e4db79de127cfe961195f52907af8451026eaa20Chris Craik            *mCanvasState.currentSnapshot()->transform);
543b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    mCanvasState.concatMatrix(op.localMatrix);
544b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
5450b7e8245db728d127ada698be63d78b33fc6e4daChris Craik    // then apply state from node properties, and defer ops
5460b7e8245db728d127ada698be63d78b33fc6e4daChris Craik    deferNodePropsAndOps(*op.renderNode);
5470b7e8245db728d127ada698be63d78b33fc6e4daChris Craik
5486fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik    mCanvasState.restoreToCount(count);
549b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
550b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
551f158b49c888f722194afe5a80539a2b020c130bcChris Craikvoid FrameBuilder::deferRenderNodeOp(const RenderNodeOp& op) {
552161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik    if (!op.skipInOrderDraw) {
553268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik        deferRenderNodeOpImpl(op);
554161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik    }
555161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik}
556161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik
557386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik/**
558386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik * Defers an unmergeable, strokeable op, accounting correctly
559386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik * for paint's style on the bounds being computed.
560386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik */
56180d2ade939153da87b3cd3b0a69a713bf68b64baChris CraikBakedOpState* FrameBuilder::deferStrokeableOp(const RecordedOp& op, batchid_t batchId,
562386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        BakedOpState::StrokeBehavior strokeBehavior) {
563386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    // Note: here we account for stroke when baking the op
564386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    BakedOpState* bakedState = BakedOpState::tryStrokeableOpConstruct(
565e4db79de127cfe961195f52907af8451026eaa20Chris Craik            mAllocator, *mCanvasState.writableSnapshot(), op, strokeBehavior);
5663a5811b50157e7ba50854caf957e806aee794d39Chris Craik    if (!bakedState) return nullptr; // quick rejected
56780d2ade939153da87b3cd3b0a69a713bf68b64baChris Craik
56880d2ade939153da87b3cd3b0a69a713bf68b64baChris Craik    if (op.opId == RecordedOpId::RectOp && op.paint->getStyle() != SkPaint::kStroke_Style) {
56980d2ade939153da87b3cd3b0a69a713bf68b64baChris Craik        bakedState->setupOpacity(op.paint);
57080d2ade939153da87b3cd3b0a69a713bf68b64baChris Craik    }
57180d2ade939153da87b3cd3b0a69a713bf68b64baChris Craik
572386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    currentLayer().deferUnmergeableOp(mAllocator, bakedState, batchId);
5733a5811b50157e7ba50854caf957e806aee794d39Chris Craik    return bakedState;
574386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik}
575386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik
576386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik/**
577386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik * Returns batch id for tessellatable shapes, based on paint. Checks to see if path effect/AA will
578386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik * be used, since they trigger significantly different rendering paths.
579386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik *
580386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik * Note: not used for lines/points, since they don't currently support path effects.
581386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik */
582386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craikstatic batchid_t tessBatchId(const RecordedOp& op) {
583386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    const SkPaint& paint = *(op.paint);
584b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    return paint.getPathEffect()
585b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            ? OpBatchType::AlphaMaskTexture
586b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            : (paint.isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices);
587b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
588b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
589f158b49c888f722194afe5a80539a2b020c130bcChris Craikvoid FrameBuilder::deferArcOp(const ArcOp& op) {
590268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik    deferStrokeableOp(op, tessBatchId(op));
591386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik}
592386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik
593b87eadda1818034ce03d85f30388384d1ac65916Chris Craikstatic bool hasMergeableClip(const BakedOpState& state) {
594b87eadda1818034ce03d85f30388384d1ac65916Chris Craik    return state.computedState.clipState
595b87eadda1818034ce03d85f30388384d1ac65916Chris Craik            || state.computedState.clipState->mode == ClipMode::Rectangle;
596b87eadda1818034ce03d85f30388384d1ac65916Chris Craik}
597b87eadda1818034ce03d85f30388384d1ac65916Chris Craik
598f158b49c888f722194afe5a80539a2b020c130bcChris Craikvoid FrameBuilder::deferBitmapOp(const BitmapOp& op) {
59915c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    BakedOpState* bakedState = tryBakeOpState(op);
60015c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    if (!bakedState) return; // quick rejected
601a82ffc549bd6dbf8cfc6f4d646d0f458dca54014sergeyv
602a82ffc549bd6dbf8cfc6f4d646d0f458dca54014sergeyv    if (op.bitmap->isOpaque()) {
603a82ffc549bd6dbf8cfc6f4d646d0f458dca54014sergeyv        bakedState->setupOpacity(op.paint);
604a82ffc549bd6dbf8cfc6f4d646d0f458dca54014sergeyv    }
60515c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik
60615c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    // Don't merge non-simply transformed or neg scale ops, SET_TEXTURE doesn't handle rotation
60715c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    // Don't merge A8 bitmaps - the paint's color isn't compared by mergeId, or in
60815c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    // MergingDrawBatch::canMergeWith()
60915c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    if (bakedState->computedState.transform.isSimple()
61015c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik            && bakedState->computedState.transform.positiveScale()
61115c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik            && PaintUtils::getXfermodeDirect(op.paint) == SkXfermode::kSrcOver_Mode
612b87eadda1818034ce03d85f30388384d1ac65916Chris Craik            && op.bitmap->colorType() != kAlpha_8_SkColorType
613b87eadda1818034ce03d85f30388384d1ac65916Chris Craik            && hasMergeableClip(*bakedState)) {
614b250a834e433b4f483e8d8362a6fda8d6594c7b8Chris Craik        mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.bitmap->getGenerationID());
61515c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        // TODO: AssetAtlas in mergeId
61615c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        currentLayer().deferMergeableOp(mAllocator, bakedState, OpBatchType::Bitmap, mergeId);
61715c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    } else {
61815c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
61915c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    }
620b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
621b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
622f158b49c888f722194afe5a80539a2b020c130bcChris Craikvoid FrameBuilder::deferBitmapMeshOp(const BitmapMeshOp& op) {
623f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    BakedOpState* bakedState = tryBakeOpState(op);
624f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    if (!bakedState) return; // quick rejected
625f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
626f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik}
627f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
628f158b49c888f722194afe5a80539a2b020c130bcChris Craikvoid FrameBuilder::deferBitmapRectOp(const BitmapRectOp& op) {
629f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    BakedOpState* bakedState = tryBakeOpState(op);
630f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    if (!bakedState) return; // quick rejected
631f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
632f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik}
633f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
634766431aa57c16ece8842287a92b2e7208e3b8ac3Doris Liuvoid FrameBuilder::deferVectorDrawableOp(const VectorDrawableOp& op) {
635766431aa57c16ece8842287a92b2e7208e3b8ac3Doris Liu    const SkBitmap& bitmap = op.vectorDrawable->getBitmapUpdateIfDirty();
636766431aa57c16ece8842287a92b2e7208e3b8ac3Doris Liu    SkPaint* paint = op.vectorDrawable->getPaint();
6377df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck    const BitmapRectOp* resolvedOp = mAllocator.create_trivial<BitmapRectOp>(op.unmappedBounds,
638766431aa57c16ece8842287a92b2e7208e3b8ac3Doris Liu            op.localMatrix,
639766431aa57c16ece8842287a92b2e7208e3b8ac3Doris Liu            op.localClip,
640766431aa57c16ece8842287a92b2e7208e3b8ac3Doris Liu            paint,
641766431aa57c16ece8842287a92b2e7208e3b8ac3Doris Liu            &bitmap,
642766431aa57c16ece8842287a92b2e7208e3b8ac3Doris Liu            Rect(bitmap.width(), bitmap.height()));
643766431aa57c16ece8842287a92b2e7208e3b8ac3Doris Liu    deferBitmapRectOp(*resolvedOp);
644766431aa57c16ece8842287a92b2e7208e3b8ac3Doris Liu}
645766431aa57c16ece8842287a92b2e7208e3b8ac3Doris Liu
646f158b49c888f722194afe5a80539a2b020c130bcChris Craikvoid FrameBuilder::deferCirclePropsOp(const CirclePropsOp& op) {
647268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik    // allocate a temporary oval op (with mAllocator, so it persists until render), so the
648268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik    // renderer doesn't have to handle the RoundRectPropsOp type, and so state baking is simple.
649268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik    float x = *(op.x);
650268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik    float y = *(op.y);
651268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik    float radius = *(op.radius);
652268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik    Rect unmappedBounds(x - radius, y - radius, x + radius, y + radius);
6537df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck    const OvalOp* resolvedOp = mAllocator.create_trivial<OvalOp>(
654268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik            unmappedBounds,
655268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik            op.localMatrix,
656e4db79de127cfe961195f52907af8451026eaa20Chris Craik            op.localClip,
657268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik            op.paint);
658268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik    deferOvalOp(*resolvedOp);
659268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik}
660268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik
661a204848b1dc63877a12e2d24108e9d8e1e691e28Chris Craikvoid FrameBuilder::deferColorOp(const ColorOp& op) {
662a204848b1dc63877a12e2d24108e9d8e1e691e28Chris Craik    BakedOpState* bakedState = tryBakeUnboundedOpState(op);
663a204848b1dc63877a12e2d24108e9d8e1e691e28Chris Craik    if (!bakedState) return; // quick rejected
664a204848b1dc63877a12e2d24108e9d8e1e691e28Chris Craik    currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Vertices);
665a204848b1dc63877a12e2d24108e9d8e1e691e28Chris Craik}
666a204848b1dc63877a12e2d24108e9d8e1e691e28Chris Craik
667f158b49c888f722194afe5a80539a2b020c130bcChris Craikvoid FrameBuilder::deferFunctorOp(const FunctorOp& op) {
6684c3980b6e43cc7c0541f54b8e7e2d9d6108be622Chris Craik    BakedOpState* bakedState = tryBakeUnboundedOpState(op);
669e29ce6f51d681af7649c0a7cddee97c471e43eb5Chris Craik    if (!bakedState) return; // quick rejected
670d2dfd8f128b632ed99418ab2b32949c939a9a369Chris Craik    currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Functor);
671e29ce6f51d681af7649c0a7cddee97c471e43eb5Chris Craik}
672e29ce6f51d681af7649c0a7cddee97c471e43eb5Chris Craik
673f158b49c888f722194afe5a80539a2b020c130bcChris Craikvoid FrameBuilder::deferLinesOp(const LinesOp& op) {
674386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    batchid_t batch = op.paint->isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices;
675268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik    deferStrokeableOp(op, batch, BakedOpState::StrokeBehavior::Forced);
676386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik}
677386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik
678f158b49c888f722194afe5a80539a2b020c130bcChris Craikvoid FrameBuilder::deferOvalOp(const OvalOp& op) {
679268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik    deferStrokeableOp(op, tessBatchId(op));
680386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik}
681386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik
682f158b49c888f722194afe5a80539a2b020c130bcChris Craikvoid FrameBuilder::deferPatchOp(const PatchOp& op) {
683f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    BakedOpState* bakedState = tryBakeOpState(op);
684f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    if (!bakedState) return; // quick rejected
685f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
686f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    if (bakedState->computedState.transform.isPureTranslate()
687b87eadda1818034ce03d85f30388384d1ac65916Chris Craik            && PaintUtils::getXfermodeDirect(op.paint) == SkXfermode::kSrcOver_Mode
688b87eadda1818034ce03d85f30388384d1ac65916Chris Craik            && hasMergeableClip(*bakedState)) {
689b250a834e433b4f483e8d8362a6fda8d6594c7b8Chris Craik        mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.bitmap->getGenerationID());
690f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        // TODO: AssetAtlas in mergeId
691f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
692f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        // Only use the MergedPatch batchId when merged, so Bitmap+Patch don't try to merge together
693f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        currentLayer().deferMergeableOp(mAllocator, bakedState, OpBatchType::MergedPatch, mergeId);
694f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    } else {
695f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        // Use Bitmap batchId since Bitmap+Patch use same shader
696f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
697f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    }
698f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik}
699f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
700f158b49c888f722194afe5a80539a2b020c130bcChris Craikvoid FrameBuilder::deferPathOp(const PathOp& op) {
7013a5811b50157e7ba50854caf957e806aee794d39Chris Craik    auto state = deferStrokeableOp(op, OpBatchType::AlphaMaskTexture);
7023a5811b50157e7ba50854caf957e806aee794d39Chris Craik    if (CC_LIKELY(state)) {
7033a5811b50157e7ba50854caf957e806aee794d39Chris Craik        mCaches.pathCache.precache(op.path, op.paint);
7043a5811b50157e7ba50854caf957e806aee794d39Chris Craik    }
705386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik}
706386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik
707f158b49c888f722194afe5a80539a2b020c130bcChris Craikvoid FrameBuilder::deferPointsOp(const PointsOp& op) {
708386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    batchid_t batch = op.paint->isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices;
709268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik    deferStrokeableOp(op, batch, BakedOpState::StrokeBehavior::Forced);
710268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik}
711268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik
712f158b49c888f722194afe5a80539a2b020c130bcChris Craikvoid FrameBuilder::deferRectOp(const RectOp& op) {
713268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik    deferStrokeableOp(op, tessBatchId(op));
714a1717271caac5e8ea3808c331d4141ac01a42134Chris Craik}
715a1717271caac5e8ea3808c331d4141ac01a42134Chris Craik
716f158b49c888f722194afe5a80539a2b020c130bcChris Craikvoid FrameBuilder::deferRoundRectOp(const RoundRectOp& op) {
7173a5811b50157e7ba50854caf957e806aee794d39Chris Craik    auto state = deferStrokeableOp(op, tessBatchId(op));
7183a5811b50157e7ba50854caf957e806aee794d39Chris Craik    if (CC_LIKELY(state && !op.paint->getPathEffect())) {
7193a5811b50157e7ba50854caf957e806aee794d39Chris Craik        // TODO: consider storing tessellation task in BakedOpState
7203a5811b50157e7ba50854caf957e806aee794d39Chris Craik        mCaches.tessellationCache.precacheRoundRect(state->computedState.transform, *(op.paint),
7213a5811b50157e7ba50854caf957e806aee794d39Chris Craik                op.unmappedBounds.getWidth(), op.unmappedBounds.getHeight(), op.rx, op.ry);
7223a5811b50157e7ba50854caf957e806aee794d39Chris Craik    }
723386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik}
724386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik
725f158b49c888f722194afe5a80539a2b020c130bcChris Craikvoid FrameBuilder::deferRoundRectPropsOp(const RoundRectPropsOp& op) {
726268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik    // allocate a temporary round rect op (with mAllocator, so it persists until render), so the
727268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik    // renderer doesn't have to handle the RoundRectPropsOp type, and so state baking is simple.
7287df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck    const RoundRectOp* resolvedOp = mAllocator.create_trivial<RoundRectOp>(
729268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik            Rect(*(op.left), *(op.top), *(op.right), *(op.bottom)),
730268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik            op.localMatrix,
731e4db79de127cfe961195f52907af8451026eaa20Chris Craik            op.localClip,
732268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik            op.paint, *op.rx, *op.ry);
733268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik    deferRoundRectOp(*resolvedOp);
734b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
735b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
736f158b49c888f722194afe5a80539a2b020c130bcChris Craikvoid FrameBuilder::deferSimpleRectsOp(const SimpleRectsOp& op) {
73715c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    BakedOpState* bakedState = tryBakeOpState(op);
73815c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    if (!bakedState) return; // quick rejected
73915c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Vertices);
740b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
741b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
742d7448e65e243754f31890baef29dff187dc2e5e5Chris Craikstatic batchid_t textBatchId(const SkPaint& paint) {
743d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik    // TODO: better handling of shader (since we won't care about color then)
744d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik    return paint.getColor() == SK_ColorBLACK ? OpBatchType::Text : OpBatchType::ColorText;
745d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik}
746d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik
747f158b49c888f722194afe5a80539a2b020c130bcChris Craikvoid FrameBuilder::deferTextOp(const TextOp& op) {
7487c02cab0b1c7db0fb786893240ec6f00f16c2ceeChris Craik    BakedOpState* bakedState = BakedOpState::tryStrokeableOpConstruct(
7497c02cab0b1c7db0fb786893240ec6f00f16c2ceeChris Craik            mAllocator, *mCanvasState.writableSnapshot(), op,
7507c02cab0b1c7db0fb786893240ec6f00f16c2ceeChris Craik            BakedOpState::StrokeBehavior::StyleDefined);
75115c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    if (!bakedState) return; // quick rejected
752a1717271caac5e8ea3808c331d4141ac01a42134Chris Craik
753d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik    batchid_t batchId = textBatchId(*(op.paint));
75415c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    if (bakedState->computedState.transform.isPureTranslate()
755b87eadda1818034ce03d85f30388384d1ac65916Chris Craik            && PaintUtils::getXfermodeDirect(op.paint) == SkXfermode::kSrcOver_Mode
756b87eadda1818034ce03d85f30388384d1ac65916Chris Craik            && hasMergeableClip(*bakedState)) {
75715c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.paint->getColor());
75815c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        currentLayer().deferMergeableOp(mAllocator, bakedState, batchId, mergeId);
75915c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    } else {
76015c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        currentLayer().deferUnmergeableOp(mAllocator, bakedState, batchId);
76115c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    }
7623a5811b50157e7ba50854caf957e806aee794d39Chris Craik
7633a5811b50157e7ba50854caf957e806aee794d39Chris Craik    FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer();
7643a5811b50157e7ba50854caf957e806aee794d39Chris Craik    auto& totalTransform = bakedState->computedState.transform;
7653a5811b50157e7ba50854caf957e806aee794d39Chris Craik    if (totalTransform.isPureTranslate() || totalTransform.isPerspective()) {
7663a5811b50157e7ba50854caf957e806aee794d39Chris Craik        fontRenderer.precache(op.paint, op.glyphs, op.glyphCount, SkMatrix::I());
7673a5811b50157e7ba50854caf957e806aee794d39Chris Craik    } else {
7683a5811b50157e7ba50854caf957e806aee794d39Chris Craik        // Partial transform case, see BakedOpDispatcher::renderTextOp
7693a5811b50157e7ba50854caf957e806aee794d39Chris Craik        float sx, sy;
7703a5811b50157e7ba50854caf957e806aee794d39Chris Craik        totalTransform.decomposeScale(sx, sy);
7713a5811b50157e7ba50854caf957e806aee794d39Chris Craik        fontRenderer.precache(op.paint, op.glyphs, op.glyphCount, SkMatrix::MakeScale(
7723a5811b50157e7ba50854caf957e806aee794d39Chris Craik                roundf(std::max(1.0f, sx)),
7733a5811b50157e7ba50854caf957e806aee794d39Chris Craik                roundf(std::max(1.0f, sy))));
7743a5811b50157e7ba50854caf957e806aee794d39Chris Craik    }
775a1717271caac5e8ea3808c331d4141ac01a42134Chris Craik}
776a1717271caac5e8ea3808c331d4141ac01a42134Chris Craik
777f158b49c888f722194afe5a80539a2b020c130bcChris Craikvoid FrameBuilder::deferTextOnPathOp(const TextOnPathOp& op) {
7784c3980b6e43cc7c0541f54b8e7e2d9d6108be622Chris Craik    BakedOpState* bakedState = tryBakeUnboundedOpState(op);
779d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik    if (!bakedState) return; // quick rejected
780d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik    currentLayer().deferUnmergeableOp(mAllocator, bakedState, textBatchId(*(op.paint)));
7813a5811b50157e7ba50854caf957e806aee794d39Chris Craik
7823a5811b50157e7ba50854caf957e806aee794d39Chris Craik    mCaches.fontRenderer.getFontRenderer().precache(
7833a5811b50157e7ba50854caf957e806aee794d39Chris Craik            op.paint, op.glyphs, op.glyphCount, SkMatrix::I());
784d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik}
785d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik
786f158b49c888f722194afe5a80539a2b020c130bcChris Craikvoid FrameBuilder::deferTextureLayerOp(const TextureLayerOp& op) {
787417ed6d4337e5409d52f58cc93677c8715193f32John Reck    if (CC_UNLIKELY(!op.layer->isRenderable())) return;
788aafb01d8ade0def3f51b74ae3bbc610c4ab33044Chris Craik
789aafb01d8ade0def3f51b74ae3bbc610c4ab33044Chris Craik    const TextureLayerOp* textureLayerOp = &op;
790aafb01d8ade0def3f51b74ae3bbc610c4ab33044Chris Craik    // Now safe to access transform (which was potentially unready at record time)
791aafb01d8ade0def3f51b74ae3bbc610c4ab33044Chris Craik    if (!op.layer->getTransform().isIdentity()) {
792aafb01d8ade0def3f51b74ae3bbc610c4ab33044Chris Craik        // non-identity transform present, so 'inject it' into op by copying + replacing matrix
793aafb01d8ade0def3f51b74ae3bbc610c4ab33044Chris Craik        Matrix4 combinedMatrix(op.localMatrix);
794aafb01d8ade0def3f51b74ae3bbc610c4ab33044Chris Craik        combinedMatrix.multiply(op.layer->getTransform());
795aafb01d8ade0def3f51b74ae3bbc610c4ab33044Chris Craik        textureLayerOp = mAllocator.create<TextureLayerOp>(op, combinedMatrix);
796aafb01d8ade0def3f51b74ae3bbc610c4ab33044Chris Craik    }
797aafb01d8ade0def3f51b74ae3bbc610c4ab33044Chris Craik    BakedOpState* bakedState = tryBakeOpState(*textureLayerOp);
798aafb01d8ade0def3f51b74ae3bbc610c4ab33044Chris Craik
799d2dfd8f128b632ed99418ab2b32949c939a9a369Chris Craik    if (!bakedState) return; // quick rejected
800d2dfd8f128b632ed99418ab2b32949c939a9a369Chris Craik    currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::TextureLayer);
801d2dfd8f128b632ed99418ab2b32949c939a9a369Chris Craik}
802d2dfd8f128b632ed99418ab2b32949c939a9a369Chris Craik
803f158b49c888f722194afe5a80539a2b020c130bcChris Craikvoid FrameBuilder::saveForLayer(uint32_t layerWidth, uint32_t layerHeight,
8048ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik        float contentTranslateX, float contentTranslateY,
8058ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik        const Rect& repaintRect,
8068ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik        const Vector3& lightCenter,
8070b7e8245db728d127ada698be63d78b33fc6e4daChris Craik        const BeginLayerOp* beginLayerOp, RenderNode* renderNode) {
808eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita    mCanvasState.save(SaveFlags::MatrixClip);
809818c9fbf1d76d5df19253ba4eb964efa939ec9ecChris Craik    mCanvasState.writableSnapshot()->initializeViewport(layerWidth, layerHeight);
8106fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik    mCanvasState.writableSnapshot()->roundRectClipState = nullptr;
81198787e6c9b2c10b1ab7820bdac168686025b924aChris Craik    mCanvasState.writableSnapshot()->setRelativeLightCenter(lightCenter);
8128ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    mCanvasState.writableSnapshot()->transform->loadTranslate(
8138ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik            contentTranslateX, contentTranslateY, 0);
8148ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    mCanvasState.writableSnapshot()->setClip(
8158ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik            repaintRect.left, repaintRect.top, repaintRect.right, repaintRect.bottom);
81698787e6c9b2c10b1ab7820bdac168686025b924aChris Craik
8178ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    // create a new layer repaint, and push its index on the stack
818f158b49c888f722194afe5a80539a2b020c130bcChris Craik    mLayerStack.push_back(mLayerBuilders.size());
819f158b49c888f722194afe5a80539a2b020c130bcChris Craik    auto newFbo = mAllocator.create<LayerBuilder>(layerWidth, layerHeight,
82084ad6149db0e1dc98ed3778ee4fc2ab685765d9aChris Craik            repaintRect, beginLayerOp, renderNode);
821f158b49c888f722194afe5a80539a2b020c130bcChris Craik    mLayerBuilders.push_back(newFbo);
822b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
823b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
824f158b49c888f722194afe5a80539a2b020c130bcChris Craikvoid FrameBuilder::restoreForLayer() {
8250b7e8245db728d127ada698be63d78b33fc6e4daChris Craik    // restore canvas, and pop finished layer off of the stack
8266fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik    mCanvasState.restore();
8270b7e8245db728d127ada698be63d78b33fc6e4daChris Craik    mLayerStack.pop_back();
8280b7e8245db728d127ada698be63d78b33fc6e4daChris Craik}
829b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
830b87eadda1818034ce03d85f30388384d1ac65916Chris Craik// TODO: defer time rejection (when bounds become empty) + tests
831b87eadda1818034ce03d85f30388384d1ac65916Chris Craik// Option - just skip layers with no bounds at playback + defer?
832f158b49c888f722194afe5a80539a2b020c130bcChris Craikvoid FrameBuilder::deferBeginLayerOp(const BeginLayerOp& op) {
8338ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    uint32_t layerWidth = (uint32_t) op.unmappedBounds.getWidth();
8348ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    uint32_t layerHeight = (uint32_t) op.unmappedBounds.getHeight();
8358ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik
8368ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    auto previous = mCanvasState.currentSnapshot();
8378ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    Vector3 lightCenter = previous->getRelativeLightCenter();
8388ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik
8398ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    // Combine all transforms used to present saveLayer content:
8408ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    // parent content transform * canvas transform * bounds offset
841b87eadda1818034ce03d85f30388384d1ac65916Chris Craik    Matrix4 contentTransform(*(previous->transform));
8428ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    contentTransform.multiply(op.localMatrix);
8438ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    contentTransform.translate(op.unmappedBounds.left, op.unmappedBounds.top);
8448ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik
8458ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    Matrix4 inverseContentTransform;
8468ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    inverseContentTransform.loadInverse(contentTransform);
8478ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik
8488ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    // map the light center into layer-relative space
8498ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    inverseContentTransform.mapPoint3d(lightCenter);
8508ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik
8518ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    // Clip bounds of temporary layer to parent's clip rect, so:
8528ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    Rect saveLayerBounds(layerWidth, layerHeight);
8538ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    //     1) transform Rect(width, height) into parent's space
8548ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    //        note: left/top offsets put in contentTransform above
8558ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    contentTransform.mapRect(saveLayerBounds);
8568ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    //     2) intersect with parent's clip
8578ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    saveLayerBounds.doIntersect(previous->getRenderTargetClip());
8588ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    //     3) and transform back
8598ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    inverseContentTransform.mapRect(saveLayerBounds);
8608ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    saveLayerBounds.doIntersect(Rect(layerWidth, layerHeight));
8618ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    saveLayerBounds.roundOut();
8628ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik
8638ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    // if bounds are reduced, will clip the layer's area by reducing required bounds...
8648ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    layerWidth = saveLayerBounds.getWidth();
8658ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    layerHeight = saveLayerBounds.getHeight();
8668ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    // ...and shifting drawing content to account for left/top side clipping
8678ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    float contentTranslateX = -saveLayerBounds.left;
8688ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    float contentTranslateY = -saveLayerBounds.top;
8698ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik
8708ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik    saveForLayer(layerWidth, layerHeight,
8718ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik            contentTranslateX, contentTranslateY,
8728ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik            Rect(layerWidth, layerHeight),
8738ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik            lightCenter,
8748ecf41c61a5185207a21d64681e8ebc2502b7b2aChris Craik            &op, nullptr);
8750b7e8245db728d127ada698be63d78b33fc6e4daChris Craik}
8766fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik
877f158b49c888f722194afe5a80539a2b020c130bcChris Craikvoid FrameBuilder::deferEndLayerOp(const EndLayerOp& /* ignored */) {
8780b7e8245db728d127ada698be63d78b33fc6e4daChris Craik    const BeginLayerOp& beginLayerOp = *currentLayer().beginLayerOp;
8796fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik    int finishedLayerIndex = mLayerStack.back();
8800b7e8245db728d127ada698be63d78b33fc6e4daChris Craik
8810b7e8245db728d127ada698be63d78b33fc6e4daChris Craik    restoreForLayer();
8826fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik
883c9bb1a38d356087a4e5578307a6839eac0a1e6eeJohn Reck    // saveLayer will clip & translate the draw contents, so we need
884c9bb1a38d356087a4e5578307a6839eac0a1e6eeJohn Reck    // to translate the drawLayer by how much the contents was translated
885c9bb1a38d356087a4e5578307a6839eac0a1e6eeJohn Reck    // TODO: Unify this with beginLayerOp so we don't have to calculate this
886c9bb1a38d356087a4e5578307a6839eac0a1e6eeJohn Reck    // twice
887c9bb1a38d356087a4e5578307a6839eac0a1e6eeJohn Reck    uint32_t layerWidth = (uint32_t) beginLayerOp.unmappedBounds.getWidth();
888c9bb1a38d356087a4e5578307a6839eac0a1e6eeJohn Reck    uint32_t layerHeight = (uint32_t) beginLayerOp.unmappedBounds.getHeight();
889c9bb1a38d356087a4e5578307a6839eac0a1e6eeJohn Reck
890c9bb1a38d356087a4e5578307a6839eac0a1e6eeJohn Reck    auto previous = mCanvasState.currentSnapshot();
891c9bb1a38d356087a4e5578307a6839eac0a1e6eeJohn Reck    Vector3 lightCenter = previous->getRelativeLightCenter();
892c9bb1a38d356087a4e5578307a6839eac0a1e6eeJohn Reck
893c9bb1a38d356087a4e5578307a6839eac0a1e6eeJohn Reck    // Combine all transforms used to present saveLayer content:
894c9bb1a38d356087a4e5578307a6839eac0a1e6eeJohn Reck    // parent content transform * canvas transform * bounds offset
895c9bb1a38d356087a4e5578307a6839eac0a1e6eeJohn Reck    Matrix4 contentTransform(*(previous->transform));
896c9bb1a38d356087a4e5578307a6839eac0a1e6eeJohn Reck    contentTransform.multiply(beginLayerOp.localMatrix);
897c9bb1a38d356087a4e5578307a6839eac0a1e6eeJohn Reck    contentTransform.translate(beginLayerOp.unmappedBounds.left,
898c9bb1a38d356087a4e5578307a6839eac0a1e6eeJohn Reck            beginLayerOp.unmappedBounds.top);
899c9bb1a38d356087a4e5578307a6839eac0a1e6eeJohn Reck
900c9bb1a38d356087a4e5578307a6839eac0a1e6eeJohn Reck    Matrix4 inverseContentTransform;
901c9bb1a38d356087a4e5578307a6839eac0a1e6eeJohn Reck    inverseContentTransform.loadInverse(contentTransform);
902c9bb1a38d356087a4e5578307a6839eac0a1e6eeJohn Reck
903c9bb1a38d356087a4e5578307a6839eac0a1e6eeJohn Reck    // map the light center into layer-relative space
904c9bb1a38d356087a4e5578307a6839eac0a1e6eeJohn Reck    inverseContentTransform.mapPoint3d(lightCenter);
905c9bb1a38d356087a4e5578307a6839eac0a1e6eeJohn Reck
906c9bb1a38d356087a4e5578307a6839eac0a1e6eeJohn Reck    // Clip bounds of temporary layer to parent's clip rect, so:
907c9bb1a38d356087a4e5578307a6839eac0a1e6eeJohn Reck    Rect saveLayerBounds(layerWidth, layerHeight);
908c9bb1a38d356087a4e5578307a6839eac0a1e6eeJohn Reck    //     1) transform Rect(width, height) into parent's space
909c9bb1a38d356087a4e5578307a6839eac0a1e6eeJohn Reck    //        note: left/top offsets put in contentTransform above
910c9bb1a38d356087a4e5578307a6839eac0a1e6eeJohn Reck    contentTransform.mapRect(saveLayerBounds);
911c9bb1a38d356087a4e5578307a6839eac0a1e6eeJohn Reck    //     2) intersect with parent's clip
912c9bb1a38d356087a4e5578307a6839eac0a1e6eeJohn Reck    saveLayerBounds.doIntersect(previous->getRenderTargetClip());
913c9bb1a38d356087a4e5578307a6839eac0a1e6eeJohn Reck    //     3) and transform back
914c9bb1a38d356087a4e5578307a6839eac0a1e6eeJohn Reck    inverseContentTransform.mapRect(saveLayerBounds);
915c9bb1a38d356087a4e5578307a6839eac0a1e6eeJohn Reck    saveLayerBounds.doIntersect(Rect(layerWidth, layerHeight));
916c9bb1a38d356087a4e5578307a6839eac0a1e6eeJohn Reck    saveLayerBounds.roundOut();
917c9bb1a38d356087a4e5578307a6839eac0a1e6eeJohn Reck
918c9bb1a38d356087a4e5578307a6839eac0a1e6eeJohn Reck    Matrix4 localMatrix(beginLayerOp.localMatrix);
919c9bb1a38d356087a4e5578307a6839eac0a1e6eeJohn Reck    localMatrix.translate(saveLayerBounds.left, saveLayerBounds.top);
920c9bb1a38d356087a4e5578307a6839eac0a1e6eeJohn Reck
9216fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik    // record the draw operation into the previous layer's list of draw commands
9226fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik    // uses state from the associated beginLayerOp, since it has all the state needed for drawing
9237df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck    LayerOp* drawLayerOp = mAllocator.create_trivial<LayerOp>(
9246fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik            beginLayerOp.unmappedBounds,
925c9bb1a38d356087a4e5578307a6839eac0a1e6eeJohn Reck            localMatrix,
926e4db79de127cfe961195f52907af8451026eaa20Chris Craik            beginLayerOp.localClip,
927818c9fbf1d76d5df19253ba4eb964efa939ec9ecChris Craik            beginLayerOp.paint,
928f158b49c888f722194afe5a80539a2b020c130bcChris Craik            &(mLayerBuilders[finishedLayerIndex]->offscreenBuffer));
9296fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik    BakedOpState* bakedOpState = tryBakeOpState(*drawLayerOp);
9306fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik
9316fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik    if (bakedOpState) {
9326fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik        // Layer will be drawn into parent layer (which is now current, since we popped mLayerStack)
9336fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik        currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Bitmap);
9346fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik    } else {
9356fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik        // Layer won't be drawn - delete its drawing batches to prevent it from doing any work
936b87eadda1818034ce03d85f30388384d1ac65916Chris Craik        // TODO: need to prevent any render work from being done
937b87eadda1818034ce03d85f30388384d1ac65916Chris Craik        // - create layerop earlier for reject purposes?
938f158b49c888f722194afe5a80539a2b020c130bcChris Craik        mLayerBuilders[finishedLayerIndex]->clear();
9396fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik        return;
940b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    }
941b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
942b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
943f158b49c888f722194afe5a80539a2b020c130bcChris Craikvoid FrameBuilder::deferBeginUnclippedLayerOp(const BeginUnclippedLayerOp& op) {
944b87eadda1818034ce03d85f30388384d1ac65916Chris Craik    Matrix4 boundsTransform(*(mCanvasState.currentSnapshot()->transform));
945b87eadda1818034ce03d85f30388384d1ac65916Chris Craik    boundsTransform.multiply(op.localMatrix);
946b87eadda1818034ce03d85f30388384d1ac65916Chris Craik
947b87eadda1818034ce03d85f30388384d1ac65916Chris Craik    Rect dstRect(op.unmappedBounds);
948b87eadda1818034ce03d85f30388384d1ac65916Chris Craik    boundsTransform.mapRect(dstRect);
94945e83338e525070702c12d68d904107989f439f9Chris Craik    dstRect.roundOut();
950b87eadda1818034ce03d85f30388384d1ac65916Chris Craik    dstRect.doIntersect(mCanvasState.currentSnapshot()->getRenderTargetClip());
951b87eadda1818034ce03d85f30388384d1ac65916Chris Craik
9524876de16e34622634266d09522c9153c78c7c2fbChris Craik    if (dstRect.isEmpty()) {
9534876de16e34622634266d09522c9153c78c7c2fbChris Craik        // Unclipped layer rejected - push a null op, so next EndUnclippedLayerOp is ignored
9544876de16e34622634266d09522c9153c78c7c2fbChris Craik        currentLayer().activeUnclippedSaveLayers.push_back(nullptr);
9554876de16e34622634266d09522c9153c78c7c2fbChris Craik    } else {
9564876de16e34622634266d09522c9153c78c7c2fbChris Craik        // Allocate a holding position for the layer object (copyTo will produce, copyFrom will consume)
9574876de16e34622634266d09522c9153c78c7c2fbChris Craik        OffscreenBuffer** layerHandle = mAllocator.create<OffscreenBuffer*>(nullptr);
9584876de16e34622634266d09522c9153c78c7c2fbChris Craik
9594876de16e34622634266d09522c9153c78c7c2fbChris Craik        /**
9604876de16e34622634266d09522c9153c78c7c2fbChris Craik         * First, defer an operation to copy out the content from the rendertarget into a layer.
9614876de16e34622634266d09522c9153c78c7c2fbChris Craik         */
9624876de16e34622634266d09522c9153c78c7c2fbChris Craik        auto copyToOp = mAllocator.create_trivial<CopyToLayerOp>(op, layerHandle);
9634876de16e34622634266d09522c9153c78c7c2fbChris Craik        BakedOpState* bakedState = BakedOpState::directConstruct(mAllocator,
9644876de16e34622634266d09522c9153c78c7c2fbChris Craik                &(currentLayer().repaintClip), dstRect, *copyToOp);
9654876de16e34622634266d09522c9153c78c7c2fbChris Craik        currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::CopyToLayer);
9664876de16e34622634266d09522c9153c78c7c2fbChris Craik
9674876de16e34622634266d09522c9153c78c7c2fbChris Craik        /**
9684876de16e34622634266d09522c9153c78c7c2fbChris Craik         * Defer a clear rect, so that clears from multiple unclipped layers can be drawn
9694876de16e34622634266d09522c9153c78c7c2fbChris Craik         * both 1) simultaneously, and 2) as long after the copyToLayer executes as possible
9704876de16e34622634266d09522c9153c78c7c2fbChris Craik         */
9714876de16e34622634266d09522c9153c78c7c2fbChris Craik        currentLayer().deferLayerClear(dstRect);
9724876de16e34622634266d09522c9153c78c7c2fbChris Craik
9734876de16e34622634266d09522c9153c78c7c2fbChris Craik        /**
9744876de16e34622634266d09522c9153c78c7c2fbChris Craik         * And stash an operation to copy that layer back under the rendertarget until
9754876de16e34622634266d09522c9153c78c7c2fbChris Craik         * a balanced EndUnclippedLayerOp is seen
9764876de16e34622634266d09522c9153c78c7c2fbChris Craik         */
9774876de16e34622634266d09522c9153c78c7c2fbChris Craik        auto copyFromOp = mAllocator.create_trivial<CopyFromLayerOp>(op, layerHandle);
9784876de16e34622634266d09522c9153c78c7c2fbChris Craik        bakedState = BakedOpState::directConstruct(mAllocator,
9794876de16e34622634266d09522c9153c78c7c2fbChris Craik                &(currentLayer().repaintClip), dstRect, *copyFromOp);
9804876de16e34622634266d09522c9153c78c7c2fbChris Craik        currentLayer().activeUnclippedSaveLayers.push_back(bakedState);
9814876de16e34622634266d09522c9153c78c7c2fbChris Craik    }
982b87eadda1818034ce03d85f30388384d1ac65916Chris Craik}
983b87eadda1818034ce03d85f30388384d1ac65916Chris Craik
984f158b49c888f722194afe5a80539a2b020c130bcChris Craikvoid FrameBuilder::deferEndUnclippedLayerOp(const EndUnclippedLayerOp& /* ignored */) {
985b87eadda1818034ce03d85f30388384d1ac65916Chris Craik    LOG_ALWAYS_FATAL_IF(currentLayer().activeUnclippedSaveLayers.empty(), "no layer to end!");
986b87eadda1818034ce03d85f30388384d1ac65916Chris Craik
987b87eadda1818034ce03d85f30388384d1ac65916Chris Craik    BakedOpState* copyFromLayerOp = currentLayer().activeUnclippedSaveLayers.back();
988b87eadda1818034ce03d85f30388384d1ac65916Chris Craik    currentLayer().activeUnclippedSaveLayers.pop_back();
9894876de16e34622634266d09522c9153c78c7c2fbChris Craik    if (copyFromLayerOp) {
9904876de16e34622634266d09522c9153c78c7c2fbChris Craik        currentLayer().deferUnmergeableOp(mAllocator, copyFromLayerOp, OpBatchType::CopyFromLayer);
9914876de16e34622634266d09522c9153c78c7c2fbChris Craik    }
992b87eadda1818034ce03d85f30388384d1ac65916Chris Craik}
993b87eadda1818034ce03d85f30388384d1ac65916Chris Craik
9943a5811b50157e7ba50854caf957e806aee794d39Chris Craikvoid FrameBuilder::finishDefer() {
9953a5811b50157e7ba50854caf957e806aee794d39Chris Craik    mCaches.fontRenderer.endPrecaching();
9963a5811b50157e7ba50854caf957e806aee794d39Chris Craik}
9973a5811b50157e7ba50854caf957e806aee794d39Chris Craik
998b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik} // namespace uirenderer
999b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik} // namespace android
1000