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