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