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 "screen.h"
17
18 #include "display_manager_adapter.h"
19 #include "screen_info.h"
20 #include "window_manager_hilog.h"
21
22 namespace OHOS::Rosen {
23 namespace {
24 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, 0, "Screen"};
25 }
26 class Screen::Impl : public RefBase {
27 public:
Impl(sptr<ScreenInfo> info)28 Impl(sptr<ScreenInfo> info)
29 {
30 screenInfo_ = info;
31 }
32 ~Impl() = default;
33 DEFINE_VAR_FUNC_GET_SET_WITH_LOCK(sptr<ScreenInfo>, ScreenInfo, screenInfo);
34 };
35
Screen(sptr<ScreenInfo> info)36 Screen::Screen(sptr<ScreenInfo> info)
37 : pImpl_(new Impl(info))
38 {
39 }
40
~Screen()41 Screen::~Screen()
42 {
43 }
44
IsGroup() const45 bool Screen::IsGroup() const
46 {
47 UpdateScreenInfo();
48 return pImpl_->GetScreenInfo()->GetIsScreenGroup();
49 }
50
GetName() const51 std::string Screen::GetName() const
52 {
53 return pImpl_->GetScreenInfo()->GetName();
54 }
55
GetId() const56 ScreenId Screen::GetId() const
57 {
58 return pImpl_->GetScreenInfo()->GetScreenId();
59 }
60
GetWidth() const61 uint32_t Screen::GetWidth() const
62 {
63 auto modeId = GetModeId();
64 auto modes = GetSupportedModes();
65 if (modeId < 0 || modeId >= modes.size()) {
66 return 0;
67 }
68 return modes[modeId]->width_;
69 }
70
GetHeight() const71 uint32_t Screen::GetHeight() const
72 {
73 UpdateScreenInfo();
74 auto modeId = GetModeId();
75 auto modes = GetSupportedModes();
76 if (modeId < 0 || modeId >= modes.size()) {
77 return 0;
78 }
79 return modes[modeId]->height_;
80 }
81
GetVirtualWidth() const82 uint32_t Screen::GetVirtualWidth() const
83 {
84 UpdateScreenInfo();
85 return pImpl_->GetScreenInfo()->GetVirtualWidth();
86 }
87
GetVirtualHeight() const88 uint32_t Screen::GetVirtualHeight() const
89 {
90 UpdateScreenInfo();
91 return pImpl_->GetScreenInfo()->GetVirtualHeight();
92 }
93
GetVirtualPixelRatio() const94 float Screen::GetVirtualPixelRatio() const
95 {
96 UpdateScreenInfo();
97 return pImpl_->GetScreenInfo()->GetVirtualPixelRatio();
98 }
99
GetRotation() const100 Rotation Screen::GetRotation() const
101 {
102 UpdateScreenInfo();
103 return pImpl_->GetScreenInfo()->GetRotation();
104 }
105
GetOrientation() const106 Orientation Screen::GetOrientation() const
107 {
108 UpdateScreenInfo();
109 return pImpl_->GetScreenInfo()->GetOrientation();
110 }
111
IsReal() const112 bool Screen::IsReal() const
113 {
114 return pImpl_->GetScreenInfo()->GetType() == ScreenType::REAL;
115 }
116
SetOrientation(Orientation orientation) const117 bool Screen::SetOrientation(Orientation orientation) const
118 {
119 WLOGFD("set orientation %{public}u", orientation);
120 return SingletonContainer::Get<ScreenManagerAdapter>().SetOrientation(GetId(), orientation);
121 }
122
GetScreenSupportedColorGamuts(std::vector<ScreenColorGamut> & colorGamuts) const123 DMError Screen::GetScreenSupportedColorGamuts(std::vector<ScreenColorGamut>& colorGamuts) const
124 {
125 return SingletonContainer::Get<ScreenManagerAdapter>().GetScreenSupportedColorGamuts(GetId(), colorGamuts);
126 }
127
GetScreenColorGamut(ScreenColorGamut & colorGamut) const128 DMError Screen::GetScreenColorGamut(ScreenColorGamut& colorGamut) const
129 {
130 return SingletonContainer::Get<ScreenManagerAdapter>().GetScreenColorGamut(GetId(), colorGamut);
131 }
132
SetScreenColorGamut(int32_t colorGamutIdx)133 DMError Screen::SetScreenColorGamut(int32_t colorGamutIdx)
134 {
135 return SingletonContainer::Get<ScreenManagerAdapter>().SetScreenColorGamut(GetId(), colorGamutIdx);
136 }
137
GetScreenGamutMap(ScreenGamutMap & gamutMap) const138 DMError Screen::GetScreenGamutMap(ScreenGamutMap& gamutMap) const
139 {
140 return SingletonContainer::Get<ScreenManagerAdapter>().GetScreenGamutMap(GetId(), gamutMap);
141 }
142
SetScreenGamutMap(ScreenGamutMap gamutMap)143 DMError Screen::SetScreenGamutMap(ScreenGamutMap gamutMap)
144 {
145 return SingletonContainer::Get<ScreenManagerAdapter>().SetScreenGamutMap(GetId(), gamutMap);
146 }
147
SetScreenColorTransform()148 DMError Screen::SetScreenColorTransform()
149 {
150 return SingletonContainer::Get<ScreenManagerAdapter>().SetScreenColorTransform(GetId());
151 }
152
GetParentId() const153 ScreenId Screen::GetParentId() const
154 {
155 UpdateScreenInfo();
156 return pImpl_->GetScreenInfo()->GetParentId();
157 }
158
GetModeId() const159 uint32_t Screen::GetModeId() const
160 {
161 UpdateScreenInfo();
162 return pImpl_->GetScreenInfo()->GetModeId();
163 }
164
GetSupportedModes() const165 std::vector<sptr<SupportedScreenModes>> Screen::GetSupportedModes() const
166 {
167 return pImpl_->GetScreenInfo()->GetModes();
168 }
169
SetScreenActiveMode(uint32_t modeId)170 bool Screen::SetScreenActiveMode(uint32_t modeId)
171 {
172 ScreenId screenId = GetId();
173 if (modeId >= GetSupportedModes().size()) {
174 return false;
175 }
176 return SingletonContainer::Get<ScreenManagerAdapter>().SetScreenActiveMode(screenId, modeId);
177 }
178
UpdateScreenInfo(sptr<ScreenInfo> info) const179 void Screen::UpdateScreenInfo(sptr<ScreenInfo> info) const
180 {
181 if (info == nullptr) {
182 WLOGFE("ScreenInfo is invalid");
183 return;
184 }
185 pImpl_->SetScreenInfo(info);
186 }
187
UpdateScreenInfo() const188 void Screen::UpdateScreenInfo() const
189 {
190 auto screenInfo = SingletonContainer::Get<ScreenManagerAdapter>().GetScreenInfo(GetId());
191 UpdateScreenInfo(screenInfo);
192 }
193 } // namespace OHOS::Rosen