RenderNode.h revision 91eff22b5d7f8fe551bae01331948858ce932a96
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#ifndef RENDERNODE_H
17#define RENDERNODE_H
18
19#include <SkCamera.h>
20#include <SkMatrix.h>
21
22#include <utils/LinearAllocator.h>
23#include <utils/RefBase.h>
24#include <utils/String8.h>
25
26#include <cutils/compiler.h>
27
28#include <androidfw/ResourceTypes.h>
29
30#include "AnimatorManager.h"
31#include "Debug.h"
32#include "DisplayList.h"
33#include "Matrix.h"
34#include "RenderProperties.h"
35
36#include <vector>
37
38class SkBitmap;
39class SkPaint;
40class SkPath;
41class SkRegion;
42
43namespace android {
44namespace uirenderer {
45
46class CanvasState;
47class DisplayListCanvas;
48class DisplayListOp;
49class OpenGLRenderer;
50class Rect;
51class SkiaShader;
52
53#if HWUI_NEW_OPS
54class FrameBuilder;
55class OffscreenBuffer;
56struct RenderNodeOp;
57typedef OffscreenBuffer layer_t;
58typedef RenderNodeOp renderNodeOp_t;
59#else
60class Layer;
61typedef Layer layer_t;
62typedef DrawRenderNodeOp renderNodeOp_t;
63#endif
64
65class ClipRectOp;
66class DrawRenderNodeOp;
67class SaveLayerOp;
68class SaveOp;
69class RestoreToCountOp;
70class TreeInfo;
71
72namespace proto {
73class RenderNode;
74}
75
76/**
77 * Primary class for storing recorded canvas commands, as well as per-View/ViewGroup display properties.
78 *
79 * Recording of canvas commands is somewhat similar to SkPicture, except the canvas-recording
80 * functionality is split between DisplayListCanvas (which manages the recording), DisplayList
81 * (which holds the actual data), and DisplayList (which holds properties and performs playback onto
82 * a renderer).
83 *
84 * Note that DisplayList is swapped out from beneath an individual RenderNode when a view's
85 * recorded stream of canvas operations is refreshed. The RenderNode (and its properties) stay
86 * attached.
87 */
88class RenderNode : public VirtualLightRefBase {
89friend class TestUtils; // allow TestUtils to access syncDisplayList / syncProperties
90friend class FrameBuilder;
91public:
92    enum DirtyPropertyMask {
93        GENERIC         = 1 << 1,
94        TRANSLATION_X   = 1 << 2,
95        TRANSLATION_Y   = 1 << 3,
96        TRANSLATION_Z   = 1 << 4,
97        SCALE_X         = 1 << 5,
98        SCALE_Y         = 1 << 6,
99        ROTATION        = 1 << 7,
100        ROTATION_X      = 1 << 8,
101        ROTATION_Y      = 1 << 9,
102        X               = 1 << 10,
103        Y               = 1 << 11,
104        Z               = 1 << 12,
105        ALPHA           = 1 << 13,
106        DISPLAY_LIST    = 1 << 14,
107    };
108
109    ANDROID_API RenderNode();
110    ANDROID_API virtual ~RenderNode();
111
112    // See flags defined in DisplayList.java
113    enum ReplayFlag {
114        kReplayFlag_ClipChildren = 0x1
115    };
116
117    void debugDumpLayers(const char* prefix);
118
119    ANDROID_API void setStagingDisplayList(DisplayList* newData);
120
121    void computeOrdering();
122
123    void defer(DeferStateStruct& deferStruct, const int level);
124    void replay(ReplayStateStruct& replayStruct, const int level);
125
126#if HWUI_NEW_OPS
127    ANDROID_API void output(uint32_t level = 0, const char* label = "Root");
128#else
129    ANDROID_API void output(uint32_t level = 1);
130#endif
131    ANDROID_API int getDebugSize();
132    void copyTo(proto::RenderNode* node);
133
134    bool isRenderable() const {
135        return mDisplayList && !mDisplayList->isEmpty();
136    }
137
138    bool hasProjectionReceiver() const {
139        return mDisplayList && mDisplayList->projectionReceiveIndex >= 0;
140    }
141
142    const char* getName() const {
143        return mName.string();
144    }
145
146    void setName(const char* name) {
147        if (name) {
148            char* lastPeriod = strrchr(name, '.');
149            if (lastPeriod) {
150                mName.setTo(lastPeriod + 1);
151            } else {
152                mName.setTo(name);
153            }
154        }
155    }
156
157    bool isPropertyFieldDirty(DirtyPropertyMask field) const {
158        return mDirtyPropertyFields & field;
159    }
160
161    void setPropertyFieldsDirty(uint32_t fields) {
162        mDirtyPropertyFields |= fields;
163    }
164
165    const RenderProperties& properties() const {
166        return mProperties;
167    }
168
169    RenderProperties& animatorProperties() {
170        return mProperties;
171    }
172
173    const RenderProperties& stagingProperties() {
174        return mStagingProperties;
175    }
176
177    RenderProperties& mutateStagingProperties() {
178        return mStagingProperties;
179    }
180
181    int getWidth() const {
182        return properties().getWidth();
183    }
184
185    int getHeight() const {
186        return properties().getHeight();
187    }
188
189    ANDROID_API virtual void prepareTree(TreeInfo& info);
190    void destroyHardwareResources();
191
192    // UI thread only!
193    ANDROID_API void addAnimator(const sp<BaseRenderNodeAnimator>& animator);
194    void removeAnimator(const sp<BaseRenderNodeAnimator>& animator);
195
196    // This can only happen during pushStaging()
197    void onAnimatorTargetChanged(BaseRenderNodeAnimator* animator) {
198        mAnimatorManager.onAnimatorTargetChanged(animator);
199    }
200
201    AnimatorManager& animators() { return mAnimatorManager; }
202
203    void applyViewPropertyTransforms(mat4& matrix, bool true3dTransform = false) const;
204
205    bool nothingToDraw() const {
206        const Outline& outline = properties().getOutline();
207        return mDisplayList == nullptr
208                || properties().getAlpha() <= 0
209                || (outline.getShouldClip() && outline.isEmpty())
210                || properties().getScaleX() == 0
211                || properties().getScaleY() == 0;
212    }
213
214    const DisplayList* getDisplayList() const {
215        return mDisplayList;
216    }
217#if HWUI_NEW_OPS
218    OffscreenBuffer* getLayer() const { return mLayer; }
219    OffscreenBuffer** getLayerHandle() { return &mLayer; } // ugh...
220#endif
221
222    class ANDROID_API PositionListener {
223    public:
224        virtual ~PositionListener() {}
225        virtual void onPositionUpdated(RenderNode& node, const TreeInfo& info) = 0;
226    };
227
228    // Note this is not thread safe, this needs to be called
229    // before the RenderNode is used for drawing.
230    // RenderNode takes ownership of the pointer
231    ANDROID_API void setPositionListener(PositionListener* listener) {
232        mPositionListener.reset(listener);
233    }
234
235private:
236    typedef key_value_pair_t<float, DrawRenderNodeOp*> ZDrawRenderNodeOpPair;
237
238    static size_t findNonNegativeIndex(const std::vector<ZDrawRenderNodeOpPair>& nodes) {
239        for (size_t i = 0; i < nodes.size(); i++) {
240            if (nodes[i].key >= 0.0f) return i;
241        }
242        return nodes.size();
243    }
244
245    enum class ChildrenSelectMode {
246        NegativeZChildren,
247        PositiveZChildren
248    };
249
250    void computeOrderingImpl(renderNodeOp_t* opState,
251            std::vector<renderNodeOp_t*>* compositedChildrenOfProjectionSurface,
252            const mat4* transformFromProjectionSurface);
253
254    template <class T>
255    inline void setViewProperties(OpenGLRenderer& renderer, T& handler);
256
257    void buildZSortedChildList(const DisplayList::Chunk& chunk,
258            std::vector<ZDrawRenderNodeOpPair>& zTranslatedNodes);
259
260    template<class T>
261    inline void issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler);
262
263    template <class T>
264    inline void issueOperationsOf3dChildren(ChildrenSelectMode mode,
265            const Matrix4& initialTransform, const std::vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
266            OpenGLRenderer& renderer, T& handler);
267
268    template <class T>
269    inline void issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler);
270
271    /**
272     * Issue the RenderNode's operations into a handler, recursing for subtrees through
273     * DrawRenderNodeOp's defer() or replay() methods
274     */
275    template <class T>
276    inline void issueOperations(OpenGLRenderer& renderer, T& handler);
277
278    class TextContainer {
279    public:
280        size_t length() const {
281            return mByteLength;
282        }
283
284        const char* text() const {
285            return (const char*) mText;
286        }
287
288        size_t mByteLength;
289        const char* mText;
290    };
291
292
293    void syncProperties();
294    void syncDisplayList();
295
296    void prepareTreeImpl(TreeInfo& info, bool functorsNeedLayer);
297    void pushStagingPropertiesChanges(TreeInfo& info);
298    void pushStagingDisplayListChanges(TreeInfo& info);
299    void prepareSubTree(TreeInfo& info, bool functorsNeedLayer, DisplayList* subtree);
300#if !HWUI_NEW_OPS
301    void applyLayerPropertiesToLayer(TreeInfo& info);
302#endif
303    void prepareLayer(TreeInfo& info, uint32_t dirtyMask);
304    void pushLayerUpdate(TreeInfo& info);
305    void deleteDisplayList();
306    void damageSelf(TreeInfo& info);
307
308    void incParentRefCount() { mParentCount++; }
309    void decParentRefCount();
310
311    String8 mName;
312
313    uint32_t mDirtyPropertyFields;
314    RenderProperties mProperties;
315    RenderProperties mStagingProperties;
316
317    bool mNeedsDisplayListSync;
318    // WARNING: Do not delete this directly, you must go through deleteDisplayList()!
319    DisplayList* mDisplayList;
320    DisplayList* mStagingDisplayList;
321
322    friend class AnimatorManager;
323    AnimatorManager mAnimatorManager;
324
325    // Owned by RT. Lifecycle is managed by prepareTree(), with the exception
326    // being in ~RenderNode() which may happen on any thread.
327    layer_t* mLayer = nullptr;
328
329    /**
330     * Draw time state - these properties are only set and used during rendering
331     */
332
333    // for projection surfaces, contains a list of all children items
334    std::vector<renderNodeOp_t*> mProjectedNodes;
335
336    // How many references our parent(s) have to us. Typically this should alternate
337    // between 2 and 1 (when a staging push happens we inc first then dec)
338    // When this hits 0 we are no longer in the tree, so any hardware resources
339    // (specifically Layers) should be released.
340    // This is *NOT* thread-safe, and should therefore only be tracking
341    // mDisplayList, not mStagingDisplayList.
342    uint32_t mParentCount;
343
344    std::unique_ptr<PositionListener> mPositionListener;
345}; // class RenderNode
346
347} /* namespace uirenderer */
348} /* namespace android */
349
350#endif /* RENDERNODE_H */
351