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