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.h"
17
18 #include <cstdint>
19 #include <new>
20 #include <refbase.h>
21
22 #include "class_var_definition.h"
23 #include "display_info.h"
24 #include "display_manager_adapter.h"
25 #include "dm_common.h"
26 #include "singleton_container.h"
27 #include "window_manager_hilog.h"
28
29 namespace OHOS::Rosen {
30 class Display::Impl : public RefBase {
31 public:
Impl(const std::string & name,sptr<DisplayInfo> info)32 Impl(const std::string& name, sptr<DisplayInfo> info)
33 {
34 name_= name;
35 displayInfo_ = info;
36 }
37 ~Impl() = default;
38 DEFINE_VAR_FUNC_GET_SET(std::string, Name, name);
GetDisplayInfo()39 sptr<DisplayInfo> GetDisplayInfo()
40 {
41 std::lock_guard<std::mutex> lock(displayInfoMutex_);
42 return displayInfo_;
43 }
44
SetDisplayInfo(sptr<DisplayInfo> value)45 void SetDisplayInfo(sptr<DisplayInfo> value)
46 {
47 std::lock_guard<std::mutex> lock(displayInfoMutex_);
48 displayInfo_ = value;
49 }
50
51 private:
52 sptr<DisplayInfo> displayInfo_;
53 std::mutex displayInfoMutex_;
54 };
55
Display(const std::string & name,sptr<DisplayInfo> info)56 Display::Display(const std::string& name, sptr<DisplayInfo> info)
57 : pImpl_(new Impl(name, info))
58 {
59 }
60
~Display()61 Display::~Display()
62 {
63 }
64
GetId() const65 DisplayId Display::GetId() const
66 {
67 if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
68 TLOGE(WmsLogTag::DMS, "pImpl_ or pImpl_->GetDisplayInfo is nullptr");
69 return DisplayId(0);
70 }
71 return pImpl_->GetDisplayInfo()->GetDisplayId();
72 }
73
GetName() const74 std::string Display::GetName() const
75 {
76 if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
77 TLOGE(WmsLogTag::DMS, "pImpl_ or pImpl_->GetDisplayInfo is nullptr");
78 return std::string();
79 }
80 return pImpl_->GetDisplayInfo()->GetName();
81 }
82
GetWidth() const83 int32_t Display::GetWidth() const
84 {
85 UpdateDisplayInfo();
86 if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
87 TLOGE(WmsLogTag::DMS, "pImpl_ or pImpl_->GetDisplayInfo is nullptr");
88 return 0;
89 }
90 return pImpl_->GetDisplayInfo()->GetWidth();
91 }
92
GetHeight() const93 int32_t Display::GetHeight() const
94 {
95 UpdateDisplayInfo();
96 if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
97 TLOGE(WmsLogTag::DMS, "pImpl_ or pImpl_->GetDisplayInfo is nullptr");
98 return 0;
99 }
100 return pImpl_->GetDisplayInfo()->GetHeight();
101 }
102
GetPhysicalWidth() const103 int32_t Display::GetPhysicalWidth() const
104 {
105 UpdateDisplayInfo();
106 if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
107 TLOGE(WmsLogTag::DMS, "pImpl_ or pImpl_->GetDisplayInfo is nullptr");
108 return 0;
109 }
110 return pImpl_->GetDisplayInfo()->GetPhysicalWidth();
111 }
112
GetPhysicalHeight() const113 int32_t Display::GetPhysicalHeight() const
114 {
115 UpdateDisplayInfo();
116 if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
117 TLOGE(WmsLogTag::DMS, "pImpl_ or pImpl_->GetDisplayInfo is nullptr");
118 return 0;
119 }
120 return pImpl_->GetDisplayInfo()->GetPhysicalHeight();
121 }
122
GetRefreshRate() const123 uint32_t Display::GetRefreshRate() const
124 {
125 UpdateDisplayInfo();
126 if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
127 TLOGE(WmsLogTag::DMS, "pImpl_ or pImpl_->GetDisplayInfo is nullptr");
128 return 0;
129 }
130 return pImpl_->GetDisplayInfo()->GetRefreshRate();
131 }
132
GetScreenId() const133 ScreenId Display::GetScreenId() const
134 {
135 UpdateDisplayInfo();
136 if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
137 TLOGE(WmsLogTag::DMS, "pImpl_ or pImpl_->GetDisplayInfo is nullptr");
138 return SCREEN_ID_INVALID;
139 }
140 return pImpl_->GetDisplayInfo()->GetScreenId();
141 }
142
GetRotation() const143 Rotation Display::GetRotation() const
144 {
145 UpdateDisplayInfo();
146 if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
147 TLOGE(WmsLogTag::DMS, "pImpl_ or pImpl_->GetDisplayInfo is nullptr");
148 return Rotation::ROTATION_0;
149 }
150 return pImpl_->GetDisplayInfo()->GetRotation();
151 }
152
GetOriginRotation() const153 Rotation Display::GetOriginRotation() const
154 {
155 UpdateDisplayInfo();
156 if (pImpl_ == nullptr) {
157 TLOGE(WmsLogTag::DMS, "pImpl_ is nullptr");
158 return Rotation::ROTATION_0;
159 }
160 auto displayInfo = pImpl_->GetDisplayInfo();
161 if (displayInfo == nullptr) {
162 TLOGE(WmsLogTag::DMS, "displayInfo is nullptr");
163 return Rotation::ROTATION_0;
164 }
165 return displayInfo->GetOriginRotation();
166 }
167
GetOrientation() const168 Orientation Display::GetOrientation() const
169 {
170 UpdateDisplayInfo();
171 if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
172 TLOGE(WmsLogTag::DMS, "pImpl_ or pImpl_->GetDisplayInfo is nullptr");
173 return Orientation::UNSPECIFIED;
174 }
175 return pImpl_->GetDisplayInfo()->GetOrientation();
176 }
177
UpdateDisplayInfo(sptr<DisplayInfo> displayInfo) const178 void Display::UpdateDisplayInfo(sptr<DisplayInfo> displayInfo) const
179 {
180 if (displayInfo == nullptr) {
181 TLOGE(WmsLogTag::DMS, "displayInfo is invalid");
182 return;
183 }
184 if (pImpl_ == nullptr) {
185 TLOGE(WmsLogTag::DMS, "pImpl_ is nullptr");
186 return;
187 }
188 pImpl_->SetDisplayInfo(displayInfo);
189 }
190
UpdateDisplayInfo() const191 void Display::UpdateDisplayInfo() const
192 {
193 auto displayInfo = SingletonContainer::Get<DisplayManagerAdapter>().GetDisplayInfo(GetId());
194 UpdateDisplayInfo(displayInfo);
195 }
196
GetVirtualPixelRatio() const197 float Display::GetVirtualPixelRatio() const
198 {
199 UpdateDisplayInfo();
200 if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
201 TLOGE(WmsLogTag::DMS, "pImpl_ or pImpl_->GetDisplayInfo is nullptr");
202 return 0;
203 }
204 return pImpl_->GetDisplayInfo()->GetVirtualPixelRatio();
205 }
206
GetDpi() const207 int Display::GetDpi() const
208 {
209 return static_cast<int>(GetVirtualPixelRatio() * DOT_PER_INCH);
210 }
211
GetDisplayInfo() const212 sptr<DisplayInfo> Display::GetDisplayInfo() const
213 {
214 UpdateDisplayInfo();
215 if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
216 TLOGE(WmsLogTag::DMS, "pImpl_ or pImpl_->GetDisplayInfo is nullptr");
217 return nullptr;
218 }
219 return pImpl_->GetDisplayInfo();
220 }
221
GetDisplayInfoWithCache() const222 sptr<DisplayInfo> Display::GetDisplayInfoWithCache() const
223 {
224 if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
225 TLOGE(WmsLogTag::DMS, "pImpl_ or pImpl_->GetDisplayInfo is nullptr");
226 return nullptr;
227 }
228 return pImpl_->GetDisplayInfo();
229 }
230
GetCutoutInfo() const231 sptr<CutoutInfo> Display::GetCutoutInfo() const
232 {
233 return SingletonContainer::Get<DisplayManagerAdapter>().GetCutoutInfo(GetId(), GetWidth(),
234 GetHeight(), GetOriginRotation());
235 }
236
HasImmersiveWindow(bool & immersive)237 DMError Display::HasImmersiveWindow(bool& immersive)
238 {
239 return SingletonContainer::Get<DisplayManagerAdapter>().HasImmersiveWindow(GetScreenId(), immersive);
240 }
241
GetAvailableArea(DMRect & area) const242 DMError Display::GetAvailableArea(DMRect& area) const
243 {
244 return SingletonContainer::Get<DisplayManagerAdapter>().GetAvailableArea(GetId(), area);
245 }
246
GetSupportedHDRFormats(std::vector<uint32_t> & hdrFormats) const247 DMError Display::GetSupportedHDRFormats(std::vector<uint32_t>& hdrFormats) const
248 {
249 return SingletonContainer::Get<ScreenManagerAdapter>().GetSupportedHDRFormats(GetScreenId(), hdrFormats);
250 }
251
GetSupportedColorSpaces(std::vector<uint32_t> & colorSpaces) const252 DMError Display::GetSupportedColorSpaces(std::vector<uint32_t>& colorSpaces) const
253 {
254 return SingletonContainer::Get<ScreenManagerAdapter>().GetSupportedColorSpaces(GetScreenId(), colorSpaces);
255 }
256
GetDisplayCapability(std::string & capabilitInfo) const257 DMError Display::GetDisplayCapability(std::string& capabilitInfo) const
258 {
259 return SingletonContainer::Get<DisplayManagerAdapter>().GetDisplayCapability(capabilitInfo);
260 }
261
GetLiveCreaseRegion(FoldCreaseRegion & region) const262 DMError Display::GetLiveCreaseRegion(FoldCreaseRegion& region) const
263 {
264 ScreenId screenId = GetScreenId();
265 ScreenId rsScreenId;
266 bool ret = SingletonContainer::Get<DisplayManagerAdapter>().ConvertScreenIdToRsScreenId(screenId, rsScreenId);
267 if (!ret) {
268 TLOGE(WmsLogTag::DMS, "convertScreenIdToRsScreenId falied");
269 return DMError::DM_ERROR_DEVICE_NOT_SUPPORT;
270 }
271 // when rsScreenId is not 0, there is no crease region in the current screen
272 if (rsScreenId == MAIN_SCREEN_ID_DEFAULT) {
273 return SingletonContainer::Get<DisplayManagerAdapter>().GetLiveCreaseRegion(region);
274 }
275 region = FoldCreaseRegion(screenId, {});
276 return DMError::DM_OK;
277 }
278 } // namespace OHOS::Rosen
279