RenderNode.cpp revision e0bb87d4bdbd3b08ab6a8569c8e564ed59b8a5a7
1113e0824d6bddf4376240681f9cf6a2deded9498John Reck/*
2113e0824d6bddf4376240681f9cf6a2deded9498John Reck * Copyright (C) 2014 The Android Open Source Project
3113e0824d6bddf4376240681f9cf6a2deded9498John Reck *
4113e0824d6bddf4376240681f9cf6a2deded9498John Reck * Licensed under the Apache License, Version 2.0 (the "License");
5113e0824d6bddf4376240681f9cf6a2deded9498John Reck * you may not use this file except in compliance with the License.
6113e0824d6bddf4376240681f9cf6a2deded9498John Reck * You may obtain a copy of the License at
7113e0824d6bddf4376240681f9cf6a2deded9498John Reck *
8113e0824d6bddf4376240681f9cf6a2deded9498John Reck *      http://www.apache.org/licenses/LICENSE-2.0
9113e0824d6bddf4376240681f9cf6a2deded9498John Reck *
10113e0824d6bddf4376240681f9cf6a2deded9498John Reck * Unless required by applicable law or agreed to in writing, software
11113e0824d6bddf4376240681f9cf6a2deded9498John Reck * distributed under the License is distributed on an "AS IS" BASIS,
12113e0824d6bddf4376240681f9cf6a2deded9498John Reck * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13113e0824d6bddf4376240681f9cf6a2deded9498John Reck * See the License for the specific language governing permissions and
14113e0824d6bddf4376240681f9cf6a2deded9498John Reck * limitations under the License.
15113e0824d6bddf4376240681f9cf6a2deded9498John Reck */
16113e0824d6bddf4376240681f9cf6a2deded9498John Reck
17113e0824d6bddf4376240681f9cf6a2deded9498John Reck#define ATRACE_TAG ATRACE_TAG_VIEW
18113e0824d6bddf4376240681f9cf6a2deded9498John Reck
19113e0824d6bddf4376240681f9cf6a2deded9498John Reck#include "RenderNode.h"
20113e0824d6bddf4376240681f9cf6a2deded9498John Reck
21113e0824d6bddf4376240681f9cf6a2deded9498John Reck#include <SkCanvas.h>
22113e0824d6bddf4376240681f9cf6a2deded9498John Reck#include <algorithm>
23113e0824d6bddf4376240681f9cf6a2deded9498John Reck
24113e0824d6bddf4376240681f9cf6a2deded9498John Reck#include <utils/Trace.h>
25113e0824d6bddf4376240681f9cf6a2deded9498John Reck
26113e0824d6bddf4376240681f9cf6a2deded9498John Reck#include "Debug.h"
27113e0824d6bddf4376240681f9cf6a2deded9498John Reck#include "DisplayListOp.h"
28113e0824d6bddf4376240681f9cf6a2deded9498John Reck#include "DisplayListLogBuffer.h"
29e0bb87d4bdbd3b08ab6a8569c8e564ed59b8a5a7Chris Craik#include "utils/MathUtils.h"
30113e0824d6bddf4376240681f9cf6a2deded9498John Reck
31113e0824d6bddf4376240681f9cf6a2deded9498John Recknamespace android {
32113e0824d6bddf4376240681f9cf6a2deded9498John Recknamespace uirenderer {
33113e0824d6bddf4376240681f9cf6a2deded9498John Reck
34113e0824d6bddf4376240681f9cf6a2deded9498John Reckvoid RenderNode::outputLogBuffer(int fd) {
35113e0824d6bddf4376240681f9cf6a2deded9498John Reck    DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance();
36113e0824d6bddf4376240681f9cf6a2deded9498John Reck    if (logBuffer.isEmpty()) {
37113e0824d6bddf4376240681f9cf6a2deded9498John Reck        return;
38113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
39113e0824d6bddf4376240681f9cf6a2deded9498John Reck
40113e0824d6bddf4376240681f9cf6a2deded9498John Reck    FILE *file = fdopen(fd, "a");
41113e0824d6bddf4376240681f9cf6a2deded9498John Reck
42113e0824d6bddf4376240681f9cf6a2deded9498John Reck    fprintf(file, "\nRecent DisplayList operations\n");
43113e0824d6bddf4376240681f9cf6a2deded9498John Reck    logBuffer.outputCommands(file);
44113e0824d6bddf4376240681f9cf6a2deded9498John Reck
45113e0824d6bddf4376240681f9cf6a2deded9498John Reck    String8 cachesLog;
46113e0824d6bddf4376240681f9cf6a2deded9498John Reck    Caches::getInstance().dumpMemoryUsage(cachesLog);
47113e0824d6bddf4376240681f9cf6a2deded9498John Reck    fprintf(file, "\nCaches:\n%s", cachesLog.string());
48113e0824d6bddf4376240681f9cf6a2deded9498John Reck    fprintf(file, "\n");
49113e0824d6bddf4376240681f9cf6a2deded9498John Reck
50113e0824d6bddf4376240681f9cf6a2deded9498John Reck    fflush(file);
51113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
52113e0824d6bddf4376240681f9cf6a2deded9498John Reck
538de65a8e05285df52a1e6f0c1d5616dd233298a7John ReckRenderNode::RenderNode()
54143912fef8eff58146705849a0ba441ab6163409Chris Craik        : mNeedsPropertiesSync(false)
558de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck        , mNeedsDisplayListDataSync(false)
568de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck        , mDisplayListData(0)
578de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck        , mStagingDisplayListData(0) {
58113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
59113e0824d6bddf4376240681f9cf6a2deded9498John Reck
60113e0824d6bddf4376240681f9cf6a2deded9498John ReckRenderNode::~RenderNode() {
61113e0824d6bddf4376240681f9cf6a2deded9498John Reck    delete mDisplayListData;
628de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck    delete mStagingDisplayListData;
63113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
64113e0824d6bddf4376240681f9cf6a2deded9498John Reck
658de65a8e05285df52a1e6f0c1d5616dd233298a7John Reckvoid RenderNode::setStagingDisplayList(DisplayListData* data) {
668de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck    mNeedsDisplayListDataSync = true;
678de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck    delete mStagingDisplayListData;
688de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck    mStagingDisplayListData = data;
698de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck    if (mStagingDisplayListData) {
708de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck        Caches::getInstance().registerFunctors(mStagingDisplayListData->functorCount);
71113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
72113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
73113e0824d6bddf4376240681f9cf6a2deded9498John Reck
74113e0824d6bddf4376240681f9cf6a2deded9498John Reck/**
75113e0824d6bddf4376240681f9cf6a2deded9498John Reck * This function is a simplified version of replay(), where we simply retrieve and log the
76113e0824d6bddf4376240681f9cf6a2deded9498John Reck * display list. This function should remain in sync with the replay() function.
77113e0824d6bddf4376240681f9cf6a2deded9498John Reck */
78113e0824d6bddf4376240681f9cf6a2deded9498John Reckvoid RenderNode::output(uint32_t level) {
79113e0824d6bddf4376240681f9cf6a2deded9498John Reck    ALOGD("%*sStart display list (%p, %s, render=%d)", (level - 1) * 2, "", this,
80113e0824d6bddf4376240681f9cf6a2deded9498John Reck            mName.string(), isRenderable());
81113e0824d6bddf4376240681f9cf6a2deded9498John Reck    ALOGD("%*s%s %d", level * 2, "", "Save",
82113e0824d6bddf4376240681f9cf6a2deded9498John Reck            SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
83113e0824d6bddf4376240681f9cf6a2deded9498John Reck
84d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    properties().debugOutputProperties(level);
85113e0824d6bddf4376240681f9cf6a2deded9498John Reck    int flags = DisplayListOp::kOpLogFlag_Recurse;
86113e0824d6bddf4376240681f9cf6a2deded9498John Reck    for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) {
87113e0824d6bddf4376240681f9cf6a2deded9498John Reck        mDisplayListData->displayListOps[i]->output(level, flags);
88113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
89113e0824d6bddf4376240681f9cf6a2deded9498John Reck
90113e0824d6bddf4376240681f9cf6a2deded9498John Reck    ALOGD("%*sDone (%p, %s)", (level - 1) * 2, "", this, mName.string());
91113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
92113e0824d6bddf4376240681f9cf6a2deded9498John Reck
93f4198b713e43c0c0f9adac74203cf24c2a49b802John Reckvoid RenderNode::prepareTree(TreeInfo& info) {
94f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck    ATRACE_CALL();
95f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck
96f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck    prepareTreeImpl(info);
97f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck}
98f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck
99f4198b713e43c0c0f9adac74203cf24c2a49b802John Reckvoid RenderNode::prepareTreeImpl(TreeInfo& info) {
100f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck    pushStagingChanges(info);
101f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck    prepareSubTree(info, mDisplayListData);
102f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck}
103f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck
104f4198b713e43c0c0f9adac74203cf24c2a49b802John Reckvoid RenderNode::pushStagingChanges(TreeInfo& info) {
105d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (mNeedsPropertiesSync) {
106d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        mNeedsPropertiesSync = false;
107d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        mProperties = mStagingProperties;
108113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
1098de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck    if (mNeedsDisplayListDataSync) {
1108de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck        mNeedsDisplayListDataSync = false;
1118de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck        // Do a push pass on the old tree to handle freeing DisplayListData
1128de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck        // that are no longer used
113860d155f866cc15a725e7ce03763280987f24901John Reck        TreeInfo oldTreeInfo;
114f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck        prepareSubTree(oldTreeInfo, mDisplayListData);
115f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck        // TODO: The damage for the old tree should be accounted for
1168de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck        delete mDisplayListData;
1178de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck        mDisplayListData = mStagingDisplayListData;
1188de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck        mStagingDisplayListData = 0;
1198de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck    }
1208de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck}
121113e0824d6bddf4376240681f9cf6a2deded9498John Reck
122f4198b713e43c0c0f9adac74203cf24c2a49b802John Reckvoid RenderNode::prepareSubTree(TreeInfo& info, DisplayListData* subtree) {
1238de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck    if (subtree) {
124860d155f866cc15a725e7ce03763280987f24901John Reck        TextureCache& cache = Caches::getInstance().textureCache;
125860d155f866cc15a725e7ce03763280987f24901John Reck        info.hasFunctors |= subtree->functorCount;
126860d155f866cc15a725e7ce03763280987f24901John Reck        // TODO: Fix ownedBitmapResources to not require disabling prepareTextures
127860d155f866cc15a725e7ce03763280987f24901John Reck        // and thus falling out of async drawing path.
128860d155f866cc15a725e7ce03763280987f24901John Reck        if (subtree->ownedBitmapResources.size()) {
129860d155f866cc15a725e7ce03763280987f24901John Reck            info.prepareTextures = false;
130860d155f866cc15a725e7ce03763280987f24901John Reck        }
131860d155f866cc15a725e7ce03763280987f24901John Reck        for (size_t i = 0; info.prepareTextures && i < subtree->bitmapResources.size(); i++) {
132860d155f866cc15a725e7ce03763280987f24901John Reck            info.prepareTextures = cache.prefetchAndMarkInUse(subtree->bitmapResources[i]);
133f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck        }
1348de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck        for (size_t i = 0; i < subtree->children().size(); i++) {
1358de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck            RenderNode* childNode = subtree->children()[i]->mDisplayList;
136f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck            childNode->prepareTreeImpl(info);
1375bf11bb98f5dbe278c257355d24c181237abd68cJohn Reck        }
138113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
139113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
140113e0824d6bddf4376240681f9cf6a2deded9498John Reck
141113e0824d6bddf4376240681f9cf6a2deded9498John Reck/*
142113e0824d6bddf4376240681f9cf6a2deded9498John Reck * For property operations, we pass a savecount of 0, since the operations aren't part of the
143113e0824d6bddf4376240681f9cf6a2deded9498John Reck * displaylist, and thus don't have to compensate for the record-time/playback-time discrepancy in
144d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck * base saveCount (i.e., how RestoreToCount uses saveCount + properties().getCount())
145113e0824d6bddf4376240681f9cf6a2deded9498John Reck */
146113e0824d6bddf4376240681f9cf6a2deded9498John Reck#define PROPERTY_SAVECOUNT 0
147113e0824d6bddf4376240681f9cf6a2deded9498John Reck
148113e0824d6bddf4376240681f9cf6a2deded9498John Recktemplate <class T>
149b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craikvoid RenderNode::setViewProperties(OpenGLRenderer& renderer, T& handler) {
150113e0824d6bddf4376240681f9cf6a2deded9498John Reck#if DEBUG_DISPLAY_LIST
151b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    properties().debugOutputProperties(handler.level() + 1);
152113e0824d6bddf4376240681f9cf6a2deded9498John Reck#endif
153d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (properties().getLeft() != 0 || properties().getTop() != 0) {
154d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        renderer.translate(properties().getLeft(), properties().getTop());
155113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
156d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (properties().getStaticMatrix()) {
157d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        renderer.concatMatrix(properties().getStaticMatrix());
158d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    } else if (properties().getAnimationMatrix()) {
159d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        renderer.concatMatrix(properties().getAnimationMatrix());
160113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
161f7483e3af0513a1baa8341d403df2e0c0896a9ffJohn Reck    if (properties().hasTransformMatrix()) {
162f7483e3af0513a1baa8341d403df2e0c0896a9ffJohn Reck        if (properties().isTransformTranslateOnly()) {
163d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            renderer.translate(properties().getTranslationX(), properties().getTranslationY());
164113e0824d6bddf4376240681f9cf6a2deded9498John Reck        } else {
165d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            renderer.concatMatrix(*properties().getTransformMatrix());
166113e0824d6bddf4376240681f9cf6a2deded9498John Reck        }
167113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
168d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    bool clipToBoundsNeeded = properties().getCaching() ? false : properties().getClipToBounds();
169d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (properties().getAlpha() < 1) {
170d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        if (properties().getCaching()) {
171d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            renderer.setOverrideLayerAlpha(properties().getAlpha());
172d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        } else if (!properties().getHasOverlappingRendering()) {
173d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            renderer.scaleAlpha(properties().getAlpha());
174113e0824d6bddf4376240681f9cf6a2deded9498John Reck        } else {
175113e0824d6bddf4376240681f9cf6a2deded9498John Reck            // TODO: should be able to store the size of a DL at record time and not
176113e0824d6bddf4376240681f9cf6a2deded9498John Reck            // have to pass it into this call. In fact, this information might be in the
177113e0824d6bddf4376240681f9cf6a2deded9498John Reck            // location/size info that we store with the new native transform data.
178113e0824d6bddf4376240681f9cf6a2deded9498John Reck            int saveFlags = SkCanvas::kHasAlphaLayer_SaveFlag;
179113e0824d6bddf4376240681f9cf6a2deded9498John Reck            if (clipToBoundsNeeded) {
180113e0824d6bddf4376240681f9cf6a2deded9498John Reck                saveFlags |= SkCanvas::kClipToLayer_SaveFlag;
181113e0824d6bddf4376240681f9cf6a2deded9498John Reck                clipToBoundsNeeded = false; // clipping done by saveLayer
182113e0824d6bddf4376240681f9cf6a2deded9498John Reck            }
183113e0824d6bddf4376240681f9cf6a2deded9498John Reck
184113e0824d6bddf4376240681f9cf6a2deded9498John Reck            SaveLayerOp* op = new (handler.allocator()) SaveLayerOp(
1858c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik                    0, 0, properties().getWidth(), properties().getHeight(),
1868c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik                    properties().getAlpha() * 255, saveFlags);
187d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
188113e0824d6bddf4376240681f9cf6a2deded9498John Reck        }
189113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
190113e0824d6bddf4376240681f9cf6a2deded9498John Reck    if (clipToBoundsNeeded) {
1918c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik        ClipRectOp* op = new (handler.allocator()) ClipRectOp(
1928c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik                0, 0, properties().getWidth(), properties().getHeight(), SkRegion::kIntersect_Op);
193d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
194113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
1958c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik
1968c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik    if (CC_UNLIKELY(properties().hasClippingPath())) {
1978c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik        // TODO: optimize for round rect/circle clipping
1988c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik        const SkPath* path = properties().getClippingPath();
1998c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik        ClipPathOp* op = new (handler.allocator()) ClipPathOp(path, SkRegion::kIntersect_Op);
200d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
201113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
202113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
203113e0824d6bddf4376240681f9cf6a2deded9498John Reck
204113e0824d6bddf4376240681f9cf6a2deded9498John Reck/**
205113e0824d6bddf4376240681f9cf6a2deded9498John Reck * Apply property-based transformations to input matrix
206113e0824d6bddf4376240681f9cf6a2deded9498John Reck *
207113e0824d6bddf4376240681f9cf6a2deded9498John Reck * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
208113e0824d6bddf4376240681f9cf6a2deded9498John Reck * matrix computation instead of the Skia 3x3 matrix + camera hackery.
209113e0824d6bddf4376240681f9cf6a2deded9498John Reck */
210113e0824d6bddf4376240681f9cf6a2deded9498John Reckvoid RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) {
211d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (properties().getLeft() != 0 || properties().getTop() != 0) {
212d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        matrix.translate(properties().getLeft(), properties().getTop());
213113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
214d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (properties().getStaticMatrix()) {
215d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        mat4 stat(*properties().getStaticMatrix());
216113e0824d6bddf4376240681f9cf6a2deded9498John Reck        matrix.multiply(stat);
217d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    } else if (properties().getAnimationMatrix()) {
218d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        mat4 anim(*properties().getAnimationMatrix());
219113e0824d6bddf4376240681f9cf6a2deded9498John Reck        matrix.multiply(anim);
220113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
221e0bb87d4bdbd3b08ab6a8569c8e564ed59b8a5a7Chris Craik
222e0bb87d4bdbd3b08ab6a8569c8e564ed59b8a5a7Chris Craik    bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getTranslationZ());
223e0bb87d4bdbd3b08ab6a8569c8e564ed59b8a5a7Chris Craik    if (properties().hasTransformMatrix() || applyTranslationZ) {
224f7483e3af0513a1baa8341d403df2e0c0896a9ffJohn Reck        if (properties().isTransformTranslateOnly()) {
225d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
226d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                    true3dTransform ? properties().getTranslationZ() : 0.0f);
227113e0824d6bddf4376240681f9cf6a2deded9498John Reck        } else {
228113e0824d6bddf4376240681f9cf6a2deded9498John Reck            if (!true3dTransform) {
229d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                matrix.multiply(*properties().getTransformMatrix());
230113e0824d6bddf4376240681f9cf6a2deded9498John Reck            } else {
231113e0824d6bddf4376240681f9cf6a2deded9498John Reck                mat4 true3dMat;
232113e0824d6bddf4376240681f9cf6a2deded9498John Reck                true3dMat.loadTranslate(
233d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                        properties().getPivotX() + properties().getTranslationX(),
234d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                        properties().getPivotY() + properties().getTranslationY(),
235d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                        properties().getTranslationZ());
236d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
237d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
238d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                true3dMat.rotate(properties().getRotation(), 0, 0, 1);
239d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
240d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
241113e0824d6bddf4376240681f9cf6a2deded9498John Reck
242113e0824d6bddf4376240681f9cf6a2deded9498John Reck                matrix.multiply(true3dMat);
243113e0824d6bddf4376240681f9cf6a2deded9498John Reck            }
244113e0824d6bddf4376240681f9cf6a2deded9498John Reck        }
245113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
246113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
247113e0824d6bddf4376240681f9cf6a2deded9498John Reck
248113e0824d6bddf4376240681f9cf6a2deded9498John Reck/**
249113e0824d6bddf4376240681f9cf6a2deded9498John Reck * Organizes the DisplayList hierarchy to prepare for background projection reordering.
250113e0824d6bddf4376240681f9cf6a2deded9498John Reck *
251113e0824d6bddf4376240681f9cf6a2deded9498John Reck * This should be called before a call to defer() or drawDisplayList()
252113e0824d6bddf4376240681f9cf6a2deded9498John Reck *
253113e0824d6bddf4376240681f9cf6a2deded9498John Reck * Each DisplayList that serves as a 3d root builds its list of composited children,
254113e0824d6bddf4376240681f9cf6a2deded9498John Reck * which are flagged to not draw in the standard draw loop.
255113e0824d6bddf4376240681f9cf6a2deded9498John Reck */
256113e0824d6bddf4376240681f9cf6a2deded9498John Reckvoid RenderNode::computeOrdering() {
257113e0824d6bddf4376240681f9cf6a2deded9498John Reck    ATRACE_CALL();
258113e0824d6bddf4376240681f9cf6a2deded9498John Reck    mProjectedNodes.clear();
259113e0824d6bddf4376240681f9cf6a2deded9498John Reck
260113e0824d6bddf4376240681f9cf6a2deded9498John Reck    // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that
261113e0824d6bddf4376240681f9cf6a2deded9498John Reck    // transform properties are applied correctly to top level children
262113e0824d6bddf4376240681f9cf6a2deded9498John Reck    if (mDisplayListData == NULL) return;
263087bc0c14bdccf7c258dce0cdef46a69a839b427John Reck    for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
264087bc0c14bdccf7c258dce0cdef46a69a839b427John Reck        DrawDisplayListOp* childOp = mDisplayListData->children()[i];
265113e0824d6bddf4376240681f9cf6a2deded9498John Reck        childOp->mDisplayList->computeOrderingImpl(childOp,
266113e0824d6bddf4376240681f9cf6a2deded9498John Reck                &mProjectedNodes, &mat4::identity());
267113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
268113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
269113e0824d6bddf4376240681f9cf6a2deded9498John Reck
270113e0824d6bddf4376240681f9cf6a2deded9498John Reckvoid RenderNode::computeOrderingImpl(
271113e0824d6bddf4376240681f9cf6a2deded9498John Reck        DrawDisplayListOp* opState,
272113e0824d6bddf4376240681f9cf6a2deded9498John Reck        Vector<DrawDisplayListOp*>* compositedChildrenOfProjectionSurface,
273113e0824d6bddf4376240681f9cf6a2deded9498John Reck        const mat4* transformFromProjectionSurface) {
274113e0824d6bddf4376240681f9cf6a2deded9498John Reck    mProjectedNodes.clear();
275113e0824d6bddf4376240681f9cf6a2deded9498John Reck    if (mDisplayListData == NULL || mDisplayListData->isEmpty()) return;
276113e0824d6bddf4376240681f9cf6a2deded9498John Reck
277113e0824d6bddf4376240681f9cf6a2deded9498John Reck    // TODO: should avoid this calculation in most cases
278113e0824d6bddf4376240681f9cf6a2deded9498John Reck    // TODO: just calculate single matrix, down to all leaf composited elements
279113e0824d6bddf4376240681f9cf6a2deded9498John Reck    Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface);
280113e0824d6bddf4376240681f9cf6a2deded9498John Reck    localTransformFromProjectionSurface.multiply(opState->mTransformFromParent);
281113e0824d6bddf4376240681f9cf6a2deded9498John Reck
282d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (properties().getProjectBackwards()) {
283113e0824d6bddf4376240681f9cf6a2deded9498John Reck        // composited projectee, flag for out of order draw, save matrix, and store in proj surface
284113e0824d6bddf4376240681f9cf6a2deded9498John Reck        opState->mSkipInOrderDraw = true;
285113e0824d6bddf4376240681f9cf6a2deded9498John Reck        opState->mTransformFromCompositingAncestor.load(localTransformFromProjectionSurface);
286113e0824d6bddf4376240681f9cf6a2deded9498John Reck        compositedChildrenOfProjectionSurface->add(opState);
287113e0824d6bddf4376240681f9cf6a2deded9498John Reck    } else {
288113e0824d6bddf4376240681f9cf6a2deded9498John Reck        // standard in order draw
289113e0824d6bddf4376240681f9cf6a2deded9498John Reck        opState->mSkipInOrderDraw = false;
290113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
291113e0824d6bddf4376240681f9cf6a2deded9498John Reck
292087bc0c14bdccf7c258dce0cdef46a69a839b427John Reck    if (mDisplayListData->children().size() > 0) {
293113e0824d6bddf4376240681f9cf6a2deded9498John Reck        const bool isProjectionReceiver = mDisplayListData->projectionReceiveIndex >= 0;
294113e0824d6bddf4376240681f9cf6a2deded9498John Reck        bool haveAppliedPropertiesToProjection = false;
295087bc0c14bdccf7c258dce0cdef46a69a839b427John Reck        for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
296087bc0c14bdccf7c258dce0cdef46a69a839b427John Reck            DrawDisplayListOp* childOp = mDisplayListData->children()[i];
297113e0824d6bddf4376240681f9cf6a2deded9498John Reck            RenderNode* child = childOp->mDisplayList;
298113e0824d6bddf4376240681f9cf6a2deded9498John Reck
299113e0824d6bddf4376240681f9cf6a2deded9498John Reck            Vector<DrawDisplayListOp*>* projectionChildren = NULL;
300113e0824d6bddf4376240681f9cf6a2deded9498John Reck            const mat4* projectionTransform = NULL;
301d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            if (isProjectionReceiver && !child->properties().getProjectBackwards()) {
302113e0824d6bddf4376240681f9cf6a2deded9498John Reck                // if receiving projections, collect projecting descendent
303113e0824d6bddf4376240681f9cf6a2deded9498John Reck
304113e0824d6bddf4376240681f9cf6a2deded9498John Reck                // Note that if a direct descendent is projecting backwards, we pass it's
305113e0824d6bddf4376240681f9cf6a2deded9498John Reck                // grandparent projection collection, since it shouldn't project onto it's
306113e0824d6bddf4376240681f9cf6a2deded9498John Reck                // parent, where it will already be drawing.
307113e0824d6bddf4376240681f9cf6a2deded9498John Reck                projectionChildren = &mProjectedNodes;
308113e0824d6bddf4376240681f9cf6a2deded9498John Reck                projectionTransform = &mat4::identity();
309113e0824d6bddf4376240681f9cf6a2deded9498John Reck            } else {
310113e0824d6bddf4376240681f9cf6a2deded9498John Reck                if (!haveAppliedPropertiesToProjection) {
311113e0824d6bddf4376240681f9cf6a2deded9498John Reck                    applyViewPropertyTransforms(localTransformFromProjectionSurface);
312113e0824d6bddf4376240681f9cf6a2deded9498John Reck                    haveAppliedPropertiesToProjection = true;
313113e0824d6bddf4376240681f9cf6a2deded9498John Reck                }
314113e0824d6bddf4376240681f9cf6a2deded9498John Reck                projectionChildren = compositedChildrenOfProjectionSurface;
315113e0824d6bddf4376240681f9cf6a2deded9498John Reck                projectionTransform = &localTransformFromProjectionSurface;
316113e0824d6bddf4376240681f9cf6a2deded9498John Reck            }
317113e0824d6bddf4376240681f9cf6a2deded9498John Reck            child->computeOrderingImpl(childOp, projectionChildren, projectionTransform);
318113e0824d6bddf4376240681f9cf6a2deded9498John Reck        }
319113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
320113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
321113e0824d6bddf4376240681f9cf6a2deded9498John Reck
322113e0824d6bddf4376240681f9cf6a2deded9498John Reckclass DeferOperationHandler {
323113e0824d6bddf4376240681f9cf6a2deded9498John Reckpublic:
324113e0824d6bddf4376240681f9cf6a2deded9498John Reck    DeferOperationHandler(DeferStateStruct& deferStruct, int level)
325113e0824d6bddf4376240681f9cf6a2deded9498John Reck        : mDeferStruct(deferStruct), mLevel(level) {}
326113e0824d6bddf4376240681f9cf6a2deded9498John Reck    inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
327113e0824d6bddf4376240681f9cf6a2deded9498John Reck        operation->defer(mDeferStruct, saveCount, mLevel, clipToBounds);
328113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
329113e0824d6bddf4376240681f9cf6a2deded9498John Reck    inline LinearAllocator& allocator() { return *(mDeferStruct.mAllocator); }
330b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    inline void startMark(const char* name) {} // do nothing
331b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    inline void endMark() {}
332b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    inline int level() { return mLevel; }
333b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    inline int replayFlags() { return mDeferStruct.mReplayFlags; }
334113e0824d6bddf4376240681f9cf6a2deded9498John Reck
335113e0824d6bddf4376240681f9cf6a2deded9498John Reckprivate:
336113e0824d6bddf4376240681f9cf6a2deded9498John Reck    DeferStateStruct& mDeferStruct;
337113e0824d6bddf4376240681f9cf6a2deded9498John Reck    const int mLevel;
338113e0824d6bddf4376240681f9cf6a2deded9498John Reck};
339113e0824d6bddf4376240681f9cf6a2deded9498John Reck
340b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craikvoid RenderNode::deferNodeTree(DeferStateStruct& deferStruct) {
341b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    DeferOperationHandler handler(deferStruct, 0);
342b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    if (properties().getTranslationZ() > 0.0f) issueDrawShadowOperation(Matrix4::identity(), handler);
343b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler);
344b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik}
345b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik
346b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craikvoid RenderNode::deferNodeInParent(DeferStateStruct& deferStruct, const int level) {
347113e0824d6bddf4376240681f9cf6a2deded9498John Reck    DeferOperationHandler handler(deferStruct, level);
348b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler);
349113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
350113e0824d6bddf4376240681f9cf6a2deded9498John Reck
351113e0824d6bddf4376240681f9cf6a2deded9498John Reckclass ReplayOperationHandler {
352113e0824d6bddf4376240681f9cf6a2deded9498John Reckpublic:
353113e0824d6bddf4376240681f9cf6a2deded9498John Reck    ReplayOperationHandler(ReplayStateStruct& replayStruct, int level)
354113e0824d6bddf4376240681f9cf6a2deded9498John Reck        : mReplayStruct(replayStruct), mLevel(level) {}
355113e0824d6bddf4376240681f9cf6a2deded9498John Reck    inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
356113e0824d6bddf4376240681f9cf6a2deded9498John Reck#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
357d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        properties().getReplayStruct().mRenderer.eventMark(operation->name());
358113e0824d6bddf4376240681f9cf6a2deded9498John Reck#endif
359113e0824d6bddf4376240681f9cf6a2deded9498John Reck        operation->replay(mReplayStruct, saveCount, mLevel, clipToBounds);
360113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
361113e0824d6bddf4376240681f9cf6a2deded9498John Reck    inline LinearAllocator& allocator() { return *(mReplayStruct.mAllocator); }
362b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    inline void startMark(const char* name) {
363b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik        mReplayStruct.mRenderer.startMark(name);
364b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    }
365b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    inline void endMark() {
366b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik        mReplayStruct.mRenderer.endMark();
367b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik        DISPLAY_LIST_LOGD("%*sDone (%p, %s), returning %d", level * 2, "", this, mName.string(),
368b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik                mReplayStruct.mDrawGlStatus);
369b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    }
370b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    inline int level() { return mLevel; }
371b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    inline int replayFlags() { return mReplayStruct.mReplayFlags; }
372113e0824d6bddf4376240681f9cf6a2deded9498John Reck
373113e0824d6bddf4376240681f9cf6a2deded9498John Reckprivate:
374113e0824d6bddf4376240681f9cf6a2deded9498John Reck    ReplayStateStruct& mReplayStruct;
375113e0824d6bddf4376240681f9cf6a2deded9498John Reck    const int mLevel;
376113e0824d6bddf4376240681f9cf6a2deded9498John Reck};
377113e0824d6bddf4376240681f9cf6a2deded9498John Reck
378b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craikvoid RenderNode::replayNodeTree(ReplayStateStruct& replayStruct) {
379b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    ReplayOperationHandler handler(replayStruct, 0);
380b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    if (properties().getTranslationZ() > 0.0f) issueDrawShadowOperation(Matrix4::identity(), handler);
381b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler);
382b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik}
383113e0824d6bddf4376240681f9cf6a2deded9498John Reck
384b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craikvoid RenderNode::replayNodeInParent(ReplayStateStruct& replayStruct, const int level) {
385b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    ReplayOperationHandler handler(replayStruct, level);
386b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler);
387113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
388113e0824d6bddf4376240681f9cf6a2deded9498John Reck
389113e0824d6bddf4376240681f9cf6a2deded9498John Reckvoid RenderNode::buildZSortedChildList(Vector<ZDrawDisplayListOpPair>& zTranslatedNodes) {
390087bc0c14bdccf7c258dce0cdef46a69a839b427John Reck    if (mDisplayListData == NULL || mDisplayListData->children().size() == 0) return;
391113e0824d6bddf4376240681f9cf6a2deded9498John Reck
392087bc0c14bdccf7c258dce0cdef46a69a839b427John Reck    for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
393087bc0c14bdccf7c258dce0cdef46a69a839b427John Reck        DrawDisplayListOp* childOp = mDisplayListData->children()[i];
394113e0824d6bddf4376240681f9cf6a2deded9498John Reck        RenderNode* child = childOp->mDisplayList;
395d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        float childZ = child->properties().getTranslationZ();
396113e0824d6bddf4376240681f9cf6a2deded9498John Reck
397e0bb87d4bdbd3b08ab6a8569c8e564ed59b8a5a7Chris Craik        if (!MathUtils::isZero(childZ)) {
398113e0824d6bddf4376240681f9cf6a2deded9498John Reck            zTranslatedNodes.add(ZDrawDisplayListOpPair(childZ, childOp));
399113e0824d6bddf4376240681f9cf6a2deded9498John Reck            childOp->mSkipInOrderDraw = true;
400d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        } else if (!child->properties().getProjectBackwards()) {
401113e0824d6bddf4376240681f9cf6a2deded9498John Reck            // regular, in order drawing DisplayList
402113e0824d6bddf4376240681f9cf6a2deded9498John Reck            childOp->mSkipInOrderDraw = false;
403113e0824d6bddf4376240681f9cf6a2deded9498John Reck        }
404113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
405113e0824d6bddf4376240681f9cf6a2deded9498John Reck
406113e0824d6bddf4376240681f9cf6a2deded9498John Reck    // Z sort 3d children (stable-ness makes z compare fall back to standard drawing order)
407113e0824d6bddf4376240681f9cf6a2deded9498John Reck    std::stable_sort(zTranslatedNodes.begin(), zTranslatedNodes.end());
408113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
409113e0824d6bddf4376240681f9cf6a2deded9498John Reck
410b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craiktemplate <class T>
411b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craikvoid RenderNode::issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler) {
412b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    if (properties().getAlpha() <= 0.0f) return;
413b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik
414b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    mat4 shadowMatrixXY(transformFromParent);
415b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    applyViewPropertyTransforms(shadowMatrixXY);
416b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik
417b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    // Z matrix needs actual 3d transformation, so mapped z values will be correct
418b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    mat4 shadowMatrixZ(transformFromParent);
419b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    applyViewPropertyTransforms(shadowMatrixZ, true);
420b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik
421b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    const SkPath* outlinePath = properties().getOutline().getPath();
422b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    const RevealClip& revealClip = properties().getRevealClip();
423b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    const SkPath* revealClipPath = revealClip.hasConvexClip()
424b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik            ?  revealClip.getPath() : NULL; // only pass the reveal clip's path if it's convex
425b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik
426b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    /**
427b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik     * The drawing area of the caster is always the same as the its perimeter (which
428b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik     * the shadow system uses) *except* in the inverse clip case. Inform the shadow
429b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik     * system that the caster's drawing area (as opposed to its perimeter) has been
430b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik     * clipped, so that it knows the caster can't be opaque.
431b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik     */
432b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    bool casterUnclipped = !revealClip.willClip() || revealClip.hasConvexClip();
433b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik
434b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    DisplayListOp* shadowOp  = new (handler.allocator()) DrawShadowOp(
435b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik            shadowMatrixXY, shadowMatrixZ,
436b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik            properties().getAlpha(), casterUnclipped,
437b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik            properties().getWidth(), properties().getHeight(),
438b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik            outlinePath, revealClipPath);
439b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    handler(shadowOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
440b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik}
441b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik
442113e0824d6bddf4376240681f9cf6a2deded9498John Reck#define SHADOW_DELTA 0.1f
443113e0824d6bddf4376240681f9cf6a2deded9498John Reck
444113e0824d6bddf4376240681f9cf6a2deded9498John Recktemplate <class T>
445b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craikvoid RenderNode::issueOperationsOf3dChildren(const Vector<ZDrawDisplayListOpPair>& zTranslatedNodes,
446113e0824d6bddf4376240681f9cf6a2deded9498John Reck        ChildrenSelectMode mode, OpenGLRenderer& renderer, T& handler) {
447113e0824d6bddf4376240681f9cf6a2deded9498John Reck    const int size = zTranslatedNodes.size();
448113e0824d6bddf4376240681f9cf6a2deded9498John Reck    if (size == 0
449113e0824d6bddf4376240681f9cf6a2deded9498John Reck            || (mode == kNegativeZChildren && zTranslatedNodes[0].key > 0.0f)
450113e0824d6bddf4376240681f9cf6a2deded9498John Reck            || (mode == kPositiveZChildren && zTranslatedNodes[size - 1].key < 0.0f)) {
451113e0824d6bddf4376240681f9cf6a2deded9498John Reck        // no 3d children to draw
452113e0824d6bddf4376240681f9cf6a2deded9498John Reck        return;
453113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
454113e0824d6bddf4376240681f9cf6a2deded9498John Reck
455113e0824d6bddf4376240681f9cf6a2deded9498John Reck    /**
456113e0824d6bddf4376240681f9cf6a2deded9498John Reck     * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
457113e0824d6bddf4376240681f9cf6a2deded9498John Reck     * with very similar Z heights to draw together.
458113e0824d6bddf4376240681f9cf6a2deded9498John Reck     *
459113e0824d6bddf4376240681f9cf6a2deded9498John Reck     * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
460113e0824d6bddf4376240681f9cf6a2deded9498John Reck     * underneath both, and neither's shadow is drawn on top of the other.
461113e0824d6bddf4376240681f9cf6a2deded9498John Reck     */
462113e0824d6bddf4376240681f9cf6a2deded9498John Reck    const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
463113e0824d6bddf4376240681f9cf6a2deded9498John Reck    size_t drawIndex, shadowIndex, endIndex;
464113e0824d6bddf4376240681f9cf6a2deded9498John Reck    if (mode == kNegativeZChildren) {
465113e0824d6bddf4376240681f9cf6a2deded9498John Reck        drawIndex = 0;
466113e0824d6bddf4376240681f9cf6a2deded9498John Reck        endIndex = nonNegativeIndex;
467113e0824d6bddf4376240681f9cf6a2deded9498John Reck        shadowIndex = endIndex; // draw no shadows
468113e0824d6bddf4376240681f9cf6a2deded9498John Reck    } else {
469113e0824d6bddf4376240681f9cf6a2deded9498John Reck        drawIndex = nonNegativeIndex;
470113e0824d6bddf4376240681f9cf6a2deded9498John Reck        endIndex = size;
471113e0824d6bddf4376240681f9cf6a2deded9498John Reck        shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
472113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
473113e0824d6bddf4376240681f9cf6a2deded9498John Reck    float lastCasterZ = 0.0f;
474113e0824d6bddf4376240681f9cf6a2deded9498John Reck    while (shadowIndex < endIndex || drawIndex < endIndex) {
475113e0824d6bddf4376240681f9cf6a2deded9498John Reck        if (shadowIndex < endIndex) {
476113e0824d6bddf4376240681f9cf6a2deded9498John Reck            DrawDisplayListOp* casterOp = zTranslatedNodes[shadowIndex].value;
477113e0824d6bddf4376240681f9cf6a2deded9498John Reck            RenderNode* caster = casterOp->mDisplayList;
478113e0824d6bddf4376240681f9cf6a2deded9498John Reck            const float casterZ = zTranslatedNodes[shadowIndex].key;
479113e0824d6bddf4376240681f9cf6a2deded9498John Reck            // attempt to render the shadow if the caster about to be drawn is its caster,
480113e0824d6bddf4376240681f9cf6a2deded9498John Reck            // OR if its caster's Z value is similar to the previous potential caster
481113e0824d6bddf4376240681f9cf6a2deded9498John Reck            if (shadowIndex == drawIndex || casterZ - lastCasterZ < SHADOW_DELTA) {
482b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik                caster->issueDrawShadowOperation(casterOp->mTransformFromParent, handler);
483113e0824d6bddf4376240681f9cf6a2deded9498John Reck
484113e0824d6bddf4376240681f9cf6a2deded9498John Reck                lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
485113e0824d6bddf4376240681f9cf6a2deded9498John Reck                shadowIndex++;
486113e0824d6bddf4376240681f9cf6a2deded9498John Reck                continue;
487113e0824d6bddf4376240681f9cf6a2deded9498John Reck            }
488113e0824d6bddf4376240681f9cf6a2deded9498John Reck        }
489113e0824d6bddf4376240681f9cf6a2deded9498John Reck
490113e0824d6bddf4376240681f9cf6a2deded9498John Reck        // only the actual child DL draw needs to be in save/restore,
491113e0824d6bddf4376240681f9cf6a2deded9498John Reck        // since it modifies the renderer's matrix
492113e0824d6bddf4376240681f9cf6a2deded9498John Reck        int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
493113e0824d6bddf4376240681f9cf6a2deded9498John Reck
494113e0824d6bddf4376240681f9cf6a2deded9498John Reck        DrawDisplayListOp* childOp = zTranslatedNodes[drawIndex].value;
495113e0824d6bddf4376240681f9cf6a2deded9498John Reck        RenderNode* child = childOp->mDisplayList;
496113e0824d6bddf4376240681f9cf6a2deded9498John Reck
497113e0824d6bddf4376240681f9cf6a2deded9498John Reck        renderer.concatMatrix(childOp->mTransformFromParent);
498113e0824d6bddf4376240681f9cf6a2deded9498John Reck        childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
499d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
500113e0824d6bddf4376240681f9cf6a2deded9498John Reck        childOp->mSkipInOrderDraw = true;
501113e0824d6bddf4376240681f9cf6a2deded9498John Reck
502113e0824d6bddf4376240681f9cf6a2deded9498John Reck        renderer.restoreToCount(restoreTo);
503113e0824d6bddf4376240681f9cf6a2deded9498John Reck        drawIndex++;
504113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
505113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
506113e0824d6bddf4376240681f9cf6a2deded9498John Reck
507113e0824d6bddf4376240681f9cf6a2deded9498John Recktemplate <class T>
508b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craikvoid RenderNode::issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler) {
509113e0824d6bddf4376240681f9cf6a2deded9498John Reck    for (size_t i = 0; i < mProjectedNodes.size(); i++) {
510113e0824d6bddf4376240681f9cf6a2deded9498John Reck        DrawDisplayListOp* childOp = mProjectedNodes[i];
511113e0824d6bddf4376240681f9cf6a2deded9498John Reck
512113e0824d6bddf4376240681f9cf6a2deded9498John Reck        // matrix save, concat, and restore can be done safely without allocating operations
513113e0824d6bddf4376240681f9cf6a2deded9498John Reck        int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
514113e0824d6bddf4376240681f9cf6a2deded9498John Reck        renderer.concatMatrix(childOp->mTransformFromCompositingAncestor);
515113e0824d6bddf4376240681f9cf6a2deded9498John Reck        childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
516d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
517113e0824d6bddf4376240681f9cf6a2deded9498John Reck        childOp->mSkipInOrderDraw = true;
518113e0824d6bddf4376240681f9cf6a2deded9498John Reck        renderer.restoreToCount(restoreTo);
519113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
520113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
521113e0824d6bddf4376240681f9cf6a2deded9498John Reck
522113e0824d6bddf4376240681f9cf6a2deded9498John Reck/**
523113e0824d6bddf4376240681f9cf6a2deded9498John Reck * This function serves both defer and replay modes, and will organize the displayList's component
524113e0824d6bddf4376240681f9cf6a2deded9498John Reck * operations for a single frame:
525113e0824d6bddf4376240681f9cf6a2deded9498John Reck *
526113e0824d6bddf4376240681f9cf6a2deded9498John Reck * Every 'simple' state operation that affects just the matrix and alpha (or other factors of
527113e0824d6bddf4376240681f9cf6a2deded9498John Reck * DeferredDisplayState) may be issued directly to the renderer, but complex operations (with custom
528113e0824d6bddf4376240681f9cf6a2deded9498John Reck * defer logic) and operations in displayListOps are issued through the 'handler' which handles the
529113e0824d6bddf4376240681f9cf6a2deded9498John Reck * defer vs replay logic, per operation
530113e0824d6bddf4376240681f9cf6a2deded9498John Reck */
531113e0824d6bddf4376240681f9cf6a2deded9498John Recktemplate <class T>
532b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craikvoid RenderNode::issueOperations(OpenGLRenderer& renderer, T& handler) {
533b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    const int level = handler.level();
534d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (mDisplayListData->isEmpty() || properties().getAlpha() <= 0) {
535113e0824d6bddf4376240681f9cf6a2deded9498John Reck        DISPLAY_LIST_LOGD("%*sEmpty display list (%p, %s)", level * 2, "", this, mName.string());
536113e0824d6bddf4376240681f9cf6a2deded9498John Reck        return;
537113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
538113e0824d6bddf4376240681f9cf6a2deded9498John Reck
539b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    handler.startMark(mName.string());
540b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik
541113e0824d6bddf4376240681f9cf6a2deded9498John Reck#if DEBUG_DISPLAY_LIST
542113e0824d6bddf4376240681f9cf6a2deded9498John Reck    Rect* clipRect = renderer.getClipRect();
543113e0824d6bddf4376240681f9cf6a2deded9498John Reck    DISPLAY_LIST_LOGD("%*sStart display list (%p, %s), clipRect: %.0f, %.0f, %.0f, %.0f",
544113e0824d6bddf4376240681f9cf6a2deded9498John Reck            level * 2, "", this, mName.string(), clipRect->left, clipRect->top,
545113e0824d6bddf4376240681f9cf6a2deded9498John Reck            clipRect->right, clipRect->bottom);
546113e0824d6bddf4376240681f9cf6a2deded9498John Reck#endif
547113e0824d6bddf4376240681f9cf6a2deded9498John Reck
548113e0824d6bddf4376240681f9cf6a2deded9498John Reck    LinearAllocator& alloc = handler.allocator();
549113e0824d6bddf4376240681f9cf6a2deded9498John Reck    int restoreTo = renderer.getSaveCount();
550113e0824d6bddf4376240681f9cf6a2deded9498John Reck    handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
551d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            PROPERTY_SAVECOUNT, properties().getClipToBounds());
552113e0824d6bddf4376240681f9cf6a2deded9498John Reck
553113e0824d6bddf4376240681f9cf6a2deded9498John Reck    DISPLAY_LIST_LOGD("%*sSave %d %d", (level + 1) * 2, "",
554113e0824d6bddf4376240681f9cf6a2deded9498John Reck            SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag, restoreTo);
555113e0824d6bddf4376240681f9cf6a2deded9498John Reck
556b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    setViewProperties<T>(renderer, handler);
557113e0824d6bddf4376240681f9cf6a2deded9498John Reck
5588c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik    bool quickRejected = properties().getClipToBounds()
5598c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik            && renderer.quickRejectConservative(0, 0, properties().getWidth(), properties().getHeight());
560113e0824d6bddf4376240681f9cf6a2deded9498John Reck    if (!quickRejected) {
561113e0824d6bddf4376240681f9cf6a2deded9498John Reck        Vector<ZDrawDisplayListOpPair> zTranslatedNodes;
562113e0824d6bddf4376240681f9cf6a2deded9498John Reck        buildZSortedChildList(zTranslatedNodes);
563113e0824d6bddf4376240681f9cf6a2deded9498John Reck
564113e0824d6bddf4376240681f9cf6a2deded9498John Reck        // for 3d root, draw children with negative z values
565b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik        issueOperationsOf3dChildren(zTranslatedNodes, kNegativeZChildren, renderer, handler);
566113e0824d6bddf4376240681f9cf6a2deded9498John Reck
567113e0824d6bddf4376240681f9cf6a2deded9498John Reck        DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance();
568113e0824d6bddf4376240681f9cf6a2deded9498John Reck        const int saveCountOffset = renderer.getSaveCount() - 1;
569113e0824d6bddf4376240681f9cf6a2deded9498John Reck        const int projectionReceiveIndex = mDisplayListData->projectionReceiveIndex;
570113e0824d6bddf4376240681f9cf6a2deded9498John Reck        for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) {
571113e0824d6bddf4376240681f9cf6a2deded9498John Reck            DisplayListOp *op = mDisplayListData->displayListOps[i];
572113e0824d6bddf4376240681f9cf6a2deded9498John Reck
573113e0824d6bddf4376240681f9cf6a2deded9498John Reck#if DEBUG_DISPLAY_LIST
574113e0824d6bddf4376240681f9cf6a2deded9498John Reck            op->output(level + 1);
575113e0824d6bddf4376240681f9cf6a2deded9498John Reck#endif
576113e0824d6bddf4376240681f9cf6a2deded9498John Reck            logBuffer.writeCommand(level, op->name());
577d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            handler(op, saveCountOffset, properties().getClipToBounds());
578113e0824d6bddf4376240681f9cf6a2deded9498John Reck
579113e0824d6bddf4376240681f9cf6a2deded9498John Reck            if (CC_UNLIKELY(i == projectionReceiveIndex && mProjectedNodes.size() > 0)) {
580b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik                issueOperationsOfProjectedChildren(renderer, handler);
581113e0824d6bddf4376240681f9cf6a2deded9498John Reck            }
582113e0824d6bddf4376240681f9cf6a2deded9498John Reck        }
583113e0824d6bddf4376240681f9cf6a2deded9498John Reck
584113e0824d6bddf4376240681f9cf6a2deded9498John Reck        // for 3d root, draw children with positive z values
585b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik        issueOperationsOf3dChildren(zTranslatedNodes, kPositiveZChildren, renderer, handler);
586113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
587113e0824d6bddf4376240681f9cf6a2deded9498John Reck
588113e0824d6bddf4376240681f9cf6a2deded9498John Reck    DISPLAY_LIST_LOGD("%*sRestoreToCount %d", (level + 1) * 2, "", restoreTo);
589113e0824d6bddf4376240681f9cf6a2deded9498John Reck    handler(new (alloc) RestoreToCountOp(restoreTo),
590d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            PROPERTY_SAVECOUNT, properties().getClipToBounds());
591113e0824d6bddf4376240681f9cf6a2deded9498John Reck    renderer.setOverrideLayerAlpha(1.0f);
592b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik
593b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    handler.endMark();
594113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
595113e0824d6bddf4376240681f9cf6a2deded9498John Reck
596113e0824d6bddf4376240681f9cf6a2deded9498John Reck} /* namespace uirenderer */
597113e0824d6bddf4376240681f9cf6a2deded9498John Reck} /* namespace android */
598