RenderNode.h revision 51f2d606dcbfba3cc5b03dfea37c1304b91c232f
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 38class SkBitmap; 39class SkPaint; 40class SkPath; 41class SkRegion; 42 43namespace android { 44namespace uirenderer { 45 46class CanvasState; 47class DisplayListCanvas; 48class DisplayListOp; 49class OpenGLRenderer; 50class Rect; 51class SkiaShader; 52 53#if HWUI_NEW_OPS 54class FrameBuilder; 55class OffscreenBuffer; 56struct RenderNodeOp; 57typedef OffscreenBuffer layer_t; 58typedef RenderNodeOp renderNodeOp_t; 59#else 60class Layer; 61typedef Layer layer_t; 62typedef DrawRenderNodeOp renderNodeOp_t; 63#endif 64 65class ClipRectOp; 66class DrawRenderNodeOp; 67class SaveLayerOp; 68class SaveOp; 69class RestoreToCountOp; 70class TreeInfo; 71class TreeObserver; 72 73namespace proto { 74class 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 */ 89class RenderNode : public VirtualLightRefBase { 90friend class TestUtils; // allow TestUtils to access syncDisplayList / syncProperties 91friend class FrameBuilder; 92public: 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 135 bool isRenderable() const { 136 return mDisplayList && !mDisplayList->isEmpty(); 137 } 138 139 bool hasProjectionReceiver() const { 140 return mDisplayList && mDisplayList->projectionReceiveIndex >= 0; 141 } 142 143 const char* getName() const { 144 return mName.string(); 145 } 146 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 158 VirtualLightRefBase* getUserContext() const { 159 return mUserContext.get(); 160 } 161 162 void setUserContext(VirtualLightRefBase* context) { 163 mUserContext = context; 164 } 165 166 bool isPropertyFieldDirty(DirtyPropertyMask field) const { 167 return mDirtyPropertyFields & field; 168 } 169 170 void setPropertyFieldsDirty(uint32_t fields) { 171 mDirtyPropertyFields |= fields; 172 } 173 174 const RenderProperties& properties() const { 175 return mProperties; 176 } 177 178 RenderProperties& animatorProperties() { 179 return mProperties; 180 } 181 182 const RenderProperties& stagingProperties() { 183 return mStagingProperties; 184 } 185 186 RenderProperties& mutateStagingProperties() { 187 return mStagingProperties; 188 } 189 190 int getWidth() const { 191 return properties().getWidth(); 192 } 193 194 int getHeight() const { 195 return properties().getHeight(); 196 } 197 198 ANDROID_API virtual void prepareTree(TreeInfo& info); 199 void destroyHardwareResources(TreeObserver* observer); 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() 206 void onAnimatorTargetChanged(BaseRenderNodeAnimator* animator) { 207 mAnimatorManager.onAnimatorTargetChanged(animator); 208 } 209 210 AnimatorManager& animators() { return mAnimatorManager; } 211 212 void applyViewPropertyTransforms(mat4& matrix, bool true3dTransform = false) const; 213 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 223 const DisplayList* getDisplayList() const { 224 return mDisplayList; 225 } 226#if HWUI_NEW_OPS 227 OffscreenBuffer* getLayer() const { return mLayer; } 228 OffscreenBuffer** getLayerHandle() { return &mLayer; } // ugh... 229#endif 230 231 class ANDROID_API PositionListener { 232 public: 233 virtual ~PositionListener() {} 234 virtual void onPositionUpdated(RenderNode& node, const TreeInfo& info) = 0; 235 }; 236 237 // Note this is not thread safe, this needs to be called 238 // before the RenderNode is used for drawing. 239 // RenderNode takes ownership of the pointer 240 ANDROID_API void setPositionListener(PositionListener* listener) { 241 mPositionListener.reset(listener); 242 } 243 244 // This is only modified in MODE_FULL, so it can be safely accessed 245 // on the UI thread. 246 ANDROID_API bool hasParents() { 247 return mParentCount; 248 } 249 250private: 251 typedef key_value_pair_t<float, DrawRenderNodeOp*> ZDrawRenderNodeOpPair; 252 253 static size_t findNonNegativeIndex(const std::vector<ZDrawRenderNodeOpPair>& nodes) { 254 for (size_t i = 0; i < nodes.size(); i++) { 255 if (nodes[i].key >= 0.0f) return i; 256 } 257 return nodes.size(); 258 } 259 260 enum class ChildrenSelectMode { 261 NegativeZChildren, 262 PositiveZChildren 263 }; 264 265 void computeOrderingImpl(renderNodeOp_t* opState, 266 std::vector<renderNodeOp_t*>* compositedChildrenOfProjectionSurface, 267 const mat4* transformFromProjectionSurface); 268 269 template <class T> 270 inline void setViewProperties(OpenGLRenderer& renderer, T& handler); 271 272 void buildZSortedChildList(const DisplayList::Chunk& chunk, 273 std::vector<ZDrawRenderNodeOpPair>& zTranslatedNodes); 274 275 template<class T> 276 inline void issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler); 277 278 template <class T> 279 inline void issueOperationsOf3dChildren(ChildrenSelectMode mode, 280 const Matrix4& initialTransform, const std::vector<ZDrawRenderNodeOpPair>& zTranslatedNodes, 281 OpenGLRenderer& renderer, T& handler); 282 283 template <class T> 284 inline void issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler); 285 286 /** 287 * Issue the RenderNode's operations into a handler, recursing for subtrees through 288 * DrawRenderNodeOp's defer() or replay() methods 289 */ 290 template <class T> 291 inline void issueOperations(OpenGLRenderer& renderer, T& handler); 292 293 class TextContainer { 294 public: 295 size_t length() const { 296 return mByteLength; 297 } 298 299 const char* text() const { 300 return (const char*) mText; 301 } 302 303 size_t mByteLength; 304 const char* mText; 305 }; 306 307 308 void syncProperties(); 309 void syncDisplayList(TreeObserver* observer); 310 311 void prepareTreeImpl(TreeInfo& info, bool functorsNeedLayer); 312 void pushStagingPropertiesChanges(TreeInfo& info); 313 void pushStagingDisplayListChanges(TreeInfo& info); 314 void prepareSubTree(TreeInfo& info, bool functorsNeedLayer, DisplayList* subtree); 315#if !HWUI_NEW_OPS 316 void applyLayerPropertiesToLayer(TreeInfo& info); 317#endif 318 void prepareLayer(TreeInfo& info, uint32_t dirtyMask); 319 void pushLayerUpdate(TreeInfo& info); 320 void deleteDisplayList(TreeObserver* observer); 321 void damageSelf(TreeInfo& info); 322 323 void incParentRefCount() { mParentCount++; } 324 void decParentRefCount(TreeObserver* observer); 325 326 String8 mName; 327 sp<VirtualLightRefBase> mUserContext; 328 329 uint32_t mDirtyPropertyFields; 330 RenderProperties mProperties; 331 RenderProperties mStagingProperties; 332 333 bool mNeedsDisplayListSync; 334 // WARNING: Do not delete this directly, you must go through deleteDisplayList()! 335 DisplayList* mDisplayList; 336 DisplayList* mStagingDisplayList; 337 338 friend class AnimatorManager; 339 AnimatorManager mAnimatorManager; 340 341 // Owned by RT. Lifecycle is managed by prepareTree(), with the exception 342 // being in ~RenderNode() which may happen on any thread. 343 layer_t* mLayer = nullptr; 344 345 /** 346 * Draw time state - these properties are only set and used during rendering 347 */ 348 349 // for projection surfaces, contains a list of all children items 350 std::vector<renderNodeOp_t*> mProjectedNodes; 351 352 // How many references our parent(s) have to us. Typically this should alternate 353 // between 2 and 1 (when a staging push happens we inc first then dec) 354 // When this hits 0 we are no longer in the tree, so any hardware resources 355 // (specifically Layers) should be released. 356 // This is *NOT* thread-safe, and should therefore only be tracking 357 // mDisplayList, not mStagingDisplayList. 358 uint32_t mParentCount; 359 360 std::unique_ptr<PositionListener> mPositionListener; 361}; // class RenderNode 362 363} /* namespace uirenderer */ 364} /* namespace android */ 365 366#endif /* RENDERNODE_H */ 367