blob: 291d03d892ba510e18a6834f7bac3a138ea07bba [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
23#include <SkCamera.h>
24#include <SkMatrix.h>
25
26#include <private/hwui/DrawGlInfo.h>
27
28#include <utils/KeyedVector.h>
29#include <utils/LinearAllocator.h>
30#include <utils/RefBase.h>
31#include <utils/SortedVector.h>
32#include <utils/String8.h>
33#include <utils/Vector.h>
34
35#include <cutils/compiler.h>
36
37#include <androidfw/ResourceTypes.h>
38
39#include "Debug.h"
40#include "Matrix.h"
41#include "DeferredDisplayList.h"
42#include "DisplayList.h"
43#include "RenderProperties.h"
44
45class SkBitmap;
46class SkPaint;
47class SkPath;
48class SkRegion;
49
50namespace android {
51namespace uirenderer {
52
53class DeferredDisplayList;
54class DisplayListOp;
55class DisplayListRenderer;
56class OpenGLRenderer;
57class Rect;
58class Layer;
59class SkiaShader;
60
61class ClipRectOp;
62class SaveLayerOp;
63class SaveOp;
64class RestoreToCountOp;
65class DrawDisplayListOp;
66
67/**
68 * Primary class for storing recorded canvas commands, as well as per-View/ViewGroup display properties.
69 *
70 * Recording of canvas commands is somewhat similar to SkPicture, except the canvas-recording
71 * functionality is split between DisplayListRenderer (which manages the recording), DisplayListData
72 * (which holds the actual data), and DisplayList (which holds properties and performs playback onto
73 * a renderer).
74 *
75 * Note that DisplayListData is swapped out from beneath an individual DisplayList when a view's
76 * recorded stream of canvas operations is refreshed. The DisplayList (and its properties) stay
77 * attached.
78 */
79class RenderNode {
80public:
81 ANDROID_API RenderNode();
82 ANDROID_API ~RenderNode();
83
84 // See flags defined in DisplayList.java
85 enum ReplayFlag {
86 kReplayFlag_ClipChildren = 0x1
87 };
88
89 ANDROID_API static void destroyDisplayListDeferred(RenderNode* displayList);
90 ANDROID_API static void outputLogBuffer(int fd);
91
92 ANDROID_API void setData(DisplayListData* newData);
93
94 void computeOrdering();
95 void defer(DeferStateStruct& deferStruct, const int level);
96 void replay(ReplayStateStruct& replayStruct, const int level);
97
98 ANDROID_API void output(uint32_t level = 1);
99
100 bool isRenderable() const {
101 return mDisplayListData && mDisplayListData->hasDrawOps;
102 }
103
104 void setName(const char* name) {
105 if (name) {
106 char* lastPeriod = strrchr(name, '.');
107 if (lastPeriod) {
108 mName.setTo(lastPeriod + 1);
109 } else {
110 mName.setTo(name);
111 }
112 }
113 }
114
John Reckd0a0b2a2014-03-20 16:28:56 -0700115 const RenderProperties& properties() {
John Reck113e0822014-03-18 09:22:59 -0700116 return mProperties;
117 }
118
John Reckd0a0b2a2014-03-20 16:28:56 -0700119 const RenderProperties& stagingProperties() {
120 return mStagingProperties;
121 }
122
123 RenderProperties& mutateStagingProperties() {
124 mNeedsPropertiesSync = true;
125 return mStagingProperties;
126 }
127
John Reck113e0822014-03-18 09:22:59 -0700128 bool isProjectionReceiver() {
129 return properties().isProjectionReceiver();
130 }
131
132 int getWidth() {
133 return properties().getWidth();
134 }
135
136 int getHeight() {
137 return properties().getHeight();
138 }
139
John Reckbfb07a02014-03-24 21:00:18 -0700140 ANDROID_API void updateProperties();
John Reckd0a0b2a2014-03-20 16:28:56 -0700141
John Reck668f0e32014-03-26 15:10:40 -0700142 // Returns true if this RenderNode or any of its children have functors
143 bool hasFunctors();
144
John Reck113e0822014-03-18 09:22:59 -0700145private:
146 typedef key_value_pair_t<float, DrawDisplayListOp*> ZDrawDisplayListOpPair;
147
148 static size_t findNonNegativeIndex(const Vector<ZDrawDisplayListOpPair>& nodes) {
149 for (size_t i = 0; i < nodes.size(); i++) {
150 if (nodes[i].key >= 0.0f) return i;
151 }
152 return nodes.size();
153 }
154
155 enum ChildrenSelectMode {
156 kNegativeZChildren,
157 kPositiveZChildren
158 };
159
John Reck113e0822014-03-18 09:22:59 -0700160 void applyViewPropertyTransforms(mat4& matrix, bool true3dTransform = false);
161
162 void computeOrderingImpl(DrawDisplayListOp* opState,
163 Vector<DrawDisplayListOp*>* compositedChildrenOfProjectionSurface,
164 const mat4* transformFromProjectionSurface);
165
166 template <class T>
167 inline void setViewProperties(OpenGLRenderer& renderer, T& handler, const int level);
168
169 void buildZSortedChildList(Vector<ZDrawDisplayListOpPair>& zTranslatedNodes);
170
171 template <class T>
172 inline void iterate3dChildren(const Vector<ZDrawDisplayListOpPair>& zTranslatedNodes,
173 ChildrenSelectMode mode, OpenGLRenderer& renderer, T& handler);
174
175 template <class T>
176 inline void iterateProjectedChildren(OpenGLRenderer& renderer, T& handler, const int level);
177
178 template <class T>
179 inline void iterate(OpenGLRenderer& renderer, T& handler, const int level);
180
181 class TextContainer {
182 public:
183 size_t length() const {
184 return mByteLength;
185 }
186
187 const char* text() const {
188 return (const char*) mText;
189 }
190
191 size_t mByteLength;
192 const char* mText;
193 };
194
195 String8 mName;
196 bool mDestroyed; // used for debugging crash, TODO: remove once invalid state crash fixed
197
John Reckd0a0b2a2014-03-20 16:28:56 -0700198 bool mNeedsPropertiesSync;
John Reck113e0822014-03-18 09:22:59 -0700199 RenderProperties mProperties;
John Reckd0a0b2a2014-03-20 16:28:56 -0700200 RenderProperties mStagingProperties;
201
John Reck113e0822014-03-18 09:22:59 -0700202 DisplayListData* mDisplayListData;
203
204 /**
205 * Draw time state - these properties are only set and used during rendering
206 */
207
208 // for projection surfaces, contains a list of all children items
209 Vector<DrawDisplayListOp*> mProjectedNodes;
210}; // class RenderNode
211
212} /* namespace uirenderer */
213} /* namespace android */
214
215#endif /* RENDERNODE_H */