drm_rect.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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 (WARN_ON(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. * @prefix: prefix string
  241. * @r: rectangle to print
  242. * @fixed_point: rectangle is in 16.16 fixed point format
  243. */
  244. void drm_rect_debug_print(const char *prefix, const struct drm_rect *r, bool fixed_point)
  245. {
  246. int w = drm_rect_width(r);
  247. int h = drm_rect_height(r);
  248. if (fixed_point)
  249. DRM_DEBUG_KMS("%s%d.%06ux%d.%06u%+d.%06u%+d.%06u\n", prefix,
  250. w >> 16, ((w & 0xffff) * 15625) >> 10,
  251. h >> 16, ((h & 0xffff) * 15625) >> 10,
  252. r->x1 >> 16, ((r->x1 & 0xffff) * 15625) >> 10,
  253. r->y1 >> 16, ((r->y1 & 0xffff) * 15625) >> 10);
  254. else
  255. DRM_DEBUG_KMS("%s%dx%d%+d%+d\n", prefix, w, h, r->x1, r->y1);
  256. }
  257. EXPORT_SYMBOL(drm_rect_debug_print);
  258. /**
  259. * drm_rect_rotate - Rotate the rectangle
  260. * @r: rectangle to be rotated
  261. * @width: Width of the coordinate space
  262. * @height: Height of the coordinate space
  263. * @rotation: Transformation to be applied
  264. *
  265. * Apply @rotation to the coordinates of rectangle @r.
  266. *
  267. * @width and @height combined with @rotation define
  268. * the location of the new origin.
  269. *
  270. * @width correcsponds to the horizontal and @height
  271. * to the vertical axis of the untransformed coordinate
  272. * space.
  273. */
  274. void drm_rect_rotate(struct drm_rect *r,
  275. int width, int height,
  276. unsigned int rotation)
  277. {
  278. struct drm_rect tmp;
  279. if (rotation & (DRM_REFLECT_X | DRM_REFLECT_Y)) {
  280. tmp = *r;
  281. if (rotation & DRM_REFLECT_X) {
  282. r->x1 = width - tmp.x2;
  283. r->x2 = width - tmp.x1;
  284. }
  285. if (rotation & DRM_REFLECT_Y) {
  286. r->y1 = height - tmp.y2;
  287. r->y2 = height - tmp.y1;
  288. }
  289. }
  290. switch (rotation & DRM_ROTATE_MASK) {
  291. case DRM_ROTATE_0:
  292. break;
  293. case DRM_ROTATE_90:
  294. tmp = *r;
  295. r->x1 = tmp.y1;
  296. r->x2 = tmp.y2;
  297. r->y1 = width - tmp.x2;
  298. r->y2 = width - tmp.x1;
  299. break;
  300. case DRM_ROTATE_180:
  301. tmp = *r;
  302. r->x1 = width - tmp.x2;
  303. r->x2 = width - tmp.x1;
  304. r->y1 = height - tmp.y2;
  305. r->y2 = height - tmp.y1;
  306. break;
  307. case DRM_ROTATE_270:
  308. tmp = *r;
  309. r->x1 = height - tmp.y2;
  310. r->x2 = height - tmp.y1;
  311. r->y1 = tmp.x1;
  312. r->y2 = tmp.x2;
  313. break;
  314. default:
  315. break;
  316. }
  317. }
  318. EXPORT_SYMBOL(drm_rect_rotate);
  319. /**
  320. * drm_rect_rotate_inv - Inverse rotate the rectangle
  321. * @r: rectangle to be rotated
  322. * @width: Width of the coordinate space
  323. * @height: Height of the coordinate space
  324. * @rotation: Transformation whose inverse is to be applied
  325. *
  326. * Apply the inverse of @rotation to the coordinates
  327. * of rectangle @r.
  328. *
  329. * @width and @height combined with @rotation define
  330. * the location of the new origin.
  331. *
  332. * @width correcsponds to the horizontal and @height
  333. * to the vertical axis of the original untransformed
  334. * coordinate space, so that you never have to flip
  335. * them when doing a rotatation and its inverse.
  336. * That is, if you do:
  337. *
  338. * drm_rotate(&r, width, height, rotation);
  339. * drm_rotate_inv(&r, width, height, rotation);
  340. *
  341. * you will always get back the original rectangle.
  342. */
  343. void drm_rect_rotate_inv(struct drm_rect *r,
  344. int width, int height,
  345. unsigned int rotation)
  346. {
  347. struct drm_rect tmp;
  348. switch (rotation & DRM_ROTATE_MASK) {
  349. case DRM_ROTATE_0:
  350. break;
  351. case DRM_ROTATE_90:
  352. tmp = *r;
  353. r->x1 = width - tmp.y2;
  354. r->x2 = width - tmp.y1;
  355. r->y1 = tmp.x1;
  356. r->y2 = tmp.x2;
  357. break;
  358. case DRM_ROTATE_180:
  359. tmp = *r;
  360. r->x1 = width - tmp.x2;
  361. r->x2 = width - tmp.x1;
  362. r->y1 = height - tmp.y2;
  363. r->y2 = height - tmp.y1;
  364. break;
  365. case DRM_ROTATE_270:
  366. tmp = *r;
  367. r->x1 = tmp.y1;
  368. r->x2 = tmp.y2;
  369. r->y1 = height - tmp.x2;
  370. r->y2 = height - tmp.x1;
  371. break;
  372. default:
  373. break;
  374. }
  375. if (rotation & (DRM_REFLECT_X | DRM_REFLECT_Y)) {
  376. tmp = *r;
  377. if (rotation & DRM_REFLECT_X) {
  378. r->x1 = width - tmp.x2;
  379. r->x2 = width - tmp.x1;
  380. }
  381. if (rotation & DRM_REFLECT_Y) {
  382. r->y1 = height - tmp.y2;
  383. r->y2 = height - tmp.y1;
  384. }
  385. }
  386. }
  387. EXPORT_SYMBOL(drm_rect_rotate_inv);