plane.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * Copyright (C) 2017 NVIDIA CORPORATION. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <drm/drm_atomic.h>
  9. #include <drm/drm_atomic_helper.h>
  10. #include <drm/drm_plane_helper.h>
  11. #include "dc.h"
  12. #include "plane.h"
  13. static void tegra_plane_destroy(struct drm_plane *plane)
  14. {
  15. struct tegra_plane *p = to_tegra_plane(plane);
  16. drm_plane_cleanup(plane);
  17. kfree(p);
  18. }
  19. static void tegra_plane_reset(struct drm_plane *plane)
  20. {
  21. struct tegra_plane_state *state;
  22. if (plane->state)
  23. __drm_atomic_helper_plane_destroy_state(plane->state);
  24. kfree(plane->state);
  25. plane->state = NULL;
  26. state = kzalloc(sizeof(*state), GFP_KERNEL);
  27. if (state) {
  28. plane->state = &state->base;
  29. plane->state->plane = plane;
  30. }
  31. }
  32. static struct drm_plane_state *
  33. tegra_plane_atomic_duplicate_state(struct drm_plane *plane)
  34. {
  35. struct tegra_plane_state *state = to_tegra_plane_state(plane->state);
  36. struct tegra_plane_state *copy;
  37. unsigned int i;
  38. copy = kmalloc(sizeof(*copy), GFP_KERNEL);
  39. if (!copy)
  40. return NULL;
  41. __drm_atomic_helper_plane_duplicate_state(plane, &copy->base);
  42. copy->tiling = state->tiling;
  43. copy->format = state->format;
  44. copy->swap = state->swap;
  45. copy->opaque = state->opaque;
  46. for (i = 0; i < 3; i++)
  47. copy->dependent[i] = state->dependent[i];
  48. return &copy->base;
  49. }
  50. static void tegra_plane_atomic_destroy_state(struct drm_plane *plane,
  51. struct drm_plane_state *state)
  52. {
  53. __drm_atomic_helper_plane_destroy_state(state);
  54. kfree(state);
  55. }
  56. const struct drm_plane_funcs tegra_plane_funcs = {
  57. .update_plane = drm_atomic_helper_update_plane,
  58. .disable_plane = drm_atomic_helper_disable_plane,
  59. .destroy = tegra_plane_destroy,
  60. .reset = tegra_plane_reset,
  61. .atomic_duplicate_state = tegra_plane_atomic_duplicate_state,
  62. .atomic_destroy_state = tegra_plane_atomic_destroy_state,
  63. };
  64. int tegra_plane_state_add(struct tegra_plane *plane,
  65. struct drm_plane_state *state)
  66. {
  67. struct drm_crtc_state *crtc_state;
  68. struct tegra_dc_state *tegra;
  69. struct drm_rect clip;
  70. int err;
  71. /* Propagate errors from allocation or locking failures. */
  72. crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
  73. if (IS_ERR(crtc_state))
  74. return PTR_ERR(crtc_state);
  75. clip.x1 = 0;
  76. clip.y1 = 0;
  77. clip.x2 = crtc_state->mode.hdisplay;
  78. clip.y2 = crtc_state->mode.vdisplay;
  79. /* Check plane state for visibility and calculate clipping bounds */
  80. err = drm_atomic_helper_check_plane_state(state, crtc_state, &clip,
  81. 0, INT_MAX, true, true);
  82. if (err < 0)
  83. return err;
  84. tegra = to_dc_state(crtc_state);
  85. tegra->planes |= WIN_A_ACT_REQ << plane->index;
  86. return 0;
  87. }
  88. int tegra_plane_format(u32 fourcc, u32 *format, u32 *swap)
  89. {
  90. /* assume no swapping of fetched data */
  91. if (swap)
  92. *swap = BYTE_SWAP_NOSWAP;
  93. switch (fourcc) {
  94. case DRM_FORMAT_ARGB4444:
  95. *format = WIN_COLOR_DEPTH_B4G4R4A4;
  96. break;
  97. case DRM_FORMAT_ARGB1555:
  98. *format = WIN_COLOR_DEPTH_B5G5R5A1;
  99. break;
  100. case DRM_FORMAT_RGB565:
  101. *format = WIN_COLOR_DEPTH_B5G6R5;
  102. break;
  103. case DRM_FORMAT_RGBA5551:
  104. *format = WIN_COLOR_DEPTH_A1B5G5R5;
  105. break;
  106. case DRM_FORMAT_ARGB8888:
  107. *format = WIN_COLOR_DEPTH_B8G8R8A8;
  108. break;
  109. case DRM_FORMAT_ABGR8888:
  110. *format = WIN_COLOR_DEPTH_R8G8B8A8;
  111. break;
  112. case DRM_FORMAT_ABGR4444:
  113. *format = WIN_COLOR_DEPTH_R4G4B4A4;
  114. break;
  115. case DRM_FORMAT_ABGR1555:
  116. *format = WIN_COLOR_DEPTH_R5G5B5A;
  117. break;
  118. case DRM_FORMAT_BGRA5551:
  119. *format = WIN_COLOR_DEPTH_AR5G5B5;
  120. break;
  121. case DRM_FORMAT_XRGB1555:
  122. *format = WIN_COLOR_DEPTH_B5G5R5X1;
  123. break;
  124. case DRM_FORMAT_RGBX5551:
  125. *format = WIN_COLOR_DEPTH_X1B5G5R5;
  126. break;
  127. case DRM_FORMAT_XBGR1555:
  128. *format = WIN_COLOR_DEPTH_R5G5B5X1;
  129. break;
  130. case DRM_FORMAT_BGRX5551:
  131. *format = WIN_COLOR_DEPTH_X1R5G5B5;
  132. break;
  133. case DRM_FORMAT_BGR565:
  134. *format = WIN_COLOR_DEPTH_R5G6B5;
  135. break;
  136. case DRM_FORMAT_BGRA8888:
  137. *format = WIN_COLOR_DEPTH_A8R8G8B8;
  138. break;
  139. case DRM_FORMAT_RGBA8888:
  140. *format = WIN_COLOR_DEPTH_A8B8G8R8;
  141. break;
  142. case DRM_FORMAT_XRGB8888:
  143. *format = WIN_COLOR_DEPTH_B8G8R8X8;
  144. break;
  145. case DRM_FORMAT_XBGR8888:
  146. *format = WIN_COLOR_DEPTH_R8G8B8X8;
  147. break;
  148. case DRM_FORMAT_UYVY:
  149. *format = WIN_COLOR_DEPTH_YCbCr422;
  150. break;
  151. case DRM_FORMAT_YUYV:
  152. if (!swap)
  153. return -EINVAL;
  154. *format = WIN_COLOR_DEPTH_YCbCr422;
  155. *swap = BYTE_SWAP_SWAP2;
  156. break;
  157. case DRM_FORMAT_YUV420:
  158. *format = WIN_COLOR_DEPTH_YCbCr420P;
  159. break;
  160. case DRM_FORMAT_YUV422:
  161. *format = WIN_COLOR_DEPTH_YCbCr422P;
  162. break;
  163. default:
  164. return -EINVAL;
  165. }
  166. return 0;
  167. }
  168. bool tegra_plane_format_is_yuv(unsigned int format, bool *planar)
  169. {
  170. switch (format) {
  171. case WIN_COLOR_DEPTH_YCbCr422:
  172. case WIN_COLOR_DEPTH_YUV422:
  173. if (planar)
  174. *planar = false;
  175. return true;
  176. case WIN_COLOR_DEPTH_YCbCr420P:
  177. case WIN_COLOR_DEPTH_YUV420P:
  178. case WIN_COLOR_DEPTH_YCbCr422P:
  179. case WIN_COLOR_DEPTH_YUV422P:
  180. case WIN_COLOR_DEPTH_YCbCr422R:
  181. case WIN_COLOR_DEPTH_YUV422R:
  182. case WIN_COLOR_DEPTH_YCbCr422RA:
  183. case WIN_COLOR_DEPTH_YUV422RA:
  184. if (planar)
  185. *planar = true;
  186. return true;
  187. }
  188. if (planar)
  189. *planar = false;
  190. return false;
  191. }
  192. static bool __drm_format_has_alpha(u32 format)
  193. {
  194. switch (format) {
  195. case DRM_FORMAT_ARGB1555:
  196. case DRM_FORMAT_RGBA5551:
  197. case DRM_FORMAT_ABGR8888:
  198. case DRM_FORMAT_ARGB8888:
  199. return true;
  200. }
  201. return false;
  202. }
  203. /*
  204. * This is applicable to Tegra20 and Tegra30 only where the opaque formats can
  205. * be emulated using the alpha formats and alpha blending disabled.
  206. */
  207. bool tegra_plane_format_has_alpha(unsigned int format)
  208. {
  209. switch (format) {
  210. case WIN_COLOR_DEPTH_B5G5R5A1:
  211. case WIN_COLOR_DEPTH_A1B5G5R5:
  212. case WIN_COLOR_DEPTH_R8G8B8A8:
  213. case WIN_COLOR_DEPTH_B8G8R8A8:
  214. return true;
  215. }
  216. return false;
  217. }
  218. int tegra_plane_format_get_alpha(unsigned int opaque, unsigned int *alpha)
  219. {
  220. if (tegra_plane_format_is_yuv(opaque, NULL)) {
  221. *alpha = opaque;
  222. return 0;
  223. }
  224. switch (opaque) {
  225. case WIN_COLOR_DEPTH_B5G5R5X1:
  226. *alpha = WIN_COLOR_DEPTH_B5G5R5A1;
  227. return 0;
  228. case WIN_COLOR_DEPTH_X1B5G5R5:
  229. *alpha = WIN_COLOR_DEPTH_A1B5G5R5;
  230. return 0;
  231. case WIN_COLOR_DEPTH_R8G8B8X8:
  232. *alpha = WIN_COLOR_DEPTH_R8G8B8A8;
  233. return 0;
  234. case WIN_COLOR_DEPTH_B8G8R8X8:
  235. *alpha = WIN_COLOR_DEPTH_B8G8R8A8;
  236. return 0;
  237. }
  238. return -EINVAL;
  239. }
  240. unsigned int tegra_plane_get_overlap_index(struct tegra_plane *plane,
  241. struct tegra_plane *other)
  242. {
  243. unsigned int index = 0, i;
  244. WARN_ON(plane == other);
  245. for (i = 0; i < 3; i++) {
  246. if (i == plane->index)
  247. continue;
  248. if (i == other->index)
  249. break;
  250. index++;
  251. }
  252. return index;
  253. }
  254. void tegra_plane_check_dependent(struct tegra_plane *tegra,
  255. struct tegra_plane_state *state)
  256. {
  257. struct drm_plane_state *old, *new;
  258. struct drm_plane *plane;
  259. unsigned int zpos[2];
  260. unsigned int i;
  261. for (i = 0; i < 3; i++)
  262. state->dependent[i] = false;
  263. for (i = 0; i < 2; i++)
  264. zpos[i] = 0;
  265. for_each_oldnew_plane_in_state(state->base.state, plane, old, new, i) {
  266. struct tegra_plane *p = to_tegra_plane(plane);
  267. unsigned index;
  268. /* skip this plane and planes on different CRTCs */
  269. if (p == tegra || new->crtc != state->base.crtc)
  270. continue;
  271. index = tegra_plane_get_overlap_index(tegra, p);
  272. /*
  273. * If any of the other planes is on top of this plane and uses
  274. * a format with an alpha component, mark this plane as being
  275. * dependent, meaning it's alpha value will be 1 minus the sum
  276. * of alpha components of the overlapping planes.
  277. */
  278. if (p->index > tegra->index) {
  279. if (__drm_format_has_alpha(new->fb->format->format))
  280. state->dependent[index] = true;
  281. /* keep track of the Z position */
  282. zpos[index] = p->index;
  283. }
  284. }
  285. /*
  286. * The region where three windows overlap is the intersection of the
  287. * two regions where two windows overlap. It contributes to the area
  288. * if any of the windows on top of it have an alpha component.
  289. */
  290. for (i = 0; i < 2; i++)
  291. state->dependent[2] = state->dependent[2] ||
  292. state->dependent[i];
  293. /*
  294. * However, if any of the windows on top of this window is opaque, it
  295. * will completely conceal this window within that area, so avoid the
  296. * window from contributing to the area.
  297. */
  298. for (i = 0; i < 2; i++) {
  299. if (zpos[i] > tegra->index)
  300. state->dependent[2] = state->dependent[2] &&
  301. state->dependent[i];
  302. }
  303. }