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 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 19 | #include <SkCamera.h> |
| 20 | #include <SkMatrix.h> |
| 21 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 22 | #include <utils/LinearAllocator.h> |
| 23 | #include <utils/RefBase.h> |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 24 | #include <utils/String8.h> |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 25 | |
| 26 | #include <cutils/compiler.h> |
| 27 | |
| 28 | #include <androidfw/ResourceTypes.h> |
| 29 | |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 30 | #include "AnimatorManager.h" |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 31 | #include "Debug.h" |
| 32 | #include "Matrix.h" |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 33 | #include "DisplayList.h" |
| 34 | #include "RenderProperties.h" |
| 35 | |
John Reck | 272a685 | 2015-07-29 16:48:58 -0700 | [diff] [blame] | 36 | #include <vector> |
| 37 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 38 | class SkBitmap; |
| 39 | class SkPaint; |
| 40 | class SkPath; |
| 41 | class SkRegion; |
| 42 | |
| 43 | namespace android { |
| 44 | namespace uirenderer { |
| 45 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 46 | class DisplayListOp; |
Chris Craik | db663fe | 2015-04-20 13:34:45 -0700 | [diff] [blame] | 47 | class DisplayListCanvas; |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 48 | class OpenGLRenderer; |
| 49 | class Rect; |
| 50 | class Layer; |
| 51 | class SkiaShader; |
| 52 | |
| 53 | class ClipRectOp; |
| 54 | class SaveLayerOp; |
| 55 | class SaveOp; |
| 56 | class RestoreToCountOp; |
Chris Craik | a7090e0 | 2014-06-20 16:01:00 -0700 | [diff] [blame] | 57 | class DrawRenderNodeOp; |
Tom Hudson | 2dc236b | 2014-10-15 15:46:42 -0400 | [diff] [blame] | 58 | class TreeInfo; |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * Primary class for storing recorded canvas commands, as well as per-View/ViewGroup display properties. |
| 62 | * |
| 63 | * Recording of canvas commands is somewhat similar to SkPicture, except the canvas-recording |
Chris Craik | db663fe | 2015-04-20 13:34:45 -0700 | [diff] [blame] | 64 | * functionality is split between DisplayListCanvas (which manages the recording), DisplayListData |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 65 | * (which holds the actual data), and DisplayList (which holds properties and performs playback onto |
| 66 | * a renderer). |
| 67 | * |
| 68 | * Note that DisplayListData is swapped out from beneath an individual DisplayList when a view's |
| 69 | * recorded stream of canvas operations is refreshed. The DisplayList (and its properties) stay |
| 70 | * attached. |
| 71 | */ |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame] | 72 | class RenderNode : public VirtualLightRefBase { |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 73 | public: |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 74 | enum DirtyPropertyMask { |
| 75 | GENERIC = 1 << 1, |
| 76 | TRANSLATION_X = 1 << 2, |
| 77 | TRANSLATION_Y = 1 << 3, |
| 78 | TRANSLATION_Z = 1 << 4, |
| 79 | SCALE_X = 1 << 5, |
| 80 | SCALE_Y = 1 << 6, |
| 81 | ROTATION = 1 << 7, |
| 82 | ROTATION_X = 1 << 8, |
| 83 | ROTATION_Y = 1 << 9, |
| 84 | X = 1 << 10, |
| 85 | Y = 1 << 11, |
| 86 | Z = 1 << 12, |
| 87 | ALPHA = 1 << 13, |
John Reck | a7c2ea2 | 2014-08-08 13:21:00 -0700 | [diff] [blame] | 88 | DISPLAY_LIST = 1 << 14, |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 89 | }; |
| 90 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 91 | ANDROID_API RenderNode(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 92 | ANDROID_API virtual ~RenderNode(); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 93 | |
| 94 | // See flags defined in DisplayList.java |
| 95 | enum ReplayFlag { |
| 96 | kReplayFlag_ClipChildren = 0x1 |
| 97 | }; |
| 98 | |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 99 | static void outputLogBuffer(int fd); |
John Reck | 443a714 | 2014-09-04 17:40:05 -0700 | [diff] [blame] | 100 | void debugDumpLayers(const char* prefix); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 101 | |
John Reck | 8de65a8 | 2014-04-09 15:23:38 -0700 | [diff] [blame] | 102 | ANDROID_API void setStagingDisplayList(DisplayListData* newData); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 103 | |
| 104 | void computeOrdering(); |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 105 | |
Chris Craik | 80d4902 | 2014-06-20 15:03:43 -0700 | [diff] [blame] | 106 | void defer(DeferStateStruct& deferStruct, const int level); |
| 107 | void replay(ReplayStateStruct& replayStruct, const int level); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 108 | |
| 109 | ANDROID_API void output(uint32_t level = 1); |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 110 | ANDROID_API int getDebugSize(); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 111 | |
| 112 | bool isRenderable() const { |
Chris Craik | 8afd0f2 | 2014-08-21 17:41:57 -0700 | [diff] [blame] | 113 | return mDisplayListData && !mDisplayListData->isEmpty(); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 114 | } |
| 115 | |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 116 | bool hasProjectionReceiver() const { |
| 117 | return mDisplayListData && mDisplayListData->projectionReceiveIndex >= 0; |
| 118 | } |
| 119 | |
Chris Craik | defb7f3 | 2014-04-08 18:17:07 -0700 | [diff] [blame] | 120 | const char* getName() const { |
| 121 | return mName.string(); |
| 122 | } |
| 123 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 124 | void setName(const char* name) { |
| 125 | if (name) { |
| 126 | char* lastPeriod = strrchr(name, '.'); |
| 127 | if (lastPeriod) { |
| 128 | mName.setTo(lastPeriod + 1); |
| 129 | } else { |
| 130 | mName.setTo(name); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 135 | bool isPropertyFieldDirty(DirtyPropertyMask field) const { |
| 136 | return mDirtyPropertyFields & field; |
| 137 | } |
| 138 | |
| 139 | void setPropertyFieldsDirty(uint32_t fields) { |
| 140 | mDirtyPropertyFields |= fields; |
| 141 | } |
| 142 | |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 143 | const RenderProperties& properties() const { |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 144 | return mProperties; |
| 145 | } |
| 146 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 147 | RenderProperties& animatorProperties() { |
| 148 | return mProperties; |
| 149 | } |
| 150 | |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 151 | const RenderProperties& stagingProperties() { |
| 152 | return mStagingProperties; |
| 153 | } |
| 154 | |
| 155 | RenderProperties& mutateStagingProperties() { |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 156 | return mStagingProperties; |
| 157 | } |
| 158 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 159 | int getWidth() { |
| 160 | return properties().getWidth(); |
| 161 | } |
| 162 | |
| 163 | int getHeight() { |
| 164 | return properties().getHeight(); |
| 165 | } |
| 166 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 167 | ANDROID_API virtual void prepareTree(TreeInfo& info); |
John Reck | dcba672 | 2014-07-08 13:59:49 -0700 | [diff] [blame] | 168 | void destroyHardwareResources(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 169 | |
| 170 | // UI thread only! |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 171 | ANDROID_API void addAnimator(const sp<BaseRenderNodeAnimator>& animator); |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 172 | |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 173 | AnimatorManager& animators() { return mAnimatorManager; } |
| 174 | |
Chris Craik | 69e5adf | 2014-08-14 13:34:01 -0700 | [diff] [blame] | 175 | void applyViewPropertyTransforms(mat4& matrix, bool true3dTransform = false) const; |
| 176 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 177 | private: |
Chris Craik | a7090e0 | 2014-06-20 16:01:00 -0700 | [diff] [blame] | 178 | typedef key_value_pair_t<float, DrawRenderNodeOp*> ZDrawRenderNodeOpPair; |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 179 | |
John Reck | 272a685 | 2015-07-29 16:48:58 -0700 | [diff] [blame] | 180 | static size_t findNonNegativeIndex(const std::vector<ZDrawRenderNodeOpPair>& nodes) { |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 181 | for (size_t i = 0; i < nodes.size(); i++) { |
| 182 | if (nodes[i].key >= 0.0f) return i; |
| 183 | } |
| 184 | return nodes.size(); |
| 185 | } |
| 186 | |
| 187 | enum ChildrenSelectMode { |
| 188 | kNegativeZChildren, |
| 189 | kPositiveZChildren |
| 190 | }; |
| 191 | |
Chris Craik | a7090e0 | 2014-06-20 16:01:00 -0700 | [diff] [blame] | 192 | void computeOrderingImpl(DrawRenderNodeOp* opState, |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 193 | const SkPath* outlineOfProjectionSurface, |
John Reck | 272a685 | 2015-07-29 16:48:58 -0700 | [diff] [blame] | 194 | std::vector<DrawRenderNodeOp*>* compositedChildrenOfProjectionSurface, |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 195 | const mat4* transformFromProjectionSurface); |
| 196 | |
| 197 | template <class T> |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 198 | inline void setViewProperties(OpenGLRenderer& renderer, T& handler); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 199 | |
Chris Craik | 8afd0f2 | 2014-08-21 17:41:57 -0700 | [diff] [blame] | 200 | void buildZSortedChildList(const DisplayListData::Chunk& chunk, |
John Reck | 272a685 | 2015-07-29 16:48:58 -0700 | [diff] [blame] | 201 | std::vector<ZDrawRenderNodeOpPair>& zTranslatedNodes); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 202 | |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 203 | template<class T> |
| 204 | inline void issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler); |
| 205 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 206 | template <class T> |
Chris Craik | c3e75f9 | 2014-08-27 15:34:52 -0700 | [diff] [blame] | 207 | inline void issueOperationsOf3dChildren(ChildrenSelectMode mode, |
John Reck | 272a685 | 2015-07-29 16:48:58 -0700 | [diff] [blame] | 208 | const Matrix4& initialTransform, const std::vector<ZDrawRenderNodeOpPair>& zTranslatedNodes, |
Chris Craik | 80d4902 | 2014-06-20 15:03:43 -0700 | [diff] [blame] | 209 | OpenGLRenderer& renderer, T& handler); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 210 | |
| 211 | template <class T> |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 212 | inline void issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 213 | |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 214 | /** |
| 215 | * Issue the RenderNode's operations into a handler, recursing for subtrees through |
Chris Craik | a7090e0 | 2014-06-20 16:01:00 -0700 | [diff] [blame] | 216 | * DrawRenderNodeOp's defer() or replay() methods |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 217 | */ |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 218 | template <class T> |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 219 | inline void issueOperations(OpenGLRenderer& renderer, T& handler); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 220 | |
| 221 | class TextContainer { |
| 222 | public: |
| 223 | size_t length() const { |
| 224 | return mByteLength; |
| 225 | } |
| 226 | |
| 227 | const char* text() const { |
| 228 | return (const char*) mText; |
| 229 | } |
| 230 | |
| 231 | size_t mByteLength; |
| 232 | const char* mText; |
| 233 | }; |
| 234 | |
Chris Craik | a766cb2 | 2015-06-08 16:49:43 -0700 | [diff] [blame] | 235 | void prepareTreeImpl(TreeInfo& info, bool functorsNeedLayer); |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 236 | void pushStagingPropertiesChanges(TreeInfo& info); |
| 237 | void pushStagingDisplayListChanges(TreeInfo& info); |
Chris Craik | a766cb2 | 2015-06-08 16:49:43 -0700 | [diff] [blame] | 238 | void prepareSubTree(TreeInfo& info, bool functorsNeedLayer, DisplayListData* subtree); |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 239 | void applyLayerPropertiesToLayer(TreeInfo& info); |
John Reck | a7c2ea2 | 2014-08-08 13:21:00 -0700 | [diff] [blame] | 240 | void prepareLayer(TreeInfo& info, uint32_t dirtyMask); |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 241 | void pushLayerUpdate(TreeInfo& info); |
John Reck | dcba672 | 2014-07-08 13:59:49 -0700 | [diff] [blame] | 242 | void deleteDisplayListData(); |
John Reck | 0a97330 | 2014-07-16 13:29:45 -0700 | [diff] [blame] | 243 | void damageSelf(TreeInfo& info); |
John Reck | dcba672 | 2014-07-08 13:59:49 -0700 | [diff] [blame] | 244 | |
| 245 | void incParentRefCount() { mParentCount++; } |
| 246 | void decParentRefCount(); |
John Reck | 8de65a8 | 2014-04-09 15:23:38 -0700 | [diff] [blame] | 247 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 248 | String8 mName; |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 249 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 250 | uint32_t mDirtyPropertyFields; |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 251 | RenderProperties mProperties; |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 252 | RenderProperties mStagingProperties; |
| 253 | |
John Reck | 8de65a8 | 2014-04-09 15:23:38 -0700 | [diff] [blame] | 254 | bool mNeedsDisplayListDataSync; |
John Reck | dcba672 | 2014-07-08 13:59:49 -0700 | [diff] [blame] | 255 | // WARNING: Do not delete this directly, you must go through deleteDisplayListData()! |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 256 | DisplayListData* mDisplayListData; |
John Reck | 8de65a8 | 2014-04-09 15:23:38 -0700 | [diff] [blame] | 257 | DisplayListData* mStagingDisplayListData; |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 258 | |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 259 | friend class AnimatorManager; |
| 260 | AnimatorManager mAnimatorManager; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 261 | |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 262 | // Owned by RT. Lifecycle is managed by prepareTree(), with the exception |
| 263 | // being in ~RenderNode() which may happen on any thread. |
| 264 | Layer* mLayer; |
| 265 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 266 | /** |
| 267 | * Draw time state - these properties are only set and used during rendering |
| 268 | */ |
| 269 | |
| 270 | // for projection surfaces, contains a list of all children items |
John Reck | 272a685 | 2015-07-29 16:48:58 -0700 | [diff] [blame] | 271 | std::vector<DrawRenderNodeOp*> mProjectedNodes; |
John Reck | dcba672 | 2014-07-08 13:59:49 -0700 | [diff] [blame] | 272 | |
| 273 | // How many references our parent(s) have to us. Typically this should alternate |
| 274 | // between 2 and 1 (when a staging push happens we inc first then dec) |
| 275 | // When this hits 0 we are no longer in the tree, so any hardware resources |
| 276 | // (specifically Layers) should be released. |
| 277 | // This is *NOT* thread-safe, and should therefore only be tracking |
| 278 | // mDisplayListData, not mStagingDisplayListData. |
| 279 | uint32_t mParentCount; |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 280 | }; // class RenderNode |
| 281 | |
| 282 | } /* namespace uirenderer */ |
| 283 | } /* namespace android */ |
| 284 | |
| 285 | #endif /* RENDERNODE_H */ |