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