drm_rect.h 7.2 KB

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