blob: bc62ee19f6e5c4f667c047e23efcc600f61c0ae8 [file] [log] [blame]
John Reck113e0822014-03-18 09:22:59 -07001/*
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
John Recke45b1fd2014-04-15 09:50:16 -070023#include <set>
24#include <vector>
25
John Reck113e0822014-03-18 09:22:59 -070026#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"
John Recke45b1fd2014-04-15 09:50:16 -070047#include "TreeInfo.h"
John Reck113e0822014-03-18 09:22:59 -070048
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 */
John Reck087bc0c2014-04-04 16:20:08 -070083class RenderNode : public VirtualLightRefBase {
John Reck113e0822014-03-18 09:22:59 -070084public:
85 ANDROID_API RenderNode();
John Recke45b1fd2014-04-15 09:50:16 -070086 ANDROID_API virtual ~RenderNode();
John Reck113e0822014-03-18 09:22:59 -070087
88 // See flags defined in DisplayList.java
89 enum ReplayFlag {
90 kReplayFlag_ClipChildren = 0x1
91 };
92
John Reck113e0822014-03-18 09:22:59 -070093 ANDROID_API static void outputLogBuffer(int fd);
94
John Reck8de65a82014-04-09 15:23:38 -070095 ANDROID_API void setStagingDisplayList(DisplayListData* newData);
John Reck113e0822014-03-18 09:22:59 -070096
97 void computeOrdering();
Chris Craikb265e2c2014-03-27 15:50:09 -070098
99 void deferNodeTree(DeferStateStruct& deferStruct);
100 void deferNodeInParent(DeferStateStruct& deferStruct, const int level);
101
102 void replayNodeTree(ReplayStateStruct& replayStruct);
103 void replayNodeInParent(ReplayStateStruct& replayStruct, const int level);
John Reck113e0822014-03-18 09:22:59 -0700104
105 ANDROID_API void output(uint32_t level = 1);
106
107 bool isRenderable() const {
108 return mDisplayListData && mDisplayListData->hasDrawOps;
109 }
110
Chris Craikdefb7f32014-04-08 18:17:07 -0700111 const char* getName() const {
112 return mName.string();
113 }
114
John Reck113e0822014-03-18 09:22:59 -0700115 void setName(const char* name) {
116 if (name) {
117 char* lastPeriod = strrchr(name, '.');
118 if (lastPeriod) {
119 mName.setTo(lastPeriod + 1);
120 } else {
121 mName.setTo(name);
122 }
123 }
124 }
125
John Reckd0a0b2a2014-03-20 16:28:56 -0700126 const RenderProperties& properties() {
John Reck113e0822014-03-18 09:22:59 -0700127 return mProperties;
128 }
129
John Reck52244ff2014-05-01 21:27:37 -0700130 RenderProperties& animatorProperties() {
131 return mProperties;
132 }
133
John Reckd0a0b2a2014-03-20 16:28:56 -0700134 const RenderProperties& stagingProperties() {
135 return mStagingProperties;
136 }
137
138 RenderProperties& mutateStagingProperties() {
139 mNeedsPropertiesSync = true;
140 return mStagingProperties;
141 }
142
John Reck113e0822014-03-18 09:22:59 -0700143 int getWidth() {
144 return properties().getWidth();
145 }
146
147 int getHeight() {
148 return properties().getHeight();
149 }
150
John Recke45b1fd2014-04-15 09:50:16 -0700151 ANDROID_API virtual void prepareTree(TreeInfo& info);
152
153 // UI thread only!
John Reck52244ff2014-05-01 21:27:37 -0700154 ANDROID_API void addAnimator(const sp<BaseRenderNodeAnimator>& animator) {
John Recke45b1fd2014-04-15 09:50:16 -0700155 mStagingAnimators.insert(animator);
156 mNeedsAnimatorsSync = true;
157 }
158
159 // UI thread only!
John Reck52244ff2014-05-01 21:27:37 -0700160 ANDROID_API void removeAnimator(const sp<BaseRenderNodeAnimator>& animator) {
John Recke45b1fd2014-04-15 09:50:16 -0700161 mStagingAnimators.erase(animator);
162 mNeedsAnimatorsSync = true;
163 }
John Reck668f0e32014-03-26 15:10:40 -0700164
John Reck113e0822014-03-18 09:22:59 -0700165private:
166 typedef key_value_pair_t<float, DrawDisplayListOp*> ZDrawDisplayListOpPair;
167
168 static size_t findNonNegativeIndex(const Vector<ZDrawDisplayListOpPair>& nodes) {
169 for (size_t i = 0; i < nodes.size(); i++) {
170 if (nodes[i].key >= 0.0f) return i;
171 }
172 return nodes.size();
173 }
174
175 enum ChildrenSelectMode {
176 kNegativeZChildren,
177 kPositiveZChildren
178 };
179
John Reck113e0822014-03-18 09:22:59 -0700180 void applyViewPropertyTransforms(mat4& matrix, bool true3dTransform = false);
181
182 void computeOrderingImpl(DrawDisplayListOp* opState,
Chris Craik3f0854292014-04-15 16:18:08 -0700183 const SkPath* outlineOfProjectionSurface,
John Reck113e0822014-03-18 09:22:59 -0700184 Vector<DrawDisplayListOp*>* compositedChildrenOfProjectionSurface,
185 const mat4* transformFromProjectionSurface);
186
187 template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700188 inline void setViewProperties(OpenGLRenderer& renderer, T& handler);
John Reck113e0822014-03-18 09:22:59 -0700189
190 void buildZSortedChildList(Vector<ZDrawDisplayListOpPair>& zTranslatedNodes);
191
Chris Craikb265e2c2014-03-27 15:50:09 -0700192 template<class T>
193 inline void issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler);
194
John Reck113e0822014-03-18 09:22:59 -0700195 template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700196 inline void issueOperationsOf3dChildren(const Vector<ZDrawDisplayListOpPair>& zTranslatedNodes,
John Reck113e0822014-03-18 09:22:59 -0700197 ChildrenSelectMode mode, OpenGLRenderer& renderer, T& handler);
198
199 template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700200 inline void issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler);
John Reck113e0822014-03-18 09:22:59 -0700201
Chris Craikb265e2c2014-03-27 15:50:09 -0700202 /**
203 * Issue the RenderNode's operations into a handler, recursing for subtrees through
204 * DrawDisplayListOp's defer() or replay() methods
205 */
John Reck113e0822014-03-18 09:22:59 -0700206 template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700207 inline void issueOperations(OpenGLRenderer& renderer, T& handler);
John Reck113e0822014-03-18 09:22:59 -0700208
209 class TextContainer {
210 public:
211 size_t length() const {
212 return mByteLength;
213 }
214
215 const char* text() const {
216 return (const char*) mText;
217 }
218
219 size_t mByteLength;
220 const char* mText;
221 };
222
John Reckf4198b72014-04-09 17:00:04 -0700223 void prepareTreeImpl(TreeInfo& info);
224 void pushStagingChanges(TreeInfo& info);
John Recke45b1fd2014-04-15 09:50:16 -0700225 void evaluateAnimations(TreeInfo& info);
John Reckf4198b72014-04-09 17:00:04 -0700226 void prepareSubTree(TreeInfo& info, DisplayListData* subtree);
John Reck8de65a82014-04-09 15:23:38 -0700227
John Reck113e0822014-03-18 09:22:59 -0700228 String8 mName;
John Reck113e0822014-03-18 09:22:59 -0700229
John Reckd0a0b2a2014-03-20 16:28:56 -0700230 bool mNeedsPropertiesSync;
John Reck113e0822014-03-18 09:22:59 -0700231 RenderProperties mProperties;
John Reckd0a0b2a2014-03-20 16:28:56 -0700232 RenderProperties mStagingProperties;
233
John Reck8de65a82014-04-09 15:23:38 -0700234 bool mNeedsDisplayListDataSync;
John Reck113e0822014-03-18 09:22:59 -0700235 DisplayListData* mDisplayListData;
John Reck8de65a82014-04-09 15:23:38 -0700236 DisplayListData* mStagingDisplayListData;
John Reck113e0822014-03-18 09:22:59 -0700237
John Recke45b1fd2014-04-15 09:50:16 -0700238 bool mNeedsAnimatorsSync;
John Reck52244ff2014-05-01 21:27:37 -0700239 std::set< sp<BaseRenderNodeAnimator> > mStagingAnimators;
240 std::vector< sp<BaseRenderNodeAnimator> > mAnimators;
John Recke45b1fd2014-04-15 09:50:16 -0700241
John Reck113e0822014-03-18 09:22:59 -0700242 /**
243 * Draw time state - these properties are only set and used during rendering
244 */
245
246 // for projection surfaces, contains a list of all children items
247 Vector<DrawDisplayListOp*> mProjectedNodes;
248}; // class RenderNode
249
250} /* namespace uirenderer */
251} /* namespace android */
252
253#endif /* RENDERNODE_H */