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 #include <SkCamera.h> 20 #include <SkMatrix.h> 21 22 #include <utils/LinearAllocator.h> 23 #include <utils/RefBase.h> 24 #include <utils/String8.h> 25 26 #include <cutils/compiler.h> 27 28 #include <androidfw/ResourceTypes.h> 29 30 #include "AnimatorManager.h" 31 #include "Debug.h" 32 #include "DisplayList.h" 33 #include "Matrix.h" 34 #include "RenderProperties.h" 35 36 #include <vector> 37 38 class SkBitmap; 39 class SkPaint; 40 class SkPath; 41 class SkRegion; 42 43 namespace android { 44 namespace uirenderer { 45 46 class CanvasState; 47 class DisplayListCanvas; 48 class DisplayListOp; 49 class OpenGLRenderer; 50 class Rect; 51 class SkiaShader; 52 53 #if HWUI_NEW_OPS 54 class FrameBuilder; 55 class OffscreenBuffer; 56 struct RenderNodeOp; 57 typedef OffscreenBuffer layer_t; 58 typedef RenderNodeOp renderNodeOp_t; 59 #else 60 class Layer; 61 typedef Layer layer_t; 62 typedef DrawRenderNodeOp renderNodeOp_t; 63 #endif 64 65 class ClipRectOp; 66 class DrawRenderNodeOp; 67 class SaveLayerOp; 68 class SaveOp; 69 class RestoreToCountOp; 70 class TreeInfo; 71 class TreeObserver; 72 73 namespace proto { 74 class RenderNode; 75 } 76 77 /** 78 * Primary class for storing recorded canvas commands, as well as per-View/ViewGroup display properties. 79 * 80 * Recording of canvas commands is somewhat similar to SkPicture, except the canvas-recording 81 * functionality is split between DisplayListCanvas (which manages the recording), DisplayList 82 * (which holds the actual data), and DisplayList (which holds properties and performs playback onto 83 * a renderer). 84 * 85 * Note that DisplayList is swapped out from beneath an individual RenderNode when a view's 86 * recorded stream of canvas operations is refreshed. The RenderNode (and its properties) stay 87 * attached. 88 */ 89 class RenderNode : public VirtualLightRefBase { 90 friend class TestUtils; // allow TestUtils to access syncDisplayList / syncProperties 91 friend class FrameBuilder; 92 public: 93 enum DirtyPropertyMask { 94 GENERIC = 1 << 1, 95 TRANSLATION_X = 1 << 2, 96 TRANSLATION_Y = 1 << 3, 97 TRANSLATION_Z = 1 << 4, 98 SCALE_X = 1 << 5, 99 SCALE_Y = 1 << 6, 100 ROTATION = 1 << 7, 101 ROTATION_X = 1 << 8, 102 ROTATION_Y = 1 << 9, 103 X = 1 << 10, 104 Y = 1 << 11, 105 Z = 1 << 12, 106 ALPHA = 1 << 13, 107 DISPLAY_LIST = 1 << 14, 108 }; 109 110 ANDROID_API RenderNode(); 111 ANDROID_API virtual ~RenderNode(); 112 113 // See flags defined in DisplayList.java 114 enum ReplayFlag { 115 kReplayFlag_ClipChildren = 0x1 116 }; 117 118 void debugDumpLayers(const char* prefix); 119 120 ANDROID_API void setStagingDisplayList(DisplayList* newData, TreeObserver* observer); 121 122 void computeOrdering(); 123 124 void defer(DeferStateStruct& deferStruct, const int level); 125 void replay(ReplayStateStruct& replayStruct, const int level); 126 127 #if HWUI_NEW_OPS 128 ANDROID_API void output(uint32_t level = 0, const char* label = "Root"); 129 #else 130 ANDROID_API void output(uint32_t level = 1); 131 #endif 132 ANDROID_API int getDebugSize(); 133 void copyTo(proto::RenderNode* node); 134 isRenderable()135 bool isRenderable() const { 136 return mDisplayList && !mDisplayList->isEmpty(); 137 } 138 hasProjectionReceiver()139 bool hasProjectionReceiver() const { 140 return mDisplayList && mDisplayList->projectionReceiveIndex >= 0; 141 } 142 getName()143 const char* getName() const { 144 return mName.string(); 145 } 146 setName(const char * name)147 void setName(const char* name) { 148 if (name) { 149 char* lastPeriod = strrchr(name, '.'); 150 if (lastPeriod) { 151 mName.setTo(lastPeriod + 1); 152 } else { 153 mName.setTo(name); 154 } 155 } 156 } 157 getUserContext()158 VirtualLightRefBase* getUserContext() const { 159 return mUserContext.get(); 160 } 161 setUserContext(VirtualLightRefBase * context)162 void setUserContext(VirtualLightRefBase* context) { 163 mUserContext = context; 164 } 165 isPropertyFieldDirty(DirtyPropertyMask field)166 bool isPropertyFieldDirty(DirtyPropertyMask field) const { 167 return mDirtyPropertyFields & field; 168 } 169 setPropertyFieldsDirty(uint32_t fields)170 void setPropertyFieldsDirty(uint32_t fields) { 171 mDirtyPropertyFields |= fields; 172 } 173 properties()174 const RenderProperties& properties() const { 175 return mProperties; 176 } 177 animatorProperties()178 RenderProperties& animatorProperties() { 179 return mProperties; 180 } 181 stagingProperties()182 const RenderProperties& stagingProperties() { 183 return mStagingProperties; 184 } 185 mutateStagingProperties()186 RenderProperties& mutateStagingProperties() { 187 return mStagingProperties; 188 } 189 getWidth()190 int getWidth() const { 191 return properties().getWidth(); 192 } 193 getHeight()194 int getHeight() const { 195 return properties().getHeight(); 196 } 197 198 ANDROID_API virtual void prepareTree(TreeInfo& info); 199 void destroyHardwareResources(TreeObserver* observer, TreeInfo* info = nullptr); 200 201 // UI thread only! 202 ANDROID_API void addAnimator(const sp<BaseRenderNodeAnimator>& animator); 203 void removeAnimator(const sp<BaseRenderNodeAnimator>& animator); 204 205 // This can only happen during pushStaging() onAnimatorTargetChanged(BaseRenderNodeAnimator * animator)206 void onAnimatorTargetChanged(BaseRenderNodeAnimator* animator) { 207 mAnimatorManager.onAnimatorTargetChanged(animator); 208 } 209 animators()210 AnimatorManager& animators() { return mAnimatorManager; } 211 212 void applyViewPropertyTransforms(mat4& matrix, bool true3dTransform = false) const; 213 nothingToDraw()214 bool nothingToDraw() const { 215 const Outline& outline = properties().getOutline(); 216 return mDisplayList == nullptr 217 || properties().getAlpha() <= 0 218 || (outline.getShouldClip() && outline.isEmpty()) 219 || properties().getScaleX() == 0 220 || properties().getScaleY() == 0; 221 } 222 getDisplayList()223 const DisplayList* getDisplayList() const { 224 return mDisplayList; 225 } 226 #if HWUI_NEW_OPS getLayer()227 OffscreenBuffer* getLayer() const { return mLayer; } getLayerHandle()228 OffscreenBuffer** getLayerHandle() { return &mLayer; } // ugh... 229 #endif 230 231 // Note: The position callbacks are relying on the listener using 232 // the frameNumber to appropriately batch/synchronize these transactions. 233 // There is no other filtering/batching to ensure that only the "final" 234 // state called once per frame. 235 class ANDROID_API PositionListener { 236 public: ~PositionListener()237 virtual ~PositionListener() {} 238 // Called when the RenderNode's position changes 239 virtual void onPositionUpdated(RenderNode& node, const TreeInfo& info) = 0; 240 // Called when the RenderNode no longer has a position. As in, it's 241 // no longer being drawn. 242 // Note, tree info might be null 243 virtual void onPositionLost(RenderNode& node, const TreeInfo* info) = 0; 244 }; 245 246 // Note this is not thread safe, this needs to be called 247 // before the RenderNode is used for drawing. 248 // RenderNode takes ownership of the pointer setPositionListener(PositionListener * listener)249 ANDROID_API void setPositionListener(PositionListener* listener) { 250 mPositionListener.reset(listener); 251 } 252 253 // This is only modified in MODE_FULL, so it can be safely accessed 254 // on the UI thread. hasParents()255 ANDROID_API bool hasParents() { 256 return mParentCount; 257 } 258 259 private: 260 typedef key_value_pair_t<float, DrawRenderNodeOp*> ZDrawRenderNodeOpPair; 261 findNonNegativeIndex(const std::vector<ZDrawRenderNodeOpPair> & nodes)262 static size_t findNonNegativeIndex(const std::vector<ZDrawRenderNodeOpPair>& nodes) { 263 for (size_t i = 0; i < nodes.size(); i++) { 264 if (nodes[i].key >= 0.0f) return i; 265 } 266 return nodes.size(); 267 } 268 269 enum class ChildrenSelectMode { 270 NegativeZChildren, 271 PositiveZChildren 272 }; 273 274 void computeOrderingImpl(renderNodeOp_t* opState, 275 std::vector<renderNodeOp_t*>* compositedChildrenOfProjectionSurface, 276 const mat4* transformFromProjectionSurface); 277 278 template <class T> 279 inline void setViewProperties(OpenGLRenderer& renderer, T& handler); 280 281 void buildZSortedChildList(const DisplayList::Chunk& chunk, 282 std::vector<ZDrawRenderNodeOpPair>& zTranslatedNodes); 283 284 template<class T> 285 inline void issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler); 286 287 template <class T> 288 inline void issueOperationsOf3dChildren(ChildrenSelectMode mode, 289 const Matrix4& initialTransform, const std::vector<ZDrawRenderNodeOpPair>& zTranslatedNodes, 290 OpenGLRenderer& renderer, T& handler); 291 292 template <class T> 293 inline void issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler); 294 295 /** 296 * Issue the RenderNode's operations into a handler, recursing for subtrees through 297 * DrawRenderNodeOp's defer() or replay() methods 298 */ 299 template <class T> 300 inline void issueOperations(OpenGLRenderer& renderer, T& handler); 301 302 class TextContainer { 303 public: length()304 size_t length() const { 305 return mByteLength; 306 } 307 text()308 const char* text() const { 309 return (const char*) mText; 310 } 311 312 size_t mByteLength; 313 const char* mText; 314 }; 315 316 317 void syncProperties(); 318 void syncDisplayList(TreeInfo* info); 319 320 void prepareTreeImpl(TreeInfo& info, bool functorsNeedLayer); 321 void pushStagingPropertiesChanges(TreeInfo& info); 322 void pushStagingDisplayListChanges(TreeInfo& info); 323 void prepareSubTree(TreeInfo& info, bool functorsNeedLayer, DisplayList* subtree); 324 #if !HWUI_NEW_OPS 325 void applyLayerPropertiesToLayer(TreeInfo& info); 326 #endif 327 void prepareLayer(TreeInfo& info, uint32_t dirtyMask); 328 void pushLayerUpdate(TreeInfo& info); 329 void deleteDisplayList(TreeObserver* observer, TreeInfo* info = nullptr); 330 void damageSelf(TreeInfo& info); 331 incParentRefCount()332 void incParentRefCount() { mParentCount++; } 333 void decParentRefCount(TreeObserver* observer, TreeInfo* info = nullptr); 334 335 String8 mName; 336 sp<VirtualLightRefBase> mUserContext; 337 338 uint32_t mDirtyPropertyFields; 339 RenderProperties mProperties; 340 RenderProperties mStagingProperties; 341 342 bool mNeedsDisplayListSync; 343 // WARNING: Do not delete this directly, you must go through deleteDisplayList()! 344 DisplayList* mDisplayList; 345 DisplayList* mStagingDisplayList; 346 347 friend class AnimatorManager; 348 AnimatorManager mAnimatorManager; 349 350 // Owned by RT. Lifecycle is managed by prepareTree(), with the exception 351 // being in ~RenderNode() which may happen on any thread. 352 layer_t* mLayer = nullptr; 353 354 /** 355 * Draw time state - these properties are only set and used during rendering 356 */ 357 358 // for projection surfaces, contains a list of all children items 359 std::vector<renderNodeOp_t*> mProjectedNodes; 360 361 // How many references our parent(s) have to us. Typically this should alternate 362 // between 2 and 1 (when a staging push happens we inc first then dec) 363 // When this hits 0 we are no longer in the tree, so any hardware resources 364 // (specifically Layers) should be released. 365 // This is *NOT* thread-safe, and should therefore only be tracking 366 // mDisplayList, not mStagingDisplayList. 367 uint32_t mParentCount; 368 369 std::unique_ptr<PositionListener> mPositionListener; 370 }; // class RenderNode 371 372 } /* namespace uirenderer */ 373 } /* namespace android */ 374 375 #endif /* RENDERNODE_H */ 376