RenderNode.cpp revision 8d0ec389531d071529fb0a800f10733b057205d9
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#include "RenderNode.h"
18113e0824d6bddf4376240681f9cf6a2deded9498John Reck
19e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck#include <algorithm>
20c25e506f7fc08790c0532f927f31310a2ca778b7John Reck#include <string>
21e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck
22113e0824d6bddf4376240681f9cf6a2deded9498John Reck#include <SkCanvas.h>
23113e0824d6bddf4376240681f9cf6a2deded9498John Reck#include <algorithm>
24113e0824d6bddf4376240681f9cf6a2deded9498John Reck
25113e0824d6bddf4376240681f9cf6a2deded9498John Reck
26e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck#include "DamageAccumulator.h"
27113e0824d6bddf4376240681f9cf6a2deded9498John Reck#include "Debug.h"
28113e0824d6bddf4376240681f9cf6a2deded9498John Reck#include "DisplayListOp.h"
2925fbb3fa1138675379102a44405852555cefccbdJohn Reck#include "LayerRenderer.h"
3025fbb3fa1138675379102a44405852555cefccbdJohn Reck#include "OpenGLRenderer.h"
312dc236b2bae13b9a0ed9b3f7320502aecd7983b3Tom Hudson#include "TreeInfo.h"
32e0bb87d4bdbd3b08ab6a8569c8e564ed59b8a5a7Chris Craik#include "utils/MathUtils.h"
3370850ea258cbf91477efa57a1f1a23cc0044cc93Chris Craik#include "utils/TraceUtils.h"
34998a6d81896df8b662cc10ddeb35087b78b38d72John Reck#include "renderthread/CanvasContext.h"
35113e0824d6bddf4376240681f9cf6a2deded9498John Reck
36e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck#include "protos/hwui.pb.h"
37e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck#include "protos/ProtoHelpers.h"
38e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn 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
108e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reckvoid RenderNode::copyTo(proto::RenderNode *pnode) {
109e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    pnode->set_id(static_cast<uint64_t>(
110e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck            reinterpret_cast<uintptr_t>(this)));
111e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    pnode->set_name(mName.string(), mName.length());
112e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck
113e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    proto::RenderProperties* pprops = pnode->mutable_properties();
114e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    pprops->set_left(properties().getLeft());
115e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    pprops->set_top(properties().getTop());
116e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    pprops->set_right(properties().getRight());
117e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    pprops->set_bottom(properties().getBottom());
118e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    pprops->set_clip_flags(properties().getClippingFlags());
119e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    pprops->set_alpha(properties().getAlpha());
120e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    pprops->set_translation_x(properties().getTranslationX());
121e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    pprops->set_translation_y(properties().getTranslationY());
122e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    pprops->set_translation_z(properties().getTranslationZ());
123e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    pprops->set_elevation(properties().getElevation());
124e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    pprops->set_rotation(properties().getRotation());
125e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    pprops->set_rotation_x(properties().getRotationX());
126e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    pprops->set_rotation_y(properties().getRotationY());
127e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    pprops->set_scale_x(properties().getScaleX());
128e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    pprops->set_scale_y(properties().getScaleY());
129e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    pprops->set_pivot_x(properties().getPivotX());
130e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    pprops->set_pivot_y(properties().getPivotY());
131e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    pprops->set_has_overlapping_rendering(properties().getHasOverlappingRendering());
132e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    pprops->set_pivot_explicitly_set(properties().isPivotExplicitlySet());
133e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    pprops->set_project_backwards(properties().getProjectBackwards());
134e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    pprops->set_projection_receiver(properties().isProjectionReceiver());
135e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    set(pprops->mutable_clip_bounds(), properties().getClipBounds());
136e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck
137e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    const Outline& outline = properties().getOutline();
138e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    if (outline.getType() != Outline::Type::None) {
139e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck        proto::Outline* poutline = pprops->mutable_outline();
140e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck        poutline->clear_path();
141e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck        if (outline.getType() == Outline::Type::Empty) {
142e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck            poutline->set_type(proto::Outline_Type_Empty);
143e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck        } else if (outline.getType() == Outline::Type::ConvexPath) {
144e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck            poutline->set_type(proto::Outline_Type_ConvexPath);
145e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck            if (const SkPath* path = outline.getPath()) {
146e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck                set(poutline->mutable_path(), *path);
147e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck            }
148e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck        } else if (outline.getType() == Outline::Type::RoundRect) {
149e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck            poutline->set_type(proto::Outline_Type_RoundRect);
150e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck        } else {
151e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck            ALOGW("Uknown outline type! %d", static_cast<int>(outline.getType()));
152e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck            poutline->set_type(proto::Outline_Type_None);
153e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck        }
154e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck        poutline->set_should_clip(outline.getShouldClip());
155e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck        poutline->set_alpha(outline.getAlpha());
156e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck        poutline->set_radius(outline.getRadius());
157e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck        set(poutline->mutable_bounds(), outline.getBounds());
158e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    } else {
159e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck        pprops->clear_outline();
160e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    }
161e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck
162e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    const RevealClip& revealClip = properties().getRevealClip();
163e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    if (revealClip.willClip()) {
164e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck        proto::RevealClip* prevealClip = pprops->mutable_reveal_clip();
165e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck        prevealClip->set_x(revealClip.getX());
166e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck        prevealClip->set_y(revealClip.getY());
167e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck        prevealClip->set_radius(revealClip.getRadius());
168e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    } else {
169e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck        pprops->clear_reveal_clip();
170e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    }
171e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck
172e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    pnode->clear_children();
173e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    if (mDisplayListData) {
174e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck        for (auto&& child : mDisplayListData->children()) {
175e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck            child->mRenderNode->copyTo(pnode->add_children());
176e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck        }
177e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    }
178e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck}
179e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck
180fe5e7b7346a54537b980796ceeca66bfdbd05561John Reckint RenderNode::getDebugSize() {
181fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck    int size = sizeof(RenderNode);
182fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck    if (mStagingDisplayListData) {
1838afd0f245cc0c4a0366f39f41b5f78e47ee83be3Chris Craik        size += mStagingDisplayListData->getUsedSize();
184fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck    }
185fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck    if (mDisplayListData && mDisplayListData != mStagingDisplayListData) {
1868afd0f245cc0c4a0366f39f41b5f78e47ee83be3Chris Craik        size += mDisplayListData->getUsedSize();
187fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck    }
188fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck    return size;
189fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck}
190fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck
191f4198b713e43c0c0f9adac74203cf24c2a49b802John Reckvoid RenderNode::prepareTree(TreeInfo& info) {
192f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck    ATRACE_CALL();
19369e5adffb19135d51bde8e458f4907d7265f3e23Chris Craik    LOG_ALWAYS_FATAL_IF(!info.damageAccumulator, "DamageAccumulator missing");
194f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck
195a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craik    // Functors don't correctly handle stencil usage of overdraw debugging - shove 'em in a layer.
196a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craik    bool functorsNeedLayer = Properties::debugOverdraw;
197a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craik
198a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craik    prepareTreeImpl(info, functorsNeedLayer);
199f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck}
200f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck
20168bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reckvoid RenderNode::addAnimator(const sp<BaseRenderNodeAnimator>& animator) {
20268bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck    mAnimatorManager.addAnimator(animator);
20368bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck}
20468bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck
205e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reckvoid RenderNode::damageSelf(TreeInfo& info) {
206ce9f308063cc2bc7851f3f58c881da8a7271eeccJohn Reck    if (isRenderable()) {
207293e868bfc1b07aad2cb2ebeee5b07424852c510John Reck        if (properties().getClipDamageToBounds()) {
208a447d29c65fb811cd184775a3476101a1cede929John Reck            info.damageAccumulator->dirty(0, 0, properties().getWidth(), properties().getHeight());
209a447d29c65fb811cd184775a3476101a1cede929John Reck        } else {
210a447d29c65fb811cd184775a3476101a1cede929John Reck            // Hope this is big enough?
211a447d29c65fb811cd184775a3476101a1cede929John Reck            // TODO: Get this from the display list ops or something
212c128823940fb0be96eda810fa9f0c75f66d944b0John Reck            info.damageAccumulator->dirty(DIRTY_MIN, DIRTY_MIN, DIRTY_MAX, DIRTY_MAX);
213a447d29c65fb811cd184775a3476101a1cede929John Reck        }
214e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck    }
215e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck}
216e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck
217a7c2ea20c43ab797bef5801530687e22e83def8fJohn Reckvoid RenderNode::prepareLayer(TreeInfo& info, uint32_t dirtyMask) {
218856f0cc3e382206e67c1a62fe878627d7dd9ada5Chris Craik    LayerType layerType = properties().effectiveLayerType();
219182952f5eeefc2a21d76d4664ada0e2d78c1105cChris Craik    if (CC_UNLIKELY(layerType == LayerType::RenderLayer)) {
220a7c2ea20c43ab797bef5801530687e22e83def8fJohn Reck        // Damage applied so far needs to affect our parent, but does not require
221a7c2ea20c43ab797bef5801530687e22e83def8fJohn Reck        // the layer to be updated. So we pop/push here to clear out the current
222a7c2ea20c43ab797bef5801530687e22e83def8fJohn Reck        // damage and get a clean state for display list or children updates to
223a7c2ea20c43ab797bef5801530687e22e83def8fJohn Reck        // affect, which will require the layer to be updated
224a7c2ea20c43ab797bef5801530687e22e83def8fJohn Reck        info.damageAccumulator->popTransform();
225a7c2ea20c43ab797bef5801530687e22e83def8fJohn Reck        info.damageAccumulator->pushTransform(this);
226a7c2ea20c43ab797bef5801530687e22e83def8fJohn Reck        if (dirtyMask & DISPLAY_LIST) {
227a7c2ea20c43ab797bef5801530687e22e83def8fJohn Reck            damageSelf(info);
228a7c2ea20c43ab797bef5801530687e22e83def8fJohn Reck        }
22925fbb3fa1138675379102a44405852555cefccbdJohn Reck    }
23025fbb3fa1138675379102a44405852555cefccbdJohn Reck}
23125fbb3fa1138675379102a44405852555cefccbdJohn Reck
23225fbb3fa1138675379102a44405852555cefccbdJohn Reckvoid RenderNode::pushLayerUpdate(TreeInfo& info) {
233856f0cc3e382206e67c1a62fe878627d7dd9ada5Chris Craik    LayerType layerType = properties().effectiveLayerType();
23425fbb3fa1138675379102a44405852555cefccbdJohn Reck    // If we are not a layer OR we cannot be rendered (eg, view was detached)
23525fbb3fa1138675379102a44405852555cefccbdJohn Reck    // we need to destroy any Layers we may have had previously
236182952f5eeefc2a21d76d4664ada0e2d78c1105cChris Craik    if (CC_LIKELY(layerType != LayerType::RenderLayer) || CC_UNLIKELY(!isRenderable())) {
23725fbb3fa1138675379102a44405852555cefccbdJohn Reck        if (CC_UNLIKELY(mLayer)) {
23825fbb3fa1138675379102a44405852555cefccbdJohn Reck            LayerRenderer::destroyLayer(mLayer);
239d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik            mLayer = nullptr;
24025fbb3fa1138675379102a44405852555cefccbdJohn Reck        }
24125fbb3fa1138675379102a44405852555cefccbdJohn Reck        return;
24225fbb3fa1138675379102a44405852555cefccbdJohn Reck    }
24325fbb3fa1138675379102a44405852555cefccbdJohn Reck
24469e5adffb19135d51bde8e458f4907d7265f3e23Chris Craik    bool transformUpdateNeeded = false;
24525fbb3fa1138675379102a44405852555cefccbdJohn Reck    if (!mLayer) {
2463b20251a355c88193c439f928a84ae69483fb488John Reck        mLayer = LayerRenderer::createRenderLayer(info.renderState, getWidth(), getHeight());
24725fbb3fa1138675379102a44405852555cefccbdJohn Reck        applyLayerPropertiesToLayer(info);
24825fbb3fa1138675379102a44405852555cefccbdJohn Reck        damageSelf(info);
24969e5adffb19135d51bde8e458f4907d7265f3e23Chris Craik        transformUpdateNeeded = true;
25025fbb3fa1138675379102a44405852555cefccbdJohn Reck    } else if (mLayer->layer.getWidth() != getWidth() || mLayer->layer.getHeight() != getHeight()) {
251c25e506f7fc08790c0532f927f31310a2ca778b7John Reck        if (!LayerRenderer::resizeLayer(mLayer, getWidth(), getHeight())) {
252c25e506f7fc08790c0532f927f31310a2ca778b7John Reck            LayerRenderer::destroyLayer(mLayer);
253d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik            mLayer = nullptr;
254c25e506f7fc08790c0532f927f31310a2ca778b7John Reck        }
25525fbb3fa1138675379102a44405852555cefccbdJohn Reck        damageSelf(info);
25669e5adffb19135d51bde8e458f4907d7265f3e23Chris Craik        transformUpdateNeeded = true;
25769e5adffb19135d51bde8e458f4907d7265f3e23Chris Craik    }
25869e5adffb19135d51bde8e458f4907d7265f3e23Chris Craik
25925fbb3fa1138675379102a44405852555cefccbdJohn Reck    SkRect dirty;
26025fbb3fa1138675379102a44405852555cefccbdJohn Reck    info.damageAccumulator->peekAtDirty(&dirty);
26125fbb3fa1138675379102a44405852555cefccbdJohn Reck
262c25e506f7fc08790c0532f927f31310a2ca778b7John Reck    if (!mLayer) {
2630e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck        Caches::getInstance().dumpMemoryUsage();
264c25e506f7fc08790c0532f927f31310a2ca778b7John Reck        if (info.errorHandler) {
265c25e506f7fc08790c0532f927f31310a2ca778b7John Reck            std::string msg = "Unable to create layer for ";
266c25e506f7fc08790c0532f927f31310a2ca778b7John Reck            msg += getName();
267c25e506f7fc08790c0532f927f31310a2ca778b7John Reck            info.errorHandler->onError(msg);
268c25e506f7fc08790c0532f927f31310a2ca778b7John Reck        }
269c25e506f7fc08790c0532f927f31310a2ca778b7John Reck        return;
270c25e506f7fc08790c0532f927f31310a2ca778b7John Reck    }
271c25e506f7fc08790c0532f927f31310a2ca778b7John Reck
272c71bfcaa182e3d4fd9874362d3b4781fda934a21Chris Craik    if (transformUpdateNeeded) {
273c71bfcaa182e3d4fd9874362d3b4781fda934a21Chris Craik        // update the transform in window of the layer to reset its origin wrt light source position
274c71bfcaa182e3d4fd9874362d3b4781fda934a21Chris Craik        Matrix4 windowTransform;
275c71bfcaa182e3d4fd9874362d3b4781fda934a21Chris Craik        info.damageAccumulator->computeCurrentTransform(&windowTransform);
276c71bfcaa182e3d4fd9874362d3b4781fda934a21Chris Craik        mLayer->setWindowTransform(windowTransform);
277c71bfcaa182e3d4fd9874362d3b4781fda934a21Chris Craik    }
278c79eabcd3c6306bb2ec75f9584b79e661f265127John Reck
279c79eabcd3c6306bb2ec75f9584b79e661f265127John Reck    if (dirty.intersect(0, 0, getWidth(), getHeight())) {
28071487eb0ceb2b7dea02649e78d99bb5952f5eaefMike Reed        dirty.roundOut(&dirty);
28125fbb3fa1138675379102a44405852555cefccbdJohn Reck        mLayer->updateDeferred(this, dirty.fLeft, dirty.fTop, dirty.fRight, dirty.fBottom);
28225fbb3fa1138675379102a44405852555cefccbdJohn Reck    }
28325fbb3fa1138675379102a44405852555cefccbdJohn Reck    // This is not inside the above if because we may have called
28425fbb3fa1138675379102a44405852555cefccbdJohn Reck    // updateDeferred on a previous prepare pass that didn't have a renderer
28525fbb3fa1138675379102a44405852555cefccbdJohn Reck    if (info.renderer && mLayer->deferredUpdateScheduled) {
28625fbb3fa1138675379102a44405852555cefccbdJohn Reck        info.renderer->pushLayerUpdate(mLayer);
28725fbb3fa1138675379102a44405852555cefccbdJohn Reck    }
288998a6d81896df8b662cc10ddeb35087b78b38d72John Reck
28900e79c9947b741194ff6c0d08ede9b3befbf9c9dJohn Reck    if (info.canvasContext) {
29000e79c9947b741194ff6c0d08ede9b3befbf9c9dJohn Reck        // There might be prefetched layers that need to be accounted for.
29100e79c9947b741194ff6c0d08ede9b3befbf9c9dJohn Reck        // That might be us, so tell CanvasContext that this layer is in the
29200e79c9947b741194ff6c0d08ede9b3befbf9c9dJohn Reck        // tree and should not be destroyed.
293998a6d81896df8b662cc10ddeb35087b78b38d72John Reck        info.canvasContext->markLayerInUse(this);
294998a6d81896df8b662cc10ddeb35087b78b38d72John Reck    }
29525fbb3fa1138675379102a44405852555cefccbdJohn Reck}
29625fbb3fa1138675379102a44405852555cefccbdJohn Reck
297a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craik/**
298a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craik * Traverse down the the draw tree to prepare for a frame.
299a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craik *
300a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craik * MODE_FULL = UI Thread-driven (thus properties must be synced), otherwise RT driven
301a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craik *
302a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craik * While traversing down the tree, functorsNeedLayer flag is set to true if anything that uses the
303a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craik * stencil buffer may be needed. Views that use a functor to draw will be forced onto a layer.
304a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craik */
305a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craikvoid RenderNode::prepareTreeImpl(TreeInfo& info, bool functorsNeedLayer) {
306a447d29c65fb811cd184775a3476101a1cede929John Reck    info.damageAccumulator->pushTransform(this);
307f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck
308dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck    if (info.mode == TreeInfo::MODE_FULL) {
30925fbb3fa1138675379102a44405852555cefccbdJohn Reck        pushStagingPropertiesChanges(info);
310e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck    }
3119eb9f6f8cbbbd87d45da8071aa54cb066a797723John Reck    uint32_t animatorDirtyMask = 0;
3129eb9f6f8cbbbd87d45da8071aa54cb066a797723John Reck    if (CC_LIKELY(info.runAnimations)) {
3139eb9f6f8cbbbd87d45da8071aa54cb066a797723John Reck        animatorDirtyMask = mAnimatorManager.animate(info);
3149eb9f6f8cbbbd87d45da8071aa54cb066a797723John Reck    }
315a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craik
3163f725f0a5abd23833176bdc26fda964e923e75deJohn Reck    bool willHaveFunctor = false;
3173f725f0a5abd23833176bdc26fda964e923e75deJohn Reck    if (info.mode == TreeInfo::MODE_FULL && mStagingDisplayListData) {
3183f725f0a5abd23833176bdc26fda964e923e75deJohn Reck        willHaveFunctor = !mStagingDisplayListData->functors.isEmpty();
3193f725f0a5abd23833176bdc26fda964e923e75deJohn Reck    } else if (mDisplayListData) {
3203f725f0a5abd23833176bdc26fda964e923e75deJohn Reck        willHaveFunctor = !mDisplayListData->functors.isEmpty();
3213f725f0a5abd23833176bdc26fda964e923e75deJohn Reck    }
322a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craik    bool childFunctorsNeedLayer = mProperties.prepareForFunctorPresence(
323a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craik            willHaveFunctor, functorsNeedLayer);
324a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craik
325a7c2ea20c43ab797bef5801530687e22e83def8fJohn Reck    prepareLayer(info, animatorDirtyMask);
326dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck    if (info.mode == TreeInfo::MODE_FULL) {
32725fbb3fa1138675379102a44405852555cefccbdJohn Reck        pushStagingDisplayListChanges(info);
32825fbb3fa1138675379102a44405852555cefccbdJohn Reck    }
329a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craik    prepareSubTree(info, childFunctorsNeedLayer, mDisplayListData);
33025fbb3fa1138675379102a44405852555cefccbdJohn Reck    pushLayerUpdate(info);
33125fbb3fa1138675379102a44405852555cefccbdJohn Reck
332a447d29c65fb811cd184775a3476101a1cede929John Reck    info.damageAccumulator->popTransform();
333f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck}
334f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck
33525fbb3fa1138675379102a44405852555cefccbdJohn Reckvoid RenderNode::pushStagingPropertiesChanges(TreeInfo& info) {
336ff941dcd815021bb20d6504eb486acb1e50592c3John Reck    // Push the animators first so that setupStartValueIfNecessary() is called
337ff941dcd815021bb20d6504eb486acb1e50592c3John Reck    // before properties() is trampled by stagingProperties(), as they are
338ff941dcd815021bb20d6504eb486acb1e50592c3John Reck    // required by some animators.
3399eb9f6f8cbbbd87d45da8071aa54cb066a797723John Reck    if (CC_LIKELY(info.runAnimations)) {
340119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck        mAnimatorManager.pushStaging();
3419eb9f6f8cbbbd87d45da8071aa54cb066a797723John Reck    }
342ff941dcd815021bb20d6504eb486acb1e50592c3John Reck    if (mDirtyPropertyFields) {
343ff941dcd815021bb20d6504eb486acb1e50592c3John Reck        mDirtyPropertyFields = 0;
344e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck        damageSelf(info);
345a447d29c65fb811cd184775a3476101a1cede929John Reck        info.damageAccumulator->popTransform();
346ff941dcd815021bb20d6504eb486acb1e50592c3John Reck        mProperties = mStagingProperties;
34725fbb3fa1138675379102a44405852555cefccbdJohn Reck        applyLayerPropertiesToLayer(info);
348e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck        // We could try to be clever and only re-damage if the matrix changed.
349e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck        // However, we don't need to worry about that. The cost of over-damaging
350e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck        // here is only going to be a single additional map rect of this node
351e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck        // plus a rect join(). The parent's transform (and up) will only be
352e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck        // performed once.
353a447d29c65fb811cd184775a3476101a1cede929John Reck        info.damageAccumulator->pushTransform(this);
354e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck        damageSelf(info);
355ff941dcd815021bb20d6504eb486acb1e50592c3John Reck    }
35625fbb3fa1138675379102a44405852555cefccbdJohn Reck}
35725fbb3fa1138675379102a44405852555cefccbdJohn Reck
35864bb413a664001c95c8439cf097dc3033f4ed733Andreas Gampevoid RenderNode::applyLayerPropertiesToLayer(TreeInfo& info) {
35925fbb3fa1138675379102a44405852555cefccbdJohn Reck    if (CC_LIKELY(!mLayer)) return;
36025fbb3fa1138675379102a44405852555cefccbdJohn Reck
36125fbb3fa1138675379102a44405852555cefccbdJohn Reck    const LayerProperties& props = properties().layerProperties();
36225fbb3fa1138675379102a44405852555cefccbdJohn Reck    mLayer->setAlpha(props.alpha(), props.xferMode());
36325fbb3fa1138675379102a44405852555cefccbdJohn Reck    mLayer->setColorFilter(props.colorFilter());
36425fbb3fa1138675379102a44405852555cefccbdJohn Reck    mLayer->setBlend(props.needsBlending());
36525fbb3fa1138675379102a44405852555cefccbdJohn Reck}
36625fbb3fa1138675379102a44405852555cefccbdJohn Reck
36725fbb3fa1138675379102a44405852555cefccbdJohn Reckvoid RenderNode::pushStagingDisplayListChanges(TreeInfo& info) {
3688de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck    if (mNeedsDisplayListDataSync) {
3698de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck        mNeedsDisplayListDataSync = false;
370dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck        // Make sure we inc first so that we don't fluctuate between 0 and 1,
371dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck        // which would thrash the layer cache
372dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck        if (mStagingDisplayListData) {
373dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck            for (size_t i = 0; i < mStagingDisplayListData->children().size(); i++) {
374dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck                mStagingDisplayListData->children()[i]->mRenderNode->incParentRefCount();
375dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck            }
376dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck        }
3775c9d717d3e7064db76d2a01223af51379dadf2c3John Reck        // Damage with the old display list first then the new one to catch any
3785c9d717d3e7064db76d2a01223af51379dadf2c3John Reck        // changes in isRenderable or, in the future, bounds
3795c9d717d3e7064db76d2a01223af51379dadf2c3John Reck        damageSelf(info);
380dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck        deleteDisplayListData();
3818de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck        mDisplayListData = mStagingDisplayListData;
382d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik        mStagingDisplayListData = nullptr;
38309d5cddf67b676018700bcc10a72242641cd7eecJohn Reck        if (mDisplayListData) {
38409d5cddf67b676018700bcc10a72242641cd7eecJohn Reck            for (size_t i = 0; i < mDisplayListData->functors.size(); i++) {
385d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik                (*mDisplayListData->functors[i])(DrawGlInfo::kModeSync, nullptr);
38609d5cddf67b676018700bcc10a72242641cd7eecJohn Reck            }
38709d5cddf67b676018700bcc10a72242641cd7eecJohn Reck        }
388e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck        damageSelf(info);
3898de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck    }
3908de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck}
391113e0824d6bddf4376240681f9cf6a2deded9498John Reck
392dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reckvoid RenderNode::deleteDisplayListData() {
393dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck    if (mDisplayListData) {
394dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck        for (size_t i = 0; i < mDisplayListData->children().size(); i++) {
395dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck            mDisplayListData->children()[i]->mRenderNode->decParentRefCount();
396dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck        }
397dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck    }
398dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck    delete mDisplayListData;
399d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    mDisplayListData = nullptr;
400dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck}
401dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck
402a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craikvoid RenderNode::prepareSubTree(TreeInfo& info, bool functorsNeedLayer, DisplayListData* subtree) {
4038de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck    if (subtree) {
404860d155f866cc15a725e7ce03763280987f24901John Reck        TextureCache& cache = Caches::getInstance().textureCache;
40509d5cddf67b676018700bcc10a72242641cd7eecJohn Reck        info.out.hasFunctors |= subtree->functors.size();
406860d155f866cc15a725e7ce03763280987f24901John Reck        for (size_t i = 0; info.prepareTextures && i < subtree->bitmapResources.size(); i++) {
40700e79c9947b741194ff6c0d08ede9b3befbf9c9dJohn Reck            info.prepareTextures = cache.prefetchAndMarkInUse(
40800e79c9947b741194ff6c0d08ede9b3befbf9c9dJohn Reck                    info.canvasContext, subtree->bitmapResources[i]);
409f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck        }
4108de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck        for (size_t i = 0; i < subtree->children().size(); i++) {
411a7090e0cfd7c719a6d4c03aae34f5db98754cbddChris Craik            DrawRenderNodeOp* op = subtree->children()[i];
412a7090e0cfd7c719a6d4c03aae34f5db98754cbddChris Craik            RenderNode* childNode = op->mRenderNode;
413a447d29c65fb811cd184775a3476101a1cede929John Reck            info.damageAccumulator->pushTransform(&op->mTransformFromParent);
414a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craik            bool childFunctorsNeedLayer = functorsNeedLayer
415a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craik                    // Recorded with non-rect clip, or canvas-rotated by parent
416a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craik                    || op->mRecordedWithPotentialStencilClip;
417a766cb2bce5db9108c0266fbebea6aa18d5713ffChris Craik            childNode->prepareTreeImpl(info, childFunctorsNeedLayer);
418a447d29c65fb811cd184775a3476101a1cede929John Reck            info.damageAccumulator->popTransform();
4195bf11bb98f5dbe278c257355d24c181237abd68cJohn Reck        }
420113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
421113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
422113e0824d6bddf4376240681f9cf6a2deded9498John Reck
423dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reckvoid RenderNode::destroyHardwareResources() {
424dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck    if (mLayer) {
425dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck        LayerRenderer::destroyLayer(mLayer);
426d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik        mLayer = nullptr;
427dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck    }
428dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck    if (mDisplayListData) {
429dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck        for (size_t i = 0; i < mDisplayListData->children().size(); i++) {
430dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck            mDisplayListData->children()[i]->mRenderNode->destroyHardwareResources();
431dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck        }
432dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck        if (mNeedsDisplayListDataSync) {
433dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck            // Next prepare tree we are going to push a new display list, so we can
434dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck            // drop our current one now
435dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck            deleteDisplayListData();
436dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck        }
437dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck    }
438dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck}
439dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck
440dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reckvoid RenderNode::decParentRefCount() {
441dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck    LOG_ALWAYS_FATAL_IF(!mParentCount, "already 0!");
442dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck    mParentCount--;
443dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck    if (!mParentCount) {
444dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck        // If a child of ours is being attached to our parent then this will incorrectly
445dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck        // destroy its hardware resources. However, this situation is highly unlikely
446dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck        // and the failure is "just" that the layer is re-created, so this should
447dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck        // be safe enough
448dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck        destroyHardwareResources();
449dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck    }
450dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck}
451dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck
452113e0824d6bddf4376240681f9cf6a2deded9498John Reck/*
453113e0824d6bddf4376240681f9cf6a2deded9498John Reck * For property operations, we pass a savecount of 0, since the operations aren't part of the
454113e0824d6bddf4376240681f9cf6a2deded9498John Reck * displaylist, and thus don't have to compensate for the record-time/playback-time discrepancy in
455d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck * base saveCount (i.e., how RestoreToCount uses saveCount + properties().getCount())
456113e0824d6bddf4376240681f9cf6a2deded9498John Reck */
457113e0824d6bddf4376240681f9cf6a2deded9498John Reck#define PROPERTY_SAVECOUNT 0
458113e0824d6bddf4376240681f9cf6a2deded9498John Reck
459113e0824d6bddf4376240681f9cf6a2deded9498John Recktemplate <class T>
460b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craikvoid RenderNode::setViewProperties(OpenGLRenderer& renderer, T& handler) {
461113e0824d6bddf4376240681f9cf6a2deded9498John Reck#if DEBUG_DISPLAY_LIST
462b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    properties().debugOutputProperties(handler.level() + 1);
463113e0824d6bddf4376240681f9cf6a2deded9498John Reck#endif
464d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (properties().getLeft() != 0 || properties().getTop() != 0) {
465d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        renderer.translate(properties().getLeft(), properties().getTop());
466113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
467d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (properties().getStaticMatrix()) {
468139088228faa7f3c446af7387e017933998a5570Derek Sollenberger        renderer.concatMatrix(*properties().getStaticMatrix());
469d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    } else if (properties().getAnimationMatrix()) {
470139088228faa7f3c446af7387e017933998a5570Derek Sollenberger        renderer.concatMatrix(*properties().getAnimationMatrix());
471113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
472f7483e3af0513a1baa8341d403df2e0c0896a9ffJohn Reck    if (properties().hasTransformMatrix()) {
473f7483e3af0513a1baa8341d403df2e0c0896a9ffJohn Reck        if (properties().isTransformTranslateOnly()) {
474d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            renderer.translate(properties().getTranslationX(), properties().getTranslationY());
475113e0824d6bddf4376240681f9cf6a2deded9498John Reck        } else {
476d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            renderer.concatMatrix(*properties().getTransformMatrix());
477113e0824d6bddf4376240681f9cf6a2deded9498John Reck        }
478113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
479856f0cc3e382206e67c1a62fe878627d7dd9ada5Chris Craik    const bool isLayer = properties().effectiveLayerType() != LayerType::None;
480a753f4c6cb8558795e673df1896532cd148781e2Chris Craik    int clipFlags = properties().getClippingFlags();
481d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (properties().getAlpha() < 1) {
48225fbb3fa1138675379102a44405852555cefccbdJohn Reck        if (isLayer) {
483a753f4c6cb8558795e673df1896532cd148781e2Chris Craik            clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
484113e0824d6bddf4376240681f9cf6a2deded9498John Reck        }
4854e9d9b2a590105035c29f12ecf0689333e6b55e7Chris Craik        if (CC_LIKELY(isLayer || !properties().getHasOverlappingRendering())) {
4864e9d9b2a590105035c29f12ecf0689333e6b55e7Chris Craik            // simply scale rendering content's alpha
4874e9d9b2a590105035c29f12ecf0689333e6b55e7Chris Craik            renderer.scaleAlpha(properties().getAlpha());
4884e9d9b2a590105035c29f12ecf0689333e6b55e7Chris Craik        } else {
4894e9d9b2a590105035c29f12ecf0689333e6b55e7Chris Craik            // savelayer needed to create an offscreen buffer
4904e9d9b2a590105035c29f12ecf0689333e6b55e7Chris Craik            Rect layerBounds(0, 0, getWidth(), getHeight());
4914e9d9b2a590105035c29f12ecf0689333e6b55e7Chris Craik            if (clipFlags) {
4924e9d9b2a590105035c29f12ecf0689333e6b55e7Chris Craik                properties().getClippingRectForFlags(clipFlags, &layerBounds);
4934e9d9b2a590105035c29f12ecf0689333e6b55e7Chris Craik                clipFlags = 0; // all clipping done by savelayer
4944e9d9b2a590105035c29f12ecf0689333e6b55e7Chris Craik            }
4954e9d9b2a590105035c29f12ecf0689333e6b55e7Chris Craik            SaveLayerOp* op = new (handler.allocator()) SaveLayerOp(
4964e9d9b2a590105035c29f12ecf0689333e6b55e7Chris Craik                    layerBounds.left, layerBounds.top,
4974e9d9b2a590105035c29f12ecf0689333e6b55e7Chris Craik                    layerBounds.right, layerBounds.bottom,
4984e9d9b2a590105035c29f12ecf0689333e6b55e7Chris Craik                    (int) (properties().getAlpha() * 255),
4994e9d9b2a590105035c29f12ecf0689333e6b55e7Chris Craik                    SkCanvas::kHasAlphaLayer_SaveFlag | SkCanvas::kClipToLayer_SaveFlag);
5004e9d9b2a590105035c29f12ecf0689333e6b55e7Chris Craik            handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
5014e9d9b2a590105035c29f12ecf0689333e6b55e7Chris Craik        }
5021a0808e64c170fa7b620b858fa5875f6f08a2b54Chris Craik
5031a0808e64c170fa7b620b858fa5875f6f08a2b54Chris Craik        if (CC_UNLIKELY(ATRACE_ENABLED() && properties().promotedToLayer())) {
5044e9d9b2a590105035c29f12ecf0689333e6b55e7Chris Craik            // pretend alpha always causes savelayer to warn about
5054e9d9b2a590105035c29f12ecf0689333e6b55e7Chris Craik            // performance problem affecting old versions
5061a0808e64c170fa7b620b858fa5875f6f08a2b54Chris Craik            ATRACE_FORMAT("%s alpha caused saveLayer %dx%d", getName(),
5071a0808e64c170fa7b620b858fa5875f6f08a2b54Chris Craik                    static_cast<int>(getWidth()),
5081a0808e64c170fa7b620b858fa5875f6f08a2b54Chris Craik                    static_cast<int>(getHeight()));
5091a0808e64c170fa7b620b858fa5875f6f08a2b54Chris Craik        }
510113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
511a753f4c6cb8558795e673df1896532cd148781e2Chris Craik    if (clipFlags) {
512a753f4c6cb8558795e673df1896532cd148781e2Chris Craik        Rect clipRect;
513a753f4c6cb8558795e673df1896532cd148781e2Chris Craik        properties().getClippingRectForFlags(clipFlags, &clipRect);
5148c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik        ClipRectOp* op = new (handler.allocator()) ClipRectOp(
515a753f4c6cb8558795e673df1896532cd148781e2Chris Craik                clipRect.left, clipRect.top, clipRect.right, clipRect.bottom,
516a753f4c6cb8558795e673df1896532cd148781e2Chris Craik                SkRegion::kIntersect_Op);
517d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
518113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
5198c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik
520e83cbd451868a734bfac07ccd680d5617080b579Chris Craik    // TODO: support nesting round rect clips
521af4d04cab6d48ae0d6a5e79bd30f679af87abaadChris Craik    if (mProperties.getRevealClip().willClip()) {
522af4d04cab6d48ae0d6a5e79bd30f679af87abaadChris Craik        Rect bounds;
523af4d04cab6d48ae0d6a5e79bd30f679af87abaadChris Craik        mProperties.getRevealClip().getBounds(&bounds);
524af4d04cab6d48ae0d6a5e79bd30f679af87abaadChris Craik        renderer.setClippingRoundRect(handler.allocator(), bounds, mProperties.getRevealClip().getRadius());
525af4d04cab6d48ae0d6a5e79bd30f679af87abaadChris Craik    } else if (mProperties.getOutline().willClip()) {
526af4d04cab6d48ae0d6a5e79bd30f679af87abaadChris Craik        renderer.setClippingOutline(handler.allocator(), &(mProperties.getOutline()));
527113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
528113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
529113e0824d6bddf4376240681f9cf6a2deded9498John Reck
530113e0824d6bddf4376240681f9cf6a2deded9498John Reck/**
531113e0824d6bddf4376240681f9cf6a2deded9498John Reck * Apply property-based transformations to input matrix
532113e0824d6bddf4376240681f9cf6a2deded9498John Reck *
533113e0824d6bddf4376240681f9cf6a2deded9498John Reck * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
534113e0824d6bddf4376240681f9cf6a2deded9498John Reck * matrix computation instead of the Skia 3x3 matrix + camera hackery.
535113e0824d6bddf4376240681f9cf6a2deded9498John Reck */
53669e5adffb19135d51bde8e458f4907d7265f3e23Chris Craikvoid RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) const {
537d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (properties().getLeft() != 0 || properties().getTop() != 0) {
538d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        matrix.translate(properties().getLeft(), properties().getTop());
539113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
540d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (properties().getStaticMatrix()) {
541d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        mat4 stat(*properties().getStaticMatrix());
542113e0824d6bddf4376240681f9cf6a2deded9498John Reck        matrix.multiply(stat);
543d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    } else if (properties().getAnimationMatrix()) {
544d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        mat4 anim(*properties().getAnimationMatrix());
545113e0824d6bddf4376240681f9cf6a2deded9498John Reck        matrix.multiply(anim);
546113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
547e0bb87d4bdbd3b08ab6a8569c8e564ed59b8a5a7Chris Craik
548cc39e16cb98855f35079941b5e7e6eac2b7bc388Chris Craik    bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getZ());
549e0bb87d4bdbd3b08ab6a8569c8e564ed59b8a5a7Chris Craik    if (properties().hasTransformMatrix() || applyTranslationZ) {
550f7483e3af0513a1baa8341d403df2e0c0896a9ffJohn Reck        if (properties().isTransformTranslateOnly()) {
551d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
552cc39e16cb98855f35079941b5e7e6eac2b7bc388Chris Craik                    true3dTransform ? properties().getZ() : 0.0f);
553113e0824d6bddf4376240681f9cf6a2deded9498John Reck        } else {
554113e0824d6bddf4376240681f9cf6a2deded9498John Reck            if (!true3dTransform) {
555d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                matrix.multiply(*properties().getTransformMatrix());
556113e0824d6bddf4376240681f9cf6a2deded9498John Reck            } else {
557113e0824d6bddf4376240681f9cf6a2deded9498John Reck                mat4 true3dMat;
558113e0824d6bddf4376240681f9cf6a2deded9498John Reck                true3dMat.loadTranslate(
559d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                        properties().getPivotX() + properties().getTranslationX(),
560d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                        properties().getPivotY() + properties().getTranslationY(),
561cc39e16cb98855f35079941b5e7e6eac2b7bc388Chris Craik                        properties().getZ());
562d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
563d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
564d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                true3dMat.rotate(properties().getRotation(), 0, 0, 1);
565d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
566d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
567113e0824d6bddf4376240681f9cf6a2deded9498John Reck
568113e0824d6bddf4376240681f9cf6a2deded9498John Reck                matrix.multiply(true3dMat);
569113e0824d6bddf4376240681f9cf6a2deded9498John Reck            }
570113e0824d6bddf4376240681f9cf6a2deded9498John Reck        }
571113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
572113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
573113e0824d6bddf4376240681f9cf6a2deded9498John Reck
574113e0824d6bddf4376240681f9cf6a2deded9498John Reck/**
575113e0824d6bddf4376240681f9cf6a2deded9498John Reck * Organizes the DisplayList hierarchy to prepare for background projection reordering.
576113e0824d6bddf4376240681f9cf6a2deded9498John Reck *
577113e0824d6bddf4376240681f9cf6a2deded9498John Reck * This should be called before a call to defer() or drawDisplayList()
578113e0824d6bddf4376240681f9cf6a2deded9498John Reck *
579113e0824d6bddf4376240681f9cf6a2deded9498John Reck * Each DisplayList that serves as a 3d root builds its list of composited children,
580113e0824d6bddf4376240681f9cf6a2deded9498John Reck * which are flagged to not draw in the standard draw loop.
581113e0824d6bddf4376240681f9cf6a2deded9498John Reck */
582113e0824d6bddf4376240681f9cf6a2deded9498John Reckvoid RenderNode::computeOrdering() {
583113e0824d6bddf4376240681f9cf6a2deded9498John Reck    ATRACE_CALL();
584113e0824d6bddf4376240681f9cf6a2deded9498John Reck    mProjectedNodes.clear();
585113e0824d6bddf4376240681f9cf6a2deded9498John Reck
586113e0824d6bddf4376240681f9cf6a2deded9498John Reck    // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that
587113e0824d6bddf4376240681f9cf6a2deded9498John Reck    // transform properties are applied correctly to top level children
588d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    if (mDisplayListData == nullptr) return;
589087bc0c14bdccf7c258dce0cdef46a69a839b427John Reck    for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
590a7090e0cfd7c719a6d4c03aae34f5db98754cbddChris Craik        DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
591f293259c0d7331892dfc87180f813ccfd0101c09Derek Sollenberger        childOp->mRenderNode->computeOrderingImpl(childOp, &mProjectedNodes, &mat4::identity());
592113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
593113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
594113e0824d6bddf4376240681f9cf6a2deded9498John Reck
595113e0824d6bddf4376240681f9cf6a2deded9498John Reckvoid RenderNode::computeOrderingImpl(
596a7090e0cfd7c719a6d4c03aae34f5db98754cbddChris Craik        DrawRenderNodeOp* opState,
597272a685f17cc4828257e521a6f62b7b17870f75eJohn Reck        std::vector<DrawRenderNodeOp*>* compositedChildrenOfProjectionSurface,
598113e0824d6bddf4376240681f9cf6a2deded9498John Reck        const mat4* transformFromProjectionSurface) {
599113e0824d6bddf4376240681f9cf6a2deded9498John Reck    mProjectedNodes.clear();
600d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    if (mDisplayListData == nullptr || mDisplayListData->isEmpty()) return;
601113e0824d6bddf4376240681f9cf6a2deded9498John Reck
602113e0824d6bddf4376240681f9cf6a2deded9498John Reck    // TODO: should avoid this calculation in most cases
603113e0824d6bddf4376240681f9cf6a2deded9498John Reck    // TODO: just calculate single matrix, down to all leaf composited elements
604113e0824d6bddf4376240681f9cf6a2deded9498John Reck    Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface);
605113e0824d6bddf4376240681f9cf6a2deded9498John Reck    localTransformFromProjectionSurface.multiply(opState->mTransformFromParent);
606113e0824d6bddf4376240681f9cf6a2deded9498John Reck
607d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (properties().getProjectBackwards()) {
608113e0824d6bddf4376240681f9cf6a2deded9498John Reck        // composited projectee, flag for out of order draw, save matrix, and store in proj surface
609113e0824d6bddf4376240681f9cf6a2deded9498John Reck        opState->mSkipInOrderDraw = true;
6107c85c54499994c687a833644f7f213e747fadb98Chris Craik        opState->mTransformFromCompositingAncestor = localTransformFromProjectionSurface;
611272a685f17cc4828257e521a6f62b7b17870f75eJohn Reck        compositedChildrenOfProjectionSurface->push_back(opState);
612113e0824d6bddf4376240681f9cf6a2deded9498John Reck    } else {
613113e0824d6bddf4376240681f9cf6a2deded9498John Reck        // standard in order draw
614113e0824d6bddf4376240681f9cf6a2deded9498John Reck        opState->mSkipInOrderDraw = false;
615113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
616113e0824d6bddf4376240681f9cf6a2deded9498John Reck
617087bc0c14bdccf7c258dce0cdef46a69a839b427John Reck    if (mDisplayListData->children().size() > 0) {
618113e0824d6bddf4376240681f9cf6a2deded9498John Reck        const bool isProjectionReceiver = mDisplayListData->projectionReceiveIndex >= 0;
619113e0824d6bddf4376240681f9cf6a2deded9498John Reck        bool haveAppliedPropertiesToProjection = false;
620087bc0c14bdccf7c258dce0cdef46a69a839b427John Reck        for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
621a7090e0cfd7c719a6d4c03aae34f5db98754cbddChris Craik            DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
622a7090e0cfd7c719a6d4c03aae34f5db98754cbddChris Craik            RenderNode* child = childOp->mRenderNode;
623113e0824d6bddf4376240681f9cf6a2deded9498John Reck
624272a685f17cc4828257e521a6f62b7b17870f75eJohn Reck            std::vector<DrawRenderNodeOp*>* projectionChildren = nullptr;
625d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik            const mat4* projectionTransform = nullptr;
626d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            if (isProjectionReceiver && !child->properties().getProjectBackwards()) {
627bf72eb80d9e00fe2fa360dff0ef581ea27c8e965Chris Craik                // if receiving projections, collect projecting descendant
628113e0824d6bddf4376240681f9cf6a2deded9498John Reck
629bf72eb80d9e00fe2fa360dff0ef581ea27c8e965Chris Craik                // Note that if a direct descendant is projecting backwards, we pass its
630bf72eb80d9e00fe2fa360dff0ef581ea27c8e965Chris Craik                // grandparent projection collection, since it shouldn't project onto its
631113e0824d6bddf4376240681f9cf6a2deded9498John Reck                // parent, where it will already be drawing.
632113e0824d6bddf4376240681f9cf6a2deded9498John Reck                projectionChildren = &mProjectedNodes;
633113e0824d6bddf4376240681f9cf6a2deded9498John Reck                projectionTransform = &mat4::identity();
634113e0824d6bddf4376240681f9cf6a2deded9498John Reck            } else {
635113e0824d6bddf4376240681f9cf6a2deded9498John Reck                if (!haveAppliedPropertiesToProjection) {
636113e0824d6bddf4376240681f9cf6a2deded9498John Reck                    applyViewPropertyTransforms(localTransformFromProjectionSurface);
637113e0824d6bddf4376240681f9cf6a2deded9498John Reck                    haveAppliedPropertiesToProjection = true;
638113e0824d6bddf4376240681f9cf6a2deded9498John Reck                }
639113e0824d6bddf4376240681f9cf6a2deded9498John Reck                projectionChildren = compositedChildrenOfProjectionSurface;
640113e0824d6bddf4376240681f9cf6a2deded9498John Reck                projectionTransform = &localTransformFromProjectionSurface;
641113e0824d6bddf4376240681f9cf6a2deded9498John Reck            }
642f293259c0d7331892dfc87180f813ccfd0101c09Derek Sollenberger            child->computeOrderingImpl(childOp, projectionChildren, projectionTransform);
643113e0824d6bddf4376240681f9cf6a2deded9498John Reck        }
644113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
645113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
646113e0824d6bddf4376240681f9cf6a2deded9498John Reck
647113e0824d6bddf4376240681f9cf6a2deded9498John Reckclass DeferOperationHandler {
648113e0824d6bddf4376240681f9cf6a2deded9498John Reckpublic:
649113e0824d6bddf4376240681f9cf6a2deded9498John Reck    DeferOperationHandler(DeferStateStruct& deferStruct, int level)
650113e0824d6bddf4376240681f9cf6a2deded9498John Reck        : mDeferStruct(deferStruct), mLevel(level) {}
651113e0824d6bddf4376240681f9cf6a2deded9498John Reck    inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
652113e0824d6bddf4376240681f9cf6a2deded9498John Reck        operation->defer(mDeferStruct, saveCount, mLevel, clipToBounds);
653113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
654113e0824d6bddf4376240681f9cf6a2deded9498John Reck    inline LinearAllocator& allocator() { return *(mDeferStruct.mAllocator); }
65564bb413a664001c95c8439cf097dc3033f4ed733Andreas Gampe    inline void startMark(const char* name) {} // do nothing
656b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    inline void endMark() {}
657b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    inline int level() { return mLevel; }
658b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    inline int replayFlags() { return mDeferStruct.mReplayFlags; }
6597466986d2055eb8711f36a85ac539b1572ffe805Chris Craik    inline SkPath* allocPathForFrame() { return mDeferStruct.allocPathForFrame(); }
660113e0824d6bddf4376240681f9cf6a2deded9498John Reck
661113e0824d6bddf4376240681f9cf6a2deded9498John Reckprivate:
662113e0824d6bddf4376240681f9cf6a2deded9498John Reck    DeferStateStruct& mDeferStruct;
663113e0824d6bddf4376240681f9cf6a2deded9498John Reck    const int mLevel;
664113e0824d6bddf4376240681f9cf6a2deded9498John Reck};
665113e0824d6bddf4376240681f9cf6a2deded9498John Reck
66680d4902196899d1325cd9f52c06ae0174cf9bd4cChris Craikvoid RenderNode::defer(DeferStateStruct& deferStruct, const int level) {
667113e0824d6bddf4376240681f9cf6a2deded9498John Reck    DeferOperationHandler handler(deferStruct, level);
668b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler);
669113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
670113e0824d6bddf4376240681f9cf6a2deded9498John Reck
671113e0824d6bddf4376240681f9cf6a2deded9498John Reckclass ReplayOperationHandler {
672113e0824d6bddf4376240681f9cf6a2deded9498John Reckpublic:
673113e0824d6bddf4376240681f9cf6a2deded9498John Reck    ReplayOperationHandler(ReplayStateStruct& replayStruct, int level)
674113e0824d6bddf4376240681f9cf6a2deded9498John Reck        : mReplayStruct(replayStruct), mLevel(level) {}
675113e0824d6bddf4376240681f9cf6a2deded9498John Reck    inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
676113e0824d6bddf4376240681f9cf6a2deded9498John Reck#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
6773f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik        mReplayStruct.mRenderer.eventMark(operation->name());
678113e0824d6bddf4376240681f9cf6a2deded9498John Reck#endif
679113e0824d6bddf4376240681f9cf6a2deded9498John Reck        operation->replay(mReplayStruct, saveCount, mLevel, clipToBounds);
680113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
681113e0824d6bddf4376240681f9cf6a2deded9498John Reck    inline LinearAllocator& allocator() { return *(mReplayStruct.mAllocator); }
682b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    inline void startMark(const char* name) {
683b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik        mReplayStruct.mRenderer.startMark(name);
684b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    }
685b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    inline void endMark() {
686b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik        mReplayStruct.mRenderer.endMark();
687b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    }
688b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    inline int level() { return mLevel; }
689b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    inline int replayFlags() { return mReplayStruct.mReplayFlags; }
6907466986d2055eb8711f36a85ac539b1572ffe805Chris Craik    inline SkPath* allocPathForFrame() { return mReplayStruct.allocPathForFrame(); }
691113e0824d6bddf4376240681f9cf6a2deded9498John Reck
692113e0824d6bddf4376240681f9cf6a2deded9498John Reckprivate:
693113e0824d6bddf4376240681f9cf6a2deded9498John Reck    ReplayStateStruct& mReplayStruct;
694113e0824d6bddf4376240681f9cf6a2deded9498John Reck    const int mLevel;
695113e0824d6bddf4376240681f9cf6a2deded9498John Reck};
696113e0824d6bddf4376240681f9cf6a2deded9498John Reck
69780d4902196899d1325cd9f52c06ae0174cf9bd4cChris Craikvoid RenderNode::replay(ReplayStateStruct& replayStruct, const int level) {
698b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    ReplayOperationHandler handler(replayStruct, level);
699b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler);
700113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
701113e0824d6bddf4376240681f9cf6a2deded9498John Reck
7028afd0f245cc0c4a0366f39f41b5f78e47ee83be3Chris Craikvoid RenderNode::buildZSortedChildList(const DisplayListData::Chunk& chunk,
703272a685f17cc4828257e521a6f62b7b17870f75eJohn Reck        std::vector<ZDrawRenderNodeOpPair>& zTranslatedNodes) {
7048afd0f245cc0c4a0366f39f41b5f78e47ee83be3Chris Craik    if (chunk.beginChildIndex == chunk.endChildIndex) return;
705113e0824d6bddf4376240681f9cf6a2deded9498John Reck
7068afd0f245cc0c4a0366f39f41b5f78e47ee83be3Chris Craik    for (unsigned int i = chunk.beginChildIndex; i < chunk.endChildIndex; i++) {
707a7090e0cfd7c719a6d4c03aae34f5db98754cbddChris Craik        DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
708a7090e0cfd7c719a6d4c03aae34f5db98754cbddChris Craik        RenderNode* child = childOp->mRenderNode;
709cc39e16cb98855f35079941b5e7e6eac2b7bc388Chris Craik        float childZ = child->properties().getZ();
710113e0824d6bddf4376240681f9cf6a2deded9498John Reck
7118afd0f245cc0c4a0366f39f41b5f78e47ee83be3Chris Craik        if (!MathUtils::isZero(childZ) && chunk.reorderChildren) {
712272a685f17cc4828257e521a6f62b7b17870f75eJohn Reck            zTranslatedNodes.push_back(ZDrawRenderNodeOpPair(childZ, childOp));
713113e0824d6bddf4376240681f9cf6a2deded9498John Reck            childOp->mSkipInOrderDraw = true;
714d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        } else if (!child->properties().getProjectBackwards()) {
715113e0824d6bddf4376240681f9cf6a2deded9498John Reck            // regular, in order drawing DisplayList
716113e0824d6bddf4376240681f9cf6a2deded9498John Reck            childOp->mSkipInOrderDraw = false;
717113e0824d6bddf4376240681f9cf6a2deded9498John Reck        }
718113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
719113e0824d6bddf4376240681f9cf6a2deded9498John Reck
7208afd0f245cc0c4a0366f39f41b5f78e47ee83be3Chris Craik    // Z sort any 3d children (stable-ness makes z compare fall back to standard drawing order)
721113e0824d6bddf4376240681f9cf6a2deded9498John Reck    std::stable_sort(zTranslatedNodes.begin(), zTranslatedNodes.end());
722113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
723113e0824d6bddf4376240681f9cf6a2deded9498John Reck
724b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craiktemplate <class T>
725b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craikvoid RenderNode::issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler) {
72677b5cad3efedd20f2b7cc14d87ccce1b0261960aChris Craik    if (properties().getAlpha() <= 0.0f
72777b5cad3efedd20f2b7cc14d87ccce1b0261960aChris Craik            || properties().getOutline().getAlpha() <= 0.0f
7288d0ec389531d071529fb0a800f10733b057205d9Teng-Hui Zhu            || !properties().getOutline().getPath()
7298d0ec389531d071529fb0a800f10733b057205d9Teng-Hui Zhu            || properties().getScaleX() == 0
7308d0ec389531d071529fb0a800f10733b057205d9Teng-Hui Zhu            || properties().getScaleY() == 0) {
73177b5cad3efedd20f2b7cc14d87ccce1b0261960aChris Craik        // no shadow to draw
73277b5cad3efedd20f2b7cc14d87ccce1b0261960aChris Craik        return;
73377b5cad3efedd20f2b7cc14d87ccce1b0261960aChris Craik    }
734b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik
735b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    mat4 shadowMatrixXY(transformFromParent);
736b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    applyViewPropertyTransforms(shadowMatrixXY);
737b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik
738b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    // Z matrix needs actual 3d transformation, so mapped z values will be correct
739b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    mat4 shadowMatrixZ(transformFromParent);
740b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    applyViewPropertyTransforms(shadowMatrixZ, true);
741b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik
7427466986d2055eb8711f36a85ac539b1572ffe805Chris Craik    const SkPath* casterOutlinePath = properties().getOutline().getPath();
743af4d04cab6d48ae0d6a5e79bd30f679af87abaadChris Craik    const SkPath* revealClipPath = properties().getRevealClip().getPath();
74461317325b7b4b4ffafd9400ab5966e8d7c67df2eChris Craik    if (revealClipPath && revealClipPath->isEmpty()) return;
74561317325b7b4b4ffafd9400ab5966e8d7c67df2eChris Craik
74677b5cad3efedd20f2b7cc14d87ccce1b0261960aChris Craik    float casterAlpha = properties().getAlpha() * properties().getOutline().getAlpha();
7477466986d2055eb8711f36a85ac539b1572ffe805Chris Craik
748faa79ff9d964de940660d2226d5b06ded9294597Chris Craik
749faa79ff9d964de940660d2226d5b06ded9294597Chris Craik    // holds temporary SkPath to store the result of intersections
750d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    SkPath* frameAllocatedPath = nullptr;
7517466986d2055eb8711f36a85ac539b1572ffe805Chris Craik    const SkPath* outlinePath = casterOutlinePath;
752faa79ff9d964de940660d2226d5b06ded9294597Chris Craik
753faa79ff9d964de940660d2226d5b06ded9294597Chris Craik    // intersect the outline with the reveal clip, if present
7547466986d2055eb8711f36a85ac539b1572ffe805Chris Craik    if (revealClipPath) {
755faa79ff9d964de940660d2226d5b06ded9294597Chris Craik        frameAllocatedPath = handler.allocPathForFrame();
756faa79ff9d964de940660d2226d5b06ded9294597Chris Craik
75702a26300fb129608eb1df8cf31c14a5a5d8bd6edTom Hudson        Op(*outlinePath, *revealClipPath, kIntersect_SkPathOp, frameAllocatedPath);
758faa79ff9d964de940660d2226d5b06ded9294597Chris Craik        outlinePath = frameAllocatedPath;
759faa79ff9d964de940660d2226d5b06ded9294597Chris Craik    }
760faa79ff9d964de940660d2226d5b06ded9294597Chris Craik
761faa79ff9d964de940660d2226d5b06ded9294597Chris Craik    // intersect the outline with the clipBounds, if present
762faa79ff9d964de940660d2226d5b06ded9294597Chris Craik    if (properties().getClippingFlags() & CLIP_TO_CLIP_BOUNDS) {
763faa79ff9d964de940660d2226d5b06ded9294597Chris Craik        if (!frameAllocatedPath) {
764faa79ff9d964de940660d2226d5b06ded9294597Chris Craik            frameAllocatedPath = handler.allocPathForFrame();
765faa79ff9d964de940660d2226d5b06ded9294597Chris Craik        }
766faa79ff9d964de940660d2226d5b06ded9294597Chris Craik
767faa79ff9d964de940660d2226d5b06ded9294597Chris Craik        Rect clipBounds;
768faa79ff9d964de940660d2226d5b06ded9294597Chris Craik        properties().getClippingRectForFlags(CLIP_TO_CLIP_BOUNDS, &clipBounds);
769faa79ff9d964de940660d2226d5b06ded9294597Chris Craik        SkPath clipBoundsPath;
770faa79ff9d964de940660d2226d5b06ded9294597Chris Craik        clipBoundsPath.addRect(clipBounds.left, clipBounds.top,
771faa79ff9d964de940660d2226d5b06ded9294597Chris Craik                clipBounds.right, clipBounds.bottom);
7727466986d2055eb8711f36a85ac539b1572ffe805Chris Craik
77302a26300fb129608eb1df8cf31c14a5a5d8bd6edTom Hudson        Op(*outlinePath, clipBoundsPath, kIntersect_SkPathOp, frameAllocatedPath);
7747466986d2055eb8711f36a85ac539b1572ffe805Chris Craik        outlinePath = frameAllocatedPath;
7757466986d2055eb8711f36a85ac539b1572ffe805Chris Craik    }
7767466986d2055eb8711f36a85ac539b1572ffe805Chris Craik
777b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    DisplayListOp* shadowOp  = new (handler.allocator()) DrawShadowOp(
7787466986d2055eb8711f36a85ac539b1572ffe805Chris Craik            shadowMatrixXY, shadowMatrixZ, casterAlpha, outlinePath);
779b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    handler(shadowOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
780b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik}
781b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik
782113e0824d6bddf4376240681f9cf6a2deded9498John Reck#define SHADOW_DELTA 0.1f
783113e0824d6bddf4376240681f9cf6a2deded9498John Reck
784113e0824d6bddf4376240681f9cf6a2deded9498John Recktemplate <class T>
785c3e75f9d54b3629b3fd27afafa2e07bd07dad9b3Chris Craikvoid RenderNode::issueOperationsOf3dChildren(ChildrenSelectMode mode,
786272a685f17cc4828257e521a6f62b7b17870f75eJohn Reck        const Matrix4& initialTransform, const std::vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
787c3e75f9d54b3629b3fd27afafa2e07bd07dad9b3Chris Craik        OpenGLRenderer& renderer, T& handler) {
788113e0824d6bddf4376240681f9cf6a2deded9498John Reck    const int size = zTranslatedNodes.size();
789113e0824d6bddf4376240681f9cf6a2deded9498John Reck    if (size == 0
790b9ce116dac378b4cf4490f265dcbd5704a1dd43cChris Craik            || (mode == ChildrenSelectMode::NegativeZChildren && zTranslatedNodes[0].key > 0.0f)
791b9ce116dac378b4cf4490f265dcbd5704a1dd43cChris Craik            || (mode == ChildrenSelectMode::PositiveZChildren && zTranslatedNodes[size - 1].key < 0.0f)) {
792113e0824d6bddf4376240681f9cf6a2deded9498John Reck        // no 3d children to draw
793113e0824d6bddf4376240681f9cf6a2deded9498John Reck        return;
794113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
795113e0824d6bddf4376240681f9cf6a2deded9498John Reck
796c3e75f9d54b3629b3fd27afafa2e07bd07dad9b3Chris Craik    // Apply the base transform of the parent of the 3d children. This isolates
797c3e75f9d54b3629b3fd27afafa2e07bd07dad9b3Chris Craik    // 3d children of the current chunk from transformations made in previous chunks.
798c3e75f9d54b3629b3fd27afafa2e07bd07dad9b3Chris Craik    int rootRestoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
7996daa13c5fa7577fa1d8371deca446f6ca911f38fChris Craik    renderer.setGlobalMatrix(initialTransform);
800c3e75f9d54b3629b3fd27afafa2e07bd07dad9b3Chris Craik
801113e0824d6bddf4376240681f9cf6a2deded9498John Reck    /**
802113e0824d6bddf4376240681f9cf6a2deded9498John Reck     * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
803113e0824d6bddf4376240681f9cf6a2deded9498John Reck     * with very similar Z heights to draw together.
804113e0824d6bddf4376240681f9cf6a2deded9498John Reck     *
805113e0824d6bddf4376240681f9cf6a2deded9498John Reck     * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
806113e0824d6bddf4376240681f9cf6a2deded9498John Reck     * underneath both, and neither's shadow is drawn on top of the other.
807113e0824d6bddf4376240681f9cf6a2deded9498John Reck     */
808113e0824d6bddf4376240681f9cf6a2deded9498John Reck    const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
809113e0824d6bddf4376240681f9cf6a2deded9498John Reck    size_t drawIndex, shadowIndex, endIndex;
810b9ce116dac378b4cf4490f265dcbd5704a1dd43cChris Craik    if (mode == ChildrenSelectMode::NegativeZChildren) {
811113e0824d6bddf4376240681f9cf6a2deded9498John Reck        drawIndex = 0;
812113e0824d6bddf4376240681f9cf6a2deded9498John Reck        endIndex = nonNegativeIndex;
813113e0824d6bddf4376240681f9cf6a2deded9498John Reck        shadowIndex = endIndex; // draw no shadows
814113e0824d6bddf4376240681f9cf6a2deded9498John Reck    } else {
815113e0824d6bddf4376240681f9cf6a2deded9498John Reck        drawIndex = nonNegativeIndex;
816113e0824d6bddf4376240681f9cf6a2deded9498John Reck        endIndex = size;
817113e0824d6bddf4376240681f9cf6a2deded9498John Reck        shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
818113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
8193f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik
8203f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik    DISPLAY_LIST_LOGD("%*s%d %s 3d children:", (handler.level() + 1) * 2, "",
8213f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik            endIndex - drawIndex, mode == kNegativeZChildren ? "negative" : "positive");
8223f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik
823113e0824d6bddf4376240681f9cf6a2deded9498John Reck    float lastCasterZ = 0.0f;
824113e0824d6bddf4376240681f9cf6a2deded9498John Reck    while (shadowIndex < endIndex || drawIndex < endIndex) {
825113e0824d6bddf4376240681f9cf6a2deded9498John Reck        if (shadowIndex < endIndex) {
826a7090e0cfd7c719a6d4c03aae34f5db98754cbddChris Craik            DrawRenderNodeOp* casterOp = zTranslatedNodes[shadowIndex].value;
827a7090e0cfd7c719a6d4c03aae34f5db98754cbddChris Craik            RenderNode* caster = casterOp->mRenderNode;
828113e0824d6bddf4376240681f9cf6a2deded9498John Reck            const float casterZ = zTranslatedNodes[shadowIndex].key;
829113e0824d6bddf4376240681f9cf6a2deded9498John Reck            // attempt to render the shadow if the caster about to be drawn is its caster,
830113e0824d6bddf4376240681f9cf6a2deded9498John Reck            // OR if its caster's Z value is similar to the previous potential caster
831113e0824d6bddf4376240681f9cf6a2deded9498John Reck            if (shadowIndex == drawIndex || casterZ - lastCasterZ < SHADOW_DELTA) {
832b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik                caster->issueDrawShadowOperation(casterOp->mTransformFromParent, handler);
833113e0824d6bddf4376240681f9cf6a2deded9498John Reck
834113e0824d6bddf4376240681f9cf6a2deded9498John Reck                lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
835113e0824d6bddf4376240681f9cf6a2deded9498John Reck                shadowIndex++;
836113e0824d6bddf4376240681f9cf6a2deded9498John Reck                continue;
837113e0824d6bddf4376240681f9cf6a2deded9498John Reck            }
838113e0824d6bddf4376240681f9cf6a2deded9498John Reck        }
839113e0824d6bddf4376240681f9cf6a2deded9498John Reck
840113e0824d6bddf4376240681f9cf6a2deded9498John Reck        // only the actual child DL draw needs to be in save/restore,
841113e0824d6bddf4376240681f9cf6a2deded9498John Reck        // since it modifies the renderer's matrix
842113e0824d6bddf4376240681f9cf6a2deded9498John Reck        int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
843113e0824d6bddf4376240681f9cf6a2deded9498John Reck
844a7090e0cfd7c719a6d4c03aae34f5db98754cbddChris Craik        DrawRenderNodeOp* childOp = zTranslatedNodes[drawIndex].value;
845113e0824d6bddf4376240681f9cf6a2deded9498John Reck
846113e0824d6bddf4376240681f9cf6a2deded9498John Reck        renderer.concatMatrix(childOp->mTransformFromParent);
847113e0824d6bddf4376240681f9cf6a2deded9498John Reck        childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
848d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
849113e0824d6bddf4376240681f9cf6a2deded9498John Reck        childOp->mSkipInOrderDraw = true;
850113e0824d6bddf4376240681f9cf6a2deded9498John Reck
851113e0824d6bddf4376240681f9cf6a2deded9498John Reck        renderer.restoreToCount(restoreTo);
852113e0824d6bddf4376240681f9cf6a2deded9498John Reck        drawIndex++;
853113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
854c3e75f9d54b3629b3fd27afafa2e07bd07dad9b3Chris Craik    renderer.restoreToCount(rootRestoreTo);
855113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
856113e0824d6bddf4376240681f9cf6a2deded9498John Reck
857113e0824d6bddf4376240681f9cf6a2deded9498John Recktemplate <class T>
858b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craikvoid RenderNode::issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler) {
8593f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik    DISPLAY_LIST_LOGD("%*s%d projected children:", (handler.level() + 1) * 2, "", mProjectedNodes.size());
8603f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik    const SkPath* projectionReceiverOutline = properties().getOutline().getPath();
8613f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik    int restoreTo = renderer.getSaveCount();
8623f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik
863b3cca876c2e11b865cb4f83abe2c48a60b95af5bChris Craik    LinearAllocator& alloc = handler.allocator();
864b3cca876c2e11b865cb4f83abe2c48a60b95af5bChris Craik    handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
865b3cca876c2e11b865cb4f83abe2c48a60b95af5bChris Craik            PROPERTY_SAVECOUNT, properties().getClipToBounds());
866b3cca876c2e11b865cb4f83abe2c48a60b95af5bChris Craik
867b3cca876c2e11b865cb4f83abe2c48a60b95af5bChris Craik    // Transform renderer to match background we're projecting onto
868b3cca876c2e11b865cb4f83abe2c48a60b95af5bChris Craik    // (by offsetting canvas by translationX/Y of background rendernode, since only those are set)
869b3cca876c2e11b865cb4f83abe2c48a60b95af5bChris Craik    const DisplayListOp* op =
870b3cca876c2e11b865cb4f83abe2c48a60b95af5bChris Craik            (mDisplayListData->displayListOps[mDisplayListData->projectionReceiveIndex]);
871b3cca876c2e11b865cb4f83abe2c48a60b95af5bChris Craik    const DrawRenderNodeOp* backgroundOp = reinterpret_cast<const DrawRenderNodeOp*>(op);
872b3cca876c2e11b865cb4f83abe2c48a60b95af5bChris Craik    const RenderProperties& backgroundProps = backgroundOp->mRenderNode->properties();
873b3cca876c2e11b865cb4f83abe2c48a60b95af5bChris Craik    renderer.translate(backgroundProps.getTranslationX(), backgroundProps.getTranslationY());
874b3cca876c2e11b865cb4f83abe2c48a60b95af5bChris Craik
875fca52b7583d1e5f5ff8ed06554875d2a30ef56faChris Craik    // If the projection reciever has an outline, we mask projected content to it
876fca52b7583d1e5f5ff8ed06554875d2a30ef56faChris Craik    // (which we know, apriori, are all tessellated paths)
877fca52b7583d1e5f5ff8ed06554875d2a30ef56faChris Craik    renderer.setProjectionPathMask(alloc, projectionReceiverOutline);
8783f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik
8793f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik    // draw projected nodes
880113e0824d6bddf4376240681f9cf6a2deded9498John Reck    for (size_t i = 0; i < mProjectedNodes.size(); i++) {
881a7090e0cfd7c719a6d4c03aae34f5db98754cbddChris Craik        DrawRenderNodeOp* childOp = mProjectedNodes[i];
882113e0824d6bddf4376240681f9cf6a2deded9498John Reck
883113e0824d6bddf4376240681f9cf6a2deded9498John Reck        // matrix save, concat, and restore can be done safely without allocating operations
884113e0824d6bddf4376240681f9cf6a2deded9498John Reck        int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
885113e0824d6bddf4376240681f9cf6a2deded9498John Reck        renderer.concatMatrix(childOp->mTransformFromCompositingAncestor);
886113e0824d6bddf4376240681f9cf6a2deded9498John Reck        childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
887d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
888113e0824d6bddf4376240681f9cf6a2deded9498John Reck        childOp->mSkipInOrderDraw = true;
889113e0824d6bddf4376240681f9cf6a2deded9498John Reck        renderer.restoreToCount(restoreTo);
890113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
8913f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik
892fca52b7583d1e5f5ff8ed06554875d2a30ef56faChris Craik    handler(new (alloc) RestoreToCountOp(restoreTo),
893fca52b7583d1e5f5ff8ed06554875d2a30ef56faChris Craik            PROPERTY_SAVECOUNT, properties().getClipToBounds());
894113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
895113e0824d6bddf4376240681f9cf6a2deded9498John Reck
896113e0824d6bddf4376240681f9cf6a2deded9498John Reck/**
897113e0824d6bddf4376240681f9cf6a2deded9498John Reck * This function serves both defer and replay modes, and will organize the displayList's component
898113e0824d6bddf4376240681f9cf6a2deded9498John Reck * operations for a single frame:
899113e0824d6bddf4376240681f9cf6a2deded9498John Reck *
900113e0824d6bddf4376240681f9cf6a2deded9498John Reck * Every 'simple' state operation that affects just the matrix and alpha (or other factors of
901113e0824d6bddf4376240681f9cf6a2deded9498John Reck * DeferredDisplayState) may be issued directly to the renderer, but complex operations (with custom
902113e0824d6bddf4376240681f9cf6a2deded9498John Reck * defer logic) and operations in displayListOps are issued through the 'handler' which handles the
903113e0824d6bddf4376240681f9cf6a2deded9498John Reck * defer vs replay logic, per operation
904113e0824d6bddf4376240681f9cf6a2deded9498John Reck */
905113e0824d6bddf4376240681f9cf6a2deded9498John Recktemplate <class T>
906b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craikvoid RenderNode::issueOperations(OpenGLRenderer& renderer, T& handler) {
9070645128b80621edee70f8cab4afb208fe0c26becChris Craik    if (mDisplayListData->isEmpty()) {
908bf72eb80d9e00fe2fa360dff0ef581ea27c8e965Chris Craik        DISPLAY_LIST_LOGD("%*sEmpty display list (%p, %s)", handler.level() * 2, "",
909bf72eb80d9e00fe2fa360dff0ef581ea27c8e965Chris Craik                this, getName());
9100645128b80621edee70f8cab4afb208fe0c26becChris Craik        return;
9110645128b80621edee70f8cab4afb208fe0c26becChris Craik    }
9120645128b80621edee70f8cab4afb208fe0c26becChris Craik
91351d6a3db97bdd5315f1a17a4b447d10a92217b98Chris Craik    const bool drawLayer = (mLayer && (&renderer != mLayer->renderer.get()));
91425fbb3fa1138675379102a44405852555cefccbdJohn Reck    // If we are updating the contents of mLayer, we don't want to apply any of
91525fbb3fa1138675379102a44405852555cefccbdJohn Reck    // the RenderNode's properties to this issueOperations pass. Those will all
91625fbb3fa1138675379102a44405852555cefccbdJohn Reck    // be applied when the layer is drawn, aka when this is true.
91725fbb3fa1138675379102a44405852555cefccbdJohn Reck    const bool useViewProperties = (!mLayer || drawLayer);
9180645128b80621edee70f8cab4afb208fe0c26becChris Craik    if (useViewProperties) {
9190645128b80621edee70f8cab4afb208fe0c26becChris Craik        const Outline& outline = properties().getOutline();
9208d0ec389531d071529fb0a800f10733b057205d9Teng-Hui Zhu        if (properties().getAlpha() <= 0
9218d0ec389531d071529fb0a800f10733b057205d9Teng-Hui Zhu                || (outline.getShouldClip() && outline.isEmpty())
9228d0ec389531d071529fb0a800f10733b057205d9Teng-Hui Zhu                || properties().getScaleX() == 0
9238d0ec389531d071529fb0a800f10733b057205d9Teng-Hui Zhu                || properties().getScaleY() == 0) {
924bf72eb80d9e00fe2fa360dff0ef581ea27c8e965Chris Craik            DISPLAY_LIST_LOGD("%*sRejected display list (%p, %s)", handler.level() * 2, "",
925bf72eb80d9e00fe2fa360dff0ef581ea27c8e965Chris Craik                    this, getName());
9260645128b80621edee70f8cab4afb208fe0c26becChris Craik            return;
9270645128b80621edee70f8cab4afb208fe0c26becChris Craik        }
928113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
929113e0824d6bddf4376240681f9cf6a2deded9498John Reck
9303f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik    handler.startMark(getName());
931b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik
932113e0824d6bddf4376240681f9cf6a2deded9498John Reck#if DEBUG_DISPLAY_LIST
9333f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik    const Rect& clipRect = renderer.getLocalClipBounds();
9343f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik    DISPLAY_LIST_LOGD("%*sStart display list (%p, %s), localClipBounds: %.0f, %.0f, %.0f, %.0f",
935031888744e24b5c7243ac99ec98b78aff5db1c78Chris Craik            handler.level() * 2, "", this, getName(),
9363f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik            clipRect.left, clipRect.top, clipRect.right, clipRect.bottom);
937113e0824d6bddf4376240681f9cf6a2deded9498John Reck#endif
938113e0824d6bddf4376240681f9cf6a2deded9498John Reck
939113e0824d6bddf4376240681f9cf6a2deded9498John Reck    LinearAllocator& alloc = handler.allocator();
940113e0824d6bddf4376240681f9cf6a2deded9498John Reck    int restoreTo = renderer.getSaveCount();
941113e0824d6bddf4376240681f9cf6a2deded9498John Reck    handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
942d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            PROPERTY_SAVECOUNT, properties().getClipToBounds());
943113e0824d6bddf4376240681f9cf6a2deded9498John Reck
944bf72eb80d9e00fe2fa360dff0ef581ea27c8e965Chris Craik    DISPLAY_LIST_LOGD("%*sSave %d %d", (handler.level() + 1) * 2, "",
945113e0824d6bddf4376240681f9cf6a2deded9498John Reck            SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag, restoreTo);
946113e0824d6bddf4376240681f9cf6a2deded9498John Reck
94725fbb3fa1138675379102a44405852555cefccbdJohn Reck    if (useViewProperties) {
94825fbb3fa1138675379102a44405852555cefccbdJohn Reck        setViewProperties<T>(renderer, handler);
94925fbb3fa1138675379102a44405852555cefccbdJohn Reck    }
950113e0824d6bddf4376240681f9cf6a2deded9498John Reck
9518c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik    bool quickRejected = properties().getClipToBounds()
9528c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik            && renderer.quickRejectConservative(0, 0, properties().getWidth(), properties().getHeight());
953113e0824d6bddf4376240681f9cf6a2deded9498John Reck    if (!quickRejected) {
954c3e75f9d54b3629b3fd27afafa2e07bd07dad9b3Chris Craik        Matrix4 initialTransform(*(renderer.currentTransform()));
955ac7b6d33d23cb0baaf61c723346198d41f012035Tom Hudson        renderer.setBaseTransform(initialTransform);
956c3e75f9d54b3629b3fd27afafa2e07bd07dad9b3Chris Craik
95725fbb3fa1138675379102a44405852555cefccbdJohn Reck        if (drawLayer) {
9583aadd60521960be063ee06208562ccb63dc414e3Chris Craik            handler(new (alloc) DrawLayerOp(mLayer),
95925fbb3fa1138675379102a44405852555cefccbdJohn Reck                    renderer.getSaveCount() - 1, properties().getClipToBounds());
96025fbb3fa1138675379102a44405852555cefccbdJohn Reck        } else {
961c166b6c4fc3e1158ca08cfed24639409161055cfChris Craik            const int saveCountOffset = renderer.getSaveCount() - 1;
962c166b6c4fc3e1158ca08cfed24639409161055cfChris Craik            const int projectionReceiveIndex = mDisplayListData->projectionReceiveIndex;
9638afd0f245cc0c4a0366f39f41b5f78e47ee83be3Chris Craik            for (size_t chunkIndex = 0; chunkIndex < mDisplayListData->getChunks().size(); chunkIndex++) {
9648afd0f245cc0c4a0366f39f41b5f78e47ee83be3Chris Craik                const DisplayListData::Chunk& chunk = mDisplayListData->getChunks()[chunkIndex];
96525fbb3fa1138675379102a44405852555cefccbdJohn Reck
966272a685f17cc4828257e521a6f62b7b17870f75eJohn Reck                std::vector<ZDrawRenderNodeOpPair> zTranslatedNodes;
9678afd0f245cc0c4a0366f39f41b5f78e47ee83be3Chris Craik                buildZSortedChildList(chunk, zTranslatedNodes);
96825fbb3fa1138675379102a44405852555cefccbdJohn Reck
969b9ce116dac378b4cf4490f265dcbd5704a1dd43cChris Craik                issueOperationsOf3dChildren(ChildrenSelectMode::NegativeZChildren,
970c3e75f9d54b3629b3fd27afafa2e07bd07dad9b3Chris Craik                        initialTransform, zTranslatedNodes, renderer, handler);
971c3e75f9d54b3629b3fd27afafa2e07bd07dad9b3Chris Craik
97225fbb3fa1138675379102a44405852555cefccbdJohn Reck
973edaecc1db0584fa017822dfc2da0c968b53967e6Andreas Gampe                for (size_t opIndex = chunk.beginOpIndex; opIndex < chunk.endOpIndex; opIndex++) {
9748afd0f245cc0c4a0366f39f41b5f78e47ee83be3Chris Craik                    DisplayListOp *op = mDisplayListData->displayListOps[opIndex];
97580d4902196899d1325cd9f52c06ae0174cf9bd4cChris Craik#if DEBUG_DISPLAY_LIST
976031888744e24b5c7243ac99ec98b78aff5db1c78Chris Craik                    op->output(handler.level() + 1);
97780d4902196899d1325cd9f52c06ae0174cf9bd4cChris Craik#endif
9788afd0f245cc0c4a0366f39f41b5f78e47ee83be3Chris Craik                    handler(op, saveCountOffset, properties().getClipToBounds());
97925fbb3fa1138675379102a44405852555cefccbdJohn Reck
980272a685f17cc4828257e521a6f62b7b17870f75eJohn Reck                    if (CC_UNLIKELY(!mProjectedNodes.empty() && projectionReceiveIndex >= 0 &&
981edaecc1db0584fa017822dfc2da0c968b53967e6Andreas Gampe                        opIndex == static_cast<size_t>(projectionReceiveIndex))) {
9828afd0f245cc0c4a0366f39f41b5f78e47ee83be3Chris Craik                        issueOperationsOfProjectedChildren(renderer, handler);
9838afd0f245cc0c4a0366f39f41b5f78e47ee83be3Chris Craik                    }
98425fbb3fa1138675379102a44405852555cefccbdJohn Reck                }
985113e0824d6bddf4376240681f9cf6a2deded9498John Reck
986b9ce116dac378b4cf4490f265dcbd5704a1dd43cChris Craik                issueOperationsOf3dChildren(ChildrenSelectMode::PositiveZChildren,
987c3e75f9d54b3629b3fd27afafa2e07bd07dad9b3Chris Craik                        initialTransform, zTranslatedNodes, renderer, handler);
9888afd0f245cc0c4a0366f39f41b5f78e47ee83be3Chris Craik            }
98925fbb3fa1138675379102a44405852555cefccbdJohn Reck        }
990113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
991113e0824d6bddf4376240681f9cf6a2deded9498John Reck
992bf72eb80d9e00fe2fa360dff0ef581ea27c8e965Chris Craik    DISPLAY_LIST_LOGD("%*sRestoreToCount %d", (handler.level() + 1) * 2, "", restoreTo);
993113e0824d6bddf4376240681f9cf6a2deded9498John Reck    handler(new (alloc) RestoreToCountOp(restoreTo),
994d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            PROPERTY_SAVECOUNT, properties().getClipToBounds());
995b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik
996bf72eb80d9e00fe2fa360dff0ef581ea27c8e965Chris Craik    DISPLAY_LIST_LOGD("%*sDone (%p, %s)", handler.level() * 2, "", this, getName());
997b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    handler.endMark();
998113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
999113e0824d6bddf4376240681f9cf6a2deded9498John Reck
1000113e0824d6bddf4376240681f9cf6a2deded9498John Reck} /* namespace uirenderer */
1001113e0824d6bddf4376240681f9cf6a2deded9498John Reck} /* namespace android */
1002