drm_rect.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. #include <linux/errno.h>
  24. #include <linux/export.h>
  25. #include <linux/kernel.h>
  26. #include <drm/drm_rect.h>
  27. /**
  28. * drm_rect_intersect - intersect two rectangles
  29. * @r1: first rectangle
  30. * @r2: second rectangle
  31. *
  32. * Calculate the intersection of rectangles @r1 and @r2.
  33. * @r1 will be overwritten with the intersection.
  34. *
  35. * RETURNS:
  36. * %true if rectangle @r1 is still visible after the operation,
  37. * %false otherwise.
  38. */
  39. bool drm_rect_intersect(struct drm_rect *r1, const struct drm_rect *r2)
  40. {
  41. r1->x1 = max(r1->x1, r2->x1);
  42. r1->y1 = max(r1->y1, r2->y1);
  43. r1->x2 = min(r1->x2, r2->x2);
  44. r1->y2 = min(r1->y2, r2->y2);
  45. return drm_rect_visible(r1);
  46. }
  47. EXPORT_SYMBOL(drm_rect_intersect);
  48. /**
  49. * drm_rect_clip_scaled - perform a scaled clip operation
  50. * @src: source window rectangle
  51. * @dst: destination window rectangle
  52. * @clip: clip rectangle
  53. * @hscale: horizontal scaling factor
  54. * @vscale: vertical scaling factor
  55. *
  56. * Clip rectangle @dst by rectangle @clip. Clip rectangle @src by the
  57. * same amounts multiplied by @hscale and @vscale.
  58. *
  59. * RETURNS:
  60. * %true if rectangle @dst is still visible after being clipped,
  61. * %false otherwise
  62. */
  63. bool drm_rect_clip_scaled(struct drm_rect *src, struct drm_rect *dst,
  64. const struct drm_rect *clip,
  65. int hscale, int vscale)
  66. {
  67. int diff;
  68. diff = clip->x1 - dst->x1;
  69. if (diff > 0) {
  70. int64_t tmp = src->x1 + (int64_t) diff * hscale;
  71. src->x1 = clamp_t(int64_t, tmp, INT_MIN, INT_MAX);
  72. }
  73. diff = clip->y1 - dst->y1;
  74. if (diff > 0) {
  75. int64_t tmp = src->y1 + (int64_t) diff * vscale;
  76. src->y1 = clamp_t(int64_t, tmp, INT_MIN, INT_MAX);
  77. }
  78. diff = dst->x2 - clip->x2;
  79. if (diff > 0) {
  80. int64_t tmp = src->x2 - (int64_t) diff * hscale;
  81. src->x2 = clamp_t(int64_t, tmp, INT_MIN, INT_MAX);
  82. }
  83. diff = dst->y2 - clip->y2;
  84. if (diff > 0) {
  85. int64_t tmp = src->y2 - (int64_t) diff * vscale;
  86. src->y2 = clamp_t(int64_t, tmp, INT_MIN, INT_MAX);
  87. }
  88. return drm_rect_intersect(dst, clip);
  89. }
  90. EXPORT_SYMBOL(drm_rect_clip_scaled);
  91. static int drm_calc_scale(int src, int dst)
  92. {
  93. int scale = 0;
  94. if (src < 0 || dst < 0)
  95. return -EINVAL;
  96. if (dst == 0)
  97. return 0;
  98. scale = src / dst;
  99. return scale;
  100. }
  101. /**
  102. * drm_rect_calc_hscale - calculate the horizontal scaling factor
  103. * @src: source window rectangle
  104. * @dst: destination window rectangle
  105. * @min_hscale: minimum allowed horizontal scaling factor
  106. * @max_hscale: maximum allowed horizontal scaling factor
  107. *
  108. * Calculate the horizontal scaling factor as
  109. * (@src width) / (@dst width).
  110. *
  111. * RETURNS:
  112. * The horizontal scaling factor, or errno of out of limits.
  113. */
  114. int drm_rect_calc_hscale(const struct drm_rect *src,
  115. const struct drm_rect *dst,
  116. int min_hscale, int max_hscale)
  117. {
  118. int src_w = drm_rect_width(src);
  119. int dst_w = drm_rect_width(dst);
  120. int hscale = drm_calc_scale(src_w, dst_w);
  121. if (hscale < 0 || dst_w == 0)
  122. return hscale;
  123. if (hscale < min_hscale || hscale > max_hscale)
  124. return -ERANGE;
  125. return hscale;
  126. }
  127. EXPORT_SYMBOL(drm_rect_calc_hscale);
  128. /**
  129. * drm_rect_calc_vscale - calculate the vertical scaling factor
  130. * @src: source window rectangle
  131. * @dst: destination window rectangle
  132. * @min_vscale: minimum allowed vertical scaling factor
  133. * @max_vscale: maximum allowed vertical scaling factor
  134. *
  135. * Calculate the vertical scaling factor as
  136. * (@src height) / (@dst height).
  137. *
  138. * RETURNS:
  139. * The vertical scaling factor, or errno of out of limits.
  140. */
  141. int drm_rect_calc_vscale(const struct drm_rect *src,
  142. const struct drm_rect *dst,
  143. int min_vscale, int max_vscale)
  144. {
  145. int src_h = drm_rect_height(src);
  146. int dst_h = drm_rect_height(dst);
  147. int vscale = drm_calc_scale(src_h, dst_h);
  148. if (vscale < 0 || dst_h == 0)
  149. return vscale;
  150. if (vscale < min_vscale || vscale > max_vscale)
  151. return -ERANGE;
  152. return vscale;
  153. }
  154. EXPORT_SYMBOL(drm_rect_calc_vscale);
  155. /**
  156. * drm_calc_hscale_relaxed - calculate the horizontal scaling factor
  157. * @src: source window rectangle
  158. * @dst: destination window rectangle
  159. * @min_hscale: minimum allowed horizontal scaling factor
  160. * @max_hscale: maximum allowed horizontal scaling factor
  161. *
  162. * Calculate the horizontal scaling factor as
  163. * (@src width) / (@dst width).
  164. *
  165. * If the calculated scaling factor is below @min_vscale,
  166. * decrease the height of rectangle @dst to compensate.
  167. *
  168. * If the calculated scaling factor is above @max_vscale,
  169. * decrease the height of rectangle @src to compensate.
  170. *
  171. * RETURNS:
  172. * The horizontal scaling factor.
  173. */
  174. int drm_rect_calc_hscale_relaxed(struct drm_rect *src,
  175. struct drm_rect *dst,
  176. int min_hscale, int max_hscale)
  177. {
  178. int src_w = drm_rect_width(src);
  179. int dst_w = drm_rect_width(dst);
  180. int hscale = drm_calc_scale(src_w, dst_w);
  181. if (hscale < 0 || dst_w == 0)
  182. return hscale;
  183. if (hscale < min_hscale) {
  184. int max_dst_w = src_w / min_hscale;
  185. drm_rect_adjust_size(dst, max_dst_w - dst_w, 0);
  186. return min_hscale;
  187. }
  188. if (hscale > max_hscale) {
  189. int max_src_w = dst_w * max_hscale;
  190. drm_rect_adjust_size(src, max_src_w - src_w, 0);
  191. return max_hscale;
  192. }
  193. return hscale;
  194. }
  195. EXPORT_SYMBOL(drm_rect_calc_hscale_relaxed);
  196. /**
  197. * drm_rect_calc_vscale_relaxed - calculate the vertical scaling factor
  198. * @src: source window rectangle
  199. * @dst: destination window rectangle
  200. * @min_vscale: minimum allowed vertical scaling factor
  201. * @max_vscale: maximum allowed vertical scaling factor
  202. *
  203. * Calculate the vertical scaling factor as
  204. * (@src height) / (@dst height).
  205. *
  206. * If the calculated scaling factor is below @min_vscale,
  207. * decrease the height of rectangle @dst to compensate.
  208. *
  209. * If the calculated scaling factor is above @max_vscale,
  210. * decrease the height of rectangle @src to compensate.
  211. *
  212. * RETURNS:
  213. * The vertical scaling factor.
  214. */
  215. int drm_rect_calc_vscale_relaxed(struct drm_rect *src,
  216. struct drm_rect *dst,
  217. int min_vscale, int max_vscale)
  218. {
  219. int src_h = drm_rect_height(src);
  220. int dst_h = drm_rect_height(dst);
  221. int vscale = drm_calc_scale(src_h, dst_h);
  222. if (vscale < 0 || dst_h == 0)
  223. return vscale;
  224. if (vscale < min_vscale) {
  225. int max_dst_h = src_h / min_vscale;
  226. drm_rect_adjust_size(dst, 0, max_dst_h - dst_h);
  227. return min_vscale;
  228. }
  229. if (vscale > max_vscale) {
  230. int max_src_h = dst_h * max_vscale;
  231. drm_rect_adjust_size(src, 0, max_src_h - src_h);
  232. return max_vscale;
  233. }
  234. return vscale;
  235. }
  236. EXPORT_SYMBOL(drm_rect_calc_vscale_relaxed);