2 * Copyright (C) 2017 NVIDIA CORPORATION. All rights reserved.
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.
9 #include <drm/drm_atomic.h>
10 #include <drm/drm_atomic_helper.h>
11 #include <drm/drm_plane_helper.h>
16 static void tegra_plane_destroy(struct drm_plane *plane)
18 struct tegra_plane *p = to_tegra_plane(plane);
20 drm_plane_cleanup(plane);
24 static void tegra_plane_reset(struct drm_plane *plane)
26 struct tegra_plane_state *state;
29 __drm_atomic_helper_plane_destroy_state(plane->state);
34 state = kzalloc(sizeof(*state), GFP_KERNEL);
36 plane->state = &state->base;
37 plane->state->plane = plane;
41 static struct drm_plane_state *
42 tegra_plane_atomic_duplicate_state(struct drm_plane *plane)
44 struct tegra_plane_state *state = to_tegra_plane_state(plane->state);
45 struct tegra_plane_state *copy;
48 copy = kmalloc(sizeof(*copy), GFP_KERNEL);
52 __drm_atomic_helper_plane_duplicate_state(plane, ©->base);
53 copy->tiling = state->tiling;
54 copy->format = state->format;
55 copy->swap = state->swap;
56 copy->opaque = state->opaque;
58 for (i = 0; i < 3; i++)
59 copy->dependent[i] = state->dependent[i];
64 static void tegra_plane_atomic_destroy_state(struct drm_plane *plane,
65 struct drm_plane_state *state)
67 __drm_atomic_helper_plane_destroy_state(state);
71 static bool tegra_plane_format_mod_supported(struct drm_plane *plane,
75 const struct drm_format_info *info = drm_format_info(format);
77 if (modifier == DRM_FORMAT_MOD_LINEAR)
80 if (info->num_planes == 1)
86 const struct drm_plane_funcs tegra_plane_funcs = {
87 .update_plane = drm_atomic_helper_update_plane,
88 .disable_plane = drm_atomic_helper_disable_plane,
89 .destroy = tegra_plane_destroy,
90 .reset = tegra_plane_reset,
91 .atomic_duplicate_state = tegra_plane_atomic_duplicate_state,
92 .atomic_destroy_state = tegra_plane_atomic_destroy_state,
93 .format_mod_supported = tegra_plane_format_mod_supported,
96 int tegra_plane_state_add(struct tegra_plane *plane,
97 struct drm_plane_state *state)
99 struct drm_crtc_state *crtc_state;
100 struct tegra_dc_state *tegra;
103 /* Propagate errors from allocation or locking failures. */
104 crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
105 if (IS_ERR(crtc_state))
106 return PTR_ERR(crtc_state);
108 /* Check plane state for visibility and calculate clipping bounds */
109 err = drm_atomic_helper_check_plane_state(state, crtc_state,
110 0, INT_MAX, true, true);
114 tegra = to_dc_state(crtc_state);
116 tegra->planes |= WIN_A_ACT_REQ << plane->index;
121 int tegra_plane_format(u32 fourcc, u32 *format, u32 *swap)
123 /* assume no swapping of fetched data */
125 *swap = BYTE_SWAP_NOSWAP;
128 case DRM_FORMAT_ARGB4444:
129 *format = WIN_COLOR_DEPTH_B4G4R4A4;
132 case DRM_FORMAT_ARGB1555:
133 *format = WIN_COLOR_DEPTH_B5G5R5A1;
136 case DRM_FORMAT_RGB565:
137 *format = WIN_COLOR_DEPTH_B5G6R5;
140 case DRM_FORMAT_RGBA5551:
141 *format = WIN_COLOR_DEPTH_A1B5G5R5;
144 case DRM_FORMAT_ARGB8888:
145 *format = WIN_COLOR_DEPTH_B8G8R8A8;
148 case DRM_FORMAT_ABGR8888:
149 *format = WIN_COLOR_DEPTH_R8G8B8A8;
152 case DRM_FORMAT_ABGR4444:
153 *format = WIN_COLOR_DEPTH_R4G4B4A4;
156 case DRM_FORMAT_ABGR1555:
157 *format = WIN_COLOR_DEPTH_R5G5B5A;
160 case DRM_FORMAT_BGRA5551:
161 *format = WIN_COLOR_DEPTH_AR5G5B5;
164 case DRM_FORMAT_XRGB1555:
165 *format = WIN_COLOR_DEPTH_B5G5R5X1;
168 case DRM_FORMAT_RGBX5551:
169 *format = WIN_COLOR_DEPTH_X1B5G5R5;
172 case DRM_FORMAT_XBGR1555:
173 *format = WIN_COLOR_DEPTH_R5G5B5X1;
176 case DRM_FORMAT_BGRX5551:
177 *format = WIN_COLOR_DEPTH_X1R5G5B5;
180 case DRM_FORMAT_BGR565:
181 *format = WIN_COLOR_DEPTH_R5G6B5;
184 case DRM_FORMAT_BGRA8888:
185 *format = WIN_COLOR_DEPTH_A8R8G8B8;
188 case DRM_FORMAT_RGBA8888:
189 *format = WIN_COLOR_DEPTH_A8B8G8R8;
192 case DRM_FORMAT_XRGB8888:
193 *format = WIN_COLOR_DEPTH_B8G8R8X8;
196 case DRM_FORMAT_XBGR8888:
197 *format = WIN_COLOR_DEPTH_R8G8B8X8;
200 case DRM_FORMAT_UYVY:
201 *format = WIN_COLOR_DEPTH_YCbCr422;
204 case DRM_FORMAT_YUYV:
208 *format = WIN_COLOR_DEPTH_YCbCr422;
209 *swap = BYTE_SWAP_SWAP2;
212 case DRM_FORMAT_YUV420:
213 *format = WIN_COLOR_DEPTH_YCbCr420P;
216 case DRM_FORMAT_YUV422:
217 *format = WIN_COLOR_DEPTH_YCbCr422P;
227 bool tegra_plane_format_is_yuv(unsigned int format, bool *planar)
230 case WIN_COLOR_DEPTH_YCbCr422:
231 case WIN_COLOR_DEPTH_YUV422:
237 case WIN_COLOR_DEPTH_YCbCr420P:
238 case WIN_COLOR_DEPTH_YUV420P:
239 case WIN_COLOR_DEPTH_YCbCr422P:
240 case WIN_COLOR_DEPTH_YUV422P:
241 case WIN_COLOR_DEPTH_YCbCr422R:
242 case WIN_COLOR_DEPTH_YUV422R:
243 case WIN_COLOR_DEPTH_YCbCr422RA:
244 case WIN_COLOR_DEPTH_YUV422RA:
257 static bool __drm_format_has_alpha(u32 format)
260 case DRM_FORMAT_ARGB1555:
261 case DRM_FORMAT_RGBA5551:
262 case DRM_FORMAT_ABGR8888:
263 case DRM_FORMAT_ARGB8888:
271 * This is applicable to Tegra20 and Tegra30 only where the opaque formats can
272 * be emulated using the alpha formats and alpha blending disabled.
274 bool tegra_plane_format_has_alpha(unsigned int format)
277 case WIN_COLOR_DEPTH_B5G5R5A1:
278 case WIN_COLOR_DEPTH_A1B5G5R5:
279 case WIN_COLOR_DEPTH_R8G8B8A8:
280 case WIN_COLOR_DEPTH_B8G8R8A8:
287 int tegra_plane_format_get_alpha(unsigned int opaque, unsigned int *alpha)
289 if (tegra_plane_format_is_yuv(opaque, NULL)) {
295 case WIN_COLOR_DEPTH_B5G5R5X1:
296 *alpha = WIN_COLOR_DEPTH_B5G5R5A1;
299 case WIN_COLOR_DEPTH_X1B5G5R5:
300 *alpha = WIN_COLOR_DEPTH_A1B5G5R5;
303 case WIN_COLOR_DEPTH_R8G8B8X8:
304 *alpha = WIN_COLOR_DEPTH_R8G8B8A8;
307 case WIN_COLOR_DEPTH_B8G8R8X8:
308 *alpha = WIN_COLOR_DEPTH_B8G8R8A8;
311 case WIN_COLOR_DEPTH_B5G6R5:
319 static unsigned int tegra_plane_get_overlap_index(struct tegra_plane *plane,
320 struct tegra_plane *other)
322 unsigned int index = 0, i;
324 WARN_ON(plane == other);
326 for (i = 0; i < 3; i++) {
327 if (i == plane->index)
330 if (i == other->index)
339 void tegra_plane_check_dependent(struct tegra_plane *tegra,
340 struct tegra_plane_state *state)
342 struct drm_plane_state *old, *new;
343 struct drm_plane *plane;
344 unsigned int zpos[2];
347 for (i = 0; i < 2; i++)
350 for_each_oldnew_plane_in_state(state->base.state, plane, old, new, i) {
351 struct tegra_plane *p = to_tegra_plane(plane);
354 /* skip this plane and planes on different CRTCs */
355 if (p == tegra || new->crtc != state->base.crtc)
358 index = tegra_plane_get_overlap_index(tegra, p);
360 state->dependent[index] = false;
363 * If any of the other planes is on top of this plane and uses
364 * a format with an alpha component, mark this plane as being
365 * dependent, meaning it's alpha value will be 1 minus the sum
366 * of alpha components of the overlapping planes.
368 if (p->index > tegra->index) {
369 if (__drm_format_has_alpha(new->fb->format->format))
370 state->dependent[index] = true;
372 /* keep track of the Z position */
373 zpos[index] = p->index;
378 * The region where three windows overlap is the intersection of the
379 * two regions where two windows overlap. It contributes to the area
380 * if any of the windows on top of it have an alpha component.
382 for (i = 0; i < 2; i++)
383 state->dependent[2] = state->dependent[2] ||
387 * However, if any of the windows on top of this window is opaque, it
388 * will completely conceal this window within that area, so avoid the
389 * window from contributing to the area.
391 for (i = 0; i < 2; i++) {
392 if (zpos[i] > tegra->index)
393 state->dependent[2] = state->dependent[2] &&