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 namespace {
31 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_DISPLAY, "Display"};
32 }
33 class Display::Impl : public RefBase {
34 public:
Impl(const std::string & name,sptr<DisplayInfo> info)35 Impl(const std::string& name, sptr<DisplayInfo> info)
36 {
37 name_= name;
38 displayInfo_ = info;
39 }
40 ~Impl() = default;
41 DEFINE_VAR_FUNC_GET_SET(std::string, Name, name);
GetDisplayInfo()42 sptr<DisplayInfo> GetDisplayInfo()
43 {
44 std::lock_guard<std::mutex> lock(displayInfoMutex_);
45 return displayInfo_;
46 }
47
SetDisplayInfo(sptr<DisplayInfo> value)48 void SetDisplayInfo(sptr<DisplayInfo> value)
49 {
50 std::lock_guard<std::mutex> lock(displayInfoMutex_);
51 displayInfo_ = value;
52 }
53
54 private:
55 sptr<DisplayInfo> displayInfo_;
56 std::mutex displayInfoMutex_;
57 };
58
Display(const std::string & name,sptr<DisplayInfo> info)59 Display::Display(const std::string& name, sptr<DisplayInfo> info)
60 : pImpl_(new Impl(name, info))
61 {
62 }
63
~Display()64 Display::~Display()
65 {
66 }
67
GetId() const68 DisplayId Display::GetId() const
69 {
70 if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
71 WLOGFE("pImpl_ or pImpl_->GetDisplayInfo is nullptr");
72 return DisplayId(0);
73 }
74 return pImpl_->GetDisplayInfo()->GetDisplayId();
75 }
76
GetName() const77 std::string Display::GetName() const
78 {
79 if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
80 WLOGFE("pImpl_ or pImpl_->GetDisplayInfo is nullptr");
81 return std::string();
82 }
83 return pImpl_->GetDisplayInfo()->GetName();
84 }
85
GetWidth() const86 int32_t Display::GetWidth() const
87 {
88 UpdateDisplayInfo();
89 if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
90 WLOGFE("pImpl_ or pImpl_->GetDisplayInfo is nullptr");
91 return 0;
92 }
93 return pImpl_->GetDisplayInfo()->GetWidth();
94 }
95
GetHeight() const96 int32_t Display::GetHeight() const
97 {
98 UpdateDisplayInfo();
99 if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
100 WLOGFE("pImpl_ or pImpl_->GetDisplayInfo is nullptr");
101 return 0;
102 }
103 return pImpl_->GetDisplayInfo()->GetHeight();
104 }
105
GetPhysicalWidth() const106 int32_t Display::GetPhysicalWidth() const
107 {
108 UpdateDisplayInfo();
109 if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
110 WLOGFE("pImpl_ or pImpl_->GetDisplayInfo is nullptr");
111 return 0;
112 }
113 return pImpl_->GetDisplayInfo()->GetPhysicalWidth();
114 }
115
GetPhysicalHeight() const116 int32_t Display::GetPhysicalHeight() const
117 {
118 UpdateDisplayInfo();
119 if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
120 WLOGFE("pImpl_ or pImpl_->GetDisplayInfo is nullptr");
121 return 0;
122 }
123 return pImpl_->GetDisplayInfo()->GetPhysicalHeight();
124 }
125
GetRefreshRate() const126 uint32_t Display::GetRefreshRate() const
127 {
128 UpdateDisplayInfo();
129 if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
130 WLOGFE("pImpl_ or pImpl_->GetDisplayInfo is nullptr");
131 return 0;
132 }
133 return pImpl_->GetDisplayInfo()->GetRefreshRate();
134 }
135
GetScreenId() const136 ScreenId Display::GetScreenId() const
137 {
138 UpdateDisplayInfo();
139 if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
140 WLOGFE("pImpl_ or pImpl_->GetDisplayInfo is nullptr");
141 return SCREEN_ID_INVALID;
142 }
143 return pImpl_->GetDisplayInfo()->GetScreenId();
144 }
145
GetRotation() const146 Rotation Display::GetRotation() const
147 {
148 UpdateDisplayInfo();
149 if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
150 WLOGFE("pImpl_ or pImpl_->GetDisplayInfo is nullptr");
151 return Rotation::ROTATION_0;
152 }
153 return pImpl_->GetDisplayInfo()->GetRotation();
154 }
155
GetOrientation() const156 Orientation Display::GetOrientation() const
157 {
158 UpdateDisplayInfo();
159 if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
160 WLOGFE("pImpl_ or pImpl_->GetDisplayInfo is nullptr");
161 return Orientation::UNSPECIFIED;
162 }
163 return pImpl_->GetDisplayInfo()->GetOrientation();
164 }
165
UpdateDisplayInfo(sptr<DisplayInfo> displayInfo) const166 void Display::UpdateDisplayInfo(sptr<DisplayInfo> displayInfo) const
167 {
168 if (displayInfo == nullptr) {
169 WLOGFE("displayInfo is invalid");
170 return;
171 }
172 if (pImpl_ == nullptr) {
173 WLOGFE("pImpl_ is nullptr");
174 return;
175 }
176 pImpl_->SetDisplayInfo(displayInfo);
177 }
178
UpdateDisplayInfo() const179 void Display::UpdateDisplayInfo() const
180 {
181 auto displayInfo = SingletonContainer::Get<DisplayManagerAdapter>().GetDisplayInfo(GetId());
182 UpdateDisplayInfo(displayInfo);
183 }
184
GetVirtualPixelRatio() const185 float Display::GetVirtualPixelRatio() const
186 {
187 UpdateDisplayInfo();
188 if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
189 WLOGFE("pImpl_ or pImpl_->GetDisplayInfo is nullptr");
190 return 0;
191 }
192 return pImpl_->GetDisplayInfo()->GetVirtualPixelRatio();
193 }
194
GetDpi() const195 int Display::GetDpi() const
196 {
197 return static_cast<int>(GetVirtualPixelRatio() * DOT_PER_INCH);
198 }
199
GetDisplayInfo() const200 sptr<DisplayInfo> Display::GetDisplayInfo() const
201 {
202 UpdateDisplayInfo();
203 if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
204 WLOGFE("pImpl_ or pImpl_->GetDisplayInfo is nullptr");
205 return nullptr;
206 }
207 return pImpl_->GetDisplayInfo();
208 }
209
GetDisplayInfoWithCache() const210 sptr<DisplayInfo> Display::GetDisplayInfoWithCache() const
211 {
212 if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
213 WLOGFE("pImpl_ or pImpl_->GetDisplayInfo is nullptr");
214 return nullptr;
215 }
216 return pImpl_->GetDisplayInfo();
217 }
218
GetCutoutInfo() const219 sptr<CutoutInfo> Display::GetCutoutInfo() const
220 {
221 return SingletonContainer::Get<DisplayManagerAdapter>().GetCutoutInfo(GetId());
222 }
223
HasImmersiveWindow(bool & immersive)224 DMError Display::HasImmersiveWindow(bool& immersive)
225 {
226 return SingletonContainer::Get<DisplayManagerAdapter>().HasImmersiveWindow(GetScreenId(), immersive);
227 }
228
GetSupportedHDRFormats(std::vector<uint32_t> & hdrFormats) const229 DMError Display::GetSupportedHDRFormats(std::vector<uint32_t>& hdrFormats) const
230 {
231 return SingletonContainer::Get<ScreenManagerAdapter>().GetSupportedHDRFormats(GetScreenId(), hdrFormats);
232 }
233
GetSupportedColorSpaces(std::vector<uint32_t> & colorSpaces) const234 DMError Display::GetSupportedColorSpaces(std::vector<uint32_t>& colorSpaces) const
235 {
236 return SingletonContainer::Get<ScreenManagerAdapter>().GetSupportedColorSpaces(GetScreenId(), colorSpaces);
237 }
238
GetAvailableArea(DMRect & area) const239 DMError Display::GetAvailableArea(DMRect& area) const
240 {
241 return SingletonContainer::Get<DisplayManagerAdapter>().GetAvailableArea(GetId(), area);
242 }
243
GetDisplayCapability(std::string & capabilitInfo) const244 DMError Display::GetDisplayCapability(std::string& capabilitInfo) const
245 {
246 return SingletonContainer::Get<DisplayManagerAdapter>().GetDisplayCapability(capabilitInfo);
247 }
248
249 } // namespace OHOS::Rosen
250