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