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