drm_rect.c 11 KB

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