From owner-dev-commits-src-branches@freebsd.org Wed Sep 8 00:02:24 2021 Return-Path: Delivered-To: dev-commits-src-branches@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 95A1B670124; Wed, 8 Sep 2021 00:02:24 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4H42Pq2W8bz4WdX; Wed, 8 Sep 2021 00:02:23 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 3696A1BC5D; Wed, 8 Sep 2021 00:02:23 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 18802No1050238; Wed, 8 Sep 2021 00:02:23 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 18802NST050237; Wed, 8 Sep 2021 00:02:23 GMT (envelope-from git) Date: Wed, 8 Sep 2021 00:02:23 GMT Message-Id: <202109080002.18802NST050237@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Vladimir Kondratyev Subject: git: 5bf6cf0fbb2d - stable/13 - evdev: Use bitsets to track active touches and slots changed in current report MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: wulf X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 5bf6cf0fbb2d033b95f0b38ce2c46f07c68480ac Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-branches@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commits to the stable branches of the FreeBSD src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 08 Sep 2021 00:02:24 -0000 The branch stable/13 has been updated by wulf: URL: https://cgit.FreeBSD.org/src/commit/?id=5bf6cf0fbb2d033b95f0b38ce2c46f07c68480ac commit 5bf6cf0fbb2d033b95f0b38ce2c46f07c68480ac Author: Vladimir Kondratyev AuthorDate: 2021-08-24 22:44:36 +0000 Commit: Vladimir Kondratyev CommitDate: 2021-09-07 23:55:37 +0000 evdev: Use bitsets to track active touches and slots changed in current report Obtained from: OpenBSD (cherry picked from commit 2dc7188e532f0147b36a44ce5c033d9f59cbd9f4) --- sys/dev/evdev/evdev_mt.c | 94 ++++++++++++++++++++++-------------------------- 1 file changed, 43 insertions(+), 51 deletions(-) diff --git a/sys/dev/evdev/evdev_mt.c b/sys/dev/evdev/evdev_mt.c index e8f892e235c0..a28b034884ba 100644 --- a/sys/dev/evdev/evdev_mt.c +++ b/sys/dev/evdev/evdev_mt.c @@ -42,6 +42,13 @@ #define debugf(fmt, args...) #endif +typedef u_int slotset_t; + +_Static_assert(MAX_MT_SLOTS < sizeof(slotset_t) * 8, "MAX_MT_SLOTS too big"); + +#define FOREACHBIT(v, i) \ + for ((i) = ffs(v) - 1; (i) != -1; (i) = ffs((v) & (~1 << (i))) - 1) + static uint16_t evdev_mtstmap[][2] = { { ABS_MT_POSITION_X, ABS_X }, { ABS_MT_POSITION_Y, ABS_Y }, @@ -50,18 +57,27 @@ static uint16_t evdev_mtstmap[][2] = { }; struct evdev_mt_slot { - uint64_t ev_report; int32_t val[MT_CNT]; }; struct evdev_mt { int last_reported_slot; + /* the set of slots with active touches */ + slotset_t touches; + /* the set of slots with unsynchronized state */ + slotset_t frame; struct evdev_mt_slot slots[]; }; static void evdev_mt_send_st_compat(struct evdev_dev *); static void evdev_mt_send_autorel(struct evdev_dev *); +static inline int +ffc_slot(struct evdev_dev *evdev, slotset_t slots) +{ + return (ffs(~slots & (2U << MAXIMAL_MT_SLOT(evdev)) - 1) - 1); +} + void evdev_mt_init(struct evdev_dev *evdev) { @@ -73,17 +89,9 @@ evdev_mt_init(struct evdev_dev *evdev) sizeof(struct evdev_mt_slot) * slots, M_EVDEV, M_WAITOK | M_ZERO); /* Initialize multitouch protocol type B states */ - for (slot = 0; slot < slots; slot++) { - /* - * .ev_report should not be initialized to initial value of - * report counter (0) as it brokes free slot detection in - * evdev_get_mt_slot_by_tracking_id. So initialize it to -1 - */ - evdev->ev_mt->slots[slot] = (struct evdev_mt_slot) { - .ev_report = 0xFFFFFFFFFFFFFFFFULL, - .val[ABS_MT_INDEX(ABS_MT_TRACKING_ID)] = -1, - }; - } + for (slot = 0; slot < slots; slot++) + evdev->ev_mt->slots[slot].val[ABS_MT_INDEX(ABS_MT_TRACKING_ID)] + = -1; if (bit_test(evdev->ev_flags, EVDEV_FLAG_MT_STCOMPAT)) evdev_support_mt_compat(evdev); @@ -103,6 +111,7 @@ evdev_mt_sync_frame(struct evdev_dev *evdev) if (evdev->ev_report_opened && bit_test(evdev->ev_flags, EVDEV_FLAG_MT_STCOMPAT)) evdev_mt_send_st_compat(evdev); + evdev->ev_mt->frame = 0; } int @@ -118,7 +127,7 @@ evdev_mt_set_last_slot(struct evdev_dev *evdev, int slot) MPASS(slot >= 0 && slot <= MAXIMAL_MT_SLOT(evdev)); - mt->slots[slot].ev_report = evdev->ev_report_count; + mt->frame |= 1U << slot; mt->last_reported_slot = slot; } @@ -140,6 +149,12 @@ evdev_mt_set_value(struct evdev_dev *evdev, int slot, int16_t code, MPASS(slot >= 0 && slot <= MAXIMAL_MT_SLOT(evdev)); + if (code == ABS_MT_TRACKING_ID) { + if (value != -1) + mt->touches |= 1U << slot; + else + mt->touches &= ~(1U << slot); + } mt->slots[slot].val[ABS_MT_INDEX(code)] = value; } @@ -147,25 +162,17 @@ int evdev_get_mt_slot_by_tracking_id(struct evdev_dev *evdev, int32_t tracking_id) { struct evdev_mt *mt = evdev->ev_mt; - int32_t tr_id; - int slot, free_slot = -1; + int slot; - for (slot = 0; slot <= MAXIMAL_MT_SLOT(evdev); slot++) { - tr_id = evdev_mt_get_value(evdev, slot, ABS_MT_TRACKING_ID); - if (tr_id == tracking_id) + FOREACHBIT(mt->touches, slot) + if (evdev_mt_get_value(evdev, slot, ABS_MT_TRACKING_ID) == + tracking_id) return (slot); - /* - * Its possible that slot will be reassigned in a place of just - * released one within the same report. To avoid this compare - * report counter with slot`s report number updated with each - * ABS_MT_TRACKING_ID change. - */ - if (free_slot == -1 && tr_id == -1 && - mt->slots[slot].ev_report != evdev->ev_report_count) - free_slot = slot; - } - - return (free_slot); + /* + * Do not allow allocation of new slot in a place of just + * released one within the same report. + */ + return (ffc_slot(evdev, mt->touches | mt->frame)); } void @@ -194,29 +201,18 @@ evdev_support_mt_compat(struct evdev_dev *evdev) evdev->ev_absinfo[evdev_mtstmap[i][0]].resolution); } -static int32_t -evdev_count_fingers(struct evdev_dev *evdev) -{ - int nfingers = 0, i; - - for (i = 0; i <= MAXIMAL_MT_SLOT(evdev); i++) - if (evdev_mt_get_value(evdev, i, ABS_MT_TRACKING_ID) != -1) - nfingers++; - - return (nfingers); -} - static void evdev_mt_send_st_compat(struct evdev_dev *evdev) { + struct evdev_mt *mt = evdev->ev_mt; int nfingers, i; EVDEV_LOCK_ASSERT(evdev); - nfingers = evdev_count_fingers(evdev); + nfingers = bitcount(mt->touches); evdev_send_event(evdev, EV_KEY, BTN_TOUCH, nfingers > 0); - if (evdev_mt_get_value(evdev, 0, ABS_MT_TRACKING_ID) != -1) + if ((mt->touches & 1U << 0) != 0) /* Echo 0-th MT-slot as ST-slot */ for (i = 0; i < nitems(evdev_mtstmap); i++) if (bit_test(evdev->ev_abs_flags, evdev_mtstmap[i][1])) @@ -250,13 +246,9 @@ evdev_mt_send_autorel(struct evdev_dev *evdev) EVDEV_LOCK_ASSERT(evdev); - for (slot = 0; slot <= MAXIMAL_MT_SLOT(evdev); slot++) { - if (mt->slots[slot].ev_report != evdev->ev_report_count && - evdev_mt_get_value(evdev, slot, ABS_MT_TRACKING_ID) != -1){ - evdev_send_event(evdev, EV_ABS, ABS_MT_SLOT, slot); - evdev_send_event(evdev, EV_ABS, ABS_MT_TRACKING_ID, - -1); - } + FOREACHBIT(mt->touches & ~mt->frame, slot) { + evdev_send_event(evdev, EV_ABS, ABS_MT_SLOT, slot); + evdev_send_event(evdev, EV_ABS, ABS_MT_TRACKING_ID, -1); } }