drm_rect.c 12 KB

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