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