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_inner.h"
17
18#include <cinttypes>
19#include <iservice_registry.h>
20
21#include "display_manager_service.h"
22#include "window_manager_hilog.h"
23
24namespace OHOS::Rosen {
25namespace {
26constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_DISPLAY, "DisplayManagerServiceInner"};
27}
28WM_IMPLEMENT_SINGLE_INSTANCE(DisplayManagerServiceInner)
29
30DisplayId DisplayManagerServiceInner::GetDefaultDisplayId() const
31{
32    auto defaultDisplayInfo = DisplayManagerService::GetInstance().GetDefaultDisplayInfo();
33    if (defaultDisplayInfo == nullptr) {
34        WLOGFE("GetDefaultDisplayId, defaultDisplayInfo is nullptr.");
35        return DISPLAY_ID_INVALID;
36    }
37    return defaultDisplayInfo->GetDisplayId();
38}
39
40sptr<DisplayInfo> DisplayManagerServiceInner::GetDisplayById(DisplayId displayId) const
41{
42    sptr<DisplayInfo> display = DisplayManagerService::GetInstance().GetDisplayInfoById(displayId);
43    if (display == nullptr) {
44        WLOGFE("GetDisplayById can not find corresponding display!\n");
45    }
46    return display;
47}
48
49sptr<DisplayInfo> DisplayManagerServiceInner::GetDefaultDisplay() const
50{
51    return DisplayManagerService::GetInstance().GetDefaultDisplayInfo();
52}
53
54std::vector<DisplayId> DisplayManagerServiceInner::GetAllDisplayIds() const
55{
56    return DisplayManagerService::GetInstance().GetAllDisplayIds();
57}
58
59void DisplayManagerServiceInner::RegisterWindowInfoQueriedListener(const sptr<IWindowInfoQueriedListener>& listener)
60{
61    DisplayManagerService::GetInstance().RegisterWindowInfoQueriedListener(listener);
62}
63
64std::vector<sptr<DisplayInfo>> DisplayManagerServiceInner::GetAllDisplays() const
65{
66    std::vector<sptr<DisplayInfo>> res;
67    auto displayIds = GetAllDisplayIds();
68    for (auto displayId : displayIds) {
69        sptr<DisplayInfo> display = DisplayManagerService::GetInstance().GetDisplayInfoById(displayId);
70        if (display != nullptr) {
71            res.emplace_back(display);
72        } else {
73            WLOGFE("GetAllDisplays display %" PRIu64" nullptr!", displayId);
74        }
75    }
76    return res;
77}
78
79void DisplayManagerServiceInner::UpdateRSTree(DisplayId displayId, DisplayId parentDisplayId,
80    std::shared_ptr<RSSurfaceNode>& surfaceNode, bool isAdd, bool isMultiDisplay)
81{
82    DisplayManagerService::GetInstance().UpdateRSTree(displayId, parentDisplayId, surfaceNode, isAdd, isMultiDisplay);
83}
84
85sptr<ScreenInfo> DisplayManagerServiceInner::GetScreenInfoByDisplayId(DisplayId displayId) const
86{
87    auto displayInfo = DisplayManagerService::GetInstance().GetDisplayInfoById(displayId);
88    if (displayInfo == nullptr) {
89        WLOGFE("can not get display.");
90        return nullptr;
91    }
92    return DisplayManagerService::GetInstance().GetScreenInfoById(displayInfo->GetScreenId());
93}
94
95ScreenId DisplayManagerServiceInner::GetScreenGroupIdByDisplayId(DisplayId displayId) const
96{
97    auto displayInfo = DisplayManagerService::GetInstance().GetDisplayInfoById(displayId);
98    if (displayInfo == nullptr) {
99        WLOGFE("can not get display.");
100        return INVALID_SCREEN_ID;
101    }
102    return DisplayManagerService::GetInstance().GetScreenGroupIdByScreenId(displayInfo->GetScreenId());
103}
104
105sptr<SupportedScreenModes> DisplayManagerServiceInner::GetScreenModesByDisplayId(DisplayId displayId) const
106{
107    const sptr<ScreenInfo> screenInfo = GetScreenInfoByDisplayId(displayId);
108    if (screenInfo == nullptr) {
109        WLOGFE("can not get display.");
110        return nullptr;
111    }
112    auto modes = screenInfo->GetModes();
113    auto id = screenInfo->GetModeId();
114    if (id >= modes.size()) {
115        WLOGFE("can not get screenMode.");
116        return nullptr;
117    }
118    return modes[id];
119}
120
121std::shared_ptr<Media::PixelMap> DisplayManagerServiceInner::GetDisplaySnapshot(DisplayId displayId,
122    DmErrorCode* errorCode) const
123{
124    return DisplayManagerService::GetInstance().GetDisplaySnapshot(displayId, errorCode);
125}
126
127void DisplayManagerServiceInner::RegisterDisplayChangeListener(sptr<IDisplayChangeListener> listener)
128{
129    DisplayManagerService::GetInstance().RegisterDisplayChangeListener(listener);
130}
131
132DMError DisplayManagerServiceInner::SetOrientationFromWindow(DisplayId displayId, Orientation orientation,
133    bool withAnimation)
134{
135    auto displayInfo = GetDisplayById(displayId);
136    if (displayInfo == nullptr) {
137        return DMError::DM_ERROR_NULLPTR;
138    }
139    return DisplayManagerService::GetInstance().
140        SetOrientationFromWindow(displayInfo->GetScreenId(), orientation, withAnimation);
141}
142
143bool DisplayManagerServiceInner::SetRotationFromWindow(DisplayId displayId, Rotation targetRotation, bool withAnimation)
144{
145    auto displayInfo = GetDisplayById(displayId);
146    if (displayInfo == nullptr) {
147        return false;
148    }
149    return DisplayManagerService::GetInstance().
150        SetRotationFromWindow(displayInfo->GetScreenId(), targetRotation, withAnimation);
151}
152
153void DisplayManagerServiceInner::SetGravitySensorSubscriptionEnabled()
154{
155    DisplayManagerService::GetInstance().SetGravitySensorSubscriptionEnabled();
156}
157
158sptr<CutoutInfo> DisplayManagerServiceInner::GetCutoutInfo(DisplayId displayId) const
159{
160    return DisplayManagerService::GetInstance().GetCutoutInfo(displayId);
161}
162
163void DisplayManagerServiceInner::NotifyPrivateWindowStateChanged(bool hasPrivate)
164{
165    return DisplayManagerService::GetInstance().NotifyPrivateWindowStateChanged(hasPrivate);
166}
167} // namespace OHOS::Rosen