RenderNode.h revision 12abdeb3c7bffbc7c07b913595f6ea2cb66154d1
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    ANDROID_API void output(uint32_t level = 1);
127    ANDROID_API int getDebugSize();
128    void copyTo(proto::RenderNode* node);
129
130    bool isRenderable() const {
131        return mDisplayList && !mDisplayList->isEmpty();
132    }
133
134    bool hasProjectionReceiver() const {
135        return mDisplayList && mDisplayList->projectionReceiveIndex >= 0;
136    }
137
138    const char* getName() const {
139        return mName.string();
140    }
141
142    void setName(const char* name) {
143        if (name) {
144            char* lastPeriod = strrchr(name, '.');
145            if (lastPeriod) {
146                mName.setTo(lastPeriod + 1);
147            } else {
148                mName.setTo(name);
149            }
150        }
151    }
152
153    bool isPropertyFieldDirty(DirtyPropertyMask field) const {
154        return mDirtyPropertyFields & field;
155    }
156
157    void setPropertyFieldsDirty(uint32_t fields) {
158        mDirtyPropertyFields |= fields;
159    }
160
161    const RenderProperties& properties() const {
162        return mProperties;
163    }
164
165    RenderProperties& animatorProperties() {
166        return mProperties;
167    }
168
169    const RenderProperties& stagingProperties() {
170        return mStagingProperties;
171    }
172
173    RenderProperties& mutateStagingProperties() {
174        return mStagingProperties;
175    }
176
177    int getWidth() const {
178        return properties().getWidth();
179    }
180
181    int getHeight() const {
182        return properties().getHeight();
183    }
184
185    ANDROID_API virtual void prepareTree(TreeInfo& info);
186    void destroyHardwareResources();
187
188    // UI thread only!
189    ANDROID_API void addAnimator(const sp<BaseRenderNodeAnimator>& animator);
190    void removeAnimator(const sp<BaseRenderNodeAnimator>& animator);
191
192    // This can only happen during pushStaging()
193    void onAnimatorTargetChanged(BaseRenderNodeAnimator* animator) {
194        mAnimatorManager.onAnimatorTargetChanged(animator);
195    }
196
197    AnimatorManager& animators() { return mAnimatorManager; }
198
199    void applyViewPropertyTransforms(mat4& matrix, bool true3dTransform = false) const;
200
201    bool nothingToDraw() const {
202        const Outline& outline = properties().getOutline();
203        return mDisplayList == nullptr
204                || properties().getAlpha() <= 0
205                || (outline.getShouldClip() && outline.isEmpty())
206                || properties().getScaleX() == 0
207                || properties().getScaleY() == 0;
208    }
209
210    const DisplayList* getDisplayList() const {
211        return mDisplayList;
212    }
213#if HWUI_NEW_OPS
214    OffscreenBuffer* getLayer() const { return mLayer; }
215    OffscreenBuffer** getLayerHandle() { return &mLayer; } // ugh...
216#endif
217
218    class ANDROID_API PositionListener {
219    public:
220        virtual ~PositionListener() {}
221        virtual void onPositionUpdated(RenderNode& node, const TreeInfo& info) = 0;
222    };
223
224    // Note this is not thread safe, this needs to be called
225    // before the RenderNode is used for drawing.
226    // RenderNode takes ownership of the pointer
227    ANDROID_API void setPositionListener(PositionListener* listener) {
228        mPositionListener.reset(listener);
229    }
230
231private:
232    typedef key_value_pair_t<float, DrawRenderNodeOp*> ZDrawRenderNodeOpPair;
233
234    static size_t findNonNegativeIndex(const std::vector<ZDrawRenderNodeOpPair>& nodes) {
235        for (size_t i = 0; i < nodes.size(); i++) {
236            if (nodes[i].key >= 0.0f) return i;
237        }
238        return nodes.size();
239    }
240
241    enum class ChildrenSelectMode {
242        NegativeZChildren,
243        PositiveZChildren
244    };
245
246    void computeOrderingImpl(renderNodeOp_t* opState,
247            std::vector<renderNodeOp_t*>* compositedChildrenOfProjectionSurface,
248            const mat4* transformFromProjectionSurface);
249
250    template <class T>
251    inline void setViewProperties(OpenGLRenderer& renderer, T& handler);
252
253    void buildZSortedChildList(const DisplayList::Chunk& chunk,
254            std::vector<ZDrawRenderNodeOpPair>& zTranslatedNodes);
255
256    template<class T>
257    inline void issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler);
258
259    template <class T>
260    inline void issueOperationsOf3dChildren(ChildrenSelectMode mode,
261            const Matrix4& initialTransform, const std::vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
262            OpenGLRenderer& renderer, T& handler);
263
264    template <class T>
265    inline void issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler);
266
267    /**
268     * Issue the RenderNode's operations into a handler, recursing for subtrees through
269     * DrawRenderNodeOp's defer() or replay() methods
270     */
271    template <class T>
272    inline void issueOperations(OpenGLRenderer& renderer, T& handler);
273
274    class TextContainer {
275    public:
276        size_t length() const {
277            return mByteLength;
278        }
279
280        const char* text() const {
281            return (const char*) mText;
282        }
283
284        size_t mByteLength;
285        const char* mText;
286    };
287
288
289    void syncProperties();
290    void syncDisplayList();
291
292    void prepareTreeImpl(TreeInfo& info, bool functorsNeedLayer);
293    void pushStagingPropertiesChanges(TreeInfo& info);
294    void pushStagingDisplayListChanges(TreeInfo& info);
295    void prepareSubTree(TreeInfo& info, bool functorsNeedLayer, DisplayList* subtree);
296#if !HWUI_NEW_OPS
297    void applyLayerPropertiesToLayer(TreeInfo& info);
298#endif
299    void prepareLayer(TreeInfo& info, uint32_t dirtyMask);
300    void pushLayerUpdate(TreeInfo& info);
301    void deleteDisplayList();
302    void damageSelf(TreeInfo& info);
303
304    void incParentRefCount() { mParentCount++; }
305    void decParentRefCount();
306
307    String8 mName;
308
309    uint32_t mDirtyPropertyFields;
310    RenderProperties mProperties;
311    RenderProperties mStagingProperties;
312
313    bool mNeedsDisplayListSync;
314    // WARNING: Do not delete this directly, you must go through deleteDisplayList()!
315    DisplayList* mDisplayList;
316    DisplayList* mStagingDisplayList;
317
318    friend class AnimatorManager;
319    AnimatorManager mAnimatorManager;
320
321    // Owned by RT. Lifecycle is managed by prepareTree(), with the exception
322    // being in ~RenderNode() which may happen on any thread.
323    layer_t* mLayer = nullptr;
324
325    /**
326     * Draw time state - these properties are only set and used during rendering
327     */
328
329    // for projection surfaces, contains a list of all children items
330    std::vector<renderNodeOp_t*> mProjectedNodes;
331
332    // How many references our parent(s) have to us. Typically this should alternate
333    // between 2 and 1 (when a staging push happens we inc first then dec)
334    // When this hits 0 we are no longer in the tree, so any hardware resources
335    // (specifically Layers) should be released.
336    // This is *NOT* thread-safe, and should therefore only be tracking
337    // mDisplayList, not mStagingDisplayList.
338    uint32_t mParentCount;
339
340    std::unique_ptr<PositionListener> mPositionListener;
341}; // class RenderNode
342
343} /* namespace uirenderer */
344} /* namespace android */
345
346#endif /* RENDERNODE_H */
347