plane.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. int err;
  70. /* Propagate errors from allocation or locking failures. */
  71. crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
  72. if (IS_ERR(crtc_state))
  73. return PTR_ERR(crtc_state);
  74. /* Check plane state for visibility and calculate clipping bounds */
  75. err = drm_atomic_helper_check_plane_state(state, crtc_state,
  76. 0, INT_MAX, true, true);
  77. if (err < 0)
  78. return err;
  79. tegra = to_dc_state(crtc_state);
  80. tegra->planes |= WIN_A_ACT_REQ << plane->index;
  81. return 0;
  82. }
  83. int tegra_plane_format(u32 fourcc, u32 *format, u32 *swap)
  84. {
  85. /* assume no swapping of fetched data */
  86. if (swap)
  87. *swap = BYTE_SWAP_NOSWAP;
  88. switch (fourcc) {
  89. case DRM_FORMAT_ARGB4444:
  90. *format = WIN_COLOR_DEPTH_B4G4R4A4;
  91. break;
  92. case DRM_FORMAT_ARGB1555:
  93. *format = WIN_COLOR_DEPTH_B5G5R5A1;
  94. break;
  95. case DRM_FORMAT_RGB565:
  96. *format = WIN_COLOR_DEPTH_B5G6R5;
  97. break;
  98. case DRM_FORMAT_RGBA5551:
  99. *format = WIN_COLOR_DEPTH_A1B5G5R5;
  100. break;
  101. case DRM_FORMAT_ARGB8888:
  102. *format = WIN_COLOR_DEPTH_B8G8R8A8;
  103. break;
  104. case DRM_FORMAT_ABGR8888:
  105. *format = WIN_COLOR_DEPTH_R8G8B8A8;
  106. break;
  107. case DRM_FORMAT_ABGR4444:
  108. *format = WIN_COLOR_DEPTH_R4G4B4A4;
  109. break;
  110. case DRM_FORMAT_ABGR1555:
  111. *format = WIN_COLOR_DEPTH_R5G5B5A;
  112. break;
  113. case DRM_FORMAT_BGRA5551:
  114. *format = WIN_COLOR_DEPTH_AR5G5B5;
  115. break;
  116. case DRM_FORMAT_XRGB1555:
  117. *format = WIN_COLOR_DEPTH_B5G5R5X1;
  118. break;
  119. case DRM_FORMAT_RGBX5551:
  120. *format = WIN_COLOR_DEPTH_X1B5G5R5;
  121. break;
  122. case DRM_FORMAT_XBGR1555:
  123. *format = WIN_COLOR_DEPTH_R5G5B5X1;
  124. break;
  125. case DRM_FORMAT_BGRX5551:
  126. *format = WIN_COLOR_DEPTH_X1R5G5B5;
  127. break;
  128. case DRM_FORMAT_BGR565:
  129. *format = WIN_COLOR_DEPTH_R5G6B5;
  130. break;
  131. case DRM_FORMAT_BGRA8888:
  132. *format = WIN_COLOR_DEPTH_A8R8G8B8;
  133. break;
  134. case DRM_FORMAT_RGBA8888:
  135. *format = WIN_COLOR_DEPTH_A8B8G8R8;
  136. break;
  137. case DRM_FORMAT_XRGB8888:
  138. *format = WIN_COLOR_DEPTH_B8G8R8X8;
  139. break;
  140. case DRM_FORMAT_XBGR8888:
  141. *format = WIN_COLOR_DEPTH_R8G8B8X8;
  142. break;
  143. case DRM_FORMAT_UYVY:
  144. *format = WIN_COLOR_DEPTH_YCbCr422;
  145. break;
  146. case DRM_FORMAT_YUYV:
  147. if (!swap)
  148. return -EINVAL;
  149. *format = WIN_COLOR_DEPTH_YCbCr422;
  150. *swap = BYTE_SWAP_SWAP2;
  151. break;
  152. case DRM_FORMAT_YUV420:
  153. *format = WIN_COLOR_DEPTH_YCbCr420P;
  154. break;
  155. case DRM_FORMAT_YUV422:
  156. *format = WIN_COLOR_DEPTH_YCbCr422P;
  157. break;
  158. default:
  159. return -EINVAL;
  160. }
  161. return 0;
  162. }
  163. bool tegra_plane_format_is_yuv(unsigned int format, bool *planar)
  164. {
  165. switch (format) {
  166. case WIN_COLOR_DEPTH_YCbCr422:
  167. case WIN_COLOR_DEPTH_YUV422:
  168. if (planar)
  169. *planar = false;
  170. return true;
  171. case WIN_COLOR_DEPTH_YCbCr420P:
  172. case WIN_COLOR_DEPTH_YUV420P:
  173. case WIN_COLOR_DEPTH_YCbCr422P:
  174. case WIN_COLOR_DEPTH_YUV422P:
  175. case WIN_COLOR_DEPTH_YCbCr422R:
  176. case WIN_COLOR_DEPTH_YUV422R:
  177. case WIN_COLOR_DEPTH_YCbCr422RA:
  178. case WIN_COLOR_DEPTH_YUV422RA:
  179. if (planar)
  180. *planar = true;
  181. return true;
  182. }
  183. if (planar)
  184. *planar = false;
  185. return false;
  186. }
  187. static bool __drm_format_has_alpha(u32 format)
  188. {
  189. switch (format) {
  190. case DRM_FORMAT_ARGB1555:
  191. case DRM_FORMAT_RGBA5551:
  192. case DRM_FORMAT_ABGR8888:
  193. case DRM_FORMAT_ARGB8888:
  194. return true;
  195. }
  196. return false;
  197. }
  198. /*
  199. * This is applicable to Tegra20 and Tegra30 only where the opaque formats can
  200. * be emulated using the alpha formats and alpha blending disabled.
  201. */
  202. bool tegra_plane_format_has_alpha(unsigned int format)
  203. {
  204. switch (format) {
  205. case WIN_COLOR_DEPTH_B5G5R5A1:
  206. case WIN_COLOR_DEPTH_A1B5G5R5:
  207. case WIN_COLOR_DEPTH_R8G8B8A8:
  208. case WIN_COLOR_DEPTH_B8G8R8A8:
  209. return true;
  210. }
  211. return false;
  212. }
  213. int tegra_plane_format_get_alpha(unsigned int opaque, unsigned int *alpha)
  214. {
  215. if (tegra_plane_format_is_yuv(opaque, NULL)) {
  216. *alpha = opaque;
  217. return 0;
  218. }
  219. switch (opaque) {
  220. case WIN_COLOR_DEPTH_B5G5R5X1:
  221. *alpha = WIN_COLOR_DEPTH_B5G5R5A1;
  222. return 0;
  223. case WIN_COLOR_DEPTH_X1B5G5R5:
  224. *alpha = WIN_COLOR_DEPTH_A1B5G5R5;
  225. return 0;
  226. case WIN_COLOR_DEPTH_R8G8B8X8:
  227. *alpha = WIN_COLOR_DEPTH_R8G8B8A8;
  228. return 0;
  229. case WIN_COLOR_DEPTH_B8G8R8X8:
  230. *alpha = WIN_COLOR_DEPTH_B8G8R8A8;
  231. return 0;
  232. }
  233. return -EINVAL;
  234. }
  235. unsigned int tegra_plane_get_overlap_index(struct tegra_plane *plane,
  236. struct tegra_plane *other)
  237. {
  238. unsigned int index = 0, i;
  239. WARN_ON(plane == other);
  240. for (i = 0; i < 3; i++) {
  241. if (i == plane->index)
  242. continue;
  243. if (i == other->index)
  244. break;
  245. index++;
  246. }
  247. return index;
  248. }
  249. void tegra_plane_check_dependent(struct tegra_plane *tegra,
  250. struct tegra_plane_state *state)
  251. {
  252. struct drm_plane_state *old, *new;
  253. struct drm_plane *plane;
  254. unsigned int zpos[2];
  255. unsigned int i;
  256. for (i = 0; i < 3; i++)
  257. state->dependent[i] = false;
  258. for (i = 0; i < 2; i++)
  259. zpos[i] = 0;
  260. for_each_oldnew_plane_in_state(state->base.state, plane, old, new, i) {
  261. struct tegra_plane *p = to_tegra_plane(plane);
  262. unsigned index;
  263. /* skip this plane and planes on different CRTCs */
  264. if (p == tegra || new->crtc != state->base.crtc)
  265. continue;
  266. index = tegra_plane_get_overlap_index(tegra, p);
  267. /*
  268. * If any of the other planes is on top of this plane and uses
  269. * a format with an alpha component, mark this plane as being
  270. * dependent, meaning it's alpha value will be 1 minus the sum
  271. * of alpha components of the overlapping planes.
  272. */
  273. if (p->index > tegra->index) {
  274. if (__drm_format_has_alpha(new->fb->format->format))
  275. state->dependent[index] = true;
  276. /* keep track of the Z position */
  277. zpos[index] = p->index;
  278. }
  279. }
  280. /*
  281. * The region where three windows overlap is the intersection of the
  282. * two regions where two windows overlap. It contributes to the area
  283. * if any of the windows on top of it have an alpha component.
  284. */
  285. for (i = 0; i < 2; i++)
  286. state->dependent[2] = state->dependent[2] ||
  287. state->dependent[i];
  288. /*
  289. * However, if any of the windows on top of this window is opaque, it
  290. * will completely conceal this window within that area, so avoid the
  291. * window from contributing to the area.
  292. */
  293. for (i = 0; i < 2; i++) {
  294. if (zpos[i] > tegra->index)
  295. state->dependent[2] = state->dependent[2] &&
  296. state->dependent[i];
  297. }
  298. }