blob: 3980dad29044f7e0157cefcd1109bcfe646ed170 [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
John Reck113e0822014-03-18 09:22:59 -070058class DisplayListOp;
59class DisplayListRenderer;
60class OpenGLRenderer;
61class Rect;
62class Layer;
63class SkiaShader;
64
65class ClipRectOp;
66class SaveLayerOp;
67class SaveOp;
68class RestoreToCountOp;
Chris Craika7090e02014-06-20 16:01:00 -070069class DrawRenderNodeOp;
John Reck113e0822014-03-18 09:22:59 -070070
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:
John Reckff941dc2014-05-14 16:34:14 -070085 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
John Reck113e0822014-03-18 09:22:59 -0700101 ANDROID_API RenderNode();
John Recke45b1fd2014-04-15 09:50:16 -0700102 ANDROID_API virtual ~RenderNode();
John Reck113e0822014-03-18 09:22:59 -0700103
104 // See flags defined in DisplayList.java
105 enum ReplayFlag {
106 kReplayFlag_ClipChildren = 0x1
107 };
108
John Reck113e0822014-03-18 09:22:59 -0700109 ANDROID_API static void outputLogBuffer(int fd);
110
John Reck8de65a82014-04-09 15:23:38 -0700111 ANDROID_API void setStagingDisplayList(DisplayListData* newData);
John Reck113e0822014-03-18 09:22:59 -0700112
113 void computeOrdering();
Chris Craikb265e2c2014-03-27 15:50:09 -0700114
Chris Craik80d49022014-06-20 15:03:43 -0700115 void defer(DeferStateStruct& deferStruct, const int level);
116 void replay(ReplayStateStruct& replayStruct, const int level);
John Reck113e0822014-03-18 09:22:59 -0700117
118 ANDROID_API void output(uint32_t level = 1);
John Reckfe5e7b72014-05-23 17:42:28 -0700119 ANDROID_API int getDebugSize();
John Reck113e0822014-03-18 09:22:59 -0700120
121 bool isRenderable() const {
122 return mDisplayListData && mDisplayListData->hasDrawOps;
123 }
124
John Recka447d292014-06-11 18:39:44 -0700125 bool hasProjectionReceiver() const {
126 return mDisplayListData && mDisplayListData->projectionReceiveIndex >= 0;
127 }
128
Chris Craikdefb7f32014-04-08 18:17:07 -0700129 const char* getName() const {
130 return mName.string();
131 }
132
John Reck113e0822014-03-18 09:22:59 -0700133 void setName(const char* name) {
134 if (name) {
135 char* lastPeriod = strrchr(name, '.');
136 if (lastPeriod) {
137 mName.setTo(lastPeriod + 1);
138 } else {
139 mName.setTo(name);
140 }
141 }
142 }
143
John Reckff941dc2014-05-14 16:34:14 -0700144 bool isPropertyFieldDirty(DirtyPropertyMask field) const {
145 return mDirtyPropertyFields & field;
146 }
147
148 void setPropertyFieldsDirty(uint32_t fields) {
149 mDirtyPropertyFields |= fields;
150 }
151
John Recke4267ea2014-06-03 15:53:15 -0700152 const RenderProperties& properties() const {
John Reck113e0822014-03-18 09:22:59 -0700153 return mProperties;
154 }
155
John Reck52244ff2014-05-01 21:27:37 -0700156 RenderProperties& animatorProperties() {
157 return mProperties;
158 }
159
John Reckd0a0b2a2014-03-20 16:28:56 -0700160 const RenderProperties& stagingProperties() {
161 return mStagingProperties;
162 }
163
164 RenderProperties& mutateStagingProperties() {
John Reckd0a0b2a2014-03-20 16:28:56 -0700165 return mStagingProperties;
166 }
167
John Reck113e0822014-03-18 09:22:59 -0700168 int getWidth() {
169 return properties().getWidth();
170 }
171
172 int getHeight() {
173 return properties().getHeight();
174 }
175
John Recke45b1fd2014-04-15 09:50:16 -0700176 ANDROID_API virtual void prepareTree(TreeInfo& info);
177
178 // UI thread only!
John Reck52244ff2014-05-01 21:27:37 -0700179 ANDROID_API void addAnimator(const sp<BaseRenderNodeAnimator>& animator) {
John Reckff941dc2014-05-14 16:34:14 -0700180 animator->onAttached(this);
John Recke45b1fd2014-04-15 09:50:16 -0700181 mStagingAnimators.insert(animator);
182 mNeedsAnimatorsSync = true;
183 }
184
185 // UI thread only!
John Reck52244ff2014-05-01 21:27:37 -0700186 ANDROID_API void removeAnimator(const sp<BaseRenderNodeAnimator>& animator) {
John Recke45b1fd2014-04-15 09:50:16 -0700187 mStagingAnimators.erase(animator);
John Reck22184722014-06-20 07:19:30 -0700188 // Force a sync of the staging property value
189 mDirtyPropertyFields |= animator->dirtyMask();
John Recke45b1fd2014-04-15 09:50:16 -0700190 mNeedsAnimatorsSync = true;
191 }
John Reck668f0e32014-03-26 15:10:40 -0700192
John Recke4267ea2014-06-03 15:53:15 -0700193protected:
194 virtual void damageSelf(TreeInfo& info);
195
John Reck113e0822014-03-18 09:22:59 -0700196private:
Chris Craika7090e02014-06-20 16:01:00 -0700197 typedef key_value_pair_t<float, DrawRenderNodeOp*> ZDrawRenderNodeOpPair;
John Reck113e0822014-03-18 09:22:59 -0700198
Chris Craika7090e02014-06-20 16:01:00 -0700199 static size_t findNonNegativeIndex(const Vector<ZDrawRenderNodeOpPair>& nodes) {
John Reck113e0822014-03-18 09:22:59 -0700200 for (size_t i = 0; i < nodes.size(); i++) {
201 if (nodes[i].key >= 0.0f) return i;
202 }
203 return nodes.size();
204 }
205
206 enum ChildrenSelectMode {
207 kNegativeZChildren,
208 kPositiveZChildren
209 };
210
John Reck113e0822014-03-18 09:22:59 -0700211 void applyViewPropertyTransforms(mat4& matrix, bool true3dTransform = false);
212
Chris Craika7090e02014-06-20 16:01:00 -0700213 void computeOrderingImpl(DrawRenderNodeOp* opState,
Chris Craik3f0854292014-04-15 16:18:08 -0700214 const SkPath* outlineOfProjectionSurface,
Chris Craika7090e02014-06-20 16:01:00 -0700215 Vector<DrawRenderNodeOp*>* compositedChildrenOfProjectionSurface,
John Reck113e0822014-03-18 09:22:59 -0700216 const mat4* transformFromProjectionSurface);
217
218 template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700219 inline void setViewProperties(OpenGLRenderer& renderer, T& handler);
John Reck113e0822014-03-18 09:22:59 -0700220
Chris Craika7090e02014-06-20 16:01:00 -0700221 void buildZSortedChildList(Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes);
John Reck113e0822014-03-18 09:22:59 -0700222
Chris Craikb265e2c2014-03-27 15:50:09 -0700223 template<class T>
224 inline void issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler);
225
John Reck113e0822014-03-18 09:22:59 -0700226 template <class T>
Chris Craik80d49022014-06-20 15:03:43 -0700227 inline int issueOperationsOfNegZChildren(
Chris Craika7090e02014-06-20 16:01:00 -0700228 const Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
Chris Craik80d49022014-06-20 15:03:43 -0700229 OpenGLRenderer& renderer, T& handler);
230 template <class T>
231 inline void issueOperationsOfPosZChildren(int shadowRestoreTo,
Chris Craika7090e02014-06-20 16:01:00 -0700232 const Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
Chris Craik80d49022014-06-20 15:03:43 -0700233 OpenGLRenderer& renderer, T& handler);
234 template <class T>
Chris Craika7090e02014-06-20 16:01:00 -0700235 inline void issueOperationsOf3dChildren(const Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
John Reck113e0822014-03-18 09:22:59 -0700236 ChildrenSelectMode mode, OpenGLRenderer& renderer, T& handler);
237
238 template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700239 inline void issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler);
John Reck113e0822014-03-18 09:22:59 -0700240
Chris Craikb265e2c2014-03-27 15:50:09 -0700241 /**
242 * Issue the RenderNode's operations into a handler, recursing for subtrees through
Chris Craika7090e02014-06-20 16:01:00 -0700243 * DrawRenderNodeOp's defer() or replay() methods
Chris Craikb265e2c2014-03-27 15:50:09 -0700244 */
John Reck113e0822014-03-18 09:22:59 -0700245 template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700246 inline void issueOperations(OpenGLRenderer& renderer, T& handler);
John Reck113e0822014-03-18 09:22:59 -0700247
248 class TextContainer {
249 public:
250 size_t length() const {
251 return mByteLength;
252 }
253
254 const char* text() const {
255 return (const char*) mText;
256 }
257
258 size_t mByteLength;
259 const char* mText;
260 };
261
John Reckf4198b72014-04-09 17:00:04 -0700262 void prepareTreeImpl(TreeInfo& info);
John Reck25fbb3f2014-06-12 13:46:45 -0700263 void pushStagingPropertiesChanges(TreeInfo& info);
264 void pushStagingDisplayListChanges(TreeInfo& info);
John Recke45b1fd2014-04-15 09:50:16 -0700265 void evaluateAnimations(TreeInfo& info);
John Reckf4198b72014-04-09 17:00:04 -0700266 void prepareSubTree(TreeInfo& info, DisplayListData* subtree);
John Reck25fbb3f2014-06-12 13:46:45 -0700267 void applyLayerPropertiesToLayer(TreeInfo& info);
268 void prepareLayer(TreeInfo& info);
269 void pushLayerUpdate(TreeInfo& info);
John Reck8de65a82014-04-09 15:23:38 -0700270
John Reck113e0822014-03-18 09:22:59 -0700271 String8 mName;
John Reck113e0822014-03-18 09:22:59 -0700272
John Reckff941dc2014-05-14 16:34:14 -0700273 uint32_t mDirtyPropertyFields;
John Reck113e0822014-03-18 09:22:59 -0700274 RenderProperties mProperties;
John Reckd0a0b2a2014-03-20 16:28:56 -0700275 RenderProperties mStagingProperties;
276
John Reck8de65a82014-04-09 15:23:38 -0700277 bool mNeedsDisplayListDataSync;
John Reck113e0822014-03-18 09:22:59 -0700278 DisplayListData* mDisplayListData;
John Reck8de65a82014-04-09 15:23:38 -0700279 DisplayListData* mStagingDisplayListData;
John Reck113e0822014-03-18 09:22:59 -0700280
John Recke45b1fd2014-04-15 09:50:16 -0700281 bool mNeedsAnimatorsSync;
John Reck52244ff2014-05-01 21:27:37 -0700282 std::set< sp<BaseRenderNodeAnimator> > mStagingAnimators;
283 std::vector< sp<BaseRenderNodeAnimator> > mAnimators;
John Recke45b1fd2014-04-15 09:50:16 -0700284
John Reck25fbb3f2014-06-12 13:46:45 -0700285 // Owned by RT. Lifecycle is managed by prepareTree(), with the exception
286 // being in ~RenderNode() which may happen on any thread.
287 Layer* mLayer;
288
John Reck113e0822014-03-18 09:22:59 -0700289 /**
290 * Draw time state - these properties are only set and used during rendering
291 */
292
293 // for projection surfaces, contains a list of all children items
Chris Craika7090e02014-06-20 16:01:00 -0700294 Vector<DrawRenderNodeOp*> mProjectedNodes;
John Reck113e0822014-03-18 09:22:59 -0700295}; // class RenderNode
296
297} /* namespace uirenderer */
298} /* namespace android */
299
300#endif /* RENDERNODE_H */