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 class Screen::Impl : public RefBase {
30 public:
Impl(sptr<ScreenInfo> info)31 explicit Impl(sptr<ScreenInfo> info)
32 {
33 screenInfo_ = info;
34 }
35 ~Impl() = default;
36 DEFINE_VAR_FUNC_GET_SET_WITH_LOCK(sptr<ScreenInfo>, ScreenInfo, screenInfo);
37 };
38
Screen(sptr<ScreenInfo> info)39 Screen::Screen(sptr<ScreenInfo> info)
40 : pImpl_(new Impl(info))
41 {
42 }
43
~Screen()44 Screen::~Screen()
45 {
46 }
47
IsGroup() const48 bool Screen::IsGroup() const
49 {
50 UpdateScreenInfo();
51 return pImpl_->GetScreenInfo()->GetIsScreenGroup();
52 }
53
GetName() const54 std::string Screen::GetName() const
55 {
56 return pImpl_->GetScreenInfo()->GetName();
57 }
58
GetId() const59 ScreenId Screen::GetId() const
60 {
61 return pImpl_->GetScreenInfo()->GetScreenId();
62 }
63
GetWidth() const64 uint32_t Screen::GetWidth() const
65 {
66 auto modeId = GetModeId();
67 auto modes = GetSupportedModes();
68 if (modeId < 0 || modeId >= modes.size()) {
69 return 0;
70 }
71 return modes[modeId]->width_;
72 }
73
GetHeight() const74 uint32_t Screen::GetHeight() const
75 {
76 UpdateScreenInfo();
77 auto modeId = GetModeId();
78 auto modes = GetSupportedModes();
79 if (modeId < 0 || modeId >= modes.size()) {
80 return 0;
81 }
82 return modes[modeId]->height_;
83 }
84
GetVirtualWidth() const85 uint32_t Screen::GetVirtualWidth() const
86 {
87 UpdateScreenInfo();
88 return pImpl_->GetScreenInfo()->GetVirtualWidth();
89 }
90
GetVirtualHeight() const91 uint32_t Screen::GetVirtualHeight() const
92 {
93 UpdateScreenInfo();
94 return pImpl_->GetScreenInfo()->GetVirtualHeight();
95 }
96
GetVirtualPixelRatio() const97 float Screen::GetVirtualPixelRatio() const
98 {
99 UpdateScreenInfo();
100 return pImpl_->GetScreenInfo()->GetVirtualPixelRatio();
101 }
102
GetRotation() const103 Rotation Screen::GetRotation() const
104 {
105 UpdateScreenInfo();
106 return pImpl_->GetScreenInfo()->GetRotation();
107 }
108
GetOrientation() const109 Orientation Screen::GetOrientation() const
110 {
111 UpdateScreenInfo();
112 return pImpl_->GetScreenInfo()->GetOrientation();
113 }
114
IsReal() const115 bool Screen::IsReal() const
116 {
117 return pImpl_->GetScreenInfo()->GetType() == ScreenType::REAL;
118 }
119
SetOrientation(Orientation orientation) const120 DMError Screen::SetOrientation(Orientation orientation) const
121 {
122 TLOGD(WmsLogTag::DMS, "Orientation %{public}u", orientation);
123 return SingletonContainer::Get<ScreenManagerAdapter>().SetOrientation(GetId(), orientation);
124 }
125
GetScreenSupportedColorGamuts(std::vector<ScreenColorGamut> & colorGamuts) const126 DMError Screen::GetScreenSupportedColorGamuts(std::vector<ScreenColorGamut>& colorGamuts) const
127 {
128 return SingletonContainer::Get<ScreenManagerAdapter>().GetScreenSupportedColorGamuts(GetId(), colorGamuts);
129 }
130
GetScreenColorGamut(ScreenColorGamut & colorGamut) const131 DMError Screen::GetScreenColorGamut(ScreenColorGamut& colorGamut) const
132 {
133 return SingletonContainer::Get<ScreenManagerAdapter>().GetScreenColorGamut(GetId(), colorGamut);
134 }
135
SetScreenColorGamut(int32_t colorGamutIdx)136 DMError Screen::SetScreenColorGamut(int32_t colorGamutIdx)
137 {
138 return SingletonContainer::Get<ScreenManagerAdapter>().SetScreenColorGamut(GetId(), colorGamutIdx);
139 }
140
GetScreenGamutMap(ScreenGamutMap & gamutMap) const141 DMError Screen::GetScreenGamutMap(ScreenGamutMap& gamutMap) const
142 {
143 return SingletonContainer::Get<ScreenManagerAdapter>().GetScreenGamutMap(GetId(), gamutMap);
144 }
145
SetScreenGamutMap(ScreenGamutMap gamutMap)146 DMError Screen::SetScreenGamutMap(ScreenGamutMap gamutMap)
147 {
148 return SingletonContainer::Get<ScreenManagerAdapter>().SetScreenGamutMap(GetId(), gamutMap);
149 }
150
SetScreenColorTransform()151 DMError Screen::SetScreenColorTransform()
152 {
153 return SingletonContainer::Get<ScreenManagerAdapter>().SetScreenColorTransform(GetId());
154 }
155
GetPixelFormat(GraphicPixelFormat & pixelFormat) const156 DMError Screen::GetPixelFormat(GraphicPixelFormat& pixelFormat) const
157 {
158 return SingletonContainer::Get<ScreenManagerAdapter>().GetPixelFormat(GetId(), pixelFormat);
159 }
160
SetPixelFormat(GraphicPixelFormat pixelFormat)161 DMError Screen::SetPixelFormat(GraphicPixelFormat pixelFormat)
162 {
163 return SingletonContainer::Get<ScreenManagerAdapter>().SetPixelFormat(GetId(), pixelFormat);
164 }
165
GetSupportedHDRFormats(std::vector<ScreenHDRFormat> & hdrFormats) const166 DMError Screen::GetSupportedHDRFormats(std::vector<ScreenHDRFormat>& hdrFormats) const
167 {
168 return SingletonContainer::Get<ScreenManagerAdapter>().GetSupportedHDRFormats(GetId(), hdrFormats);
169 }
170
GetScreenHDRFormat(ScreenHDRFormat & hdrFormat) const171 DMError Screen::GetScreenHDRFormat(ScreenHDRFormat& hdrFormat) const
172 {
173 return SingletonContainer::Get<ScreenManagerAdapter>().GetScreenHDRFormat(GetId(), hdrFormat);
174 }
175
SetScreenHDRFormat(int32_t modeIdx)176 DMError Screen::SetScreenHDRFormat(int32_t modeIdx)
177 {
178 return SingletonContainer::Get<ScreenManagerAdapter>().SetScreenHDRFormat(GetId(), modeIdx);
179 }
180
GetSupportedColorSpaces(std::vector<GraphicCM_ColorSpaceType> & colorSpaces) const181 DMError Screen::GetSupportedColorSpaces(std::vector<GraphicCM_ColorSpaceType>& colorSpaces) const
182 {
183 return SingletonContainer::Get<ScreenManagerAdapter>().GetSupportedColorSpaces(GetId(), colorSpaces);
184 }
185
GetScreenColorSpace(GraphicCM_ColorSpaceType & colorSpace) const186 DMError Screen::GetScreenColorSpace(GraphicCM_ColorSpaceType& colorSpace) const
187 {
188 return SingletonContainer::Get<ScreenManagerAdapter>().GetScreenColorSpace(GetId(), colorSpace);
189 }
190
SetScreenColorSpace(GraphicCM_ColorSpaceType colorSpace)191 DMError Screen::SetScreenColorSpace(GraphicCM_ColorSpaceType colorSpace)
192 {
193 return SingletonContainer::Get<ScreenManagerAdapter>().SetScreenColorSpace(GetId(), colorSpace);
194 }
195
GetParentId() const196 ScreenId Screen::GetParentId() const
197 {
198 UpdateScreenInfo();
199 return pImpl_->GetScreenInfo()->GetParentId();
200 }
201
GetModeId() const202 uint32_t Screen::GetModeId() const
203 {
204 UpdateScreenInfo();
205 return pImpl_->GetScreenInfo()->GetModeId();
206 }
207
GetSerialNumber() const208 std::string Screen::GetSerialNumber() const
209 {
210 UpdateScreenInfo();
211 return pImpl_->GetScreenInfo()->GetSerialNumber();
212 }
213
GetSupportedModes() const214 std::vector<sptr<SupportedScreenModes>> Screen::GetSupportedModes() const
215 {
216 return pImpl_->GetScreenInfo()->GetModes();
217 }
218
SetScreenActiveMode(uint32_t modeId)219 DMError Screen::SetScreenActiveMode(uint32_t modeId)
220 {
221 ScreenId screenId = GetId();
222 if (modeId >= GetSupportedModes().size()) {
223 return DMError::DM_ERROR_INVALID_PARAM;
224 }
225 return SingletonContainer::Get<ScreenManagerAdapter>().SetScreenActiveMode(screenId, modeId);
226 }
227
UpdateScreenInfo(sptr<ScreenInfo> info) const228 void Screen::UpdateScreenInfo(sptr<ScreenInfo> info) const
229 {
230 if (info == nullptr) {
231 TLOGE(WmsLogTag::DMS, "ScreenInfo is invalid");
232 return;
233 }
234 pImpl_->SetScreenInfo(info);
235 }
236
UpdateScreenInfo() const237 void Screen::UpdateScreenInfo() const
238 {
239 auto screenInfo = SingletonContainer::Get<ScreenManagerAdapter>().GetScreenInfo(GetId());
240 UpdateScreenInfo(screenInfo);
241 }
242
SetDensityDpi(uint32_t dpi) const243 DMError Screen::SetDensityDpi(uint32_t dpi) const
244 {
245 if (dpi > DOT_PER_INCH_MAXIMUM_VALUE || dpi < DOT_PER_INCH_MINIMUM_VALUE) {
246 TLOGE(WmsLogTag::DMS, "Invalid input dpi value, valid input range for DPI is %{public}u ~ %{public}u",
247 DOT_PER_INCH_MINIMUM_VALUE, DOT_PER_INCH_MAXIMUM_VALUE);
248 return DMError::DM_ERROR_INVALID_PARAM;
249 }
250 // Calculate display density, Density = Dpi / 160.
251 float density = static_cast<float>(dpi) / 160; // 160 is the coefficient between density and dpi.
252 return SingletonContainer::Get<ScreenManagerAdapter>().SetVirtualPixelRatio(GetId(), density);
253 }
254
SetDensityDpiSystem(uint32_t dpi) const255 DMError Screen::SetDensityDpiSystem(uint32_t dpi) const
256 {
257 if (dpi > DOT_PER_INCH_MAXIMUM_VALUE || dpi < DOT_PER_INCH_MINIMUM_VALUE) {
258 TLOGE(WmsLogTag::DMS, "Invalid input dpi value, valid input range for DPI is %{public}u ~ %{public}u",
259 DOT_PER_INCH_MINIMUM_VALUE, DOT_PER_INCH_MAXIMUM_VALUE);
260 return DMError::DM_ERROR_INVALID_PARAM;
261 }
262 float density = static_cast<float>(dpi) / 160; // 160 is the coefficient between density and dpi.
263 return SingletonContainer::Get<ScreenManagerAdapter>().SetVirtualPixelRatioSystem(GetId(), density);
264 }
265
SetDefaultDensityDpi(uint32_t dpi) const266 DMError Screen::SetDefaultDensityDpi(uint32_t dpi) const
267 {
268 if (dpi > DOT_PER_INCH_MAXIMUM_VALUE || dpi < DOT_PER_INCH_MINIMUM_VALUE) {
269 TLOGE(WmsLogTag::DMS, "Invalid input dpi value, valid input range for DPI is %{public}u ~ %{public}u",
270 DOT_PER_INCH_MINIMUM_VALUE, DOT_PER_INCH_MAXIMUM_VALUE);
271 return DMError::DM_ERROR_INVALID_PARAM;
272 }
273 float density = static_cast<float>(dpi) / BASELINE_DENSITY; // 160 is the coefficient between density and dpi.
274 return SingletonContainer::Get<ScreenManagerAdapter>().SetDefaultDensityDpi(GetId(), density);
275 }
276
SetResolution(uint32_t width,uint32_t height,uint32_t dpi) const277 DMError Screen::SetResolution(uint32_t width, uint32_t height, uint32_t dpi) const
278 {
279 if (width <= 0 || height <= 0 || dpi > DOT_PER_INCH_MAXIMUM_VALUE || dpi < DOT_PER_INCH_MINIMUM_VALUE) {
280 TLOGE(WmsLogTag::DMS, "Invalid param, w:%{public}u h:%{public}u dpi:%{public}u", width, height, dpi);
281 return DMError::DM_ERROR_INVALID_PARAM;
282 }
283 // Calculate display density, Density = Dpi / 160.
284 float density = static_cast<float>(dpi) / 160; // 160 is the coefficient between density and dpi.
285 return SingletonContainer::Get<ScreenManagerAdapter>().SetResolution(GetId(), width, height, density);
286 }
287
GetDensityInCurResolution(float & virtualPixelRatio) const288 DMError Screen::GetDensityInCurResolution(float& virtualPixelRatio) const
289 {
290 return SingletonContainer::Get<ScreenManagerAdapter>().GetDensityInCurResolution(GetId(), virtualPixelRatio);
291 }
292
GetScreenInfo() const293 sptr<ScreenInfo> Screen::GetScreenInfo() const
294 {
295 UpdateScreenInfo();
296 return pImpl_->GetScreenInfo();
297 }
298 } // namespace OHOS::Rosen