• 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 
16 #include "ui/rs_node.h"
17 
18 #include <algorithm>
19 #include <memory>
20 #include <sstream>
21 #include <string>
22 
23 #include "sandbox_utils.h"
24 
25 #include "animation/rs_animation.h"
26 #include "animation/rs_animation_group.h"
27 #include "animation/rs_animation_callback.h"
28 #include "animation/rs_implicit_animator.h"
29 #include "animation/rs_implicit_animator_map.h"
30 #include "animation/rs_render_particle_animation.h"
31 #include "command/rs_base_node_command.h"
32 #include "command/rs_node_command.h"
33 #include "common/rs_color.h"
34 #include "common/rs_common_def.h"
35 #include "common/rs_obj_geometry.h"
36 #include "common/rs_vector4.h"
37 #include "modifier/rs_modifier.h"
38 #include "modifier/rs_property.h"
39 #include "modifier/rs_property_modifier.h"
40 #include "pipeline/rs_node_map.h"
41 #include "platform/common/rs_log.h"
42 #include "render/rs_path.h"
43 #include "transaction/rs_transaction_proxy.h"
44 #include "ui/rs_canvas_drawing_node.h"
45 #include "ui/rs_canvas_node.h"
46 #include "ui/rs_display_node.h"
47 #include "ui/rs_frame_rate_policy.h"
48 #include "ui/rs_node.h"
49 #include "ui/rs_proxy_node.h"
50 #include "ui/rs_root_node.h"
51 #include "ui/rs_surface_node.h"
52 
53 #ifdef _WIN32
54 #include <windows.h>
55 #define gettid GetCurrentThreadId
56 #endif
57 
58 #ifdef __APPLE__
59 #define gettid getpid
60 #endif
61 
62 #ifdef __gnu_linux__
63 #include <sys/types.h>
64 #include <sys/syscall.h>
65 #define gettid []() -> int32_t { return static_cast<int32_t>(syscall(SYS_gettid)); }
66 #endif
67 
68 namespace OHOS {
69 namespace Rosen {
70 namespace {
71 static bool g_isUniRenderEnabled = false;
72 static const std::unordered_map<RSUINodeType, std::string> RSUINodeTypeStrs = {
73     {RSUINodeType::UNKNOW,              "UNKNOW"},
74     {RSUINodeType::DISPLAY_NODE,        "DisplayNode"},
75     {RSUINodeType::RS_NODE,             "RsNode"},
76     {RSUINodeType::SURFACE_NODE,        "SurfaceNode"},
77     {RSUINodeType::PROXY_NODE,          "ProxyNode"},
78     {RSUINodeType::CANVAS_NODE,         "CanvasNode"},
79     {RSUINodeType::ROOT_NODE,           "RootNode"},
80     {RSUINodeType::EFFECT_NODE,         "EffectNode"},
81     {RSUINodeType::CANVAS_DRAWING_NODE, "CanvasDrawingNode"},
82 };
83 std::once_flag flag_;
IsPathAnimatableModifier(const RSModifierType & type)84 bool IsPathAnimatableModifier(const RSModifierType& type)
85 {
86     if (type == RSModifierType::BOUNDS || type == RSModifierType::FRAME || type == RSModifierType::TRANSLATE) {
87         return true;
88     }
89     return false;
90 }
91 }
92 
RSNode(bool isRenderServiceNode,NodeId id,bool isTextureExportNode)93 RSNode::RSNode(bool isRenderServiceNode, NodeId id, bool isTextureExportNode)
94     : isRenderServiceNode_(isRenderServiceNode), isTextureExportNode_(isTextureExportNode),
95     id_(id), stagingPropertiesExtractor_(id), showingPropertiesFreezer_(id)
96 {
97     InitUniRenderEnabled();
98     if (g_isUniRenderEnabled && isTextureExportNode) {
99         std::call_once(flag_, []() {
100             auto renderThreadClient = RSIRenderClient::CreateRenderThreadClient();
101             auto transactionProxy = RSTransactionProxy::GetInstance();
102             if (transactionProxy != nullptr) {
103                 transactionProxy->SetRenderThreadClient(renderThreadClient);
104             }
105         });
106     }
107     UpdateImplicitAnimator();
108 }
109 
RSNode(bool isRenderServiceNode,bool isTextureExportNode)110 RSNode::RSNode(bool isRenderServiceNode, bool isTextureExportNode)
111     : RSNode(isRenderServiceNode, GenerateId(), isTextureExportNode) {}
112 
~RSNode()113 RSNode::~RSNode()
114 {
115     FallbackAnimationsToRoot();
116     ClearAllModifiers();
117 
118     // break current (ui) parent-child relationship.
119     // render nodes will check if its child is expired and remove it, no need to manually remove it here.
120     if (auto parentPtr = RSNodeMap::Instance().GetNode(parent_)) {
121         parentPtr->RemoveChildById(id_);
122     }
123     // unregister node from node map
124     RSNodeMap::MutableInstance().UnregisterNode(id_);
125 
126     // tell RT/RS to destroy related render node
127     auto transactionProxy = RSTransactionProxy::GetInstance();
128     if (transactionProxy == nullptr || skipDestroyCommandInDestructor_) {
129         return;
130     }
131     std::unique_ptr<RSCommand> command = std::make_unique<RSBaseNodeDestroy>(id_);
132     transactionProxy->AddCommand(command, IsRenderServiceNode());
133 }
134 
OpenImplicitAnimation(const RSAnimationTimingProtocol & timingProtocol,const RSAnimationTimingCurve & timingCurve,const std::function<void ()> & finishCallback)135 void RSNode::OpenImplicitAnimation(const RSAnimationTimingProtocol& timingProtocol,
136     const RSAnimationTimingCurve& timingCurve, const std::function<void()>& finishCallback)
137 {
138     auto implicitAnimator = RSImplicitAnimatorMap::Instance().GetAnimator(gettid());
139     if (implicitAnimator == nullptr) {
140         ROSEN_LOGE("Failed to open implicit animation, implicit animator is null!");
141         return;
142     }
143 
144     std::shared_ptr<AnimationFinishCallback> animationFinishCallback;
145     if (finishCallback != nullptr) {
146         animationFinishCallback =
147             std::make_shared<AnimationFinishCallback>(finishCallback, timingProtocol.GetFinishCallbackType());
148     }
149     implicitAnimator->OpenImplicitAnimation(timingProtocol, timingCurve, std::move(animationFinishCallback));
150 }
151 
CloseImplicitAnimation()152 std::vector<std::shared_ptr<RSAnimation>> RSNode::CloseImplicitAnimation()
153 {
154     auto implicitAnimator = RSImplicitAnimatorMap::Instance().GetAnimator(gettid());
155     if (implicitAnimator == nullptr) {
156         ROSEN_LOGE("Failed to close implicit animation, implicit animator is null!");
157         return {};
158     }
159 
160     return implicitAnimator->CloseImplicitAnimation();
161 }
162 
SetFrameNodeInfo(int32_t id,std::string tag)163 void RSNode::SetFrameNodeInfo(int32_t id, std::string tag)
164 {
165     frameNodeId_ = id;
166     frameNodeTag_ = tag;
167 }
168 
GetFrameNodeId()169 int32_t RSNode::GetFrameNodeId()
170 {
171     return frameNodeId_;
172 }
173 
GetFrameNodeTag()174 std::string RSNode::GetFrameNodeTag()
175 {
176     return frameNodeTag_;
177 }
178 
AddKeyFrame(float fraction,const RSAnimationTimingCurve & timingCurve,const PropertyCallback & propertyCallback)179 void RSNode::AddKeyFrame(
180     float fraction, const RSAnimationTimingCurve& timingCurve, const PropertyCallback& propertyCallback)
181 {
182     auto implicitAnimator = RSImplicitAnimatorMap::Instance().GetAnimator(gettid());
183     if (implicitAnimator == nullptr) {
184         ROSEN_LOGE("Failed to add keyframe, implicit animator is null!");
185         return;
186     }
187 
188     implicitAnimator->BeginImplicitKeyFrameAnimation(fraction, timingCurve);
189     propertyCallback();
190     implicitAnimator->EndImplicitKeyFrameAnimation();
191 }
192 
AddKeyFrame(float fraction,const PropertyCallback & propertyCallback)193 void RSNode::AddKeyFrame(float fraction, const PropertyCallback& propertyCallback)
194 {
195     auto implicitAnimator = RSImplicitAnimatorMap::Instance().GetAnimator(gettid());
196     if (implicitAnimator == nullptr) {
197         ROSEN_LOGE("Failed to add keyframe, implicit animator is null!");
198         return;
199     }
200 
201     implicitAnimator->BeginImplicitKeyFrameAnimation(fraction);
202     propertyCallback();
203     implicitAnimator->EndImplicitKeyFrameAnimation();
204 }
205 
AddDurationKeyFrame(int duration,const RSAnimationTimingCurve & timingCurve,const PropertyCallback & propertyCallback)206 void RSNode::AddDurationKeyFrame(
207     int duration, const RSAnimationTimingCurve& timingCurve, const PropertyCallback& propertyCallback)
208 {
209     auto implicitAnimator = RSImplicitAnimatorMap::Instance().GetAnimator(gettid());
210     if (implicitAnimator == nullptr) {
211         ROSEN_LOGE("Failed to add keyframe, implicit animator is null!");
212         return;
213     }
214 
215     implicitAnimator->BeginImplicitDurationKeyFrameAnimation(duration, timingCurve);
216     propertyCallback();
217     implicitAnimator->EndImplicitDurationKeyFrameAnimation();
218 }
219 
IsImplicitAnimationOpen()220 bool RSNode::IsImplicitAnimationOpen()
221 {
222     auto implicitAnimator = RSImplicitAnimatorMap::Instance().GetAnimator(gettid());
223     return implicitAnimator && implicitAnimator->NeedImplicitAnimation();
224 }
225 
Animate(const RSAnimationTimingProtocol & timingProtocol,const RSAnimationTimingCurve & timingCurve,const PropertyCallback & propertyCallback,const std::function<void ()> & finishCallback,const std::function<void ()> & repeatCallback)226 std::vector<std::shared_ptr<RSAnimation>> RSNode::Animate(const RSAnimationTimingProtocol& timingProtocol,
227     const RSAnimationTimingCurve& timingCurve, const PropertyCallback& propertyCallback,
228     const std::function<void()>& finishCallback, const std::function<void()>& repeatCallback)
229 {
230     if (propertyCallback == nullptr) {
231         ROSEN_LOGE("Failed to add curve animation, property callback is null!");
232         return {};
233     }
234 
235     auto implicitAnimator = RSImplicitAnimatorMap::Instance().GetAnimator(gettid());
236     if (implicitAnimator == nullptr) {
237         ROSEN_LOGE("Failed to open implicit animation, implicit animator is null!");
238         return {};
239     }
240     std::shared_ptr<AnimationFinishCallback> animationFinishCallback;
241     if (finishCallback != nullptr) {
242         animationFinishCallback =
243             std::make_shared<AnimationFinishCallback>(finishCallback, timingProtocol.GetFinishCallbackType());
244     }
245     std::shared_ptr<AnimationRepeatCallback> animationRepeatCallback;
246     if (repeatCallback != nullptr) {
247         animationRepeatCallback = std::make_shared<AnimationRepeatCallback>(repeatCallback);
248     }
249     implicitAnimator->OpenImplicitAnimation(
250         timingProtocol, timingCurve, std::move(animationFinishCallback), std::move(animationRepeatCallback));
251     propertyCallback();
252     return implicitAnimator->CloseImplicitAnimation();
253 }
254 
AnimateWithCurrentOptions(const PropertyCallback & propertyCallback,const std::function<void ()> & finishCallback,bool timingSensitive)255 std::vector<std::shared_ptr<RSAnimation>> RSNode::AnimateWithCurrentOptions(
256     const PropertyCallback& propertyCallback, const std::function<void()>& finishCallback, bool timingSensitive)
257 {
258     if (propertyCallback == nullptr) {
259         ROSEN_LOGE("Failed to add curve animation, property callback is null!");
260         return {};
261     }
262     if (finishCallback == nullptr) {
263         ROSEN_LOGE("Failed to add curve animation, finish callback is null!");
264         propertyCallback();
265         return {};
266     }
267 
268     auto implicitAnimator = RSImplicitAnimatorMap::Instance().GetAnimator(gettid());
269     if (implicitAnimator == nullptr) {
270         ROSEN_LOGE("Failed to open implicit animation, implicit animator is null!");
271         return {};
272     }
273     auto finishCallbackType =
274         timingSensitive ? FinishCallbackType::TIME_SENSITIVE : FinishCallbackType::TIME_INSENSITIVE;
275     // re-use the current options and replace the finish callback
276     auto animationFinishCallback = std::make_shared<AnimationFinishCallback>(finishCallback, finishCallbackType);
277     implicitAnimator->OpenImplicitAnimation(std::move(animationFinishCallback));
278     propertyCallback();
279     return implicitAnimator->CloseImplicitAnimation();
280 }
281 
AnimateWithCurrentCallback(const RSAnimationTimingProtocol & timingProtocol,const RSAnimationTimingCurve & timingCurve,const PropertyCallback & propertyCallback)282 std::vector<std::shared_ptr<RSAnimation>> RSNode::AnimateWithCurrentCallback(
283     const RSAnimationTimingProtocol& timingProtocol, const RSAnimationTimingCurve& timingCurve,
284     const PropertyCallback& propertyCallback)
285 {
286     if (propertyCallback == nullptr) {
287         ROSEN_LOGE("Failed to add curve animation, property callback is null!");
288         return {};
289     }
290 
291     auto implicitAnimator = RSImplicitAnimatorMap::Instance().GetAnimator(gettid());
292     if (implicitAnimator == nullptr) {
293         ROSEN_LOGE("Failed to open implicit animation, implicit animator is null!");
294         return {};
295     }
296     // re-use the current finish callback and replace the options
297     implicitAnimator->OpenImplicitAnimation(timingProtocol, timingCurve);
298     propertyCallback();
299     return implicitAnimator->CloseImplicitAnimation();
300 }
301 
ExecuteWithoutAnimation(const PropertyCallback & callback,std::shared_ptr<RSImplicitAnimator> implicitAnimator)302 void RSNode::ExecuteWithoutAnimation(
303     const PropertyCallback& callback, std::shared_ptr<RSImplicitAnimator> implicitAnimator)
304 {
305     if (callback == nullptr) {
306         ROSEN_LOGE("Failed to execute without animation, property callback is null!");
307         return;
308     }
309     if (implicitAnimator == nullptr) {
310         implicitAnimator = RSImplicitAnimatorMap::Instance().GetAnimator(gettid());
311     }
312     if (implicitAnimator == nullptr) {
313         callback();
314     } else {
315         implicitAnimator->ExecuteWithoutAnimation(callback);
316     }
317 }
318 
FallbackAnimationsToRoot()319 void RSNode::FallbackAnimationsToRoot()
320 {
321     auto target = RSNodeMap::Instance().GetAnimationFallbackNode();
322 
323     if (target == nullptr) {
324         ROSEN_LOGE("Failed to move animation to root, root node is null!");
325         return;
326     }
327     for (auto& [unused, animation] : animations_) {
328         if (animation && animation->GetRepeatCount() == -1) {
329             continue;
330         }
331         std::unique_lock<std::mutex> lock(animationMutex_);
332         target->AddAnimationInner(std::move(animation));
333     }
334     std::unique_lock<std::mutex> lock(animationMutex_);
335     animations_.clear();
336 }
337 
AddAnimationInner(const std::shared_ptr<RSAnimation> & animation)338 void RSNode::AddAnimationInner(const std::shared_ptr<RSAnimation>& animation)
339 {
340     animations_.emplace(animation->GetId(), animation);
341     animatingPropertyNum_[animation->GetPropertyId()]++;
342 }
343 
RemoveAnimationInner(const std::shared_ptr<RSAnimation> & animation)344 void RSNode::RemoveAnimationInner(const std::shared_ptr<RSAnimation>& animation)
345 {
346     std::unique_lock<std::mutex> lock(animationMutex_);
347     if (auto it = animatingPropertyNum_.find(animation->GetPropertyId()); it != animatingPropertyNum_.end()) {
348         it->second--;
349         if (it->second == 0) {
350             animatingPropertyNum_.erase(it);
351             animation->SetPropertyOnAllAnimationFinish();
352         }
353     }
354     animations_.erase(animation->GetId());
355 }
356 
FinishAnimationByProperty(const PropertyId & id)357 void RSNode::FinishAnimationByProperty(const PropertyId& id)
358 {
359     for (const auto& [animationId, animation] : animations_) {
360         if (animation->GetPropertyId() == id) {
361             animation->Finish();
362         }
363     }
364 }
365 
CancelAnimationByProperty(const PropertyId & id,const bool needForceSync)366 void RSNode::CancelAnimationByProperty(const PropertyId& id, const bool needForceSync)
367 {
368     animatingPropertyNum_.erase(id);
369     std::vector<std::shared_ptr<RSAnimation>> toBeRemoved;
370     {
371         std::unique_lock<std::mutex> lock(animationMutex_, std::defer_lock);
372         if (!lock.try_lock()) {
373             // The Arkui component has logic to cancel animation within the callback of another animation. However, this
374             // approach may cause a deadlock. Although it is a dirty workaround, it currently works as intended.
375             FinishAnimationByProperty(id);
376             return;
377         }
378         EraseIf(animations_, [id, &toBeRemoved](const auto& pair) {
379             if (pair.second && (pair.second->GetPropertyId() == id)) {
380                 toBeRemoved.emplace_back(pair.second);
381                 return true;
382             }
383             return false;
384         });
385     }
386     // Destroy the cancelled animations outside the lock, since destroying them may trigger OnFinish callbacks, and
387     // callbacks may add/remove other animations, doing this with the lock would cause a deadlock.
388     toBeRemoved.clear();
389 
390     if (needForceSync) {
391         // Avoid animation on current property not cancelled in RS
392         auto transactionProxy = RSTransactionProxy::GetInstance();
393         if (transactionProxy == nullptr) {
394             ROSEN_LOGE("RSNode::CancelAnimationByProperty, failed to get RSTransactionProxy!");
395             return;
396         }
397 
398         std::unique_ptr<RSCommand> command = std::make_unique<RSAnimationCancel>(id_, id);
399         transactionProxy->AddCommand(command, IsRenderServiceNode(), GetFollowType(), id_);
400         if (NeedForcedSendToRemote()) {
401             std::unique_ptr<RSCommand> commandForRemote = std::make_unique<RSAnimationCancel>(id_, id);
402             transactionProxy->AddCommand(commandForRemote, true, GetFollowType(), id_);
403         }
404     }
405 }
406 
GetStagingProperties() const407 const RSModifierExtractor& RSNode::GetStagingProperties() const
408 {
409     return stagingPropertiesExtractor_;
410 }
411 
GetShowingProperties() const412 const RSShowingPropertiesFreezer& RSNode::GetShowingProperties() const
413 {
414     return showingPropertiesFreezer_;
415 }
416 
AddAnimation(const std::shared_ptr<RSAnimation> & animation)417 void RSNode::AddAnimation(const std::shared_ptr<RSAnimation>& animation)
418 {
419     if (animation == nullptr) {
420         ROSEN_LOGE("Failed to add animation, animation is null!");
421         return;
422     }
423 
424     auto animationId = animation->GetId();
425     {
426         std::unique_lock<std::mutex> lock(animationMutex_);
427         if (animations_.find(animationId) != animations_.end()) {
428             ROSEN_LOGE("Failed to add animation, animation already exists!");
429             return;
430         }
431     }
432 
433     // Note: Animation cancellation logic is now handled by RSImplicitAnimator. The code below might cause Spring
434     // Animations with a zero duration to not inherit velocity correctly, an issue slated for future resolution.
435     // This code is retained to ensure backward compatibility with specific arkui component animations.
436     if (animation->GetDuration() <= 0 && id_ != 0) {
437         FinishAnimationByProperty(animation->GetPropertyId());
438     }
439 
440     {
441         std::unique_lock<std::mutex> lock(animationMutex_);
442         AddAnimationInner(animation);
443     }
444     animation->StartInner(shared_from_this());
445 }
446 
RemoveAllAnimations()447 void RSNode::RemoveAllAnimations()
448 {
449     for (const auto& [id, animation] : animations_) {
450         RemoveAnimation(animation);
451     }
452 }
453 
RemoveAnimation(const std::shared_ptr<RSAnimation> & animation)454 void RSNode::RemoveAnimation(const std::shared_ptr<RSAnimation>& animation)
455 {
456     if (animation == nullptr) {
457         ROSEN_LOGE("Failed to remove animation, animation is null!");
458         return;
459     }
460 
461     if (animations_.find(animation->GetId()) == animations_.end()) {
462         ROSEN_LOGE("Failed to remove animation, animation not exists!");
463         return;
464     }
465 
466     animation->Finish();
467 }
468 
SetMotionPathOption(const std::shared_ptr<RSMotionPathOption> & motionPathOption)469 void RSNode::SetMotionPathOption(const std::shared_ptr<RSMotionPathOption>& motionPathOption)
470 {
471     motionPathOption_ = motionPathOption;
472     UpdateModifierMotionPathOption();
473 }
474 
GetMotionPathOption() const475 const std::shared_ptr<RSMotionPathOption> RSNode::GetMotionPathOption() const
476 {
477     return motionPathOption_;
478 }
479 
HasPropertyAnimation(const PropertyId & id)480 bool RSNode::HasPropertyAnimation(const PropertyId& id)
481 {
482     std::unique_lock<std::mutex> lock(animationMutex_);
483     auto it = animatingPropertyNum_.find(id);
484     return it != animatingPropertyNum_.end() && it->second > 0;
485 }
486 
487 template<typename ModifierName, typename PropertyName, typename T>
SetProperty(RSModifierType modifierType,T value)488 void RSNode::SetProperty(RSModifierType modifierType, T value)
489 {
490     std::unique_lock<std::recursive_mutex> lock(propertyMutex_);
491     auto iter = propertyModifiers_.find(modifierType);
492     if (iter != propertyModifiers_.end()) {
493         auto property = std::static_pointer_cast<PropertyName>(iter->second->GetProperty());
494         if (property == nullptr) {
495             ROSEN_LOGE("RSNode::SetProperty: failed to set property, property is null!");
496             return;
497         }
498         property->Set(value);
499         return;
500     }
501     auto property = std::make_shared<PropertyName>(value);
502     auto propertyModifier = std::make_shared<ModifierName>(property);
503     propertyModifiers_.emplace(modifierType, propertyModifier);
504     AddModifier(propertyModifier);
505 }
506 
507 // alpha
SetAlpha(float alpha)508 void RSNode::SetAlpha(float alpha)
509 {
510     SetProperty<RSAlphaModifier, RSAnimatableProperty<float>>(RSModifierType::ALPHA, alpha);
511 }
512 
SetAlphaOffscreen(bool alphaOffscreen)513 void RSNode::SetAlphaOffscreen(bool alphaOffscreen)
514 {
515     SetProperty<RSAlphaOffscreenModifier, RSProperty<bool>>(RSModifierType::ALPHA_OFFSCREEN, alphaOffscreen);
516 }
517 
518 // Bounds
SetBounds(const Vector4f & bounds)519 void RSNode::SetBounds(const Vector4f& bounds)
520 {
521     SetProperty<RSBoundsModifier, RSAnimatableProperty<Vector4f>>(RSModifierType::BOUNDS, bounds);
522     OnBoundsSizeChanged();
523 }
524 
SetBounds(float positionX,float positionY,float width,float height)525 void RSNode::SetBounds(float positionX, float positionY, float width, float height)
526 {
527     SetBounds({ positionX, positionY, width, height });
528 }
529 
SetBoundsWidth(float width)530 void RSNode::SetBoundsWidth(float width)
531 {
532     std::unordered_map<RSModifierType, std::shared_ptr<RSModifier>>::iterator iter;
533     {
534         std::unique_lock<std::recursive_mutex> lock(propertyMutex_);
535         iter = propertyModifiers_.find(RSModifierType::BOUNDS);
536         if (iter == propertyModifiers_.end()) {
537             SetBounds(0.f, 0.f, width, 0.f);
538             return;
539         }
540     }
541 
542     auto property = std::static_pointer_cast<RSAnimatableProperty<Vector4f>>(iter->second->GetProperty());
543     if (property == nullptr) {
544         return;
545     }
546     auto bounds = property->Get();
547     bounds.z_ = width;
548     property->Set(bounds);
549     OnBoundsSizeChanged();
550 }
551 
SetBoundsHeight(float height)552 void RSNode::SetBoundsHeight(float height)
553 {
554     std::unordered_map<RSModifierType, std::shared_ptr<RSModifier>>::iterator iter;
555     {
556         std::unique_lock<std::recursive_mutex> lock(propertyMutex_);
557         iter = propertyModifiers_.find(RSModifierType::BOUNDS);
558         if (iter == propertyModifiers_.end()) {
559             SetBounds(0.f, 0.f, 0.f, height);
560             return;
561         }
562     }
563 
564     auto property = std::static_pointer_cast<RSAnimatableProperty<Vector4f>>(iter->second->GetProperty());
565     if (property == nullptr) {
566         return;
567     }
568     auto bounds = property->Get();
569     bounds.w_ = height;
570     property->Set(bounds);
571     OnBoundsSizeChanged();
572 }
573 
574 // Frame
SetFrame(const Vector4f & bounds)575 void RSNode::SetFrame(const Vector4f& bounds)
576 {
577     SetProperty<RSFrameModifier, RSAnimatableProperty<Vector4f>>(RSModifierType::FRAME, bounds);
578 }
579 
SetFrame(float positionX,float positionY,float width,float height)580 void RSNode::SetFrame(float positionX, float positionY, float width, float height)
581 {
582     SetFrame({ positionX, positionY, width, height });
583 }
584 
SetFramePositionX(float positionX)585 void RSNode::SetFramePositionX(float positionX)
586 {
587     std::unordered_map<RSModifierType, std::shared_ptr<RSModifier>>::iterator iter;
588     {
589         std::unique_lock<std::recursive_mutex> lock(propertyMutex_);
590         iter = propertyModifiers_.find(RSModifierType::FRAME);
591         if (iter == propertyModifiers_.end()) {
592             SetFrame(positionX, 0.f, 0.f, 0.f);
593             return;
594         }
595     }
596 
597     auto property = std::static_pointer_cast<RSAnimatableProperty<Vector4f>>(iter->second->GetProperty());
598     if (property == nullptr) {
599         return;
600     }
601     auto frame = property->Get();
602     frame.x_ = positionX;
603     property->Set(frame);
604 }
605 
SetFramePositionY(float positionY)606 void RSNode::SetFramePositionY(float positionY)
607 {
608     std::unordered_map<RSModifierType, std::shared_ptr<RSModifier>>::iterator iter;
609     {
610         std::unique_lock<std::recursive_mutex> lock(propertyMutex_);
611         iter = propertyModifiers_.find(RSModifierType::FRAME);
612         if (iter == propertyModifiers_.end()) {
613             SetFrame(0.f, positionY, 0.f, 0.f);
614             return;
615         }
616     }
617     auto property = std::static_pointer_cast<RSAnimatableProperty<Vector4f>>(iter->second->GetProperty());
618     if (property == nullptr) {
619         return;
620     }
621     auto frame = property->Get();
622     frame.y_ = positionY;
623     property->Set(frame);
624 }
625 
SetSandBox(std::optional<Vector2f> parentPosition)626 void RSNode::SetSandBox(std::optional<Vector2f> parentPosition)
627 {
628     if (!parentPosition.has_value()) {
629         std::unique_lock<std::recursive_mutex> lock(propertyMutex_);
630         auto iter = propertyModifiers_.find(RSModifierType::SANDBOX);
631         if (iter != propertyModifiers_.end()) {
632             RemoveModifier(iter->second);
633             propertyModifiers_.erase(iter);
634         }
635         return;
636     }
637     SetProperty<RSSandBoxModifier, RSAnimatableProperty<Vector2f>>(RSModifierType::SANDBOX, parentPosition.value());
638 }
639 
SetPositionZ(float positionZ)640 void RSNode::SetPositionZ(float positionZ)
641 {
642     SetProperty<RSPositionZModifier, RSAnimatableProperty<float>>(RSModifierType::POSITION_Z, positionZ);
643 }
644 
645 // pivot
SetPivot(const Vector2f & pivot)646 void RSNode::SetPivot(const Vector2f& pivot)
647 {
648     SetProperty<RSPivotModifier, RSAnimatableProperty<Vector2f>>(RSModifierType::PIVOT, pivot);
649 }
650 
SetPivot(float pivotX,float pivotY)651 void RSNode::SetPivot(float pivotX, float pivotY)
652 {
653     SetPivot({ pivotX, pivotY });
654 }
655 
SetPivotX(float pivotX)656 void RSNode::SetPivotX(float pivotX)
657 {
658     std::unordered_map<RSModifierType, std::shared_ptr<RSModifier>>::iterator iter;
659     {
660         std::unique_lock<std::recursive_mutex> lock(propertyMutex_);
661         iter = propertyModifiers_.find(RSModifierType::PIVOT);
662         if (iter == propertyModifiers_.end()) {
663             SetPivot(pivotX, 0.5f);
664             return;
665         }
666     }
667 
668     auto property = std::static_pointer_cast<RSAnimatableProperty<Vector2f>>(iter->second->GetProperty());
669     if (property == nullptr) {
670         return;
671     }
672     auto pivot = property->Get();
673     pivot.x_ = pivotX;
674     property->Set(pivot);
675 }
676 
SetPivotY(float pivotY)677 void RSNode::SetPivotY(float pivotY)
678 {
679     std::unordered_map<RSModifierType, std::shared_ptr<RSModifier>>::iterator iter;
680     {
681         std::unique_lock<std::recursive_mutex> lock(propertyMutex_);
682         iter = propertyModifiers_.find(RSModifierType::PIVOT);
683         if (iter == propertyModifiers_.end()) {
684             SetPivot(0.5f, pivotY);
685             return;
686         }
687     }
688 
689     auto property = std::static_pointer_cast<RSAnimatableProperty<Vector2f>>(iter->second->GetProperty());
690     if (property == nullptr) {
691         return;
692     }
693     auto pivot = property->Get();
694     pivot.y_ = pivotY;
695     property->Set(pivot);
696 }
697 
SetPivotZ(const float pivotZ)698 void RSNode::SetPivotZ(const float pivotZ)
699 {
700     SetProperty<RSPivotZModifier, RSAnimatableProperty<float>>(RSModifierType::PIVOT_Z, pivotZ);
701 }
702 
SetCornerRadius(float cornerRadius)703 void RSNode::SetCornerRadius(float cornerRadius)
704 {
705     SetCornerRadius(Vector4f(cornerRadius));
706 }
707 
SetCornerRadius(const Vector4f & cornerRadius)708 void RSNode::SetCornerRadius(const Vector4f& cornerRadius)
709 {
710     SetProperty<RSCornerRadiusModifier, RSAnimatableProperty<Vector4f>>(RSModifierType::CORNER_RADIUS, cornerRadius);
711 }
712 
713 // transform
SetRotation(const Quaternion & quaternion)714 void RSNode::SetRotation(const Quaternion& quaternion)
715 {
716     SetProperty<RSQuaternionModifier, RSAnimatableProperty<Quaternion>>(RSModifierType::QUATERNION, quaternion);
717 }
718 
SetRotation(float degree)719 void RSNode::SetRotation(float degree)
720 {
721     SetProperty<RSRotationModifier, RSAnimatableProperty<float>>(RSModifierType::ROTATION, degree);
722 }
723 
SetRotation(float degreeX,float degreeY,float degreeZ)724 void RSNode::SetRotation(float degreeX, float degreeY, float degreeZ)
725 {
726     SetRotationX(degreeX);
727     SetRotationY(degreeY);
728     SetRotation(degreeZ);
729 }
730 
SetRotationX(float degree)731 void RSNode::SetRotationX(float degree)
732 {
733     SetProperty<RSRotationXModifier, RSAnimatableProperty<float>>(RSModifierType::ROTATION_X, degree);
734 }
735 
SetRotationY(float degree)736 void RSNode::SetRotationY(float degree)
737 {
738     SetProperty<RSRotationYModifier, RSAnimatableProperty<float>>(RSModifierType::ROTATION_Y, degree);
739 }
740 
SetCameraDistance(float cameraDistance)741 void RSNode::SetCameraDistance(float cameraDistance)
742 {
743     SetProperty<RSCameraDistanceModifier, RSAnimatableProperty<float>>(RSModifierType::CAMERA_DISTANCE, cameraDistance);
744 }
745 
SetTranslate(const Vector2f & translate)746 void RSNode::SetTranslate(const Vector2f& translate)
747 {
748     SetProperty<RSTranslateModifier, RSAnimatableProperty<Vector2f>>(RSModifierType::TRANSLATE, translate);
749 }
750 
SetTranslate(float translateX,float translateY,float translateZ)751 void RSNode::SetTranslate(float translateX, float translateY, float translateZ)
752 {
753     SetTranslate({ translateX, translateY });
754     SetTranslateZ(translateZ);
755 }
SetTranslateX(float translate)756 void RSNode::SetTranslateX(float translate)
757 {
758     std::unordered_map<RSModifierType, std::shared_ptr<RSModifier>>::iterator iter;
759     {
760         std::unique_lock<std::recursive_mutex> lock(propertyMutex_);
761         iter = propertyModifiers_.find(RSModifierType::TRANSLATE);
762         if (iter == propertyModifiers_.end()) {
763             SetTranslate({ translate, 0.f });
764             return;
765         }
766     }
767 
768     auto property = std::static_pointer_cast<RSAnimatableProperty<Vector2f>>(iter->second->GetProperty());
769     if (property == nullptr) {
770         return;
771     }
772     auto trans = property->Get();
773     trans.x_ = translate;
774     property->Set(trans);
775 }
776 
SetTranslateY(float translate)777 void RSNode::SetTranslateY(float translate)
778 {
779     std::unordered_map<RSModifierType, std::shared_ptr<RSModifier>>::iterator iter;
780     {
781         std::unique_lock<std::recursive_mutex> lock(propertyMutex_);
782         iter = propertyModifiers_.find(RSModifierType::TRANSLATE);
783         if (iter == propertyModifiers_.end()) {
784             SetTranslate({ 0.f, translate });
785             return;
786         }
787     }
788     auto property = std::static_pointer_cast<RSAnimatableProperty<Vector2f>>(iter->second->GetProperty());
789     if (property == nullptr) {
790         return;
791     }
792     auto trans = property->Get();
793     trans.y_ = translate;
794     property->Set(trans);
795 }
796 
SetTranslateZ(float translate)797 void RSNode::SetTranslateZ(float translate)
798 {
799     SetProperty<RSTranslateZModifier, RSAnimatableProperty<float>>(RSModifierType::TRANSLATE_Z, translate);
800 }
801 
SetScale(float scale)802 void RSNode::SetScale(float scale)
803 {
804     SetScale({ scale, scale });
805 }
806 
SetScale(float scaleX,float scaleY)807 void RSNode::SetScale(float scaleX, float scaleY)
808 {
809     SetScale({ scaleX, scaleY });
810 }
811 
SetScale(const Vector2f & scale)812 void RSNode::SetScale(const Vector2f& scale)
813 {
814     SetProperty<RSScaleModifier, RSAnimatableProperty<Vector2f>>(RSModifierType::SCALE, scale);
815 }
816 
SetScaleX(float scaleX)817 void RSNode::SetScaleX(float scaleX)
818 {
819     std::unordered_map<RSModifierType, std::shared_ptr<RSModifier>>::iterator iter;
820     {
821         std::unique_lock<std::recursive_mutex> lock(propertyMutex_);
822         iter = propertyModifiers_.find(RSModifierType::SCALE);
823         if (iter == propertyModifiers_.end()) {
824             SetScale(scaleX, 1.f);
825             return;
826         }
827     }
828 
829     auto property = std::static_pointer_cast<RSAnimatableProperty<Vector2f>>(iter->second->GetProperty());
830     if (property == nullptr) {
831         return;
832     }
833     auto scale = property->Get();
834     scale.x_ = scaleX;
835     property->Set(scale);
836 }
837 
SetScaleY(float scaleY)838 void RSNode::SetScaleY(float scaleY)
839 {
840     std::unordered_map<RSModifierType, std::shared_ptr<RSModifier>>::iterator iter;
841     {
842         std::unique_lock<std::recursive_mutex> lock(propertyMutex_);
843         iter = propertyModifiers_.find(RSModifierType::SCALE);
844         if (iter == propertyModifiers_.end()) {
845             SetScale(1.f, scaleY);
846             return;
847         }
848     }
849 
850     auto property = std::static_pointer_cast<RSAnimatableProperty<Vector2f>>(iter->second->GetProperty());
851     if (property == nullptr) {
852         return;
853     }
854     auto scale = property->Get();
855     scale.y_ = scaleY;
856     property->Set(scale);
857 }
858 // Set the foreground color of the control
SetEnvForegroundColor(uint32_t colorValue)859 void RSNode::SetEnvForegroundColor(uint32_t colorValue)
860 {
861     auto color = Color::FromArgbInt(colorValue);
862     SetProperty<RSEnvForegroundColorModifier, RSAnimatableProperty<Color>>(RSModifierType::ENV_FOREGROUND_COLOR, color);
863 }
864 
865 // Set the foreground color strategy of the control
SetEnvForegroundColorStrategy(ForegroundColorStrategyType strategyType)866 void RSNode::SetEnvForegroundColorStrategy(ForegroundColorStrategyType strategyType)
867 {
868     SetProperty<RSEnvForegroundColorStrategyModifier,
869         RSProperty<ForegroundColorStrategyType>>(RSModifierType::ENV_FOREGROUND_COLOR_STRATEGY, strategyType);
870 }
871 
872 // Set ParticleParams
SetParticleParams(std::vector<ParticleParams> & particleParams,const std::function<void ()> & finishCallback)873 void RSNode::SetParticleParams(std::vector<ParticleParams>& particleParams, const std::function<void()>& finishCallback)
874 {
875     std::vector<std::shared_ptr<ParticleRenderParams>> particlesRenderParams;
876     for (size_t i = 0; i < particleParams.size(); i++) {
877         particlesRenderParams.push_back(particleParams[i].SetParamsToRenderParticle());
878     }
879     SetParticleDrawRegion(particleParams);
880     auto property = std::make_shared<RSPropertyBase>();
881     auto propertyId = property->GetId();
882     auto uiAnimation = std::make_shared<RSAnimationGroup>();
883     auto animationId = uiAnimation->GetId();
884     AddAnimation(uiAnimation);
885     if (finishCallback != nullptr) {
886         uiAnimation->SetFinishCallback(std::make_shared<AnimationFinishCallback>(finishCallback));
887     }
888     auto animation =
889         std::make_shared<RSRenderParticleAnimation>(animationId, propertyId, std::move(particlesRenderParams));
890 
891     std::unique_ptr<RSCommand> command = std::make_unique<RSAnimationCreateParticle>(GetId(), animation);
892     auto transactionProxy = RSTransactionProxy::GetInstance();
893     if (transactionProxy != nullptr) {
894         transactionProxy->AddCommand(command, IsRenderServiceNode(), GetFollowType(), GetId());
895         if (NeedForcedSendToRemote()) {
896             std::unique_ptr<RSCommand> cmdForRemote =
897                 std::make_unique<RSAnimationCreateParticle>(GetId(), animation);
898             transactionProxy->AddCommand(cmdForRemote, true, GetFollowType(), GetId());
899         }
900     }
901 }
902 
SetParticleDrawRegion(std::vector<ParticleParams> & particleParams)903 void RSNode::SetParticleDrawRegion(std::vector<ParticleParams>& particleParams)
904 {
905     Vector4f bounds = GetStagingProperties().GetBounds();
906     float left = 0.f;
907     float top = 0.f;
908     float right = bounds.z_;
909     float bottom = bounds.w_;
910     for (size_t i = 0; i < particleParams.size(); i++) {
911         auto particleType = particleParams[i].emitterConfig_.type_;
912         auto position = particleParams[i].emitterConfig_.position_;
913         auto emitSize = particleParams[i].emitterConfig_.emitSize_;
914         float scaleMax = particleParams[i].scale_.val_.end_;
915         if (particleType == ParticleType::POINTS) {
916             auto radius = particleParams[i].emitterConfig_.radius_;
917             auto radiusMax = radius * scaleMax;
918             left = std::min(left - radiusMax, position.x_ - radiusMax);
919             top = std::min(top - radiusMax, position.y_ - radiusMax);
920             right = std::max(right + radiusMax + radiusMax, position.x_ + emitSize.x_ + radiusMax + radiusMax);
921             bottom = std::max(bottom + radiusMax + radiusMax, position.y_ + emitSize.y_ + radiusMax + radiusMax);
922         } else {
923             float imageSizeWidth = 0.f;
924             float imageSizeHeight = 0.f;
925             auto image = particleParams[i].emitterConfig_.image_;
926             auto imageSize = particleParams[i].emitterConfig_.imageSize_;
927             if (image == nullptr)
928                 continue;
929             auto pixelMap = image->GetPixelMap();
930             if (pixelMap != nullptr) {
931                 imageSizeWidth = std::max(imageSize.x_, static_cast<float>(pixelMap->GetWidth()));
932                 imageSizeHeight = std::max(imageSize.y_, static_cast<float>(pixelMap->GetHeight()));
933             }
934             float imageSizeWidthMax = imageSizeWidth * scaleMax;
935             float imageSizeHeightMax = imageSizeHeight * scaleMax;
936             left = std::min(left - imageSizeWidthMax, position.x_ - imageSizeWidthMax);
937             top = std::min(top - imageSizeHeightMax, position.y_ - imageSizeHeightMax);
938             right = std::max(right + imageSizeWidthMax + imageSizeWidthMax,
939                 position.x_ + emitSize.x_ + imageSizeWidthMax + imageSizeWidthMax);
940             bottom = std::max(bottom + imageSizeHeightMax + imageSizeHeightMax,
941                 position.y_ + emitSize.y_ + imageSizeHeightMax + imageSizeHeightMax);
942         }
943         std::shared_ptr<RectF> overlayRect = std::make_shared<RectF>(left, top, right, bottom);
944         SetDrawRegion(overlayRect);
945     }
946 }
947 
948 // foreground
SetForegroundColor(uint32_t colorValue)949 void RSNode::SetForegroundColor(uint32_t colorValue)
950 {
951     auto color = Color::FromArgbInt(colorValue);
952     SetProperty<RSForegroundColorModifier, RSAnimatableProperty<Color>>(RSModifierType::FOREGROUND_COLOR, color);
953 }
954 
SetBackgroundColor(uint32_t colorValue)955 void RSNode::SetBackgroundColor(uint32_t colorValue)
956 {
957     auto color = Color::FromArgbInt(colorValue);
958     SetProperty<RSBackgroundColorModifier, RSAnimatableProperty<Color>>(RSModifierType::BACKGROUND_COLOR, color);
959 }
960 
SetBackgroundShader(const std::shared_ptr<RSShader> & shader)961 void RSNode::SetBackgroundShader(const std::shared_ptr<RSShader>& shader)
962 {
963     SetProperty<RSBackgroundShaderModifier, RSProperty<std::shared_ptr<RSShader>>>(
964         RSModifierType::BACKGROUND_SHADER, shader);
965 }
966 
967 // background
SetBgImage(const std::shared_ptr<RSImage> & image)968 void RSNode::SetBgImage(const std::shared_ptr<RSImage>& image)
969 {
970     image->SetNodeId(GetId());
971     SetProperty<RSBgImageModifier, RSProperty<std::shared_ptr<RSImage>>>(RSModifierType::BG_IMAGE, image);
972 }
973 
SetBgImageSize(float width,float height)974 void RSNode::SetBgImageSize(float width, float height)
975 {
976     SetBgImageWidth(width);
977     SetBgImageHeight(height);
978 }
979 
SetBgImageWidth(float width)980 void RSNode::SetBgImageWidth(float width)
981 {
982     SetProperty<RSBgImageWidthModifier, RSAnimatableProperty<float>>(RSModifierType::BG_IMAGE_WIDTH, width);
983 }
984 
SetBgImageHeight(float height)985 void RSNode::SetBgImageHeight(float height)
986 {
987     SetProperty<RSBgImageHeightModifier, RSAnimatableProperty<float>>(RSModifierType::BG_IMAGE_HEIGHT, height);
988 }
989 
SetBgImagePosition(float positionX,float positionY)990 void RSNode::SetBgImagePosition(float positionX, float positionY)
991 {
992     SetBgImagePositionX(positionX);
993     SetBgImagePositionY(positionY);
994 }
995 
SetBgImagePositionX(float positionX)996 void RSNode::SetBgImagePositionX(float positionX)
997 {
998     SetProperty<RSBgImagePositionXModifier, RSAnimatableProperty<float>>(
999         RSModifierType::BG_IMAGE_POSITION_X, positionX);
1000 }
1001 
SetBgImagePositionY(float positionY)1002 void RSNode::SetBgImagePositionY(float positionY)
1003 {
1004     SetProperty<RSBgImagePositionYModifier, RSAnimatableProperty<float>>(
1005         RSModifierType::BG_IMAGE_POSITION_Y, positionY);
1006 }
1007 
1008 // set inner border color
SetBorderColor(uint32_t colorValue)1009 void RSNode::SetBorderColor(uint32_t colorValue)
1010 {
1011     SetBorderColor(colorValue, colorValue, colorValue, colorValue);
1012 }
1013 
1014 // set inner border color
SetBorderColor(uint32_t left,uint32_t top,uint32_t right,uint32_t bottom)1015 void RSNode::SetBorderColor(uint32_t left, uint32_t top, uint32_t right, uint32_t bottom)
1016 {
1017     Vector4<Color> color(Color::FromArgbInt(left), Color::FromArgbInt(top),
1018                          Color::FromArgbInt(right), Color::FromArgbInt(bottom));
1019     SetBorderColor(color);
1020 }
1021 
1022 // set inner border color
SetBorderColor(const Vector4<Color> & color)1023 void RSNode::SetBorderColor(const Vector4<Color>& color)
1024 {
1025     SetProperty<RSBorderColorModifier, RSAnimatableProperty<Vector4<Color>>>(RSModifierType::BORDER_COLOR, color);
1026 }
1027 
1028 // set inner border width
SetBorderWidth(float width)1029 void RSNode::SetBorderWidth(float width)
1030 {
1031     SetBorderWidth(width, width, width, width);
1032 }
1033 
1034 // set inner border width
SetBorderWidth(float left,float top,float right,float bottom)1035 void RSNode::SetBorderWidth(float left, float top, float right, float bottom)
1036 {
1037     Vector4f width(left, top, right, bottom);
1038     SetBorderWidth(width);
1039 }
1040 
1041 // set inner border width
SetBorderWidth(const Vector4f & width)1042 void RSNode::SetBorderWidth(const Vector4f& width)
1043 {
1044     SetProperty<RSBorderWidthModifier, RSAnimatableProperty<Vector4f>>(RSModifierType::BORDER_WIDTH, width);
1045 }
1046 
1047 // set inner border style
SetBorderStyle(uint32_t styleValue)1048 void RSNode::SetBorderStyle(uint32_t styleValue)
1049 {
1050     SetBorderStyle(styleValue, styleValue, styleValue, styleValue);
1051 }
1052 
1053 // set inner border style
SetBorderStyle(uint32_t left,uint32_t top,uint32_t right,uint32_t bottom)1054 void RSNode::SetBorderStyle(uint32_t left, uint32_t top, uint32_t right, uint32_t bottom)
1055 {
1056     Vector4<BorderStyle> style(static_cast<BorderStyle>(left), static_cast<BorderStyle>(top),
1057                                static_cast<BorderStyle>(right), static_cast<BorderStyle>(bottom));
1058     SetBorderStyle(style);
1059 }
1060 
1061 // set inner border style
SetBorderStyle(const Vector4<BorderStyle> & style)1062 void RSNode::SetBorderStyle(const Vector4<BorderStyle>& style)
1063 {
1064     Vector4<uint32_t> styles(static_cast<uint32_t>(style.x_), static_cast<uint32_t>(style.y_),
1065                              static_cast<uint32_t>(style.z_), static_cast<uint32_t>(style.w_));
1066     SetProperty<RSBorderStyleModifier, RSProperty<Vector4<uint32_t>>>(RSModifierType::BORDER_STYLE, styles);
1067 }
1068 
SetOuterBorderColor(const Vector4<Color> & color)1069 void RSNode::SetOuterBorderColor(const Vector4<Color>& color)
1070 {
1071     SetOutlineColor(color);
1072 }
1073 
SetOuterBorderWidth(const Vector4f & width)1074 void RSNode::SetOuterBorderWidth(const Vector4f& width)
1075 {
1076     SetOutlineWidth(width);
1077 }
1078 
SetOuterBorderStyle(const Vector4<BorderStyle> & style)1079 void RSNode::SetOuterBorderStyle(const Vector4<BorderStyle>& style)
1080 {
1081     SetOutlineStyle(style);
1082 }
1083 
SetOuterBorderRadius(const Vector4f & radius)1084 void RSNode::SetOuterBorderRadius(const Vector4f& radius)
1085 {
1086     SetOutlineRadius(radius);
1087 }
1088 
SetOutlineColor(const Vector4<Color> & color)1089 void RSNode::SetOutlineColor(const Vector4<Color>& color)
1090 {
1091     SetProperty<RSOutlineColorModifier, RSAnimatableProperty<Vector4<Color>>>(
1092         RSModifierType::OUTLINE_COLOR, color);
1093 }
1094 
SetOutlineWidth(const Vector4f & width)1095 void RSNode::SetOutlineWidth(const Vector4f& width)
1096 {
1097     SetProperty<RSOutlineWidthModifier, RSAnimatableProperty<Vector4f>>(
1098         RSModifierType::OUTLINE_WIDTH, width);
1099 }
1100 
SetOutlineStyle(const Vector4<BorderStyle> & style)1101 void RSNode::SetOutlineStyle(const Vector4<BorderStyle>& style)
1102 {
1103     Vector4<uint32_t> styles(static_cast<uint32_t>(style.x_), static_cast<uint32_t>(style.y_),
1104                              static_cast<uint32_t>(style.z_), static_cast<uint32_t>(style.w_));
1105     SetProperty<RSOutlineStyleModifier, RSProperty<Vector4<uint32_t>>>(
1106         RSModifierType::OUTLINE_STYLE, styles);
1107 }
1108 
SetOutlineRadius(const Vector4f & radius)1109 void RSNode::SetOutlineRadius(const Vector4f& radius)
1110 {
1111     SetProperty<RSOutlineRadiusModifier, RSAnimatableProperty<Vector4f>>(
1112         RSModifierType::OUTLINE_RADIUS, radius);
1113 }
1114 
SetBackgroundFilter(const std::shared_ptr<RSFilter> & backgroundFilter)1115 void RSNode::SetBackgroundFilter(const std::shared_ptr<RSFilter>& backgroundFilter)
1116 {
1117     SetProperty<RSBackgroundFilterModifier, RSAnimatableProperty<std::shared_ptr<RSFilter>>>(
1118         RSModifierType::BACKGROUND_FILTER, backgroundFilter);
1119 }
1120 
SetFilter(const std::shared_ptr<RSFilter> & filter)1121 void RSNode::SetFilter(const std::shared_ptr<RSFilter>& filter)
1122 {
1123     SetProperty<RSFilterModifier, RSAnimatableProperty<std::shared_ptr<RSFilter>>>(RSModifierType::FILTER, filter);
1124 }
1125 
SetLinearGradientBlurPara(const std::shared_ptr<RSLinearGradientBlurPara> & para)1126 void RSNode::SetLinearGradientBlurPara(const std::shared_ptr<RSLinearGradientBlurPara>& para)
1127 {
1128     SetProperty<RSLinearGradientBlurParaModifier, RSProperty<std::shared_ptr<RSLinearGradientBlurPara>>>
1129                                                                     (RSModifierType::LINEAR_GRADIENT_BLUR_PARA, para);
1130 }
1131 
SetDynamicLightUpRate(const float rate)1132 void RSNode::SetDynamicLightUpRate(const float rate)
1133 {
1134     SetProperty<RSDynamicLightUpRateModifier, RSAnimatableProperty<float>>(RSModifierType::DYNAMIC_LIGHT_UP_RATE, rate);
1135 }
1136 
SetDynamicLightUpDegree(const float lightUpDegree)1137 void RSNode::SetDynamicLightUpDegree(const float lightUpDegree)
1138 {
1139     SetProperty<RSDynamicLightUpDegreeModifier,
1140         RSAnimatableProperty<float>>(RSModifierType::DYNAMIC_LIGHT_UP_DEGREE, lightUpDegree);
1141 }
1142 
SetGreyCoef1(const float greyCoef1)1143 void RSNode::SetGreyCoef1(const float greyCoef1)
1144 {
1145     SetProperty<RSGreyCoef1Modifier,
1146         RSAnimatableProperty<float>>(RSModifierType::GREY_COEF1, greyCoef1);
1147 }
1148 
SetGreyCoef2(const float greyCoef2)1149 void RSNode::SetGreyCoef2(const float greyCoef2)
1150 {
1151     SetProperty<RSGreyCoef2Modifier,
1152         RSAnimatableProperty<float>>(RSModifierType::GREY_COEF2, greyCoef2);
1153 }
1154 
SetCompositingFilter(const std::shared_ptr<RSFilter> & compositingFilter)1155 void RSNode::SetCompositingFilter(const std::shared_ptr<RSFilter>& compositingFilter) {}
1156 
SetShadowColor(uint32_t colorValue)1157 void RSNode::SetShadowColor(uint32_t colorValue)
1158 {
1159     auto color = Color::FromArgbInt(colorValue);
1160     SetProperty<RSShadowColorModifier, RSAnimatableProperty<Color>>(RSModifierType::SHADOW_COLOR, color);
1161 }
1162 
SetShadowOffset(float offsetX,float offsetY)1163 void RSNode::SetShadowOffset(float offsetX, float offsetY)
1164 {
1165     SetShadowOffsetX(offsetX);
1166     SetShadowOffsetY(offsetY);
1167 }
1168 
SetShadowOffsetX(float offsetX)1169 void RSNode::SetShadowOffsetX(float offsetX)
1170 {
1171     SetProperty<RSShadowOffsetXModifier, RSAnimatableProperty<float>>(RSModifierType::SHADOW_OFFSET_X, offsetX);
1172 }
1173 
SetShadowOffsetY(float offsetY)1174 void RSNode::SetShadowOffsetY(float offsetY)
1175 {
1176     SetProperty<RSShadowOffsetYModifier, RSAnimatableProperty<float>>(RSModifierType::SHADOW_OFFSET_Y, offsetY);
1177 }
1178 
SetShadowAlpha(float alpha)1179 void RSNode::SetShadowAlpha(float alpha)
1180 {
1181     SetProperty<RSShadowAlphaModifier, RSAnimatableProperty<float>>(RSModifierType::SHADOW_ALPHA, alpha);
1182 }
1183 
SetShadowElevation(float elevation)1184 void RSNode::SetShadowElevation(float elevation)
1185 {
1186     SetProperty<RSShadowRadiusModifier, RSAnimatableProperty<float>>(RSModifierType::SHADOW_RADIUS, 0);
1187     SetProperty<RSShadowElevationModifier, RSAnimatableProperty<float>>(RSModifierType::SHADOW_ELEVATION, elevation);
1188 }
1189 
SetShadowRadius(float radius)1190 void RSNode::SetShadowRadius(float radius)
1191 {
1192     SetProperty<RSShadowElevationModifier, RSAnimatableProperty<float>>(RSModifierType::SHADOW_ELEVATION, 0);
1193     SetProperty<RSShadowRadiusModifier, RSAnimatableProperty<float>>(RSModifierType::SHADOW_RADIUS, radius);
1194 }
1195 
SetShadowPath(const std::shared_ptr<RSPath> & shadowPath)1196 void RSNode::SetShadowPath(const std::shared_ptr<RSPath>& shadowPath)
1197 {
1198     SetProperty<RSShadowPathModifier, RSProperty<std::shared_ptr<RSPath>>>(RSModifierType::SHADOW_PATH, shadowPath);
1199 }
1200 
SetShadowMask(bool shadowMask)1201 void RSNode::SetShadowMask(bool shadowMask)
1202 {
1203     SetProperty<RSShadowMaskModifier, RSProperty<bool>>(RSModifierType::SHADOW_MASK, shadowMask);
1204 }
1205 
SetShadowIsFilled(bool shadowIsFilled)1206 void RSNode::SetShadowIsFilled(bool shadowIsFilled)
1207 {
1208     SetProperty<RSShadowIsFilledModifier, RSProperty<bool>>(RSModifierType::SHADOW_IS_FILLED, shadowIsFilled);
1209 }
1210 
SetShadowColorStrategy(int shadowColorStrategy)1211 void RSNode::SetShadowColorStrategy(int shadowColorStrategy)
1212 {
1213     SetProperty<RSShadowColorStrategyModifier, RSProperty<int>>(
1214         RSModifierType::SHADOW_COLOR_STRATEGY, shadowColorStrategy);
1215 }
1216 
SetFrameGravity(Gravity gravity)1217 void RSNode::SetFrameGravity(Gravity gravity)
1218 {
1219     ROSEN_LOGD("RSNode::SetFrameGravity, gravity = %{public}d", gravity);
1220     SetProperty<RSFrameGravityModifier, RSProperty<Gravity>>(RSModifierType::FRAME_GRAVITY, gravity);
1221 }
1222 
SetClipRRect(const Vector4f & clipRect,const Vector4f & clipRadius)1223 void RSNode::SetClipRRect(const Vector4f& clipRect, const Vector4f& clipRadius)
1224 {
1225     SetProperty<RSClipRRectModifier, RSAnimatableProperty<RRect>>(
1226         RSModifierType::CLIP_RRECT, RRect(clipRect, clipRadius));
1227 }
1228 
SetClipBounds(const std::shared_ptr<RSPath> & path)1229 void RSNode::SetClipBounds(const std::shared_ptr<RSPath>& path)
1230 {
1231     SetProperty<RSClipBoundsModifier, RSProperty<std::shared_ptr<RSPath>>>(RSModifierType::CLIP_BOUNDS, path);
1232 }
1233 
SetClipToBounds(bool clipToBounds)1234 void RSNode::SetClipToBounds(bool clipToBounds)
1235 {
1236     SetProperty<RSClipToBoundsModifier, RSProperty<bool>>(RSModifierType::CLIP_TO_BOUNDS, clipToBounds);
1237 }
1238 
SetClipToFrame(bool clipToFrame)1239 void RSNode::SetClipToFrame(bool clipToFrame)
1240 {
1241     SetProperty<RSClipToFrameModifier, RSProperty<bool>>(RSModifierType::CLIP_TO_FRAME, clipToFrame);
1242 }
1243 
SetVisible(bool visible)1244 void RSNode::SetVisible(bool visible)
1245 {
1246     // kick off transition only if it's on tree(has valid parent) and visibility is changed.
1247     if (transitionEffect_ != nullptr && GetParent() != nullptr && visible != GetStagingProperties().GetVisible()) {
1248         NotifyTransition(transitionEffect_, visible);
1249     }
1250 
1251     SetProperty<RSVisibleModifier, RSProperty<bool>>(RSModifierType::VISIBLE, visible);
1252 }
1253 
SetMask(const std::shared_ptr<RSMask> & mask)1254 void RSNode::SetMask(const std::shared_ptr<RSMask>& mask)
1255 {
1256     SetProperty<RSMaskModifier, RSProperty<std::shared_ptr<RSMask>>>(RSModifierType::MASK, mask);
1257 }
1258 
SetUseEffect(bool useEffect)1259 void RSNode::SetUseEffect(bool useEffect)
1260 {
1261     SetProperty<RSUseEffectModifier, RSProperty<bool>>(RSModifierType::USE_EFFECT, useEffect);
1262 }
1263 
SetUseShadowBatching(bool useShadowBatching)1264 void RSNode::SetUseShadowBatching(bool useShadowBatching)
1265 {
1266     SetProperty<RSUseShadowBatchingModifier, RSProperty<bool>>(RSModifierType::USE_SHADOW_BATCHING, useShadowBatching);
1267 }
1268 
SetColorBlendMode(RSColorBlendMode colorBlendMode)1269 void RSNode::SetColorBlendMode(RSColorBlendMode colorBlendMode)
1270 {
1271     SetProperty<RSColorBlendModeModifier, RSProperty<int>>(
1272         RSModifierType::COLOR_BLEND_MODE, static_cast<int>(colorBlendMode));
1273 }
1274 
SetColorBlendApplyType(RSColorBlendApplyType colorBlendApplyType)1275 void RSNode::SetColorBlendApplyType(RSColorBlendApplyType colorBlendApplyType)
1276 {
1277     SetProperty<RSColorBlendApplyTypeModifier, RSProperty<int>>(
1278         RSModifierType::COLOR_BLEND_APPLY_TYPE, static_cast<int>(colorBlendApplyType));
1279 }
1280 
SetPixelStretch(const Vector4f & stretchSize)1281 void RSNode::SetPixelStretch(const Vector4f& stretchSize)
1282 {
1283     SetProperty<RSPixelStretchModifier, RSAnimatableProperty<Vector4f>>(RSModifierType::PIXEL_STRETCH, stretchSize);
1284 }
1285 
SetPixelStretchPercent(const Vector4f & stretchPercent)1286 void RSNode::SetPixelStretchPercent(const Vector4f& stretchPercent)
1287 {
1288     SetProperty<RSPixelStretchPercentModifier, RSAnimatableProperty<Vector4f>>(RSModifierType::PIXEL_STRETCH_PERCENT,
1289         stretchPercent);
1290 }
1291 
SetFreeze(bool isFreeze)1292 void RSNode::SetFreeze(bool isFreeze)
1293 {
1294     ROSEN_LOGE("SetFreeze only support RSSurfaceNode and RSCanvasNode in uniRender");
1295 }
1296 
SetSpherizeDegree(float spherizeDegree)1297 void RSNode::SetSpherizeDegree(float spherizeDegree)
1298 {
1299     SetProperty<RSSpherizeModifier, RSAnimatableProperty<float>>(RSModifierType::SPHERIZE, spherizeDegree);
1300 }
1301 
SetLightUpEffectDegree(float LightUpEffectDegree)1302 void RSNode::SetLightUpEffectDegree(float LightUpEffectDegree)
1303 {
1304     SetProperty<RSLightUpEffectModifier, RSAnimatableProperty<float>>(
1305         RSModifierType::LIGHT_UP_EFFECT, LightUpEffectDegree);
1306 }
1307 
NotifyTransition(const std::shared_ptr<const RSTransitionEffect> & effect,bool isTransitionIn)1308 void RSNode::NotifyTransition(const std::shared_ptr<const RSTransitionEffect>& effect, bool isTransitionIn)
1309 {
1310     // temporary fix for multithread issue in implicit animator
1311     UpdateImplicitAnimator();
1312     if (implicitAnimator_ == nullptr) {
1313         ROSEN_LOGE("Failed to notify transition, implicit animator is null!");
1314         return;
1315     }
1316 
1317     if (!implicitAnimator_->NeedImplicitAnimation()) {
1318         return;
1319     }
1320 
1321     auto& customEffects = isTransitionIn ? effect->customTransitionInEffects_ : effect->customTransitionOutEffects_;
1322     // temporary close the implicit animation
1323     ExecuteWithoutAnimation(
1324         [&customEffects] {
1325             for (auto& customEffect : customEffects) {
1326                 customEffect->Active();
1327             }
1328         },
1329         implicitAnimator_);
1330 
1331     implicitAnimator_->BeginImplicitTransition(effect, isTransitionIn);
1332     for (auto& customEffect : customEffects) {
1333         customEffect->Identity();
1334     }
1335     implicitAnimator_->CreateImplicitTransition(*this);
1336     implicitAnimator_->EndImplicitTransition();
1337 }
1338 
OnAddChildren()1339 void RSNode::OnAddChildren()
1340 {
1341     // kick off transition only if it's visible.
1342     if (transitionEffect_ != nullptr && GetStagingProperties().GetVisible()) {
1343         NotifyTransition(transitionEffect_, true);
1344     }
1345 }
1346 
OnRemoveChildren()1347 void RSNode::OnRemoveChildren()
1348 {
1349     // kick off transition only if it's visible.
1350     if (transitionEffect_ != nullptr && GetStagingProperties().GetVisible()) {
1351         NotifyTransition(transitionEffect_, false);
1352     }
1353 }
1354 
AnimationCallback(AnimationId animationId,AnimationCallbackEvent event)1355 bool RSNode::AnimationCallback(AnimationId animationId, AnimationCallbackEvent event)
1356 {
1357     std::shared_ptr<RSAnimation> animation = nullptr;
1358     {
1359         std::unique_lock<std::mutex> lock(animationMutex_);
1360         auto animationItr = animations_.find(animationId);
1361         if (animationItr == animations_.end()) {
1362             ROSEN_LOGE("Failed to find animation[%{public}" PRIu64 "]!", animationId);
1363             return false;
1364         }
1365         animation = animationItr->second;
1366     }
1367 
1368     if (animation == nullptr) {
1369         ROSEN_LOGE("Failed to callback animation[%{public}" PRIu64 "], animation is null!", animationId);
1370         return false;
1371     }
1372     if (event == FINISHED) {
1373         RemoveAnimationInner(animation);
1374         animation->CallFinishCallback();
1375         return true;
1376     } else if (event == REPEAT_FINISHED) {
1377         animation->CallRepeatCallback();
1378         return true;
1379     } else if (event == LOGICALLY_FINISHED) {
1380         animation->CallLogicallyFinishCallback();
1381         return true;
1382     }
1383     ROSEN_LOGE("Failed to callback animation event[%{public}d], event is null!", event);
1384     return false;
1385 }
1386 
SetPaintOrder(bool drawContentLast)1387 void RSNode::SetPaintOrder(bool drawContentLast)
1388 {
1389     drawContentLast_ = drawContentLast;
1390 }
1391 
MarkDrivenRender(bool flag)1392 void RSNode::MarkDrivenRender(bool flag)
1393 {
1394     if (drivenFlag_ != flag) {
1395         std::unique_ptr<RSCommand> command = std::make_unique<RSMarkDrivenRender>(GetId(), flag);
1396         auto transactionProxy = RSTransactionProxy::GetInstance();
1397         if (transactionProxy != nullptr) {
1398             transactionProxy->AddCommand(command, IsRenderServiceNode());
1399         }
1400         drivenFlag_ = flag;
1401     }
1402 }
1403 
MarkDrivenRenderItemIndex(int index)1404 void RSNode::MarkDrivenRenderItemIndex(int index)
1405 {
1406     std::unique_ptr<RSCommand> command = std::make_unique<RSMarkDrivenRenderItemIndex>(GetId(), index);
1407     auto transactionProxy = RSTransactionProxy::GetInstance();
1408     if (transactionProxy != nullptr) {
1409         transactionProxy->AddCommand(command, IsRenderServiceNode());
1410     }
1411 }
1412 
MarkDrivenRenderFramePaintState(bool flag)1413 void RSNode::MarkDrivenRenderFramePaintState(bool flag)
1414 {
1415     std::unique_ptr<RSCommand> command =
1416         std::make_unique<RSMarkDrivenRenderFramePaintState>(GetId(), flag);
1417     auto transactionProxy = RSTransactionProxy::GetInstance();
1418     if (transactionProxy != nullptr) {
1419         transactionProxy->AddCommand(command, IsRenderServiceNode());
1420     }
1421 }
1422 
MarkContentChanged(bool isChanged)1423 void RSNode::MarkContentChanged(bool isChanged)
1424 {
1425     std::unique_ptr<RSCommand> command = std::make_unique<RSMarkContentChanged>(GetId(), isChanged);
1426     auto transactionProxy = RSTransactionProxy::GetInstance();
1427     if (transactionProxy != nullptr) {
1428         transactionProxy->AddCommand(command, IsRenderServiceNode());
1429     }
1430 }
1431 
ClearAllModifiers()1432 void RSNode::ClearAllModifiers()
1433 {
1434     std::unique_lock<std::recursive_mutex> lock(propertyMutex_);
1435     for (auto [id, modifier] : modifiers_) {
1436         if (modifier) {
1437             modifier->DetachFromNode();
1438         }
1439     }
1440     modifiers_.clear();
1441     propertyModifiers_.clear();
1442 }
1443 
AddModifier(const std::shared_ptr<RSModifier> modifier)1444 void RSNode::AddModifier(const std::shared_ptr<RSModifier> modifier)
1445 {
1446     std::unique_lock<std::recursive_mutex> lock(propertyMutex_);
1447     if (!modifier || modifiers_.count(modifier->GetPropertyId())) {
1448         return;
1449     }
1450     if (motionPathOption_ != nullptr && IsPathAnimatableModifier(modifier->GetModifierType())) {
1451         modifier->SetMotionPathOption(motionPathOption_);
1452     }
1453     auto rsnode = std::static_pointer_cast<RSNode>(shared_from_this());
1454     modifier->AttachToNode(rsnode);
1455     modifiers_.emplace(modifier->GetPropertyId(), modifier);
1456     if (modifier->GetModifierType() == RSModifierType::NODE_MODIFIER) {
1457         return;
1458     }
1459     std::unique_ptr<RSCommand> command = std::make_unique<RSAddModifier>(GetId(), modifier->CreateRenderModifier());
1460     auto transactionProxy = RSTransactionProxy::GetInstance();
1461     if (transactionProxy != nullptr) {
1462         transactionProxy->AddCommand(command, IsRenderServiceNode(), GetFollowType(), GetId());
1463         if (NeedForcedSendToRemote()) {
1464             std::unique_ptr<RSCommand> cmdForRemote =
1465                 std::make_unique<RSAddModifier>(GetId(), modifier->CreateRenderModifier());
1466             transactionProxy->AddCommand(cmdForRemote, true, GetFollowType(), GetId());
1467         }
1468     }
1469 }
1470 
DoFlushModifier()1471 void RSNode::DoFlushModifier()
1472 {
1473     std::unique_lock<std::recursive_mutex> lock(propertyMutex_);
1474     if (modifiers_.empty()) {
1475         return;
1476     }
1477     for (const auto& [_, modifier] : modifiers_) {
1478         std::unique_ptr<RSCommand> command = std::make_unique<RSAddModifier>(GetId(), modifier->CreateRenderModifier());
1479         auto transactionProxy = RSTransactionProxy::GetInstance();
1480         if (transactionProxy != nullptr) {
1481             transactionProxy->AddCommand(command, IsRenderServiceNode(), GetFollowType(), GetId());
1482         }
1483     }
1484 }
1485 
RemoveModifier(const std::shared_ptr<RSModifier> modifier)1486 void RSNode::RemoveModifier(const std::shared_ptr<RSModifier> modifier)
1487 {
1488     std::unique_lock<std::recursive_mutex> lock(propertyMutex_);
1489     if (!modifier) {
1490         return;
1491     }
1492     auto iter = modifiers_.find(modifier->GetPropertyId());
1493     if (iter == modifiers_.end()) {
1494         return;
1495     }
1496     modifiers_.erase(iter);
1497     modifier->DetachFromNode();
1498     std::unique_ptr<RSCommand> command = std::make_unique<RSRemoveModifier>(GetId(), modifier->GetPropertyId());
1499     auto transactionProxy = RSTransactionProxy::GetInstance();
1500     if (transactionProxy != nullptr) {
1501         transactionProxy->AddCommand(command, IsRenderServiceNode(), GetFollowType(), GetId());
1502         if (NeedForcedSendToRemote()) {
1503             std::unique_ptr<RSCommand> cmdForRemote =
1504                 std::make_unique<RSRemoveModifier>(GetId(), modifier->GetPropertyId());
1505             transactionProxy->AddCommand(cmdForRemote, true, GetFollowType(), GetId());
1506         }
1507     }
1508 }
1509 
GetModifier(const PropertyId & propertyId)1510 const std::shared_ptr<RSModifier> RSNode::GetModifier(const PropertyId& propertyId)
1511 {
1512     std::unique_lock<std::recursive_mutex> lock(propertyMutex_);
1513     auto iter = modifiers_.find(propertyId);
1514     if (iter != modifiers_.end()) {
1515         return iter->second;
1516     }
1517 
1518     return {};
1519 }
1520 
UpdateModifierMotionPathOption()1521 void RSNode::UpdateModifierMotionPathOption()
1522 {
1523     std::unique_lock<std::recursive_mutex> lock(propertyMutex_);
1524     for (auto& [type, modifier] : propertyModifiers_) {
1525         if (IsPathAnimatableModifier(type)) {
1526             modifier->SetMotionPathOption(motionPathOption_);
1527         }
1528     }
1529     for (auto& [id, modifier] : modifiers_) {
1530         if (IsPathAnimatableModifier(modifier->GetModifierType())) {
1531             modifier->SetMotionPathOption(motionPathOption_);
1532         }
1533     }
1534 }
1535 
UpdateImplicitAnimator()1536 void RSNode::UpdateImplicitAnimator()
1537 {
1538     auto tid = gettid();
1539     if (tid == implicitAnimatorTid_) {
1540         return;
1541     }
1542     implicitAnimatorTid_ = tid;
1543     implicitAnimator_ = RSImplicitAnimatorMap::Instance().GetAnimator(tid);
1544 }
1545 
GetModifierIds() const1546 std::vector<PropertyId> RSNode::GetModifierIds() const
1547 {
1548     std::vector<PropertyId> ids;
1549     for (const auto& [id, _] : modifiers_) {
1550         ids.push_back(id);
1551     }
1552     return ids;
1553 }
1554 
MarkAllExtendModifierDirty()1555 void RSNode::MarkAllExtendModifierDirty()
1556 {
1557     std::unique_lock<std::recursive_mutex> lock(propertyMutex_);
1558     if (extendModifierIsDirty_) {
1559         return;
1560     }
1561 
1562     extendModifierIsDirty_ = true;
1563     for (auto& [id, modifier] : modifiers_) {
1564         if (modifier->GetModifierType() < RSModifierType::CUSTOM) {
1565             continue;
1566         }
1567         modifier->SetDirty(true);
1568     }
1569 }
1570 
ResetExtendModifierDirty()1571 void RSNode::ResetExtendModifierDirty()
1572 {
1573     extendModifierIsDirty_ = false;
1574 }
1575 
SetIsCustomTextType(bool isCustomTextType)1576 void RSNode::SetIsCustomTextType(bool isCustomTextType)
1577 {
1578     isCustomTextType_ = isCustomTextType;
1579 }
1580 
GetIsCustomTextType()1581 bool RSNode::GetIsCustomTextType()
1582 {
1583     return isCustomTextType_;
1584 }
1585 
SetDrawRegion(std::shared_ptr<RectF> rect)1586 void RSNode::SetDrawRegion(std::shared_ptr<RectF> rect)
1587 {
1588     if (drawRegion_ != rect) {
1589         drawRegion_ = rect;
1590         std::unique_ptr<RSCommand> command = std::make_unique<RSSetDrawRegion>(GetId(), rect);
1591         auto transactionProxy = RSTransactionProxy::GetInstance();
1592         if (transactionProxy != nullptr) {
1593             transactionProxy->AddCommand(command, IsRenderServiceNode(), GetFollowType(), GetId());
1594         }
1595     }
1596 }
1597 
RegisterTransitionPair(NodeId inNodeId,NodeId outNodeId)1598 void RSNode::RegisterTransitionPair(NodeId inNodeId, NodeId outNodeId)
1599 {
1600     std::unique_ptr<RSCommand> command =
1601         std::make_unique<RSRegisterGeometryTransitionNodePair>(inNodeId, outNodeId);
1602     auto transactionProxy = RSTransactionProxy::GetInstance();
1603     if (transactionProxy != nullptr) {
1604         transactionProxy->AddCommand(command, true);
1605     }
1606 }
1607 
UnregisterTransitionPair(NodeId inNodeId,NodeId outNodeId)1608 void RSNode::UnregisterTransitionPair(NodeId inNodeId, NodeId outNodeId)
1609 {
1610     std::unique_ptr<RSCommand> command =
1611         std::make_unique<RSUnregisterGeometryTransitionNodePair>(inNodeId, outNodeId);
1612     auto transactionProxy = RSTransactionProxy::GetInstance();
1613     if (transactionProxy != nullptr) {
1614         transactionProxy->AddCommand(command, true);
1615     }
1616 }
1617 
MarkNodeGroup(bool isNodeGroup,bool isForced,bool includeProperty)1618 void RSNode::MarkNodeGroup(bool isNodeGroup, bool isForced, bool includeProperty)
1619 {
1620     if (isNodeGroup_ == isNodeGroup) {
1621         return;
1622     }
1623     isNodeGroup_ = isNodeGroup;
1624     std::unique_ptr<RSCommand> command = std::make_unique<RSMarkNodeGroup>(GetId(), isNodeGroup, isForced,
1625         includeProperty);
1626     auto transactionProxy = RSTransactionProxy::GetInstance();
1627     if (transactionProxy != nullptr) {
1628         transactionProxy->AddCommand(command, IsRenderServiceNode());
1629     }
1630 }
1631 
MarkNodeSingleFrameComposer(bool isNodeSingleFrameComposer)1632 void RSNode::MarkNodeSingleFrameComposer(bool isNodeSingleFrameComposer)
1633 {
1634     if (isNodeSingleFrameComposer_ != isNodeSingleFrameComposer) {
1635         isNodeSingleFrameComposer_ = isNodeSingleFrameComposer;
1636         std::unique_ptr<RSCommand> command =
1637             std::make_unique<RSMarkNodeSingleFrameComposer>(GetId(), isNodeSingleFrameComposer, GetRealPid());
1638         auto transactionProxy = RSTransactionProxy::GetInstance();
1639         if (transactionProxy != nullptr) {
1640             transactionProxy->AddCommand(command, IsRenderServiceNode());
1641         }
1642     }
1643 }
1644 
SetGrayScale(float grayScale)1645 void RSNode::SetGrayScale(float grayScale)
1646 {
1647     SetProperty<RSGrayScaleModifier, RSAnimatableProperty<float>>(RSModifierType::GRAY_SCALE, grayScale);
1648 }
1649 
SetLightIntensity(float lightIntensity)1650 void RSNode::SetLightIntensity(float lightIntensity)
1651 {
1652     SetProperty<RSLightIntensityModifier, RSAnimatableProperty<float>>(RSModifierType::LIGHT_INTENSITY, lightIntensity);
1653 }
1654 
SetLightPosition(float positionX,float positionY,float positionZ)1655 void RSNode::SetLightPosition(float positionX, float positionY, float positionZ)
1656 {
1657     SetLightPosition(Vector4f(positionX, positionY, positionZ, 0.f));
1658 }
1659 
SetLightPosition(const Vector4f & lightPosition)1660 void RSNode::SetLightPosition(const Vector4f& lightPosition)
1661 {
1662     SetProperty<RSLightPositionModifier, RSAnimatableProperty<Vector4f>>(RSModifierType::LIGHT_POSITION, lightPosition);
1663 }
1664 
SetIlluminatedBorderWidth(float illuminatedBorderWidth)1665 void RSNode::SetIlluminatedBorderWidth(float illuminatedBorderWidth)
1666 {
1667     SetProperty<RSIlluminatedBorderWidthModifier, RSAnimatableProperty<float>>(
1668         RSModifierType::ILLUMINATED_BORDER_WIDTH, illuminatedBorderWidth);
1669 }
1670 
SetIlluminatedType(uint32_t illuminatedType)1671 void RSNode::SetIlluminatedType(uint32_t illuminatedType)
1672 {
1673     SetProperty<RSIlluminatedTypeModifier, RSProperty<int>>(
1674         RSModifierType::ILLUMINATED_TYPE, illuminatedType);
1675 }
1676 
SetBloom(float bloomIntensity)1677 void RSNode::SetBloom(float bloomIntensity)
1678 {
1679     SetProperty<RSBloomModifier, RSAnimatableProperty<float>>(RSModifierType::BLOOM, bloomIntensity);
1680 }
1681 
SetBrightness(float brightness)1682 void RSNode::SetBrightness(float brightness)
1683 {
1684     SetProperty<RSBrightnessModifier, RSAnimatableProperty<float>>(RSModifierType::BRIGHTNESS, brightness);
1685 }
1686 
SetContrast(float contrast)1687 void RSNode::SetContrast(float contrast)
1688 {
1689     SetProperty<RSContrastModifier, RSAnimatableProperty<float>>(RSModifierType::CONTRAST, contrast);
1690 }
1691 
SetSaturate(float saturate)1692 void RSNode::SetSaturate(float saturate)
1693 {
1694     SetProperty<RSSaturateModifier, RSAnimatableProperty<float>>(RSModifierType::SATURATE, saturate);
1695 }
1696 
SetSepia(float sepia)1697 void RSNode::SetSepia(float sepia)
1698 {
1699     SetProperty<RSSepiaModifier, RSAnimatableProperty<float>>(RSModifierType::SEPIA, sepia);
1700 }
1701 
SetInvert(float invert)1702 void RSNode::SetInvert(float invert)
1703 {
1704     SetProperty<RSInvertModifier, RSAnimatableProperty<float>>(RSModifierType::INVERT, invert);
1705 }
1706 
SetAiInvert(const Vector4f & aiInvert)1707 void RSNode::SetAiInvert(const Vector4f& aiInvert)
1708 {
1709     SetProperty<RSAiInvertModifier, RSAnimatableProperty<Vector4f>>(RSModifierType::AIINVERT, aiInvert);
1710 }
1711 
SetSystemBarEffect()1712 void RSNode::SetSystemBarEffect()
1713 {
1714     SetProperty<RSSystemBarEffectModifier, RSAnimatableProperty<bool>>(RSModifierType::SYSTEMBAREFFECT, true);
1715 }
1716 
SetHueRotate(float hueRotate)1717 void RSNode::SetHueRotate(float hueRotate)
1718 {
1719     SetProperty<RSHueRotateModifier, RSAnimatableProperty<float>>(RSModifierType::HUE_ROTATE, hueRotate);
1720 }
1721 
SetColorBlend(uint32_t colorValue)1722 void RSNode::SetColorBlend(uint32_t colorValue)
1723 {
1724     auto colorBlend = Color::FromArgbInt(colorValue);
1725     SetProperty<RSColorBlendModifier, RSAnimatableProperty<Color>>(RSModifierType::COLOR_BLEND, colorBlend);
1726 }
1727 
CalcExpectedFrameRate(const std::string & scene,float speed)1728 int32_t RSNode::CalcExpectedFrameRate(const std::string& scene, float speed)
1729 {
1730     auto preferredFps = RSFrameRatePolicy::GetInstance()->GetPreferredFps(scene, speed);
1731     return preferredFps;
1732 }
1733 
SetOutOfParent(OutOfParentType outOfParent)1734 void RSNode::SetOutOfParent(OutOfParentType outOfParent)
1735 {
1736     if (outOfParent != outOfParent_) {
1737         outOfParent_ = outOfParent;
1738 
1739         std::unique_ptr<RSCommand> command = std::make_unique<RSSetOutOfParent>(GetId(), outOfParent);
1740         auto transactionProxy = RSTransactionProxy::GetInstance();
1741         if (transactionProxy != nullptr) {
1742             transactionProxy->AddCommand(command, IsRenderServiceNode());
1743         }
1744     }
1745 }
1746 
GenerateId()1747 NodeId RSNode::GenerateId()
1748 {
1749     static pid_t pid_ = GetRealPid();
1750     static std::atomic<uint32_t> currentId_ = 1; // surfaceNode is seted correctly during boot when currentId is 1
1751 
1752     auto currentId = currentId_.fetch_add(1, std::memory_order_relaxed);
1753     if (currentId == UINT32_MAX) {
1754         // [PLANNING]:process the overflow situations
1755         ROSEN_LOGE("Node Id overflow");
1756     }
1757 
1758     // concat two 32-bit numbers to one 64-bit number
1759     return ((NodeId)pid_ << 32) | currentId;
1760 }
1761 
InitUniRenderEnabled()1762 void RSNode::InitUniRenderEnabled()
1763 {
1764     static bool inited = false;
1765     if (!inited) {
1766         inited = true;
1767         g_isUniRenderEnabled = RSSystemProperties::GetUniRenderEnabled();
1768         ROSEN_LOGD("RSNode::InitUniRenderEnabled:%{public}d", g_isUniRenderEnabled);
1769     }
1770 }
1771 
1772 
1773 // RSNode::~RSNode()
1774 // {
1775 
1776 // }
1777 
IsUniRenderEnabled() const1778 bool RSNode::IsUniRenderEnabled() const
1779 {
1780     return g_isUniRenderEnabled;
1781 }
1782 
IsRenderServiceNode() const1783 bool RSNode::IsRenderServiceNode() const
1784 {
1785     return (g_isUniRenderEnabled || isRenderServiceNode_) && (!isTextureExportNode_);
1786 }
1787 
AddChild(SharedPtr child,int index)1788 void RSNode::AddChild(SharedPtr child, int index)
1789 {
1790     if (child == nullptr) {
1791         ROSEN_LOGE("RSNode::AddChild, child is nullptr");
1792         return;
1793     }
1794     if (child->parent_ == id_) {
1795         ROSEN_LOGD("RSNode::AddChild, child already exist");
1796         return;
1797     }
1798     if (child->GetType() == RSUINodeType::DISPLAY_NODE) {
1799         // Disallow to add display node as child.
1800         return;
1801     }
1802     NodeId childId = child->GetId();
1803     if (child->parent_ != 0 && !child->isTextureExportNode_) {
1804         child->RemoveFromTree();
1805     }
1806 
1807     if (index < 0 || index >= static_cast<int>(children_.size())) {
1808         children_.push_back(childId);
1809     } else {
1810         children_.insert(children_.begin() + index, childId);
1811     }
1812     child->SetParent(id_);
1813     child->OnAddChildren();
1814     auto transactionProxy = RSTransactionProxy::GetInstance();
1815     if (transactionProxy == nullptr) {
1816         return;
1817     }
1818     // construct command using child's GetHierarchyCommandNodeId(), not GetId()
1819     childId = child->GetHierarchyCommandNodeId();
1820     std::unique_ptr<RSCommand> command = std::make_unique<RSBaseNodeAddChild>(id_, childId, index);
1821     transactionProxy->AddCommand(command, IsRenderServiceNode(), GetFollowType(), id_);
1822 }
1823 
MoveChild(SharedPtr child,int index)1824 void RSNode::MoveChild(SharedPtr child, int index)
1825 {
1826     if (child == nullptr || child->parent_ != id_) {
1827         ROSEN_LOGD("RSNode::MoveChild, not valid child");
1828         return;
1829     }
1830     NodeId childId = child->GetId();
1831     auto itr = std::find(children_.begin(), children_.end(), childId);
1832     if (itr == children_.end()) {
1833         ROSEN_LOGD("RSNode::MoveChild, not child");
1834         return;
1835     }
1836     children_.erase(itr);
1837     if (index < 0 || index >= static_cast<int>(children_.size())) {
1838         children_.push_back(childId);
1839     } else {
1840         children_.insert(children_.begin() + index, childId);
1841     }
1842 
1843     auto transactionProxy = RSTransactionProxy::GetInstance();
1844     if (transactionProxy == nullptr) {
1845         return;
1846     }
1847     // construct command using child's GetHierarchyCommandNodeId(), not GetId()
1848     childId = child->GetHierarchyCommandNodeId();
1849     std::unique_ptr<RSCommand> command = std::make_unique<RSBaseNodeMoveChild>(id_, childId, index);
1850     transactionProxy->AddCommand(command, IsRenderServiceNode(), GetFollowType(), id_);
1851 }
1852 
RemoveChild(SharedPtr child)1853 void RSNode::RemoveChild(SharedPtr child)
1854 {
1855     if (child == nullptr || child->parent_ != id_) {
1856         ROSEN_LOGI("RSNode::RemoveChild, child is nullptr");
1857         return;
1858     }
1859     NodeId childId = child->GetId();
1860     RemoveChildById(childId);
1861     child->OnRemoveChildren();
1862     child->SetParent(0);
1863 
1864     auto transactionProxy = RSTransactionProxy::GetInstance();
1865     if (transactionProxy == nullptr) {
1866         return;
1867     }
1868     // construct command using child's GetHierarchyCommandNodeId(), not GetId()
1869     childId = child->GetHierarchyCommandNodeId();
1870     std::unique_ptr<RSCommand> command = std::make_unique<RSBaseNodeRemoveChild>(id_, childId);
1871     transactionProxy->AddCommand(command, IsRenderServiceNode(), GetFollowType(), id_);
1872 }
1873 
RemoveChildByNodeId(NodeId childId)1874 void RSNode::RemoveChildByNodeId(NodeId childId)
1875 {
1876     if (auto childPtr = RSNodeMap::Instance().GetNode(childId)) {
1877         RemoveChild(childPtr);
1878     } else {
1879         ROSEN_LOGE("RSNode::RemoveChildByNodeId, childId not found");
1880     }
1881 }
1882 
AddCrossParentChild(SharedPtr child,int index)1883 void RSNode::AddCrossParentChild(SharedPtr child, int index)
1884 {
1885     // AddCrossParentChild only used as: the child is under multiple parents(e.g. a window cross multi-screens),
1886     // so this child will not remove from the old parent.
1887     if (child == nullptr) {
1888         ROSEN_LOGE("RSNode::AddCrossScreenChild, child is nullptr");
1889         return;
1890     }
1891     if (!this->IsInstanceOf<RSDisplayNode>()) {
1892         ROSEN_LOGE("RSNode::AddCrossScreenChild, only displayNode support AddCrossScreenChild");
1893         return;
1894     }
1895     NodeId childId = child->GetId();
1896 
1897     if (index < 0 || index >= static_cast<int>(children_.size())) {
1898         children_.push_back(childId);
1899     } else {
1900         children_.insert(children_.begin() + index, childId);
1901     }
1902     child->SetParent(id_);
1903     child->OnAddChildren();
1904     auto transactionProxy = RSTransactionProxy::GetInstance();
1905     if (transactionProxy == nullptr) {
1906         return;
1907     }
1908     // construct command using child's GetHierarchyCommandNodeId(), not GetId()
1909     childId = child->GetHierarchyCommandNodeId();
1910     std::unique_ptr<RSCommand> command = std::make_unique<RSBaseNodeAddCrossParentChild>(id_, childId, index);
1911     transactionProxy->AddCommand(command, IsRenderServiceNode(), GetFollowType(), id_);
1912 }
1913 
RemoveCrossParentChild(SharedPtr child,NodeId newParentId)1914 void RSNode::RemoveCrossParentChild(SharedPtr child, NodeId newParentId)
1915 {
1916     // RemoveCrossParentChild only used as: the child is under multiple parents(e.g. a window cross multi-screens),
1917     // set the newParentId to rebuild the parent-child relationship.
1918     if (child == nullptr) {
1919         ROSEN_LOGI("RSNode::RemoveCrossScreenChild, child is nullptr");
1920         return;
1921     }
1922     if (!this->IsInstanceOf<RSDisplayNode>()) {
1923         ROSEN_LOGE("RSNode::RemoveCrossScreenChild, only displayNode support RemoveCrossScreenChild");
1924         return;
1925     }
1926     NodeId childId = child->GetId();
1927     RemoveChildById(childId);
1928     child->OnRemoveChildren();
1929     child->SetParent(newParentId);
1930 
1931     auto transactionProxy = RSTransactionProxy::GetInstance();
1932     if (transactionProxy == nullptr) {
1933         return;
1934     }
1935     // construct command using child's GetHierarchyCommandNodeId(), not GetId()
1936     childId = child->GetHierarchyCommandNodeId();
1937     std::unique_ptr<RSCommand> command = std::make_unique<RSBaseNodeRemoveCrossParentChild>(id_, childId, newParentId);
1938     transactionProxy->AddCommand(command, IsRenderServiceNode(), GetFollowType(), id_);
1939 }
1940 
RemoveChildById(NodeId childId)1941 void RSNode::RemoveChildById(NodeId childId)
1942 {
1943     auto itr = std::find(children_.begin(), children_.end(), childId);
1944     if (itr != children_.end()) {
1945         children_.erase(itr);
1946     }
1947 }
1948 
RemoveFromTree()1949 void RSNode::RemoveFromTree()
1950 {
1951     if (auto parentPtr = RSNodeMap::Instance().GetNode(parent_)) {
1952         parentPtr->RemoveChildById(GetId());
1953         OnRemoveChildren();
1954         SetParent(0);
1955     }
1956     // always send Remove-From-Tree command
1957     auto transactionProxy = RSTransactionProxy::GetInstance();
1958     if (transactionProxy == nullptr) {
1959         return;
1960     }
1961     // construct command using own GetHierarchyCommandNodeId(), not GetId()
1962     auto nodeId = GetHierarchyCommandNodeId();
1963     std::unique_ptr<RSCommand> command = std::make_unique<RSBaseNodeRemoveFromTree>(nodeId);
1964     transactionProxy->AddCommand(command, IsRenderServiceNode(), GetFollowType(), nodeId);
1965 }
1966 
ClearChildren()1967 void RSNode::ClearChildren()
1968 {
1969     for (auto child : children_) {
1970         if (auto childPtr = RSNodeMap::Instance().GetNode(child)) {
1971             childPtr->SetParent(0);
1972         }
1973     }
1974     children_.clear();
1975 
1976     auto transactionProxy = RSTransactionProxy::GetInstance();
1977     if (transactionProxy == nullptr) {
1978         return;
1979     }
1980     // construct command using own GetHierarchyCommandNodeId(), not GetId()
1981     auto nodeId = GetHierarchyCommandNodeId();
1982     std::unique_ptr<RSCommand> command = std::make_unique<RSBaseNodeClearChild>(nodeId);
1983     transactionProxy->AddCommand(command, IsRenderServiceNode(), GetFollowType(), nodeId);
1984 }
1985 
GetChildIdByIndex(int index) const1986 const std::optional<NodeId> RSNode::GetChildIdByIndex(int index) const
1987 {
1988     int childrenTotal = static_cast<int>(children_.size());
1989     if (childrenTotal <= 0 || index < -1 || index >= childrenTotal) {
1990         ROSEN_LOGE("RSNode::GetChildIdByIndex, index out of bound");
1991         return std::nullopt;
1992     }
1993     if (index == -1) {
1994         index = childrenTotal - 1;
1995     }
1996     return children_.at(index);
1997 }
1998 
SetParent(NodeId parentId)1999 void RSNode::SetParent(NodeId parentId)
2000 {
2001     parent_ = parentId;
2002 }
2003 
GetParent()2004 RSNode::SharedPtr RSNode::GetParent()
2005 {
2006     return RSNodeMap::Instance().GetNode(parent_);
2007 }
2008 
DumpNode(int depth) const2009 std::string RSNode::DumpNode(int depth) const
2010 {
2011     std::stringstream ss;
2012     auto it = RSUINodeTypeStrs.find(GetType());
2013     if (it == RSUINodeTypeStrs.end()) {
2014         return "";
2015     }
2016     ss << it->second << "[" << std::to_string(id_) << "] child[";
2017     for (auto child : children_) {
2018         ss << std::to_string(child) << " ";
2019     }
2020     ss << "]";
2021 
2022     if (!animations_.empty()) {
2023         ss << " animation:" << std::to_string(animations_.size());
2024     }
2025     ss << " " << GetStagingProperties().Dump();
2026     return ss.str();
2027 }
2028 
IsInstanceOf(RSUINodeType type) const2029 bool RSNode::IsInstanceOf(RSUINodeType type) const
2030 {
2031     auto targetType = static_cast<uint32_t>(type);
2032     auto instanceType = static_cast<uint32_t>(GetType());
2033     // use bitmask to check whether the instance is a subclass of the target type
2034     return (instanceType & targetType) == targetType;
2035 }
2036 
2037 template<typename T>
IsInstanceOf() const2038 bool RSNode::IsInstanceOf() const
2039 {
2040     return IsInstanceOf(T::Type);
2041 }
2042 
2043 // explicit instantiation with all render node types
2044 template bool RSNode::IsInstanceOf<RSDisplayNode>() const;
2045 template bool RSNode::IsInstanceOf<RSSurfaceNode>() const;
2046 template bool RSNode::IsInstanceOf<RSProxyNode>() const;
2047 template bool RSNode::IsInstanceOf<RSCanvasNode>() const;
2048 template bool RSNode::IsInstanceOf<RSRootNode>() const;
2049 template bool RSNode::IsInstanceOf<RSCanvasDrawingNode>() const;
2050 
2051 } // namespace Rosen
2052 } // namespace OHOS
2053