drm_rect.c 9.4 KB

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