xref: /openbmc/linux/include/drm/drm_rect.h (revision 84509eed)
13512f976SVille Syrjälä /*
23512f976SVille Syrjälä  * Copyright (C) 2011-2013 Intel Corporation
33512f976SVille Syrjälä  *
43512f976SVille Syrjälä  * Permission is hereby granted, free of charge, to any person obtaining a
53512f976SVille Syrjälä  * copy of this software and associated documentation files (the "Software"),
63512f976SVille Syrjälä  * to deal in the Software without restriction, including without limitation
73512f976SVille Syrjälä  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
83512f976SVille Syrjälä  * and/or sell copies of the Software, and to permit persons to whom the
93512f976SVille Syrjälä  * Software is furnished to do so, subject to the following conditions:
103512f976SVille Syrjälä  *
113512f976SVille Syrjälä  * The above copyright notice and this permission notice (including the next
123512f976SVille Syrjälä  * paragraph) shall be included in all copies or substantial portions of the
133512f976SVille Syrjälä  * Software.
143512f976SVille Syrjälä  *
153512f976SVille Syrjälä  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
163512f976SVille Syrjälä  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
173512f976SVille Syrjälä  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
183512f976SVille Syrjälä  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
193512f976SVille Syrjälä  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
203512f976SVille Syrjälä  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
213512f976SVille Syrjälä  * SOFTWARE.
223512f976SVille Syrjälä  */
233512f976SVille Syrjälä 
243512f976SVille Syrjälä #ifndef DRM_RECT_H
253512f976SVille Syrjälä #define DRM_RECT_H
263512f976SVille Syrjälä 
2730218eb7SVille Syrjälä #include <linux/types.h>
2830218eb7SVille Syrjälä 
293512f976SVille Syrjälä /**
3003973536SVille Syrjälä  * DOC: rect utils
3103973536SVille Syrjälä  *
3203973536SVille Syrjälä  * Utility functions to help manage rectangular areas for
3303973536SVille Syrjälä  * clipping, scaling, etc. calculations.
3403973536SVille Syrjälä  */
3503973536SVille Syrjälä 
3603973536SVille Syrjälä /**
3703973536SVille Syrjälä  * struct drm_rect - two dimensional rectangle
383512f976SVille Syrjälä  * @x1: horizontal starting coordinate (inclusive)
393512f976SVille Syrjälä  * @x2: horizontal ending coordinate (exclusive)
403512f976SVille Syrjälä  * @y1: vertical starting coordinate (inclusive)
413512f976SVille Syrjälä  * @y2: vertical ending coordinate (exclusive)
426f11f374SDaniel Vetter  *
436f11f374SDaniel Vetter  * Note that this must match the layout of struct drm_mode_rect or the damage
446f11f374SDaniel Vetter  * helpers like drm_atomic_helper_damage_iter_init() break.
453512f976SVille Syrjälä  */
463512f976SVille Syrjälä struct drm_rect {
473512f976SVille Syrjälä 	int x1, y1, x2, y2;
483512f976SVille Syrjälä };
493512f976SVille Syrjälä 
503512f976SVille Syrjälä /**
51*84509eedSJosé Expósito  * DRM_RECT_INIT - initialize a rectangle from x/y/w/h
52*84509eedSJosé Expósito  * @x: x coordinate
53*84509eedSJosé Expósito  * @y: y coordinate
54*84509eedSJosé Expósito  * @w: width
55*84509eedSJosé Expósito  * @h: height
56*84509eedSJosé Expósito  *
57*84509eedSJosé Expósito  * RETURNS:
58*84509eedSJosé Expósito  * A new rectangle of the specified size.
59*84509eedSJosé Expósito  */
60*84509eedSJosé Expósito #define DRM_RECT_INIT(x, y, w, h) ((struct drm_rect){ \
61*84509eedSJosé Expósito 		.x1 = (x), \
62*84509eedSJosé Expósito 		.y1 = (y), \
63*84509eedSJosé Expósito 		.x2 = (x) + (w), \
64*84509eedSJosé Expósito 		.y2 = (y) + (h) })
65*84509eedSJosé Expósito 
66*84509eedSJosé Expósito /**
6799cdb35eSDaniel Vetter  * DRM_RECT_FMT - printf string for &struct drm_rect
6899cdb35eSDaniel Vetter  */
6999cdb35eSDaniel Vetter #define DRM_RECT_FMT    "%dx%d%+d%+d"
7099cdb35eSDaniel Vetter /**
7199cdb35eSDaniel Vetter  * DRM_RECT_ARG - printf arguments for &struct drm_rect
7299cdb35eSDaniel Vetter  * @r: rectangle struct
7399cdb35eSDaniel Vetter  */
7499cdb35eSDaniel Vetter #define DRM_RECT_ARG(r) drm_rect_width(r), drm_rect_height(r), (r)->x1, (r)->y1
7599cdb35eSDaniel Vetter 
7699cdb35eSDaniel Vetter /**
7799cdb35eSDaniel Vetter  * DRM_RECT_FP_FMT - printf string for &struct drm_rect in 16.16 fixed point
7899cdb35eSDaniel Vetter  */
7999cdb35eSDaniel Vetter #define DRM_RECT_FP_FMT "%d.%06ux%d.%06u%+d.%06u%+d.%06u"
8099cdb35eSDaniel Vetter /**
8199cdb35eSDaniel Vetter  * DRM_RECT_FP_ARG - printf arguments for &struct drm_rect in 16.16 fixed point
8299cdb35eSDaniel Vetter  * @r: rectangle struct
8399cdb35eSDaniel Vetter  *
8499cdb35eSDaniel Vetter  * This is useful for e.g. printing plane source rectangles, which are in 16.16
8599cdb35eSDaniel Vetter  * fixed point.
8699cdb35eSDaniel Vetter  */
8799cdb35eSDaniel Vetter #define DRM_RECT_FP_ARG(r) \
8899cdb35eSDaniel Vetter 		drm_rect_width(r) >> 16, ((drm_rect_width(r) & 0xffff) * 15625) >> 10, \
8999cdb35eSDaniel Vetter 		drm_rect_height(r) >> 16, ((drm_rect_height(r) & 0xffff) * 15625) >> 10, \
9099cdb35eSDaniel Vetter 		(r)->x1 >> 16, (((r)->x1 & 0xffff) * 15625) >> 10, \
9199cdb35eSDaniel Vetter 		(r)->y1 >> 16, (((r)->y1 & 0xffff) * 15625) >> 10
9299cdb35eSDaniel Vetter 
9399cdb35eSDaniel Vetter /**
94e22b86e9SVille Syrjälä  * drm_rect_init - initialize the rectangle from x/y/w/h
95e22b86e9SVille Syrjälä  * @r: rectangle
96e22b86e9SVille Syrjälä  * @x: x coordinate
97e22b86e9SVille Syrjälä  * @y: y coordinate
98e22b86e9SVille Syrjälä  * @width: width
99e22b86e9SVille Syrjälä  * @height: height
100e22b86e9SVille Syrjälä  */
drm_rect_init(struct drm_rect * r,int x,int y,int width,int height)101e22b86e9SVille Syrjälä static inline void drm_rect_init(struct drm_rect *r, int x, int y,
102e22b86e9SVille Syrjälä 				 int width, int height)
103e22b86e9SVille Syrjälä {
104e22b86e9SVille Syrjälä 	r->x1 = x;
105e22b86e9SVille Syrjälä 	r->y1 = y;
106e22b86e9SVille Syrjälä 	r->x2 = x + width;
107e22b86e9SVille Syrjälä 	r->y2 = y + height;
108e22b86e9SVille Syrjälä }
109e22b86e9SVille Syrjälä 
110e22b86e9SVille Syrjälä /**
1113512f976SVille Syrjälä  * drm_rect_adjust_size - adjust the size of the rectangle
1123512f976SVille Syrjälä  * @r: rectangle to be adjusted
1133512f976SVille Syrjälä  * @dw: horizontal adjustment
1143512f976SVille Syrjälä  * @dh: vertical adjustment
1153512f976SVille Syrjälä  *
1163512f976SVille Syrjälä  * Change the size of rectangle @r by @dw in the horizontal direction,
1173512f976SVille Syrjälä  * and by @dh in the vertical direction, while keeping the center
1183512f976SVille Syrjälä  * of @r stationary.
1193512f976SVille Syrjälä  *
1203512f976SVille Syrjälä  * Positive @dw and @dh increase the size, negative values decrease it.
1213512f976SVille Syrjälä  */
drm_rect_adjust_size(struct drm_rect * r,int dw,int dh)1223512f976SVille Syrjälä static inline void drm_rect_adjust_size(struct drm_rect *r, int dw, int dh)
1233512f976SVille Syrjälä {
1243512f976SVille Syrjälä 	r->x1 -= dw >> 1;
1253512f976SVille Syrjälä 	r->y1 -= dh >> 1;
1263512f976SVille Syrjälä 	r->x2 += (dw + 1) >> 1;
1273512f976SVille Syrjälä 	r->y2 += (dh + 1) >> 1;
1283512f976SVille Syrjälä }
1293512f976SVille Syrjälä 
1303512f976SVille Syrjälä /**
1313512f976SVille Syrjälä  * drm_rect_translate - translate the rectangle
1323512f976SVille Syrjälä  * @r: rectangle to be tranlated
1333512f976SVille Syrjälä  * @dx: horizontal translation
1343512f976SVille Syrjälä  * @dy: vertical translation
1353512f976SVille Syrjälä  *
1363512f976SVille Syrjälä  * Move rectangle @r by @dx in the horizontal direction,
1373512f976SVille Syrjälä  * and by @dy in the vertical direction.
1383512f976SVille Syrjälä  */
drm_rect_translate(struct drm_rect * r,int dx,int dy)1393512f976SVille Syrjälä static inline void drm_rect_translate(struct drm_rect *r, int dx, int dy)
1403512f976SVille Syrjälä {
1413512f976SVille Syrjälä 	r->x1 += dx;
1423512f976SVille Syrjälä 	r->y1 += dy;
1433512f976SVille Syrjälä 	r->x2 += dx;
1443512f976SVille Syrjälä 	r->y2 += dy;
1453512f976SVille Syrjälä }
1463512f976SVille Syrjälä 
1473512f976SVille Syrjälä /**
148763ba2ecSVille Syrjälä  * drm_rect_translate_to - translate the rectangle to an absolute position
149763ba2ecSVille Syrjälä  * @r: rectangle to be tranlated
150763ba2ecSVille Syrjälä  * @x: horizontal position
151763ba2ecSVille Syrjälä  * @y: vertical position
152763ba2ecSVille Syrjälä  *
153763ba2ecSVille Syrjälä  * Move rectangle @r to @x in the horizontal direction,
154763ba2ecSVille Syrjälä  * and to @y in the vertical direction.
155763ba2ecSVille Syrjälä  */
drm_rect_translate_to(struct drm_rect * r,int x,int y)156763ba2ecSVille Syrjälä static inline void drm_rect_translate_to(struct drm_rect *r, int x, int y)
157763ba2ecSVille Syrjälä {
158763ba2ecSVille Syrjälä 	drm_rect_translate(r, x - r->x1, y - r->y1);
159763ba2ecSVille Syrjälä }
160763ba2ecSVille Syrjälä 
161763ba2ecSVille Syrjälä /**
1623512f976SVille Syrjälä  * drm_rect_downscale - downscale a rectangle
1633512f976SVille Syrjälä  * @r: rectangle to be downscaled
1643512f976SVille Syrjälä  * @horz: horizontal downscale factor
1653512f976SVille Syrjälä  * @vert: vertical downscale factor
1663512f976SVille Syrjälä  *
1673512f976SVille Syrjälä  * Divide the coordinates of rectangle @r by @horz and @vert.
1683512f976SVille Syrjälä  */
drm_rect_downscale(struct drm_rect * r,int horz,int vert)1693512f976SVille Syrjälä static inline void drm_rect_downscale(struct drm_rect *r, int horz, int vert)
1703512f976SVille Syrjälä {
1713512f976SVille Syrjälä 	r->x1 /= horz;
1723512f976SVille Syrjälä 	r->y1 /= vert;
1733512f976SVille Syrjälä 	r->x2 /= horz;
1743512f976SVille Syrjälä 	r->y2 /= vert;
1753512f976SVille Syrjälä }
1763512f976SVille Syrjälä 
1773512f976SVille Syrjälä /**
1783512f976SVille Syrjälä  * drm_rect_width - determine the rectangle width
1793512f976SVille Syrjälä  * @r: rectangle whose width is returned
1803512f976SVille Syrjälä  *
1813512f976SVille Syrjälä  * RETURNS:
1823512f976SVille Syrjälä  * The width of the rectangle.
1833512f976SVille Syrjälä  */
drm_rect_width(const struct drm_rect * r)1843512f976SVille Syrjälä static inline int drm_rect_width(const struct drm_rect *r)
1853512f976SVille Syrjälä {
1863512f976SVille Syrjälä 	return r->x2 - r->x1;
1873512f976SVille Syrjälä }
1883512f976SVille Syrjälä 
1893512f976SVille Syrjälä /**
1903512f976SVille Syrjälä  * drm_rect_height - determine the rectangle height
1913512f976SVille Syrjälä  * @r: rectangle whose height is returned
1923512f976SVille Syrjälä  *
1933512f976SVille Syrjälä  * RETURNS:
1943512f976SVille Syrjälä  * The height of the rectangle.
1953512f976SVille Syrjälä  */
drm_rect_height(const struct drm_rect * r)1963512f976SVille Syrjälä static inline int drm_rect_height(const struct drm_rect *r)
1973512f976SVille Syrjälä {
1983512f976SVille Syrjälä 	return r->y2 - r->y1;
1993512f976SVille Syrjälä }
2003512f976SVille Syrjälä 
2013512f976SVille Syrjälä /**
202947fcfeaSRandy Dunlap  * drm_rect_visible - determine if the rectangle is visible
2033512f976SVille Syrjälä  * @r: rectangle whose visibility is returned
2043512f976SVille Syrjälä  *
2053512f976SVille Syrjälä  * RETURNS:
2063512f976SVille Syrjälä  * %true if the rectangle is visible, %false otherwise.
2073512f976SVille Syrjälä  */
drm_rect_visible(const struct drm_rect * r)2083512f976SVille Syrjälä static inline bool drm_rect_visible(const struct drm_rect *r)
2093512f976SVille Syrjälä {
2103512f976SVille Syrjälä 	return drm_rect_width(r) > 0 && drm_rect_height(r) > 0;
2113512f976SVille Syrjälä }
2123512f976SVille Syrjälä 
2130894c96bSVille Syrjälä /**
2140894c96bSVille Syrjälä  * drm_rect_equals - determine if two rectangles are equal
2150894c96bSVille Syrjälä  * @r1: first rectangle
2160894c96bSVille Syrjälä  * @r2: second rectangle
2170894c96bSVille Syrjälä  *
2180894c96bSVille Syrjälä  * RETURNS:
2190894c96bSVille Syrjälä  * %true if the rectangles are equal, %false otherwise.
2200894c96bSVille Syrjälä  */
drm_rect_equals(const struct drm_rect * r1,const struct drm_rect * r2)2210894c96bSVille Syrjälä static inline bool drm_rect_equals(const struct drm_rect *r1,
2220894c96bSVille Syrjälä 				   const struct drm_rect *r2)
2230894c96bSVille Syrjälä {
2240894c96bSVille Syrjälä 	return r1->x1 == r2->x1 && r1->x2 == r2->x2 &&
2250894c96bSVille Syrjälä 		r1->y1 == r2->y1 && r1->y2 == r2->y2;
2260894c96bSVille Syrjälä }
2270894c96bSVille Syrjälä 
2287d8ac172SJosé Roberto de Souza /**
2297d8ac172SJosé Roberto de Souza  * drm_rect_fp_to_int - Convert a rect in 16.16 fixed point form to int form.
2307d8ac172SJosé Roberto de Souza  * @dst: rect to be stored the converted value
2317d8ac172SJosé Roberto de Souza  * @src: rect in 16.16 fixed point form
2327d8ac172SJosé Roberto de Souza  */
drm_rect_fp_to_int(struct drm_rect * dst,const struct drm_rect * src)2337d8ac172SJosé Roberto de Souza static inline void drm_rect_fp_to_int(struct drm_rect *dst,
2347d8ac172SJosé Roberto de Souza 				      const struct drm_rect *src)
2357d8ac172SJosé Roberto de Souza {
2367d8ac172SJosé Roberto de Souza 	drm_rect_init(dst, src->x1 >> 16, src->y1 >> 16,
2377d8ac172SJosé Roberto de Souza 		      drm_rect_width(src) >> 16,
2387d8ac172SJosé Roberto de Souza 		      drm_rect_height(src) >> 16);
2397d8ac172SJosé Roberto de Souza }
2407d8ac172SJosé Roberto de Souza 
2413512f976SVille Syrjälä bool drm_rect_intersect(struct drm_rect *r, const struct drm_rect *clip);
2423512f976SVille Syrjälä bool drm_rect_clip_scaled(struct drm_rect *src, struct drm_rect *dst,
243f96bdf56SMaarten Lankhorst 			  const struct drm_rect *clip);
2444954c428SVille Syrjälä int drm_rect_calc_hscale(const struct drm_rect *src,
2454954c428SVille Syrjälä 			 const struct drm_rect *dst,
2464954c428SVille Syrjälä 			 int min_hscale, int max_hscale);
2474954c428SVille Syrjälä int drm_rect_calc_vscale(const struct drm_rect *src,
2484954c428SVille Syrjälä 			 const struct drm_rect *dst,
2494954c428SVille Syrjälä 			 int min_vscale, int max_vscale);
250c70f577aSVille Syrjälä void drm_rect_debug_print(const char *prefix,
251c70f577aSVille Syrjälä 			  const struct drm_rect *r, bool fixed_point);
25207074006SVille Syrjälä void drm_rect_rotate(struct drm_rect *r,
25307074006SVille Syrjälä 		     int width, int height,
25407074006SVille Syrjälä 		     unsigned int rotation);
25507074006SVille Syrjälä void drm_rect_rotate_inv(struct drm_rect *r,
25607074006SVille Syrjälä 			 int width, int height,
25707074006SVille Syrjälä 			 unsigned int rotation);
2583512f976SVille Syrjälä 
2593512f976SVille Syrjälä #endif
260