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