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 constexpr int32_t TRUNCATE_THREE_DECIMALS = 1000;
28 constexpr float SECONDARY_ROTATION_0 = 0.0F;
29 constexpr float SECONDARY_ROTATION_90 = 90.0F;
30 constexpr float SECONDARY_ROTATION_180 = 180.0F;
31 constexpr float SECONDARY_ROTATION_270 = 270.0F;
32 constexpr int32_t SECONDARY_MAIN_OFFSETY = -2176;
33 constexpr int32_t SECONDARY_FULL_OFFSETY = 1136;
34 constexpr float FULL_STATUS_WIDTH = 2048;
35 constexpr float MAIN_STATUS_WIDTH = 1008;
36 constexpr float SCREEN_HEIGHT = 2232;
37 }
38
SetRotation(float rotation)39 void ScreenProperty::SetRotation(float rotation)
40 {
41 rotation_ = rotation;
42 }
43
GetRotation() const44 float ScreenProperty::GetRotation() const
45 {
46 return rotation_;
47 }
48
SetPhysicalRotation(float rotation)49 void ScreenProperty::SetPhysicalRotation(float rotation)
50 {
51 physicalRotation_ = rotation;
52 }
53
GetPhysicalRotation() const54 float ScreenProperty::GetPhysicalRotation() const
55 {
56 return physicalRotation_;
57 }
58
SetScreenComponentRotation(float rotation)59 void ScreenProperty::SetScreenComponentRotation(float rotation)
60 {
61 screenComponentRotation_ = rotation;
62 }
63
GetScreenComponentRotation() const64 float ScreenProperty::GetScreenComponentRotation() const
65 {
66 return screenComponentRotation_;
67 }
68
SetBounds(const RRect & bounds)69 void ScreenProperty::SetBounds(const RRect& bounds)
70 {
71 bounds_ = bounds;
72 UpdateXDpi();
73 UpdateYDpi();
74 }
75
GetBounds() const76 RRect ScreenProperty::GetBounds() const
77 {
78 return bounds_;
79 }
80
SetScaleX(float scaleX)81 void ScreenProperty::SetScaleX(float scaleX)
82 {
83 scaleX_ = scaleX;
84 }
85
GetScaleX() const86 float ScreenProperty::GetScaleX() const
87 {
88 return scaleX_;
89 }
90
SetScaleY(float scaleY)91 void ScreenProperty::SetScaleY(float scaleY)
92 {
93 scaleY_ = scaleY;
94 }
95
GetScaleY() const96 float ScreenProperty::GetScaleY() const
97 {
98 return scaleY_;
99 }
100
SetPivotX(float pivotX)101 void ScreenProperty::SetPivotX(float pivotX)
102 {
103 pivotX_ = pivotX;
104 }
105
GetPivotX() const106 float ScreenProperty::GetPivotX() const
107 {
108 return pivotX_;
109 }
110
SetPivotY(float pivotY)111 void ScreenProperty::SetPivotY(float pivotY)
112 {
113 pivotY_ = pivotY;
114 }
115
GetPivotY() const116 float ScreenProperty::GetPivotY() const
117 {
118 return pivotY_;
119 }
120
SetTranslateX(float translateX)121 void ScreenProperty::SetTranslateX(float translateX)
122 {
123 translateX_ = translateX;
124 }
125
GetTranslateX() const126 float ScreenProperty::GetTranslateX() const
127 {
128 return translateX_;
129 }
130
SetTranslateY(float translateY)131 void ScreenProperty::SetTranslateY(float translateY)
132 {
133 translateY_ = translateY;
134 }
135
GetTranslateY() const136 float ScreenProperty::GetTranslateY() const
137 {
138 return translateY_;
139 }
140
SetPhyBounds(const RRect & phyBounds)141 void ScreenProperty::SetPhyBounds(const RRect& phyBounds)
142 {
143 phyBounds_ = phyBounds;
144 }
145
GetPhyBounds() const146 RRect ScreenProperty::GetPhyBounds() const
147 {
148 return phyBounds_;
149 }
150
GetDensity()151 float ScreenProperty::GetDensity()
152 {
153 return virtualPixelRatio_;
154 }
155
GetDefaultDensity()156 float ScreenProperty::GetDefaultDensity()
157 {
158 return defaultDensity_;
159 }
160
SetDefaultDensity(float defaultDensity)161 void ScreenProperty::SetDefaultDensity(float defaultDensity)
162 {
163 defaultDensity_ = defaultDensity;
164 }
165
GetDensityInCurResolution() const166 float ScreenProperty::GetDensityInCurResolution() const
167 {
168 return densityInCurResolution_;
169 }
170
SetDensityInCurResolution(float densityInCurResolution)171 void ScreenProperty::SetDensityInCurResolution(float densityInCurResolution)
172 {
173 densityInCurResolution_ = densityInCurResolution;
174 }
175
SetPhyWidth(uint32_t phyWidth)176 void ScreenProperty::SetPhyWidth(uint32_t phyWidth)
177 {
178 phyWidth_ = phyWidth;
179 }
180
GetPhyWidth() const181 int32_t ScreenProperty::GetPhyWidth() const
182 {
183 return phyWidth_;
184 }
185
SetPhyHeight(uint32_t phyHeight)186 void ScreenProperty::SetPhyHeight(uint32_t phyHeight)
187 {
188 phyHeight_ = phyHeight;
189 }
190
GetPhyHeight() const191 int32_t ScreenProperty::GetPhyHeight() const
192 {
193 return phyHeight_;
194 }
195
SetDpiPhyBounds(uint32_t phyWidth,uint32_t phyHeight)196 void ScreenProperty::SetDpiPhyBounds(uint32_t phyWidth, uint32_t phyHeight)
197 {
198 dpiPhyWidth_ = phyWidth;
199 dpiPhyHeight_ = phyHeight;
200 }
201
SetRefreshRate(uint32_t refreshRate)202 void ScreenProperty::SetRefreshRate(uint32_t refreshRate)
203 {
204 refreshRate_ = refreshRate;
205 }
206
GetRefreshRate() const207 uint32_t ScreenProperty::GetRefreshRate() const
208 {
209 return refreshRate_;
210 }
211
SetPropertyChangeReason(std::string propertyChangeReason)212 void ScreenProperty::SetPropertyChangeReason(std::string propertyChangeReason)
213 {
214 propertyChangeReason_ = propertyChangeReason;
215 }
216
GetPropertyChangeReason() const217 std::string ScreenProperty::GetPropertyChangeReason() const
218 {
219 return propertyChangeReason_;
220 }
221
SetVirtualPixelRatio(float virtualPixelRatio)222 void ScreenProperty::SetVirtualPixelRatio(float virtualPixelRatio)
223 {
224 virtualPixelRatio_ = virtualPixelRatio;
225 }
226
GetVirtualPixelRatio() const227 float ScreenProperty::GetVirtualPixelRatio() const
228 {
229 return virtualPixelRatio_;
230 }
231
SetScreenRotation(Rotation rotation)232 void ScreenProperty::SetScreenRotation(Rotation rotation)
233 {
234 bool enableRotation = system::GetParameter("persist.window.rotation.enabled", "1") == "1";
235 if (!enableRotation) {
236 return;
237 }
238 if (IsVertical(rotation) != IsVertical(screenRotation_)) {
239 std::swap(bounds_.rect_.width_, bounds_.rect_.height_);
240 int32_t width = bounds_.rect_.width_;
241 int32_t height = bounds_.rect_.height_;
242 if (IsVertical(screenRotation_)) {
243 bounds_.rect_.left_ -= static_cast<float>(width - height) / static_cast<float>(HALF_VALUE) -
244 static_cast<float>(offsetY_);
245 bounds_.rect_.top_ += static_cast<float>(width - height) / static_cast<float>(HALF_VALUE);
246 } else {
247 bounds_.rect_.left_ += static_cast<float>(height - width) / static_cast<float>(HALF_VALUE);
248 bounds_.rect_.top_ -= static_cast<float>(height - width) / static_cast<float>(HALF_VALUE) +
249 static_cast<float>(offsetY_);
250 }
251 }
252 switch (rotation) {
253 case Rotation::ROTATION_90:
254 rotation_ = 90.f;
255 break;
256 case Rotation::ROTATION_180:
257 rotation_ = 180.f;
258 break;
259 case Rotation::ROTATION_270:
260 rotation_ = 270.f;
261 break;
262 default:
263 rotation_ = 0.f;
264 break;
265 }
266 screenRotation_ = rotation;
267 }
268
SetRotationAndScreenRotationOnly(Rotation rotation)269 void ScreenProperty::SetRotationAndScreenRotationOnly(Rotation rotation)
270 {
271 bool enableRotation = (system::GetParameter("persist.window.rotation.enabled", "1") == "1");
272 if (!enableRotation) {
273 return;
274 }
275 switch (rotation) {
276 case Rotation::ROTATION_90:
277 rotation_ = 90.f;
278 break;
279 case Rotation::ROTATION_180:
280 rotation_ = 180.f;
281 break;
282 case Rotation::ROTATION_270:
283 rotation_ = 270.f;
284 break;
285 default:
286 rotation_ = 0.f;
287 break;
288 }
289 screenRotation_ = rotation;
290 }
291
UpdateScreenRotation(Rotation rotation)292 void ScreenProperty::UpdateScreenRotation(Rotation rotation)
293 {
294 screenRotation_ = rotation;
295 }
296
GetScreenRotation() const297 Rotation ScreenProperty::GetScreenRotation() const
298 {
299 return screenRotation_;
300 }
301
UpdateDeviceRotation(Rotation rotation)302 void ScreenProperty::UpdateDeviceRotation(Rotation rotation)
303 {
304 deviceRotation_ = rotation;
305 }
306
GetDeviceRotation() const307 Rotation ScreenProperty::GetDeviceRotation() const
308 {
309 return deviceRotation_;
310 }
311
SetOrientation(Orientation orientation)312 void ScreenProperty::SetOrientation(Orientation orientation)
313 {
314 orientation_ = orientation;
315 }
316
GetOrientation() const317 Orientation ScreenProperty::GetOrientation() const
318 {
319 return orientation_;
320 }
321
SetDisplayState(DisplayState displayState)322 void ScreenProperty::SetDisplayState(DisplayState displayState)
323 {
324 displayState_ = displayState;
325 }
326
GetDisplayState() const327 DisplayState ScreenProperty::GetDisplayState() const
328 {
329 return displayState_;
330 }
331
SetDisplayOrientation(DisplayOrientation displayOrientation)332 void ScreenProperty::SetDisplayOrientation(DisplayOrientation displayOrientation)
333 {
334 displayOrientation_ = displayOrientation;
335 }
336
GetDisplayOrientation() const337 DisplayOrientation ScreenProperty::GetDisplayOrientation() const
338 {
339 return displayOrientation_;
340 }
341
SetDeviceOrientation(DisplayOrientation displayOrientation)342 void ScreenProperty::SetDeviceOrientation(DisplayOrientation displayOrientation)
343 {
344 deviceOrientation_ = displayOrientation;
345 }
346
GetDeviceOrientation() const347 DisplayOrientation ScreenProperty::GetDeviceOrientation() const
348 {
349 return deviceOrientation_;
350 }
351
UpdateXDpi()352 void ScreenProperty::UpdateXDpi()
353 {
354 if (dpiPhyWidth_ != UINT32_MAX) {
355 int32_t width = phyBounds_.rect_.width_;
356 xDpi_ = width * INCH_2_MM / dpiPhyWidth_;
357 xDpi_ = std::floor(xDpi_ * TRUNCATE_THREE_DECIMALS) / TRUNCATE_THREE_DECIMALS;
358 }
359 }
360
UpdateYDpi()361 void ScreenProperty::UpdateYDpi()
362 {
363 if (dpiPhyHeight_ != UINT32_MAX) {
364 int32_t height_ = phyBounds_.rect_.height_;
365 yDpi_ = height_ * INCH_2_MM / dpiPhyHeight_;
366 yDpi_ = std::floor(yDpi_ * TRUNCATE_THREE_DECIMALS) / TRUNCATE_THREE_DECIMALS;
367 }
368 }
369
UpdateVirtualPixelRatio(const RRect & bounds)370 void ScreenProperty::UpdateVirtualPixelRatio(const RRect& bounds)
371 {
372 int32_t width = bounds.rect_.width_;
373 int32_t height = bounds.rect_.height_;
374
375 if (width == PHONE_SCREEN_WIDTH && height == PHONE_SCREEN_HEIGHT) { // telephone
376 virtualPixelRatio_ = PHONE_SCREEN_DENSITY;
377 } else {
378 virtualPixelRatio_ = ELSE_SCREEN_DENSITY;
379 }
380 defaultDensity_ = virtualPixelRatio_;
381 }
382
CalcDefaultDisplayOrientation()383 void ScreenProperty::CalcDefaultDisplayOrientation()
384 {
385 if (bounds_.rect_.width_ > bounds_.rect_.height_) {
386 displayOrientation_ = DisplayOrientation::LANDSCAPE;
387 deviceOrientation_ = DisplayOrientation::LANDSCAPE;
388 } else {
389 displayOrientation_ = DisplayOrientation::PORTRAIT;
390 deviceOrientation_ = DisplayOrientation::PORTRAIT;
391 }
392 }
393
CalculateXYDpi(uint32_t phyWidth,uint32_t phyHeight)394 void ScreenProperty::CalculateXYDpi(uint32_t phyWidth, uint32_t phyHeight)
395 {
396 if (phyWidth == 0 || phyHeight == 0) {
397 return;
398 }
399
400 phyWidth_ = phyWidth;
401 phyHeight_ = phyHeight;
402 int32_t width_ = phyBounds_.rect_.width_;
403 int32_t height_ = phyBounds_.rect_.height_;
404 xDpi_ = width_ * INCH_2_MM / phyWidth_;
405 yDpi_ = height_ * INCH_2_MM / phyHeight_;
406 xDpi_ = std::floor(xDpi_ * TRUNCATE_THREE_DECIMALS) / TRUNCATE_THREE_DECIMALS;
407 yDpi_ = std::floor(yDpi_ * TRUNCATE_THREE_DECIMALS) / TRUNCATE_THREE_DECIMALS;
408 }
409
GetXDpi()410 float ScreenProperty::GetXDpi()
411 {
412 return xDpi_;
413 }
414
GetYDpi()415 float ScreenProperty::GetYDpi()
416 {
417 return yDpi_;
418 }
419
SetOffsetX(int32_t offsetX)420 void ScreenProperty::SetOffsetX(int32_t offsetX)
421 {
422 offsetX_ = offsetX;
423 }
424
GetOffsetX() const425 int32_t ScreenProperty::GetOffsetX() const
426 {
427 return offsetX_;
428 }
429
SetOffsetY(int32_t offsetY)430 void ScreenProperty::SetOffsetY(int32_t offsetY)
431 {
432 offsetY_ = offsetY;
433 }
434
GetOffsetY() const435 int32_t ScreenProperty::GetOffsetY() const
436 {
437 return offsetY_;
438 }
439
SetOffset(int32_t offsetX,int32_t offsetY)440 void ScreenProperty::SetOffset(int32_t offsetX, int32_t offsetY)
441 {
442 offsetX_ = offsetX;
443 offsetY_ = offsetY;
444 }
445
SetScreenType(ScreenType type)446 void ScreenProperty::SetScreenType(ScreenType type)
447 {
448 type_ = type;
449 }
450
GetScreenType() const451 ScreenType ScreenProperty::GetScreenType() const
452 {
453 return type_;
454 }
455
SetScreenRequestedOrientation(Orientation orientation)456 void ScreenProperty::SetScreenRequestedOrientation(Orientation orientation)
457 {
458 screenRequestedOrientation_ = orientation;
459 }
460
GetScreenRequestedOrientation() const461 Orientation ScreenProperty::GetScreenRequestedOrientation() const
462 {
463 return screenRequestedOrientation_;
464 }
465
SetDefaultDeviceRotationOffset(uint32_t defaultRotationOffset)466 void ScreenProperty::SetDefaultDeviceRotationOffset(uint32_t defaultRotationOffset)
467 {
468 defaultDeviceRotationOffset_ = defaultRotationOffset;
469 }
470
GetDefaultDeviceRotationOffset() const471 uint32_t ScreenProperty::GetDefaultDeviceRotationOffset() const
472 {
473 return defaultDeviceRotationOffset_;
474 }
475
GetPhysicalTouchBounds()476 RRect ScreenProperty::GetPhysicalTouchBounds()
477 {
478 return physicalTouchBounds_;
479 }
480
SetPhysicalTouchBounds(bool isSecondaryDevice)481 void ScreenProperty::SetPhysicalTouchBounds(bool isSecondaryDevice)
482 {
483 if (!isSecondaryDevice) {
484 physicalTouchBounds_.rect_.width_ = bounds_.rect_.width_;
485 physicalTouchBounds_.rect_.height_ = bounds_.rect_.height_;
486 return;
487 }
488 if (rotation_ == SECONDARY_ROTATION_90 || rotation_ == SECONDARY_ROTATION_270) {
489 physicalTouchBounds_.rect_.width_ = phyBounds_.rect_.width_;
490 physicalTouchBounds_.rect_.height_ = phyBounds_.rect_.height_;
491 } else {
492 physicalTouchBounds_.rect_.width_ = phyBounds_.rect_.height_;
493 physicalTouchBounds_.rect_.height_ = phyBounds_.rect_.width_;
494 }
495 }
496
GetInputOffsetX()497 int32_t ScreenProperty::GetInputOffsetX()
498 {
499 return inputOffsetX_;
500 }
501
GetInputOffsetY()502 int32_t ScreenProperty::GetInputOffsetY()
503 {
504 return inputOffsetY_;
505 }
506
IsWidthHeightMatch(float width,float height,float targetWidth,float targetHeight)507 static inline bool IsWidthHeightMatch(float width, float height, float targetWidth, float targetHeight)
508 {
509 return (width == targetWidth && height == targetHeight) || (width == targetHeight && height == targetWidth);
510 }
511
SetInputOffsetY(bool isSecondaryDevice,FoldDisplayMode foldDisplayMode)512 void ScreenProperty::SetInputOffsetY(bool isSecondaryDevice, FoldDisplayMode foldDisplayMode)
513 {
514 inputOffsetY_ = 0;
515 if (!isSecondaryDevice) {
516 return;
517 }
518 if (IsWidthHeightMatch(bounds_.rect_.GetWidth(), bounds_.rect_.GetHeight(),
519 MAIN_STATUS_WIDTH, SCREEN_HEIGHT)) {
520 foldDisplayMode = FoldDisplayMode::MAIN;
521 } else if (IsWidthHeightMatch(bounds_.rect_.GetWidth(), bounds_.rect_.GetHeight(),
522 FULL_STATUS_WIDTH, SCREEN_HEIGHT)) {
523 foldDisplayMode = FoldDisplayMode::FULL;
524 } else {
525 return;
526 }
527 if (foldDisplayMode == FoldDisplayMode::FULL) {
528 if (rotation_ == SECONDARY_ROTATION_0 || rotation_ == SECONDARY_ROTATION_90) {
529 inputOffsetY_ = SECONDARY_FULL_OFFSETY;
530 }
531 } else if (foldDisplayMode == FoldDisplayMode::MAIN) {
532 if (rotation_ == SECONDARY_ROTATION_180 || rotation_ == SECONDARY_ROTATION_270) {
533 inputOffsetY_ = SECONDARY_MAIN_OFFSETY;
534 }
535 }
536 }
537 } // namespace OHOS::Rosen
538