• 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 <optional>
19 #include <unordered_map>
20 
21 #include "animation/rs_animation_timing_curve.h"
22 #include "animation/rs_animation_timing_protocol.h"
23 #include "animation/rs_motion_path_option.h"
24 #include "animation/rs_particle_params.h"
25 #include "animation/rs_transition_effect.h"
26 #include "command/rs_animation_command.h"
27 #include "common/rs_vector2.h"
28 #include "common/rs_vector4.h"
29 #include "modifier/rs_modifier_extractor.h"
30 #include "modifier/rs_modifier_type.h"
31 #include "modifier/rs_showing_properties_freezer.h"
32 #include "pipeline/rs_recording_canvas.h"
33 #include "property/rs_properties.h"
34 #include "render/rs_mask.h"
35 #include "render/rs_path.h"
36 
37 #ifndef USE_ROSEN_DRAWING
38 class SkCanvas;
39 #else
40 #include "recording/recording_canvas.h"
41 #endif
42 
43 namespace OHOS {
44 namespace Rosen {
45 #ifndef USE_ROSEN_DRAWING
46 using DrawFunc = std::function<void(std::shared_ptr<SkCanvas>)>;
47 #else
48 using DrawFunc = std::function<void(std::shared_ptr<Drawing::Canvas>)>;
49 #endif
50 using PropertyCallback = std::function<void()>;
51 class RSAnimation;
52 class RSCommand;
53 class RSImplicitAnimParam;
54 class RSImplicitAnimator;
55 class RSModifier;
56 
57 class RSC_EXPORT RSNode : public std::enable_shared_from_this<RSNode> {
58 public:
59     using WeakPtr = std::weak_ptr<RSNode>;
60     using SharedPtr = std::shared_ptr<RSNode>;
61     static inline constexpr RSUINodeType Type = RSUINodeType::RS_NODE;
GetType()62     virtual RSUINodeType GetType() const
63     {
64         return Type;
65     }
66 
67     RSNode(const RSNode&) = delete;
68     RSNode(const RSNode&&) = delete;
69     RSNode& operator=(const RSNode&) = delete;
70     RSNode& operator=(const RSNode&&) = delete;
71     virtual ~RSNode();
72 
73     // this id is ONLY used in hierarchy operation commands, this may differ from id_ when the node is a proxy node.
GetHierarchyCommandNodeId()74     virtual NodeId GetHierarchyCommandNodeId() const
75     {
76         return id_;
77     }
78 
79     virtual void AddChild(SharedPtr child, int index = -1);
80     void MoveChild(SharedPtr child, int index);
81     virtual void RemoveChild(SharedPtr child);
82     void RemoveChildByNodeId(NodeId childId);
83     void RemoveFromTree();
84     virtual void ClearChildren();
GetChildren()85     const std::vector<NodeId>& GetChildren() const
86     {
87         return children_;
88     }
89     // ONLY support index in [0, childrenTotal) or index = -1, otherwise return std::nullopt
90     const std::optional<NodeId> GetChildIdByIndex(int index) const;
91 
92     // Add/RemoveCrossParentChild only used as: the child is under multiple parents(e.g. a window cross multi-screens)
93     void AddCrossParentChild(SharedPtr child, int index);
94     void RemoveCrossParentChild(SharedPtr child, NodeId newParentId);
95 
GetId()96     NodeId GetId() const
97     {
98         return id_;
99     }
100 
GetFollowType()101     virtual FollowType GetFollowType() const
102     {
103         return FollowType::NONE;
104     }
105 
106     bool IsInstanceOf(RSUINodeType type) const;
107     template<typename T>
108     RSC_EXPORT bool IsInstanceOf() const;
109 
110     // type-safe reinterpret_cast
111     template<typename T>
ReinterpretCast(const std::shared_ptr<RSNode> & node)112     static std::shared_ptr<T> ReinterpretCast(const std::shared_ptr<RSNode>& node)
113     {
114         return node ? node->ReinterpretCastTo<T>() : nullptr;
115     }
116     template<typename T>
ReinterpretCastTo()117     std::shared_ptr<T> ReinterpretCastTo()
118     {
119         return (IsInstanceOf<T>()) ? std::static_pointer_cast<T>(shared_from_this()) : nullptr;
120     }
121     virtual std::string DumpNode(int depth) const;
122     SharedPtr GetParent();
123 
SetId(const NodeId & id)124     void SetId(const NodeId& id)
125     {
126         id_ = id;
127     }
128 
129     bool IsUniRenderEnabled() const;
130     bool IsRenderServiceNode() const;
131 
132     static std::vector<std::shared_ptr<RSAnimation>> Animate(const RSAnimationTimingProtocol& timingProtocol,
133         const RSAnimationTimingCurve& timingCurve, const PropertyCallback& callback,
134         const std::function<void()>& finishCallback = nullptr, const std::function<void()>& repeatCallback = nullptr);
135 
136     static std::vector<std::shared_ptr<RSAnimation>> AnimateWithCurrentOptions(
137         const PropertyCallback& callback, const std::function<void()>& finishCallback, bool timingSensitive = true);
138     static std::vector<std::shared_ptr<RSAnimation>> AnimateWithCurrentCallback(
139         const RSAnimationTimingProtocol& timingProtocol, const RSAnimationTimingCurve& timingCurve,
140         const PropertyCallback& callback);
141 
142     static void RegisterTransitionPair(NodeId inNodeId, NodeId outNodeId);
143     static void UnregisterTransitionPair(NodeId inNodeId, NodeId outNodeId);
144 
145     static void OpenImplicitAnimation(const RSAnimationTimingProtocol& timingProtocol,
146         const RSAnimationTimingCurve& timingCurve, const std::function<void()>& finishCallback = nullptr);
147     static std::vector<std::shared_ptr<RSAnimation>> CloseImplicitAnimation();
148     static bool IsImplicitAnimationOpen();
149 
150     static void ExecuteWithoutAnimation(
151         const PropertyCallback& callback, std::shared_ptr<RSImplicitAnimator> implicitAnimator = nullptr);
152 
153     static void AddKeyFrame(
154         float fraction, const RSAnimationTimingCurve& timingCurve, const PropertyCallback& callback);
155     static void AddKeyFrame(float fraction, const PropertyCallback& callback);
156     static void AddDurationKeyFrame(
157         int duration, const RSAnimationTimingCurve& timingCurve, const PropertyCallback& callback);
158 
159     void NotifyTransition(const std::shared_ptr<const RSTransitionEffect>& effect, bool isTransitionIn);
160 
161     void AddAnimation(const std::shared_ptr<RSAnimation>& animation);
162     void RemoveAllAnimations();
163     void RemoveAnimation(const std::shared_ptr<RSAnimation>& animation);
164     void SetMotionPathOption(const std::shared_ptr<RSMotionPathOption>& motionPathOption);
165     const std::shared_ptr<RSMotionPathOption> GetMotionPathOption() const;
166 
DrawOnNode(RSModifierType type,DrawFunc func)167     virtual void DrawOnNode(RSModifierType type, DrawFunc func) {} // [PLANNING]: support SurfaceNode
168 
169     const RSModifierExtractor& GetStagingProperties() const;
170     const RSShowingPropertiesFreezer& GetShowingProperties() const;
171 
172     template<typename ModifierName, typename PropertyName, typename T>
173     void SetProperty(RSModifierType modifierType, T value);
174 
175     virtual void SetBounds(const Vector4f& bounds);
176     virtual void SetBounds(float positionX, float positionY, float width, float height);
177     virtual void SetBoundsWidth(float width);
178     virtual void SetBoundsHeight(float height);
179 
180     virtual void SetFrame(const Vector4f& frame);
181     virtual void SetFrame(float positionX, float positionY, float width, float height);
182     virtual void SetFramePositionX(float positionX);
183     virtual void SetFramePositionY(float positionY);
184 
185     // The property is valid only for CanvasNode and SurfaceNode in uniRender.
186     virtual void SetFreeze(bool isFreeze);
187 
188     void SetSandBox(std::optional<Vector2f> parentPosition);
189 
190     void SetPositionZ(float positionZ);
191 
192     void SetPivot(const Vector2f& pivot);
193     void SetPivot(float pivotX, float pivotY);
194     void SetPivotX(float pivotX);
195     void SetPivotY(float pivotY);
196     void SetPivotZ(float pivotZ);
197 
198     void SetCornerRadius(float cornerRadius);
199     void SetCornerRadius(const Vector4f& cornerRadius);
200 
201     void SetRotation(const Quaternion& quaternion);
202     void SetRotation(float degreeX, float degreeY, float degreeZ);
203     void SetRotation(float degree);
204     void SetRotationX(float degree);
205     void SetRotationY(float degree);
206     void SetCameraDistance(float cameraDistance);
207 
208     void SetTranslate(const Vector2f& translate);
209     void SetTranslate(float translateX, float translateY, float translateZ);
210     void SetTranslateX(float translate);
211     void SetTranslateY(float translate);
212     void SetTranslateZ(float translate);
213 
214     void SetScale(float scale);
215     void SetScale(float scaleX, float scaleY);
216     void SetScale(const Vector2f& scale);
217     void SetScaleX(float scaleX);
218     void SetScaleY(float scaleY);
219 
220     void SetAlpha(float alpha);
221     void SetAlphaOffscreen(bool alphaOffscreen);
222 
223     void SetEnvForegroundColor(uint32_t colorValue);
224     void SetEnvForegroundColorStrategy(ForegroundColorStrategyType colorType);
225     void SetParticleParams(
226         std::vector<ParticleParams>& particleParams, const std::function<void()>& finishCallback = nullptr);
227     void SetParticleDrawRegion(std::vector<ParticleParams>& particleParams);
228     void SetForegroundColor(uint32_t colorValue);
229     void SetBackgroundColor(uint32_t colorValue);
230     void SetBackgroundShader(const std::shared_ptr<RSShader>& shader);
231 
232     void SetBgImage(const std::shared_ptr<RSImage>& image);
233     void SetBgImageSize(float width, float height);
234     void SetBgImageWidth(float width);
235     void SetBgImageHeight(float height);
236     void SetBgImagePosition(float positionX, float positionY);
237     void SetBgImagePositionX(float positionX);
238     void SetBgImagePositionY(float positionY);
239 
240     void SetBorderColor(uint32_t colorValue);
241     void SetBorderColor(uint32_t left, uint32_t top, uint32_t right, uint32_t bottom);
242     void SetBorderColor(const Vector4<Color>& color);
243     void SetBorderWidth(float width);
244     void SetBorderWidth(float left, float top, float right, float bottom);
245     void SetBorderWidth(const Vector4f& width);
246     void SetBorderStyle(uint32_t styleValue);
247     void SetBorderStyle(uint32_t left, uint32_t top, uint32_t right, uint32_t bottom);
248     void SetBorderStyle(const Vector4<BorderStyle>& style);
249     void SetOuterBorderColor(const Vector4<Color>& color);
250     void SetOuterBorderWidth(const Vector4f& width);
251     void SetOuterBorderStyle(const Vector4<BorderStyle>& style);
252     void SetOuterBorderRadius(const Vector4f& radius);
253     void SetOutlineColor(const Vector4<Color>& color);
254     void SetOutlineWidth(const Vector4f& width);
255     void SetOutlineStyle(const Vector4<BorderStyle>& style);
256     void SetOutlineRadius(const Vector4f& radius);
257 
258     void SetBackgroundFilter(const std::shared_ptr<RSFilter>& backgroundFilter);
259     void SetFilter(const std::shared_ptr<RSFilter>& filter);
260     void SetLinearGradientBlurPara(const std::shared_ptr<RSLinearGradientBlurPara>& para);
261     void SetDynamicLightUpRate(const float rate);
262     void SetDynamicLightUpDegree(const float lightUpDegree);
263     void SetGreyCoef1(const float greyCoef1);
264     void SetGreyCoef2(const float greyCoef2);
265     void SetCompositingFilter(const std::shared_ptr<RSFilter>& compositingFilter);
266 
267     void SetShadowColor(uint32_t colorValue);
268     void SetShadowOffset(float offsetX, float offsetY);
269     void SetShadowOffsetX(float offsetX);
270     void SetShadowOffsetY(float offsetY);
271     void SetShadowAlpha(float alpha);
272     void SetShadowElevation(float elevation);
273     void SetShadowRadius(float radius);
274     void SetShadowPath(const std::shared_ptr<RSPath>& shadowPath);
275     void SetShadowMask(bool shadowMask);
276     void SetShadowIsFilled(bool shadowIsFilled);
277     void SetShadowColorStrategy(int shadowColorStrategy);
278 
279     void SetFrameGravity(Gravity gravity);
280 
281     void SetClipRRect(const Vector4f& clipRect, const Vector4f& clipRadius);
282     void SetClipBounds(const std::shared_ptr<RSPath>& clipToBounds);
283     void SetClipToBounds(bool clipToBounds);
284     void SetClipToFrame(bool clipToFrame);
285 
286     void SetVisible(bool visible);
287     void SetMask(const std::shared_ptr<RSMask>& mask);
288     void SetSpherizeDegree(float spherizeDegree);
289     void SetLightUpEffectDegree(float LightUpEffectDegree);
290 
291     void SetPixelStretch(const Vector4f& stretchSize);
292     void SetPixelStretchPercent(const Vector4f& stretchPercent);
293 
294     void SetPaintOrder(bool drawContentLast);
295 
SetTransitionEffect(const std::shared_ptr<const RSTransitionEffect> & effect)296     void SetTransitionEffect(const std::shared_ptr<const RSTransitionEffect>& effect)
297     {
298         transitionEffect_ = effect;
299     }
300 
301     void SetUseEffect(bool useEffect);
302 
303     void SetUseShadowBatching(bool useShadowBatching);
304 
305     void SetColorBlendMode(RSColorBlendMode colorBlendMode);
306 
307     void SetColorBlendApplyType(RSColorBlendApplyType colorBlendApplyType);
308 
309     // driven render
310     void MarkDrivenRender(bool flag);
311     void MarkDrivenRenderItemIndex(int index);
312     void MarkDrivenRenderFramePaintState(bool flag);
313     void MarkContentChanged(bool isChanged);
314 
315     void AddModifier(const std::shared_ptr<RSModifier> modifier);
316     void RemoveModifier(const std::shared_ptr<RSModifier> modifier);
317 
318     void SetIsCustomTextType(bool isCustomTextType);
319 
320     bool GetIsCustomTextType();
321 
322     void SetDrawRegion(std::shared_ptr<RectF> rect);
323 
324     // Mark preferentially draw node and childrens
325     void MarkNodeGroup(bool isNodeGroup, bool isForced = true, bool includeProperty = false);
326 
327     void MarkNodeSingleFrameComposer(bool isNodeSingleFrameComposer);
328 
329     void SetGrayScale(float grayScale);
330 
331     void SetLightIntensity(float lightIntensity);
332 
333     void SetLightPosition(const Vector4f& lightPosition);
334 
335     void SetLightPosition(float positionX, float positionY, float positionZ);
336 
337     void SetIlluminatedBorderWidth(float illuminatedBorderWidth);
338 
339     void SetIlluminatedType(uint32_t illuminatedType);
340 
341     void SetBloom(float bloomIntensity);
342 
343     void SetBrightness(float brightness);
344 
345     void SetContrast(float contrast);
346 
347     void SetSaturate(float saturate);
348 
349     void SetSepia(float sepia);
350 
351     void SetInvert(float invert);
352 
353     void SetAiInvert(const Vector4f& aiInvert);
354 
355     void SetSystemBarEffect();
356 
357     void SetHueRotate(float hueRotate);
358 
359     void SetColorBlend(uint32_t colorValue);
360 
361     int32_t CalcExpectedFrameRate(const std::string& scene, float speed);
362 
363     void SetOutOfParent(OutOfParentType outOfParent);
364 
365     void SetFrameNodeInfo(int32_t id, std::string tag);
366 
367     int32_t GetFrameNodeId();
368 
369     std::string GetFrameNodeTag();
370 
371     using BoundsChangedCallback = std::function<void (const Rosen::Vector4f&)>;
SetBoundsChangedCallback(BoundsChangedCallback callback)372     virtual void SetBoundsChangedCallback(BoundsChangedCallback callback){};
IsTextureExportNode()373     bool IsTextureExportNode() const
374     {
375         return isTextureExportNode_;
376     }
377 
378     // key: symbolSpanID, value:symbol animation node list
379     std::unordered_map<uint64_t, std::list<SharedPtr>> canvasNodesListMap;
380 protected:
381     explicit RSNode(bool isRenderServiceNode, bool isTextureExportNode = false);
382     explicit RSNode(bool isRenderServiceNode, NodeId id, bool isTextureExportNode = false);
383 
384     bool isRenderServiceNode_;
385     bool isTextureExportNode_ = false;
386     bool skipDestroyCommandInDestructor_ = false;
387 
388     bool drawContentLast_ = false;
389 
390     virtual void OnAddChildren();
391     virtual void OnRemoveChildren();
392 
NeedForcedSendToRemote()393     virtual bool NeedForcedSendToRemote() const
394     {
395         return false;
396     }
397 
398     void DoFlushModifier();
399 
400     std::vector<PropertyId> GetModifierIds() const;
401     bool isCustomTextType_ = false;
402 
GetPropertyMutex()403     std::recursive_mutex& GetPropertyMutex()
404     {
405         return propertyMutex_;
406     }
407 private:
408     static NodeId GenerateId();
409     static void InitUniRenderEnabled();
410     NodeId id_;
411     NodeId parent_ = 0;
412     int32_t frameNodeId_ = -1;
413     std::string frameNodeTag_;
414     std::vector<NodeId> children_;
415     void SetParent(NodeId parent);
416     void RemoveChildById(NodeId childId);
417 
418     bool AnimationCallback(AnimationId animationId, AnimationCallbackEvent event);
419     bool HasPropertyAnimation(const PropertyId& id);
420     void FallbackAnimationsToRoot();
421     void AddAnimationInner(const std::shared_ptr<RSAnimation>& animation);
422     void FinishAnimationByProperty(const PropertyId& id);
423     void RemoveAnimationInner(const std::shared_ptr<RSAnimation>& animation);
424     void CancelAnimationByProperty(const PropertyId& id, const bool needForceSync = false);
425     const std::shared_ptr<RSModifier> GetModifier(const PropertyId& propertyId);
OnBoundsSizeChanged()426     virtual void OnBoundsSizeChanged() const {};
427     void UpdateModifierMotionPathOption();
428     void MarkAllExtendModifierDirty();
429     void ResetExtendModifierDirty();
430     void UpdateImplicitAnimator();
431 
432     // Planning: refactor RSUIAnimationManager and remove this method
433     void ClearAllModifiers();
434 
435     pid_t implicitAnimatorTid_ = 0;
436     bool extendModifierIsDirty_ { false };
437     // driven render
438     bool drivenFlag_ = false;
439 
440     bool isNodeGroup_ = false;
441 
442     bool isNodeSingleFrameComposer_ = false;
443 
444     RSModifierExtractor stagingPropertiesExtractor_;
445     RSShowingPropertiesFreezer showingPropertiesFreezer_;
446     std::unordered_map<PropertyId, std::shared_ptr<RSModifier>> modifiers_;
447     std::unordered_map<RSModifierType, std::shared_ptr<RSModifier>> propertyModifiers_;
448     std::shared_ptr<RectF> drawRegion_;
449     OutOfParentType outOfParent_ = OutOfParentType::UNKNOWN;
450 
451     std::unordered_map<AnimationId, std::shared_ptr<RSAnimation>> animations_;
452     std::unordered_map<PropertyId, uint32_t> animatingPropertyNum_;
453     std::shared_ptr<RSMotionPathOption> motionPathOption_;
454     std::shared_ptr<RSImplicitAnimator> implicitAnimator_;
455     std::shared_ptr<const RSTransitionEffect> transitionEffect_;
456 
457     std::mutex animationMutex_;
458     std::recursive_mutex propertyMutex_;
459 
460     friend class RSUIDirector;
461     friend class RSTransition;
462     friend class RSSpringAnimation;
463     friend class RSShowingPropertiesFreezer;
464     friend class RSPropertyBase;
465     friend class RSPropertyAnimation;
466     friend class RSPathAnimation;
467     friend class RSModifierExtractor;
468     friend class RSModifier;
469     friend class RSKeyframeAnimation;
470     friend class RSInterpolatingSpringAnimation;
471     friend class RSImplicitCancelAnimationParam;
472     friend class RSImplicitAnimator;
473     friend class RSGeometryTransModifier;
474     friend class RSExtendedModifier;
475     friend class RSCurveAnimation;
476     friend class RSAnimation;
477     template<typename T>
478     friend class RSProperty;
479     template<typename T>
480     friend class RSAnimatableProperty;
481 };
482 // backward compatibility
483 using RSBaseNode = RSNode;
484 } // namespace Rosen
485 } // namespace OHOS
486 
487 #endif // RENDER_SERVICE_CLIENT_CORE_UI_RS_NODE_H
488