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
GetPixelFormat(GraphicPixelFormat & pixelFormat) const159 DMError Screen::GetPixelFormat(GraphicPixelFormat& pixelFormat) const
160 {
161 return SingletonContainer::Get<ScreenManagerAdapter>().GetPixelFormat(GetId(), pixelFormat);
162 }
163
SetPixelFormat(GraphicPixelFormat pixelFormat)164 DMError Screen::SetPixelFormat(GraphicPixelFormat pixelFormat)
165 {
166 return SingletonContainer::Get<ScreenManagerAdapter>().SetPixelFormat(GetId(), pixelFormat);
167 }
168
GetSupportedHDRFormats(std::vector<ScreenHDRFormat> & hdrFormats) const169 DMError Screen::GetSupportedHDRFormats(std::vector<ScreenHDRFormat>& hdrFormats) const
170 {
171 return SingletonContainer::Get<ScreenManagerAdapter>().GetSupportedHDRFormats(GetId(), hdrFormats);
172 }
173
GetScreenHDRFormat(ScreenHDRFormat & hdrFormat) const174 DMError Screen::GetScreenHDRFormat(ScreenHDRFormat& hdrFormat) const
175 {
176 return SingletonContainer::Get<ScreenManagerAdapter>().GetScreenHDRFormat(GetId(), hdrFormat);
177 }
178
SetScreenHDRFormat(int32_t modeIdx)179 DMError Screen::SetScreenHDRFormat(int32_t modeIdx)
180 {
181 return SingletonContainer::Get<ScreenManagerAdapter>().SetScreenHDRFormat(GetId(), modeIdx);
182 }
183
GetSupportedColorSpaces(std::vector<GraphicCM_ColorSpaceType> & colorSpaces) const184 DMError Screen::GetSupportedColorSpaces(std::vector<GraphicCM_ColorSpaceType>& colorSpaces) const
185 {
186 return SingletonContainer::Get<ScreenManagerAdapter>().GetSupportedColorSpaces(GetId(), colorSpaces);
187 }
188
GetScreenColorSpace(GraphicCM_ColorSpaceType & colorSpace) const189 DMError Screen::GetScreenColorSpace(GraphicCM_ColorSpaceType& colorSpace) const
190 {
191 return SingletonContainer::Get<ScreenManagerAdapter>().GetScreenColorSpace(GetId(), colorSpace);
192 }
193
SetScreenColorSpace(GraphicCM_ColorSpaceType colorSpace)194 DMError Screen::SetScreenColorSpace(GraphicCM_ColorSpaceType colorSpace)
195 {
196 return SingletonContainer::Get<ScreenManagerAdapter>().SetScreenColorSpace(GetId(), colorSpace);
197 }
198
GetParentId() const199 ScreenId Screen::GetParentId() const
200 {
201 UpdateScreenInfo();
202 return pImpl_->GetScreenInfo()->GetParentId();
203 }
204
GetModeId() const205 uint32_t Screen::GetModeId() const
206 {
207 UpdateScreenInfo();
208 return pImpl_->GetScreenInfo()->GetModeId();
209 }
210
GetSupportedModes() const211 std::vector<sptr<SupportedScreenModes>> Screen::GetSupportedModes() const
212 {
213 return pImpl_->GetScreenInfo()->GetModes();
214 }
215
SetScreenActiveMode(uint32_t modeId)216 DMError Screen::SetScreenActiveMode(uint32_t modeId)
217 {
218 ScreenId screenId = GetId();
219 if (modeId >= GetSupportedModes().size()) {
220 return DMError::DM_ERROR_INVALID_PARAM;
221 }
222 return SingletonContainer::Get<ScreenManagerAdapter>().SetScreenActiveMode(screenId, modeId);
223 }
224
UpdateScreenInfo(sptr<ScreenInfo> info) const225 void Screen::UpdateScreenInfo(sptr<ScreenInfo> info) const
226 {
227 if (info == nullptr) {
228 WLOGFE("ScreenInfo is invalid");
229 return;
230 }
231 pImpl_->SetScreenInfo(info);
232 }
233
UpdateScreenInfo() const234 void Screen::UpdateScreenInfo() const
235 {
236 auto screenInfo = SingletonContainer::Get<ScreenManagerAdapter>().GetScreenInfo(GetId());
237 UpdateScreenInfo(screenInfo);
238 }
239
SetDensityDpi(uint32_t dpi) const240 DMError Screen::SetDensityDpi(uint32_t dpi) const
241 {
242 if (dpi > DOT_PER_INCH_MAXIMUM_VALUE || dpi < DOT_PER_INCH_MINIMUM_VALUE) {
243 WLOGE("Invalid input dpi value, valid input range for DPI is %{public}u ~ %{public}u",
244 DOT_PER_INCH_MINIMUM_VALUE, DOT_PER_INCH_MAXIMUM_VALUE);
245 return DMError::DM_ERROR_INVALID_PARAM;
246 }
247 // Calculate display density, Density = Dpi / 160.
248 float density = static_cast<float>(dpi) / 160; // 160 is the coefficient between density and dpi.
249 return SingletonContainer::Get<ScreenManagerAdapter>().SetVirtualPixelRatio(GetId(), density);
250 }
251
SetDensityDpiSystem(uint32_t dpi) const252 DMError Screen::SetDensityDpiSystem(uint32_t dpi) const
253 {
254 if (dpi > DOT_PER_INCH_MAXIMUM_VALUE || dpi < DOT_PER_INCH_MINIMUM_VALUE) {
255 WLOGE("Invalid input dpi value, valid input range for DPI is %{public}u ~ %{public}u",
256 DOT_PER_INCH_MINIMUM_VALUE, DOT_PER_INCH_MAXIMUM_VALUE);
257 return DMError::DM_ERROR_INVALID_PARAM;
258 }
259 float density = static_cast<float>(dpi) / 160; // 160 is the coefficient between density and dpi.
260 return SingletonContainer::Get<ScreenManagerAdapter>().SetVirtualPixelRatioSystem(GetId(), density);
261 }
262
SetResolution(uint32_t width,uint32_t height,uint32_t dpi) const263 DMError Screen::SetResolution(uint32_t width, uint32_t height, uint32_t dpi) const
264 {
265 if (width <= 0 || height <= 0 || dpi > DOT_PER_INCH_MAXIMUM_VALUE || dpi < DOT_PER_INCH_MINIMUM_VALUE) {
266 WLOGFE("Invalid param, w:%{public}u h:%{public}u dpi:%{public}u", width, height, dpi);
267 return DMError::DM_ERROR_INVALID_PARAM;
268 }
269 // Calculate display density, Density = Dpi / 160.
270 float density = static_cast<float>(dpi) / 160; // 160 is the coefficient between density and dpi.
271 return SingletonContainer::Get<ScreenManagerAdapter>().SetResolution(GetId(), width, height, density);
272 }
273
GetDensityInCurResolution(float & virtualPixelRatio) const274 DMError Screen::GetDensityInCurResolution(float& virtualPixelRatio) const
275 {
276 return SingletonContainer::Get<ScreenManagerAdapter>().GetDensityInCurResolution(GetId(), virtualPixelRatio);
277 }
278
GetScreenInfo() const279 sptr<ScreenInfo> Screen::GetScreenInfo() const
280 {
281 UpdateScreenInfo();
282 return pImpl_->GetScreenInfo();
283 }
284 } // namespace OHOS::Rosen