From patchwork Thu Nov 19 16:39:43 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 29041 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id A4FC8BDE17 for ; Thu, 19 Nov 2026 16:40:01 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 647C66A930; Thu, 19 Nov 2026 17:39:57 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="EfVwtqxN"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id DABDB6A922 for ; Thu, 19 Nov 2026 17:39:53 +0100 (CET) Received: from [192.168.1.7] (unknown [93.65.100.155]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 892B11F10; Thu, 19 Nov 2026 17:37:24 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1795106244; bh=dOWUB+yXzRftj3oIpSJg9OwBOmSl/b2KwGl8YSKLPj8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=EfVwtqxNJfK0Zs0zFE4vTQQLw4ldbVnJcTPEd464C9CZ8rc7KYwIzr2sqSey935G2 FJJwsHUA0OJddiWSoDmpJtdy8fG/kFsAysTcNN9PqAc8FNMZX1jBuDA2nNlqxQ0WYu NK7qwZzZi+e7cD9937ilb4Hc/rQXASt88ZFUW11A= From: Jacopo Mondi Date: Thu, 19 Nov 2026 17:39:43 +0100 Subject: [PATCH 1/6] libcamera: Move DeviceMatch::match() to MediaDevice MIME-Version: 1.0 Message-Id: <20261119-multicontext-prep-v1-1-f68ea7ef960f@ideasonboard.com> References: <20261119-multicontext-prep-v1-0-f68ea7ef960f@ideasonboard.com> In-Reply-To: <20261119-multicontext-prep-v1-0-f68ea7ef960f@ideasonboard.com> To: libcamera-devel@lists.libcamera.org Cc: Jacopo Mondi X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=8080; i=jacopo.mondi@ideasonboard.com; h=from:subject:message-id; bh=dOWUB+yXzRftj3oIpSJg9OwBOmSl/b2KwGl8YSKLPj8=; b=owEBbQKS/ZANAwAKAXI0Bo8WoVY8AcsmYgBq/yZYB91532Uh8aC66fYY8vfGgBAE1RT/O+yLY bNjD/WXiJuJAjMEAAEKAB0WIQS1xD1IgJogio9YOMByNAaPFqFWPAUCav8mWAAKCRByNAaPFqFW PAGmD/9ywBQNOLJC1D3lXk+HoZd/0SSEGKO13FeFgPkQD0mvtVjbqJ0zvWRR1Jx1mtuRZmtdu/N 43pR39StuJmeY09MX1jyZPcdjcw9WWL+oE4d2VegebMTmY5v0T91Lvt9eT1kMyTGgTF8X3CT4GI 2wPCD/jmEL3ReFSIMdwAU7CHcEJw9ubaB6ZpJqGkgPe2hEHBLzZYIg33P6eO07kjrLne6rhno0F VaI1e25YYznr1qSrTiJ1Mu7937flN3S0zrmHyOhiJLtJcj/7zUtaqHJd55zEYC8LbWUhZarlaxf djpNI72eOBCk/43brl+JBTi+deDX8uNR0lWrwJAGgaykrAY7fMGWHGcXdxrWApWtIBjE3o4ur/U ys12qooWRCW2rHBjx2dOsmIo06BBAIHw2UsjtJzJ2sYdfETixU1gcFFyjF2jcRiFNYo5+b9QGUl cUcp6IKgy+jxr4g3tBp0F1Shicmj9zU5im02vsPzFvG/ZyXEpgsMDBHWctAn4CWcJ8xMOHmOimz YPB6z+OhYsYImTOPzrQ5OtG+cW0Ct6cMEGNhF9eQitCWHK9ZR4TAPdslc/xGiZjLy6oA3u4V/ox QedjPZntIMNwJyA5p2bbxQGlfxxSBNZXwk3VscwwKw7fBYNmcddRqUW6IEAcXWxC5oNShya40tk oC8MU5f++cS23HQ== X-Developer-Key: i=jacopo.mondi@ideasonboard.com; a=openpgp; fpr=72392EDC88144A65C701EA9BA5826A2587AD026B X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" The DeviceMatch class offers a match() function that compares its search criteria with the MediaDevice driver name and all its entities names. To prepare to remove all users of the entities() function from the DeviceEnumerator and MediaDevice users, move the match() function to the MediaDevice class. Add accessors to the DeviceMatch class so that the MediaDevice can compare itself to the search patterns. While at it perform a little rename of the DeviceMatch class members to allow to have more expressive function names, to maintain the convention that accessors have the same name as the fields they return. Signed-off-by: Jacopo Mondi --- include/libcamera/internal/device_enumerator.h | 10 +-- include/libcamera/internal/media_device.h | 4 ++ src/libcamera/device_enumerator.cpp | 89 ++++++-------------------- src/libcamera/media_device.cpp | 69 ++++++++++++++++++++ 4 files changed, 100 insertions(+), 72 deletions(-) diff --git a/include/libcamera/internal/device_enumerator.h b/include/libcamera/internal/device_enumerator.h index 5e1aec9924c5..5d142c2e0b31 100644 --- a/include/libcamera/internal/device_enumerator.h +++ b/include/libcamera/internal/device_enumerator.h @@ -23,15 +23,17 @@ class DeviceMatch public: DeviceMatch(const std::string &driver); + const std::string &driver() const { return driver_; } + const std::vector &entitiesNames() const { return entitiesNames_; } + const std::vector &entitiesRegexs() const { return entitiesRegexs_; } + void add(const std::string &entity); void add(std::regex entity); - bool match(const MediaDevice *device) const; - private: std::string driver_; - std::vector entities_; - std::vector entityRegexs_; + std::vector entitiesNames_; + std::vector entitiesRegexs_; }; class DeviceEnumerator diff --git a/include/libcamera/internal/media_device.h b/include/libcamera/internal/media_device.h index 5c1f14b63823..c1405142f726 100644 --- a/include/libcamera/internal/media_device.h +++ b/include/libcamera/internal/media_device.h @@ -24,6 +24,8 @@ namespace libcamera { +class DeviceMatch; + class MediaDevice : protected Loggable { public: @@ -40,6 +42,8 @@ public: int populate(); bool isValid() const { return valid_; } + bool match(const DeviceMatch &device) const; + const std::string &driver() const { return driver_; } const std::string &deviceNode() const { return deviceNode_; } const std::string &model() const { return model_; } diff --git a/src/libcamera/device_enumerator.cpp b/src/libcamera/device_enumerator.cpp index 0ce65e89a67f..2b1056425f61 100644 --- a/src/libcamera/device_enumerator.cpp +++ b/src/libcamera/device_enumerator.cpp @@ -73,13 +73,31 @@ DeviceMatch::DeviceMatch(const std::string &driver) { } +/** + * \fn DeviceMatch::driver() + * \brief Retrieve the target media device driver name + * \return The media device driver name + */ + +/** + * \fn DeviceMatch::entitiesNames() + * \brief Retrieve the list of target media entities names + * \return A vector of media entities names + */ + +/** + * \fn DeviceMatch::entitiesRegexs() + * \brief Retrieve the list of target media entities search patterns + * \return A vector of media entities search patterns + */ + /** * \brief Add a media entity name to the search pattern * \param[in] entity The name of the entity in the media graph */ void DeviceMatch::add(const std::string &entity) { - entities_.push_back(entity); + entitiesNames_.push_back(entity); } /** @@ -88,72 +106,7 @@ void DeviceMatch::add(const std::string &entity) */ void DeviceMatch::add(std::regex entity) { - entityRegexs_.push_back(std::move(entity)); -} - -/** - * \brief Compare a search pattern with a media device - * \param[in] device The media device - * - * Matching is performed on the Linux device driver name and entity names from - * the media graph. A match is found if both the driver name matches and the - * media device contains all the entities listed in the search pattern. - * - * \return true if the media device matches the search pattern, false otherwise - */ -bool DeviceMatch::match(const MediaDevice *device) const -{ - if (driver_ != device->driver()) - return false; - - for (const std::string &name : entities_) { - bool found = false; - - for (const MediaEntity *entity : device->entities()) { - if (name == entity->name()) { - if (!entity->deviceNode().empty()) { - found = true; - break; - } else { - LOG(DeviceEnumerator, Debug) - << "Skip " << entity->name() - << ": no device node"; - } - } - } - - if (!found) - return false; - } - - for (const std::regex &nameRegex : entityRegexs_) { - bool found = false; - - for (const MediaEntity *entity : device->entities()) { - if (!std::regex_search(entity->name(), nameRegex)) - continue; - - if (found) { - LOG(DeviceEnumerator, Error) - << "Multiple entities match regex"; - return false; - } - - if (entity->deviceNode().empty()) { - LOG(DeviceEnumerator, Debug) - << "Skip " << entity->name() - << ": no device node"; - continue; - } - - found = true; - } - - if (!found) - return false; - } - - return true; + entitiesRegexs_.push_back(std::move(entity)); } /** @@ -351,7 +304,7 @@ std::shared_ptr DeviceEnumerator::search(const DeviceMatch &dm) if (media->busy()) continue; - if (dm.match(media.get())) { + if (media->match(dm)) { LOG(DeviceEnumerator, Debug) << "Successful match for media device \"" << media->driver() << "\""; diff --git a/src/libcamera/media_device.cpp b/src/libcamera/media_device.cpp index 3f974eaabd67..b50d7328e47c 100644 --- a/src/libcamera/media_device.cpp +++ b/src/libcamera/media_device.cpp @@ -20,6 +20,7 @@ #include #include +#include "libcamera/internal/device_enumerator.h" #include "libcamera/internal/v4l2_request.h" /** @@ -286,6 +287,74 @@ done: * \return true if the media graph is valid, false otherwise */ +/** + * \brief Compare a media device with a search pattern + * \param[in] device The media device search pattern + * + * Matching is performed on the Linux device driver name and entity names from + * the \a device search pattern. A match is found if both the driver name + * matches and the media device contains all the entities listed in the search + * pattern. + * + * \return True if the media device matches the search pattern, false otherwise + */ +bool MediaDevice::match(const DeviceMatch &device) const +{ + ASSERT(isValid()); + + if (driver_ != device.driver()) + return false; + + for (const std::string &name : device.entitiesNames()) { + bool found = false; + + for (const MediaEntity *entity : entities()) { + if (name == entity->name()) { + if (!entity->deviceNode().empty()) { + found = true; + break; + } else { + LOG(MediaDevice, Debug) + << "Skip " << entity->name() + << ": no device node"; + } + } + } + + if (!found) + return false; + } + + for (const std::regex &nameRegex : device.entitiesRegexs()) { + bool found = false; + + for (const MediaEntity *entity : entities()) { + if (!std::regex_search(entity->name(), nameRegex)) + continue; + + if (found) { + LOG(MediaDevice, Error) + << "Multiple entities match regex"; + return false; + } + + if (entity->deviceNode().empty()) { + LOG(MediaDevice, Debug) + << "Skip " << entity->name() + << ": no device node"; + continue; + } + + found = true; + } + + if (!found) + return false; + } + + return true; +} + /** * \fn MediaDevice::driver() * \brief Retrieve the media device driver name From patchwork Thu Nov 19 16:39:44 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 29042 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id D317DC3355 for ; Thu, 19 Nov 2026 16:40:03 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 14F5E6A939; Thu, 19 Nov 2026 17:39:58 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="F9bcqrDo"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 0D3D36A923 for ; Thu, 19 Nov 2026 17:39:54 +0100 (CET) Received: from [192.168.1.7] (unknown [93.65.100.155]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id CB4211F1A; Thu, 19 Nov 2026 17:37:24 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1795106244; bh=3Li6ZKxdCZbaoX6b0BxnfDcODmfXlkiGCK892e04vmQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=F9bcqrDox1hUDzC7nqyn6YQZb/gYhe2Khy2c1UD301GCwHr3Dy6QWGdnIeoJauVB3 ffnF4cRI0PoDjyZ7XKpGRdKMVuiXn4PW85KCgBM7XL7hO0eJ/CXR4qwPGWT6fqJzcI rfGhyafLisRFZT4IyPR+X9iTxWg4ctzpMHMfr1+Y= From: Jacopo Mondi Date: Thu, 19 Nov 2026 17:39:44 +0100 Subject: [PATCH 2/6] libcamera: media_device: Align the two loops in match() MIME-Version: 1.0 Message-Id: <20261119-multicontext-prep-v1-2-f68ea7ef960f@ideasonboard.com> References: <20261119-multicontext-prep-v1-0-f68ea7ef960f@ideasonboard.com> In-Reply-To: <20261119-multicontext-prep-v1-0-f68ea7ef960f@ideasonboard.com> To: libcamera-devel@lists.libcamera.org Cc: Jacopo Mondi X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2903; i=jacopo.mondi@ideasonboard.com; h=from:subject:message-id; bh=3Li6ZKxdCZbaoX6b0BxnfDcODmfXlkiGCK892e04vmQ=; b=owEBbQKS/ZANAwAKAXI0Bo8WoVY8AcsmYgBq/yZYNey6LD+1Dm8JOmSer1guPB4A7JalRO2jA MDY4YMUd7WJAjMEAAEKAB0WIQS1xD1IgJogio9YOMByNAaPFqFWPAUCav8mWAAKCRByNAaPFqFW PKW3D/9K45cj+wuBeRoJik3R1MK1+/NkzJpui+PpdHlCbKMbhPDDwAwNgfs4hC+hTnny6H523fD Up2FQTajmhjCxrJG7A3q1l3NYdSMw9KR1LrmZsvGXgQuw8lFWuAgPeN+oBIpo6O/rrLtQpn1YEz xI2qXrGR66vYtBzBGZb4fhoKwulM1nrQmDqwTeShxrQHWzjuCFW5RelxVLP4kJlUdFMWVvFoxRx d04dZV/mI8mgD+LFKICHTwSHLab3DdUnVpD0ILI5yUi5/sDkahu4A64mgbKl71SAr/9TOcuGszm sPKpmuxjj4acsEY8d9bvs8VaiM+kugWVDBIpzJC7yfvSRMdKwj5mGETMRRbMWecELKNMtz1BdXG PHk9DMvNOYSNUoqUSsNsPcXv1mX8HrER4MWIIS2aOQ5iarjjwBXAUfJTUtuuTCbMsI2r+84xmqj Taxnc+pJ48mdIOcpXiFO6y39dJox1JAcr7o0+cPX3O4hfcN2JkuZA9sqmAA+wDNRLq7R8uLRWex BBZStr9CmzIrsKNfXuvwxjwNiw3KaiL+L3cLreBLTTadVtjdmjPracW7UcOvvByE68GKrOml5rT afoJbtSZK216Csgc6yqwjrt32Db6tfQH1WTQ+fAFlk5t4sphVmqWY6oWaGEMmZsLU0EEQlpxLbZ RPPqvtqGFz+JOnw== X-Developer-Key: i=jacopo.mondi@ideasonboard.com; a=openpgp; fpr=72392EDC88144A65C701EA9BA5826A2587AD026B X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Align the implementations of the two search loops to be identical by using a lambda function. The only difference between the two existing implementation is that with string-based matching as soon as we find a match we can break from the loop, while regex-based matching needs to walk all entities even if a match is found to make sure no multiple matches happen. To support both behaviours pass to the lambda a boolean flag that allows to interrupt the loop as soon as a match is found. Signed-off-by: Jacopo Mondi --- src/libcamera/media_device.cpp | 57 +++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 29 deletions(-) diff --git a/src/libcamera/media_device.cpp b/src/libcamera/media_device.cpp index b50d7328e47c..a84bc9abc4e9 100644 --- a/src/libcamera/media_device.cpp +++ b/src/libcamera/media_device.cpp @@ -305,39 +305,14 @@ bool MediaDevice::match(const DeviceMatch &device) const if (driver_ != device.driver()) return false; - for (const std::string &name : device.entitiesNames()) { - bool found = false; - - for (const MediaEntity *entity : entities()) { - if (name == entity->name()) { - if (!entity->deviceNode().empty()) { - found = true; - break; - } else { - LOG(MediaDevice, Debug) - << "Skip " << entity->name() - << ": no device node"; - } - } - } - - if (!found) - return false; - } - - for (const std::regex &nameRegex : device.entitiesRegexs()) { + const auto searchEntities = [&](const auto &searchPattern, + const auto &searchFn, bool breakEarly) { bool found = false; for (const MediaEntity *entity : entities()) { - if (!std::regex_search(entity->name(), nameRegex)) + if (!searchFn(searchPattern, entity->name())) continue; - if (found) { - LOG(MediaDevice, Error) - << "Multiple entities match regex"; - return false; - } - if (entity->deviceNode().empty()) { LOG(MediaDevice, Debug) << "Skip " << entity->name() @@ -345,10 +320,34 @@ bool MediaDevice::match(const DeviceMatch &device) const continue; } + if (found) { + LOG(MediaDevice, Error) + << "Multiple entities match " + << entity->name(); + return false; + } + found = true; + if (breakEarly) + break; } - if (!found) + return found; + }; + + for (const std::string &name : device.entitiesNames()) { + if (!searchEntities(name, [](const std::string &s, + const std::string &e) { + return s == e; + }, true)) + return false; + } + + for (const std::regex &nameRegex : device.entitiesRegexs()) { + if (!searchEntities(nameRegex, [](const std::regex &r, + const std::string &e) { + return std::regex_search(e, r); + }, false)) return false; } From patchwork Thu Nov 19 16:39:45 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 29043 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 32159BDE17 for ; Thu, 19 Nov 2026 16:40:05 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 81FCF6A93F; Thu, 19 Nov 2026 17:39:59 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="Vkwjhyjj"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 47B6D6A924 for ; Thu, 19 Nov 2026 17:39:54 +0100 (CET) Received: from [192.168.1.7] (unknown [93.65.100.155]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 19F0F13D3; Thu, 19 Nov 2026 17:37:25 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1795106245; bh=jjwzQV4uahwqqKFit26MBl2N/6xPJ8FV+mRs56l41Yg=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=Vkwjhyjjav5XSYoGVQMperL4UOZw0K6RpnqzjGH8/LzB1BwH9fveZGuV4+02+DXaG mVsPw5cTtPXAYdu9z0X3Te3P3a0WlXo1Gwh8sn94SMdW3GdkU2eqvbB55ANJpKWuPJ 3HhzzpHxJv3msgVVhjKVpPwU5jYGoKee6LZBqP+M= From: Jacopo Mondi Date: Thu, 19 Nov 2026 17:39:45 +0100 Subject: [PATCH 3/6] libcamera: media_device: Store the media v2 topology MIME-Version: 1.0 Message-Id: <20261119-multicontext-prep-v1-3-f68ea7ef960f@ideasonboard.com> References: <20261119-multicontext-prep-v1-0-f68ea7ef960f@ideasonboard.com> In-Reply-To: <20261119-multicontext-prep-v1-0-f68ea7ef960f@ideasonboard.com> To: libcamera-devel@lists.libcamera.org Cc: Jacopo Mondi X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=5065; i=jacopo.mondi@ideasonboard.com; h=from:subject:message-id; bh=jjwzQV4uahwqqKFit26MBl2N/6xPJ8FV+mRs56l41Yg=; b=owEBbQKS/ZANAwAKAXI0Bo8WoVY8AcsmYgBq/yZY583/qi9dhswZ+pU0R42eoj7rFZ2uThtlx khNAH+JIjeJAjMEAAEKAB0WIQS1xD1IgJogio9YOMByNAaPFqFWPAUCav8mWAAKCRByNAaPFqFW PO7OD/9tL/mutwE8wB9WSW3vkFe5hZAHMFFYEWBjyJEfA3/CFsn9mvtijnCAT1sK6d6+i7Ftw3z yXxspQX/GflAWDC+4CWHmSubl4nXB59eVUyOuZW7d90sO44++e6GFDerPU/pcX/QewEb/cyaySd GGwmjU+cBOMxFoXEu4dsopB0H4ExNk+LfNiLryUC6S2q5EYEGg2ZPtb82ToBlAtYmqD5c0SmxPS QS9dMv5iwY5pXLsaMfRzm5eAQVik36u1qGK243k8b/ZW0izBl0P6JTNGdkZpcRkOzLK/Qk1ZgYV zdFwlmfScuSIdU0NlvNySGagEb83PkKQ1TiOKZsDPyh2DQAnXRnJRwpFIIzLV1fN/CaF9k/Gj94 g1Vqxovp4QRJRTtVgaGDHUGh9dxWGdyy8ysu3cXDms8dblfjeFirZZAoyxqazNYqFglnnelU4Vv LvhXiE0qdU69T2xAhT6N0BMMZsvMg9kABHiBYQECuxqDo+DyROU69iirwtkgkEXcigJoQZ/wEvX jYNkQPwliav2pYBK05ZWm+ER0DGi7bEJ5i5VZeCJjG1K3lI6b6fx2s7jHMg4KImXICOxOlGkYFh /6evD1RFhFkknIKmSr8Nx4eSAIftp+ARln/6zInxGsUp4sHgIVzJqniGNe92HabL6kVPTaQdUBV m++bwAAnv1To0jQ== X-Developer-Key: i=jacopo.mondi@ideasonboard.com; a=openpgp; fpr=72392EDC88144A65C701EA9BA5826A2587AD026B X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Create an helper class in MediaDevice that wraps the kernel type struct media_v2_topology and its backing memory instead of manually allocating it in MediaDevice::populate(). Make an instance of the new MediaV2Topology a class member. This means the kernel types and their backing memory remain valid after MediaDevice::populate() returns. This will be useful to delay the creation of MediaObject to a later point in time, and have the MediaDevice class users that need to only enumerate entities to operate using the raw kernel structures. In the long run, this will allow us to split the MediaDevice class into a static version and one populated version where MediaObjects are accessible from. Signed-off-by: Jacopo Mondi --- include/libcamera/internal/media_device.h | 40 ++++++++++++++++++++++++++++++ src/libcamera/media_device.cpp | 41 +++++++------------------------ 2 files changed, 49 insertions(+), 32 deletions(-) diff --git a/include/libcamera/internal/media_device.h b/include/libcamera/internal/media_device.h index c1405142f726..8bab60343aad 100644 --- a/include/libcamera/internal/media_device.h +++ b/include/libcamera/internal/media_device.h @@ -97,6 +97,46 @@ private: unsigned int version_; unsigned int hwRevision_; + class MediaV2Topology + { + public: + uint64_t version() const { return topology_.topology_version; } + + void allocate() + { + if (!topology_.num_entities) + return; + + clear(); + + ents_ = new struct media_v2_entity[topology_.num_entities](); + interfaces_ = new struct media_v2_interface[topology_.num_interfaces](); + links_ = new struct media_v2_link[topology_.num_links](); + pads_ = new struct media_v2_pad[topology_.num_pads](); + + topology_.ptr_entities = reinterpret_cast(ents_); + topology_.ptr_interfaces = reinterpret_cast(interfaces_); + topology_.ptr_links = reinterpret_cast(links_); + topology_.ptr_pads = reinterpret_cast(pads_); + } + + void clear() + { + delete[] ents_; + delete[] interfaces_; + delete[] pads_; + delete[] links_; + } + + struct media_v2_topology topology_ = {}; + + private: + struct media_v2_entity *ents_ = nullptr; + struct media_v2_interface *interfaces_ = nullptr; + struct media_v2_link *links_ = nullptr; + struct media_v2_pad *pads_ = nullptr; + } mediaV2Topology_; + UniqueFD fd_; bool valid_; bool acquired_; diff --git a/src/libcamera/media_device.cpp b/src/libcamera/media_device.cpp index a84bc9abc4e9..f9562aa18b80 100644 --- a/src/libcamera/media_device.cpp +++ b/src/libcamera/media_device.cpp @@ -195,11 +195,6 @@ void MediaDevice::unlock() */ int MediaDevice::populate() { - struct media_v2_topology topology = {}; - struct media_v2_entity *ents = nullptr; - struct media_v2_interface *interfaces = nullptr; - struct media_v2_link *links = nullptr; - struct media_v2_pad *pads = nullptr; __u64 version = -1; int ret; @@ -227,13 +222,7 @@ int MediaDevice::populate() * Keep calling G_TOPOLOGY until the version number stays stable. */ while (true) { - topology.topology_version = 0; - topology.ptr_entities = reinterpret_cast(ents); - topology.ptr_interfaces = reinterpret_cast(interfaces); - topology.ptr_links = reinterpret_cast(links); - topology.ptr_pads = reinterpret_cast(pads); - - ret = ioctl(fd_.get(), MEDIA_IOC_G_TOPOLOGY, &topology); + ret = ioctl(fd_.get(), MEDIA_IOC_G_TOPOLOGY, &mediaV2Topology_.topology_); if (ret < 0) { ret = -errno; LOG(MediaDevice, Error) @@ -242,37 +231,23 @@ int MediaDevice::populate() goto done; } - if (version == topology.topology_version) + if (version == mediaV2Topology_.version()) break; - delete[] ents; - delete[] interfaces; - delete[] pads; - delete[] links; - - ents = new struct media_v2_entity[topology.num_entities](); - interfaces = new struct media_v2_interface[topology.num_interfaces](); - links = new struct media_v2_link[topology.num_links](); - pads = new struct media_v2_pad[topology.num_pads](); - - version = topology.topology_version; + mediaV2Topology_.allocate(); + version = mediaV2Topology_.version(); } /* Populate entities, pads and links. */ - if (populateEntities(topology) && - populatePads(topology) && - populateLinks(topology)) + if (populateEntities(mediaV2Topology_.topology_) && + populatePads(mediaV2Topology_.topology_) && + populateLinks(mediaV2Topology_.topology_)) valid_ = true; ret = 0; done: close(); - delete[] ents; - delete[] interfaces; - delete[] pads; - delete[] links; - if (!valid_) { clear(); return -EINVAL; @@ -657,6 +632,8 @@ bool MediaDevice::addObject(MediaObject *object) */ void MediaDevice::clear() { + mediaV2Topology_.clear(); + for (const auto &o : objects_) delete o.second; From patchwork Thu Nov 19 16:39:46 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 29044 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id EBC4AC3355 for ; Thu, 19 Nov 2026 16:40:05 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 0A2D56A942; Thu, 19 Nov 2026 17:40:01 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="scNB+cnC"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 940436A861 for ; Thu, 19 Nov 2026 17:39:54 +0100 (CET) Received: from [192.168.1.7] (unknown [93.65.100.155]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 61B8A1F10; Thu, 19 Nov 2026 17:37:25 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1795106245; bh=PBHO3vFG4Ws1W1Tu0OCotZ37SqpKxRPTgwZHHe8mUdw=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=scNB+cnCEE0LTZdnLNdcbVrKt2h52SdcITLrK1gyO0mra4p1Pxb3luArp3hZ9wRJC +gOjEV79uCzM6cS6yLQ6dEUSKKAcizgwIoAVvY+dCLArcesMedf1Qkms3BiGuvcpjC z8nungXxvGkjA5Ubu3bT2oLI3Zdi3Zn0k7huBsGA= From: Jacopo Mondi Date: Thu, 19 Nov 2026 17:39:46 +0100 Subject: [PATCH 4/6] libcamera: media_device: Store entities ids by devnum MIME-Version: 1.0 Message-Id: <20261119-multicontext-prep-v1-4-f68ea7ef960f@ideasonboard.com> References: <20261119-multicontext-prep-v1-0-f68ea7ef960f@ideasonboard.com> In-Reply-To: <20261119-multicontext-prep-v1-0-f68ea7ef960f@ideasonboard.com> To: libcamera-devel@lists.libcamera.org Cc: Jacopo Mondi X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=4393; i=jacopo.mondi@ideasonboard.com; h=from:subject:message-id; bh=PBHO3vFG4Ws1W1Tu0OCotZ37SqpKxRPTgwZHHe8mUdw=; b=owEBbQKS/ZANAwAKAXI0Bo8WoVY8AcsmYgBq/yZYM/zzmb+s/Alj1pMNmkXH1kYgoqqf/YSaa Awa7Xq3t5mJAjMEAAEKAB0WIQS1xD1IgJogio9YOMByNAaPFqFWPAUCav8mWAAKCRByNAaPFqFW PO2xD/4ye7IK+S5rf0xd2DDgxgMrKysqiCPRkEAz+N/UvRAi6DN2WxDjIxZFzpG87S0JIL1Qgi4 Oab32ncQlGO+6bcPsnoxzJ4r0DqXeSq5sOazUcclWyEpF1/5V36dD3n2/0RbgJnQzvoDJPSdrQR ISH7ekqU9zwlSQnRO7qaFXjMA76rw6xlVDlt5I72TVRe9/aSAtUX16xDrGkd209HNlzxWai5VRJ /4ThKqeq4w96yPRfur1ZcNoutHuXfpd5ZJO66HScxeVUq3742VbHNYyeFxNcxhXKAy77JEyzGcg gCceE6TZNism0FkXGIW2oTbLZLCp8KHE8W0qQWW79PYYQ7x3hIhomhjoDdHbdcOFaepufnzewi3 9ymziqdOhZ2nky150e+WKDnImcPcml7Wejatze2XIHP5fVU9nCevnptAu0IUbAI2efnz0MeL2Af AJPnj7nFTtL4epljmeF7eMlUr1M8nhb7JWZD3LMWASbNTpf8Wnx+1EuhGom+Ifu3GveV4l/SPjX C+cKLUIC2HevRKy/MHonVj+7iMQ/tcxl/qYf9mCgLFcAx8Bng09STpcGrR2CaXNB89c8pE7/ul+ u9sDAc9zrnXXjc72uj4Snbil2wUoR+laF2s1F5w0fSxhYNXB4vg/QcCX31a1AnTuskaCG2F/9HU CQZldGzqnx87ssg== X-Developer-Key: i=jacopo.mondi@ideasonboard.com; a=openpgp; fpr=72392EDC88144A65C701EA9BA5826A2587AD026B X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" After the media topology has been queried and deemed to be stable, create a map of device numbers to entity ids. Multiple entities can have the same devnum as they refer to the same devnode, hence we map the devnum to a list of entities ids. Add two accessors to the MediaDevice class to: - Get all the devnums - Get the list of entities ids given a devnum These helpers will be used to de-couple the device enumerators from the MediaObject class hierarchy, specifically the devnode path injection that requires to know the entities' devnums. Signed-off-by: Jacopo Mondi --- include/libcamera/internal/media_device.h | 21 +++++++++++++++ src/libcamera/media_device.cpp | 44 +++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/include/libcamera/internal/media_device.h b/include/libcamera/internal/media_device.h index 8bab60343aad..6fd84de033b6 100644 --- a/include/libcamera/internal/media_device.h +++ b/include/libcamera/internal/media_device.h @@ -9,7 +9,9 @@ #include #include +#include #include +#include #include #include @@ -50,6 +52,17 @@ public: unsigned int version() const { return version_; } unsigned int hwRevision() const { return hwRevision_; } + const std::vector entitiesDevnums() const + { + return utils::map_keys(devnumsMap_); + } + + const std::list &entitiesIdsByDevNum(dev_t devnum) const + { + ASSERT(devnumsMap_.contains(devnum)); + return devnumsMap_.at(devnum); + } + const std::vector &entities() const { return entities_; } MediaEntity *getEntityByName(const std::string &name) const; MediaEntity *getEntityByName(const std::regex &name) const; @@ -128,6 +141,12 @@ private: delete[] links_; } + std::span entities() const + { + return { reinterpret_cast + (topology_.ptr_entities), topology_.num_entities }; + } + struct media_v2_topology topology_ = {}; private: @@ -137,6 +156,8 @@ private: struct media_v2_pad *pads_ = nullptr; } mediaV2Topology_; + std::map> devnumsMap_; + UniqueFD fd_; bool valid_; bool acquired_; diff --git a/src/libcamera/media_device.cpp b/src/libcamera/media_device.cpp index f9562aa18b80..6dd4bdcc49c7 100644 --- a/src/libcamera/media_device.cpp +++ b/src/libcamera/media_device.cpp @@ -238,6 +238,28 @@ int MediaDevice::populate() version = mediaV2Topology_.version(); } + /* + * Create a map of devnums to entity ids. It will be used to inject the + * entity devnode with the support of the device enumerators. + */ + for (const auto &ent : mediaV2Topology_.entities()) { + struct media_v2_interface *iface = + findInterface(mediaV2Topology_.topology_, ent.id); + if (!iface) + continue; + + dev_t devnum = makedev(iface->devnode.major, iface->devnode.minor); + + /* Multiple entities can refer to the same devnum. */ + auto it = devnumsMap_.find(devnum); + if (it == devnumsMap_.end()) { + devnumsMap_[devnum].push_back(ent.id); + continue; + } + + it->second.push_back(ent.id); + } + /* Populate entities, pads and links. */ if (populateEntities(mediaV2Topology_.topology_) && populatePads(mediaV2Topology_.topology_) && @@ -365,6 +387,28 @@ bool MediaDevice::match(const DeviceMatch &device) const * \return The MediaDevice hardware revision */ +/** + * \fn MediaDevice::entitiesDevnums() + * \brief Return the devnums for all entities in the MediaDevice + * + * devnums are created for each media entity which register a device node using + * mkdev(major, minor). + * + * \return A vector of devnums + */ + +/** + * \fn MediaDevice::entitiesIdsByDevNum(dev_t devnum) + * \brief Return a list of entities ids associated with \a devnum + * \param[in] devnum The device numer + * + * Return a list of media entities ids whose device node is associated with + * \a devnum. As multiple media entites can refer to the same device node + * return a list. + * + * \return A list of media entities ids + */ + /** * \fn MediaDevice::entities() * \brief Retrieve the list of entities in the media graph From patchwork Thu Nov 19 16:39:47 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 29045 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id A84ECC3361 for ; Thu, 19 Nov 2026 16:40:06 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 692946A92F; Thu, 19 Nov 2026 17:40:02 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="wkN2qerK"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id D757F6A928 for ; Thu, 19 Nov 2026 17:39:54 +0100 (CET) Received: from [192.168.1.7] (unknown [93.65.100.155]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id A645113D3; Thu, 19 Nov 2026 17:37:25 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1795106245; bh=CLpBa9ETYoiNILn2p7xAtPG6Vwz2eJoYXSQUTl5zlAc=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=wkN2qerK+uC1fvfqBLynoa6nfD0mdhMnh7UQZJGHcSAEP06jd3rz48oCKFtfaEUx5 kF1qwzq/nQJhKGqwxCjeQYoT3tPZo6m82z7KoSfHMKzd0UJuol8YssfQElTqMjQSi4 ZB3gMUxnC2Ww9avdJKIWWydxJGpaLyqDpwjPll+Q= From: Jacopo Mondi Date: Thu, 19 Nov 2026 17:39:47 +0100 Subject: [PATCH 5/6] libcamera: device_enumerator: Use devnum to identify entities MIME-Version: 1.0 Message-Id: <20261119-multicontext-prep-v1-5-f68ea7ef960f@ideasonboard.com> References: <20261119-multicontext-prep-v1-0-f68ea7ef960f@ideasonboard.com> In-Reply-To: <20261119-multicontext-prep-v1-0-f68ea7ef960f@ideasonboard.com> To: libcamera-devel@lists.libcamera.org Cc: Jacopo Mondi X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=10060; i=jacopo.mondi@ideasonboard.com; h=from:subject:message-id; bh=CLpBa9ETYoiNILn2p7xAtPG6Vwz2eJoYXSQUTl5zlAc=; b=owEBbQKS/ZANAwAKAXI0Bo8WoVY8AcsmYgBq/yZYCrTXTE03GVIrIr4/4kQ41xej9dIRTexhR hjU1tn+s7uJAjMEAAEKAB0WIQS1xD1IgJogio9YOMByNAaPFqFWPAUCav8mWAAKCRByNAaPFqFW PIbWEACVL2lSCjGmjE+haR0qRvAIvkJ7fOGcXvdgTm93MEK3zhR+fuvmECWEvsmHNuOhiyQ6d7t dvJajXn3h0AE8c58c6mUYSY3Yr1MIkL6uebo52Zpkrxra1GJY2A0BhszEqWnK/tlR8KKAXSUVVg 38N6+bwKibKPY3h94muFvu2QByo7j9N1MkzZ6JW/+/DNsvyVhjDlv9Ui7kSymFc8qTPvnOEqvTF BosOPuOaz5YZsCKvd3l65LTIngT1+UapR/0J4c/VNKUYM7TEQWMDB5jq3jw7MJ5f9oudlz8Lgxl QgS0z/H8E3oQO9S8Y9uwleazF9LcZBew8faBX/Ieo/2N6uGM57Hppui6vG4QE34pu5+nSz2F+mg zGUcLBjn+fH+szq+jQiDTPLLyOAJO00dlhPhFiZWeicVfQ9lnjoOlGy7VcF6CoSa1sfZBZdNzef nabC6tdLTAsTrVwbR3uhRhda6t3rI/DU1DPbPVqViQj44yL6j7hPAKBD+FlzrhPkeTJpHAqs28F XJCG2E7L0hyRaVzfesC4VwZzWJRs+mXxEd2L0G4USiAeUArKf/H/bYtK0BwsdcNWVV/pR/eVifX aeXrlIg0NZfBjfzrq88YzHR6ONWUWKA8RLy880hAPPWlXKiv6yQeIddrN+XmzeoyUpOOugj9x3q mjhqdeW1rpyowNA== X-Developer-Key: i=jacopo.mondi@ideasonboard.com; a=openpgp; fpr=72392EDC88144A65C701EA9BA5826A2587AD026B X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" The DeviceEnumeratorUdev and DeviceEnumeratorSysfs populates the media graph in two steps. Initially, at DeviceEnumerator::createDevice() time, the media device is created and its media topology enumerated in MediaDevice::populate(). Then the device node path is injected in the corresponding MediaEntity. To progress with the de-coupling of the DeviceEnumerator hierarchy from the MediaObject class, remove all usages of MediaEntity from the DeviceEnumerator and replace it with entities ids and the devnum-based interface of the MediaDevice class. The MediaDevice collects a list of media entities ids for each devnum it identifies during topology enumeration. The device enumerator uses those devnums to calculate the device node paths and passes them to the MediaDevice which stores them in a map of entity ids to devnode paths. When the MediaObjects are created later on the devnode paths are fetched from the map and injected in the MediaEntity. This change requires to delay the creation of MediaObjects after the enumerator has populated the devnode paths so that a valid path is available for each MediaEntity. Split the MediaObject creation to a dedicated function MediaDevice::createTopology() and call it in DeviceEnumerator just before adding the new MediaDevice. This nicely fit with the design goal of delaying MediaObject creation to a later point in time in the next patches, precisely when the MediaDevice is acquired. Signed-off-by: Jacopo Mondi --- .../libcamera/internal/device_enumerator_udev.h | 3 +- include/libcamera/internal/media_device.h | 8 ++++ src/libcamera/device_enumerator.cpp | 2 + src/libcamera/device_enumerator_sysfs.cpp | 15 ++++--- src/libcamera/device_enumerator_udev.cpp | 29 ++++++-------- src/libcamera/media_device.cpp | 46 +++++++++++++++++++--- 6 files changed, 71 insertions(+), 32 deletions(-) diff --git a/include/libcamera/internal/device_enumerator_udev.h b/include/libcamera/internal/device_enumerator_udev.h index 1ce988ab84d1..a7b2047d7d7a 100644 --- a/include/libcamera/internal/device_enumerator_udev.h +++ b/include/libcamera/internal/device_enumerator_udev.h @@ -27,7 +27,6 @@ namespace libcamera { class EventNotifier; class MediaDevice; -class MediaEntity; class DeviceEnumeratorUdev final : public DeviceEnumerator { @@ -39,7 +38,7 @@ public: int enumerate(); private: - using DependencyMap = std::map>; + using DependencyMap = std::map>; struct MediaDeviceDeps { MediaDeviceDeps(std::unique_ptr media, diff --git a/include/libcamera/internal/media_device.h b/include/libcamera/internal/media_device.h index 6fd84de033b6..0e064e3fbcde 100644 --- a/include/libcamera/internal/media_device.h +++ b/include/libcamera/internal/media_device.h @@ -42,6 +42,7 @@ public: void unlock(); int populate(); + int createTopology(); bool isValid() const { return valid_; } bool match(const DeviceMatch &device) const; @@ -63,6 +64,12 @@ public: return devnumsMap_.at(devnum); } + void setEntityDeviceNode(unsigned int id, const std::string &devnode) + { + ASSERT(!devnodesMap_.contains(id)); + devnodesMap_[id] = devnode; + } + const std::vector &entities() const { return entities_; } MediaEntity *getEntityByName(const std::string &name) const; MediaEntity *getEntityByName(const std::regex &name) const; @@ -157,6 +164,7 @@ private: } mediaV2Topology_; std::map> devnumsMap_; + std::map devnodesMap_; UniqueFD fd_; bool valid_; diff --git a/src/libcamera/device_enumerator.cpp b/src/libcamera/device_enumerator.cpp index 2b1056425f61..ded3b41b5f26 100644 --- a/src/libcamera/device_enumerator.cpp +++ b/src/libcamera/device_enumerator.cpp @@ -248,6 +248,8 @@ void DeviceEnumerator::addDevice(std::unique_ptr media) LOG(DeviceEnumerator, Debug) << "Added device " << media->deviceNode() << ": " << media->driver(); + media->createTopology(); + devices_.push_back(std::move(media)); /* \todo To batch multiple additions, emit with a small delay here. */ diff --git a/src/libcamera/device_enumerator_sysfs.cpp b/src/libcamera/device_enumerator_sysfs.cpp index 7866885c0f73..f5248dd72315 100644 --- a/src/libcamera/device_enumerator_sysfs.cpp +++ b/src/libcamera/device_enumerator_sysfs.cpp @@ -96,18 +96,17 @@ int DeviceEnumeratorSysfs::enumerate() int DeviceEnumeratorSysfs::populateMediaDevice(MediaDevice *media) { /* Associate entities to device node paths. */ - for (MediaEntity *entity : media->entities()) { - if (entity->deviceMajor() == 0 && entity->deviceMinor() == 0) - continue; + for (dev_t devnum : media->entitiesDevnums()) { + unsigned int major = major(devnum); + unsigned int minor = minor(devnum); - std::string deviceNode = lookupDeviceNode(entity->deviceMajor(), - entity->deviceMinor()); + std::string deviceNode = lookupDeviceNode(major, minor); if (deviceNode.empty()) return -EINVAL; - int ret = entity->setDeviceNode(deviceNode); - if (ret) - return ret; + const std::list &entitiesIds = media->entitiesIdsByDevNum(devnum); + for (unsigned int id : entitiesIds) + media->setEntityDeviceNode(id, deviceNode); } return 0; diff --git a/src/libcamera/device_enumerator_udev.cpp b/src/libcamera/device_enumerator_udev.cpp index 8b9376ca6ac4..dbd0668e5b3b 100644 --- a/src/libcamera/device_enumerator_udev.cpp +++ b/src/libcamera/device_enumerator_udev.cpp @@ -221,19 +221,20 @@ int DeviceEnumeratorUdev::populateMediaDevice(MediaDevice *media, DependencyMap std::set children; /* Associate entities to device node paths. */ - for (MediaEntity *entity : media->entities()) { - if (entity->deviceMajor() == 0 && entity->deviceMinor() == 0) - continue; + for (dev_t devnum : media->entitiesDevnums()) { - dev_t devnum = makedev(entity->deviceMajor(), - entity->deviceMinor()); + const std::list &entitiesIds = + media->entitiesIdsByDevNum(devnum); /* - * If the devnum isn't in the orphans list, add it to the unmet - * dependencies. + * If the devnum isn't in the orphans list, add all the entities + * ids to the list of dependencies. */ if (orphans_.find(devnum) == orphans_.end()) { - (*deps)[devnum].push_back(entity); + auto &it = (*deps)[devnum]; + for (unsigned int id : entitiesIds) + it.push_back(id); + continue; } @@ -246,9 +247,8 @@ int DeviceEnumeratorUdev::populateMediaDevice(MediaDevice *media, DependencyMap if (deviceNode.empty()) return -EINVAL; - int ret = entity->setDeviceNode(deviceNode); - if (ret) - return ret; + for (unsigned int id : entitiesIds) + media->setEntityDeviceNode(id, deviceNode); children.insert(devnum); } @@ -326,11 +326,8 @@ int DeviceEnumeratorUdev::addV4L2Device(dev_t devnum) return -EINVAL; MediaDeviceDeps *deps = it->second; - for (MediaEntity *entity : deps->deps_[devnum]) { - int ret = entity->setDeviceNode(deviceNode); - if (ret) - return ret; - } + for (unsigned int id : deps->deps_[devnum]) + deps->media_->setEntityDeviceNode(id, deviceNode); /* * Remove the devnum from the unmet dependencies for this media device. diff --git a/src/libcamera/media_device.cpp b/src/libcamera/media_device.cpp index 6dd4bdcc49c7..0d5c3a18b471 100644 --- a/src/libcamera/media_device.cpp +++ b/src/libcamera/media_device.cpp @@ -260,12 +260,7 @@ int MediaDevice::populate() it->second.push_back(ent.id); } - /* Populate entities, pads and links. */ - if (populateEntities(mediaV2Topology_.topology_) && - populatePads(mediaV2Topology_.topology_) && - populateLinks(mediaV2Topology_.topology_)) - valid_ = true; - + valid_ = true; ret = 0; done: close(); @@ -278,6 +273,27 @@ done: return ret; } +/** + * \brief Create MediaObjects from a populated topology + * + * Populate the MediaObjects list and create MediaEntities, MediaPads and + * MediaLinks by parsing the topology populated at MediaDevice::populate() + * time. + * + * Requires the topology to be valid. + * + * \todo This function will be move to a new MediaContext type which will + * be created at MediaDevice::acquire() time. + */ +int MediaDevice::createTopology() +{ + ASSERT(valid_); + + return populateEntities(mediaV2Topology_.topology_) && + populatePads(mediaV2Topology_.topology_) && + populateLinks(mediaV2Topology_.topology_); +} + /** * \fn MediaDevice::isValid() * \brief Query whether the media graph has been populated and is valid @@ -409,6 +425,16 @@ bool MediaDevice::match(const DeviceMatch &device) const * \return A list of media entities ids */ +/** + * \fn MediaDevice::setEntityDeviceNode(unsigned in id, const std::string &devnode) + * \brief Associate the device node path \a devnode to the entity \a id + * \param[in] id The media entity id + * \param[in] devnode The device node path + * + * Associate to the media entity identified by \a id the path of its device + * node \a devnode. + */ + /** * \fn MediaDevice::entities() * \brief Retrieve the list of entities in the media graph @@ -757,6 +783,14 @@ bool MediaDevice::populateEntities(const struct media_v2_topology &topology) findInterface(topology, ent->id); MediaEntity *entity = new MediaEntity(this, ent, iface); + /* Inject the devnode path for entities with a devnum. */ + auto it = devnodesMap_.find(entity->id()); + if (it != devnodesMap_.end()) { + int ret = entity->setDeviceNode(it->second); + if (ret) + return false; + } + if (!addObject(entity)) { delete entity; return false; From patchwork Thu Nov 19 16:39:48 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 29046 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 6A81BBDE17 for ; Thu, 19 Nov 2026 16:40:07 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 093756A931; Thu, 19 Nov 2026 17:40:03 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="WsfAFuYF"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 25AA26A92A for ; Thu, 19 Nov 2026 17:39:55 +0100 (CET) Received: from [192.168.1.7] (unknown [93.65.100.155]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id ECFAB1F10; Thu, 19 Nov 2026 17:37:25 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1795106246; bh=rhub5p8Xchm7d7XbW+A0OKJFJuH203E8BzGGiYgH4eM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=WsfAFuYFyQ3M/0lkAinHeEH0QkjtuGZCIyIQ4MHA/JQAg1vJIaKANAHikAWy6s8fp +qOvFSaAnFuPFpM5IPMUs7AXmyNYyeeEpqoTX8zPDFfYPcM5wqNJKrn6EsUQQBjEq1 kcEbliVkGRaYZ0npYY5Y2/k2Kpi4/1kMHeb/erzE= From: Jacopo Mondi Date: Thu, 19 Nov 2026 17:39:48 +0100 Subject: [PATCH 6/6] libcamera: media_device: Delay topology creation at acquire() MIME-Version: 1.0 Message-Id: <20261119-multicontext-prep-v1-6-f68ea7ef960f@ideasonboard.com> References: <20261119-multicontext-prep-v1-0-f68ea7ef960f@ideasonboard.com> In-Reply-To: <20261119-multicontext-prep-v1-0-f68ea7ef960f@ideasonboard.com> To: libcamera-devel@lists.libcamera.org Cc: Jacopo Mondi X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=7603; i=jacopo.mondi@ideasonboard.com; h=from:subject:message-id; bh=rhub5p8Xchm7d7XbW+A0OKJFJuH203E8BzGGiYgH4eM=; b=owEBbQKS/ZANAwAKAXI0Bo8WoVY8AcsmYgBq/yZYLqIX5XyQl4l4kmFBDgtkizZmvz1j+gmhI 26bGlmScx6JAjMEAAEKAB0WIQS1xD1IgJogio9YOMByNAaPFqFWPAUCav8mWAAKCRByNAaPFqFW PHNnD/kBRkQvGRXrmdAg1XNYUF8PxpivIXvE3Q0wLGgdpmedYXPU85ylh+DMWDNmV3XbAdy5gGy D8xmuOqA0qi+nrWGD5OSQuAND1AhVkxUpkfwuCZYwhEmZGTP5Ex+7hyhCNfyzWBmhq4UMuxOgTR udhykqRivpSe8iCzJ0vlXMfeSod4lR81gPVNEofwveYHG+skUEYwqok0WaCeEWolzNLmjItmx7i FOyyn7g2htGC9O5n4iJdMHsvFFlG2eiTftBI9t51oxDSQVVyrKZLybNOGyJ6M20/jNNDjb6OJnW a9dqHjMW2ky/2jEcfCECj1zlf/0y7WPD9pA5WGJ4c85db8HIid2g8IPk02Sk6WNbAhDTeKVfLdw BU2vtauLCbBIdjFSuBUCYu1+tM4MGEGqRcRMst8XGYWufOTw45yaxj3Y3twVl/+CkDR9KCte8At jXKEFhFS3tw4C/xugsvDnEE/NQjZ7CIhiHZsCu3/4KRHxDzIIvSt/8wPNj5Py5F4L6a7exmJSmn Zujry6tWqKd3gwvxslvc0CtYnl7JBlaNUXeP1FSLnGV8oTpCkwd+89Td8sO5K5izwm78g+UVeJF cbRXlfLbRAcR8OxhPwlHGUOxcAf1ZUvCkBqWfyxS84MN68QSxl/EcyGWWNyJO7J7JehS41HT+B9 2JW/Wd3yrLWvsvw== X-Developer-Key: i=jacopo.mondi@ideasonboard.com; a=openpgp; fpr=72392EDC88144A65C701EA9BA5826A2587AD026B X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Now that we have removed all external dependencies on the MediaObject class hierarchy, we can delay its creation at MediaDevice::acquire() time. The only remaining user is MediaDevice::match() which runs before acquire(). As this is an internal user, modify it to use the raw kernel types to perform matching so that it can be run before MediaObjects are created from the raw topology. Add an enumerated_ flag to avoid double enumeration on acquire/release/acquire cycles, which is effectivelly a use case only for test units. The additional flag is not an ideal solution but will only stay until the introduction of the MediaContext class. Now that MediaObjects are only available after acquire(), update all tests that create V4L2Devices by entity name to aquire the media device first. Signed-off-by: Jacopo Mondi --- include/libcamera/internal/media_device.h | 1 + src/libcamera/device_enumerator.cpp | 2 -- src/libcamera/media_device.cpp | 20 ++++++++++++++------ test/camera-sensor.cpp | 8 ++++++++ test/delayed_controls.cpp | 12 ++++++++++++ test/libtest/buffer_source.cpp | 6 ++++++ test/v4l2_subdevice/v4l2_subdevice_test.cpp | 7 +++++++ test/v4l2_videodevice/v4l2_m2mdevice.cpp | 8 ++++++++ 8 files changed, 56 insertions(+), 8 deletions(-) diff --git a/include/libcamera/internal/media_device.h b/include/libcamera/internal/media_device.h index 0e064e3fbcde..72b387926ee9 100644 --- a/include/libcamera/internal/media_device.h +++ b/include/libcamera/internal/media_device.h @@ -169,6 +169,7 @@ private: UniqueFD fd_; bool valid_; bool acquired_; + bool enumerated_; std::optional supportsRequests_; std::map objects_; diff --git a/src/libcamera/device_enumerator.cpp b/src/libcamera/device_enumerator.cpp index ded3b41b5f26..2b1056425f61 100644 --- a/src/libcamera/device_enumerator.cpp +++ b/src/libcamera/device_enumerator.cpp @@ -248,8 +248,6 @@ void DeviceEnumerator::addDevice(std::unique_ptr media) LOG(DeviceEnumerator, Debug) << "Added device " << media->deviceNode() << ": " << media->driver(); - media->createTopology(); - devices_.push_back(std::move(media)); /* \todo To batch multiple additions, emit with a small delay here. */ diff --git a/src/libcamera/media_device.cpp b/src/libcamera/media_device.cpp index 0d5c3a18b471..ad38d7c4078e 100644 --- a/src/libcamera/media_device.cpp +++ b/src/libcamera/media_device.cpp @@ -66,7 +66,7 @@ LOG_DEFINE_CATEGORY(MediaDevice) * populate() before the media graph can be queried. */ MediaDevice::MediaDevice(const std::string &deviceNode) - : deviceNode_(deviceNode), valid_(false), acquired_(false) + : deviceNode_(deviceNode), valid_(false), acquired_(false), enumerated_(false) { } @@ -111,7 +111,13 @@ bool MediaDevice::acquire() if (open()) return false; + if (!enumerated_ && !createTopology()) { + close(); + return false; + } + acquired_ = true; + enumerated_ = true; return true; } @@ -322,13 +328,14 @@ bool MediaDevice::match(const DeviceMatch &device) const const auto &searchFn, bool breakEarly) { bool found = false; - for (const MediaEntity *entity : entities()) { - if (!searchFn(searchPattern, entity->name())) + for (const auto &ent : mediaV2Topology_.entities()) { + if (!searchFn(searchPattern, ent.name)) continue; - if (entity->deviceNode().empty()) { + const auto &it = devnodesMap_.find(ent.id); + if (it == devnodesMap_.end()) { LOG(MediaDevice, Debug) - << "Skip " << entity->name() + << "Skip " << ent.name << ": no device node"; continue; } @@ -336,7 +343,7 @@ bool MediaDevice::match(const DeviceMatch &device) const if (found) { LOG(MediaDevice, Error) << "Multiple entities match " - << entity->name(); + << ent.name; return false; } @@ -710,6 +717,7 @@ void MediaDevice::clear() objects_.clear(); entities_.clear(); valid_ = false; + enumerated_ = false; } /** diff --git a/test/camera-sensor.cpp b/test/camera-sensor.cpp index c30a2212e362..04f01d2a14c7 100644 --- a/test/camera-sensor.cpp +++ b/test/camera-sensor.cpp @@ -46,6 +46,12 @@ protected: return TestSkip; } + if (!media_->acquire()) { + cerr << "Unable to acquire media device " + << media_->driver() << endl; + return TestFail; + } + MediaEntity *entity = media_->getEntityByName("Sensor A"); if (!entity) { cerr << "Unable to find media entity 'Sensor A'" << endl; @@ -121,6 +127,8 @@ protected: void cleanup() { + if (media_) + media_->release(); } private: diff --git a/test/delayed_controls.cpp b/test/delayed_controls.cpp index 7bd30e7aead8..36bb522e43b3 100644 --- a/test/delayed_controls.cpp +++ b/test/delayed_controls.cpp @@ -47,6 +47,12 @@ protected: return TestSkip; } + if (!media_->acquire()) { + cerr << "Unable to acquire media device " + << media_->driver() << endl; + return TestFail; + } + dev_ = V4L2VideoDevice::fromEntityName(media_.get(), "vivid-000-vid-cap"); if (dev_->open()) { cerr << "Failed to open video device" << endl; @@ -70,6 +76,12 @@ protected: return TestPass; } + void cleanup() override + { + if (media_) + media_->release(); + } + int singleControlNoDelay() { std::unordered_map delays = { diff --git a/test/libtest/buffer_source.cpp b/test/libtest/buffer_source.cpp index 97c8ae1cbd31..756931918871 100644 --- a/test/libtest/buffer_source.cpp +++ b/test/libtest/buffer_source.cpp @@ -52,6 +52,12 @@ int BufferSource::allocate(const StreamConfiguration &config) return TestSkip; } + if (!media_->acquire()) { + std::cerr << "Unable to acquire media device " + << media_->driver() << std::endl; + return TestFail; + } + std::unique_ptr video = V4L2VideoDevice::fromEntityName(media_.get(), videoDeviceName); if (!video) { diff --git a/test/v4l2_subdevice/v4l2_subdevice_test.cpp b/test/v4l2_subdevice/v4l2_subdevice_test.cpp index c349c9e31c40..59f44a3bc124 100644 --- a/test/v4l2_subdevice/v4l2_subdevice_test.cpp +++ b/test/v4l2_subdevice/v4l2_subdevice_test.cpp @@ -46,6 +46,11 @@ int V4L2SubdeviceTest::init() return TestSkip; } + if (!media_->acquire()) { + cerr << "Unable to acquire media device " << media_->driver() << endl; + return TestFail; + } + MediaEntity *videoEntity = media_->getEntityByName("Scaler"); if (!videoEntity) { cerr << "Unable to find media entity 'Scaler'" << endl; @@ -64,5 +69,7 @@ int V4L2SubdeviceTest::init() void V4L2SubdeviceTest::cleanup() { + if (media_) + media_->release(); delete scaler_; } diff --git a/test/v4l2_videodevice/v4l2_m2mdevice.cpp b/test/v4l2_videodevice/v4l2_m2mdevice.cpp index c45f581a8653..90dc87366746 100644 --- a/test/v4l2_videodevice/v4l2_m2mdevice.cpp +++ b/test/v4l2_videodevice/v4l2_m2mdevice.cpp @@ -75,6 +75,12 @@ protected: return TestSkip; } + if (!media_->acquire()) { + cerr << "Unable to acquire media device " + << media_->driver() << endl; + return TestFail; + } + return TestPass; } @@ -193,6 +199,8 @@ protected: void cleanup() { + if (media_) + media_->release(); delete vim2m_; }