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