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  }
29  
SetRotation(float rotation)30  void ScreenProperty::SetRotation(float rotation)
31  {
32      rotation_ = rotation;
33  }
34  
GetRotation() const35  float ScreenProperty::GetRotation() const
36  {
37      return rotation_;
38  }
39  
SetPhysicalRotation(float rotation)40  void ScreenProperty::SetPhysicalRotation(float rotation)
41  {
42      physicalRotation_ = rotation;
43  }
44  
GetPhysicalRotation() const45  float ScreenProperty::GetPhysicalRotation() const
46  {
47      return physicalRotation_;
48  }
49  
SetScreenComponentRotation(float rotation)50  void ScreenProperty::SetScreenComponentRotation(float rotation)
51  {
52      screenComponentRotation_ = rotation;
53  }
54  
GetScreenComponentRotation() const55  float ScreenProperty::GetScreenComponentRotation() const
56  {
57      return screenComponentRotation_;
58  }
59  
SetBounds(const RRect & bounds)60  void ScreenProperty::SetBounds(const RRect& bounds)
61  {
62      bounds_ = bounds;
63      UpdateXDpi();
64      UpdateYDpi();
65  }
66  
GetBounds() const67  RRect ScreenProperty::GetBounds() const
68  {
69      return bounds_;
70  }
71  
SetScaleX(float scaleX)72  void ScreenProperty::SetScaleX(float scaleX)
73  {
74      scaleX_ = scaleX;
75  }
76  
GetScaleX() const77  float ScreenProperty::GetScaleX() const
78  {
79      return scaleX_;
80  }
81  
SetScaleY(float scaleY)82  void ScreenProperty::SetScaleY(float scaleY)
83  {
84      scaleY_ = scaleY;
85  }
86  
GetScaleY() const87  float ScreenProperty::GetScaleY() const
88  {
89      return scaleY_;
90  }
91  
SetPivotX(float pivotX)92  void ScreenProperty::SetPivotX(float pivotX)
93  {
94      pivotX_ = pivotX;
95  }
96  
GetPivotX() const97  float ScreenProperty::GetPivotX() const
98  {
99      return pivotX_;
100  }
101  
SetPivotY(float pivotY)102  void ScreenProperty::SetPivotY(float pivotY)
103  {
104      pivotY_ = pivotY;
105  }
106  
GetPivotY() const107  float ScreenProperty::GetPivotY() const
108  {
109      return pivotY_;
110  }
111  
SetTranslateX(float translateX)112  void ScreenProperty::SetTranslateX(float translateX)
113  {
114      translateX_ = translateX;
115  }
116  
GetTranslateX() const117  float ScreenProperty::GetTranslateX() const
118  {
119      return translateX_;
120  }
121  
SetTranslateY(float translateY)122  void ScreenProperty::SetTranslateY(float translateY)
123  {
124      translateY_ = translateY;
125  }
126  
GetTranslateY() const127  float ScreenProperty::GetTranslateY() const
128  {
129      return translateY_;
130  }
131  
SetPhyBounds(const RRect & phyBounds)132  void ScreenProperty::SetPhyBounds(const RRect& phyBounds)
133  {
134      phyBounds_ = phyBounds;
135  }
136  
GetPhyBounds() const137  RRect ScreenProperty::GetPhyBounds() const
138  {
139      return phyBounds_;
140  }
141  
GetDensity()142  float ScreenProperty::GetDensity()
143  {
144      return virtualPixelRatio_;
145  }
146  
GetDefaultDensity()147  float ScreenProperty::GetDefaultDensity()
148  {
149      return defaultDensity_;
150  }
151  
SetDefaultDensity(float defaultDensity)152  void ScreenProperty::SetDefaultDensity(float defaultDensity)
153  {
154      defaultDensity_ = defaultDensity;
155  }
156  
GetDensityInCurResolution() const157  float ScreenProperty::GetDensityInCurResolution() const
158  {
159      return densityInCurResolution_;
160  }
161  
SetDensityInCurResolution(float densityInCurResolution)162  void ScreenProperty::SetDensityInCurResolution(float densityInCurResolution)
163  {
164      densityInCurResolution_ = densityInCurResolution;
165  }
166  
SetPhyWidth(uint32_t phyWidth)167  void ScreenProperty::SetPhyWidth(uint32_t phyWidth)
168  {
169      phyWidth_ = phyWidth;
170  }
171  
GetPhyWidth() const172  int32_t ScreenProperty::GetPhyWidth() const
173  {
174      return phyWidth_;
175  }
176  
SetPhyHeight(uint32_t phyHeight)177  void ScreenProperty::SetPhyHeight(uint32_t phyHeight)
178  {
179      phyHeight_ = phyHeight;
180  }
181  
GetPhyHeight() const182  int32_t ScreenProperty::GetPhyHeight() const
183  {
184      return phyHeight_;
185  }
186  
SetDpiPhyBounds(uint32_t phyWidth,uint32_t phyHeight)187  void ScreenProperty::SetDpiPhyBounds(uint32_t phyWidth, uint32_t phyHeight)
188  {
189      dpiPhyWidth_ = phyWidth;
190      dpiPhyHeight_ = phyHeight;
191  }
192  
SetRefreshRate(uint32_t refreshRate)193  void ScreenProperty::SetRefreshRate(uint32_t refreshRate)
194  {
195      refreshRate_ = refreshRate;
196  }
197  
GetRefreshRate() const198  uint32_t ScreenProperty::GetRefreshRate() const
199  {
200      return refreshRate_;
201  }
202  
SetPropertyChangeReason(std::string propertyChangeReason)203  void ScreenProperty::SetPropertyChangeReason(std::string propertyChangeReason)
204  {
205      propertyChangeReason_ = propertyChangeReason;
206  }
207  
GetPropertyChangeReason() const208  std::string ScreenProperty::GetPropertyChangeReason() const
209  {
210      return propertyChangeReason_;
211  }
212  
SetVirtualPixelRatio(float virtualPixelRatio)213  void ScreenProperty::SetVirtualPixelRatio(float virtualPixelRatio)
214  {
215      virtualPixelRatio_ = virtualPixelRatio;
216  }
217  
GetVirtualPixelRatio() const218  float ScreenProperty::GetVirtualPixelRatio() const
219  {
220      return virtualPixelRatio_;
221  }
222  
SetScreenRotation(Rotation rotation)223  void ScreenProperty::SetScreenRotation(Rotation rotation)
224  {
225      bool enableRotation = system::GetParameter("persist.window.rotation.enabled", "1") == "1";
226      if (!enableRotation) {
227          return;
228      }
229      if (IsVertical(rotation) != IsVertical(screenRotation_)) {
230          std::swap(bounds_.rect_.width_, bounds_.rect_.height_);
231          int32_t width = bounds_.rect_.width_;
232          int32_t height = bounds_.rect_.height_;
233          if (IsVertical(screenRotation_)) {
234              bounds_.rect_.left_ -= static_cast<float>(width - height) / static_cast<float>(HALF_VALUE) -
235                  static_cast<float>(offsetY_);
236              bounds_.rect_.top_ += static_cast<float>(width - height) / static_cast<float>(HALF_VALUE);
237          } else {
238              bounds_.rect_.left_ += static_cast<float>(height - width) / static_cast<float>(HALF_VALUE);
239              bounds_.rect_.top_ -= static_cast<float>(height - width) / static_cast<float>(HALF_VALUE) +
240                  static_cast<float>(offsetY_);
241          }
242      }
243      switch (rotation) {
244          case Rotation::ROTATION_90:
245              rotation_ = 90.f;
246              break;
247          case Rotation::ROTATION_180:
248              rotation_ = 180.f;
249              break;
250          case Rotation::ROTATION_270:
251              rotation_ = 270.f;
252              break;
253          default:
254              rotation_ = 0.f;
255              break;
256      }
257      screenRotation_ = rotation;
258  }
259  
SetRotationAndScreenRotationOnly(Rotation rotation)260  void ScreenProperty::SetRotationAndScreenRotationOnly(Rotation rotation)
261  {
262      bool enableRotation = (system::GetParameter("persist.window.rotation.enabled", "1") == "1");
263      if (!enableRotation) {
264          return;
265      }
266      switch (rotation) {
267          case Rotation::ROTATION_90:
268              rotation_ = 90.f;
269              break;
270          case Rotation::ROTATION_180:
271              rotation_ = 180.f;
272              break;
273          case Rotation::ROTATION_270:
274              rotation_ = 270.f;
275              break;
276          default:
277              rotation_ = 0.f;
278              break;
279      }
280      screenRotation_ = rotation;
281  }
282  
UpdateScreenRotation(Rotation rotation)283  void ScreenProperty::UpdateScreenRotation(Rotation rotation)
284  {
285      screenRotation_ = rotation;
286  }
287  
GetScreenRotation() const288  Rotation ScreenProperty::GetScreenRotation() const
289  {
290      return screenRotation_;
291  }
292  
UpdateDeviceRotation(Rotation rotation)293  void ScreenProperty::UpdateDeviceRotation(Rotation rotation)
294  {
295      deviceRotation_ = rotation;
296  }
297  
GetDeviceRotation() const298  Rotation ScreenProperty::GetDeviceRotation() const
299  {
300      return deviceRotation_;
301  }
302  
SetOrientation(Orientation orientation)303  void ScreenProperty::SetOrientation(Orientation orientation)
304  {
305      orientation_ = orientation;
306  }
307  
GetOrientation() const308  Orientation ScreenProperty::GetOrientation() const
309  {
310      return orientation_;
311  }
312  
SetDisplayState(DisplayState displayState)313  void ScreenProperty::SetDisplayState(DisplayState displayState)
314  {
315      displayState_ = displayState;
316  }
317  
GetDisplayState() const318  DisplayState ScreenProperty::GetDisplayState() const
319  {
320      return displayState_;
321  }
322  
SetDisplayOrientation(DisplayOrientation displayOrientation)323  void ScreenProperty::SetDisplayOrientation(DisplayOrientation displayOrientation)
324  {
325      displayOrientation_ = displayOrientation;
326  }
327  
GetDisplayOrientation() const328  DisplayOrientation ScreenProperty::GetDisplayOrientation() const
329  {
330      return displayOrientation_;
331  }
332  
SetDeviceOrientation(DisplayOrientation displayOrientation)333  void ScreenProperty::SetDeviceOrientation(DisplayOrientation displayOrientation)
334  {
335      deviceOrientation_ = displayOrientation;
336  }
337  
GetDeviceOrientation() const338  DisplayOrientation ScreenProperty::GetDeviceOrientation() const
339  {
340      return deviceOrientation_;
341  }
342  
UpdateXDpi()343  void ScreenProperty::UpdateXDpi()
344  {
345      if (dpiPhyWidth_ != UINT32_MAX) {
346          int32_t width = phyBounds_.rect_.width_;
347          xDpi_ = width * INCH_2_MM / dpiPhyWidth_;
348          xDpi_ = std::floor(xDpi_ * TRUNCATE_THREE_DECIMALS) / TRUNCATE_THREE_DECIMALS;
349      }
350  }
351  
UpdateYDpi()352  void ScreenProperty::UpdateYDpi()
353  {
354      if (dpiPhyHeight_ != UINT32_MAX) {
355          int32_t height_ = phyBounds_.rect_.height_;
356          yDpi_ = height_ * INCH_2_MM / dpiPhyHeight_;
357          yDpi_ = std::floor(yDpi_ * TRUNCATE_THREE_DECIMALS) / TRUNCATE_THREE_DECIMALS;
358      }
359  }
360  
UpdateVirtualPixelRatio(const RRect & bounds)361  void ScreenProperty::UpdateVirtualPixelRatio(const RRect& bounds)
362  {
363      int32_t width = bounds.rect_.width_;
364      int32_t height = bounds.rect_.height_;
365  
366      if (width == PHONE_SCREEN_WIDTH && height == PHONE_SCREEN_HEIGHT) { // telephone
367          virtualPixelRatio_ = PHONE_SCREEN_DENSITY;
368      } else {
369          virtualPixelRatio_ = ELSE_SCREEN_DENSITY;
370      }
371      defaultDensity_ = virtualPixelRatio_;
372  }
373  
CalcDefaultDisplayOrientation()374  void ScreenProperty::CalcDefaultDisplayOrientation()
375  {
376      if (bounds_.rect_.width_ > bounds_.rect_.height_) {
377          displayOrientation_ = DisplayOrientation::LANDSCAPE;
378          deviceOrientation_ = DisplayOrientation::LANDSCAPE;
379      } else {
380          displayOrientation_ = DisplayOrientation::PORTRAIT;
381          deviceOrientation_ = DisplayOrientation::PORTRAIT;
382      }
383  }
384  
CalculateXYDpi(uint32_t phyWidth,uint32_t phyHeight)385  void ScreenProperty::CalculateXYDpi(uint32_t phyWidth, uint32_t phyHeight)
386  {
387      if (phyWidth == 0 || phyHeight == 0) {
388          return;
389      }
390  
391      phyWidth_ = phyWidth;
392      phyHeight_ = phyHeight;
393      int32_t width_ = phyBounds_.rect_.width_;
394      int32_t height_ = phyBounds_.rect_.height_;
395      xDpi_ = width_ * INCH_2_MM / phyWidth_;
396      yDpi_ = height_ * INCH_2_MM / phyHeight_;
397      xDpi_ = std::floor(xDpi_ * TRUNCATE_THREE_DECIMALS) / TRUNCATE_THREE_DECIMALS;
398      yDpi_ = std::floor(yDpi_ * TRUNCATE_THREE_DECIMALS) / TRUNCATE_THREE_DECIMALS;
399  }
400  
GetXDpi()401  float ScreenProperty::GetXDpi()
402  {
403      return xDpi_;
404  }
405  
GetYDpi()406  float ScreenProperty::GetYDpi()
407  {
408      return yDpi_;
409  }
410  
SetOffsetX(int32_t offsetX)411  void ScreenProperty::SetOffsetX(int32_t offsetX)
412  {
413      offsetX_ = offsetX;
414  }
415  
GetOffsetX() const416  int32_t ScreenProperty::GetOffsetX() const
417  {
418      return offsetX_;
419  }
420  
SetOffsetY(int32_t offsetY)421  void ScreenProperty::SetOffsetY(int32_t offsetY)
422  {
423      offsetY_ = offsetY;
424  }
425  
GetOffsetY() const426  int32_t ScreenProperty::GetOffsetY() const
427  {
428      return offsetY_;
429  }
430  
SetOffset(int32_t offsetX,int32_t offsetY)431  void ScreenProperty::SetOffset(int32_t offsetX, int32_t offsetY)
432  {
433      offsetX_ = offsetX;
434      offsetY_ = offsetY;
435  }
436  
SetScreenType(ScreenType type)437  void ScreenProperty::SetScreenType(ScreenType type)
438  {
439      type_ = type;
440  }
441  
GetScreenType() const442  ScreenType ScreenProperty::GetScreenType() const
443  {
444      return type_;
445  }
446  
SetScreenRequestedOrientation(Orientation orientation)447  void ScreenProperty::SetScreenRequestedOrientation(Orientation orientation)
448  {
449      screenRequestedOrientation_ = orientation;
450  }
451  
GetScreenRequestedOrientation() const452  Orientation ScreenProperty::GetScreenRequestedOrientation() const
453  {
454      return screenRequestedOrientation_;
455  }
456  
SetDefaultDeviceRotationOffset(uint32_t defaultRotationOffset)457  void ScreenProperty::SetDefaultDeviceRotationOffset(uint32_t defaultRotationOffset)
458  {
459      defaultDeviceRotationOffset_ = defaultRotationOffset;
460  }
461  
GetDefaultDeviceRotationOffset() const462  uint32_t ScreenProperty::GetDefaultDeviceRotationOffset() const
463  {
464      return defaultDeviceRotationOffset_;
465  }
466  } // namespace OHOS::Rosen
467