• 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 "screen_session_manager_client.h"
17 
18 #include <iservice_registry.h>
19 #include <system_ability_definition.h>
20 #include "os_account_manager.h"
21 #include "window_manager_hilog.h"
22 
23 namespace OHOS::Rosen {
24 namespace {
25 constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, HILOG_DOMAIN_DISPLAY, "ScreenSessionManagerClient" };
26 std::mutex g_instanceMutex;
27 } // namespace
28 
GetInstance()29 ScreenSessionManagerClient& ScreenSessionManagerClient::GetInstance()
30 {
31     std::lock_guard<std::mutex> lock(g_instanceMutex);
32     static sptr<ScreenSessionManagerClient> instance = nullptr;
33     if (instance == nullptr) {
34         instance = new ScreenSessionManagerClient();
35     }
36     return *instance;
37 }
38 
ConnectToServer()39 void ScreenSessionManagerClient::ConnectToServer()
40 {
41     if (screenSessionManager_) {
42         return;
43     }
44     auto systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
45     if (!systemAbilityMgr) {
46         WLOGFE("Failed to get system ability mgr");
47         return;
48     }
49 
50     auto remoteObject = systemAbilityMgr->GetSystemAbility(DISPLAY_MANAGER_SERVICE_SA_ID);
51     if (!remoteObject) {
52         WLOGFE("Failed to get display manager service");
53         return;
54     }
55 
56     screenSessionManager_ = iface_cast<IScreenSessionManager>(remoteObject);
57     if (!screenSessionManager_) {
58         WLOGFE("Failed to get screen session manager proxy");
59         return;
60     }
61     std::vector<int32_t> userIds;
62     int32_t userId = 0;
63     ErrCode errCode = OHOS::AccountSA::OsAccountManager::QueryActiveOsAccountIds(userIds);
64     if (errCode != ERR_OK || userIds.empty()) {
65         WLOGFE("get userId failed");
66     } else {
67         userId = userIds[0];
68     }
69     WLOGFI("SetClient in userid: %{public}d", userId);
70 
71     screenSessionManager_->SetClient(this, userId);
72 }
73 
RegisterScreenConnectionListener(IScreenConnectionListener * listener)74 void ScreenSessionManagerClient::RegisterScreenConnectionListener(IScreenConnectionListener* listener)
75 {
76     if (listener == nullptr) {
77         WLOGFE("Failed to register screen connection listener, listener is null");
78         return;
79     }
80 
81     screenConnectionListener_ = listener;
82     ConnectToServer();
83 }
84 
CheckIfNeedCennectScreen(ScreenId screenId,ScreenId rsId,const std::string & name)85 bool ScreenSessionManagerClient::CheckIfNeedCennectScreen(ScreenId screenId, ScreenId rsId, const std::string& name)
86 {
87     if (rsId == SCREEN_ID_INVALID) {
88         WLOGFE("rsId is invalid");
89         return false;
90     }
91     if (screenSessionManager_->GetScreenProperty(screenId).GetScreenType() == ScreenType::VIRTUAL) {
92         if (name == "HiCar" || name == "SuperLauncher") {
93             WLOGFI("HiCar or SuperLauncher, need to connect the screen");
94             return true;
95         } else {
96             WLOGFE("ScreenType is virtual, no need to connect the screen");
97             return false;
98         }
99     }
100     return true;
101 }
102 
OnScreenConnectionChanged(ScreenId screenId,ScreenEvent screenEvent,ScreenId rsId,const std::string & name)103 void ScreenSessionManagerClient::OnScreenConnectionChanged(ScreenId screenId, ScreenEvent screenEvent,
104     ScreenId rsId, const std::string& name)
105 {
106     WLOGFI("screenId: %{public}" PRIu64 " screenEvent: %{public}d rsId: %{public}" PRIu64 " name: %{public}s",
107         screenId, static_cast<int>(screenEvent), rsId, name.c_str());
108     if (screenEvent == ScreenEvent::CONNECTED) {
109         if (!CheckIfNeedCennectScreen(screenId, rsId, name)) {
110             WLOGFE("There is no need to connect the screen");
111             return;
112         }
113         auto screenProperty = screenSessionManager_->GetScreenProperty(screenId);
114         auto displayNode = screenSessionManager_->GetDisplayNode(screenId);
115         sptr<ScreenSession> screenSession = new ScreenSession(screenId, rsId, name, screenProperty, displayNode);
116         {
117             std::lock_guard<std::mutex> lock(screenSessionMapMutex_);
118             screenSessionMap_.emplace(screenId, screenSession);
119         }
120         if (screenConnectionListener_) {
121             screenConnectionListener_->OnScreenConnected(screenSession);
122         }
123         screenSession->Connect();
124         return;
125     }
126     if (screenEvent == ScreenEvent::DISCONNECTED) {
127         auto screenSession = GetScreenSession(screenId);
128         if (!screenSession) {
129             WLOGFE("screenSession is null");
130             return;
131         }
132         if (screenConnectionListener_) {
133             screenConnectionListener_->OnScreenDisconnected(screenSession);
134         }
135         {
136             std::lock_guard<std::mutex> lock(screenSessionMapMutex_);
137             screenSessionMap_.erase(screenId);
138         }
139     }
140 }
141 
GetScreenSession(ScreenId screenId) const142 sptr<ScreenSession> ScreenSessionManagerClient::GetScreenSession(ScreenId screenId) const
143 {
144     std::lock_guard<std::mutex> lock(screenSessionMapMutex_);
145     auto iter = screenSessionMap_.find(screenId);
146     if (iter == screenSessionMap_.end()) {
147         WLOGFD("Error found screen session with id: %{public}" PRIu64, screenId);
148         return nullptr;
149     }
150     return iter->second;
151 }
152 
OnPropertyChanged(ScreenId screenId,const ScreenProperty & property,ScreenPropertyChangeReason reason)153 void ScreenSessionManagerClient::OnPropertyChanged(ScreenId screenId,
154     const ScreenProperty& property, ScreenPropertyChangeReason reason)
155 {
156     auto screenSession = GetScreenSession(screenId);
157     if (!screenSession) {
158         WLOGFE("screenSession is null");
159         return;
160     }
161     screenSession->PropertyChange(property, reason);
162 }
163 
OnPowerStatusChanged(DisplayPowerEvent event,EventStatus status,PowerStateChangeReason reason)164 void ScreenSessionManagerClient::OnPowerStatusChanged(DisplayPowerEvent event, EventStatus status,
165     PowerStateChangeReason reason)
166 {
167     std::lock_guard<std::mutex> lock(screenSessionMapMutex_);
168     for (auto screenSession:screenSessionMap_) {
169         (screenSession.second)->PowerStatusChange(event, status, reason);
170     }
171 }
172 
OnSensorRotationChanged(ScreenId screenId,float sensorRotation)173 void ScreenSessionManagerClient::OnSensorRotationChanged(ScreenId screenId, float sensorRotation)
174 {
175     auto screenSession = GetScreenSession(screenId);
176     if (!screenSession) {
177         WLOGFE("screenSession is null");
178         return;
179     }
180     screenSession->SensorRotationChange(sensorRotation);
181 }
182 
OnScreenOrientationChanged(ScreenId screenId,float screenOrientation)183 void ScreenSessionManagerClient::OnScreenOrientationChanged(ScreenId screenId, float screenOrientation)
184 {
185     auto screenSession = GetScreenSession(screenId);
186     if (!screenSession) {
187         WLOGFE("screenSession is null");
188         return;
189     }
190     screenSession->ScreenOrientationChange(screenOrientation);
191 }
192 
OnScreenRotationLockedChanged(ScreenId screenId,bool isLocked)193 void ScreenSessionManagerClient::OnScreenRotationLockedChanged(ScreenId screenId, bool isLocked)
194 {
195     auto screenSession = GetScreenSession(screenId);
196     if (!screenSession) {
197         WLOGFE("screenSession is null");
198         return;
199     }
200     screenSession->SetScreenRotationLocked(isLocked);
201 }
202 
RegisterDisplayChangeListener(const sptr<IDisplayChangeListener> & listener)203 void ScreenSessionManagerClient::RegisterDisplayChangeListener(const sptr<IDisplayChangeListener>& listener)
204 {
205     displayChangeListener_ = listener;
206 }
207 
OnDisplayStateChanged(DisplayId defaultDisplayId,sptr<DisplayInfo> displayInfo,const std::map<DisplayId,sptr<DisplayInfo>> & displayInfoMap,DisplayStateChangeType type)208 void ScreenSessionManagerClient::OnDisplayStateChanged(DisplayId defaultDisplayId, sptr<DisplayInfo> displayInfo,
209     const std::map<DisplayId, sptr<DisplayInfo>>& displayInfoMap, DisplayStateChangeType type)
210 {
211     if (displayChangeListener_) {
212         displayChangeListener_->OnDisplayStateChange(defaultDisplayId, displayInfo, displayInfoMap, type);
213     }
214 }
215 
OnGetSurfaceNodeIdsFromMissionIdsChanged(std::vector<uint64_t> & missionIds,std::vector<uint64_t> & surfaceNodeIds)216 void ScreenSessionManagerClient::OnGetSurfaceNodeIdsFromMissionIdsChanged(std::vector<uint64_t>& missionIds,
217     std::vector<uint64_t>& surfaceNodeIds)
218 {
219     if (displayChangeListener_) {
220         displayChangeListener_->OnGetSurfaceNodeIdsFromMissionIds(missionIds, surfaceNodeIds);
221     }
222 }
223 
OnScreenshot(DisplayId displayId)224 void ScreenSessionManagerClient::OnScreenshot(DisplayId displayId)
225 {
226     if (displayChangeListener_) {
227         displayChangeListener_->OnScreenshot(displayId);
228     }
229 }
230 
OnImmersiveStateChanged(bool & immersive)231 void ScreenSessionManagerClient::OnImmersiveStateChanged(bool& immersive)
232 {
233     if (displayChangeListener_ != nullptr) {
234         displayChangeListener_->OnImmersiveStateChange(immersive);
235     }
236 }
237 
GetAllScreensProperties() const238 std::unordered_map<ScreenId, ScreenProperty> ScreenSessionManagerClient::GetAllScreensProperties() const
239 {
240     std::lock_guard<std::mutex> lock(screenSessionMapMutex_);
241     std::unordered_map<ScreenId, ScreenProperty> screensProperties;
242     for (const auto& iter: screenSessionMap_) {
243         auto session = iter.second;
244         if (session == nullptr) {
245             continue;
246         }
247         screensProperties[iter.first] = session->GetScreenProperty();
248     }
249     return screensProperties;
250 }
251 
GetFoldDisplayMode() const252 FoldDisplayMode ScreenSessionManagerClient::GetFoldDisplayMode() const
253 {
254     if (screenSessionManager_ == nullptr) {
255         WLOGFE("screenSessionManager_ is null while get displayMode");
256         return FoldDisplayMode::UNKNOWN;
257     }
258     return screenSessionManager_->GetFoldDisplayMode();
259 }
260 
UpdateScreenRotationProperty(ScreenId screenId,const RRect & bounds,float rotation)261 void ScreenSessionManagerClient::UpdateScreenRotationProperty(ScreenId screenId, const RRect& bounds, float rotation)
262 {
263     if (!screenSessionManager_) {
264         WLOGFE("screenSessionManager_ is null");
265         return;
266     }
267     screenSessionManager_->UpdateScreenRotationProperty(screenId, bounds, rotation);
268 
269     auto screenSession = GetScreenSession(screenId);
270     if (!screenSession) {
271         WLOGFE("screenSession is null");
272         return;
273     }
274     auto foldDisplayMode = screenSessionManager_->GetFoldDisplayMode();
275     screenSession->UpdateToInputManager(bounds, rotation, foldDisplayMode);
276 }
277 
SetDisplayNodeScreenId(ScreenId screenId,ScreenId displayNodeScreenId)278 void ScreenSessionManagerClient::SetDisplayNodeScreenId(ScreenId screenId, ScreenId displayNodeScreenId)
279 {
280     auto screenSession = GetScreenSession(screenId);
281     if (!screenSession) {
282         WLOGFE("screenSession is null");
283         return;
284     }
285     screenSession->SetDisplayNodeScreenId(displayNodeScreenId);
286 }
287 
GetCurvedCompressionArea()288 uint32_t ScreenSessionManagerClient::GetCurvedCompressionArea()
289 {
290     if (!screenSessionManager_) {
291         WLOGFE("screenSessionManager_ is null");
292         return 0;
293     }
294     return screenSessionManager_->GetCurvedCompressionArea();
295 }
296 
GetPhyScreenProperty(ScreenId screenId)297 ScreenProperty ScreenSessionManagerClient::GetPhyScreenProperty(ScreenId screenId)
298 {
299     if (!screenSessionManager_) {
300         WLOGFE("screenSessionManager_ is null");
301         return {};
302     }
303     return screenSessionManager_->GetPhyScreenProperty(screenId);
304 }
305 
306 __attribute__((no_sanitize("cfi")))
NotifyDisplayChangeInfoChanged(const sptr<DisplayChangeInfo> & info)307 void ScreenSessionManagerClient::NotifyDisplayChangeInfoChanged(const sptr<DisplayChangeInfo>& info)
308 {
309     if (!screenSessionManager_) {
310         WLOGFE("screenSessionManager_ is null");
311         return;
312     }
313     screenSessionManager_->NotifyDisplayChangeInfoChanged(info);
314 }
315 
SetScreenPrivacyState(bool hasPrivate)316 void ScreenSessionManagerClient::SetScreenPrivacyState(bool hasPrivate)
317 {
318     if (!screenSessionManager_) {
319         WLOGFE("screenSessionManager_ is null");
320         return;
321     }
322     WLOGFI("Begin calling the SetScreenPrivacyState() of screenSessionManager_, hasPrivate: %{public}d", hasPrivate);
323     screenSessionManager_->SetScreenPrivacyState(hasPrivate);
324     WLOGFI("End calling the SetScreenPrivacyState() of screenSessionManager_");
325 }
326 
UpdateAvailableArea(ScreenId screenId,DMRect area)327 void ScreenSessionManagerClient::UpdateAvailableArea(ScreenId screenId, DMRect area)
328 {
329     if (!screenSessionManager_) {
330         WLOGFE("screenSessionManager_ is null");
331         return;
332     }
333     screenSessionManager_->UpdateAvailableArea(screenId, area);
334 }
335 
NotifyFoldToExpandCompletion(bool foldToExpand)336 void ScreenSessionManagerClient::NotifyFoldToExpandCompletion(bool foldToExpand)
337 {
338     if (!screenSessionManager_) {
339         WLOGFE("screenSessionManager_ is null");
340         return;
341     }
342     screenSessionManager_->NotifyFoldToExpandCompletion(foldToExpand);
343 }
344 
GetFoldStatus()345 FoldStatus ScreenSessionManagerClient::GetFoldStatus()
346 {
347     if (!screenSessionManager_) {
348         WLOGFE("screenSessionManager_ is null");
349         return FoldStatus::UNKNOWN;
350     }
351     return screenSessionManager_->GetFoldStatus();
352 }
353 
GetScreenSnapshot(ScreenId screenId,float scaleX,float scaleY)354 std::shared_ptr<Media::PixelMap> ScreenSessionManagerClient::GetScreenSnapshot(ScreenId screenId,
355     float scaleX, float scaleY)
356 {
357     auto screenSession = GetScreenSession(screenId);
358     if (!screenSession) {
359         WLOGFE("get screen session is null");
360         return nullptr;
361     }
362     return screenSession->GetScreenSnapshot(scaleX, scaleY);
363 }
364 
GetScreenSessionById(const ScreenId id)365 sptr<ScreenSession>ScreenSessionManagerClient::GetScreenSessionById(const ScreenId id)
366 {
367     std::lock_guard<std::mutex> lock(screenSessionMapMutex_);
368     auto iter = screenSessionMap_.find(id);
369     if (iter == screenSessionMap_.end()) {
370         return nullptr;
371     }
372     return iter->second;
373 }
374 } // namespace OHOS::Rosen
375