1 /*
2 * Copyright (c) 2015-2017, The Linux Foundation. All rights reserved.
3 *
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 
30 #include <math.h>
31 #include <utils/rect.h>
32 #include <utils/constants.h>
33 #include <algorithm>
34 
35 #define __CLASS__ "RectUtils"
36 
37 namespace sdm {
38 
IsValid(const LayerRect & rect)39 bool IsValid(const LayerRect &rect) {
40   return ((rect.bottom > rect.top) && (rect.right > rect.left));
41 }
42 
IsCongruent(const LayerRect & rect1,const LayerRect & rect2)43 bool 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 }
49 
Log(DebugTag debug_tag,const char * prefix,const LayerRect & roi)50 void Log(DebugTag debug_tag, const char *prefix, const LayerRect &roi) {
51   DLOGV_IF(debug_tag, "%s: left = %.0f, top = %.0f, right = %.0f, bottom = %.0f",
52            prefix, roi.left, roi.top, roi.right, roi.bottom);
53 }
54 
Normalize(const uint32_t & align_x,const uint32_t & align_y,LayerRect * rect)55 void Normalize(const uint32_t &align_x, const uint32_t &align_y, LayerRect *rect) {
56     rect->left = ROUND_UP_ALIGN_UP(rect->left, align_x);
57     rect->right = ROUND_UP_ALIGN_DOWN(rect->right, align_x);
58     rect->top = ROUND_UP_ALIGN_UP(rect->top, align_y);
59     rect->bottom = ROUND_UP_ALIGN_DOWN(rect->bottom, align_y);
60 }
61 
Intersection(const LayerRect & rect1,const LayerRect & rect2)62 LayerRect Intersection(const LayerRect &rect1, const LayerRect &rect2) {
63   LayerRect res;
64 
65   if (!IsValid(rect1) || !IsValid(rect2)) {
66     return LayerRect();
67   }
68 
69   res.left = std::max(rect1.left, rect2.left);
70   res.top = std::max(rect1.top, rect2.top);
71   res.right = std::min(rect1.right, rect2.right);
72   res.bottom = std::min(rect1.bottom, rect2.bottom);
73 
74   if (!IsValid(res)) {
75     return LayerRect();
76   }
77 
78   return res;
79 }
80 
Reposition(const LayerRect & rect,const int & x_offset,const int & y_offset)81 LayerRect Reposition(const LayerRect &rect, const int &x_offset, const int &y_offset) {
82   LayerRect res;
83 
84   if (!IsValid(rect)) {
85     return LayerRect();
86   }
87 
88   res.left = rect.left + FLOAT(x_offset);
89   res.top = rect.top + FLOAT(y_offset);
90   res.right = rect.right + FLOAT(x_offset);
91   res.bottom = rect.bottom + FLOAT(y_offset);
92 
93   return res;
94 }
95 
96 // Not a geometrical rect deduction. Deducts rect2 from rect1 only if it results a single rect
Subtract(const LayerRect & rect1,const LayerRect & rect2)97 LayerRect Subtract(const LayerRect &rect1, const LayerRect &rect2) {
98   LayerRect res;
99 
100   res = rect1;
101 
102   if ((rect1.left == rect2.left) && (rect1.right == rect2.right)) {
103     if ((rect1.top == rect2.top) && (rect2.bottom <= rect1.bottom)) {
104       res.top = rect2.bottom;
105     } else if ((rect1.bottom == rect2.bottom) && (rect2.top >= rect1.top)) {
106       res.bottom = rect2.top;
107     }
108   } else if ((rect1.top == rect2.top) && (rect1.bottom == rect2.bottom)) {
109     if ((rect1.left == rect2.left) && (rect2.right <= rect1.right)) {
110       res.left = rect2.right;
111     } else if ((rect1.right == rect2.right) && (rect2.left >= rect1.left)) {
112       res.right = rect2.left;
113     }
114   }
115 
116   return res;
117 }
118 
Union(const LayerRect & rect1,const LayerRect & rect2)119 LayerRect Union(const LayerRect &rect1, const LayerRect &rect2) {
120   LayerRect res;
121 
122   if (!IsValid(rect1) && !IsValid(rect2)) {
123     return LayerRect();
124   }
125 
126   if (!IsValid(rect1)) {
127     return rect2;
128   }
129 
130   if (!IsValid(rect2)) {
131     return rect1;
132   }
133 
134   res.left = std::min(rect1.left, rect2.left);
135   res.top = std::min(rect1.top, rect2.top);
136   res.right = std::max(rect1.right, rect2.right);
137   res.bottom = std::max(rect1.bottom, rect2.bottom);
138 
139   return res;
140 }
141 
SplitLeftRight(const LayerRect & in_rect,uint32_t split_count,uint32_t align_x,bool flip_horizontal,LayerRect * out_rects)142 void SplitLeftRight(const LayerRect &in_rect, uint32_t split_count, uint32_t align_x,
143                     bool flip_horizontal, LayerRect *out_rects) {
144   LayerRect rect_temp = in_rect;
145 
146   uint32_t split_width = UINT32(rect_temp.right - rect_temp.left) / split_count;
147   float aligned_width = FLOAT(CeilToMultipleOf(split_width, align_x));
148 
149   for (uint32_t count = 0; count < split_count; count++) {
150     float aligned_right = rect_temp.left + aligned_width;
151     out_rects[count].left = rect_temp.left;
152     out_rects[count].right = std::min(rect_temp.right, aligned_right);
153     out_rects[count].top = rect_temp.top;
154     out_rects[count].bottom = rect_temp.bottom;
155 
156     rect_temp.left = out_rects[count].right;
157 
158     Log(kTagRotator, "SplitLeftRight", out_rects[count]);
159   }
160 
161   // If we have a horizontal flip, then we should be splitting the source from right to left
162   // to ensure that the right split will have an aligned width that matches the alignment on the
163   // destination.
164   if (flip_horizontal && split_count > 1) {
165     out_rects[0].right = out_rects[0].left + (out_rects[1].right - out_rects[1].left);
166     out_rects[1].left = out_rects[0].right;
167     Log(kTagRotator, "Adjusted Left", out_rects[0]);
168     Log(kTagRotator, "Adjusted Right", out_rects[1]);
169   }
170 }
171 
SplitTopBottom(const LayerRect & in_rect,uint32_t split_count,uint32_t align_y,bool flip_horizontal,LayerRect * out_rects)172 void SplitTopBottom(const LayerRect &in_rect, uint32_t split_count, uint32_t align_y,
173                     bool flip_horizontal, LayerRect *out_rects) {
174   LayerRect rect_temp = in_rect;
175 
176   uint32_t split_height = UINT32(rect_temp.bottom - rect_temp.top) / split_count;
177   float aligned_height = FLOAT(CeilToMultipleOf(split_height, align_y));
178 
179   for (uint32_t count = 0; count < split_count; count++) {
180     float aligned_bottom = rect_temp.top + aligned_height;
181     out_rects[count].top = rect_temp.top;
182     out_rects[count].bottom = std::min(rect_temp.bottom, aligned_bottom);
183     out_rects[count].left = rect_temp.left;
184     out_rects[count].right = rect_temp.right;
185 
186     rect_temp.top = out_rects[count].bottom;
187 
188     Log(kTagRotator, "SplitTopBottom", out_rects[count]);
189   }
190 
191   // If we have a horizontal flip, then we should be splitting the destination from bottom to top
192   // to ensure that the bottom split's y-offset is aligned correctly after we swap the destinations
193   // while accounting for the flip.
194   if (flip_horizontal && split_count > 1) {
195     out_rects[0].bottom = out_rects[0].top + (out_rects[1].bottom - out_rects[1].top);
196     out_rects[1].top = out_rects[0].bottom;
197     Log(kTagRotator, "Adjusted Top", out_rects[0]);
198     Log(kTagRotator, "Adjusted Bottom", out_rects[1]);
199   }
200 }
201 
MapRect(const LayerRect & src_domain,const LayerRect & dst_domain,const LayerRect & in_rect,LayerRect * out_rect)202 void MapRect(const LayerRect &src_domain, const LayerRect &dst_domain, const LayerRect &in_rect,
203              LayerRect *out_rect) {
204   if (!IsValid(src_domain) || !IsValid(dst_domain) || !IsValid(in_rect)) {
205     return;
206   }
207 
208   int x_offset = INT(src_domain.left);
209   int y_offset = INT(src_domain.top);
210 
211   LayerRect modified_in_rect = Reposition(in_rect, -x_offset, -y_offset);
212   float src_domain_width = src_domain.right - src_domain.left;
213   float src_domain_height = src_domain.bottom - src_domain.top;
214   float dst_domain_width = dst_domain.right - dst_domain.left;
215   float dst_domain_height = dst_domain.bottom - dst_domain.top;
216 
217   float width_ratio = dst_domain_width / src_domain_width;
218   float height_ratio = dst_domain_height / src_domain_height;
219 
220   out_rect->left = dst_domain.left + (width_ratio * modified_in_rect.left);
221   out_rect->top = dst_domain.top + (height_ratio * modified_in_rect.top);
222   out_rect->right = dst_domain.left + (width_ratio * modified_in_rect.right);
223   out_rect->bottom = dst_domain.top + (height_ratio * modified_in_rect.bottom);
224 }
225 
TransformHV(const LayerRect & src_domain,const LayerRect & in_rect,const LayerTransform & transform,LayerRect * out_rect)226 void TransformHV(const LayerRect &src_domain, const LayerRect &in_rect,
227                  const LayerTransform &transform, LayerRect *out_rect) {
228   if (!IsValid(src_domain) || !IsValid(in_rect)) {
229     return;
230   }
231 
232   float in_width = in_rect.right - in_rect.left;
233   float in_height = in_rect.bottom - in_rect.top;
234   float x_offset = in_rect.left - src_domain.left;
235   float y_offset = in_rect.top - src_domain.top;
236   *out_rect = in_rect;
237 
238   if (transform.flip_horizontal) {
239     out_rect->right = src_domain.right - x_offset;
240     out_rect->left = out_rect->right - in_width;
241   }
242 
243   if (transform.flip_vertical) {
244     out_rect->bottom = src_domain.bottom - y_offset;
245     out_rect->top = out_rect->bottom - in_height;
246   }
247 }
248 
GetOrientation(const LayerRect & in_rect)249 RectOrientation GetOrientation(const LayerRect &in_rect) {
250   if (!IsValid(in_rect)) {
251     return kOrientationUnknown;
252   }
253 
254   float input_width = in_rect.right - in_rect.left;
255   float input_height = in_rect.bottom - in_rect.top;
256 
257   if (input_width < input_height) {
258     return kOrientationPortrait;
259   }
260 
261   return kOrientationLandscape;
262 }
263 
264 }  // namespace sdm
265 
266