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