drm_rect.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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_mode.h>
  27. #include <drm/drm_print.h>
  28. #include <drm/drm_rect.h>
  29. /**
  30. * drm_rect_intersect - intersect two rectangles
  31. * @r1: first rectangle
  32. * @r2: second rectangle
  33. *
  34. * Calculate the intersection of rectangles @r1 and @r2.
  35. * @r1 will be overwritten with the intersection.
  36. *
  37. * RETURNS:
  38. * %true if rectangle @r1 is still visible after the operation,
  39. * %false otherwise.
  40. */
  41. bool drm_rect_intersect(struct drm_rect *r1, const struct drm_rect *r2)
  42. {
  43. r1->x1 = max(r1->x1, r2->x1);
  44. r1->y1 = max(r1->y1, r2->y1);
  45. r1->x2 = min(r1->x2, r2->x2);
  46. r1->y2 = min(r1->y2, r2->y2);
  47. return drm_rect_visible(r1);
  48. }
  49. EXPORT_SYMBOL(drm_rect_intersect);
  50. static u32 clip_scaled(int src, int dst, int *clip)
  51. {
  52. u64 tmp;
  53. if (dst == 0)
  54. return 0;
  55. /* Only clip what we have. Keeps the result bounded. */
  56. *clip = min(*clip, dst);
  57. tmp = mul_u32_u32(src, dst - *clip);
  58. /*
  59. * Round toward 1.0 when clipping so that we don't accidentally
  60. * change upscaling to downscaling or vice versa.
  61. */
  62. if (src < (dst << 16))
  63. return DIV_ROUND_UP_ULL(tmp, dst);
  64. else
  65. return DIV_ROUND_DOWN_ULL(tmp, dst);
  66. }
  67. /**
  68. * drm_rect_clip_scaled - perform a scaled clip operation
  69. * @src: source window rectangle
  70. * @dst: destination window rectangle
  71. * @clip: clip rectangle
  72. *
  73. * Clip rectangle @dst by rectangle @clip. Clip rectangle @src by the
  74. * the corresponding amounts, retaining the vertical and horizontal scaling
  75. * factors from @src to @dst.
  76. *
  77. * RETURNS:
  78. *
  79. * %true if rectangle @dst is still visible after being clipped,
  80. * %false otherwise.
  81. */
  82. bool drm_rect_clip_scaled(struct drm_rect *src, struct drm_rect *dst,
  83. const struct drm_rect *clip)
  84. {
  85. int diff;
  86. diff = clip->x1 - dst->x1;
  87. if (diff > 0) {
  88. u32 new_src_w = clip_scaled(drm_rect_width(src),
  89. drm_rect_width(dst), &diff);
  90. src->x1 = src->x2 - new_src_w;
  91. dst->x1 += diff;
  92. }
  93. diff = clip->y1 - dst->y1;
  94. if (diff > 0) {
  95. u32 new_src_h = clip_scaled(drm_rect_height(src),
  96. drm_rect_height(dst), &diff);
  97. src->y1 = src->y2 - new_src_h;
  98. dst->y1 += diff;
  99. }
  100. diff = dst->x2 - clip->x2;
  101. if (diff > 0) {
  102. u32 new_src_w = clip_scaled(drm_rect_width(src),
  103. drm_rect_width(dst), &diff);
  104. src->x2 = src->x1 + new_src_w;
  105. dst->x2 -= diff;
  106. }
  107. diff = dst->y2 - clip->y2;
  108. if (diff > 0) {
  109. u32 new_src_h = clip_scaled(drm_rect_height(src),
  110. drm_rect_height(dst), &diff);
  111. src->y2 = src->y1 + new_src_h;
  112. dst->y2 -= diff;
  113. }
  114. return drm_rect_visible(dst);
  115. }
  116. EXPORT_SYMBOL(drm_rect_clip_scaled);
  117. static int drm_calc_scale(int src, int dst)
  118. {
  119. int scale = 0;
  120. if (WARN_ON(src < 0 || dst < 0))
  121. return -EINVAL;
  122. if (dst == 0)
  123. return 0;
  124. if (src > (dst << 16))
  125. return DIV_ROUND_UP(src, dst);
  126. else
  127. scale = src / dst;
  128. return scale;
  129. }
  130. /**
  131. * drm_rect_calc_hscale - calculate the horizontal scaling factor
  132. * @src: source window rectangle
  133. * @dst: destination window rectangle
  134. * @min_hscale: minimum allowed horizontal scaling factor
  135. * @max_hscale: maximum allowed horizontal scaling factor
  136. *
  137. * Calculate the horizontal scaling factor as
  138. * (@src width) / (@dst width).
  139. *
  140. * If the scale is below 1 << 16, round down. If the scale is above
  141. * 1 << 16, round up. This will calculate the scale with the most
  142. * pessimistic limit calculation.
  143. *
  144. * RETURNS:
  145. * The horizontal scaling factor, or errno of out of limits.
  146. */
  147. int drm_rect_calc_hscale(const struct drm_rect *src,
  148. const struct drm_rect *dst,
  149. int min_hscale, int max_hscale)
  150. {
  151. int src_w = drm_rect_width(src);
  152. int dst_w = drm_rect_width(dst);
  153. int hscale = drm_calc_scale(src_w, dst_w);
  154. if (hscale < 0 || dst_w == 0)
  155. return hscale;
  156. if (hscale < min_hscale || hscale > max_hscale)
  157. return -ERANGE;
  158. return hscale;
  159. }
  160. EXPORT_SYMBOL(drm_rect_calc_hscale);
  161. /**
  162. * drm_rect_calc_vscale - calculate the vertical scaling factor
  163. * @src: source window rectangle
  164. * @dst: destination window rectangle
  165. * @min_vscale: minimum allowed vertical scaling factor
  166. * @max_vscale: maximum allowed vertical scaling factor
  167. *
  168. * Calculate the vertical scaling factor as
  169. * (@src height) / (@dst height).
  170. *
  171. * If the scale is below 1 << 16, round down. If the scale is above
  172. * 1 << 16, round up. This will calculate the scale with the most
  173. * pessimistic limit calculation.
  174. *
  175. * RETURNS:
  176. * The vertical scaling factor, or errno of out of limits.
  177. */
  178. int drm_rect_calc_vscale(const struct drm_rect *src,
  179. const struct drm_rect *dst,
  180. int min_vscale, int max_vscale)
  181. {
  182. int src_h = drm_rect_height(src);
  183. int dst_h = drm_rect_height(dst);
  184. int vscale = drm_calc_scale(src_h, dst_h);
  185. if (vscale < 0 || dst_h == 0)
  186. return vscale;
  187. if (vscale < min_vscale || vscale > max_vscale)
  188. return -ERANGE;
  189. return vscale;
  190. }
  191. EXPORT_SYMBOL(drm_rect_calc_vscale);
  192. /**
  193. * drm_rect_debug_print - print the rectangle information
  194. * @prefix: prefix string
  195. * @r: rectangle to print
  196. * @fixed_point: rectangle is in 16.16 fixed point format
  197. */
  198. void drm_rect_debug_print(const char *prefix, const struct drm_rect *r, bool fixed_point)
  199. {
  200. if (fixed_point)
  201. DRM_DEBUG_KMS("%s" DRM_RECT_FP_FMT "\n", prefix, DRM_RECT_FP_ARG(r));
  202. else
  203. DRM_DEBUG_KMS("%s" DRM_RECT_FMT "\n", prefix, DRM_RECT_ARG(r));
  204. }
  205. EXPORT_SYMBOL(drm_rect_debug_print);
  206. /**
  207. * drm_rect_rotate - Rotate the rectangle
  208. * @r: rectangle to be rotated
  209. * @width: Width of the coordinate space
  210. * @height: Height of the coordinate space
  211. * @rotation: Transformation to be applied
  212. *
  213. * Apply @rotation to the coordinates of rectangle @r.
  214. *
  215. * @width and @height combined with @rotation define
  216. * the location of the new origin.
  217. *
  218. * @width correcsponds to the horizontal and @height
  219. * to the vertical axis of the untransformed coordinate
  220. * space.
  221. */
  222. void drm_rect_rotate(struct drm_rect *r,
  223. int width, int height,
  224. unsigned int rotation)
  225. {
  226. struct drm_rect tmp;
  227. if (rotation & (DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y)) {
  228. tmp = *r;
  229. if (rotation & DRM_MODE_REFLECT_X) {
  230. r->x1 = width - tmp.x2;
  231. r->x2 = width - tmp.x1;
  232. }
  233. if (rotation & DRM_MODE_REFLECT_Y) {
  234. r->y1 = height - tmp.y2;
  235. r->y2 = height - tmp.y1;
  236. }
  237. }
  238. switch (rotation & DRM_MODE_ROTATE_MASK) {
  239. case DRM_MODE_ROTATE_0:
  240. break;
  241. case DRM_MODE_ROTATE_90:
  242. tmp = *r;
  243. r->x1 = tmp.y1;
  244. r->x2 = tmp.y2;
  245. r->y1 = width - tmp.x2;
  246. r->y2 = width - tmp.x1;
  247. break;
  248. case DRM_MODE_ROTATE_180:
  249. tmp = *r;
  250. r->x1 = width - tmp.x2;
  251. r->x2 = width - tmp.x1;
  252. r->y1 = height - tmp.y2;
  253. r->y2 = height - tmp.y1;
  254. break;
  255. case DRM_MODE_ROTATE_270:
  256. tmp = *r;
  257. r->x1 = height - tmp.y2;
  258. r->x2 = height - tmp.y1;
  259. r->y1 = tmp.x1;
  260. r->y2 = tmp.x2;
  261. break;
  262. default:
  263. break;
  264. }
  265. }
  266. EXPORT_SYMBOL(drm_rect_rotate);
  267. /**
  268. * drm_rect_rotate_inv - Inverse rotate the rectangle
  269. * @r: rectangle to be rotated
  270. * @width: Width of the coordinate space
  271. * @height: Height of the coordinate space
  272. * @rotation: Transformation whose inverse is to be applied
  273. *
  274. * Apply the inverse of @rotation to the coordinates
  275. * of rectangle @r.
  276. *
  277. * @width and @height combined with @rotation define
  278. * the location of the new origin.
  279. *
  280. * @width correcsponds to the horizontal and @height
  281. * to the vertical axis of the original untransformed
  282. * coordinate space, so that you never have to flip
  283. * them when doing a rotatation and its inverse.
  284. * That is, if you do ::
  285. *
  286. * drm_rect_rotate(&r, width, height, rotation);
  287. * drm_rect_rotate_inv(&r, width, height, rotation);
  288. *
  289. * you will always get back the original rectangle.
  290. */
  291. void drm_rect_rotate_inv(struct drm_rect *r,
  292. int width, int height,
  293. unsigned int rotation)
  294. {
  295. struct drm_rect tmp;
  296. switch (rotation & DRM_MODE_ROTATE_MASK) {
  297. case DRM_MODE_ROTATE_0:
  298. break;
  299. case DRM_MODE_ROTATE_90:
  300. tmp = *r;
  301. r->x1 = width - tmp.y2;
  302. r->x2 = width - tmp.y1;
  303. r->y1 = tmp.x1;
  304. r->y2 = tmp.x2;
  305. break;
  306. case DRM_MODE_ROTATE_180:
  307. tmp = *r;
  308. r->x1 = width - tmp.x2;
  309. r->x2 = width - tmp.x1;
  310. r->y1 = height - tmp.y2;
  311. r->y2 = height - tmp.y1;
  312. break;
  313. case DRM_MODE_ROTATE_270:
  314. tmp = *r;
  315. r->x1 = tmp.y1;
  316. r->x2 = tmp.y2;
  317. r->y1 = height - tmp.x2;
  318. r->y2 = height - tmp.x1;
  319. break;
  320. default:
  321. break;
  322. }
  323. if (rotation & (DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y)) {
  324. tmp = *r;
  325. if (rotation & DRM_MODE_REFLECT_X) {
  326. r->x1 = width - tmp.x2;
  327. r->x2 = width - tmp.x1;
  328. }
  329. if (rotation & DRM_MODE_REFLECT_Y) {
  330. r->y1 = height - tmp.y2;
  331. r->y2 = height - tmp.y1;
  332. }
  333. }
  334. }
  335. EXPORT_SYMBOL(drm_rect_rotate_inv);