blob: b2fe849318772d00d3b0774674a9bd6e098d156d [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
John Recka447d292014-06-11 18:39:44 -070042#include "DamageAccumulator.h"
John Reck113e0822014-03-18 09:22:59 -070043#include "Debug.h"
44#include "Matrix.h"
45#include "DeferredDisplayList.h"
46#include "DisplayList.h"
47#include "RenderProperties.h"
John Recke45b1fd2014-04-15 09:50:16 -070048#include "TreeInfo.h"
John Reck113e0822014-03-18 09:22:59 -070049
50class SkBitmap;
51class SkPaint;
52class SkPath;
53class SkRegion;
54
55namespace android {
56namespace uirenderer {
57
58class DeferredDisplayList;
59class DisplayListOp;
60class DisplayListRenderer;
61class OpenGLRenderer;
62class Rect;
63class Layer;
64class SkiaShader;
65
66class ClipRectOp;
67class SaveLayerOp;
68class SaveOp;
69class RestoreToCountOp;
70class DrawDisplayListOp;
71
72/**
73 * Primary class for storing recorded canvas commands, as well as per-View/ViewGroup display properties.
74 *
75 * Recording of canvas commands is somewhat similar to SkPicture, except the canvas-recording
76 * functionality is split between DisplayListRenderer (which manages the recording), DisplayListData
77 * (which holds the actual data), and DisplayList (which holds properties and performs playback onto
78 * a renderer).
79 *
80 * Note that DisplayListData is swapped out from beneath an individual DisplayList when a view's
81 * recorded stream of canvas operations is refreshed. The DisplayList (and its properties) stay
82 * attached.
83 */
John Reck087bc0c2014-04-04 16:20:08 -070084class RenderNode : public VirtualLightRefBase {
John Reck113e0822014-03-18 09:22:59 -070085public:
John Reckff941dc2014-05-14 16:34:14 -070086 enum DirtyPropertyMask {
87 GENERIC = 1 << 1,
88 TRANSLATION_X = 1 << 2,
89 TRANSLATION_Y = 1 << 3,
90 TRANSLATION_Z = 1 << 4,
91 SCALE_X = 1 << 5,
92 SCALE_Y = 1 << 6,
93 ROTATION = 1 << 7,
94 ROTATION_X = 1 << 8,
95 ROTATION_Y = 1 << 9,
96 X = 1 << 10,
97 Y = 1 << 11,
98 Z = 1 << 12,
99 ALPHA = 1 << 13,
100 };
101
John Reck113e0822014-03-18 09:22:59 -0700102 ANDROID_API RenderNode();
John Recke45b1fd2014-04-15 09:50:16 -0700103 ANDROID_API virtual ~RenderNode();
John Reck113e0822014-03-18 09:22:59 -0700104
105 // See flags defined in DisplayList.java
106 enum ReplayFlag {
107 kReplayFlag_ClipChildren = 0x1
108 };
109
John Reck113e0822014-03-18 09:22:59 -0700110 ANDROID_API static void outputLogBuffer(int fd);
111
John Reck8de65a82014-04-09 15:23:38 -0700112 ANDROID_API void setStagingDisplayList(DisplayListData* newData);
John Reck113e0822014-03-18 09:22:59 -0700113
114 void computeOrdering();
Chris Craikb265e2c2014-03-27 15:50:09 -0700115
116 void deferNodeTree(DeferStateStruct& deferStruct);
117 void deferNodeInParent(DeferStateStruct& deferStruct, const int level);
118
119 void replayNodeTree(ReplayStateStruct& replayStruct);
120 void replayNodeInParent(ReplayStateStruct& replayStruct, const int level);
John Reck113e0822014-03-18 09:22:59 -0700121
122 ANDROID_API void output(uint32_t level = 1);
John Reckfe5e7b72014-05-23 17:42:28 -0700123 ANDROID_API int getDebugSize();
John Reck113e0822014-03-18 09:22:59 -0700124
125 bool isRenderable() const {
126 return mDisplayListData && mDisplayListData->hasDrawOps;
127 }
128
John Recka447d292014-06-11 18:39:44 -0700129 bool hasProjectionReceiver() const {
130 return mDisplayListData && mDisplayListData->projectionReceiveIndex >= 0;
131 }
132
Chris Craikdefb7f32014-04-08 18:17:07 -0700133 const char* getName() const {
134 return mName.string();
135 }
136
John Reck113e0822014-03-18 09:22:59 -0700137 void setName(const char* name) {
138 if (name) {
139 char* lastPeriod = strrchr(name, '.');
140 if (lastPeriod) {
141 mName.setTo(lastPeriod + 1);
142 } else {
143 mName.setTo(name);
144 }
145 }
146 }
147
John Reckff941dc2014-05-14 16:34:14 -0700148 bool isPropertyFieldDirty(DirtyPropertyMask field) const {
149 return mDirtyPropertyFields & field;
150 }
151
152 void setPropertyFieldsDirty(uint32_t fields) {
153 mDirtyPropertyFields |= fields;
154 }
155
John Recke4267ea2014-06-03 15:53:15 -0700156 const RenderProperties& properties() const {
John Reck113e0822014-03-18 09:22:59 -0700157 return mProperties;
158 }
159
John Reck52244ff2014-05-01 21:27:37 -0700160 RenderProperties& animatorProperties() {
161 return mProperties;
162 }
163
John Reckd0a0b2a2014-03-20 16:28:56 -0700164 const RenderProperties& stagingProperties() {
165 return mStagingProperties;
166 }
167
168 RenderProperties& mutateStagingProperties() {
John Reckd0a0b2a2014-03-20 16:28:56 -0700169 return mStagingProperties;
170 }
171
John Reck113e0822014-03-18 09:22:59 -0700172 int getWidth() {
173 return properties().getWidth();
174 }
175
176 int getHeight() {
177 return properties().getHeight();
178 }
179
John Recke45b1fd2014-04-15 09:50:16 -0700180 ANDROID_API virtual void prepareTree(TreeInfo& info);
181
182 // UI thread only!
John Reck52244ff2014-05-01 21:27:37 -0700183 ANDROID_API void addAnimator(const sp<BaseRenderNodeAnimator>& animator) {
John Reckff941dc2014-05-14 16:34:14 -0700184 animator->onAttached(this);
John Recke45b1fd2014-04-15 09:50:16 -0700185 mStagingAnimators.insert(animator);
186 mNeedsAnimatorsSync = true;
187 }
188
189 // UI thread only!
John Reck52244ff2014-05-01 21:27:37 -0700190 ANDROID_API void removeAnimator(const sp<BaseRenderNodeAnimator>& animator) {
John Recke45b1fd2014-04-15 09:50:16 -0700191 mStagingAnimators.erase(animator);
192 mNeedsAnimatorsSync = true;
193 }
John Reck668f0e32014-03-26 15:10:40 -0700194
John Recke4267ea2014-06-03 15:53:15 -0700195protected:
196 virtual void damageSelf(TreeInfo& info);
197
John Reck113e0822014-03-18 09:22:59 -0700198private:
199 typedef key_value_pair_t<float, DrawDisplayListOp*> ZDrawDisplayListOpPair;
200
201 static size_t findNonNegativeIndex(const Vector<ZDrawDisplayListOpPair>& nodes) {
202 for (size_t i = 0; i < nodes.size(); i++) {
203 if (nodes[i].key >= 0.0f) return i;
204 }
205 return nodes.size();
206 }
207
208 enum ChildrenSelectMode {
209 kNegativeZChildren,
210 kPositiveZChildren
211 };
212
John Reck113e0822014-03-18 09:22:59 -0700213 void applyViewPropertyTransforms(mat4& matrix, bool true3dTransform = false);
214
215 void computeOrderingImpl(DrawDisplayListOp* opState,
Chris Craik3f0854292014-04-15 16:18:08 -0700216 const SkPath* outlineOfProjectionSurface,
John Reck113e0822014-03-18 09:22:59 -0700217 Vector<DrawDisplayListOp*>* compositedChildrenOfProjectionSurface,
218 const mat4* transformFromProjectionSurface);
219
220 template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700221 inline void setViewProperties(OpenGLRenderer& renderer, T& handler);
John Reck113e0822014-03-18 09:22:59 -0700222
223 void buildZSortedChildList(Vector<ZDrawDisplayListOpPair>& zTranslatedNodes);
224
Chris Craikb265e2c2014-03-27 15:50:09 -0700225 template<class T>
226 inline void issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler);
227
John Reck113e0822014-03-18 09:22:59 -0700228 template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700229 inline void issueOperationsOf3dChildren(const Vector<ZDrawDisplayListOpPair>& zTranslatedNodes,
John Reck113e0822014-03-18 09:22:59 -0700230 ChildrenSelectMode mode, OpenGLRenderer& renderer, T& handler);
231
232 template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700233 inline void issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler);
John Reck113e0822014-03-18 09:22:59 -0700234
Chris Craikb265e2c2014-03-27 15:50:09 -0700235 /**
236 * Issue the RenderNode's operations into a handler, recursing for subtrees through
237 * DrawDisplayListOp's defer() or replay() methods
238 */
John Reck113e0822014-03-18 09:22:59 -0700239 template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700240 inline void issueOperations(OpenGLRenderer& renderer, T& handler);
John Reck113e0822014-03-18 09:22:59 -0700241
242 class TextContainer {
243 public:
244 size_t length() const {
245 return mByteLength;
246 }
247
248 const char* text() const {
249 return (const char*) mText;
250 }
251
252 size_t mByteLength;
253 const char* mText;
254 };
255
John Reckf4198b72014-04-09 17:00:04 -0700256 void prepareTreeImpl(TreeInfo& info);
John Reck25fbb3f2014-06-12 13:46:45 -0700257 void pushStagingPropertiesChanges(TreeInfo& info);
258 void pushStagingDisplayListChanges(TreeInfo& info);
John Recke45b1fd2014-04-15 09:50:16 -0700259 void evaluateAnimations(TreeInfo& info);
John Reckf4198b72014-04-09 17:00:04 -0700260 void prepareSubTree(TreeInfo& info, DisplayListData* subtree);
John Reck25fbb3f2014-06-12 13:46:45 -0700261 void applyLayerPropertiesToLayer(TreeInfo& info);
262 void prepareLayer(TreeInfo& info);
263 void pushLayerUpdate(TreeInfo& info);
John Reck8de65a82014-04-09 15:23:38 -0700264
John Reck113e0822014-03-18 09:22:59 -0700265 String8 mName;
John Reck113e0822014-03-18 09:22:59 -0700266
John Reckff941dc2014-05-14 16:34:14 -0700267 uint32_t mDirtyPropertyFields;
John Reck113e0822014-03-18 09:22:59 -0700268 RenderProperties mProperties;
John Reckd0a0b2a2014-03-20 16:28:56 -0700269 RenderProperties mStagingProperties;
270
John Reck8de65a82014-04-09 15:23:38 -0700271 bool mNeedsDisplayListDataSync;
John Reck113e0822014-03-18 09:22:59 -0700272 DisplayListData* mDisplayListData;
John Reck8de65a82014-04-09 15:23:38 -0700273 DisplayListData* mStagingDisplayListData;
John Reck113e0822014-03-18 09:22:59 -0700274
John Recke45b1fd2014-04-15 09:50:16 -0700275 bool mNeedsAnimatorsSync;
John Reck52244ff2014-05-01 21:27:37 -0700276 std::set< sp<BaseRenderNodeAnimator> > mStagingAnimators;
277 std::vector< sp<BaseRenderNodeAnimator> > mAnimators;
John Recke45b1fd2014-04-15 09:50:16 -0700278
John Reck25fbb3f2014-06-12 13:46:45 -0700279 // Owned by RT. Lifecycle is managed by prepareTree(), with the exception
280 // being in ~RenderNode() which may happen on any thread.
281 Layer* mLayer;
282
John Reck113e0822014-03-18 09:22:59 -0700283 /**
284 * Draw time state - these properties are only set and used during rendering
285 */
286
287 // for projection surfaces, contains a list of all children items
288 Vector<DrawDisplayListOp*> mProjectedNodes;
289}; // class RenderNode
290
291} /* namespace uirenderer */
292} /* namespace android */
293
294#endif /* RENDERNODE_H */