• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "adapter/ohos/entrance/global_pipeline_context_manager.h"
17 
18 #include "bridge/declarative_frontend/declarative_frontend.h"
19 #include "bridge/declarative_frontend/ng/declarative_frontend_ng.h"
20 #include "core/common/asset_manager_impl.h"
21 #include "core/common/task_executor_impl.h"
22 #include "core/common/window.h"
23 #include "core/components_ng/render/adapter/rosen_window.h"
24 #include "core/pipeline_ng/pipeline_context.h"
25 
26 namespace OHOS::Ace {
AfterDestroyed()27 void WindowLifeCycle::AfterDestroyed()
28 {
29     auto globalPipelineManager = GlobalPipelineContextManager::GetInstance();
30     auto globalPipelineContext = globalPipelineManager->RemoveGlobalPipelineContext(name_);
31     auto modalPageNode = globalPipelineManager->RemoveModalPageNode(name_);
32     globalPipelineManager->RemoveSessionId(name_);
33     if (modalPageNode) {
34         auto parentNode = modalPageNode->GetParent();
35         if (parentNode) {
36             parentNode->RemoveChild(modalPageNode);
37         }
38         modalPageNode->MountToParent(globalPipelineContext->GetRootElement());
39     }
40     CHECK_NULL_VOID(globalPipelineContext);
41     globalPipelineContext->Destroy();
42     globalPipelineContext.Reset();
43 }
44 
GetInstance()45 GlobalPipelineContextManager* GlobalPipelineContextManager::GetInstance()
46 {
47     static GlobalPipelineContextManager instance;
48     return &instance;
49 }
50 
GetGlobalPipelineContext(const std::string & name) const51 RefPtr<NG::PipelineContext> GlobalPipelineContextManager::GetGlobalPipelineContext(const std::string& name) const
52 {
53     auto iter = globalContextMap_.find(name);
54     if (iter == globalContextMap_.end()) {
55         return nullptr;
56     }
57     return iter->second;
58 }
59 
CreateGlobalPipelineContext(const std::string & name,const sptr<OHOS::Rosen::Window> & rsWindow,FrontendType frontendType,int32_t instanceId)60 RefPtr<NG::PipelineContext> GlobalPipelineContextManager::CreateGlobalPipelineContext(
61     const std::string& name, const sptr<OHOS::Rosen::Window>& rsWindow, FrontendType frontendType, int32_t instanceId)
62 {
63     // taskExecutor
64     auto taskExecutor = Referenced::MakeRefPtr<TaskExecutorImpl>();
65     taskExecutor->InitPlatformThread(true); // useCurrentEventRunner true: 复用taskExecutor
66     taskExecutor->InitOtherThreads(threadModelImpl_.get());
67     if (frontendType != FrontendType::DECLARATIVE_JS && frontendType != FrontendType::ETS_CARD) {
68         taskExecutor->InitJsThread();
69     }
70 
71     // assetManager
72     auto assetManagerImpl = Referenced::MakeRefPtr<AssetManagerImpl>();
73 
74     // frontend
75 #ifdef NG_BUILD
76     auto frontend = AceType::MakeRefPtr<DeclarativeFrontendNG>();
77     auto declarativeFrontend = AceType::DynamicCast<DeclarativeFrontendNG>(frontend);
78 #else
79     auto frontend = AceType::MakeRefPtr<DeclarativeFrontend>();
80     auto declarativeFrontend = AceType::DynamicCast<DeclarativeFrontend>(frontend);
81 #endif
82 
83     auto window = std::make_shared<NG::RosenWindow>(rsWindow, taskExecutor, instanceId);
84     auto pipelineContext =
85         AceType::MakeRefPtr<NG::PipelineContext>(window, taskExecutor, assetManagerImpl, nullptr, frontend, instanceId);
86     AddGlobalPipelineContext(name, pipelineContext);
87 
88     auto windowLifeCycle = sptr<WindowLifeCycle>::MakeSptr(name);
89     rsWindow->RegisterLifeCycleListener(windowLifeCycle);
90 
91     return pipelineContext;
92 }
93 
AddGlobalPipelineContext(const std::string & name,const RefPtr<NG::PipelineContext> & pipelineContext)94 void GlobalPipelineContextManager::AddGlobalPipelineContext(
95     const std::string& name, const RefPtr<NG::PipelineContext>& pipelineContext)
96 {
97     auto iter = globalContextMap_.find(name);
98     if (iter != globalContextMap_.end()) {
99         LOGW("There already exists a global Pipeline Context with the same name!");
100         iter->second = pipelineContext;
101         return;
102     }
103     globalContextMap_.emplace(name, pipelineContext);
104 }
105 
RemoveGlobalPipelineContext(const std::string & name)106 RefPtr<NG::PipelineContext> GlobalPipelineContextManager::RemoveGlobalPipelineContext(const std::string& name)
107 {
108     auto iter = globalContextMap_.find(name);
109     if (iter == globalContextMap_.end()) {
110         return nullptr;
111     }
112     auto pipeline = iter->second;
113     globalContextMap_.erase(name);
114     return pipeline;
115 }
116 
RemoveModalPageNode(const std::string & name)117 RefPtr<NG::FrameNode> GlobalPipelineContextManager::RemoveModalPageNode(const std::string& name)
118 {
119     auto iter = modalPageMap_.find(name);
120     if (iter == modalPageMap_.end()) {
121         return nullptr;
122     }
123     auto modalPage = iter->second;
124     modalPageMap_.erase(name);
125     return modalPage;
126 }
127 
GetModalPageNode(const std::string & name)128 RefPtr<NG::FrameNode> GlobalPipelineContextManager::GetModalPageNode(const std::string& name)
129 {
130     auto iter = modalPageMap_.find(name);
131     if (iter == modalPageMap_.end()) {
132         return nullptr;
133     }
134     return iter->second;
135 }
136 
ProcessModalPageNode(const std::string & name,int32_t instanceId)137 void GlobalPipelineContextManager::ProcessModalPageNode(const std::string& name, int32_t instanceId)
138 {
139     auto globalPipelineContext = GetGlobalPipelineContext(name);
140     auto pipelineContext = NG::PipelineContext::GetContextByContainerId(instanceId);
141     if (!globalPipelineContext || !pipelineContext) {
142         LOGE("ProcessModalPageNode globalPipelineContext or pipelineContext invalid %{public}d", instanceId);
143         return;
144     }
145     if (modalPageMap_.find(name) == modalPageMap_.end()) {
146         auto globalOverlayManager = globalPipelineContext->GetOverlayManager();
147         CHECK_NULL_VOID(globalOverlayManager);
148         auto modalPageNode = globalOverlayManager->GetModalStackTop();
149         CHECK_NULL_VOID(modalPageNode);
150         auto globalParent = modalPageNode->GetParent();
151         if (globalParent) {
152             globalParent->RemoveChild(modalPageNode);
153             globalParent->RebuildRenderContextTree();
154         }
155         modalPageMap_.emplace(name, modalPageNode);
156     }
157     auto rootNode = pipelineContext->GetRootElement();
158     CHECK_NULL_VOID(rootNode);
159     auto modalPageNode = modalPageMap_[name];
160     CHECK_NULL_VOID(modalPageNode);
161     modalPageNode->MountToParent(rootNode);
162     rootNode->RebuildRenderContextTree();
163     rootNode->MarkDirtyNode(NG::PROPERTY_UPDATE_MEASURE);
164 }
165 
RegisterSessionId(const std::string & name,int32_t sessionId)166 void GlobalPipelineContextManager::RegisterSessionId(const std::string& name, int32_t sessionId)
167 {
168     auto iter = sessionIdMap_.find(name);
169     if (iter != sessionIdMap_.end()) {
170         LOGW("The sessionId %{public}d with the name %{public}s already exists", sessionId, name.c_str());
171         return;
172     }
173     sessionIdMap_.emplace(name, sessionId);
174 }
175 
GetUecNameBySessionId(int32_t sessionId)176 std::string GlobalPipelineContextManager::GetUecNameBySessionId(int32_t sessionId)
177 {
178     for (const auto& pair : sessionIdMap_) {
179         if (pair.second == sessionId) {
180             return pair.first;
181         }
182     }
183     LOGW("The sessionId %{public}d does not exist", sessionId);
184     return "";
185 }
186 
GetSessionId(const std::string & name)187 int32_t GlobalPipelineContextManager::GetSessionId(const std::string& name)
188 {
189     auto iter = sessionIdMap_.find(name);
190     if (iter == sessionIdMap_.end()) {
191         LOGW("The sessionId with the name %{public}s does not exist", name.c_str());
192         return 0;
193     }
194     return iter->second;
195 }
196 
RemoveSessionId(const std::string & name)197 void GlobalPipelineContextManager::RemoveSessionId(const std::string& name)
198 {
199     auto iter = sessionIdMap_.find(name);
200     if (iter == sessionIdMap_.end()) {
201         return;
202     }
203     sessionIdMap_.erase(name);
204 }
205 
GlobalPipelineContextManager()206 GlobalPipelineContextManager::GlobalPipelineContextManager()
207 {
208     threadModelImpl_ = ThreadModelImpl::CreateThreadModel(false, false, !SystemProperties::GetRosenBackendEnabled());
209 }
210 } // namespace OHOS::Ace
211