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
28namespace OHOS::Rosen {
29namespace {
30constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_DISPLAY, "Display"};
31}
32class Display::Impl : public RefBase {
33public:
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);
41    DEFINE_VAR_FUNC_GET_SET_WITH_LOCK(sptr<DisplayInfo>, DisplayInfo, displayInfo);
42};
43
44Display::Display(const std::string& name, sptr<DisplayInfo> info)
45    : pImpl_(new Impl(name, info))
46{
47}
48
49Display::~Display()
50{
51}
52
53DisplayId Display::GetId() const
54{
55    if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
56        WLOGFE("pImpl_ or pImpl_->GetDisplayInfo is nullptr");
57        return DisplayId(0);
58    }
59    return pImpl_->GetDisplayInfo()->GetDisplayId();
60}
61
62std::string Display::GetName() const
63{
64    if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
65        WLOGFE("pImpl_ or pImpl_->GetDisplayInfo is nullptr");
66        return std::string();
67    }
68    return pImpl_->GetDisplayInfo()->GetName();
69}
70
71int32_t Display::GetWidth() const
72{
73    UpdateDisplayInfo();
74    if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
75        WLOGFE("pImpl_ or pImpl_->GetDisplayInfo is nullptr");
76        return 0;
77    }
78    return pImpl_->GetDisplayInfo()->GetWidth();
79}
80
81int32_t Display::GetHeight() const
82{
83    UpdateDisplayInfo();
84    if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
85        WLOGFE("pImpl_ or pImpl_->GetDisplayInfo is nullptr");
86        return 0;
87    }
88    return pImpl_->GetDisplayInfo()->GetHeight();
89}
90
91int32_t Display::GetPhysicalWidth() const
92{
93    UpdateDisplayInfo();
94    if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
95        WLOGFE("pImpl_ or pImpl_->GetDisplayInfo is nullptr");
96        return 0;
97    }
98    return pImpl_->GetDisplayInfo()->GetPhysicalWidth();
99}
100
101int32_t Display::GetPhysicalHeight() const
102{
103    UpdateDisplayInfo();
104    if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
105        WLOGFE("pImpl_ or pImpl_->GetDisplayInfo is nullptr");
106        return 0;
107    }
108    return pImpl_->GetDisplayInfo()->GetPhysicalHeight();
109}
110
111uint32_t Display::GetRefreshRate() const
112{
113    UpdateDisplayInfo();
114    if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
115        WLOGFE("pImpl_ or pImpl_->GetDisplayInfo is nullptr");
116        return 0;
117    }
118    return pImpl_->GetDisplayInfo()->GetRefreshRate();
119}
120
121ScreenId Display::GetScreenId() const
122{
123    UpdateDisplayInfo();
124    if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
125        WLOGFE("pImpl_ or pImpl_->GetDisplayInfo is nullptr");
126        return SCREEN_ID_INVALID;
127    }
128    return pImpl_->GetDisplayInfo()->GetScreenId();
129}
130
131Rotation Display::GetRotation() const
132{
133    UpdateDisplayInfo();
134    if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
135        WLOGFE("pImpl_ or pImpl_->GetDisplayInfo is nullptr");
136        return Rotation::ROTATION_0;
137    }
138    return pImpl_->GetDisplayInfo()->GetRotation();
139}
140
141Orientation Display::GetOrientation() const
142{
143    UpdateDisplayInfo();
144    if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
145        WLOGFE("pImpl_ or pImpl_->GetDisplayInfo is nullptr");
146        return Orientation::UNSPECIFIED;
147    }
148    return pImpl_->GetDisplayInfo()->GetOrientation();
149}
150
151void Display::UpdateDisplayInfo(sptr<DisplayInfo> displayInfo) const
152{
153    if (displayInfo == nullptr) {
154        WLOGFE("displayInfo is invalid");
155        return;
156    }
157    if (pImpl_ == nullptr) {
158        WLOGFE("pImpl_ is nullptr");
159        return;
160    }
161    pImpl_->SetDisplayInfo(displayInfo);
162}
163
164void Display::UpdateDisplayInfo() const
165{
166    auto displayInfo = SingletonContainer::Get<DisplayManagerAdapter>().GetDisplayInfo(GetId());
167    UpdateDisplayInfo(displayInfo);
168}
169
170float Display::GetVirtualPixelRatio() const
171{
172    UpdateDisplayInfo();
173    if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
174        WLOGFE("pImpl_ or pImpl_->GetDisplayInfo is nullptr");
175        return 0;
176    }
177    return pImpl_->GetDisplayInfo()->GetVirtualPixelRatio();
178}
179
180int Display::GetDpi() const
181{
182    return static_cast<int>(GetVirtualPixelRatio() * DOT_PER_INCH);
183}
184
185sptr<DisplayInfo> Display::GetDisplayInfo() const
186{
187    UpdateDisplayInfo();
188    if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
189        WLOGFE("pImpl_ or pImpl_->GetDisplayInfo is nullptr");
190        return nullptr;
191    }
192    return pImpl_->GetDisplayInfo();
193}
194
195sptr<DisplayInfo> Display::GetDisplayInfoByJs() const
196{
197    if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
198        WLOGFE("pImpl_ or pImpl_->GetDisplayInfo is nullptr");
199        return nullptr;
200    }
201    return pImpl_->GetDisplayInfo();
202}
203
204sptr<CutoutInfo> Display::GetCutoutInfo() const
205{
206    return SingletonContainer::Get<DisplayManagerAdapter>().GetCutoutInfo(GetId());
207}
208
209DMError Display::HasImmersiveWindow(bool& immersive)
210{
211    return SingletonContainer::Get<DisplayManagerAdapter>().HasImmersiveWindow(GetScreenId(), immersive);
212}
213
214DMError Display::GetSupportedHDRFormats(std::vector<uint32_t>& hdrFormats) const
215{
216    return SingletonContainer::Get<ScreenManagerAdapter>().GetSupportedHDRFormats(GetScreenId(), hdrFormats);
217}
218
219DMError Display::GetSupportedColorSpaces(std::vector<uint32_t>& colorSpaces) const
220{
221    return SingletonContainer::Get<ScreenManagerAdapter>().GetSupportedColorSpaces(GetScreenId(), colorSpaces);
222}
223
224DMError Display::GetAvailableArea(DMRect& area) const
225{
226    return SingletonContainer::Get<DisplayManagerAdapter>().GetAvailableArea(GetId(), area);
227}
228
229} // namespace OHOS::Rosen
230