SkiaDisplayList.h revision b7d34b64dd32e3d84bd43344c9c3d9ad098129af
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 {
29namespace skiapipeline {
30
31/**
32 * This class is intended to be self contained, but still subclasses from
33 * DisplayList to make it easier to support switching between the two at
34 * runtime.  The downside of this inheritance is that we pay for the overhead
35 * of the parent class construction/destruction without any real benefit.
36 */
37class SkiaDisplayList : public DisplayList {
38public:
39    SkiaDisplayList(SkRect bounds);
40    virtual ~SkiaDisplayList() {
41        /* Given that we are using a LinearStdAllocator to store some of the
42         * SkDrawable contents we must ensure that any other object that is
43         * holding a reference to those drawables is destroyed prior to their
44         * deletion.
45         */
46        mDrawable.reset();
47    }
48
49    /**
50     * This resets the DisplayList so that it behaves as if the object were newly
51     * constructed with the provided bounds.  The reuse avoids any overhead
52     * associated with destroying the SkLiteDL as well as the deques and vectors.
53     */
54    void reset(SkRect bounds);
55
56    /**
57     * Use the linear allocator to create any SkDrawables needed by the display
58     * list. This could be dangerous as these objects are ref-counted, so we
59     * need to monitor that they don't extend beyond the lifetime of the class
60     * that creates them.
61     */
62    template<class T, typename... Params>
63    SkDrawable* allocateDrawable(Params&&... params) {
64        return allocator.create<T>(std::forward<Params>(params)...);
65    }
66
67    bool isSkiaDL() const override { return true; }
68
69    /**
70     * Returns true if the DisplayList does not have any recorded content
71     */
72    bool isEmpty() const override { return mDrawable->empty(); }
73
74    /**
75     * Returns true if this list directly contains a GLFunctor drawing command.
76     */
77    bool hasFunctor() const override { return !mChildFunctors.empty(); }
78
79    /**
80     * Returns true if this list directly contains a VectorDrawable drawing command.
81     */
82    bool hasVectorDrawables() const override { return !mVectorDrawables.empty(); }
83
84    /**
85     * Attempts to reset and reuse this DisplayList.
86     *
87     * @return true if the displayList will be reused and therefore should not be deleted
88     */
89    bool reuseDisplayList(RenderNode* node, renderthread::CanvasContext* context) override;
90
91    /**
92     * ONLY to be called by RenderNode::syncDisplayList so that we can notify any
93     * contained VectorDrawables or GLFunctors to sync their state.
94     *
95     * NOTE: This function can be folded into RenderNode when we no longer need
96     *       to subclass from DisplayList
97     */
98    void syncContents() override;
99
100    /**
101     * ONLY to be called by RenderNode::prepareTree in order to prepare this
102     * list while the UI thread is blocked.  Here we can upload mutable bitmaps
103     * and notify our parent if any of our content has been invalidated and in
104     * need of a redraw.  If the renderNode has any children then they are also
105     * call in order to prepare them.
106     *
107     * @return true if any content change requires the node to be invalidated
108     *
109     * NOTE: This function can be folded into RenderNode when we no longer need
110     *       to subclass from DisplayList
111     */
112
113    bool prepareListAndChildren(TreeInfo& info, bool functorsNeedLayer,
114            std::function<void(RenderNode*, TreeInfo&, bool)> childFn) override;
115
116    /**
117     *  Calls the provided function once for each child of this DisplayList
118     */
119    void updateChildren(std::function<void(RenderNode*)> updateFn) override;
120
121    /**
122     * We use std::deque here because (1) we need to iterate through these
123     * elements and (2) mDrawable holds pointers to the elements, so they cannot
124     * relocate.
125     */
126    std::deque<RenderNodeDrawable> mChildNodes;
127    std::deque<GLFunctorDrawable> mChildFunctors;
128    std::vector<SkImage*> mMutableImages;
129    std::vector<VectorDrawableRoot*> mVectorDrawables;
130    sk_sp<SkLiteDL> mDrawable;
131
132    bool mIsProjectionReceiver = false;
133};
134
135}; // namespace skiapipeline
136}; // namespace uirenderer
137}; // namespace android
138