RenderNode.cpp revision 143912fef8eff58146705849a0ba441ab6163409
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 112f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck TreeInfo oldTreeInfo = {0}; 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) { 123f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck if (!info.hasFunctors) { 124f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck info.hasFunctors = subtree->functorCount; 125f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck } 1268de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck for (size_t i = 0; i < subtree->children().size(); i++) { 1278de65a8e05285df52a1e6f0c1d5616dd233298a7John Reck RenderNode* childNode = subtree->children()[i]->mDisplayList; 128f4198b713e43c0c0f9adac74203cf24c2a49b802John Reck childNode->prepareTreeImpl(info); 1295bf11bb98f5dbe278c257355d24c181237abd68cJohn Reck } 130113e0824d6bddf4376240681f9cf6a2deded9498John Reck } 131113e0824d6bddf4376240681f9cf6a2deded9498John Reck} 132113e0824d6bddf4376240681f9cf6a2deded9498John Reck 133113e0824d6bddf4376240681f9cf6a2deded9498John Reck/* 134113e0824d6bddf4376240681f9cf6a2deded9498John Reck * For property operations, we pass a savecount of 0, since the operations aren't part of the 135113e0824d6bddf4376240681f9cf6a2deded9498John Reck * displaylist, and thus don't have to compensate for the record-time/playback-time discrepancy in 136d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck * base saveCount (i.e., how RestoreToCount uses saveCount + properties().getCount()) 137113e0824d6bddf4376240681f9cf6a2deded9498John Reck */ 138113e0824d6bddf4376240681f9cf6a2deded9498John Reck#define PROPERTY_SAVECOUNT 0 139113e0824d6bddf4376240681f9cf6a2deded9498John Reck 140113e0824d6bddf4376240681f9cf6a2deded9498John Recktemplate <class T> 141b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craikvoid RenderNode::setViewProperties(OpenGLRenderer& renderer, T& handler) { 142113e0824d6bddf4376240681f9cf6a2deded9498John Reck#if DEBUG_DISPLAY_LIST 143b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik properties().debugOutputProperties(handler.level() + 1); 144113e0824d6bddf4376240681f9cf6a2deded9498John Reck#endif 145d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck if (properties().getLeft() != 0 || properties().getTop() != 0) { 146d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck renderer.translate(properties().getLeft(), properties().getTop()); 147113e0824d6bddf4376240681f9cf6a2deded9498John Reck } 148d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck if (properties().getStaticMatrix()) { 149d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck renderer.concatMatrix(properties().getStaticMatrix()); 150d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck } else if (properties().getAnimationMatrix()) { 151d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck renderer.concatMatrix(properties().getAnimationMatrix()); 152113e0824d6bddf4376240681f9cf6a2deded9498John Reck } 153d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck if (properties().getMatrixFlags() != 0) { 154d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck if (properties().getMatrixFlags() == TRANSLATION) { 155d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck renderer.translate(properties().getTranslationX(), properties().getTranslationY()); 156113e0824d6bddf4376240681f9cf6a2deded9498John Reck } else { 157d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck renderer.concatMatrix(*properties().getTransformMatrix()); 158113e0824d6bddf4376240681f9cf6a2deded9498John Reck } 159113e0824d6bddf4376240681f9cf6a2deded9498John Reck } 160d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck bool clipToBoundsNeeded = properties().getCaching() ? false : properties().getClipToBounds(); 161d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck if (properties().getAlpha() < 1) { 162d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck if (properties().getCaching()) { 163d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck renderer.setOverrideLayerAlpha(properties().getAlpha()); 164d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck } else if (!properties().getHasOverlappingRendering()) { 165d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck renderer.scaleAlpha(properties().getAlpha()); 166113e0824d6bddf4376240681f9cf6a2deded9498John Reck } else { 167113e0824d6bddf4376240681f9cf6a2deded9498John Reck // TODO: should be able to store the size of a DL at record time and not 168113e0824d6bddf4376240681f9cf6a2deded9498John Reck // have to pass it into this call. In fact, this information might be in the 169113e0824d6bddf4376240681f9cf6a2deded9498John Reck // location/size info that we store with the new native transform data. 170113e0824d6bddf4376240681f9cf6a2deded9498John Reck int saveFlags = SkCanvas::kHasAlphaLayer_SaveFlag; 171113e0824d6bddf4376240681f9cf6a2deded9498John Reck if (clipToBoundsNeeded) { 172113e0824d6bddf4376240681f9cf6a2deded9498John Reck saveFlags |= SkCanvas::kClipToLayer_SaveFlag; 173113e0824d6bddf4376240681f9cf6a2deded9498John Reck clipToBoundsNeeded = false; // clipping done by saveLayer 174113e0824d6bddf4376240681f9cf6a2deded9498John Reck } 175113e0824d6bddf4376240681f9cf6a2deded9498John Reck 176113e0824d6bddf4376240681f9cf6a2deded9498John Reck SaveLayerOp* op = new (handler.allocator()) SaveLayerOp( 1778c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik 0, 0, properties().getWidth(), properties().getHeight(), 1788c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik properties().getAlpha() * 255, saveFlags); 179d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds()); 180113e0824d6bddf4376240681f9cf6a2deded9498John Reck } 181113e0824d6bddf4376240681f9cf6a2deded9498John Reck } 182113e0824d6bddf4376240681f9cf6a2deded9498John Reck if (clipToBoundsNeeded) { 1838c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik ClipRectOp* op = new (handler.allocator()) ClipRectOp( 1848c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik 0, 0, properties().getWidth(), properties().getHeight(), SkRegion::kIntersect_Op); 185d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds()); 186113e0824d6bddf4376240681f9cf6a2deded9498John Reck } 1878c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik 1888c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik if (CC_UNLIKELY(properties().hasClippingPath())) { 1898c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik // TODO: optimize for round rect/circle clipping 1908c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik const SkPath* path = properties().getClippingPath(); 1918c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik ClipPathOp* op = new (handler.allocator()) ClipPathOp(path, SkRegion::kIntersect_Op); 192d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds()); 193113e0824d6bddf4376240681f9cf6a2deded9498John Reck } 194113e0824d6bddf4376240681f9cf6a2deded9498John Reck} 195113e0824d6bddf4376240681f9cf6a2deded9498John Reck 196113e0824d6bddf4376240681f9cf6a2deded9498John Reck/** 197113e0824d6bddf4376240681f9cf6a2deded9498John Reck * Apply property-based transformations to input matrix 198113e0824d6bddf4376240681f9cf6a2deded9498John Reck * 199113e0824d6bddf4376240681f9cf6a2deded9498John Reck * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4 200113e0824d6bddf4376240681f9cf6a2deded9498John Reck * matrix computation instead of the Skia 3x3 matrix + camera hackery. 201113e0824d6bddf4376240681f9cf6a2deded9498John Reck */ 202113e0824d6bddf4376240681f9cf6a2deded9498John Reckvoid RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) { 203d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck if (properties().getLeft() != 0 || properties().getTop() != 0) { 204d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck matrix.translate(properties().getLeft(), properties().getTop()); 205113e0824d6bddf4376240681f9cf6a2deded9498John Reck } 206d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck if (properties().getStaticMatrix()) { 207d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck mat4 stat(*properties().getStaticMatrix()); 208113e0824d6bddf4376240681f9cf6a2deded9498John Reck matrix.multiply(stat); 209d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck } else if (properties().getAnimationMatrix()) { 210d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck mat4 anim(*properties().getAnimationMatrix()); 211113e0824d6bddf4376240681f9cf6a2deded9498John Reck matrix.multiply(anim); 212113e0824d6bddf4376240681f9cf6a2deded9498John Reck } 213d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck if (properties().getMatrixFlags() != 0) { 214d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck if (properties().getMatrixFlags() == TRANSLATION) { 215d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck matrix.translate(properties().getTranslationX(), properties().getTranslationY(), 216d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck true3dTransform ? properties().getTranslationZ() : 0.0f); 217113e0824d6bddf4376240681f9cf6a2deded9498John Reck } else { 218113e0824d6bddf4376240681f9cf6a2deded9498John Reck if (!true3dTransform) { 219d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck matrix.multiply(*properties().getTransformMatrix()); 220113e0824d6bddf4376240681f9cf6a2deded9498John Reck } else { 221113e0824d6bddf4376240681f9cf6a2deded9498John Reck mat4 true3dMat; 222113e0824d6bddf4376240681f9cf6a2deded9498John Reck true3dMat.loadTranslate( 223d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck properties().getPivotX() + properties().getTranslationX(), 224d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck properties().getPivotY() + properties().getTranslationY(), 225d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck properties().getTranslationZ()); 226d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck true3dMat.rotate(properties().getRotationX(), 1, 0, 0); 227d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck true3dMat.rotate(properties().getRotationY(), 0, 1, 0); 228d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck true3dMat.rotate(properties().getRotation(), 0, 0, 1); 229d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1); 230d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck true3dMat.translate(-properties().getPivotX(), -properties().getPivotY()); 231113e0824d6bddf4376240681f9cf6a2deded9498John Reck 232113e0824d6bddf4376240681f9cf6a2deded9498John Reck matrix.multiply(true3dMat); 233113e0824d6bddf4376240681f9cf6a2deded9498John Reck } 234113e0824d6bddf4376240681f9cf6a2deded9498John Reck } 235113e0824d6bddf4376240681f9cf6a2deded9498John Reck } 236113e0824d6bddf4376240681f9cf6a2deded9498John Reck} 237113e0824d6bddf4376240681f9cf6a2deded9498John Reck 238113e0824d6bddf4376240681f9cf6a2deded9498John Reck/** 239113e0824d6bddf4376240681f9cf6a2deded9498John Reck * Organizes the DisplayList hierarchy to prepare for background projection reordering. 240113e0824d6bddf4376240681f9cf6a2deded9498John Reck * 241113e0824d6bddf4376240681f9cf6a2deded9498John Reck * This should be called before a call to defer() or drawDisplayList() 242113e0824d6bddf4376240681f9cf6a2deded9498John Reck * 243113e0824d6bddf4376240681f9cf6a2deded9498John Reck * Each DisplayList that serves as a 3d root builds its list of composited children, 244113e0824d6bddf4376240681f9cf6a2deded9498John Reck * which are flagged to not draw in the standard draw loop. 245113e0824d6bddf4376240681f9cf6a2deded9498John Reck */ 246113e0824d6bddf4376240681f9cf6a2deded9498John Reckvoid RenderNode::computeOrdering() { 247113e0824d6bddf4376240681f9cf6a2deded9498John Reck ATRACE_CALL(); 248113e0824d6bddf4376240681f9cf6a2deded9498John Reck mProjectedNodes.clear(); 249113e0824d6bddf4376240681f9cf6a2deded9498John Reck 250113e0824d6bddf4376240681f9cf6a2deded9498John Reck // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that 251113e0824d6bddf4376240681f9cf6a2deded9498John Reck // transform properties are applied correctly to top level children 252113e0824d6bddf4376240681f9cf6a2deded9498John Reck if (mDisplayListData == NULL) return; 253087bc0c14bdccf7c258dce0cdef46a69a839b427John Reck for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) { 254087bc0c14bdccf7c258dce0cdef46a69a839b427John Reck DrawDisplayListOp* childOp = mDisplayListData->children()[i]; 255113e0824d6bddf4376240681f9cf6a2deded9498John Reck childOp->mDisplayList->computeOrderingImpl(childOp, 256113e0824d6bddf4376240681f9cf6a2deded9498John Reck &mProjectedNodes, &mat4::identity()); 257113e0824d6bddf4376240681f9cf6a2deded9498John Reck } 258113e0824d6bddf4376240681f9cf6a2deded9498John Reck} 259113e0824d6bddf4376240681f9cf6a2deded9498John Reck 260113e0824d6bddf4376240681f9cf6a2deded9498John Reckvoid RenderNode::computeOrderingImpl( 261113e0824d6bddf4376240681f9cf6a2deded9498John Reck DrawDisplayListOp* opState, 262113e0824d6bddf4376240681f9cf6a2deded9498John Reck Vector<DrawDisplayListOp*>* compositedChildrenOfProjectionSurface, 263113e0824d6bddf4376240681f9cf6a2deded9498John Reck const mat4* transformFromProjectionSurface) { 264113e0824d6bddf4376240681f9cf6a2deded9498John Reck mProjectedNodes.clear(); 265113e0824d6bddf4376240681f9cf6a2deded9498John Reck if (mDisplayListData == NULL || mDisplayListData->isEmpty()) return; 266113e0824d6bddf4376240681f9cf6a2deded9498John Reck 267113e0824d6bddf4376240681f9cf6a2deded9498John Reck // TODO: should avoid this calculation in most cases 268113e0824d6bddf4376240681f9cf6a2deded9498John Reck // TODO: just calculate single matrix, down to all leaf composited elements 269113e0824d6bddf4376240681f9cf6a2deded9498John Reck Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface); 270113e0824d6bddf4376240681f9cf6a2deded9498John Reck localTransformFromProjectionSurface.multiply(opState->mTransformFromParent); 271113e0824d6bddf4376240681f9cf6a2deded9498John Reck 272d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck if (properties().getProjectBackwards()) { 273113e0824d6bddf4376240681f9cf6a2deded9498John Reck // composited projectee, flag for out of order draw, save matrix, and store in proj surface 274113e0824d6bddf4376240681f9cf6a2deded9498John Reck opState->mSkipInOrderDraw = true; 275113e0824d6bddf4376240681f9cf6a2deded9498John Reck opState->mTransformFromCompositingAncestor.load(localTransformFromProjectionSurface); 276113e0824d6bddf4376240681f9cf6a2deded9498John Reck compositedChildrenOfProjectionSurface->add(opState); 277113e0824d6bddf4376240681f9cf6a2deded9498John Reck } else { 278113e0824d6bddf4376240681f9cf6a2deded9498John Reck // standard in order draw 279113e0824d6bddf4376240681f9cf6a2deded9498John Reck opState->mSkipInOrderDraw = false; 280113e0824d6bddf4376240681f9cf6a2deded9498John Reck } 281113e0824d6bddf4376240681f9cf6a2deded9498John Reck 282087bc0c14bdccf7c258dce0cdef46a69a839b427John Reck if (mDisplayListData->children().size() > 0) { 283113e0824d6bddf4376240681f9cf6a2deded9498John Reck const bool isProjectionReceiver = mDisplayListData->projectionReceiveIndex >= 0; 284113e0824d6bddf4376240681f9cf6a2deded9498John Reck bool haveAppliedPropertiesToProjection = false; 285087bc0c14bdccf7c258dce0cdef46a69a839b427John Reck for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) { 286087bc0c14bdccf7c258dce0cdef46a69a839b427John Reck DrawDisplayListOp* childOp = mDisplayListData->children()[i]; 287113e0824d6bddf4376240681f9cf6a2deded9498John Reck RenderNode* child = childOp->mDisplayList; 288113e0824d6bddf4376240681f9cf6a2deded9498John Reck 289113e0824d6bddf4376240681f9cf6a2deded9498John Reck Vector<DrawDisplayListOp*>* projectionChildren = NULL; 290113e0824d6bddf4376240681f9cf6a2deded9498John Reck const mat4* projectionTransform = NULL; 291d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck if (isProjectionReceiver && !child->properties().getProjectBackwards()) { 292113e0824d6bddf4376240681f9cf6a2deded9498John Reck // if receiving projections, collect projecting descendent 293113e0824d6bddf4376240681f9cf6a2deded9498John Reck 294113e0824d6bddf4376240681f9cf6a2deded9498John Reck // Note that if a direct descendent is projecting backwards, we pass it's 295113e0824d6bddf4376240681f9cf6a2deded9498John Reck // grandparent projection collection, since it shouldn't project onto it's 296113e0824d6bddf4376240681f9cf6a2deded9498John Reck // parent, where it will already be drawing. 297113e0824d6bddf4376240681f9cf6a2deded9498John Reck projectionChildren = &mProjectedNodes; 298113e0824d6bddf4376240681f9cf6a2deded9498John Reck projectionTransform = &mat4::identity(); 299113e0824d6bddf4376240681f9cf6a2deded9498John Reck } else { 300113e0824d6bddf4376240681f9cf6a2deded9498John Reck if (!haveAppliedPropertiesToProjection) { 301113e0824d6bddf4376240681f9cf6a2deded9498John Reck applyViewPropertyTransforms(localTransformFromProjectionSurface); 302113e0824d6bddf4376240681f9cf6a2deded9498John Reck haveAppliedPropertiesToProjection = true; 303113e0824d6bddf4376240681f9cf6a2deded9498John Reck } 304113e0824d6bddf4376240681f9cf6a2deded9498John Reck projectionChildren = compositedChildrenOfProjectionSurface; 305113e0824d6bddf4376240681f9cf6a2deded9498John Reck projectionTransform = &localTransformFromProjectionSurface; 306113e0824d6bddf4376240681f9cf6a2deded9498John Reck } 307113e0824d6bddf4376240681f9cf6a2deded9498John Reck child->computeOrderingImpl(childOp, projectionChildren, projectionTransform); 308113e0824d6bddf4376240681f9cf6a2deded9498John Reck } 309113e0824d6bddf4376240681f9cf6a2deded9498John Reck } 310113e0824d6bddf4376240681f9cf6a2deded9498John Reck} 311113e0824d6bddf4376240681f9cf6a2deded9498John Reck 312113e0824d6bddf4376240681f9cf6a2deded9498John Reckclass DeferOperationHandler { 313113e0824d6bddf4376240681f9cf6a2deded9498John Reckpublic: 314113e0824d6bddf4376240681f9cf6a2deded9498John Reck DeferOperationHandler(DeferStateStruct& deferStruct, int level) 315113e0824d6bddf4376240681f9cf6a2deded9498John Reck : mDeferStruct(deferStruct), mLevel(level) {} 316113e0824d6bddf4376240681f9cf6a2deded9498John Reck inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) { 317113e0824d6bddf4376240681f9cf6a2deded9498John Reck operation->defer(mDeferStruct, saveCount, mLevel, clipToBounds); 318113e0824d6bddf4376240681f9cf6a2deded9498John Reck } 319113e0824d6bddf4376240681f9cf6a2deded9498John Reck inline LinearAllocator& allocator() { return *(mDeferStruct.mAllocator); } 320b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik inline void startMark(const char* name) {} // do nothing 321b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik inline void endMark() {} 322b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik inline int level() { return mLevel; } 323b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik inline int replayFlags() { return mDeferStruct.mReplayFlags; } 324113e0824d6bddf4376240681f9cf6a2deded9498John Reck 325113e0824d6bddf4376240681f9cf6a2deded9498John Reckprivate: 326113e0824d6bddf4376240681f9cf6a2deded9498John Reck DeferStateStruct& mDeferStruct; 327113e0824d6bddf4376240681f9cf6a2deded9498John Reck const int mLevel; 328113e0824d6bddf4376240681f9cf6a2deded9498John Reck}; 329113e0824d6bddf4376240681f9cf6a2deded9498John Reck 330b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craikvoid RenderNode::deferNodeTree(DeferStateStruct& deferStruct) { 331b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik DeferOperationHandler handler(deferStruct, 0); 332b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik if (properties().getTranslationZ() > 0.0f) issueDrawShadowOperation(Matrix4::identity(), handler); 333b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler); 334b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik} 335b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik 336b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craikvoid RenderNode::deferNodeInParent(DeferStateStruct& deferStruct, const int level) { 337113e0824d6bddf4376240681f9cf6a2deded9498John Reck DeferOperationHandler handler(deferStruct, level); 338b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler); 339113e0824d6bddf4376240681f9cf6a2deded9498John Reck} 340113e0824d6bddf4376240681f9cf6a2deded9498John Reck 341113e0824d6bddf4376240681f9cf6a2deded9498John Reckclass ReplayOperationHandler { 342113e0824d6bddf4376240681f9cf6a2deded9498John Reckpublic: 343113e0824d6bddf4376240681f9cf6a2deded9498John Reck ReplayOperationHandler(ReplayStateStruct& replayStruct, int level) 344113e0824d6bddf4376240681f9cf6a2deded9498John Reck : mReplayStruct(replayStruct), mLevel(level) {} 345113e0824d6bddf4376240681f9cf6a2deded9498John Reck inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) { 346113e0824d6bddf4376240681f9cf6a2deded9498John Reck#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS 347d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck properties().getReplayStruct().mRenderer.eventMark(operation->name()); 348113e0824d6bddf4376240681f9cf6a2deded9498John Reck#endif 349113e0824d6bddf4376240681f9cf6a2deded9498John Reck operation->replay(mReplayStruct, saveCount, mLevel, clipToBounds); 350113e0824d6bddf4376240681f9cf6a2deded9498John Reck } 351113e0824d6bddf4376240681f9cf6a2deded9498John Reck inline LinearAllocator& allocator() { return *(mReplayStruct.mAllocator); } 352b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik inline void startMark(const char* name) { 353b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik mReplayStruct.mRenderer.startMark(name); 354b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik } 355b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik inline void endMark() { 356b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik mReplayStruct.mRenderer.endMark(); 357b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik DISPLAY_LIST_LOGD("%*sDone (%p, %s), returning %d", level * 2, "", this, mName.string(), 358b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik mReplayStruct.mDrawGlStatus); 359b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik } 360b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik inline int level() { return mLevel; } 361b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik inline int replayFlags() { return mReplayStruct.mReplayFlags; } 362113e0824d6bddf4376240681f9cf6a2deded9498John Reck 363113e0824d6bddf4376240681f9cf6a2deded9498John Reckprivate: 364113e0824d6bddf4376240681f9cf6a2deded9498John Reck ReplayStateStruct& mReplayStruct; 365113e0824d6bddf4376240681f9cf6a2deded9498John Reck const int mLevel; 366113e0824d6bddf4376240681f9cf6a2deded9498John Reck}; 367113e0824d6bddf4376240681f9cf6a2deded9498John Reck 368b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craikvoid RenderNode::replayNodeTree(ReplayStateStruct& replayStruct) { 369b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik ReplayOperationHandler handler(replayStruct, 0); 370b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik if (properties().getTranslationZ() > 0.0f) issueDrawShadowOperation(Matrix4::identity(), handler); 371b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler); 372b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik} 373113e0824d6bddf4376240681f9cf6a2deded9498John Reck 374b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craikvoid RenderNode::replayNodeInParent(ReplayStateStruct& replayStruct, const int level) { 375b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik ReplayOperationHandler handler(replayStruct, level); 376b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler); 377113e0824d6bddf4376240681f9cf6a2deded9498John Reck} 378113e0824d6bddf4376240681f9cf6a2deded9498John Reck 379113e0824d6bddf4376240681f9cf6a2deded9498John Reckvoid RenderNode::buildZSortedChildList(Vector<ZDrawDisplayListOpPair>& zTranslatedNodes) { 380087bc0c14bdccf7c258dce0cdef46a69a839b427John Reck if (mDisplayListData == NULL || mDisplayListData->children().size() == 0) return; 381113e0824d6bddf4376240681f9cf6a2deded9498John Reck 382087bc0c14bdccf7c258dce0cdef46a69a839b427John Reck for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) { 383087bc0c14bdccf7c258dce0cdef46a69a839b427John Reck DrawDisplayListOp* childOp = mDisplayListData->children()[i]; 384113e0824d6bddf4376240681f9cf6a2deded9498John Reck RenderNode* child = childOp->mDisplayList; 385d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck float childZ = child->properties().getTranslationZ(); 386113e0824d6bddf4376240681f9cf6a2deded9498John Reck 387113e0824d6bddf4376240681f9cf6a2deded9498John Reck if (childZ != 0.0f) { 388113e0824d6bddf4376240681f9cf6a2deded9498John Reck zTranslatedNodes.add(ZDrawDisplayListOpPair(childZ, childOp)); 389113e0824d6bddf4376240681f9cf6a2deded9498John Reck childOp->mSkipInOrderDraw = true; 390d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck } else if (!child->properties().getProjectBackwards()) { 391113e0824d6bddf4376240681f9cf6a2deded9498John Reck // regular, in order drawing DisplayList 392113e0824d6bddf4376240681f9cf6a2deded9498John Reck childOp->mSkipInOrderDraw = false; 393113e0824d6bddf4376240681f9cf6a2deded9498John Reck } 394113e0824d6bddf4376240681f9cf6a2deded9498John Reck } 395113e0824d6bddf4376240681f9cf6a2deded9498John Reck 396113e0824d6bddf4376240681f9cf6a2deded9498John Reck // Z sort 3d children (stable-ness makes z compare fall back to standard drawing order) 397113e0824d6bddf4376240681f9cf6a2deded9498John Reck std::stable_sort(zTranslatedNodes.begin(), zTranslatedNodes.end()); 398113e0824d6bddf4376240681f9cf6a2deded9498John Reck} 399113e0824d6bddf4376240681f9cf6a2deded9498John Reck 400b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craiktemplate <class T> 401b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craikvoid RenderNode::issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler) { 402b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik if (properties().getAlpha() <= 0.0f) return; 403b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik 404b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik mat4 shadowMatrixXY(transformFromParent); 405b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik applyViewPropertyTransforms(shadowMatrixXY); 406b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik 407b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik // Z matrix needs actual 3d transformation, so mapped z values will be correct 408b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik mat4 shadowMatrixZ(transformFromParent); 409b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik applyViewPropertyTransforms(shadowMatrixZ, true); 410b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik 411b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik const SkPath* outlinePath = properties().getOutline().getPath(); 412b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik const RevealClip& revealClip = properties().getRevealClip(); 413b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik const SkPath* revealClipPath = revealClip.hasConvexClip() 414b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik ? revealClip.getPath() : NULL; // only pass the reveal clip's path if it's convex 415b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik 416b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik /** 417b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik * The drawing area of the caster is always the same as the its perimeter (which 418b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik * the shadow system uses) *except* in the inverse clip case. Inform the shadow 419b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik * system that the caster's drawing area (as opposed to its perimeter) has been 420b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik * clipped, so that it knows the caster can't be opaque. 421b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik */ 422b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik bool casterUnclipped = !revealClip.willClip() || revealClip.hasConvexClip(); 423b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik 424b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik DisplayListOp* shadowOp = new (handler.allocator()) DrawShadowOp( 425b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik shadowMatrixXY, shadowMatrixZ, 426b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik properties().getAlpha(), casterUnclipped, 427b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik properties().getWidth(), properties().getHeight(), 428b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik outlinePath, revealClipPath); 429b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik handler(shadowOp, PROPERTY_SAVECOUNT, properties().getClipToBounds()); 430b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik} 431b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik 432113e0824d6bddf4376240681f9cf6a2deded9498John Reck#define SHADOW_DELTA 0.1f 433113e0824d6bddf4376240681f9cf6a2deded9498John Reck 434113e0824d6bddf4376240681f9cf6a2deded9498John Recktemplate <class T> 435b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craikvoid RenderNode::issueOperationsOf3dChildren(const Vector<ZDrawDisplayListOpPair>& zTranslatedNodes, 436113e0824d6bddf4376240681f9cf6a2deded9498John Reck ChildrenSelectMode mode, OpenGLRenderer& renderer, T& handler) { 437113e0824d6bddf4376240681f9cf6a2deded9498John Reck const int size = zTranslatedNodes.size(); 438113e0824d6bddf4376240681f9cf6a2deded9498John Reck if (size == 0 439113e0824d6bddf4376240681f9cf6a2deded9498John Reck || (mode == kNegativeZChildren && zTranslatedNodes[0].key > 0.0f) 440113e0824d6bddf4376240681f9cf6a2deded9498John Reck || (mode == kPositiveZChildren && zTranslatedNodes[size - 1].key < 0.0f)) { 441113e0824d6bddf4376240681f9cf6a2deded9498John Reck // no 3d children to draw 442113e0824d6bddf4376240681f9cf6a2deded9498John Reck return; 443113e0824d6bddf4376240681f9cf6a2deded9498John Reck } 444113e0824d6bddf4376240681f9cf6a2deded9498John Reck 445113e0824d6bddf4376240681f9cf6a2deded9498John Reck /** 446113e0824d6bddf4376240681f9cf6a2deded9498John Reck * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters 447113e0824d6bddf4376240681f9cf6a2deded9498John Reck * with very similar Z heights to draw together. 448113e0824d6bddf4376240681f9cf6a2deded9498John Reck * 449113e0824d6bddf4376240681f9cf6a2deded9498John Reck * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are 450113e0824d6bddf4376240681f9cf6a2deded9498John Reck * underneath both, and neither's shadow is drawn on top of the other. 451113e0824d6bddf4376240681f9cf6a2deded9498John Reck */ 452113e0824d6bddf4376240681f9cf6a2deded9498John Reck const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes); 453113e0824d6bddf4376240681f9cf6a2deded9498John Reck size_t drawIndex, shadowIndex, endIndex; 454113e0824d6bddf4376240681f9cf6a2deded9498John Reck if (mode == kNegativeZChildren) { 455113e0824d6bddf4376240681f9cf6a2deded9498John Reck drawIndex = 0; 456113e0824d6bddf4376240681f9cf6a2deded9498John Reck endIndex = nonNegativeIndex; 457113e0824d6bddf4376240681f9cf6a2deded9498John Reck shadowIndex = endIndex; // draw no shadows 458113e0824d6bddf4376240681f9cf6a2deded9498John Reck } else { 459113e0824d6bddf4376240681f9cf6a2deded9498John Reck drawIndex = nonNegativeIndex; 460113e0824d6bddf4376240681f9cf6a2deded9498John Reck endIndex = size; 461113e0824d6bddf4376240681f9cf6a2deded9498John Reck shadowIndex = drawIndex; // potentially draw shadow for each pos Z child 462113e0824d6bddf4376240681f9cf6a2deded9498John Reck } 463113e0824d6bddf4376240681f9cf6a2deded9498John Reck float lastCasterZ = 0.0f; 464113e0824d6bddf4376240681f9cf6a2deded9498John Reck while (shadowIndex < endIndex || drawIndex < endIndex) { 465113e0824d6bddf4376240681f9cf6a2deded9498John Reck if (shadowIndex < endIndex) { 466113e0824d6bddf4376240681f9cf6a2deded9498John Reck DrawDisplayListOp* casterOp = zTranslatedNodes[shadowIndex].value; 467113e0824d6bddf4376240681f9cf6a2deded9498John Reck RenderNode* caster = casterOp->mDisplayList; 468113e0824d6bddf4376240681f9cf6a2deded9498John Reck const float casterZ = zTranslatedNodes[shadowIndex].key; 469113e0824d6bddf4376240681f9cf6a2deded9498John Reck // attempt to render the shadow if the caster about to be drawn is its caster, 470113e0824d6bddf4376240681f9cf6a2deded9498John Reck // OR if its caster's Z value is similar to the previous potential caster 471113e0824d6bddf4376240681f9cf6a2deded9498John Reck if (shadowIndex == drawIndex || casterZ - lastCasterZ < SHADOW_DELTA) { 472b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik caster->issueDrawShadowOperation(casterOp->mTransformFromParent, handler); 473113e0824d6bddf4376240681f9cf6a2deded9498John Reck 474113e0824d6bddf4376240681f9cf6a2deded9498John Reck lastCasterZ = casterZ; // must do this even if current caster not casting a shadow 475113e0824d6bddf4376240681f9cf6a2deded9498John Reck shadowIndex++; 476113e0824d6bddf4376240681f9cf6a2deded9498John Reck continue; 477113e0824d6bddf4376240681f9cf6a2deded9498John Reck } 478113e0824d6bddf4376240681f9cf6a2deded9498John Reck } 479113e0824d6bddf4376240681f9cf6a2deded9498John Reck 480113e0824d6bddf4376240681f9cf6a2deded9498John Reck // only the actual child DL draw needs to be in save/restore, 481113e0824d6bddf4376240681f9cf6a2deded9498John Reck // since it modifies the renderer's matrix 482113e0824d6bddf4376240681f9cf6a2deded9498John Reck int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag); 483113e0824d6bddf4376240681f9cf6a2deded9498John Reck 484113e0824d6bddf4376240681f9cf6a2deded9498John Reck DrawDisplayListOp* childOp = zTranslatedNodes[drawIndex].value; 485113e0824d6bddf4376240681f9cf6a2deded9498John Reck RenderNode* child = childOp->mDisplayList; 486113e0824d6bddf4376240681f9cf6a2deded9498John Reck 487113e0824d6bddf4376240681f9cf6a2deded9498John Reck renderer.concatMatrix(childOp->mTransformFromParent); 488113e0824d6bddf4376240681f9cf6a2deded9498John Reck childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone 489d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds()); 490113e0824d6bddf4376240681f9cf6a2deded9498John Reck childOp->mSkipInOrderDraw = true; 491113e0824d6bddf4376240681f9cf6a2deded9498John Reck 492113e0824d6bddf4376240681f9cf6a2deded9498John Reck renderer.restoreToCount(restoreTo); 493113e0824d6bddf4376240681f9cf6a2deded9498John Reck drawIndex++; 494113e0824d6bddf4376240681f9cf6a2deded9498John Reck } 495113e0824d6bddf4376240681f9cf6a2deded9498John Reck} 496113e0824d6bddf4376240681f9cf6a2deded9498John Reck 497113e0824d6bddf4376240681f9cf6a2deded9498John Recktemplate <class T> 498b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craikvoid RenderNode::issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler) { 499113e0824d6bddf4376240681f9cf6a2deded9498John Reck for (size_t i = 0; i < mProjectedNodes.size(); i++) { 500113e0824d6bddf4376240681f9cf6a2deded9498John Reck DrawDisplayListOp* childOp = mProjectedNodes[i]; 501113e0824d6bddf4376240681f9cf6a2deded9498John Reck 502113e0824d6bddf4376240681f9cf6a2deded9498John Reck // matrix save, concat, and restore can be done safely without allocating operations 503113e0824d6bddf4376240681f9cf6a2deded9498John Reck int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag); 504113e0824d6bddf4376240681f9cf6a2deded9498John Reck renderer.concatMatrix(childOp->mTransformFromCompositingAncestor); 505113e0824d6bddf4376240681f9cf6a2deded9498John Reck childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone 506d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds()); 507113e0824d6bddf4376240681f9cf6a2deded9498John Reck childOp->mSkipInOrderDraw = true; 508113e0824d6bddf4376240681f9cf6a2deded9498John Reck renderer.restoreToCount(restoreTo); 509113e0824d6bddf4376240681f9cf6a2deded9498John Reck } 510113e0824d6bddf4376240681f9cf6a2deded9498John Reck} 511113e0824d6bddf4376240681f9cf6a2deded9498John Reck 512113e0824d6bddf4376240681f9cf6a2deded9498John Reck/** 513113e0824d6bddf4376240681f9cf6a2deded9498John Reck * This function serves both defer and replay modes, and will organize the displayList's component 514113e0824d6bddf4376240681f9cf6a2deded9498John Reck * operations for a single frame: 515113e0824d6bddf4376240681f9cf6a2deded9498John Reck * 516113e0824d6bddf4376240681f9cf6a2deded9498John Reck * Every 'simple' state operation that affects just the matrix and alpha (or other factors of 517113e0824d6bddf4376240681f9cf6a2deded9498John Reck * DeferredDisplayState) may be issued directly to the renderer, but complex operations (with custom 518113e0824d6bddf4376240681f9cf6a2deded9498John Reck * defer logic) and operations in displayListOps are issued through the 'handler' which handles the 519113e0824d6bddf4376240681f9cf6a2deded9498John Reck * defer vs replay logic, per operation 520113e0824d6bddf4376240681f9cf6a2deded9498John Reck */ 521113e0824d6bddf4376240681f9cf6a2deded9498John Recktemplate <class T> 522b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craikvoid RenderNode::issueOperations(OpenGLRenderer& renderer, T& handler) { 523b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik const int level = handler.level(); 524d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck if (mDisplayListData->isEmpty() || properties().getAlpha() <= 0) { 525113e0824d6bddf4376240681f9cf6a2deded9498John Reck DISPLAY_LIST_LOGD("%*sEmpty display list (%p, %s)", level * 2, "", this, mName.string()); 526113e0824d6bddf4376240681f9cf6a2deded9498John Reck return; 527113e0824d6bddf4376240681f9cf6a2deded9498John Reck } 528113e0824d6bddf4376240681f9cf6a2deded9498John Reck 529b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik handler.startMark(mName.string()); 530b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik 531113e0824d6bddf4376240681f9cf6a2deded9498John Reck#if DEBUG_DISPLAY_LIST 532113e0824d6bddf4376240681f9cf6a2deded9498John Reck Rect* clipRect = renderer.getClipRect(); 533113e0824d6bddf4376240681f9cf6a2deded9498John Reck DISPLAY_LIST_LOGD("%*sStart display list (%p, %s), clipRect: %.0f, %.0f, %.0f, %.0f", 534113e0824d6bddf4376240681f9cf6a2deded9498John Reck level * 2, "", this, mName.string(), clipRect->left, clipRect->top, 535113e0824d6bddf4376240681f9cf6a2deded9498John Reck clipRect->right, clipRect->bottom); 536113e0824d6bddf4376240681f9cf6a2deded9498John Reck#endif 537113e0824d6bddf4376240681f9cf6a2deded9498John Reck 538113e0824d6bddf4376240681f9cf6a2deded9498John Reck LinearAllocator& alloc = handler.allocator(); 539113e0824d6bddf4376240681f9cf6a2deded9498John Reck int restoreTo = renderer.getSaveCount(); 540113e0824d6bddf4376240681f9cf6a2deded9498John Reck handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag), 541d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck PROPERTY_SAVECOUNT, properties().getClipToBounds()); 542113e0824d6bddf4376240681f9cf6a2deded9498John Reck 543113e0824d6bddf4376240681f9cf6a2deded9498John Reck DISPLAY_LIST_LOGD("%*sSave %d %d", (level + 1) * 2, "", 544113e0824d6bddf4376240681f9cf6a2deded9498John Reck SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag, restoreTo); 545113e0824d6bddf4376240681f9cf6a2deded9498John Reck 546b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik setViewProperties<T>(renderer, handler); 547113e0824d6bddf4376240681f9cf6a2deded9498John Reck 5488c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik bool quickRejected = properties().getClipToBounds() 5498c271ca63b62061fd22cfee78fd6a574b44476fdChris Craik && renderer.quickRejectConservative(0, 0, properties().getWidth(), properties().getHeight()); 550113e0824d6bddf4376240681f9cf6a2deded9498John Reck if (!quickRejected) { 551113e0824d6bddf4376240681f9cf6a2deded9498John Reck Vector<ZDrawDisplayListOpPair> zTranslatedNodes; 552113e0824d6bddf4376240681f9cf6a2deded9498John Reck buildZSortedChildList(zTranslatedNodes); 553113e0824d6bddf4376240681f9cf6a2deded9498John Reck 554113e0824d6bddf4376240681f9cf6a2deded9498John Reck // for 3d root, draw children with negative z values 555b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik issueOperationsOf3dChildren(zTranslatedNodes, kNegativeZChildren, renderer, handler); 556113e0824d6bddf4376240681f9cf6a2deded9498John Reck 557113e0824d6bddf4376240681f9cf6a2deded9498John Reck DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance(); 558113e0824d6bddf4376240681f9cf6a2deded9498John Reck const int saveCountOffset = renderer.getSaveCount() - 1; 559113e0824d6bddf4376240681f9cf6a2deded9498John Reck const int projectionReceiveIndex = mDisplayListData->projectionReceiveIndex; 560113e0824d6bddf4376240681f9cf6a2deded9498John Reck for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) { 561113e0824d6bddf4376240681f9cf6a2deded9498John Reck DisplayListOp *op = mDisplayListData->displayListOps[i]; 562113e0824d6bddf4376240681f9cf6a2deded9498John Reck 563113e0824d6bddf4376240681f9cf6a2deded9498John Reck#if DEBUG_DISPLAY_LIST 564113e0824d6bddf4376240681f9cf6a2deded9498John Reck op->output(level + 1); 565113e0824d6bddf4376240681f9cf6a2deded9498John Reck#endif 566113e0824d6bddf4376240681f9cf6a2deded9498John Reck logBuffer.writeCommand(level, op->name()); 567d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck handler(op, saveCountOffset, properties().getClipToBounds()); 568113e0824d6bddf4376240681f9cf6a2deded9498John Reck 569113e0824d6bddf4376240681f9cf6a2deded9498John Reck if (CC_UNLIKELY(i == projectionReceiveIndex && mProjectedNodes.size() > 0)) { 570b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik issueOperationsOfProjectedChildren(renderer, handler); 571113e0824d6bddf4376240681f9cf6a2deded9498John Reck } 572113e0824d6bddf4376240681f9cf6a2deded9498John Reck } 573113e0824d6bddf4376240681f9cf6a2deded9498John Reck 574113e0824d6bddf4376240681f9cf6a2deded9498John Reck // for 3d root, draw children with positive z values 575b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik issueOperationsOf3dChildren(zTranslatedNodes, kPositiveZChildren, renderer, handler); 576113e0824d6bddf4376240681f9cf6a2deded9498John Reck } 577113e0824d6bddf4376240681f9cf6a2deded9498John Reck 578113e0824d6bddf4376240681f9cf6a2deded9498John Reck DISPLAY_LIST_LOGD("%*sRestoreToCount %d", (level + 1) * 2, "", restoreTo); 579113e0824d6bddf4376240681f9cf6a2deded9498John Reck handler(new (alloc) RestoreToCountOp(restoreTo), 580d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck PROPERTY_SAVECOUNT, properties().getClipToBounds()); 581113e0824d6bddf4376240681f9cf6a2deded9498John Reck renderer.setOverrideLayerAlpha(1.0f); 582b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik 583b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik handler.endMark(); 584113e0824d6bddf4376240681f9cf6a2deded9498John Reck} 585113e0824d6bddf4376240681f9cf6a2deded9498John Reck 586113e0824d6bddf4376240681f9cf6a2deded9498John Reck} /* namespace uirenderer */ 587113e0824d6bddf4376240681f9cf6a2deded9498John Reck} /* namespace android */ 588