1 /* 2 * Copyright (C) 2011-2013 Intel Corporation 3 * 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: 10 * 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 13 * Software. 14 * 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 21 * SOFTWARE. 22 */ 23 24 #ifndef DRM_RECT_H 25 #define DRM_RECT_H 26 27 /** 28 * DOC: rect utils 29 * 30 * Utility functions to help manage rectangular areas for 31 * clipping, scaling, etc. calculations. 32 */ 33 34 /** 35 * struct drm_rect - two dimensional rectangle 36 * @x1: horizontal starting coordinate (inclusive) 37 * @x2: horizontal ending coordinate (exclusive) 38 * @y1: vertical starting coordinate (inclusive) 39 * @y2: vertical ending coordinate (exclusive) 40 */ 41 struct drm_rect { 42 int x1, y1, x2, y2; 43 }; 44 45 /** 46 * DRM_RECT_FMT - printf string for &struct drm_rect 47 */ 48 #define DRM_RECT_FMT "%dx%d%+d%+d" 49 /** 50 * DRM_RECT_ARG - printf arguments for &struct drm_rect 51 * @r: rectangle struct 52 */ 53 #define DRM_RECT_ARG(r) drm_rect_width(r), drm_rect_height(r), (r)->x1, (r)->y1 54 55 /** 56 * DRM_RECT_FP_FMT - printf string for &struct drm_rect in 16.16 fixed point 57 */ 58 #define DRM_RECT_FP_FMT "%d.%06ux%d.%06u%+d.%06u%+d.%06u" 59 /** 60 * DRM_RECT_FP_ARG - printf arguments for &struct drm_rect in 16.16 fixed point 61 * @r: rectangle struct 62 * 63 * This is useful for e.g. printing plane source rectangles, which are in 16.16 64 * fixed point. 65 */ 66 #define DRM_RECT_FP_ARG(r) \ 67 drm_rect_width(r) >> 16, ((drm_rect_width(r) & 0xffff) * 15625) >> 10, \ 68 drm_rect_height(r) >> 16, ((drm_rect_height(r) & 0xffff) * 15625) >> 10, \ 69 (r)->x1 >> 16, (((r)->x1 & 0xffff) * 15625) >> 10, \ 70 (r)->y1 >> 16, (((r)->y1 & 0xffff) * 15625) >> 10 71 72 /** 73 * drm_rect_init - initialize the rectangle from x/y/w/h 74 * @r: rectangle 75 * @x: x coordinate 76 * @y: y coordinate 77 * @width: width 78 * @height: height 79 */ 80 static inline void drm_rect_init(struct drm_rect *r, int x, int y, 81 int width, int height) 82 { 83 r->x1 = x; 84 r->y1 = y; 85 r->x2 = x + width; 86 r->y2 = y + height; 87 } 88 89 /** 90 * drm_rect_adjust_size - adjust the size of the rectangle 91 * @r: rectangle to be adjusted 92 * @dw: horizontal adjustment 93 * @dh: vertical adjustment 94 * 95 * Change the size of rectangle @r by @dw in the horizontal direction, 96 * and by @dh in the vertical direction, while keeping the center 97 * of @r stationary. 98 * 99 * Positive @dw and @dh increase the size, negative values decrease it. 100 */ 101 static inline void drm_rect_adjust_size(struct drm_rect *r, int dw, int dh) 102 { 103 r->x1 -= dw >> 1; 104 r->y1 -= dh >> 1; 105 r->x2 += (dw + 1) >> 1; 106 r->y2 += (dh + 1) >> 1; 107 } 108 109 /** 110 * drm_rect_translate - translate the rectangle 111 * @r: rectangle to be tranlated 112 * @dx: horizontal translation 113 * @dy: vertical translation 114 * 115 * Move rectangle @r by @dx in the horizontal direction, 116 * and by @dy in the vertical direction. 117 */ 118 static inline void drm_rect_translate(struct drm_rect *r, int dx, int dy) 119 { 120 r->x1 += dx; 121 r->y1 += dy; 122 r->x2 += dx; 123 r->y2 += dy; 124 } 125 126 /** 127 * drm_rect_translate_to - translate the rectangle to an absolute position 128 * @r: rectangle to be tranlated 129 * @x: horizontal position 130 * @y: vertical position 131 * 132 * Move rectangle @r to @x in the horizontal direction, 133 * and to @y in the vertical direction. 134 */ 135 static inline void drm_rect_translate_to(struct drm_rect *r, int x, int y) 136 { 137 drm_rect_translate(r, x - r->x1, y - r->y1); 138 } 139 140 /** 141 * drm_rect_downscale - downscale a rectangle 142 * @r: rectangle to be downscaled 143 * @horz: horizontal downscale factor 144 * @vert: vertical downscale factor 145 * 146 * Divide the coordinates of rectangle @r by @horz and @vert. 147 */ 148 static inline void drm_rect_downscale(struct drm_rect *r, int horz, int vert) 149 { 150 r->x1 /= horz; 151 r->y1 /= vert; 152 r->x2 /= horz; 153 r->y2 /= vert; 154 } 155 156 /** 157 * drm_rect_width - determine the rectangle width 158 * @r: rectangle whose width is returned 159 * 160 * RETURNS: 161 * The width of the rectangle. 162 */ 163 static inline int drm_rect_width(const struct drm_rect *r) 164 { 165 return r->x2 - r->x1; 166 } 167 168 /** 169 * drm_rect_height - determine the rectangle height 170 * @r: rectangle whose height is returned 171 * 172 * RETURNS: 173 * The height of the rectangle. 174 */ 175 static inline int drm_rect_height(const struct drm_rect *r) 176 { 177 return r->y2 - r->y1; 178 } 179 180 /** 181 * drm_rect_visible - determine if the the rectangle is visible 182 * @r: rectangle whose visibility is returned 183 * 184 * RETURNS: 185 * %true if the rectangle is visible, %false otherwise. 186 */ 187 static inline bool drm_rect_visible(const struct drm_rect *r) 188 { 189 return drm_rect_width(r) > 0 && drm_rect_height(r) > 0; 190 } 191 192 /** 193 * drm_rect_equals - determine if two rectangles are equal 194 * @r1: first rectangle 195 * @r2: second rectangle 196 * 197 * RETURNS: 198 * %true if the rectangles are equal, %false otherwise. 199 */ 200 static inline bool drm_rect_equals(const struct drm_rect *r1, 201 const struct drm_rect *r2) 202 { 203 return r1->x1 == r2->x1 && r1->x2 == r2->x2 && 204 r1->y1 == r2->y1 && r1->y2 == r2->y2; 205 } 206 207 bool drm_rect_intersect(struct drm_rect *r, const struct drm_rect *clip); 208 bool drm_rect_clip_scaled(struct drm_rect *src, struct drm_rect *dst, 209 const struct drm_rect *clip); 210 int drm_rect_calc_hscale(const struct drm_rect *src, 211 const struct drm_rect *dst, 212 int min_hscale, int max_hscale); 213 int drm_rect_calc_vscale(const struct drm_rect *src, 214 const struct drm_rect *dst, 215 int min_vscale, int max_vscale); 216 void drm_rect_debug_print(const char *prefix, 217 const struct drm_rect *r, bool fixed_point); 218 void drm_rect_rotate(struct drm_rect *r, 219 int width, int height, 220 unsigned int rotation); 221 void drm_rect_rotate_inv(struct drm_rect *r, 222 int width, int height, 223 unsigned int rotation); 224 225 #endif 226