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