1 /*
2 * Copyright (c) 2025 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_ui_context.h"
17
18 #include "modifier/rs_modifier_manager_map.h"
19 #include "platform/common/rs_log.h"
20
21 namespace OHOS {
22 namespace Rosen {
RSUIContext(uint64_t token)23 RSUIContext::RSUIContext(uint64_t token) : token_(token)
24 {
25 rsTransactionHandler_ = std::shared_ptr<RSTransactionHandler>(new RSTransactionHandler());
26 rsSyncTransactionHandler_ = std::shared_ptr<RSSyncTransactionHandler>(new RSSyncTransactionHandler());
27 rsSyncTransactionHandler_->SetTransactionHandler(rsTransactionHandler_);
28 }
29
RSUIContext()30 RSUIContext::RSUIContext() : RSUIContext(0) {}
31
~RSUIContext()32 RSUIContext::~RSUIContext() {}
33
GetRSImplicitAnimator()34 const std::shared_ptr<RSImplicitAnimator> RSUIContext::GetRSImplicitAnimator()
35 {
36 if (rsImplicitAnimator_ == nullptr) {
37 rsImplicitAnimator_ = std::make_shared<RSImplicitAnimator>();
38 rsImplicitAnimator_->SetRSUIContext(weak_from_this());
39 }
40 return rsImplicitAnimator_;
41 }
42
GetRSModifierManager()43 const std::shared_ptr<RSModifierManager> RSUIContext::GetRSModifierManager()
44 {
45 if (rsModifierManager_ == nullptr) {
46 rsModifierManager_ = std::make_shared<RSModifierManager>();
47 }
48 return rsModifierManager_;
49 }
50
AnimationCallback(AnimationId animationId,AnimationCallbackEvent event)51 bool RSUIContext::AnimationCallback(AnimationId animationId, AnimationCallbackEvent event)
52 {
53 std::shared_ptr<RSAnimation> animation = nullptr;
54 {
55 std::unique_lock<std::recursive_mutex> lock(animationMutex_);
56 auto animationItr = animations_.find(animationId);
57 if (animationItr == animations_.end()) {
58 ROSEN_LOGE("Failed to find animation[%{public}" PRIu64 "]!", animationId);
59 return false;
60 }
61 animation = animationItr->second;
62 }
63
64 if (animation == nullptr) {
65 ROSEN_LOGE("Failed to callback animation[%{public}" PRIu64 "], animation is null!", animationId);
66 return false;
67 }
68 if (event == FINISHED) {
69 RemoveAnimationInner(animation);
70 animation->CallFinishCallback();
71 return true;
72 } else if (event == REPEAT_FINISHED) {
73 animation->CallRepeatCallback();
74 return true;
75 } else if (event == LOGICALLY_FINISHED) {
76 animation->CallLogicallyFinishCallback();
77 return true;
78 }
79 ROSEN_LOGE("Failed to callback animation event[%{public}d], event is null!", event);
80 return false;
81 }
82
AddAnimationInner(const std::shared_ptr<RSAnimation> & animation)83 void RSUIContext::AddAnimationInner(const std::shared_ptr<RSAnimation>& animation)
84 {
85 std::unique_lock<std::recursive_mutex> lock(animationMutex_);
86 animations_.emplace(animation->GetId(), animation);
87 animatingPropertyNum_[animation->GetPropertyId()]++;
88 }
89
RemoveAnimationInner(const std::shared_ptr<RSAnimation> & animation)90 void RSUIContext::RemoveAnimationInner(const std::shared_ptr<RSAnimation>& animation)
91 {
92 std::unique_lock<std::recursive_mutex> lock(animationMutex_);
93 if (auto it = animatingPropertyNum_.find(animation->GetPropertyId()); it != animatingPropertyNum_.end()) {
94 it->second--;
95 if (it->second == 0) {
96 animatingPropertyNum_.erase(it);
97 animation->SetPropertyOnAllAnimationFinish();
98 }
99 }
100 animations_.erase(animation->GetId());
101 }
102
SetUITaskRunner(const TaskRunner & uiTaskRunner)103 void RSUIContext::SetUITaskRunner(const TaskRunner& uiTaskRunner)
104 {
105 taskRunner_ = uiTaskRunner;
106 }
107
PostTask(const std::function<void ()> & task)108 void RSUIContext::PostTask(const std::function<void()>& task)
109 {
110 PostDelayTask(task, 0);
111 }
112
PostDelayTask(const std::function<void ()> & task,uint32_t delay)113 void RSUIContext::PostDelayTask(const std::function<void()>& task, uint32_t delay)
114 {
115 if (taskRunner_ == nullptr) {
116 ROSEN_LOGD(
117 "multi-instance RSUIContext::PostDelayTask failed, taskRunner is empty, token=%{public}" PRIu64, token_);
118 return;
119 }
120 taskRunner_(task, delay);
121 ROSEN_LOGD("multi-instance RSUIContext::PostDelayTask success token=%{public}" PRIu64, token_);
122 }
123
124 } // namespace Rosen
125 } // namespace OHOS