RenderNode.cpp revision d0a0b2a3140bfb1819a116413ce9d81886697a07
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() : mDestroyed(false), mNeedsPropertiesSync(false), mDisplayListData(0) {
53}
54
55RenderNode::~RenderNode() {
56    LOG_ALWAYS_FATAL_IF(mDestroyed, "Double destroyed DisplayList %p", this);
57
58    mDestroyed = true;
59    delete mDisplayListData;
60}
61
62void RenderNode::destroyDisplayListDeferred(RenderNode* displayList) {
63    if (displayList) {
64        DISPLAY_LIST_LOGD("Deferring display list destruction");
65        Caches::getInstance().deleteDisplayListDeferred(displayList);
66    }
67}
68
69void RenderNode::setData(DisplayListData* data) {
70    delete mDisplayListData;
71    mDisplayListData = data;
72    if (mDisplayListData) {
73        Caches::getInstance().registerFunctors(mDisplayListData->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            mName.string(), 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, mName.string());
94}
95
96void RenderNode::updateProperties() {
97    if (mNeedsPropertiesSync) {
98        mNeedsPropertiesSync = false;
99        mProperties = mStagingProperties;
100    }
101
102    for (size_t i = 0; i < mDisplayListData->children.size(); i++) {
103        RenderNode* childNode = mDisplayListData->children[i]->mDisplayList;
104        childNode->updateProperties();
105    }
106}
107
108/*
109 * For property operations, we pass a savecount of 0, since the operations aren't part of the
110 * displaylist, and thus don't have to compensate for the record-time/playback-time discrepancy in
111 * base saveCount (i.e., how RestoreToCount uses saveCount + properties().getCount())
112 */
113#define PROPERTY_SAVECOUNT 0
114
115template <class T>
116void RenderNode::setViewProperties(OpenGLRenderer& renderer, T& handler,
117        const int level) {
118#if DEBUG_DISPLAY_LIST
119    properties().debugOutputProperties(level);
120#endif
121    if (properties().getLeft() != 0 || properties().getTop() != 0) {
122        renderer.translate(properties().getLeft(), properties().getTop());
123    }
124    if (properties().getStaticMatrix()) {
125        renderer.concatMatrix(properties().getStaticMatrix());
126    } else if (properties().getAnimationMatrix()) {
127        renderer.concatMatrix(properties().getAnimationMatrix());
128    }
129    if (properties().getMatrixFlags() != 0) {
130        if (properties().getMatrixFlags() == TRANSLATION) {
131            renderer.translate(properties().getTranslationX(), properties().getTranslationY());
132        } else {
133            renderer.concatMatrix(*properties().getTransformMatrix());
134        }
135    }
136    bool clipToBoundsNeeded = properties().getCaching() ? false : properties().getClipToBounds();
137    if (properties().getAlpha() < 1) {
138        if (properties().getCaching()) {
139            renderer.setOverrideLayerAlpha(properties().getAlpha());
140        } else if (!properties().getHasOverlappingRendering()) {
141            renderer.scaleAlpha(properties().getAlpha());
142        } else {
143            // TODO: should be able to store the size of a DL at record time and not
144            // have to pass it into this call. In fact, this information might be in the
145            // location/size info that we store with the new native transform data.
146            int saveFlags = SkCanvas::kHasAlphaLayer_SaveFlag;
147            if (clipToBoundsNeeded) {
148                saveFlags |= SkCanvas::kClipToLayer_SaveFlag;
149                clipToBoundsNeeded = false; // clipping done by saveLayer
150            }
151
152            SaveLayerOp* op = new (handler.allocator()) SaveLayerOp(
153                    0, 0, properties().getWidth(), properties().getHeight(), properties().getAlpha() * 255, saveFlags);
154            handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
155        }
156    }
157    if (clipToBoundsNeeded) {
158        ClipRectOp* op = new (handler.allocator()) ClipRectOp(0, 0,
159                properties().getWidth(), properties().getHeight(), SkRegion::kIntersect_Op);
160        handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
161    }
162    if (CC_UNLIKELY(properties().getOutline().willClip())) {
163        ClipPathOp* op = new (handler.allocator()) ClipPathOp(properties().getOutline().getPath(),
164                SkRegion::kIntersect_Op);
165        handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
166    }
167}
168
169/**
170 * Apply property-based transformations to input matrix
171 *
172 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
173 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
174 */
175void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) {
176    if (properties().getLeft() != 0 || properties().getTop() != 0) {
177        matrix.translate(properties().getLeft(), properties().getTop());
178    }
179    if (properties().getStaticMatrix()) {
180        mat4 stat(*properties().getStaticMatrix());
181        matrix.multiply(stat);
182    } else if (properties().getAnimationMatrix()) {
183        mat4 anim(*properties().getAnimationMatrix());
184        matrix.multiply(anim);
185    }
186    if (properties().getMatrixFlags() != 0) {
187        if (properties().getMatrixFlags() == TRANSLATION) {
188            matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
189                    true3dTransform ? properties().getTranslationZ() : 0.0f);
190        } else {
191            if (!true3dTransform) {
192                matrix.multiply(*properties().getTransformMatrix());
193            } else {
194                mat4 true3dMat;
195                true3dMat.loadTranslate(
196                        properties().getPivotX() + properties().getTranslationX(),
197                        properties().getPivotY() + properties().getTranslationY(),
198                        properties().getTranslationZ());
199                true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
200                true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
201                true3dMat.rotate(properties().getRotation(), 0, 0, 1);
202                true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
203                true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
204
205                matrix.multiply(true3dMat);
206            }
207        }
208    }
209}
210
211/**
212 * Organizes the DisplayList hierarchy to prepare for background projection reordering.
213 *
214 * This should be called before a call to defer() or drawDisplayList()
215 *
216 * Each DisplayList that serves as a 3d root builds its list of composited children,
217 * which are flagged to not draw in the standard draw loop.
218 */
219void RenderNode::computeOrdering() {
220    ATRACE_CALL();
221    mProjectedNodes.clear();
222
223    // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that
224    // transform properties are applied correctly to top level children
225    if (mDisplayListData == NULL) return;
226    for (unsigned int i = 0; i < mDisplayListData->children.size(); i++) {
227        DrawDisplayListOp* childOp = mDisplayListData->children[i];
228        childOp->mDisplayList->computeOrderingImpl(childOp,
229                &mProjectedNodes, &mat4::identity());
230    }
231}
232
233void RenderNode::computeOrderingImpl(
234        DrawDisplayListOp* opState,
235        Vector<DrawDisplayListOp*>* compositedChildrenOfProjectionSurface,
236        const mat4* transformFromProjectionSurface) {
237    mProjectedNodes.clear();
238    if (mDisplayListData == NULL || mDisplayListData->isEmpty()) return;
239
240    // TODO: should avoid this calculation in most cases
241    // TODO: just calculate single matrix, down to all leaf composited elements
242    Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface);
243    localTransformFromProjectionSurface.multiply(opState->mTransformFromParent);
244
245    if (properties().getProjectBackwards()) {
246        // composited projectee, flag for out of order draw, save matrix, and store in proj surface
247        opState->mSkipInOrderDraw = true;
248        opState->mTransformFromCompositingAncestor.load(localTransformFromProjectionSurface);
249        compositedChildrenOfProjectionSurface->add(opState);
250    } else {
251        // standard in order draw
252        opState->mSkipInOrderDraw = false;
253    }
254
255    if (mDisplayListData->children.size() > 0) {
256        const bool isProjectionReceiver = mDisplayListData->projectionReceiveIndex >= 0;
257        bool haveAppliedPropertiesToProjection = false;
258        for (unsigned int i = 0; i < mDisplayListData->children.size(); i++) {
259            DrawDisplayListOp* childOp = mDisplayListData->children[i];
260            RenderNode* child = childOp->mDisplayList;
261
262            Vector<DrawDisplayListOp*>* projectionChildren = NULL;
263            const mat4* projectionTransform = NULL;
264            if (isProjectionReceiver && !child->properties().getProjectBackwards()) {
265                // if receiving projections, collect projecting descendent
266
267                // Note that if a direct descendent is projecting backwards, we pass it's
268                // grandparent projection collection, since it shouldn't project onto it's
269                // parent, where it will already be drawing.
270                projectionChildren = &mProjectedNodes;
271                projectionTransform = &mat4::identity();
272            } else {
273                if (!haveAppliedPropertiesToProjection) {
274                    applyViewPropertyTransforms(localTransformFromProjectionSurface);
275                    haveAppliedPropertiesToProjection = true;
276                }
277                projectionChildren = compositedChildrenOfProjectionSurface;
278                projectionTransform = &localTransformFromProjectionSurface;
279            }
280            child->computeOrderingImpl(childOp, projectionChildren, projectionTransform);
281        }
282    }
283
284}
285
286class DeferOperationHandler {
287public:
288    DeferOperationHandler(DeferStateStruct& deferStruct, int level)
289        : mDeferStruct(deferStruct), mLevel(level) {}
290    inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
291        operation->defer(mDeferStruct, saveCount, mLevel, clipToBounds);
292    }
293    inline LinearAllocator& allocator() { return *(mDeferStruct.mAllocator); }
294
295private:
296    DeferStateStruct& mDeferStruct;
297    const int mLevel;
298};
299
300void RenderNode::defer(DeferStateStruct& deferStruct, const int level) {
301    DeferOperationHandler handler(deferStruct, level);
302    iterate<DeferOperationHandler>(deferStruct.mRenderer, handler, level);
303}
304
305class ReplayOperationHandler {
306public:
307    ReplayOperationHandler(ReplayStateStruct& replayStruct, int level)
308        : mReplayStruct(replayStruct), mLevel(level) {}
309    inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
310#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
311        properties().getReplayStruct().mRenderer.eventMark(operation->name());
312#endif
313        operation->replay(mReplayStruct, saveCount, mLevel, clipToBounds);
314    }
315    inline LinearAllocator& allocator() { return *(mReplayStruct.mAllocator); }
316
317private:
318    ReplayStateStruct& mReplayStruct;
319    const int mLevel;
320};
321
322void RenderNode::replay(ReplayStateStruct& replayStruct, const int level) {
323    ReplayOperationHandler handler(replayStruct, level);
324
325    replayStruct.mRenderer.startMark(mName.string());
326    iterate<ReplayOperationHandler>(replayStruct.mRenderer, handler, level);
327    replayStruct.mRenderer.endMark();
328
329    DISPLAY_LIST_LOGD("%*sDone (%p, %s), returning %d", level * 2, "", this, mName.string(),
330            replayStruct.mDrawGlStatus);
331}
332
333void RenderNode::buildZSortedChildList(Vector<ZDrawDisplayListOpPair>& zTranslatedNodes) {
334    if (mDisplayListData == NULL || mDisplayListData->children.size() == 0) return;
335
336    for (unsigned int i = 0; i < mDisplayListData->children.size(); i++) {
337        DrawDisplayListOp* childOp = mDisplayListData->children[i];
338        RenderNode* child = childOp->mDisplayList;
339        float childZ = child->properties().getTranslationZ();
340
341        if (childZ != 0.0f) {
342            zTranslatedNodes.add(ZDrawDisplayListOpPair(childZ, childOp));
343            childOp->mSkipInOrderDraw = true;
344        } else if (!child->properties().getProjectBackwards()) {
345            // regular, in order drawing DisplayList
346            childOp->mSkipInOrderDraw = false;
347        }
348    }
349
350    // Z sort 3d children (stable-ness makes z compare fall back to standard drawing order)
351    std::stable_sort(zTranslatedNodes.begin(), zTranslatedNodes.end());
352}
353
354#define SHADOW_DELTA 0.1f
355
356template <class T>
357void RenderNode::iterate3dChildren(const Vector<ZDrawDisplayListOpPair>& zTranslatedNodes,
358        ChildrenSelectMode mode, OpenGLRenderer& renderer, T& handler) {
359    const int size = zTranslatedNodes.size();
360    if (size == 0
361            || (mode == kNegativeZChildren && zTranslatedNodes[0].key > 0.0f)
362            || (mode == kPositiveZChildren && zTranslatedNodes[size - 1].key < 0.0f)) {
363        // no 3d children to draw
364        return;
365    }
366
367    int rootRestoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
368    LinearAllocator& alloc = handler.allocator();
369    ClipRectOp* clipOp = new (alloc) ClipRectOp(0, 0, properties().getWidth(), properties().getHeight(),
370            SkRegion::kIntersect_Op); // clip to 3d root bounds
371    handler(clipOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
372
373    /**
374     * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
375     * with very similar Z heights to draw together.
376     *
377     * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
378     * underneath both, and neither's shadow is drawn on top of the other.
379     */
380    const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
381    size_t drawIndex, shadowIndex, endIndex;
382    if (mode == kNegativeZChildren) {
383        drawIndex = 0;
384        endIndex = nonNegativeIndex;
385        shadowIndex = endIndex; // draw no shadows
386    } else {
387        drawIndex = nonNegativeIndex;
388        endIndex = size;
389        shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
390    }
391    float lastCasterZ = 0.0f;
392    while (shadowIndex < endIndex || drawIndex < endIndex) {
393        if (shadowIndex < endIndex) {
394            DrawDisplayListOp* casterOp = zTranslatedNodes[shadowIndex].value;
395            RenderNode* caster = casterOp->mDisplayList;
396            const float casterZ = zTranslatedNodes[shadowIndex].key;
397            // attempt to render the shadow if the caster about to be drawn is its caster,
398            // OR if its caster's Z value is similar to the previous potential caster
399            if (shadowIndex == drawIndex || casterZ - lastCasterZ < SHADOW_DELTA) {
400
401                if (caster->properties().getAlpha() > 0.0f) {
402                    mat4 shadowMatrixXY(casterOp->mTransformFromParent);
403                    caster->applyViewPropertyTransforms(shadowMatrixXY);
404
405                    // Z matrix needs actual 3d transformation, so mapped z values will be correct
406                    mat4 shadowMatrixZ(casterOp->mTransformFromParent);
407                    caster->applyViewPropertyTransforms(shadowMatrixZ, true);
408
409                    DisplayListOp* shadowOp  = new (alloc) DrawShadowOp(
410                            shadowMatrixXY, shadowMatrixZ,
411                            caster->properties().getAlpha(), caster->properties().getOutline().getPath(),
412                            caster->properties().getWidth(), caster->properties().getHeight());
413                    handler(shadowOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
414                }
415
416                lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
417                shadowIndex++;
418                continue;
419            }
420        }
421
422        // only the actual child DL draw needs to be in save/restore,
423        // since it modifies the renderer's matrix
424        int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
425
426        DrawDisplayListOp* childOp = zTranslatedNodes[drawIndex].value;
427        RenderNode* child = childOp->mDisplayList;
428
429        renderer.concatMatrix(childOp->mTransformFromParent);
430        childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
431        handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
432        childOp->mSkipInOrderDraw = true;
433
434        renderer.restoreToCount(restoreTo);
435        drawIndex++;
436    }
437    handler(new (alloc) RestoreToCountOp(rootRestoreTo), PROPERTY_SAVECOUNT, properties().getClipToBounds());
438}
439
440template <class T>
441void RenderNode::iterateProjectedChildren(OpenGLRenderer& renderer, T& handler, const int level) {
442    int rootRestoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
443    LinearAllocator& alloc = handler.allocator();
444    ClipRectOp* clipOp = new (alloc) ClipRectOp(0, 0, properties().getWidth(), properties().getHeight(),
445            SkRegion::kReplace_Op); // clip to projection surface root bounds
446    handler(clipOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
447
448    for (size_t i = 0; i < mProjectedNodes.size(); i++) {
449        DrawDisplayListOp* childOp = mProjectedNodes[i];
450
451        // matrix save, concat, and restore can be done safely without allocating operations
452        int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
453        renderer.concatMatrix(childOp->mTransformFromCompositingAncestor);
454        childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
455        handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
456        childOp->mSkipInOrderDraw = true;
457        renderer.restoreToCount(restoreTo);
458    }
459    handler(new (alloc) RestoreToCountOp(rootRestoreTo), PROPERTY_SAVECOUNT, properties().getClipToBounds());
460}
461
462/**
463 * This function serves both defer and replay modes, and will organize the displayList's component
464 * operations for a single frame:
465 *
466 * Every 'simple' state operation that affects just the matrix and alpha (or other factors of
467 * DeferredDisplayState) may be issued directly to the renderer, but complex operations (with custom
468 * defer logic) and operations in displayListOps are issued through the 'handler' which handles the
469 * defer vs replay logic, per operation
470 */
471template <class T>
472void RenderNode::iterate(OpenGLRenderer& renderer, T& handler, const int level) {
473    if (CC_UNLIKELY(mDestroyed)) { // temporary debug logging
474        ALOGW("Error: %s is drawing after destruction", mName.string());
475        CRASH();
476    }
477    if (mDisplayListData->isEmpty() || properties().getAlpha() <= 0) {
478        DISPLAY_LIST_LOGD("%*sEmpty display list (%p, %s)", level * 2, "", this, mName.string());
479        return;
480    }
481
482#if DEBUG_DISPLAY_LIST
483    Rect* clipRect = renderer.getClipRect();
484    DISPLAY_LIST_LOGD("%*sStart display list (%p, %s), clipRect: %.0f, %.0f, %.0f, %.0f",
485            level * 2, "", this, mName.string(), clipRect->left, clipRect->top,
486            clipRect->right, clipRect->bottom);
487#endif
488
489    LinearAllocator& alloc = handler.allocator();
490    int restoreTo = renderer.getSaveCount();
491    handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
492            PROPERTY_SAVECOUNT, properties().getClipToBounds());
493
494    DISPLAY_LIST_LOGD("%*sSave %d %d", (level + 1) * 2, "",
495            SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag, restoreTo);
496
497    setViewProperties<T>(renderer, handler, level + 1);
498
499    bool quickRejected = properties().getClipToBounds() && renderer.quickRejectConservative(0, 0, properties().getWidth(), properties().getHeight());
500    if (!quickRejected) {
501        Vector<ZDrawDisplayListOpPair> zTranslatedNodes;
502        buildZSortedChildList(zTranslatedNodes);
503
504        // for 3d root, draw children with negative z values
505        iterate3dChildren(zTranslatedNodes, kNegativeZChildren, renderer, handler);
506
507        DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance();
508        const int saveCountOffset = renderer.getSaveCount() - 1;
509        const int projectionReceiveIndex = mDisplayListData->projectionReceiveIndex;
510        for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) {
511            DisplayListOp *op = mDisplayListData->displayListOps[i];
512
513#if DEBUG_DISPLAY_LIST
514            op->output(level + 1);
515#endif
516
517            logBuffer.writeCommand(level, op->name());
518            handler(op, saveCountOffset, properties().getClipToBounds());
519
520            if (CC_UNLIKELY(i == projectionReceiveIndex && mProjectedNodes.size() > 0)) {
521                iterateProjectedChildren(renderer, handler, level);
522            }
523        }
524
525        // for 3d root, draw children with positive z values
526        iterate3dChildren(zTranslatedNodes, kPositiveZChildren, renderer, handler);
527    }
528
529    DISPLAY_LIST_LOGD("%*sRestoreToCount %d", (level + 1) * 2, "", restoreTo);
530    handler(new (alloc) RestoreToCountOp(restoreTo),
531            PROPERTY_SAVECOUNT, properties().getClipToBounds());
532    renderer.setOverrideLayerAlpha(1.0f);
533}
534
535} /* namespace uirenderer */
536} /* namespace android */
537