• 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 "animation/rs_animation.h"
24 #include "animation/rs_animation_manager_map.h"
25 #include "animation/rs_implicit_animator.h"
26 #include "animation/rs_implicit_animator_map.h"
27 #include "command/rs_node_command.h"
28 #include "common/rs_color.h"
29 #include "common/rs_obj_geometry.h"
30 #include "modifier/rs_modifier.h"
31 #include "pipeline/rs_node_map.h"
32 #include "platform/common/rs_log.h"
33 #include "render/rs_path.h"
34 #include "transaction/rs_transaction_proxy.h"
35 #include "animation/rs_ui_animation_manager.h"
36 #include "modifier/rs_property_modifier.h"
37 
38 #ifdef _WIN32
39 #include <windows.h>
40 #define gettid GetCurrentThreadId
41 #endif
42 
43 #ifdef __APPLE__
44 #define gettid getpid
45 #endif
46 
47 #ifdef __gnu_linux__
48 #include <sys/types.h>
49 #include <sys/syscall.h>
50 #define gettid []() -> int32_t { return static_cast<int32_t>(syscall(SYS_gettid)); }
51 #endif
52 
53 namespace OHOS {
54 namespace Rosen {
55 namespace {
IsPathAnimatableModifier(const RSModifierType & type)56 bool IsPathAnimatableModifier(const RSModifierType& type)
57 {
58     if (type == RSModifierType::BOUNDS || type == RSModifierType::FRAME || type == RSModifierType::TRANSLATE) {
59         return true;
60     }
61     return false;
62 }
63 }
64 
RSNode(bool isRenderServiceNode)65 RSNode::RSNode(bool isRenderServiceNode)
66     : RSBaseNode(isRenderServiceNode), stagingPropertiesExtractor_(GetId())
67 {
68     UpdateImplicitAnimator();
69 }
70 
RSNode(bool isRenderServiceNode,NodeId id)71 RSNode::RSNode(bool isRenderServiceNode, NodeId id)
72     : RSBaseNode(isRenderServiceNode, id), stagingPropertiesExtractor_(id)
73 {
74     UpdateImplicitAnimator();
75 }
76 
~RSNode()77 RSNode::~RSNode()
78 {
79     FallbackAnimationsToRoot();
80     ClearAllModifiers();
81 }
82 
OpenImplicitAnimation(const RSAnimationTimingProtocol & timingProtocol,const RSAnimationTimingCurve & timingCurve,const std::function<void ()> & finishCallback)83 void RSNode::OpenImplicitAnimation(const RSAnimationTimingProtocol& timingProtocol,
84     const RSAnimationTimingCurve& timingCurve, const std::function<void()>& finishCallback)
85 {
86     auto implicitAnimator = RSImplicitAnimatorMap::Instance().GetAnimator(gettid());
87     if (implicitAnimator == nullptr) {
88         ROSEN_LOGE("Failed to open implicit animation, implicit animator is null!");
89         return;
90     }
91 
92     implicitAnimator->OpenImplicitAnimation(timingProtocol, timingCurve, finishCallback);
93 }
94 
CloseImplicitAnimation()95 std::vector<std::shared_ptr<RSAnimation>> RSNode::CloseImplicitAnimation()
96 {
97     auto implicitAnimator = RSImplicitAnimatorMap::Instance().GetAnimator(gettid());
98     if (implicitAnimator == nullptr) {
99         ROSEN_LOGE("Failed to close implicit animation, implicit animator is null!");
100         return {};
101     }
102 
103     return implicitAnimator->CloseImplicitAnimation();
104 }
105 
AddKeyFrame(float fraction,const RSAnimationTimingCurve & timingCurve,const PropertyCallback & propertyCallback)106 void RSNode::AddKeyFrame(
107     float fraction, const RSAnimationTimingCurve& timingCurve, const PropertyCallback& propertyCallback)
108 {
109     auto implicitAnimator = RSImplicitAnimatorMap::Instance().GetAnimator(gettid());
110     if (implicitAnimator == nullptr) {
111         ROSEN_LOGE("Failed to add keyframe, implicit animator is null!");
112         return;
113     }
114 
115     implicitAnimator->BeginImplicitKeyFrameAnimation(fraction, timingCurve);
116     propertyCallback();
117     implicitAnimator->EndImplicitKeyFrameAnimation();
118 }
119 
AddKeyFrame(float fraction,const PropertyCallback & propertyCallback)120 void RSNode::AddKeyFrame(float fraction, const PropertyCallback& propertyCallback)
121 {
122     auto implicitAnimator = RSImplicitAnimatorMap::Instance().GetAnimator(gettid());
123     if (implicitAnimator == nullptr) {
124         ROSEN_LOGE("Failed to add keyframe, implicit animator is null!");
125         return;
126     }
127 
128     implicitAnimator->BeginImplicitKeyFrameAnimation(fraction);
129     propertyCallback();
130     implicitAnimator->EndImplicitKeyFrameAnimation();
131 }
132 
Animate(const RSAnimationTimingProtocol & timingProtocol,const RSAnimationTimingCurve & timingCurve,const PropertyCallback & propertyCallback,const std::function<void ()> & finishCallback)133 std::vector<std::shared_ptr<RSAnimation>> RSNode::Animate(const RSAnimationTimingProtocol& timingProtocol,
134     const RSAnimationTimingCurve& timingCurve, const PropertyCallback& propertyCallback,
135     const std::function<void()>& finishCallback)
136 {
137     if (propertyCallback == nullptr) {
138         ROSEN_LOGE("Failed to add curve animation, property callback is null!");
139         return {};
140     }
141 
142     if (RSImplicitAnimatorMap::Instance().GetAnimator(gettid()) == nullptr) {
143         ROSEN_LOGE("Failed to add curve animation, implicit animator is null!");
144         return {};
145     }
146 
147     OpenImplicitAnimation(timingProtocol, timingCurve, finishCallback);
148     propertyCallback();
149     return CloseImplicitAnimation();
150 }
151 
FallbackAnimationsToRoot()152 void RSNode::FallbackAnimationsToRoot()
153 {
154     auto target = RSNodeMap::Instance().GetAnimationFallbackNode();
155 
156     if (target == nullptr) {
157         ROSEN_LOGE("Failed to move animation to root, root node is null!");
158         return;
159     }
160     for (const auto& [animationId, animation] : animations_) {
161         target->AddAnimationInner(animation);
162     }
163 }
164 
AddAnimationInner(const std::shared_ptr<RSAnimation> & animation)165 void RSNode::AddAnimationInner(const std::shared_ptr<RSAnimation>& animation)
166 {
167     animations_.emplace(animation->GetId(), animation);
168     animatingPropertyNum_[animation->GetPropertyId()]++;
169 }
170 
RemoveAnimationInner(const std::shared_ptr<RSAnimation> & animation)171 void RSNode::RemoveAnimationInner(const std::shared_ptr<RSAnimation>& animation)
172 {
173     animatingPropertyNum_[animation->GetPropertyId()]--;
174     if (animatingPropertyNum_[animation->GetPropertyId()] == 0) {
175         animation->SetPropertyOnAllAnimationFinish();
176     }
177     animations_.erase(animation->GetId());
178 }
179 
FinishAnimationByProperty(const PropertyId & id)180 void RSNode::FinishAnimationByProperty(const PropertyId& id)
181 {
182     for (const auto& [animationId, animation] : animations_) {
183         if (animation->GetPropertyId() == id) {
184             animation->Finish();
185         }
186     }
187 }
188 
GetStagingProperties() const189 const RSModifierExtractor& RSNode::GetStagingProperties() const
190 {
191     return stagingPropertiesExtractor_;
192 }
193 
AddAnimation(const std::shared_ptr<RSAnimation> & animation)194 void RSNode::AddAnimation(const std::shared_ptr<RSAnimation>& animation)
195 {
196     if (animation == nullptr) {
197         ROSEN_LOGE("Failed to add animation, animation is null!");
198         return;
199     }
200 
201     auto animationId = animation->GetId();
202     if (animations_.find(animationId) != animations_.end()) {
203         ROSEN_LOGE("Failed to add animation, animation already exists!");
204         return;
205     }
206 
207     if (animation->GetDuration() <= 0) {
208         FinishAnimationByProperty(animation->GetPropertyId());
209     }
210 
211     AddAnimationInner(animation);
212     animation->StartInner(std::static_pointer_cast<RSNode>(shared_from_this()));
213 }
214 
RemoveAllAnimations()215 void RSNode::RemoveAllAnimations()
216 {
217     for (const auto& [id, animation] : animations_) {
218         RemoveAnimation(animation);
219     }
220 }
221 
RemoveAnimation(const std::shared_ptr<RSAnimation> & animation)222 void RSNode::RemoveAnimation(const std::shared_ptr<RSAnimation>& animation)
223 {
224     if (animation == nullptr) {
225         ROSEN_LOGE("Failed to remove animation, animation is null!");
226         return;
227     }
228 
229     if (animations_.find(animation->GetId()) == animations_.end()) {
230         ROSEN_LOGE("Failed to remove animation, animation not exists!");
231         return;
232     }
233 
234     animation->Finish();
235 }
236 
SetMotionPathOption(const std::shared_ptr<RSMotionPathOption> & motionPathOption)237 void RSNode::SetMotionPathOption(const std::shared_ptr<RSMotionPathOption>& motionPathOption)
238 {
239     motionPathOption_ = motionPathOption;
240     UpdateModifierMotionPathOption();
241 }
242 
GetMotionPathOption() const243 const std::shared_ptr<RSMotionPathOption> RSNode::GetMotionPathOption() const
244 {
245     return motionPathOption_;
246 }
247 
HasPropertyAnimation(const PropertyId & id)248 bool RSNode::HasPropertyAnimation(const PropertyId& id)
249 {
250     auto it = animatingPropertyNum_.find(id);
251     return it != animatingPropertyNum_.end() && it->second > 0;
252 }
253 
254 template<typename ModifierName, typename PropertyName, typename T>
SetProperty(RSModifierType modifierType,T value)255 void RSNode::SetProperty(RSModifierType modifierType, T value)
256 {
257     auto iter = propertyModifiers_.find(modifierType);
258     if (iter != propertyModifiers_.end()) {
259         auto property = std::static_pointer_cast<PropertyName>(iter->second->GetProperty());
260         if (property == nullptr) {
261             ROSEN_LOGE("RSNode::SetProperty: failed to set property, property is null!");
262             return;
263         }
264         property->Set(value);
265         return;
266     }
267     auto property = std::make_shared<PropertyName>(value);
268     auto propertyModifier = std::make_shared<ModifierName>(property);
269     propertyModifiers_.emplace(modifierType, propertyModifier);
270     AddModifier(propertyModifier);
271 }
272 
273 // alpha
SetAlpha(float alpha)274 void RSNode::SetAlpha(float alpha)
275 {
276     SetProperty<RSAlphaModifier, RSAnimatableProperty<float>>(RSModifierType::ALPHA, alpha);
277 }
278 
SetAlphaOffscreen(bool alphaOffscreen)279 void RSNode::SetAlphaOffscreen(bool alphaOffscreen)
280 {
281     SetProperty<RSAlphaOffscreenModifier, RSProperty<bool>>(RSModifierType::ALPHA_OFFSCREEN, alphaOffscreen);
282 }
283 
284 // Bounds
SetBounds(const Vector4f & bounds)285 void RSNode::SetBounds(const Vector4f& bounds)
286 {
287     SetProperty<RSBoundsModifier, RSAnimatableProperty<Vector4f>>(RSModifierType::BOUNDS, bounds);
288     OnBoundsSizeChanged();
289 }
290 
SetBounds(float positionX,float positionY,float width,float height)291 void RSNode::SetBounds(float positionX, float positionY, float width, float height)
292 {
293     SetBounds({ positionX, positionY, width, height });
294 }
295 
SetBoundsWidth(float width)296 void RSNode::SetBoundsWidth(float width)
297 {
298     auto iter = propertyModifiers_.find(RSModifierType::BOUNDS);
299     if (iter == propertyModifiers_.end()) {
300         SetBounds(0.f, 0.f, width, 0.f);
301         return;
302     }
303 
304     auto property = std::static_pointer_cast<RSAnimatableProperty<Vector4f>>(iter->second->GetProperty());
305     if (property == nullptr) {
306         return;
307     }
308     auto bounds = property->Get();
309     bounds.z_ = width;
310     property->Set(bounds);
311     OnBoundsSizeChanged();
312 }
313 
SetBoundsHeight(float height)314 void RSNode::SetBoundsHeight(float height)
315 {
316     auto iter = propertyModifiers_.find(RSModifierType::BOUNDS);
317     if (iter == propertyModifiers_.end()) {
318         SetBounds(0.f, 0.f, 0.f, height);
319         return;
320     }
321 
322     auto property = std::static_pointer_cast<RSAnimatableProperty<Vector4f>>(iter->second->GetProperty());
323     if (property == nullptr) {
324         return;
325     }
326     auto bounds = property->Get();
327     bounds.w_ = height;
328     property->Set(bounds);
329     OnBoundsSizeChanged();
330 }
331 
332 // Frame
SetFrame(const Vector4f & bounds)333 void RSNode::SetFrame(const Vector4f& bounds)
334 {
335     SetProperty<RSFrameModifier, RSAnimatableProperty<Vector4f>>(RSModifierType::FRAME, bounds);
336 }
337 
SetFrame(float positionX,float positionY,float width,float height)338 void RSNode::SetFrame(float positionX, float positionY, float width, float height)
339 {
340     SetFrame({ positionX, positionY, width, height });
341 }
342 
SetFramePositionX(float positionX)343 void RSNode::SetFramePositionX(float positionX)
344 {
345     auto iter = propertyModifiers_.find(RSModifierType::FRAME);
346     if (iter == propertyModifiers_.end()) {
347         SetFrame(positionX, 0.f, 0.f, 0.f);
348         return;
349     }
350 
351     auto property = std::static_pointer_cast<RSAnimatableProperty<Vector4f>>(iter->second->GetProperty());
352     if (property == nullptr) {
353         return;
354     }
355     auto frame = property->Get();
356     frame.x_ = positionX;
357     property->Set(frame);
358 }
359 
SetFramePositionY(float positionY)360 void RSNode::SetFramePositionY(float positionY)
361 {
362     auto iter = propertyModifiers_.find(RSModifierType::FRAME);
363     if (iter == propertyModifiers_.end()) {
364         SetFrame(0.f, positionY, 0.f, 0.f);
365         return;
366     }
367     auto property = std::static_pointer_cast<RSAnimatableProperty<Vector4f>>(iter->second->GetProperty());
368     if (property == nullptr) {
369         return;
370     }
371     auto frame = property->Get();
372     frame.y_ = positionY;
373     property->Set(frame);
374 }
375 
SetPositionZ(float positionZ)376 void RSNode::SetPositionZ(float positionZ)
377 {
378     SetProperty<RSPositionZModifier, RSAnimatableProperty<float>>(RSModifierType::POSITION_Z, positionZ);
379 }
380 
381 // pivot
SetPivot(const Vector2f & pivot)382 void RSNode::SetPivot(const Vector2f& pivot)
383 {
384     SetProperty<RSPivotModifier, RSAnimatableProperty<Vector2f>>(RSModifierType::PIVOT, pivot);
385 }
386 
SetPivot(float pivotX,float pivotY)387 void RSNode::SetPivot(float pivotX, float pivotY)
388 {
389     SetPivot({ pivotX, pivotY });
390 }
391 
SetPivotX(float pivotX)392 void RSNode::SetPivotX(float pivotX)
393 {
394     auto iter = propertyModifiers_.find(RSModifierType::PIVOT);
395     if (iter == propertyModifiers_.end()) {
396         SetPivot(pivotX, 0.5f);
397         return;
398     }
399 
400     auto property = std::static_pointer_cast<RSAnimatableProperty<Vector2f>>(iter->second->GetProperty());
401     if (property == nullptr) {
402         return;
403     }
404     auto pivot = property->Get();
405     pivot.x_ = pivotX;
406     property->Set(pivot);
407 }
408 
SetPivotY(float pivotY)409 void RSNode::SetPivotY(float pivotY)
410 {
411     auto iter = propertyModifiers_.find(RSModifierType::PIVOT);
412     if (iter == propertyModifiers_.end()) {
413         SetPivot(0.5f, pivotY);
414         return;
415     }
416 
417     auto property = std::static_pointer_cast<RSAnimatableProperty<Vector2f>>(iter->second->GetProperty());
418     if (property == nullptr) {
419         return;
420     }
421     auto pivot = property->Get();
422     pivot.y_ = pivotY;
423     property->Set(pivot);
424 }
425 
SetCornerRadius(float cornerRadius)426 void RSNode::SetCornerRadius(float cornerRadius)
427 {
428     SetCornerRadius(Vector4f(cornerRadius));
429 }
430 
SetCornerRadius(const Vector4f & cornerRadius)431 void RSNode::SetCornerRadius(const Vector4f& cornerRadius)
432 {
433     SetProperty<RSCornerRadiusModifier, RSAnimatableProperty<Vector4f>>(RSModifierType::CORNER_RADIUS, cornerRadius);
434 }
435 
436 // transform
SetRotation(const Quaternion & quaternion)437 void RSNode::SetRotation(const Quaternion& quaternion)
438 {
439     SetProperty<RSQuaternionModifier, RSAnimatableProperty<Quaternion>>(RSModifierType::QUATERNION, quaternion);
440 }
441 
SetRotation(float degree)442 void RSNode::SetRotation(float degree)
443 {
444     SetProperty<RSRotationModifier, RSAnimatableProperty<float>>(RSModifierType::ROTATION, degree);
445 }
446 
SetRotation(float degreeX,float degreeY,float degreeZ)447 void RSNode::SetRotation(float degreeX, float degreeY, float degreeZ)
448 {
449     SetRotationX(degreeX);
450     SetRotationY(degreeY);
451     SetRotation(degreeZ);
452 }
453 
SetRotationX(float degree)454 void RSNode::SetRotationX(float degree)
455 {
456     SetProperty<RSRotationXModifier, RSAnimatableProperty<float>>(RSModifierType::ROTATION_X, degree);
457 }
458 
SetRotationY(float degree)459 void RSNode::SetRotationY(float degree)
460 {
461     SetProperty<RSRotationYModifier, RSAnimatableProperty<float>>(RSModifierType::ROTATION_Y, degree);
462 }
463 
SetTranslate(const Vector2f & translate)464 void RSNode::SetTranslate(const Vector2f& translate)
465 {
466     SetProperty<RSTranslateModifier, RSAnimatableProperty<Vector2f>>(RSModifierType::TRANSLATE, translate);
467 }
468 
SetTranslate(float translateX,float translateY,float translateZ)469 void RSNode::SetTranslate(float translateX, float translateY, float translateZ)
470 {
471     SetTranslate({ translateX, translateY });
472     SetTranslateZ(translateZ);
473 }
SetTranslateX(float translate)474 void RSNode::SetTranslateX(float translate)
475 {
476     auto iter = propertyModifiers_.find(RSModifierType::TRANSLATE);
477     if (iter == propertyModifiers_.end()) {
478         SetTranslate({ translate, 0.f });
479         return;
480     }
481 
482     auto property = std::static_pointer_cast<RSAnimatableProperty<Vector2f>>(iter->second->GetProperty());
483     if (property == nullptr) {
484         return;
485     }
486     auto trans = property->Get();
487     trans.x_ = translate;
488     property->Set(trans);
489 }
490 
SetTranslateY(float translate)491 void RSNode::SetTranslateY(float translate)
492 {
493     auto iter = propertyModifiers_.find(RSModifierType::TRANSLATE);
494     if (iter == propertyModifiers_.end()) {
495         SetTranslate({ 0.f, translate });
496         return;
497     }
498 
499     auto property = std::static_pointer_cast<RSAnimatableProperty<Vector2f>>(iter->second->GetProperty());
500     if (property == nullptr) {
501         return;
502     }
503     auto trans = property->Get();
504     trans.y_ = translate;
505     property->Set(trans);
506 }
507 
SetTranslateZ(float translate)508 void RSNode::SetTranslateZ(float translate)
509 {
510     SetProperty<RSTranslateZModifier, RSAnimatableProperty<float>>(RSModifierType::TRANSLATE_Z, translate);
511 }
512 
SetScale(float scale)513 void RSNode::SetScale(float scale)
514 {
515     SetScale({ scale, scale });
516 }
517 
SetScale(float scaleX,float scaleY)518 void RSNode::SetScale(float scaleX, float scaleY)
519 {
520     SetScale({ scaleX, scaleY });
521 }
522 
SetScale(const Vector2f & scale)523 void RSNode::SetScale(const Vector2f& scale)
524 {
525     SetProperty<RSScaleModifier, RSAnimatableProperty<Vector2f>>(RSModifierType::SCALE, scale);
526 }
527 
SetScaleX(float scaleX)528 void RSNode::SetScaleX(float scaleX)
529 {
530     auto iter = propertyModifiers_.find(RSModifierType::SCALE);
531     if (iter == propertyModifiers_.end()) {
532         SetScale(scaleX, 1.f);
533         return;
534     }
535 
536     auto property = std::static_pointer_cast<RSAnimatableProperty<Vector2f>>(iter->second->GetProperty());
537     if (property == nullptr) {
538         return;
539     }
540     auto scale = property->Get();
541     scale.x_ = scaleX;
542     property->Set(scale);
543 }
544 
SetScaleY(float scaleY)545 void RSNode::SetScaleY(float scaleY)
546 {
547     auto iter = propertyModifiers_.find(RSModifierType::SCALE);
548     if (iter == propertyModifiers_.end()) {
549         SetScale(1.f, scaleY);
550         return;
551     }
552 
553     auto property = std::static_pointer_cast<RSAnimatableProperty<Vector2f>>(iter->second->GetProperty());
554     if (property == nullptr) {
555         return;
556     }
557     auto scale = property->Get();
558     scale.y_ = scaleY;
559     property->Set(scale);
560 }
561 
562 // foreground
SetForegroundColor(uint32_t colorValue)563 void RSNode::SetForegroundColor(uint32_t colorValue)
564 {
565     auto color = Color::FromArgbInt(colorValue);
566     SetProperty<RSForegroundColorModifier, RSAnimatableProperty<Color>>(RSModifierType::FOREGROUND_COLOR, color);
567 }
568 
SetBackgroundColor(uint32_t colorValue)569 void RSNode::SetBackgroundColor(uint32_t colorValue)
570 {
571     auto color = Color::FromArgbInt(colorValue);
572     SetProperty<RSBackgroundColorModifier, RSAnimatableProperty<Color>>(RSModifierType::BACKGROUND_COLOR, color);
573 }
574 
SetBackgroundShader(const std::shared_ptr<RSShader> & shader)575 void RSNode::SetBackgroundShader(const std::shared_ptr<RSShader>& shader)
576 {
577     SetProperty<RSBackgroundShaderModifier, RSProperty<std::shared_ptr<RSShader>>>(
578         RSModifierType::BACKGROUND_SHADER, shader);
579 }
580 
581 // background
SetBgImage(const std::shared_ptr<RSImage> & image)582 void RSNode::SetBgImage(const std::shared_ptr<RSImage>& image)
583 {
584     SetProperty<RSBgImageModifier, RSProperty<std::shared_ptr<RSImage>>>(RSModifierType::BG_IMAGE, image);
585 }
586 
SetBgImageSize(float width,float height)587 void RSNode::SetBgImageSize(float width, float height)
588 {
589     SetBgImageWidth(width);
590     SetBgImageHeight(height);
591 }
592 
SetBgImageWidth(float width)593 void RSNode::SetBgImageWidth(float width)
594 {
595     SetProperty<RSBgImageWidthModifier, RSAnimatableProperty<float>>(RSModifierType::BG_IMAGE_WIDTH, width);
596 }
597 
SetBgImageHeight(float height)598 void RSNode::SetBgImageHeight(float height)
599 {
600     SetProperty<RSBgImageHeightModifier, RSAnimatableProperty<float>>(RSModifierType::BG_IMAGE_HEIGHT, height);
601 }
602 
SetBgImagePosition(float positionX,float positionY)603 void RSNode::SetBgImagePosition(float positionX, float positionY)
604 {
605     SetBgImagePositionX(positionX);
606     SetBgImagePositionY(positionY);
607 }
608 
SetBgImagePositionX(float positionX)609 void RSNode::SetBgImagePositionX(float positionX)
610 {
611     SetProperty<RSBgImagePositionXModifier, RSAnimatableProperty<float>>(
612         RSModifierType::BG_IMAGE_POSITION_X, positionX);
613 }
614 
SetBgImagePositionY(float positionY)615 void RSNode::SetBgImagePositionY(float positionY)
616 {
617     SetProperty<RSBgImagePositionYModifier, RSAnimatableProperty<float>>(
618         RSModifierType::BG_IMAGE_POSITION_Y, positionY);
619 }
620 
621 // border
SetBorderColor(uint32_t colorValue)622 void RSNode::SetBorderColor(uint32_t colorValue)
623 {
624     SetBorderColor(colorValue, colorValue, colorValue, colorValue);
625 }
626 
SetBorderColor(uint32_t left,uint32_t top,uint32_t right,uint32_t bottom)627 void RSNode::SetBorderColor(uint32_t left, uint32_t top, uint32_t right, uint32_t bottom)
628 {
629     Vector4<Color> color(Color::FromArgbInt(left), Color::FromArgbInt(top),
630                          Color::FromArgbInt(right), Color::FromArgbInt(bottom));
631     SetBorderColor(color);
632 }
633 
SetBorderColor(const Vector4<Color> & color)634 void RSNode::SetBorderColor(const Vector4<Color>& color)
635 {
636     SetProperty<RSBorderColorModifier, RSAnimatableProperty<Vector4<Color>>>(RSModifierType::BORDER_COLOR, color);
637 }
638 
SetBorderWidth(float width)639 void RSNode::SetBorderWidth(float width)
640 {
641     SetBorderWidth(width, width, width, width);
642 }
643 
SetBorderWidth(float left,float top,float right,float bottom)644 void RSNode::SetBorderWidth(float left, float top, float right, float bottom)
645 {
646     Vector4f width(left, top, right, bottom);
647     SetBorderWidth(width);
648 }
649 
SetBorderWidth(const Vector4f & width)650 void RSNode::SetBorderWidth(const Vector4f& width)
651 {
652     SetProperty<RSBorderWidthModifier, RSAnimatableProperty<Vector4f>>(RSModifierType::BORDER_WIDTH, width);
653 }
654 
SetBorderStyle(uint32_t styleValue)655 void RSNode::SetBorderStyle(uint32_t styleValue)
656 {
657     SetBorderStyle(styleValue, styleValue, styleValue, styleValue);
658 }
659 
SetBorderStyle(uint32_t left,uint32_t top,uint32_t right,uint32_t bottom)660 void RSNode::SetBorderStyle(uint32_t left, uint32_t top, uint32_t right, uint32_t bottom)
661 {
662     Vector4<BorderStyle> style(static_cast<BorderStyle>(left), static_cast<BorderStyle>(top),
663                                static_cast<BorderStyle>(right), static_cast<BorderStyle>(bottom));
664     SetBorderStyle(style);
665 }
666 
SetBorderStyle(const Vector4<BorderStyle> & style)667 void RSNode::SetBorderStyle(const Vector4<BorderStyle>& style)
668 {
669     Vector4<uint32_t> styles(static_cast<uint32_t>(style.x_), static_cast<uint32_t>(style.y_),
670                              static_cast<uint32_t>(style.z_), static_cast<uint32_t>(style.w_));
671     SetProperty<RSBorderStyleModifier, RSProperty<Vector4<uint32_t>>>(RSModifierType::BORDER_STYLE, styles);
672 }
673 
SetBackgroundFilter(const std::shared_ptr<RSFilter> & backgroundFilter)674 void RSNode::SetBackgroundFilter(const std::shared_ptr<RSFilter>& backgroundFilter)
675 {
676     SetProperty<RSBackgroundFilterModifier, RSAnimatableProperty<std::shared_ptr<RSFilter>>>(
677         RSModifierType::BACKGROUND_FILTER, backgroundFilter);
678 }
679 
SetFilter(const std::shared_ptr<RSFilter> & filter)680 void RSNode::SetFilter(const std::shared_ptr<RSFilter>& filter)
681 {
682     SetProperty<RSFilterModifier, RSAnimatableProperty<std::shared_ptr<RSFilter>>>(RSModifierType::FILTER, filter);
683 }
684 
SetCompositingFilter(const std::shared_ptr<RSFilter> & compositingFilter)685 void RSNode::SetCompositingFilter(const std::shared_ptr<RSFilter>& compositingFilter) {}
686 
SetShadowColor(uint32_t colorValue)687 void RSNode::SetShadowColor(uint32_t colorValue)
688 {
689     auto color = Color::FromArgbInt(colorValue);
690     SetProperty<RSShadowColorModifier, RSAnimatableProperty<Color>>(RSModifierType::SHADOW_COLOR, color);
691 }
692 
SetShadowOffset(float offsetX,float offsetY)693 void RSNode::SetShadowOffset(float offsetX, float offsetY)
694 {
695     SetShadowOffsetX(offsetX);
696     SetShadowOffsetY(offsetY);
697 }
698 
SetShadowOffsetX(float offsetX)699 void RSNode::SetShadowOffsetX(float offsetX)
700 {
701     SetProperty<RSShadowOffsetXModifier, RSAnimatableProperty<float>>(RSModifierType::SHADOW_OFFSET_X, offsetX);
702 }
703 
SetShadowOffsetY(float offsetY)704 void RSNode::SetShadowOffsetY(float offsetY)
705 {
706     SetProperty<RSShadowOffsetYModifier, RSAnimatableProperty<float>>(RSModifierType::SHADOW_OFFSET_Y, offsetY);
707 }
708 
SetShadowAlpha(float alpha)709 void RSNode::SetShadowAlpha(float alpha)
710 {
711     SetProperty<RSShadowAlphaModifier, RSAnimatableProperty<float>>(RSModifierType::SHADOW_ALPHA, alpha);
712 }
713 
SetShadowElevation(float elevation)714 void RSNode::SetShadowElevation(float elevation)
715 {
716     SetProperty<RSShadowElevationModifier, RSAnimatableProperty<float>>(RSModifierType::SHADOW_ELEVATION, elevation);
717 }
718 
SetShadowRadius(float radius)719 void RSNode::SetShadowRadius(float radius)
720 {
721     SetProperty<RSShadowRadiusModifier, RSAnimatableProperty<float>>(RSModifierType::SHADOW_RADIUS, radius);
722 }
723 
SetShadowPath(const std::shared_ptr<RSPath> & shadowPath)724 void RSNode::SetShadowPath(const std::shared_ptr<RSPath>& shadowPath)
725 {
726     SetProperty<RSShadowPathModifier, RSProperty<std::shared_ptr<RSPath>>>(RSModifierType::SHADOW_PATH, shadowPath);
727 }
728 
SetFrameGravity(Gravity gravity)729 void RSNode::SetFrameGravity(Gravity gravity)
730 {
731     SetProperty<RSFrameGravityModifier, RSProperty<Gravity>>(RSModifierType::FRAME_GRAVITY, gravity);
732 }
733 
SetClipBounds(const std::shared_ptr<RSPath> & path)734 void RSNode::SetClipBounds(const std::shared_ptr<RSPath>& path)
735 {
736     SetProperty<RSClipBoundsModifier, RSProperty<std::shared_ptr<RSPath>>>(RSModifierType::CLIP_BOUNDS, path);
737 }
738 
SetClipToBounds(bool clipToBounds)739 void RSNode::SetClipToBounds(bool clipToBounds)
740 {
741     SetProperty<RSClipToBoundsModifier, RSProperty<bool>>(RSModifierType::CLIP_TO_BOUNDS, clipToBounds);
742 }
743 
SetClipToFrame(bool clipToFrame)744 void RSNode::SetClipToFrame(bool clipToFrame)
745 {
746     SetProperty<RSClipToFrameModifier, RSProperty<bool>>(RSModifierType::CLIP_TO_FRAME, clipToFrame);
747 }
748 
SetVisible(bool visible)749 void RSNode::SetVisible(bool visible)
750 {
751     // kick off transition only if it's on tree(has valid parent) and visibility is changed.
752     if (transitionEffect_ != nullptr && GetParent() != nullptr && visible != GetStagingProperties().GetVisible()) {
753         NotifyTransition(transitionEffect_, visible);
754     }
755 
756     SetProperty<RSVisibleModifier, RSProperty<bool>>(RSModifierType::VISIBLE, visible);
757 }
758 
SetMask(const std::shared_ptr<RSMask> & mask)759 void RSNode::SetMask(const std::shared_ptr<RSMask>& mask)
760 {
761     SetProperty<RSMaskModifier, RSProperty<std::shared_ptr<RSMask>>>(RSModifierType::MASK, mask);
762 }
763 
NotifyTransition(const std::shared_ptr<const RSTransitionEffect> & effect,bool isTransitionIn)764 void RSNode::NotifyTransition(const std::shared_ptr<const RSTransitionEffect>& effect, bool isTransitionIn)
765 {
766     // temporary fix for multithread issue in implicit animator
767     UpdateImplicitAnimator();
768     if (implicitAnimator_ == nullptr) {
769         ROSEN_LOGE("Failed to notify transition, implicit animator is null!");
770         return;
771     }
772 
773     if (!implicitAnimator_->NeedImplicitAnimation()) {
774         return;
775     }
776 
777     implicitAnimator_->BeginImplicitTransition(effect);
778     implicitAnimator_->CreateImplicitTransition(*this, isTransitionIn);
779     implicitAnimator_->EndImplicitTransition();
780 }
781 
OnAddChildren()782 void RSNode::OnAddChildren()
783 {
784     // kick off transition only if it's visible.
785     if (transitionEffect_ != nullptr && GetStagingProperties().GetVisible()) {
786         NotifyTransition(transitionEffect_, true);
787     }
788 }
789 
OnRemoveChildren()790 void RSNode::OnRemoveChildren()
791 {
792     // kick off transition only if it's visible.
793     if (transitionEffect_ != nullptr && GetStagingProperties().GetVisible()) {
794         NotifyTransition(transitionEffect_, false);
795     }
796 }
797 
AnimationFinish(AnimationId animationId)798 bool RSNode::AnimationFinish(AnimationId animationId)
799 {
800     auto animationItr = animations_.find(animationId);
801     if (animationItr == animations_.end()) {
802         ROSEN_LOGE("Failed to find animation[%" PRIu64 "]!", animationId);
803         return false;
804     }
805 
806     auto& animation = animationItr->second;
807     if (animation == nullptr) {
808         ROSEN_LOGE("Failed to finish animation[%" PRIu64 "], animation is null!", animationId);
809         return false;
810     }
811 
812     animation->CallFinishCallback();
813     RemoveAnimationInner(animation);
814     return true;
815 }
816 
SetPaintOrder(bool drawContentLast)817 void RSNode::SetPaintOrder(bool drawContentLast)
818 {
819     drawContentLast_ = drawContentLast;
820 }
821 
ClearAllModifiers()822 void RSNode::ClearAllModifiers()
823 {
824     for (auto [id, modifier] : modifiers_) {
825         if (modifier) {
826             modifier->DetachFromNode();
827         }
828     }
829 }
830 
AddModifier(const std::shared_ptr<RSModifier> modifier)831 void RSNode::AddModifier(const std::shared_ptr<RSModifier> modifier)
832 {
833     if (!modifier || modifiers_.count(modifier->GetPropertyId())) {
834         return;
835     }
836     if (motionPathOption_ != nullptr && IsPathAnimatableModifier(modifier->GetModifierType())) {
837         modifier->SetMotionPathOption(motionPathOption_);
838     }
839     auto rsnode = std::static_pointer_cast<RSNode>(shared_from_this());
840     modifier->AttachToNode(rsnode);
841     modifiers_.emplace(modifier->GetPropertyId(), modifier);
842     std::unique_ptr<RSCommand> command = std::make_unique<RSAddModifier>(GetId(), modifier->CreateRenderModifier());
843     auto transactionProxy = RSTransactionProxy::GetInstance();
844     if (transactionProxy != nullptr) {
845         transactionProxy->AddCommand(command, IsRenderServiceNode(), GetFollowType(), GetId());
846         if (NeedForcedSendToRemote()) {
847             std::unique_ptr<RSCommand> cmdForRemote =
848                 std::make_unique<RSAddModifier>(GetId(), modifier->CreateRenderModifier());
849             transactionProxy->AddCommand(cmdForRemote, true, GetFollowType(), GetId());
850         }
851         if (NeedSendExtraCommand()) {
852             std::unique_ptr<RSCommand> extraCommand =
853                 std::make_unique<RSAddModifier>(GetId(), modifier->CreateRenderModifier());
854             transactionProxy->AddCommand(extraCommand, !IsRenderServiceNode(), GetFollowType(), GetId());
855         }
856     }
857 }
858 
RemoveModifier(const std::shared_ptr<RSModifier> modifier)859 void RSNode::RemoveModifier(const std::shared_ptr<RSModifier> modifier)
860 {
861     if (!modifier) {
862         return;
863     }
864     auto iter = modifiers_.find(modifier->GetPropertyId());
865     if (iter == modifiers_.end()) {
866         return;
867     }
868 
869     modifiers_.erase(iter);
870     modifier->DetachFromNode();
871     std::unique_ptr<RSCommand> command = std::make_unique<RSRemoveModifier>(GetId(), modifier->GetPropertyId());
872     auto transactionProxy = RSTransactionProxy::GetInstance();
873     if (transactionProxy != nullptr) {
874         transactionProxy->AddCommand(command, IsRenderServiceNode(), GetFollowType(), GetId());
875         if (NeedForcedSendToRemote()) {
876             std::unique_ptr<RSCommand> cmdForRemote =
877                 std::make_unique<RSRemoveModifier>(GetId(), modifier->GetPropertyId());
878             transactionProxy->AddCommand(cmdForRemote, true, GetFollowType(), GetId());
879         }
880         if (NeedSendExtraCommand()) {
881             std::unique_ptr<RSCommand> extraCommand =
882                 std::make_unique<RSRemoveModifier>(GetId(), modifier->GetPropertyId());
883             transactionProxy->AddCommand(extraCommand, !IsRenderServiceNode(), GetFollowType(), GetId());
884         }
885     }
886 }
887 
GetModifier(const PropertyId & propertyId)888 const std::shared_ptr<RSModifier> RSNode::GetModifier(const PropertyId& propertyId)
889 {
890     auto iter = modifiers_.find(propertyId);
891     if (iter != modifiers_.end()) {
892         return iter->second;
893     }
894 
895     return {};
896 }
897 
UpdateModifierMotionPathOption()898 void RSNode::UpdateModifierMotionPathOption()
899 {
900     for (auto& [type, modifier] : propertyModifiers_) {
901         if (IsPathAnimatableModifier(type)) {
902             modifier->SetMotionPathOption(motionPathOption_);
903         }
904     }
905     for (auto& [id, modifier] : modifiers_) {
906         if (IsPathAnimatableModifier(modifier->GetModifierType())) {
907             modifier->SetMotionPathOption(motionPathOption_);
908         }
909     }
910 }
911 
UpdateExtendedModifier(const std::weak_ptr<RSModifier> & modifier)912 void RSNode::UpdateExtendedModifier(const std::weak_ptr<RSModifier>& modifier)
913 {
914     if (auto sharedModifier = modifier.lock()) {
915         sharedModifier->UpdateToRender();
916     }
917 }
918 
DumpNode(int depth) const919 std::string RSNode::DumpNode(int depth) const
920 {
921     std::stringstream ss;
922     ss << RSBaseNode::DumpNode(depth);
923     if (!animations_.empty()) {
924         ss << " animation:" << std::to_string(animations_.size());
925     }
926     ss << " " << GetStagingProperties().Dump();
927     return ss.str();
928 }
929 
UpdateImplicitAnimator()930 void RSNode::UpdateImplicitAnimator()
931 {
932     auto tid = gettid();
933     if (tid == implicitAnimatorTid_) {
934         return;
935     }
936     implicitAnimatorTid_ = tid;
937     implicitAnimator_ = RSImplicitAnimatorMap::Instance().GetAnimator(tid);
938 }
939 
GetModifierIds() const940 std::vector<PropertyId> RSNode::GetModifierIds() const
941 {
942     std::vector<PropertyId> ids;
943     for (const auto& [id, _] : modifiers_) {
944         ids.push_back(id);
945     }
946     return ids;
947 }
948 } // namespace Rosen
949 } // namespace OHOS
950