John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 1 | /* |
| 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 Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 23 | #include <set> |
| 24 | #include <vector> |
| 25 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 26 | #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 Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 42 | #include "DamageAccumulator.h" |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 43 | #include "Debug.h" |
| 44 | #include "Matrix.h" |
| 45 | #include "DeferredDisplayList.h" |
| 46 | #include "DisplayList.h" |
| 47 | #include "RenderProperties.h" |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 48 | #include "TreeInfo.h" |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 49 | |
| 50 | class SkBitmap; |
| 51 | class SkPaint; |
| 52 | class SkPath; |
| 53 | class SkRegion; |
| 54 | |
| 55 | namespace android { |
| 56 | namespace uirenderer { |
| 57 | |
| 58 | class DeferredDisplayList; |
| 59 | class DisplayListOp; |
| 60 | class DisplayListRenderer; |
| 61 | class OpenGLRenderer; |
| 62 | class Rect; |
| 63 | class Layer; |
| 64 | class SkiaShader; |
| 65 | |
| 66 | class ClipRectOp; |
| 67 | class SaveLayerOp; |
| 68 | class SaveOp; |
| 69 | class RestoreToCountOp; |
Chris Craik | a7090e0 | 2014-06-20 16:01:00 -0700 | [diff] [blame^] | 70 | class DrawRenderNodeOp; |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 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 Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame] | 84 | class RenderNode : public VirtualLightRefBase { |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 85 | public: |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 86 | 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 Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 102 | ANDROID_API RenderNode(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 103 | ANDROID_API virtual ~RenderNode(); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 104 | |
| 105 | // See flags defined in DisplayList.java |
| 106 | enum ReplayFlag { |
| 107 | kReplayFlag_ClipChildren = 0x1 |
| 108 | }; |
| 109 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 110 | ANDROID_API static void outputLogBuffer(int fd); |
| 111 | |
John Reck | 8de65a8 | 2014-04-09 15:23:38 -0700 | [diff] [blame] | 112 | ANDROID_API void setStagingDisplayList(DisplayListData* newData); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 113 | |
| 114 | void computeOrdering(); |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 115 | |
Chris Craik | 80d4902 | 2014-06-20 15:03:43 -0700 | [diff] [blame] | 116 | void defer(DeferStateStruct& deferStruct, const int level); |
| 117 | void replay(ReplayStateStruct& replayStruct, const int level); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 118 | |
| 119 | ANDROID_API void output(uint32_t level = 1); |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 120 | ANDROID_API int getDebugSize(); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 121 | |
| 122 | bool isRenderable() const { |
| 123 | return mDisplayListData && mDisplayListData->hasDrawOps; |
| 124 | } |
| 125 | |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 126 | bool hasProjectionReceiver() const { |
| 127 | return mDisplayListData && mDisplayListData->projectionReceiveIndex >= 0; |
| 128 | } |
| 129 | |
Chris Craik | defb7f3 | 2014-04-08 18:17:07 -0700 | [diff] [blame] | 130 | const char* getName() const { |
| 131 | return mName.string(); |
| 132 | } |
| 133 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 134 | void setName(const char* name) { |
| 135 | if (name) { |
| 136 | char* lastPeriod = strrchr(name, '.'); |
| 137 | if (lastPeriod) { |
| 138 | mName.setTo(lastPeriod + 1); |
| 139 | } else { |
| 140 | mName.setTo(name); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 145 | bool isPropertyFieldDirty(DirtyPropertyMask field) const { |
| 146 | return mDirtyPropertyFields & field; |
| 147 | } |
| 148 | |
| 149 | void setPropertyFieldsDirty(uint32_t fields) { |
| 150 | mDirtyPropertyFields |= fields; |
| 151 | } |
| 152 | |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 153 | const RenderProperties& properties() const { |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 154 | return mProperties; |
| 155 | } |
| 156 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 157 | RenderProperties& animatorProperties() { |
| 158 | return mProperties; |
| 159 | } |
| 160 | |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 161 | const RenderProperties& stagingProperties() { |
| 162 | return mStagingProperties; |
| 163 | } |
| 164 | |
| 165 | RenderProperties& mutateStagingProperties() { |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 166 | return mStagingProperties; |
| 167 | } |
| 168 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 169 | int getWidth() { |
| 170 | return properties().getWidth(); |
| 171 | } |
| 172 | |
| 173 | int getHeight() { |
| 174 | return properties().getHeight(); |
| 175 | } |
| 176 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 177 | ANDROID_API virtual void prepareTree(TreeInfo& info); |
| 178 | |
| 179 | // UI thread only! |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 180 | ANDROID_API void addAnimator(const sp<BaseRenderNodeAnimator>& animator) { |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 181 | animator->onAttached(this); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 182 | mStagingAnimators.insert(animator); |
| 183 | mNeedsAnimatorsSync = true; |
| 184 | } |
| 185 | |
| 186 | // UI thread only! |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 187 | ANDROID_API void removeAnimator(const sp<BaseRenderNodeAnimator>& animator) { |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 188 | mStagingAnimators.erase(animator); |
John Reck | 2218472 | 2014-06-20 07:19:30 -0700 | [diff] [blame] | 189 | // Force a sync of the staging property value |
| 190 | mDirtyPropertyFields |= animator->dirtyMask(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 191 | mNeedsAnimatorsSync = true; |
| 192 | } |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 193 | |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 194 | protected: |
| 195 | virtual void damageSelf(TreeInfo& info); |
| 196 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 197 | private: |
Chris Craik | a7090e0 | 2014-06-20 16:01:00 -0700 | [diff] [blame^] | 198 | typedef key_value_pair_t<float, DrawRenderNodeOp*> ZDrawRenderNodeOpPair; |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 199 | |
Chris Craik | a7090e0 | 2014-06-20 16:01:00 -0700 | [diff] [blame^] | 200 | static size_t findNonNegativeIndex(const Vector<ZDrawRenderNodeOpPair>& nodes) { |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 201 | for (size_t i = 0; i < nodes.size(); i++) { |
| 202 | if (nodes[i].key >= 0.0f) return i; |
| 203 | } |
| 204 | return nodes.size(); |
| 205 | } |
| 206 | |
| 207 | enum ChildrenSelectMode { |
| 208 | kNegativeZChildren, |
| 209 | kPositiveZChildren |
| 210 | }; |
| 211 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 212 | void applyViewPropertyTransforms(mat4& matrix, bool true3dTransform = false); |
| 213 | |
Chris Craik | a7090e0 | 2014-06-20 16:01:00 -0700 | [diff] [blame^] | 214 | void computeOrderingImpl(DrawRenderNodeOp* opState, |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 215 | const SkPath* outlineOfProjectionSurface, |
Chris Craik | a7090e0 | 2014-06-20 16:01:00 -0700 | [diff] [blame^] | 216 | Vector<DrawRenderNodeOp*>* compositedChildrenOfProjectionSurface, |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 217 | const mat4* transformFromProjectionSurface); |
| 218 | |
| 219 | template <class T> |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 220 | inline void setViewProperties(OpenGLRenderer& renderer, T& handler); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 221 | |
Chris Craik | a7090e0 | 2014-06-20 16:01:00 -0700 | [diff] [blame^] | 222 | void buildZSortedChildList(Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 223 | |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 224 | template<class T> |
| 225 | inline void issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler); |
| 226 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 227 | template <class T> |
Chris Craik | 80d4902 | 2014-06-20 15:03:43 -0700 | [diff] [blame] | 228 | inline int issueOperationsOfNegZChildren( |
Chris Craik | a7090e0 | 2014-06-20 16:01:00 -0700 | [diff] [blame^] | 229 | const Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes, |
Chris Craik | 80d4902 | 2014-06-20 15:03:43 -0700 | [diff] [blame] | 230 | OpenGLRenderer& renderer, T& handler); |
| 231 | template <class T> |
| 232 | inline void issueOperationsOfPosZChildren(int shadowRestoreTo, |
Chris Craik | a7090e0 | 2014-06-20 16:01:00 -0700 | [diff] [blame^] | 233 | const Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes, |
Chris Craik | 80d4902 | 2014-06-20 15:03:43 -0700 | [diff] [blame] | 234 | OpenGLRenderer& renderer, T& handler); |
| 235 | template <class T> |
Chris Craik | a7090e0 | 2014-06-20 16:01:00 -0700 | [diff] [blame^] | 236 | inline void issueOperationsOf3dChildren(const Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes, |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 237 | ChildrenSelectMode mode, OpenGLRenderer& renderer, T& handler); |
| 238 | |
| 239 | template <class T> |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 240 | inline void issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 241 | |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 242 | /** |
| 243 | * Issue the RenderNode's operations into a handler, recursing for subtrees through |
Chris Craik | a7090e0 | 2014-06-20 16:01:00 -0700 | [diff] [blame^] | 244 | * DrawRenderNodeOp's defer() or replay() methods |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 245 | */ |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 246 | template <class T> |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 247 | inline void issueOperations(OpenGLRenderer& renderer, T& handler); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 248 | |
| 249 | class TextContainer { |
| 250 | public: |
| 251 | size_t length() const { |
| 252 | return mByteLength; |
| 253 | } |
| 254 | |
| 255 | const char* text() const { |
| 256 | return (const char*) mText; |
| 257 | } |
| 258 | |
| 259 | size_t mByteLength; |
| 260 | const char* mText; |
| 261 | }; |
| 262 | |
John Reck | f4198b7 | 2014-04-09 17:00:04 -0700 | [diff] [blame] | 263 | void prepareTreeImpl(TreeInfo& info); |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 264 | void pushStagingPropertiesChanges(TreeInfo& info); |
| 265 | void pushStagingDisplayListChanges(TreeInfo& info); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 266 | void evaluateAnimations(TreeInfo& info); |
John Reck | f4198b7 | 2014-04-09 17:00:04 -0700 | [diff] [blame] | 267 | void prepareSubTree(TreeInfo& info, DisplayListData* subtree); |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 268 | void applyLayerPropertiesToLayer(TreeInfo& info); |
| 269 | void prepareLayer(TreeInfo& info); |
| 270 | void pushLayerUpdate(TreeInfo& info); |
John Reck | 8de65a8 | 2014-04-09 15:23:38 -0700 | [diff] [blame] | 271 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 272 | String8 mName; |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 273 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 274 | uint32_t mDirtyPropertyFields; |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 275 | RenderProperties mProperties; |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 276 | RenderProperties mStagingProperties; |
| 277 | |
John Reck | 8de65a8 | 2014-04-09 15:23:38 -0700 | [diff] [blame] | 278 | bool mNeedsDisplayListDataSync; |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 279 | DisplayListData* mDisplayListData; |
John Reck | 8de65a8 | 2014-04-09 15:23:38 -0700 | [diff] [blame] | 280 | DisplayListData* mStagingDisplayListData; |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 281 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 282 | bool mNeedsAnimatorsSync; |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 283 | std::set< sp<BaseRenderNodeAnimator> > mStagingAnimators; |
| 284 | std::vector< sp<BaseRenderNodeAnimator> > mAnimators; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 285 | |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 286 | // Owned by RT. Lifecycle is managed by prepareTree(), with the exception |
| 287 | // being in ~RenderNode() which may happen on any thread. |
| 288 | Layer* mLayer; |
| 289 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 290 | /** |
| 291 | * Draw time state - these properties are only set and used during rendering |
| 292 | */ |
| 293 | |
| 294 | // for projection surfaces, contains a list of all children items |
Chris Craik | a7090e0 | 2014-06-20 16:01:00 -0700 | [diff] [blame^] | 295 | Vector<DrawRenderNodeOp*> mProjectedNodes; |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 296 | }; // class RenderNode |
| 297 | |
| 298 | } /* namespace uirenderer */ |
| 299 | } /* namespace android */ |
| 300 | |
| 301 | #endif /* RENDERNODE_H */ |