RenderNode.cpp revision f4198b713e43c0c0f9adac74203cf24c2a49b802
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
528de65a8e05285df52a1e6f0c1d5616dd233298a7John ReckRenderNode::RenderNode()
538de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck        : mDestroyed(false)
548de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck        , mNeedsPropertiesSync(false)
558de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck        , mNeedsDisplayListDataSync(false)
568de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck        , mDisplayListData(0)
578de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck        , mStagingDisplayListData(0) {
58113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
59113e0824d6bddf4376240681f9cf6a2deded9498John Reck
60113e0824d6bddf4376240681f9cf6a2deded9498John ReckRenderNode::~RenderNode() {
61113e0824d6bddf4376240681f9cf6a2deded9498John Reck    LOG_ALWAYS_FATAL_IF(mDestroyed, "Double destroyed DisplayList %p", this);
62113e0824d6bddf4376240681f9cf6a2deded9498John Reck
63113e0824d6bddf4376240681f9cf6a2deded9498John Reck    mDestroyed = true;
64113e0824d6bddf4376240681f9cf6a2deded9498John Reck    delete mDisplayListData;
658de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck    delete mStagingDisplayListData;
66113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
67113e0824d6bddf4376240681f9cf6a2deded9498John Reck
688de65a8e05285df52a1e6f0c1d5616dd233298a7John Reckvoid RenderNode::setStagingDisplayList(DisplayListData* data) {
698de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck    mNeedsDisplayListDataSync = true;
708de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck    delete mStagingDisplayListData;
718de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck    mStagingDisplayListData = data;
728de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck    if (mStagingDisplayListData) {
738de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck        Caches::getInstance().registerFunctors(mStagingDisplayListData->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
96f4198b713e43c0c0f9adac74203cf24c2a49b802John Reckvoid RenderNode::prepareTree(TreeInfo& info) {
97f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck    ATRACE_CALL();
98f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck
99f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck    prepareTreeImpl(info);
100f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck}
101f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck
102f4198b713e43c0c0f9adac74203cf24c2a49b802John Reckvoid RenderNode::prepareTreeImpl(TreeInfo& info) {
103f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck    pushStagingChanges(info);
104f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck    prepareSubTree(info, mDisplayListData);
105f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck}
106f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck
107f4198b713e43c0c0f9adac74203cf24c2a49b802John Reckvoid RenderNode::pushStagingChanges(TreeInfo& info) {
108d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (mNeedsPropertiesSync) {
109d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        mNeedsPropertiesSync = false;
110d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        mProperties = mStagingProperties;
111113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
1128de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck    if (mNeedsDisplayListDataSync) {
1138de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck        mNeedsDisplayListDataSync = false;
1148de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck        // Do a push pass on the old tree to handle freeing DisplayListData
1158de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck        // that are no longer used
116f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck        TreeInfo oldTreeInfo = {0};
117f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck        prepareSubTree(oldTreeInfo, mDisplayListData);
118f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck        // TODO: The damage for the old tree should be accounted for
1198de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck        delete mDisplayListData;
1208de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck        mDisplayListData = mStagingDisplayListData;
1218de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck        mStagingDisplayListData = 0;
1228de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck    }
1238de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck}
124113e0824d6bddf4376240681f9cf6a2deded9498John Reck
125f4198b713e43c0c0f9adac74203cf24c2a49b802John Reckvoid RenderNode::prepareSubTree(TreeInfo& info, DisplayListData* subtree) {
1268de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck    if (subtree) {
127f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck        if (!info.hasFunctors) {
128f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck            info.hasFunctors = subtree->functorCount;
129f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck        }
1308de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck        for (size_t i = 0; i < subtree->children().size(); i++) {
1318de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck            RenderNode* childNode = subtree->children()[i]->mDisplayList;
132f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck            childNode->prepareTreeImpl(info);
1335bf11bb98f5dbe278c257355d24c181237abd68cJohn Reck        }
134113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
135113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
136113e0824d6bddf4376240681f9cf6a2deded9498John Reck
137113e0824d6bddf4376240681f9cf6a2deded9498John Reck/*
138113e0824d6bddf4376240681f9cf6a2deded9498John Reck * For property operations, we pass a savecount of 0, since the operations aren't part of the
139113e0824d6bddf4376240681f9cf6a2deded9498John Reck * displaylist, and thus don't have to compensate for the record-time/playback-time discrepancy in
140d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck * base saveCount (i.e., how RestoreToCount uses saveCount + properties().getCount())
141113e0824d6bddf4376240681f9cf6a2deded9498John Reck */
142113e0824d6bddf4376240681f9cf6a2deded9498John Reck#define PROPERTY_SAVECOUNT 0
143113e0824d6bddf4376240681f9cf6a2deded9498John Reck
144113e0824d6bddf4376240681f9cf6a2deded9498John Recktemplate <class T>
145b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craikvoid RenderNode::setViewProperties(OpenGLRenderer& renderer, T& handler) {
146113e0824d6bddf4376240681f9cf6a2deded9498John Reck#if DEBUG_DISPLAY_LIST
147b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    properties().debugOutputProperties(handler.level() + 1);
148113e0824d6bddf4376240681f9cf6a2deded9498John Reck#endif
149d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (properties().getLeft() != 0 || properties().getTop() != 0) {
150d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        renderer.translate(properties().getLeft(), properties().getTop());
151113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
152d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (properties().getStaticMatrix()) {
153d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        renderer.concatMatrix(properties().getStaticMatrix());
154d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    } else if (properties().getAnimationMatrix()) {
155d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        renderer.concatMatrix(properties().getAnimationMatrix());
156113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
157d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (properties().getMatrixFlags() != 0) {
158d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        if (properties().getMatrixFlags() == TRANSLATION) {
159d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            renderer.translate(properties().getTranslationX(), properties().getTranslationY());
160113e0824d6bddf4376240681f9cf6a2deded9498John Reck        } else {
161d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            renderer.concatMatrix(*properties().getTransformMatrix());
162113e0824d6bddf4376240681f9cf6a2deded9498John Reck        }
163113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
164d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    bool clipToBoundsNeeded = properties().getCaching() ? false : properties().getClipToBounds();
165d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (properties().getAlpha() < 1) {
166d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        if (properties().getCaching()) {
167d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            renderer.setOverrideLayerAlpha(properties().getAlpha());
168d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        } else if (!properties().getHasOverlappingRendering()) {
169d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            renderer.scaleAlpha(properties().getAlpha());
170113e0824d6bddf4376240681f9cf6a2deded9498John Reck        } else {
171113e0824d6bddf4376240681f9cf6a2deded9498John Reck            // TODO: should be able to store the size of a DL at record time and not
172113e0824d6bddf4376240681f9cf6a2deded9498John Reck            // have to pass it into this call. In fact, this information might be in the
173113e0824d6bddf4376240681f9cf6a2deded9498John Reck            // location/size info that we store with the new native transform data.
174113e0824d6bddf4376240681f9cf6a2deded9498John Reck            int saveFlags = SkCanvas::kHasAlphaLayer_SaveFlag;
175113e0824d6bddf4376240681f9cf6a2deded9498John Reck            if (clipToBoundsNeeded) {
176113e0824d6bddf4376240681f9cf6a2deded9498John Reck                saveFlags |= SkCanvas::kClipToLayer_SaveFlag;
177113e0824d6bddf4376240681f9cf6a2deded9498John Reck                clipToBoundsNeeded = false; // clipping done by saveLayer
178113e0824d6bddf4376240681f9cf6a2deded9498John Reck            }
179113e0824d6bddf4376240681f9cf6a2deded9498John Reck
180113e0824d6bddf4376240681f9cf6a2deded9498John Reck            SaveLayerOp* op = new (handler.allocator()) SaveLayerOp(
1818c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik                    0, 0, properties().getWidth(), properties().getHeight(),
1828c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik                    properties().getAlpha() * 255, saveFlags);
183d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
184113e0824d6bddf4376240681f9cf6a2deded9498John Reck        }
185113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
186113e0824d6bddf4376240681f9cf6a2deded9498John Reck    if (clipToBoundsNeeded) {
1878c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik        ClipRectOp* op = new (handler.allocator()) ClipRectOp(
1888c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik                0, 0, properties().getWidth(), properties().getHeight(), SkRegion::kIntersect_Op);
189d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
190113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
1918c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik
1928c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik    if (CC_UNLIKELY(properties().hasClippingPath())) {
1938c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik        // TODO: optimize for round rect/circle clipping
1948c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik        const SkPath* path = properties().getClippingPath();
1958c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik        ClipPathOp* op = new (handler.allocator()) ClipPathOp(path, SkRegion::kIntersect_Op);
196d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
197113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
198113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
199113e0824d6bddf4376240681f9cf6a2deded9498John Reck
200113e0824d6bddf4376240681f9cf6a2deded9498John Reck/**
201113e0824d6bddf4376240681f9cf6a2deded9498John Reck * Apply property-based transformations to input matrix
202113e0824d6bddf4376240681f9cf6a2deded9498John Reck *
203113e0824d6bddf4376240681f9cf6a2deded9498John Reck * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
204113e0824d6bddf4376240681f9cf6a2deded9498John Reck * matrix computation instead of the Skia 3x3 matrix + camera hackery.
205113e0824d6bddf4376240681f9cf6a2deded9498John Reck */
206113e0824d6bddf4376240681f9cf6a2deded9498John Reckvoid RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) {
207d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (properties().getLeft() != 0 || properties().getTop() != 0) {
208d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        matrix.translate(properties().getLeft(), properties().getTop());
209113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
210d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (properties().getStaticMatrix()) {
211d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        mat4 stat(*properties().getStaticMatrix());
212113e0824d6bddf4376240681f9cf6a2deded9498John Reck        matrix.multiply(stat);
213d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    } else if (properties().getAnimationMatrix()) {
214d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        mat4 anim(*properties().getAnimationMatrix());
215113e0824d6bddf4376240681f9cf6a2deded9498John Reck        matrix.multiply(anim);
216113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
217d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (properties().getMatrixFlags() != 0) {
218d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        if (properties().getMatrixFlags() == TRANSLATION) {
219d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
220d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                    true3dTransform ? properties().getTranslationZ() : 0.0f);
221113e0824d6bddf4376240681f9cf6a2deded9498John Reck        } else {
222113e0824d6bddf4376240681f9cf6a2deded9498John Reck            if (!true3dTransform) {
223d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                matrix.multiply(*properties().getTransformMatrix());
224113e0824d6bddf4376240681f9cf6a2deded9498John Reck            } else {
225113e0824d6bddf4376240681f9cf6a2deded9498John Reck                mat4 true3dMat;
226113e0824d6bddf4376240681f9cf6a2deded9498John Reck                true3dMat.loadTranslate(
227d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                        properties().getPivotX() + properties().getTranslationX(),
228d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                        properties().getPivotY() + properties().getTranslationY(),
229d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                        properties().getTranslationZ());
230d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
231d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
232d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                true3dMat.rotate(properties().getRotation(), 0, 0, 1);
233d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
234d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
235113e0824d6bddf4376240681f9cf6a2deded9498John Reck
236113e0824d6bddf4376240681f9cf6a2deded9498John Reck                matrix.multiply(true3dMat);
237113e0824d6bddf4376240681f9cf6a2deded9498John Reck            }
238113e0824d6bddf4376240681f9cf6a2deded9498John Reck        }
239113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
240113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
241113e0824d6bddf4376240681f9cf6a2deded9498John Reck
242113e0824d6bddf4376240681f9cf6a2deded9498John Reck/**
243113e0824d6bddf4376240681f9cf6a2deded9498John Reck * Organizes the DisplayList hierarchy to prepare for background projection reordering.
244113e0824d6bddf4376240681f9cf6a2deded9498John Reck *
245113e0824d6bddf4376240681f9cf6a2deded9498John Reck * This should be called before a call to defer() or drawDisplayList()
246113e0824d6bddf4376240681f9cf6a2deded9498John Reck *
247113e0824d6bddf4376240681f9cf6a2deded9498John Reck * Each DisplayList that serves as a 3d root builds its list of composited children,
248113e0824d6bddf4376240681f9cf6a2deded9498John Reck * which are flagged to not draw in the standard draw loop.
249113e0824d6bddf4376240681f9cf6a2deded9498John Reck */
250113e0824d6bddf4376240681f9cf6a2deded9498John Reckvoid RenderNode::computeOrdering() {
251113e0824d6bddf4376240681f9cf6a2deded9498John Reck    ATRACE_CALL();
252113e0824d6bddf4376240681f9cf6a2deded9498John Reck    mProjectedNodes.clear();
253113e0824d6bddf4376240681f9cf6a2deded9498John Reck
254113e0824d6bddf4376240681f9cf6a2deded9498John Reck    // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that
255113e0824d6bddf4376240681f9cf6a2deded9498John Reck    // transform properties are applied correctly to top level children
256113e0824d6bddf4376240681f9cf6a2deded9498John Reck    if (mDisplayListData == NULL) return;
257087bc0c14bdccf7c258dce0cdef46a69a839b427John Reck    for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
258087bc0c14bdccf7c258dce0cdef46a69a839b427John Reck        DrawDisplayListOp* childOp = mDisplayListData->children()[i];
259113e0824d6bddf4376240681f9cf6a2deded9498John Reck        childOp->mDisplayList->computeOrderingImpl(childOp,
260113e0824d6bddf4376240681f9cf6a2deded9498John Reck                &mProjectedNodes, &mat4::identity());
261113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
262113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
263113e0824d6bddf4376240681f9cf6a2deded9498John Reck
264113e0824d6bddf4376240681f9cf6a2deded9498John Reckvoid RenderNode::computeOrderingImpl(
265113e0824d6bddf4376240681f9cf6a2deded9498John Reck        DrawDisplayListOp* opState,
266113e0824d6bddf4376240681f9cf6a2deded9498John Reck        Vector<DrawDisplayListOp*>* compositedChildrenOfProjectionSurface,
267113e0824d6bddf4376240681f9cf6a2deded9498John Reck        const mat4* transformFromProjectionSurface) {
268113e0824d6bddf4376240681f9cf6a2deded9498John Reck    mProjectedNodes.clear();
269113e0824d6bddf4376240681f9cf6a2deded9498John Reck    if (mDisplayListData == NULL || mDisplayListData->isEmpty()) return;
270113e0824d6bddf4376240681f9cf6a2deded9498John Reck
271113e0824d6bddf4376240681f9cf6a2deded9498John Reck    // TODO: should avoid this calculation in most cases
272113e0824d6bddf4376240681f9cf6a2deded9498John Reck    // TODO: just calculate single matrix, down to all leaf composited elements
273113e0824d6bddf4376240681f9cf6a2deded9498John Reck    Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface);
274113e0824d6bddf4376240681f9cf6a2deded9498John Reck    localTransformFromProjectionSurface.multiply(opState->mTransformFromParent);
275113e0824d6bddf4376240681f9cf6a2deded9498John Reck
276d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (properties().getProjectBackwards()) {
277113e0824d6bddf4376240681f9cf6a2deded9498John Reck        // composited projectee, flag for out of order draw, save matrix, and store in proj surface
278113e0824d6bddf4376240681f9cf6a2deded9498John Reck        opState->mSkipInOrderDraw = true;
279113e0824d6bddf4376240681f9cf6a2deded9498John Reck        opState->mTransformFromCompositingAncestor.load(localTransformFromProjectionSurface);
280113e0824d6bddf4376240681f9cf6a2deded9498John Reck        compositedChildrenOfProjectionSurface->add(opState);
281113e0824d6bddf4376240681f9cf6a2deded9498John Reck    } else {
282113e0824d6bddf4376240681f9cf6a2deded9498John Reck        // standard in order draw
283113e0824d6bddf4376240681f9cf6a2deded9498John Reck        opState->mSkipInOrderDraw = false;
284113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
285113e0824d6bddf4376240681f9cf6a2deded9498John Reck
286087bc0c14bdccf7c258dce0cdef46a69a839b427John Reck    if (mDisplayListData->children().size() > 0) {
287113e0824d6bddf4376240681f9cf6a2deded9498John Reck        const bool isProjectionReceiver = mDisplayListData->projectionReceiveIndex >= 0;
288113e0824d6bddf4376240681f9cf6a2deded9498John Reck        bool haveAppliedPropertiesToProjection = false;
289087bc0c14bdccf7c258dce0cdef46a69a839b427John Reck        for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
290087bc0c14bdccf7c258dce0cdef46a69a839b427John Reck            DrawDisplayListOp* childOp = mDisplayListData->children()[i];
291113e0824d6bddf4376240681f9cf6a2deded9498John Reck            RenderNode* child = childOp->mDisplayList;
292113e0824d6bddf4376240681f9cf6a2deded9498John Reck
293113e0824d6bddf4376240681f9cf6a2deded9498John Reck            Vector<DrawDisplayListOp*>* projectionChildren = NULL;
294113e0824d6bddf4376240681f9cf6a2deded9498John Reck            const mat4* projectionTransform = NULL;
295d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            if (isProjectionReceiver && !child->properties().getProjectBackwards()) {
296113e0824d6bddf4376240681f9cf6a2deded9498John Reck                // if receiving projections, collect projecting descendent
297113e0824d6bddf4376240681f9cf6a2deded9498John Reck
298113e0824d6bddf4376240681f9cf6a2deded9498John Reck                // Note that if a direct descendent is projecting backwards, we pass it's
299113e0824d6bddf4376240681f9cf6a2deded9498John Reck                // grandparent projection collection, since it shouldn't project onto it's
300113e0824d6bddf4376240681f9cf6a2deded9498John Reck                // parent, where it will already be drawing.
301113e0824d6bddf4376240681f9cf6a2deded9498John Reck                projectionChildren = &mProjectedNodes;
302113e0824d6bddf4376240681f9cf6a2deded9498John Reck                projectionTransform = &mat4::identity();
303113e0824d6bddf4376240681f9cf6a2deded9498John Reck            } else {
304113e0824d6bddf4376240681f9cf6a2deded9498John Reck                if (!haveAppliedPropertiesToProjection) {
305113e0824d6bddf4376240681f9cf6a2deded9498John Reck                    applyViewPropertyTransforms(localTransformFromProjectionSurface);
306113e0824d6bddf4376240681f9cf6a2deded9498John Reck                    haveAppliedPropertiesToProjection = true;
307113e0824d6bddf4376240681f9cf6a2deded9498John Reck                }
308113e0824d6bddf4376240681f9cf6a2deded9498John Reck                projectionChildren = compositedChildrenOfProjectionSurface;
309113e0824d6bddf4376240681f9cf6a2deded9498John Reck                projectionTransform = &localTransformFromProjectionSurface;
310113e0824d6bddf4376240681f9cf6a2deded9498John Reck            }
311113e0824d6bddf4376240681f9cf6a2deded9498John Reck            child->computeOrderingImpl(childOp, projectionChildren, projectionTransform);
312113e0824d6bddf4376240681f9cf6a2deded9498John Reck        }
313113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
314113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
315113e0824d6bddf4376240681f9cf6a2deded9498John Reck
316113e0824d6bddf4376240681f9cf6a2deded9498John Reckclass DeferOperationHandler {
317113e0824d6bddf4376240681f9cf6a2deded9498John Reckpublic:
318113e0824d6bddf4376240681f9cf6a2deded9498John Reck    DeferOperationHandler(DeferStateStruct& deferStruct, int level)
319113e0824d6bddf4376240681f9cf6a2deded9498John Reck        : mDeferStruct(deferStruct), mLevel(level) {}
320113e0824d6bddf4376240681f9cf6a2deded9498John Reck    inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
321113e0824d6bddf4376240681f9cf6a2deded9498John Reck        operation->defer(mDeferStruct, saveCount, mLevel, clipToBounds);
322113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
323113e0824d6bddf4376240681f9cf6a2deded9498John Reck    inline LinearAllocator& allocator() { return *(mDeferStruct.mAllocator); }
324b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    inline void startMark(const char* name) {} // do nothing
325b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    inline void endMark() {}
326b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    inline int level() { return mLevel; }
327b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    inline int replayFlags() { return mDeferStruct.mReplayFlags; }
328113e0824d6bddf4376240681f9cf6a2deded9498John Reck
329113e0824d6bddf4376240681f9cf6a2deded9498John Reckprivate:
330113e0824d6bddf4376240681f9cf6a2deded9498John Reck    DeferStateStruct& mDeferStruct;
331113e0824d6bddf4376240681f9cf6a2deded9498John Reck    const int mLevel;
332113e0824d6bddf4376240681f9cf6a2deded9498John Reck};
333113e0824d6bddf4376240681f9cf6a2deded9498John Reck
334b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craikvoid RenderNode::deferNodeTree(DeferStateStruct& deferStruct) {
335b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    DeferOperationHandler handler(deferStruct, 0);
336b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    if (properties().getTranslationZ() > 0.0f) issueDrawShadowOperation(Matrix4::identity(), handler);
337b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler);
338b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik}
339b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik
340b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craikvoid RenderNode::deferNodeInParent(DeferStateStruct& deferStruct, const int level) {
341113e0824d6bddf4376240681f9cf6a2deded9498John Reck    DeferOperationHandler handler(deferStruct, level);
342b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler);
343113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
344113e0824d6bddf4376240681f9cf6a2deded9498John Reck
345113e0824d6bddf4376240681f9cf6a2deded9498John Reckclass ReplayOperationHandler {
346113e0824d6bddf4376240681f9cf6a2deded9498John Reckpublic:
347113e0824d6bddf4376240681f9cf6a2deded9498John Reck    ReplayOperationHandler(ReplayStateStruct& replayStruct, int level)
348113e0824d6bddf4376240681f9cf6a2deded9498John Reck        : mReplayStruct(replayStruct), mLevel(level) {}
349113e0824d6bddf4376240681f9cf6a2deded9498John Reck    inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
350113e0824d6bddf4376240681f9cf6a2deded9498John Reck#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
351d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        properties().getReplayStruct().mRenderer.eventMark(operation->name());
352113e0824d6bddf4376240681f9cf6a2deded9498John Reck#endif
353113e0824d6bddf4376240681f9cf6a2deded9498John Reck        operation->replay(mReplayStruct, saveCount, mLevel, clipToBounds);
354113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
355113e0824d6bddf4376240681f9cf6a2deded9498John Reck    inline LinearAllocator& allocator() { return *(mReplayStruct.mAllocator); }
356b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    inline void startMark(const char* name) {
357b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik        mReplayStruct.mRenderer.startMark(name);
358b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    }
359b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    inline void endMark() {
360b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik        mReplayStruct.mRenderer.endMark();
361b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik        DISPLAY_LIST_LOGD("%*sDone (%p, %s), returning %d", level * 2, "", this, mName.string(),
362b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik                mReplayStruct.mDrawGlStatus);
363b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    }
364b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    inline int level() { return mLevel; }
365b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    inline int replayFlags() { return mReplayStruct.mReplayFlags; }
366113e0824d6bddf4376240681f9cf6a2deded9498John Reck
367113e0824d6bddf4376240681f9cf6a2deded9498John Reckprivate:
368113e0824d6bddf4376240681f9cf6a2deded9498John Reck    ReplayStateStruct& mReplayStruct;
369113e0824d6bddf4376240681f9cf6a2deded9498John Reck    const int mLevel;
370113e0824d6bddf4376240681f9cf6a2deded9498John Reck};
371113e0824d6bddf4376240681f9cf6a2deded9498John Reck
372b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craikvoid RenderNode::replayNodeTree(ReplayStateStruct& replayStruct) {
373b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    ReplayOperationHandler handler(replayStruct, 0);
374b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    if (properties().getTranslationZ() > 0.0f) issueDrawShadowOperation(Matrix4::identity(), handler);
375b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler);
376b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik}
377113e0824d6bddf4376240681f9cf6a2deded9498John Reck
378b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craikvoid RenderNode::replayNodeInParent(ReplayStateStruct& replayStruct, const int level) {
379b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    ReplayOperationHandler handler(replayStruct, level);
380b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler);
381113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
382113e0824d6bddf4376240681f9cf6a2deded9498John Reck
383113e0824d6bddf4376240681f9cf6a2deded9498John Reckvoid RenderNode::buildZSortedChildList(Vector<ZDrawDisplayListOpPair>& zTranslatedNodes) {
384087bc0c14bdccf7c258dce0cdef46a69a839b427John Reck    if (mDisplayListData == NULL || mDisplayListData->children().size() == 0) return;
385113e0824d6bddf4376240681f9cf6a2deded9498John Reck
386087bc0c14bdccf7c258dce0cdef46a69a839b427John Reck    for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
387087bc0c14bdccf7c258dce0cdef46a69a839b427John Reck        DrawDisplayListOp* childOp = mDisplayListData->children()[i];
388113e0824d6bddf4376240681f9cf6a2deded9498John Reck        RenderNode* child = childOp->mDisplayList;
389d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        float childZ = child->properties().getTranslationZ();
390113e0824d6bddf4376240681f9cf6a2deded9498John Reck
391113e0824d6bddf4376240681f9cf6a2deded9498John Reck        if (childZ != 0.0f) {
392113e0824d6bddf4376240681f9cf6a2deded9498John Reck            zTranslatedNodes.add(ZDrawDisplayListOpPair(childZ, childOp));
393113e0824d6bddf4376240681f9cf6a2deded9498John Reck            childOp->mSkipInOrderDraw = true;
394d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        } else if (!child->properties().getProjectBackwards()) {
395113e0824d6bddf4376240681f9cf6a2deded9498John Reck            // regular, in order drawing DisplayList
396113e0824d6bddf4376240681f9cf6a2deded9498John Reck            childOp->mSkipInOrderDraw = false;
397113e0824d6bddf4376240681f9cf6a2deded9498John Reck        }
398113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
399113e0824d6bddf4376240681f9cf6a2deded9498John Reck
400113e0824d6bddf4376240681f9cf6a2deded9498John Reck    // Z sort 3d children (stable-ness makes z compare fall back to standard drawing order)
401113e0824d6bddf4376240681f9cf6a2deded9498John Reck    std::stable_sort(zTranslatedNodes.begin(), zTranslatedNodes.end());
402113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
403113e0824d6bddf4376240681f9cf6a2deded9498John Reck
404b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craiktemplate <class T>
405b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craikvoid RenderNode::issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler) {
406b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    if (properties().getAlpha() <= 0.0f) return;
407b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik
408b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    mat4 shadowMatrixXY(transformFromParent);
409b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    applyViewPropertyTransforms(shadowMatrixXY);
410b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik
411b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    // Z matrix needs actual 3d transformation, so mapped z values will be correct
412b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    mat4 shadowMatrixZ(transformFromParent);
413b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    applyViewPropertyTransforms(shadowMatrixZ, true);
414b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik
415b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    const SkPath* outlinePath = properties().getOutline().getPath();
416b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    const RevealClip& revealClip = properties().getRevealClip();
417b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    const SkPath* revealClipPath = revealClip.hasConvexClip()
418b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik            ?  revealClip.getPath() : NULL; // only pass the reveal clip's path if it's convex
419b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik
420b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    /**
421b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik     * The drawing area of the caster is always the same as the its perimeter (which
422b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik     * the shadow system uses) *except* in the inverse clip case. Inform the shadow
423b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik     * system that the caster's drawing area (as opposed to its perimeter) has been
424b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik     * clipped, so that it knows the caster can't be opaque.
425b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik     */
426b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    bool casterUnclipped = !revealClip.willClip() || revealClip.hasConvexClip();
427b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik
428b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    DisplayListOp* shadowOp  = new (handler.allocator()) DrawShadowOp(
429b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik            shadowMatrixXY, shadowMatrixZ,
430b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik            properties().getAlpha(), casterUnclipped,
431b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik            properties().getWidth(), properties().getHeight(),
432b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik            outlinePath, revealClipPath);
433b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    handler(shadowOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
434b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik}
435b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik
436113e0824d6bddf4376240681f9cf6a2deded9498John Reck#define SHADOW_DELTA 0.1f
437113e0824d6bddf4376240681f9cf6a2deded9498John Reck
438113e0824d6bddf4376240681f9cf6a2deded9498John Recktemplate <class T>
439b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craikvoid RenderNode::issueOperationsOf3dChildren(const Vector<ZDrawDisplayListOpPair>& zTranslatedNodes,
440113e0824d6bddf4376240681f9cf6a2deded9498John Reck        ChildrenSelectMode mode, OpenGLRenderer& renderer, T& handler) {
441113e0824d6bddf4376240681f9cf6a2deded9498John Reck    const int size = zTranslatedNodes.size();
442113e0824d6bddf4376240681f9cf6a2deded9498John Reck    if (size == 0
443113e0824d6bddf4376240681f9cf6a2deded9498John Reck            || (mode == kNegativeZChildren && zTranslatedNodes[0].key > 0.0f)
444113e0824d6bddf4376240681f9cf6a2deded9498John Reck            || (mode == kPositiveZChildren && zTranslatedNodes[size - 1].key < 0.0f)) {
445113e0824d6bddf4376240681f9cf6a2deded9498John Reck        // no 3d children to draw
446113e0824d6bddf4376240681f9cf6a2deded9498John Reck        return;
447113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
448113e0824d6bddf4376240681f9cf6a2deded9498John Reck
449113e0824d6bddf4376240681f9cf6a2deded9498John Reck    /**
450113e0824d6bddf4376240681f9cf6a2deded9498John Reck     * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
451113e0824d6bddf4376240681f9cf6a2deded9498John Reck     * with very similar Z heights to draw together.
452113e0824d6bddf4376240681f9cf6a2deded9498John Reck     *
453113e0824d6bddf4376240681f9cf6a2deded9498John Reck     * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
454113e0824d6bddf4376240681f9cf6a2deded9498John Reck     * underneath both, and neither's shadow is drawn on top of the other.
455113e0824d6bddf4376240681f9cf6a2deded9498John Reck     */
456113e0824d6bddf4376240681f9cf6a2deded9498John Reck    const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
457113e0824d6bddf4376240681f9cf6a2deded9498John Reck    size_t drawIndex, shadowIndex, endIndex;
458113e0824d6bddf4376240681f9cf6a2deded9498John Reck    if (mode == kNegativeZChildren) {
459113e0824d6bddf4376240681f9cf6a2deded9498John Reck        drawIndex = 0;
460113e0824d6bddf4376240681f9cf6a2deded9498John Reck        endIndex = nonNegativeIndex;
461113e0824d6bddf4376240681f9cf6a2deded9498John Reck        shadowIndex = endIndex; // draw no shadows
462113e0824d6bddf4376240681f9cf6a2deded9498John Reck    } else {
463113e0824d6bddf4376240681f9cf6a2deded9498John Reck        drawIndex = nonNegativeIndex;
464113e0824d6bddf4376240681f9cf6a2deded9498John Reck        endIndex = size;
465113e0824d6bddf4376240681f9cf6a2deded9498John Reck        shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
466113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
467113e0824d6bddf4376240681f9cf6a2deded9498John Reck    float lastCasterZ = 0.0f;
468113e0824d6bddf4376240681f9cf6a2deded9498John Reck    while (shadowIndex < endIndex || drawIndex < endIndex) {
469113e0824d6bddf4376240681f9cf6a2deded9498John Reck        if (shadowIndex < endIndex) {
470113e0824d6bddf4376240681f9cf6a2deded9498John Reck            DrawDisplayListOp* casterOp = zTranslatedNodes[shadowIndex].value;
471113e0824d6bddf4376240681f9cf6a2deded9498John Reck            RenderNode* caster = casterOp->mDisplayList;
472113e0824d6bddf4376240681f9cf6a2deded9498John Reck            const float casterZ = zTranslatedNodes[shadowIndex].key;
473113e0824d6bddf4376240681f9cf6a2deded9498John Reck            // attempt to render the shadow if the caster about to be drawn is its caster,
474113e0824d6bddf4376240681f9cf6a2deded9498John Reck            // OR if its caster's Z value is similar to the previous potential caster
475113e0824d6bddf4376240681f9cf6a2deded9498John Reck            if (shadowIndex == drawIndex || casterZ - lastCasterZ < SHADOW_DELTA) {
476b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik                caster->issueDrawShadowOperation(casterOp->mTransformFromParent, handler);
477113e0824d6bddf4376240681f9cf6a2deded9498John Reck
478113e0824d6bddf4376240681f9cf6a2deded9498John Reck                lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
479113e0824d6bddf4376240681f9cf6a2deded9498John Reck                shadowIndex++;
480113e0824d6bddf4376240681f9cf6a2deded9498John Reck                continue;
481113e0824d6bddf4376240681f9cf6a2deded9498John Reck            }
482113e0824d6bddf4376240681f9cf6a2deded9498John Reck        }
483113e0824d6bddf4376240681f9cf6a2deded9498John Reck
484113e0824d6bddf4376240681f9cf6a2deded9498John Reck        // only the actual child DL draw needs to be in save/restore,
485113e0824d6bddf4376240681f9cf6a2deded9498John Reck        // since it modifies the renderer's matrix
486113e0824d6bddf4376240681f9cf6a2deded9498John Reck        int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
487113e0824d6bddf4376240681f9cf6a2deded9498John Reck
488113e0824d6bddf4376240681f9cf6a2deded9498John Reck        DrawDisplayListOp* childOp = zTranslatedNodes[drawIndex].value;
489113e0824d6bddf4376240681f9cf6a2deded9498John Reck        RenderNode* child = childOp->mDisplayList;
490113e0824d6bddf4376240681f9cf6a2deded9498John Reck
491113e0824d6bddf4376240681f9cf6a2deded9498John Reck        renderer.concatMatrix(childOp->mTransformFromParent);
492113e0824d6bddf4376240681f9cf6a2deded9498John Reck        childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
493d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
494113e0824d6bddf4376240681f9cf6a2deded9498John Reck        childOp->mSkipInOrderDraw = true;
495113e0824d6bddf4376240681f9cf6a2deded9498John Reck
496113e0824d6bddf4376240681f9cf6a2deded9498John Reck        renderer.restoreToCount(restoreTo);
497113e0824d6bddf4376240681f9cf6a2deded9498John Reck        drawIndex++;
498113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
499113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
500113e0824d6bddf4376240681f9cf6a2deded9498John Reck
501113e0824d6bddf4376240681f9cf6a2deded9498John Recktemplate <class T>
502b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craikvoid RenderNode::issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler) {
503113e0824d6bddf4376240681f9cf6a2deded9498John Reck    for (size_t i = 0; i < mProjectedNodes.size(); i++) {
504113e0824d6bddf4376240681f9cf6a2deded9498John Reck        DrawDisplayListOp* childOp = mProjectedNodes[i];
505113e0824d6bddf4376240681f9cf6a2deded9498John Reck
506113e0824d6bddf4376240681f9cf6a2deded9498John Reck        // matrix save, concat, and restore can be done safely without allocating operations
507113e0824d6bddf4376240681f9cf6a2deded9498John Reck        int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
508113e0824d6bddf4376240681f9cf6a2deded9498John Reck        renderer.concatMatrix(childOp->mTransformFromCompositingAncestor);
509113e0824d6bddf4376240681f9cf6a2deded9498John Reck        childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
510d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
511113e0824d6bddf4376240681f9cf6a2deded9498John Reck        childOp->mSkipInOrderDraw = true;
512113e0824d6bddf4376240681f9cf6a2deded9498John Reck        renderer.restoreToCount(restoreTo);
513113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
514113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
515113e0824d6bddf4376240681f9cf6a2deded9498John Reck
516113e0824d6bddf4376240681f9cf6a2deded9498John Reck/**
517113e0824d6bddf4376240681f9cf6a2deded9498John Reck * This function serves both defer and replay modes, and will organize the displayList's component
518113e0824d6bddf4376240681f9cf6a2deded9498John Reck * operations for a single frame:
519113e0824d6bddf4376240681f9cf6a2deded9498John Reck *
520113e0824d6bddf4376240681f9cf6a2deded9498John Reck * Every 'simple' state operation that affects just the matrix and alpha (or other factors of
521113e0824d6bddf4376240681f9cf6a2deded9498John Reck * DeferredDisplayState) may be issued directly to the renderer, but complex operations (with custom
522113e0824d6bddf4376240681f9cf6a2deded9498John Reck * defer logic) and operations in displayListOps are issued through the 'handler' which handles the
523113e0824d6bddf4376240681f9cf6a2deded9498John Reck * defer vs replay logic, per operation
524113e0824d6bddf4376240681f9cf6a2deded9498John Reck */
525113e0824d6bddf4376240681f9cf6a2deded9498John Recktemplate <class T>
526b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craikvoid RenderNode::issueOperations(OpenGLRenderer& renderer, T& handler) {
527b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    const int level = handler.level();
528113e0824d6bddf4376240681f9cf6a2deded9498John Reck    if (CC_UNLIKELY(mDestroyed)) { // temporary debug logging
529113e0824d6bddf4376240681f9cf6a2deded9498John Reck        ALOGW("Error: %s is drawing after destruction", mName.string());
530113e0824d6bddf4376240681f9cf6a2deded9498John Reck        CRASH();
531113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
532d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (mDisplayListData->isEmpty() || properties().getAlpha() <= 0) {
533113e0824d6bddf4376240681f9cf6a2deded9498John Reck        DISPLAY_LIST_LOGD("%*sEmpty display list (%p, %s)", level * 2, "", this, mName.string());
534113e0824d6bddf4376240681f9cf6a2deded9498John Reck        return;
535113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
536113e0824d6bddf4376240681f9cf6a2deded9498John Reck
537b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    handler.startMark(mName.string());
538b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik
539113e0824d6bddf4376240681f9cf6a2deded9498John Reck#if DEBUG_DISPLAY_LIST
540113e0824d6bddf4376240681f9cf6a2deded9498John Reck    Rect* clipRect = renderer.getClipRect();
541113e0824d6bddf4376240681f9cf6a2deded9498John Reck    DISPLAY_LIST_LOGD("%*sStart display list (%p, %s), clipRect: %.0f, %.0f, %.0f, %.0f",
542113e0824d6bddf4376240681f9cf6a2deded9498John Reck            level * 2, "", this, mName.string(), clipRect->left, clipRect->top,
543113e0824d6bddf4376240681f9cf6a2deded9498John Reck            clipRect->right, clipRect->bottom);
544113e0824d6bddf4376240681f9cf6a2deded9498John Reck#endif
545113e0824d6bddf4376240681f9cf6a2deded9498John Reck
546113e0824d6bddf4376240681f9cf6a2deded9498John Reck    LinearAllocator& alloc = handler.allocator();
547113e0824d6bddf4376240681f9cf6a2deded9498John Reck    int restoreTo = renderer.getSaveCount();
548113e0824d6bddf4376240681f9cf6a2deded9498John Reck    handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
549d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            PROPERTY_SAVECOUNT, properties().getClipToBounds());
550113e0824d6bddf4376240681f9cf6a2deded9498John Reck
551113e0824d6bddf4376240681f9cf6a2deded9498John Reck    DISPLAY_LIST_LOGD("%*sSave %d %d", (level + 1) * 2, "",
552113e0824d6bddf4376240681f9cf6a2deded9498John Reck            SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag, restoreTo);
553113e0824d6bddf4376240681f9cf6a2deded9498John Reck
554b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    setViewProperties<T>(renderer, handler);
555113e0824d6bddf4376240681f9cf6a2deded9498John Reck
5568c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik    bool quickRejected = properties().getClipToBounds()
5578c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik            && renderer.quickRejectConservative(0, 0, properties().getWidth(), properties().getHeight());
558113e0824d6bddf4376240681f9cf6a2deded9498John Reck    if (!quickRejected) {
559113e0824d6bddf4376240681f9cf6a2deded9498John Reck        Vector<ZDrawDisplayListOpPair> zTranslatedNodes;
560113e0824d6bddf4376240681f9cf6a2deded9498John Reck        buildZSortedChildList(zTranslatedNodes);
561113e0824d6bddf4376240681f9cf6a2deded9498John Reck
562113e0824d6bddf4376240681f9cf6a2deded9498John Reck        // for 3d root, draw children with negative z values
563b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik        issueOperationsOf3dChildren(zTranslatedNodes, kNegativeZChildren, renderer, handler);
564113e0824d6bddf4376240681f9cf6a2deded9498John Reck
565113e0824d6bddf4376240681f9cf6a2deded9498John Reck        DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance();
566113e0824d6bddf4376240681f9cf6a2deded9498John Reck        const int saveCountOffset = renderer.getSaveCount() - 1;
567113e0824d6bddf4376240681f9cf6a2deded9498John Reck        const int projectionReceiveIndex = mDisplayListData->projectionReceiveIndex;
568113e0824d6bddf4376240681f9cf6a2deded9498John Reck        for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) {
569113e0824d6bddf4376240681f9cf6a2deded9498John Reck            DisplayListOp *op = mDisplayListData->displayListOps[i];
570113e0824d6bddf4376240681f9cf6a2deded9498John Reck
571113e0824d6bddf4376240681f9cf6a2deded9498John Reck#if DEBUG_DISPLAY_LIST
572113e0824d6bddf4376240681f9cf6a2deded9498John Reck            op->output(level + 1);
573113e0824d6bddf4376240681f9cf6a2deded9498John Reck#endif
574113e0824d6bddf4376240681f9cf6a2deded9498John Reck            logBuffer.writeCommand(level, op->name());
575d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            handler(op, saveCountOffset, properties().getClipToBounds());
576113e0824d6bddf4376240681f9cf6a2deded9498John Reck
577113e0824d6bddf4376240681f9cf6a2deded9498John Reck            if (CC_UNLIKELY(i == projectionReceiveIndex && mProjectedNodes.size() > 0)) {
578b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik                issueOperationsOfProjectedChildren(renderer, handler);
579113e0824d6bddf4376240681f9cf6a2deded9498John Reck            }
580113e0824d6bddf4376240681f9cf6a2deded9498John Reck        }
581113e0824d6bddf4376240681f9cf6a2deded9498John Reck
582113e0824d6bddf4376240681f9cf6a2deded9498John Reck        // for 3d root, draw children with positive z values
583b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik        issueOperationsOf3dChildren(zTranslatedNodes, kPositiveZChildren, renderer, handler);
584113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
585113e0824d6bddf4376240681f9cf6a2deded9498John Reck
586113e0824d6bddf4376240681f9cf6a2deded9498John Reck    DISPLAY_LIST_LOGD("%*sRestoreToCount %d", (level + 1) * 2, "", restoreTo);
587113e0824d6bddf4376240681f9cf6a2deded9498John Reck    handler(new (alloc) RestoreToCountOp(restoreTo),
588d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            PROPERTY_SAVECOUNT, properties().getClipToBounds());
589113e0824d6bddf4376240681f9cf6a2deded9498John Reck    renderer.setOverrideLayerAlpha(1.0f);
590b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik
591b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    handler.endMark();
592113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
593113e0824d6bddf4376240681f9cf6a2deded9498John Reck
594113e0824d6bddf4376240681f9cf6a2deded9498John Reck} /* namespace uirenderer */
595113e0824d6bddf4376240681f9cf6a2deded9498John Reck} /* namespace android */
596