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 "singleton_container.h"
25 #include "window_manager_hilog.h"
26
27 namespace OHOS::Rosen {
28 namespace {
29 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_DISPLAY, "Display"};
30 }
31 class Display::Impl : public RefBase {
32 public:
Impl(const std::string & name,sptr<DisplayInfo> info)33 Impl(const std::string& name, sptr<DisplayInfo> info)
34 {
35 name_= name;
36 displayInfo_ = info;
37 }
38 ~Impl() = default;
39 DEFINE_VAR_FUNC_GET_SET(std::string, Name, name);
40 DEFINE_VAR_FUNC_GET_SET_WITH_LOCK(sptr<DisplayInfo>, DisplayInfo, displayInfo);
41 };
42
Display(const std::string & name,sptr<DisplayInfo> info)43 Display::Display(const std::string& name, sptr<DisplayInfo> info)
44 : pImpl_(new Impl(name, info))
45 {
46 }
47
~Display()48 Display::~Display()
49 {
50 }
51
GetId() const52 DisplayId Display::GetId() const
53 {
54 return pImpl_->GetDisplayInfo()->GetDisplayId();
55 }
56
GetName() const57 std::string Display::GetName() const
58 {
59 return pImpl_->GetDisplayInfo()->GetName();
60 }
61
GetWidth() const62 int32_t Display::GetWidth() const
63 {
64 UpdateDisplayInfo();
65 return pImpl_->GetDisplayInfo()->GetWidth();
66 }
67
GetHeight() const68 int32_t Display::GetHeight() const
69 {
70 UpdateDisplayInfo();
71 return pImpl_->GetDisplayInfo()->GetHeight();
72 }
73
GetRefreshRate() const74 uint32_t Display::GetRefreshRate() const
75 {
76 UpdateDisplayInfo();
77 return pImpl_->GetDisplayInfo()->GetRefreshRate();
78 }
79
GetScreenId() const80 ScreenId Display::GetScreenId() const
81 {
82 UpdateDisplayInfo();
83 return pImpl_->GetDisplayInfo()->GetScreenId();
84 }
85
GetRotation() const86 Rotation Display::GetRotation() const
87 {
88 UpdateDisplayInfo();
89 return pImpl_->GetDisplayInfo()->GetRotation();
90 }
91
GetOrientation() const92 Orientation Display::GetOrientation() const
93 {
94 UpdateDisplayInfo();
95 return pImpl_->GetDisplayInfo()->GetOrientation();
96 }
97
UpdateDisplayInfo(sptr<DisplayInfo> displayInfo) const98 void Display::UpdateDisplayInfo(sptr<DisplayInfo> displayInfo) const
99 {
100 if (displayInfo == nullptr) {
101 WLOGFE("displayInfo is invalid");
102 return;
103 }
104 pImpl_->SetDisplayInfo(displayInfo);
105 }
106
UpdateDisplayInfo() const107 void Display::UpdateDisplayInfo() const
108 {
109 auto displayInfo = SingletonContainer::Get<DisplayManagerAdapter>().GetDisplayInfo(GetId());
110 UpdateDisplayInfo(displayInfo);
111 }
112
GetVirtualPixelRatio() const113 float Display::GetVirtualPixelRatio() const
114 {
115 UpdateDisplayInfo();
116 return pImpl_->GetDisplayInfo()->GetVirtualPixelRatio();
117 }
118
GetDpi() const119 int Display::GetDpi() const
120 {
121 return static_cast<int>(GetVirtualPixelRatio() * DOT_PER_INCH);
122 }
123
GetDisplayInfo() const124 sptr<DisplayInfo> Display::GetDisplayInfo() const
125 {
126 return pImpl_->GetDisplayInfo();
127 }
128
GetCutoutInfo() const129 sptr<CutoutInfo> Display::GetCutoutInfo() const
130 {
131 return SingletonContainer::Get<DisplayManagerAdapter>().GetCutoutInfo(GetId());
132 }
133 } // namespace OHOS::Rosen
134