RenderNode.cpp revision 860d155f866cc15a725e7ce03763280987f24901
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()
53143912fef8eff58146705849a0ba441ab6163409Chris Craik        : mNeedsPropertiesSync(false)
548de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck        , mNeedsDisplayListDataSync(false)
558de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck        , mDisplayListData(0)
568de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck        , mStagingDisplayListData(0) {
57113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
58113e0824d6bddf4376240681f9cf6a2deded9498John Reck
59113e0824d6bddf4376240681f9cf6a2deded9498John ReckRenderNode::~RenderNode() {
60113e0824d6bddf4376240681f9cf6a2deded9498John Reck    delete mDisplayListData;
618de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck    delete mStagingDisplayListData;
62113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
63113e0824d6bddf4376240681f9cf6a2deded9498John Reck
648de65a8e05285df52a1e6f0c1d5616dd233298a7John Reckvoid RenderNode::setStagingDisplayList(DisplayListData* data) {
658de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck    mNeedsDisplayListDataSync = true;
668de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck    delete mStagingDisplayListData;
678de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck    mStagingDisplayListData = data;
688de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck    if (mStagingDisplayListData) {
698de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck        Caches::getInstance().registerFunctors(mStagingDisplayListData->functorCount);
70113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
71113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
72113e0824d6bddf4376240681f9cf6a2deded9498John Reck
73113e0824d6bddf4376240681f9cf6a2deded9498John Reck/**
74113e0824d6bddf4376240681f9cf6a2deded9498John Reck * This function is a simplified version of replay(), where we simply retrieve and log the
75113e0824d6bddf4376240681f9cf6a2deded9498John Reck * display list. This function should remain in sync with the replay() function.
76113e0824d6bddf4376240681f9cf6a2deded9498John Reck */
77113e0824d6bddf4376240681f9cf6a2deded9498John Reckvoid RenderNode::output(uint32_t level) {
78113e0824d6bddf4376240681f9cf6a2deded9498John Reck    ALOGD("%*sStart display list (%p, %s, render=%d)", (level - 1) * 2, "", this,
79113e0824d6bddf4376240681f9cf6a2deded9498John Reck            mName.string(), isRenderable());
80113e0824d6bddf4376240681f9cf6a2deded9498John Reck    ALOGD("%*s%s %d", level * 2, "", "Save",
81113e0824d6bddf4376240681f9cf6a2deded9498John Reck            SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
82113e0824d6bddf4376240681f9cf6a2deded9498John Reck
83d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    properties().debugOutputProperties(level);
84113e0824d6bddf4376240681f9cf6a2deded9498John Reck    int flags = DisplayListOp::kOpLogFlag_Recurse;
85113e0824d6bddf4376240681f9cf6a2deded9498John Reck    for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) {
86113e0824d6bddf4376240681f9cf6a2deded9498John Reck        mDisplayListData->displayListOps[i]->output(level, flags);
87113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
88113e0824d6bddf4376240681f9cf6a2deded9498John Reck
89113e0824d6bddf4376240681f9cf6a2deded9498John Reck    ALOGD("%*sDone (%p, %s)", (level - 1) * 2, "", this, mName.string());
90113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
91113e0824d6bddf4376240681f9cf6a2deded9498John Reck
92f4198b713e43c0c0f9adac74203cf24c2a49b802John Reckvoid RenderNode::prepareTree(TreeInfo& info) {
93f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck    ATRACE_CALL();
94f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck
95f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck    prepareTreeImpl(info);
96f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck}
97f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck
98f4198b713e43c0c0f9adac74203cf24c2a49b802John Reckvoid RenderNode::prepareTreeImpl(TreeInfo& info) {
99f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck    pushStagingChanges(info);
100f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck    prepareSubTree(info, mDisplayListData);
101f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck}
102f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck
103f4198b713e43c0c0f9adac74203cf24c2a49b802John Reckvoid RenderNode::pushStagingChanges(TreeInfo& info) {
104d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (mNeedsPropertiesSync) {
105d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        mNeedsPropertiesSync = false;
106d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        mProperties = mStagingProperties;
107113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
1088de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck    if (mNeedsDisplayListDataSync) {
1098de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck        mNeedsDisplayListDataSync = false;
1108de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck        // Do a push pass on the old tree to handle freeing DisplayListData
1118de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck        // that are no longer used
112860d155f866cc15a725e7ce03763280987f24901John Reck        TreeInfo oldTreeInfo;
113f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck        prepareSubTree(oldTreeInfo, mDisplayListData);
114f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck        // TODO: The damage for the old tree should be accounted for
1158de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck        delete mDisplayListData;
1168de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck        mDisplayListData = mStagingDisplayListData;
1178de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck        mStagingDisplayListData = 0;
1188de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck    }
1198de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck}
120113e0824d6bddf4376240681f9cf6a2deded9498John Reck
121f4198b713e43c0c0f9adac74203cf24c2a49b802John Reckvoid RenderNode::prepareSubTree(TreeInfo& info, DisplayListData* subtree) {
1228de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck    if (subtree) {
123860d155f866cc15a725e7ce03763280987f24901John Reck        TextureCache& cache = Caches::getInstance().textureCache;
124860d155f866cc15a725e7ce03763280987f24901John Reck        info.hasFunctors |= subtree->functorCount;
125860d155f866cc15a725e7ce03763280987f24901John Reck        // TODO: Fix ownedBitmapResources to not require disabling prepareTextures
126860d155f866cc15a725e7ce03763280987f24901John Reck        // and thus falling out of async drawing path.
127860d155f866cc15a725e7ce03763280987f24901John Reck        if (subtree->ownedBitmapResources.size()) {
128860d155f866cc15a725e7ce03763280987f24901John Reck            info.prepareTextures = false;
129860d155f866cc15a725e7ce03763280987f24901John Reck        }
130860d155f866cc15a725e7ce03763280987f24901John Reck        for (size_t i = 0; info.prepareTextures && i < subtree->bitmapResources.size(); i++) {
131860d155f866cc15a725e7ce03763280987f24901John Reck            info.prepareTextures = cache.prefetchAndMarkInUse(subtree->bitmapResources[i]);
132f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck        }
1338de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck        for (size_t i = 0; i < subtree->children().size(); i++) {
1348de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck            RenderNode* childNode = subtree->children()[i]->mDisplayList;
135f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck            childNode->prepareTreeImpl(info);
1365bf11bb98f5dbe278c257355d24c181237abd68cJohn Reck        }
137113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
138113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
139113e0824d6bddf4376240681f9cf6a2deded9498John Reck
140113e0824d6bddf4376240681f9cf6a2deded9498John Reck/*
141113e0824d6bddf4376240681f9cf6a2deded9498John Reck * For property operations, we pass a savecount of 0, since the operations aren't part of the
142113e0824d6bddf4376240681f9cf6a2deded9498John Reck * displaylist, and thus don't have to compensate for the record-time/playback-time discrepancy in
143d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck * base saveCount (i.e., how RestoreToCount uses saveCount + properties().getCount())
144113e0824d6bddf4376240681f9cf6a2deded9498John Reck */
145113e0824d6bddf4376240681f9cf6a2deded9498John Reck#define PROPERTY_SAVECOUNT 0
146113e0824d6bddf4376240681f9cf6a2deded9498John Reck
147113e0824d6bddf4376240681f9cf6a2deded9498John Recktemplate <class T>
148b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craikvoid RenderNode::setViewProperties(OpenGLRenderer& renderer, T& handler) {
149113e0824d6bddf4376240681f9cf6a2deded9498John Reck#if DEBUG_DISPLAY_LIST
150b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    properties().debugOutputProperties(handler.level() + 1);
151113e0824d6bddf4376240681f9cf6a2deded9498John Reck#endif
152d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (properties().getLeft() != 0 || properties().getTop() != 0) {
153d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        renderer.translate(properties().getLeft(), properties().getTop());
154113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
155d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (properties().getStaticMatrix()) {
156d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        renderer.concatMatrix(properties().getStaticMatrix());
157d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    } else if (properties().getAnimationMatrix()) {
158d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        renderer.concatMatrix(properties().getAnimationMatrix());
159113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
160f7483e3af0513a1baa8341d403df2e0c0896a9ffJohn Reck    if (properties().hasTransformMatrix()) {
161f7483e3af0513a1baa8341d403df2e0c0896a9ffJohn Reck        if (properties().isTransformTranslateOnly()) {
162d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            renderer.translate(properties().getTranslationX(), properties().getTranslationY());
163113e0824d6bddf4376240681f9cf6a2deded9498John Reck        } else {
164d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            renderer.concatMatrix(*properties().getTransformMatrix());
165113e0824d6bddf4376240681f9cf6a2deded9498John Reck        }
166113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
167d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    bool clipToBoundsNeeded = properties().getCaching() ? false : properties().getClipToBounds();
168d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (properties().getAlpha() < 1) {
169d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        if (properties().getCaching()) {
170d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            renderer.setOverrideLayerAlpha(properties().getAlpha());
171d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        } else if (!properties().getHasOverlappingRendering()) {
172d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            renderer.scaleAlpha(properties().getAlpha());
173113e0824d6bddf4376240681f9cf6a2deded9498John Reck        } else {
174113e0824d6bddf4376240681f9cf6a2deded9498John Reck            // TODO: should be able to store the size of a DL at record time and not
175113e0824d6bddf4376240681f9cf6a2deded9498John Reck            // have to pass it into this call. In fact, this information might be in the
176113e0824d6bddf4376240681f9cf6a2deded9498John Reck            // location/size info that we store with the new native transform data.
177113e0824d6bddf4376240681f9cf6a2deded9498John Reck            int saveFlags = SkCanvas::kHasAlphaLayer_SaveFlag;
178113e0824d6bddf4376240681f9cf6a2deded9498John Reck            if (clipToBoundsNeeded) {
179113e0824d6bddf4376240681f9cf6a2deded9498John Reck                saveFlags |= SkCanvas::kClipToLayer_SaveFlag;
180113e0824d6bddf4376240681f9cf6a2deded9498John Reck                clipToBoundsNeeded = false; // clipping done by saveLayer
181113e0824d6bddf4376240681f9cf6a2deded9498John Reck            }
182113e0824d6bddf4376240681f9cf6a2deded9498John Reck
183113e0824d6bddf4376240681f9cf6a2deded9498John Reck            SaveLayerOp* op = new (handler.allocator()) SaveLayerOp(
1848c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik                    0, 0, properties().getWidth(), properties().getHeight(),
1858c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik                    properties().getAlpha() * 255, saveFlags);
186d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
187113e0824d6bddf4376240681f9cf6a2deded9498John Reck        }
188113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
189113e0824d6bddf4376240681f9cf6a2deded9498John Reck    if (clipToBoundsNeeded) {
1908c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik        ClipRectOp* op = new (handler.allocator()) ClipRectOp(
1918c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik                0, 0, properties().getWidth(), properties().getHeight(), SkRegion::kIntersect_Op);
192d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
193113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
1948c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik
1958c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik    if (CC_UNLIKELY(properties().hasClippingPath())) {
1968c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik        // TODO: optimize for round rect/circle clipping
1978c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik        const SkPath* path = properties().getClippingPath();
1988c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik        ClipPathOp* op = new (handler.allocator()) ClipPathOp(path, SkRegion::kIntersect_Op);
199d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
200113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
201113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
202113e0824d6bddf4376240681f9cf6a2deded9498John Reck
203113e0824d6bddf4376240681f9cf6a2deded9498John Reck/**
204113e0824d6bddf4376240681f9cf6a2deded9498John Reck * Apply property-based transformations to input matrix
205113e0824d6bddf4376240681f9cf6a2deded9498John Reck *
206113e0824d6bddf4376240681f9cf6a2deded9498John Reck * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
207113e0824d6bddf4376240681f9cf6a2deded9498John Reck * matrix computation instead of the Skia 3x3 matrix + camera hackery.
208113e0824d6bddf4376240681f9cf6a2deded9498John Reck */
209113e0824d6bddf4376240681f9cf6a2deded9498John Reckvoid RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) {
210d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (properties().getLeft() != 0 || properties().getTop() != 0) {
211d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        matrix.translate(properties().getLeft(), properties().getTop());
212113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
213d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (properties().getStaticMatrix()) {
214d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        mat4 stat(*properties().getStaticMatrix());
215113e0824d6bddf4376240681f9cf6a2deded9498John Reck        matrix.multiply(stat);
216d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    } else if (properties().getAnimationMatrix()) {
217d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        mat4 anim(*properties().getAnimationMatrix());
218113e0824d6bddf4376240681f9cf6a2deded9498John Reck        matrix.multiply(anim);
219113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
220f7483e3af0513a1baa8341d403df2e0c0896a9ffJohn Reck    if (properties().hasTransformMatrix()) {
221f7483e3af0513a1baa8341d403df2e0c0896a9ffJohn Reck        if (properties().isTransformTranslateOnly()) {
222d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
223d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                    true3dTransform ? properties().getTranslationZ() : 0.0f);
224113e0824d6bddf4376240681f9cf6a2deded9498John Reck        } else {
225113e0824d6bddf4376240681f9cf6a2deded9498John Reck            if (!true3dTransform) {
226d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                matrix.multiply(*properties().getTransformMatrix());
227113e0824d6bddf4376240681f9cf6a2deded9498John Reck            } else {
228113e0824d6bddf4376240681f9cf6a2deded9498John Reck                mat4 true3dMat;
229113e0824d6bddf4376240681f9cf6a2deded9498John Reck                true3dMat.loadTranslate(
230d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                        properties().getPivotX() + properties().getTranslationX(),
231d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                        properties().getPivotY() + properties().getTranslationY(),
232d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                        properties().getTranslationZ());
233d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
234d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
235d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                true3dMat.rotate(properties().getRotation(), 0, 0, 1);
236d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
237d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck                true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
238113e0824d6bddf4376240681f9cf6a2deded9498John Reck
239113e0824d6bddf4376240681f9cf6a2deded9498John Reck                matrix.multiply(true3dMat);
240113e0824d6bddf4376240681f9cf6a2deded9498John Reck            }
241113e0824d6bddf4376240681f9cf6a2deded9498John Reck        }
242113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
243113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
244113e0824d6bddf4376240681f9cf6a2deded9498John Reck
245113e0824d6bddf4376240681f9cf6a2deded9498John Reck/**
246113e0824d6bddf4376240681f9cf6a2deded9498John Reck * Organizes the DisplayList hierarchy to prepare for background projection reordering.
247113e0824d6bddf4376240681f9cf6a2deded9498John Reck *
248113e0824d6bddf4376240681f9cf6a2deded9498John Reck * This should be called before a call to defer() or drawDisplayList()
249113e0824d6bddf4376240681f9cf6a2deded9498John Reck *
250113e0824d6bddf4376240681f9cf6a2deded9498John Reck * Each DisplayList that serves as a 3d root builds its list of composited children,
251113e0824d6bddf4376240681f9cf6a2deded9498John Reck * which are flagged to not draw in the standard draw loop.
252113e0824d6bddf4376240681f9cf6a2deded9498John Reck */
253113e0824d6bddf4376240681f9cf6a2deded9498John Reckvoid RenderNode::computeOrdering() {
254113e0824d6bddf4376240681f9cf6a2deded9498John Reck    ATRACE_CALL();
255113e0824d6bddf4376240681f9cf6a2deded9498John Reck    mProjectedNodes.clear();
256113e0824d6bddf4376240681f9cf6a2deded9498John Reck
257113e0824d6bddf4376240681f9cf6a2deded9498John Reck    // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that
258113e0824d6bddf4376240681f9cf6a2deded9498John Reck    // transform properties are applied correctly to top level children
259113e0824d6bddf4376240681f9cf6a2deded9498John Reck    if (mDisplayListData == NULL) return;
260087bc0c14bdccf7c258dce0cdef46a69a839b427John Reck    for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
261087bc0c14bdccf7c258dce0cdef46a69a839b427John Reck        DrawDisplayListOp* childOp = mDisplayListData->children()[i];
262113e0824d6bddf4376240681f9cf6a2deded9498John Reck        childOp->mDisplayList->computeOrderingImpl(childOp,
263113e0824d6bddf4376240681f9cf6a2deded9498John Reck                &mProjectedNodes, &mat4::identity());
264113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
265113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
266113e0824d6bddf4376240681f9cf6a2deded9498John Reck
267113e0824d6bddf4376240681f9cf6a2deded9498John Reckvoid RenderNode::computeOrderingImpl(
268113e0824d6bddf4376240681f9cf6a2deded9498John Reck        DrawDisplayListOp* opState,
269113e0824d6bddf4376240681f9cf6a2deded9498John Reck        Vector<DrawDisplayListOp*>* compositedChildrenOfProjectionSurface,
270113e0824d6bddf4376240681f9cf6a2deded9498John Reck        const mat4* transformFromProjectionSurface) {
271113e0824d6bddf4376240681f9cf6a2deded9498John Reck    mProjectedNodes.clear();
272113e0824d6bddf4376240681f9cf6a2deded9498John Reck    if (mDisplayListData == NULL || mDisplayListData->isEmpty()) return;
273113e0824d6bddf4376240681f9cf6a2deded9498John Reck
274113e0824d6bddf4376240681f9cf6a2deded9498John Reck    // TODO: should avoid this calculation in most cases
275113e0824d6bddf4376240681f9cf6a2deded9498John Reck    // TODO: just calculate single matrix, down to all leaf composited elements
276113e0824d6bddf4376240681f9cf6a2deded9498John Reck    Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface);
277113e0824d6bddf4376240681f9cf6a2deded9498John Reck    localTransformFromProjectionSurface.multiply(opState->mTransformFromParent);
278113e0824d6bddf4376240681f9cf6a2deded9498John Reck
279d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (properties().getProjectBackwards()) {
280113e0824d6bddf4376240681f9cf6a2deded9498John Reck        // composited projectee, flag for out of order draw, save matrix, and store in proj surface
281113e0824d6bddf4376240681f9cf6a2deded9498John Reck        opState->mSkipInOrderDraw = true;
282113e0824d6bddf4376240681f9cf6a2deded9498John Reck        opState->mTransformFromCompositingAncestor.load(localTransformFromProjectionSurface);
283113e0824d6bddf4376240681f9cf6a2deded9498John Reck        compositedChildrenOfProjectionSurface->add(opState);
284113e0824d6bddf4376240681f9cf6a2deded9498John Reck    } else {
285113e0824d6bddf4376240681f9cf6a2deded9498John Reck        // standard in order draw
286113e0824d6bddf4376240681f9cf6a2deded9498John Reck        opState->mSkipInOrderDraw = false;
287113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
288113e0824d6bddf4376240681f9cf6a2deded9498John Reck
289087bc0c14bdccf7c258dce0cdef46a69a839b427John Reck    if (mDisplayListData->children().size() > 0) {
290113e0824d6bddf4376240681f9cf6a2deded9498John Reck        const bool isProjectionReceiver = mDisplayListData->projectionReceiveIndex >= 0;
291113e0824d6bddf4376240681f9cf6a2deded9498John Reck        bool haveAppliedPropertiesToProjection = false;
292087bc0c14bdccf7c258dce0cdef46a69a839b427John Reck        for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
293087bc0c14bdccf7c258dce0cdef46a69a839b427John Reck            DrawDisplayListOp* childOp = mDisplayListData->children()[i];
294113e0824d6bddf4376240681f9cf6a2deded9498John Reck            RenderNode* child = childOp->mDisplayList;
295113e0824d6bddf4376240681f9cf6a2deded9498John Reck
296113e0824d6bddf4376240681f9cf6a2deded9498John Reck            Vector<DrawDisplayListOp*>* projectionChildren = NULL;
297113e0824d6bddf4376240681f9cf6a2deded9498John Reck            const mat4* projectionTransform = NULL;
298d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            if (isProjectionReceiver && !child->properties().getProjectBackwards()) {
299113e0824d6bddf4376240681f9cf6a2deded9498John Reck                // if receiving projections, collect projecting descendent
300113e0824d6bddf4376240681f9cf6a2deded9498John Reck
301113e0824d6bddf4376240681f9cf6a2deded9498John Reck                // Note that if a direct descendent is projecting backwards, we pass it's
302113e0824d6bddf4376240681f9cf6a2deded9498John Reck                // grandparent projection collection, since it shouldn't project onto it's
303113e0824d6bddf4376240681f9cf6a2deded9498John Reck                // parent, where it will already be drawing.
304113e0824d6bddf4376240681f9cf6a2deded9498John Reck                projectionChildren = &mProjectedNodes;
305113e0824d6bddf4376240681f9cf6a2deded9498John Reck                projectionTransform = &mat4::identity();
306113e0824d6bddf4376240681f9cf6a2deded9498John Reck            } else {
307113e0824d6bddf4376240681f9cf6a2deded9498John Reck                if (!haveAppliedPropertiesToProjection) {
308113e0824d6bddf4376240681f9cf6a2deded9498John Reck                    applyViewPropertyTransforms(localTransformFromProjectionSurface);
309113e0824d6bddf4376240681f9cf6a2deded9498John Reck                    haveAppliedPropertiesToProjection = true;
310113e0824d6bddf4376240681f9cf6a2deded9498John Reck                }
311113e0824d6bddf4376240681f9cf6a2deded9498John Reck                projectionChildren = compositedChildrenOfProjectionSurface;
312113e0824d6bddf4376240681f9cf6a2deded9498John Reck                projectionTransform = &localTransformFromProjectionSurface;
313113e0824d6bddf4376240681f9cf6a2deded9498John Reck            }
314113e0824d6bddf4376240681f9cf6a2deded9498John Reck            child->computeOrderingImpl(childOp, projectionChildren, projectionTransform);
315113e0824d6bddf4376240681f9cf6a2deded9498John Reck        }
316113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
317113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
318113e0824d6bddf4376240681f9cf6a2deded9498John Reck
319113e0824d6bddf4376240681f9cf6a2deded9498John Reckclass DeferOperationHandler {
320113e0824d6bddf4376240681f9cf6a2deded9498John Reckpublic:
321113e0824d6bddf4376240681f9cf6a2deded9498John Reck    DeferOperationHandler(DeferStateStruct& deferStruct, int level)
322113e0824d6bddf4376240681f9cf6a2deded9498John Reck        : mDeferStruct(deferStruct), mLevel(level) {}
323113e0824d6bddf4376240681f9cf6a2deded9498John Reck    inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
324113e0824d6bddf4376240681f9cf6a2deded9498John Reck        operation->defer(mDeferStruct, saveCount, mLevel, clipToBounds);
325113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
326113e0824d6bddf4376240681f9cf6a2deded9498John Reck    inline LinearAllocator& allocator() { return *(mDeferStruct.mAllocator); }
327b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    inline void startMark(const char* name) {} // do nothing
328b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    inline void endMark() {}
329b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    inline int level() { return mLevel; }
330b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    inline int replayFlags() { return mDeferStruct.mReplayFlags; }
331113e0824d6bddf4376240681f9cf6a2deded9498John Reck
332113e0824d6bddf4376240681f9cf6a2deded9498John Reckprivate:
333113e0824d6bddf4376240681f9cf6a2deded9498John Reck    DeferStateStruct& mDeferStruct;
334113e0824d6bddf4376240681f9cf6a2deded9498John Reck    const int mLevel;
335113e0824d6bddf4376240681f9cf6a2deded9498John Reck};
336113e0824d6bddf4376240681f9cf6a2deded9498John Reck
337b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craikvoid RenderNode::deferNodeTree(DeferStateStruct& deferStruct) {
338b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    DeferOperationHandler handler(deferStruct, 0);
339b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    if (properties().getTranslationZ() > 0.0f) issueDrawShadowOperation(Matrix4::identity(), handler);
340b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler);
341b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik}
342b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik
343b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craikvoid RenderNode::deferNodeInParent(DeferStateStruct& deferStruct, const int level) {
344113e0824d6bddf4376240681f9cf6a2deded9498John Reck    DeferOperationHandler handler(deferStruct, level);
345b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler);
346113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
347113e0824d6bddf4376240681f9cf6a2deded9498John Reck
348113e0824d6bddf4376240681f9cf6a2deded9498John Reckclass ReplayOperationHandler {
349113e0824d6bddf4376240681f9cf6a2deded9498John Reckpublic:
350113e0824d6bddf4376240681f9cf6a2deded9498John Reck    ReplayOperationHandler(ReplayStateStruct& replayStruct, int level)
351113e0824d6bddf4376240681f9cf6a2deded9498John Reck        : mReplayStruct(replayStruct), mLevel(level) {}
352113e0824d6bddf4376240681f9cf6a2deded9498John Reck    inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
353113e0824d6bddf4376240681f9cf6a2deded9498John Reck#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
354d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        properties().getReplayStruct().mRenderer.eventMark(operation->name());
355113e0824d6bddf4376240681f9cf6a2deded9498John Reck#endif
356113e0824d6bddf4376240681f9cf6a2deded9498John Reck        operation->replay(mReplayStruct, saveCount, mLevel, clipToBounds);
357113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
358113e0824d6bddf4376240681f9cf6a2deded9498John Reck    inline LinearAllocator& allocator() { return *(mReplayStruct.mAllocator); }
359b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    inline void startMark(const char* name) {
360b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik        mReplayStruct.mRenderer.startMark(name);
361b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    }
362b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    inline void endMark() {
363b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik        mReplayStruct.mRenderer.endMark();
364b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik        DISPLAY_LIST_LOGD("%*sDone (%p, %s), returning %d", level * 2, "", this, mName.string(),
365b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik                mReplayStruct.mDrawGlStatus);
366b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    }
367b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    inline int level() { return mLevel; }
368b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    inline int replayFlags() { return mReplayStruct.mReplayFlags; }
369113e0824d6bddf4376240681f9cf6a2deded9498John Reck
370113e0824d6bddf4376240681f9cf6a2deded9498John Reckprivate:
371113e0824d6bddf4376240681f9cf6a2deded9498John Reck    ReplayStateStruct& mReplayStruct;
372113e0824d6bddf4376240681f9cf6a2deded9498John Reck    const int mLevel;
373113e0824d6bddf4376240681f9cf6a2deded9498John Reck};
374113e0824d6bddf4376240681f9cf6a2deded9498John Reck
375b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craikvoid RenderNode::replayNodeTree(ReplayStateStruct& replayStruct) {
376b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    ReplayOperationHandler handler(replayStruct, 0);
377b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    if (properties().getTranslationZ() > 0.0f) issueDrawShadowOperation(Matrix4::identity(), handler);
378b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler);
379b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik}
380113e0824d6bddf4376240681f9cf6a2deded9498John Reck
381b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craikvoid RenderNode::replayNodeInParent(ReplayStateStruct& replayStruct, const int level) {
382b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    ReplayOperationHandler handler(replayStruct, level);
383b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler);
384113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
385113e0824d6bddf4376240681f9cf6a2deded9498John Reck
386113e0824d6bddf4376240681f9cf6a2deded9498John Reckvoid RenderNode::buildZSortedChildList(Vector<ZDrawDisplayListOpPair>& zTranslatedNodes) {
387087bc0c14bdccf7c258dce0cdef46a69a839b427John Reck    if (mDisplayListData == NULL || mDisplayListData->children().size() == 0) return;
388113e0824d6bddf4376240681f9cf6a2deded9498John Reck
389087bc0c14bdccf7c258dce0cdef46a69a839b427John Reck    for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
390087bc0c14bdccf7c258dce0cdef46a69a839b427John Reck        DrawDisplayListOp* childOp = mDisplayListData->children()[i];
391113e0824d6bddf4376240681f9cf6a2deded9498John Reck        RenderNode* child = childOp->mDisplayList;
392d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        float childZ = child->properties().getTranslationZ();
393113e0824d6bddf4376240681f9cf6a2deded9498John Reck
394113e0824d6bddf4376240681f9cf6a2deded9498John Reck        if (childZ != 0.0f) {
395113e0824d6bddf4376240681f9cf6a2deded9498John Reck            zTranslatedNodes.add(ZDrawDisplayListOpPair(childZ, childOp));
396113e0824d6bddf4376240681f9cf6a2deded9498John Reck            childOp->mSkipInOrderDraw = true;
397d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        } else if (!child->properties().getProjectBackwards()) {
398113e0824d6bddf4376240681f9cf6a2deded9498John Reck            // regular, in order drawing DisplayList
399113e0824d6bddf4376240681f9cf6a2deded9498John Reck            childOp->mSkipInOrderDraw = false;
400113e0824d6bddf4376240681f9cf6a2deded9498John Reck        }
401113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
402113e0824d6bddf4376240681f9cf6a2deded9498John Reck
403113e0824d6bddf4376240681f9cf6a2deded9498John Reck    // Z sort 3d children (stable-ness makes z compare fall back to standard drawing order)
404113e0824d6bddf4376240681f9cf6a2deded9498John Reck    std::stable_sort(zTranslatedNodes.begin(), zTranslatedNodes.end());
405113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
406113e0824d6bddf4376240681f9cf6a2deded9498John Reck
407b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craiktemplate <class T>
408b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craikvoid RenderNode::issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler) {
409b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    if (properties().getAlpha() <= 0.0f) return;
410b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik
411b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    mat4 shadowMatrixXY(transformFromParent);
412b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    applyViewPropertyTransforms(shadowMatrixXY);
413b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik
414b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    // Z matrix needs actual 3d transformation, so mapped z values will be correct
415b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    mat4 shadowMatrixZ(transformFromParent);
416b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    applyViewPropertyTransforms(shadowMatrixZ, true);
417b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik
418b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    const SkPath* outlinePath = properties().getOutline().getPath();
419b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    const RevealClip& revealClip = properties().getRevealClip();
420b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    const SkPath* revealClipPath = revealClip.hasConvexClip()
421b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik            ?  revealClip.getPath() : NULL; // only pass the reveal clip's path if it's convex
422b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik
423b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    /**
424b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik     * The drawing area of the caster is always the same as the its perimeter (which
425b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik     * the shadow system uses) *except* in the inverse clip case. Inform the shadow
426b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik     * system that the caster's drawing area (as opposed to its perimeter) has been
427b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik     * clipped, so that it knows the caster can't be opaque.
428b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik     */
429b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    bool casterUnclipped = !revealClip.willClip() || revealClip.hasConvexClip();
430b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik
431b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    DisplayListOp* shadowOp  = new (handler.allocator()) DrawShadowOp(
432b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik            shadowMatrixXY, shadowMatrixZ,
433b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik            properties().getAlpha(), casterUnclipped,
434b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik            properties().getWidth(), properties().getHeight(),
435b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik            outlinePath, revealClipPath);
436b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    handler(shadowOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
437b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik}
438b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik
439113e0824d6bddf4376240681f9cf6a2deded9498John Reck#define SHADOW_DELTA 0.1f
440113e0824d6bddf4376240681f9cf6a2deded9498John Reck
441113e0824d6bddf4376240681f9cf6a2deded9498John Recktemplate <class T>
442b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craikvoid RenderNode::issueOperationsOf3dChildren(const Vector<ZDrawDisplayListOpPair>& zTranslatedNodes,
443113e0824d6bddf4376240681f9cf6a2deded9498John Reck        ChildrenSelectMode mode, OpenGLRenderer& renderer, T& handler) {
444113e0824d6bddf4376240681f9cf6a2deded9498John Reck    const int size = zTranslatedNodes.size();
445113e0824d6bddf4376240681f9cf6a2deded9498John Reck    if (size == 0
446113e0824d6bddf4376240681f9cf6a2deded9498John Reck            || (mode == kNegativeZChildren && zTranslatedNodes[0].key > 0.0f)
447113e0824d6bddf4376240681f9cf6a2deded9498John Reck            || (mode == kPositiveZChildren && zTranslatedNodes[size - 1].key < 0.0f)) {
448113e0824d6bddf4376240681f9cf6a2deded9498John Reck        // no 3d children to draw
449113e0824d6bddf4376240681f9cf6a2deded9498John Reck        return;
450113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
451113e0824d6bddf4376240681f9cf6a2deded9498John Reck
452113e0824d6bddf4376240681f9cf6a2deded9498John Reck    /**
453113e0824d6bddf4376240681f9cf6a2deded9498John Reck     * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
454113e0824d6bddf4376240681f9cf6a2deded9498John Reck     * with very similar Z heights to draw together.
455113e0824d6bddf4376240681f9cf6a2deded9498John Reck     *
456113e0824d6bddf4376240681f9cf6a2deded9498John Reck     * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
457113e0824d6bddf4376240681f9cf6a2deded9498John Reck     * underneath both, and neither's shadow is drawn on top of the other.
458113e0824d6bddf4376240681f9cf6a2deded9498John Reck     */
459113e0824d6bddf4376240681f9cf6a2deded9498John Reck    const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
460113e0824d6bddf4376240681f9cf6a2deded9498John Reck    size_t drawIndex, shadowIndex, endIndex;
461113e0824d6bddf4376240681f9cf6a2deded9498John Reck    if (mode == kNegativeZChildren) {
462113e0824d6bddf4376240681f9cf6a2deded9498John Reck        drawIndex = 0;
463113e0824d6bddf4376240681f9cf6a2deded9498John Reck        endIndex = nonNegativeIndex;
464113e0824d6bddf4376240681f9cf6a2deded9498John Reck        shadowIndex = endIndex; // draw no shadows
465113e0824d6bddf4376240681f9cf6a2deded9498John Reck    } else {
466113e0824d6bddf4376240681f9cf6a2deded9498John Reck        drawIndex = nonNegativeIndex;
467113e0824d6bddf4376240681f9cf6a2deded9498John Reck        endIndex = size;
468113e0824d6bddf4376240681f9cf6a2deded9498John Reck        shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
469113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
470113e0824d6bddf4376240681f9cf6a2deded9498John Reck    float lastCasterZ = 0.0f;
471113e0824d6bddf4376240681f9cf6a2deded9498John Reck    while (shadowIndex < endIndex || drawIndex < endIndex) {
472113e0824d6bddf4376240681f9cf6a2deded9498John Reck        if (shadowIndex < endIndex) {
473113e0824d6bddf4376240681f9cf6a2deded9498John Reck            DrawDisplayListOp* casterOp = zTranslatedNodes[shadowIndex].value;
474113e0824d6bddf4376240681f9cf6a2deded9498John Reck            RenderNode* caster = casterOp->mDisplayList;
475113e0824d6bddf4376240681f9cf6a2deded9498John Reck            const float casterZ = zTranslatedNodes[shadowIndex].key;
476113e0824d6bddf4376240681f9cf6a2deded9498John Reck            // attempt to render the shadow if the caster about to be drawn is its caster,
477113e0824d6bddf4376240681f9cf6a2deded9498John Reck            // OR if its caster's Z value is similar to the previous potential caster
478113e0824d6bddf4376240681f9cf6a2deded9498John Reck            if (shadowIndex == drawIndex || casterZ - lastCasterZ < SHADOW_DELTA) {
479b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik                caster->issueDrawShadowOperation(casterOp->mTransformFromParent, handler);
480113e0824d6bddf4376240681f9cf6a2deded9498John Reck
481113e0824d6bddf4376240681f9cf6a2deded9498John Reck                lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
482113e0824d6bddf4376240681f9cf6a2deded9498John Reck                shadowIndex++;
483113e0824d6bddf4376240681f9cf6a2deded9498John Reck                continue;
484113e0824d6bddf4376240681f9cf6a2deded9498John Reck            }
485113e0824d6bddf4376240681f9cf6a2deded9498John Reck        }
486113e0824d6bddf4376240681f9cf6a2deded9498John Reck
487113e0824d6bddf4376240681f9cf6a2deded9498John Reck        // only the actual child DL draw needs to be in save/restore,
488113e0824d6bddf4376240681f9cf6a2deded9498John Reck        // since it modifies the renderer's matrix
489113e0824d6bddf4376240681f9cf6a2deded9498John Reck        int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
490113e0824d6bddf4376240681f9cf6a2deded9498John Reck
491113e0824d6bddf4376240681f9cf6a2deded9498John Reck        DrawDisplayListOp* childOp = zTranslatedNodes[drawIndex].value;
492113e0824d6bddf4376240681f9cf6a2deded9498John Reck        RenderNode* child = childOp->mDisplayList;
493113e0824d6bddf4376240681f9cf6a2deded9498John Reck
494113e0824d6bddf4376240681f9cf6a2deded9498John Reck        renderer.concatMatrix(childOp->mTransformFromParent);
495113e0824d6bddf4376240681f9cf6a2deded9498John Reck        childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
496d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
497113e0824d6bddf4376240681f9cf6a2deded9498John Reck        childOp->mSkipInOrderDraw = true;
498113e0824d6bddf4376240681f9cf6a2deded9498John Reck
499113e0824d6bddf4376240681f9cf6a2deded9498John Reck        renderer.restoreToCount(restoreTo);
500113e0824d6bddf4376240681f9cf6a2deded9498John Reck        drawIndex++;
501113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
502113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
503113e0824d6bddf4376240681f9cf6a2deded9498John Reck
504113e0824d6bddf4376240681f9cf6a2deded9498John Recktemplate <class T>
505b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craikvoid RenderNode::issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler) {
506113e0824d6bddf4376240681f9cf6a2deded9498John Reck    for (size_t i = 0; i < mProjectedNodes.size(); i++) {
507113e0824d6bddf4376240681f9cf6a2deded9498John Reck        DrawDisplayListOp* childOp = mProjectedNodes[i];
508113e0824d6bddf4376240681f9cf6a2deded9498John Reck
509113e0824d6bddf4376240681f9cf6a2deded9498John Reck        // matrix save, concat, and restore can be done safely without allocating operations
510113e0824d6bddf4376240681f9cf6a2deded9498John Reck        int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
511113e0824d6bddf4376240681f9cf6a2deded9498John Reck        renderer.concatMatrix(childOp->mTransformFromCompositingAncestor);
512113e0824d6bddf4376240681f9cf6a2deded9498John Reck        childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
513d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
514113e0824d6bddf4376240681f9cf6a2deded9498John Reck        childOp->mSkipInOrderDraw = true;
515113e0824d6bddf4376240681f9cf6a2deded9498John Reck        renderer.restoreToCount(restoreTo);
516113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
517113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
518113e0824d6bddf4376240681f9cf6a2deded9498John Reck
519113e0824d6bddf4376240681f9cf6a2deded9498John Reck/**
520113e0824d6bddf4376240681f9cf6a2deded9498John Reck * This function serves both defer and replay modes, and will organize the displayList's component
521113e0824d6bddf4376240681f9cf6a2deded9498John Reck * operations for a single frame:
522113e0824d6bddf4376240681f9cf6a2deded9498John Reck *
523113e0824d6bddf4376240681f9cf6a2deded9498John Reck * Every 'simple' state operation that affects just the matrix and alpha (or other factors of
524113e0824d6bddf4376240681f9cf6a2deded9498John Reck * DeferredDisplayState) may be issued directly to the renderer, but complex operations (with custom
525113e0824d6bddf4376240681f9cf6a2deded9498John Reck * defer logic) and operations in displayListOps are issued through the 'handler' which handles the
526113e0824d6bddf4376240681f9cf6a2deded9498John Reck * defer vs replay logic, per operation
527113e0824d6bddf4376240681f9cf6a2deded9498John Reck */
528113e0824d6bddf4376240681f9cf6a2deded9498John Recktemplate <class T>
529b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craikvoid RenderNode::issueOperations(OpenGLRenderer& renderer, T& handler) {
530b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    const int level = handler.level();
531d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    if (mDisplayListData->isEmpty() || properties().getAlpha() <= 0) {
532113e0824d6bddf4376240681f9cf6a2deded9498John Reck        DISPLAY_LIST_LOGD("%*sEmpty display list (%p, %s)", level * 2, "", this, mName.string());
533113e0824d6bddf4376240681f9cf6a2deded9498John Reck        return;
534113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
535113e0824d6bddf4376240681f9cf6a2deded9498John Reck
536b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    handler.startMark(mName.string());
537b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik
538113e0824d6bddf4376240681f9cf6a2deded9498John Reck#if DEBUG_DISPLAY_LIST
539113e0824d6bddf4376240681f9cf6a2deded9498John Reck    Rect* clipRect = renderer.getClipRect();
540113e0824d6bddf4376240681f9cf6a2deded9498John Reck    DISPLAY_LIST_LOGD("%*sStart display list (%p, %s), clipRect: %.0f, %.0f, %.0f, %.0f",
541113e0824d6bddf4376240681f9cf6a2deded9498John Reck            level * 2, "", this, mName.string(), clipRect->left, clipRect->top,
542113e0824d6bddf4376240681f9cf6a2deded9498John Reck            clipRect->right, clipRect->bottom);
543113e0824d6bddf4376240681f9cf6a2deded9498John Reck#endif
544113e0824d6bddf4376240681f9cf6a2deded9498John Reck
545113e0824d6bddf4376240681f9cf6a2deded9498John Reck    LinearAllocator& alloc = handler.allocator();
546113e0824d6bddf4376240681f9cf6a2deded9498John Reck    int restoreTo = renderer.getSaveCount();
547113e0824d6bddf4376240681f9cf6a2deded9498John Reck    handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
548d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            PROPERTY_SAVECOUNT, properties().getClipToBounds());
549113e0824d6bddf4376240681f9cf6a2deded9498John Reck
550113e0824d6bddf4376240681f9cf6a2deded9498John Reck    DISPLAY_LIST_LOGD("%*sSave %d %d", (level + 1) * 2, "",
551113e0824d6bddf4376240681f9cf6a2deded9498John Reck            SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag, restoreTo);
552113e0824d6bddf4376240681f9cf6a2deded9498John Reck
553b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    setViewProperties<T>(renderer, handler);
554113e0824d6bddf4376240681f9cf6a2deded9498John Reck
5558c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik    bool quickRejected = properties().getClipToBounds()
5568c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik            && renderer.quickRejectConservative(0, 0, properties().getWidth(), properties().getHeight());
557113e0824d6bddf4376240681f9cf6a2deded9498John Reck    if (!quickRejected) {
558113e0824d6bddf4376240681f9cf6a2deded9498John Reck        Vector<ZDrawDisplayListOpPair> zTranslatedNodes;
559113e0824d6bddf4376240681f9cf6a2deded9498John Reck        buildZSortedChildList(zTranslatedNodes);
560113e0824d6bddf4376240681f9cf6a2deded9498John Reck
561113e0824d6bddf4376240681f9cf6a2deded9498John Reck        // for 3d root, draw children with negative z values
562b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik        issueOperationsOf3dChildren(zTranslatedNodes, kNegativeZChildren, renderer, handler);
563113e0824d6bddf4376240681f9cf6a2deded9498John Reck
564113e0824d6bddf4376240681f9cf6a2deded9498John Reck        DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance();
565113e0824d6bddf4376240681f9cf6a2deded9498John Reck        const int saveCountOffset = renderer.getSaveCount() - 1;
566113e0824d6bddf4376240681f9cf6a2deded9498John Reck        const int projectionReceiveIndex = mDisplayListData->projectionReceiveIndex;
567113e0824d6bddf4376240681f9cf6a2deded9498John Reck        for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) {
568113e0824d6bddf4376240681f9cf6a2deded9498John Reck            DisplayListOp *op = mDisplayListData->displayListOps[i];
569113e0824d6bddf4376240681f9cf6a2deded9498John Reck
570113e0824d6bddf4376240681f9cf6a2deded9498John Reck#if DEBUG_DISPLAY_LIST
571113e0824d6bddf4376240681f9cf6a2deded9498John Reck            op->output(level + 1);
572113e0824d6bddf4376240681f9cf6a2deded9498John Reck#endif
573113e0824d6bddf4376240681f9cf6a2deded9498John Reck            logBuffer.writeCommand(level, op->name());
574d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            handler(op, saveCountOffset, properties().getClipToBounds());
575113e0824d6bddf4376240681f9cf6a2deded9498John Reck
576113e0824d6bddf4376240681f9cf6a2deded9498John Reck            if (CC_UNLIKELY(i == projectionReceiveIndex && mProjectedNodes.size() > 0)) {
577b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik                issueOperationsOfProjectedChildren(renderer, handler);
578113e0824d6bddf4376240681f9cf6a2deded9498John Reck            }
579113e0824d6bddf4376240681f9cf6a2deded9498John Reck        }
580113e0824d6bddf4376240681f9cf6a2deded9498John Reck
581113e0824d6bddf4376240681f9cf6a2deded9498John Reck        // for 3d root, draw children with positive z values
582b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik        issueOperationsOf3dChildren(zTranslatedNodes, kPositiveZChildren, renderer, handler);
583113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
584113e0824d6bddf4376240681f9cf6a2deded9498John Reck
585113e0824d6bddf4376240681f9cf6a2deded9498John Reck    DISPLAY_LIST_LOGD("%*sRestoreToCount %d", (level + 1) * 2, "", restoreTo);
586113e0824d6bddf4376240681f9cf6a2deded9498John Reck    handler(new (alloc) RestoreToCountOp(restoreTo),
587d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck            PROPERTY_SAVECOUNT, properties().getClipToBounds());
588113e0824d6bddf4376240681f9cf6a2deded9498John Reck    renderer.setOverrideLayerAlpha(1.0f);
589b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik
590b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    handler.endMark();
591113e0824d6bddf4376240681f9cf6a2deded9498John Reck}
592113e0824d6bddf4376240681f9cf6a2deded9498John Reck
593113e0824d6bddf4376240681f9cf6a2deded9498John Reck} /* namespace uirenderer */
594113e0824d6bddf4376240681f9cf6a2deded9498John Reck} /* namespace android */
595