xref: /openbmc/linux/include/drm/drm_rect.h (revision ac5f3136)
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 #include <linux/types.h>
28 
29 /**
30  * DOC: rect utils
31  *
32  * Utility functions to help manage rectangular areas for
33  * clipping, scaling, etc. calculations.
34  */
35 
36 /**
37  * struct drm_rect - two dimensional rectangle
38  * @x1: horizontal starting coordinate (inclusive)
39  * @x2: horizontal ending coordinate (exclusive)
40  * @y1: vertical starting coordinate (inclusive)
41  * @y2: vertical ending coordinate (exclusive)
42  *
43  * Note that this must match the layout of struct drm_mode_rect or the damage
44  * helpers like drm_atomic_helper_damage_iter_init() break.
45  */
46 struct drm_rect {
47 	int x1, y1, x2, y2;
48 };
49 
50 /**
51  * DRM_RECT_FMT - printf string for &struct drm_rect
52  */
53 #define DRM_RECT_FMT    "%dx%d%+d%+d"
54 /**
55  * DRM_RECT_ARG - printf arguments for &struct drm_rect
56  * @r: rectangle struct
57  */
58 #define DRM_RECT_ARG(r) drm_rect_width(r), drm_rect_height(r), (r)->x1, (r)->y1
59 
60 /**
61  * DRM_RECT_FP_FMT - printf string for &struct drm_rect in 16.16 fixed point
62  */
63 #define DRM_RECT_FP_FMT "%d.%06ux%d.%06u%+d.%06u%+d.%06u"
64 /**
65  * DRM_RECT_FP_ARG - printf arguments for &struct drm_rect in 16.16 fixed point
66  * @r: rectangle struct
67  *
68  * This is useful for e.g. printing plane source rectangles, which are in 16.16
69  * fixed point.
70  */
71 #define DRM_RECT_FP_ARG(r) \
72 		drm_rect_width(r) >> 16, ((drm_rect_width(r) & 0xffff) * 15625) >> 10, \
73 		drm_rect_height(r) >> 16, ((drm_rect_height(r) & 0xffff) * 15625) >> 10, \
74 		(r)->x1 >> 16, (((r)->x1 & 0xffff) * 15625) >> 10, \
75 		(r)->y1 >> 16, (((r)->y1 & 0xffff) * 15625) >> 10
76 
77 /**
78  * drm_rect_init - initialize the rectangle from x/y/w/h
79  * @r: rectangle
80  * @x: x coordinate
81  * @y: y coordinate
82  * @width: width
83  * @height: height
84  */
85 static inline void drm_rect_init(struct drm_rect *r, int x, int y,
86 				 int width, int height)
87 {
88 	r->x1 = x;
89 	r->y1 = y;
90 	r->x2 = x + width;
91 	r->y2 = y + height;
92 }
93 
94 /**
95  * drm_rect_adjust_size - adjust the size of the rectangle
96  * @r: rectangle to be adjusted
97  * @dw: horizontal adjustment
98  * @dh: vertical adjustment
99  *
100  * Change the size of rectangle @r by @dw in the horizontal direction,
101  * and by @dh in the vertical direction, while keeping the center
102  * of @r stationary.
103  *
104  * Positive @dw and @dh increase the size, negative values decrease it.
105  */
106 static inline void drm_rect_adjust_size(struct drm_rect *r, int dw, int dh)
107 {
108 	r->x1 -= dw >> 1;
109 	r->y1 -= dh >> 1;
110 	r->x2 += (dw + 1) >> 1;
111 	r->y2 += (dh + 1) >> 1;
112 }
113 
114 /**
115  * drm_rect_translate - translate the rectangle
116  * @r: rectangle to be tranlated
117  * @dx: horizontal translation
118  * @dy: vertical translation
119  *
120  * Move rectangle @r by @dx in the horizontal direction,
121  * and by @dy in the vertical direction.
122  */
123 static inline void drm_rect_translate(struct drm_rect *r, int dx, int dy)
124 {
125 	r->x1 += dx;
126 	r->y1 += dy;
127 	r->x2 += dx;
128 	r->y2 += dy;
129 }
130 
131 /**
132  * drm_rect_translate_to - translate the rectangle to an absolute position
133  * @r: rectangle to be tranlated
134  * @x: horizontal position
135  * @y: vertical position
136  *
137  * Move rectangle @r to @x in the horizontal direction,
138  * and to @y in the vertical direction.
139  */
140 static inline void drm_rect_translate_to(struct drm_rect *r, int x, int y)
141 {
142 	drm_rect_translate(r, x - r->x1, y - r->y1);
143 }
144 
145 /**
146  * drm_rect_downscale - downscale a rectangle
147  * @r: rectangle to be downscaled
148  * @horz: horizontal downscale factor
149  * @vert: vertical downscale factor
150  *
151  * Divide the coordinates of rectangle @r by @horz and @vert.
152  */
153 static inline void drm_rect_downscale(struct drm_rect *r, int horz, int vert)
154 {
155 	r->x1 /= horz;
156 	r->y1 /= vert;
157 	r->x2 /= horz;
158 	r->y2 /= vert;
159 }
160 
161 /**
162  * drm_rect_width - determine the rectangle width
163  * @r: rectangle whose width is returned
164  *
165  * RETURNS:
166  * The width of the rectangle.
167  */
168 static inline int drm_rect_width(const struct drm_rect *r)
169 {
170 	return r->x2 - r->x1;
171 }
172 
173 /**
174  * drm_rect_height - determine the rectangle height
175  * @r: rectangle whose height is returned
176  *
177  * RETURNS:
178  * The height of the rectangle.
179  */
180 static inline int drm_rect_height(const struct drm_rect *r)
181 {
182 	return r->y2 - r->y1;
183 }
184 
185 /**
186  * drm_rect_visible - determine if the rectangle is visible
187  * @r: rectangle whose visibility is returned
188  *
189  * RETURNS:
190  * %true if the rectangle is visible, %false otherwise.
191  */
192 static inline bool drm_rect_visible(const struct drm_rect *r)
193 {
194 	return drm_rect_width(r) > 0 && drm_rect_height(r) > 0;
195 }
196 
197 /**
198  * drm_rect_equals - determine if two rectangles are equal
199  * @r1: first rectangle
200  * @r2: second rectangle
201  *
202  * RETURNS:
203  * %true if the rectangles are equal, %false otherwise.
204  */
205 static inline bool drm_rect_equals(const struct drm_rect *r1,
206 				   const struct drm_rect *r2)
207 {
208 	return r1->x1 == r2->x1 && r1->x2 == r2->x2 &&
209 		r1->y1 == r2->y1 && r1->y2 == r2->y2;
210 }
211 
212 /**
213  * drm_rect_fp_to_int - Convert a rect in 16.16 fixed point form to int form.
214  * @dst: rect to be stored the converted value
215  * @src: rect in 16.16 fixed point form
216  */
217 static inline void drm_rect_fp_to_int(struct drm_rect *dst,
218 				      const struct drm_rect *src)
219 {
220 	drm_rect_init(dst, src->x1 >> 16, src->y1 >> 16,
221 		      drm_rect_width(src) >> 16,
222 		      drm_rect_height(src) >> 16);
223 }
224 
225 bool drm_rect_intersect(struct drm_rect *r, const struct drm_rect *clip);
226 bool drm_rect_clip_scaled(struct drm_rect *src, struct drm_rect *dst,
227 			  const struct drm_rect *clip);
228 int drm_rect_calc_hscale(const struct drm_rect *src,
229 			 const struct drm_rect *dst,
230 			 int min_hscale, int max_hscale);
231 int drm_rect_calc_vscale(const struct drm_rect *src,
232 			 const struct drm_rect *dst,
233 			 int min_vscale, int max_vscale);
234 void drm_rect_debug_print(const char *prefix,
235 			  const struct drm_rect *r, bool fixed_point);
236 void drm_rect_rotate(struct drm_rect *r,
237 		     int width, int height,
238 		     unsigned int rotation);
239 void drm_rect_rotate_inv(struct drm_rect *r,
240 			 int width, int height,
241 			 unsigned int rotation);
242 
243 #endif
244