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#define LOG_TAG "OpenGLRenderer"
19
20#include "RenderNode.h"
21
22#include <algorithm>
23#include <string>
24
25#include <SkCanvas.h>
26#include <algorithm>
27
28
29#include "DamageAccumulator.h"
30#include "Debug.h"
31#include "DisplayListOp.h"
32#include "LayerRenderer.h"
33#include "OpenGLRenderer.h"
34#include "TreeInfo.h"
35#include "utils/MathUtils.h"
36#include "utils/TraceUtils.h"
37#include "renderthread/CanvasContext.h"
38
39namespace android {
40namespace uirenderer {
41
42void RenderNode::debugDumpLayers(const char* prefix) {
43    if (mLayer) {
44        ALOGD("%sNode %p (%s) has layer %p (fbo = %u, wasBuildLayered = %s)",
45                prefix, this, getName(), mLayer, mLayer->getFbo(),
46                mLayer->wasBuildLayered ? "true" : "false");
47    }
48    if (mDisplayListData) {
49        for (size_t i = 0; i < mDisplayListData->children().size(); i++) {
50            mDisplayListData->children()[i]->mRenderNode->debugDumpLayers(prefix);
51        }
52    }
53}
54
55RenderNode::RenderNode()
56        : mDirtyPropertyFields(0)
57        , mNeedsDisplayListDataSync(false)
58        , mDisplayListData(nullptr)
59        , mStagingDisplayListData(nullptr)
60        , mAnimatorManager(*this)
61        , mLayer(nullptr)
62        , mParentCount(0) {
63}
64
65RenderNode::~RenderNode() {
66    deleteDisplayListData();
67    delete mStagingDisplayListData;
68    if (mLayer) {
69        ALOGW("Memory Warning: Layer %p missed its detachment, held on to for far too long!", mLayer);
70        mLayer->postDecStrong();
71        mLayer = nullptr;
72    }
73}
74
75void RenderNode::setStagingDisplayList(DisplayListData* data) {
76    mNeedsDisplayListDataSync = true;
77    delete mStagingDisplayListData;
78    mStagingDisplayListData = data;
79}
80
81/**
82 * This function is a simplified version of replay(), where we simply retrieve and log the
83 * display list. This function should remain in sync with the replay() function.
84 */
85void RenderNode::output(uint32_t level) {
86    ALOGD("%*sStart display list (%p, %s%s%s%s%s%s)", (level - 1) * 2, "", this,
87            getName(),
88            (MathUtils::isZero(properties().getAlpha()) ? ", zero alpha" : ""),
89            (properties().hasShadow() ? ", casting shadow" : ""),
90            (isRenderable() ? "" : ", empty"),
91            (properties().getProjectBackwards() ? ", projected" : ""),
92            (mLayer != nullptr ? ", on HW Layer" : ""));
93    ALOGD("%*s%s %d", level * 2, "", "Save",
94            SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
95
96    properties().debugOutputProperties(level);
97    int flags = DisplayListOp::kOpLogFlag_Recurse;
98    if (mDisplayListData) {
99        // TODO: consider printing the chunk boundaries here
100        for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) {
101            mDisplayListData->displayListOps[i]->output(level, flags);
102        }
103    }
104
105    ALOGD("%*sDone (%p, %s)", (level - 1) * 2, "", this, getName());
106}
107
108int RenderNode::getDebugSize() {
109    int size = sizeof(RenderNode);
110    if (mStagingDisplayListData) {
111        size += mStagingDisplayListData->getUsedSize();
112    }
113    if (mDisplayListData && mDisplayListData != mStagingDisplayListData) {
114        size += mDisplayListData->getUsedSize();
115    }
116    return size;
117}
118
119void RenderNode::prepareTree(TreeInfo& info) {
120    ATRACE_CALL();
121    LOG_ALWAYS_FATAL_IF(!info.damageAccumulator, "DamageAccumulator missing");
122
123    // Functors don't correctly handle stencil usage of overdraw debugging - shove 'em in a layer.
124    bool functorsNeedLayer = Properties::debugOverdraw;
125
126    prepareTreeImpl(info, functorsNeedLayer);
127}
128
129void RenderNode::addAnimator(const sp<BaseRenderNodeAnimator>& animator) {
130    mAnimatorManager.addAnimator(animator);
131}
132
133void RenderNode::damageSelf(TreeInfo& info) {
134    if (isRenderable()) {
135        if (properties().getClipDamageToBounds()) {
136            info.damageAccumulator->dirty(0, 0, properties().getWidth(), properties().getHeight());
137        } else {
138            // Hope this is big enough?
139            // TODO: Get this from the display list ops or something
140            info.damageAccumulator->dirty(INT_MIN, INT_MIN, INT_MAX, INT_MAX);
141        }
142    }
143}
144
145void RenderNode::prepareLayer(TreeInfo& info, uint32_t dirtyMask) {
146    LayerType layerType = properties().effectiveLayerType();
147    if (CC_UNLIKELY(layerType == LayerType::RenderLayer)) {
148        // Damage applied so far needs to affect our parent, but does not require
149        // the layer to be updated. So we pop/push here to clear out the current
150        // damage and get a clean state for display list or children updates to
151        // affect, which will require the layer to be updated
152        info.damageAccumulator->popTransform();
153        info.damageAccumulator->pushTransform(this);
154        if (dirtyMask & DISPLAY_LIST) {
155            damageSelf(info);
156        }
157    }
158}
159
160void RenderNode::pushLayerUpdate(TreeInfo& info) {
161    LayerType layerType = properties().effectiveLayerType();
162    // If we are not a layer OR we cannot be rendered (eg, view was detached)
163    // we need to destroy any Layers we may have had previously
164    if (CC_LIKELY(layerType != LayerType::RenderLayer) || CC_UNLIKELY(!isRenderable())) {
165        if (CC_UNLIKELY(mLayer)) {
166            LayerRenderer::destroyLayer(mLayer);
167            mLayer = nullptr;
168        }
169        return;
170    }
171
172    bool transformUpdateNeeded = false;
173    if (!mLayer) {
174        mLayer = LayerRenderer::createRenderLayer(info.renderState, getWidth(), getHeight());
175        applyLayerPropertiesToLayer(info);
176        damageSelf(info);
177        transformUpdateNeeded = true;
178    } else if (mLayer->layer.getWidth() != getWidth() || mLayer->layer.getHeight() != getHeight()) {
179        if (!LayerRenderer::resizeLayer(mLayer, getWidth(), getHeight())) {
180            LayerRenderer::destroyLayer(mLayer);
181            mLayer = nullptr;
182        }
183        damageSelf(info);
184        transformUpdateNeeded = true;
185    }
186
187    SkRect dirty;
188    info.damageAccumulator->peekAtDirty(&dirty);
189
190    if (!mLayer) {
191        Caches::getInstance().dumpMemoryUsage();
192        if (info.errorHandler) {
193            std::string msg = "Unable to create layer for ";
194            msg += getName();
195            info.errorHandler->onError(msg);
196        }
197        return;
198    }
199
200    if (transformUpdateNeeded) {
201        // update the transform in window of the layer to reset its origin wrt light source position
202        Matrix4 windowTransform;
203        info.damageAccumulator->computeCurrentTransform(&windowTransform);
204        mLayer->setWindowTransform(windowTransform);
205    }
206
207    if (dirty.intersect(0, 0, getWidth(), getHeight())) {
208        dirty.roundOut(&dirty);
209        mLayer->updateDeferred(this, dirty.fLeft, dirty.fTop, dirty.fRight, dirty.fBottom);
210    }
211    // This is not inside the above if because we may have called
212    // updateDeferred on a previous prepare pass that didn't have a renderer
213    if (info.renderer && mLayer->deferredUpdateScheduled) {
214        info.renderer->pushLayerUpdate(mLayer);
215    }
216
217    if (info.canvasContext) {
218        // There might be prefetched layers that need to be accounted for.
219        // That might be us, so tell CanvasContext that this layer is in the
220        // tree and should not be destroyed.
221        info.canvasContext->markLayerInUse(this);
222    }
223}
224
225/**
226 * Traverse down the the draw tree to prepare for a frame.
227 *
228 * MODE_FULL = UI Thread-driven (thus properties must be synced), otherwise RT driven
229 *
230 * While traversing down the tree, functorsNeedLayer flag is set to true if anything that uses the
231 * stencil buffer may be needed. Views that use a functor to draw will be forced onto a layer.
232 */
233void RenderNode::prepareTreeImpl(TreeInfo& info, bool functorsNeedLayer) {
234    info.damageAccumulator->pushTransform(this);
235
236    if (info.mode == TreeInfo::MODE_FULL) {
237        pushStagingPropertiesChanges(info);
238    }
239    uint32_t animatorDirtyMask = 0;
240    if (CC_LIKELY(info.runAnimations)) {
241        animatorDirtyMask = mAnimatorManager.animate(info);
242    }
243
244    bool willHaveFunctor = false;
245    if (info.mode == TreeInfo::MODE_FULL && mStagingDisplayListData) {
246        willHaveFunctor = !mStagingDisplayListData->functors.isEmpty();
247    } else if (mDisplayListData) {
248        willHaveFunctor = !mDisplayListData->functors.isEmpty();
249    }
250    bool childFunctorsNeedLayer = mProperties.prepareForFunctorPresence(
251            willHaveFunctor, functorsNeedLayer);
252
253    prepareLayer(info, animatorDirtyMask);
254    if (info.mode == TreeInfo::MODE_FULL) {
255        pushStagingDisplayListChanges(info);
256    }
257    prepareSubTree(info, childFunctorsNeedLayer, mDisplayListData);
258    pushLayerUpdate(info);
259
260    info.damageAccumulator->popTransform();
261}
262
263void RenderNode::pushStagingPropertiesChanges(TreeInfo& info) {
264    // Push the animators first so that setupStartValueIfNecessary() is called
265    // before properties() is trampled by stagingProperties(), as they are
266    // required by some animators.
267    if (CC_LIKELY(info.runAnimations)) {
268        mAnimatorManager.pushStaging();
269    }
270    if (mDirtyPropertyFields) {
271        mDirtyPropertyFields = 0;
272        damageSelf(info);
273        info.damageAccumulator->popTransform();
274        mProperties = mStagingProperties;
275        applyLayerPropertiesToLayer(info);
276        // We could try to be clever and only re-damage if the matrix changed.
277        // However, we don't need to worry about that. The cost of over-damaging
278        // here is only going to be a single additional map rect of this node
279        // plus a rect join(). The parent's transform (and up) will only be
280        // performed once.
281        info.damageAccumulator->pushTransform(this);
282        damageSelf(info);
283    }
284}
285
286void RenderNode::applyLayerPropertiesToLayer(TreeInfo& info) {
287    if (CC_LIKELY(!mLayer)) return;
288
289    const LayerProperties& props = properties().layerProperties();
290    mLayer->setAlpha(props.alpha(), props.xferMode());
291    mLayer->setColorFilter(props.colorFilter());
292    mLayer->setBlend(props.needsBlending());
293}
294
295void RenderNode::pushStagingDisplayListChanges(TreeInfo& info) {
296    if (mNeedsDisplayListDataSync) {
297        mNeedsDisplayListDataSync = false;
298        // Make sure we inc first so that we don't fluctuate between 0 and 1,
299        // which would thrash the layer cache
300        if (mStagingDisplayListData) {
301            for (size_t i = 0; i < mStagingDisplayListData->children().size(); i++) {
302                mStagingDisplayListData->children()[i]->mRenderNode->incParentRefCount();
303            }
304        }
305        // Damage with the old display list first then the new one to catch any
306        // changes in isRenderable or, in the future, bounds
307        damageSelf(info);
308        deleteDisplayListData();
309        // TODO: Remove this caches stuff
310        if (mStagingDisplayListData && mStagingDisplayListData->functors.size()) {
311            Caches::getInstance().registerFunctors(mStagingDisplayListData->functors.size());
312        }
313        mDisplayListData = mStagingDisplayListData;
314        mStagingDisplayListData = nullptr;
315        if (mDisplayListData) {
316            for (size_t i = 0; i < mDisplayListData->functors.size(); i++) {
317                (*mDisplayListData->functors[i])(DrawGlInfo::kModeSync, nullptr);
318            }
319        }
320        damageSelf(info);
321    }
322}
323
324void RenderNode::deleteDisplayListData() {
325    if (mDisplayListData) {
326        for (size_t i = 0; i < mDisplayListData->children().size(); i++) {
327            mDisplayListData->children()[i]->mRenderNode->decParentRefCount();
328        }
329        if (mDisplayListData->functors.size()) {
330            Caches::getInstance().unregisterFunctors(mDisplayListData->functors.size());
331        }
332    }
333    delete mDisplayListData;
334    mDisplayListData = nullptr;
335}
336
337void RenderNode::prepareSubTree(TreeInfo& info, bool functorsNeedLayer, DisplayListData* subtree) {
338    if (subtree) {
339        TextureCache& cache = Caches::getInstance().textureCache;
340        info.out.hasFunctors |= subtree->functors.size();
341        for (size_t i = 0; info.prepareTextures && i < subtree->bitmapResources.size(); i++) {
342            info.prepareTextures = cache.prefetchAndMarkInUse(
343                    info.canvasContext, subtree->bitmapResources[i]);
344        }
345        for (size_t i = 0; i < subtree->children().size(); i++) {
346            DrawRenderNodeOp* op = subtree->children()[i];
347            RenderNode* childNode = op->mRenderNode;
348            info.damageAccumulator->pushTransform(&op->mTransformFromParent);
349            bool childFunctorsNeedLayer = functorsNeedLayer
350                    // Recorded with non-rect clip, or canvas-rotated by parent
351                    || op->mRecordedWithPotentialStencilClip;
352            childNode->prepareTreeImpl(info, childFunctorsNeedLayer);
353            info.damageAccumulator->popTransform();
354        }
355    }
356}
357
358void RenderNode::destroyHardwareResources() {
359    if (mLayer) {
360        LayerRenderer::destroyLayer(mLayer);
361        mLayer = nullptr;
362    }
363    if (mDisplayListData) {
364        for (size_t i = 0; i < mDisplayListData->children().size(); i++) {
365            mDisplayListData->children()[i]->mRenderNode->destroyHardwareResources();
366        }
367        if (mNeedsDisplayListDataSync) {
368            // Next prepare tree we are going to push a new display list, so we can
369            // drop our current one now
370            deleteDisplayListData();
371        }
372    }
373}
374
375void RenderNode::decParentRefCount() {
376    LOG_ALWAYS_FATAL_IF(!mParentCount, "already 0!");
377    mParentCount--;
378    if (!mParentCount) {
379        // If a child of ours is being attached to our parent then this will incorrectly
380        // destroy its hardware resources. However, this situation is highly unlikely
381        // and the failure is "just" that the layer is re-created, so this should
382        // be safe enough
383        destroyHardwareResources();
384    }
385}
386
387/*
388 * For property operations, we pass a savecount of 0, since the operations aren't part of the
389 * displaylist, and thus don't have to compensate for the record-time/playback-time discrepancy in
390 * base saveCount (i.e., how RestoreToCount uses saveCount + properties().getCount())
391 */
392#define PROPERTY_SAVECOUNT 0
393
394template <class T>
395void RenderNode::setViewProperties(OpenGLRenderer& renderer, T& handler) {
396#if DEBUG_DISPLAY_LIST
397    properties().debugOutputProperties(handler.level() + 1);
398#endif
399    if (properties().getLeft() != 0 || properties().getTop() != 0) {
400        renderer.translate(properties().getLeft(), properties().getTop());
401    }
402    if (properties().getStaticMatrix()) {
403        renderer.concatMatrix(*properties().getStaticMatrix());
404    } else if (properties().getAnimationMatrix()) {
405        renderer.concatMatrix(*properties().getAnimationMatrix());
406    }
407    if (properties().hasTransformMatrix()) {
408        if (properties().isTransformTranslateOnly()) {
409            renderer.translate(properties().getTranslationX(), properties().getTranslationY());
410        } else {
411            renderer.concatMatrix(*properties().getTransformMatrix());
412        }
413    }
414    const bool isLayer = properties().effectiveLayerType() != LayerType::None;
415    int clipFlags = properties().getClippingFlags();
416    if (properties().getAlpha() < 1) {
417        if (isLayer) {
418            clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
419        }
420        if (CC_LIKELY(isLayer || !properties().getHasOverlappingRendering())) {
421            // simply scale rendering content's alpha
422            renderer.scaleAlpha(properties().getAlpha());
423        } else {
424            // savelayer needed to create an offscreen buffer
425            Rect layerBounds(0, 0, getWidth(), getHeight());
426            if (clipFlags) {
427                properties().getClippingRectForFlags(clipFlags, &layerBounds);
428                clipFlags = 0; // all clipping done by savelayer
429            }
430            SaveLayerOp* op = new (handler.allocator()) SaveLayerOp(
431                    layerBounds.left, layerBounds.top,
432                    layerBounds.right, layerBounds.bottom,
433                    (int) (properties().getAlpha() * 255),
434                    SkCanvas::kHasAlphaLayer_SaveFlag | SkCanvas::kClipToLayer_SaveFlag);
435            handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
436        }
437
438        if (CC_UNLIKELY(ATRACE_ENABLED() && properties().promotedToLayer())) {
439            // pretend alpha always causes savelayer to warn about
440            // performance problem affecting old versions
441            ATRACE_FORMAT("%s alpha caused saveLayer %dx%d", getName(),
442                    static_cast<int>(getWidth()),
443                    static_cast<int>(getHeight()));
444        }
445    }
446    if (clipFlags) {
447        Rect clipRect;
448        properties().getClippingRectForFlags(clipFlags, &clipRect);
449        ClipRectOp* op = new (handler.allocator()) ClipRectOp(
450                clipRect.left, clipRect.top, clipRect.right, clipRect.bottom,
451                SkRegion::kIntersect_Op);
452        handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
453    }
454
455    // TODO: support nesting round rect clips
456    if (mProperties.getRevealClip().willClip()) {
457        Rect bounds;
458        mProperties.getRevealClip().getBounds(&bounds);
459        renderer.setClippingRoundRect(handler.allocator(), bounds, mProperties.getRevealClip().getRadius());
460    } else if (mProperties.getOutline().willClip()) {
461        renderer.setClippingOutline(handler.allocator(), &(mProperties.getOutline()));
462    }
463}
464
465/**
466 * Apply property-based transformations to input matrix
467 *
468 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
469 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
470 */
471void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) const {
472    if (properties().getLeft() != 0 || properties().getTop() != 0) {
473        matrix.translate(properties().getLeft(), properties().getTop());
474    }
475    if (properties().getStaticMatrix()) {
476        mat4 stat(*properties().getStaticMatrix());
477        matrix.multiply(stat);
478    } else if (properties().getAnimationMatrix()) {
479        mat4 anim(*properties().getAnimationMatrix());
480        matrix.multiply(anim);
481    }
482
483    bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getZ());
484    if (properties().hasTransformMatrix() || applyTranslationZ) {
485        if (properties().isTransformTranslateOnly()) {
486            matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
487                    true3dTransform ? properties().getZ() : 0.0f);
488        } else {
489            if (!true3dTransform) {
490                matrix.multiply(*properties().getTransformMatrix());
491            } else {
492                mat4 true3dMat;
493                true3dMat.loadTranslate(
494                        properties().getPivotX() + properties().getTranslationX(),
495                        properties().getPivotY() + properties().getTranslationY(),
496                        properties().getZ());
497                true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
498                true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
499                true3dMat.rotate(properties().getRotation(), 0, 0, 1);
500                true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
501                true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
502
503                matrix.multiply(true3dMat);
504            }
505        }
506    }
507}
508
509/**
510 * Organizes the DisplayList hierarchy to prepare for background projection reordering.
511 *
512 * This should be called before a call to defer() or drawDisplayList()
513 *
514 * Each DisplayList that serves as a 3d root builds its list of composited children,
515 * which are flagged to not draw in the standard draw loop.
516 */
517void RenderNode::computeOrdering() {
518    ATRACE_CALL();
519    mProjectedNodes.clear();
520
521    // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that
522    // transform properties are applied correctly to top level children
523    if (mDisplayListData == nullptr) return;
524    for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
525        DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
526        childOp->mRenderNode->computeOrderingImpl(childOp,
527                properties().getOutline().getPath(), &mProjectedNodes, &mat4::identity());
528    }
529}
530
531void RenderNode::computeOrderingImpl(
532        DrawRenderNodeOp* opState,
533        const SkPath* outlineOfProjectionSurface,
534        Vector<DrawRenderNodeOp*>* compositedChildrenOfProjectionSurface,
535        const mat4* transformFromProjectionSurface) {
536    mProjectedNodes.clear();
537    if (mDisplayListData == nullptr || mDisplayListData->isEmpty()) return;
538
539    // TODO: should avoid this calculation in most cases
540    // TODO: just calculate single matrix, down to all leaf composited elements
541    Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface);
542    localTransformFromProjectionSurface.multiply(opState->mTransformFromParent);
543
544    if (properties().getProjectBackwards()) {
545        // composited projectee, flag for out of order draw, save matrix, and store in proj surface
546        opState->mSkipInOrderDraw = true;
547        opState->mTransformFromCompositingAncestor.load(localTransformFromProjectionSurface);
548        compositedChildrenOfProjectionSurface->add(opState);
549    } else {
550        // standard in order draw
551        opState->mSkipInOrderDraw = false;
552    }
553
554    if (mDisplayListData->children().size() > 0) {
555        const bool isProjectionReceiver = mDisplayListData->projectionReceiveIndex >= 0;
556        bool haveAppliedPropertiesToProjection = false;
557        for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
558            DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
559            RenderNode* child = childOp->mRenderNode;
560
561            const SkPath* projectionOutline = nullptr;
562            Vector<DrawRenderNodeOp*>* projectionChildren = nullptr;
563            const mat4* projectionTransform = nullptr;
564            if (isProjectionReceiver && !child->properties().getProjectBackwards()) {
565                // if receiving projections, collect projecting descendant
566
567                // Note that if a direct descendant is projecting backwards, we pass its
568                // grandparent projection collection, since it shouldn't project onto its
569                // parent, where it will already be drawing.
570                projectionOutline = properties().getOutline().getPath();
571                projectionChildren = &mProjectedNodes;
572                projectionTransform = &mat4::identity();
573            } else {
574                if (!haveAppliedPropertiesToProjection) {
575                    applyViewPropertyTransforms(localTransformFromProjectionSurface);
576                    haveAppliedPropertiesToProjection = true;
577                }
578                projectionOutline = outlineOfProjectionSurface;
579                projectionChildren = compositedChildrenOfProjectionSurface;
580                projectionTransform = &localTransformFromProjectionSurface;
581            }
582            child->computeOrderingImpl(childOp,
583                    projectionOutline, projectionChildren, projectionTransform);
584        }
585    }
586}
587
588class DeferOperationHandler {
589public:
590    DeferOperationHandler(DeferStateStruct& deferStruct, int level)
591        : mDeferStruct(deferStruct), mLevel(level) {}
592    inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
593        operation->defer(mDeferStruct, saveCount, mLevel, clipToBounds);
594    }
595    inline LinearAllocator& allocator() { return *(mDeferStruct.mAllocator); }
596    inline void startMark(const char* name) {} // do nothing
597    inline void endMark() {}
598    inline int level() { return mLevel; }
599    inline int replayFlags() { return mDeferStruct.mReplayFlags; }
600    inline SkPath* allocPathForFrame() { return mDeferStruct.allocPathForFrame(); }
601
602private:
603    DeferStateStruct& mDeferStruct;
604    const int mLevel;
605};
606
607void RenderNode::defer(DeferStateStruct& deferStruct, const int level) {
608    DeferOperationHandler handler(deferStruct, level);
609    issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler);
610}
611
612class ReplayOperationHandler {
613public:
614    ReplayOperationHandler(ReplayStateStruct& replayStruct, int level)
615        : mReplayStruct(replayStruct), mLevel(level) {}
616    inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
617#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
618        mReplayStruct.mRenderer.eventMark(operation->name());
619#endif
620        operation->replay(mReplayStruct, saveCount, mLevel, clipToBounds);
621    }
622    inline LinearAllocator& allocator() { return *(mReplayStruct.mAllocator); }
623    inline void startMark(const char* name) {
624        mReplayStruct.mRenderer.startMark(name);
625    }
626    inline void endMark() {
627        mReplayStruct.mRenderer.endMark();
628    }
629    inline int level() { return mLevel; }
630    inline int replayFlags() { return mReplayStruct.mReplayFlags; }
631    inline SkPath* allocPathForFrame() { return mReplayStruct.allocPathForFrame(); }
632
633private:
634    ReplayStateStruct& mReplayStruct;
635    const int mLevel;
636};
637
638void RenderNode::replay(ReplayStateStruct& replayStruct, const int level) {
639    ReplayOperationHandler handler(replayStruct, level);
640    issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler);
641}
642
643void RenderNode::buildZSortedChildList(const DisplayListData::Chunk& chunk,
644        Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes) {
645    if (chunk.beginChildIndex == chunk.endChildIndex) return;
646
647    for (unsigned int i = chunk.beginChildIndex; i < chunk.endChildIndex; i++) {
648        DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
649        RenderNode* child = childOp->mRenderNode;
650        float childZ = child->properties().getZ();
651
652        if (!MathUtils::isZero(childZ) && chunk.reorderChildren) {
653            zTranslatedNodes.add(ZDrawRenderNodeOpPair(childZ, childOp));
654            childOp->mSkipInOrderDraw = true;
655        } else if (!child->properties().getProjectBackwards()) {
656            // regular, in order drawing DisplayList
657            childOp->mSkipInOrderDraw = false;
658        }
659    }
660
661    // Z sort any 3d children (stable-ness makes z compare fall back to standard drawing order)
662    std::stable_sort(zTranslatedNodes.begin(), zTranslatedNodes.end());
663}
664
665template <class T>
666void RenderNode::issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler) {
667    if (properties().getAlpha() <= 0.0f
668            || properties().getOutline().getAlpha() <= 0.0f
669            || !properties().getOutline().getPath()
670            || properties().getScaleX() == 0
671            || properties().getScaleY() == 0) {
672        // no shadow to draw
673        return;
674    }
675
676    mat4 shadowMatrixXY(transformFromParent);
677    applyViewPropertyTransforms(shadowMatrixXY);
678
679    // Z matrix needs actual 3d transformation, so mapped z values will be correct
680    mat4 shadowMatrixZ(transformFromParent);
681    applyViewPropertyTransforms(shadowMatrixZ, true);
682
683    const SkPath* casterOutlinePath = properties().getOutline().getPath();
684    const SkPath* revealClipPath = properties().getRevealClip().getPath();
685    if (revealClipPath && revealClipPath->isEmpty()) return;
686
687    float casterAlpha = properties().getAlpha() * properties().getOutline().getAlpha();
688
689
690    // holds temporary SkPath to store the result of intersections
691    SkPath* frameAllocatedPath = nullptr;
692    const SkPath* outlinePath = casterOutlinePath;
693
694    // intersect the outline with the reveal clip, if present
695    if (revealClipPath) {
696        frameAllocatedPath = handler.allocPathForFrame();
697
698        Op(*outlinePath, *revealClipPath, kIntersect_PathOp, frameAllocatedPath);
699        outlinePath = frameAllocatedPath;
700    }
701
702    // intersect the outline with the clipBounds, if present
703    if (properties().getClippingFlags() & CLIP_TO_CLIP_BOUNDS) {
704        if (!frameAllocatedPath) {
705            frameAllocatedPath = handler.allocPathForFrame();
706        }
707
708        Rect clipBounds;
709        properties().getClippingRectForFlags(CLIP_TO_CLIP_BOUNDS, &clipBounds);
710        SkPath clipBoundsPath;
711        clipBoundsPath.addRect(clipBounds.left, clipBounds.top,
712                clipBounds.right, clipBounds.bottom);
713
714        Op(*outlinePath, clipBoundsPath, kIntersect_PathOp, frameAllocatedPath);
715        outlinePath = frameAllocatedPath;
716    }
717
718    DisplayListOp* shadowOp  = new (handler.allocator()) DrawShadowOp(
719            shadowMatrixXY, shadowMatrixZ, casterAlpha, outlinePath);
720    handler(shadowOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
721}
722
723#define SHADOW_DELTA 0.1f
724
725template <class T>
726void RenderNode::issueOperationsOf3dChildren(ChildrenSelectMode mode,
727        const Matrix4& initialTransform, const Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
728        OpenGLRenderer& renderer, T& handler) {
729    const int size = zTranslatedNodes.size();
730    if (size == 0
731            || (mode == kNegativeZChildren && zTranslatedNodes[0].key > 0.0f)
732            || (mode == kPositiveZChildren && zTranslatedNodes[size - 1].key < 0.0f)) {
733        // no 3d children to draw
734        return;
735    }
736
737    // Apply the base transform of the parent of the 3d children. This isolates
738    // 3d children of the current chunk from transformations made in previous chunks.
739    int rootRestoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
740    renderer.setMatrix(initialTransform);
741
742    /**
743     * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
744     * with very similar Z heights to draw together.
745     *
746     * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
747     * underneath both, and neither's shadow is drawn on top of the other.
748     */
749    const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
750    size_t drawIndex, shadowIndex, endIndex;
751    if (mode == kNegativeZChildren) {
752        drawIndex = 0;
753        endIndex = nonNegativeIndex;
754        shadowIndex = endIndex; // draw no shadows
755    } else {
756        drawIndex = nonNegativeIndex;
757        endIndex = size;
758        shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
759    }
760
761    DISPLAY_LIST_LOGD("%*s%d %s 3d children:", (handler.level() + 1) * 2, "",
762            endIndex - drawIndex, mode == kNegativeZChildren ? "negative" : "positive");
763
764    float lastCasterZ = 0.0f;
765    while (shadowIndex < endIndex || drawIndex < endIndex) {
766        if (shadowIndex < endIndex) {
767            DrawRenderNodeOp* casterOp = zTranslatedNodes[shadowIndex].value;
768            RenderNode* caster = casterOp->mRenderNode;
769            const float casterZ = zTranslatedNodes[shadowIndex].key;
770            // attempt to render the shadow if the caster about to be drawn is its caster,
771            // OR if its caster's Z value is similar to the previous potential caster
772            if (shadowIndex == drawIndex || casterZ - lastCasterZ < SHADOW_DELTA) {
773                caster->issueDrawShadowOperation(casterOp->mTransformFromParent, handler);
774
775                lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
776                shadowIndex++;
777                continue;
778            }
779        }
780
781        // only the actual child DL draw needs to be in save/restore,
782        // since it modifies the renderer's matrix
783        int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
784
785        DrawRenderNodeOp* childOp = zTranslatedNodes[drawIndex].value;
786
787        renderer.concatMatrix(childOp->mTransformFromParent);
788        childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
789        handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
790        childOp->mSkipInOrderDraw = true;
791
792        renderer.restoreToCount(restoreTo);
793        drawIndex++;
794    }
795    renderer.restoreToCount(rootRestoreTo);
796}
797
798template <class T>
799void RenderNode::issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler) {
800    DISPLAY_LIST_LOGD("%*s%d projected children:", (handler.level() + 1) * 2, "", mProjectedNodes.size());
801    const SkPath* projectionReceiverOutline = properties().getOutline().getPath();
802    int restoreTo = renderer.getSaveCount();
803
804    LinearAllocator& alloc = handler.allocator();
805    handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
806            PROPERTY_SAVECOUNT, properties().getClipToBounds());
807
808    // Transform renderer to match background we're projecting onto
809    // (by offsetting canvas by translationX/Y of background rendernode, since only those are set)
810    const DisplayListOp* op =
811            (mDisplayListData->displayListOps[mDisplayListData->projectionReceiveIndex]);
812    const DrawRenderNodeOp* backgroundOp = reinterpret_cast<const DrawRenderNodeOp*>(op);
813    const RenderProperties& backgroundProps = backgroundOp->mRenderNode->properties();
814    renderer.translate(backgroundProps.getTranslationX(), backgroundProps.getTranslationY());
815
816    // If the projection reciever has an outline, we mask projected content to it
817    // (which we know, apriori, are all tessellated paths)
818    renderer.setProjectionPathMask(alloc, projectionReceiverOutline);
819
820    // draw projected nodes
821    for (size_t i = 0; i < mProjectedNodes.size(); i++) {
822        DrawRenderNodeOp* childOp = mProjectedNodes[i];
823
824        // matrix save, concat, and restore can be done safely without allocating operations
825        int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
826        renderer.concatMatrix(childOp->mTransformFromCompositingAncestor);
827        childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
828        handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
829        childOp->mSkipInOrderDraw = true;
830        renderer.restoreToCount(restoreTo);
831    }
832
833    handler(new (alloc) RestoreToCountOp(restoreTo),
834            PROPERTY_SAVECOUNT, properties().getClipToBounds());
835}
836
837/**
838 * This function serves both defer and replay modes, and will organize the displayList's component
839 * operations for a single frame:
840 *
841 * Every 'simple' state operation that affects just the matrix and alpha (or other factors of
842 * DeferredDisplayState) may be issued directly to the renderer, but complex operations (with custom
843 * defer logic) and operations in displayListOps are issued through the 'handler' which handles the
844 * defer vs replay logic, per operation
845 */
846template <class T>
847void RenderNode::issueOperations(OpenGLRenderer& renderer, T& handler) {
848    if (mDisplayListData->isEmpty()) {
849        DISPLAY_LIST_LOGD("%*sEmpty display list (%p, %s)", handler.level() * 2, "",
850                this, getName());
851        return;
852    }
853
854    const bool drawLayer = (mLayer && (&renderer != mLayer->renderer.get()));
855    // If we are updating the contents of mLayer, we don't want to apply any of
856    // the RenderNode's properties to this issueOperations pass. Those will all
857    // be applied when the layer is drawn, aka when this is true.
858    const bool useViewProperties = (!mLayer || drawLayer);
859    if (useViewProperties) {
860        const Outline& outline = properties().getOutline();
861        if (properties().getAlpha() <= 0
862                || (outline.getShouldClip() && outline.isEmpty())
863                || properties().getScaleX() == 0
864                || properties().getScaleY() == 0) {
865            DISPLAY_LIST_LOGD("%*sRejected display list (%p, %s)", handler.level() * 2, "",
866                    this, getName());
867            return;
868        }
869    }
870
871    handler.startMark(getName());
872
873#if DEBUG_DISPLAY_LIST
874    const Rect& clipRect = renderer.getLocalClipBounds();
875    DISPLAY_LIST_LOGD("%*sStart display list (%p, %s), localClipBounds: %.0f, %.0f, %.0f, %.0f",
876            handler.level() * 2, "", this, getName(),
877            clipRect.left, clipRect.top, clipRect.right, clipRect.bottom);
878#endif
879
880    LinearAllocator& alloc = handler.allocator();
881    int restoreTo = renderer.getSaveCount();
882    handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
883            PROPERTY_SAVECOUNT, properties().getClipToBounds());
884
885    DISPLAY_LIST_LOGD("%*sSave %d %d", (handler.level() + 1) * 2, "",
886            SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag, restoreTo);
887
888    if (useViewProperties) {
889        setViewProperties<T>(renderer, handler);
890    }
891
892    bool quickRejected = properties().getClipToBounds()
893            && renderer.quickRejectConservative(0, 0, properties().getWidth(), properties().getHeight());
894    if (!quickRejected) {
895        Matrix4 initialTransform(*(renderer.currentTransform()));
896        renderer.setBaseTransform(initialTransform);
897
898        if (drawLayer) {
899            handler(new (alloc) DrawLayerOp(mLayer, 0, 0),
900                    renderer.getSaveCount() - 1, properties().getClipToBounds());
901        } else {
902            const int saveCountOffset = renderer.getSaveCount() - 1;
903            const int projectionReceiveIndex = mDisplayListData->projectionReceiveIndex;
904            for (size_t chunkIndex = 0; chunkIndex < mDisplayListData->getChunks().size(); chunkIndex++) {
905                const DisplayListData::Chunk& chunk = mDisplayListData->getChunks()[chunkIndex];
906
907                Vector<ZDrawRenderNodeOpPair> zTranslatedNodes;
908                buildZSortedChildList(chunk, zTranslatedNodes);
909
910                issueOperationsOf3dChildren(kNegativeZChildren,
911                        initialTransform, zTranslatedNodes, renderer, handler);
912
913
914                for (size_t opIndex = chunk.beginOpIndex; opIndex < chunk.endOpIndex; opIndex++) {
915                    DisplayListOp *op = mDisplayListData->displayListOps[opIndex];
916#if DEBUG_DISPLAY_LIST
917                    op->output(handler.level() + 1);
918#endif
919                    handler(op, saveCountOffset, properties().getClipToBounds());
920
921                    if (CC_UNLIKELY(!mProjectedNodes.isEmpty() && projectionReceiveIndex >= 0 &&
922                        opIndex == static_cast<size_t>(projectionReceiveIndex))) {
923                        issueOperationsOfProjectedChildren(renderer, handler);
924                    }
925                }
926
927                issueOperationsOf3dChildren(kPositiveZChildren,
928                        initialTransform, zTranslatedNodes, renderer, handler);
929            }
930        }
931    }
932
933    DISPLAY_LIST_LOGD("%*sRestoreToCount %d", (handler.level() + 1) * 2, "", restoreTo);
934    handler(new (alloc) RestoreToCountOp(restoreTo),
935            PROPERTY_SAVECOUNT, properties().getClipToBounds());
936
937    DISPLAY_LIST_LOGD("%*sDone (%p, %s)", handler.level() * 2, "", this, getName());
938    handler.endMark();
939}
940
941} /* namespace uirenderer */
942} /* namespace android */
943