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, "[UL_POWER]dms:WakeUpBegin(%u)", reason);
395     if (!Permission::IsSystemServiceCalling()) {
396         WLOGFE("[UL_POWER]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, "[UL_POWER]dms:WakeUpEnd");
406     if (!Permission::IsSystemServiceCalling()) {
407         WLOGFE("[UL_POWER]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, "[UL_POWER]dms:SuspendBegin(%u)", reason);
417     if (!Permission::IsSystemServiceCalling()) {
418         WLOGFE("[UL_POWER]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, "[UL_POWER]dms:SuspendEnd");
429     if (!Permission::IsSystemServiceCalling()) {
430         WLOGFE("[UL_POWER]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("[UL_POWER]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("[UL_POWER]SetScreenPowerForAll");
447     if (!Permission::IsSystemServiceCalling()) {
448         WLOGFE("[UL_POWER]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("[UL_POWER]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 
TryToCancelScreenOff()489 bool DisplayManagerService::TryToCancelScreenOff()
490 {
491     WLOGFE("[UL_POWER]DMS not support TryToCancelScreenOff");
492     return false;
493 }
494 
SetScreenBrightness(uint64_t screenId, uint32_t level)495 bool DisplayManagerService::SetScreenBrightness(uint64_t screenId, uint32_t level)
496 {
497     RSInterfaces::GetInstance().SetScreenBacklight(screenId, level);
498     return true;
499 }
500 
GetScreenBrightness(uint64_t screenId)501 uint32_t DisplayManagerService::GetScreenBrightness(uint64_t screenId)
502 {
503     uint32_t level = static_cast<uint32_t>(RSInterfaces::GetInstance().GetScreenBacklight(screenId));
504     TLOGI(WmsLogTag::DMS, "GetScreenBrightness screenId:%{public}" PRIu64", level:%{public}u,", screenId, level);
505     return level;
506 }
507 
NotifyDisplayEvent(DisplayEvent event)508 void DisplayManagerService::NotifyDisplayEvent(DisplayEvent event)
509 {
510     if (!Permission::IsSystemServiceCalling()) {
511         WLOGFE("[UL_POWER]notify display event permission denied!");
512         return;
513     }
514     displayPowerController_->NotifyDisplayEvent(event);
515 }
516 
SetFreeze(std::vector<DisplayId> displayIds, bool isFreeze)517 bool DisplayManagerService::SetFreeze(std::vector<DisplayId> displayIds, bool isFreeze)
518 {
519     if (!Permission::IsSystemCalling() && !Permission::IsStartByHdcd()) {
520         WLOGFE("set freeze permission denied!");
521         return false;
522     }
523     abstractDisplayController_->SetFreeze(displayIds, isFreeze);
524     return true;
525 }
526 
MakeMirror(ScreenId mainScreenId, std::vector<ScreenId> mirrorScreenIds, ScreenId& screenGroupId)527 DMError DisplayManagerService::MakeMirror(ScreenId mainScreenId, std::vector<ScreenId> mirrorScreenIds,
528                                           ScreenId& screenGroupId)
529 {
530     if (!Permission::IsSystemCalling() && !Permission::IsStartByHdcd()) {
531         WLOGFE("make mirror permission denied!");
532         return DMError::DM_ERROR_NOT_SYSTEM_APP;
533     }
534     WLOGFI("MakeMirror. mainScreenId :%{public}" PRIu64"", mainScreenId);
535     auto allMirrorScreenIds = abstractScreenController_->GetAllValidScreenIds(mirrorScreenIds);
536     auto iter = std::find(allMirrorScreenIds.begin(), allMirrorScreenIds.end(), mainScreenId);
537     if (iter != allMirrorScreenIds.end()) {
538         allMirrorScreenIds.erase(iter);
539     }
540     auto mainScreen = abstractScreenController_->GetAbstractScreen(mainScreenId);
541     if (mainScreen == nullptr || allMirrorScreenIds.empty()) {
542         WLOGFI("create mirror fail. main screen :%{public}" PRIu64", screens' size:%{public}u",
543             mainScreenId, static_cast<uint32_t>(allMirrorScreenIds.size()));
544         return DMError::DM_ERROR_INVALID_PARAM;
545     }
546     HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "dms:MakeMirror");
547     DMError ret = abstractScreenController_->MakeMirror(mainScreenId, allMirrorScreenIds);
548     if (ret != DMError::DM_OK) {
549         WLOGFE("make mirror failed.");
550         return ret;
551     }
552     if (abstractScreenController_->GetAbstractScreenGroup(mainScreen->groupDmsId_) == nullptr) {
553         WLOGFE("get screen group failed.");
554         return DMError::DM_ERROR_NULLPTR;
555     }
556     screenGroupId = mainScreen->groupDmsId_;
557     return DMError::DM_OK;
558 }
559 
StopMirror(const std::vector<ScreenId>& mirrorScreenIds)560 DMError DisplayManagerService::StopMirror(const std::vector<ScreenId>& mirrorScreenIds)
561 {
562     if (!Permission::IsSystemCalling() && !Permission::IsStartByHdcd()) {
563         WLOGFE("stop mirror permission denied!");
564         return DMError::DM_ERROR_NOT_SYSTEM_APP;
565     }
566 
567     auto allMirrorScreenIds = abstractScreenController_->GetAllValidScreenIds(mirrorScreenIds);
568     if (allMirrorScreenIds.empty()) {
569         WLOGFI("stop mirror done. screens' size:%{public}u", static_cast<uint32_t>(allMirrorScreenIds.size()));
570         return DMError::DM_OK;
571     }
572 
573     DMError ret = abstractScreenController_->StopScreens(allMirrorScreenIds, ScreenCombination::SCREEN_MIRROR);
574     if (ret != DMError::DM_OK) {
575         WLOGFE("stop mirror failed.");
576         return ret;
577     }
578 
579     return DMError::DM_OK;
580 }
581 
RemoveVirtualScreenFromGroup(std::vector<ScreenId> screens)582 void DisplayManagerService::RemoveVirtualScreenFromGroup(std::vector<ScreenId> screens)
583 {
584     abstractScreenController_->RemoveVirtualScreenFromGroup(screens);
585 }
586 
UpdateRSTree(DisplayId displayId, DisplayId parentDisplayId, std::shared_ptr<RSSurfaceNode>& surfaceNode, bool isAdd, bool isMultiDisplay)587 void DisplayManagerService::UpdateRSTree(DisplayId displayId, DisplayId parentDisplayId,
588     std::shared_ptr<RSSurfaceNode>& surfaceNode, bool isAdd, bool isMultiDisplay)
589 {
590     WLOGFD("UpdateRSTree, currentDisplayId: %{public}" PRIu64", isAdd: %{public}d, isMultiDisplay: %{public}d, "
591         "parentDisplayId: %{public}" PRIu64"", displayId, isAdd, isMultiDisplay, parentDisplayId);
592     ScreenId screenId = GetScreenIdByDisplayId(displayId);
593     ScreenId parentScreenId = GetScreenIdByDisplayId(parentDisplayId);
594     CHECK_SCREEN_AND_RETURN(screenId, void());
595 
596     abstractScreenController_->UpdateRSTree(screenId, parentScreenId, surfaceNode, isAdd, isMultiDisplay);
597 }
598 
AddSurfaceNodeToDisplay(DisplayId displayId, std::shared_ptr<RSSurfaceNode>& surfaceNode, bool onTop)599 DMError DisplayManagerService::AddSurfaceNodeToDisplay(DisplayId displayId,
600     std::shared_ptr<RSSurfaceNode>& surfaceNode, bool onTop)
601 {
602     WLOGFI("DisplayId: %{public}" PRIu64", onTop: %{public}d", displayId, onTop);
603     if (surfaceNode == nullptr) {
604         WLOGFW("Surface is null");
605         return DMError::DM_ERROR_NULLPTR;
606     }
607     ScreenId screenId = GetScreenIdByDisplayId(displayId);
608     return abstractScreenController_->AddSurfaceNodeToScreen(screenId, surfaceNode, true);
609 }
610 
RemoveSurfaceNodeFromDisplay(DisplayId displayId, std::shared_ptr<RSSurfaceNode>& surfaceNode)611 DMError DisplayManagerService::RemoveSurfaceNodeFromDisplay(DisplayId displayId,
612     std::shared_ptr<RSSurfaceNode>& surfaceNode)
613 {
614     WLOGFI("DisplayId: %{public}" PRIu64"", displayId);
615     if (surfaceNode == nullptr) {
616         WLOGFW("Surface is null");
617         return DMError::DM_ERROR_NULLPTR;
618     }
619     ScreenId screenId = GetScreenIdByDisplayId(displayId);
620     return abstractScreenController_->RemoveSurfaceNodeFromScreen(screenId, surfaceNode);
621 }
622 
GetScreenInfoById(ScreenId screenId)623 sptr<ScreenInfo> DisplayManagerService::GetScreenInfoById(ScreenId screenId)
624 {
625     auto screen = abstractScreenController_->GetAbstractScreen(screenId);
626     if (screen == nullptr) {
627         WLOGE("cannot find screenInfo: %{public}" PRIu64"", screenId);
628         return nullptr;
629     }
630     return screen->ConvertToScreenInfo();
631 }
632 
GetScreenGroupInfoById(ScreenId screenId)633 sptr<ScreenGroupInfo> DisplayManagerService::GetScreenGroupInfoById(ScreenId screenId)
634 {
635     auto screenGroup = abstractScreenController_->GetAbstractScreenGroup(screenId);
636     if (screenGroup == nullptr) {
637         WLOGE("cannot find screenGroupInfo: %{public}" PRIu64"", screenId);
638         return nullptr;
639     }
640     return screenGroup->ConvertToScreenGroupInfo();
641 }
642 
GetScreenGroupIdByScreenId(ScreenId screenId)643 ScreenId DisplayManagerService::GetScreenGroupIdByScreenId(ScreenId screenId)
644 {
645     auto screen = abstractScreenController_->GetAbstractScreen(screenId);
646     if (screen == nullptr) {
647         WLOGE("cannot find screenInfo: %{public}" PRIu64"", screenId);
648         return SCREEN_ID_INVALID;
649     }
650     return screen->GetScreenGroupId();
651 }
652 
GetAllDisplayIds()653 std::vector<DisplayId> DisplayManagerService::GetAllDisplayIds()
654 {
655     return abstractDisplayController_->GetAllDisplayIds();
656 }
657 
GetAllScreenInfos(std::vector<sptr<ScreenInfo>>& screenInfos)658 DMError DisplayManagerService::GetAllScreenInfos(std::vector<sptr<ScreenInfo>>& screenInfos)
659 {
660     if (!Permission::IsSystemCalling() && !Permission::IsStartByHdcd()) {
661         WLOGFE("get all screen infos permission denied!");
662         return DMError::DM_ERROR_NOT_SYSTEM_APP;
663     }
664     std::vector<ScreenId> screenIds = abstractScreenController_->GetAllScreenIds();
665     for (auto screenId: screenIds) {
666         auto screenInfo = GetScreenInfoById(screenId);
667         if (screenInfo == nullptr) {
668             WLOGE("cannot find screenInfo: %{public}" PRIu64"", screenId);
669             continue;
670         }
671         screenInfos.emplace_back(screenInfo);
672     }
673     return DMError::DM_OK;
674 }
675 
MakeExpand(std::vector<ScreenId> expandScreenIds, std::vector<Point> startPoints, ScreenId& screenGroupId)676 DMError DisplayManagerService::MakeExpand(std::vector<ScreenId> expandScreenIds, std::vector<Point> startPoints,
677                                           ScreenId& screenGroupId)
678 {
679     if (!Permission::IsSystemCalling() && !Permission::IsStartByHdcd()) {
680         WLOGFE("make expand permission denied!");
681         return DMError::DM_ERROR_NOT_SYSTEM_APP;
682     }
683     if (expandScreenIds.empty() || startPoints.empty() || expandScreenIds.size() != startPoints.size()) {
684         WLOGFE("create expand fail, input params is invalid. "
685             "screenId vector size :%{public}ud, startPoint vector size :%{public}ud",
686             static_cast<uint32_t>(expandScreenIds.size()), static_cast<uint32_t>(startPoints.size()));
687         return DMError::DM_ERROR_INVALID_PARAM;
688     }
689     std::map<ScreenId, Point> pointsMap;
690     uint32_t size = expandScreenIds.size();
691     for (uint32_t i = 0; i < size; i++) {
692         if (pointsMap.find(expandScreenIds[i]) != pointsMap.end()) {
693             continue;
694         }
695         pointsMap[expandScreenIds[i]] = startPoints[i];
696     }
697     ScreenId defaultScreenId = abstractScreenController_->GetDefaultAbstractScreenId();
698     WLOGFI("MakeExpand, defaultScreenId:%{public}" PRIu64"", defaultScreenId);
699     auto allExpandScreenIds = abstractScreenController_->GetAllValidScreenIds(expandScreenIds);
700     auto iter = std::find(allExpandScreenIds.begin(), allExpandScreenIds.end(), defaultScreenId);
701     if (iter != allExpandScreenIds.end()) {
702         allExpandScreenIds.erase(iter);
703     }
704     if (allExpandScreenIds.empty()) {
705         WLOGFE("allExpandScreenIds is empty. make expand failed.");
706         return DMError::DM_ERROR_NULLPTR;
707     }
708     std::shared_ptr<RSDisplayNode> rsDisplayNode;
709     std::vector<Point> points;
710     for (uint32_t i = 0; i < allExpandScreenIds.size(); i++) {
711         rsDisplayNode = abstractScreenController_->GetRSDisplayNodeByScreenId(allExpandScreenIds[i]);
712         points.emplace_back(pointsMap[allExpandScreenIds[i]]);
713         if (rsDisplayNode != nullptr) {
714             rsDisplayNode->SetDisplayOffset(pointsMap[allExpandScreenIds[i]].posX_,
715                 pointsMap[allExpandScreenIds[i]].posY_);
716         }
717     }
718     HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "dms:MakeExpand");
719     if (!abstractScreenController_->MakeExpand(allExpandScreenIds, points)) {
720         WLOGFE("make expand failed.");
721         return DMError::DM_ERROR_NULLPTR;
722     }
723     auto screen = abstractScreenController_->GetAbstractScreen(allExpandScreenIds[0]);
724     if (screen == nullptr || abstractScreenController_->GetAbstractScreenGroup(screen->groupDmsId_) == nullptr) {
725         WLOGFE("get screen group failed.");
726         return DMError::DM_ERROR_NULLPTR;
727     }
728     screenGroupId = screen->groupDmsId_;
729     return DMError::DM_OK;
730 }
731 
StopExpand(const std::vector<ScreenId>& expandScreenIds)732 DMError DisplayManagerService::StopExpand(const std::vector<ScreenId>& expandScreenIds)
733 {
734     if (!Permission::IsSystemCalling() && !Permission::IsStartByHdcd()) {
735         WLOGFE("stop expand permission denied!");
736         return DMError::DM_ERROR_NOT_SYSTEM_APP;
737     }
738     auto allExpandScreenIds = abstractScreenController_->GetAllValidScreenIds(expandScreenIds);
739     if (allExpandScreenIds.empty()) {
740         WLOGFI("stop expand done. screens' size:%{public}u", static_cast<uint32_t>(allExpandScreenIds.size()));
741         return DMError::DM_OK;
742     }
743 
744     DMError ret = abstractScreenController_->StopScreens(allExpandScreenIds, ScreenCombination::SCREEN_EXPAND);
745     if (ret != DMError::DM_OK) {
746         WLOGFE("stop expand failed.");
747         return ret;
748     }
749 
750     return DMError::DM_OK;
751 }
752 
SetScreenActiveMode(ScreenId screenId, uint32_t modeId)753 DMError DisplayManagerService::SetScreenActiveMode(ScreenId screenId, uint32_t modeId)
754 {
755     if (!Permission::IsSystemCalling() && !Permission::IsStartByHdcd()) {
756         WLOGFE("set screen active permission denied!");
757         return DMError::DM_ERROR_NOT_SYSTEM_APP;
758     }
759     HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "dms:SetScreenActiveMode(%" PRIu64", %u)", screenId, modeId);
760     return abstractScreenController_->SetScreenActiveMode(screenId, modeId);
761 }
762 
SetVirtualPixelRatio(ScreenId screenId, float virtualPixelRatio)763 DMError DisplayManagerService::SetVirtualPixelRatio(ScreenId screenId, float virtualPixelRatio)
764 {
765     if (!Permission::IsSystemCalling() && !Permission::IsStartByHdcd()) {
766         WLOGFE("set virtual pixel permission denied!");
767         return DMError::DM_ERROR_NOT_SYSTEM_APP;
768     }
769     HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "dms:SetVirtualPixelRatio(%" PRIu64", %f)", screenId,
770         virtualPixelRatio);
771     return abstractScreenController_->SetVirtualPixelRatio(screenId, virtualPixelRatio);
772 }
773 
IsScreenRotationLocked(bool& isLocked)774 DMError DisplayManagerService::IsScreenRotationLocked(bool& isLocked)
775 {
776     if (!Permission::IsSystemCalling() && !Permission::IsStartByHdcd()) {
777         WLOGFE("is screen rotation locked permission denied!");
778         return DMError::DM_ERROR_NOT_SYSTEM_APP;
779     }
780     isLocked = ScreenRotationController::IsScreenRotationLocked();
781     return DMError::DM_OK;
782 }
783 
SetScreenRotationLocked(bool isLocked)784 DMError DisplayManagerService::SetScreenRotationLocked(bool isLocked)
785 {
786     if (!Permission::IsSystemCalling() && !Permission::IsStartByHdcd()) {
787         WLOGFE("set screen rotation locked permission denied!");
788         return DMError::DM_ERROR_NOT_SYSTEM_APP;
789     }
790     return ScreenRotationController::SetScreenRotationLocked(isLocked);
791 }
792 
SetScreenRotationLockedFromJs(bool isLocked)793 DMError DisplayManagerService::SetScreenRotationLockedFromJs(bool isLocked)
794 {
795     if (!Permission::IsSystemCalling() && !Permission::IsStartByHdcd()) {
796         WLOGFE("set screen rotation locked from js permission denied!");
797         return DMError::DM_ERROR_NOT_SYSTEM_APP;
798     }
799     return ScreenRotationController::SetScreenRotationLocked(isLocked);
800 }
801 
SetGravitySensorSubscriptionEnabled()802 void DisplayManagerService::SetGravitySensorSubscriptionEnabled()
803 {
804     if (!isAutoRotationOpen_) {
805         WLOGFE("autoRotation is not open");
806         ScreenRotationController::Init();
807         return;
808     }
809     SensorConnector::SubscribeRotationSensor();
810 }
811 
GetCutoutInfo(DisplayId displayId)812 sptr<CutoutInfo> DisplayManagerService::GetCutoutInfo(DisplayId displayId)
813 {
814     return displayCutoutController_->GetCutoutInfo(displayId);
815 }
816 
NotifyPrivateWindowStateChanged(bool hasPrivate)817 void DisplayManagerService::NotifyPrivateWindowStateChanged(bool hasPrivate)
818 {
819     DisplayManagerAgentController::GetInstance().NotifyPrivateWindowStateChanged(hasPrivate);
820 }
821 
GetAllDisplayPhysicalResolution()822 std::vector<DisplayPhysicalResolution> DisplayManagerService::GetAllDisplayPhysicalResolution()
823 {
824     if (allDisplayPhysicalResolution_.empty()) {
825         sptr<DisplayInfo> displayInfo = DisplayManagerService::GetDefaultDisplayInfo();
826         if (displayInfo == nullptr) {
827             TLOGE(WmsLogTag::DMS, "default display null");
828             return allDisplayPhysicalResolution_;
829         }
830         DisplayPhysicalResolution defaultResolution;
831         defaultResolution.foldDisplayMode_ = FoldDisplayMode::UNKNOWN;
832         defaultResolution.physicalWidth_ = static_cast<uint32_t>(displayInfo->GetWidth());
833         defaultResolution.physicalHeight_ = static_cast<uint32_t>(displayInfo->GetHeight());
834         allDisplayPhysicalResolution_.emplace_back(defaultResolution);
835     }
836     return allDisplayPhysicalResolution_;
837 }
838 } // namespace OHOS::Rosen