RenderNode.h revision 500a0c30d4dcd012218c3e44a62926a1c34a259f
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 17#pragma once 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#include "pipeline/skia/SkiaDisplayList.h" 36#include "pipeline/skia/SkiaLayer.h" 37 38#include <vector> 39 40class SkBitmap; 41class SkPaint; 42class SkPath; 43class SkRegion; 44class SkSurface; 45 46namespace android { 47namespace uirenderer { 48 49class CanvasState; 50class DisplayListOp; 51class FrameBuilder; 52class OffscreenBuffer; 53class Rect; 54class SkiaShader; 55struct RenderNodeOp; 56 57class TreeInfo; 58class TreeObserver; 59 60namespace proto { 61class RenderNode; 62} 63 64/** 65 * Primary class for storing recorded canvas commands, as well as per-View/ViewGroup display properties. 66 * 67 * Recording of canvas commands is somewhat similar to SkPicture, except the canvas-recording 68 * functionality is split between RecordingCanvas (which manages the recording), DisplayList 69 * (which holds the actual data), and RenderNode (which holds properties used for render playback). 70 * 71 * Note that DisplayList is swapped out from beneath an individual RenderNode when a view's 72 * recorded stream of canvas operations is refreshed. The RenderNode (and its properties) stay 73 * attached. 74 */ 75class RenderNode : public VirtualLightRefBase { 76friend class TestUtils; // allow TestUtils to access syncDisplayList / syncProperties 77friend class FrameBuilder; 78public: 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, 93 DISPLAY_LIST = 1 << 14, 94 }; 95 96 ANDROID_API RenderNode(); 97 ANDROID_API virtual ~RenderNode(); 98 99 // See flags defined in DisplayList.java 100 enum ReplayFlag { 101 kReplayFlag_ClipChildren = 0x1 102 }; 103 104 ANDROID_API void setStagingDisplayList(DisplayList* newData, TreeObserver* observer); 105 106 void computeOrdering(); 107 108 ANDROID_API void output(); 109 ANDROID_API int getDebugSize(); 110 void copyTo(proto::RenderNode* node); 111 112 bool isRenderable() const { 113 return mDisplayList && !mDisplayList->isEmpty(); 114 } 115 116 bool hasProjectionReceiver() const { 117 return mDisplayList && mDisplayList->projectionReceiveIndex >= 0; 118 } 119 120 const char* getName() const { 121 return mName.string(); 122 } 123 124 void setName(const char* name) { 125 if (name) { 126 const char* lastPeriod = strrchr(name, '.'); 127 if (lastPeriod) { 128 mName.setTo(lastPeriod + 1); 129 } else { 130 mName.setTo(name); 131 } 132 } 133 } 134 135 VirtualLightRefBase* getUserContext() const { 136 return mUserContext.get(); 137 } 138 139 void setUserContext(VirtualLightRefBase* context) { 140 mUserContext = context; 141 } 142 143 bool isPropertyFieldDirty(DirtyPropertyMask field) const { 144 return mDirtyPropertyFields & field; 145 } 146 147 void setPropertyFieldsDirty(uint32_t fields) { 148 mDirtyPropertyFields |= fields; 149 } 150 151 const RenderProperties& properties() const { 152 return mProperties; 153 } 154 155 RenderProperties& animatorProperties() { 156 return mProperties; 157 } 158 159 const RenderProperties& stagingProperties() { 160 return mStagingProperties; 161 } 162 163 RenderProperties& mutateStagingProperties() { 164 return mStagingProperties; 165 } 166 167 int getWidth() const { 168 return properties().getWidth(); 169 } 170 171 int getHeight() const { 172 return properties().getHeight(); 173 } 174 175 ANDROID_API virtual void prepareTree(TreeInfo& info); 176 void destroyHardwareResources(TreeObserver* observer, TreeInfo* info = nullptr); 177 178 // UI thread only! 179 ANDROID_API void addAnimator(const sp<BaseRenderNodeAnimator>& animator); 180 void removeAnimator(const sp<BaseRenderNodeAnimator>& animator); 181 182 // This can only happen during pushStaging() 183 void onAnimatorTargetChanged(BaseRenderNodeAnimator* animator) { 184 mAnimatorManager.onAnimatorTargetChanged(animator); 185 } 186 187 AnimatorManager& animators() { return mAnimatorManager; } 188 189 void applyViewPropertyTransforms(mat4& matrix, bool true3dTransform = false) const; 190 191 bool nothingToDraw() const { 192 const Outline& outline = properties().getOutline(); 193 return mDisplayList == nullptr 194 || properties().getAlpha() <= 0 195 || (outline.getShouldClip() && outline.isEmpty()) 196 || properties().getScaleX() == 0 197 || properties().getScaleY() == 0; 198 } 199 200 const DisplayList* getDisplayList() const { 201 return mDisplayList; 202 } 203 OffscreenBuffer* getLayer() const { return mLayer; } 204 OffscreenBuffer** getLayerHandle() { return &mLayer; } // ugh... 205 void setLayer(OffscreenBuffer* layer) { mLayer = layer; } 206 207 // Note: The position callbacks are relying on the listener using 208 // the frameNumber to appropriately batch/synchronize these transactions. 209 // There is no other filtering/batching to ensure that only the "final" 210 // state called once per frame. 211 class ANDROID_API PositionListener : public VirtualLightRefBase { 212 public: 213 virtual ~PositionListener() {} 214 // Called when the RenderNode's position changes 215 virtual void onPositionUpdated(RenderNode& node, const TreeInfo& info) = 0; 216 // Called when the RenderNode no longer has a position. As in, it's 217 // no longer being drawn. 218 // Note, tree info might be null 219 virtual void onPositionLost(RenderNode& node, const TreeInfo* info) = 0; 220 }; 221 222 // Note this is not thread safe, this needs to be called 223 // before the RenderNode is used for drawing. 224 // RenderNode takes ownership of the pointer 225 ANDROID_API void setPositionListener(PositionListener* listener) { 226 mPositionListener = listener; 227 } 228 229 // This is only modified in MODE_FULL, so it can be safely accessed 230 // on the UI thread. 231 ANDROID_API bool hasParents() { 232 return mParentCount; 233 } 234 235private: 236 void computeOrderingImpl(RenderNodeOp* opState, 237 std::vector<RenderNodeOp*>* compositedChildrenOfProjectionSurface, 238 const mat4* transformFromProjectionSurface); 239 240 void syncProperties(); 241 void syncDisplayList(TreeInfo* info); 242 243 void prepareTreeImpl(TreeInfo& info, bool functorsNeedLayer); 244 void pushStagingPropertiesChanges(TreeInfo& info); 245 void pushStagingDisplayListChanges(TreeInfo& info); 246 void prepareLayer(TreeInfo& info, uint32_t dirtyMask); 247 void pushLayerUpdate(TreeInfo& info); 248 void deleteDisplayList(TreeObserver* observer, TreeInfo* info = nullptr); 249 void damageSelf(TreeInfo& info); 250 251 void incParentRefCount() { mParentCount++; } 252 void decParentRefCount(TreeObserver* observer, TreeInfo* info = nullptr); 253 void output(std::ostream& output, uint32_t level); 254 255 String8 mName; 256 sp<VirtualLightRefBase> mUserContext; 257 258 uint32_t mDirtyPropertyFields; 259 RenderProperties mProperties; 260 RenderProperties mStagingProperties; 261 262 bool mNeedsDisplayListSync; 263 // WARNING: Do not delete this directly, you must go through deleteDisplayList()! 264 DisplayList* mDisplayList; 265 DisplayList* mStagingDisplayList; 266 267 friend class AnimatorManager; 268 AnimatorManager mAnimatorManager; 269 270 // Owned by RT. Lifecycle is managed by prepareTree(), with the exception 271 // being in ~RenderNode() which may happen on any thread. 272 OffscreenBuffer* mLayer = nullptr; 273 274 /** 275 * Draw time state - these properties are only set and used during rendering 276 */ 277 278 // for projection surfaces, contains a list of all children items 279 std::vector<RenderNodeOp*> mProjectedNodes; 280 281 // How many references our parent(s) have to us. Typically this should alternate 282 // between 2 and 1 (when a staging push happens we inc first then dec) 283 // When this hits 0 we are no longer in the tree, so any hardware resources 284 // (specifically Layers) should be released. 285 // This is *NOT* thread-safe, and should therefore only be tracking 286 // mDisplayList, not mStagingDisplayList. 287 uint32_t mParentCount; 288 289 sp<PositionListener> mPositionListener; 290 291// METHODS & FIELDS ONLY USED BY THE SKIA RENDERER 292public: 293 /** 294 * Detach and transfer ownership of an already allocated displayList for use 295 * in recording updated content for this renderNode 296 */ 297 std::unique_ptr<skiapipeline::SkiaDisplayList> detachAvailableList() { 298 return std::move(mAvailableDisplayList); 299 } 300 301 /** 302 * Attach unused displayList to this node for potential future reuse. 303 */ 304 void attachAvailableList(skiapipeline::SkiaDisplayList* skiaDisplayList) { 305 mAvailableDisplayList.reset(skiaDisplayList); 306 } 307 308 /** 309 * Returns true if an offscreen layer from any renderPipeline is attached 310 * to this node. 311 */ 312 bool hasLayer() const { return mLayer || mSkiaLayer.get(); } 313 314 /** 315 * Used by the RenderPipeline to attach an offscreen surface to the RenderNode. 316 * The surface is then will be used to store the contents of a layer. 317 */ 318 void setLayerSurface(sk_sp<SkSurface> layer) { 319 if (layer.get()) { 320 if (!mSkiaLayer.get()) { 321 mSkiaLayer = std::make_unique<skiapipeline::SkiaLayer>(); 322 } 323 mSkiaLayer->layerSurface = std::move(layer); 324 mSkiaLayer->inverseTransformInWindow.loadIdentity(); 325 } else { 326 mSkiaLayer.reset(); 327 } 328 } 329 330 /** 331 * If the RenderNode is of type LayerType::RenderLayer then this method will 332 * return the an offscreen rendering surface that is used to both render into 333 * the layer and composite the layer into its parent. If the type is not 334 * LayerType::RenderLayer then it will return a nullptr. 335 * 336 * NOTE: this function is only guaranteed to return accurate results after 337 * prepareTree has been run for this RenderNode 338 */ 339 SkSurface* getLayerSurface() const { 340 return mSkiaLayer.get() ? mSkiaLayer->layerSurface.get() : nullptr; 341 } 342 343 skiapipeline::SkiaLayer* getSkiaLayer() const { 344 return mSkiaLayer.get(); 345 } 346 347private: 348 /** 349 * If this RenderNode has been used in a previous frame then the SkiaDisplayList 350 * from that frame is cached here until one of the following conditions is met: 351 * 1) The RenderNode is deleted (causing this to be deleted) 352 * 2) It is replaced with the displayList from the next completed frame 353 * 3) It is detached and used to to record a new displayList for a later frame 354 */ 355 std::unique_ptr<skiapipeline::SkiaDisplayList> mAvailableDisplayList; 356 357 /** 358 * An offscreen rendering target used to contain the contents this RenderNode 359 * when it has been set to draw as a LayerType::RenderLayer. 360 */ 361 std::unique_ptr<skiapipeline::SkiaLayer> mSkiaLayer; 362}; // class RenderNode 363 364} /* namespace uirenderer */ 365} /* namespace android */ 366