1113e0824d6bddf4376240681f9cf6a2deded9498John Reck/*
2113e0824d6bddf4376240681f9cf6a2deded9498John Reck * Copyright (C) 2014 The Android Open Source Project
3113e0824d6bddf4376240681f9cf6a2deded9498John Reck *
4113e0824d6bddf4376240681f9cf6a2deded9498John Reck * Licensed under the Apache License, Version 2.0 (the "License");
5113e0824d6bddf4376240681f9cf6a2deded9498John Reck * you may not use this file except in compliance with the License.
6113e0824d6bddf4376240681f9cf6a2deded9498John Reck * You may obtain a copy of the License at
7113e0824d6bddf4376240681f9cf6a2deded9498John Reck *
8113e0824d6bddf4376240681f9cf6a2deded9498John Reck *      http://www.apache.org/licenses/LICENSE-2.0
9113e0824d6bddf4376240681f9cf6a2deded9498John Reck *
10113e0824d6bddf4376240681f9cf6a2deded9498John Reck * Unless required by applicable law or agreed to in writing, software
11113e0824d6bddf4376240681f9cf6a2deded9498John Reck * distributed under the License is distributed on an "AS IS" BASIS,
12113e0824d6bddf4376240681f9cf6a2deded9498John Reck * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13113e0824d6bddf4376240681f9cf6a2deded9498John Reck * See the License for the specific language governing permissions and
14113e0824d6bddf4376240681f9cf6a2deded9498John Reck * limitations under the License.
15113e0824d6bddf4376240681f9cf6a2deded9498John Reck */
16113e0824d6bddf4376240681f9cf6a2deded9498John Reck
17113e0824d6bddf4376240681f9cf6a2deded9498John Reck#define ATRACE_TAG ATRACE_TAG_VIEW
1880d4902196899d1325cd9f52c06ae0174cf9bd4cChris Craik#define LOG_TAG "OpenGLRenderer"
19113e0824d6bddf4376240681f9cf6a2deded9498John Reck
20113e0824d6bddf4376240681f9cf6a2deded9498John Reck#include "RenderNode.h"
21113e0824d6bddf4376240681f9cf6a2deded9498John Reck
22e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck#include <algorithm>
23c25e506f7fc08790c0532f927f31310a2ca778b7John Reck#include <string>
24e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck
25113e0824d6bddf4376240681f9cf6a2deded9498John Reck#include <SkCanvas.h>
26113e0824d6bddf4376240681f9cf6a2deded9498John Reck#include <algorithm>
27113e0824d6bddf4376240681f9cf6a2deded9498John Reck
28113e0824d6bddf4376240681f9cf6a2deded9498John Reck
29e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck#include "DamageAccumulator.h"
30113e0824d6bddf4376240681f9cf6a2deded9498John Reck#include "Debug.h"
31113e0824d6bddf4376240681f9cf6a2deded9498John Reck#include "DisplayListOp.h"
3225fbb3fa1138675379102a44405852555cefccbdJohn Reck#include "LayerRenderer.h"
3325fbb3fa1138675379102a44405852555cefccbdJohn Reck#include "OpenGLRenderer.h"
342dc236b2bae13b9a0ed9b3f7320502aecd7983b3Tom Hudson#include "TreeInfo.h"
35e0bb87d4bdbd3b08ab6a8569c8e564ed59b8a5a7Chris Craik#include "utils/MathUtils.h"
3670850ea258cbf91477efa57a1f1a23cc0044cc93Chris Craik#include "utils/TraceUtils.h"
37998a6d81896df8b662cc10ddeb35087b78b38d72John Reck#include "renderthread/CanvasContext.h"
38113e0824d6bddf4376240681f9cf6a2deded9498John Reck
39113e0824d6bddf4376240681f9cf6a2deded9498John Recknamespace android {
40113e0824d6bddf4376240681f9cf6a2deded9498John Recknamespace uirenderer {
41113e0824d6bddf4376240681f9cf6a2deded9498John Reck
42443a714fa7c0dd07fee3527cc5bc3d3ca1fb7d44John Reckvoid RenderNode::debugDumpLayers(const char* prefix) {
43443a714fa7c0dd07fee3527cc5bc3d3ca1fb7d44John Reck    if (mLayer) {
44443a714fa7c0dd07fee3527cc5bc3d3ca1fb7d44John Reck        ALOGD("%sNode %p (%s) has layer %p (fbo = %u, wasBuildLayered = %s)",
45443a714fa7c0dd07fee3527cc5bc3d3ca1fb7d44John Reck                prefix, this, getName(), mLayer, mLayer->getFbo(),
46443a714fa7c0dd07fee3527cc5bc3d3ca1fb7d44John Reck                mLayer->wasBuildLayered ? "true" : "false");
47443a714fa7c0dd07fee3527cc5bc3d3ca1fb7d44John Reck    }
48443a714fa7c0dd07fee3527cc5bc3d3ca1fb7d44John Reck    if (mDisplayListData) {
49443a714fa7c0dd07fee3527cc5bc3d3ca1fb7d44John Reck        for (size_t i = 0; i < mDisplayListData->children().size(); i++) {
50443a714fa7c0dd07fee3527cc5bc3d3ca1fb7d44John Reck            mDisplayListData->children()[i]->mRenderNode->debugDumpLayers(prefix);
51443a714fa7c0dd07fee3527cc5bc3d3ca1fb7d44John Reck        }
52443a714fa7c0dd07fee3527cc5bc3d3ca1fb7d44John Reck    }
53443a714fa7c0dd07fee3527cc5bc3d3ca1fb7d44John Reck}
54443a714fa7c0dd07fee3527cc5bc3d3ca1fb7d44John Reck
558de65a8e05285df52a1e6f0c1d5616dd233298a7John ReckRenderNode::RenderNode()
56ff941dcd815021bb20d6504eb486acb1e50592c3John Reck        : mDirtyPropertyFields(0)
578de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck        , mNeedsDisplayListDataSync(false)
58d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik        , mDisplayListData(nullptr)
59d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik        , mStagingDisplayListData(nullptr)
6068bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck        , mAnimatorManager(*this)
61d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik        , mLayer(nullptr)
62dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck        , mParentCount(0) {
63113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
64113e0824d6bddf4376240681f9cf6a2deded9498John Reck
65113e0824d6bddf4376240681f9cf6a2deded9498John ReckRenderNode::~RenderNode() {
66dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck    deleteDisplayListData();
678de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck    delete mStagingDisplayListData;
680e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck    if (mLayer) {
690e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck        ALOGW("Memory Warning: Layer %p missed its detachment, held on to for far too long!", mLayer);
700e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck        mLayer->postDecStrong();
71d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik        mLayer = nullptr;
720e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck    }
73113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
74113e0824d6bddf4376240681f9cf6a2deded9498John Reck
758de65a8e05285df52a1e6f0c1d5616dd233298a7John Reckvoid RenderNode::setStagingDisplayList(DisplayListData* data) {
768de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck    mNeedsDisplayListDataSync = true;
778de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck    delete mStagingDisplayListData;
788de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck    mStagingDisplayListData = data;
79113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
80113e0824d6bddf4376240681f9cf6a2deded9498John Reck
81113e0824d6bddf4376240681f9cf6a2deded9498John Reck/**
82113e0824d6bddf4376240681f9cf6a2deded9498John Reck * This function is a simplified version of replay(), where we simply retrieve and log the
83113e0824d6bddf4376240681f9cf6a2deded9498John Reck * display list. This function should remain in sync with the replay() function.
84113e0824d6bddf4376240681f9cf6a2deded9498John Reck */
85113e0824d6bddf4376240681f9cf6a2deded9498John Reckvoid RenderNode::output(uint32_t level) {
86bf72eb80d9e00fe2fa360dff0ef581ea27c8e965Chris Craik    ALOGD("%*sStart display list (%p, %s%s%s%s%s%s)", (level - 1) * 2, "", this,
87b5a5435a77bae40b55053ccde93b1ed45d0e8329Chris Craik            getName(),
8843a1d31a2837406a437937b340db85c1a05ed1edChris Craik            (MathUtils::isZero(properties().getAlpha()) ? ", zero alpha" : ""),
89b5a5435a77bae40b55053ccde93b1ed45d0e8329Chris Craik            (properties().hasShadow() ? ", casting shadow" : ""),
90b5a5435a77bae40b55053ccde93b1ed45d0e8329Chris Craik            (isRenderable() ? "" : ", empty"),
91bf72eb80d9e00fe2fa360dff0ef581ea27c8e965Chris Craik            (properties().getProjectBackwards() ? ", projected" : ""),
92d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik            (mLayer != nullptr ? ", on HW Layer" : ""));
93113e0824d6bddf4376240681f9cf6a2deded9498John Reck    ALOGD("%*s%s %d", level * 2, "", "Save",
94113e0824d6bddf4376240681f9cf6a2deded9498John Reck            SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
95113e0824d6bddf4376240681f9cf6a2deded9498John Reck
96d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    properties().debugOutputProperties(level);
97113e0824d6bddf4376240681f9cf6a2deded9498John Reck    int flags = DisplayListOp::kOpLogFlag_Recurse;
98dc0349bebafb486b044655f3c146450b527a5123John Reck    if (mDisplayListData) {
998afd0f245cc0c4a0366f39f41b5f78e47ee83be3Chris Craik        // TODO: consider printing the chunk boundaries here
100dc0349bebafb486b044655f3c146450b527a5123John Reck        for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) {
101dc0349bebafb486b044655f3c146450b527a5123John Reck            mDisplayListData->displayListOps[i]->output(level, flags);
102dc0349bebafb486b044655f3c146450b527a5123John Reck        }
103113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
104113e0824d6bddf4376240681f9cf6a2deded9498John Reck
1053f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik    ALOGD("%*sDone (%p, %s)", (level - 1) * 2, "", this, getName());
106113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
107113e0824d6bddf4376240681f9cf6a2deded9498John Reck
108fe5e7b7346a54537b980796ceeca66bfdbd05561John Reckint RenderNode::getDebugSize() {
109fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck    int size = sizeof(RenderNode);
110fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck    if (mStagingDisplayListData) {
1118afd0f245cc0c4a0366f39f41b5f78e47ee83be3Chris Craik        size += mStagingDisplayListData->getUsedSize();
112fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck    }
113fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck    if (mDisplayListData && mDisplayListData != mStagingDisplayListData) {
1148afd0f245cc0c4a0366f39f41b5f78e47ee83be3Chris Craik        size += mDisplayListData->getUsedSize();
115fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck    }
116fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck    return size;
117fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck}
118fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck
119f4198b713e43c0c0f9adac74203cf24c2a49b802John Reckvoid RenderNode::prepareTree(TreeInfo& info) {
120f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck    ATRACE_CALL();
12169e5adffb19135d51bde8e458f4907d7265f3e23Chris Craik    LOG_ALWAYS_FATAL_IF(!info.damageAccumulator, "DamageAccumulator missing");
122f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck
123a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craik    // Functors don't correctly handle stencil usage of overdraw debugging - shove 'em in a layer.
124a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craik    bool functorsNeedLayer = Properties::debugOverdraw;
125a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craik
126a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craik    prepareTreeImpl(info, functorsNeedLayer);
127f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck}
128f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck
12968bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reckvoid RenderNode::addAnimator(const sp<BaseRenderNodeAnimator>& animator) {
13068bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck    mAnimatorManager.addAnimator(animator);
13168bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck}
13268bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck
133e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reckvoid RenderNode::damageSelf(TreeInfo& info) {
134ce9f308063cc2bc7851f3f58c881da8a7271eeccJohn Reck    if (isRenderable()) {
135293e868bfc1b07aad2cb2ebeee5b07424852c510John Reck        if (properties().getClipDamageToBounds()) {
136a447d29c65fb811cd184775a3476101a1cede929John Reck            info.damageAccumulator->dirty(0, 0, properties().getWidth(), properties().getHeight());
137a447d29c65fb811cd184775a3476101a1cede929John Reck        } else {
138a447d29c65fb811cd184775a3476101a1cede929John Reck            // Hope this is big enough?
139a447d29c65fb811cd184775a3476101a1cede929John Reck            // TODO: Get this from the display list ops or something
140a447d29c65fb811cd184775a3476101a1cede929John Reck            info.damageAccumulator->dirty(INT_MIN, INT_MIN, INT_MAX, INT_MAX);
141a447d29c65fb811cd184775a3476101a1cede929John Reck        }
142e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck    }
143e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck}
144e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck
145a7c2ea20c43ab797bef5801530687e22e83def8fJohn Reckvoid RenderNode::prepareLayer(TreeInfo& info, uint32_t dirtyMask) {
146856f0cc3e382206e67c1a62fe878627d7dd9ada5Chris Craik    LayerType layerType = properties().effectiveLayerType();
147182952f5eeefc2a21d76d4664ada0e2d78c1105cChris Craik    if (CC_UNLIKELY(layerType == LayerType::RenderLayer)) {
148a7c2ea20c43ab797bef5801530687e22e83def8fJohn Reck        // Damage applied so far needs to affect our parent, but does not require
149a7c2ea20c43ab797bef5801530687e22e83def8fJohn Reck        // the layer to be updated. So we pop/push here to clear out the current
150a7c2ea20c43ab797bef5801530687e22e83def8fJohn Reck        // damage and get a clean state for display list or children updates to
151a7c2ea20c43ab797bef5801530687e22e83def8fJohn Reck        // affect, which will require the layer to be updated
152a7c2ea20c43ab797bef5801530687e22e83def8fJohn Reck        info.damageAccumulator->popTransform();
153a7c2ea20c43ab797bef5801530687e22e83def8fJohn Reck        info.damageAccumulator->pushTransform(this);
154a7c2ea20c43ab797bef5801530687e22e83def8fJohn Reck        if (dirtyMask & DISPLAY_LIST) {
155a7c2ea20c43ab797bef5801530687e22e83def8fJohn Reck            damageSelf(info);
156a7c2ea20c43ab797bef5801530687e22e83def8fJohn Reck        }
15725fbb3fa1138675379102a44405852555cefccbdJohn Reck    }
15825fbb3fa1138675379102a44405852555cefccbdJohn Reck}
15925fbb3fa1138675379102a44405852555cefccbdJohn Reck
16025fbb3fa1138675379102a44405852555cefccbdJohn Reckvoid RenderNode::pushLayerUpdate(TreeInfo& info) {
161856f0cc3e382206e67c1a62fe878627d7dd9ada5Chris Craik    LayerType layerType = properties().effectiveLayerType();
16225fbb3fa1138675379102a44405852555cefccbdJohn Reck    // If we are not a layer OR we cannot be rendered (eg, view was detached)
16325fbb3fa1138675379102a44405852555cefccbdJohn Reck    // we need to destroy any Layers we may have had previously
164182952f5eeefc2a21d76d4664ada0e2d78c1105cChris Craik    if (CC_LIKELY(layerType != LayerType::RenderLayer) || CC_UNLIKELY(!isRenderable())) {
16525fbb3fa1138675379102a44405852555cefccbdJohn Reck        if (CC_UNLIKELY(mLayer)) {
16625fbb3fa1138675379102a44405852555cefccbdJohn Reck            LayerRenderer::destroyLayer(mLayer);
167d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik            mLayer = nullptr;
16825fbb3fa1138675379102a44405852555cefccbdJohn Reck        }
16925fbb3fa1138675379102a44405852555cefccbdJohn Reck        return;
17025fbb3fa1138675379102a44405852555cefccbdJohn Reck    }
17125fbb3fa1138675379102a44405852555cefccbdJohn Reck
17269e5adffb19135d51bde8e458f4907d7265f3e23Chris Craik    bool transformUpdateNeeded = false;
17325fbb3fa1138675379102a44405852555cefccbdJohn Reck    if (!mLayer) {
1743b20251a355c88193c439f928a84ae69483fb488John Reck        mLayer = LayerRenderer::createRenderLayer(info.renderState, getWidth(), getHeight());
17525fbb3fa1138675379102a44405852555cefccbdJohn Reck        applyLayerPropertiesToLayer(info);
17625fbb3fa1138675379102a44405852555cefccbdJohn Reck        damageSelf(info);
17769e5adffb19135d51bde8e458f4907d7265f3e23Chris Craik        transformUpdateNeeded = true;
17825fbb3fa1138675379102a44405852555cefccbdJohn Reck    } else if (mLayer->layer.getWidth() != getWidth() || mLayer->layer.getHeight() != getHeight()) {
179c25e506f7fc08790c0532f927f31310a2ca778b7John Reck        if (!LayerRenderer::resizeLayer(mLayer, getWidth(), getHeight())) {
180c25e506f7fc08790c0532f927f31310a2ca778b7John Reck            LayerRenderer::destroyLayer(mLayer);
181d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik            mLayer = nullptr;
182c25e506f7fc08790c0532f927f31310a2ca778b7John Reck        }
18325fbb3fa1138675379102a44405852555cefccbdJohn Reck        damageSelf(info);
18469e5adffb19135d51bde8e458f4907d7265f3e23Chris Craik        transformUpdateNeeded = true;
18569e5adffb19135d51bde8e458f4907d7265f3e23Chris Craik    }
18669e5adffb19135d51bde8e458f4907d7265f3e23Chris Craik
18725fbb3fa1138675379102a44405852555cefccbdJohn Reck    SkRect dirty;
18825fbb3fa1138675379102a44405852555cefccbdJohn Reck    info.damageAccumulator->peekAtDirty(&dirty);
18925fbb3fa1138675379102a44405852555cefccbdJohn Reck
190c25e506f7fc08790c0532f927f31310a2ca778b7John Reck    if (!mLayer) {
1910e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck        Caches::getInstance().dumpMemoryUsage();
192c25e506f7fc08790c0532f927f31310a2ca778b7John Reck        if (info.errorHandler) {
193c25e506f7fc08790c0532f927f31310a2ca778b7John Reck            std::string msg = "Unable to create layer for ";
194c25e506f7fc08790c0532f927f31310a2ca778b7John Reck            msg += getName();
195c25e506f7fc08790c0532f927f31310a2ca778b7John Reck            info.errorHandler->onError(msg);
196c25e506f7fc08790c0532f927f31310a2ca778b7John Reck        }
197c25e506f7fc08790c0532f927f31310a2ca778b7John Reck        return;
198c25e506f7fc08790c0532f927f31310a2ca778b7John Reck    }
199c25e506f7fc08790c0532f927f31310a2ca778b7John Reck
200c71bfcaa182e3d4fd9874362d3b4781fda934a21Chris Craik    if (transformUpdateNeeded) {
201c71bfcaa182e3d4fd9874362d3b4781fda934a21Chris Craik        // update the transform in window of the layer to reset its origin wrt light source position
202c71bfcaa182e3d4fd9874362d3b4781fda934a21Chris Craik        Matrix4 windowTransform;
203c71bfcaa182e3d4fd9874362d3b4781fda934a21Chris Craik        info.damageAccumulator->computeCurrentTransform(&windowTransform);
204c71bfcaa182e3d4fd9874362d3b4781fda934a21Chris Craik        mLayer->setWindowTransform(windowTransform);
205c71bfcaa182e3d4fd9874362d3b4781fda934a21Chris Craik    }
206c79eabcd3c6306bb2ec75f9584b79e661f265127John Reck
207c79eabcd3c6306bb2ec75f9584b79e661f265127John Reck    if (dirty.intersect(0, 0, getWidth(), getHeight())) {
20871487eb0ceb2b7dea02649e78d99bb5952f5eaefMike Reed        dirty.roundOut(&dirty);
20925fbb3fa1138675379102a44405852555cefccbdJohn Reck        mLayer->updateDeferred(this, dirty.fLeft, dirty.fTop, dirty.fRight, dirty.fBottom);
21025fbb3fa1138675379102a44405852555cefccbdJohn Reck    }
21125fbb3fa1138675379102a44405852555cefccbdJohn Reck    // This is not inside the above if because we may have called
21225fbb3fa1138675379102a44405852555cefccbdJohn Reck    // updateDeferred on a previous prepare pass that didn't have a renderer
21325fbb3fa1138675379102a44405852555cefccbdJohn Reck    if (info.renderer && mLayer->deferredUpdateScheduled) {
21425fbb3fa1138675379102a44405852555cefccbdJohn Reck        info.renderer->pushLayerUpdate(mLayer);
21525fbb3fa1138675379102a44405852555cefccbdJohn Reck    }
216998a6d81896df8b662cc10ddeb35087b78b38d72John Reck
21700e79c9947b741194ff6c0d08ede9b3befbf9c9dJohn Reck    if (info.canvasContext) {
21800e79c9947b741194ff6c0d08ede9b3befbf9c9dJohn Reck        // There might be prefetched layers that need to be accounted for.
21900e79c9947b741194ff6c0d08ede9b3befbf9c9dJohn Reck        // That might be us, so tell CanvasContext that this layer is in the
22000e79c9947b741194ff6c0d08ede9b3befbf9c9dJohn Reck        // tree and should not be destroyed.
221998a6d81896df8b662cc10ddeb35087b78b38d72John Reck        info.canvasContext->markLayerInUse(this);
222998a6d81896df8b662cc10ddeb35087b78b38d72John Reck    }
22325fbb3fa1138675379102a44405852555cefccbdJohn Reck}
22425fbb3fa1138675379102a44405852555cefccbdJohn Reck
225a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craik/**
226a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craik * Traverse down the the draw tree to prepare for a frame.
227a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craik *
228a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craik * MODE_FULL = UI Thread-driven (thus properties must be synced), otherwise RT driven
229a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craik *
230a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craik * While traversing down the tree, functorsNeedLayer flag is set to true if anything that uses the
231a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craik * stencil buffer may be needed. Views that use a functor to draw will be forced onto a layer.
232a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craik */
233a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craikvoid RenderNode::prepareTreeImpl(TreeInfo& info, bool functorsNeedLayer) {
234a447d29c65fb811cd184775a3476101a1cede929John Reck    info.damageAccumulator->pushTransform(this);
235f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck
236dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck    if (info.mode == TreeInfo::MODE_FULL) {
23725fbb3fa1138675379102a44405852555cefccbdJohn Reck        pushStagingPropertiesChanges(info);
238e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck    }
2399eb9f6f8cbbbd87d45da8071aa54cb066a797723John Reck    uint32_t animatorDirtyMask = 0;
2409eb9f6f8cbbbd87d45da8071aa54cb066a797723John Reck    if (CC_LIKELY(info.runAnimations)) {
2419eb9f6f8cbbbd87d45da8071aa54cb066a797723John Reck        animatorDirtyMask = mAnimatorManager.animate(info);
2429eb9f6f8cbbbd87d45da8071aa54cb066a797723John Reck    }
243a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craik
2443f725f0a5abd23833176bdc26fda964e923e75deJohn Reck    bool willHaveFunctor = false;
2453f725f0a5abd23833176bdc26fda964e923e75deJohn Reck    if (info.mode == TreeInfo::MODE_FULL && mStagingDisplayListData) {
2463f725f0a5abd23833176bdc26fda964e923e75deJohn Reck        willHaveFunctor = !mStagingDisplayListData->functors.isEmpty();
2473f725f0a5abd23833176bdc26fda964e923e75deJohn Reck    } else if (mDisplayListData) {
2483f725f0a5abd23833176bdc26fda964e923e75deJohn Reck        willHaveFunctor = !mDisplayListData->functors.isEmpty();
2493f725f0a5abd23833176bdc26fda964e923e75deJohn Reck    }
250a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craik    bool childFunctorsNeedLayer = mProperties.prepareForFunctorPresence(
251a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craik            willHaveFunctor, functorsNeedLayer);
252a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craik
253a7c2ea20c43ab797bef5801530687e22e83def8fJohn Reck    prepareLayer(info, animatorDirtyMask);
254dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck    if (info.mode == TreeInfo::MODE_FULL) {
25525fbb3fa1138675379102a44405852555cefccbdJohn Reck        pushStagingDisplayListChanges(info);
25625fbb3fa1138675379102a44405852555cefccbdJohn Reck    }
257a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craik    prepareSubTree(info, childFunctorsNeedLayer, mDisplayListData);
25825fbb3fa1138675379102a44405852555cefccbdJohn Reck    pushLayerUpdate(info);
25925fbb3fa1138675379102a44405852555cefccbdJohn Reck
260a447d29c65fb811cd184775a3476101a1cede929John Reck    info.damageAccumulator->popTransform();
261f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck}
262f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck
26325fbb3fa1138675379102a44405852555cefccbdJohn Reckvoid RenderNode::pushStagingPropertiesChanges(TreeInfo& info) {
264ff941dcd815021bb20d6504eb486acb1e50592c3John Reck    // Push the animators first so that setupStartValueIfNecessary() is called
265ff941dcd815021bb20d6504eb486acb1e50592c3John Reck    // before properties() is trampled by stagingProperties(), as they are
266ff941dcd815021bb20d6504eb486acb1e50592c3John Reck    // required by some animators.
2679eb9f6f8cbbbd87d45da8071aa54cb066a797723John Reck    if (CC_LIKELY(info.runAnimations)) {
268119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck        mAnimatorManager.pushStaging();
2699eb9f6f8cbbbd87d45da8071aa54cb066a797723John Reck    }
270ff941dcd815021bb20d6504eb486acb1e50592c3John Reck    if (mDirtyPropertyFields) {
271ff941dcd815021bb20d6504eb486acb1e50592c3John Reck        mDirtyPropertyFields = 0;
272e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck        damageSelf(info);
273a447d29c65fb811cd184775a3476101a1cede929John Reck        info.damageAccumulator->popTransform();
274ff941dcd815021bb20d6504eb486acb1e50592c3John Reck        mProperties = mStagingProperties;
27525fbb3fa1138675379102a44405852555cefccbdJohn Reck        applyLayerPropertiesToLayer(info);
276e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck        // We could try to be clever and only re-damage if the matrix changed.
277e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck        // However, we don't need to worry about that. The cost of over-damaging
278e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck        // here is only going to be a single additional map rect of this node
279e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck        // plus a rect join(). The parent's transform (and up) will only be
280e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck        // performed once.
281a447d29c65fb811cd184775a3476101a1cede929John Reck        info.damageAccumulator->pushTransform(this);
282e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck        damageSelf(info);
283ff941dcd815021bb20d6504eb486acb1e50592c3John Reck    }
28425fbb3fa1138675379102a44405852555cefccbdJohn Reck}
28525fbb3fa1138675379102a44405852555cefccbdJohn Reck
28664bb413a664001c95c8439cf097dc3033f4ed733Andreas Gampevoid RenderNode::applyLayerPropertiesToLayer(TreeInfo& info) {
28725fbb3fa1138675379102a44405852555cefccbdJohn Reck    if (CC_LIKELY(!mLayer)) return;
28825fbb3fa1138675379102a44405852555cefccbdJohn Reck
28925fbb3fa1138675379102a44405852555cefccbdJohn Reck    const LayerProperties& props = properties().layerProperties();
29025fbb3fa1138675379102a44405852555cefccbdJohn Reck    mLayer->setAlpha(props.alpha(), props.xferMode());
29125fbb3fa1138675379102a44405852555cefccbdJohn Reck    mLayer->setColorFilter(props.colorFilter());
29225fbb3fa1138675379102a44405852555cefccbdJohn Reck    mLayer->setBlend(props.needsBlending());
29325fbb3fa1138675379102a44405852555cefccbdJohn Reck}
29425fbb3fa1138675379102a44405852555cefccbdJohn Reck
29525fbb3fa1138675379102a44405852555cefccbdJohn Reckvoid RenderNode::pushStagingDisplayListChanges(TreeInfo& info) {
2968de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck    if (mNeedsDisplayListDataSync) {
2978de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck        mNeedsDisplayListDataSync = false;
298dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck        // Make sure we inc first so that we don't fluctuate between 0 and 1,
299dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck        // which would thrash the layer cache
300dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck        if (mStagingDisplayListData) {
301dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck            for (size_t i = 0; i < mStagingDisplayListData->children().size(); i++) {
302dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck                mStagingDisplayListData->children()[i]->mRenderNode->incParentRefCount();
303dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck            }
304dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck        }
3055c9d717d3e7064db76d2a01223af51379dadf2c3John Reck        // Damage with the old display list first then the new one to catch any
3065c9d717d3e7064db76d2a01223af51379dadf2c3John Reck        // changes in isRenderable or, in the future, bounds
3075c9d717d3e7064db76d2a01223af51379dadf2c3John Reck        damageSelf(info);
308dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck        deleteDisplayListData();
309a35778c799e8073a42b9e22191bde9d838327ab7John Reck        // TODO: Remove this caches stuff
310a35778c799e8073a42b9e22191bde9d838327ab7John Reck        if (mStagingDisplayListData && mStagingDisplayListData->functors.size()) {
311a35778c799e8073a42b9e22191bde9d838327ab7John Reck            Caches::getInstance().registerFunctors(mStagingDisplayListData->functors.size());
312a35778c799e8073a42b9e22191bde9d838327ab7John Reck        }
3138de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck        mDisplayListData = mStagingDisplayListData;
314d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik        mStagingDisplayListData = nullptr;
31509d5cddf67b676018700bcc10a72242641cd7eecJohn Reck        if (mDisplayListData) {
31609d5cddf67b676018700bcc10a72242641cd7eecJohn Reck            for (size_t i = 0; i < mDisplayListData->functors.size(); i++) {
317d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik                (*mDisplayListData->functors[i])(DrawGlInfo::kModeSync, nullptr);
31809d5cddf67b676018700bcc10a72242641cd7eecJohn Reck            }
31909d5cddf67b676018700bcc10a72242641cd7eecJohn Reck        }
320e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck        damageSelf(info);
3218de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck    }
3228de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck}
323113e0824d6bddf4376240681f9cf6a2deded9498John Reck
324dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reckvoid RenderNode::deleteDisplayListData() {
325dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck    if (mDisplayListData) {
326dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck        for (size_t i = 0; i < mDisplayListData->children().size(); i++) {
327dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck            mDisplayListData->children()[i]->mRenderNode->decParentRefCount();
328dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck        }
329a35778c799e8073a42b9e22191bde9d838327ab7John Reck        if (mDisplayListData->functors.size()) {
330a35778c799e8073a42b9e22191bde9d838327ab7John Reck            Caches::getInstance().unregisterFunctors(mDisplayListData->functors.size());
331a35778c799e8073a42b9e22191bde9d838327ab7John Reck        }
332dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck    }
333dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck    delete mDisplayListData;
334d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    mDisplayListData = nullptr;
335dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck}
336dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck
337a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craikvoid RenderNode::prepareSubTree(TreeInfo& info, bool functorsNeedLayer, DisplayListData* subtree) {
3388de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck    if (subtree) {
339860d155f866cc15a725e7ce03763280987f24901John Reck        TextureCache& cache = Caches::getInstance().textureCache;
34009d5cddf67b676018700bcc10a72242641cd7eecJohn Reck        info.out.hasFunctors |= subtree->functors.size();
341860d155f866cc15a725e7ce03763280987f24901John Reck        for (size_t i = 0; info.prepareTextures && i < subtree->bitmapResources.size(); i++) {
34200e79c9947b741194ff6c0d08ede9b3befbf9c9dJohn Reck            info.prepareTextures = cache.prefetchAndMarkInUse(
34300e79c9947b741194ff6c0d08ede9b3befbf9c9dJohn Reck                    info.canvasContext, subtree->bitmapResources[i]);
344f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck        }
3458de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck        for (size_t i = 0; i < subtree->children().size(); i++) {
346a7090e0cfd7c719a6d4c03aae34f5db98754cbddChris Craik            DrawRenderNodeOp* op = subtree->children()[i];
347a7090e0cfd7c719a6d4c03aae34f5db98754cbddChris Craik            RenderNode* childNode = op->mRenderNode;
348a447d29c65fb811cd184775a3476101a1cede929John Reck            info.damageAccumulator->pushTransform(&op->mTransformFromParent);
349a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craik            bool childFunctorsNeedLayer = functorsNeedLayer
350a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craik                    // Recorded with non-rect clip, or canvas-rotated by parent
351a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craik                    || op->mRecordedWithPotentialStencilClip;
352a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craik            childNode->prepareTreeImpl(info, childFunctorsNeedLayer);
353a447d29c65fb811cd184775a3476101a1cede929John Reck            info.damageAccumulator->popTransform();
3545bf11bb98f5dbe278c257355d24c181237abd68cJohn Reck        }
355113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
356113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
357113e0824d6bddf4376240681f9cf6a2deded9498John Reck
358dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reckvoid RenderNode::destroyHardwareResources() {
359dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck    if (mLayer) {
360dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck        LayerRenderer::destroyLayer(mLayer);
361d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik        mLayer = nullptr;
362dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck    }
363dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck    if (mDisplayListData) {
364dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck        for (size_t i = 0; i < mDisplayListData->children().size(); i++) {
365dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck            mDisplayListData->children()[i]->mRenderNode->destroyHardwareResources();
366dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck        }
367dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck        if (mNeedsDisplayListDataSync) {
368dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck            // Next prepare tree we are going to push a new display list, so we can
369dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck            // drop our current one now
370dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck            deleteDisplayListData();
371dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck        }
372dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck    }
373dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck}
374dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck
375dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reckvoid RenderNode::decParentRefCount() {
376dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck    LOG_ALWAYS_FATAL_IF(!mParentCount, "already 0!");
377dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck    mParentCount--;
378dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck    if (!mParentCount) {
379dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck        // If a child of ours is being attached to our parent then this will incorrectly
380dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck        // destroy its hardware resources. However, this situation is highly unlikely
381dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck        // and the failure is "just" that the layer is re-created, so this should
382dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck        // be safe enough
383dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck        destroyHardwareResources();
384dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck    }
385dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck}
386dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck
387113e0824d6bddf4376240681f9cf6a2deded9498John Reck/*
388113e0824d6bddf4376240681f9cf6a2deded9498John Reck * For property operations, we pass a savecount of 0, since the operations aren't part of the
389113e0824d6bddf4376240681f9cf6a2deded9498John Reck * displaylist, and thus don't have to compensate for the record-time/playback-time discrepancy in
390d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck * base saveCount (i.e., how RestoreToCount uses saveCount + properties().getCount())
391113e0824d6bddf4376240681f9cf6a2deded9498John Reck */
392113e0824d6bddf4376240681f9cf6a2deded9498John Reck#define PROPERTY_SAVECOUNT 0
393113e0824d6bddf4376240681f9cf6a2deded9498John Reck
394113e0824d6bddf4376240681f9cf6a2deded9498John Recktemplate <class T>
395b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craikvoid RenderNode::setViewProperties(OpenGLRenderer& renderer, T& handler) {
396113e0824d6bddf4376240681f9cf6a2deded9498John Reck#if DEBUG_DISPLAY_LIST
397b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    properties().debugOutputProperties(handler.level() + 1);
398113e0824d6bddf4376240681f9cf6a2deded9498John Reck#endif
399d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (properties().getLeft() != 0 || properties().getTop() != 0) {
400d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        renderer.translate(properties().getLeft(), properties().getTop());
401113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
402d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (properties().getStaticMatrix()) {
403139088228faa7f3c446af7387e017933998a5570Derek Sollenberger        renderer.concatMatrix(*properties().getStaticMatrix());
404d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    } else if (properties().getAnimationMatrix()) {
405139088228faa7f3c446af7387e017933998a5570Derek Sollenberger        renderer.concatMatrix(*properties().getAnimationMatrix());
406113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
407f7483e3af0513a1baa8341d403df2e0c0896a9ffJohn Reck    if (properties().hasTransformMatrix()) {
408f7483e3af0513a1baa8341d403df2e0c0896a9ffJohn Reck        if (properties().isTransformTranslateOnly()) {
409d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            renderer.translate(properties().getTranslationX(), properties().getTranslationY());
410113e0824d6bddf4376240681f9cf6a2deded9498John Reck        } else {
411d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            renderer.concatMatrix(*properties().getTransformMatrix());
412113e0824d6bddf4376240681f9cf6a2deded9498John Reck        }
413113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
414856f0cc3e382206e67c1a62fe878627d7dd9ada5Chris Craik    const bool isLayer = properties().effectiveLayerType() != LayerType::None;
415a753f4c6cb8558795e673df1896532cd148781e2Chris Craik    int clipFlags = properties().getClippingFlags();
416d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (properties().getAlpha() < 1) {
41725fbb3fa1138675379102a44405852555cefccbdJohn Reck        if (isLayer) {
418a753f4c6cb8558795e673df1896532cd148781e2Chris Craik            clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
419113e0824d6bddf4376240681f9cf6a2deded9498John Reck        }
4204e9d9b2a590105035c29f12ecf0689333e6b55e7Chris Craik        if (CC_LIKELY(isLayer || !properties().getHasOverlappingRendering())) {
4214e9d9b2a590105035c29f12ecf0689333e6b55e7Chris Craik            // simply scale rendering content's alpha
4224e9d9b2a590105035c29f12ecf0689333e6b55e7Chris Craik            renderer.scaleAlpha(properties().getAlpha());
4234e9d9b2a590105035c29f12ecf0689333e6b55e7Chris Craik        } else {
4244e9d9b2a590105035c29f12ecf0689333e6b55e7Chris Craik            // savelayer needed to create an offscreen buffer
4254e9d9b2a590105035c29f12ecf0689333e6b55e7Chris Craik            Rect layerBounds(0, 0, getWidth(), getHeight());
4264e9d9b2a590105035c29f12ecf0689333e6b55e7Chris Craik            if (clipFlags) {
4274e9d9b2a590105035c29f12ecf0689333e6b55e7Chris Craik                properties().getClippingRectForFlags(clipFlags, &layerBounds);
4284e9d9b2a590105035c29f12ecf0689333e6b55e7Chris Craik                clipFlags = 0; // all clipping done by savelayer
4294e9d9b2a590105035c29f12ecf0689333e6b55e7Chris Craik            }
4304e9d9b2a590105035c29f12ecf0689333e6b55e7Chris Craik            SaveLayerOp* op = new (handler.allocator()) SaveLayerOp(
4314e9d9b2a590105035c29f12ecf0689333e6b55e7Chris Craik                    layerBounds.left, layerBounds.top,
4324e9d9b2a590105035c29f12ecf0689333e6b55e7Chris Craik                    layerBounds.right, layerBounds.bottom,
4334e9d9b2a590105035c29f12ecf0689333e6b55e7Chris Craik                    (int) (properties().getAlpha() * 255),
4344e9d9b2a590105035c29f12ecf0689333e6b55e7Chris Craik                    SkCanvas::kHasAlphaLayer_SaveFlag | SkCanvas::kClipToLayer_SaveFlag);
4354e9d9b2a590105035c29f12ecf0689333e6b55e7Chris Craik            handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
4364e9d9b2a590105035c29f12ecf0689333e6b55e7Chris Craik        }
4371a0808e64c170fa7b620b858fa5875f6f08a2b54Chris Craik
4381a0808e64c170fa7b620b858fa5875f6f08a2b54Chris Craik        if (CC_UNLIKELY(ATRACE_ENABLED() && properties().promotedToLayer())) {
4394e9d9b2a590105035c29f12ecf0689333e6b55e7Chris Craik            // pretend alpha always causes savelayer to warn about
4404e9d9b2a590105035c29f12ecf0689333e6b55e7Chris Craik            // performance problem affecting old versions
4411a0808e64c170fa7b620b858fa5875f6f08a2b54Chris Craik            ATRACE_FORMAT("%s alpha caused saveLayer %dx%d", getName(),
4421a0808e64c170fa7b620b858fa5875f6f08a2b54Chris Craik                    static_cast<int>(getWidth()),
4431a0808e64c170fa7b620b858fa5875f6f08a2b54Chris Craik                    static_cast<int>(getHeight()));
4441a0808e64c170fa7b620b858fa5875f6f08a2b54Chris Craik        }
445113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
446a753f4c6cb8558795e673df1896532cd148781e2Chris Craik    if (clipFlags) {
447a753f4c6cb8558795e673df1896532cd148781e2Chris Craik        Rect clipRect;
448a753f4c6cb8558795e673df1896532cd148781e2Chris Craik        properties().getClippingRectForFlags(clipFlags, &clipRect);
4498c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik        ClipRectOp* op = new (handler.allocator()) ClipRectOp(
450a753f4c6cb8558795e673df1896532cd148781e2Chris Craik                clipRect.left, clipRect.top, clipRect.right, clipRect.bottom,
451a753f4c6cb8558795e673df1896532cd148781e2Chris Craik                SkRegion::kIntersect_Op);
452d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
453113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
4548c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik
455e83cbd451868a734bfac07ccd680d5617080b579Chris Craik    // TODO: support nesting round rect clips
456af4d04cab6d48ae0d6a5e79bd30f679af87abaadChris Craik    if (mProperties.getRevealClip().willClip()) {
457af4d04cab6d48ae0d6a5e79bd30f679af87abaadChris Craik        Rect bounds;
458af4d04cab6d48ae0d6a5e79bd30f679af87abaadChris Craik        mProperties.getRevealClip().getBounds(&bounds);
459af4d04cab6d48ae0d6a5e79bd30f679af87abaadChris Craik        renderer.setClippingRoundRect(handler.allocator(), bounds, mProperties.getRevealClip().getRadius());
460af4d04cab6d48ae0d6a5e79bd30f679af87abaadChris Craik    } else if (mProperties.getOutline().willClip()) {
461af4d04cab6d48ae0d6a5e79bd30f679af87abaadChris Craik        renderer.setClippingOutline(handler.allocator(), &(mProperties.getOutline()));
462113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
463113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
464113e0824d6bddf4376240681f9cf6a2deded9498John Reck
465113e0824d6bddf4376240681f9cf6a2deded9498John Reck/**
466113e0824d6bddf4376240681f9cf6a2deded9498John Reck * Apply property-based transformations to input matrix
467113e0824d6bddf4376240681f9cf6a2deded9498John Reck *
468113e0824d6bddf4376240681f9cf6a2deded9498John Reck * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
469113e0824d6bddf4376240681f9cf6a2deded9498John Reck * matrix computation instead of the Skia 3x3 matrix + camera hackery.
470113e0824d6bddf4376240681f9cf6a2deded9498John Reck */
47169e5adffb19135d51bde8e458f4907d7265f3e23Chris Craikvoid RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) const {
472d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (properties().getLeft() != 0 || properties().getTop() != 0) {
473d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        matrix.translate(properties().getLeft(), properties().getTop());
474113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
475d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (properties().getStaticMatrix()) {
476d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        mat4 stat(*properties().getStaticMatrix());
477113e0824d6bddf4376240681f9cf6a2deded9498John Reck        matrix.multiply(stat);
478d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    } else if (properties().getAnimationMatrix()) {
479d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        mat4 anim(*properties().getAnimationMatrix());
480113e0824d6bddf4376240681f9cf6a2deded9498John Reck        matrix.multiply(anim);
481113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
482e0bb87d4bdbd3b08ab6a8569c8e564ed59b8a5a7Chris Craik
483cc39e16cb98855f35079941b5e7e6eac2b7bc388Chris Craik    bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getZ());
484e0bb87d4bdbd3b08ab6a8569c8e564ed59b8a5a7Chris Craik    if (properties().hasTransformMatrix() || applyTranslationZ) {
485f7483e3af0513a1baa8341d403df2e0c0896a9ffJohn Reck        if (properties().isTransformTranslateOnly()) {
486d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
487cc39e16cb98855f35079941b5e7e6eac2b7bc388Chris Craik                    true3dTransform ? properties().getZ() : 0.0f);
488113e0824d6bddf4376240681f9cf6a2deded9498John Reck        } else {
489113e0824d6bddf4376240681f9cf6a2deded9498John Reck            if (!true3dTransform) {
490d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                matrix.multiply(*properties().getTransformMatrix());
491113e0824d6bddf4376240681f9cf6a2deded9498John Reck            } else {
492113e0824d6bddf4376240681f9cf6a2deded9498John Reck                mat4 true3dMat;
493113e0824d6bddf4376240681f9cf6a2deded9498John Reck                true3dMat.loadTranslate(
494d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                        properties().getPivotX() + properties().getTranslationX(),
495d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                        properties().getPivotY() + properties().getTranslationY(),
496cc39e16cb98855f35079941b5e7e6eac2b7bc388Chris Craik                        properties().getZ());
497d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
498d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
499d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                true3dMat.rotate(properties().getRotation(), 0, 0, 1);
500d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
501d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
502113e0824d6bddf4376240681f9cf6a2deded9498John Reck
503113e0824d6bddf4376240681f9cf6a2deded9498John Reck                matrix.multiply(true3dMat);
504113e0824d6bddf4376240681f9cf6a2deded9498John Reck            }
505113e0824d6bddf4376240681f9cf6a2deded9498John Reck        }
506113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
507113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
508113e0824d6bddf4376240681f9cf6a2deded9498John Reck
509113e0824d6bddf4376240681f9cf6a2deded9498John Reck/**
510113e0824d6bddf4376240681f9cf6a2deded9498John Reck * Organizes the DisplayList hierarchy to prepare for background projection reordering.
511113e0824d6bddf4376240681f9cf6a2deded9498John Reck *
512113e0824d6bddf4376240681f9cf6a2deded9498John Reck * This should be called before a call to defer() or drawDisplayList()
513113e0824d6bddf4376240681f9cf6a2deded9498John Reck *
514113e0824d6bddf4376240681f9cf6a2deded9498John Reck * Each DisplayList that serves as a 3d root builds its list of composited children,
515113e0824d6bddf4376240681f9cf6a2deded9498John Reck * which are flagged to not draw in the standard draw loop.
516113e0824d6bddf4376240681f9cf6a2deded9498John Reck */
517113e0824d6bddf4376240681f9cf6a2deded9498John Reckvoid RenderNode::computeOrdering() {
518113e0824d6bddf4376240681f9cf6a2deded9498John Reck    ATRACE_CALL();
519113e0824d6bddf4376240681f9cf6a2deded9498John Reck    mProjectedNodes.clear();
520113e0824d6bddf4376240681f9cf6a2deded9498John Reck
521113e0824d6bddf4376240681f9cf6a2deded9498John Reck    // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that
522113e0824d6bddf4376240681f9cf6a2deded9498John Reck    // transform properties are applied correctly to top level children
523d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    if (mDisplayListData == nullptr) return;
524087bc0c14bdccf7c258dce0cdef46a69a839b427John Reck    for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
525a7090e0cfd7c719a6d4c03aae34f5db98754cbddChris Craik        DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
526a7090e0cfd7c719a6d4c03aae34f5db98754cbddChris Craik        childOp->mRenderNode->computeOrderingImpl(childOp,
5273f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik                properties().getOutline().getPath(), &mProjectedNodes, &mat4::identity());
528113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
529113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
530113e0824d6bddf4376240681f9cf6a2deded9498John Reck
531113e0824d6bddf4376240681f9cf6a2deded9498John Reckvoid RenderNode::computeOrderingImpl(
532a7090e0cfd7c719a6d4c03aae34f5db98754cbddChris Craik        DrawRenderNodeOp* opState,
5333f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik        const SkPath* outlineOfProjectionSurface,
534a7090e0cfd7c719a6d4c03aae34f5db98754cbddChris Craik        Vector<DrawRenderNodeOp*>* compositedChildrenOfProjectionSurface,
535113e0824d6bddf4376240681f9cf6a2deded9498John Reck        const mat4* transformFromProjectionSurface) {
536113e0824d6bddf4376240681f9cf6a2deded9498John Reck    mProjectedNodes.clear();
537d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    if (mDisplayListData == nullptr || mDisplayListData->isEmpty()) return;
538113e0824d6bddf4376240681f9cf6a2deded9498John Reck
539113e0824d6bddf4376240681f9cf6a2deded9498John Reck    // TODO: should avoid this calculation in most cases
540113e0824d6bddf4376240681f9cf6a2deded9498John Reck    // TODO: just calculate single matrix, down to all leaf composited elements
541113e0824d6bddf4376240681f9cf6a2deded9498John Reck    Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface);
542113e0824d6bddf4376240681f9cf6a2deded9498John Reck    localTransformFromProjectionSurface.multiply(opState->mTransformFromParent);
543113e0824d6bddf4376240681f9cf6a2deded9498John Reck
544d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (properties().getProjectBackwards()) {
545113e0824d6bddf4376240681f9cf6a2deded9498John Reck        // composited projectee, flag for out of order draw, save matrix, and store in proj surface
546113e0824d6bddf4376240681f9cf6a2deded9498John Reck        opState->mSkipInOrderDraw = true;
547113e0824d6bddf4376240681f9cf6a2deded9498John Reck        opState->mTransformFromCompositingAncestor.load(localTransformFromProjectionSurface);
548113e0824d6bddf4376240681f9cf6a2deded9498John Reck        compositedChildrenOfProjectionSurface->add(opState);
549113e0824d6bddf4376240681f9cf6a2deded9498John Reck    } else {
550113e0824d6bddf4376240681f9cf6a2deded9498John Reck        // standard in order draw
551113e0824d6bddf4376240681f9cf6a2deded9498John Reck        opState->mSkipInOrderDraw = false;
552113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
553113e0824d6bddf4376240681f9cf6a2deded9498John Reck
554087bc0c14bdccf7c258dce0cdef46a69a839b427John Reck    if (mDisplayListData->children().size() > 0) {
555113e0824d6bddf4376240681f9cf6a2deded9498John Reck        const bool isProjectionReceiver = mDisplayListData->projectionReceiveIndex >= 0;
556113e0824d6bddf4376240681f9cf6a2deded9498John Reck        bool haveAppliedPropertiesToProjection = false;
557087bc0c14bdccf7c258dce0cdef46a69a839b427John Reck        for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
558a7090e0cfd7c719a6d4c03aae34f5db98754cbddChris Craik            DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
559a7090e0cfd7c719a6d4c03aae34f5db98754cbddChris Craik            RenderNode* child = childOp->mRenderNode;
560113e0824d6bddf4376240681f9cf6a2deded9498John Reck
561d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik            const SkPath* projectionOutline = nullptr;
562d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik            Vector<DrawRenderNodeOp*>* projectionChildren = nullptr;
563d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik            const mat4* projectionTransform = nullptr;
564d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            if (isProjectionReceiver && !child->properties().getProjectBackwards()) {
565bf72eb80d9e00fe2fa360dff0ef581ea27c8e965Chris Craik                // if receiving projections, collect projecting descendant
566113e0824d6bddf4376240681f9cf6a2deded9498John Reck
567bf72eb80d9e00fe2fa360dff0ef581ea27c8e965Chris Craik                // Note that if a direct descendant is projecting backwards, we pass its
568bf72eb80d9e00fe2fa360dff0ef581ea27c8e965Chris Craik                // grandparent projection collection, since it shouldn't project onto its
569113e0824d6bddf4376240681f9cf6a2deded9498John Reck                // parent, where it will already be drawing.
5703f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik                projectionOutline = properties().getOutline().getPath();
571113e0824d6bddf4376240681f9cf6a2deded9498John Reck                projectionChildren = &mProjectedNodes;
572113e0824d6bddf4376240681f9cf6a2deded9498John Reck                projectionTransform = &mat4::identity();
573113e0824d6bddf4376240681f9cf6a2deded9498John Reck            } else {
574113e0824d6bddf4376240681f9cf6a2deded9498John Reck                if (!haveAppliedPropertiesToProjection) {
575113e0824d6bddf4376240681f9cf6a2deded9498John Reck                    applyViewPropertyTransforms(localTransformFromProjectionSurface);
576113e0824d6bddf4376240681f9cf6a2deded9498John Reck                    haveAppliedPropertiesToProjection = true;
577113e0824d6bddf4376240681f9cf6a2deded9498John Reck                }
5783f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik                projectionOutline = outlineOfProjectionSurface;
579113e0824d6bddf4376240681f9cf6a2deded9498John Reck                projectionChildren = compositedChildrenOfProjectionSurface;
580113e0824d6bddf4376240681f9cf6a2deded9498John Reck                projectionTransform = &localTransformFromProjectionSurface;
581113e0824d6bddf4376240681f9cf6a2deded9498John Reck            }
5823f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik            child->computeOrderingImpl(childOp,
5833f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik                    projectionOutline, projectionChildren, projectionTransform);
584113e0824d6bddf4376240681f9cf6a2deded9498John Reck        }
585113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
586113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
587113e0824d6bddf4376240681f9cf6a2deded9498John Reck
588113e0824d6bddf4376240681f9cf6a2deded9498John Reckclass DeferOperationHandler {
589113e0824d6bddf4376240681f9cf6a2deded9498John Reckpublic:
590113e0824d6bddf4376240681f9cf6a2deded9498John Reck    DeferOperationHandler(DeferStateStruct& deferStruct, int level)
591113e0824d6bddf4376240681f9cf6a2deded9498John Reck        : mDeferStruct(deferStruct), mLevel(level) {}
592113e0824d6bddf4376240681f9cf6a2deded9498John Reck    inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
593113e0824d6bddf4376240681f9cf6a2deded9498John Reck        operation->defer(mDeferStruct, saveCount, mLevel, clipToBounds);
594113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
595113e0824d6bddf4376240681f9cf6a2deded9498John Reck    inline LinearAllocator& allocator() { return *(mDeferStruct.mAllocator); }
59664bb413a664001c95c8439cf097dc3033f4ed733Andreas Gampe    inline void startMark(const char* name) {} // do nothing
597b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    inline void endMark() {}
598b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    inline int level() { return mLevel; }
599b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    inline int replayFlags() { return mDeferStruct.mReplayFlags; }
6007466986d2055eb8711f36a85ac539b1572ffe805Chris Craik    inline SkPath* allocPathForFrame() { return mDeferStruct.allocPathForFrame(); }
601113e0824d6bddf4376240681f9cf6a2deded9498John Reck
602113e0824d6bddf4376240681f9cf6a2deded9498John Reckprivate:
603113e0824d6bddf4376240681f9cf6a2deded9498John Reck    DeferStateStruct& mDeferStruct;
604113e0824d6bddf4376240681f9cf6a2deded9498John Reck    const int mLevel;
605113e0824d6bddf4376240681f9cf6a2deded9498John Reck};
606113e0824d6bddf4376240681f9cf6a2deded9498John Reck
60780d4902196899d1325cd9f52c06ae0174cf9bd4cChris Craikvoid RenderNode::defer(DeferStateStruct& deferStruct, const int level) {
608113e0824d6bddf4376240681f9cf6a2deded9498John Reck    DeferOperationHandler handler(deferStruct, level);
609b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler);
610113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
611113e0824d6bddf4376240681f9cf6a2deded9498John Reck
612113e0824d6bddf4376240681f9cf6a2deded9498John Reckclass ReplayOperationHandler {
613113e0824d6bddf4376240681f9cf6a2deded9498John Reckpublic:
614113e0824d6bddf4376240681f9cf6a2deded9498John Reck    ReplayOperationHandler(ReplayStateStruct& replayStruct, int level)
615113e0824d6bddf4376240681f9cf6a2deded9498John Reck        : mReplayStruct(replayStruct), mLevel(level) {}
616113e0824d6bddf4376240681f9cf6a2deded9498John Reck    inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
617113e0824d6bddf4376240681f9cf6a2deded9498John Reck#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
6183f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik        mReplayStruct.mRenderer.eventMark(operation->name());
619113e0824d6bddf4376240681f9cf6a2deded9498John Reck#endif
620113e0824d6bddf4376240681f9cf6a2deded9498John Reck        operation->replay(mReplayStruct, saveCount, mLevel, clipToBounds);
621113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
622113e0824d6bddf4376240681f9cf6a2deded9498John Reck    inline LinearAllocator& allocator() { return *(mReplayStruct.mAllocator); }
623b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    inline void startMark(const char* name) {
624b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik        mReplayStruct.mRenderer.startMark(name);
625b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    }
626b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    inline void endMark() {
627b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik        mReplayStruct.mRenderer.endMark();
628b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    }
629b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    inline int level() { return mLevel; }
630b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    inline int replayFlags() { return mReplayStruct.mReplayFlags; }
6317466986d2055eb8711f36a85ac539b1572ffe805Chris Craik    inline SkPath* allocPathForFrame() { return mReplayStruct.allocPathForFrame(); }
632113e0824d6bddf4376240681f9cf6a2deded9498John Reck
633113e0824d6bddf4376240681f9cf6a2deded9498John Reckprivate:
634113e0824d6bddf4376240681f9cf6a2deded9498John Reck    ReplayStateStruct& mReplayStruct;
635113e0824d6bddf4376240681f9cf6a2deded9498John Reck    const int mLevel;
636113e0824d6bddf4376240681f9cf6a2deded9498John Reck};
637113e0824d6bddf4376240681f9cf6a2deded9498John Reck
63880d4902196899d1325cd9f52c06ae0174cf9bd4cChris Craikvoid RenderNode::replay(ReplayStateStruct& replayStruct, const int level) {
639b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    ReplayOperationHandler handler(replayStruct, level);
640b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler);
641113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
642113e0824d6bddf4376240681f9cf6a2deded9498John Reck
6438afd0f245cc0c4a0366f39f41b5f78e47ee83be3Chris Craikvoid RenderNode::buildZSortedChildList(const DisplayListData::Chunk& chunk,
6448afd0f245cc0c4a0366f39f41b5f78e47ee83be3Chris Craik        Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes) {
6458afd0f245cc0c4a0366f39f41b5f78e47ee83be3Chris Craik    if (chunk.beginChildIndex == chunk.endChildIndex) return;
646113e0824d6bddf4376240681f9cf6a2deded9498John Reck
6478afd0f245cc0c4a0366f39f41b5f78e47ee83be3Chris Craik    for (unsigned int i = chunk.beginChildIndex; i < chunk.endChildIndex; i++) {
648a7090e0cfd7c719a6d4c03aae34f5db98754cbddChris Craik        DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
649a7090e0cfd7c719a6d4c03aae34f5db98754cbddChris Craik        RenderNode* child = childOp->mRenderNode;
650cc39e16cb98855f35079941b5e7e6eac2b7bc388Chris Craik        float childZ = child->properties().getZ();
651113e0824d6bddf4376240681f9cf6a2deded9498John Reck
6528afd0f245cc0c4a0366f39f41b5f78e47ee83be3Chris Craik        if (!MathUtils::isZero(childZ) && chunk.reorderChildren) {
653a7090e0cfd7c719a6d4c03aae34f5db98754cbddChris Craik            zTranslatedNodes.add(ZDrawRenderNodeOpPair(childZ, childOp));
654113e0824d6bddf4376240681f9cf6a2deded9498John Reck            childOp->mSkipInOrderDraw = true;
655d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        } else if (!child->properties().getProjectBackwards()) {
656113e0824d6bddf4376240681f9cf6a2deded9498John Reck            // regular, in order drawing DisplayList
657113e0824d6bddf4376240681f9cf6a2deded9498John Reck            childOp->mSkipInOrderDraw = false;
658113e0824d6bddf4376240681f9cf6a2deded9498John Reck        }
659113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
660113e0824d6bddf4376240681f9cf6a2deded9498John Reck
6618afd0f245cc0c4a0366f39f41b5f78e47ee83be3Chris Craik    // Z sort any 3d children (stable-ness makes z compare fall back to standard drawing order)
662113e0824d6bddf4376240681f9cf6a2deded9498John Reck    std::stable_sort(zTranslatedNodes.begin(), zTranslatedNodes.end());
663113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
664113e0824d6bddf4376240681f9cf6a2deded9498John Reck
665b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craiktemplate <class T>
666b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craikvoid RenderNode::issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler) {
66777b5cad3efedd20f2b7cc14d87ccce1b0261960aChris Craik    if (properties().getAlpha() <= 0.0f
66877b5cad3efedd20f2b7cc14d87ccce1b0261960aChris Craik            || properties().getOutline().getAlpha() <= 0.0f
66912412ce87025d41e3cd3ac3f93f8638c6dae1b32Teng-Hui Zhu            || !properties().getOutline().getPath()
67012412ce87025d41e3cd3ac3f93f8638c6dae1b32Teng-Hui Zhu            || properties().getScaleX() == 0
67112412ce87025d41e3cd3ac3f93f8638c6dae1b32Teng-Hui Zhu            || properties().getScaleY() == 0) {
67277b5cad3efedd20f2b7cc14d87ccce1b0261960aChris Craik        // no shadow to draw
67377b5cad3efedd20f2b7cc14d87ccce1b0261960aChris Craik        return;
67477b5cad3efedd20f2b7cc14d87ccce1b0261960aChris Craik    }
675b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik
676b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    mat4 shadowMatrixXY(transformFromParent);
677b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    applyViewPropertyTransforms(shadowMatrixXY);
678b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik
679b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    // Z matrix needs actual 3d transformation, so mapped z values will be correct
680b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    mat4 shadowMatrixZ(transformFromParent);
681b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    applyViewPropertyTransforms(shadowMatrixZ, true);
682b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik
6837466986d2055eb8711f36a85ac539b1572ffe805Chris Craik    const SkPath* casterOutlinePath = properties().getOutline().getPath();
684af4d04cab6d48ae0d6a5e79bd30f679af87abaadChris Craik    const SkPath* revealClipPath = properties().getRevealClip().getPath();
68561317325b7b4b4ffafd9400ab5966e8d7c67df2eChris Craik    if (revealClipPath && revealClipPath->isEmpty()) return;
68661317325b7b4b4ffafd9400ab5966e8d7c67df2eChris Craik
68777b5cad3efedd20f2b7cc14d87ccce1b0261960aChris Craik    float casterAlpha = properties().getAlpha() * properties().getOutline().getAlpha();
6887466986d2055eb8711f36a85ac539b1572ffe805Chris Craik
689faa79ff9d964de940660d2226d5b06ded9294597Chris Craik
690faa79ff9d964de940660d2226d5b06ded9294597Chris Craik    // holds temporary SkPath to store the result of intersections
691d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    SkPath* frameAllocatedPath = nullptr;
6927466986d2055eb8711f36a85ac539b1572ffe805Chris Craik    const SkPath* outlinePath = casterOutlinePath;
693faa79ff9d964de940660d2226d5b06ded9294597Chris Craik
694faa79ff9d964de940660d2226d5b06ded9294597Chris Craik    // intersect the outline with the reveal clip, if present
6957466986d2055eb8711f36a85ac539b1572ffe805Chris Craik    if (revealClipPath) {
696faa79ff9d964de940660d2226d5b06ded9294597Chris Craik        frameAllocatedPath = handler.allocPathForFrame();
697faa79ff9d964de940660d2226d5b06ded9294597Chris Craik
698faa79ff9d964de940660d2226d5b06ded9294597Chris Craik        Op(*outlinePath, *revealClipPath, kIntersect_PathOp, frameAllocatedPath);
699faa79ff9d964de940660d2226d5b06ded9294597Chris Craik        outlinePath = frameAllocatedPath;
700faa79ff9d964de940660d2226d5b06ded9294597Chris Craik    }
701faa79ff9d964de940660d2226d5b06ded9294597Chris Craik
702faa79ff9d964de940660d2226d5b06ded9294597Chris Craik    // intersect the outline with the clipBounds, if present
703faa79ff9d964de940660d2226d5b06ded9294597Chris Craik    if (properties().getClippingFlags() & CLIP_TO_CLIP_BOUNDS) {
704faa79ff9d964de940660d2226d5b06ded9294597Chris Craik        if (!frameAllocatedPath) {
705faa79ff9d964de940660d2226d5b06ded9294597Chris Craik            frameAllocatedPath = handler.allocPathForFrame();
706faa79ff9d964de940660d2226d5b06ded9294597Chris Craik        }
707faa79ff9d964de940660d2226d5b06ded9294597Chris Craik
708faa79ff9d964de940660d2226d5b06ded9294597Chris Craik        Rect clipBounds;
709faa79ff9d964de940660d2226d5b06ded9294597Chris Craik        properties().getClippingRectForFlags(CLIP_TO_CLIP_BOUNDS, &clipBounds);
710faa79ff9d964de940660d2226d5b06ded9294597Chris Craik        SkPath clipBoundsPath;
711faa79ff9d964de940660d2226d5b06ded9294597Chris Craik        clipBoundsPath.addRect(clipBounds.left, clipBounds.top,
712faa79ff9d964de940660d2226d5b06ded9294597Chris Craik                clipBounds.right, clipBounds.bottom);
7137466986d2055eb8711f36a85ac539b1572ffe805Chris Craik
714faa79ff9d964de940660d2226d5b06ded9294597Chris Craik        Op(*outlinePath, clipBoundsPath, kIntersect_PathOp, frameAllocatedPath);
7157466986d2055eb8711f36a85ac539b1572ffe805Chris Craik        outlinePath = frameAllocatedPath;
7167466986d2055eb8711f36a85ac539b1572ffe805Chris Craik    }
7177466986d2055eb8711f36a85ac539b1572ffe805Chris Craik
718b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    DisplayListOp* shadowOp  = new (handler.allocator()) DrawShadowOp(
7197466986d2055eb8711f36a85ac539b1572ffe805Chris Craik            shadowMatrixXY, shadowMatrixZ, casterAlpha, outlinePath);
720b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    handler(shadowOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
721b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik}
722b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik
723113e0824d6bddf4376240681f9cf6a2deded9498John Reck#define SHADOW_DELTA 0.1f
724113e0824d6bddf4376240681f9cf6a2deded9498John Reck
725113e0824d6bddf4376240681f9cf6a2deded9498John Recktemplate <class T>
726c3e75f9d54b3629b3fd27afafa2e07bd07dad9b3Chris Craikvoid RenderNode::issueOperationsOf3dChildren(ChildrenSelectMode mode,
727c3e75f9d54b3629b3fd27afafa2e07bd07dad9b3Chris Craik        const Matrix4& initialTransform, const Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
728c3e75f9d54b3629b3fd27afafa2e07bd07dad9b3Chris Craik        OpenGLRenderer& renderer, T& handler) {
729113e0824d6bddf4376240681f9cf6a2deded9498John Reck    const int size = zTranslatedNodes.size();
730113e0824d6bddf4376240681f9cf6a2deded9498John Reck    if (size == 0
731113e0824d6bddf4376240681f9cf6a2deded9498John Reck            || (mode == kNegativeZChildren && zTranslatedNodes[0].key > 0.0f)
732113e0824d6bddf4376240681f9cf6a2deded9498John Reck            || (mode == kPositiveZChildren && zTranslatedNodes[size - 1].key < 0.0f)) {
733113e0824d6bddf4376240681f9cf6a2deded9498John Reck        // no 3d children to draw
734113e0824d6bddf4376240681f9cf6a2deded9498John Reck        return;
735113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
736113e0824d6bddf4376240681f9cf6a2deded9498John Reck
737c3e75f9d54b3629b3fd27afafa2e07bd07dad9b3Chris Craik    // Apply the base transform of the parent of the 3d children. This isolates
738c3e75f9d54b3629b3fd27afafa2e07bd07dad9b3Chris Craik    // 3d children of the current chunk from transformations made in previous chunks.
739c3e75f9d54b3629b3fd27afafa2e07bd07dad9b3Chris Craik    int rootRestoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
740c3e75f9d54b3629b3fd27afafa2e07bd07dad9b3Chris Craik    renderer.setMatrix(initialTransform);
741c3e75f9d54b3629b3fd27afafa2e07bd07dad9b3Chris Craik
742113e0824d6bddf4376240681f9cf6a2deded9498John Reck    /**
743113e0824d6bddf4376240681f9cf6a2deded9498John Reck     * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
744113e0824d6bddf4376240681f9cf6a2deded9498John Reck     * with very similar Z heights to draw together.
745113e0824d6bddf4376240681f9cf6a2deded9498John Reck     *
746113e0824d6bddf4376240681f9cf6a2deded9498John Reck     * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
747113e0824d6bddf4376240681f9cf6a2deded9498John Reck     * underneath both, and neither's shadow is drawn on top of the other.
748113e0824d6bddf4376240681f9cf6a2deded9498John Reck     */
749113e0824d6bddf4376240681f9cf6a2deded9498John Reck    const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
750113e0824d6bddf4376240681f9cf6a2deded9498John Reck    size_t drawIndex, shadowIndex, endIndex;
751113e0824d6bddf4376240681f9cf6a2deded9498John Reck    if (mode == kNegativeZChildren) {
752113e0824d6bddf4376240681f9cf6a2deded9498John Reck        drawIndex = 0;
753113e0824d6bddf4376240681f9cf6a2deded9498John Reck        endIndex = nonNegativeIndex;
754113e0824d6bddf4376240681f9cf6a2deded9498John Reck        shadowIndex = endIndex; // draw no shadows
755113e0824d6bddf4376240681f9cf6a2deded9498John Reck    } else {
756113e0824d6bddf4376240681f9cf6a2deded9498John Reck        drawIndex = nonNegativeIndex;
757113e0824d6bddf4376240681f9cf6a2deded9498John Reck        endIndex = size;
758113e0824d6bddf4376240681f9cf6a2deded9498John Reck        shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
759113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
7603f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik
7613f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik    DISPLAY_LIST_LOGD("%*s%d %s 3d children:", (handler.level() + 1) * 2, "",
7623f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik            endIndex - drawIndex, mode == kNegativeZChildren ? "negative" : "positive");
7633f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik
764113e0824d6bddf4376240681f9cf6a2deded9498John Reck    float lastCasterZ = 0.0f;
765113e0824d6bddf4376240681f9cf6a2deded9498John Reck    while (shadowIndex < endIndex || drawIndex < endIndex) {
766113e0824d6bddf4376240681f9cf6a2deded9498John Reck        if (shadowIndex < endIndex) {
767a7090e0cfd7c719a6d4c03aae34f5db98754cbddChris Craik            DrawRenderNodeOp* casterOp = zTranslatedNodes[shadowIndex].value;
768a7090e0cfd7c719a6d4c03aae34f5db98754cbddChris Craik            RenderNode* caster = casterOp->mRenderNode;
769113e0824d6bddf4376240681f9cf6a2deded9498John Reck            const float casterZ = zTranslatedNodes[shadowIndex].key;
770113e0824d6bddf4376240681f9cf6a2deded9498John Reck            // attempt to render the shadow if the caster about to be drawn is its caster,
771113e0824d6bddf4376240681f9cf6a2deded9498John Reck            // OR if its caster's Z value is similar to the previous potential caster
772113e0824d6bddf4376240681f9cf6a2deded9498John Reck            if (shadowIndex == drawIndex || casterZ - lastCasterZ < SHADOW_DELTA) {
773b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik                caster->issueDrawShadowOperation(casterOp->mTransformFromParent, handler);
774113e0824d6bddf4376240681f9cf6a2deded9498John Reck
775113e0824d6bddf4376240681f9cf6a2deded9498John Reck                lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
776113e0824d6bddf4376240681f9cf6a2deded9498John Reck                shadowIndex++;
777113e0824d6bddf4376240681f9cf6a2deded9498John Reck                continue;
778113e0824d6bddf4376240681f9cf6a2deded9498John Reck            }
779113e0824d6bddf4376240681f9cf6a2deded9498John Reck        }
780113e0824d6bddf4376240681f9cf6a2deded9498John Reck
781113e0824d6bddf4376240681f9cf6a2deded9498John Reck        // only the actual child DL draw needs to be in save/restore,
782113e0824d6bddf4376240681f9cf6a2deded9498John Reck        // since it modifies the renderer's matrix
783113e0824d6bddf4376240681f9cf6a2deded9498John Reck        int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
784113e0824d6bddf4376240681f9cf6a2deded9498John Reck
785a7090e0cfd7c719a6d4c03aae34f5db98754cbddChris Craik        DrawRenderNodeOp* childOp = zTranslatedNodes[drawIndex].value;
786113e0824d6bddf4376240681f9cf6a2deded9498John Reck
787113e0824d6bddf4376240681f9cf6a2deded9498John Reck        renderer.concatMatrix(childOp->mTransformFromParent);
788113e0824d6bddf4376240681f9cf6a2deded9498John Reck        childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
789d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
790113e0824d6bddf4376240681f9cf6a2deded9498John Reck        childOp->mSkipInOrderDraw = true;
791113e0824d6bddf4376240681f9cf6a2deded9498John Reck
792113e0824d6bddf4376240681f9cf6a2deded9498John Reck        renderer.restoreToCount(restoreTo);
793113e0824d6bddf4376240681f9cf6a2deded9498John Reck        drawIndex++;
794113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
795c3e75f9d54b3629b3fd27afafa2e07bd07dad9b3Chris Craik    renderer.restoreToCount(rootRestoreTo);
796113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
797113e0824d6bddf4376240681f9cf6a2deded9498John Reck
798113e0824d6bddf4376240681f9cf6a2deded9498John Recktemplate <class T>
799b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craikvoid RenderNode::issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler) {
8003f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik    DISPLAY_LIST_LOGD("%*s%d projected children:", (handler.level() + 1) * 2, "", mProjectedNodes.size());
8013f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik    const SkPath* projectionReceiverOutline = properties().getOutline().getPath();
8023f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik    int restoreTo = renderer.getSaveCount();
8033f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik
804b3cca876c2e11b865cb4f83abe2c48a60b95af5bChris Craik    LinearAllocator& alloc = handler.allocator();
805b3cca876c2e11b865cb4f83abe2c48a60b95af5bChris Craik    handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
806b3cca876c2e11b865cb4f83abe2c48a60b95af5bChris Craik            PROPERTY_SAVECOUNT, properties().getClipToBounds());
807b3cca876c2e11b865cb4f83abe2c48a60b95af5bChris Craik
808b3cca876c2e11b865cb4f83abe2c48a60b95af5bChris Craik    // Transform renderer to match background we're projecting onto
809b3cca876c2e11b865cb4f83abe2c48a60b95af5bChris Craik    // (by offsetting canvas by translationX/Y of background rendernode, since only those are set)
810b3cca876c2e11b865cb4f83abe2c48a60b95af5bChris Craik    const DisplayListOp* op =
811b3cca876c2e11b865cb4f83abe2c48a60b95af5bChris Craik            (mDisplayListData->displayListOps[mDisplayListData->projectionReceiveIndex]);
812b3cca876c2e11b865cb4f83abe2c48a60b95af5bChris Craik    const DrawRenderNodeOp* backgroundOp = reinterpret_cast<const DrawRenderNodeOp*>(op);
813b3cca876c2e11b865cb4f83abe2c48a60b95af5bChris Craik    const RenderProperties& backgroundProps = backgroundOp->mRenderNode->properties();
814b3cca876c2e11b865cb4f83abe2c48a60b95af5bChris Craik    renderer.translate(backgroundProps.getTranslationX(), backgroundProps.getTranslationY());
815b3cca876c2e11b865cb4f83abe2c48a60b95af5bChris Craik
816fca52b7583d1e5f5ff8ed06554875d2a30ef56faChris Craik    // If the projection reciever has an outline, we mask projected content to it
817fca52b7583d1e5f5ff8ed06554875d2a30ef56faChris Craik    // (which we know, apriori, are all tessellated paths)
818fca52b7583d1e5f5ff8ed06554875d2a30ef56faChris Craik    renderer.setProjectionPathMask(alloc, projectionReceiverOutline);
8193f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik
8203f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik    // draw projected nodes
821113e0824d6bddf4376240681f9cf6a2deded9498John Reck    for (size_t i = 0; i < mProjectedNodes.size(); i++) {
822a7090e0cfd7c719a6d4c03aae34f5db98754cbddChris Craik        DrawRenderNodeOp* childOp = mProjectedNodes[i];
823113e0824d6bddf4376240681f9cf6a2deded9498John Reck
824113e0824d6bddf4376240681f9cf6a2deded9498John Reck        // matrix save, concat, and restore can be done safely without allocating operations
825113e0824d6bddf4376240681f9cf6a2deded9498John Reck        int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
826113e0824d6bddf4376240681f9cf6a2deded9498John Reck        renderer.concatMatrix(childOp->mTransformFromCompositingAncestor);
827113e0824d6bddf4376240681f9cf6a2deded9498John Reck        childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
828d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
829113e0824d6bddf4376240681f9cf6a2deded9498John Reck        childOp->mSkipInOrderDraw = true;
830113e0824d6bddf4376240681f9cf6a2deded9498John Reck        renderer.restoreToCount(restoreTo);
831113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
8323f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik
833fca52b7583d1e5f5ff8ed06554875d2a30ef56faChris Craik    handler(new (alloc) RestoreToCountOp(restoreTo),
834fca52b7583d1e5f5ff8ed06554875d2a30ef56faChris Craik            PROPERTY_SAVECOUNT, properties().getClipToBounds());
835113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
836113e0824d6bddf4376240681f9cf6a2deded9498John Reck
837113e0824d6bddf4376240681f9cf6a2deded9498John Reck/**
838113e0824d6bddf4376240681f9cf6a2deded9498John Reck * This function serves both defer and replay modes, and will organize the displayList's component
839113e0824d6bddf4376240681f9cf6a2deded9498John Reck * operations for a single frame:
840113e0824d6bddf4376240681f9cf6a2deded9498John Reck *
841113e0824d6bddf4376240681f9cf6a2deded9498John Reck * Every 'simple' state operation that affects just the matrix and alpha (or other factors of
842113e0824d6bddf4376240681f9cf6a2deded9498John Reck * DeferredDisplayState) may be issued directly to the renderer, but complex operations (with custom
843113e0824d6bddf4376240681f9cf6a2deded9498John Reck * defer logic) and operations in displayListOps are issued through the 'handler' which handles the
844113e0824d6bddf4376240681f9cf6a2deded9498John Reck * defer vs replay logic, per operation
845113e0824d6bddf4376240681f9cf6a2deded9498John Reck */
846113e0824d6bddf4376240681f9cf6a2deded9498John Recktemplate <class T>
847b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craikvoid RenderNode::issueOperations(OpenGLRenderer& renderer, T& handler) {
8480645128b80621edee70f8cab4afb208fe0c26becChris Craik    if (mDisplayListData->isEmpty()) {
849bf72eb80d9e00fe2fa360dff0ef581ea27c8e965Chris Craik        DISPLAY_LIST_LOGD("%*sEmpty display list (%p, %s)", handler.level() * 2, "",
850bf72eb80d9e00fe2fa360dff0ef581ea27c8e965Chris Craik                this, getName());
8510645128b80621edee70f8cab4afb208fe0c26becChris Craik        return;
8520645128b80621edee70f8cab4afb208fe0c26becChris Craik    }
8530645128b80621edee70f8cab4afb208fe0c26becChris Craik
85451d6a3db97bdd5315f1a17a4b447d10a92217b98Chris Craik    const bool drawLayer = (mLayer && (&renderer != mLayer->renderer.get()));
85525fbb3fa1138675379102a44405852555cefccbdJohn Reck    // If we are updating the contents of mLayer, we don't want to apply any of
85625fbb3fa1138675379102a44405852555cefccbdJohn Reck    // the RenderNode's properties to this issueOperations pass. Those will all
85725fbb3fa1138675379102a44405852555cefccbdJohn Reck    // be applied when the layer is drawn, aka when this is true.
85825fbb3fa1138675379102a44405852555cefccbdJohn Reck    const bool useViewProperties = (!mLayer || drawLayer);
8590645128b80621edee70f8cab4afb208fe0c26becChris Craik    if (useViewProperties) {
8600645128b80621edee70f8cab4afb208fe0c26becChris Craik        const Outline& outline = properties().getOutline();
86112412ce87025d41e3cd3ac3f93f8638c6dae1b32Teng-Hui Zhu        if (properties().getAlpha() <= 0
86212412ce87025d41e3cd3ac3f93f8638c6dae1b32Teng-Hui Zhu                || (outline.getShouldClip() && outline.isEmpty())
86312412ce87025d41e3cd3ac3f93f8638c6dae1b32Teng-Hui Zhu                || properties().getScaleX() == 0
86412412ce87025d41e3cd3ac3f93f8638c6dae1b32Teng-Hui Zhu                || properties().getScaleY() == 0) {
865bf72eb80d9e00fe2fa360dff0ef581ea27c8e965Chris Craik            DISPLAY_LIST_LOGD("%*sRejected display list (%p, %s)", handler.level() * 2, "",
866bf72eb80d9e00fe2fa360dff0ef581ea27c8e965Chris Craik                    this, getName());
8670645128b80621edee70f8cab4afb208fe0c26becChris Craik            return;
8680645128b80621edee70f8cab4afb208fe0c26becChris Craik        }
869113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
870113e0824d6bddf4376240681f9cf6a2deded9498John Reck
8713f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik    handler.startMark(getName());
872b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik
873113e0824d6bddf4376240681f9cf6a2deded9498John Reck#if DEBUG_DISPLAY_LIST
8743f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik    const Rect& clipRect = renderer.getLocalClipBounds();
8753f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik    DISPLAY_LIST_LOGD("%*sStart display list (%p, %s), localClipBounds: %.0f, %.0f, %.0f, %.0f",
876031888744e24b5c7243ac99ec98b78aff5db1c78Chris Craik            handler.level() * 2, "", this, getName(),
8773f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik            clipRect.left, clipRect.top, clipRect.right, clipRect.bottom);
878113e0824d6bddf4376240681f9cf6a2deded9498John Reck#endif
879113e0824d6bddf4376240681f9cf6a2deded9498John Reck
880113e0824d6bddf4376240681f9cf6a2deded9498John Reck    LinearAllocator& alloc = handler.allocator();
881113e0824d6bddf4376240681f9cf6a2deded9498John Reck    int restoreTo = renderer.getSaveCount();
882113e0824d6bddf4376240681f9cf6a2deded9498John Reck    handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
883d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            PROPERTY_SAVECOUNT, properties().getClipToBounds());
884113e0824d6bddf4376240681f9cf6a2deded9498John Reck
885bf72eb80d9e00fe2fa360dff0ef581ea27c8e965Chris Craik    DISPLAY_LIST_LOGD("%*sSave %d %d", (handler.level() + 1) * 2, "",
886113e0824d6bddf4376240681f9cf6a2deded9498John Reck            SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag, restoreTo);
887113e0824d6bddf4376240681f9cf6a2deded9498John Reck
88825fbb3fa1138675379102a44405852555cefccbdJohn Reck    if (useViewProperties) {
88925fbb3fa1138675379102a44405852555cefccbdJohn Reck        setViewProperties<T>(renderer, handler);
89025fbb3fa1138675379102a44405852555cefccbdJohn Reck    }
891113e0824d6bddf4376240681f9cf6a2deded9498John Reck
8928c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik    bool quickRejected = properties().getClipToBounds()
8938c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik            && renderer.quickRejectConservative(0, 0, properties().getWidth(), properties().getHeight());
894113e0824d6bddf4376240681f9cf6a2deded9498John Reck    if (!quickRejected) {
895c3e75f9d54b3629b3fd27afafa2e07bd07dad9b3Chris Craik        Matrix4 initialTransform(*(renderer.currentTransform()));
896ac7b6d33d23cb0baaf61c723346198d41f012035Tom Hudson        renderer.setBaseTransform(initialTransform);
897c3e75f9d54b3629b3fd27afafa2e07bd07dad9b3Chris Craik
89825fbb3fa1138675379102a44405852555cefccbdJohn Reck        if (drawLayer) {
89925fbb3fa1138675379102a44405852555cefccbdJohn Reck            handler(new (alloc) DrawLayerOp(mLayer, 0, 0),
90025fbb3fa1138675379102a44405852555cefccbdJohn Reck                    renderer.getSaveCount() - 1, properties().getClipToBounds());
90125fbb3fa1138675379102a44405852555cefccbdJohn Reck        } else {
902c166b6c4fc3e1158ca08cfed24639409161055cfChris Craik            const int saveCountOffset = renderer.getSaveCount() - 1;
903c166b6c4fc3e1158ca08cfed24639409161055cfChris Craik            const int projectionReceiveIndex = mDisplayListData->projectionReceiveIndex;
9048afd0f245cc0c4a0366f39f41b5f78e47ee83be3Chris Craik            for (size_t chunkIndex = 0; chunkIndex < mDisplayListData->getChunks().size(); chunkIndex++) {
9058afd0f245cc0c4a0366f39f41b5f78e47ee83be3Chris Craik                const DisplayListData::Chunk& chunk = mDisplayListData->getChunks()[chunkIndex];
90625fbb3fa1138675379102a44405852555cefccbdJohn Reck
9078afd0f245cc0c4a0366f39f41b5f78e47ee83be3Chris Craik                Vector<ZDrawRenderNodeOpPair> zTranslatedNodes;
9088afd0f245cc0c4a0366f39f41b5f78e47ee83be3Chris Craik                buildZSortedChildList(chunk, zTranslatedNodes);
90925fbb3fa1138675379102a44405852555cefccbdJohn Reck
910c3e75f9d54b3629b3fd27afafa2e07bd07dad9b3Chris Craik                issueOperationsOf3dChildren(kNegativeZChildren,
911c3e75f9d54b3629b3fd27afafa2e07bd07dad9b3Chris Craik                        initialTransform, zTranslatedNodes, renderer, handler);
912c3e75f9d54b3629b3fd27afafa2e07bd07dad9b3Chris Craik
91325fbb3fa1138675379102a44405852555cefccbdJohn Reck
914edaecc1db0584fa017822dfc2da0c968b53967e6Andreas Gampe                for (size_t opIndex = chunk.beginOpIndex; opIndex < chunk.endOpIndex; opIndex++) {
9158afd0f245cc0c4a0366f39f41b5f78e47ee83be3Chris Craik                    DisplayListOp *op = mDisplayListData->displayListOps[opIndex];
91680d4902196899d1325cd9f52c06ae0174cf9bd4cChris Craik#if DEBUG_DISPLAY_LIST
917031888744e24b5c7243ac99ec98b78aff5db1c78Chris Craik                    op->output(handler.level() + 1);
91880d4902196899d1325cd9f52c06ae0174cf9bd4cChris Craik#endif
9198afd0f245cc0c4a0366f39f41b5f78e47ee83be3Chris Craik                    handler(op, saveCountOffset, properties().getClipToBounds());
92025fbb3fa1138675379102a44405852555cefccbdJohn Reck
921edaecc1db0584fa017822dfc2da0c968b53967e6Andreas Gampe                    if (CC_UNLIKELY(!mProjectedNodes.isEmpty() && projectionReceiveIndex >= 0 &&
922edaecc1db0584fa017822dfc2da0c968b53967e6Andreas Gampe                        opIndex == static_cast<size_t>(projectionReceiveIndex))) {
9238afd0f245cc0c4a0366f39f41b5f78e47ee83be3Chris Craik                        issueOperationsOfProjectedChildren(renderer, handler);
9248afd0f245cc0c4a0366f39f41b5f78e47ee83be3Chris Craik                    }
92525fbb3fa1138675379102a44405852555cefccbdJohn Reck                }
926113e0824d6bddf4376240681f9cf6a2deded9498John Reck
927c3e75f9d54b3629b3fd27afafa2e07bd07dad9b3Chris Craik                issueOperationsOf3dChildren(kPositiveZChildren,
928c3e75f9d54b3629b3fd27afafa2e07bd07dad9b3Chris Craik                        initialTransform, zTranslatedNodes, renderer, handler);
9298afd0f245cc0c4a0366f39f41b5f78e47ee83be3Chris Craik            }
93025fbb3fa1138675379102a44405852555cefccbdJohn Reck        }
931113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
932113e0824d6bddf4376240681f9cf6a2deded9498John Reck
933bf72eb80d9e00fe2fa360dff0ef581ea27c8e965Chris Craik    DISPLAY_LIST_LOGD("%*sRestoreToCount %d", (handler.level() + 1) * 2, "", restoreTo);
934113e0824d6bddf4376240681f9cf6a2deded9498John Reck    handler(new (alloc) RestoreToCountOp(restoreTo),
935d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            PROPERTY_SAVECOUNT, properties().getClipToBounds());
936b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik
937bf72eb80d9e00fe2fa360dff0ef581ea27c8e965Chris Craik    DISPLAY_LIST_LOGD("%*sDone (%p, %s)", handler.level() * 2, "", this, getName());
938b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    handler.endMark();
939113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
940113e0824d6bddf4376240681f9cf6a2deded9498John Reck
941113e0824d6bddf4376240681f9cf6a2deded9498John Reck} /* namespace uirenderer */
942113e0824d6bddf4376240681f9cf6a2deded9498John Reck} /* namespace android */
943