• 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_obj_geometry.h"
35 #include "modifier/rs_modifier.h"
36 #include "modifier/rs_property_modifier.h"
37 #include "pipeline/rs_node_map.h"
38 #include "platform/common/rs_log.h"
39 #include "render/rs_path.h"
40 #include "transaction/rs_transaction_proxy.h"
41 #include "ui/rs_canvas_drawing_node.h"
42 #include "ui/rs_canvas_node.h"
43 #include "ui/rs_display_node.h"
44 #include "ui/rs_node.h"
45 #include "ui/rs_proxy_node.h"
46 #include "ui/rs_root_node.h"
47 #include "ui/rs_surface_node.h"
48 
49 #ifdef _WIN32
50 #include <windows.h>
51 #define gettid GetCurrentThreadId
52 #endif
53 
54 #ifdef __APPLE__
55 #define gettid getpid
56 #endif
57 
58 #ifdef __gnu_linux__
59 #include <sys/types.h>
60 #include <sys/syscall.h>
61 #define gettid []() -> int32_t { return static_cast<int32_t>(syscall(SYS_gettid)); }
62 #endif
63 
64 namespace OHOS {
65 namespace Rosen {
66 namespace {
67 static bool g_isUniRenderEnabled = false;
IsPathAnimatableModifier(const RSModifierType & type)68 bool IsPathAnimatableModifier(const RSModifierType& type)
69 {
70     if (type == RSModifierType::BOUNDS || type == RSModifierType::FRAME || type == RSModifierType::TRANSLATE) {
71         return true;
72     }
73     return false;
74 }
75 }
76 
RSNode(bool isRenderServiceNode,NodeId id)77 RSNode::RSNode(bool isRenderServiceNode, NodeId id)
78     : isRenderServiceNode_(isRenderServiceNode), id_(id), stagingPropertiesExtractor_(id), showingPropertiesFreezer_(id)
79 {
80     InitUniRenderEnabled();
81     UpdateImplicitAnimator();
82 }
83 
RSNode(bool isRenderServiceNode)84 RSNode::RSNode(bool isRenderServiceNode) : RSNode(isRenderServiceNode, GenerateId())
85 {}
86 
~RSNode()87 RSNode::~RSNode()
88 {
89     FallbackAnimationsToRoot();
90     ClearAllModifiers();
91 
92     // break current (ui) parent-child relationship.
93     // render nodes will check if its child is expired and remove it, no need to manually remove it here.
94     if (auto parentPtr = RSNodeMap::Instance().GetNode(parent_)) {
95         parentPtr->RemoveChildById(id_);
96     }
97     // unregister node from node map
98     RSNodeMap::MutableInstance().UnregisterNode(id_);
99 
100     // tell RT/RS to destroy related render node
101     auto transactionProxy = RSTransactionProxy::GetInstance();
102     if (transactionProxy == nullptr || skipDestroyCommandInDestructor_) {
103         return;
104     }
105     std::unique_ptr<RSCommand> command = std::make_unique<RSBaseNodeDestroy>(id_);
106     transactionProxy->AddCommand(command, IsRenderServiceNode());
107 }
108 
OpenImplicitAnimation(const RSAnimationTimingProtocol & timingProtocol,const RSAnimationTimingCurve & timingCurve,const std::function<void ()> & finishCallback)109 void RSNode::OpenImplicitAnimation(const RSAnimationTimingProtocol& timingProtocol,
110     const RSAnimationTimingCurve& timingCurve, const std::function<void()>& finishCallback)
111 {
112     auto implicitAnimator = RSImplicitAnimatorMap::Instance().GetAnimator(gettid());
113     if (implicitAnimator == nullptr) {
114         ROSEN_LOGE("Failed to open implicit animation, implicit animator is null!");
115         return;
116     }
117 
118     std::shared_ptr<AnimationFinishCallback> animationFinishCallback;
119     if (finishCallback != nullptr) {
120         animationFinishCallback = std::make_shared<AnimationFinishCallback>(finishCallback);
121     }
122     implicitAnimator->OpenImplicitAnimation(timingProtocol, timingCurve, std::move(animationFinishCallback));
123 }
124 
CloseImplicitAnimation()125 std::vector<std::shared_ptr<RSAnimation>> RSNode::CloseImplicitAnimation()
126 {
127     auto implicitAnimator = RSImplicitAnimatorMap::Instance().GetAnimator(gettid());
128     if (implicitAnimator == nullptr) {
129         ROSEN_LOGE("Failed to close implicit animation, implicit animator is null!");
130         return {};
131     }
132 
133     return implicitAnimator->CloseImplicitAnimation();
134 }
135 
AddKeyFrame(float fraction,const RSAnimationTimingCurve & timingCurve,const PropertyCallback & propertyCallback)136 void RSNode::AddKeyFrame(
137     float fraction, const RSAnimationTimingCurve& timingCurve, const PropertyCallback& propertyCallback)
138 {
139     auto implicitAnimator = RSImplicitAnimatorMap::Instance().GetAnimator(gettid());
140     if (implicitAnimator == nullptr) {
141         ROSEN_LOGE("Failed to add keyframe, implicit animator is null!");
142         return;
143     }
144 
145     implicitAnimator->BeginImplicitKeyFrameAnimation(fraction, timingCurve);
146     propertyCallback();
147     implicitAnimator->EndImplicitKeyFrameAnimation();
148 }
149 
AddKeyFrame(float fraction,const PropertyCallback & propertyCallback)150 void RSNode::AddKeyFrame(float fraction, const PropertyCallback& propertyCallback)
151 {
152     auto implicitAnimator = RSImplicitAnimatorMap::Instance().GetAnimator(gettid());
153     if (implicitAnimator == nullptr) {
154         ROSEN_LOGE("Failed to add keyframe, implicit animator is null!");
155         return;
156     }
157 
158     implicitAnimator->BeginImplicitKeyFrameAnimation(fraction);
159     propertyCallback();
160     implicitAnimator->EndImplicitKeyFrameAnimation();
161 }
162 
Animate(const RSAnimationTimingProtocol & timingProtocol,const RSAnimationTimingCurve & timingCurve,const PropertyCallback & propertyCallback,const std::function<void ()> & finishCallback,const std::function<void ()> & repeatCallback)163 std::vector<std::shared_ptr<RSAnimation>> RSNode::Animate(const RSAnimationTimingProtocol& timingProtocol,
164     const RSAnimationTimingCurve& timingCurve, const PropertyCallback& propertyCallback,
165     const std::function<void()>& finishCallback, const std::function<void()>& repeatCallback)
166 {
167     if (propertyCallback == nullptr) {
168         ROSEN_LOGE("Failed to add curve animation, property callback is null!");
169         return {};
170     }
171 
172     auto implicitAnimator = RSImplicitAnimatorMap::Instance().GetAnimator(gettid());
173     if (implicitAnimator == nullptr) {
174         ROSEN_LOGE("Failed to open implicit animation, implicit animator is null!");
175         return {};
176     }
177     std::shared_ptr<AnimationFinishCallback> animationFinishCallback;
178     if (finishCallback != nullptr) {
179         animationFinishCallback = std::make_shared<AnimationFinishCallback>(finishCallback);
180     }
181     std::shared_ptr<AnimationRepeatCallback> animationRepeatCallback;
182     if (repeatCallback != nullptr) {
183         animationRepeatCallback = std::make_shared<AnimationRepeatCallback>(repeatCallback);
184     }
185     implicitAnimator->OpenImplicitAnimation(timingProtocol, timingCurve, std::move(animationFinishCallback),
186         std::move(animationRepeatCallback));
187     propertyCallback();
188     return implicitAnimator->CloseImplicitAnimation();
189 }
190 
AnimateWithCurrentOptions(const PropertyCallback & propertyCallback,const std::function<void ()> & finishCallback,bool timingSensitive)191 std::vector<std::shared_ptr<RSAnimation>> RSNode::AnimateWithCurrentOptions(
192     const PropertyCallback& propertyCallback, const std::function<void()>& finishCallback, bool timingSensitive)
193 {
194     if (propertyCallback == nullptr) {
195         ROSEN_LOGE("Failed to add curve animation, property callback is null!");
196         return {};
197     }
198     if (finishCallback == nullptr) {
199         ROSEN_LOGE("Failed to add curve animation, finish callback is null!");
200         propertyCallback();
201         return {};
202     }
203 
204     auto implicitAnimator = RSImplicitAnimatorMap::Instance().GetAnimator(gettid());
205     if (implicitAnimator == nullptr) {
206         ROSEN_LOGE("Failed to open implicit animation, implicit animator is null!");
207         return {};
208     }
209     // re-use the current options and replace the finish callback
210     auto animationFinishCallback = std::make_shared<AnimationFinishCallback>(finishCallback, timingSensitive);
211     implicitAnimator->OpenImplicitAnimation(std::move(animationFinishCallback));
212     propertyCallback();
213     return implicitAnimator->CloseImplicitAnimation();
214 }
215 
AnimateWithCurrentCallback(const RSAnimationTimingProtocol & timingProtocol,const RSAnimationTimingCurve & timingCurve,const PropertyCallback & propertyCallback)216 std::vector<std::shared_ptr<RSAnimation>> RSNode::AnimateWithCurrentCallback(
217     const RSAnimationTimingProtocol& timingProtocol, const RSAnimationTimingCurve& timingCurve,
218     const PropertyCallback& propertyCallback)
219 {
220     if (propertyCallback == nullptr) {
221         ROSEN_LOGE("Failed to add curve animation, property callback is null!");
222         return {};
223     }
224 
225     auto implicitAnimator = RSImplicitAnimatorMap::Instance().GetAnimator(gettid());
226     if (implicitAnimator == nullptr) {
227         ROSEN_LOGE("Failed to open implicit animation, implicit animator is null!");
228         return {};
229     }
230     // re-use the current finish callback and replace the options
231     implicitAnimator->OpenImplicitAnimation(timingProtocol, timingCurve);
232     propertyCallback();
233     return implicitAnimator->CloseImplicitAnimation();
234 }
235 
ExecuteWithoutAnimation(const PropertyCallback & callback,std::shared_ptr<RSImplicitAnimator> implicitAnimator)236 void RSNode::ExecuteWithoutAnimation(
237     const PropertyCallback& callback, std::shared_ptr<RSImplicitAnimator> implicitAnimator)
238 {
239     if (callback == nullptr) {
240         ROSEN_LOGE("Failed to execute without animation, property callback is null!");
241         return;
242     }
243     if (implicitAnimator == nullptr) {
244         implicitAnimator = RSImplicitAnimatorMap::Instance().GetAnimator(gettid());
245     }
246     if (implicitAnimator == nullptr) {
247         callback();
248     } else {
249         implicitAnimator->ExecuteWithoutAnimation(callback);
250     }
251 }
252 
FallbackAnimationsToRoot()253 void RSNode::FallbackAnimationsToRoot()
254 {
255     auto target = RSNodeMap::Instance().GetAnimationFallbackNode();
256 
257     if (target == nullptr) {
258         ROSEN_LOGE("Failed to move animation to root, root node is null!");
259         return;
260     }
261     for (auto& [unused, animation] : animations_) {
262         if (animation && animation->GetRepeatCount() == -1) {
263             continue;
264         }
265         target->AddAnimationInner(std::move(animation));
266     }
267     std::unique_lock<std::mutex> lock(animationMutex_);
268     animations_.clear();
269 }
270 
AddAnimationInner(const std::shared_ptr<RSAnimation> & animation)271 void RSNode::AddAnimationInner(const std::shared_ptr<RSAnimation>& animation)
272 {
273     std::unique_lock<std::mutex> lock(animationMutex_);
274     animations_.emplace(animation->GetId(), animation);
275     animatingPropertyNum_[animation->GetPropertyId()]++;
276 }
277 
RemoveAnimationInner(const std::shared_ptr<RSAnimation> & animation)278 void RSNode::RemoveAnimationInner(const std::shared_ptr<RSAnimation>& animation)
279 {
280     std::unique_lock<std::mutex> lock(animationMutex_);
281     if (auto it = animatingPropertyNum_.find(animation->GetPropertyId()); it != animatingPropertyNum_.end()) {
282         it->second--;
283         if (it->second == 0) {
284             animatingPropertyNum_.erase(it);
285             animation->SetPropertyOnAllAnimationFinish();
286         }
287     }
288     animations_.erase(animation->GetId());
289 }
290 
FinishAnimationByProperty(const PropertyId & id)291 void RSNode::FinishAnimationByProperty(const PropertyId& id)
292 {
293     for (const auto& [animationId, animation] : animations_) {
294         if (animation->GetPropertyId() == id) {
295             animation->Finish();
296         }
297     }
298 }
299 
CancelAnimationByProperty(const PropertyId & id)300 void RSNode::CancelAnimationByProperty(const PropertyId& id)
301 {
302     std::unique_lock<std::mutex> lock(animationMutex_);
303     animatingPropertyNum_.erase(id);
304     EraseIf(animations_, [id](const auto& pair) { return (pair.second && (pair.second->GetPropertyId() == id)); });
305 }
306 
GetStagingProperties() const307 const RSModifierExtractor& RSNode::GetStagingProperties() const
308 {
309     return stagingPropertiesExtractor_;
310 }
311 
GetShowingProperties() const312 const RSShowingPropertiesFreezer& RSNode::GetShowingProperties() const
313 {
314     return showingPropertiesFreezer_;
315 }
316 
AddAnimation(const std::shared_ptr<RSAnimation> & animation)317 void RSNode::AddAnimation(const std::shared_ptr<RSAnimation>& animation)
318 {
319     if (animation == nullptr) {
320         ROSEN_LOGE("Failed to add animation, animation is null!");
321         return;
322     }
323 
324     auto animationId = animation->GetId();
325     {
326         std::unique_lock<std::mutex> lock(animationMutex_);
327         if (animations_.find(animationId) != animations_.end()) {
328             ROSEN_LOGE("Failed to add animation, animation already exists!");
329             return;
330         }
331     }
332 
333     if (animation->GetDuration() <= 0) {
334         FinishAnimationByProperty(animation->GetPropertyId());
335     }
336 
337     AddAnimationInner(animation);
338     animation->StartInner(shared_from_this());
339 }
340 
RemoveAllAnimations()341 void RSNode::RemoveAllAnimations()
342 {
343     for (const auto& [id, animation] : animations_) {
344         RemoveAnimation(animation);
345     }
346 }
347 
RemoveAnimation(const std::shared_ptr<RSAnimation> & animation)348 void RSNode::RemoveAnimation(const std::shared_ptr<RSAnimation>& animation)
349 {
350     if (animation == nullptr) {
351         ROSEN_LOGE("Failed to remove animation, animation is null!");
352         return;
353     }
354 
355     if (animations_.find(animation->GetId()) == animations_.end()) {
356         ROSEN_LOGE("Failed to remove animation, animation not exists!");
357         return;
358     }
359 
360     animation->Finish();
361 }
362 
SetMotionPathOption(const std::shared_ptr<RSMotionPathOption> & motionPathOption)363 void RSNode::SetMotionPathOption(const std::shared_ptr<RSMotionPathOption>& motionPathOption)
364 {
365     motionPathOption_ = motionPathOption;
366     UpdateModifierMotionPathOption();
367 }
368 
GetMotionPathOption() const369 const std::shared_ptr<RSMotionPathOption> RSNode::GetMotionPathOption() const
370 {
371     return motionPathOption_;
372 }
373 
HasPropertyAnimation(const PropertyId & id)374 bool RSNode::HasPropertyAnimation(const PropertyId& id)
375 {
376     std::unique_lock<std::mutex> lock(animationMutex_);
377     auto it = animatingPropertyNum_.find(id);
378     return it != animatingPropertyNum_.end() && it->second > 0;
379 }
380 
381 template<typename ModifierName, typename PropertyName, typename T>
SetProperty(RSModifierType modifierType,T value)382 void RSNode::SetProperty(RSModifierType modifierType, T value)
383 {
384     auto iter = propertyModifiers_.find(modifierType);
385     if (iter != propertyModifiers_.end()) {
386         auto property = std::static_pointer_cast<PropertyName>(iter->second->GetProperty());
387         if (property == nullptr) {
388             ROSEN_LOGE("RSNode::SetProperty: failed to set property, property is null!");
389             return;
390         }
391         property->Set(value);
392         return;
393     }
394     auto property = std::make_shared<PropertyName>(value);
395     auto propertyModifier = std::make_shared<ModifierName>(property);
396     propertyModifiers_.emplace(modifierType, propertyModifier);
397     AddModifier(propertyModifier);
398 }
399 
400 // alpha
SetAlpha(float alpha)401 void RSNode::SetAlpha(float alpha)
402 {
403     SetProperty<RSAlphaModifier, RSAnimatableProperty<float>>(RSModifierType::ALPHA, alpha);
404 }
405 
SetAlphaOffscreen(bool alphaOffscreen)406 void RSNode::SetAlphaOffscreen(bool alphaOffscreen)
407 {
408     SetProperty<RSAlphaOffscreenModifier, RSProperty<bool>>(RSModifierType::ALPHA_OFFSCREEN, alphaOffscreen);
409 }
410 
411 // Bounds
SetBounds(const Vector4f & bounds)412 void RSNode::SetBounds(const Vector4f& bounds)
413 {
414     SetProperty<RSBoundsModifier, RSAnimatableProperty<Vector4f>>(RSModifierType::BOUNDS, bounds);
415     OnBoundsSizeChanged();
416 }
417 
SetBounds(float positionX,float positionY,float width,float height)418 void RSNode::SetBounds(float positionX, float positionY, float width, float height)
419 {
420     SetBounds({ positionX, positionY, width, height });
421 }
422 
SetBoundsWidth(float width)423 void RSNode::SetBoundsWidth(float width)
424 {
425     auto iter = propertyModifiers_.find(RSModifierType::BOUNDS);
426     if (iter == propertyModifiers_.end()) {
427         SetBounds(0.f, 0.f, width, 0.f);
428         return;
429     }
430 
431     auto property = std::static_pointer_cast<RSAnimatableProperty<Vector4f>>(iter->second->GetProperty());
432     if (property == nullptr) {
433         return;
434     }
435     auto bounds = property->Get();
436     bounds.z_ = width;
437     property->Set(bounds);
438     OnBoundsSizeChanged();
439 }
440 
SetBoundsHeight(float height)441 void RSNode::SetBoundsHeight(float height)
442 {
443     auto iter = propertyModifiers_.find(RSModifierType::BOUNDS);
444     if (iter == propertyModifiers_.end()) {
445         SetBounds(0.f, 0.f, 0.f, height);
446         return;
447     }
448 
449     auto property = std::static_pointer_cast<RSAnimatableProperty<Vector4f>>(iter->second->GetProperty());
450     if (property == nullptr) {
451         return;
452     }
453     auto bounds = property->Get();
454     bounds.w_ = height;
455     property->Set(bounds);
456     OnBoundsSizeChanged();
457 }
458 
459 // Frame
SetFrame(const Vector4f & bounds)460 void RSNode::SetFrame(const Vector4f& bounds)
461 {
462     SetProperty<RSFrameModifier, RSAnimatableProperty<Vector4f>>(RSModifierType::FRAME, bounds);
463 }
464 
SetFrame(float positionX,float positionY,float width,float height)465 void RSNode::SetFrame(float positionX, float positionY, float width, float height)
466 {
467     SetFrame({ positionX, positionY, width, height });
468 }
469 
SetFramePositionX(float positionX)470 void RSNode::SetFramePositionX(float positionX)
471 {
472     auto iter = propertyModifiers_.find(RSModifierType::FRAME);
473     if (iter == propertyModifiers_.end()) {
474         SetFrame(positionX, 0.f, 0.f, 0.f);
475         return;
476     }
477 
478     auto property = std::static_pointer_cast<RSAnimatableProperty<Vector4f>>(iter->second->GetProperty());
479     if (property == nullptr) {
480         return;
481     }
482     auto frame = property->Get();
483     frame.x_ = positionX;
484     property->Set(frame);
485 }
486 
SetFramePositionY(float positionY)487 void RSNode::SetFramePositionY(float positionY)
488 {
489     auto iter = propertyModifiers_.find(RSModifierType::FRAME);
490     if (iter == propertyModifiers_.end()) {
491         SetFrame(0.f, positionY, 0.f, 0.f);
492         return;
493     }
494     auto property = std::static_pointer_cast<RSAnimatableProperty<Vector4f>>(iter->second->GetProperty());
495     if (property == nullptr) {
496         return;
497     }
498     auto frame = property->Get();
499     frame.y_ = positionY;
500     property->Set(frame);
501 }
502 
SetSandBox(std::optional<Vector2f> parentPosition)503 void RSNode::SetSandBox(std::optional<Vector2f> parentPosition)
504 {
505     if (!parentPosition.has_value()) {
506         auto iter = propertyModifiers_.find(RSModifierType::SANDBOX);
507         if (iter != propertyModifiers_.end()) {
508             RemoveModifier(iter->second);
509             propertyModifiers_.erase(iter);
510         }
511         return;
512     }
513     SetProperty<RSSandBoxModifier, RSAnimatableProperty<Vector2f>>(RSModifierType::SANDBOX, parentPosition.value());
514 }
515 
SetPositionZ(float positionZ)516 void RSNode::SetPositionZ(float positionZ)
517 {
518     SetProperty<RSPositionZModifier, RSAnimatableProperty<float>>(RSModifierType::POSITION_Z, positionZ);
519 }
520 
521 // pivot
SetPivot(const Vector2f & pivot)522 void RSNode::SetPivot(const Vector2f& pivot)
523 {
524     SetProperty<RSPivotModifier, RSAnimatableProperty<Vector2f>>(RSModifierType::PIVOT, pivot);
525 }
526 
SetPivot(float pivotX,float pivotY)527 void RSNode::SetPivot(float pivotX, float pivotY)
528 {
529     SetPivot({ pivotX, pivotY });
530 }
531 
SetPivotX(float pivotX)532 void RSNode::SetPivotX(float pivotX)
533 {
534     auto iter = propertyModifiers_.find(RSModifierType::PIVOT);
535     if (iter == propertyModifiers_.end()) {
536         SetPivot(pivotX, 0.5f);
537         return;
538     }
539 
540     auto property = std::static_pointer_cast<RSAnimatableProperty<Vector2f>>(iter->second->GetProperty());
541     if (property == nullptr) {
542         return;
543     }
544     auto pivot = property->Get();
545     pivot.x_ = pivotX;
546     property->Set(pivot);
547 }
548 
SetPivotY(float pivotY)549 void RSNode::SetPivotY(float pivotY)
550 {
551     auto iter = propertyModifiers_.find(RSModifierType::PIVOT);
552     if (iter == propertyModifiers_.end()) {
553         SetPivot(0.5f, pivotY);
554         return;
555     }
556 
557     auto property = std::static_pointer_cast<RSAnimatableProperty<Vector2f>>(iter->second->GetProperty());
558     if (property == nullptr) {
559         return;
560     }
561     auto pivot = property->Get();
562     pivot.y_ = pivotY;
563     property->Set(pivot);
564 }
565 
SetPivotZ(const float pivotZ)566 void RSNode::SetPivotZ(const float pivotZ)
567 {
568     SetProperty<RSPivotZModifier, RSAnimatableProperty<float>>(RSModifierType::PIVOT_Z, pivotZ);
569 }
570 
SetCornerRadius(float cornerRadius)571 void RSNode::SetCornerRadius(float cornerRadius)
572 {
573     SetCornerRadius(Vector4f(cornerRadius));
574 }
575 
SetCornerRadius(const Vector4f & cornerRadius)576 void RSNode::SetCornerRadius(const Vector4f& cornerRadius)
577 {
578     SetProperty<RSCornerRadiusModifier, RSAnimatableProperty<Vector4f>>(RSModifierType::CORNER_RADIUS, cornerRadius);
579 }
580 
581 // transform
SetRotation(const Quaternion & quaternion)582 void RSNode::SetRotation(const Quaternion& quaternion)
583 {
584     SetProperty<RSQuaternionModifier, RSAnimatableProperty<Quaternion>>(RSModifierType::QUATERNION, quaternion);
585 }
586 
SetRotation(float degree)587 void RSNode::SetRotation(float degree)
588 {
589     SetProperty<RSRotationModifier, RSAnimatableProperty<float>>(RSModifierType::ROTATION, degree);
590 }
591 
SetRotation(float degreeX,float degreeY,float degreeZ)592 void RSNode::SetRotation(float degreeX, float degreeY, float degreeZ)
593 {
594     SetRotationX(degreeX);
595     SetRotationY(degreeY);
596     SetRotation(degreeZ);
597 }
598 
SetRotationX(float degree)599 void RSNode::SetRotationX(float degree)
600 {
601     SetProperty<RSRotationXModifier, RSAnimatableProperty<float>>(RSModifierType::ROTATION_X, degree);
602 }
603 
SetRotationY(float degree)604 void RSNode::SetRotationY(float degree)
605 {
606     SetProperty<RSRotationYModifier, RSAnimatableProperty<float>>(RSModifierType::ROTATION_Y, degree);
607 }
608 
SetCameraDistance(float cameraDistance)609 void RSNode::SetCameraDistance(float cameraDistance)
610 {
611     SetProperty<RSCameraDistanceModifier, RSAnimatableProperty<float>>(RSModifierType::CAMERA_DISTANCE, cameraDistance);
612 }
613 
SetTranslate(const Vector2f & translate)614 void RSNode::SetTranslate(const Vector2f& translate)
615 {
616     SetProperty<RSTranslateModifier, RSAnimatableProperty<Vector2f>>(RSModifierType::TRANSLATE, translate);
617 }
618 
SetTranslate(float translateX,float translateY,float translateZ)619 void RSNode::SetTranslate(float translateX, float translateY, float translateZ)
620 {
621     SetTranslate({ translateX, translateY });
622     SetTranslateZ(translateZ);
623 }
SetTranslateX(float translate)624 void RSNode::SetTranslateX(float translate)
625 {
626     auto iter = propertyModifiers_.find(RSModifierType::TRANSLATE);
627     if (iter == propertyModifiers_.end()) {
628         SetTranslate({ translate, 0.f });
629         return;
630     }
631 
632     auto property = std::static_pointer_cast<RSAnimatableProperty<Vector2f>>(iter->second->GetProperty());
633     if (property == nullptr) {
634         return;
635     }
636     auto trans = property->Get();
637     trans.x_ = translate;
638     property->Set(trans);
639 }
640 
SetTranslateY(float translate)641 void RSNode::SetTranslateY(float translate)
642 {
643     auto iter = propertyModifiers_.find(RSModifierType::TRANSLATE);
644     if (iter == propertyModifiers_.end()) {
645         SetTranslate({ 0.f, translate });
646         return;
647     }
648 
649     auto property = std::static_pointer_cast<RSAnimatableProperty<Vector2f>>(iter->second->GetProperty());
650     if (property == nullptr) {
651         return;
652     }
653     auto trans = property->Get();
654     trans.y_ = translate;
655     property->Set(trans);
656 }
657 
SetTranslateZ(float translate)658 void RSNode::SetTranslateZ(float translate)
659 {
660     SetProperty<RSTranslateZModifier, RSAnimatableProperty<float>>(RSModifierType::TRANSLATE_Z, translate);
661 }
662 
SetScale(float scale)663 void RSNode::SetScale(float scale)
664 {
665     SetScale({ scale, scale });
666 }
667 
SetScale(float scaleX,float scaleY)668 void RSNode::SetScale(float scaleX, float scaleY)
669 {
670     SetScale({ scaleX, scaleY });
671 }
672 
SetScale(const Vector2f & scale)673 void RSNode::SetScale(const Vector2f& scale)
674 {
675     SetProperty<RSScaleModifier, RSAnimatableProperty<Vector2f>>(RSModifierType::SCALE, scale);
676 }
677 
SetScaleX(float scaleX)678 void RSNode::SetScaleX(float scaleX)
679 {
680     auto iter = propertyModifiers_.find(RSModifierType::SCALE);
681     if (iter == propertyModifiers_.end()) {
682         SetScale(scaleX, 1.f);
683         return;
684     }
685 
686     auto property = std::static_pointer_cast<RSAnimatableProperty<Vector2f>>(iter->second->GetProperty());
687     if (property == nullptr) {
688         return;
689     }
690     auto scale = property->Get();
691     scale.x_ = scaleX;
692     property->Set(scale);
693 }
694 
SetScaleY(float scaleY)695 void RSNode::SetScaleY(float scaleY)
696 {
697     auto iter = propertyModifiers_.find(RSModifierType::SCALE);
698     if (iter == propertyModifiers_.end()) {
699         SetScale(1.f, scaleY);
700         return;
701     }
702 
703     auto property = std::static_pointer_cast<RSAnimatableProperty<Vector2f>>(iter->second->GetProperty());
704     if (property == nullptr) {
705         return;
706     }
707     auto scale = property->Get();
708     scale.y_ = scaleY;
709     property->Set(scale);
710 }
711 // Set the foreground color of the control
SetEnvForegroundColor(uint32_t colorValue)712 void RSNode::SetEnvForegroundColor(uint32_t colorValue)
713 {
714     auto color = Color::FromArgbInt(colorValue);
715     SetProperty<RSEnvForegroundColorModifier, RSAnimatableProperty<Color>>(RSModifierType::ENV_FOREGROUND_COLOR, color);
716 }
717 
718 // Set the foreground color strategy of the control
SetEnvForegroundColorStrategy(ForegroundColorStrategyType strategyType)719 void RSNode::SetEnvForegroundColorStrategy(ForegroundColorStrategyType strategyType)
720 {
721     SetProperty<RSEnvForegroundColorStrategyModifier,
722         RSProperty<ForegroundColorStrategyType>>(RSModifierType::ENV_FOREGROUND_COLOR_STRATEGY, strategyType);
723 }
724 
725 // Set ParticleParams
SetParticleParams(std::vector<ParticleParams> & particleParams,const std::function<void ()> & finishCallback)726 void RSNode::SetParticleParams(std::vector<ParticleParams>& particleParams, const std::function<void()>& finishCallback)
727 {
728     std::vector<std::shared_ptr<ParticleRenderParams>> particlesRenderParams;
729     for (size_t i = 0; i < particleParams.size(); i++) {
730         particlesRenderParams.push_back(particleParams[i].SetParamsToRenderParticle());
731     }
732     SetParticleDrawRegion(particleParams);
733     auto property = std::make_shared<RSPropertyBase>();
734     auto propertyId = property->GetId();
735     auto uiAnimation = std::make_shared<RSAnimationGroup>();
736     auto animationId = uiAnimation->GetId();
737     AddAnimation(uiAnimation);
738     if (finishCallback != nullptr) {
739         uiAnimation->SetFinishCallback(std::make_shared<AnimationFinishCallback>(finishCallback));
740     }
741     auto animation =
742         std::make_shared<RSRenderParticleAnimation>(animationId, propertyId, particlesRenderParams);
743 
744     std::unique_ptr<RSCommand> command = std::make_unique<RSAnimationCreateParticle>(GetId(), animation);
745     auto transactionProxy = RSTransactionProxy::GetInstance();
746     if (transactionProxy != nullptr) {
747         transactionProxy->AddCommand(command, IsRenderServiceNode(), GetFollowType(), GetId());
748         if (NeedForcedSendToRemote()) {
749             std::unique_ptr<RSCommand> cmdForRemote =
750                 std::make_unique<RSAnimationCreateParticle>(GetId(), animation);
751             transactionProxy->AddCommand(cmdForRemote, true, GetFollowType(), GetId());
752         }
753     }
754 }
755 
SetParticleDrawRegion(std::vector<ParticleParams> & particleParams)756 void RSNode::SetParticleDrawRegion(std::vector<ParticleParams>& particleParams)
757 {
758     Vector4f bounds = GetStagingProperties().GetBounds();
759     float left = bounds.x_;
760     float top = bounds.y_;
761     float right = bounds.z_;
762     float bottom = bounds.w_;
763     for (size_t i = 0; i < particleParams.size(); i++) {
764         auto particleType = particleParams[i].emitterConfig_.type_;
765         auto position = particleParams[i].emitterConfig_.position_;
766         auto emitSize = particleParams[i].emitterConfig_.emitSize_;
767         float scaleMax = particleParams[i].scale_.val_.end_;
768         if (particleType == ParticleType::POINTS) {
769             auto radius = particleParams[i].emitterConfig_.radius_;
770             auto radiusMax = radius * scaleMax;
771             left = std::min(0.f, position.x_ - radiusMax);
772             top = std::min(0.f, position.y_ - radiusMax);
773             right = std::max(right + radiusMax + radiusMax, position.x_ + emitSize.x_ + radiusMax + radiusMax);
774             bottom = std::max(bottom + radiusMax + radiusMax, position.y_ + emitSize.y_ + radiusMax + radiusMax);
775         } else {
776             float imageSizeWidth = 0.f;
777             float imageSizeHeight = 0.f;
778             auto image = particleParams[i].emitterConfig_.image_;
779             auto imageSize = particleParams[i].emitterConfig_.imageSize_;
780             if (image == nullptr)
781                 continue;
782             auto pixelMap = image->GetPixelMap();
783             if (pixelMap != nullptr) {
784                 imageSizeWidth = std::max(imageSize.x_, static_cast<float>(pixelMap->GetWidth()));
785                 imageSizeHeight = std::max(imageSize.y_, static_cast<float>(pixelMap->GetHeight()));
786             }
787             float imageSizeWidthMax = imageSizeWidth * scaleMax;
788             float imageSizeHeightMax = imageSizeHeight * scaleMax;
789             left = std::min(0.f, position.x_ - imageSizeWidthMax);
790             top = std::min(0.f, position.y_ - imageSizeHeightMax);
791             right = std::max(right + imageSizeWidthMax + imageSizeWidthMax,
792                 position.x_ + emitSize.x_ + imageSizeWidthMax + imageSizeWidthMax);
793             bottom = std::max(bottom + imageSizeHeightMax + imageSizeHeightMax,
794                 position.y_ + emitSize.y_ + imageSizeHeightMax + imageSizeHeightMax);
795         }
796         std::shared_ptr<RectF> overlayRect = std::make_shared<RectF>(left, top, right, bottom);
797         SetDrawRegion(overlayRect);
798     }
799 }
800 
801 // foreground
SetForegroundColor(uint32_t colorValue)802 void RSNode::SetForegroundColor(uint32_t colorValue)
803 {
804     auto color = Color::FromArgbInt(colorValue);
805     SetProperty<RSForegroundColorModifier, RSAnimatableProperty<Color>>(RSModifierType::FOREGROUND_COLOR, color);
806 }
807 
SetBackgroundColor(uint32_t colorValue)808 void RSNode::SetBackgroundColor(uint32_t colorValue)
809 {
810     auto color = Color::FromArgbInt(colorValue);
811     SetProperty<RSBackgroundColorModifier, RSAnimatableProperty<Color>>(RSModifierType::BACKGROUND_COLOR, color);
812 }
813 
SetBackgroundShader(const std::shared_ptr<RSShader> & shader)814 void RSNode::SetBackgroundShader(const std::shared_ptr<RSShader>& shader)
815 {
816     SetProperty<RSBackgroundShaderModifier, RSProperty<std::shared_ptr<RSShader>>>(
817         RSModifierType::BACKGROUND_SHADER, shader);
818 }
819 
820 // background
SetBgImage(const std::shared_ptr<RSImage> & image)821 void RSNode::SetBgImage(const std::shared_ptr<RSImage>& image)
822 {
823     image->SetNodeId(GetId());
824     SetProperty<RSBgImageModifier, RSProperty<std::shared_ptr<RSImage>>>(RSModifierType::BG_IMAGE, image);
825 }
826 
SetBgImageSize(float width,float height)827 void RSNode::SetBgImageSize(float width, float height)
828 {
829     SetBgImageWidth(width);
830     SetBgImageHeight(height);
831 }
832 
SetBgImageWidth(float width)833 void RSNode::SetBgImageWidth(float width)
834 {
835     SetProperty<RSBgImageWidthModifier, RSAnimatableProperty<float>>(RSModifierType::BG_IMAGE_WIDTH, width);
836 }
837 
SetBgImageHeight(float height)838 void RSNode::SetBgImageHeight(float height)
839 {
840     SetProperty<RSBgImageHeightModifier, RSAnimatableProperty<float>>(RSModifierType::BG_IMAGE_HEIGHT, height);
841 }
842 
SetBgImagePosition(float positionX,float positionY)843 void RSNode::SetBgImagePosition(float positionX, float positionY)
844 {
845     SetBgImagePositionX(positionX);
846     SetBgImagePositionY(positionY);
847 }
848 
SetBgImagePositionX(float positionX)849 void RSNode::SetBgImagePositionX(float positionX)
850 {
851     SetProperty<RSBgImagePositionXModifier, RSAnimatableProperty<float>>(
852         RSModifierType::BG_IMAGE_POSITION_X, positionX);
853 }
854 
SetBgImagePositionY(float positionY)855 void RSNode::SetBgImagePositionY(float positionY)
856 {
857     SetProperty<RSBgImagePositionYModifier, RSAnimatableProperty<float>>(
858         RSModifierType::BG_IMAGE_POSITION_Y, positionY);
859 }
860 
861 // border
SetBorderColor(uint32_t colorValue)862 void RSNode::SetBorderColor(uint32_t colorValue)
863 {
864     SetBorderColor(colorValue, colorValue, colorValue, colorValue);
865 }
866 
SetBorderColor(uint32_t left,uint32_t top,uint32_t right,uint32_t bottom)867 void RSNode::SetBorderColor(uint32_t left, uint32_t top, uint32_t right, uint32_t bottom)
868 {
869     Vector4<Color> color(Color::FromArgbInt(left), Color::FromArgbInt(top),
870                          Color::FromArgbInt(right), Color::FromArgbInt(bottom));
871     SetBorderColor(color);
872 }
873 
SetBorderColor(const Vector4<Color> & color)874 void RSNode::SetBorderColor(const Vector4<Color>& color)
875 {
876     SetProperty<RSBorderColorModifier, RSAnimatableProperty<Vector4<Color>>>(RSModifierType::BORDER_COLOR, color);
877 }
878 
SetBorderWidth(float width)879 void RSNode::SetBorderWidth(float width)
880 {
881     SetBorderWidth(width, width, width, width);
882 }
883 
SetBorderWidth(float left,float top,float right,float bottom)884 void RSNode::SetBorderWidth(float left, float top, float right, float bottom)
885 {
886     Vector4f width(left, top, right, bottom);
887     SetBorderWidth(width);
888 }
889 
SetBorderWidth(const Vector4f & width)890 void RSNode::SetBorderWidth(const Vector4f& width)
891 {
892     SetProperty<RSBorderWidthModifier, RSAnimatableProperty<Vector4f>>(RSModifierType::BORDER_WIDTH, width);
893 }
894 
SetBorderStyle(uint32_t styleValue)895 void RSNode::SetBorderStyle(uint32_t styleValue)
896 {
897     SetBorderStyle(styleValue, styleValue, styleValue, styleValue);
898 }
899 
SetBorderStyle(uint32_t left,uint32_t top,uint32_t right,uint32_t bottom)900 void RSNode::SetBorderStyle(uint32_t left, uint32_t top, uint32_t right, uint32_t bottom)
901 {
902     Vector4<BorderStyle> style(static_cast<BorderStyle>(left), static_cast<BorderStyle>(top),
903                                static_cast<BorderStyle>(right), static_cast<BorderStyle>(bottom));
904     SetBorderStyle(style);
905 }
906 
SetBorderStyle(const Vector4<BorderStyle> & style)907 void RSNode::SetBorderStyle(const Vector4<BorderStyle>& style)
908 {
909     Vector4<uint32_t> styles(static_cast<uint32_t>(style.x_), static_cast<uint32_t>(style.y_),
910                              static_cast<uint32_t>(style.z_), static_cast<uint32_t>(style.w_));
911     SetProperty<RSBorderStyleModifier, RSProperty<Vector4<uint32_t>>>(RSModifierType::BORDER_STYLE, styles);
912 }
913 
SetBackgroundFilter(const std::shared_ptr<RSFilter> & backgroundFilter)914 void RSNode::SetBackgroundFilter(const std::shared_ptr<RSFilter>& backgroundFilter)
915 {
916     SetProperty<RSBackgroundFilterModifier, RSAnimatableProperty<std::shared_ptr<RSFilter>>>(
917         RSModifierType::BACKGROUND_FILTER, backgroundFilter);
918 }
919 
SetFilter(const std::shared_ptr<RSFilter> & filter)920 void RSNode::SetFilter(const std::shared_ptr<RSFilter>& filter)
921 {
922     SetProperty<RSFilterModifier, RSAnimatableProperty<std::shared_ptr<RSFilter>>>(RSModifierType::FILTER, filter);
923 }
924 
SetLinearGradientBlurPara(const std::shared_ptr<RSLinearGradientBlurPara> & para)925 void RSNode::SetLinearGradientBlurPara(const std::shared_ptr<RSLinearGradientBlurPara>& para)
926 {
927     SetProperty<RSLinearGradientBlurParaModifier, RSProperty<std::shared_ptr<RSLinearGradientBlurPara>>>
928                                                                     (RSModifierType::LINEAR_GRADIENT_BLUR_PARA, para);
929 }
930 
SetDynamicLightUpRate(const float rate)931 void RSNode::SetDynamicLightUpRate(const float rate)
932 {
933     SetProperty<RSDynamicLightUpRateModifier, RSAnimatableProperty<float>>(RSModifierType::DYNAMIC_LIGHT_UP_RATE, rate);
934 }
935 
SetDynamicLightUpDegree(const float lightUpDegree)936 void RSNode::SetDynamicLightUpDegree(const float lightUpDegree)
937 {
938     SetProperty<RSDynamicLightUpDegreeModifier,
939         RSAnimatableProperty<float>>(RSModifierType::DYNAMIC_LIGHT_UP_DEGREE, lightUpDegree);
940 }
941 
SetCompositingFilter(const std::shared_ptr<RSFilter> & compositingFilter)942 void RSNode::SetCompositingFilter(const std::shared_ptr<RSFilter>& compositingFilter) {}
943 
SetShadowColor(uint32_t colorValue)944 void RSNode::SetShadowColor(uint32_t colorValue)
945 {
946     auto color = Color::FromArgbInt(colorValue);
947     SetProperty<RSShadowColorModifier, RSAnimatableProperty<Color>>(RSModifierType::SHADOW_COLOR, color);
948 }
949 
SetShadowOffset(float offsetX,float offsetY)950 void RSNode::SetShadowOffset(float offsetX, float offsetY)
951 {
952     SetShadowOffsetX(offsetX);
953     SetShadowOffsetY(offsetY);
954 }
955 
SetShadowOffsetX(float offsetX)956 void RSNode::SetShadowOffsetX(float offsetX)
957 {
958     SetProperty<RSShadowOffsetXModifier, RSAnimatableProperty<float>>(RSModifierType::SHADOW_OFFSET_X, offsetX);
959 }
960 
SetShadowOffsetY(float offsetY)961 void RSNode::SetShadowOffsetY(float offsetY)
962 {
963     SetProperty<RSShadowOffsetYModifier, RSAnimatableProperty<float>>(RSModifierType::SHADOW_OFFSET_Y, offsetY);
964 }
965 
SetShadowAlpha(float alpha)966 void RSNode::SetShadowAlpha(float alpha)
967 {
968     SetProperty<RSShadowAlphaModifier, RSAnimatableProperty<float>>(RSModifierType::SHADOW_ALPHA, alpha);
969 }
970 
SetShadowElevation(float elevation)971 void RSNode::SetShadowElevation(float elevation)
972 {
973     SetProperty<RSShadowElevationModifier, RSAnimatableProperty<float>>(RSModifierType::SHADOW_ELEVATION, elevation);
974 }
975 
SetShadowRadius(float radius)976 void RSNode::SetShadowRadius(float radius)
977 {
978     SetProperty<RSShadowRadiusModifier, RSAnimatableProperty<float>>(RSModifierType::SHADOW_RADIUS, radius);
979 }
980 
SetShadowPath(const std::shared_ptr<RSPath> & shadowPath)981 void RSNode::SetShadowPath(const std::shared_ptr<RSPath>& shadowPath)
982 {
983     SetProperty<RSShadowPathModifier, RSProperty<std::shared_ptr<RSPath>>>(RSModifierType::SHADOW_PATH, shadowPath);
984 }
985 
SetShadowMask(bool shadowMask)986 void RSNode::SetShadowMask(bool shadowMask)
987 {
988     SetProperty<RSShadowMaskModifier, RSProperty<bool>>(RSModifierType::SHADOW_MASK, shadowMask);
989 }
990 
SetFrameGravity(Gravity gravity)991 void RSNode::SetFrameGravity(Gravity gravity)
992 {
993     SetProperty<RSFrameGravityModifier, RSProperty<Gravity>>(RSModifierType::FRAME_GRAVITY, gravity);
994 }
995 
SetClipRRect(const Vector4f & clipRect,const Vector4f & clipRadius)996 void RSNode::SetClipRRect(const Vector4f& clipRect, const Vector4f& clipRadius)
997 {
998     SetProperty<RSClipRRectModifier, RSAnimatableProperty<RRect>>(
999         RSModifierType::CLIP_RRECT, RRect(clipRect, clipRadius));
1000 }
1001 
SetClipBounds(const std::shared_ptr<RSPath> & path)1002 void RSNode::SetClipBounds(const std::shared_ptr<RSPath>& path)
1003 {
1004     SetProperty<RSClipBoundsModifier, RSProperty<std::shared_ptr<RSPath>>>(RSModifierType::CLIP_BOUNDS, path);
1005 }
1006 
SetClipToBounds(bool clipToBounds)1007 void RSNode::SetClipToBounds(bool clipToBounds)
1008 {
1009     SetProperty<RSClipToBoundsModifier, RSProperty<bool>>(RSModifierType::CLIP_TO_BOUNDS, clipToBounds);
1010 }
1011 
SetClipToFrame(bool clipToFrame)1012 void RSNode::SetClipToFrame(bool clipToFrame)
1013 {
1014     SetProperty<RSClipToFrameModifier, RSProperty<bool>>(RSModifierType::CLIP_TO_FRAME, clipToFrame);
1015 }
1016 
SetVisible(bool visible)1017 void RSNode::SetVisible(bool visible)
1018 {
1019     // kick off transition only if it's on tree(has valid parent) and visibility is changed.
1020     if (transitionEffect_ != nullptr && GetParent() != nullptr && visible != GetStagingProperties().GetVisible()) {
1021         NotifyTransition(transitionEffect_, visible);
1022     }
1023 
1024     SetProperty<RSVisibleModifier, RSProperty<bool>>(RSModifierType::VISIBLE, visible);
1025 }
1026 
SetMask(const std::shared_ptr<RSMask> & mask)1027 void RSNode::SetMask(const std::shared_ptr<RSMask>& mask)
1028 {
1029     SetProperty<RSMaskModifier, RSProperty<std::shared_ptr<RSMask>>>(RSModifierType::MASK, mask);
1030 }
1031 
SetUseEffect(bool useEffect)1032 void RSNode::SetUseEffect(bool useEffect)
1033 {
1034     SetProperty<RSUseEffectModifier, RSProperty<bool>>(RSModifierType::USE_EFFECT, useEffect);
1035 }
1036 
SetPixelStretch(const Vector4f & stretchSize)1037 void RSNode::SetPixelStretch(const Vector4f& stretchSize)
1038 {
1039     SetProperty<RSPixelStretchModifier, RSAnimatableProperty<Vector4f>>(RSModifierType::PIXEL_STRETCH, stretchSize);
1040 }
1041 
SetPixelStretchPercent(const Vector4f & stretchPercent)1042 void RSNode::SetPixelStretchPercent(const Vector4f& stretchPercent)
1043 {
1044     SetProperty<RSPixelStretchPercentModifier, RSAnimatableProperty<Vector4f>>(RSModifierType::PIXEL_STRETCH_PERCENT,
1045         stretchPercent);
1046 }
1047 
SetFreeze(bool isFreeze)1048 void RSNode::SetFreeze(bool isFreeze)
1049 {
1050     ROSEN_LOGE("SetFreeze only support RSSurfaceNode and RSCanvasNode in uniRender");
1051 }
1052 
SetSpherizeDegree(float spherizeDegree)1053 void RSNode::SetSpherizeDegree(float spherizeDegree)
1054 {
1055     SetProperty<RSSpherizeModifier, RSAnimatableProperty<float>>(RSModifierType::SPHERIZE, spherizeDegree);
1056 }
1057 
SetLightUpEffectDegree(float LightUpEffectDegree)1058 void RSNode::SetLightUpEffectDegree(float LightUpEffectDegree)
1059 {
1060     SetProperty<RSLightUpEffectModifier, RSAnimatableProperty<float>>(
1061         RSModifierType::LIGHT_UP_EFFECT, LightUpEffectDegree);
1062 }
1063 
NotifyTransition(const std::shared_ptr<const RSTransitionEffect> & effect,bool isTransitionIn)1064 void RSNode::NotifyTransition(const std::shared_ptr<const RSTransitionEffect>& effect, bool isTransitionIn)
1065 {
1066     // temporary fix for multithread issue in implicit animator
1067     UpdateImplicitAnimator();
1068     if (implicitAnimator_ == nullptr) {
1069         ROSEN_LOGE("Failed to notify transition, implicit animator is null!");
1070         return;
1071     }
1072 
1073     if (!implicitAnimator_->NeedImplicitAnimation()) {
1074         return;
1075     }
1076 
1077     auto& customEffects = isTransitionIn ? effect->customTransitionInEffects_ : effect->customTransitionOutEffects_;
1078     // temporary close the implicit animation
1079     ExecuteWithoutAnimation(
1080         [&customEffects] {
1081             for (auto& customEffect : customEffects) {
1082                 customEffect->Active();
1083             }
1084         },
1085         implicitAnimator_);
1086 
1087     implicitAnimator_->BeginImplicitTransition(effect, isTransitionIn);
1088     for (auto& customEffect : customEffects) {
1089         customEffect->Identity();
1090     }
1091     implicitAnimator_->CreateImplicitTransition(*this);
1092     implicitAnimator_->EndImplicitTransition();
1093 }
1094 
OnAddChildren()1095 void RSNode::OnAddChildren()
1096 {
1097     // kick off transition only if it's visible.
1098     if (transitionEffect_ != nullptr && GetStagingProperties().GetVisible()) {
1099         NotifyTransition(transitionEffect_, true);
1100     }
1101 }
1102 
OnRemoveChildren()1103 void RSNode::OnRemoveChildren()
1104 {
1105     // kick off transition only if it's visible.
1106     if (transitionEffect_ != nullptr && GetStagingProperties().GetVisible()) {
1107         NotifyTransition(transitionEffect_, false);
1108     }
1109 }
1110 
AnimationCallback(AnimationId animationId,AnimationCallbackEvent event)1111 bool RSNode::AnimationCallback(AnimationId animationId, AnimationCallbackEvent event)
1112 {
1113     std::shared_ptr<RSAnimation> animation = nullptr;
1114     {
1115         std::unique_lock<std::mutex> lock(animationMutex_);
1116         auto animationItr = animations_.find(animationId);
1117         if (animationItr == animations_.end()) {
1118             ROSEN_LOGE("Failed to find animation[%" PRIu64 "]!", animationId);
1119             return false;
1120         }
1121         animation = animationItr->second;
1122     }
1123 
1124     if (animation == nullptr) {
1125         ROSEN_LOGE("Failed to callback animation[%" PRIu64 "], animation is null!", animationId);
1126         return false;
1127     }
1128     if (event == FINISHED) {
1129         RemoveAnimationInner(animation);
1130         animation->CallFinishCallback();
1131         return true;
1132     } else if (event == REPEAT_FINISHED) {
1133         animation->CallRepeatCallback();
1134         return true;
1135     }
1136     ROSEN_LOGE("Failed to callback animation event[%" PRIu64 "], event is null!", event);
1137     return false;
1138 }
1139 
SetPaintOrder(bool drawContentLast)1140 void RSNode::SetPaintOrder(bool drawContentLast)
1141 {
1142     drawContentLast_ = drawContentLast;
1143 }
1144 
MarkDrivenRender(bool flag)1145 void RSNode::MarkDrivenRender(bool flag)
1146 {
1147     if (drivenFlag_ != flag) {
1148         std::unique_ptr<RSCommand> command = std::make_unique<RSMarkDrivenRender>(GetId(), flag);
1149         auto transactionProxy = RSTransactionProxy::GetInstance();
1150         if (transactionProxy != nullptr) {
1151             transactionProxy->AddCommand(command, IsRenderServiceNode());
1152         }
1153         drivenFlag_ = flag;
1154     }
1155 }
1156 
MarkDrivenRenderItemIndex(int index)1157 void RSNode::MarkDrivenRenderItemIndex(int index)
1158 {
1159     std::unique_ptr<RSCommand> command = std::make_unique<RSMarkDrivenRenderItemIndex>(GetId(), index);
1160     auto transactionProxy = RSTransactionProxy::GetInstance();
1161     if (transactionProxy != nullptr) {
1162         transactionProxy->AddCommand(command, IsRenderServiceNode());
1163     }
1164 }
1165 
MarkDrivenRenderFramePaintState(bool flag)1166 void RSNode::MarkDrivenRenderFramePaintState(bool flag)
1167 {
1168     std::unique_ptr<RSCommand> command =
1169         std::make_unique<RSMarkDrivenRenderFramePaintState>(GetId(), flag);
1170     auto transactionProxy = RSTransactionProxy::GetInstance();
1171     if (transactionProxy != nullptr) {
1172         transactionProxy->AddCommand(command, IsRenderServiceNode());
1173     }
1174 }
1175 
MarkContentChanged(bool isChanged)1176 void RSNode::MarkContentChanged(bool isChanged)
1177 {
1178     std::unique_ptr<RSCommand> command = std::make_unique<RSMarkContentChanged>(GetId(), isChanged);
1179     auto transactionProxy = RSTransactionProxy::GetInstance();
1180     if (transactionProxy != nullptr) {
1181         transactionProxy->AddCommand(command, IsRenderServiceNode());
1182     }
1183 }
1184 
ClearAllModifiers()1185 void RSNode::ClearAllModifiers()
1186 {
1187     for (auto [id, modifier] : modifiers_) {
1188         if (modifier) {
1189             modifier->DetachFromNode();
1190         }
1191     }
1192 }
1193 
AddModifier(const std::shared_ptr<RSModifier> modifier)1194 void RSNode::AddModifier(const std::shared_ptr<RSModifier> modifier)
1195 {
1196     if (!modifier || modifiers_.count(modifier->GetPropertyId())) {
1197         return;
1198     }
1199     if (motionPathOption_ != nullptr && IsPathAnimatableModifier(modifier->GetModifierType())) {
1200         modifier->SetMotionPathOption(motionPathOption_);
1201     }
1202     auto rsnode = std::static_pointer_cast<RSNode>(shared_from_this());
1203     modifier->AttachToNode(rsnode);
1204     modifiers_.emplace(modifier->GetPropertyId(), modifier);
1205     if (modifier->GetModifierType() == RSModifierType::NODE_MODIFIER) {
1206         return;
1207     }
1208     std::unique_ptr<RSCommand> command = std::make_unique<RSAddModifier>(GetId(), modifier->CreateRenderModifier());
1209     auto transactionProxy = RSTransactionProxy::GetInstance();
1210     if (transactionProxy != nullptr) {
1211         transactionProxy->AddCommand(command, IsRenderServiceNode(), GetFollowType(), GetId());
1212         if (NeedForcedSendToRemote()) {
1213             std::unique_ptr<RSCommand> cmdForRemote =
1214                 std::make_unique<RSAddModifier>(GetId(), modifier->CreateRenderModifier());
1215             transactionProxy->AddCommand(cmdForRemote, true, GetFollowType(), GetId());
1216         }
1217     }
1218 }
1219 
RemoveModifier(const std::shared_ptr<RSModifier> modifier)1220 void RSNode::RemoveModifier(const std::shared_ptr<RSModifier> modifier)
1221 {
1222     if (!modifier) {
1223         return;
1224     }
1225     auto iter = modifiers_.find(modifier->GetPropertyId());
1226     if (iter == modifiers_.end()) {
1227         return;
1228     }
1229 
1230     modifiers_.erase(iter);
1231     modifier->DetachFromNode();
1232     std::unique_ptr<RSCommand> command = std::make_unique<RSRemoveModifier>(GetId(), modifier->GetPropertyId());
1233     auto transactionProxy = RSTransactionProxy::GetInstance();
1234     if (transactionProxy != nullptr) {
1235         transactionProxy->AddCommand(command, IsRenderServiceNode(), GetFollowType(), GetId());
1236         if (NeedForcedSendToRemote()) {
1237             std::unique_ptr<RSCommand> cmdForRemote =
1238                 std::make_unique<RSRemoveModifier>(GetId(), modifier->GetPropertyId());
1239             transactionProxy->AddCommand(cmdForRemote, true, GetFollowType(), GetId());
1240         }
1241     }
1242 }
1243 
GetModifier(const PropertyId & propertyId)1244 const std::shared_ptr<RSModifier> RSNode::GetModifier(const PropertyId& propertyId)
1245 {
1246     auto iter = modifiers_.find(propertyId);
1247     if (iter != modifiers_.end()) {
1248         return iter->second;
1249     }
1250 
1251     return {};
1252 }
1253 
UpdateModifierMotionPathOption()1254 void RSNode::UpdateModifierMotionPathOption()
1255 {
1256     for (auto& [type, modifier] : propertyModifiers_) {
1257         if (IsPathAnimatableModifier(type)) {
1258             modifier->SetMotionPathOption(motionPathOption_);
1259         }
1260     }
1261     for (auto& [id, modifier] : modifiers_) {
1262         if (IsPathAnimatableModifier(modifier->GetModifierType())) {
1263             modifier->SetMotionPathOption(motionPathOption_);
1264         }
1265     }
1266 }
1267 
UpdateImplicitAnimator()1268 void RSNode::UpdateImplicitAnimator()
1269 {
1270     auto tid = gettid();
1271     if (tid == implicitAnimatorTid_) {
1272         return;
1273     }
1274     implicitAnimatorTid_ = tid;
1275     implicitAnimator_ = RSImplicitAnimatorMap::Instance().GetAnimator(tid);
1276 }
1277 
GetModifierIds() const1278 std::vector<PropertyId> RSNode::GetModifierIds() const
1279 {
1280     std::vector<PropertyId> ids;
1281     for (const auto& [id, _] : modifiers_) {
1282         ids.push_back(id);
1283     }
1284     return ids;
1285 }
1286 
MarkAllExtendModifierDirty()1287 void RSNode::MarkAllExtendModifierDirty()
1288 {
1289     if (extendModifierIsDirty_) {
1290         return;
1291     }
1292 
1293     extendModifierIsDirty_ = true;
1294     for (auto& [id, modifier] : modifiers_) {
1295         if (modifier->GetModifierType() < RSModifierType::CUSTOM) {
1296             continue;
1297         }
1298         modifier->SetDirty(true);
1299     }
1300 }
1301 
ResetExtendModifierDirty()1302 void RSNode::ResetExtendModifierDirty()
1303 {
1304     extendModifierIsDirty_ = false;
1305 }
1306 
SetIsCustomTextType(bool isCustomTextType)1307 void RSNode::SetIsCustomTextType(bool isCustomTextType)
1308 {
1309     isCustomTextType_ = isCustomTextType;
1310 }
1311 
GetIsCustomTextType()1312 bool RSNode::GetIsCustomTextType()
1313 {
1314     return isCustomTextType_;
1315 }
1316 
SetDrawRegion(std::shared_ptr<RectF> rect)1317 void RSNode::SetDrawRegion(std::shared_ptr<RectF> rect)
1318 {
1319     if (drawRegion_ != rect) {
1320         drawRegion_ = rect;
1321         std::unique_ptr<RSCommand> command = std::make_unique<RSSetDrawRegion>(GetId(), rect);
1322         auto transactionProxy = RSTransactionProxy::GetInstance();
1323         if (transactionProxy != nullptr) {
1324             transactionProxy->AddCommand(command, IsRenderServiceNode(), GetFollowType(), GetId());
1325         }
1326     }
1327 }
1328 
RegisterTransitionPair(NodeId inNodeId,NodeId outNodeId)1329 void RSNode::RegisterTransitionPair(NodeId inNodeId, NodeId outNodeId)
1330 {
1331     std::unique_ptr<RSCommand> command =
1332         std::make_unique<RSRegisterGeometryTransitionNodePair>(inNodeId, outNodeId);
1333     auto transactionProxy = RSTransactionProxy::GetInstance();
1334     if (transactionProxy != nullptr) {
1335         transactionProxy->AddCommand(command, true);
1336     }
1337 }
1338 
UnregisterTransitionPair(NodeId inNodeId,NodeId outNodeId)1339 void RSNode::UnregisterTransitionPair(NodeId inNodeId, NodeId outNodeId)
1340 {
1341     std::unique_ptr<RSCommand> command =
1342         std::make_unique<RSUnregisterGeometryTransitionNodePair>(inNodeId, outNodeId);
1343     auto transactionProxy = RSTransactionProxy::GetInstance();
1344     if (transactionProxy != nullptr) {
1345         transactionProxy->AddCommand(command, true);
1346     }
1347 }
1348 
MarkNodeGroup(bool isNodeGroup)1349 void RSNode::MarkNodeGroup(bool isNodeGroup)
1350 {
1351     if (isNodeGroup_ == isNodeGroup) {
1352         return;
1353     }
1354     isNodeGroup_ = isNodeGroup;
1355     std::unique_ptr<RSCommand> command = std::make_unique<RSMarkNodeGroup>(GetId(), isNodeGroup);
1356     auto transactionProxy = RSTransactionProxy::GetInstance();
1357     if (transactionProxy != nullptr) {
1358         transactionProxy->AddCommand(command, IsRenderServiceNode());
1359     }
1360 }
1361 
1362 
SetGrayScale(float grayScale)1363 void RSNode::SetGrayScale(float grayScale)
1364 {
1365     SetProperty<RSGrayScaleModifier, RSAnimatableProperty<float>>(RSModifierType::GRAY_SCALE, grayScale);
1366 }
1367 
SetBrightness(float brightness)1368 void RSNode::SetBrightness(float brightness)
1369 {
1370     SetProperty<RSBrightnessModifier, RSAnimatableProperty<float>>(RSModifierType::BRIGHTNESS, brightness);
1371 }
1372 
SetContrast(float contrast)1373 void RSNode::SetContrast(float contrast)
1374 {
1375     SetProperty<RSContrastModifier, RSAnimatableProperty<float>>(RSModifierType::CONTRAST, contrast);
1376 }
1377 
SetSaturate(float saturate)1378 void RSNode::SetSaturate(float saturate)
1379 {
1380     SetProperty<RSSaturateModifier, RSAnimatableProperty<float>>(RSModifierType::SATURATE, saturate);
1381 }
1382 
SetSepia(float sepia)1383 void RSNode::SetSepia(float sepia)
1384 {
1385     SetProperty<RSSepiaModifier, RSAnimatableProperty<float>>(RSModifierType::SEPIA, sepia);
1386 }
1387 
SetInvert(float invert)1388 void RSNode::SetInvert(float invert)
1389 {
1390     SetProperty<RSInvertModifier, RSAnimatableProperty<float>>(RSModifierType::INVERT, invert);
1391 }
1392 
SetHueRotate(float hueRotate)1393 void RSNode::SetHueRotate(float hueRotate)
1394 {
1395     SetProperty<RSHueRotateModifier, RSAnimatableProperty<float>>(RSModifierType::HUE_ROTATE, hueRotate);
1396 }
1397 
SetColorBlend(uint32_t colorValue)1398 void RSNode::SetColorBlend(uint32_t colorValue)
1399 {
1400     auto colorBlend = Color::FromArgbInt(colorValue);
1401     SetProperty<RSColorBlendModifier, RSAnimatableProperty<Color>>(RSModifierType::COLOR_BLEND, colorBlend);
1402 }
1403 
UpdateFrameRateRange(FrameRateRange range)1404 void RSNode::UpdateFrameRateRange(FrameRateRange range)
1405 {
1406     if (!range.IsValid()) {
1407         return;
1408     }
1409     nodeRange_ = range;
1410 
1411     std::unique_ptr<RSCommand> command = std::make_unique<RSSetUIFrameRateRange>(GetId(), range);
1412     auto transactionProxy = RSTransactionProxy::GetInstance();
1413     if (transactionProxy != nullptr) {
1414         transactionProxy->AddCommand(command, IsRenderServiceNode());
1415     }
1416 }
1417 
GenerateId()1418 NodeId RSNode::GenerateId()
1419 {
1420     static pid_t pid_ = GetRealPid();
1421     static std::atomic<uint32_t> currentId_ = 1; // surfaceNode is seted correctly during boot when currentId is 1
1422 
1423     auto currentId = currentId_.fetch_add(1, std::memory_order_relaxed);
1424     if (currentId == UINT32_MAX) {
1425         // [PLANNING]:process the overflow situations
1426         ROSEN_LOGE("Node Id overflow");
1427     }
1428 
1429     // concat two 32-bit numbers to one 64-bit number
1430     return ((NodeId)pid_ << 32) | currentId;
1431 }
1432 
InitUniRenderEnabled()1433 void RSNode::InitUniRenderEnabled()
1434 {
1435     static bool inited = false;
1436     if (!inited) {
1437         inited = true;
1438         g_isUniRenderEnabled = RSSystemProperties::GetUniRenderEnabled();
1439         ROSEN_LOGD("RSNode::InitUniRenderEnabled:%d", g_isUniRenderEnabled);
1440     }
1441 }
1442 
1443 
1444 // RSNode::~RSNode()
1445 // {
1446 
1447 // }
1448 
IsUniRenderEnabled() const1449 bool RSNode::IsUniRenderEnabled() const
1450 {
1451     return g_isUniRenderEnabled;
1452 }
1453 
IsRenderServiceNode() const1454 bool RSNode::IsRenderServiceNode() const
1455 {
1456     return g_isUniRenderEnabled || isRenderServiceNode_;
1457 }
1458 
AddChild(SharedPtr child,int index)1459 void RSNode::AddChild(SharedPtr child, int index)
1460 {
1461     if (child == nullptr) {
1462         ROSEN_LOGE("RSNode::AddChild, child is nullptr");
1463         return;
1464     }
1465     if (child->parent_ == id_) {
1466         ROSEN_LOGI("RSNode::AddChild, child already exist");
1467         return;
1468     }
1469     if (child->GetType() == RSUINodeType::DISPLAY_NODE) {
1470         // Disallow to add display node as child.
1471         return;
1472     }
1473     NodeId childId = child->GetId();
1474     if (child->parent_ != 0) {
1475         child->RemoveFromTree();
1476     }
1477 
1478     if (index < 0 || index >= static_cast<int>(children_.size())) {
1479         children_.push_back(childId);
1480     } else {
1481         children_.insert(children_.begin() + index, childId);
1482     }
1483     child->SetParent(id_);
1484     child->OnAddChildren();
1485     auto transactionProxy = RSTransactionProxy::GetInstance();
1486     if (transactionProxy == nullptr) {
1487         return;
1488     }
1489     // construct command using child's GetHierarchyCommandNodeId(), not GetId()
1490     childId = child->GetHierarchyCommandNodeId();
1491     std::unique_ptr<RSCommand> command = std::make_unique<RSBaseNodeAddChild>(id_, childId, index);
1492     transactionProxy->AddCommand(command, IsRenderServiceNode(), GetFollowType(), id_);
1493 }
1494 
MoveChild(SharedPtr child,int index)1495 void RSNode::MoveChild(SharedPtr child, int index)
1496 {
1497     if (child == nullptr || child->parent_ != id_) {
1498         ROSEN_LOGD("RSNode::MoveChild, not valid child");
1499         return;
1500     }
1501     NodeId childId = child->GetId();
1502     auto itr = std::find(children_.begin(), children_.end(), childId);
1503     if (itr == children_.end()) {
1504         ROSEN_LOGD("RSNode::MoveChild, not child");
1505         return;
1506     }
1507     children_.erase(itr);
1508     if (index < 0 || index >= static_cast<int>(children_.size())) {
1509         children_.push_back(childId);
1510     } else {
1511         children_.insert(children_.begin() + index, childId);
1512     }
1513 
1514     auto transactionProxy = RSTransactionProxy::GetInstance();
1515     if (transactionProxy == nullptr) {
1516         return;
1517     }
1518     // construct command using child's GetHierarchyCommandNodeId(), not GetId()
1519     childId = child->GetHierarchyCommandNodeId();
1520     std::unique_ptr<RSCommand> command = std::make_unique<RSBaseNodeMoveChild>(id_, childId, index);
1521     transactionProxy->AddCommand(command, IsRenderServiceNode(), GetFollowType(), id_);
1522 }
1523 
RemoveChild(SharedPtr child)1524 void RSNode::RemoveChild(SharedPtr child)
1525 {
1526     if (child == nullptr || child->parent_ != id_) {
1527         ROSEN_LOGI("RSNode::RemoveChild, child is nullptr");
1528         return;
1529     }
1530     NodeId childId = child->GetId();
1531     RemoveChildById(childId);
1532     child->OnRemoveChildren();
1533     child->SetParent(0);
1534 
1535     auto transactionProxy = RSTransactionProxy::GetInstance();
1536     if (transactionProxy == nullptr) {
1537         return;
1538     }
1539     // construct command using child's GetHierarchyCommandNodeId(), not GetId()
1540     childId = child->GetHierarchyCommandNodeId();
1541     std::unique_ptr<RSCommand> command = std::make_unique<RSBaseNodeRemoveChild>(id_, childId);
1542     transactionProxy->AddCommand(command, IsRenderServiceNode(), GetFollowType(), id_);
1543 }
1544 
AddCrossParentChild(SharedPtr child,int index)1545 void RSNode::AddCrossParentChild(SharedPtr child, int index)
1546 {
1547     // AddCrossParentChild only used as: the child is under multiple parents(e.g. a window cross multi-screens),
1548     // so this child will not remove from the old parent.
1549     if (child == nullptr) {
1550         ROSEN_LOGE("RSNode::AddCrossScreenChild, child is nullptr");
1551         return;
1552     }
1553     if (!this->IsInstanceOf<RSDisplayNode>()) {
1554         ROSEN_LOGE("RSNode::AddCrossScreenChild, only displayNode support AddCrossScreenChild");
1555         return;
1556     }
1557     NodeId childId = child->GetId();
1558 
1559     if (index < 0 || index >= static_cast<int>(children_.size())) {
1560         children_.push_back(childId);
1561     } else {
1562         children_.insert(children_.begin() + index, childId);
1563     }
1564     child->SetParent(id_);
1565     child->OnAddChildren();
1566     auto transactionProxy = RSTransactionProxy::GetInstance();
1567     if (transactionProxy == nullptr) {
1568         return;
1569     }
1570     // construct command using child's GetHierarchyCommandNodeId(), not GetId()
1571     childId = child->GetHierarchyCommandNodeId();
1572     std::unique_ptr<RSCommand> command = std::make_unique<RSBaseNodeAddCrossParentChild>(id_, childId, index);
1573     transactionProxy->AddCommand(command, IsRenderServiceNode(), GetFollowType(), id_);
1574 }
1575 
RemoveCrossParentChild(SharedPtr child,NodeId newParentId)1576 void RSNode::RemoveCrossParentChild(SharedPtr child, NodeId newParentId)
1577 {
1578     // RemoveCrossParentChild only used as: the child is under multiple parents(e.g. a window cross multi-screens),
1579     // set the newParentId to rebuild the parent-child relationship.
1580     if (child == nullptr) {
1581         ROSEN_LOGI("RSNode::RemoveCrossScreenChild, child is nullptr");
1582         return;
1583     }
1584     if (!this->IsInstanceOf<RSDisplayNode>()) {
1585         ROSEN_LOGE("RSNode::RemoveCrossScreenChild, only displayNode support RemoveCrossScreenChild");
1586         return;
1587     }
1588     NodeId childId = child->GetId();
1589     RemoveChildById(childId);
1590     child->OnRemoveChildren();
1591     child->SetParent(newParentId);
1592 
1593     auto transactionProxy = RSTransactionProxy::GetInstance();
1594     if (transactionProxy == nullptr) {
1595         return;
1596     }
1597     // construct command using child's GetHierarchyCommandNodeId(), not GetId()
1598     childId = child->GetHierarchyCommandNodeId();
1599     std::unique_ptr<RSCommand> command = std::make_unique<RSBaseNodeRemoveCrossParentChild>(id_, childId, newParentId);
1600     transactionProxy->AddCommand(command, IsRenderServiceNode(), GetFollowType(), id_);
1601 }
1602 
RemoveChildById(NodeId childId)1603 void RSNode::RemoveChildById(NodeId childId)
1604 {
1605     auto itr = std::find(children_.begin(), children_.end(), childId);
1606     if (itr != children_.end()) {
1607         children_.erase(itr);
1608     }
1609 }
1610 
RemoveFromTree()1611 void RSNode::RemoveFromTree()
1612 {
1613     if (auto parentPtr = RSNodeMap::Instance().GetNode(parent_)) {
1614         parentPtr->RemoveChildById(GetId());
1615         OnRemoveChildren();
1616         SetParent(0);
1617     }
1618     // always send Remove-From-Tree command
1619     auto transactionProxy = RSTransactionProxy::GetInstance();
1620     if (transactionProxy == nullptr) {
1621         return;
1622     }
1623     // construct command using own GetHierarchyCommandNodeId(), not GetId()
1624     auto nodeId = GetHierarchyCommandNodeId();
1625     std::unique_ptr<RSCommand> command = std::make_unique<RSBaseNodeRemoveFromTree>(nodeId);
1626     transactionProxy->AddCommand(command, IsRenderServiceNode(), GetFollowType(), nodeId);
1627 }
1628 
ClearChildren()1629 void RSNode::ClearChildren()
1630 {
1631     for (auto child : children_) {
1632         if (auto childPtr = RSNodeMap::Instance().GetNode(child)) {
1633             childPtr->SetParent(0);
1634         }
1635     }
1636     children_.clear();
1637 
1638     auto transactionProxy = RSTransactionProxy::GetInstance();
1639     if (transactionProxy == nullptr) {
1640         return;
1641     }
1642     // construct command using own GetHierarchyCommandNodeId(), not GetId()
1643     auto nodeId = GetHierarchyCommandNodeId();
1644     std::unique_ptr<RSCommand> command = std::make_unique<RSBaseNodeClearChild>(nodeId);
1645     transactionProxy->AddCommand(command, IsRenderServiceNode(), GetFollowType(), nodeId);
1646 }
1647 
SetParent(NodeId parentId)1648 void RSNode::SetParent(NodeId parentId)
1649 {
1650     parent_ = parentId;
1651 }
1652 
GetParent()1653 RSNode::SharedPtr RSNode::GetParent()
1654 {
1655     return RSNodeMap::Instance().GetNode(parent_);
1656 }
1657 
DumpNode(int depth) const1658 std::string RSNode::DumpNode(int depth) const
1659 {
1660     std::stringstream ss;
1661     auto it = RSUINodeTypeStrs.find(GetType());
1662     if (it == RSUINodeTypeStrs.end()) {
1663         return "";
1664     }
1665     ss << it->second << "[" << std::to_string(id_) << "] child[";
1666     for (auto child : children_) {
1667         ss << std::to_string(child) << " ";
1668     }
1669     ss << "]";
1670 
1671     if (!animations_.empty()) {
1672         ss << " animation:" << std::to_string(animations_.size());
1673     }
1674     ss << " " << GetStagingProperties().Dump();
1675     return ss.str();
1676 }
1677 
IsInstanceOf(RSUINodeType type) const1678 bool RSNode::IsInstanceOf(RSUINodeType type) const
1679 {
1680     auto targetType = static_cast<uint32_t>(type);
1681     auto instanceType = static_cast<uint32_t>(GetType());
1682     // use bitmask to check whether the instance is a subclass of the target type
1683     return (instanceType & targetType) == targetType;
1684 }
1685 
1686 template<typename T>
IsInstanceOf() const1687 bool RSNode::IsInstanceOf() const
1688 {
1689     return IsInstanceOf(T::Type);
1690 }
1691 
1692 // explicit instantiation with all render node types
1693 template bool RSNode::IsInstanceOf<RSDisplayNode>() const;
1694 template bool RSNode::IsInstanceOf<RSSurfaceNode>() const;
1695 template bool RSNode::IsInstanceOf<RSProxyNode>() const;
1696 template bool RSNode::IsInstanceOf<RSCanvasNode>() const;
1697 template bool RSNode::IsInstanceOf<RSRootNode>() const;
1698 template bool RSNode::IsInstanceOf<RSCanvasDrawingNode>() const;
1699 
1700 } // namespace Rosen
1701 } // namespace OHOS
1702