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