1 /* 2 * Copyright (c) 2021-2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef RENDER_SERVICE_CLIENT_CORE_UI_RS_NODE_H 16 #define RENDER_SERVICE_CLIENT_CORE_UI_RS_NODE_H 17 18 #include <unordered_map> 19 20 #include "animation/rs_animation_timing_curve.h" 21 #include "animation/rs_animation_timing_protocol.h" 22 #include "animation/rs_motion_path_option.h" 23 #include "animation/rs_particle_params.h" 24 #include "animation/rs_transition_effect.h" 25 #include "command/rs_animation_command.h" 26 #include "common/rs_vector2.h" 27 #include "common/rs_vector4.h" 28 #include "modifier/rs_modifier_extractor.h" 29 #include "modifier/rs_modifier_type.h" 30 #include "modifier/rs_showing_properties_freezer.h" 31 #include "pipeline/rs_recording_canvas.h" 32 #include "property/rs_properties.h" 33 #include "render/rs_mask.h" 34 #include "render/rs_path.h" 35 36 #ifndef USE_ROSEN_DRAWING 37 class SkCanvas; 38 #else 39 #include "recording/recording_canvas.h" 40 #endif 41 42 namespace OHOS { 43 namespace Rosen { 44 #ifndef USE_ROSEN_DRAWING 45 using DrawFunc = std::function<void(std::shared_ptr<SkCanvas>)>; 46 #else 47 using DrawFunc = std::function<void(std::shared_ptr<Drawing::Canvas>)>; 48 #endif 49 using PropertyCallback = std::function<void()>; 50 class RSAnimation; 51 class RSCommand; 52 class RSImplicitAnimParam; 53 class RSImplicitAnimator; 54 class RSModifier; 55 56 class RSC_EXPORT RSNode : public std::enable_shared_from_this<RSNode> { 57 public: 58 using WeakPtr = std::weak_ptr<RSNode>; 59 using SharedPtr = std::shared_ptr<RSNode>; 60 static inline constexpr RSUINodeType Type = RSUINodeType::RS_NODE; GetType()61 virtual RSUINodeType GetType() const 62 { 63 return Type; 64 } 65 66 RSNode(const RSNode&) = delete; 67 RSNode(const RSNode&&) = delete; 68 RSNode& operator=(const RSNode&) = delete; 69 RSNode& operator=(const RSNode&&) = delete; 70 virtual ~RSNode(); 71 72 // this id is ONLY used in hierarchy operation commands, this may differ from id_ when the node is a proxy node. GetHierarchyCommandNodeId()73 virtual NodeId GetHierarchyCommandNodeId() const 74 { 75 return id_; 76 } 77 78 virtual void AddChild(SharedPtr child, int index = -1); 79 void MoveChild(SharedPtr child, int index); 80 virtual void RemoveChild(SharedPtr child); 81 void RemoveFromTree(); 82 virtual void ClearChildren(); GetChildren()83 const std::vector<NodeId>& GetChildren() const 84 { 85 return children_; 86 } 87 88 // Add/RemoveCrossParentChild only used as: the child is under multiple parents(e.g. a window cross multi-screens) 89 void AddCrossParentChild(SharedPtr child, int index); 90 void RemoveCrossParentChild(SharedPtr child, NodeId newParentId); 91 GetId()92 NodeId GetId() const 93 { 94 return id_; 95 } 96 GetFollowType()97 virtual FollowType GetFollowType() const 98 { 99 return FollowType::NONE; 100 } 101 102 bool IsInstanceOf(RSUINodeType type) const; 103 template<typename T> 104 RSC_EXPORT bool IsInstanceOf() const; 105 106 // type-safe reinterpret_cast 107 template<typename T> ReinterpretCast(const std::shared_ptr<RSNode> & node)108 static std::shared_ptr<T> ReinterpretCast(const std::shared_ptr<RSNode>& node) 109 { 110 return node ? node->ReinterpretCastTo<T>() : nullptr; 111 } 112 template<typename T> ReinterpretCastTo()113 std::shared_ptr<T> ReinterpretCastTo() 114 { 115 return (IsInstanceOf<T>()) ? std::static_pointer_cast<T>(shared_from_this()) : nullptr; 116 } 117 virtual std::string DumpNode(int depth) const; 118 SharedPtr GetParent(); 119 SetId(const NodeId & id)120 void SetId(const NodeId& id) 121 { 122 id_ = id; 123 } 124 125 bool IsUniRenderEnabled() const; 126 bool IsRenderServiceNode() const; 127 128 static std::vector<std::shared_ptr<RSAnimation>> Animate(const RSAnimationTimingProtocol& timingProtocol, 129 const RSAnimationTimingCurve& timingCurve, const PropertyCallback& callback, 130 const std::function<void()>& finishCallback = nullptr, const std::function<void()>& repeatCallback = nullptr); 131 132 static std::vector<std::shared_ptr<RSAnimation>> AnimateWithCurrentOptions( 133 const PropertyCallback& callback, const std::function<void()>& finishCallback, bool timingSensitive = true); 134 static std::vector<std::shared_ptr<RSAnimation>> AnimateWithCurrentCallback( 135 const RSAnimationTimingProtocol& timingProtocol, const RSAnimationTimingCurve& timingCurve, 136 const PropertyCallback& callback); 137 138 static void RegisterTransitionPair(NodeId inNodeId, NodeId outNodeId); 139 static void UnregisterTransitionPair(NodeId inNodeId, NodeId outNodeId); 140 141 static void OpenImplicitAnimation(const RSAnimationTimingProtocol& timingProtocol, 142 const RSAnimationTimingCurve& timingCurve, const std::function<void()>& finishCallback = nullptr); 143 static std::vector<std::shared_ptr<RSAnimation>> CloseImplicitAnimation(); 144 145 static void ExecuteWithoutAnimation( 146 const PropertyCallback& callback, std::shared_ptr<RSImplicitAnimator> implicitAnimator = nullptr); 147 148 static void AddKeyFrame( 149 float fraction, const RSAnimationTimingCurve& timingCurve, const PropertyCallback& callback); 150 static void AddKeyFrame(float fraction, const PropertyCallback& callback); 151 152 void NotifyTransition(const std::shared_ptr<const RSTransitionEffect>& effect, bool isTransitionIn); 153 154 void AddAnimation(const std::shared_ptr<RSAnimation>& animation); 155 void RemoveAllAnimations(); 156 void RemoveAnimation(const std::shared_ptr<RSAnimation>& animation); 157 void SetMotionPathOption(const std::shared_ptr<RSMotionPathOption>& motionPathOption); 158 const std::shared_ptr<RSMotionPathOption> GetMotionPathOption() const; 159 DrawOnNode(RSModifierType type,DrawFunc func)160 virtual void DrawOnNode(RSModifierType type, DrawFunc func) {} // [PLANNING]: support SurfaceNode 161 162 const RSModifierExtractor& GetStagingProperties() const; 163 const RSShowingPropertiesFreezer& GetShowingProperties() const; 164 165 template<typename ModifierName, typename PropertyName, typename T> 166 void SetProperty(RSModifierType modifierType, T value); 167 168 virtual void SetBounds(const Vector4f& bounds); 169 virtual void SetBounds(float positionX, float positionY, float width, float height); 170 virtual void SetBoundsWidth(float width); 171 virtual void SetBoundsHeight(float height); 172 173 virtual void SetFrame(const Vector4f& frame); 174 virtual void SetFrame(float positionX, float positionY, float width, float height); 175 virtual void SetFramePositionX(float positionX); 176 virtual void SetFramePositionY(float positionY); 177 178 // The property is valid only for CanvasNode and SurfaceNode in uniRender. 179 virtual void SetFreeze(bool isFreeze); 180 181 void SetSandBox(std::optional<Vector2f> parentPosition); 182 183 void SetPositionZ(float positionZ); 184 185 void SetPivot(const Vector2f& pivot); 186 void SetPivot(float pivotX, float pivotY); 187 void SetPivotX(float pivotX); 188 void SetPivotY(float pivotY); 189 void SetPivotZ(float pivotZ); 190 191 void SetCornerRadius(float cornerRadius); 192 void SetCornerRadius(const Vector4f& cornerRadius); 193 194 void SetRotation(const Quaternion& quaternion); 195 void SetRotation(float degreeX, float degreeY, float degreeZ); 196 void SetRotation(float degree); 197 void SetRotationX(float degree); 198 void SetRotationY(float degree); 199 void SetCameraDistance(float cameraDistance); 200 201 void SetTranslate(const Vector2f& translate); 202 void SetTranslate(float translateX, float translateY, float translateZ); 203 void SetTranslateX(float translate); 204 void SetTranslateY(float translate); 205 void SetTranslateZ(float translate); 206 207 void SetScale(float scale); 208 void SetScale(float scaleX, float scaleY); 209 void SetScale(const Vector2f& scale); 210 void SetScaleX(float scaleX); 211 void SetScaleY(float scaleY); 212 213 void SetAlpha(float alpha); 214 void SetAlphaOffscreen(bool alphaOffscreen); 215 216 void SetEnvForegroundColor(uint32_t colorValue); 217 void SetEnvForegroundColorStrategy(ForegroundColorStrategyType colorType); 218 void SetParticleParams( 219 std::vector<ParticleParams>& particleParams, const std::function<void()>& finishCallback = nullptr); 220 void SetParticleDrawRegion(std::vector<ParticleParams>& particleParams); 221 void SetForegroundColor(uint32_t colorValue); 222 void SetBackgroundColor(uint32_t colorValue); 223 void SetBackgroundShader(const std::shared_ptr<RSShader>& shader); 224 225 void SetBgImage(const std::shared_ptr<RSImage>& image); 226 void SetBgImageSize(float width, float height); 227 void SetBgImageWidth(float width); 228 void SetBgImageHeight(float height); 229 void SetBgImagePosition(float positionX, float positionY); 230 void SetBgImagePositionX(float positionX); 231 void SetBgImagePositionY(float positionY); 232 233 void SetBorderColor(uint32_t colorValue); 234 void SetBorderColor(uint32_t left, uint32_t top, uint32_t right, uint32_t bottom); 235 void SetBorderColor(const Vector4<Color>& color); 236 void SetBorderWidth(float width); 237 void SetBorderWidth(float left, float top, float right, float bottom); 238 void SetBorderWidth(const Vector4f& width); 239 void SetBorderStyle(uint32_t styleValue); 240 void SetBorderStyle(uint32_t left, uint32_t top, uint32_t right, uint32_t bottom); 241 void SetBorderStyle(const Vector4<BorderStyle>& style); 242 243 void SetBackgroundFilter(const std::shared_ptr<RSFilter>& backgroundFilter); 244 void SetFilter(const std::shared_ptr<RSFilter>& filter); 245 void SetLinearGradientBlurPara(const std::shared_ptr<RSLinearGradientBlurPara>& para); 246 void SetDynamicLightUpRate(const float rate); 247 void SetDynamicLightUpDegree(const float lightUpDegree); 248 void SetCompositingFilter(const std::shared_ptr<RSFilter>& compositingFilter); 249 250 void SetShadowColor(uint32_t colorValue); 251 void SetShadowOffset(float offsetX, float offsetY); 252 void SetShadowOffsetX(float offsetX); 253 void SetShadowOffsetY(float offsetY); 254 void SetShadowAlpha(float alpha); 255 void SetShadowElevation(float elevation); 256 void SetShadowRadius(float radius); 257 void SetShadowPath(const std::shared_ptr<RSPath>& shadowPath); 258 void SetShadowMask(bool shadowMask); 259 260 void SetFrameGravity(Gravity gravity); 261 262 void SetClipRRect(const Vector4f& clipRect, const Vector4f& clipRadius); 263 void SetClipBounds(const std::shared_ptr<RSPath>& clipToBounds); 264 void SetClipToBounds(bool clipToBounds); 265 void SetClipToFrame(bool clipToFrame); 266 267 void SetVisible(bool visible); 268 void SetMask(const std::shared_ptr<RSMask>& mask); 269 void SetSpherizeDegree(float spherizeDegree); 270 void SetLightUpEffectDegree(float LightUpEffectDegree); 271 272 void SetPixelStretch(const Vector4f& stretchSize); 273 void SetPixelStretchPercent(const Vector4f& stretchPercent); 274 275 void SetPaintOrder(bool drawContentLast); 276 SetTransitionEffect(const std::shared_ptr<const RSTransitionEffect> & effect)277 void SetTransitionEffect(const std::shared_ptr<const RSTransitionEffect>& effect) 278 { 279 transitionEffect_ = effect; 280 } 281 282 void SetUseEffect(bool useEffect); 283 284 // driven render 285 void MarkDrivenRender(bool flag); 286 void MarkDrivenRenderItemIndex(int index); 287 void MarkDrivenRenderFramePaintState(bool flag); 288 void MarkContentChanged(bool isChanged); 289 290 void AddModifier(const std::shared_ptr<RSModifier> modifier); 291 void RemoveModifier(const std::shared_ptr<RSModifier> modifier); 292 293 void SetIsCustomTextType(bool isCustomTextType); 294 295 bool GetIsCustomTextType(); 296 297 void SetDrawRegion(std::shared_ptr<RectF> rect); 298 299 // Mark preferentially draw node and childrens 300 void MarkNodeGroup(bool isNodeGroup); 301 302 void SetGrayScale(float grayScale); 303 304 void SetBrightness(float brightness); 305 306 void SetContrast(float contrast); 307 308 void SetSaturate(float saturate); 309 310 void SetSepia(float sepia); 311 312 void SetInvert(float invert); 313 314 void SetHueRotate(float hueRotate); 315 316 void SetColorBlend(uint32_t colorValue); 317 318 void UpdateFrameRateRange(FrameRateRange range); 319 GetFrameRateRange()320 FrameRateRange GetFrameRateRange() 321 { 322 return nodeRange_; 323 } 324 325 protected: 326 explicit RSNode(bool isRenderServiceNode); 327 explicit RSNode(bool isRenderServiceNode, NodeId id); 328 329 bool isRenderServiceNode_; 330 bool skipDestroyCommandInDestructor_ = false; 331 332 bool drawContentLast_ = false; 333 334 virtual void OnAddChildren(); 335 virtual void OnRemoveChildren(); 336 NeedForcedSendToRemote()337 virtual bool NeedForcedSendToRemote() const 338 { 339 return false; 340 } 341 342 std::vector<PropertyId> GetModifierIds() const; 343 bool isCustomTextType_ = false; 344 345 private: 346 static NodeId GenerateId(); 347 static void InitUniRenderEnabled(); 348 NodeId id_; 349 NodeId parent_ = 0; 350 std::vector<NodeId> children_; 351 void SetParent(NodeId parent); 352 void RemoveChildById(NodeId childId); 353 354 bool AnimationCallback(AnimationId animationId, AnimationCallbackEvent event); 355 bool HasPropertyAnimation(const PropertyId& id); 356 void FallbackAnimationsToRoot(); 357 void AddAnimationInner(const std::shared_ptr<RSAnimation>& animation); 358 void RemoveAnimationInner(const std::shared_ptr<RSAnimation>& animation); 359 void FinishAnimationByProperty(const PropertyId& id); 360 void CancelAnimationByProperty(const PropertyId& id); 361 const std::shared_ptr<RSModifier> GetModifier(const PropertyId& propertyId); OnBoundsSizeChanged()362 virtual void OnBoundsSizeChanged() const {}; 363 void UpdateModifierMotionPathOption(); 364 void MarkAllExtendModifierDirty(); 365 void ResetExtendModifierDirty(); 366 void UpdateImplicitAnimator(); 367 368 // Planning: refactor RSUIAnimationManager and remove this method 369 void ClearAllModifiers(); 370 371 pid_t implicitAnimatorTid_ = 0; 372 bool extendModifierIsDirty_ { false }; 373 // driven render 374 bool drivenFlag_ = false; 375 376 bool isNodeGroup_ = false; 377 378 RSModifierExtractor stagingPropertiesExtractor_; 379 RSShowingPropertiesFreezer showingPropertiesFreezer_; 380 std::unordered_map<PropertyId, std::shared_ptr<RSModifier>> modifiers_; 381 std::unordered_map<RSModifierType, std::shared_ptr<RSModifier>> propertyModifiers_; 382 std::shared_ptr<RectF> drawRegion_; 383 384 std::unordered_map<AnimationId, std::shared_ptr<RSAnimation>> animations_; 385 std::unordered_map<PropertyId, uint32_t> animatingPropertyNum_; 386 std::shared_ptr<RSMotionPathOption> motionPathOption_; 387 std::shared_ptr<RSImplicitAnimator> implicitAnimator_; 388 std::shared_ptr<const RSTransitionEffect> transitionEffect_; 389 390 FrameRateRange nodeRange_ = { 0, 0, 0 }; 391 std::mutex animationMutex_; 392 393 friend class RSAnimation; 394 friend class RSCurveAnimation; 395 friend class RSExtendedModifier; 396 friend class RSGeometryTransModifier; 397 friend class RSImplicitAnimator; 398 friend class RSInterpolatingSpringAnimation; 399 friend class RSKeyframeAnimation; 400 friend class RSModifier; 401 friend class RSModifierExtractor; 402 friend class RSPathAnimation; 403 friend class RSPropertyAnimation; 404 friend class RSPropertyBase; 405 friend class RSShowingPropertiesFreezer; 406 friend class RSSpringAnimation; 407 friend class RSTransition; 408 friend class RSUIDirector; 409 template<typename T> 410 friend class RSProperty; 411 template<typename T> 412 friend class RSAnimatableProperty; 413 }; 414 // backward compatibility 415 using RSBaseNode = RSNode; 416 } // namespace Rosen 417 } // namespace OHOS 418 419 #endif // RENDER_SERVICE_CLIENT_CORE_UI_RS_NODE_H 420