SkiaDisplayList.h revision d21723704571dba7e69947d92856f22989d53dbf
1/*
2 * Copyright (C) 2016 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#pragma once
18
19#include "DisplayList.h"
20#include "GLFunctorDrawable.h"
21#include "RenderNodeDrawable.h"
22
23#include <deque>
24#include <SkLiteDL.h>
25#include <SkPictureRecorder.h>
26
27namespace android {
28namespace uirenderer {
29
30class Outline;
31
32namespace skiapipeline {
33
34/**
35 * This class is intended to be self contained, but still subclasses from
36 * DisplayList to make it easier to support switching between the two at
37 * runtime.  The downside of this inheritance is that we pay for the overhead
38 * of the parent class construction/destruction without any real benefit.
39 */
40class SkiaDisplayList : public DisplayList {
41public:
42    SkiaDisplayList(SkRect bounds);
43    virtual ~SkiaDisplayList() {
44        /* Given that we are using a LinearStdAllocator to store some of the
45         * SkDrawable contents we must ensure that any other object that is
46         * holding a reference to those drawables is destroyed prior to their
47         * deletion.
48         */
49        mDrawable.reset();
50    }
51
52    /**
53     * This resets the DisplayList so that it behaves as if the object were newly
54     * constructed with the provided bounds.  The reuse avoids any overhead
55     * associated with destroying the SkLiteDL as well as the deques and vectors.
56     */
57    void reset(SkRect bounds);
58
59    /**
60     * Use the linear allocator to create any SkDrawables needed by the display
61     * list. This could be dangerous as these objects are ref-counted, so we
62     * need to monitor that they don't extend beyond the lifetime of the class
63     * that creates them.
64     */
65    template<class T, typename... Params>
66    SkDrawable* allocateDrawable(Params&&... params) {
67        return allocator.create<T>(std::forward<Params>(params)...);
68    }
69
70    bool isSkiaDL() const override { return true; }
71
72    /**
73     * Returns true if the DisplayList does not have any recorded content
74     */
75    bool isEmpty() const override { return mDrawable->empty(); }
76
77    /**
78     * Returns true if this list directly contains a GLFunctor drawing command.
79     */
80    bool hasFunctor() const override { return !mChildFunctors.empty(); }
81
82    /**
83     * Returns true if this list directly contains a VectorDrawable drawing command.
84     */
85    bool hasVectorDrawables() const override { return !mVectorDrawables.empty(); }
86
87    /**
88     * Attempts to reset and reuse this DisplayList.
89     *
90     * @return true if the displayList will be reused and therefore should not be deleted
91     */
92    bool reuseDisplayList(RenderNode* node, renderthread::CanvasContext* context) override;
93
94    /**
95     * ONLY to be called by RenderNode::syncDisplayList so that we can notify any
96     * contained VectorDrawables or GLFunctors to sync their state.
97     *
98     * NOTE: This function can be folded into RenderNode when we no longer need
99     *       to subclass from DisplayList
100     */
101    void syncContents() override;
102
103    /**
104     * ONLY to be called by RenderNode::prepareTree in order to prepare this
105     * list while the UI thread is blocked.  Here we can upload mutable bitmaps
106     * and notify our parent if any of our content has been invalidated and in
107     * need of a redraw.  If the renderNode has any children then they are also
108     * call in order to prepare them.
109     *
110     * @return true if any content change requires the node to be invalidated
111     *
112     * NOTE: This function can be folded into RenderNode when we no longer need
113     *       to subclass from DisplayList
114     */
115
116    bool prepareListAndChildren(TreeObserver& observer, TreeInfo& info, bool functorsNeedLayer,
117            std::function<void(RenderNode*, TreeObserver&, TreeInfo&, bool)> childFn) override;
118
119    /**
120     *  Calls the provided function once for each child of this DisplayList
121     */
122    void updateChildren(std::function<void(RenderNode*)> updateFn) override;
123
124    /**
125     *  Returns true if there is a child render node that is a projection receiver.
126     */
127    inline bool containsProjectionReceiver() const { return mProjectionReceiver; }
128
129    void output(std::ostream& output, uint32_t level) override;
130
131    /**
132     * We use std::deque here because (1) we need to iterate through these
133     * elements and (2) mDrawable holds pointers to the elements, so they cannot
134     * relocate.
135     */
136    std::deque<RenderNodeDrawable> mChildNodes;
137    std::deque<GLFunctorDrawable> mChildFunctors;
138    std::vector<SkImage*> mMutableImages;
139    std::vector<VectorDrawableRoot*> mVectorDrawables;
140    sk_sp<SkLiteDL> mDrawable;
141
142    //mProjectionReceiver points to a child node (stored in mChildNodes) that is as a projection
143    //receiver. It is set at record time and used at both prepare and draw tree traversals to
144    //make sure backward projected nodes are found and drawn immediately after mProjectionReceiver.
145    RenderNodeDrawable* mProjectionReceiver = nullptr;
146
147    //mProjectedOutline is valid only when render node tree is traversed during the draw pass.
148    //Render nodes that have a child receiver node, will store a pointer to their outline in
149    //mProjectedOutline. Child receiver node will apply the clip before any backward projected
150    //node is drawn.
151    const Outline* mProjectedOutline = nullptr;
152
153    //mProjectedReceiverParentMatrix is valid when render node tree is traversed during the draw
154    //pass. Render nodes that have a child receiver node, will store their matrix in
155    //mProjectedReceiverParentMatrix. Child receiver node will set the matrix and then clip with the
156    //outline of their parent.
157    SkMatrix mProjectedReceiverParentMatrix;
158};
159
160}; // namespace skiapipeline
161}; // namespace uirenderer
162}; // namespace android
163