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