chiark / gitweb /
terminal: fix mode sync for connectors
[elogind.git] / src / libsystemd-terminal / grdev-drm.c
index 3481584fbf760bc4e3331b9a2e76a6c4b0e5240d..2e55ad326b15ff3484b52179949673a2497267e0 100644 (file)
@@ -264,6 +264,7 @@ struct grdrm_card {
         Hashmap *object_map;
 
         bool async_hotplug : 1;
+        bool hotplug : 1;
         bool running : 1;
         bool ready : 1;
         bool cap_dumb : 1;
@@ -346,6 +347,8 @@ static bool grdrm_modes_compatible(const struct drm_mode_modeinfo *a, const stru
                 return false;
         if (a->vdisplay != b->vdisplay)
                 return false;
+        if (a->vrefresh != b->vrefresh)
+                return false;
 
         return true;
 }
@@ -601,12 +604,19 @@ static int grdrm_connector_resync(grdrm_connector *connector) {
                 res.count_encoders = connector->kern.max_encoders;
                 res.count_props = connector->kern.max_props;
 
-                /* Retrieve modes only if we have none. This avoids expensive
-                 * EDID reads in the kernel, that can slow down resyncs
-                 * considerably! */
-                if (connector->kern.n_modes == 0) {
-                        res.modes_ptr = PTR_TO_UINT64(connector->kern.modes);
-                        res.count_modes = connector->kern.max_modes;
+                /* The kernel reads modes from the EDID information only if we
+                 * pass count_modes==0. This is a legacy hack for libdrm (which
+                 * called every ioctl twice). Now we have to adopt.. *sigh*.
+                 * If we never received an hotplug event, there's no reason to
+                 * sync modes. EDID reads are heavy, so skip that if not
+                 * required. */
+                if (card->hotplug) {
+                        if (tries > 0) {
+                                res.modes_ptr = PTR_TO_UINT64(connector->kern.modes);
+                                res.count_modes = connector->kern.max_modes;
+                        } else {
+                                resized = true;
+                        }
                 }
 
                 r = ioctl(card->fd, DRM_IOCTL_MODE_GETCONNECTOR, &res);
@@ -687,7 +697,6 @@ static int grdrm_connector_resync(grdrm_connector *connector) {
                         continue;
 
                 connector->kern.n_encoders = res.count_encoders;
-                connector->kern.n_modes = res.count_modes;
                 connector->kern.n_props = res.count_props;
                 connector->kern.type = res.connector_type;
                 connector->kern.type_id = res.connector_type_id;
@@ -696,6 +705,8 @@ static int grdrm_connector_resync(grdrm_connector *connector) {
                 connector->kern.mm_width = res.mm_width;
                 connector->kern.mm_height = res.mm_height;
                 connector->kern.subpixel = res.subpixel;
+                if (res.modes_ptr == PTR_TO_UINT64(connector->kern.modes))
+                        connector->kern.n_modes = res.count_modes;
 
                 break;
         }
@@ -1038,7 +1049,8 @@ static void grdrm_crtc_expose(grdrm_crtc *crtc) {
         pipe = crtc->pipe;
         if (pipe) {
                 if (pipe->base.width != crtc->set.mode.hdisplay ||
-                    pipe->base.height != crtc->set.mode.vdisplay) {
+                    pipe->base.height != crtc->set.mode.vdisplay ||
+                    pipe->base.vrefresh != crtc->set.mode.vrefresh) {
                         grdev_pipe_free(&pipe->base);
                         crtc->pipe = NULL;
                         pipe = NULL;
@@ -1083,15 +1095,151 @@ static void grdrm_crtc_expose(grdrm_crtc *crtc) {
         grdev_pipe_ready(&crtc->pipe->base, true);
 }
 
-static void grdrm_crtc_commit(grdrm_crtc *crtc) {
+static void grdrm_crtc_commit_deep(grdrm_crtc *crtc, grdev_fb **slot) {
+        struct drm_mode_crtc set_crtc = { .crtc_id = crtc->object.id };
+        grdrm_card *card = crtc->object.card;
+        grdrm_pipe *pipe = crtc->pipe;
+        grdrm_fb *fb = fb_from_base(*slot);
+        size_t i;
+        int r;
+
+        assert(crtc);
+        assert(slot);
+        assert(*slot);
+        assert(pipe);
+
+        set_crtc.set_connectors_ptr = PTR_TO_UINT64(crtc->set.connectors);
+        set_crtc.count_connectors = crtc->set.n_connectors;
+        set_crtc.fb_id = fb->id;
+        set_crtc.x = 0;
+        set_crtc.y = 0;
+        set_crtc.mode_valid = 1;
+        set_crtc.mode = crtc->set.mode;
+
+        r = ioctl(card->fd, DRM_IOCTL_MODE_SETCRTC, &set_crtc);
+        if (r < 0) {
+                r = -errno;
+                log_debug("grdrm: %s: cannot set crtc %" PRIu32 ": %m",
+                          card->base.name, crtc->object.id);
+
+                grdrm_card_async(card, r);
+                return;
+        }
+
+        if (!crtc->applied) {
+                log_debug("grdrm: %s: crtc %" PRIu32 " applied via deep modeset",
+                          card->base.name, crtc->object.id);
+                crtc->applied = true;
+        }
+
+        *slot = NULL;
+        pipe->base.front = &fb->base;
+        fb->flipid = 0;
+        ++pipe->counter;
+        pipe->base.flipping = false;
+        pipe->base.flip = false;
+
+        /* We cannot schedule dummy page-flips on pipes, hence, the
+         * application would have to schedule their own frame-timers.
+         * To avoid duplicating that everywhere, we schedule our own
+         * timer and raise a fake FRAME event when it fires. */
+        grdev_pipe_schedule(&pipe->base, 1);
+
+        if (!pipe->base.back) {
+                for (i = 0; i < pipe->base.max_fbs; ++i) {
+                        if (!pipe->base.fbs[i])
+                                continue;
+
+                        fb = fb_from_base(pipe->base.fbs[i]);
+                        if (&fb->base == pipe->base.front)
+                                continue;
+
+                        fb->flipid = 0;
+                        pipe->base.back = &fb->base;
+                        break;
+                }
+        }
+}
+
+static int grdrm_crtc_commit_flip(grdrm_crtc *crtc, grdev_fb **slot) {
         struct drm_mode_crtc_page_flip page_flip = { .crtc_id = crtc->object.id };
+        grdrm_card *card = crtc->object.card;
+        grdrm_pipe *pipe = crtc->pipe;
+        grdrm_fb *fb = fb_from_base(*slot);
+        uint32_t cnt;
+        size_t i;
+        int r;
+
+        assert(crtc);
+        assert(slot);
+        assert(*slot);
+        assert(pipe);
+
+        if (!crtc->applied && !grdrm_modes_compatible(&crtc->kern.mode, &crtc->set.mode))
+                return 0;
+
+        cnt = ++pipe->counter ? : ++pipe->counter;
+        page_flip.fb_id = fb->id;
+        page_flip.flags = DRM_MODE_PAGE_FLIP_EVENT;
+        page_flip.user_data = grdrm_encode_vblank_data(crtc->object.id, cnt);
+
+        r = ioctl(card->fd, DRM_IOCTL_MODE_PAGE_FLIP, &page_flip);
+        if (r < 0) {
+                r = -errno;
+                /* Avoid excessive logging on EINVAL; it is currently not
+                 * possible to see whether cards support page-flipping, so
+                 * avoid logging on each frame. */
+                if (r != -EINVAL)
+                        log_debug("grdrm: %s: cannot schedule page-flip on crtc %" PRIu32 ": %m",
+                                  card->base.name, crtc->object.id);
+
+                if (grdrm_card_async(card, r))
+                        return r;
+
+                return 0;
+        }
+
+        if (!crtc->applied) {
+                log_debug("grdrm: %s: crtc %" PRIu32 " applied via page flip",
+                          card->base.name, crtc->object.id);
+                crtc->applied = true;
+        }
+
+        pipe->base.flipping = true;
+        pipe->base.flip = false;
+        pipe->counter = cnt;
+        fb->flipid = cnt;
+        *slot = NULL;
+
+        /* Raise fake FRAME event if it takes longer than 2
+         * frames to receive the pageflip event. We assume the
+         * queue ran over or some other error happened. */
+        grdev_pipe_schedule(&pipe->base, 2);
+
+        if (!pipe->base.back) {
+                for (i = 0; i < pipe->base.max_fbs; ++i) {
+                        if (!pipe->base.fbs[i])
+                                continue;
+
+                        fb = fb_from_base(pipe->base.fbs[i]);
+                        if (&fb->base == pipe->base.front)
+                                continue;
+                        if (fb->flipid)
+                                continue;
+
+                        pipe->base.back = &fb->base;
+                        break;
+                }
+        }
+
+        return 1;
+}
+
+static void grdrm_crtc_commit(grdrm_crtc *crtc) {
         struct drm_mode_crtc set_crtc = { .crtc_id = crtc->object.id };
         grdrm_card *card = crtc->object.card;
         grdrm_pipe *pipe;
         grdev_fb **slot;
-        grdrm_fb *fb;
-        uint32_t cnt;
-        size_t i;
         int r;
 
         assert(crtc);
@@ -1141,102 +1289,11 @@ static void grdrm_crtc_commit(grdrm_crtc *crtc) {
         if (!*slot)
                 return;
 
-        fb = fb_from_base(*slot);
-
-        if (crtc->applied || grdrm_modes_compatible(&crtc->kern.mode, &crtc->set.mode)) {
-                cnt = ++pipe->counter ? : ++pipe->counter;
-                page_flip.fb_id = fb->id;
-                page_flip.flags = DRM_MODE_PAGE_FLIP_EVENT;
-                page_flip.user_data = grdrm_encode_vblank_data(crtc->object.id, cnt);
-
-                r = ioctl(card->fd, DRM_IOCTL_MODE_PAGE_FLIP, &page_flip);
-                if (r < 0) {
-                        r = -errno;
-                        log_debug("grdrm: %s: cannot schedule page-flip on crtc %" PRIu32 ": %m",
-                                  card->base.name, crtc->object.id);
-
-                        if (grdrm_card_async(card, r))
-                                return;
-
-                        /* fall through to deep modeset */
-                } else {
-                        if (!crtc->applied) {
-                                log_debug("grdrm: %s: crtc %" PRIu32 " applied via page flip",
-                                          card->base.name, crtc->object.id);
-                                crtc->applied = true;
-                        }
-
-                        pipe->base.flipping = true;
-                        pipe->counter = cnt;
-                        fb->flipid = cnt;
-                        *slot = NULL;
-
-                        if (!pipe->base.back) {
-                                for (i = 0; i < pipe->base.max_fbs; ++i) {
-                                        if (!pipe->base.fbs[i])
-                                                continue;
-
-                                        fb = fb_from_base(pipe->base.fbs[i]);
-                                        if (&fb->base == pipe->base.front)
-                                                continue;
-                                        if (fb->flipid)
-                                                continue;
-
-                                        pipe->base.back = &fb->base;
-                                        break;
-                                }
-                        }
-                }
+        r = grdrm_crtc_commit_flip(crtc, slot);
+        if (r == 0) {
+                /* in case we couldn't page-flip, perform deep modeset */
+                grdrm_crtc_commit_deep(crtc, slot);
         }
-
-        if (!crtc->applied) {
-                set_crtc.set_connectors_ptr = PTR_TO_UINT64(crtc->set.connectors);
-                set_crtc.count_connectors = crtc->set.n_connectors;
-                set_crtc.fb_id = fb->id;
-                set_crtc.x = 0;
-                set_crtc.y = 0;
-                set_crtc.mode_valid = 1;
-                set_crtc.mode = crtc->set.mode;
-
-                r = ioctl(card->fd, DRM_IOCTL_MODE_SETCRTC, &set_crtc);
-                if (r < 0) {
-                        r = -errno;
-                        log_debug("grdrm: %s: cannot set crtc %" PRIu32 ": %m",
-                                  card->base.name, crtc->object.id);
-
-                        grdrm_card_async(card, r);
-                        return;
-                }
-
-                if (!crtc->applied) {
-                        log_debug("grdrm: %s: crtc %" PRIu32 " applied via deep modeset",
-                                  card->base.name, crtc->object.id);
-                        crtc->applied = true;
-                }
-
-                *slot = NULL;
-                pipe->base.front = &fb->base;
-                fb->flipid = 0;
-                ++pipe->counter;
-                pipe->base.flipping = false;
-
-                if (!pipe->base.back) {
-                        for (i = 0; i < pipe->base.max_fbs; ++i) {
-                                if (!pipe->base.fbs[i])
-                                        continue;
-
-                                fb = fb_from_base(pipe->base.fbs[i]);
-                                if (&fb->base == pipe->base.front)
-                                        continue;
-
-                                fb->flipid = 0;
-                                pipe->base.back = &fb->base;
-                                break;
-                        }
-                }
-        }
-
-        pipe->base.flip = false;
 }
 
 static void grdrm_crtc_restore(grdrm_crtc *crtc) {
@@ -1471,6 +1528,7 @@ static int grdrm_pipe_new(grdrm_pipe **out, grdrm_crtc *crtc, struct drm_mode_mo
         pipe->crtc = crtc;
         pipe->base.width = mode->hdisplay;
         pipe->base.height = mode->vdisplay;
+        pipe->base.vrefresh = mode->vrefresh ? : 25;
 
         grdrm_pipe_name(name, crtc);
         r = grdev_pipe_add(&pipe->base, name, n_fbs);
@@ -2096,8 +2154,13 @@ static void grdrm_card_hotplug(grdrm_card *card) {
         int r;
 
         assert(card);
-        assert(!card->ready);
 
+        if (!card->running)
+                return;
+
+        log_debug("grdrm: %s/%s: reconfigure card", card->base.session->name, card->base.name);
+
+        card->ready = false;
         r = grdrm_card_resync(card);
         if (r < 0) {
                 log_debug("grdrm: %s/%s: cannot re-sync card: %s",
@@ -2107,9 +2170,13 @@ static void grdrm_card_hotplug(grdrm_card *card) {
 
         grdev_session_pin(card->base.session);
 
-        grdrm_card_print(card);
+        /* debug statement to print card information */
+        if (0)
+                grdrm_card_print(card);
+
         grdrm_card_configure(card);
         card->ready = true;
+        card->hotplug = false;
 
         grdev_session_unpin(card->base.session);
 }
@@ -2317,6 +2384,7 @@ static int grdrm_card_open(grdrm_card *card, int dev_fd) {
 
         sd_event_source_set_enabled(card->fd_src, SD_EVENT_OFF);
 
+        card->hotplug = true;
         card->fd = fd;
         fd = -1;
 
@@ -2955,3 +3023,34 @@ int grdev_drm_card_new(grdev_card **out, grdev_session *session, struct udev_dev
 
         return session->managed ? managed_card_new(out, session, ud) : unmanaged_card_new(out, session, ud);
 }
+
+void grdev_drm_card_hotplug(grdev_card *basecard, struct udev_device *ud) {
+        const char *p, *action;
+        grdrm_card *card;
+        dev_t devnum;
+
+        assert(basecard);
+        assert(grdev_is_drm_card(basecard));
+        assert(ud);
+
+        card = grdrm_card_from_base(basecard);
+
+        action = udev_device_get_action(ud);
+        if (!action || streq(action, "add") || streq(action, "remove")) {
+                /* If we get add/remove events on DRM nodes without devnum, we
+                 * got hotplugged DRM objects so refresh the device. */
+                devnum = udev_device_get_devnum(ud);
+                if (devnum == 0) {
+                        card->hotplug = true;
+                        grdrm_card_hotplug(card);
+                }
+        } else if (streq_ptr(action, "change")) {
+                /* A change event with HOTPLUG=1 is sent whenever a connector
+                 * changed state. Refresh the device to update our state. */
+                p = udev_device_get_property_value(ud, "HOTPLUG");
+                if (streq_ptr(p, "1")) {
+                        card->hotplug = true;
+                        grdrm_card_hotplug(card);
+                }
+        }
+}