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 #include "display.h"
16 
17 #include <cstdint>
18 #include <new>
19 #include <refbase.h>
20 
21 #include "class_var_definition.h"
22 #include "display_info.h"
23 #include "display_manager_adapter.h"
24 #include "dm_common.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, "Display"};
31 }
32 class Display::Impl : public RefBase {
33 public:
Impl(const std::string & name,sptr<DisplayInfo> info)34     Impl(const std::string& name, sptr<DisplayInfo> info)
35     {
36         name_= name;
37         displayInfo_ = info;
38     }
39     ~Impl() = default;
40     DEFINE_VAR_FUNC_GET_SET(std::string, Name, name);
GetDisplayInfo()41     sptr<DisplayInfo> GetDisplayInfo()
42     {
43         std::lock_guard<std::mutex> lock(displayInfoMutex_);
44         return displayInfo_;
45     }
46 
SetDisplayInfo(sptr<DisplayInfo> value)47     void SetDisplayInfo(sptr<DisplayInfo> value)
48     {
49         std::lock_guard<std::mutex> lock(displayInfoMutex_);
50         displayInfo_ = value;
51     }
52 
53 private:
54     sptr<DisplayInfo> displayInfo_;
55     std::mutex displayInfoMutex_;
56 };
57 
Display(const std::string & name,sptr<DisplayInfo> info)58 Display::Display(const std::string& name, sptr<DisplayInfo> info)
59     : pImpl_(new Impl(name, info))
60 {
61 }
62 
~Display()63 Display::~Display()
64 {
65 }
66 
GetId() const67 DisplayId Display::GetId() const
68 {
69     if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
70         WLOGFE("pImpl_ or pImpl_->GetDisplayInfo is nullptr");
71         return DisplayId(0);
72     }
73     return pImpl_->GetDisplayInfo()->GetDisplayId();
74 }
75 
GetName() const76 std::string Display::GetName() const
77 {
78     if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
79         WLOGFE("pImpl_ or pImpl_->GetDisplayInfo is nullptr");
80         return std::string();
81     }
82     return pImpl_->GetDisplayInfo()->GetName();
83 }
84 
GetWidth() const85 int32_t Display::GetWidth() const
86 {
87     UpdateDisplayInfo();
88     if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
89         WLOGFE("pImpl_ or pImpl_->GetDisplayInfo is nullptr");
90         return 0;
91     }
92     return pImpl_->GetDisplayInfo()->GetWidth();
93 }
94 
GetHeight() const95 int32_t Display::GetHeight() const
96 {
97     UpdateDisplayInfo();
98     if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
99         WLOGFE("pImpl_ or pImpl_->GetDisplayInfo is nullptr");
100         return 0;
101     }
102     return pImpl_->GetDisplayInfo()->GetHeight();
103 }
104 
GetPhysicalWidth() const105 int32_t Display::GetPhysicalWidth() const
106 {
107     UpdateDisplayInfo();
108     if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
109         WLOGFE("pImpl_ or pImpl_->GetDisplayInfo is nullptr");
110         return 0;
111     }
112     return pImpl_->GetDisplayInfo()->GetPhysicalWidth();
113 }
114 
GetPhysicalHeight() const115 int32_t Display::GetPhysicalHeight() const
116 {
117     UpdateDisplayInfo();
118     if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
119         WLOGFE("pImpl_ or pImpl_->GetDisplayInfo is nullptr");
120         return 0;
121     }
122     return pImpl_->GetDisplayInfo()->GetPhysicalHeight();
123 }
124 
GetRefreshRate() const125 uint32_t Display::GetRefreshRate() const
126 {
127     UpdateDisplayInfo();
128     if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
129         WLOGFE("pImpl_ or pImpl_->GetDisplayInfo is nullptr");
130         return 0;
131     }
132     return pImpl_->GetDisplayInfo()->GetRefreshRate();
133 }
134 
GetScreenId() const135 ScreenId Display::GetScreenId() const
136 {
137     UpdateDisplayInfo();
138     if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
139         WLOGFE("pImpl_ or pImpl_->GetDisplayInfo is nullptr");
140         return SCREEN_ID_INVALID;
141     }
142     return pImpl_->GetDisplayInfo()->GetScreenId();
143 }
144 
GetRotation() const145 Rotation Display::GetRotation() const
146 {
147     UpdateDisplayInfo();
148     if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
149         WLOGFE("pImpl_ or pImpl_->GetDisplayInfo is nullptr");
150         return Rotation::ROTATION_0;
151     }
152     return pImpl_->GetDisplayInfo()->GetRotation();
153 }
154 
GetOrientation() const155 Orientation Display::GetOrientation() const
156 {
157     UpdateDisplayInfo();
158     if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
159         WLOGFE("pImpl_ or pImpl_->GetDisplayInfo is nullptr");
160         return Orientation::UNSPECIFIED;
161     }
162     return pImpl_->GetDisplayInfo()->GetOrientation();
163 }
164 
UpdateDisplayInfo(sptr<DisplayInfo> displayInfo) const165 void Display::UpdateDisplayInfo(sptr<DisplayInfo> displayInfo) const
166 {
167     if (displayInfo == nullptr) {
168         WLOGFE("displayInfo is invalid");
169         return;
170     }
171     if (pImpl_ == nullptr) {
172         WLOGFE("pImpl_ is nullptr");
173         return;
174     }
175     pImpl_->SetDisplayInfo(displayInfo);
176 }
177 
UpdateDisplayInfo() const178 void Display::UpdateDisplayInfo() const
179 {
180     auto displayInfo = SingletonContainer::Get<DisplayManagerAdapter>().GetDisplayInfo(GetId());
181     UpdateDisplayInfo(displayInfo);
182 }
183 
GetVirtualPixelRatio() const184 float Display::GetVirtualPixelRatio() const
185 {
186     UpdateDisplayInfo();
187     if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
188         WLOGFE("pImpl_ or pImpl_->GetDisplayInfo is nullptr");
189         return 0;
190     }
191     return pImpl_->GetDisplayInfo()->GetVirtualPixelRatio();
192 }
193 
GetDpi() const194 int Display::GetDpi() const
195 {
196     return static_cast<int>(GetVirtualPixelRatio() * DOT_PER_INCH);
197 }
198 
GetDisplayInfo() const199 sptr<DisplayInfo> Display::GetDisplayInfo() const
200 {
201     UpdateDisplayInfo();
202     if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
203         WLOGFE("pImpl_ or pImpl_->GetDisplayInfo is nullptr");
204         return nullptr;
205     }
206     return pImpl_->GetDisplayInfo();
207 }
208 
GetDisplayInfoByJs() const209 sptr<DisplayInfo> Display::GetDisplayInfoByJs() const
210 {
211     if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
212         WLOGFE("pImpl_ or pImpl_->GetDisplayInfo is nullptr");
213         return nullptr;
214     }
215     return pImpl_->GetDisplayInfo();
216 }
217 
GetCutoutInfo() const218 sptr<CutoutInfo> Display::GetCutoutInfo() const
219 {
220     return SingletonContainer::Get<DisplayManagerAdapter>().GetCutoutInfo(GetId());
221 }
222 
HasImmersiveWindow(bool & immersive)223 DMError Display::HasImmersiveWindow(bool& immersive)
224 {
225     return SingletonContainer::Get<DisplayManagerAdapter>().HasImmersiveWindow(immersive);
226 }
227 
GetAvailableArea(DMRect & area) const228 DMError Display::GetAvailableArea(DMRect& area) const
229 {
230     return SingletonContainer::Get<DisplayManagerAdapter>().GetAvailableArea(GetId(), area);
231 }
232 
GetSupportedHDRFormats(std::vector<uint32_t> & hdrFormats) const233 DMError Display::GetSupportedHDRFormats(std::vector<uint32_t>& hdrFormats) const
234 {
235     return SingletonContainer::Get<ScreenManagerAdapter>().GetSupportedHDRFormats(GetScreenId(), hdrFormats);
236 }
237 
GetSupportedColorSpaces(std::vector<uint32_t> & colorSpaces) const238 DMError Display::GetSupportedColorSpaces(std::vector<uint32_t>& colorSpaces) const
239 {
240     return SingletonContainer::Get<ScreenManagerAdapter>().GetSupportedColorSpaces(GetScreenId(), colorSpaces);
241 }
242 
243 } // namespace OHOS::Rosen
244