RenderNode.h revision 52244fff29042926e21fa897ef5ab11148e35299
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 <set> 24#include <vector> 25 26#include <SkCamera.h> 27#include <SkMatrix.h> 28 29#include <private/hwui/DrawGlInfo.h> 30 31#include <utils/KeyedVector.h> 32#include <utils/LinearAllocator.h> 33#include <utils/RefBase.h> 34#include <utils/SortedVector.h> 35#include <utils/String8.h> 36#include <utils/Vector.h> 37 38#include <cutils/compiler.h> 39 40#include <androidfw/ResourceTypes.h> 41 42#include "Debug.h" 43#include "Matrix.h" 44#include "DeferredDisplayList.h" 45#include "DisplayList.h" 46#include "RenderProperties.h" 47#include "TreeInfo.h" 48#include "utils/VirtualLightRefBase.h" 49 50class SkBitmap; 51class SkPaint; 52class SkPath; 53class SkRegion; 54 55namespace android { 56namespace uirenderer { 57 58class DeferredDisplayList; 59class DisplayListOp; 60class DisplayListRenderer; 61class OpenGLRenderer; 62class Rect; 63class Layer; 64class SkiaShader; 65 66class ClipRectOp; 67class SaveLayerOp; 68class SaveOp; 69class RestoreToCountOp; 70class DrawDisplayListOp; 71 72/** 73 * Primary class for storing recorded canvas commands, as well as per-View/ViewGroup display properties. 74 * 75 * Recording of canvas commands is somewhat similar to SkPicture, except the canvas-recording 76 * functionality is split between DisplayListRenderer (which manages the recording), DisplayListData 77 * (which holds the actual data), and DisplayList (which holds properties and performs playback onto 78 * a renderer). 79 * 80 * Note that DisplayListData is swapped out from beneath an individual DisplayList when a view's 81 * recorded stream of canvas operations is refreshed. The DisplayList (and its properties) stay 82 * attached. 83 */ 84class RenderNode : public VirtualLightRefBase { 85public: 86 ANDROID_API RenderNode(); 87 ANDROID_API virtual ~RenderNode(); 88 89 // See flags defined in DisplayList.java 90 enum ReplayFlag { 91 kReplayFlag_ClipChildren = 0x1 92 }; 93 94 ANDROID_API static void outputLogBuffer(int fd); 95 96 ANDROID_API void setStagingDisplayList(DisplayListData* newData); 97 98 void computeOrdering(); 99 100 void deferNodeTree(DeferStateStruct& deferStruct); 101 void deferNodeInParent(DeferStateStruct& deferStruct, const int level); 102 103 void replayNodeTree(ReplayStateStruct& replayStruct); 104 void replayNodeInParent(ReplayStateStruct& replayStruct, const int level); 105 106 ANDROID_API void output(uint32_t level = 1); 107 108 bool isRenderable() const { 109 return mDisplayListData && mDisplayListData->hasDrawOps; 110 } 111 112 const char* getName() const { 113 return mName.string(); 114 } 115 116 void setName(const char* name) { 117 if (name) { 118 char* lastPeriod = strrchr(name, '.'); 119 if (lastPeriod) { 120 mName.setTo(lastPeriod + 1); 121 } else { 122 mName.setTo(name); 123 } 124 } 125 } 126 127 const RenderProperties& properties() { 128 return mProperties; 129 } 130 131 RenderProperties& animatorProperties() { 132 return mProperties; 133 } 134 135 const RenderProperties& stagingProperties() { 136 return mStagingProperties; 137 } 138 139 RenderProperties& mutateStagingProperties() { 140 mNeedsPropertiesSync = true; 141 return mStagingProperties; 142 } 143 144 int getWidth() { 145 return properties().getWidth(); 146 } 147 148 int getHeight() { 149 return properties().getHeight(); 150 } 151 152 ANDROID_API virtual void prepareTree(TreeInfo& info); 153 154 // UI thread only! 155 ANDROID_API void addAnimator(const sp<BaseRenderNodeAnimator>& animator) { 156 mStagingAnimators.insert(animator); 157 mNeedsAnimatorsSync = true; 158 } 159 160 // UI thread only! 161 ANDROID_API void removeAnimator(const sp<BaseRenderNodeAnimator>& animator) { 162 mStagingAnimators.erase(animator); 163 mNeedsAnimatorsSync = true; 164 } 165 166private: 167 typedef key_value_pair_t<float, DrawDisplayListOp*> ZDrawDisplayListOpPair; 168 169 static size_t findNonNegativeIndex(const Vector<ZDrawDisplayListOpPair>& nodes) { 170 for (size_t i = 0; i < nodes.size(); i++) { 171 if (nodes[i].key >= 0.0f) return i; 172 } 173 return nodes.size(); 174 } 175 176 enum ChildrenSelectMode { 177 kNegativeZChildren, 178 kPositiveZChildren 179 }; 180 181 void applyViewPropertyTransforms(mat4& matrix, bool true3dTransform = false); 182 183 void computeOrderingImpl(DrawDisplayListOp* opState, 184 const SkPath* outlineOfProjectionSurface, 185 Vector<DrawDisplayListOp*>* compositedChildrenOfProjectionSurface, 186 const mat4* transformFromProjectionSurface); 187 188 template <class T> 189 inline void setViewProperties(OpenGLRenderer& renderer, T& handler); 190 191 void buildZSortedChildList(Vector<ZDrawDisplayListOpPair>& zTranslatedNodes); 192 193 template<class T> 194 inline void issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler); 195 196 template <class T> 197 inline void issueOperationsOf3dChildren(const Vector<ZDrawDisplayListOpPair>& zTranslatedNodes, 198 ChildrenSelectMode mode, OpenGLRenderer& renderer, T& handler); 199 200 template <class T> 201 inline void issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler); 202 203 /** 204 * Issue the RenderNode's operations into a handler, recursing for subtrees through 205 * DrawDisplayListOp's defer() or replay() methods 206 */ 207 template <class T> 208 inline void issueOperations(OpenGLRenderer& renderer, T& handler); 209 210 class TextContainer { 211 public: 212 size_t length() const { 213 return mByteLength; 214 } 215 216 const char* text() const { 217 return (const char*) mText; 218 } 219 220 size_t mByteLength; 221 const char* mText; 222 }; 223 224 void prepareTreeImpl(TreeInfo& info); 225 void pushStagingChanges(TreeInfo& info); 226 void evaluateAnimations(TreeInfo& info); 227 void prepareSubTree(TreeInfo& info, DisplayListData* subtree); 228 229 String8 mName; 230 231 bool mNeedsPropertiesSync; 232 RenderProperties mProperties; 233 RenderProperties mStagingProperties; 234 235 bool mNeedsDisplayListDataSync; 236 DisplayListData* mDisplayListData; 237 DisplayListData* mStagingDisplayListData; 238 239 bool mNeedsAnimatorsSync; 240 std::set< sp<BaseRenderNodeAnimator> > mStagingAnimators; 241 std::vector< sp<BaseRenderNodeAnimator> > mAnimators; 242 243 /** 244 * Draw time state - these properties are only set and used during rendering 245 */ 246 247 // for projection surfaces, contains a list of all children items 248 Vector<DrawDisplayListOp*> mProjectedNodes; 249}; // class RenderNode 250 251} /* namespace uirenderer */ 252} /* namespace android */ 253 254#endif /* RENDERNODE_H */ 255