xref: /openbmc/linux/include/drm/drm_rect.h (revision 0883c2c0)
1 /*
2  * Copyright (C) 2011-2013 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 
24 #ifndef DRM_RECT_H
25 #define DRM_RECT_H
26 
27 /**
28  * DOC: rect utils
29  *
30  * Utility functions to help manage rectangular areas for
31  * clipping, scaling, etc. calculations.
32  */
33 
34 /**
35  * struct drm_rect - two dimensional rectangle
36  * @x1: horizontal starting coordinate (inclusive)
37  * @x2: horizontal ending coordinate (exclusive)
38  * @y1: vertical starting coordinate (inclusive)
39  * @y2: vertical ending coordinate (exclusive)
40  */
41 struct drm_rect {
42 	int x1, y1, x2, y2;
43 };
44 
45 /**
46  * drm_rect_adjust_size - adjust the size of the rectangle
47  * @r: rectangle to be adjusted
48  * @dw: horizontal adjustment
49  * @dh: vertical adjustment
50  *
51  * Change the size of rectangle @r by @dw in the horizontal direction,
52  * and by @dh in the vertical direction, while keeping the center
53  * of @r stationary.
54  *
55  * Positive @dw and @dh increase the size, negative values decrease it.
56  */
57 static inline void drm_rect_adjust_size(struct drm_rect *r, int dw, int dh)
58 {
59 	r->x1 -= dw >> 1;
60 	r->y1 -= dh >> 1;
61 	r->x2 += (dw + 1) >> 1;
62 	r->y2 += (dh + 1) >> 1;
63 }
64 
65 /**
66  * drm_rect_translate - translate the rectangle
67  * @r: rectangle to be tranlated
68  * @dx: horizontal translation
69  * @dy: vertical translation
70  *
71  * Move rectangle @r by @dx in the horizontal direction,
72  * and by @dy in the vertical direction.
73  */
74 static inline void drm_rect_translate(struct drm_rect *r, int dx, int dy)
75 {
76 	r->x1 += dx;
77 	r->y1 += dy;
78 	r->x2 += dx;
79 	r->y2 += dy;
80 }
81 
82 /**
83  * drm_rect_downscale - downscale a rectangle
84  * @r: rectangle to be downscaled
85  * @horz: horizontal downscale factor
86  * @vert: vertical downscale factor
87  *
88  * Divide the coordinates of rectangle @r by @horz and @vert.
89  */
90 static inline void drm_rect_downscale(struct drm_rect *r, int horz, int vert)
91 {
92 	r->x1 /= horz;
93 	r->y1 /= vert;
94 	r->x2 /= horz;
95 	r->y2 /= vert;
96 }
97 
98 /**
99  * drm_rect_width - determine the rectangle width
100  * @r: rectangle whose width is returned
101  *
102  * RETURNS:
103  * The width of the rectangle.
104  */
105 static inline int drm_rect_width(const struct drm_rect *r)
106 {
107 	return r->x2 - r->x1;
108 }
109 
110 /**
111  * drm_rect_height - determine the rectangle height
112  * @r: rectangle whose height is returned
113  *
114  * RETURNS:
115  * The height of the rectangle.
116  */
117 static inline int drm_rect_height(const struct drm_rect *r)
118 {
119 	return r->y2 - r->y1;
120 }
121 
122 /**
123  * drm_rect_visible - determine if the the rectangle is visible
124  * @r: rectangle whose visibility is returned
125  *
126  * RETURNS:
127  * %true if the rectangle is visible, %false otherwise.
128  */
129 static inline bool drm_rect_visible(const struct drm_rect *r)
130 {
131 	return drm_rect_width(r) > 0 && drm_rect_height(r) > 0;
132 }
133 
134 /**
135  * drm_rect_equals - determine if two rectangles are equal
136  * @r1: first rectangle
137  * @r2: second rectangle
138  *
139  * RETURNS:
140  * %true if the rectangles are equal, %false otherwise.
141  */
142 static inline bool drm_rect_equals(const struct drm_rect *r1,
143 				   const struct drm_rect *r2)
144 {
145 	return r1->x1 == r2->x1 && r1->x2 == r2->x2 &&
146 		r1->y1 == r2->y1 && r1->y2 == r2->y2;
147 }
148 
149 bool drm_rect_intersect(struct drm_rect *r, const struct drm_rect *clip);
150 bool drm_rect_clip_scaled(struct drm_rect *src, struct drm_rect *dst,
151 			  const struct drm_rect *clip,
152 			  int hscale, int vscale);
153 int drm_rect_calc_hscale(const struct drm_rect *src,
154 			 const struct drm_rect *dst,
155 			 int min_hscale, int max_hscale);
156 int drm_rect_calc_vscale(const struct drm_rect *src,
157 			 const struct drm_rect *dst,
158 			 int min_vscale, int max_vscale);
159 int drm_rect_calc_hscale_relaxed(struct drm_rect *src,
160 				 struct drm_rect *dst,
161 				 int min_hscale, int max_hscale);
162 int drm_rect_calc_vscale_relaxed(struct drm_rect *src,
163 				 struct drm_rect *dst,
164 				 int min_vscale, int max_vscale);
165 void drm_rect_debug_print(const char *prefix,
166 			  const struct drm_rect *r, bool fixed_point);
167 void drm_rect_rotate(struct drm_rect *r,
168 		     int width, int height,
169 		     unsigned int rotation);
170 void drm_rect_rotate_inv(struct drm_rect *r,
171 			 int width, int height,
172 			 unsigned int rotation);
173 
174 #endif
175