RenderNode.cpp revision 139088228faa7f3c446af7387e017933998a5570
1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define ATRACE_TAG ATRACE_TAG_VIEW
18
19#include "RenderNode.h"
20
21#include <algorithm>
22
23#include <SkCanvas.h>
24#include <algorithm>
25
26#include <utils/Trace.h>
27
28#include "Debug.h"
29#include "DisplayListOp.h"
30#include "DisplayListLogBuffer.h"
31#include "utils/MathUtils.h"
32
33namespace android {
34namespace uirenderer {
35
36void RenderNode::outputLogBuffer(int fd) {
37    DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance();
38    if (logBuffer.isEmpty()) {
39        return;
40    }
41
42    FILE *file = fdopen(fd, "a");
43
44    fprintf(file, "\nRecent DisplayList operations\n");
45    logBuffer.outputCommands(file);
46
47    String8 cachesLog;
48    Caches::getInstance().dumpMemoryUsage(cachesLog);
49    fprintf(file, "\nCaches:\n%s", cachesLog.string());
50    fprintf(file, "\n");
51
52    fflush(file);
53}
54
55RenderNode::RenderNode()
56        : mDirtyPropertyFields(0)
57        , mNeedsDisplayListDataSync(false)
58        , mDisplayListData(0)
59        , mStagingDisplayListData(0)
60        , mNeedsAnimatorsSync(false) {
61}
62
63RenderNode::~RenderNode() {
64    delete mDisplayListData;
65    delete mStagingDisplayListData;
66}
67
68void RenderNode::setStagingDisplayList(DisplayListData* data) {
69    mNeedsDisplayListDataSync = true;
70    delete mStagingDisplayListData;
71    mStagingDisplayListData = data;
72    if (mStagingDisplayListData) {
73        Caches::getInstance().registerFunctors(mStagingDisplayListData->functorCount);
74    }
75}
76
77/**
78 * This function is a simplified version of replay(), where we simply retrieve and log the
79 * display list. This function should remain in sync with the replay() function.
80 */
81void RenderNode::output(uint32_t level) {
82    ALOGD("%*sStart display list (%p, %s, render=%d)", (level - 1) * 2, "", this,
83            getName(), isRenderable());
84    ALOGD("%*s%s %d", level * 2, "", "Save",
85            SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
86
87    properties().debugOutputProperties(level);
88    int flags = DisplayListOp::kOpLogFlag_Recurse;
89    for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) {
90        mDisplayListData->displayListOps[i]->output(level, flags);
91    }
92
93    ALOGD("%*sDone (%p, %s)", (level - 1) * 2, "", this, getName());
94}
95
96void RenderNode::prepareTree(TreeInfo& info) {
97    ATRACE_CALL();
98
99    prepareTreeImpl(info);
100}
101
102void RenderNode::prepareTreeImpl(TreeInfo& info) {
103    if (info.performStagingPush) {
104        pushStagingChanges(info);
105    }
106    if (info.evaluateAnimations) {
107        evaluateAnimations(info);
108    }
109    prepareSubTree(info, mDisplayListData);
110}
111
112class PushAnimatorsFunctor {
113public:
114    PushAnimatorsFunctor(RenderNode* target, TreeInfo& info)
115            : mTarget(target), mInfo(info) {}
116
117    bool operator() (const sp<BaseRenderNodeAnimator>& animator) {
118        animator->setupStartValueIfNecessary(mTarget, mInfo);
119        return animator->isFinished();
120    }
121private:
122    RenderNode* mTarget;
123    TreeInfo& mInfo;
124};
125
126void RenderNode::pushStagingChanges(TreeInfo& info) {
127    // Push the animators first so that setupStartValueIfNecessary() is called
128    // before properties() is trampled by stagingProperties(), as they are
129    // required by some animators.
130    if (mNeedsAnimatorsSync) {
131        mAnimators.resize(mStagingAnimators.size());
132        std::vector< sp<BaseRenderNodeAnimator> >::iterator it;
133        PushAnimatorsFunctor functor(this, info);
134        // hint: this means copy_if_not()
135        it = std::remove_copy_if(mStagingAnimators.begin(), mStagingAnimators.end(),
136                mAnimators.begin(), functor);
137        mAnimators.resize(std::distance(mAnimators.begin(), it));
138    }
139    if (mDirtyPropertyFields) {
140        mDirtyPropertyFields = 0;
141        mProperties = mStagingProperties;
142    }
143    if (mNeedsDisplayListDataSync) {
144        mNeedsDisplayListDataSync = false;
145        // Do a push pass on the old tree to handle freeing DisplayListData
146        // that are no longer used
147        TreeInfo oldTreeInfo;
148        prepareSubTree(oldTreeInfo, mDisplayListData);
149        // TODO: The damage for the old tree should be accounted for
150        delete mDisplayListData;
151        mDisplayListData = mStagingDisplayListData;
152        mStagingDisplayListData = 0;
153    }
154}
155
156class AnimateFunctor {
157public:
158    AnimateFunctor(RenderNode* target, TreeInfo& info)
159            : mTarget(target), mInfo(info) {}
160
161    bool operator() (const sp<BaseRenderNodeAnimator>& animator) {
162        return animator->animate(mTarget, mInfo);
163    }
164private:
165    RenderNode* mTarget;
166    TreeInfo& mInfo;
167};
168
169void RenderNode::evaluateAnimations(TreeInfo& info) {
170    if (!mAnimators.size()) return;
171
172    AnimateFunctor functor(this, info);
173    std::vector< sp<BaseRenderNodeAnimator> >::iterator newEnd;
174    newEnd = std::remove_if(mAnimators.begin(), mAnimators.end(), functor);
175    mAnimators.erase(newEnd, mAnimators.end());
176    mProperties.updateMatrix();
177    info.out.hasAnimations |= mAnimators.size();
178}
179
180void RenderNode::prepareSubTree(TreeInfo& info, DisplayListData* subtree) {
181    if (subtree) {
182        TextureCache& cache = Caches::getInstance().textureCache;
183        info.out.hasFunctors |= subtree->functorCount;
184        // TODO: Fix ownedBitmapResources to not require disabling prepareTextures
185        // and thus falling out of async drawing path.
186        if (subtree->ownedBitmapResources.size()) {
187            info.prepareTextures = false;
188        }
189        for (size_t i = 0; info.prepareTextures && i < subtree->bitmapResources.size(); i++) {
190            info.prepareTextures = cache.prefetchAndMarkInUse(subtree->bitmapResources[i]);
191        }
192        for (size_t i = 0; i < subtree->children().size(); i++) {
193            RenderNode* childNode = subtree->children()[i]->mDisplayList;
194            childNode->prepareTreeImpl(info);
195        }
196    }
197}
198
199/*
200 * For property operations, we pass a savecount of 0, since the operations aren't part of the
201 * displaylist, and thus don't have to compensate for the record-time/playback-time discrepancy in
202 * base saveCount (i.e., how RestoreToCount uses saveCount + properties().getCount())
203 */
204#define PROPERTY_SAVECOUNT 0
205
206template <class T>
207void RenderNode::setViewProperties(OpenGLRenderer& renderer, T& handler) {
208#if DEBUG_DISPLAY_LIST
209    properties().debugOutputProperties(handler.level() + 1);
210#endif
211    if (properties().getLeft() != 0 || properties().getTop() != 0) {
212        renderer.translate(properties().getLeft(), properties().getTop());
213    }
214    if (properties().getStaticMatrix()) {
215        renderer.concatMatrix(*properties().getStaticMatrix());
216    } else if (properties().getAnimationMatrix()) {
217        renderer.concatMatrix(*properties().getAnimationMatrix());
218    }
219    if (properties().hasTransformMatrix()) {
220        if (properties().isTransformTranslateOnly()) {
221            renderer.translate(properties().getTranslationX(), properties().getTranslationY());
222        } else {
223            renderer.concatMatrix(*properties().getTransformMatrix());
224        }
225    }
226    bool clipToBoundsNeeded = properties().getCaching() ? false : properties().getClipToBounds();
227    if (properties().getAlpha() < 1) {
228        if (properties().getCaching()) {
229            renderer.setOverrideLayerAlpha(properties().getAlpha());
230        } else if (!properties().getHasOverlappingRendering()) {
231            renderer.scaleAlpha(properties().getAlpha());
232        } else {
233            // TODO: should be able to store the size of a DL at record time and not
234            // have to pass it into this call. In fact, this information might be in the
235            // location/size info that we store with the new native transform data.
236            int saveFlags = SkCanvas::kHasAlphaLayer_SaveFlag;
237            if (clipToBoundsNeeded) {
238                saveFlags |= SkCanvas::kClipToLayer_SaveFlag;
239                clipToBoundsNeeded = false; // clipping done by saveLayer
240            }
241
242            SaveLayerOp* op = new (handler.allocator()) SaveLayerOp(
243                    0, 0, properties().getWidth(), properties().getHeight(),
244                    properties().getAlpha() * 255, saveFlags);
245            handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
246        }
247    }
248    if (clipToBoundsNeeded) {
249        ClipRectOp* op = new (handler.allocator()) ClipRectOp(
250                0, 0, properties().getWidth(), properties().getHeight(), SkRegion::kIntersect_Op);
251        handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
252    }
253
254    if (CC_UNLIKELY(properties().hasClippingPath())) {
255        ClipPathOp* op = new (handler.allocator()) ClipPathOp(
256                properties().getClippingPath(), properties().getClippingPathOp());
257        handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
258    }
259}
260
261/**
262 * Apply property-based transformations to input matrix
263 *
264 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
265 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
266 */
267void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) {
268    if (properties().getLeft() != 0 || properties().getTop() != 0) {
269        matrix.translate(properties().getLeft(), properties().getTop());
270    }
271    if (properties().getStaticMatrix()) {
272        mat4 stat(*properties().getStaticMatrix());
273        matrix.multiply(stat);
274    } else if (properties().getAnimationMatrix()) {
275        mat4 anim(*properties().getAnimationMatrix());
276        matrix.multiply(anim);
277    }
278
279    bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getZ());
280    if (properties().hasTransformMatrix() || applyTranslationZ) {
281        if (properties().isTransformTranslateOnly()) {
282            matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
283                    true3dTransform ? properties().getZ() : 0.0f);
284        } else {
285            if (!true3dTransform) {
286                matrix.multiply(*properties().getTransformMatrix());
287            } else {
288                mat4 true3dMat;
289                true3dMat.loadTranslate(
290                        properties().getPivotX() + properties().getTranslationX(),
291                        properties().getPivotY() + properties().getTranslationY(),
292                        properties().getZ());
293                true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
294                true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
295                true3dMat.rotate(properties().getRotation(), 0, 0, 1);
296                true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
297                true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
298
299                matrix.multiply(true3dMat);
300            }
301        }
302    }
303}
304
305/**
306 * Organizes the DisplayList hierarchy to prepare for background projection reordering.
307 *
308 * This should be called before a call to defer() or drawDisplayList()
309 *
310 * Each DisplayList that serves as a 3d root builds its list of composited children,
311 * which are flagged to not draw in the standard draw loop.
312 */
313void RenderNode::computeOrdering() {
314    ATRACE_CALL();
315    mProjectedNodes.clear();
316
317    // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that
318    // transform properties are applied correctly to top level children
319    if (mDisplayListData == NULL) return;
320    for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
321        DrawDisplayListOp* childOp = mDisplayListData->children()[i];
322        childOp->mDisplayList->computeOrderingImpl(childOp,
323                properties().getOutline().getPath(), &mProjectedNodes, &mat4::identity());
324    }
325}
326
327void RenderNode::computeOrderingImpl(
328        DrawDisplayListOp* opState,
329        const SkPath* outlineOfProjectionSurface,
330        Vector<DrawDisplayListOp*>* compositedChildrenOfProjectionSurface,
331        const mat4* transformFromProjectionSurface) {
332    mProjectedNodes.clear();
333    if (mDisplayListData == NULL || mDisplayListData->isEmpty()) return;
334
335    // TODO: should avoid this calculation in most cases
336    // TODO: just calculate single matrix, down to all leaf composited elements
337    Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface);
338    localTransformFromProjectionSurface.multiply(opState->mTransformFromParent);
339
340    if (properties().getProjectBackwards()) {
341        // composited projectee, flag for out of order draw, save matrix, and store in proj surface
342        opState->mSkipInOrderDraw = true;
343        opState->mTransformFromCompositingAncestor.load(localTransformFromProjectionSurface);
344        compositedChildrenOfProjectionSurface->add(opState);
345    } else {
346        // standard in order draw
347        opState->mSkipInOrderDraw = false;
348    }
349
350    if (mDisplayListData->children().size() > 0) {
351        const bool isProjectionReceiver = mDisplayListData->projectionReceiveIndex >= 0;
352        bool haveAppliedPropertiesToProjection = false;
353        for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
354            DrawDisplayListOp* childOp = mDisplayListData->children()[i];
355            RenderNode* child = childOp->mDisplayList;
356
357            const SkPath* projectionOutline = NULL;
358            Vector<DrawDisplayListOp*>* projectionChildren = NULL;
359            const mat4* projectionTransform = NULL;
360            if (isProjectionReceiver && !child->properties().getProjectBackwards()) {
361                // if receiving projections, collect projecting descendent
362
363                // Note that if a direct descendent is projecting backwards, we pass it's
364                // grandparent projection collection, since it shouldn't project onto it's
365                // parent, where it will already be drawing.
366                projectionOutline = properties().getOutline().getPath();
367                projectionChildren = &mProjectedNodes;
368                projectionTransform = &mat4::identity();
369            } else {
370                if (!haveAppliedPropertiesToProjection) {
371                    applyViewPropertyTransforms(localTransformFromProjectionSurface);
372                    haveAppliedPropertiesToProjection = true;
373                }
374                projectionOutline = outlineOfProjectionSurface;
375                projectionChildren = compositedChildrenOfProjectionSurface;
376                projectionTransform = &localTransformFromProjectionSurface;
377            }
378            child->computeOrderingImpl(childOp,
379                    projectionOutline, projectionChildren, projectionTransform);
380        }
381    }
382}
383
384class DeferOperationHandler {
385public:
386    DeferOperationHandler(DeferStateStruct& deferStruct, int level)
387        : mDeferStruct(deferStruct), mLevel(level) {}
388    inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
389        operation->defer(mDeferStruct, saveCount, mLevel, clipToBounds);
390    }
391    inline LinearAllocator& allocator() { return *(mDeferStruct.mAllocator); }
392    inline void startMark(const char* name) {} // do nothing
393    inline void endMark() {}
394    inline int level() { return mLevel; }
395    inline int replayFlags() { return mDeferStruct.mReplayFlags; }
396
397private:
398    DeferStateStruct& mDeferStruct;
399    const int mLevel;
400};
401
402void RenderNode::deferNodeTree(DeferStateStruct& deferStruct) {
403    DeferOperationHandler handler(deferStruct, 0);
404    if (MathUtils::isPositive(properties().getZ())) {
405        issueDrawShadowOperation(Matrix4::identity(), handler);
406    }
407    issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler);
408}
409
410void RenderNode::deferNodeInParent(DeferStateStruct& deferStruct, const int level) {
411    DeferOperationHandler handler(deferStruct, level);
412    issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler);
413}
414
415class ReplayOperationHandler {
416public:
417    ReplayOperationHandler(ReplayStateStruct& replayStruct, int level)
418        : mReplayStruct(replayStruct), mLevel(level) {}
419    inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
420#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
421        mReplayStruct.mRenderer.eventMark(operation->name());
422#endif
423        operation->replay(mReplayStruct, saveCount, mLevel, clipToBounds);
424    }
425    inline LinearAllocator& allocator() { return *(mReplayStruct.mAllocator); }
426    inline void startMark(const char* name) {
427        mReplayStruct.mRenderer.startMark(name);
428    }
429    inline void endMark() {
430        mReplayStruct.mRenderer.endMark();
431    }
432    inline int level() { return mLevel; }
433    inline int replayFlags() { return mReplayStruct.mReplayFlags; }
434
435private:
436    ReplayStateStruct& mReplayStruct;
437    const int mLevel;
438};
439
440void RenderNode::replayNodeTree(ReplayStateStruct& replayStruct) {
441    ReplayOperationHandler handler(replayStruct, 0);
442    if (MathUtils::isPositive(properties().getZ())) {
443        issueDrawShadowOperation(Matrix4::identity(), handler);
444    }
445    issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler);
446}
447
448void RenderNode::replayNodeInParent(ReplayStateStruct& replayStruct, const int level) {
449    ReplayOperationHandler handler(replayStruct, level);
450    issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler);
451}
452
453void RenderNode::buildZSortedChildList(Vector<ZDrawDisplayListOpPair>& zTranslatedNodes) {
454    if (mDisplayListData == NULL || mDisplayListData->children().size() == 0) return;
455
456    for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
457        DrawDisplayListOp* childOp = mDisplayListData->children()[i];
458        RenderNode* child = childOp->mDisplayList;
459        float childZ = child->properties().getZ();
460
461        if (!MathUtils::isZero(childZ)) {
462            zTranslatedNodes.add(ZDrawDisplayListOpPair(childZ, childOp));
463            childOp->mSkipInOrderDraw = true;
464        } else if (!child->properties().getProjectBackwards()) {
465            // regular, in order drawing DisplayList
466            childOp->mSkipInOrderDraw = false;
467        }
468    }
469
470    // Z sort 3d children (stable-ness makes z compare fall back to standard drawing order)
471    std::stable_sort(zTranslatedNodes.begin(), zTranslatedNodes.end());
472}
473
474template <class T>
475void RenderNode::issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler) {
476    if (properties().getAlpha() <= 0.0f || properties().getOutline().isEmpty()) return;
477
478    mat4 shadowMatrixXY(transformFromParent);
479    applyViewPropertyTransforms(shadowMatrixXY);
480
481    // Z matrix needs actual 3d transformation, so mapped z values will be correct
482    mat4 shadowMatrixZ(transformFromParent);
483    applyViewPropertyTransforms(shadowMatrixZ, true);
484
485    const SkPath* outlinePath = properties().getOutline().getPath();
486    const RevealClip& revealClip = properties().getRevealClip();
487    const SkPath* revealClipPath = revealClip.hasConvexClip()
488            ?  revealClip.getPath() : NULL; // only pass the reveal clip's path if it's convex
489
490    if (revealClipPath && revealClipPath->isEmpty()) return;
491
492    /**
493     * The drawing area of the caster is always the same as the its perimeter (which
494     * the shadow system uses) *except* in the inverse clip case. Inform the shadow
495     * system that the caster's drawing area (as opposed to its perimeter) has been
496     * clipped, so that it knows the caster can't be opaque.
497     */
498    bool casterUnclipped = !revealClip.willClip() || revealClip.hasConvexClip();
499
500    DisplayListOp* shadowOp  = new (handler.allocator()) DrawShadowOp(
501            shadowMatrixXY, shadowMatrixZ,
502            properties().getAlpha(), casterUnclipped,
503            outlinePath, revealClipPath);
504    handler(shadowOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
505}
506
507#define SHADOW_DELTA 0.1f
508
509template <class T>
510void RenderNode::issueOperationsOf3dChildren(const Vector<ZDrawDisplayListOpPair>& zTranslatedNodes,
511        ChildrenSelectMode mode, OpenGLRenderer& renderer, T& handler) {
512    const int size = zTranslatedNodes.size();
513    if (size == 0
514            || (mode == kNegativeZChildren && zTranslatedNodes[0].key > 0.0f)
515            || (mode == kPositiveZChildren && zTranslatedNodes[size - 1].key < 0.0f)) {
516        // no 3d children to draw
517        return;
518    }
519
520    /**
521     * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
522     * with very similar Z heights to draw together.
523     *
524     * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
525     * underneath both, and neither's shadow is drawn on top of the other.
526     */
527    const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
528    size_t drawIndex, shadowIndex, endIndex;
529    if (mode == kNegativeZChildren) {
530        drawIndex = 0;
531        endIndex = nonNegativeIndex;
532        shadowIndex = endIndex; // draw no shadows
533    } else {
534        drawIndex = nonNegativeIndex;
535        endIndex = size;
536        shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
537    }
538
539    DISPLAY_LIST_LOGD("%*s%d %s 3d children:", (handler.level() + 1) * 2, "",
540            endIndex - drawIndex, mode == kNegativeZChildren ? "negative" : "positive");
541
542    float lastCasterZ = 0.0f;
543    while (shadowIndex < endIndex || drawIndex < endIndex) {
544        if (shadowIndex < endIndex) {
545            DrawDisplayListOp* casterOp = zTranslatedNodes[shadowIndex].value;
546            RenderNode* caster = casterOp->mDisplayList;
547            const float casterZ = zTranslatedNodes[shadowIndex].key;
548            // attempt to render the shadow if the caster about to be drawn is its caster,
549            // OR if its caster's Z value is similar to the previous potential caster
550            if (shadowIndex == drawIndex || casterZ - lastCasterZ < SHADOW_DELTA) {
551                caster->issueDrawShadowOperation(casterOp->mTransformFromParent, handler);
552
553                lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
554                shadowIndex++;
555                continue;
556            }
557        }
558
559        // only the actual child DL draw needs to be in save/restore,
560        // since it modifies the renderer's matrix
561        int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
562
563        DrawDisplayListOp* childOp = zTranslatedNodes[drawIndex].value;
564        RenderNode* child = childOp->mDisplayList;
565
566        renderer.concatMatrix(childOp->mTransformFromParent);
567        childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
568        handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
569        childOp->mSkipInOrderDraw = true;
570
571        renderer.restoreToCount(restoreTo);
572        drawIndex++;
573    }
574}
575
576template <class T>
577void RenderNode::issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler) {
578    DISPLAY_LIST_LOGD("%*s%d projected children:", (handler.level() + 1) * 2, "", mProjectedNodes.size());
579    const SkPath* projectionReceiverOutline = properties().getOutline().getPath();
580    bool maskProjecteesWithPath = projectionReceiverOutline != NULL
581            && !projectionReceiverOutline->isRect(NULL);
582    int restoreTo = renderer.getSaveCount();
583
584    // If the projection reciever has an outline, we mask each of the projected rendernodes to it
585    // Either with clipRect, or special saveLayer masking
586    LinearAllocator& alloc = handler.allocator();
587    if (projectionReceiverOutline != NULL) {
588        const SkRect& outlineBounds = projectionReceiverOutline->getBounds();
589        if (projectionReceiverOutline->isRect(NULL)) {
590            // mask to the rect outline simply with clipRect
591            handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
592                    PROPERTY_SAVECOUNT, properties().getClipToBounds());
593            ClipRectOp* clipOp = new (alloc) ClipRectOp(
594                    outlineBounds.left(), outlineBounds.top(),
595                    outlineBounds.right(), outlineBounds.bottom(), SkRegion::kIntersect_Op);
596            handler(clipOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
597        } else {
598            // wrap the projected RenderNodes with a SaveLayer that will mask to the outline
599            SaveLayerOp* op = new (alloc) SaveLayerOp(
600                    outlineBounds.left(), outlineBounds.top(),
601                    outlineBounds.right(), outlineBounds.bottom(),
602                    255, SkCanvas::kARGB_ClipLayer_SaveFlag);
603            op->setMask(projectionReceiverOutline);
604            handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
605
606            /* TODO: add optimizations here to take advantage of placement/size of projected
607             * children (which may shrink saveLayer area significantly). This is dependent on
608             * passing actual drawing/dirtying bounds of projected content down to native.
609             */
610        }
611    }
612
613    // draw projected nodes
614    for (size_t i = 0; i < mProjectedNodes.size(); i++) {
615        DrawDisplayListOp* childOp = mProjectedNodes[i];
616
617        // matrix save, concat, and restore can be done safely without allocating operations
618        int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
619        renderer.concatMatrix(childOp->mTransformFromCompositingAncestor);
620        childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
621        handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
622        childOp->mSkipInOrderDraw = true;
623        renderer.restoreToCount(restoreTo);
624    }
625
626    if (projectionReceiverOutline != NULL) {
627        handler(new (alloc) RestoreToCountOp(restoreTo),
628                PROPERTY_SAVECOUNT, properties().getClipToBounds());
629    }
630}
631
632/**
633 * This function serves both defer and replay modes, and will organize the displayList's component
634 * operations for a single frame:
635 *
636 * Every 'simple' state operation that affects just the matrix and alpha (or other factors of
637 * DeferredDisplayState) may be issued directly to the renderer, but complex operations (with custom
638 * defer logic) and operations in displayListOps are issued through the 'handler' which handles the
639 * defer vs replay logic, per operation
640 */
641template <class T>
642void RenderNode::issueOperations(OpenGLRenderer& renderer, T& handler) {
643    const int level = handler.level();
644    if (mDisplayListData->isEmpty() || properties().getAlpha() <= 0) {
645        DISPLAY_LIST_LOGD("%*sEmpty display list (%p, %s)", level * 2, "", this, getName());
646        return;
647    }
648
649    handler.startMark(getName());
650
651#if DEBUG_DISPLAY_LIST
652    const Rect& clipRect = renderer.getLocalClipBounds();
653    DISPLAY_LIST_LOGD("%*sStart display list (%p, %s), localClipBounds: %.0f, %.0f, %.0f, %.0f",
654            level * 2, "", this, getName(),
655            clipRect.left, clipRect.top, clipRect.right, clipRect.bottom);
656#endif
657
658    LinearAllocator& alloc = handler.allocator();
659    int restoreTo = renderer.getSaveCount();
660    handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
661            PROPERTY_SAVECOUNT, properties().getClipToBounds());
662
663    DISPLAY_LIST_LOGD("%*sSave %d %d", (level + 1) * 2, "",
664            SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag, restoreTo);
665
666    setViewProperties<T>(renderer, handler);
667
668    bool quickRejected = properties().getClipToBounds()
669            && renderer.quickRejectConservative(0, 0, properties().getWidth(), properties().getHeight());
670    if (!quickRejected) {
671        if (mProperties.getOutline().willClip()) {
672            renderer.setClippingOutline(alloc, &(mProperties.getOutline()));
673        }
674
675        Vector<ZDrawDisplayListOpPair> zTranslatedNodes;
676        buildZSortedChildList(zTranslatedNodes);
677
678        // for 3d root, draw children with negative z values
679        issueOperationsOf3dChildren(zTranslatedNodes, kNegativeZChildren, renderer, handler);
680
681        DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance();
682        const int saveCountOffset = renderer.getSaveCount() - 1;
683        const int projectionReceiveIndex = mDisplayListData->projectionReceiveIndex;
684        for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) {
685            DisplayListOp *op = mDisplayListData->displayListOps[i];
686
687#if DEBUG_DISPLAY_LIST
688            op->output(level + 1);
689#endif
690            logBuffer.writeCommand(level, op->name());
691            handler(op, saveCountOffset, properties().getClipToBounds());
692
693            if (CC_UNLIKELY(i == projectionReceiveIndex && mProjectedNodes.size() > 0)) {
694                issueOperationsOfProjectedChildren(renderer, handler);
695            }
696        }
697
698        // for 3d root, draw children with positive z values
699        issueOperationsOf3dChildren(zTranslatedNodes, kPositiveZChildren, renderer, handler);
700    }
701
702    DISPLAY_LIST_LOGD("%*sRestoreToCount %d", (level + 1) * 2, "", restoreTo);
703    handler(new (alloc) RestoreToCountOp(restoreTo),
704            PROPERTY_SAVECOUNT, properties().getClipToBounds());
705    renderer.setOverrideLayerAlpha(1.0f);
706
707    DISPLAY_LIST_LOGD("%*sDone (%p, %s)", level * 2, "", this, getName());
708    handler.endMark();
709}
710
711} /* namespace uirenderer */
712} /* namespace android */
713