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 #include "ui_effect_manager.h"
16 #include "window_manager_hilog.h"
17 #include "ui_effect_controller_client_proxy.h"
18
19 constexpr int CONTROLLER_INDEX = 0;
20 constexpr int CONTROLLER_CLIENT_INDEX = 1;
21 constexpr int CONTROLLER_CLIENT_DEATH = 2;
22 namespace OHOS::Rosen {
23 WM_IMPLEMENT_SINGLE_INSTANCE(UIEffectManager);
RegisterUIEffectSetParamsCallback(NotifyUIEffectSetParamFunc func)24 void UIEffectManager::RegisterUIEffectSetParamsCallback(NotifyUIEffectSetParamFunc func)
25 {
26 onUIEffectSetParams_ = func;
27 }
28
RegisterUIEffectAnimateToCallback(NotifyUIEffectAnimateToFunc func)29 void UIEffectManager::RegisterUIEffectAnimateToCallback(NotifyUIEffectAnimateToFunc func)
30 {
31 onUIEffectAnimateTo_ = func;
32 }
33
CreateUIEffectController(const sptr<IUIEffectControllerClient> & controllerClient,sptr<IUIEffectController> & controller,int32_t & controllerId)34 WMError UIEffectManager::CreateUIEffectController(const sptr<IUIEffectControllerClient>& controllerClient,
35 sptr<IUIEffectController>& controller, int32_t& controllerId)
36 {
37 if (!onUIEffectSetParams_ || !onUIEffectAnimateTo_) {
38 TLOGE(WmsLogTag::WMS_ANIMATION, "scb not register animation func!");
39 return WMError::WM_ERROR_NULLPTR;
40 }
41 static std::atomic<int32_t> globalControllerId = 0;
42 globalControllerId++;
43 controllerId = globalControllerId;
44 sptr<UIEffectControllerClientDeath> controllerDeath = sptr<UIEffectControllerClientDeath>::MakeSptr(
45 [controllerId, this] () { EraseUIEffectController(controllerId); }
46 );
47 if (!controllerClient->AsObject() || !controllerClient->AsObject()->AddDeathRecipient(controllerDeath)) {
48 TLOGE(WmsLogTag::WMS_ANIMATION, "Failed to add death recipient");
49 return WMError::WM_ERROR_NULLPTR;
50 }
51 sptr<UIEffectController> newController =
52 sptr<UIEffectController>::MakeSptr(controllerId, onUIEffectSetParams_, onUIEffectAnimateTo_);
53 std::unique_lock<std::mutex> lock(UIEffectControllerMapMutex_);
54 UIEffectControllerMap_[controllerId] = std::make_tuple(newController, controllerClient, controllerDeath);
55 controller = newController;
56 TLOGI(WmsLogTag::WMS_ANIMATION, "create ui effect success, id: %{public}d, map size %{public}zu", controllerId,
57 UIEffectControllerMap_.size());
58 return WMError::WM_OK;
59 }
60
IsControllerListValid(const ControllerList & list) const61 bool UIEffectManager::IsControllerListValid(const ControllerList& list) const
62 {
63 if (std::get<CONTROLLER_INDEX>(list) != nullptr && std::get<CONTROLLER_CLIENT_INDEX>(list) != nullptr &&
64 std::get<CONTROLLER_CLIENT_DEATH>(list) != nullptr) {
65 return true;
66 } else {
67 return false;
68 }
69 }
70
SetUIEffectControllerAliveState(int32_t id,bool isAlive)71 void UIEffectManager::SetUIEffectControllerAliveState(int32_t id, bool isAlive)
72 {
73 std::unique_lock<std::mutex> lock(UIEffectControllerMapMutex_);
74 if (auto it = UIEffectControllerMap_.find(id); it != UIEffectControllerMap_.end()) {
75 auto& controllerList = it->second;
76 if (!IsControllerListValid(controllerList)) {
77 TLOGE(WmsLogTag::WMS_ANIMATION, "controller invalid");
78 return;
79 }
80 sptr<UIEffectController> controller = std::get<CONTROLLER_INDEX>(controllerList);
81 controller->SetIsAliveInUI(isAlive);
82 } else {
83 TLOGE(WmsLogTag::WMS_ANIMATION, "can not find current controller");
84 }
85 }
86
EraseUIEffectController(int32_t id)87 void UIEffectManager::EraseUIEffectController(int32_t id)
88 {
89 std::unique_lock<std::mutex> lock(UIEffectControllerMapMutex_);
90 if (auto it = UIEffectControllerMap_.find(id); it != UIEffectControllerMap_.end()) {
91 auto& controllerList = it->second;
92 if (!IsControllerListValid(controllerList)) {
93 TLOGE(WmsLogTag::WMS_ANIMATION, "controller invalid");
94 UIEffectControllerMap_.erase(id);
95 return;
96 }
97 sptr<IUIEffectControllerClient> controllerClient = std::get<CONTROLLER_CLIENT_INDEX>(controllerList);
98 sptr<UIEffectControllerClientDeath> clientDeath = std::get<CONTROLLER_CLIENT_DEATH>(controllerList);
99 if (!controllerClient->AsObject()) {
100 TLOGE(WmsLogTag::WMS_ANIMATION, "controllerClient is nullptr or not proxy");
101 UIEffectControllerMap_.erase(id);
102 return;
103 }
104 controllerClient->AsObject()->RemoveDeathRecipient(clientDeath);
105 }
106 UIEffectControllerMap_.erase(id);
107 }
108 }