RenderNode.h revision f648108f83d4e74811919e9811efb8fcc184b8a3
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
191    AnimatorManager& animators() { return mAnimatorManager; }
192
193    void applyViewPropertyTransforms(mat4& matrix, bool true3dTransform = false) const;
194
195    bool nothingToDraw() const {
196        const Outline& outline = properties().getOutline();
197        return mDisplayList == nullptr
198                || properties().getAlpha() <= 0
199                || (outline.getShouldClip() && outline.isEmpty())
200                || properties().getScaleX() == 0
201                || properties().getScaleY() == 0;
202    }
203
204    const DisplayList* getDisplayList() const {
205        return mDisplayList;
206    }
207#if HWUI_NEW_OPS
208    OffscreenBuffer* getLayer() const { return mLayer; }
209    OffscreenBuffer** getLayerHandle() { return &mLayer; } // ugh...
210#endif
211
212    class ANDROID_API PositionListener {
213    public:
214        virtual ~PositionListener() {}
215        virtual void onPositionUpdated(RenderNode& node, const TreeInfo& info) = 0;
216    };
217
218    // Note this is not thread safe, this needs to be called
219    // before the RenderNode is used for drawing.
220    // RenderNode takes ownership of the pointer
221    ANDROID_API void setPositionListener(PositionListener* listener) {
222        mPositionListener.reset(listener);
223    }
224
225private:
226    typedef key_value_pair_t<float, DrawRenderNodeOp*> ZDrawRenderNodeOpPair;
227
228    static size_t findNonNegativeIndex(const std::vector<ZDrawRenderNodeOpPair>& nodes) {
229        for (size_t i = 0; i < nodes.size(); i++) {
230            if (nodes[i].key >= 0.0f) return i;
231        }
232        return nodes.size();
233    }
234
235    enum class ChildrenSelectMode {
236        NegativeZChildren,
237        PositiveZChildren
238    };
239
240    void computeOrderingImpl(renderNodeOp_t* opState,
241            std::vector<renderNodeOp_t*>* compositedChildrenOfProjectionSurface,
242            const mat4* transformFromProjectionSurface);
243
244    template <class T>
245    inline void setViewProperties(OpenGLRenderer& renderer, T& handler);
246
247    void buildZSortedChildList(const DisplayList::Chunk& chunk,
248            std::vector<ZDrawRenderNodeOpPair>& zTranslatedNodes);
249
250    template<class T>
251    inline void issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler);
252
253    template <class T>
254    inline void issueOperationsOf3dChildren(ChildrenSelectMode mode,
255            const Matrix4& initialTransform, const std::vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
256            OpenGLRenderer& renderer, T& handler);
257
258    template <class T>
259    inline void issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler);
260
261    /**
262     * Issue the RenderNode's operations into a handler, recursing for subtrees through
263     * DrawRenderNodeOp's defer() or replay() methods
264     */
265    template <class T>
266    inline void issueOperations(OpenGLRenderer& renderer, T& handler);
267
268    class TextContainer {
269    public:
270        size_t length() const {
271            return mByteLength;
272        }
273
274        const char* text() const {
275            return (const char*) mText;
276        }
277
278        size_t mByteLength;
279        const char* mText;
280    };
281
282
283    void syncProperties();
284    void syncDisplayList();
285
286    void prepareTreeImpl(TreeInfo& info, bool functorsNeedLayer);
287    void pushStagingPropertiesChanges(TreeInfo& info);
288    void pushStagingDisplayListChanges(TreeInfo& info);
289    void prepareSubTree(TreeInfo& info, bool functorsNeedLayer, DisplayList* subtree);
290#if !HWUI_NEW_OPS
291    void applyLayerPropertiesToLayer(TreeInfo& info);
292#endif
293    void prepareLayer(TreeInfo& info, uint32_t dirtyMask);
294    void pushLayerUpdate(TreeInfo& info);
295    void deleteDisplayList();
296    void damageSelf(TreeInfo& info);
297
298    void incParentRefCount() { mParentCount++; }
299    void decParentRefCount();
300
301    String8 mName;
302
303    uint32_t mDirtyPropertyFields;
304    RenderProperties mProperties;
305    RenderProperties mStagingProperties;
306
307    bool mNeedsDisplayListSync;
308    // WARNING: Do not delete this directly, you must go through deleteDisplayList()!
309    DisplayList* mDisplayList;
310    DisplayList* mStagingDisplayList;
311
312    friend class AnimatorManager;
313    AnimatorManager mAnimatorManager;
314
315    // Owned by RT. Lifecycle is managed by prepareTree(), with the exception
316    // being in ~RenderNode() which may happen on any thread.
317    layer_t* mLayer = nullptr;
318
319    /**
320     * Draw time state - these properties are only set and used during rendering
321     */
322
323    // for projection surfaces, contains a list of all children items
324    std::vector<renderNodeOp_t*> mProjectedNodes;
325
326    // How many references our parent(s) have to us. Typically this should alternate
327    // between 2 and 1 (when a staging push happens we inc first then dec)
328    // When this hits 0 we are no longer in the tree, so any hardware resources
329    // (specifically Layers) should be released.
330    // This is *NOT* thread-safe, and should therefore only be tracking
331    // mDisplayList, not mStagingDisplayList.
332    uint32_t mParentCount;
333
334    std::unique_ptr<PositionListener> mPositionListener;
335}; // class RenderNode
336
337} /* namespace uirenderer */
338} /* namespace android */
339
340#endif /* RENDERNODE_H */
341