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