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