RenderNode.cpp revision 8c271ca63b62061fd22cfee78fd6a574b44476fd
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"
29113e0824d6bddf4376240681f9cf6a2deded9498John Reck
30113e0824d6bddf4376240681f9cf6a2deded9498John Recknamespace android {
31113e0824d6bddf4376240681f9cf6a2deded9498John Recknamespace uirenderer {
32113e0824d6bddf4376240681f9cf6a2deded9498John Reck
33113e0824d6bddf4376240681f9cf6a2deded9498John Reckvoid RenderNode::outputLogBuffer(int fd) {
34113e0824d6bddf4376240681f9cf6a2deded9498John Reck    DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance();
35113e0824d6bddf4376240681f9cf6a2deded9498John Reck    if (logBuffer.isEmpty()) {
36113e0824d6bddf4376240681f9cf6a2deded9498John Reck        return;
37113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
38113e0824d6bddf4376240681f9cf6a2deded9498John Reck
39113e0824d6bddf4376240681f9cf6a2deded9498John Reck    FILE *file = fdopen(fd, "a");
40113e0824d6bddf4376240681f9cf6a2deded9498John Reck
41113e0824d6bddf4376240681f9cf6a2deded9498John Reck    fprintf(file, "\nRecent DisplayList operations\n");
42113e0824d6bddf4376240681f9cf6a2deded9498John Reck    logBuffer.outputCommands(file);
43113e0824d6bddf4376240681f9cf6a2deded9498John Reck
44113e0824d6bddf4376240681f9cf6a2deded9498John Reck    String8 cachesLog;
45113e0824d6bddf4376240681f9cf6a2deded9498John Reck    Caches::getInstance().dumpMemoryUsage(cachesLog);
46113e0824d6bddf4376240681f9cf6a2deded9498John Reck    fprintf(file, "\nCaches:\n%s", cachesLog.string());
47113e0824d6bddf4376240681f9cf6a2deded9498John Reck    fprintf(file, "\n");
48113e0824d6bddf4376240681f9cf6a2deded9498John Reck
49113e0824d6bddf4376240681f9cf6a2deded9498John Reck    fflush(file);
50113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
51113e0824d6bddf4376240681f9cf6a2deded9498John Reck
52d0a0b2a3140bfb1819a116413ce9d81886697a07John ReckRenderNode::RenderNode() : mDestroyed(false), mNeedsPropertiesSync(false), mDisplayListData(0) {
53113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
54113e0824d6bddf4376240681f9cf6a2deded9498John Reck
55113e0824d6bddf4376240681f9cf6a2deded9498John ReckRenderNode::~RenderNode() {
56113e0824d6bddf4376240681f9cf6a2deded9498John Reck    LOG_ALWAYS_FATAL_IF(mDestroyed, "Double destroyed DisplayList %p", this);
57113e0824d6bddf4376240681f9cf6a2deded9498John Reck
58113e0824d6bddf4376240681f9cf6a2deded9498John Reck    mDestroyed = true;
59113e0824d6bddf4376240681f9cf6a2deded9498John Reck    delete mDisplayListData;
60113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
61113e0824d6bddf4376240681f9cf6a2deded9498John Reck
62113e0824d6bddf4376240681f9cf6a2deded9498John Reckvoid RenderNode::destroyDisplayListDeferred(RenderNode* displayList) {
63113e0824d6bddf4376240681f9cf6a2deded9498John Reck    if (displayList) {
64113e0824d6bddf4376240681f9cf6a2deded9498John Reck        DISPLAY_LIST_LOGD("Deferring display list destruction");
65113e0824d6bddf4376240681f9cf6a2deded9498John Reck        Caches::getInstance().deleteDisplayListDeferred(displayList);
66113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
67113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
68113e0824d6bddf4376240681f9cf6a2deded9498John Reck
69113e0824d6bddf4376240681f9cf6a2deded9498John Reckvoid RenderNode::setData(DisplayListData* data) {
70113e0824d6bddf4376240681f9cf6a2deded9498John Reck    delete mDisplayListData;
71113e0824d6bddf4376240681f9cf6a2deded9498John Reck    mDisplayListData = data;
72113e0824d6bddf4376240681f9cf6a2deded9498John Reck    if (mDisplayListData) {
73113e0824d6bddf4376240681f9cf6a2deded9498John Reck        Caches::getInstance().registerFunctors(mDisplayListData->functorCount);
74113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
75113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
76113e0824d6bddf4376240681f9cf6a2deded9498John Reck
77113e0824d6bddf4376240681f9cf6a2deded9498John Reck/**
78113e0824d6bddf4376240681f9cf6a2deded9498John Reck * This function is a simplified version of replay(), where we simply retrieve and log the
79113e0824d6bddf4376240681f9cf6a2deded9498John Reck * display list. This function should remain in sync with the replay() function.
80113e0824d6bddf4376240681f9cf6a2deded9498John Reck */
81113e0824d6bddf4376240681f9cf6a2deded9498John Reckvoid RenderNode::output(uint32_t level) {
82113e0824d6bddf4376240681f9cf6a2deded9498John Reck    ALOGD("%*sStart display list (%p, %s, render=%d)", (level - 1) * 2, "", this,
83113e0824d6bddf4376240681f9cf6a2deded9498John Reck            mName.string(), isRenderable());
84113e0824d6bddf4376240681f9cf6a2deded9498John Reck    ALOGD("%*s%s %d", level * 2, "", "Save",
85113e0824d6bddf4376240681f9cf6a2deded9498John Reck            SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
86113e0824d6bddf4376240681f9cf6a2deded9498John Reck
87d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    properties().debugOutputProperties(level);
88113e0824d6bddf4376240681f9cf6a2deded9498John Reck    int flags = DisplayListOp::kOpLogFlag_Recurse;
89113e0824d6bddf4376240681f9cf6a2deded9498John Reck    for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) {
90113e0824d6bddf4376240681f9cf6a2deded9498John Reck        mDisplayListData->displayListOps[i]->output(level, flags);
91113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
92113e0824d6bddf4376240681f9cf6a2deded9498John Reck
93113e0824d6bddf4376240681f9cf6a2deded9498John Reck    ALOGD("%*sDone (%p, %s)", (level - 1) * 2, "", this, mName.string());
94113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
95113e0824d6bddf4376240681f9cf6a2deded9498John Reck
96d0a0b2a3140bfb1819a116413ce9d81886697a07John Reckvoid RenderNode::updateProperties() {
97d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (mNeedsPropertiesSync) {
98d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        mNeedsPropertiesSync = false;
99d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        mProperties = mStagingProperties;
100113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
101113e0824d6bddf4376240681f9cf6a2deded9498John Reck
1025bf11bb98f5dbe278c257355d24c181237abd68cJohn Reck    if (mDisplayListData) {
1035bf11bb98f5dbe278c257355d24c181237abd68cJohn Reck        for (size_t i = 0; i < mDisplayListData->children.size(); i++) {
1045bf11bb98f5dbe278c257355d24c181237abd68cJohn Reck            RenderNode* childNode = mDisplayListData->children[i]->mDisplayList;
1055bf11bb98f5dbe278c257355d24c181237abd68cJohn Reck            childNode->updateProperties();
1065bf11bb98f5dbe278c257355d24c181237abd68cJohn Reck        }
107113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
108113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
109113e0824d6bddf4376240681f9cf6a2deded9498John Reck
110113e0824d6bddf4376240681f9cf6a2deded9498John Reck/*
111113e0824d6bddf4376240681f9cf6a2deded9498John Reck * For property operations, we pass a savecount of 0, since the operations aren't part of the
112113e0824d6bddf4376240681f9cf6a2deded9498John Reck * displaylist, and thus don't have to compensate for the record-time/playback-time discrepancy in
113d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck * base saveCount (i.e., how RestoreToCount uses saveCount + properties().getCount())
114113e0824d6bddf4376240681f9cf6a2deded9498John Reck */
115113e0824d6bddf4376240681f9cf6a2deded9498John Reck#define PROPERTY_SAVECOUNT 0
116113e0824d6bddf4376240681f9cf6a2deded9498John Reck
117113e0824d6bddf4376240681f9cf6a2deded9498John Recktemplate <class T>
118113e0824d6bddf4376240681f9cf6a2deded9498John Reckvoid RenderNode::setViewProperties(OpenGLRenderer& renderer, T& handler,
119113e0824d6bddf4376240681f9cf6a2deded9498John Reck        const int level) {
120113e0824d6bddf4376240681f9cf6a2deded9498John Reck#if DEBUG_DISPLAY_LIST
121d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    properties().debugOutputProperties(level);
122113e0824d6bddf4376240681f9cf6a2deded9498John Reck#endif
123d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (properties().getLeft() != 0 || properties().getTop() != 0) {
124d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        renderer.translate(properties().getLeft(), properties().getTop());
125113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
126d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (properties().getStaticMatrix()) {
127d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        renderer.concatMatrix(properties().getStaticMatrix());
128d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    } else if (properties().getAnimationMatrix()) {
129d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        renderer.concatMatrix(properties().getAnimationMatrix());
130113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
131d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (properties().getMatrixFlags() != 0) {
132d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        if (properties().getMatrixFlags() == TRANSLATION) {
133d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            renderer.translate(properties().getTranslationX(), properties().getTranslationY());
134113e0824d6bddf4376240681f9cf6a2deded9498John Reck        } else {
135d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            renderer.concatMatrix(*properties().getTransformMatrix());
136113e0824d6bddf4376240681f9cf6a2deded9498John Reck        }
137113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
138d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    bool clipToBoundsNeeded = properties().getCaching() ? false : properties().getClipToBounds();
139d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (properties().getAlpha() < 1) {
140d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        if (properties().getCaching()) {
141d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            renderer.setOverrideLayerAlpha(properties().getAlpha());
142d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        } else if (!properties().getHasOverlappingRendering()) {
143d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            renderer.scaleAlpha(properties().getAlpha());
144113e0824d6bddf4376240681f9cf6a2deded9498John Reck        } else {
145113e0824d6bddf4376240681f9cf6a2deded9498John Reck            // TODO: should be able to store the size of a DL at record time and not
146113e0824d6bddf4376240681f9cf6a2deded9498John Reck            // have to pass it into this call. In fact, this information might be in the
147113e0824d6bddf4376240681f9cf6a2deded9498John Reck            // location/size info that we store with the new native transform data.
148113e0824d6bddf4376240681f9cf6a2deded9498John Reck            int saveFlags = SkCanvas::kHasAlphaLayer_SaveFlag;
149113e0824d6bddf4376240681f9cf6a2deded9498John Reck            if (clipToBoundsNeeded) {
150113e0824d6bddf4376240681f9cf6a2deded9498John Reck                saveFlags |= SkCanvas::kClipToLayer_SaveFlag;
151113e0824d6bddf4376240681f9cf6a2deded9498John Reck                clipToBoundsNeeded = false; // clipping done by saveLayer
152113e0824d6bddf4376240681f9cf6a2deded9498John Reck            }
153113e0824d6bddf4376240681f9cf6a2deded9498John Reck
154113e0824d6bddf4376240681f9cf6a2deded9498John Reck            SaveLayerOp* op = new (handler.allocator()) SaveLayerOp(
1558c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik                    0, 0, properties().getWidth(), properties().getHeight(),
1568c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik                    properties().getAlpha() * 255, saveFlags);
157d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
158113e0824d6bddf4376240681f9cf6a2deded9498John Reck        }
159113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
160113e0824d6bddf4376240681f9cf6a2deded9498John Reck    if (clipToBoundsNeeded) {
1618c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik        ClipRectOp* op = new (handler.allocator()) ClipRectOp(
1628c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik                0, 0, properties().getWidth(), properties().getHeight(), SkRegion::kIntersect_Op);
163d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
164113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
1658c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik
1668c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik    if (CC_UNLIKELY(properties().hasClippingPath())) {
1678c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik        // TODO: optimize for round rect/circle clipping
1688c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik        const SkPath* path = properties().getClippingPath();
1698c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik        ClipPathOp* op = new (handler.allocator()) ClipPathOp(path, SkRegion::kIntersect_Op);
170d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
171113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
172113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
173113e0824d6bddf4376240681f9cf6a2deded9498John Reck
174113e0824d6bddf4376240681f9cf6a2deded9498John Reck/**
175113e0824d6bddf4376240681f9cf6a2deded9498John Reck * Apply property-based transformations to input matrix
176113e0824d6bddf4376240681f9cf6a2deded9498John Reck *
177113e0824d6bddf4376240681f9cf6a2deded9498John Reck * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
178113e0824d6bddf4376240681f9cf6a2deded9498John Reck * matrix computation instead of the Skia 3x3 matrix + camera hackery.
179113e0824d6bddf4376240681f9cf6a2deded9498John Reck */
180113e0824d6bddf4376240681f9cf6a2deded9498John Reckvoid RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) {
181d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (properties().getLeft() != 0 || properties().getTop() != 0) {
182d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        matrix.translate(properties().getLeft(), properties().getTop());
183113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
184d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (properties().getStaticMatrix()) {
185d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        mat4 stat(*properties().getStaticMatrix());
186113e0824d6bddf4376240681f9cf6a2deded9498John Reck        matrix.multiply(stat);
187d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    } else if (properties().getAnimationMatrix()) {
188d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        mat4 anim(*properties().getAnimationMatrix());
189113e0824d6bddf4376240681f9cf6a2deded9498John Reck        matrix.multiply(anim);
190113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
191d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (properties().getMatrixFlags() != 0) {
192d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        if (properties().getMatrixFlags() == TRANSLATION) {
193d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
194d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                    true3dTransform ? properties().getTranslationZ() : 0.0f);
195113e0824d6bddf4376240681f9cf6a2deded9498John Reck        } else {
196113e0824d6bddf4376240681f9cf6a2deded9498John Reck            if (!true3dTransform) {
197d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                matrix.multiply(*properties().getTransformMatrix());
198113e0824d6bddf4376240681f9cf6a2deded9498John Reck            } else {
199113e0824d6bddf4376240681f9cf6a2deded9498John Reck                mat4 true3dMat;
200113e0824d6bddf4376240681f9cf6a2deded9498John Reck                true3dMat.loadTranslate(
201d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                        properties().getPivotX() + properties().getTranslationX(),
202d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                        properties().getPivotY() + properties().getTranslationY(),
203d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                        properties().getTranslationZ());
204d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
205d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
206d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                true3dMat.rotate(properties().getRotation(), 0, 0, 1);
207d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
208d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
209113e0824d6bddf4376240681f9cf6a2deded9498John Reck
210113e0824d6bddf4376240681f9cf6a2deded9498John Reck                matrix.multiply(true3dMat);
211113e0824d6bddf4376240681f9cf6a2deded9498John Reck            }
212113e0824d6bddf4376240681f9cf6a2deded9498John Reck        }
213113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
214113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
215113e0824d6bddf4376240681f9cf6a2deded9498John Reck
216113e0824d6bddf4376240681f9cf6a2deded9498John Reck/**
217113e0824d6bddf4376240681f9cf6a2deded9498John Reck * Organizes the DisplayList hierarchy to prepare for background projection reordering.
218113e0824d6bddf4376240681f9cf6a2deded9498John Reck *
219113e0824d6bddf4376240681f9cf6a2deded9498John Reck * This should be called before a call to defer() or drawDisplayList()
220113e0824d6bddf4376240681f9cf6a2deded9498John Reck *
221113e0824d6bddf4376240681f9cf6a2deded9498John Reck * Each DisplayList that serves as a 3d root builds its list of composited children,
222113e0824d6bddf4376240681f9cf6a2deded9498John Reck * which are flagged to not draw in the standard draw loop.
223113e0824d6bddf4376240681f9cf6a2deded9498John Reck */
224113e0824d6bddf4376240681f9cf6a2deded9498John Reckvoid RenderNode::computeOrdering() {
225113e0824d6bddf4376240681f9cf6a2deded9498John Reck    ATRACE_CALL();
226113e0824d6bddf4376240681f9cf6a2deded9498John Reck    mProjectedNodes.clear();
227113e0824d6bddf4376240681f9cf6a2deded9498John Reck
228113e0824d6bddf4376240681f9cf6a2deded9498John Reck    // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that
229113e0824d6bddf4376240681f9cf6a2deded9498John Reck    // transform properties are applied correctly to top level children
230113e0824d6bddf4376240681f9cf6a2deded9498John Reck    if (mDisplayListData == NULL) return;
231113e0824d6bddf4376240681f9cf6a2deded9498John Reck    for (unsigned int i = 0; i < mDisplayListData->children.size(); i++) {
232113e0824d6bddf4376240681f9cf6a2deded9498John Reck        DrawDisplayListOp* childOp = mDisplayListData->children[i];
233113e0824d6bddf4376240681f9cf6a2deded9498John Reck        childOp->mDisplayList->computeOrderingImpl(childOp,
234113e0824d6bddf4376240681f9cf6a2deded9498John Reck                &mProjectedNodes, &mat4::identity());
235113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
236113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
237113e0824d6bddf4376240681f9cf6a2deded9498John Reck
238113e0824d6bddf4376240681f9cf6a2deded9498John Reckvoid RenderNode::computeOrderingImpl(
239113e0824d6bddf4376240681f9cf6a2deded9498John Reck        DrawDisplayListOp* opState,
240113e0824d6bddf4376240681f9cf6a2deded9498John Reck        Vector<DrawDisplayListOp*>* compositedChildrenOfProjectionSurface,
241113e0824d6bddf4376240681f9cf6a2deded9498John Reck        const mat4* transformFromProjectionSurface) {
242113e0824d6bddf4376240681f9cf6a2deded9498John Reck    mProjectedNodes.clear();
243113e0824d6bddf4376240681f9cf6a2deded9498John Reck    if (mDisplayListData == NULL || mDisplayListData->isEmpty()) return;
244113e0824d6bddf4376240681f9cf6a2deded9498John Reck
245113e0824d6bddf4376240681f9cf6a2deded9498John Reck    // TODO: should avoid this calculation in most cases
246113e0824d6bddf4376240681f9cf6a2deded9498John Reck    // TODO: just calculate single matrix, down to all leaf composited elements
247113e0824d6bddf4376240681f9cf6a2deded9498John Reck    Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface);
248113e0824d6bddf4376240681f9cf6a2deded9498John Reck    localTransformFromProjectionSurface.multiply(opState->mTransformFromParent);
249113e0824d6bddf4376240681f9cf6a2deded9498John Reck
250d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (properties().getProjectBackwards()) {
251113e0824d6bddf4376240681f9cf6a2deded9498John Reck        // composited projectee, flag for out of order draw, save matrix, and store in proj surface
252113e0824d6bddf4376240681f9cf6a2deded9498John Reck        opState->mSkipInOrderDraw = true;
253113e0824d6bddf4376240681f9cf6a2deded9498John Reck        opState->mTransformFromCompositingAncestor.load(localTransformFromProjectionSurface);
254113e0824d6bddf4376240681f9cf6a2deded9498John Reck        compositedChildrenOfProjectionSurface->add(opState);
255113e0824d6bddf4376240681f9cf6a2deded9498John Reck    } else {
256113e0824d6bddf4376240681f9cf6a2deded9498John Reck        // standard in order draw
257113e0824d6bddf4376240681f9cf6a2deded9498John Reck        opState->mSkipInOrderDraw = false;
258113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
259113e0824d6bddf4376240681f9cf6a2deded9498John Reck
260113e0824d6bddf4376240681f9cf6a2deded9498John Reck    if (mDisplayListData->children.size() > 0) {
261113e0824d6bddf4376240681f9cf6a2deded9498John Reck        const bool isProjectionReceiver = mDisplayListData->projectionReceiveIndex >= 0;
262113e0824d6bddf4376240681f9cf6a2deded9498John Reck        bool haveAppliedPropertiesToProjection = false;
263113e0824d6bddf4376240681f9cf6a2deded9498John Reck        for (unsigned int i = 0; i < mDisplayListData->children.size(); i++) {
264113e0824d6bddf4376240681f9cf6a2deded9498John Reck            DrawDisplayListOp* childOp = mDisplayListData->children[i];
265113e0824d6bddf4376240681f9cf6a2deded9498John Reck            RenderNode* child = childOp->mDisplayList;
266113e0824d6bddf4376240681f9cf6a2deded9498John Reck
267113e0824d6bddf4376240681f9cf6a2deded9498John Reck            Vector<DrawDisplayListOp*>* projectionChildren = NULL;
268113e0824d6bddf4376240681f9cf6a2deded9498John Reck            const mat4* projectionTransform = NULL;
269d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            if (isProjectionReceiver && !child->properties().getProjectBackwards()) {
270113e0824d6bddf4376240681f9cf6a2deded9498John Reck                // if receiving projections, collect projecting descendent
271113e0824d6bddf4376240681f9cf6a2deded9498John Reck
272113e0824d6bddf4376240681f9cf6a2deded9498John Reck                // Note that if a direct descendent is projecting backwards, we pass it's
273113e0824d6bddf4376240681f9cf6a2deded9498John Reck                // grandparent projection collection, since it shouldn't project onto it's
274113e0824d6bddf4376240681f9cf6a2deded9498John Reck                // parent, where it will already be drawing.
275113e0824d6bddf4376240681f9cf6a2deded9498John Reck                projectionChildren = &mProjectedNodes;
276113e0824d6bddf4376240681f9cf6a2deded9498John Reck                projectionTransform = &mat4::identity();
277113e0824d6bddf4376240681f9cf6a2deded9498John Reck            } else {
278113e0824d6bddf4376240681f9cf6a2deded9498John Reck                if (!haveAppliedPropertiesToProjection) {
279113e0824d6bddf4376240681f9cf6a2deded9498John Reck                    applyViewPropertyTransforms(localTransformFromProjectionSurface);
280113e0824d6bddf4376240681f9cf6a2deded9498John Reck                    haveAppliedPropertiesToProjection = true;
281113e0824d6bddf4376240681f9cf6a2deded9498John Reck                }
282113e0824d6bddf4376240681f9cf6a2deded9498John Reck                projectionChildren = compositedChildrenOfProjectionSurface;
283113e0824d6bddf4376240681f9cf6a2deded9498John Reck                projectionTransform = &localTransformFromProjectionSurface;
284113e0824d6bddf4376240681f9cf6a2deded9498John Reck            }
285113e0824d6bddf4376240681f9cf6a2deded9498John Reck            child->computeOrderingImpl(childOp, projectionChildren, projectionTransform);
286113e0824d6bddf4376240681f9cf6a2deded9498John Reck        }
287113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
288113e0824d6bddf4376240681f9cf6a2deded9498John Reck
289113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
290113e0824d6bddf4376240681f9cf6a2deded9498John Reck
291113e0824d6bddf4376240681f9cf6a2deded9498John Reckclass DeferOperationHandler {
292113e0824d6bddf4376240681f9cf6a2deded9498John Reckpublic:
293113e0824d6bddf4376240681f9cf6a2deded9498John Reck    DeferOperationHandler(DeferStateStruct& deferStruct, int level)
294113e0824d6bddf4376240681f9cf6a2deded9498John Reck        : mDeferStruct(deferStruct), mLevel(level) {}
295113e0824d6bddf4376240681f9cf6a2deded9498John Reck    inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
296113e0824d6bddf4376240681f9cf6a2deded9498John Reck        operation->defer(mDeferStruct, saveCount, mLevel, clipToBounds);
297113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
298113e0824d6bddf4376240681f9cf6a2deded9498John Reck    inline LinearAllocator& allocator() { return *(mDeferStruct.mAllocator); }
299113e0824d6bddf4376240681f9cf6a2deded9498John Reck
300113e0824d6bddf4376240681f9cf6a2deded9498John Reckprivate:
301113e0824d6bddf4376240681f9cf6a2deded9498John Reck    DeferStateStruct& mDeferStruct;
302113e0824d6bddf4376240681f9cf6a2deded9498John Reck    const int mLevel;
303113e0824d6bddf4376240681f9cf6a2deded9498John Reck};
304113e0824d6bddf4376240681f9cf6a2deded9498John Reck
305113e0824d6bddf4376240681f9cf6a2deded9498John Reckvoid RenderNode::defer(DeferStateStruct& deferStruct, const int level) {
306113e0824d6bddf4376240681f9cf6a2deded9498John Reck    DeferOperationHandler handler(deferStruct, level);
307113e0824d6bddf4376240681f9cf6a2deded9498John Reck    iterate<DeferOperationHandler>(deferStruct.mRenderer, handler, level);
308113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
309113e0824d6bddf4376240681f9cf6a2deded9498John Reck
310113e0824d6bddf4376240681f9cf6a2deded9498John Reckclass ReplayOperationHandler {
311113e0824d6bddf4376240681f9cf6a2deded9498John Reckpublic:
312113e0824d6bddf4376240681f9cf6a2deded9498John Reck    ReplayOperationHandler(ReplayStateStruct& replayStruct, int level)
313113e0824d6bddf4376240681f9cf6a2deded9498John Reck        : mReplayStruct(replayStruct), mLevel(level) {}
314113e0824d6bddf4376240681f9cf6a2deded9498John Reck    inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
315113e0824d6bddf4376240681f9cf6a2deded9498John Reck#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
316d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        properties().getReplayStruct().mRenderer.eventMark(operation->name());
317113e0824d6bddf4376240681f9cf6a2deded9498John Reck#endif
318113e0824d6bddf4376240681f9cf6a2deded9498John Reck        operation->replay(mReplayStruct, saveCount, mLevel, clipToBounds);
319113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
320113e0824d6bddf4376240681f9cf6a2deded9498John Reck    inline LinearAllocator& allocator() { return *(mReplayStruct.mAllocator); }
321113e0824d6bddf4376240681f9cf6a2deded9498John Reck
322113e0824d6bddf4376240681f9cf6a2deded9498John Reckprivate:
323113e0824d6bddf4376240681f9cf6a2deded9498John Reck    ReplayStateStruct& mReplayStruct;
324113e0824d6bddf4376240681f9cf6a2deded9498John Reck    const int mLevel;
325113e0824d6bddf4376240681f9cf6a2deded9498John Reck};
326113e0824d6bddf4376240681f9cf6a2deded9498John Reck
327113e0824d6bddf4376240681f9cf6a2deded9498John Reckvoid RenderNode::replay(ReplayStateStruct& replayStruct, const int level) {
328113e0824d6bddf4376240681f9cf6a2deded9498John Reck    ReplayOperationHandler handler(replayStruct, level);
329113e0824d6bddf4376240681f9cf6a2deded9498John Reck
330113e0824d6bddf4376240681f9cf6a2deded9498John Reck    replayStruct.mRenderer.startMark(mName.string());
331113e0824d6bddf4376240681f9cf6a2deded9498John Reck    iterate<ReplayOperationHandler>(replayStruct.mRenderer, handler, level);
332113e0824d6bddf4376240681f9cf6a2deded9498John Reck    replayStruct.mRenderer.endMark();
333113e0824d6bddf4376240681f9cf6a2deded9498John Reck
334113e0824d6bddf4376240681f9cf6a2deded9498John Reck    DISPLAY_LIST_LOGD("%*sDone (%p, %s), returning %d", level * 2, "", this, mName.string(),
335113e0824d6bddf4376240681f9cf6a2deded9498John Reck            replayStruct.mDrawGlStatus);
336113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
337113e0824d6bddf4376240681f9cf6a2deded9498John Reck
338113e0824d6bddf4376240681f9cf6a2deded9498John Reckvoid RenderNode::buildZSortedChildList(Vector<ZDrawDisplayListOpPair>& zTranslatedNodes) {
339113e0824d6bddf4376240681f9cf6a2deded9498John Reck    if (mDisplayListData == NULL || mDisplayListData->children.size() == 0) return;
340113e0824d6bddf4376240681f9cf6a2deded9498John Reck
341113e0824d6bddf4376240681f9cf6a2deded9498John Reck    for (unsigned int i = 0; i < mDisplayListData->children.size(); i++) {
342113e0824d6bddf4376240681f9cf6a2deded9498John Reck        DrawDisplayListOp* childOp = mDisplayListData->children[i];
343113e0824d6bddf4376240681f9cf6a2deded9498John Reck        RenderNode* child = childOp->mDisplayList;
344d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        float childZ = child->properties().getTranslationZ();
345113e0824d6bddf4376240681f9cf6a2deded9498John Reck
346113e0824d6bddf4376240681f9cf6a2deded9498John Reck        if (childZ != 0.0f) {
347113e0824d6bddf4376240681f9cf6a2deded9498John Reck            zTranslatedNodes.add(ZDrawDisplayListOpPair(childZ, childOp));
348113e0824d6bddf4376240681f9cf6a2deded9498John Reck            childOp->mSkipInOrderDraw = true;
349d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        } else if (!child->properties().getProjectBackwards()) {
350113e0824d6bddf4376240681f9cf6a2deded9498John Reck            // regular, in order drawing DisplayList
351113e0824d6bddf4376240681f9cf6a2deded9498John Reck            childOp->mSkipInOrderDraw = false;
352113e0824d6bddf4376240681f9cf6a2deded9498John Reck        }
353113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
354113e0824d6bddf4376240681f9cf6a2deded9498John Reck
355113e0824d6bddf4376240681f9cf6a2deded9498John Reck    // Z sort 3d children (stable-ness makes z compare fall back to standard drawing order)
356113e0824d6bddf4376240681f9cf6a2deded9498John Reck    std::stable_sort(zTranslatedNodes.begin(), zTranslatedNodes.end());
357113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
358113e0824d6bddf4376240681f9cf6a2deded9498John Reck
359113e0824d6bddf4376240681f9cf6a2deded9498John Reck#define SHADOW_DELTA 0.1f
360113e0824d6bddf4376240681f9cf6a2deded9498John Reck
361113e0824d6bddf4376240681f9cf6a2deded9498John Recktemplate <class T>
362113e0824d6bddf4376240681f9cf6a2deded9498John Reckvoid RenderNode::iterate3dChildren(const Vector<ZDrawDisplayListOpPair>& zTranslatedNodes,
363113e0824d6bddf4376240681f9cf6a2deded9498John Reck        ChildrenSelectMode mode, OpenGLRenderer& renderer, T& handler) {
364113e0824d6bddf4376240681f9cf6a2deded9498John Reck    const int size = zTranslatedNodes.size();
365113e0824d6bddf4376240681f9cf6a2deded9498John Reck    if (size == 0
366113e0824d6bddf4376240681f9cf6a2deded9498John Reck            || (mode == kNegativeZChildren && zTranslatedNodes[0].key > 0.0f)
367113e0824d6bddf4376240681f9cf6a2deded9498John Reck            || (mode == kPositiveZChildren && zTranslatedNodes[size - 1].key < 0.0f)) {
368113e0824d6bddf4376240681f9cf6a2deded9498John Reck        // no 3d children to draw
369113e0824d6bddf4376240681f9cf6a2deded9498John Reck        return;
370113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
371113e0824d6bddf4376240681f9cf6a2deded9498John Reck
372113e0824d6bddf4376240681f9cf6a2deded9498John Reck    int rootRestoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
373113e0824d6bddf4376240681f9cf6a2deded9498John Reck    LinearAllocator& alloc = handler.allocator();
374d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    ClipRectOp* clipOp = new (alloc) ClipRectOp(0, 0, properties().getWidth(), properties().getHeight(),
375113e0824d6bddf4376240681f9cf6a2deded9498John Reck            SkRegion::kIntersect_Op); // clip to 3d root bounds
376d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    handler(clipOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
377113e0824d6bddf4376240681f9cf6a2deded9498John Reck
378113e0824d6bddf4376240681f9cf6a2deded9498John Reck    /**
379113e0824d6bddf4376240681f9cf6a2deded9498John Reck     * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
380113e0824d6bddf4376240681f9cf6a2deded9498John Reck     * with very similar Z heights to draw together.
381113e0824d6bddf4376240681f9cf6a2deded9498John Reck     *
382113e0824d6bddf4376240681f9cf6a2deded9498John Reck     * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
383113e0824d6bddf4376240681f9cf6a2deded9498John Reck     * underneath both, and neither's shadow is drawn on top of the other.
384113e0824d6bddf4376240681f9cf6a2deded9498John Reck     */
385113e0824d6bddf4376240681f9cf6a2deded9498John Reck    const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
386113e0824d6bddf4376240681f9cf6a2deded9498John Reck    size_t drawIndex, shadowIndex, endIndex;
387113e0824d6bddf4376240681f9cf6a2deded9498John Reck    if (mode == kNegativeZChildren) {
388113e0824d6bddf4376240681f9cf6a2deded9498John Reck        drawIndex = 0;
389113e0824d6bddf4376240681f9cf6a2deded9498John Reck        endIndex = nonNegativeIndex;
390113e0824d6bddf4376240681f9cf6a2deded9498John Reck        shadowIndex = endIndex; // draw no shadows
391113e0824d6bddf4376240681f9cf6a2deded9498John Reck    } else {
392113e0824d6bddf4376240681f9cf6a2deded9498John Reck        drawIndex = nonNegativeIndex;
393113e0824d6bddf4376240681f9cf6a2deded9498John Reck        endIndex = size;
394113e0824d6bddf4376240681f9cf6a2deded9498John Reck        shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
395113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
396113e0824d6bddf4376240681f9cf6a2deded9498John Reck    float lastCasterZ = 0.0f;
397113e0824d6bddf4376240681f9cf6a2deded9498John Reck    while (shadowIndex < endIndex || drawIndex < endIndex) {
398113e0824d6bddf4376240681f9cf6a2deded9498John Reck        if (shadowIndex < endIndex) {
399113e0824d6bddf4376240681f9cf6a2deded9498John Reck            DrawDisplayListOp* casterOp = zTranslatedNodes[shadowIndex].value;
400113e0824d6bddf4376240681f9cf6a2deded9498John Reck            RenderNode* caster = casterOp->mDisplayList;
401113e0824d6bddf4376240681f9cf6a2deded9498John Reck            const float casterZ = zTranslatedNodes[shadowIndex].key;
402113e0824d6bddf4376240681f9cf6a2deded9498John Reck            // attempt to render the shadow if the caster about to be drawn is its caster,
403113e0824d6bddf4376240681f9cf6a2deded9498John Reck            // OR if its caster's Z value is similar to the previous potential caster
404113e0824d6bddf4376240681f9cf6a2deded9498John Reck            if (shadowIndex == drawIndex || casterZ - lastCasterZ < SHADOW_DELTA) {
405113e0824d6bddf4376240681f9cf6a2deded9498John Reck
406d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                if (caster->properties().getAlpha() > 0.0f) {
407113e0824d6bddf4376240681f9cf6a2deded9498John Reck                    mat4 shadowMatrixXY(casterOp->mTransformFromParent);
408113e0824d6bddf4376240681f9cf6a2deded9498John Reck                    caster->applyViewPropertyTransforms(shadowMatrixXY);
409113e0824d6bddf4376240681f9cf6a2deded9498John Reck
410113e0824d6bddf4376240681f9cf6a2deded9498John Reck                    // Z matrix needs actual 3d transformation, so mapped z values will be correct
411113e0824d6bddf4376240681f9cf6a2deded9498John Reck                    mat4 shadowMatrixZ(casterOp->mTransformFromParent);
412113e0824d6bddf4376240681f9cf6a2deded9498John Reck                    caster->applyViewPropertyTransforms(shadowMatrixZ, true);
413113e0824d6bddf4376240681f9cf6a2deded9498John Reck
4148c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik                    const SkPath* outlinePath = caster->properties().getOutline().getPath();
4158c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik                    const RevealClip& revealClip = caster->properties().getRevealClip();
4168c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik                    const SkPath* revealClipPath = revealClip.hasConvexClip()
4178c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik                            ?  revealClip.getPath() : NULL; // only pass the reveal clip's path if it's convex
4188c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik
419113e0824d6bddf4376240681f9cf6a2deded9498John Reck                    DisplayListOp* shadowOp  = new (alloc) DrawShadowOp(
4208c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik                            shadowMatrixXY, shadowMatrixZ, caster->properties().getAlpha(),
4218c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik                            caster->properties().getWidth(), caster->properties().getHeight(),
4228c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik                            outlinePath, revealClipPath);
423d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                    handler(shadowOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
424113e0824d6bddf4376240681f9cf6a2deded9498John Reck                }
425113e0824d6bddf4376240681f9cf6a2deded9498John Reck
426113e0824d6bddf4376240681f9cf6a2deded9498John Reck                lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
427113e0824d6bddf4376240681f9cf6a2deded9498John Reck                shadowIndex++;
428113e0824d6bddf4376240681f9cf6a2deded9498John Reck                continue;
429113e0824d6bddf4376240681f9cf6a2deded9498John Reck            }
430113e0824d6bddf4376240681f9cf6a2deded9498John Reck        }
431113e0824d6bddf4376240681f9cf6a2deded9498John Reck
432113e0824d6bddf4376240681f9cf6a2deded9498John Reck        // only the actual child DL draw needs to be in save/restore,
433113e0824d6bddf4376240681f9cf6a2deded9498John Reck        // since it modifies the renderer's matrix
434113e0824d6bddf4376240681f9cf6a2deded9498John Reck        int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
435113e0824d6bddf4376240681f9cf6a2deded9498John Reck
436113e0824d6bddf4376240681f9cf6a2deded9498John Reck        DrawDisplayListOp* childOp = zTranslatedNodes[drawIndex].value;
437113e0824d6bddf4376240681f9cf6a2deded9498John Reck        RenderNode* child = childOp->mDisplayList;
438113e0824d6bddf4376240681f9cf6a2deded9498John Reck
439113e0824d6bddf4376240681f9cf6a2deded9498John Reck        renderer.concatMatrix(childOp->mTransformFromParent);
440113e0824d6bddf4376240681f9cf6a2deded9498John Reck        childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
441d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
442113e0824d6bddf4376240681f9cf6a2deded9498John Reck        childOp->mSkipInOrderDraw = true;
443113e0824d6bddf4376240681f9cf6a2deded9498John Reck
444113e0824d6bddf4376240681f9cf6a2deded9498John Reck        renderer.restoreToCount(restoreTo);
445113e0824d6bddf4376240681f9cf6a2deded9498John Reck        drawIndex++;
446113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
447d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    handler(new (alloc) RestoreToCountOp(rootRestoreTo), PROPERTY_SAVECOUNT, properties().getClipToBounds());
448113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
449113e0824d6bddf4376240681f9cf6a2deded9498John Reck
450113e0824d6bddf4376240681f9cf6a2deded9498John Recktemplate <class T>
451113e0824d6bddf4376240681f9cf6a2deded9498John Reckvoid RenderNode::iterateProjectedChildren(OpenGLRenderer& renderer, T& handler, const int level) {
452113e0824d6bddf4376240681f9cf6a2deded9498John Reck    int rootRestoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
453113e0824d6bddf4376240681f9cf6a2deded9498John Reck    LinearAllocator& alloc = handler.allocator();
454d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    ClipRectOp* clipOp = new (alloc) ClipRectOp(0, 0, properties().getWidth(), properties().getHeight(),
455113e0824d6bddf4376240681f9cf6a2deded9498John Reck            SkRegion::kReplace_Op); // clip to projection surface root bounds
456d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    handler(clipOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
457113e0824d6bddf4376240681f9cf6a2deded9498John Reck
458113e0824d6bddf4376240681f9cf6a2deded9498John Reck    for (size_t i = 0; i < mProjectedNodes.size(); i++) {
459113e0824d6bddf4376240681f9cf6a2deded9498John Reck        DrawDisplayListOp* childOp = mProjectedNodes[i];
460113e0824d6bddf4376240681f9cf6a2deded9498John Reck
461113e0824d6bddf4376240681f9cf6a2deded9498John Reck        // matrix save, concat, and restore can be done safely without allocating operations
462113e0824d6bddf4376240681f9cf6a2deded9498John Reck        int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
463113e0824d6bddf4376240681f9cf6a2deded9498John Reck        renderer.concatMatrix(childOp->mTransformFromCompositingAncestor);
464113e0824d6bddf4376240681f9cf6a2deded9498John Reck        childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
465d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
466113e0824d6bddf4376240681f9cf6a2deded9498John Reck        childOp->mSkipInOrderDraw = true;
467113e0824d6bddf4376240681f9cf6a2deded9498John Reck        renderer.restoreToCount(restoreTo);
468113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
469d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    handler(new (alloc) RestoreToCountOp(rootRestoreTo), PROPERTY_SAVECOUNT, properties().getClipToBounds());
470113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
471113e0824d6bddf4376240681f9cf6a2deded9498John Reck
472113e0824d6bddf4376240681f9cf6a2deded9498John Reck/**
473113e0824d6bddf4376240681f9cf6a2deded9498John Reck * This function serves both defer and replay modes, and will organize the displayList's component
474113e0824d6bddf4376240681f9cf6a2deded9498John Reck * operations for a single frame:
475113e0824d6bddf4376240681f9cf6a2deded9498John Reck *
476113e0824d6bddf4376240681f9cf6a2deded9498John Reck * Every 'simple' state operation that affects just the matrix and alpha (or other factors of
477113e0824d6bddf4376240681f9cf6a2deded9498John Reck * DeferredDisplayState) may be issued directly to the renderer, but complex operations (with custom
478113e0824d6bddf4376240681f9cf6a2deded9498John Reck * defer logic) and operations in displayListOps are issued through the 'handler' which handles the
479113e0824d6bddf4376240681f9cf6a2deded9498John Reck * defer vs replay logic, per operation
480113e0824d6bddf4376240681f9cf6a2deded9498John Reck */
481113e0824d6bddf4376240681f9cf6a2deded9498John Recktemplate <class T>
482113e0824d6bddf4376240681f9cf6a2deded9498John Reckvoid RenderNode::iterate(OpenGLRenderer& renderer, T& handler, const int level) {
483113e0824d6bddf4376240681f9cf6a2deded9498John Reck    if (CC_UNLIKELY(mDestroyed)) { // temporary debug logging
484113e0824d6bddf4376240681f9cf6a2deded9498John Reck        ALOGW("Error: %s is drawing after destruction", mName.string());
485113e0824d6bddf4376240681f9cf6a2deded9498John Reck        CRASH();
486113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
487d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (mDisplayListData->isEmpty() || properties().getAlpha() <= 0) {
488113e0824d6bddf4376240681f9cf6a2deded9498John Reck        DISPLAY_LIST_LOGD("%*sEmpty display list (%p, %s)", level * 2, "", this, mName.string());
489113e0824d6bddf4376240681f9cf6a2deded9498John Reck        return;
490113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
491113e0824d6bddf4376240681f9cf6a2deded9498John Reck
492113e0824d6bddf4376240681f9cf6a2deded9498John Reck#if DEBUG_DISPLAY_LIST
493113e0824d6bddf4376240681f9cf6a2deded9498John Reck    Rect* clipRect = renderer.getClipRect();
494113e0824d6bddf4376240681f9cf6a2deded9498John Reck    DISPLAY_LIST_LOGD("%*sStart display list (%p, %s), clipRect: %.0f, %.0f, %.0f, %.0f",
495113e0824d6bddf4376240681f9cf6a2deded9498John Reck            level * 2, "", this, mName.string(), clipRect->left, clipRect->top,
496113e0824d6bddf4376240681f9cf6a2deded9498John Reck            clipRect->right, clipRect->bottom);
497113e0824d6bddf4376240681f9cf6a2deded9498John Reck#endif
498113e0824d6bddf4376240681f9cf6a2deded9498John Reck
499113e0824d6bddf4376240681f9cf6a2deded9498John Reck    LinearAllocator& alloc = handler.allocator();
500113e0824d6bddf4376240681f9cf6a2deded9498John Reck    int restoreTo = renderer.getSaveCount();
501113e0824d6bddf4376240681f9cf6a2deded9498John Reck    handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
502d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            PROPERTY_SAVECOUNT, properties().getClipToBounds());
503113e0824d6bddf4376240681f9cf6a2deded9498John Reck
504113e0824d6bddf4376240681f9cf6a2deded9498John Reck    DISPLAY_LIST_LOGD("%*sSave %d %d", (level + 1) * 2, "",
505113e0824d6bddf4376240681f9cf6a2deded9498John Reck            SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag, restoreTo);
506113e0824d6bddf4376240681f9cf6a2deded9498John Reck
507113e0824d6bddf4376240681f9cf6a2deded9498John Reck    setViewProperties<T>(renderer, handler, level + 1);
508113e0824d6bddf4376240681f9cf6a2deded9498John Reck
5098c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik    bool quickRejected = properties().getClipToBounds()
5108c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik            && renderer.quickRejectConservative(0, 0, properties().getWidth(), properties().getHeight());
511113e0824d6bddf4376240681f9cf6a2deded9498John Reck    if (!quickRejected) {
512113e0824d6bddf4376240681f9cf6a2deded9498John Reck        Vector<ZDrawDisplayListOpPair> zTranslatedNodes;
513113e0824d6bddf4376240681f9cf6a2deded9498John Reck        buildZSortedChildList(zTranslatedNodes);
514113e0824d6bddf4376240681f9cf6a2deded9498John Reck
515113e0824d6bddf4376240681f9cf6a2deded9498John Reck        // for 3d root, draw children with negative z values
516113e0824d6bddf4376240681f9cf6a2deded9498John Reck        iterate3dChildren(zTranslatedNodes, kNegativeZChildren, renderer, handler);
517113e0824d6bddf4376240681f9cf6a2deded9498John Reck
518113e0824d6bddf4376240681f9cf6a2deded9498John Reck        DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance();
519113e0824d6bddf4376240681f9cf6a2deded9498John Reck        const int saveCountOffset = renderer.getSaveCount() - 1;
520113e0824d6bddf4376240681f9cf6a2deded9498John Reck        const int projectionReceiveIndex = mDisplayListData->projectionReceiveIndex;
521113e0824d6bddf4376240681f9cf6a2deded9498John Reck        for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) {
522113e0824d6bddf4376240681f9cf6a2deded9498John Reck            DisplayListOp *op = mDisplayListData->displayListOps[i];
523113e0824d6bddf4376240681f9cf6a2deded9498John Reck
524113e0824d6bddf4376240681f9cf6a2deded9498John Reck#if DEBUG_DISPLAY_LIST
525113e0824d6bddf4376240681f9cf6a2deded9498John Reck            op->output(level + 1);
526113e0824d6bddf4376240681f9cf6a2deded9498John Reck#endif
527113e0824d6bddf4376240681f9cf6a2deded9498John Reck
528113e0824d6bddf4376240681f9cf6a2deded9498John Reck            logBuffer.writeCommand(level, op->name());
529d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            handler(op, saveCountOffset, properties().getClipToBounds());
530113e0824d6bddf4376240681f9cf6a2deded9498John Reck
531113e0824d6bddf4376240681f9cf6a2deded9498John Reck            if (CC_UNLIKELY(i == projectionReceiveIndex && mProjectedNodes.size() > 0)) {
532113e0824d6bddf4376240681f9cf6a2deded9498John Reck                iterateProjectedChildren(renderer, handler, level);
533113e0824d6bddf4376240681f9cf6a2deded9498John Reck            }
534113e0824d6bddf4376240681f9cf6a2deded9498John Reck        }
535113e0824d6bddf4376240681f9cf6a2deded9498John Reck
536113e0824d6bddf4376240681f9cf6a2deded9498John Reck        // for 3d root, draw children with positive z values
537113e0824d6bddf4376240681f9cf6a2deded9498John Reck        iterate3dChildren(zTranslatedNodes, kPositiveZChildren, renderer, handler);
538113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
539113e0824d6bddf4376240681f9cf6a2deded9498John Reck
540113e0824d6bddf4376240681f9cf6a2deded9498John Reck    DISPLAY_LIST_LOGD("%*sRestoreToCount %d", (level + 1) * 2, "", restoreTo);
541113e0824d6bddf4376240681f9cf6a2deded9498John Reck    handler(new (alloc) RestoreToCountOp(restoreTo),
542d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            PROPERTY_SAVECOUNT, properties().getClipToBounds());
543113e0824d6bddf4376240681f9cf6a2deded9498John Reck    renderer.setOverrideLayerAlpha(1.0f);
544113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
545113e0824d6bddf4376240681f9cf6a2deded9498John Reck
546113e0824d6bddf4376240681f9cf6a2deded9498John Reck} /* namespace uirenderer */
547113e0824d6bddf4376240681f9cf6a2deded9498John Reck} /* namespace android */
548