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 <cstdint>
19 #include <new>
20 #include <refbase.h>
21
22 #include "class_var_definition.h"
23 #include "display_manager_adapter.h"
24 #include "screen_info.h"
25 #include "singleton_container.h"
26 #include "window_manager_hilog.h"
27
28 namespace OHOS::Rosen {
29 namespace {
30 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_DISPLAY, "Screen"};
31 }
32 class Screen::Impl : public RefBase {
33 public:
Impl(sptr<ScreenInfo> info)34 explicit Impl(sptr<ScreenInfo> info)
35 {
36 screenInfo_ = info;
37 }
38 ~Impl() = default;
39 DEFINE_VAR_FUNC_GET_SET_WITH_LOCK(sptr<ScreenInfo>, ScreenInfo, screenInfo);
40 };
41
Screen(sptr<ScreenInfo> info)42 Screen::Screen(sptr<ScreenInfo> info)
43 : pImpl_(new Impl(info))
44 {
45 }
46
~Screen()47 Screen::~Screen()
48 {
49 }
50
IsGroup() const51 bool Screen::IsGroup() const
52 {
53 UpdateScreenInfo();
54 return pImpl_->GetScreenInfo()->GetIsScreenGroup();
55 }
56
GetName() const57 std::string Screen::GetName() const
58 {
59 return pImpl_->GetScreenInfo()->GetName();
60 }
61
GetId() const62 ScreenId Screen::GetId() const
63 {
64 return pImpl_->GetScreenInfo()->GetScreenId();
65 }
66
GetWidth() const67 uint32_t Screen::GetWidth() const
68 {
69 auto modeId = GetModeId();
70 auto modes = GetSupportedModes();
71 if (modeId < 0 || modeId >= modes.size()) {
72 return 0;
73 }
74 return modes[modeId]->width_;
75 }
76
GetHeight() const77 uint32_t Screen::GetHeight() const
78 {
79 UpdateScreenInfo();
80 auto modeId = GetModeId();
81 auto modes = GetSupportedModes();
82 if (modeId < 0 || modeId >= modes.size()) {
83 return 0;
84 }
85 return modes[modeId]->height_;
86 }
87
GetVirtualWidth() const88 uint32_t Screen::GetVirtualWidth() const
89 {
90 UpdateScreenInfo();
91 return pImpl_->GetScreenInfo()->GetVirtualWidth();
92 }
93
GetVirtualHeight() const94 uint32_t Screen::GetVirtualHeight() const
95 {
96 UpdateScreenInfo();
97 return pImpl_->GetScreenInfo()->GetVirtualHeight();
98 }
99
GetVirtualPixelRatio() const100 float Screen::GetVirtualPixelRatio() const
101 {
102 UpdateScreenInfo();
103 return pImpl_->GetScreenInfo()->GetVirtualPixelRatio();
104 }
105
GetRotation() const106 Rotation Screen::GetRotation() const
107 {
108 UpdateScreenInfo();
109 return pImpl_->GetScreenInfo()->GetRotation();
110 }
111
GetOrientation() const112 Orientation Screen::GetOrientation() const
113 {
114 UpdateScreenInfo();
115 return pImpl_->GetScreenInfo()->GetOrientation();
116 }
117
IsReal() const118 bool Screen::IsReal() const
119 {
120 return pImpl_->GetScreenInfo()->GetType() == ScreenType::REAL;
121 }
122
SetOrientation(Orientation orientation) const123 DMError Screen::SetOrientation(Orientation orientation) const
124 {
125 WLOGFD("Orientation %{public}u", orientation);
126 return SingletonContainer::Get<ScreenManagerAdapter>().SetOrientation(GetId(), orientation);
127 }
128
GetScreenSupportedColorGamuts(std::vector<ScreenColorGamut> & colorGamuts) const129 DMError Screen::GetScreenSupportedColorGamuts(std::vector<ScreenColorGamut>& colorGamuts) const
130 {
131 return SingletonContainer::Get<ScreenManagerAdapter>().GetScreenSupportedColorGamuts(GetId(), colorGamuts);
132 }
133
GetScreenColorGamut(ScreenColorGamut & colorGamut) const134 DMError Screen::GetScreenColorGamut(ScreenColorGamut& colorGamut) const
135 {
136 return SingletonContainer::Get<ScreenManagerAdapter>().GetScreenColorGamut(GetId(), colorGamut);
137 }
138
SetScreenColorGamut(int32_t colorGamutIdx)139 DMError Screen::SetScreenColorGamut(int32_t colorGamutIdx)
140 {
141 return SingletonContainer::Get<ScreenManagerAdapter>().SetScreenColorGamut(GetId(), colorGamutIdx);
142 }
143
GetScreenGamutMap(ScreenGamutMap & gamutMap) const144 DMError Screen::GetScreenGamutMap(ScreenGamutMap& gamutMap) const
145 {
146 return SingletonContainer::Get<ScreenManagerAdapter>().GetScreenGamutMap(GetId(), gamutMap);
147 }
148
SetScreenGamutMap(ScreenGamutMap gamutMap)149 DMError Screen::SetScreenGamutMap(ScreenGamutMap gamutMap)
150 {
151 return SingletonContainer::Get<ScreenManagerAdapter>().SetScreenGamutMap(GetId(), gamutMap);
152 }
153
SetScreenColorTransform()154 DMError Screen::SetScreenColorTransform()
155 {
156 return SingletonContainer::Get<ScreenManagerAdapter>().SetScreenColorTransform(GetId());
157 }
158
GetParentId() const159 ScreenId Screen::GetParentId() const
160 {
161 UpdateScreenInfo();
162 return pImpl_->GetScreenInfo()->GetParentId();
163 }
164
GetModeId() const165 uint32_t Screen::GetModeId() const
166 {
167 UpdateScreenInfo();
168 return pImpl_->GetScreenInfo()->GetModeId();
169 }
170
GetSupportedModes() const171 std::vector<sptr<SupportedScreenModes>> Screen::GetSupportedModes() const
172 {
173 return pImpl_->GetScreenInfo()->GetModes();
174 }
175
SetScreenActiveMode(uint32_t modeId)176 DMError Screen::SetScreenActiveMode(uint32_t modeId)
177 {
178 ScreenId screenId = GetId();
179 if (modeId >= GetSupportedModes().size()) {
180 return DMError::DM_ERROR_INVALID_PARAM;
181 }
182 return SingletonContainer::Get<ScreenManagerAdapter>().SetScreenActiveMode(screenId, modeId);
183 }
184
UpdateScreenInfo(sptr<ScreenInfo> info) const185 void Screen::UpdateScreenInfo(sptr<ScreenInfo> info) const
186 {
187 if (info == nullptr) {
188 WLOGFE("ScreenInfo is invalid");
189 return;
190 }
191 pImpl_->SetScreenInfo(info);
192 }
193
UpdateScreenInfo() const194 void Screen::UpdateScreenInfo() const
195 {
196 auto screenInfo = SingletonContainer::Get<ScreenManagerAdapter>().GetScreenInfo(GetId());
197 UpdateScreenInfo(screenInfo);
198 }
199
SetDensityDpi(uint32_t dpi) const200 DMError Screen::SetDensityDpi(uint32_t dpi) const
201 {
202 if (dpi > DOT_PER_INCH_MAXIMUM_VALUE || dpi < DOT_PER_INCH_MINIMUM_VALUE) {
203 WLOGE("Invalid input dpi value, valid input range for DPI is %{public}u ~ %{public}u",
204 DOT_PER_INCH_MINIMUM_VALUE, DOT_PER_INCH_MAXIMUM_VALUE);
205 return DMError::DM_ERROR_INVALID_PARAM;
206 }
207 // Calculate display density, Density = Dpi / 160.
208 float density = static_cast<float>(dpi) / 160; // 160 is the coefficient between density and dpi.
209 return SingletonContainer::Get<ScreenManagerAdapter>().SetVirtualPixelRatio(GetId(), density);
210 }
211
GetScreenInfo() const212 sptr<ScreenInfo> Screen::GetScreenInfo() const
213 {
214 UpdateScreenInfo();
215 return pImpl_->GetScreenInfo();
216 }
217 } // namespace OHOS::Rosen