• 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 
SetSpecifiedScreenPower(ScreenId screenId,ScreenPowerState state,PowerStateChangeReason reason)437 bool DisplayManagerService::SetSpecifiedScreenPower(ScreenId screenId, ScreenPowerState state, PowerStateChangeReason reason)
438 {
439     WLOGFE("DMS not support SetSpecifiedScreenPower: screen:%{public}" PRIu64 ", state:%{public}u",
440         screenId, state);
441     return false;
442 }
443 
SetScreenPowerForAll(ScreenPowerState state,PowerStateChangeReason reason)444 bool DisplayManagerService::SetScreenPowerForAll(ScreenPowerState state, PowerStateChangeReason reason)
445 {
446     WLOGFI("SetScreenPowerForAll");
447     if (!Permission::IsSystemServiceCalling()) {
448         WLOGFE("set screen power for all permission denied!");
449         return false;
450     }
451     return abstractScreenController_->SetScreenPowerForAll(state, reason);
452 }
453 
GetScreenPower(ScreenId dmsScreenId)454 ScreenPowerState DisplayManagerService::GetScreenPower(ScreenId dmsScreenId)
455 {
456     return abstractScreenController_->GetScreenPower(dmsScreenId);
457 }
458 
SetDisplayState(DisplayState state)459 bool DisplayManagerService::SetDisplayState(DisplayState state)
460 {
461     if (!Permission::IsSystemServiceCalling()) {
462         WLOGFE("set display state permission denied!");
463         return false;
464     }
465     ScreenId dmsScreenId = abstractScreenController_->GetDefaultAbstractScreenId();
466     sptr<AbstractDisplay> display = abstractDisplayController_->GetAbstractDisplayByScreen(dmsScreenId);
467     if (display != nullptr) {
468         display->SetDisplayState(state);
469     }
470     return displayPowerController_->SetDisplayState(state);
471 }
472 
GetScreenIdByDisplayId(DisplayId displayId) const473 ScreenId DisplayManagerService::GetScreenIdByDisplayId(DisplayId displayId) const
474 {
475     sptr<AbstractDisplay> abstractDisplay = abstractDisplayController_->GetAbstractDisplay(displayId);
476     if (abstractDisplay == nullptr) {
477         WLOGFE("GetScreenIdByDisplayId: GetAbstractDisplay failed");
478         return SCREEN_ID_INVALID;
479     }
480     return abstractDisplay->GetAbstractScreenId();
481 }
482 
GetDisplayState(DisplayId displayId)483 DisplayState DisplayManagerService::GetDisplayState(DisplayId displayId)
484 {
485     std::lock_guard<std::recursive_mutex> lock(mutex_);
486     return displayPowerController_->GetDisplayState(displayId);
487 }
488 
NotifyDisplayEvent(DisplayEvent event)489 void DisplayManagerService::NotifyDisplayEvent(DisplayEvent event)
490 {
491     if (!Permission::IsSystemServiceCalling()) {
492         WLOGFE("notify display event permission denied!");
493         return;
494     }
495     displayPowerController_->NotifyDisplayEvent(event);
496 }
497 
SetFreeze(std::vector<DisplayId> displayIds,bool isFreeze)498 bool DisplayManagerService::SetFreeze(std::vector<DisplayId> displayIds, bool isFreeze)
499 {
500     if (!Permission::IsSystemCalling() && !Permission::IsStartByHdcd()) {
501         WLOGFE("set freeze permission denied!");
502         return false;
503     }
504     abstractDisplayController_->SetFreeze(displayIds, isFreeze);
505     return true;
506 }
507 
MakeMirror(ScreenId mainScreenId,std::vector<ScreenId> mirrorScreenIds,ScreenId & screenGroupId)508 DMError DisplayManagerService::MakeMirror(ScreenId mainScreenId, std::vector<ScreenId> mirrorScreenIds,
509                                           ScreenId& screenGroupId)
510 {
511     if (!Permission::IsSystemCalling() && !Permission::IsStartByHdcd()) {
512         WLOGFE("make mirror permission denied!");
513         return DMError::DM_ERROR_NOT_SYSTEM_APP;
514     }
515     WLOGFI("MakeMirror. mainScreenId :%{public}" PRIu64"", mainScreenId);
516     auto allMirrorScreenIds = abstractScreenController_->GetAllValidScreenIds(mirrorScreenIds);
517     auto iter = std::find(allMirrorScreenIds.begin(), allMirrorScreenIds.end(), mainScreenId);
518     if (iter != allMirrorScreenIds.end()) {
519         allMirrorScreenIds.erase(iter);
520     }
521     auto mainScreen = abstractScreenController_->GetAbstractScreen(mainScreenId);
522     if (mainScreen == nullptr || allMirrorScreenIds.empty()) {
523         WLOGFI("create mirror fail. main screen :%{public}" PRIu64", screens' size:%{public}u",
524             mainScreenId, static_cast<uint32_t>(allMirrorScreenIds.size()));
525         return DMError::DM_ERROR_INVALID_PARAM;
526     }
527     HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "dms:MakeMirror");
528     DMError ret = abstractScreenController_->MakeMirror(mainScreenId, allMirrorScreenIds);
529     if (ret != DMError::DM_OK) {
530         WLOGFE("make mirror failed.");
531         return ret;
532     }
533     if (abstractScreenController_->GetAbstractScreenGroup(mainScreen->groupDmsId_) == nullptr) {
534         WLOGFE("get screen group failed.");
535         return DMError::DM_ERROR_NULLPTR;
536     }
537     screenGroupId = mainScreen->groupDmsId_;
538     return DMError::DM_OK;
539 }
540 
StopMirror(const std::vector<ScreenId> & mirrorScreenIds)541 DMError DisplayManagerService::StopMirror(const std::vector<ScreenId>& mirrorScreenIds)
542 {
543     if (!Permission::IsSystemCalling() && !Permission::IsStartByHdcd()) {
544         WLOGFE("stop mirror permission denied!");
545         return DMError::DM_ERROR_NOT_SYSTEM_APP;
546     }
547 
548     auto allMirrorScreenIds = abstractScreenController_->GetAllValidScreenIds(mirrorScreenIds);
549     if (allMirrorScreenIds.empty()) {
550         WLOGFI("stop mirror done. screens' size:%{public}u", static_cast<uint32_t>(allMirrorScreenIds.size()));
551         return DMError::DM_OK;
552     }
553 
554     DMError ret = abstractScreenController_->StopScreens(allMirrorScreenIds, ScreenCombination::SCREEN_MIRROR);
555     if (ret != DMError::DM_OK) {
556         WLOGFE("stop mirror failed.");
557         return ret;
558     }
559 
560     return DMError::DM_OK;
561 }
562 
RemoveVirtualScreenFromGroup(std::vector<ScreenId> screens)563 void DisplayManagerService::RemoveVirtualScreenFromGroup(std::vector<ScreenId> screens)
564 {
565     abstractScreenController_->RemoveVirtualScreenFromGroup(screens);
566 }
567 
UpdateRSTree(DisplayId displayId,DisplayId parentDisplayId,std::shared_ptr<RSSurfaceNode> & surfaceNode,bool isAdd,bool isMultiDisplay)568 void DisplayManagerService::UpdateRSTree(DisplayId displayId, DisplayId parentDisplayId,
569     std::shared_ptr<RSSurfaceNode>& surfaceNode, bool isAdd, bool isMultiDisplay)
570 {
571     WLOGFD("UpdateRSTree, currentDisplayId: %{public}" PRIu64", isAdd: %{public}d, isMultiDisplay: %{public}d, "
572         "parentDisplayId: %{public}" PRIu64"", displayId, isAdd, isMultiDisplay, parentDisplayId);
573     ScreenId screenId = GetScreenIdByDisplayId(displayId);
574     ScreenId parentScreenId = GetScreenIdByDisplayId(parentDisplayId);
575     CHECK_SCREEN_AND_RETURN(screenId, void());
576 
577     abstractScreenController_->UpdateRSTree(screenId, parentScreenId, surfaceNode, isAdd, isMultiDisplay);
578 }
579 
AddSurfaceNodeToDisplay(DisplayId displayId,std::shared_ptr<RSSurfaceNode> & surfaceNode,bool onTop)580 DMError DisplayManagerService::AddSurfaceNodeToDisplay(DisplayId displayId,
581     std::shared_ptr<RSSurfaceNode>& surfaceNode, bool onTop)
582 {
583     WLOGFI("DisplayId: %{public}" PRIu64", onTop: %{public}d", displayId, onTop);
584     if (surfaceNode == nullptr) {
585         WLOGFW("Surface is null");
586         return DMError::DM_ERROR_NULLPTR;
587     }
588     ScreenId screenId = GetScreenIdByDisplayId(displayId);
589     return abstractScreenController_->AddSurfaceNodeToScreen(screenId, surfaceNode, true);
590 }
591 
RemoveSurfaceNodeFromDisplay(DisplayId displayId,std::shared_ptr<RSSurfaceNode> & surfaceNode)592 DMError DisplayManagerService::RemoveSurfaceNodeFromDisplay(DisplayId displayId,
593     std::shared_ptr<RSSurfaceNode>& surfaceNode)
594 {
595     WLOGFI("DisplayId: %{public}" PRIu64"", displayId);
596     if (surfaceNode == nullptr) {
597         WLOGFW("Surface is null");
598         return DMError::DM_ERROR_NULLPTR;
599     }
600     ScreenId screenId = GetScreenIdByDisplayId(displayId);
601     return abstractScreenController_->RemoveSurfaceNodeFromScreen(screenId, surfaceNode);
602 }
603 
GetScreenInfoById(ScreenId screenId)604 sptr<ScreenInfo> DisplayManagerService::GetScreenInfoById(ScreenId screenId)
605 {
606     auto screen = abstractScreenController_->GetAbstractScreen(screenId);
607     if (screen == nullptr) {
608         WLOGE("cannot find screenInfo: %{public}" PRIu64"", screenId);
609         return nullptr;
610     }
611     return screen->ConvertToScreenInfo();
612 }
613 
GetScreenGroupInfoById(ScreenId screenId)614 sptr<ScreenGroupInfo> DisplayManagerService::GetScreenGroupInfoById(ScreenId screenId)
615 {
616     auto screenGroup = abstractScreenController_->GetAbstractScreenGroup(screenId);
617     if (screenGroup == nullptr) {
618         WLOGE("cannot find screenGroupInfo: %{public}" PRIu64"", screenId);
619         return nullptr;
620     }
621     return screenGroup->ConvertToScreenGroupInfo();
622 }
623 
GetScreenGroupIdByScreenId(ScreenId screenId)624 ScreenId DisplayManagerService::GetScreenGroupIdByScreenId(ScreenId screenId)
625 {
626     auto screen = abstractScreenController_->GetAbstractScreen(screenId);
627     if (screen == nullptr) {
628         WLOGE("cannot find screenInfo: %{public}" PRIu64"", screenId);
629         return SCREEN_ID_INVALID;
630     }
631     return screen->GetScreenGroupId();
632 }
633 
GetAllDisplayIds()634 std::vector<DisplayId> DisplayManagerService::GetAllDisplayIds()
635 {
636     return abstractDisplayController_->GetAllDisplayIds();
637 }
638 
GetAllScreenInfos(std::vector<sptr<ScreenInfo>> & screenInfos)639 DMError DisplayManagerService::GetAllScreenInfos(std::vector<sptr<ScreenInfo>>& screenInfos)
640 {
641     if (!Permission::IsSystemCalling() && !Permission::IsStartByHdcd()) {
642         WLOGFE("get all screen infos permission denied!");
643         return DMError::DM_ERROR_NOT_SYSTEM_APP;
644     }
645     std::vector<ScreenId> screenIds = abstractScreenController_->GetAllScreenIds();
646     for (auto screenId: screenIds) {
647         auto screenInfo = GetScreenInfoById(screenId);
648         if (screenInfo == nullptr) {
649             WLOGE("cannot find screenInfo: %{public}" PRIu64"", screenId);
650             continue;
651         }
652         screenInfos.emplace_back(screenInfo);
653     }
654     return DMError::DM_OK;
655 }
656 
MakeExpand(std::vector<ScreenId> expandScreenIds,std::vector<Point> startPoints,ScreenId & screenGroupId)657 DMError DisplayManagerService::MakeExpand(std::vector<ScreenId> expandScreenIds, std::vector<Point> startPoints,
658                                           ScreenId& screenGroupId)
659 {
660     if (!Permission::IsSystemCalling() && !Permission::IsStartByHdcd()) {
661         WLOGFE("make expand permission denied!");
662         return DMError::DM_ERROR_NOT_SYSTEM_APP;
663     }
664     if (expandScreenIds.empty() || startPoints.empty() || expandScreenIds.size() != startPoints.size()) {
665         WLOGFE("create expand fail, input params is invalid. "
666             "screenId vector size :%{public}ud, startPoint vector size :%{public}ud",
667             static_cast<uint32_t>(expandScreenIds.size()), static_cast<uint32_t>(startPoints.size()));
668         return DMError::DM_ERROR_INVALID_PARAM;
669     }
670     std::map<ScreenId, Point> pointsMap;
671     uint32_t size = expandScreenIds.size();
672     for (uint32_t i = 0; i < size; i++) {
673         if (pointsMap.find(expandScreenIds[i]) != pointsMap.end()) {
674             continue;
675         }
676         pointsMap[expandScreenIds[i]] = startPoints[i];
677     }
678     ScreenId defaultScreenId = abstractScreenController_->GetDefaultAbstractScreenId();
679     WLOGFI("MakeExpand, defaultScreenId:%{public}" PRIu64"", defaultScreenId);
680     auto allExpandScreenIds = abstractScreenController_->GetAllValidScreenIds(expandScreenIds);
681     auto iter = std::find(allExpandScreenIds.begin(), allExpandScreenIds.end(), defaultScreenId);
682     if (iter != allExpandScreenIds.end()) {
683         allExpandScreenIds.erase(iter);
684     }
685     if (allExpandScreenIds.empty()) {
686         WLOGFE("allExpandScreenIds is empty. make expand failed.");
687         return DMError::DM_ERROR_NULLPTR;
688     }
689     std::shared_ptr<RSDisplayNode> rsDisplayNode;
690     std::vector<Point> points;
691     for (uint32_t i = 0; i < allExpandScreenIds.size(); i++) {
692         rsDisplayNode = abstractScreenController_->GetRSDisplayNodeByScreenId(allExpandScreenIds[i]);
693         points.emplace_back(pointsMap[allExpandScreenIds[i]]);
694         if (rsDisplayNode != nullptr) {
695             rsDisplayNode->SetDisplayOffset(pointsMap[allExpandScreenIds[i]].posX_,
696                 pointsMap[allExpandScreenIds[i]].posY_);
697         }
698     }
699     HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "dms:MakeExpand");
700     if (!abstractScreenController_->MakeExpand(allExpandScreenIds, points)) {
701         WLOGFE("make expand failed.");
702         return DMError::DM_ERROR_NULLPTR;
703     }
704     auto screen = abstractScreenController_->GetAbstractScreen(allExpandScreenIds[0]);
705     if (screen == nullptr || abstractScreenController_->GetAbstractScreenGroup(screen->groupDmsId_) == nullptr) {
706         WLOGFE("get screen group failed.");
707         return DMError::DM_ERROR_NULLPTR;
708     }
709     screenGroupId = screen->groupDmsId_;
710     return DMError::DM_OK;
711 }
712 
StopExpand(const std::vector<ScreenId> & expandScreenIds)713 DMError DisplayManagerService::StopExpand(const std::vector<ScreenId>& expandScreenIds)
714 {
715     if (!Permission::IsSystemCalling() && !Permission::IsStartByHdcd()) {
716         WLOGFE("stop expand permission denied!");
717         return DMError::DM_ERROR_NOT_SYSTEM_APP;
718     }
719     auto allExpandScreenIds = abstractScreenController_->GetAllValidScreenIds(expandScreenIds);
720     if (allExpandScreenIds.empty()) {
721         WLOGFI("stop expand done. screens' size:%{public}u", static_cast<uint32_t>(allExpandScreenIds.size()));
722         return DMError::DM_OK;
723     }
724 
725     DMError ret = abstractScreenController_->StopScreens(allExpandScreenIds, ScreenCombination::SCREEN_EXPAND);
726     if (ret != DMError::DM_OK) {
727         WLOGFE("stop expand failed.");
728         return ret;
729     }
730 
731     return DMError::DM_OK;
732 }
733 
SetScreenActiveMode(ScreenId screenId,uint32_t modeId)734 DMError DisplayManagerService::SetScreenActiveMode(ScreenId screenId, uint32_t modeId)
735 {
736     if (!Permission::IsSystemCalling() && !Permission::IsStartByHdcd()) {
737         WLOGFE("set screen active permission denied!");
738         return DMError::DM_ERROR_NOT_SYSTEM_APP;
739     }
740     HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "dms:SetScreenActiveMode(%" PRIu64", %u)", screenId, modeId);
741     return abstractScreenController_->SetScreenActiveMode(screenId, modeId);
742 }
743 
SetVirtualPixelRatio(ScreenId screenId,float virtualPixelRatio)744 DMError DisplayManagerService::SetVirtualPixelRatio(ScreenId screenId, float virtualPixelRatio)
745 {
746     if (!Permission::IsSystemCalling() && !Permission::IsStartByHdcd()) {
747         WLOGFE("set virtual pixel permission denied!");
748         return DMError::DM_ERROR_NOT_SYSTEM_APP;
749     }
750     HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "dms:SetVirtualPixelRatio(%" PRIu64", %f)", screenId,
751         virtualPixelRatio);
752     return abstractScreenController_->SetVirtualPixelRatio(screenId, virtualPixelRatio);
753 }
754 
IsScreenRotationLocked(bool & isLocked)755 DMError DisplayManagerService::IsScreenRotationLocked(bool& isLocked)
756 {
757     if (!Permission::IsSystemCalling() && !Permission::IsStartByHdcd()) {
758         WLOGFE("is screen rotation locked permission denied!");
759         return DMError::DM_ERROR_NOT_SYSTEM_APP;
760     }
761     isLocked = ScreenRotationController::IsScreenRotationLocked();
762     return DMError::DM_OK;
763 }
764 
SetScreenRotationLocked(bool isLocked)765 DMError DisplayManagerService::SetScreenRotationLocked(bool isLocked)
766 {
767     if (!Permission::IsSystemCalling() && !Permission::IsStartByHdcd()) {
768         WLOGFE("set screen rotation locked permission denied!");
769         return DMError::DM_ERROR_NOT_SYSTEM_APP;
770     }
771     return ScreenRotationController::SetScreenRotationLocked(isLocked);
772 }
773 
SetGravitySensorSubscriptionEnabled()774 void DisplayManagerService::SetGravitySensorSubscriptionEnabled()
775 {
776     if (!isAutoRotationOpen_) {
777         WLOGFE("autoRotation is not open");
778         ScreenRotationController::Init();
779         return;
780     }
781     SensorConnector::SubscribeRotationSensor();
782 }
783 
GetCutoutInfo(DisplayId displayId)784 sptr<CutoutInfo> DisplayManagerService::GetCutoutInfo(DisplayId displayId)
785 {
786     return displayCutoutController_->GetCutoutInfo(displayId);
787 }
788 
NotifyPrivateWindowStateChanged(bool hasPrivate)789 void DisplayManagerService::NotifyPrivateWindowStateChanged(bool hasPrivate)
790 {
791     DisplayManagerAgentController::GetInstance().NotifyPrivateWindowStateChanged(hasPrivate);
792 }
793 } // namespace OHOS::Rosen