drm_rect.c 12 KB

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