2 * Copyright (C) 2011-2013 Intel Corporation
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:
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
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
27 #include <linux/types.h>
32 * Utility functions to help manage rectangular areas for
33 * clipping, scaling, etc. calculations.
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)
48 * DRM_RECT_FMT - printf string for &struct drm_rect
50 #define DRM_RECT_FMT "%dx%d%+d%+d"
52 * DRM_RECT_ARG - printf arguments for &struct drm_rect
53 * @r: rectangle struct
55 #define DRM_RECT_ARG(r) drm_rect_width(r), drm_rect_height(r), (r)->x1, (r)->y1
58 * DRM_RECT_FP_FMT - printf string for &struct drm_rect in 16.16 fixed point
60 #define DRM_RECT_FP_FMT "%d.%06ux%d.%06u%+d.%06u%+d.%06u"
62 * DRM_RECT_FP_ARG - printf arguments for &struct drm_rect in 16.16 fixed point
63 * @r: rectangle struct
65 * This is useful for e.g. printing plane source rectangles, which are in 16.16
68 #define DRM_RECT_FP_ARG(r) \
69 drm_rect_width(r) >> 16, ((drm_rect_width(r) & 0xffff) * 15625) >> 10, \
70 drm_rect_height(r) >> 16, ((drm_rect_height(r) & 0xffff) * 15625) >> 10, \
71 (r)->x1 >> 16, (((r)->x1 & 0xffff) * 15625) >> 10, \
72 (r)->y1 >> 16, (((r)->y1 & 0xffff) * 15625) >> 10
75 * drm_rect_init - initialize the rectangle from x/y/w/h
82 static inline void drm_rect_init(struct drm_rect
*r
, int x
, int y
,
83 int width
, int height
)
92 * drm_rect_adjust_size - adjust the size of the rectangle
93 * @r: rectangle to be adjusted
94 * @dw: horizontal adjustment
95 * @dh: vertical adjustment
97 * Change the size of rectangle @r by @dw in the horizontal direction,
98 * and by @dh in the vertical direction, while keeping the center
101 * Positive @dw and @dh increase the size, negative values decrease it.
103 static inline void drm_rect_adjust_size(struct drm_rect
*r
, int dw
, int dh
)
107 r
->x2
+= (dw
+ 1) >> 1;
108 r
->y2
+= (dh
+ 1) >> 1;
112 * drm_rect_translate - translate the rectangle
113 * @r: rectangle to be tranlated
114 * @dx: horizontal translation
115 * @dy: vertical translation
117 * Move rectangle @r by @dx in the horizontal direction,
118 * and by @dy in the vertical direction.
120 static inline void drm_rect_translate(struct drm_rect
*r
, int dx
, int dy
)
129 * drm_rect_translate_to - translate the rectangle to an absolute position
130 * @r: rectangle to be tranlated
131 * @x: horizontal position
132 * @y: vertical position
134 * Move rectangle @r to @x in the horizontal direction,
135 * and to @y in the vertical direction.
137 static inline void drm_rect_translate_to(struct drm_rect
*r
, int x
, int y
)
139 drm_rect_translate(r
, x
- r
->x1
, y
- r
->y1
);
143 * drm_rect_downscale - downscale a rectangle
144 * @r: rectangle to be downscaled
145 * @horz: horizontal downscale factor
146 * @vert: vertical downscale factor
148 * Divide the coordinates of rectangle @r by @horz and @vert.
150 static inline void drm_rect_downscale(struct drm_rect
*r
, int horz
, int vert
)
159 * drm_rect_width - determine the rectangle width
160 * @r: rectangle whose width is returned
163 * The width of the rectangle.
165 static inline int drm_rect_width(const struct drm_rect
*r
)
167 return r
->x2
- r
->x1
;
171 * drm_rect_height - determine the rectangle height
172 * @r: rectangle whose height is returned
175 * The height of the rectangle.
177 static inline int drm_rect_height(const struct drm_rect
*r
)
179 return r
->y2
- r
->y1
;
183 * drm_rect_visible - determine if the the rectangle is visible
184 * @r: rectangle whose visibility is returned
187 * %true if the rectangle is visible, %false otherwise.
189 static inline bool drm_rect_visible(const struct drm_rect
*r
)
191 return drm_rect_width(r
) > 0 && drm_rect_height(r
) > 0;
195 * drm_rect_equals - determine if two rectangles are equal
196 * @r1: first rectangle
197 * @r2: second rectangle
200 * %true if the rectangles are equal, %false otherwise.
202 static inline bool drm_rect_equals(const struct drm_rect
*r1
,
203 const struct drm_rect
*r2
)
205 return r1
->x1
== r2
->x1
&& r1
->x2
== r2
->x2
&&
206 r1
->y1
== r2
->y1
&& r1
->y2
== r2
->y2
;
209 bool drm_rect_intersect(struct drm_rect
*r
, const struct drm_rect
*clip
);
210 bool drm_rect_clip_scaled(struct drm_rect
*src
, struct drm_rect
*dst
,
211 const struct drm_rect
*clip
);
212 int drm_rect_calc_hscale(const struct drm_rect
*src
,
213 const struct drm_rect
*dst
,
214 int min_hscale
, int max_hscale
);
215 int drm_rect_calc_vscale(const struct drm_rect
*src
,
216 const struct drm_rect
*dst
,
217 int min_vscale
, int max_vscale
);
218 void drm_rect_debug_print(const char *prefix
,
219 const struct drm_rect
*r
, bool fixed_point
);
220 void drm_rect_rotate(struct drm_rect
*r
,
221 int width
, int height
,
222 unsigned int rotation
);
223 void drm_rect_rotate_inv(struct drm_rect
*r
,
224 int width
, int height
,
225 unsigned int rotation
);