drm_rect.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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_adjust_size - adjust the size of the rectangle
  43. * @r: rectangle to be adjusted
  44. * @dw: horizontal adjustment
  45. * @dh: vertical adjustment
  46. *
  47. * Change the size of rectangle @r by @dw in the horizontal direction,
  48. * and by @dh in the vertical direction, while keeping the center
  49. * of @r stationary.
  50. *
  51. * Positive @dw and @dh increase the size, negative values decrease it.
  52. */
  53. static inline void drm_rect_adjust_size(struct drm_rect *r, int dw, int dh)
  54. {
  55. r->x1 -= dw >> 1;
  56. r->y1 -= dh >> 1;
  57. r->x2 += (dw + 1) >> 1;
  58. r->y2 += (dh + 1) >> 1;
  59. }
  60. /**
  61. * drm_rect_translate - translate the rectangle
  62. * @r: rectangle to be tranlated
  63. * @dx: horizontal translation
  64. * @dy: vertical translation
  65. *
  66. * Move rectangle @r by @dx in the horizontal direction,
  67. * and by @dy in the vertical direction.
  68. */
  69. static inline void drm_rect_translate(struct drm_rect *r, int dx, int dy)
  70. {
  71. r->x1 += dx;
  72. r->y1 += dy;
  73. r->x2 += dx;
  74. r->y2 += dy;
  75. }
  76. /**
  77. * drm_rect_downscale - downscale a rectangle
  78. * @r: rectangle to be downscaled
  79. * @horz: horizontal downscale factor
  80. * @vert: vertical downscale factor
  81. *
  82. * Divide the coordinates of rectangle @r by @horz and @vert.
  83. */
  84. static inline void drm_rect_downscale(struct drm_rect *r, int horz, int vert)
  85. {
  86. r->x1 /= horz;
  87. r->y1 /= vert;
  88. r->x2 /= horz;
  89. r->y2 /= vert;
  90. }
  91. /**
  92. * drm_rect_width - determine the rectangle width
  93. * @r: rectangle whose width is returned
  94. *
  95. * RETURNS:
  96. * The width of the rectangle.
  97. */
  98. static inline int drm_rect_width(const struct drm_rect *r)
  99. {
  100. return r->x2 - r->x1;
  101. }
  102. /**
  103. * drm_rect_height - determine the rectangle height
  104. * @r: rectangle whose height is returned
  105. *
  106. * RETURNS:
  107. * The height of the rectangle.
  108. */
  109. static inline int drm_rect_height(const struct drm_rect *r)
  110. {
  111. return r->y2 - r->y1;
  112. }
  113. /**
  114. * drm_rect_visible - determine if the the rectangle is visible
  115. * @r: rectangle whose visibility is returned
  116. *
  117. * RETURNS:
  118. * %true if the rectangle is visible, %false otherwise.
  119. */
  120. static inline bool drm_rect_visible(const struct drm_rect *r)
  121. {
  122. return drm_rect_width(r) > 0 && drm_rect_height(r) > 0;
  123. }
  124. /**
  125. * drm_rect_equals - determine if two rectangles are equal
  126. * @r1: first rectangle
  127. * @r2: second rectangle
  128. *
  129. * RETURNS:
  130. * %true if the rectangles are equal, %false otherwise.
  131. */
  132. static inline bool drm_rect_equals(const struct drm_rect *r1,
  133. const struct drm_rect *r2)
  134. {
  135. return r1->x1 == r2->x1 && r1->x2 == r2->x2 &&
  136. r1->y1 == r2->y1 && r1->y2 == r2->y2;
  137. }
  138. bool drm_rect_intersect(struct drm_rect *r, const struct drm_rect *clip);
  139. bool drm_rect_clip_scaled(struct drm_rect *src, struct drm_rect *dst,
  140. const struct drm_rect *clip,
  141. int hscale, int vscale);
  142. int drm_rect_calc_hscale(const struct drm_rect *src,
  143. const struct drm_rect *dst,
  144. int min_hscale, int max_hscale);
  145. int drm_rect_calc_vscale(const struct drm_rect *src,
  146. const struct drm_rect *dst,
  147. int min_vscale, int max_vscale);
  148. int drm_rect_calc_hscale_relaxed(struct drm_rect *src,
  149. struct drm_rect *dst,
  150. int min_hscale, int max_hscale);
  151. int drm_rect_calc_vscale_relaxed(struct drm_rect *src,
  152. struct drm_rect *dst,
  153. int min_vscale, int max_vscale);
  154. void drm_rect_debug_print(const struct drm_rect *r, bool fixed_point);
  155. void drm_rect_rotate(struct drm_rect *r,
  156. int width, int height,
  157. unsigned int rotation);
  158. void drm_rect_rotate_inv(struct drm_rect *r,
  159. int width, int height,
  160. unsigned int rotation);
  161. #endif