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