13512f976SVille Syrjälä /* 23512f976SVille Syrjälä * Copyright (C) 2011-2013 Intel Corporation 33512f976SVille Syrjälä * 43512f976SVille Syrjälä * Permission is hereby granted, free of charge, to any person obtaining a 53512f976SVille Syrjälä * copy of this software and associated documentation files (the "Software"), 63512f976SVille Syrjälä * to deal in the Software without restriction, including without limitation 73512f976SVille Syrjälä * the rights to use, copy, modify, merge, publish, distribute, sublicense, 83512f976SVille Syrjälä * and/or sell copies of the Software, and to permit persons to whom the 93512f976SVille Syrjälä * Software is furnished to do so, subject to the following conditions: 103512f976SVille Syrjälä * 113512f976SVille Syrjälä * The above copyright notice and this permission notice (including the next 123512f976SVille Syrjälä * paragraph) shall be included in all copies or substantial portions of the 133512f976SVille Syrjälä * Software. 143512f976SVille Syrjälä * 153512f976SVille Syrjälä * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 163512f976SVille Syrjälä * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 173512f976SVille Syrjälä * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 183512f976SVille Syrjälä * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 193512f976SVille Syrjälä * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 203512f976SVille Syrjälä * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 213512f976SVille Syrjälä * SOFTWARE. 223512f976SVille Syrjälä */ 233512f976SVille Syrjälä 243512f976SVille Syrjälä #ifndef DRM_RECT_H 253512f976SVille Syrjälä #define DRM_RECT_H 263512f976SVille Syrjälä 273512f976SVille Syrjälä /** 2803973536SVille Syrjälä * DOC: rect utils 2903973536SVille Syrjälä * 3003973536SVille Syrjälä * Utility functions to help manage rectangular areas for 3103973536SVille Syrjälä * clipping, scaling, etc. calculations. 3203973536SVille Syrjälä */ 3303973536SVille Syrjälä 3403973536SVille Syrjälä /** 3503973536SVille Syrjälä * struct drm_rect - two dimensional rectangle 363512f976SVille Syrjälä * @x1: horizontal starting coordinate (inclusive) 373512f976SVille Syrjälä * @x2: horizontal ending coordinate (exclusive) 383512f976SVille Syrjälä * @y1: vertical starting coordinate (inclusive) 393512f976SVille Syrjälä * @y2: vertical ending coordinate (exclusive) 403512f976SVille Syrjälä */ 413512f976SVille Syrjälä struct drm_rect { 423512f976SVille Syrjälä int x1, y1, x2, y2; 433512f976SVille Syrjälä }; 443512f976SVille Syrjälä 453512f976SVille Syrjälä /** 4699cdb35eSDaniel Vetter * DRM_RECT_FMT - printf string for &struct drm_rect 4799cdb35eSDaniel Vetter */ 4899cdb35eSDaniel Vetter #define DRM_RECT_FMT "%dx%d%+d%+d" 4999cdb35eSDaniel Vetter /** 5099cdb35eSDaniel Vetter * DRM_RECT_ARG - printf arguments for &struct drm_rect 5199cdb35eSDaniel Vetter * @r: rectangle struct 5299cdb35eSDaniel Vetter */ 5399cdb35eSDaniel Vetter #define DRM_RECT_ARG(r) drm_rect_width(r), drm_rect_height(r), (r)->x1, (r)->y1 5499cdb35eSDaniel Vetter 5599cdb35eSDaniel Vetter /** 5699cdb35eSDaniel Vetter * DRM_RECT_FP_FMT - printf string for &struct drm_rect in 16.16 fixed point 5799cdb35eSDaniel Vetter */ 5899cdb35eSDaniel Vetter #define DRM_RECT_FP_FMT "%d.%06ux%d.%06u%+d.%06u%+d.%06u" 5999cdb35eSDaniel Vetter /** 6099cdb35eSDaniel Vetter * DRM_RECT_FP_ARG - printf arguments for &struct drm_rect in 16.16 fixed point 6199cdb35eSDaniel Vetter * @r: rectangle struct 6299cdb35eSDaniel Vetter * 6399cdb35eSDaniel Vetter * This is useful for e.g. printing plane source rectangles, which are in 16.16 6499cdb35eSDaniel Vetter * fixed point. 6599cdb35eSDaniel Vetter */ 6699cdb35eSDaniel Vetter #define DRM_RECT_FP_ARG(r) \ 6799cdb35eSDaniel Vetter drm_rect_width(r) >> 16, ((drm_rect_width(r) & 0xffff) * 15625) >> 10, \ 6899cdb35eSDaniel Vetter drm_rect_height(r) >> 16, ((drm_rect_height(r) & 0xffff) * 15625) >> 10, \ 6999cdb35eSDaniel Vetter (r)->x1 >> 16, (((r)->x1 & 0xffff) * 15625) >> 10, \ 7099cdb35eSDaniel Vetter (r)->y1 >> 16, (((r)->y1 & 0xffff) * 15625) >> 10 7199cdb35eSDaniel Vetter 7299cdb35eSDaniel Vetter /** 733512f976SVille Syrjälä * drm_rect_adjust_size - adjust the size of the rectangle 743512f976SVille Syrjälä * @r: rectangle to be adjusted 753512f976SVille Syrjälä * @dw: horizontal adjustment 763512f976SVille Syrjälä * @dh: vertical adjustment 773512f976SVille Syrjälä * 783512f976SVille Syrjälä * Change the size of rectangle @r by @dw in the horizontal direction, 793512f976SVille Syrjälä * and by @dh in the vertical direction, while keeping the center 803512f976SVille Syrjälä * of @r stationary. 813512f976SVille Syrjälä * 823512f976SVille Syrjälä * Positive @dw and @dh increase the size, negative values decrease it. 833512f976SVille Syrjälä */ 843512f976SVille Syrjälä static inline void drm_rect_adjust_size(struct drm_rect *r, int dw, int dh) 853512f976SVille Syrjälä { 863512f976SVille Syrjälä r->x1 -= dw >> 1; 873512f976SVille Syrjälä r->y1 -= dh >> 1; 883512f976SVille Syrjälä r->x2 += (dw + 1) >> 1; 893512f976SVille Syrjälä r->y2 += (dh + 1) >> 1; 903512f976SVille Syrjälä } 913512f976SVille Syrjälä 923512f976SVille Syrjälä /** 933512f976SVille Syrjälä * drm_rect_translate - translate the rectangle 943512f976SVille Syrjälä * @r: rectangle to be tranlated 953512f976SVille Syrjälä * @dx: horizontal translation 963512f976SVille Syrjälä * @dy: vertical translation 973512f976SVille Syrjälä * 983512f976SVille Syrjälä * Move rectangle @r by @dx in the horizontal direction, 993512f976SVille Syrjälä * and by @dy in the vertical direction. 1003512f976SVille Syrjälä */ 1013512f976SVille Syrjälä static inline void drm_rect_translate(struct drm_rect *r, int dx, int dy) 1023512f976SVille Syrjälä { 1033512f976SVille Syrjälä r->x1 += dx; 1043512f976SVille Syrjälä r->y1 += dy; 1053512f976SVille Syrjälä r->x2 += dx; 1063512f976SVille Syrjälä r->y2 += dy; 1073512f976SVille Syrjälä } 1083512f976SVille Syrjälä 1093512f976SVille Syrjälä /** 110763ba2ecSVille Syrjälä * drm_rect_translate_to - translate the rectangle to an absolute position 111763ba2ecSVille Syrjälä * @r: rectangle to be tranlated 112763ba2ecSVille Syrjälä * @x: horizontal position 113763ba2ecSVille Syrjälä * @y: vertical position 114763ba2ecSVille Syrjälä * 115763ba2ecSVille Syrjälä * Move rectangle @r to @x in the horizontal direction, 116763ba2ecSVille Syrjälä * and to @y in the vertical direction. 117763ba2ecSVille Syrjälä */ 118763ba2ecSVille Syrjälä static inline void drm_rect_translate_to(struct drm_rect *r, int x, int y) 119763ba2ecSVille Syrjälä { 120763ba2ecSVille Syrjälä drm_rect_translate(r, x - r->x1, y - r->y1); 121763ba2ecSVille Syrjälä } 122763ba2ecSVille Syrjälä 123763ba2ecSVille Syrjälä /** 1243512f976SVille Syrjälä * drm_rect_downscale - downscale a rectangle 1253512f976SVille Syrjälä * @r: rectangle to be downscaled 1263512f976SVille Syrjälä * @horz: horizontal downscale factor 1273512f976SVille Syrjälä * @vert: vertical downscale factor 1283512f976SVille Syrjälä * 1293512f976SVille Syrjälä * Divide the coordinates of rectangle @r by @horz and @vert. 1303512f976SVille Syrjälä */ 1313512f976SVille Syrjälä static inline void drm_rect_downscale(struct drm_rect *r, int horz, int vert) 1323512f976SVille Syrjälä { 1333512f976SVille Syrjälä r->x1 /= horz; 1343512f976SVille Syrjälä r->y1 /= vert; 1353512f976SVille Syrjälä r->x2 /= horz; 1363512f976SVille Syrjälä r->y2 /= vert; 1373512f976SVille Syrjälä } 1383512f976SVille Syrjälä 1393512f976SVille Syrjälä /** 1403512f976SVille Syrjälä * drm_rect_width - determine the rectangle width 1413512f976SVille Syrjälä * @r: rectangle whose width is returned 1423512f976SVille Syrjälä * 1433512f976SVille Syrjälä * RETURNS: 1443512f976SVille Syrjälä * The width of the rectangle. 1453512f976SVille Syrjälä */ 1463512f976SVille Syrjälä static inline int drm_rect_width(const struct drm_rect *r) 1473512f976SVille Syrjälä { 1483512f976SVille Syrjälä return r->x2 - r->x1; 1493512f976SVille Syrjälä } 1503512f976SVille Syrjälä 1513512f976SVille Syrjälä /** 1523512f976SVille Syrjälä * drm_rect_height - determine the rectangle height 1533512f976SVille Syrjälä * @r: rectangle whose height is returned 1543512f976SVille Syrjälä * 1553512f976SVille Syrjälä * RETURNS: 1563512f976SVille Syrjälä * The height of the rectangle. 1573512f976SVille Syrjälä */ 1583512f976SVille Syrjälä static inline int drm_rect_height(const struct drm_rect *r) 1593512f976SVille Syrjälä { 1603512f976SVille Syrjälä return r->y2 - r->y1; 1613512f976SVille Syrjälä } 1623512f976SVille Syrjälä 1633512f976SVille Syrjälä /** 1643512f976SVille Syrjälä * drm_rect_visible - determine if the the rectangle is visible 1653512f976SVille Syrjälä * @r: rectangle whose visibility is returned 1663512f976SVille Syrjälä * 1673512f976SVille Syrjälä * RETURNS: 1683512f976SVille Syrjälä * %true if the rectangle is visible, %false otherwise. 1693512f976SVille Syrjälä */ 1703512f976SVille Syrjälä static inline bool drm_rect_visible(const struct drm_rect *r) 1713512f976SVille Syrjälä { 1723512f976SVille Syrjälä return drm_rect_width(r) > 0 && drm_rect_height(r) > 0; 1733512f976SVille Syrjälä } 1743512f976SVille Syrjälä 1750894c96bSVille Syrjälä /** 1760894c96bSVille Syrjälä * drm_rect_equals - determine if two rectangles are equal 1770894c96bSVille Syrjälä * @r1: first rectangle 1780894c96bSVille Syrjälä * @r2: second rectangle 1790894c96bSVille Syrjälä * 1800894c96bSVille Syrjälä * RETURNS: 1810894c96bSVille Syrjälä * %true if the rectangles are equal, %false otherwise. 1820894c96bSVille Syrjälä */ 1830894c96bSVille Syrjälä static inline bool drm_rect_equals(const struct drm_rect *r1, 1840894c96bSVille Syrjälä const struct drm_rect *r2) 1850894c96bSVille Syrjälä { 1860894c96bSVille Syrjälä return r1->x1 == r2->x1 && r1->x2 == r2->x2 && 1870894c96bSVille Syrjälä r1->y1 == r2->y1 && r1->y2 == r2->y2; 1880894c96bSVille Syrjälä } 1890894c96bSVille Syrjälä 1903512f976SVille Syrjälä bool drm_rect_intersect(struct drm_rect *r, const struct drm_rect *clip); 1913512f976SVille Syrjälä bool drm_rect_clip_scaled(struct drm_rect *src, struct drm_rect *dst, 192f96bdf56SMaarten Lankhorst const struct drm_rect *clip); 1934954c428SVille Syrjälä int drm_rect_calc_hscale(const struct drm_rect *src, 1944954c428SVille Syrjälä const struct drm_rect *dst, 1954954c428SVille Syrjälä int min_hscale, int max_hscale); 1964954c428SVille Syrjälä int drm_rect_calc_vscale(const struct drm_rect *src, 1974954c428SVille Syrjälä const struct drm_rect *dst, 1984954c428SVille Syrjälä int min_vscale, int max_vscale); 199c70f577aSVille Syrjälä void drm_rect_debug_print(const char *prefix, 200c70f577aSVille Syrjälä const struct drm_rect *r, bool fixed_point); 20107074006SVille Syrjälä void drm_rect_rotate(struct drm_rect *r, 20207074006SVille Syrjälä int width, int height, 20307074006SVille Syrjälä unsigned int rotation); 20407074006SVille Syrjälä void drm_rect_rotate_inv(struct drm_rect *r, 20507074006SVille Syrjälä int width, int height, 20607074006SVille Syrjälä unsigned int rotation); 2073512f976SVille Syrjälä 2083512f976SVille Syrjälä #endif 209