RenderNode.h revision b265e2ca50b6ceb2fd2987ef1f7d063b1bde19ae
1113e0824d6bddf4376240681f9cf6a2deded9498John Reck/*
2113e0824d6bddf4376240681f9cf6a2deded9498John Reck * Copyright (C) 2014 The Android Open Source Project
3113e0824d6bddf4376240681f9cf6a2deded9498John Reck *
4113e0824d6bddf4376240681f9cf6a2deded9498John Reck * Licensed under the Apache License, Version 2.0 (the "License");
5113e0824d6bddf4376240681f9cf6a2deded9498John Reck * you may not use this file except in compliance with the License.
6113e0824d6bddf4376240681f9cf6a2deded9498John Reck * You may obtain a copy of the License at
7113e0824d6bddf4376240681f9cf6a2deded9498John Reck *
8113e0824d6bddf4376240681f9cf6a2deded9498John Reck *      http://www.apache.org/licenses/LICENSE-2.0
9113e0824d6bddf4376240681f9cf6a2deded9498John Reck *
10113e0824d6bddf4376240681f9cf6a2deded9498John Reck * Unless required by applicable law or agreed to in writing, software
11113e0824d6bddf4376240681f9cf6a2deded9498John Reck * distributed under the License is distributed on an "AS IS" BASIS,
12113e0824d6bddf4376240681f9cf6a2deded9498John Reck * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13113e0824d6bddf4376240681f9cf6a2deded9498John Reck * See the License for the specific language governing permissions and
14113e0824d6bddf4376240681f9cf6a2deded9498John Reck * limitations under the License.
15113e0824d6bddf4376240681f9cf6a2deded9498John Reck */
16113e0824d6bddf4376240681f9cf6a2deded9498John Reck#ifndef RENDERNODE_H
17113e0824d6bddf4376240681f9cf6a2deded9498John Reck#define RENDERNODE_H
18113e0824d6bddf4376240681f9cf6a2deded9498John Reck
19113e0824d6bddf4376240681f9cf6a2deded9498John Reck#ifndef LOG_TAG
20113e0824d6bddf4376240681f9cf6a2deded9498John Reck    #define LOG_TAG "OpenGLRenderer"
21113e0824d6bddf4376240681f9cf6a2deded9498John Reck#endif
22113e0824d6bddf4376240681f9cf6a2deded9498John Reck
23113e0824d6bddf4376240681f9cf6a2deded9498John Reck#include <SkCamera.h>
24113e0824d6bddf4376240681f9cf6a2deded9498John Reck#include <SkMatrix.h>
25113e0824d6bddf4376240681f9cf6a2deded9498John Reck
26113e0824d6bddf4376240681f9cf6a2deded9498John Reck#include <private/hwui/DrawGlInfo.h>
27113e0824d6bddf4376240681f9cf6a2deded9498John Reck
28113e0824d6bddf4376240681f9cf6a2deded9498John Reck#include <utils/KeyedVector.h>
29113e0824d6bddf4376240681f9cf6a2deded9498John Reck#include <utils/LinearAllocator.h>
30113e0824d6bddf4376240681f9cf6a2deded9498John Reck#include <utils/RefBase.h>
31113e0824d6bddf4376240681f9cf6a2deded9498John Reck#include <utils/SortedVector.h>
32113e0824d6bddf4376240681f9cf6a2deded9498John Reck#include <utils/String8.h>
33113e0824d6bddf4376240681f9cf6a2deded9498John Reck#include <utils/Vector.h>
34113e0824d6bddf4376240681f9cf6a2deded9498John Reck
35113e0824d6bddf4376240681f9cf6a2deded9498John Reck#include <cutils/compiler.h>
36113e0824d6bddf4376240681f9cf6a2deded9498John Reck
37113e0824d6bddf4376240681f9cf6a2deded9498John Reck#include <androidfw/ResourceTypes.h>
38113e0824d6bddf4376240681f9cf6a2deded9498John Reck
39113e0824d6bddf4376240681f9cf6a2deded9498John Reck#include "Debug.h"
40113e0824d6bddf4376240681f9cf6a2deded9498John Reck#include "Matrix.h"
41113e0824d6bddf4376240681f9cf6a2deded9498John Reck#include "DeferredDisplayList.h"
42113e0824d6bddf4376240681f9cf6a2deded9498John Reck#include "DisplayList.h"
43113e0824d6bddf4376240681f9cf6a2deded9498John Reck#include "RenderProperties.h"
44113e0824d6bddf4376240681f9cf6a2deded9498John Reck
45113e0824d6bddf4376240681f9cf6a2deded9498John Reckclass SkBitmap;
46113e0824d6bddf4376240681f9cf6a2deded9498John Reckclass SkPaint;
47113e0824d6bddf4376240681f9cf6a2deded9498John Reckclass SkPath;
48113e0824d6bddf4376240681f9cf6a2deded9498John Reckclass SkRegion;
49113e0824d6bddf4376240681f9cf6a2deded9498John Reck
50113e0824d6bddf4376240681f9cf6a2deded9498John Recknamespace android {
51113e0824d6bddf4376240681f9cf6a2deded9498John Recknamespace uirenderer {
52113e0824d6bddf4376240681f9cf6a2deded9498John Reck
53113e0824d6bddf4376240681f9cf6a2deded9498John Reckclass DeferredDisplayList;
54113e0824d6bddf4376240681f9cf6a2deded9498John Reckclass DisplayListOp;
55113e0824d6bddf4376240681f9cf6a2deded9498John Reckclass DisplayListRenderer;
56113e0824d6bddf4376240681f9cf6a2deded9498John Reckclass OpenGLRenderer;
57113e0824d6bddf4376240681f9cf6a2deded9498John Reckclass Rect;
58113e0824d6bddf4376240681f9cf6a2deded9498John Reckclass Layer;
59113e0824d6bddf4376240681f9cf6a2deded9498John Reckclass SkiaShader;
60113e0824d6bddf4376240681f9cf6a2deded9498John Reck
61113e0824d6bddf4376240681f9cf6a2deded9498John Reckclass ClipRectOp;
62113e0824d6bddf4376240681f9cf6a2deded9498John Reckclass SaveLayerOp;
63113e0824d6bddf4376240681f9cf6a2deded9498John Reckclass SaveOp;
64113e0824d6bddf4376240681f9cf6a2deded9498John Reckclass RestoreToCountOp;
65113e0824d6bddf4376240681f9cf6a2deded9498John Reckclass DrawDisplayListOp;
66113e0824d6bddf4376240681f9cf6a2deded9498John Reck
67113e0824d6bddf4376240681f9cf6a2deded9498John Reck/**
68113e0824d6bddf4376240681f9cf6a2deded9498John Reck * Primary class for storing recorded canvas commands, as well as per-View/ViewGroup display properties.
69113e0824d6bddf4376240681f9cf6a2deded9498John Reck *
70113e0824d6bddf4376240681f9cf6a2deded9498John Reck * Recording of canvas commands is somewhat similar to SkPicture, except the canvas-recording
71113e0824d6bddf4376240681f9cf6a2deded9498John Reck * functionality is split between DisplayListRenderer (which manages the recording), DisplayListData
72113e0824d6bddf4376240681f9cf6a2deded9498John Reck * (which holds the actual data), and DisplayList (which holds properties and performs playback onto
73113e0824d6bddf4376240681f9cf6a2deded9498John Reck * a renderer).
74113e0824d6bddf4376240681f9cf6a2deded9498John Reck *
75113e0824d6bddf4376240681f9cf6a2deded9498John Reck * Note that DisplayListData is swapped out from beneath an individual DisplayList when a view's
76113e0824d6bddf4376240681f9cf6a2deded9498John Reck * recorded stream of canvas operations is refreshed. The DisplayList (and its properties) stay
77113e0824d6bddf4376240681f9cf6a2deded9498John Reck * attached.
78113e0824d6bddf4376240681f9cf6a2deded9498John Reck */
79113e0824d6bddf4376240681f9cf6a2deded9498John Reckclass RenderNode {
80113e0824d6bddf4376240681f9cf6a2deded9498John Reckpublic:
81113e0824d6bddf4376240681f9cf6a2deded9498John Reck    ANDROID_API RenderNode();
82113e0824d6bddf4376240681f9cf6a2deded9498John Reck    ANDROID_API ~RenderNode();
83113e0824d6bddf4376240681f9cf6a2deded9498John Reck
84113e0824d6bddf4376240681f9cf6a2deded9498John Reck    // See flags defined in DisplayList.java
85113e0824d6bddf4376240681f9cf6a2deded9498John Reck    enum ReplayFlag {
86113e0824d6bddf4376240681f9cf6a2deded9498John Reck        kReplayFlag_ClipChildren = 0x1
87113e0824d6bddf4376240681f9cf6a2deded9498John Reck    };
88113e0824d6bddf4376240681f9cf6a2deded9498John Reck
89113e0824d6bddf4376240681f9cf6a2deded9498John Reck    ANDROID_API static void destroyDisplayListDeferred(RenderNode* displayList);
90113e0824d6bddf4376240681f9cf6a2deded9498John Reck    ANDROID_API static void outputLogBuffer(int fd);
91113e0824d6bddf4376240681f9cf6a2deded9498John Reck
92113e0824d6bddf4376240681f9cf6a2deded9498John Reck    ANDROID_API void setData(DisplayListData* newData);
93113e0824d6bddf4376240681f9cf6a2deded9498John Reck
94113e0824d6bddf4376240681f9cf6a2deded9498John Reck    void computeOrdering();
95b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik
96b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    void deferNodeTree(DeferStateStruct& deferStruct);
97b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    void deferNodeInParent(DeferStateStruct& deferStruct, const int level);
98b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik
99b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    void replayNodeTree(ReplayStateStruct& replayStruct);
100b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    void replayNodeInParent(ReplayStateStruct& replayStruct, const int level);
101113e0824d6bddf4376240681f9cf6a2deded9498John Reck
102113e0824d6bddf4376240681f9cf6a2deded9498John Reck    ANDROID_API void output(uint32_t level = 1);
103113e0824d6bddf4376240681f9cf6a2deded9498John Reck
104113e0824d6bddf4376240681f9cf6a2deded9498John Reck    bool isRenderable() const {
105113e0824d6bddf4376240681f9cf6a2deded9498John Reck        return mDisplayListData && mDisplayListData->hasDrawOps;
106113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
107113e0824d6bddf4376240681f9cf6a2deded9498John Reck
108113e0824d6bddf4376240681f9cf6a2deded9498John Reck    void setName(const char* name) {
109113e0824d6bddf4376240681f9cf6a2deded9498John Reck        if (name) {
110113e0824d6bddf4376240681f9cf6a2deded9498John Reck            char* lastPeriod = strrchr(name, '.');
111113e0824d6bddf4376240681f9cf6a2deded9498John Reck            if (lastPeriod) {
112113e0824d6bddf4376240681f9cf6a2deded9498John Reck                mName.setTo(lastPeriod + 1);
113113e0824d6bddf4376240681f9cf6a2deded9498John Reck            } else {
114113e0824d6bddf4376240681f9cf6a2deded9498John Reck                mName.setTo(name);
115113e0824d6bddf4376240681f9cf6a2deded9498John Reck            }
116113e0824d6bddf4376240681f9cf6a2deded9498John Reck        }
117113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
118113e0824d6bddf4376240681f9cf6a2deded9498John Reck
119d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    const RenderProperties& properties() {
120113e0824d6bddf4376240681f9cf6a2deded9498John Reck        return mProperties;
121113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
122113e0824d6bddf4376240681f9cf6a2deded9498John Reck
123d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    const RenderProperties& stagingProperties() {
124d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        return mStagingProperties;
125d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    }
126d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck
127d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    RenderProperties& mutateStagingProperties() {
128d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        mNeedsPropertiesSync = true;
129d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck        return mStagingProperties;
130d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    }
131d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck
132113e0824d6bddf4376240681f9cf6a2deded9498John Reck    bool isProjectionReceiver() {
133113e0824d6bddf4376240681f9cf6a2deded9498John Reck        return properties().isProjectionReceiver();
134113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
135113e0824d6bddf4376240681f9cf6a2deded9498John Reck
136113e0824d6bddf4376240681f9cf6a2deded9498John Reck    int getWidth() {
137113e0824d6bddf4376240681f9cf6a2deded9498John Reck        return properties().getWidth();
138113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
139113e0824d6bddf4376240681f9cf6a2deded9498John Reck
140113e0824d6bddf4376240681f9cf6a2deded9498John Reck    int getHeight() {
141113e0824d6bddf4376240681f9cf6a2deded9498John Reck        return properties().getHeight();
142113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
143113e0824d6bddf4376240681f9cf6a2deded9498John Reck
144bfb07a03777af424e99bca1dac4c903aaf44e99dJohn Reck    ANDROID_API void updateProperties();
145d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck
146668f0e38ef0277d55d3118af37e17b8c435df85cJohn Reck    // Returns true if this RenderNode or any of its children have functors
147668f0e38ef0277d55d3118af37e17b8c435df85cJohn Reck    bool hasFunctors();
148668f0e38ef0277d55d3118af37e17b8c435df85cJohn Reck
149113e0824d6bddf4376240681f9cf6a2deded9498John Reckprivate:
150113e0824d6bddf4376240681f9cf6a2deded9498John Reck    typedef key_value_pair_t<float, DrawDisplayListOp*> ZDrawDisplayListOpPair;
151113e0824d6bddf4376240681f9cf6a2deded9498John Reck
152113e0824d6bddf4376240681f9cf6a2deded9498John Reck    static size_t findNonNegativeIndex(const Vector<ZDrawDisplayListOpPair>& nodes) {
153113e0824d6bddf4376240681f9cf6a2deded9498John Reck        for (size_t i = 0; i < nodes.size(); i++) {
154113e0824d6bddf4376240681f9cf6a2deded9498John Reck            if (nodes[i].key >= 0.0f) return i;
155113e0824d6bddf4376240681f9cf6a2deded9498John Reck        }
156113e0824d6bddf4376240681f9cf6a2deded9498John Reck        return nodes.size();
157113e0824d6bddf4376240681f9cf6a2deded9498John Reck    }
158113e0824d6bddf4376240681f9cf6a2deded9498John Reck
159113e0824d6bddf4376240681f9cf6a2deded9498John Reck    enum ChildrenSelectMode {
160113e0824d6bddf4376240681f9cf6a2deded9498John Reck        kNegativeZChildren,
161113e0824d6bddf4376240681f9cf6a2deded9498John Reck        kPositiveZChildren
162113e0824d6bddf4376240681f9cf6a2deded9498John Reck    };
163113e0824d6bddf4376240681f9cf6a2deded9498John Reck
164113e0824d6bddf4376240681f9cf6a2deded9498John Reck    void applyViewPropertyTransforms(mat4& matrix, bool true3dTransform = false);
165113e0824d6bddf4376240681f9cf6a2deded9498John Reck
166113e0824d6bddf4376240681f9cf6a2deded9498John Reck    void computeOrderingImpl(DrawDisplayListOp* opState,
167113e0824d6bddf4376240681f9cf6a2deded9498John Reck            Vector<DrawDisplayListOp*>* compositedChildrenOfProjectionSurface,
168113e0824d6bddf4376240681f9cf6a2deded9498John Reck            const mat4* transformFromProjectionSurface);
169113e0824d6bddf4376240681f9cf6a2deded9498John Reck
170113e0824d6bddf4376240681f9cf6a2deded9498John Reck    template <class T>
171b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    inline void setViewProperties(OpenGLRenderer& renderer, T& handler);
172113e0824d6bddf4376240681f9cf6a2deded9498John Reck
173113e0824d6bddf4376240681f9cf6a2deded9498John Reck    void buildZSortedChildList(Vector<ZDrawDisplayListOpPair>& zTranslatedNodes);
174113e0824d6bddf4376240681f9cf6a2deded9498John Reck
175b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    template<class T>
176b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    inline void issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler);
177b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik
178113e0824d6bddf4376240681f9cf6a2deded9498John Reck    template <class T>
179b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    inline void issueOperationsOf3dChildren(const Vector<ZDrawDisplayListOpPair>& zTranslatedNodes,
180113e0824d6bddf4376240681f9cf6a2deded9498John Reck            ChildrenSelectMode mode, OpenGLRenderer& renderer, T& handler);
181113e0824d6bddf4376240681f9cf6a2deded9498John Reck
182113e0824d6bddf4376240681f9cf6a2deded9498John Reck    template <class T>
183b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    inline void issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler);
184113e0824d6bddf4376240681f9cf6a2deded9498John Reck
185b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    /**
186b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik     * Issue the RenderNode's operations into a handler, recursing for subtrees through
187b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik     * DrawDisplayListOp's defer() or replay() methods
188b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik     */
189113e0824d6bddf4376240681f9cf6a2deded9498John Reck    template <class T>
190b265e2ca50b6ceb2fd2987ef1f7d063b1bde19aeChris Craik    inline void issueOperations(OpenGLRenderer& renderer, T& handler);
191113e0824d6bddf4376240681f9cf6a2deded9498John Reck
192113e0824d6bddf4376240681f9cf6a2deded9498John Reck    class TextContainer {
193113e0824d6bddf4376240681f9cf6a2deded9498John Reck    public:
194113e0824d6bddf4376240681f9cf6a2deded9498John Reck        size_t length() const {
195113e0824d6bddf4376240681f9cf6a2deded9498John Reck            return mByteLength;
196113e0824d6bddf4376240681f9cf6a2deded9498John Reck        }
197113e0824d6bddf4376240681f9cf6a2deded9498John Reck
198113e0824d6bddf4376240681f9cf6a2deded9498John Reck        const char* text() const {
199113e0824d6bddf4376240681f9cf6a2deded9498John Reck            return (const char*) mText;
200113e0824d6bddf4376240681f9cf6a2deded9498John Reck        }
201113e0824d6bddf4376240681f9cf6a2deded9498John Reck
202113e0824d6bddf4376240681f9cf6a2deded9498John Reck        size_t mByteLength;
203113e0824d6bddf4376240681f9cf6a2deded9498John Reck        const char* mText;
204113e0824d6bddf4376240681f9cf6a2deded9498John Reck    };
205113e0824d6bddf4376240681f9cf6a2deded9498John Reck
206113e0824d6bddf4376240681f9cf6a2deded9498John Reck    String8 mName;
207113e0824d6bddf4376240681f9cf6a2deded9498John Reck    bool mDestroyed; // used for debugging crash, TODO: remove once invalid state crash fixed
208113e0824d6bddf4376240681f9cf6a2deded9498John Reck
209d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    bool mNeedsPropertiesSync;
210113e0824d6bddf4376240681f9cf6a2deded9498John Reck    RenderProperties mProperties;
211d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck    RenderProperties mStagingProperties;
212d0a0b2a3140bfb1819a116413ce9d81886697a07John Reck
213113e0824d6bddf4376240681f9cf6a2deded9498John Reck    DisplayListData* mDisplayListData;
214113e0824d6bddf4376240681f9cf6a2deded9498John Reck
215113e0824d6bddf4376240681f9cf6a2deded9498John Reck    /**
216113e0824d6bddf4376240681f9cf6a2deded9498John Reck     * Draw time state - these properties are only set and used during rendering
217113e0824d6bddf4376240681f9cf6a2deded9498John Reck     */
218113e0824d6bddf4376240681f9cf6a2deded9498John Reck
219113e0824d6bddf4376240681f9cf6a2deded9498John Reck    // for projection surfaces, contains a list of all children items
220113e0824d6bddf4376240681f9cf6a2deded9498John Reck    Vector<DrawDisplayListOp*> mProjectedNodes;
221113e0824d6bddf4376240681f9cf6a2deded9498John Reck}; // class RenderNode
222113e0824d6bddf4376240681f9cf6a2deded9498John Reck
223113e0824d6bddf4376240681f9cf6a2deded9498John Reck} /* namespace uirenderer */
224113e0824d6bddf4376240681f9cf6a2deded9498John Reck} /* namespace android */
225113e0824d6bddf4376240681f9cf6a2deded9498John Reck
226113e0824d6bddf4376240681f9cf6a2deded9498John Reck#endif /* RENDERNODE_H */
227