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