• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "abstract_display.h"
17 
18 #include "abstract_screen_controller.h"
19 #include "display_manager_config.h"
20 #include "display_manager_service.h"
21 #include "window_manager_hilog.h"
22 
23 namespace OHOS::Rosen {
24 namespace {
25     constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_DISPLAY, "AbstractDisplay"};
26     constexpr int32_t PAD_SCREEN_WIDTH = 2560;
27     constexpr int32_t PHONE_SCREEN_WIDTH = 2160;
28     constexpr float INCH_2_MM = 25.4f;
29 }
30 
AbstractDisplay(DisplayId id,sptr<SupportedScreenModes> & info,sptr<AbstractScreen> & absScreen)31 AbstractDisplay::AbstractDisplay(DisplayId id, sptr<SupportedScreenModes>& info, sptr<AbstractScreen>& absScreen)
32     : id_(id),
33       screenId_(absScreen->dmsId_),
34       screenGroupId_(absScreen->groupDmsId_),
35       width_(info->width_),
36       height_(info->height_),
37       refreshRate_(info->refreshRate_),
38       orientation_(absScreen->orientation_)
39 {
40     name_ = absScreen->GetScreenName();
41     RequestRotation(absScreen->rotation_);
42     if (width_ > height_) {
43         displayOrientation_ = DisplayOrientation::LANDSCAPE;
44     } else {
45         displayOrientation_ = DisplayOrientation::PORTRAIT;
46     }
47     if (info->width_ < info->height_) {
48         isDefaultVertical_ = true;
49     } else {
50         isDefaultVertical_ = false;
51     }
52 
53     CalculateXYDpi(absScreen->GetPhyWidth(), absScreen->GetPhyHeight());
54     auto numbersConfig = DisplayManagerConfig::GetIntNumbersConfig();
55     if (numbersConfig.count("dpi") != 0) {
56         uint32_t densityDpi = static_cast<uint32_t>(numbersConfig["dpi"][0]);
57         if (densityDpi >= DOT_PER_INCH_MINIMUM_VALUE && densityDpi <= DOT_PER_INCH_MAXIMUM_VALUE) {
58             virtualPixelRatio_ = static_cast<float>(densityDpi) / BASELINE_DENSITY;
59             absScreen->SetVirtualPixelRatio(virtualPixelRatio_);
60             return;
61         }
62     }
63     if ((info->width_ >= PHONE_SCREEN_WIDTH) || (info->height_ >= PHONE_SCREEN_WIDTH)) {
64         if ((info->width_ == PAD_SCREEN_WIDTH) || (info->height_ == PAD_SCREEN_WIDTH)) {
65             virtualPixelRatio_ = 2.0f; // Pad is 2.0
66         } else {
67             virtualPixelRatio_ = 3.0f; // Phone is 3.0
68         }
69     } else {
70         virtualPixelRatio_ = 1.0f; // Other is 1.0
71     }
72     absScreen->SetVirtualPixelRatio(virtualPixelRatio_);
73 }
74 
CalculateXYDpi(uint32_t phyWidth,uint32_t phyHeight)75 void AbstractDisplay::CalculateXYDpi(uint32_t phyWidth, uint32_t phyHeight)
76 {
77     if (phyWidth == 0 || phyHeight == 0) {
78         return;
79     }
80 
81     phyWidth_ = phyWidth;
82     phyHeight_ = phyHeight;
83     xDpi_ = width_ * INCH_2_MM / phyWidth_;
84     yDpi_ = height_ * INCH_2_MM / phyHeight_;
85 }
86 
GetId() const87 DisplayId AbstractDisplay::GetId() const
88 {
89     return id_;
90 }
91 
GetWidth() const92 int32_t AbstractDisplay::GetWidth() const
93 {
94     return width_;
95 }
96 
GetHeight() const97 int32_t AbstractDisplay::GetHeight() const
98 {
99     return height_;
100 }
101 
GetRefreshRate() const102 uint32_t AbstractDisplay::GetRefreshRate() const
103 {
104     return refreshRate_;
105 }
106 
GetVirtualPixelRatio() const107 float AbstractDisplay::GetVirtualPixelRatio() const
108 {
109     return virtualPixelRatio_;
110 }
111 
GetOffsetX() const112 int32_t AbstractDisplay::GetOffsetX() const
113 {
114     return offsetX_;
115 }
116 
GetOffsetY() const117 int32_t AbstractDisplay::GetOffsetY() const
118 {
119     return offsetY_;
120 }
121 
SetOffsetX(int32_t offsetX)122 void AbstractDisplay::SetOffsetX(int32_t offsetX)
123 {
124     offsetX_ = offsetX;
125 }
126 
SetOffsetY(int32_t offsetY)127 void AbstractDisplay::SetOffsetY(int32_t offsetY)
128 {
129     offsetY_ = offsetY;
130 }
131 
SetWidth(int32_t width)132 void AbstractDisplay::SetWidth(int32_t width)
133 {
134     width_ = width;
135     UpdateXDpi();
136 }
137 
SetHeight(int32_t height)138 void AbstractDisplay::SetHeight(int32_t height)
139 {
140     height_ = height;
141     UpdateYDpi();
142 }
143 
UpdateXDpi()144 void AbstractDisplay::UpdateXDpi()
145 {
146     if (phyWidth_ != UINT32_MAX) {
147         xDpi_ = width_ * INCH_2_MM / phyWidth_;
148     }
149 }
150 
UpdateYDpi()151 void AbstractDisplay::UpdateYDpi()
152 {
153     if (phyHeight_ != UINT32_MAX) {
154         yDpi_ = height_ * INCH_2_MM / phyHeight_;
155     }
156 }
157 
SetOffset(int32_t offsetX,int32_t offsetY)158 void AbstractDisplay::SetOffset(int32_t offsetX, int32_t offsetY)
159 {
160     offsetX_ = offsetX;
161     offsetY_ = offsetY;
162 }
163 
SetRefreshRate(uint32_t refreshRate)164 void AbstractDisplay::SetRefreshRate(uint32_t refreshRate)
165 {
166     refreshRate_ = refreshRate;
167 }
168 
SetVirtualPixelRatio(float virtualPixelRatio)169 void AbstractDisplay::SetVirtualPixelRatio(float virtualPixelRatio)
170 {
171     virtualPixelRatio_ = virtualPixelRatio;
172 }
173 
SetId(DisplayId id)174 void AbstractDisplay::SetId(DisplayId id)
175 {
176     id_ = id;
177 }
178 
SetOrientation(Orientation orientation)179 void AbstractDisplay::SetOrientation(Orientation orientation)
180 {
181     orientation_ = orientation;
182 }
183 
SetDisplayOrientation(DisplayOrientation displayOrientation)184 void AbstractDisplay::SetDisplayOrientation(DisplayOrientation displayOrientation)
185 {
186     displayOrientation_ = displayOrientation;
187 }
188 
RequestRotation(Rotation rotation)189 bool AbstractDisplay::RequestRotation(Rotation rotation)
190 {
191     WLOGD("request rotation from %{public}u to %{public}u, display %{public}" PRIu64"", rotation_, rotation, id_);
192     if (rotation_ == rotation) {
193         WLOGFE("rotation not change %{public}u", rotation);
194         return false;
195     }
196     if (IsVertical(rotation) != IsVertical(rotation_)) {
197         std::swap(width_, height_);
198     }
199     rotation_ = rotation;
200     return true;
201 }
202 
GetRotation() const203 Rotation AbstractDisplay::GetRotation() const
204 {
205     return rotation_;
206 }
207 
GetOrientation() const208 Orientation AbstractDisplay::GetOrientation() const
209 {
210     return orientation_;
211 }
212 
GetDisplayOrientation() const213 DisplayOrientation AbstractDisplay::GetDisplayOrientation() const
214 {
215     return displayOrientation_;
216 }
217 
SetFreezeFlag(FreezeFlag freezeFlag)218 void AbstractDisplay::SetFreezeFlag(FreezeFlag freezeFlag)
219 {
220     freezeFlag_ = freezeFlag;
221 }
222 
GetFreezeFlag() const223 FreezeFlag AbstractDisplay::GetFreezeFlag() const
224 {
225     return freezeFlag_;
226 }
227 
BindAbstractScreen(sptr<AbstractScreen> abstractScreen)228 bool AbstractDisplay::BindAbstractScreen(sptr<AbstractScreen> abstractScreen)
229 {
230     if (abstractScreen == nullptr) {
231         WLOGE("display bind screen error, cannot get screen. display:%{public}" PRIu64"", id_);
232         return false;
233     }
234     ScreenId dmsScreenId = abstractScreen->dmsId_;
235     sptr<SupportedScreenModes> info = abstractScreen->GetActiveScreenMode();
236     if (info == nullptr) {
237         WLOGE("display bind screen error, cannot get info. display:%{public}" PRIu64", screen:%{public}" PRIu64"",
238             id_, dmsScreenId);
239         return false;
240     }
241 
242     Point point = abstractScreen->GetGroup()->GetChildPosition(dmsScreenId);
243     offsetX_ = point.posX_;
244     offsetY_ = point.posY_;
245     width_ = static_cast<int32_t>(info->width_);
246     height_ = static_cast<int32_t>(info->height_);
247     refreshRate_ = info->refreshRate_;
248     screenId_ = dmsScreenId;
249     WLOGD("display bind to screen. display:%{public}" PRIu64", screen:%{public}" PRIu64"", id_, dmsScreenId);
250     return true;
251 }
252 
GetAbstractScreenId() const253 ScreenId AbstractDisplay::GetAbstractScreenId() const
254 {
255     return screenId_;
256 }
257 
GetAbstractScreenGroupId() const258 ScreenId AbstractDisplay::GetAbstractScreenGroupId() const
259 {
260     return screenGroupId_;
261 }
262 
ConvertToDisplayInfo() const263 sptr<DisplayInfo> AbstractDisplay::ConvertToDisplayInfo() const
264 {
265     sptr<DisplayInfo> displayInfo = new(std::nothrow) DisplayInfo();
266     if (displayInfo == nullptr) {
267         return displayInfo;
268     }
269 
270     displayInfo->name_ = name_;
271     displayInfo->SetDisplayId(id_);
272     displayInfo->SetWidth(width_);
273     displayInfo->SetHeight(height_);
274     displayInfo->SetRefreshRate(refreshRate_);
275     displayInfo->SetScreenId(screenId_);
276     displayInfo->SetScreenGroupId(screenGroupId_);
277     displayInfo->SetVirtualPixelRatio(virtualPixelRatio_);
278     displayInfo->SetXDpi(xDpi_);
279     displayInfo->SetYDpi(yDpi_);
280     displayInfo->SetDpi(virtualPixelRatio_ * DOT_PER_INCH);
281     displayInfo->SetRotation(rotation_);
282     displayInfo->SetOrientation(orientation_);
283     displayInfo->SetOffsetX(offsetX_);
284     displayInfo->SetOffsetY(offsetY_);
285     displayInfo->displayState_ = displayState_;
286     displayInfo->SetWaterfallDisplayCompressionStatus(waterfallDisplayCompressionStatus_);
287     displayInfo->SetDisplayOrientation(displayOrientation_);
288     displayInfo->SetIsDefaultVertical(isDefaultVertical_);
289     return displayInfo;
290 }
291 } // namespace OHOS::Rosen