Show a patch.

GET /api/patches/21739/?format=api
HTTP 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 21739,
    "url": "https://patchwork.libcamera.org/api/patches/21739/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/21739/",
    "project": {
        "id": 1,
        "url": "https://patchwork.libcamera.org/api/projects/1/?format=api",
        "name": "libcamera",
        "link_name": "libcamera",
        "list_id": "libcamera_core",
        "list_email": "libcamera-devel@lists.libcamera.org",
        "web_url": "",
        "scm_url": "",
        "webscm_url": ""
    },
    "msgid": "<20241022145321.25923-4-jacopo.mondi@ideasonboard.com>",
    "date": "2024-10-22T14:53:15",
    "name": "[3/3] libcamera: camera_sensor: Sort factories by priority",
    "commit_ref": null,
    "pull_url": null,
    "state": "accepted",
    "archived": false,
    "hash": "d6245f1484911fbe2395c77e1274866939444c1a",
    "submitter": {
        "id": 143,
        "url": "https://patchwork.libcamera.org/api/people/143/?format=api",
        "name": "Jacopo Mondi",
        "email": "jacopo.mondi@ideasonboard.com"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/21739/mbox/",
    "series": [
        {
            "id": 4736,
            "url": "https://patchwork.libcamera.org/api/series/4736/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=4736",
            "date": "2024-10-22T14:53:12",
            "name": "Introduce CameraSensorFactory",
            "version": 1,
            "mbox": "https://patchwork.libcamera.org/series/4736/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/21739/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/21739/checks/",
    "tags": {},
    "headers": {
        "Return-Path": "<libcamera-devel-bounces@lists.libcamera.org>",
        "X-Original-To": "parsemail@patchwork.libcamera.org",
        "Delivered-To": "parsemail@patchwork.libcamera.org",
        "Received": [
            "from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 5D734C32C1\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 22 Oct 2024 14:53:39 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 8C7CF65398;\n\tTue, 22 Oct 2024 16:53:38 +0200 (CEST)",
            "from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 01DF865393\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 22 Oct 2024 16:53:31 +0200 (CEST)",
            "from ideasonboard.com (93-61-96-190.ip145.fastwebnet.it\n\t[93.61.96.190])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 3EF74A47;\n\tTue, 22 Oct 2024 16:51:44 +0200 (CEST)"
        ],
        "Authentication-Results": "lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"pqwbVqoM\"; dkim-atps=neutral",
        "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1729608704;\n\tbh=FQ7fZ9HQW6gfWzuHo8KMLLnos0GfE8huRu26C/Wl4lo=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=pqwbVqoMyplU4dxM8ZYEc5kM5eFE3a/KMK25JnKFSi7IhmFkpKCg44KW0O3xZ0ykV\n\tYHKejtUIAXR5YyYbmeLTEYKim3FPbMXFFGcO/1GABWXQn3wM3E9+bYZz3keyze5kyU\n\t7CVjXsNwRjRZV6PjOQDRTBwmnZ0MhloLhJeJhlJ0=",
        "From": "Jacopo Mondi <jacopo.mondi@ideasonboard.com>",
        "To": "libcamera-devel@lists.libcamera.org",
        "Cc": "Jacopo Mondi <jacopo.mondi@ideasonboard.com>,\n\tLaurent Pinchart <laurent.pinchart@ideasonboard.com>,\n\tStefan Klug <stefan.klug@ideasonboard.com>",
        "Subject": "[PATCH 3/3] libcamera: camera_sensor: Sort factories by priority",
        "Date": "Tue, 22 Oct 2024 16:53:15 +0200",
        "Message-ID": "<20241022145321.25923-4-jacopo.mondi@ideasonboard.com>",
        "X-Mailer": "git-send-email 2.47.0",
        "In-Reply-To": "<20241022145321.25923-1-jacopo.mondi@ideasonboard.com>",
        "References": "<20241022145321.25923-1-jacopo.mondi@ideasonboard.com>",
        "MIME-Version": "1.0",
        "Content-Transfer-Encoding": "8bit",
        "X-BeenThere": "libcamera-devel@lists.libcamera.org",
        "X-Mailman-Version": "2.1.29",
        "Precedence": "list",
        "List-Id": "<libcamera-devel.lists.libcamera.org>",
        "List-Unsubscribe": "<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>",
        "List-Archive": "<https://lists.libcamera.org/pipermail/libcamera-devel/>",
        "List-Post": "<mailto:libcamera-devel@lists.libcamera.org>",
        "List-Help": "<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>",
        "List-Subscribe": "<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>",
        "Errors-To": "libcamera-devel-bounces@lists.libcamera.org",
        "Sender": "\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"
    },
    "content": "From: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\nIn order to support a default implementation for camera sensors when no\nbetter implementation matches, libcamera needs to try \"specialized\"\nimplementations first and pick the default last. Make this possible by\nadding a priority value for factories. Newly registered factories are\ninserted in the factories list sorted by descending priority, and the\ndefault factory uses a negative priority to be inserted as the last\nelement.\n\nThis mechanism may be a bit overkill in the sense that there is no\nexpected use cases for priorities other than trying the default last,\nbut the implementation is simple and easy to understand.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\nReviewed-by: Stefan Klug <stefan.klug@ideasonboard.com>\nReviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>\nSigned-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>\n---\n include/libcamera/internal/camera_sensor.h    | 12 ++++---\n src/libcamera/sensor/camera_sensor.cpp        | 35 ++++++++++++++++---\n src/libcamera/sensor/camera_sensor_legacy.cpp |  2 +-\n 3 files changed, 39 insertions(+), 10 deletions(-)",
    "diff": "diff --git a/include/libcamera/internal/camera_sensor.h b/include/libcamera/internal/camera_sensor.h\nindex 7713aea02fa2..8aafd82e6547 100644\n--- a/include/libcamera/internal/camera_sensor.h\n+++ b/include/libcamera/internal/camera_sensor.h\n@@ -78,12 +78,13 @@ public:\n class CameraSensorFactoryBase\n {\n public:\n-\tCameraSensorFactoryBase(const char *name);\n+\tCameraSensorFactoryBase(const char *name, int priority);\n \tvirtual ~CameraSensorFactoryBase() = default;\n \n \tstatic std::unique_ptr<CameraSensor> create(MediaEntity *entity);\n \n \tconst std::string &name() const { return name_; }\n+\tint priority() const { return priority_; }\n \n private:\n \tLIBCAMERA_DISABLE_COPY_AND_MOVE(CameraSensorFactoryBase)\n@@ -96,14 +97,15 @@ private:\n \tmatch(MediaEntity *entity) const = 0;\n \n \tstd::string name_;\n+\tint priority_;\n };\n \n template<typename _CameraSensor>\n class CameraSensorFactory final : public CameraSensorFactoryBase\n {\n public:\n-\tCameraSensorFactory(const char *name)\n-\t\t: CameraSensorFactoryBase(name)\n+\tCameraSensorFactory(const char *name, int priority)\n+\t\t: CameraSensorFactoryBase(name, priority)\n \t{\n \t}\n \n@@ -115,7 +117,7 @@ private:\n \t}\n };\n \n-#define REGISTER_CAMERA_SENSOR(sensor) \\\n-static CameraSensorFactory<sensor> global_##sensor##Factory{ #sensor };\n+#define REGISTER_CAMERA_SENSOR(sensor, priority) \\\n+static CameraSensorFactory<sensor> global_##sensor##Factory{ #sensor, priority };\n \n } /* namespace libcamera */\ndiff --git a/src/libcamera/sensor/camera_sensor.cpp b/src/libcamera/sensor/camera_sensor.cpp\nindex 951ebd2cc39f..54cf98b203fd 100644\n--- a/src/libcamera/sensor/camera_sensor.cpp\n+++ b/src/libcamera/sensor/camera_sensor.cpp\n@@ -349,12 +349,13 @@ CameraSensor::~CameraSensor() = default;\n /**\n  * \\brief Construct a camera sensor factory base\n  * \\param[in] name The camera sensor factory name\n+ * \\param[in] priority Priority order for factory selection\n  *\n  * Creating an instance of the factory base registers it with the global list of\n  * factories, accessible through the factories() function.\n  */\n-CameraSensorFactoryBase::CameraSensorFactoryBase(const char *name)\n-\t: name_(name)\n+CameraSensorFactoryBase::CameraSensorFactoryBase(const char *name, int priority)\n+\t: name_(name), priority_(priority)\n {\n \tregisterFactory(this);\n }\n@@ -363,6 +364,12 @@ CameraSensorFactoryBase::CameraSensorFactoryBase(const char *name)\n  * \\brief Create an instance of the CameraSensor corresponding to a media entity\n  * \\param[in] entity The media entity on the source end of the sensor\n  *\n+ * When multiple factories match the same \\a entity, this function selects the\n+ * matching factory with the highest priority as specified to the\n+ * REGISTER_CAMERA_SENSOR() macro at factory registration time. If multiple\n+ * matching factories have the same highest priority value, which factory gets\n+ * selected is undefined and may vary between runs.\n+ *\n  * \\return A unique pointer to a new instance of the CameraSensor subclass\n  * matching the entity, or a null pointer if no such factory exists\n  */\n@@ -399,8 +406,17 @@ std::unique_ptr<CameraSensor> CameraSensorFactoryBase::create(MediaEntity *entit\n  * \\return The name of the factory\n  */\n \n+/**\n+ * \\fn CameraSensorFactoryBase::priority()\n+ * \\brief Retrieve the priority value for the factory\n+ * \\return The priority value for the factory\n+ */\n+\n /**\n  * \\brief Retrieve the list of all camera sensor factories\n+ *\n+ * The factories are sorted in decreasing priority order.\n+ *\n  * \\return The list of camera sensor factories\n  */\n std::vector<CameraSensorFactoryBase *> &CameraSensorFactoryBase::factories()\n@@ -423,7 +439,12 @@ void CameraSensorFactoryBase::registerFactory(CameraSensorFactoryBase *factory)\n \tstd::vector<CameraSensorFactoryBase *> &factories =\n \t\tCameraSensorFactoryBase::factories();\n \n-\tfactories.push_back(factory);\n+\tauto pos = std::upper_bound(factories.begin(), factories.end(), factory,\n+\t\t\t\t    [](const CameraSensorFactoryBase *value,\n+\t\t\t\t       const CameraSensorFactoryBase *elem) {\n+\t\t\t\t\t    return value->priority() > elem->priority();\n+\t\t\t\t    });\n+\tfactories.insert(pos, factory);\n }\n \n /**\n@@ -449,9 +470,10 @@ void CameraSensorFactoryBase::registerFactory(CameraSensorFactoryBase *factory)\n  */\n \n /**\n- * \\def REGISTER_CAMERA_SENSOR(sensor)\n+ * \\def REGISTER_CAMERA_SENSOR(sensor, priority)\n  * \\brief Register a camera sensor type to the sensor factory\n  * \\param[in] sensor Class name of the CameraSensor derived class to register\n+ * \\param[in] priority Priority order for factory selection\n  *\n  * Register a CameraSensor subclass with the factory and make it available to\n  * try and match sensors. The subclass needs to implement a static match\n@@ -469,6 +491,11 @@ void CameraSensorFactoryBase::registerFactory(CameraSensorFactoryBase *factory)\n  *   creation succeeded ;\n  * - A non-zero error code if the entity matched and the creation failed ; or\n  * - A zero error code if the entity didn't match.\n+ *\n+ * When multiple factories can support the same MediaEntity (as in the match()\n+ * function of multiple factories returning true for the same entity), the \\a\n+ * priority argument selects which factory will be used. See\n+ * CameraSensorFactoryBase::create() for more information.\n  */\n \n } /* namespace libcamera */\ndiff --git a/src/libcamera/sensor/camera_sensor_legacy.cpp b/src/libcamera/sensor/camera_sensor_legacy.cpp\nindex 68c945331044..a9b15c038251 100644\n--- a/src/libcamera/sensor/camera_sensor_legacy.cpp\n+++ b/src/libcamera/sensor/camera_sensor_legacy.cpp\n@@ -1010,6 +1010,6 @@ std::string CameraSensorLegacy::logPrefix() const\n \treturn \"'\" + entity_->name() + \"'\";\n }\n \n-REGISTER_CAMERA_SENSOR(CameraSensorLegacy)\n+REGISTER_CAMERA_SENSOR(CameraSensorLegacy, -100)\n \n } /* namespace libcamera */\n",
    "prefixes": [
        "3/3"
    ]
}