1 /*
2 * Copyright (c) 2015, 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 
34 #define __CLASS__ "RectUtils"
35 
36 namespace sdm {
37 
IsValid(const LayerRect & rect)38 bool IsValid(const LayerRect &rect) {
39   return ((rect.bottom > rect.top) && (rect.right > rect.left));
40 }
41 
IsCongruent(const LayerRect & rect1,const LayerRect & rect2)42 bool IsCongruent(const LayerRect &rect1, const LayerRect &rect2) {
43   return ((rect1.left == rect2.left) &&
44           (rect1.top == rect2.top) &&
45           (rect1.right == rect2.right) &&
46           (rect1.bottom == rect2.bottom));
47 }
48 
Log(DebugTag debug_tag,const char * prefix,const LayerRect & roi)49 void Log(DebugTag debug_tag, const char *prefix, const LayerRect &roi) {
50   DLOGV_IF(debug_tag, "%s: left = %.0f, top = %.0f, right = %.0f, bottom = %.0f",
51            prefix, roi.left, roi.top, roi.right, roi.bottom);
52 }
53 
Normalize(const uint32_t & align_x,const uint32_t & align_y,LayerRect * rect)54 void Normalize(const uint32_t &align_x, const uint32_t &align_y, LayerRect *rect) {
55     rect->left = ROUND_UP_ALIGN_UP(rect->left, align_x);
56     rect->right = ROUND_UP_ALIGN_DOWN(rect->right, align_x);
57     rect->top = ROUND_UP_ALIGN_UP(rect->top, align_y);
58     rect->bottom = ROUND_UP_ALIGN_DOWN(rect->bottom, align_y);
59 }
60 
Intersection(const LayerRect & rect1,const LayerRect & rect2)61 LayerRect Intersection(const LayerRect &rect1, const LayerRect &rect2) {
62   LayerRect res;
63 
64   if (!IsValid(rect1) || !IsValid(rect2)) {
65     return LayerRect();
66   }
67 
68   res.left = MAX(rect1.left, rect2.left);
69   res.top = MAX(rect1.top, rect2.top);
70   res.right = MIN(rect1.right, rect2.right);
71   res.bottom = MIN(rect1.bottom, rect2.bottom);
72 
73   if (!IsValid(res)) {
74     return LayerRect();
75   }
76 
77   return res;
78 }
79 
Reposition(const LayerRect & rect,const int & x_offset,const int & y_offset)80 LayerRect Reposition(const LayerRect &rect, const int &x_offset, const int &y_offset) {
81   LayerRect res;
82 
83   if (!IsValid(rect)) {
84     return LayerRect();
85   }
86 
87   res.left = rect.left + FLOAT(x_offset);
88   res.top = rect.top + FLOAT(y_offset);
89   res.right = rect.right + FLOAT(x_offset);
90   res.bottom = rect.bottom + FLOAT(y_offset);
91 
92   return res;
93 }
94 
95 // Not a geometrical rect deduction. Deducts rect2 from rect1 only if it results a single rect
Subtract(const LayerRect & rect1,const LayerRect & rect2)96 LayerRect Subtract(const LayerRect &rect1, const LayerRect &rect2) {
97   LayerRect res;
98 
99   res = rect1;
100 
101   if ((rect1.left == rect2.left) && (rect1.right == rect2.right)) {
102     if ((rect1.top == rect2.top) && (rect2.bottom <= rect1.bottom)) {
103       res.top = rect2.bottom;
104     } else if ((rect1.bottom == rect2.bottom) && (rect2.top >= rect1.top)) {
105       res.bottom = rect2.top;
106     }
107   } else if ((rect1.top == rect2.top) && (rect1.bottom == rect2.bottom)) {
108     if ((rect1.left == rect2.left) && (rect2.right <= rect1.right)) {
109       res.left = rect2.right;
110     } else if ((rect1.right == rect2.right) && (rect2.left >= rect1.left)) {
111       res.right = rect2.left;
112     }
113   }
114 
115   return res;
116 }
117 
Union(const LayerRect & rect1,const LayerRect & rect2)118 LayerRect Union(const LayerRect &rect1, const LayerRect &rect2) {
119   LayerRect res;
120 
121   if (!IsValid(rect1) && !IsValid(rect2)) {
122     return LayerRect();
123   }
124 
125   if (!IsValid(rect1)) {
126     return rect2;
127   }
128 
129   if (!IsValid(rect2)) {
130     return rect1;
131   }
132 
133   res.left = MIN(rect1.left, rect2.left);
134   res.top = MIN(rect1.top, rect2.top);
135   res.right = MAX(rect1.right, rect2.right);
136   res.bottom = MAX(rect1.bottom, rect2.bottom);
137 
138   return res;
139 }
140 
SplitLeftRight(const LayerRect & in_rect,uint32_t split_count,uint32_t align_x,bool flip_horizontal,LayerRect * out_rects)141 void SplitLeftRight(const LayerRect &in_rect, uint32_t split_count, uint32_t align_x,
142                     bool flip_horizontal, LayerRect *out_rects) {
143   LayerRect rect_temp = in_rect;
144 
145   uint32_t split_width = UINT32(rect_temp.right - rect_temp.left) / split_count;
146   float aligned_width = FLOAT(CeilToMultipleOf(split_width, align_x));
147 
148   for (uint32_t count = 0; count < split_count; count++) {
149     float aligned_right = rect_temp.left + aligned_width;
150     out_rects[count].left = rect_temp.left;
151     out_rects[count].right = MIN(rect_temp.right, aligned_right);
152     out_rects[count].top = rect_temp.top;
153     out_rects[count].bottom = rect_temp.bottom;
154 
155     rect_temp.left = out_rects[count].right;
156 
157     Log(kTagRotator, "SplitLeftRight", out_rects[count]);
158   }
159 
160   // If we have a horizontal flip, then we should be splitting the source from right to left
161   // to ensure that the right split will have an aligned width that matches the alignment on the
162   // destination.
163   if (flip_horizontal && split_count > 1) {
164     out_rects[0].right = out_rects[0].left + (out_rects[1].right - out_rects[1].left);
165     out_rects[1].left = out_rects[0].right;
166     Log(kTagRotator, "Adjusted Left", out_rects[0]);
167     Log(kTagRotator, "Adjusted Right", out_rects[1]);
168   }
169 }
170 
SplitTopBottom(const LayerRect & in_rect,uint32_t split_count,uint32_t align_y,bool flip_horizontal,LayerRect * out_rects)171 void SplitTopBottom(const LayerRect &in_rect, uint32_t split_count, uint32_t align_y,
172                     bool flip_horizontal, LayerRect *out_rects) {
173   LayerRect rect_temp = in_rect;
174 
175   uint32_t split_height = UINT32(rect_temp.bottom - rect_temp.top) / split_count;
176   float aligned_height = FLOAT(CeilToMultipleOf(split_height, align_y));
177 
178   for (uint32_t count = 0; count < split_count; count++) {
179     float aligned_bottom = rect_temp.top + aligned_height;
180     out_rects[count].top = rect_temp.top;
181     out_rects[count].bottom = MIN(rect_temp.bottom, aligned_bottom);
182     out_rects[count].left = rect_temp.left;
183     out_rects[count].right = rect_temp.right;
184 
185     rect_temp.top = out_rects[count].bottom;
186 
187     Log(kTagRotator, "SplitTopBottom", out_rects[count]);
188   }
189 
190   // If we have a horizontal flip, then we should be splitting the destination from bottom to top
191   // to ensure that the bottom split's y-offset is aligned correctly after we swap the destinations
192   // while accounting for the flip.
193   if (flip_horizontal && split_count > 1) {
194     out_rects[0].bottom = out_rects[0].top + (out_rects[1].bottom - out_rects[1].top);
195     out_rects[1].top = out_rects[0].bottom;
196     Log(kTagRotator, "Adjusted Top", out_rects[0]);
197     Log(kTagRotator, "Adjusted Bottom", out_rects[1]);
198   }
199 }
200 
201 }  // namespace sdm
202 
203