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