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 "pip_util.h"
17
18 namespace OHOS {
19 namespace Rosen {
20 namespace {
21 constexpr float DEFAULT_PROPORTION = 0.3;
22 constexpr int32_t NUMBER_TWO = 2;
23 constexpr int32_t NUMBER_THREE = 3;
24 constexpr int32_t NUMBER_FOUR = 4;
25 constexpr int32_t NUMBER_SEVEN = 7;
26 float g_vpr = 1.0f;
27 }
28
SetDisplayVpr(const float displayVpr)29 void PiPUtil::SetDisplayVpr(const float displayVpr)
30 {
31 g_vpr = displayVpr;
32 }
33
UpdateRectPivot(const int32_t start,const uint32_t len,const uint32_t totalLen,PiPScalePivot & pivot)34 void PiPUtil::UpdateRectPivot(const int32_t start, const uint32_t len, const uint32_t totalLen, PiPScalePivot& pivot)
35 {
36 int32_t end = static_cast<int32_t>(totalLen) - start - static_cast<int32_t>(len);
37 if (start > end) {
38 pivot = PiPScalePivot::END;
39 } else if (start < end) {
40 pivot = PiPScalePivot::START;
41 } else {
42 pivot = PiPScalePivot::MIDDLE;
43 }
44 }
45
GetRectByPivot(int32_t & start,const uint32_t oldLen,const uint32_t len,const uint32_t totalLen,const PiPScalePivot & pivot)46 void PiPUtil::GetRectByPivot(int32_t& start, const uint32_t oldLen, const uint32_t len, const uint32_t totalLen,
47 const PiPScalePivot& pivot)
48 {
49 switch (pivot) {
50 default:
51 case PiPScalePivot::START:
52 break;
53 case PiPScalePivot::MIDDLE:
54 start = (static_cast<int32_t>(totalLen) - static_cast<int32_t>(len)) / NUMBER_TWO;
55 break;
56 case PiPScalePivot::END:
57 start = start - static_cast<int32_t>(len) + static_cast<int32_t>(oldLen);
58 break;
59 }
60 }
61
CalcWinRectLand(const uint32_t width,const uint32_t height,const uint32_t winWidth,const uint32_t winHeight,Rect & rect)62 void CalcWinRectLand(const uint32_t width, const uint32_t height, const uint32_t winWidth,
63 const uint32_t winHeight, Rect& rect)
64 {
65 if (winWidth == 0 || winHeight == 0) {
66 return;
67 }
68 int32_t heightTmp =
69 static_cast<int32_t>(height) - PiPUtil::SAFE_PADDING_VERTICAL_TOP - PiPUtil::SAFE_PADDING_VERTICAL_BOTTOM;
70 int32_t safePaddingHorizontal = static_cast<int32_t>(PiPUtil::SAFE_PADDING_HORIZONTAL_VP * g_vpr);
71 if (winWidth <= winHeight) {
72 int32_t widthTmp = (NUMBER_THREE * static_cast<int32_t>(width)
73 - NUMBER_SEVEN * safePaddingHorizontal) / NUMBER_FOUR;
74 rect.width_ = static_cast<uint32_t>(widthTmp);
75 rect.height_ = rect.width_ * winHeight / winWidth;
76 if (rect.height_ > static_cast<uint32_t>(heightTmp)) {
77 rect.height_ = static_cast<uint32_t>(heightTmp);
78 rect.width_ = rect.height_ * winWidth / winHeight;
79 }
80 } else {
81 rect.height_ = static_cast<uint32_t>(heightTmp);
82 rect.width_ = rect.height_ * winWidth / winHeight;
83 }
84 }
85
GetRectByScale(const uint32_t width,const uint32_t height,const PiPScaleLevel & scaleLevel,Rect & rect,bool isLandscape)86 void PiPUtil::GetRectByScale(const uint32_t width, const uint32_t height, const PiPScaleLevel& scaleLevel,
87 Rect& rect, bool isLandscape)
88 {
89 uint32_t winWidth = rect.width_;
90 uint32_t winHeight = rect.height_;
91 if (winWidth == 0 || winHeight == 0) {
92 return;
93 }
94 int32_t safePaddingHorizontal = static_cast<int32_t>(SAFE_PADDING_HORIZONTAL_VP * g_vpr);
95 switch (scaleLevel) {
96 default:
97 case PiPScaleLevel::PIP_SCALE_LEVEL_SMALLEST: {
98 float shortBorder = static_cast<float>(width < height ? width : height) * DEFAULT_PROPORTION;
99 if (winWidth < winHeight) {
100 rect.width_ = static_cast<uint32_t>(shortBorder);
101 rect.height_ = rect.width_ * winHeight / winWidth;
102 } else {
103 rect.height_ = static_cast<uint32_t>(shortBorder);
104 rect.width_ = rect.height_ * winWidth / winHeight;
105 }
106 break;
107 }
108 case PiPScaleLevel::PIP_SCALE_LEVEL_BIGGEST: {
109 if (isLandscape) {
110 CalcWinRectLand(width, height, winWidth, winHeight, rect);
111 } else {
112 int32_t widthTmp = 0;
113 if (winWidth < winHeight) {
114 widthTmp = (NUMBER_THREE * static_cast<int32_t>(width) -
115 NUMBER_SEVEN * safePaddingHorizontal) / NUMBER_FOUR;
116 } else {
117 widthTmp = static_cast<int32_t>(width) - NUMBER_TWO * safePaddingHorizontal;
118 }
119 rect.width_ = static_cast<uint32_t>(widthTmp);
120 rect.height_ = rect.width_ * winHeight / winWidth;
121 }
122 break;
123 }
124 }
125 }
126
GetValidRect(const int32_t width,const int32_t height,Rect & rect)127 bool PiPUtil::GetValidRect(const int32_t width, const int32_t height, Rect& rect)
128 {
129 int32_t safePaddingHorizontal = static_cast<int32_t>(SAFE_PADDING_HORIZONTAL_VP * g_vpr);
130 bool hasChanged = false;
131 if (rect.posX_ < safePaddingHorizontal) {
132 rect.posX_ = safePaddingHorizontal;
133 hasChanged = true;
134 } else if ((rect.posX_ + static_cast<int32_t>(rect.width_)) > (width - safePaddingHorizontal)) {
135 rect.posX_ = width - safePaddingHorizontal - static_cast<int32_t>(rect.width_);
136 hasChanged = true;
137 }
138 if (rect.posY_ < SAFE_PADDING_VERTICAL_TOP) {
139 rect.posY_ = SAFE_PADDING_VERTICAL_TOP;
140 hasChanged = true;
141 } else if ((rect.posY_ + static_cast<int32_t>(rect.height_)) > (height - SAFE_PADDING_VERTICAL_BOTTOM)) {
142 rect.posY_ = height - SAFE_PADDING_VERTICAL_BOTTOM - static_cast<int32_t>(rect.height_);
143 hasChanged = true;
144 }
145 return hasChanged;
146 }
147 } // namespace Rosen
148 } // namespace OHOS