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 #include "fold_screen_state_internel.h"
19
20 namespace OHOS::Rosen {
21 namespace {
22 constexpr int32_t PHONE_SCREEN_WIDTH = 1344;
23 constexpr int32_t PHONE_SCREEN_HEIGHT = 2772;
24 constexpr float PHONE_SCREEN_DENSITY = 3.5f;
25 constexpr float ELSE_SCREEN_DENSITY = 1.5f;
26 constexpr float INCH_2_MM = 25.4f;
27 constexpr int32_t HALF_VALUE = 2;
28 constexpr int32_t TRUNCATE_TWO_DECIMALS = 100;
29 constexpr int32_t TRUNCATE_THREE_DECIMALS = 1000;
30 constexpr float SECONDARY_ROTATION_90 = 90.0F;
31 constexpr float SECONDARY_ROTATION_270 = 270.0F;
32 constexpr int32_t SECONDARY_FULL_OFFSETY = 1136;
33 constexpr int32_t FULL_STATUS_WIDTH = 2048;
34 constexpr int32_t SCREEN_HEIGHT = 2232;
35 constexpr float EPSILON = 1e-6f;
36 constexpr float PPI_TO_DPI = 1.6f;
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 if (!FoldScreenStateInternel::IsSecondaryDisplayFoldDevice()) {
73 physicalTouchBounds_.rect_.width_ = bounds_.rect_.width_;
74 physicalTouchBounds_.rect_.height_ = bounds_.rect_.height_;
75 physicalTouchBounds_.rect_.left_ = bounds_.rect_.left_;
76 physicalTouchBounds_.rect_.top_ = bounds_.rect_.top_;
77 }
78 UpdateXDpi();
79 UpdateYDpi();
80 }
81
GetBounds() const82 RRect ScreenProperty::GetBounds() const
83 {
84 return bounds_;
85 }
86
SetFakeBounds(const RRect & fakeBounds)87 void ScreenProperty::SetFakeBounds(const RRect& fakeBounds)
88 {
89 fakeBounds_ = fakeBounds;
90 }
91
GetFakeBounds() const92 RRect ScreenProperty::GetFakeBounds() const
93 {
94 return fakeBounds_;
95 }
96
SetIsFakeInUse(bool isFakeInUse)97 void ScreenProperty::SetIsFakeInUse(bool isFakeInUse)
98 {
99 isFakeInUse_ = isFakeInUse;
100 }
101
GetIsFakeInUse() const102 bool ScreenProperty::GetIsFakeInUse() const
103 {
104 return isFakeInUse_;
105 }
106
SetScaleX(float scaleX)107 void ScreenProperty::SetScaleX(float scaleX)
108 {
109 scaleX_ = scaleX;
110 }
111
GetScaleX() const112 float ScreenProperty::GetScaleX() const
113 {
114 return scaleX_;
115 }
116
SetScaleY(float scaleY)117 void ScreenProperty::SetScaleY(float scaleY)
118 {
119 scaleY_ = scaleY;
120 }
121
GetScaleY() const122 float ScreenProperty::GetScaleY() const
123 {
124 return scaleY_;
125 }
126
SetPivotX(float pivotX)127 void ScreenProperty::SetPivotX(float pivotX)
128 {
129 pivotX_ = pivotX;
130 }
131
GetPivotX() const132 float ScreenProperty::GetPivotX() const
133 {
134 return pivotX_;
135 }
136
SetPivotY(float pivotY)137 void ScreenProperty::SetPivotY(float pivotY)
138 {
139 pivotY_ = pivotY;
140 }
141
GetPivotY() const142 float ScreenProperty::GetPivotY() const
143 {
144 return pivotY_;
145 }
146
SetTranslateX(float translateX)147 void ScreenProperty::SetTranslateX(float translateX)
148 {
149 translateX_ = translateX;
150 }
151
GetTranslateX() const152 float ScreenProperty::GetTranslateX() const
153 {
154 return translateX_;
155 }
156
SetTranslateY(float translateY)157 void ScreenProperty::SetTranslateY(float translateY)
158 {
159 translateY_ = translateY;
160 }
161
GetTranslateY() const162 float ScreenProperty::GetTranslateY() const
163 {
164 return translateY_;
165 }
166
SetPhyBounds(const RRect & phyBounds)167 void ScreenProperty::SetPhyBounds(const RRect& phyBounds)
168 {
169 phyBounds_ = phyBounds;
170 }
171
GetPhyBounds() const172 RRect ScreenProperty::GetPhyBounds() const
173 {
174 return phyBounds_;
175 }
176
GetDensity()177 float ScreenProperty::GetDensity()
178 {
179 return virtualPixelRatio_;
180 }
181
GetDefaultDensity()182 float ScreenProperty::GetDefaultDensity()
183 {
184 return defaultDensity_;
185 }
186
SetDefaultDensity(float defaultDensity)187 void ScreenProperty::SetDefaultDensity(float defaultDensity)
188 {
189 defaultDensity_ = defaultDensity;
190 }
191
GetDensityInCurResolution() const192 float ScreenProperty::GetDensityInCurResolution() const
193 {
194 return densityInCurResolution_;
195 }
196
SetDensityInCurResolution(float densityInCurResolution)197 void ScreenProperty::SetDensityInCurResolution(float densityInCurResolution)
198 {
199 densityInCurResolution_ = densityInCurResolution;
200 }
201
SetValidHeight(uint32_t validHeight)202 void ScreenProperty::SetValidHeight(uint32_t validHeight)
203 {
204 validHeight_ = validHeight;
205 }
206
GetValidHeight() const207 int32_t ScreenProperty::GetValidHeight() const
208 {
209 return validHeight_;
210 }
211
SetValidWidth(uint32_t validWidth)212 void ScreenProperty::SetValidWidth(uint32_t validWidth)
213 {
214 validWidth_ = validWidth;
215 }
216
GetValidWidth() const217 int32_t ScreenProperty::GetValidWidth() const
218 {
219 return validWidth_;
220 }
221
SetPhyWidth(uint32_t phyWidth)222 void ScreenProperty::SetPhyWidth(uint32_t phyWidth)
223 {
224 phyWidth_ = phyWidth;
225 }
226
GetPhyWidth() const227 int32_t ScreenProperty::GetPhyWidth() const
228 {
229 return phyWidth_;
230 }
231
SetPhyHeight(uint32_t phyHeight)232 void ScreenProperty::SetPhyHeight(uint32_t phyHeight)
233 {
234 phyHeight_ = phyHeight;
235 }
236
GetPhyHeight() const237 int32_t ScreenProperty::GetPhyHeight() const
238 {
239 return phyHeight_;
240 }
241
SetDpiPhyBounds(uint32_t phyWidth,uint32_t phyHeight)242 void ScreenProperty::SetDpiPhyBounds(uint32_t phyWidth, uint32_t phyHeight)
243 {
244 dpiPhyWidth_ = phyWidth;
245 dpiPhyHeight_ = phyHeight;
246 }
247
SetRefreshRate(uint32_t refreshRate)248 void ScreenProperty::SetRefreshRate(uint32_t refreshRate)
249 {
250 refreshRate_ = refreshRate;
251 }
252
GetRefreshRate() const253 uint32_t ScreenProperty::GetRefreshRate() const
254 {
255 return refreshRate_;
256 }
257
SetPropertyChangeReason(std::string propertyChangeReason)258 void ScreenProperty::SetPropertyChangeReason(std::string propertyChangeReason)
259 {
260 propertyChangeReason_ = propertyChangeReason;
261 }
262
GetPropertyChangeReason() const263 std::string ScreenProperty::GetPropertyChangeReason() const
264 {
265 return propertyChangeReason_;
266 }
267
SetVirtualPixelRatio(float virtualPixelRatio)268 void ScreenProperty::SetVirtualPixelRatio(float virtualPixelRatio)
269 {
270 virtualPixelRatio_ = virtualPixelRatio;
271 }
272
GetVirtualPixelRatio() const273 float ScreenProperty::GetVirtualPixelRatio() const
274 {
275 return virtualPixelRatio_;
276 }
277
SetScreenRotation(Rotation rotation)278 void ScreenProperty::SetScreenRotation(Rotation rotation)
279 {
280 bool enableRotation = (system::GetParameter("persist.window.rotation.enabled", "1") == "1");
281 if (!enableRotation) {
282 return;
283 }
284 if (IsVertical(rotation) != IsVertical(screenRotation_)) {
285 std::swap(bounds_.rect_.width_, bounds_.rect_.height_);
286 int32_t width = bounds_.rect_.width_;
287 int32_t height = bounds_.rect_.height_;
288 if (IsVertical(screenRotation_)) {
289 bounds_.rect_.left_ -= static_cast<float>(width - height) / static_cast<float>(HALF_VALUE) -
290 static_cast<float>(offsetY_);
291 bounds_.rect_.top_ += static_cast<float>(width - height) / static_cast<float>(HALF_VALUE);
292 } else {
293 bounds_.rect_.left_ += static_cast<float>(height - width) / static_cast<float>(HALF_VALUE);
294 bounds_.rect_.top_ -= static_cast<float>(height - width) / static_cast<float>(HALF_VALUE) +
295 static_cast<float>(offsetY_);
296 }
297 }
298 switch (rotation) {
299 case Rotation::ROTATION_90:
300 rotation_ = 90.f;
301 break;
302 case Rotation::ROTATION_180:
303 rotation_ = 180.f;
304 break;
305 case Rotation::ROTATION_270:
306 rotation_ = 270.f;
307 break;
308 default:
309 rotation_ = 0.f;
310 break;
311 }
312 screenRotation_ = rotation;
313 }
314
SetRotationAndScreenRotationOnly(Rotation rotation)315 void ScreenProperty::SetRotationAndScreenRotationOnly(Rotation rotation)
316 {
317 bool enableRotation = (system::GetParameter("persist.window.rotation.enabled", "1") == "1");
318 if (!enableRotation) {
319 return;
320 }
321 switch (rotation) {
322 case Rotation::ROTATION_90:
323 rotation_ = 90.f;
324 break;
325 case Rotation::ROTATION_180:
326 rotation_ = 180.f;
327 break;
328 case Rotation::ROTATION_270:
329 rotation_ = 270.f;
330 break;
331 default:
332 rotation_ = 0.f;
333 break;
334 }
335 screenRotation_ = rotation;
336 }
337
UpdateScreenRotation(Rotation rotation)338 void ScreenProperty::UpdateScreenRotation(Rotation rotation)
339 {
340 screenRotation_ = rotation;
341 }
342
GetScreenRotation() const343 Rotation ScreenProperty::GetScreenRotation() const
344 {
345 return screenRotation_;
346 }
347
UpdateDeviceRotation(Rotation rotation)348 void ScreenProperty::UpdateDeviceRotation(Rotation rotation)
349 {
350 deviceRotation_ = rotation;
351 }
352
GetDeviceRotation() const353 Rotation ScreenProperty::GetDeviceRotation() const
354 {
355 return deviceRotation_;
356 }
357
SetOrientation(Orientation orientation)358 void ScreenProperty::SetOrientation(Orientation orientation)
359 {
360 orientation_ = orientation;
361 }
362
GetOrientation() const363 Orientation ScreenProperty::GetOrientation() const
364 {
365 return orientation_;
366 }
367
SetDisplayState(DisplayState displayState)368 void ScreenProperty::SetDisplayState(DisplayState displayState)
369 {
370 displayState_ = displayState;
371 }
372
GetDisplayState() const373 DisplayState ScreenProperty::GetDisplayState() const
374 {
375 return displayState_;
376 }
377
SetDisplayOrientation(DisplayOrientation displayOrientation)378 void ScreenProperty::SetDisplayOrientation(DisplayOrientation displayOrientation)
379 {
380 displayOrientation_ = displayOrientation;
381 }
382
GetDisplayOrientation() const383 DisplayOrientation ScreenProperty::GetDisplayOrientation() const
384 {
385 return displayOrientation_;
386 }
387
SetDeviceOrientation(DisplayOrientation displayOrientation)388 void ScreenProperty::SetDeviceOrientation(DisplayOrientation displayOrientation)
389 {
390 deviceOrientation_ = displayOrientation;
391 }
392
GetDeviceOrientation() const393 DisplayOrientation ScreenProperty::GetDeviceOrientation() const
394 {
395 return deviceOrientation_;
396 }
397
UpdateXDpi()398 void ScreenProperty::UpdateXDpi()
399 {
400 if (dpiPhyWidth_ != UINT32_MAX) {
401 int32_t width = phyBounds_.rect_.width_;
402 xDpi_ = width * INCH_2_MM / dpiPhyWidth_;
403 xDpi_ = std::floor(xDpi_ * TRUNCATE_THREE_DECIMALS) / TRUNCATE_THREE_DECIMALS;
404 }
405 }
406
UpdateYDpi()407 void ScreenProperty::UpdateYDpi()
408 {
409 if (dpiPhyHeight_ != UINT32_MAX) {
410 int32_t height_ = phyBounds_.rect_.height_;
411 yDpi_ = height_ * INCH_2_MM / dpiPhyHeight_;
412 yDpi_ = std::floor(yDpi_ * TRUNCATE_THREE_DECIMALS) / TRUNCATE_THREE_DECIMALS;
413 }
414 }
415
UpdateVirtualPixelRatio(const RRect & bounds)416 void ScreenProperty::UpdateVirtualPixelRatio(const RRect& bounds)
417 {
418 int32_t width = bounds.rect_.width_;
419 int32_t height = bounds.rect_.height_;
420
421 if (width == PHONE_SCREEN_WIDTH && height == PHONE_SCREEN_HEIGHT) { // telephone
422 virtualPixelRatio_ = PHONE_SCREEN_DENSITY;
423 } else {
424 virtualPixelRatio_ = ELSE_SCREEN_DENSITY;
425 }
426 defaultDensity_ = virtualPixelRatio_;
427 }
428
CalcDefaultDisplayOrientation()429 void ScreenProperty::CalcDefaultDisplayOrientation()
430 {
431 if (bounds_.rect_.width_ > bounds_.rect_.height_) {
432 displayOrientation_ = DisplayOrientation::LANDSCAPE;
433 deviceOrientation_ = DisplayOrientation::LANDSCAPE;
434 } else {
435 displayOrientation_ = DisplayOrientation::PORTRAIT;
436 deviceOrientation_ = DisplayOrientation::PORTRAIT;
437 }
438 }
439
CalculateXYDpi(uint32_t phyWidth,uint32_t phyHeight)440 void ScreenProperty::CalculateXYDpi(uint32_t phyWidth, uint32_t phyHeight)
441 {
442 if (phyWidth == 0 || phyHeight == 0) {
443 return;
444 }
445
446 phyWidth_ = phyWidth;
447 phyHeight_ = phyHeight;
448 int32_t width_ = phyBounds_.rect_.width_;
449 int32_t height_ = phyBounds_.rect_.height_;
450 xDpi_ = width_ * INCH_2_MM / phyWidth_;
451 yDpi_ = height_ * INCH_2_MM / phyHeight_;
452 xDpi_ = std::floor(xDpi_ * TRUNCATE_THREE_DECIMALS) / TRUNCATE_THREE_DECIMALS;
453 yDpi_ = std::floor(yDpi_ * TRUNCATE_THREE_DECIMALS) / TRUNCATE_THREE_DECIMALS;
454 }
455
GetXDpi() const456 float ScreenProperty::GetXDpi() const
457 {
458 return xDpi_;
459 }
460
GetYDpi() const461 float ScreenProperty::GetYDpi() const
462 {
463 return yDpi_;
464 }
465
SetOffsetX(int32_t offsetX)466 void ScreenProperty::SetOffsetX(int32_t offsetX)
467 {
468 offsetX_ = offsetX;
469 }
470
GetOffsetX() const471 int32_t ScreenProperty::GetOffsetX() const
472 {
473 return offsetX_;
474 }
475
SetOffsetY(int32_t offsetY)476 void ScreenProperty::SetOffsetY(int32_t offsetY)
477 {
478 offsetY_ = offsetY;
479 }
480
GetOffsetY() const481 int32_t ScreenProperty::GetOffsetY() const
482 {
483 return offsetY_;
484 }
485
SetOffset(int32_t offsetX,int32_t offsetY)486 void ScreenProperty::SetOffset(int32_t offsetX, int32_t offsetY)
487 {
488 offsetX_ = offsetX;
489 offsetY_ = offsetY;
490 }
491
SetStartX(uint32_t startX)492 void ScreenProperty::SetStartX(uint32_t startX)
493 {
494 startX_ = startX;
495 }
496
GetStartX() const497 uint32_t ScreenProperty::GetStartX() const
498 {
499 return startX_;
500 }
501
SetStartY(uint32_t startY)502 void ScreenProperty::SetStartY(uint32_t startY)
503 {
504 startY_ = startY;
505 }
506
GetStartY() const507 uint32_t ScreenProperty::GetStartY() const
508 {
509 return startY_;
510 }
511
SetStartPosition(uint32_t startX,uint32_t startY)512 void ScreenProperty::SetStartPosition(uint32_t startX, uint32_t startY)
513 {
514 startX_ = startX;
515 startY_ = startY;
516 }
517
SetScreenType(ScreenType type)518 void ScreenProperty::SetScreenType(ScreenType type)
519 {
520 type_ = type;
521 }
522
GetScreenType() const523 ScreenType ScreenProperty::GetScreenType() const
524 {
525 return type_;
526 }
527
SetScreenRequestedOrientation(Orientation orientation)528 void ScreenProperty::SetScreenRequestedOrientation(Orientation orientation)
529 {
530 screenRequestedOrientation_ = orientation;
531 }
532
GetScreenRequestedOrientation() const533 Orientation ScreenProperty::GetScreenRequestedOrientation() const
534 {
535 return screenRequestedOrientation_;
536 }
537
SetDefaultDeviceRotationOffset(uint32_t defaultRotationOffset)538 void ScreenProperty::SetDefaultDeviceRotationOffset(uint32_t defaultRotationOffset)
539 {
540 defaultDeviceRotationOffset_ = defaultRotationOffset;
541 }
542
GetDefaultDeviceRotationOffset() const543 uint32_t ScreenProperty::GetDefaultDeviceRotationOffset() const
544 {
545 return defaultDeviceRotationOffset_;
546 }
547
SetScreenShape(ScreenShape screenShape)548 void ScreenProperty::SetScreenShape(ScreenShape screenShape)
549 {
550 screenShape_ = screenShape;
551 }
552
GetScreenShape() const553 ScreenShape ScreenProperty::GetScreenShape() const
554 {
555 return screenShape_;
556 }
557
SetX(int32_t x)558 void ScreenProperty::SetX(int32_t x)
559 {
560 x_ = x;
561 }
562
GetX() const563 int32_t ScreenProperty::GetX() const
564 {
565 return x_;
566 }
567
SetY(int32_t y)568 void ScreenProperty::SetY(int32_t y)
569 {
570 y_ = y;
571 }
572
GetY() const573 int32_t ScreenProperty::GetY() const
574 {
575 return y_;
576 }
577
SetXYPosition(int32_t x,int32_t y)578 void ScreenProperty::SetXYPosition(int32_t x, int32_t y)
579 {
580 x_ = x;
581 y_ = y;
582 }
583
GetPhysicalTouchBounds()584 RRect ScreenProperty::GetPhysicalTouchBounds()
585 {
586 return physicalTouchBounds_;
587 }
588
SetPhysicalTouchBounds()589 void ScreenProperty::SetPhysicalTouchBounds()
590 {
591 if (!FoldScreenStateInternel::IsSecondaryDisplayFoldDevice()) {
592 physicalTouchBounds_.rect_.width_ = bounds_.rect_.width_;
593 physicalTouchBounds_.rect_.height_ = bounds_.rect_.height_;
594 return;
595 }
596 if (rotation_ == SECONDARY_ROTATION_90 || rotation_ == SECONDARY_ROTATION_270) {
597 physicalTouchBounds_.rect_.width_ = phyBounds_.rect_.width_;
598 physicalTouchBounds_.rect_.height_ = phyBounds_.rect_.height_;
599 } else {
600 physicalTouchBounds_.rect_.width_ = phyBounds_.rect_.height_;
601 physicalTouchBounds_.rect_.height_ = phyBounds_.rect_.width_;
602 }
603 }
604
GetInputOffsetX()605 int32_t ScreenProperty::GetInputOffsetX()
606 {
607 return inputOffsetX_;
608 }
609
GetInputOffsetY()610 int32_t ScreenProperty::GetInputOffsetY()
611 {
612 return inputOffsetY_;
613 }
614
IsWidthHeightMatch(float width,float height,float targetWidth,float targetHeight)615 static inline bool IsWidthHeightMatch(float width, float height, float targetWidth, float targetHeight)
616 {
617 return (width == targetWidth && height == targetHeight) || (width == targetHeight && height == targetWidth);
618 }
619
SetInputOffsetY()620 void ScreenProperty::SetInputOffsetY()
621 {
622 inputOffsetX_ = 0;
623 if (!FoldScreenStateInternel::IsSecondaryDisplayFoldDevice()) {
624 return;
625 }
626 if (IsWidthHeightMatch(bounds_.rect_.GetWidth(), bounds_.rect_.GetHeight(), FULL_STATUS_WIDTH, SCREEN_HEIGHT)) {
627 inputOffsetX_ = SECONDARY_FULL_OFFSETY;
628 }
629 }
630
CalculatePPI()631 float ScreenProperty::CalculatePPI()
632 {
633 int32_t phywidth = GetPhyWidth();
634 int32_t phyHeight = GetPhyHeight();
635 float phyDiagonal = std::sqrt(static_cast<float>(phywidth * phywidth + phyHeight * phyHeight));
636 if (phyDiagonal < EPSILON) {
637 return 0.0f;
638 }
639 RRect bounds = GetBounds();
640 int32_t width = bounds.rect_.GetWidth();
641 int32_t height = bounds.rect_.GetHeight();
642 float ppi = std::sqrt(static_cast<float>(width * width + height * height)) * INCH_2_MM / phyDiagonal;
643 return std::round(ppi * TRUNCATE_TWO_DECIMALS) / TRUNCATE_TWO_DECIMALS;
644 }
645
CalculateDPI()646 uint32_t ScreenProperty::CalculateDPI()
647 {
648 return static_cast<uint32_t>(std::round(CalculatePPI() * PPI_TO_DPI));
649 }
650
SetPointerActiveWidth(uint32_t pointerActiveWidth)651 void ScreenProperty::SetPointerActiveWidth(uint32_t pointerActiveWidth)
652 {
653 pointerActiveWidth_ = pointerActiveWidth;
654 }
655
GetPointerActiveWidth()656 uint32_t ScreenProperty::GetPointerActiveWidth()
657 {
658 return pointerActiveWidth_;
659 }
660
SetPointerActiveHeight(uint32_t pointerActiveHeight)661 void ScreenProperty::SetPointerActiveHeight(uint32_t pointerActiveHeight)
662 {
663 pointerActiveHeight_ = pointerActiveHeight;
664 }
665
GetPointerActiveHeight()666 uint32_t ScreenProperty::GetPointerActiveHeight()
667 {
668 return pointerActiveHeight_;
669 }
670 } // namespace OHOS::Rosen
671