Home | History | Annotate | Line # | Download | only in drm
drm_rect.h revision 1.1.1.2
      1 /*	$NetBSD: drm_rect.h,v 1.1.1.2 2018/08/27 01:35:00 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 2011-2013 Intel Corporation
      5  *
      6  * Permission is hereby granted, free of charge, to any person obtaining a
      7  * copy of this software and associated documentation files (the "Software"),
      8  * to deal in the Software without restriction, including without limitation
      9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  * and/or sell copies of the Software, and to permit persons to whom the
     11  * Software is furnished to do so, subject to the following conditions:
     12  *
     13  * The above copyright notice and this permission notice (including the next
     14  * paragraph) shall be included in all copies or substantial portions of the
     15  * Software.
     16  *
     17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     23  * SOFTWARE.
     24  */
     25 
     26 #ifndef DRM_RECT_H
     27 #define DRM_RECT_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 struct drm_rect {
     44 	int x1, y1, x2, y2;
     45 };
     46 
     47 /**
     48  * drm_rect_adjust_size - adjust the size of the rectangle
     49  * @r: rectangle to be adjusted
     50  * @dw: horizontal adjustment
     51  * @dh: vertical adjustment
     52  *
     53  * Change the size of rectangle @r by @dw in the horizontal direction,
     54  * and by @dh in the vertical direction, while keeping the center
     55  * of @r stationary.
     56  *
     57  * Positive @dw and @dh increase the size, negative values decrease it.
     58  */
     59 static inline void drm_rect_adjust_size(struct drm_rect *r, int dw, int dh)
     60 {
     61 	r->x1 -= dw >> 1;
     62 	r->y1 -= dh >> 1;
     63 	r->x2 += (dw + 1) >> 1;
     64 	r->y2 += (dh + 1) >> 1;
     65 }
     66 
     67 /**
     68  * drm_rect_translate - translate the rectangle
     69  * @r: rectangle to be tranlated
     70  * @dx: horizontal translation
     71  * @dy: vertical translation
     72  *
     73  * Move rectangle @r by @dx in the horizontal direction,
     74  * and by @dy in the vertical direction.
     75  */
     76 static inline void drm_rect_translate(struct drm_rect *r, int dx, int dy)
     77 {
     78 	r->x1 += dx;
     79 	r->y1 += dy;
     80 	r->x2 += dx;
     81 	r->y2 += dy;
     82 }
     83 
     84 /**
     85  * drm_rect_downscale - downscale a rectangle
     86  * @r: rectangle to be downscaled
     87  * @horz: horizontal downscale factor
     88  * @vert: vertical downscale factor
     89  *
     90  * Divide the coordinates of rectangle @r by @horz and @vert.
     91  */
     92 static inline void drm_rect_downscale(struct drm_rect *r, int horz, int vert)
     93 {
     94 	r->x1 /= horz;
     95 	r->y1 /= vert;
     96 	r->x2 /= horz;
     97 	r->y2 /= vert;
     98 }
     99 
    100 /**
    101  * drm_rect_width - determine the rectangle width
    102  * @r: rectangle whose width is returned
    103  *
    104  * RETURNS:
    105  * The width of the rectangle.
    106  */
    107 static inline int drm_rect_width(const struct drm_rect *r)
    108 {
    109 	return r->x2 - r->x1;
    110 }
    111 
    112 /**
    113  * drm_rect_height - determine the rectangle height
    114  * @r: rectangle whose height is returned
    115  *
    116  * RETURNS:
    117  * The height of the rectangle.
    118  */
    119 static inline int drm_rect_height(const struct drm_rect *r)
    120 {
    121 	return r->y2 - r->y1;
    122 }
    123 
    124 /**
    125  * drm_rect_visible - determine if the the rectangle is visible
    126  * @r: rectangle whose visibility is returned
    127  *
    128  * RETURNS:
    129  * %true if the rectangle is visible, %false otherwise.
    130  */
    131 static inline bool drm_rect_visible(const struct drm_rect *r)
    132 {
    133 	return drm_rect_width(r) > 0 && drm_rect_height(r) > 0;
    134 }
    135 
    136 /**
    137  * drm_rect_equals - determine if two rectangles are equal
    138  * @r1: first rectangle
    139  * @r2: second rectangle
    140  *
    141  * RETURNS:
    142  * %true if the rectangles are equal, %false otherwise.
    143  */
    144 static inline bool drm_rect_equals(const struct drm_rect *r1,
    145 				   const struct drm_rect *r2)
    146 {
    147 	return r1->x1 == r2->x1 && r1->x2 == r2->x2 &&
    148 		r1->y1 == r2->y1 && r1->y2 == r2->y2;
    149 }
    150 
    151 bool drm_rect_intersect(struct drm_rect *r, const struct drm_rect *clip);
    152 bool drm_rect_clip_scaled(struct drm_rect *src, struct drm_rect *dst,
    153 			  const struct drm_rect *clip,
    154 			  int hscale, int vscale);
    155 int drm_rect_calc_hscale(const struct drm_rect *src,
    156 			 const struct drm_rect *dst,
    157 			 int min_hscale, int max_hscale);
    158 int drm_rect_calc_vscale(const struct drm_rect *src,
    159 			 const struct drm_rect *dst,
    160 			 int min_vscale, int max_vscale);
    161 int drm_rect_calc_hscale_relaxed(struct drm_rect *src,
    162 				 struct drm_rect *dst,
    163 				 int min_hscale, int max_hscale);
    164 int drm_rect_calc_vscale_relaxed(struct drm_rect *src,
    165 				 struct drm_rect *dst,
    166 				 int min_vscale, int max_vscale);
    167 void drm_rect_debug_print(const struct drm_rect *r, bool fixed_point);
    168 void drm_rect_rotate(struct drm_rect *r,
    169 		     int width, int height,
    170 		     unsigned int rotation);
    171 void drm_rect_rotate_inv(struct drm_rect *r,
    172 			 int width, int height,
    173 			 unsigned int rotation);
    174 
    175 #endif
    176