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 }
45
GetBounds() const46 RRect ScreenProperty::GetBounds() const
47 {
48 return bounds_;
49 }
50
SetPhyBounds(const RRect & phyBounds)51 void ScreenProperty::SetPhyBounds(const RRect& phyBounds)
52 {
53 phyBounds_ = phyBounds;
54 }
55
GetPhyBounds() const56 RRect ScreenProperty::GetPhyBounds() const
57 {
58 return phyBounds_;
59 }
60
GetDensity()61 float ScreenProperty::GetDensity()
62 {
63 return virtualPixelRatio_;
64 }
65
GetDefaultDensity()66 float ScreenProperty::GetDefaultDensity()
67 {
68 return defaultDensity_;
69 }
70
SetDefaultDensity(float defaultDensity)71 void ScreenProperty::SetDefaultDensity(float defaultDensity)
72 {
73 defaultDensity_ = defaultDensity;
74 }
75
GetDensityInCurResolution() const76 float ScreenProperty::GetDensityInCurResolution() const
77 {
78 return densityInCurResolution_;
79 }
80
SetDensityInCurResolution(float densityInCurResolution)81 void ScreenProperty::SetDensityInCurResolution(float densityInCurResolution)
82 {
83 densityInCurResolution_ = densityInCurResolution;
84 }
85
SetPhyWidth(uint32_t phyWidth)86 void ScreenProperty::SetPhyWidth(uint32_t phyWidth)
87 {
88 phyWidth_ = phyWidth;
89 }
90
GetPhyWidth() const91 int32_t ScreenProperty::GetPhyWidth() const
92 {
93 return phyWidth_;
94 }
95
SetPhyHeight(uint32_t phyHeight)96 void ScreenProperty::SetPhyHeight(uint32_t phyHeight)
97 {
98 phyHeight_ = phyHeight;
99 }
100
GetPhyHeight() const101 int32_t ScreenProperty::GetPhyHeight() const
102 {
103 return phyHeight_;
104 }
105
SetRefreshRate(uint32_t refreshRate)106 void ScreenProperty::SetRefreshRate(uint32_t refreshRate)
107 {
108 refreshRate_ = refreshRate;
109 }
110
GetRefreshRate() const111 uint32_t ScreenProperty::GetRefreshRate() const
112 {
113 return refreshRate_;
114 }
115
SetVirtualPixelRatio(float virtualPixelRatio)116 void ScreenProperty::SetVirtualPixelRatio(float virtualPixelRatio)
117 {
118 virtualPixelRatio_ = virtualPixelRatio;
119 }
120
GetVirtualPixelRatio() const121 float ScreenProperty::GetVirtualPixelRatio() const
122 {
123 return virtualPixelRatio_;
124 }
125
SetScreenRotation(Rotation rotation)126 void ScreenProperty::SetScreenRotation(Rotation rotation)
127 {
128 bool enableRotation = system::GetParameter("persist.window.rotation.enabled", "1") == "1";
129 if (!enableRotation) {
130 return;
131 }
132 if (IsVertical(rotation) != IsVertical(screenRotation_)) {
133 std::swap(bounds_.rect_.width_, bounds_.rect_.height_);
134 int32_t width = bounds_.rect_.width_;
135 int32_t height = bounds_.rect_.height_;
136 if (IsVertical(screenRotation_)) {
137 bounds_.rect_.left_ -= static_cast<float>(width - height) / static_cast<float>(HALF_VALUE) -
138 static_cast<float>(offsetY_);
139 bounds_.rect_.top_ += static_cast<float>(width - height) / static_cast<float>(HALF_VALUE);
140 } else {
141 bounds_.rect_.left_ += static_cast<float>(height - width) / static_cast<float>(HALF_VALUE);
142 bounds_.rect_.top_ -= static_cast<float>(height - width) / static_cast<float>(HALF_VALUE) +
143 static_cast<float>(offsetY_);
144 }
145 }
146 switch (rotation) {
147 case Rotation::ROTATION_90:
148 rotation_ = 90.f;
149 break;
150 case Rotation::ROTATION_180:
151 rotation_ = 180.f;
152 break;
153 case Rotation::ROTATION_270:
154 rotation_ = 270.f;
155 break;
156 default:
157 rotation_ = 0.f;
158 break;
159 }
160 screenRotation_ = rotation;
161 }
162
UpdateScreenRotation(Rotation rotation)163 void ScreenProperty::UpdateScreenRotation(Rotation rotation)
164 {
165 screenRotation_ = rotation;
166 }
167
GetScreenRotation() const168 Rotation ScreenProperty::GetScreenRotation() const
169 {
170 return screenRotation_;
171 }
172
SetOrientation(Orientation orientation)173 void ScreenProperty::SetOrientation(Orientation orientation)
174 {
175 orientation_ = orientation;
176 }
177
GetOrientation() const178 Orientation ScreenProperty::GetOrientation() const
179 {
180 return orientation_;
181 }
182
SetDisplayOrientation(DisplayOrientation displayOrientation)183 void ScreenProperty::SetDisplayOrientation(DisplayOrientation displayOrientation)
184 {
185 displayOrientation_ = displayOrientation;
186 }
187
GetDisplayOrientation() const188 DisplayOrientation ScreenProperty::GetDisplayOrientation() const
189 {
190 return displayOrientation_;
191 }
192
UpdateXDpi()193 void ScreenProperty::UpdateXDpi()
194 {
195 if (phyWidth_ != UINT32_MAX) {
196 int32_t width = bounds_.rect_.width_;
197 xDpi_ = width * INCH_2_MM / phyWidth_;
198 }
199 }
200
UpdateYDpi()201 void ScreenProperty::UpdateYDpi()
202 {
203 if (phyHeight_ != UINT32_MAX) {
204 int32_t height_ = bounds_.rect_.height_;
205 yDpi_ = height_ * INCH_2_MM / phyHeight_;
206 }
207 }
208
UpdateVirtualPixelRatio(const RRect & bounds)209 void ScreenProperty::UpdateVirtualPixelRatio(const RRect& bounds)
210 {
211 int32_t width = bounds.rect_.width_;
212 int32_t height = bounds.rect_.height_;
213
214 if (width == PHONE_SCREEN_WIDTH && height == PHONE_SCREEN_HEIGHT) { // telephone
215 virtualPixelRatio_ = PHONE_SCREEN_DENSITY;
216 } else {
217 virtualPixelRatio_ = ELSE_SCREEN_DENSITY;
218 }
219 defaultDensity_ = virtualPixelRatio_;
220 }
221
CalcDefaultDisplayOrientation()222 void ScreenProperty::CalcDefaultDisplayOrientation()
223 {
224 if (bounds_.rect_.width_ > bounds_.rect_.height_) {
225 displayOrientation_ = DisplayOrientation::LANDSCAPE;
226 } else {
227 displayOrientation_ = DisplayOrientation::PORTRAIT;
228 }
229 }
230
CalculateXYDpi(uint32_t phyWidth,uint32_t phyHeight)231 void ScreenProperty::CalculateXYDpi(uint32_t phyWidth, uint32_t phyHeight)
232 {
233 if (phyWidth == 0 || phyHeight == 0) {
234 return;
235 }
236
237 phyWidth_ = phyWidth;
238 phyHeight_ = phyHeight;
239 int32_t width_ = bounds_.rect_.width_;
240 int32_t height_ = bounds_.rect_.height_;
241 xDpi_ = width_ * INCH_2_MM / phyWidth_;
242 yDpi_ = height_ * INCH_2_MM / phyHeight_;
243 }
244
GetXDpi()245 float ScreenProperty::GetXDpi()
246 {
247 return xDpi_;
248 }
249
GetYDpi()250 float ScreenProperty::GetYDpi()
251 {
252 return yDpi_;
253 }
254
SetOffsetX(int32_t offsetX)255 void ScreenProperty::SetOffsetX(int32_t offsetX)
256 {
257 offsetX_ = offsetX;
258 }
259
GetOffsetX() const260 int32_t ScreenProperty::GetOffsetX() const
261 {
262 return offsetX_;
263 }
264
SetOffsetY(int32_t offsetY)265 void ScreenProperty::SetOffsetY(int32_t offsetY)
266 {
267 offsetY_ = offsetY;
268 }
269
GetOffsetY() const270 int32_t ScreenProperty::GetOffsetY() const
271 {
272 return offsetY_;
273 }
274
SetOffset(int32_t offsetX,int32_t offsetY)275 void ScreenProperty::SetOffset(int32_t offsetX, int32_t offsetY)
276 {
277 offsetX_ = offsetX;
278 offsetY_ = offsetY;
279 }
280
SetScreenType(ScreenType type)281 void ScreenProperty::SetScreenType(ScreenType type)
282 {
283 type_ = type;
284 }
285
GetScreenType() const286 ScreenType ScreenProperty::GetScreenType() const
287 {
288 return type_;
289 }
290
SetScreenRequestedOrientation(Orientation orientation)291 void ScreenProperty::SetScreenRequestedOrientation(Orientation orientation)
292 {
293 screenRequestedOrientation_ = orientation;
294 }
295
GetScreenRequestedOrientation() const296 Orientation ScreenProperty::GetScreenRequestedOrientation() const
297 {
298 return screenRequestedOrientation_;
299 }
300 } // namespace OHOS::Rosen
301