• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "display_manager_service.h"
17 
18 #include <cinttypes>
19 #include <hitrace_meter.h>
20 #include <ipc_skeleton.h>
21 #include <iservice_registry.h>
22 #include <system_ability_definition.h>
23 
24 #include "display_manager_agent_controller.h"
25 #include "display_manager_config.h"
26 #include "dm_common.h"
27 #include "parameters.h"
28 #include "permission.h"
29 #include "sensor_connector.h"
30 #include "transaction/rs_interfaces.h"
31 #include "window_manager_hilog.h"
32 
33 namespace OHOS::Rosen {
34 namespace {
35     constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_DISPLAY, "DisplayManagerService"};
36     const std::string SCREEN_CAPTURE_PERMISSION = "ohos.permission.CAPTURE_SCREEN";
37 }
38 WM_IMPLEMENT_SINGLE_INSTANCE(DisplayManagerService)
39 const bool REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility(&SingletonContainer::Get<DisplayManagerService>());
40 
41 #define CHECK_SCREEN_AND_RETURN(ret) \
42     do { \
43         if (screenId == SCREEN_ID_INVALID) { \
44             WLOGFE("screenId invalid"); \
45             return ret; \
46         } \
47     } while (false)
48 
DisplayManagerService()49 DisplayManagerService::DisplayManagerService() : SystemAbility(DISPLAY_MANAGER_SERVICE_SA_ID, true),
50     abstractDisplayController_(new AbstractDisplayController(mutex_,
51         std::bind(&DisplayManagerService::NotifyDisplayStateChange, this,
52             std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4))),
53     abstractScreenController_(new AbstractScreenController(mutex_)),
54     displayPowerController_(new DisplayPowerController(mutex_,
55         std::bind(&DisplayManagerService::NotifyDisplayStateChange, this,
56             std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4))),
57     displayCutoutController_(new DisplayCutoutController()),
58     isAutoRotationOpen_(OHOS::system::GetParameter(
59         "persist.display.ar.enabled", "1") == "1") // autoRotation default enabled
60 {
61 }
62 
Dump(int fd,const std::vector<std::u16string> & args)63 int DisplayManagerService::Dump(int fd, const std::vector<std::u16string>& args)
64 {
65     if (displayDumper_ == nullptr) {
66         displayDumper_ = new DisplayDumper(abstractDisplayController_, abstractScreenController_, mutex_);
67     }
68     return static_cast<int>(displayDumper_->Dump(fd, args));
69 }
70 
OnStart()71 void DisplayManagerService::OnStart()
72 {
73     WLOGFI("start");
74     if (!Init()) {
75         WLOGFE("Init failed");
76         return;
77     }
78 
79     if (!Publish(this)) {
80         WLOGFE("Publish failed");
81     }
82     WLOGFI("end");
83 }
84 
Init()85 bool DisplayManagerService::Init()
86 {
87     WLOGFI("DisplayManagerService::Init start");
88     if (DisplayManagerConfig::LoadConfigXml()) {
89         DisplayManagerConfig::DumpConfig();
90         ConfigureDisplayManagerService();
91     }
92     abstractScreenController_->Init();
93     abstractDisplayController_->Init(abstractScreenController_);
94     WLOGFI("DisplayManagerService::Init success");
95     return true;
96 }
97 
ConfigureDisplayManagerService()98 void DisplayManagerService::ConfigureDisplayManagerService()
99 {
100     auto numbersConfig = DisplayManagerConfig::GetIntNumbersConfig();
101     auto enableConfig = DisplayManagerConfig::GetEnableConfig();
102     auto stringConfig = DisplayManagerConfig::GetStringConfig();
103     if (numbersConfig.count("defaultDeviceRotationOffset") != 0) {
104         uint32_t defaultDeviceRotationOffset = static_cast<uint32_t>(numbersConfig["defaultDeviceRotationOffset"][0]);
105         ScreenRotationController::SetDefaultDeviceRotationOffset(defaultDeviceRotationOffset);
106     }
107     if (enableConfig.count("isWaterfallDisplay") != 0) {
108         displayCutoutController_->SetIsWaterfallDisplay(
109             static_cast<bool>(enableConfig["isWaterfallDisplay"]));
110     }
111     if (numbersConfig.count("curvedScreenBoundary") != 0) {
112         displayCutoutController_->SetCurvedScreenBoundary(
113             static_cast<std::vector<int>>(numbersConfig["curvedScreenBoundary"]));
114     }
115     if (stringConfig.count("defaultDisplayCutoutPath") != 0) {
116         displayCutoutController_->SetBuiltInDisplayCutoutSvgPath(
117             static_cast<std::string>(stringConfig["defaultDisplayCutoutPath"]));
118     }
119     ConfigureWaterfallDisplayCompressionParams();
120     if (numbersConfig.count("buildInDefaultOrientation") != 0) {
121         Orientation orientation = static_cast<Orientation>(numbersConfig["buildInDefaultOrientation"][0]);
122         abstractScreenController_->SetBuildInDefaultOrientation(orientation);
123     }
124 }
125 
ConfigureWaterfallDisplayCompressionParams()126 void DisplayManagerService::ConfigureWaterfallDisplayCompressionParams()
127 {
128     auto numbersConfig = DisplayManagerConfig::GetIntNumbersConfig();
129     auto enableConfig = DisplayManagerConfig::GetEnableConfig();
130     if (enableConfig.count("isWaterfallAreaCompressionEnableWhenHorizontal") != 0) {
131         DisplayCutoutController::SetWaterfallAreaCompressionEnableWhenHorzontal(
132             static_cast<bool>(enableConfig["isWaterfallAreaCompressionEnableWhenHorizontal"]));
133     }
134     if (numbersConfig.count("waterfallAreaCompressionSizeWhenHorzontal") != 0) {
135         DisplayCutoutController::SetWaterfallAreaCompressionSizeWhenHorizontal(
136             static_cast<uint32_t>(numbersConfig["waterfallAreaCompressionSizeWhenHorzontal"][0]));
137     }
138 }
139 
RegisterDisplayChangeListener(sptr<IDisplayChangeListener> listener)140 void DisplayManagerService::RegisterDisplayChangeListener(sptr<IDisplayChangeListener> listener)
141 {
142     displayChangeListener_ = listener;
143     WLOGFI("IDisplayChangeListener registered");
144 }
145 
RegisterWindowInfoQueriedListener(const sptr<IWindowInfoQueriedListener> & listener)146 void DisplayManagerService::RegisterWindowInfoQueriedListener(const sptr<IWindowInfoQueriedListener>& listener)
147 {
148     windowInfoQueriedListener_ = listener;
149 }
150 
HasPrivateWindow(DisplayId displayId,bool & hasPrivateWindow)151 DMError DisplayManagerService::HasPrivateWindow(DisplayId displayId, bool& hasPrivateWindow)
152 {
153     if (!Permission::IsSystemCalling()) {
154         WLOGFE("check has private window permission denied!");
155         return DMError::DM_ERROR_INVALID_PERMISSION;
156     }
157     std::vector<DisplayId> displayIds = GetAllDisplayIds();
158     auto iter = std::find(displayIds.begin(), displayIds.end(), displayId);
159     if (iter == displayIds.end()) {
160         WLOGFE("invalid displayId");
161         return DMError::DM_ERROR_INVALID_PARAM;
162     }
163     if (windowInfoQueriedListener_ != nullptr) {
164         windowInfoQueriedListener_->HasPrivateWindow(displayId, hasPrivateWindow);
165         return DMError::DM_OK;
166     }
167     return DMError::DM_ERROR_NULLPTR;
168 }
169 
NotifyDisplayStateChange(DisplayId defaultDisplayId,sptr<DisplayInfo> displayInfo,const std::map<DisplayId,sptr<DisplayInfo>> & displayInfoMap,DisplayStateChangeType type)170 void DisplayManagerService::NotifyDisplayStateChange(DisplayId defaultDisplayId, sptr<DisplayInfo> displayInfo,
171     const std::map<DisplayId, sptr<DisplayInfo>>& displayInfoMap, DisplayStateChangeType type)
172 {
173     DisplayId id = (displayInfo == nullptr) ? DISPLAY_ID_INVALID : displayInfo->GetDisplayId();
174     WLOGFI("DisplayId %{public}" PRIu64"", id);
175     if (displayChangeListener_ != nullptr) {
176         displayChangeListener_->OnDisplayStateChange(defaultDisplayId, displayInfo, displayInfoMap, type);
177     }
178 }
179 
NotifyScreenshot(DisplayId displayId)180 void DisplayManagerService::NotifyScreenshot(DisplayId displayId)
181 {
182     if (displayChangeListener_ != nullptr) {
183         displayChangeListener_->OnScreenshot(displayId);
184     }
185 }
186 
GetDefaultDisplayInfo()187 sptr<DisplayInfo> DisplayManagerService::GetDefaultDisplayInfo()
188 {
189     ScreenId dmsScreenId = abstractScreenController_->GetDefaultAbstractScreenId();
190     WLOGFD("GetDefaultDisplayInfo %{public}" PRIu64"", dmsScreenId);
191     sptr<AbstractDisplay> display = abstractDisplayController_->GetAbstractDisplayByScreen(dmsScreenId);
192     if (display == nullptr) {
193         WLOGFE("fail to get displayInfo by id: invalid display");
194         return nullptr;
195     }
196     return display->ConvertToDisplayInfo();
197 }
198 
GetDisplayInfoById(DisplayId displayId)199 sptr<DisplayInfo> DisplayManagerService::GetDisplayInfoById(DisplayId displayId)
200 {
201     sptr<AbstractDisplay> display = abstractDisplayController_->GetAbstractDisplay(displayId);
202     if (display == nullptr) {
203         WLOGFE("fail to get displayInfo by id: invalid display");
204         return nullptr;
205     }
206     return display->ConvertToDisplayInfo();
207 }
208 
GetDisplayInfoByScreen(ScreenId screenId)209 sptr<DisplayInfo> DisplayManagerService::GetDisplayInfoByScreen(ScreenId screenId)
210 {
211     sptr<AbstractDisplay> display = abstractDisplayController_->GetAbstractDisplayByScreen(screenId);
212     if (display == nullptr) {
213         WLOGFE("fail to get displayInfo by screenId: invalid display");
214         return nullptr;
215     }
216     return display->ConvertToDisplayInfo();
217 }
218 
CreateVirtualScreen(VirtualScreenOption option,const sptr<IRemoteObject> & displayManagerAgent)219 ScreenId DisplayManagerService::CreateVirtualScreen(VirtualScreenOption option,
220     const sptr<IRemoteObject>& displayManagerAgent)
221 {
222     if (displayManagerAgent == nullptr) {
223         WLOGFE("displayManagerAgent invalid");
224         return SCREEN_ID_INVALID;
225     }
226     HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "dms:CreateVirtualScreen(%s)", option.name_.c_str());
227     if (option.surface_ != nullptr && !Permission::CheckCallingPermission(SCREEN_CAPTURE_PERMISSION) &&
228         !Permission::IsStartByHdcd()) {
229         WLOGFE("permission denied");
230         return SCREEN_ID_INVALID;
231     }
232     ScreenId screenId = abstractScreenController_->CreateVirtualScreen(option, displayManagerAgent);
233     CHECK_SCREEN_AND_RETURN(SCREEN_ID_INVALID);
234     accessTokenIdMaps_.insert(std::pair(screenId, IPCSkeleton::GetCallingTokenID()));
235     return screenId;
236 }
237 
DestroyVirtualScreen(ScreenId screenId)238 DMError DisplayManagerService::DestroyVirtualScreen(ScreenId screenId)
239 {
240     if (!Permission::IsSystemCalling()) {
241         WLOGFE("destory virtual screen permission denied!");
242         return DMError::DM_ERROR_INVALID_PERMISSION;
243     }
244     if (!accessTokenIdMaps_.isExistAndRemove(screenId, IPCSkeleton::GetCallingTokenID())) {
245         return DMError::DM_ERROR_INVALID_CALLING;
246     }
247 
248     WLOGFI("DestroyVirtualScreen::ScreenId: %{public}" PRIu64 "", screenId);
249     CHECK_SCREEN_AND_RETURN(DMError::DM_ERROR_INVALID_PARAM);
250 
251     HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "dms:DestroyVirtualScreen(%" PRIu64")", screenId);
252     return abstractScreenController_->DestroyVirtualScreen(screenId);
253 }
254 
SetVirtualScreenSurface(ScreenId screenId,sptr<Surface> surface)255 DMError DisplayManagerService::SetVirtualScreenSurface(ScreenId screenId, sptr<Surface> surface)
256 {
257     WLOGFI("SetVirtualScreenSurface::ScreenId: %{public}" PRIu64 "", screenId);
258     CHECK_SCREEN_AND_RETURN(DMError::DM_ERROR_INVALID_PARAM);
259     if (Permission::CheckCallingPermission(SCREEN_CAPTURE_PERMISSION) ||
260         Permission::IsStartByHdcd()) {
261         return abstractScreenController_->SetVirtualScreenSurface(screenId, surface);
262     }
263     WLOGFE("permission denied");
264     return DMError::DM_ERROR_INVALID_CALLING;
265 }
266 
SetOrientation(ScreenId screenId,Orientation orientation)267 bool DisplayManagerService::SetOrientation(ScreenId screenId, Orientation orientation)
268 {
269     if (!Permission::IsSystemCalling()) {
270         WLOGFE("set orientation permission denied!");
271         return false;
272     }
273     if (orientation < Orientation::UNSPECIFIED || orientation > Orientation::REVERSE_HORIZONTAL) {
274         WLOGFE("SetOrientation::orientation: %{public}u", static_cast<uint32_t>(orientation));
275         return false;
276     }
277     HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "dms:SetOrientation(%" PRIu64")", screenId);
278     return abstractScreenController_->SetOrientation(screenId, orientation, false);
279 }
280 
SetOrientationFromWindow(ScreenId screenId,Orientation orientation)281 bool DisplayManagerService::SetOrientationFromWindow(ScreenId screenId, Orientation orientation)
282 {
283     HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "dms:SetOrientationFromWindow(%" PRIu64")", screenId);
284     return abstractScreenController_->SetOrientation(screenId, orientation, true);
285 }
286 
SetRotationFromWindow(ScreenId screenId,Rotation targetRotation)287 bool DisplayManagerService::SetRotationFromWindow(ScreenId screenId, Rotation targetRotation)
288 {
289     HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "dms:SetRotationFromWindow(%" PRIu64")", screenId);
290     return abstractScreenController_->SetRotation(screenId, targetRotation, true);
291 }
292 
GetDisplaySnapshot(DisplayId displayId)293 std::shared_ptr<Media::PixelMap> DisplayManagerService::GetDisplaySnapshot(DisplayId displayId)
294 {
295     HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "dms:GetDisplaySnapshot(%" PRIu64")", displayId);
296     if (Permission::CheckCallingPermission(SCREEN_CAPTURE_PERMISSION) ||
297         Permission::IsStartByHdcd()) {
298         auto res = abstractDisplayController_->GetScreenSnapshot(displayId);
299         if (res != nullptr) {
300             NotifyScreenshot(displayId);
301         }
302         return res;
303     }
304     return nullptr;
305 }
306 
GetRSScreenNum() const307 uint32_t DisplayManagerService::GetRSScreenNum() const
308 {
309     return abstractScreenController_->GetRSScreenNum();
310 }
311 
GetScreenSupportedColorGamuts(ScreenId screenId,std::vector<ScreenColorGamut> & colorGamuts)312 DMError DisplayManagerService::GetScreenSupportedColorGamuts(ScreenId screenId,
313     std::vector<ScreenColorGamut>& colorGamuts)
314 {
315     WLOGFI("GetScreenSupportedColorGamuts::ScreenId: %{public}" PRIu64 "", screenId);
316     CHECK_SCREEN_AND_RETURN(DMError::DM_ERROR_INVALID_PARAM);
317     return abstractScreenController_->GetScreenSupportedColorGamuts(screenId, colorGamuts);
318 }
319 
GetScreenColorGamut(ScreenId screenId,ScreenColorGamut & colorGamut)320 DMError DisplayManagerService::GetScreenColorGamut(ScreenId screenId, ScreenColorGamut& colorGamut)
321 {
322     WLOGFI("GetScreenColorGamut::ScreenId: %{public}" PRIu64 "", screenId);
323     CHECK_SCREEN_AND_RETURN(DMError::DM_ERROR_INVALID_PARAM);
324     return abstractScreenController_->GetScreenColorGamut(screenId, colorGamut);
325 }
326 
SetScreenColorGamut(ScreenId screenId,int32_t colorGamutIdx)327 DMError DisplayManagerService::SetScreenColorGamut(ScreenId screenId, int32_t colorGamutIdx)
328 {
329     WLOGFI("SetScreenColorGamut::ScreenId: %{public}" PRIu64 ", colorGamutIdx %{public}d", screenId, colorGamutIdx);
330     CHECK_SCREEN_AND_RETURN(DMError::DM_ERROR_INVALID_PARAM);
331     return abstractScreenController_->SetScreenColorGamut(screenId, colorGamutIdx);
332 }
333 
GetScreenGamutMap(ScreenId screenId,ScreenGamutMap & gamutMap)334 DMError DisplayManagerService::GetScreenGamutMap(ScreenId screenId, ScreenGamutMap& gamutMap)
335 {
336     WLOGFI("GetScreenGamutMap::ScreenId: %{public}" PRIu64 "", screenId);
337     CHECK_SCREEN_AND_RETURN(DMError::DM_ERROR_INVALID_PARAM);
338     return abstractScreenController_->GetScreenGamutMap(screenId, gamutMap);
339 }
340 
SetScreenGamutMap(ScreenId screenId,ScreenGamutMap gamutMap)341 DMError DisplayManagerService::SetScreenGamutMap(ScreenId screenId, ScreenGamutMap gamutMap)
342 {
343     WLOGFI("SetScreenGamutMap::ScreenId: %{public}" PRIu64 ", ScreenGamutMap %{public}u",
344         screenId, static_cast<uint32_t>(gamutMap));
345     CHECK_SCREEN_AND_RETURN(DMError::DM_ERROR_INVALID_PARAM);
346     return abstractScreenController_->SetScreenGamutMap(screenId, gamutMap);
347 }
348 
SetScreenColorTransform(ScreenId screenId)349 DMError DisplayManagerService::SetScreenColorTransform(ScreenId screenId)
350 {
351     WLOGFI("SetScreenColorTransform::ScreenId: %{public}" PRIu64 "", screenId);
352     CHECK_SCREEN_AND_RETURN(DMError::DM_ERROR_INVALID_PARAM);
353     return abstractScreenController_->SetScreenColorTransform(screenId);
354 }
355 
OnStop()356 void DisplayManagerService::OnStop()
357 {
358     WLOGFI("ready to stop display service.");
359 }
360 
RegisterDisplayManagerAgent(const sptr<IDisplayManagerAgent> & displayManagerAgent,DisplayManagerAgentType type)361 bool DisplayManagerService::RegisterDisplayManagerAgent(const sptr<IDisplayManagerAgent>& displayManagerAgent,
362     DisplayManagerAgentType type)
363 {
364     if (type == DisplayManagerAgentType::SCREEN_EVENT_LISTENER && !Permission::IsSystemCalling()) {
365         WLOGFE("register display manager agent permission denied!");
366         return false;
367     }
368     if ((displayManagerAgent == nullptr) || (displayManagerAgent->AsObject() == nullptr)) {
369         WLOGFE("displayManagerAgent invalid");
370         return false;
371     }
372     return DisplayManagerAgentController::GetInstance().RegisterDisplayManagerAgent(displayManagerAgent, type);
373 }
374 
UnregisterDisplayManagerAgent(const sptr<IDisplayManagerAgent> & displayManagerAgent,DisplayManagerAgentType type)375 bool DisplayManagerService::UnregisterDisplayManagerAgent(const sptr<IDisplayManagerAgent>& displayManagerAgent,
376     DisplayManagerAgentType type)
377 {
378     if (type == DisplayManagerAgentType::SCREEN_EVENT_LISTENER && !Permission::IsSystemCalling()) {
379         WLOGFE("unregister display manager agent permission denied!");
380         return false;
381     }
382     if ((displayManagerAgent == nullptr) || (displayManagerAgent->AsObject() == nullptr)) {
383         WLOGFE("displayManagerAgent invalid");
384         return false;
385     }
386     return DisplayManagerAgentController::GetInstance().UnregisterDisplayManagerAgent(displayManagerAgent, type);
387 }
388 
WakeUpBegin(PowerStateChangeReason reason)389 bool DisplayManagerService::WakeUpBegin(PowerStateChangeReason reason)
390 {
391     HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "dms:WakeUpBegin(%u)", reason);
392     if (!Permission::IsSystemServiceCalling()) {
393         WLOGFE("wake up begin permission denied!");
394         return false;
395     }
396     return DisplayManagerAgentController::GetInstance().NotifyDisplayPowerEvent(DisplayPowerEvent::WAKE_UP,
397         EventStatus::BEGIN);
398 }
399 
WakeUpEnd()400 bool DisplayManagerService::WakeUpEnd()
401 {
402     if (!Permission::IsSystemServiceCalling()) {
403         WLOGFE("wake up end permission denied!");
404         return false;
405     }
406     return DisplayManagerAgentController::GetInstance().NotifyDisplayPowerEvent(DisplayPowerEvent::WAKE_UP,
407         EventStatus::END);
408 }
409 
SuspendBegin(PowerStateChangeReason reason)410 bool DisplayManagerService::SuspendBegin(PowerStateChangeReason reason)
411 {
412     HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "dms:SuspendBegin(%u)", reason);
413     if (!Permission::IsSystemServiceCalling()) {
414         WLOGFE("suspend begin permission denied!");
415         return false;
416     }
417     displayPowerController_->SuspendBegin(reason);
418     return DisplayManagerAgentController::GetInstance().NotifyDisplayPowerEvent(DisplayPowerEvent::SLEEP,
419         EventStatus::BEGIN);
420 }
421 
SuspendEnd()422 bool DisplayManagerService::SuspendEnd()
423 {
424     if (!Permission::IsSystemServiceCalling()) {
425         WLOGFE("suspend end permission denied!");
426         return false;
427     }
428     return DisplayManagerAgentController::GetInstance().NotifyDisplayPowerEvent(DisplayPowerEvent::SLEEP,
429         EventStatus::END);
430 }
431 
SetScreenPowerForAll(ScreenPowerState state,PowerStateChangeReason reason)432 bool DisplayManagerService::SetScreenPowerForAll(ScreenPowerState state, PowerStateChangeReason reason)
433 {
434     WLOGFI("SetScreenPowerForAll");
435     if (!Permission::IsSystemServiceCalling()) {
436         WLOGFE("set screen power for all permission denied!");
437         return false;
438     }
439     return abstractScreenController_->SetScreenPowerForAll(state, reason);
440 }
441 
GetScreenPower(ScreenId dmsScreenId)442 ScreenPowerState DisplayManagerService::GetScreenPower(ScreenId dmsScreenId)
443 {
444     return abstractScreenController_->GetScreenPower(dmsScreenId);
445 }
446 
SetDisplayState(DisplayState state)447 bool DisplayManagerService::SetDisplayState(DisplayState state)
448 {
449     if (!Permission::IsSystemServiceCalling()) {
450         WLOGFE("set display state permission denied!");
451         return false;
452     }
453     ScreenId dmsScreenId = abstractScreenController_->GetDefaultAbstractScreenId();
454     sptr<AbstractDisplay> display = abstractDisplayController_->GetAbstractDisplayByScreen(dmsScreenId);
455     if (display != nullptr) {
456         display->SetDisplayState(state);
457     }
458     return displayPowerController_->SetDisplayState(state);
459 }
460 
GetScreenIdByDisplayId(DisplayId displayId) const461 ScreenId DisplayManagerService::GetScreenIdByDisplayId(DisplayId displayId) const
462 {
463     sptr<AbstractDisplay> abstractDisplay = abstractDisplayController_->GetAbstractDisplay(displayId);
464     if (abstractDisplay == nullptr) {
465         WLOGFE("GetScreenIdByDisplayId: GetAbstractDisplay failed");
466         return SCREEN_ID_INVALID;
467     }
468     return abstractDisplay->GetAbstractScreenId();
469 }
470 
GetDisplayState(DisplayId displayId)471 DisplayState DisplayManagerService::GetDisplayState(DisplayId displayId)
472 {
473     std::lock_guard<std::recursive_mutex> lock(mutex_);
474     return displayPowerController_->GetDisplayState(displayId);
475 }
476 
NotifyDisplayEvent(DisplayEvent event)477 void DisplayManagerService::NotifyDisplayEvent(DisplayEvent event)
478 {
479     if (!Permission::IsSystemServiceCalling()) {
480         WLOGFE("notify display event permission denied!");
481         return;
482     }
483     displayPowerController_->NotifyDisplayEvent(event);
484 }
485 
SetFreeze(std::vector<DisplayId> displayIds,bool isFreeze)486 bool DisplayManagerService::SetFreeze(std::vector<DisplayId> displayIds, bool isFreeze)
487 {
488     if (!Permission::IsSystemCalling()) {
489         WLOGFE("set freeze permission denied!");
490         return false;
491     }
492     abstractDisplayController_->SetFreeze(displayIds, isFreeze);
493     return true;
494 }
495 
MakeMirror(ScreenId mainScreenId,std::vector<ScreenId> mirrorScreenIds)496 ScreenId DisplayManagerService::MakeMirror(ScreenId mainScreenId, std::vector<ScreenId> mirrorScreenIds)
497 {
498     if (!Permission::IsSystemCalling()) {
499         WLOGFE("make mirror permission denied!");
500         return SCREEN_ID_INVALID;
501     }
502     WLOGFI("MakeMirror. mainScreenId :%{public}" PRIu64"", mainScreenId);
503     auto allMirrorScreenIds = abstractScreenController_->GetAllValidScreenIds(mirrorScreenIds);
504     auto iter = std::find(allMirrorScreenIds.begin(), allMirrorScreenIds.end(), mainScreenId);
505     if (iter != allMirrorScreenIds.end()) {
506         allMirrorScreenIds.erase(iter);
507     }
508     auto mainScreen = abstractScreenController_->GetAbstractScreen(mainScreenId);
509     if (mainScreen == nullptr || allMirrorScreenIds.empty()) {
510         WLOGFI("create mirror fail. main screen :%{public}" PRIu64", screens' size:%{public}u",
511             mainScreenId, static_cast<uint32_t>(allMirrorScreenIds.size()));
512         return SCREEN_ID_INVALID;
513     }
514     HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "dms:MakeMirror");
515     if (!abstractScreenController_->MakeMirror(mainScreenId, allMirrorScreenIds)) {
516         WLOGFE("make mirror failed.");
517         return SCREEN_ID_INVALID;
518     }
519     if (abstractScreenController_->GetAbstractScreenGroup(mainScreen->groupDmsId_) == nullptr) {
520         WLOGFE("get screen group failed.");
521         return SCREEN_ID_INVALID;
522     }
523     return mainScreen->groupDmsId_;
524 }
525 
RemoveVirtualScreenFromGroup(std::vector<ScreenId> screens)526 void DisplayManagerService::RemoveVirtualScreenFromGroup(std::vector<ScreenId> screens)
527 {
528     abstractScreenController_->RemoveVirtualScreenFromGroup(screens);
529 }
530 
UpdateRSTree(DisplayId displayId,DisplayId parentDisplayId,std::shared_ptr<RSSurfaceNode> & surfaceNode,bool isAdd,bool isMultiDisplay)531 void DisplayManagerService::UpdateRSTree(DisplayId displayId, DisplayId parentDisplayId,
532     std::shared_ptr<RSSurfaceNode>& surfaceNode, bool isAdd, bool isMultiDisplay)
533 {
534     WLOGFI("UpdateRSTree, currentDisplayId: %{public}" PRIu64", isAdd: %{public}d, isMultiDisplay: %{public}d, "
535         "parentDisplayId: %{public}" PRIu64"", displayId, isAdd, isMultiDisplay, parentDisplayId);
536     ScreenId screenId = GetScreenIdByDisplayId(displayId);
537     ScreenId parentScreenId = GetScreenIdByDisplayId(parentDisplayId);
538     CHECK_SCREEN_AND_RETURN();
539 
540     abstractScreenController_->UpdateRSTree(screenId, parentScreenId, surfaceNode, isAdd, isMultiDisplay);
541 }
542 
GetScreenInfoById(ScreenId screenId)543 sptr<ScreenInfo> DisplayManagerService::GetScreenInfoById(ScreenId screenId)
544 {
545     auto screen = abstractScreenController_->GetAbstractScreen(screenId);
546     if (screen == nullptr) {
547         WLOGE("cannot find screenInfo: %{public}" PRIu64"", screenId);
548         return nullptr;
549     }
550     return screen->ConvertToScreenInfo();
551 }
552 
GetScreenGroupInfoById(ScreenId screenId)553 sptr<ScreenGroupInfo> DisplayManagerService::GetScreenGroupInfoById(ScreenId screenId)
554 {
555     auto screenGroup = abstractScreenController_->GetAbstractScreenGroup(screenId);
556     if (screenGroup == nullptr) {
557         WLOGE("cannot find screenGroupInfo: %{public}" PRIu64"", screenId);
558         return nullptr;
559     }
560     return screenGroup->ConvertToScreenGroupInfo();
561 }
562 
GetScreenGroupIdByScreenId(ScreenId screenId)563 ScreenId DisplayManagerService::GetScreenGroupIdByScreenId(ScreenId screenId)
564 {
565     auto screen = abstractScreenController_->GetAbstractScreen(screenId);
566     if (screen == nullptr) {
567         WLOGE("cannot find screenInfo: %{public}" PRIu64"", screenId);
568         return SCREEN_ID_INVALID;
569     }
570     return screen->GetScreenGroupId();
571 }
572 
GetAllDisplayIds()573 std::vector<DisplayId> DisplayManagerService::GetAllDisplayIds()
574 {
575     return abstractDisplayController_->GetAllDisplayIds();
576 }
577 
GetAllScreenInfos()578 std::vector<sptr<ScreenInfo>> DisplayManagerService::GetAllScreenInfos()
579 {
580     if (!Permission::IsSystemCalling()) {
581         WLOGFE("get all screen infos permission denied!");
582         return std::vector<sptr<ScreenInfo>>();
583     }
584     std::vector<ScreenId> screenIds = abstractScreenController_->GetAllScreenIds();
585     std::vector<sptr<ScreenInfo>> screenInfos;
586     for (auto screenId: screenIds) {
587         auto screenInfo = GetScreenInfoById(screenId);
588         if (screenInfo == nullptr) {
589             WLOGE("cannot find screenInfo: %{public}" PRIu64"", screenId);
590             continue;
591         }
592         screenInfos.emplace_back(screenInfo);
593     }
594     return screenInfos;
595 }
596 
MakeExpand(std::vector<ScreenId> expandScreenIds,std::vector<Point> startPoints)597 ScreenId DisplayManagerService::MakeExpand(std::vector<ScreenId> expandScreenIds, std::vector<Point> startPoints)
598 {
599     if (!Permission::IsSystemCalling()) {
600         WLOGFE("make expand permission denied!");
601         return SCREEN_ID_INVALID;
602     }
603     if (expandScreenIds.empty() || startPoints.empty() || expandScreenIds.size() != startPoints.size()) {
604         WLOGFI("create expand fail, input params is invalid. "
605             "screenId vector size :%{public}ud, startPoint vector size :%{public}ud",
606             static_cast<uint32_t>(expandScreenIds.size()), static_cast<uint32_t>(startPoints.size()));
607         return SCREEN_ID_INVALID;
608     }
609     std::map<ScreenId, Point> pointsMap;
610     uint32_t size = expandScreenIds.size();
611     for (uint32_t i = 0; i < size; i++) {
612         if (pointsMap.find(expandScreenIds[i]) != pointsMap.end()) {
613             continue;
614         }
615         pointsMap[expandScreenIds[i]] = startPoints[i];
616     }
617     ScreenId defaultScreenId = abstractScreenController_->GetDefaultAbstractScreenId();
618     WLOGFI("MakeExpand, defaultScreenId:%{public}" PRIu64"", defaultScreenId);
619     auto allExpandScreenIds = abstractScreenController_->GetAllValidScreenIds(expandScreenIds);
620     auto iter = std::find(allExpandScreenIds.begin(), allExpandScreenIds.end(), defaultScreenId);
621     if (iter != allExpandScreenIds.end()) {
622         allExpandScreenIds.erase(iter);
623     }
624     if (allExpandScreenIds.empty()) {
625         WLOGFE("allExpandScreenIds is empty. make expand failed.");
626         return SCREEN_ID_INVALID;
627     }
628     std::shared_ptr<RSDisplayNode> rsDisplayNode;
629     std::vector<Point> points;
630     for (uint32_t i = 0; i < allExpandScreenIds.size(); i++) {
631         rsDisplayNode = abstractScreenController_->GetRSDisplayNodeByScreenId(allExpandScreenIds[i]);
632         points.emplace_back(pointsMap[allExpandScreenIds[i]]);
633         if (rsDisplayNode != nullptr) {
634             rsDisplayNode->SetDisplayOffset(pointsMap[allExpandScreenIds[i]].posX_,
635                 pointsMap[allExpandScreenIds[i]].posY_);
636         }
637     }
638     HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "dms:MakeExpand");
639     if (!abstractScreenController_->MakeExpand(allExpandScreenIds, points)) {
640         WLOGFE("make expand failed.");
641         return SCREEN_ID_INVALID;
642     }
643     auto screen = abstractScreenController_->GetAbstractScreen(allExpandScreenIds[0]);
644     if (screen == nullptr || abstractScreenController_->GetAbstractScreenGroup(screen->groupDmsId_) == nullptr) {
645         WLOGFE("get screen group failed.");
646         return SCREEN_ID_INVALID;
647     }
648     return screen->groupDmsId_;
649 }
650 
SetScreenActiveMode(ScreenId screenId,uint32_t modeId)651 bool DisplayManagerService::SetScreenActiveMode(ScreenId screenId, uint32_t modeId)
652 {
653     if (!Permission::IsSystemCalling()) {
654         WLOGFE("set screen active permission denied!");
655         return false;
656     }
657     HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "dms:SetScreenActiveMode(%" PRIu64", %u)", screenId, modeId);
658     return abstractScreenController_->SetScreenActiveMode(screenId, modeId);
659 }
660 
SetVirtualPixelRatio(ScreenId screenId,float virtualPixelRatio)661 bool DisplayManagerService::SetVirtualPixelRatio(ScreenId screenId, float virtualPixelRatio)
662 {
663     if (!Permission::IsSystemCalling()) {
664         WLOGFE("set virtual pixel permission denied!");
665         return false;
666     }
667     HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "dms:SetVirtualPixelRatio(%" PRIu64", %f)", screenId,
668         virtualPixelRatio);
669     return abstractScreenController_->SetVirtualPixelRatio(screenId, virtualPixelRatio);
670 }
671 
IsScreenRotationLocked()672 bool DisplayManagerService::IsScreenRotationLocked()
673 {
674     if (!Permission::IsSystemCalling()) {
675         WLOGFE("is screen rotation locked permission denied!");
676         return false;
677     }
678     return ScreenRotationController::IsScreenRotationLocked();
679 }
680 
SetScreenRotationLocked(bool isLocked)681 void DisplayManagerService::SetScreenRotationLocked(bool isLocked)
682 {
683     if (!Permission::IsSystemCalling()) {
684         WLOGFE("set screen rotation locked permission denied!");
685         return;
686     }
687     ScreenRotationController::SetScreenRotationLocked(isLocked);
688 }
689 
SetGravitySensorSubscriptionEnabled()690 void DisplayManagerService::SetGravitySensorSubscriptionEnabled()
691 {
692     if (!isAutoRotationOpen_) {
693         WLOGFE("autoRotation is not open");
694         ScreenRotationController::Init();
695         return;
696     }
697     SensorConnector::SubscribeRotationSensor();
698 }
699 
GetCutoutInfo(DisplayId displayId)700 sptr<CutoutInfo> DisplayManagerService::GetCutoutInfo(DisplayId displayId)
701 {
702     return displayCutoutController_->GetCutoutInfo(displayId);
703 }
704 
RegisterRSScreenChangeListener(const sptr<IRSScreenChangeListener> & listener)705 void DisplayManagerService::RegisterRSScreenChangeListener(const sptr<IRSScreenChangeListener>& listener)
706 {
707     abstractScreenController_->RegisterRSScreenChangeListener(listener);
708 }
709 } // namespace OHOS::Rosen