plane.c 8.5 KB

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