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