• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "root_scene.h"
17 
18 #include <event_handler.h>
19 #include <input_manager.h>
20 #include <ui_content.h>
21 #include <viewport_config.h>
22 
23 #include "app_mgr_client.h"
24 #include "singleton.h"
25 #include "singleton_container.h"
26 
27 #include "anr_manager.h"
28 #include "intention_event_manager.h"
29 #include "vsync_station.h"
30 #include "window_manager_hilog.h"
31 
32 namespace OHOS {
33 namespace Rosen {
34 namespace {
35 constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, HILOG_DOMAIN_WINDOW, "RootScene" };
36 const std::string INPUT_AND_VSYNC_THREAD = "InputAndVsyncThread";
37 
38 class InputEventListener : public MMI::IInputEventConsumer {
39 public:
InputEventListener(RootScene * rootScene)40     explicit InputEventListener(RootScene* rootScene): rootScene_(rootScene) {}
41     virtual ~InputEventListener() = default;
42 
OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const43     void OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const override
44     {
45         rootScene_->ConsumePointerEvent(pointerEvent);
46     }
47 
OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const48     void OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const override
49     {
50         rootScene_->ConsumeKeyEvent(keyEvent);
51     }
52 
OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent) const53     void OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent) const override
54     {
55     }
56 
57 private:
58     RootScene* rootScene_;
59 };
60 } // namespace
61 
62 sptr<RootScene> RootScene::staticRootScene_;
63 
RootScene()64 RootScene::RootScene()
65 {
66 }
67 
~RootScene()68 RootScene::~RootScene()
69 {
70 }
71 
LoadContent(const std::string & contentUrl,NativeEngine * engine,NativeValue * storage,AbilityRuntime::Context * context)72 void RootScene::LoadContent(const std::string& contentUrl, NativeEngine* engine, NativeValue* storage,
73     AbilityRuntime::Context* context)
74 {
75     if (context == nullptr) {
76         WLOGFE("context is nullptr!");
77         return;
78     }
79     uiContent_ = Ace::UIContent::Create(context, engine);
80     if (uiContent_ == nullptr) {
81         WLOGFE("uiContent_ is nullptr!");
82         return;
83     }
84 
85     uiContent_->Initialize(this, contentUrl, storage);
86     uiContent_->Foreground();
87 
88     RegisterInputEventListener();
89     DelayedSingleton<ANRManager>::GetInstance()->Init();
90     DelayedSingleton<ANRManager>::GetInstance()->SetAnrObserver(([](int32_t pid) {
91         WLOGFD("Receive anr notice enter");
92         AppExecFwk::AppFaultDataBySA faultData;
93         faultData.faultType = AppExecFwk::FaultDataType::APP_FREEZE;
94         faultData.pid = pid;
95         faultData.errorObject.name = AppExecFwk::AppFreezeType::APP_INPUT_BLOCK;
96         faultData.errorObject.message = "User input does not respond normally";
97         faultData.errorObject.stack = "";
98         if (int32_t ret = DelayedSingleton<AppExecFwk::AppMgrClient>::GetInstance()->NotifyAppFaultBySA(faultData);
99             ret != 0) {
100             WLOGFE("NotifyAppFaultBySA failed, pid:%{public}d, errcode:%{public}d", pid, ret);
101         }
102         WLOGFD("Receive anr notice leave");
103     }));
104     DelayedSingleton<ANRManager>::GetInstance()->SetAppInfoGetter(
105         [](int32_t pid, std::string& bundleName, int32_t uid) {
106             int32_t ret = DelayedSingleton<AppExecFwk::AppMgrClient>::GetInstance()->GetBundleNameByPid(
107                 pid, bundleName, uid);
108             if (ret != 0) {
109                 WLOGFE("GetBundleNameByPid failed, pid:%{public}d, errcode:%{public}d", pid, ret);
110             }
111         });
112 }
113 
UpdateViewportConfig(const Rect & rect,WindowSizeChangeReason reason)114 void RootScene::UpdateViewportConfig(const Rect& rect, WindowSizeChangeReason reason)
115 {
116     std::lock_guard<std::recursive_mutex> lock(mutex_);
117     if (uiContent_ == nullptr) {
118         WLOGFE("uiContent_ is nullptr!");
119         return;
120     }
121     Ace::ViewportConfig config;
122     config.SetSize(rect.width_, rect.height_);
123     config.SetPosition(rect.posX_, rect.posY_);
124     config.SetDensity(density_);
125     uiContent_->UpdateViewportConfig(config, reason);
126 }
127 
UpdateConfiguration(const std::shared_ptr<AppExecFwk::Configuration> & configuration)128 void RootScene::UpdateConfiguration(const std::shared_ptr<AppExecFwk::Configuration>& configuration)
129 {
130     if (uiContent_) {
131         WLOGFD("notify root scene ace");
132         uiContent_->UpdateConfiguration(configuration);
133     }
134 }
UpdateConfigurationForAll(const std::shared_ptr<AppExecFwk::Configuration> & configuration)135 void RootScene::UpdateConfigurationForAll(const std::shared_ptr<AppExecFwk::Configuration>& configuration)
136 {
137     WLOGD("notify root scene ace for all");
138     if (staticRootScene_) {
139         staticRootScene_->UpdateConfiguration(configuration);
140     }
141 }
142 
RegisterInputEventListener()143 void RootScene::RegisterInputEventListener()
144 {
145     auto listener = std::make_shared<InputEventListener>(this);
146     auto mainEventRunner = AppExecFwk::EventRunner::GetMainEventRunner();
147     if (mainEventRunner) {
148         WLOGFD("MainEventRunner is available");
149         eventHandler_ = std::make_shared<AppExecFwk::EventHandler>(mainEventRunner);
150     } else {
151         WLOGFD("MainEventRunner is not available");
152         eventHandler_ = AppExecFwk::EventHandler::Current();
153         if (!eventHandler_) {
154             eventHandler_ =
155                 std::make_shared<AppExecFwk::EventHandler>(AppExecFwk::EventRunner::Create(INPUT_AND_VSYNC_THREAD));
156         }
157         VsyncStation::GetInstance().SetIsMainHandlerAvailable(false);
158         VsyncStation::GetInstance().SetVsyncEventHandler(eventHandler_);
159     }
160     if (!(DelayedSingleton<IntentionEventManager>::GetInstance()->EnableInputEventListener(
161             uiContent_.get(), eventHandler_))) {
162         WLOGFE("EnableInputEventListener fail");
163     }
164 }
165 
RequestVsync(const std::shared_ptr<VsyncCallback> & vsyncCallback)166 void RootScene::RequestVsync(const std::shared_ptr<VsyncCallback>& vsyncCallback)
167 {
168     std::lock_guard<std::recursive_mutex> lock(mutex_);
169     VsyncStation::GetInstance().RequestVsync(vsyncCallback);
170 }
171 
GetVSyncPeriod()172 int64_t RootScene::GetVSyncPeriod()
173 {
174     std::lock_guard<std::recursive_mutex> lock(mutex_);
175     return VsyncStation::GetInstance().GetVSyncPeriod();
176 }
177 } // namespace Rosen
178 } // namespace OHOS
179