• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_transition_effect.h"
24 #include "common/rs_vector2.h"
25 #include "common/rs_vector4.h"
26 #include "modifier/rs_modifier_extractor.h"
27 #include "modifier/rs_modifier_type.h"
28 #include "pipeline/rs_recording_canvas.h"
29 #include "property/rs_properties.h"
30 #include "render/rs_mask.h"
31 #include "render/rs_path.h"
32 #include "ui/rs_base_node.h"
33 
34 class SkCanvas;
35 
36 namespace OHOS {
37 namespace Rosen {
38 using DrawFunc = std::function<void(std::shared_ptr<SkCanvas>)>;
39 using PropertyCallback = std::function<void()>;
40 class RSAnimation;
41 class RSCommand;
42 class RSImplicitAnimParam;
43 class RSImplicitAnimator;
44 class RSUIAnimationManager;
45 class RSModifier;
46 
47 class RSC_EXPORT RSNode : public RSBaseNode {
48 public:
49     using WeakPtr = std::weak_ptr<RSNode>;
50     using SharedPtr = std::shared_ptr<RSNode>;
51     static inline constexpr RSUINodeType Type = RSUINodeType::RS_NODE;
GetType()52     RSUINodeType GetType() const override
53     {
54         return Type;
55     }
56 
57     ~RSNode() override;
58     std::string DumpNode(int depth) const override;
59 
60     static std::vector<std::shared_ptr<RSAnimation>> Animate(const RSAnimationTimingProtocol& timingProtocol,
61         const RSAnimationTimingCurve& timingCurve, const PropertyCallback& callback,
62         const std::function<void()>& finishCallback = nullptr);
63     static void OpenImplicitAnimation(const RSAnimationTimingProtocol& timingProtocol,
64         const RSAnimationTimingCurve& timingCurve, const std::function<void()>& finishCallback = nullptr);
65     static std::vector<std::shared_ptr<RSAnimation>> CloseImplicitAnimation();
66     static void AddKeyFrame(
67         float fraction, const RSAnimationTimingCurve& timingCurve, const PropertyCallback& callback);
68     static void AddKeyFrame(float fraction, const PropertyCallback& callback);
69 
70     void NotifyTransition(const std::shared_ptr<const RSTransitionEffect>& effect, bool isTransitionIn);
71 
72     void AddAnimation(const std::shared_ptr<RSAnimation>& animation);
73     void RemoveAllAnimations();
74     void RemoveAnimation(const std::shared_ptr<RSAnimation>& animation);
75     void SetMotionPathOption(const std::shared_ptr<RSMotionPathOption>& motionPathOption);
76     const std::shared_ptr<RSMotionPathOption> GetMotionPathOption() const;
77 
DrawOnNode(RSModifierType type,DrawFunc func)78     virtual void DrawOnNode(RSModifierType type, DrawFunc func) {} // [PLANNING]: support SurfaceNode
79 
80     const RSModifierExtractor& GetStagingProperties() const;
81 
82     template<typename ModifierName, typename PropertyName, typename T>
83     void SetProperty(RSModifierType modifierType, T value);
84 
85     virtual void SetBounds(const Vector4f& bounds);
86     virtual void SetBounds(float positionX, float positionY, float width, float height);
87     virtual void SetBoundsWidth(float width);
88     virtual void SetBoundsHeight(float height);
89 
90     virtual void SetFrame(const Vector4f& frame);
91     virtual void SetFrame(float positionX, float positionY, float width, float height);
92     virtual void SetFramePositionX(float positionX);
93     virtual void SetFramePositionY(float positionY);
94 
95     void SetPositionZ(float positionZ);
96 
97     void SetPivot(const Vector2f& pivot);
98     void SetPivot(float pivotX, float pivotY);
99     void SetPivotX(float pivotX);
100     void SetPivotY(float pivotY);
101 
102     void SetCornerRadius(float cornerRadius);
103     void SetCornerRadius(const Vector4f& cornerRadius);
104 
105     void SetRotation(const Quaternion& quaternion);
106     void SetRotation(float degreeX, float degreeY, float degreeZ);
107     void SetRotation(float degree);
108     void SetRotationX(float degree);
109     void SetRotationY(float degree);
110 
111     void SetTranslate(const Vector2f& translate);
112     void SetTranslate(float translateX, float translateY, float translateZ);
113     void SetTranslateX(float translate);
114     void SetTranslateY(float translate);
115     void SetTranslateZ(float translate);
116 
117     void SetScale(float scale);
118     void SetScale(float scaleX, float scaleY);
119     void SetScale(const Vector2f& scale);
120     void SetScaleX(float scaleX);
121     void SetScaleY(float scaleY);
122 
123     void SetAlpha(float alpha);
124     void SetAlphaOffscreen(bool alphaOffscreen);
125 
126     void SetForegroundColor(uint32_t colorValue);
127     void SetBackgroundColor(uint32_t colorValue);
128     void SetBackgroundShader(const std::shared_ptr<RSShader>& shader);
129 
130     void SetBgImage(const std::shared_ptr<RSImage>& image);
131     void SetBgImageSize(float width, float height);
132     void SetBgImageWidth(float width);
133     void SetBgImageHeight(float height);
134     void SetBgImagePosition(float positionX, float positionY);
135     void SetBgImagePositionX(float positionX);
136     void SetBgImagePositionY(float positionY);
137 
138     void SetBorderColor(uint32_t colorValue);
139     void SetBorderColor(uint32_t left, uint32_t top, uint32_t right, uint32_t bottom);
140     void SetBorderColor(const Vector4<Color>& color);
141     void SetBorderWidth(float width);
142     void SetBorderWidth(float left, float top, float right, float bottom);
143     void SetBorderWidth(const Vector4f& width);
144     void SetBorderStyle(uint32_t styleValue);
145     void SetBorderStyle(uint32_t left, uint32_t top, uint32_t right, uint32_t bottom);
146     void SetBorderStyle(const Vector4<BorderStyle>& style);
147 
148     void SetBackgroundFilter(const std::shared_ptr<RSFilter>& backgroundFilter);
149     void SetFilter(const std::shared_ptr<RSFilter>& filter);
150     void SetCompositingFilter(const std::shared_ptr<RSFilter>& compositingFilter);
151 
152     void SetShadowColor(uint32_t colorValue);
153     void SetShadowOffset(float offsetX, float offsetY);
154     void SetShadowOffsetX(float offsetX);
155     void SetShadowOffsetY(float offsetY);
156     void SetShadowAlpha(float alpha);
157     void SetShadowElevation(float elevation);
158     void SetShadowRadius(float radius);
159     void SetShadowPath(const std::shared_ptr<RSPath>& shadowPath);
160 
161     void SetFrameGravity(Gravity gravity);
162 
163     void SetClipBounds(const std::shared_ptr<RSPath>& clipToBounds);
164     void SetClipToBounds(bool clipToBounds);
165     void SetClipToFrame(bool clipToFrame);
166 
167     void SetVisible(bool visible);
168     void SetMask(const std::shared_ptr<RSMask>& mask);
169 
170     void SetPaintOrder(bool drawContentLast);
171 
SetTransitionEffect(const std::shared_ptr<const RSTransitionEffect> & effect)172     void SetTransitionEffect(const std::shared_ptr<const RSTransitionEffect>& effect)
173     {
174         transitionEffect_ = effect;
175     }
176 
177     void AddModifier(const std::shared_ptr<RSModifier> modifier);
178     void RemoveModifier(const std::shared_ptr<RSModifier> modifier);
179 
180 protected:
181     explicit RSNode(bool isRenderServiceNode);
182     explicit RSNode(bool isRenderServiceNode, NodeId id);
183     RSNode(const RSNode&) = delete;
184     RSNode(const RSNode&&) = delete;
185     RSNode& operator=(const RSNode&) = delete;
186     RSNode& operator=(const RSNode&&) = delete;
187 
188     bool drawContentLast_ = false;
189 
190     void OnAddChildren() override;
191     void OnRemoveChildren() override;
192 
NeedForcedSendToRemote()193     virtual bool NeedForcedSendToRemote() const
194     {
195         return false;
196     }
197 
198     std::vector<PropertyId> GetModifierIds() const;
199 
200 private:
201     bool AnimationFinish(AnimationId animationId);
202     bool HasPropertyAnimation(const PropertyId& id);
203     void FallbackAnimationsToRoot();
204     void AddAnimationInner(const std::shared_ptr<RSAnimation>& animation);
205     void RemoveAnimationInner(const std::shared_ptr<RSAnimation>& animation);
206     void FinishAnimationByProperty(const PropertyId& id);
207     const std::shared_ptr<RSModifier> GetModifier(const PropertyId& propertyId);
OnBoundsSizeChanged()208     virtual void OnBoundsSizeChanged() const {};
209     void UpdateModifierMotionPathOption();
210     void UpdateExtendedModifier(const std::weak_ptr<RSModifier>& modifier);
211 
212     // Planning: refactor RSUIAnimationManager and remove this method
213     void ClearAllModifiers();
214 
215     std::unordered_map<AnimationId, std::shared_ptr<RSAnimation>> animations_;
216     std::unordered_map<PropertyId, uint32_t> animatingPropertyNum_;
217     std::unordered_map<PropertyId, std::shared_ptr<RSModifier>> modifiers_;
218     std::unordered_map<RSModifierType, std::shared_ptr<RSModifier>> propertyModifiers_;
219     std::shared_ptr<RSMotionPathOption> motionPathOption_;
220 
221     void UpdateImplicitAnimator();
222     pid_t implicitAnimatorTid_ = 0;
223     std::shared_ptr<RSImplicitAnimator> implicitAnimator_;
224     std::shared_ptr<const RSTransitionEffect> transitionEffect_;
225 
226     RSModifierExtractor stagingPropertiesExtractor_;
227 
228     friend class RSAnimation;
229     friend class RSCurveAnimation;
230     friend class RSKeyframeAnimation;
231     friend class RSPropertyAnimation;
232     friend class RSSpringAnimation;
233     template<typename T>
234     friend class RSProperty;
235     template<typename T>
236     friend class RSAnimatableProperty;
237     friend class RSPathAnimation;
238     friend class RSExtendedModifier;
239     friend class RSTransition;
240     friend class RSUIDirector;
241     friend class RSImplicitAnimator;
242     friend class RSModifierExtractor;
243 };
244 } // namespace Rosen
245 } // namespace OHOS
246 
247 #endif // RENDER_SERVICE_CLIENT_CORE_UI_RS_NODE_H
248