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