1 /*
2 * Copyright (c) 2024 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 "screen_scene.h"
17
18 #include <event_handler.h>
19 #include <ui_content.h>
20 #include <viewport_config.h>
21
22 #include "anr_manager.h"
23 #include "app_mgr_client.h"
24 #include "dm_common.h"
25 #include "singleton.h"
26 #include "singleton_container.h"
27 #include "window_manager_hilog.h"
28
29 namespace OHOS {
30 namespace Rosen {
31 namespace {
32 constexpr float MIN_DPI = 1e-6;
33 std::atomic<bool> g_ssIsDestroyed = false;
34 } // namespace
35
ScreenScene(std::string name)36 ScreenScene::ScreenScene(std::string name) : name_(name)
37 {
38 orientation_ = static_cast<int32_t>(DisplayOrientation::PORTRAIT);
39 NodeId nodeId = 0;
40 vsyncStation_ = std::make_shared<VsyncStation>(nodeId);
41 handler_ = std::make_shared<AppExecFwk::EventHandler>(AppExecFwk::EventRunner::GetMainEventRunner());
42 g_ssIsDestroyed = false;
43 }
44
~ScreenScene()45 ScreenScene::~ScreenScene()
46 {
47 g_ssIsDestroyed = true;
48 Destroy();
49 }
50
Destroy()51 WMError ScreenScene::Destroy()
52 {
53 std::function<void()> task; //延长task的生命周期
54 {
55 std::lock_guard<std::mutex> lock(mutex_);
56 if (!uiContent_) {
57 TLOGD(WmsLogTag::DMS, "Destroy uiContent_ is nullptr!");
58 return WMError::WM_OK;
59 }
60 std::shared_ptr<Ace::UIContent> uiContent = std::move(uiContent_);
61 uiContent_ = nullptr;
62 vsyncStation_->Destroy();
63 task = [uiContent]() {
64 if (uiContent != nullptr) {
65 uiContent->Destroy();
66 TLOGD(WmsLogTag::DMS, "ScreenScene: uiContent destroy success!");
67 }
68 };
69 }
70 if (handler_) {
71 handler_->PostSyncTask(task, "ScreenScene:Destroy");
72 } else {
73 task();
74 }
75 return WMError::WM_OK;
76 }
77
LoadContent(const std::string & contentUrl,napi_env env,napi_value storage,AbilityRuntime::Context * context)78 void ScreenScene::LoadContent(const std::string& contentUrl, napi_env env, napi_value storage,
79 AbilityRuntime::Context* context)
80 {
81 if (g_ssIsDestroyed) {
82 TLOGI(WmsLogTag::DMS, "ScreenScene has been destructed!");
83 return;
84 }
85 if (context == nullptr) {
86 TLOGE(WmsLogTag::DMS, "context is nullptr!");
87 return;
88 }
89 std::lock_guard<std::mutex> lock(mutex_);
90 uiContent_ = Ace::UIContent::Create(context, reinterpret_cast<NativeEngine*>(env));
91 if (uiContent_ == nullptr) {
92 TLOGE(WmsLogTag::DMS, "uiContent_ is nullptr!");
93 return;
94 }
95
96 uiContent_->Initialize(this, contentUrl, storage);
97 uiContent_->Foreground();
98 uiContent_->SetFrameLayoutFinishCallback(std::move(frameLayoutFinishCb_));
99 DelayedSingleton<ANRManager>::GetInstance()->Init();
100 DelayedSingleton<ANRManager>::GetInstance()->SetAnrObserver(([](int32_t pid) {
101 TLOGD(WmsLogTag::DMS, "Receive anr notice enter");
102 AppExecFwk::AppFaultDataBySA faultData;
103 faultData.faultType = AppExecFwk::FaultDataType::APP_FREEZE;
104 faultData.pid = pid;
105 faultData.errorObject.name = AppExecFwk::AppFreezeType::APP_INPUT_BLOCK;
106 faultData.errorObject.message = "User input does not respond normally, report by sceneBoard.";
107 faultData.errorObject.stack = "";
108 if (int32_t ret = DelayedSingleton<AppExecFwk::AppMgrClient>::GetInstance()->NotifyAppFaultBySA(faultData);
109 ret != 0) {
110 TLOGE(WmsLogTag::DMS, "NotifyAppFaultBySA failed, errcode:%{public}d", ret);
111 }
112 TLOGD(WmsLogTag::DMS, "Receive anr notice leave");
113 }));
114 DelayedSingleton<ANRManager>::GetInstance()->SetAppInfoGetter(
115 [](int32_t pid, std::string& bundleName, int32_t uid) {
116 int32_t ret = DelayedSingleton<AppExecFwk::AppMgrClient>::GetInstance()->GetBundleNameByPid(
117 pid, bundleName, uid);
118 if (ret != 0) {
119 TLOGE(WmsLogTag::DMS, "GetBundleNameByPid failed, pid:%{public}d, errcode:%{public}d", pid, ret);
120 }
121 });
122 }
123
UpdateViewportConfig(const Rect & rect,WindowSizeChangeReason reason)124 void ScreenScene::UpdateViewportConfig(const Rect& rect, WindowSizeChangeReason reason)
125 {
126 if (g_ssIsDestroyed) {
127 TLOGI(WmsLogTag::DMS, "ScreenScene has been destructed!");
128 return;
129 }
130 std::lock_guard<std::mutex> lock(mutex_);
131 if (uiContent_ == nullptr) {
132 TLOGE(WmsLogTag::DMS, "uiContent_ is nullptr!");
133 return;
134 }
135 Ace::ViewportConfig config;
136 config.SetSize(rect.width_, rect.height_);
137 config.SetPosition(rect.posX_, rect.posY_);
138 config.SetDensity(density_);
139 config.SetOrientation(orientation_);
140 uiContent_->UpdateViewportConfig(config, reason);
141 }
142
UpdateConfiguration(const std::shared_ptr<AppExecFwk::Configuration> & configuration)143 void ScreenScene::UpdateConfiguration(const std::shared_ptr<AppExecFwk::Configuration>& configuration)
144 {
145 if (g_ssIsDestroyed) {
146 TLOGI(WmsLogTag::DMS, "ScreenScene has been destructed!");
147 return;
148 }
149 std::lock_guard<std::mutex> lock(mutex_);
150 if (uiContent_) {
151 TLOGD(WmsLogTag::DMS, "notify root scene ace");
152 uiContent_->UpdateConfiguration(configuration);
153 }
154 }
155
RequestVsync(const std::shared_ptr<VsyncCallback> & vsyncCallback)156 void ScreenScene::RequestVsync(const std::shared_ptr<VsyncCallback>& vsyncCallback)
157 {
158 vsyncStation_->RequestVsync(vsyncCallback);
159 }
160
GetVSyncPeriod()161 int64_t ScreenScene::GetVSyncPeriod()
162 {
163 return vsyncStation_->GetVSyncPeriod();
164 }
165
FlushFrameRate(uint32_t rate,int32_t animatorExpectedFrameRate,uint32_t rateType)166 void ScreenScene::FlushFrameRate(uint32_t rate, int32_t animatorExpectedFrameRate, uint32_t rateType)
167 {
168 vsyncStation_->FlushFrameRate(rate, animatorExpectedFrameRate, rateType);
169 }
170
OnBundleUpdated(const std::string & bundleName)171 void ScreenScene::OnBundleUpdated(const std::string& bundleName)
172 {
173 if (g_ssIsDestroyed) {
174 TLOGI(WmsLogTag::DMS, "ScreenScene has been destructed!");
175 return;
176 }
177 TLOGD(WmsLogTag::DMS, "bundle %{public}s updated", bundleName.c_str());
178 std::lock_guard<std::mutex> lock(mutex_);
179 if (uiContent_) {
180 uiContent_->UpdateResource();
181 }
182 }
183
SetFrameLayoutFinishCallback(std::function<void ()> && callback)184 void ScreenScene::SetFrameLayoutFinishCallback(std::function<void()>&& callback)
185 {
186 if (g_ssIsDestroyed) {
187 TLOGI(WmsLogTag::DMS, "ScreenScene has been destructed!");
188 return;
189 }
190 frameLayoutFinishCb_ = callback;
191 std::lock_guard<std::mutex> lock(mutex_);
192 if (uiContent_) {
193 uiContent_->SetFrameLayoutFinishCallback(std::move(frameLayoutFinishCb_));
194 }
195 TLOGI(WmsLogTag::WMS_LAYOUT, "SetFrameLayoutFinishCallback end");
196 }
197
SetDisplayDensity(float density)198 void ScreenScene::SetDisplayDensity(float density)
199 {
200 if (density < MIN_DPI) {
201 TLOGE(WmsLogTag::DMS, "invalid density");
202 return;
203 }
204 density_ = density;
205 }
206
SetDisplayOrientation(int32_t orientation)207 void ScreenScene::SetDisplayOrientation(int32_t orientation)
208 {
209 if (orientation < static_cast<int32_t>(DisplayOrientation::PORTRAIT) ||
210 orientation > static_cast<int32_t>(DisplayOrientation::UNKNOWN)) {
211 TLOGE(WmsLogTag::DMS, "invalid orientation");
212 return;
213 }
214 orientation_ = orientation;
215 }
216 } // namespace Rosen
217 } // namespace OHOS
218