drm/vc4: Take margin setup into account when updating planes
authorBoris Brezillon <boris.brezillon@bootlin.com>
Thu, 6 Dec 2018 14:24:38 +0000 (15:24 +0100)
committerBoris Brezillon <boris.brezillon@bootlin.com>
Wed, 19 Dec 2018 13:47:58 +0000 (14:47 +0100)
Applyin margins is just a matter of scaling all planes appropriately
and adjusting the CRTC X/Y offset to account for the
left/right/top/bottom borders.

Create a vc4_plane_margins_adj() function doing that and call it from
vc4_plane_setup_clipping_and_scaling() so that we are ready to attach
margins properties to the HDMI connector.

Signed-off-by: Boris Brezillon <boris.brezillon@bootlin.com>
Reviewed-by: Eric Anholt <eric@anholt.net>
Acked-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Link: https://patchwork.freedesktop.org/patch/msgid/20181206142439.10441-5-boris.brezillon@bootlin.com
drivers/gpu/drm/vc4/vc4_crtc.c
drivers/gpu/drm/vc4/vc4_drv.h
drivers/gpu/drm/vc4/vc4_plane.c

index 3ce136ba8791999f81c89c74e2b976ebe42905b2..97caf1671dd06f883db650de9c52253e0680aa82 100644 (file)
@@ -49,6 +49,13 @@ struct vc4_crtc_state {
        struct drm_mm_node mm;
        bool feed_txp;
        bool txp_armed;
+
+       struct {
+               unsigned int left;
+               unsigned int right;
+               unsigned int top;
+               unsigned int bottom;
+       } margins;
 };
 
 static inline struct vc4_crtc_state *
@@ -624,6 +631,37 @@ static enum drm_mode_status vc4_crtc_mode_valid(struct drm_crtc *crtc,
        return MODE_OK;
 }
 
+void vc4_crtc_get_margins(struct drm_crtc_state *state,
+                         unsigned int *left, unsigned int *right,
+                         unsigned int *top, unsigned int *bottom)
+{
+       struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(state);
+       struct drm_connector_state *conn_state;
+       struct drm_connector *conn;
+       int i;
+
+       *left = vc4_state->margins.left;
+       *right = vc4_state->margins.right;
+       *top = vc4_state->margins.top;
+       *bottom = vc4_state->margins.bottom;
+
+       /* We have to interate over all new connector states because
+        * vc4_crtc_get_margins() might be called before
+        * vc4_crtc_atomic_check() which means margins info in vc4_crtc_state
+        * might be outdated.
+        */
+       for_each_new_connector_in_state(state->state, conn, conn_state, i) {
+               if (conn_state->crtc != state->crtc)
+                       continue;
+
+               *left = conn_state->tv.margins.left;
+               *right = conn_state->tv.margins.right;
+               *top = conn_state->tv.margins.top;
+               *bottom = conn_state->tv.margins.bottom;
+               break;
+       }
+}
+
 static int vc4_crtc_atomic_check(struct drm_crtc *crtc,
                                 struct drm_crtc_state *state)
 {
@@ -671,6 +709,10 @@ static int vc4_crtc_atomic_check(struct drm_crtc *crtc,
                        vc4_state->feed_txp = false;
                }
 
+               vc4_state->margins.left = conn_state->tv.margins.left;
+               vc4_state->margins.right = conn_state->tv.margins.right;
+               vc4_state->margins.top = conn_state->tv.margins.top;
+               vc4_state->margins.bottom = conn_state->tv.margins.bottom;
                break;
        }
 
@@ -972,6 +1014,7 @@ static struct drm_crtc_state *vc4_crtc_duplicate_state(struct drm_crtc *crtc)
 
        old_vc4_state = to_vc4_crtc_state(crtc->state);
        vc4_state->feed_txp = old_vc4_state->feed_txp;
+       vc4_state->margins = old_vc4_state->margins;
 
        __drm_atomic_helper_crtc_duplicate_state(crtc, &vc4_state->base);
        return &vc4_state->base;
index 4f87b03f837d11d547ef649b16de99ec4c764658..c24b078f05939817ff0f94dba61ed6491eefd4a1 100644 (file)
@@ -707,6 +707,9 @@ bool vc4_crtc_get_scanoutpos(struct drm_device *dev, unsigned int crtc_id,
                             const struct drm_display_mode *mode);
 void vc4_crtc_handle_vblank(struct vc4_crtc *crtc);
 void vc4_crtc_txp_armed(struct drm_crtc_state *state);
+void vc4_crtc_get_margins(struct drm_crtc_state *state,
+                         unsigned int *right, unsigned int *left,
+                         unsigned int *top, unsigned int *bottom);
 
 /* vc4_debugfs.c */
 int vc4_debugfs_init(struct drm_minor *minor);
index 283abd7d1e9b6819f6f4b4336645b787e996918b..2901ed0c52230f2a77b79850478b0cd652a39565 100644 (file)
@@ -258,6 +258,52 @@ static u32 vc4_get_scl_field(struct drm_plane_state *state, int plane)
        }
 }
 
+static int vc4_plane_margins_adj(struct drm_plane_state *pstate)
+{
+       struct vc4_plane_state *vc4_pstate = to_vc4_plane_state(pstate);
+       unsigned int left, right, top, bottom, adjhdisplay, adjvdisplay;
+       struct drm_crtc_state *crtc_state;
+
+       crtc_state = drm_atomic_get_new_crtc_state(pstate->state,
+                                                  pstate->crtc);
+
+       vc4_crtc_get_margins(crtc_state, &left, &right, &top, &bottom);
+       if (!left && !right && !top && !bottom)
+               return 0;
+
+       if (left + right >= crtc_state->mode.hdisplay ||
+           top + bottom >= crtc_state->mode.vdisplay)
+               return -EINVAL;
+
+       adjhdisplay = crtc_state->mode.hdisplay - (left + right);
+       vc4_pstate->crtc_x = DIV_ROUND_CLOSEST(vc4_pstate->crtc_x *
+                                              adjhdisplay,
+                                              crtc_state->mode.hdisplay);
+       vc4_pstate->crtc_x += left;
+       if (vc4_pstate->crtc_x > crtc_state->mode.hdisplay - left)
+               vc4_pstate->crtc_x = crtc_state->mode.hdisplay - left;
+
+       adjvdisplay = crtc_state->mode.vdisplay - (top + bottom);
+       vc4_pstate->crtc_y = DIV_ROUND_CLOSEST(vc4_pstate->crtc_y *
+                                              adjvdisplay,
+                                              crtc_state->mode.vdisplay);
+       vc4_pstate->crtc_y += top;
+       if (vc4_pstate->crtc_y > crtc_state->mode.vdisplay - top)
+               vc4_pstate->crtc_y = crtc_state->mode.vdisplay - top;
+
+       vc4_pstate->crtc_w = DIV_ROUND_CLOSEST(vc4_pstate->crtc_w *
+                                              adjhdisplay,
+                                              crtc_state->mode.hdisplay);
+       vc4_pstate->crtc_h = DIV_ROUND_CLOSEST(vc4_pstate->crtc_h *
+                                              adjvdisplay,
+                                              crtc_state->mode.vdisplay);
+
+       if (!vc4_pstate->crtc_w || !vc4_pstate->crtc_h)
+               return -EINVAL;
+
+       return 0;
+}
+
 static int vc4_plane_setup_clipping_and_scaling(struct drm_plane_state *state)
 {
        struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
@@ -306,6 +352,10 @@ static int vc4_plane_setup_clipping_and_scaling(struct drm_plane_state *state)
        vc4_state->crtc_w = state->dst.x2 - state->dst.x1;
        vc4_state->crtc_h = state->dst.y2 - state->dst.y1;
 
+       ret = vc4_plane_margins_adj(state);
+       if (ret)
+               return ret;
+
        vc4_state->x_scaling[0] = vc4_get_scaling_mode(vc4_state->src_w[0],
                                                       vc4_state->crtc_w);
        vc4_state->y_scaling[0] = vc4_get_scaling_mode(vc4_state->src_h[0],