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