RenderNode.h revision 272a685f17cc4828257e521a6f62b7b17870f75e
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            const SkPath* outlineOfProjectionSurface,
194            std::vector<DrawRenderNodeOp*>* compositedChildrenOfProjectionSurface,
195            const mat4* transformFromProjectionSurface);
196
197    template <class T>
198    inline void setViewProperties(OpenGLRenderer& renderer, T& handler);
199
200    void buildZSortedChildList(const DisplayListData::Chunk& chunk,
201            std::vector<ZDrawRenderNodeOpPair>& zTranslatedNodes);
202
203    template<class T>
204    inline void issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler);
205
206    template <class T>
207    inline void issueOperationsOf3dChildren(ChildrenSelectMode mode,
208            const Matrix4& initialTransform, const std::vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
209            OpenGLRenderer& renderer, T& handler);
210
211    template <class T>
212    inline void issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler);
213
214    /**
215     * Issue the RenderNode's operations into a handler, recursing for subtrees through
216     * DrawRenderNodeOp's defer() or replay() methods
217     */
218    template <class T>
219    inline void issueOperations(OpenGLRenderer& renderer, T& handler);
220
221    class TextContainer {
222    public:
223        size_t length() const {
224            return mByteLength;
225        }
226
227        const char* text() const {
228            return (const char*) mText;
229        }
230
231        size_t mByteLength;
232        const char* mText;
233    };
234
235    void prepareTreeImpl(TreeInfo& info, bool functorsNeedLayer);
236    void pushStagingPropertiesChanges(TreeInfo& info);
237    void pushStagingDisplayListChanges(TreeInfo& info);
238    void prepareSubTree(TreeInfo& info, bool functorsNeedLayer, DisplayListData* subtree);
239    void applyLayerPropertiesToLayer(TreeInfo& info);
240    void prepareLayer(TreeInfo& info, uint32_t dirtyMask);
241    void pushLayerUpdate(TreeInfo& info);
242    void deleteDisplayListData();
243    void damageSelf(TreeInfo& info);
244
245    void incParentRefCount() { mParentCount++; }
246    void decParentRefCount();
247
248    String8 mName;
249
250    uint32_t mDirtyPropertyFields;
251    RenderProperties mProperties;
252    RenderProperties mStagingProperties;
253
254    bool mNeedsDisplayListDataSync;
255    // WARNING: Do not delete this directly, you must go through deleteDisplayListData()!
256    DisplayListData* mDisplayListData;
257    DisplayListData* mStagingDisplayListData;
258
259    friend class AnimatorManager;
260    AnimatorManager mAnimatorManager;
261
262    // Owned by RT. Lifecycle is managed by prepareTree(), with the exception
263    // being in ~RenderNode() which may happen on any thread.
264    Layer* mLayer;
265
266    /**
267     * Draw time state - these properties are only set and used during rendering
268     */
269
270    // for projection surfaces, contains a list of all children items
271    std::vector<DrawRenderNodeOp*> mProjectedNodes;
272
273    // How many references our parent(s) have to us. Typically this should alternate
274    // between 2 and 1 (when a staging push happens we inc first then dec)
275    // When this hits 0 we are no longer in the tree, so any hardware resources
276    // (specifically Layers) should be released.
277    // This is *NOT* thread-safe, and should therefore only be tracking
278    // mDisplayListData, not mStagingDisplayListData.
279    uint32_t mParentCount;
280}; // class RenderNode
281
282} /* namespace uirenderer */
283} /* namespace android */
284
285#endif /* RENDERNODE_H */
286