1 /*
2 * Copyright (c) 2023 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 "session/screen/include/screen_property.h"
17 #include "parameters.h"
18
19 namespace OHOS::Rosen {
20 namespace {
21 constexpr int32_t PHONE_SCREEN_WIDTH = 1344;
22 constexpr int32_t PHONE_SCREEN_HEIGHT = 2772;
23 constexpr float PHONE_SCREEN_DENSITY = 3.5f;
24 constexpr float ELSE_SCREEN_DENSITY = 1.5f;
25 constexpr float INCH_2_MM = 25.4f;
26 constexpr int32_t HALF_VALUE = 2;
27 }
28
SetRotation(float rotation)29 void ScreenProperty::SetRotation(float rotation)
30 {
31 rotation_ = rotation;
32 }
33
GetRotation() const34 float ScreenProperty::GetRotation() const
35 {
36 return rotation_;
37 }
38
SetBounds(const RRect & bounds)39 void ScreenProperty::SetBounds(const RRect& bounds)
40 {
41 bounds_ = bounds;
42 UpdateXDpi();
43 UpdateYDpi();
44 UpdateDisplayOrientation();
45 }
46
GetBounds() const47 RRect ScreenProperty::GetBounds() const
48 {
49 return bounds_;
50 }
51
GetDensity()52 float ScreenProperty::GetDensity()
53 {
54 return virtualPixelRatio_;
55 }
56
SetPhyWidth(uint32_t phyWidth)57 void ScreenProperty::SetPhyWidth(uint32_t phyWidth)
58 {
59 phyWidth_ = phyWidth;
60 }
61
GetPhyWidth() const62 int32_t ScreenProperty::GetPhyWidth() const
63 {
64 return phyWidth_;
65 }
66
SetPhyHeight(uint32_t phyHeight)67 void ScreenProperty::SetPhyHeight(uint32_t phyHeight)
68 {
69 phyHeight_ = phyHeight;
70 }
71
GetPhyHeight() const72 int32_t ScreenProperty::GetPhyHeight() const
73 {
74 return phyHeight_;
75 }
76
SetRefreshRate(uint32_t refreshRate)77 void ScreenProperty::SetRefreshRate(uint32_t refreshRate)
78 {
79 refreshRate_ = refreshRate;
80 }
81
GetRefreshRate() const82 uint32_t ScreenProperty::GetRefreshRate() const
83 {
84 return refreshRate_;
85 }
86
SetVirtualPixelRatio(float virtualPixelRatio)87 void ScreenProperty::SetVirtualPixelRatio(float virtualPixelRatio)
88 {
89 virtualPixelRatio_ = virtualPixelRatio;
90 }
91
GetVirtualPixelRatio() const92 float ScreenProperty::GetVirtualPixelRatio() const
93 {
94 return virtualPixelRatio_;
95 }
96
SetScreenRotation(Rotation rotation)97 void ScreenProperty::SetScreenRotation(Rotation rotation)
98 {
99 bool enableRotation = system::GetParameter("debug.window.rotation.enabled", "0") == "1";
100 if (!enableRotation) {
101 return;
102 }
103 if (IsVertical(rotation) != IsVertical(screenRotation_)) {
104 std::swap(bounds_.rect_.width_, bounds_.rect_.height_);
105 int32_t width = bounds_.rect_.width_;
106 int32_t height = bounds_.rect_.height_;
107 if (IsVertical(screenRotation_)) {
108 bounds_.rect_.left_ -= static_cast<float>(width - height) / static_cast<float>(HALF_VALUE) -
109 static_cast<float>(offsetY_);
110 bounds_.rect_.top_ += static_cast<float>(width - height) / static_cast<float>(HALF_VALUE);
111 } else {
112 bounds_.rect_.left_ += static_cast<float>(height - width) / static_cast<float>(HALF_VALUE);
113 bounds_.rect_.top_ -= static_cast<float>(height - width) / static_cast<float>(HALF_VALUE) +
114 static_cast<float>(offsetY_);
115 }
116 }
117 switch (rotation) {
118 case Rotation::ROTATION_90:
119 rotation_ = 90.f;
120 break;
121 case Rotation::ROTATION_180:
122 rotation_ = 180.f;
123 break;
124 case Rotation::ROTATION_270:
125 rotation_ = 270.f;
126 break;
127 default:
128 rotation_ = 0.f;
129 break;
130 }
131 screenRotation_ = rotation;
132 }
133
GetScreenRotation() const134 Rotation ScreenProperty::GetScreenRotation() const
135 {
136 return screenRotation_;
137 }
138
SetOrientation(Orientation orientation)139 void ScreenProperty::SetOrientation(Orientation orientation)
140 {
141 orientation_ = orientation;
142 }
143
GetOrientation() const144 Orientation ScreenProperty::GetOrientation() const
145 {
146 return orientation_;
147 }
148
SetDisplayOrientation(DisplayOrientation displayOrientation)149 void ScreenProperty::SetDisplayOrientation(DisplayOrientation displayOrientation)
150 {
151 displayOrientation_ = displayOrientation;
152 }
153
GetDisplayOrientation() const154 DisplayOrientation ScreenProperty::GetDisplayOrientation() const
155 {
156 return displayOrientation_;
157 }
158
UpdateXDpi()159 void ScreenProperty::UpdateXDpi()
160 {
161 if (phyWidth_ != UINT32_MAX) {
162 int32_t width = bounds_.rect_.width_;
163 xDpi_ = width * INCH_2_MM / phyWidth_;
164 }
165 }
166
UpdateYDpi()167 void ScreenProperty::UpdateYDpi()
168 {
169 if (phyHeight_ != UINT32_MAX) {
170 int32_t height_ = bounds_.rect_.height_;
171 yDpi_ = height_ * INCH_2_MM / phyHeight_;
172 }
173 }
174
UpdateVirtualPixelRatio(const RRect & bounds)175 void ScreenProperty::UpdateVirtualPixelRatio(const RRect& bounds)
176 {
177 int32_t width = bounds.rect_.width_;
178 int32_t height = bounds.rect_.height_;
179
180 if (width == PHONE_SCREEN_WIDTH && height == PHONE_SCREEN_HEIGHT) { // telephone
181 virtualPixelRatio_ = PHONE_SCREEN_DENSITY;
182 } else {
183 virtualPixelRatio_ = ELSE_SCREEN_DENSITY;
184 }
185 }
186
UpdateDisplayOrientation()187 void ScreenProperty::UpdateDisplayOrientation()
188 {
189 if (bounds_.rect_.width_ > bounds_.rect_.height_) {
190 displayOrientation_ = DisplayOrientation::LANDSCAPE;
191 } else {
192 displayOrientation_ = DisplayOrientation::PORTRAIT;
193 }
194 }
195
CalculateXYDpi(uint32_t phyWidth,uint32_t phyHeight)196 void ScreenProperty::CalculateXYDpi(uint32_t phyWidth, uint32_t phyHeight)
197 {
198 if (phyWidth == 0 || phyHeight == 0) {
199 return;
200 }
201
202 phyWidth_ = phyWidth;
203 phyHeight_ = phyHeight;
204 int32_t width_ = bounds_.rect_.width_;
205 int32_t height_ = bounds_.rect_.height_;
206 xDpi_ = width_ * INCH_2_MM / phyWidth_;
207 yDpi_ = height_ * INCH_2_MM / phyHeight_;
208 }
209
GetXDpi()210 float ScreenProperty::GetXDpi()
211 {
212 return xDpi_;
213 }
214
GetYDpi()215 float ScreenProperty::GetYDpi()
216 {
217 return yDpi_;
218 }
219
SetOffsetX(int32_t offsetX)220 void ScreenProperty::SetOffsetX(int32_t offsetX)
221 {
222 offsetX_ = offsetX;
223 }
224
GetOffsetX() const225 int32_t ScreenProperty::GetOffsetX() const
226 {
227 return offsetX_;
228 }
229
SetOffsetY(int32_t offsetY)230 void ScreenProperty::SetOffsetY(int32_t offsetY)
231 {
232 offsetY_ = offsetY;
233 }
234
GetOffsetY() const235 int32_t ScreenProperty::GetOffsetY() const
236 {
237 return offsetY_;
238 }
239
SetOffset(int32_t offsetX,int32_t offsetY)240 void ScreenProperty::SetOffset(int32_t offsetX, int32_t offsetY)
241 {
242 offsetX_ = offsetX;
243 offsetY_ = offsetY;
244 }
245
SetScreenType(ScreenType type)246 void ScreenProperty::SetScreenType(ScreenType type)
247 {
248 type_ = type;
249 }
250
GetScreenType() const251 ScreenType ScreenProperty::GetScreenType() const
252 {
253 return type_;
254 }
255
SetScreenRequestedOrientation(Orientation orientation)256 void ScreenProperty::SetScreenRequestedOrientation(Orientation orientation)
257 {
258 screenRequestedOrientation_ = orientation;
259 }
260
GetScreenRequestedOrientation() const261 Orientation ScreenProperty::GetScreenRequestedOrientation() const
262 {
263 return screenRequestedOrientation_;
264 }
265
266 } // namespace OHOS::Rosen
267