blob: a830542623dc253f787a3e379513933bc230006e [file] [log] [blame]
Manoj Kumar AVMb86bc172015-01-28 21:20:04 -08001/*
Pullakavi Srinivas1494a932016-12-30 12:51:06 +05302* Copyright (c) 2015-2017, The Linux Foundation. All rights reserved.
Manoj Kumar AVMb86bc172015-01-28 21:20:04 -08003*
4* Redistribution and use in source and binary forms, with or without
5* modification, are permitted provided that the following conditions are
6* met:
7* * Redistributions of source code must retain the above copyright
8* notice, this list of conditions and the following disclaimer.
9* * Redistributions in binary form must reproduce the above
10* copyright notice, this list of conditions and the following
11* disclaimer in the documentation and/or other materials provided
12* with the distribution.
13* * Neither the name of The Linux Foundation nor the names of its
14* contributors may be used to endorse or promote products derived
15* from this software without specific prior written permission.
16*
17* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*/
29
Tatenda Chipeperekwa2ba97892015-02-10 12:18:34 -080030#include <math.h>
Manoj Kumar AVMb86bc172015-01-28 21:20:04 -080031#include <utils/rect.h>
Tatenda Chipeperekwa2ba97892015-02-10 12:18:34 -080032#include <utils/constants.h>
Dileep Marchya7290bd22016-06-09 11:37:20 +053033#include <algorithm>
Tatenda Chipeperekwa2ba97892015-02-10 12:18:34 -080034
35#define __CLASS__ "RectUtils"
Manoj Kumar AVMb86bc172015-01-28 21:20:04 -080036
Dileep Marchya73d002e2015-05-08 18:58:33 -070037namespace sdm {
Manoj Kumar AVMb86bc172015-01-28 21:20:04 -080038
Manoj Kumar AVM351c9282015-03-18 16:34:38 -070039bool IsValid(const LayerRect &rect) {
Manoj Kumar AVMb86bc172015-01-28 21:20:04 -080040 return ((rect.bottom > rect.top) && (rect.right > rect.left));
41}
42
Manoj Kumar AVM351c9282015-03-18 16:34:38 -070043bool IsCongruent(const LayerRect &rect1, const LayerRect &rect2) {
44 return ((rect1.left == rect2.left) &&
45 (rect1.top == rect2.top) &&
46 (rect1.right == rect2.right) &&
47 (rect1.bottom == rect2.bottom));
48}
Manoj Kumar AVMb86bc172015-01-28 21:20:04 -080049
Saurabh Dubeyd90a6a42017-10-24 16:28:01 +053050void LogI(DebugTag debug_tag, const char *prefix, const LayerRect &roi) {
51 DLOGI_IF(debug_tag, "%s: left = %.0f, top = %.0f, right = %.0f, bottom = %.0f",
52 prefix, roi.left, roi.top, roi.right, roi.bottom);
53}
54
Manoj Kumar AVM351c9282015-03-18 16:34:38 -070055void Log(DebugTag debug_tag, const char *prefix, const LayerRect &roi) {
56 DLOGV_IF(debug_tag, "%s: left = %.0f, top = %.0f, right = %.0f, bottom = %.0f",
57 prefix, roi.left, roi.top, roi.right, roi.bottom);
58}
59
60void Normalize(const uint32_t &align_x, const uint32_t &align_y, LayerRect *rect) {
61 rect->left = ROUND_UP_ALIGN_UP(rect->left, align_x);
62 rect->right = ROUND_UP_ALIGN_DOWN(rect->right, align_x);
63 rect->top = ROUND_UP_ALIGN_UP(rect->top, align_y);
64 rect->bottom = ROUND_UP_ALIGN_DOWN(rect->bottom, align_y);
65}
66
67LayerRect Intersection(const LayerRect &rect1, const LayerRect &rect2) {
Manoj Kumar AVMb86bc172015-01-28 21:20:04 -080068 LayerRect res;
69
Manoj Kumar AVM351c9282015-03-18 16:34:38 -070070 if (!IsValid(rect1) || !IsValid(rect2)) {
Manoj Kumar AVMb86bc172015-01-28 21:20:04 -080071 return LayerRect();
72 }
73
Dileep Marchya7290bd22016-06-09 11:37:20 +053074 res.left = std::max(rect1.left, rect2.left);
75 res.top = std::max(rect1.top, rect2.top);
76 res.right = std::min(rect1.right, rect2.right);
77 res.bottom = std::min(rect1.bottom, rect2.bottom);
Manoj Kumar AVMb86bc172015-01-28 21:20:04 -080078
Manoj Kumar AVM351c9282015-03-18 16:34:38 -070079 if (!IsValid(res)) {
Manoj Kumar AVMb86bc172015-01-28 21:20:04 -080080 return LayerRect();
81 }
82
83 return res;
84}
85
Manoj Kumar AVM351c9282015-03-18 16:34:38 -070086LayerRect Reposition(const LayerRect &rect, const int &x_offset, const int &y_offset) {
87 LayerRect res;
88
89 if (!IsValid(rect)) {
90 return LayerRect();
91 }
92
93 res.left = rect.left + FLOAT(x_offset);
94 res.top = rect.top + FLOAT(y_offset);
95 res.right = rect.right + FLOAT(x_offset);
96 res.bottom = rect.bottom + FLOAT(y_offset);
97
98 return res;
Tatenda Chipeperekwa2ba97892015-02-10 12:18:34 -080099}
100
Manoj Kumar AVM351c9282015-03-18 16:34:38 -0700101// Not a geometrical rect deduction. Deducts rect2 from rect1 only if it results a single rect
102LayerRect Subtract(const LayerRect &rect1, const LayerRect &rect2) {
103 LayerRect res;
104
105 res = rect1;
106
107 if ((rect1.left == rect2.left) && (rect1.right == rect2.right)) {
108 if ((rect1.top == rect2.top) && (rect2.bottom <= rect1.bottom)) {
109 res.top = rect2.bottom;
110 } else if ((rect1.bottom == rect2.bottom) && (rect2.top >= rect1.top)) {
111 res.bottom = rect2.top;
112 }
113 } else if ((rect1.top == rect2.top) && (rect1.bottom == rect2.bottom)) {
114 if ((rect1.left == rect2.left) && (rect2.right <= rect1.right)) {
115 res.left = rect2.right;
116 } else if ((rect1.right == rect2.right) && (rect2.left >= rect1.left)) {
117 res.right = rect2.left;
118 }
119 }
120
121 return res;
122}
123
124LayerRect Union(const LayerRect &rect1, const LayerRect &rect2) {
125 LayerRect res;
126
127 if (!IsValid(rect1) && !IsValid(rect2)) {
128 return LayerRect();
129 }
130
Manoj Kumar AVM2405ce52015-04-02 23:13:19 -0700131 if (!IsValid(rect1)) {
Manoj Kumar AVM351c9282015-03-18 16:34:38 -0700132 return rect2;
133 }
134
Manoj Kumar AVM2405ce52015-04-02 23:13:19 -0700135 if (!IsValid(rect2)) {
Manoj Kumar AVM351c9282015-03-18 16:34:38 -0700136 return rect1;
137 }
138
Dileep Marchya7290bd22016-06-09 11:37:20 +0530139 res.left = std::min(rect1.left, rect2.left);
140 res.top = std::min(rect1.top, rect2.top);
141 res.right = std::max(rect1.right, rect2.right);
142 res.bottom = std::max(rect1.bottom, rect2.bottom);
Manoj Kumar AVM351c9282015-03-18 16:34:38 -0700143
144 return res;
Tatenda Chipeperekwa2ba97892015-02-10 12:18:34 -0800145}
146
Ramkumar Radhakrishnan34508112015-05-28 19:02:05 -0700147void SplitLeftRight(const LayerRect &in_rect, uint32_t split_count, uint32_t align_x,
Tatenda Chipeperekwaa8668642015-07-23 15:36:24 -0700148 bool flip_horizontal, LayerRect *out_rects) {
Ramkumar Radhakrishnan34508112015-05-28 19:02:05 -0700149 LayerRect rect_temp = in_rect;
150
151 uint32_t split_width = UINT32(rect_temp.right - rect_temp.left) / split_count;
Tatenda Chipeperekwaa8668642015-07-23 15:36:24 -0700152 float aligned_width = FLOAT(CeilToMultipleOf(split_width, align_x));
Ramkumar Radhakrishnan34508112015-05-28 19:02:05 -0700153
154 for (uint32_t count = 0; count < split_count; count++) {
Tatenda Chipeperekwaa8668642015-07-23 15:36:24 -0700155 float aligned_right = rect_temp.left + aligned_width;
Ramkumar Radhakrishnan34508112015-05-28 19:02:05 -0700156 out_rects[count].left = rect_temp.left;
Dileep Marchya7290bd22016-06-09 11:37:20 +0530157 out_rects[count].right = std::min(rect_temp.right, aligned_right);
Ramkumar Radhakrishnan34508112015-05-28 19:02:05 -0700158 out_rects[count].top = rect_temp.top;
159 out_rects[count].bottom = rect_temp.bottom;
160
161 rect_temp.left = out_rects[count].right;
162
163 Log(kTagRotator, "SplitLeftRight", out_rects[count]);
164 }
Tatenda Chipeperekwaa8668642015-07-23 15:36:24 -0700165
166 // If we have a horizontal flip, then we should be splitting the source from right to left
167 // to ensure that the right split will have an aligned width that matches the alignment on the
168 // destination.
169 if (flip_horizontal && split_count > 1) {
170 out_rects[0].right = out_rects[0].left + (out_rects[1].right - out_rects[1].left);
171 out_rects[1].left = out_rects[0].right;
172 Log(kTagRotator, "Adjusted Left", out_rects[0]);
173 Log(kTagRotator, "Adjusted Right", out_rects[1]);
174 }
Ramkumar Radhakrishnan34508112015-05-28 19:02:05 -0700175}
176
177void SplitTopBottom(const LayerRect &in_rect, uint32_t split_count, uint32_t align_y,
Tatenda Chipeperekwaa8668642015-07-23 15:36:24 -0700178 bool flip_horizontal, LayerRect *out_rects) {
Ramkumar Radhakrishnan34508112015-05-28 19:02:05 -0700179 LayerRect rect_temp = in_rect;
180
181 uint32_t split_height = UINT32(rect_temp.bottom - rect_temp.top) / split_count;
Tatenda Chipeperekwaa8668642015-07-23 15:36:24 -0700182 float aligned_height = FLOAT(CeilToMultipleOf(split_height, align_y));
Ramkumar Radhakrishnan34508112015-05-28 19:02:05 -0700183
184 for (uint32_t count = 0; count < split_count; count++) {
Tatenda Chipeperekwaa8668642015-07-23 15:36:24 -0700185 float aligned_bottom = rect_temp.top + aligned_height;
Ramkumar Radhakrishnan34508112015-05-28 19:02:05 -0700186 out_rects[count].top = rect_temp.top;
Dileep Marchya7290bd22016-06-09 11:37:20 +0530187 out_rects[count].bottom = std::min(rect_temp.bottom, aligned_bottom);
Ramkumar Radhakrishnan34508112015-05-28 19:02:05 -0700188 out_rects[count].left = rect_temp.left;
189 out_rects[count].right = rect_temp.right;
190
191 rect_temp.top = out_rects[count].bottom;
192
193 Log(kTagRotator, "SplitTopBottom", out_rects[count]);
194 }
Tatenda Chipeperekwaa8668642015-07-23 15:36:24 -0700195
196 // If we have a horizontal flip, then we should be splitting the destination from bottom to top
197 // to ensure that the bottom split's y-offset is aligned correctly after we swap the destinations
198 // while accounting for the flip.
199 if (flip_horizontal && split_count > 1) {
200 out_rects[0].bottom = out_rects[0].top + (out_rects[1].bottom - out_rects[1].top);
201 out_rects[1].top = out_rects[0].bottom;
202 Log(kTagRotator, "Adjusted Top", out_rects[0]);
203 Log(kTagRotator, "Adjusted Bottom", out_rects[1]);
204 }
Ramkumar Radhakrishnan34508112015-05-28 19:02:05 -0700205}
206
Ramkumar Radhakrishnanb27735f2016-08-26 22:37:23 -0700207void MapRect(const LayerRect &src_domain, const LayerRect &dst_domain, const LayerRect &in_rect,
208 LayerRect *out_rect) {
Ramkumar Radhakrishnan68840472016-05-09 13:01:25 -0700209 if (!IsValid(src_domain) || !IsValid(dst_domain) || !IsValid(in_rect)) {
210 return;
211 }
212
Prabhanjan Kandula6064cad2016-11-02 16:55:19 -0700213 int x_offset = INT(src_domain.left);
214 int y_offset = INT(src_domain.top);
215
216 LayerRect modified_in_rect = Reposition(in_rect, -x_offset, -y_offset);
Ramkumar Radhakrishnan68840472016-05-09 13:01:25 -0700217 float src_domain_width = src_domain.right - src_domain.left;
218 float src_domain_height = src_domain.bottom - src_domain.top;
219 float dst_domain_width = dst_domain.right - dst_domain.left;
220 float dst_domain_height = dst_domain.bottom - dst_domain.top;
221
222 float width_ratio = dst_domain_width / src_domain_width;
223 float height_ratio = dst_domain_height / src_domain_height;
224
Prabhanjan Kandula6064cad2016-11-02 16:55:19 -0700225 out_rect->left = dst_domain.left + (width_ratio * modified_in_rect.left);
226 out_rect->top = dst_domain.top + (height_ratio * modified_in_rect.top);
227 out_rect->right = dst_domain.left + (width_ratio * modified_in_rect.right);
228 out_rect->bottom = dst_domain.top + (height_ratio * modified_in_rect.bottom);
Ramkumar Radhakrishnan68840472016-05-09 13:01:25 -0700229}
230
Pullakavi Srinivas5de9c632017-01-30 20:30:39 +0530231void TransformHV(const LayerRect &src_domain, const LayerRect &in_rect,
232 const LayerTransform &transform, LayerRect *out_rect) {
Pullakavi Srinivas1494a932016-12-30 12:51:06 +0530233 if (!IsValid(src_domain) || !IsValid(in_rect)) {
234 return;
235 }
236
237 float in_width = in_rect.right - in_rect.left;
238 float in_height = in_rect.bottom - in_rect.top;
Pullakavi Srinivas5de9c632017-01-30 20:30:39 +0530239 float x_offset = in_rect.left - src_domain.left;
240 float y_offset = in_rect.top - src_domain.top;
241 *out_rect = in_rect;
Pullakavi Srinivas1494a932016-12-30 12:51:06 +0530242
Pullakavi Srinivas5de9c632017-01-30 20:30:39 +0530243 if (transform.flip_horizontal) {
244 out_rect->right = src_domain.right - x_offset;
245 out_rect->left = out_rect->right - in_width;
246 }
247
248 if (transform.flip_vertical) {
249 out_rect->bottom = src_domain.bottom - y_offset;
250 out_rect->top = out_rect->bottom - in_height;
251 }
Pullakavi Srinivas1494a932016-12-30 12:51:06 +0530252}
253
Ramkumar Radhakrishnan68840472016-05-09 13:01:25 -0700254RectOrientation GetOrientation(const LayerRect &in_rect) {
255 if (!IsValid(in_rect)) {
256 return kOrientationUnknown;
257 }
258
259 float input_width = in_rect.right - in_rect.left;
260 float input_height = in_rect.bottom - in_rect.top;
261
262 if (input_width < input_height) {
263 return kOrientationPortrait;
264 }
265
266 return kOrientationLandscape;
267}
268
Rohit Kulkarni8622e362017-01-30 18:14:10 -0800269DisplayError GetCropAndDestination(const LayerRect &crop, const LayerRect &dst,
270 const bool rotated90, float *crop_width,
271 float *crop_height, float *dst_width,
272 float *dst_height) {
273 if (!IsValid(crop)) {
274 Log(kTagResources, "Invalid crop rect", crop);
275 return kErrorNotSupported;
276 }
277
278 if (!IsValid(dst)) {
279 Log(kTagResources, "Invalid dst rect", dst);
280 return kErrorNotSupported;
281 }
282
283 *crop_width = crop.right - crop.left;
284 *crop_height = crop.bottom - crop.top;
285 if (rotated90) {
286 std::swap(*crop_width, *crop_height);
287 }
288
289 *dst_width = dst.right - dst.left;
290 *dst_height = dst.bottom - dst.top;
291
292 return kErrorNone;
293}
294
295DisplayError GetScaleFactor(const LayerRect &crop, const LayerRect &dst,
296 bool rotated90, float *scale_x, float *scale_y) {
297 float crop_width = 1.0f, crop_height = 1.0f, dst_width = 1.0f, dst_height = 1.0f;
298
299 DisplayError error = GetCropAndDestination(crop, dst, rotated90, &crop_width, &crop_height,
300 &dst_width, &dst_height);
301 if (error != kErrorNone) {
302 return error;
303 }
304
305 *scale_x = crop_width / dst_width;
306 *scale_y = crop_height / dst_height;
307
308 return kErrorNone;
309}
310
Dileep Marchya73d002e2015-05-08 18:58:33 -0700311} // namespace sdm
Manoj Kumar AVMb86bc172015-01-28 21:20:04 -0800312