drm_rect.h 5.9 KB

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