Show a patch.

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

{
    "id": 21355,
    "url": "https://patchwork.libcamera.org/api/patches/21355/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/21355/",
    "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": "<20240925095850.348259-2-mzamazal@redhat.com>",
    "date": "2024-09-25T09:58:33",
    "name": "[v4,01/15] config: Introduce global runtime configuration",
    "commit_ref": null,
    "pull_url": null,
    "state": "superseded",
    "archived": false,
    "hash": "16c1c0f3417469ae616d959ac682920ae4f3543a",
    "submitter": {
        "id": 177,
        "url": "https://patchwork.libcamera.org/api/people/177/?format=api",
        "name": "Milan Zamazal",
        "email": "mzamazal@redhat.com"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/21355/mbox/",
    "series": [
        {
            "id": 4623,
            "url": "https://patchwork.libcamera.org/api/series/4623/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=4623",
            "date": "2024-09-25T09:58:32",
            "name": "Add global configuration file",
            "version": 4,
            "mbox": "https://patchwork.libcamera.org/series/4623/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/21355/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/21355/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 64615C0F1B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 25 Sep 2024 09:59:16 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id A6ED363511;\n\tWed, 25 Sep 2024 11:59:15 +0200 (CEST)",
            "from us-smtp-delivery-124.mimecast.com\n\t(us-smtp-delivery-124.mimecast.com [170.10.133.124])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 89EA06350B\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 25 Sep 2024 11:59:11 +0200 (CEST)",
            "from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com\n\t(ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63])\n\tby relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3,\n\tcipher=TLS_AES_256_GCM_SHA384) id us-mta-520-ehk4ok-RMuW3OYu9H-eBFA-1;\n\tWed, 25 Sep 2024 05:59:07 -0400",
            "from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (unknown\n\t[10.30.177.12])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\tkey-exchange X25519 server-signature RSA-PSS (2048 bits)\n\tserver-digest SHA256) (No client certificate requested)\n\tby mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix)\n\twith ESMTPS id B79201944D3F; Wed, 25 Sep 2024 09:59:05 +0000 (UTC)",
            "from nuthatch.redhat.com (unknown [10.45.225.29])\n\tby mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix)\n\twith ESMTP id 2678619560AA; Wed, 25 Sep 2024 09:59:03 +0000 (UTC)"
        ],
        "Authentication-Results": "lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=redhat.com header.i=@redhat.com\n\theader.b=\"GT24heOn\"; dkim-atps=neutral",
        "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n\ts=mimecast20190719; t=1727258350;\n\th=from:from:reply-to:subject:subject:date:date:message-id:message-id:\n\tto:to:cc:cc:mime-version:mime-version:content-type:content-type:\n\tcontent-transfer-encoding:content-transfer-encoding:\n\tin-reply-to:in-reply-to:references:references;\n\tbh=d7WqZXQn5Rlwro0MAzLDMRm8pMH+gnoSEVbHtAkzQK8=;\n\tb=GT24heOnRFpdt5MPXIH1XtKQ40Ht9wPp28xkK2eV8G5Mv2cd2MnpG2Xt6Z66Q6RvDAo6gK\n\taJXQlrsDVRkSgkfG1I+u9R3Ik0b8SuejWsxSnWITyKd+wM8HSrBLfXbmoksAHg26xrwfWE\n\tnn83OwlAb2VeUC+uiI5vr7I7XeEIMno=",
        "X-MC-Unique": "ehk4ok-RMuW3OYu9H-eBFA-1",
        "From": "Milan Zamazal <mzamazal@redhat.com>",
        "To": "libcamera-devel@lists.libcamera.org",
        "Cc": "Milan Zamazal <mzamazal@redhat.com>,\n\tKieran Bingham <kieran.bingham@ideasonboard.com>,\n\tNaushir Patuck <naush@raspberrypi.com>",
        "Subject": "[PATCH v4 01/15] config: Introduce global runtime configuration",
        "Date": "Wed, 25 Sep 2024 11:58:33 +0200",
        "Message-ID": "<20240925095850.348259-2-mzamazal@redhat.com>",
        "In-Reply-To": "<20240925095850.348259-1-mzamazal@redhat.com>",
        "References": "<20240925095850.348259-1-mzamazal@redhat.com>",
        "MIME-Version": "1.0",
        "X-Scanned-By": "MIMEDefang 3.0 on 10.30.177.12",
        "X-Mimecast-Spam-Score": "0",
        "X-Mimecast-Originator": "redhat.com",
        "Content-Transfer-Encoding": "8bit",
        "Content-Type": "text/plain; charset=\"US-ASCII\"; x-default=true",
        "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": "Currently, libcamera can be configured in runtime using several\nenvironment variables.  With introducing more and more variables, this\nmechanism reaches its limits.  It would be simpler and more flexible if\nit was possible to configure libcamera in a single file.\n\nFor example, there have been a request for defining pipeline precedence\nin runtime.  We want to compile in multiple pipelines, in order to have\nthem accessible within single packages in distributions.  And then being\nable to select among the pipelines manually as needed based on the\nparticular hardware or operating system environment.  Having the\nconfiguration file then allows easy switching between hardware, GPU or\nCPU IPAs.  Another possible use case is tuning image output, especially\nwith software ISP, to user liking.  For example, some users may prefer\nhigher contrast without the need to use the corresponding knobs, if\npresent at all, in every application.  The configuration file can also\nbe used to enable or disable experimental features and avoid the need to\ntrack local patches changing configuration options hard-wired in the\ncode when working on new features.\n\nThis patch introduces basic support for configuration files.\nGlobalConfiguration class reads, stores and accesses the configuration.\nIts instance is stored as a private singleton accessed using a static\nmethod of the class.  There is currently no reason to have more than one\ninstance.\n\nlibcamera configuration can be specified using a system-wide\nconfiguration file or a user configuration file.  The user configuration\nfile takes precedence if present.  There is currently no way to merge\nmultiple configuration files, the one found is used as the only\nconfiguration file.  If no configuration file is present, nothing\nchanges to the current libcamera behavior (except for some log\nmessages related to configuration file lookup).\n\nThe configuration file is a YAML file.  We already have a mechanism for\nhandling YAML configuration files in libcamera and the given\ninfrastructure can be reused for the purpose.  However, the\nconfiguration type is abstracted to make contingent future change of the\nunderlying class easier while retaining (most of) the original API.\n\nThe configuration is versioned.  This has currently no particular\nmeaning but is likely to have its purpose in future, especially once\nconfiguration validation is introduced.\n\nThe configuration YAML file looks as follows:\n\n  ---\n  version: 1\n  configuration:\n    WHATEVER CONFIGURATION NEEDED\n\nThis patch introduces just the basic idea.  Actually using the\nconfiguration in the corresponding places (everything what is currently\nconfigurable via environment variables should be configurable in the\nfile configuration), refining the mechanism (e.g. configuration file\nvalidation to avoid mistakes caused by typos) and other improvements\nwill be addressed in followup patches.\n\nSigned-off-by: Milan Zamazal <mzamazal@redhat.com>\n---\n .../libcamera/internal/global_configuration.h |  38 ++++\n include/libcamera/internal/meson.build        |   1 +\n src/libcamera/global_configuration.cpp        | 163 ++++++++++++++++++\n src/libcamera/meson.build                     |   1 +\n 4 files changed, 203 insertions(+)\n create mode 100644 include/libcamera/internal/global_configuration.h\n create mode 100644 src/libcamera/global_configuration.cpp",
    "diff": "diff --git a/include/libcamera/internal/global_configuration.h b/include/libcamera/internal/global_configuration.h\nnew file mode 100644\nindex 000000000..2f38056a1\n--- /dev/null\n+++ b/include/libcamera/internal/global_configuration.h\n@@ -0,0 +1,38 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2024 Red Hat, inc.\n+ *\n+ * global_configuration.h - Global configuration handling\n+ */\n+\n+#pragma once\n+\n+#include <filesystem>\n+#include <vector>\n+\n+#include \"libcamera/internal/yaml_parser.h\"\n+\n+namespace libcamera {\n+\n+class GlobalConfiguration\n+{\n+public:\n+\tusing Configuration = const YamlObject &;\n+\n+\tstatic unsigned int version();\n+\tstatic Configuration configuration();\n+\n+private:\n+\tstatic const std::vector<std::filesystem::path> globalConfigurationFiles;\n+\n+\tbool initialized_;\n+\tstd::unique_ptr<YamlObject> configuration_;\n+\n+\tGlobalConfiguration();\n+\tbool loadFile(const std::filesystem::path &fileName);\n+\tvoid load();\n+\tstatic const GlobalConfiguration &instance();\n+\tstatic Configuration get();\n+};\n+\n+} /* namespace libcamera */\ndiff --git a/include/libcamera/internal/meson.build b/include/libcamera/internal/meson.build\nindex 1c5eef9ca..6fc6406d7 100644\n--- a/include/libcamera/internal/meson.build\n+++ b/include/libcamera/internal/meson.build\n@@ -21,6 +21,7 @@ libcamera_internal_headers = files([\n     'dma_buf_allocator.h',\n     'formats.h',\n     'framebuffer.h',\n+    'global_configuration.h',\n     'ipa_data_serializer.h',\n     'ipa_manager.h',\n     'ipa_module.h',\ndiff --git a/src/libcamera/global_configuration.cpp b/src/libcamera/global_configuration.cpp\nnew file mode 100644\nindex 000000000..96088b36e\n--- /dev/null\n+++ b/src/libcamera/global_configuration.cpp\n@@ -0,0 +1,163 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2024 Red Hat, inc.\n+ *\n+ * global_configuration.cpp - Global configuration handling\n+ */\n+\n+#include \"libcamera/internal/global_configuration.h\"\n+\n+#include <filesystem>\n+#include <memory>\n+#include <stdint.h>\n+#include <sys/types.h>\n+\n+#include <libcamera/base/file.h>\n+#include <libcamera/base/log.h>\n+#include <libcamera/base/utils.h>\n+\n+#include \"libcamera/internal/yaml_parser.h\"\n+\n+namespace libcamera {\n+\n+LOG_DEFINE_CATEGORY(Configuration)\n+\n+/**\n+ * \\class GlobalConfiguration\n+ * \\brief Support for global libcamera configuration\n+ *\n+ * The configuration file is a YAML file and the configuration itself is stored\n+ * under `configuration' top-level item.\n+ *\n+ * The configuration file is looked up in user's home directory first and if it\n+ * is not found then in system-wide configuration directories. If multiple\n+ * configuration files exist then only the first one found is used and no\n+ * configuration merging is performed.\n+ *\n+ * The class is used as a private singleton and the configuration can be\n+ * accessed usingGlobalConfiguration::configuration().\n+ */\n+\n+/**\n+ * \\typedef GlobalConfiguration::Configuration\n+ * \\brief Type representing global libcamera configuration\n+ *\n+ * All code outside GlobalConfiguration must use this type declaration and not\n+ * the underlying type.\n+ */\n+\n+GlobalConfiguration::GlobalConfiguration()\n+\t: initialized_(false), configuration_(std::make_unique<YamlObject>())\n+{\n+}\n+\n+const std::vector<std::filesystem::path>\n+\tGlobalConfiguration::globalConfigurationFiles = {\n+\t\tstd::filesystem::path(LIBCAMERA_SYSCONF_DIR) / \"configuration.yaml\",\n+\t\tstd::filesystem::path(\"/etc/libcamera/configuration.yaml\"),\n+\t};\n+\n+void GlobalConfiguration::load()\n+{\n+\tstd::filesystem::path userConfigurationDirectory;\n+\tchar *xdgConfigHome = utils::secure_getenv(\"XDG_CONFIG_HOME\");\n+\tif (xdgConfigHome) {\n+\t\tuserConfigurationDirectory = xdgConfigHome;\n+\t} else {\n+\t\tconst char *home = utils::secure_getenv(\"HOME\");\n+\t\tif (home)\n+\t\t\tuserConfigurationDirectory =\n+\t\t\t\tstd::filesystem::path(home) / \".config\";\n+\t}\n+\n+\tif (!userConfigurationDirectory.empty()) {\n+\t\tstd::filesystem::path user_configuration_file =\n+\t\t\tuserConfigurationDirectory / \"libcamera\" / \"configuration.yaml\";\n+\t\tif (loadFile(user_configuration_file))\n+\t\t\treturn;\n+\t}\n+\n+\tfor (auto path : globalConfigurationFiles)\n+\t\tif (loadFile(path))\n+\t\t\treturn;\n+\n+\tinitialized_ = true;\n+\tLOG(Configuration, Debug) << \"No configuration file found\";\n+}\n+\n+bool GlobalConfiguration::loadFile(const std::filesystem::path &fileName)\n+{\n+\tFile file(fileName);\n+\tif (!file.exists()) {\n+\t\treturn false;\n+\t}\n+\n+\tif (!file.open(File::OpenModeFlag::ReadOnly)) {\n+\t\tLOG(Configuration, Warning)\n+\t\t\t<< \"Failed to open configuration file '\" << fileName << \"'\";\n+\t\treturn true;\n+\t}\n+\n+\tauto root = YamlParser::parse(file);\n+\tif (!root) {\n+\t\tLOG(Configuration, Warning) << \"Failed to parse configuration file \"\n+\t\t\t\t\t    << fileName;\n+\t\treturn true;\n+\t}\n+\tconfiguration_ = std::move(root);\n+\tinitialized_ = true;\n+\tLOG(Configuration, Info) << \"Configuration file \" << fileName << \"loaded\";\n+\n+\treturn true;\n+}\n+\n+const GlobalConfiguration &GlobalConfiguration::instance()\n+{\n+\tstatic GlobalConfiguration configuration;\n+\tif (!configuration.initialized_) {\n+\t\tconfiguration.load();\n+\t}\n+\treturn configuration;\n+}\n+\n+GlobalConfiguration::Configuration GlobalConfiguration::get()\n+{\n+\treturn (*instance().configuration_);\n+}\n+\n+/**\n+ * \\brief Return configuration version\n+ *\n+ * The version is (optionally) declared in the configuration file in the\n+ * top-level section `version', alongside `configuration'. This has currently no\n+ * real use but may be needed in future if configuration incompatibilities\n+ * occur.\n+ *\n+ * \\return Configuration version as declared in the configuration file or 0 if\n+ * no version is declared there\n+ */\n+unsigned int GlobalConfiguration::version()\n+{\n+\treturn get()[\"version\"].get<unsigned int>().value_or(0);\n+}\n+\n+/**\n+ * \\brief Return libcamera global configuration\n+ *\n+ * This returns the whole configuration stored in the top-level section\n+ * `configuration' of the YAML configuration file.\n+ *\n+ * The requested part of the configuration can be accessed using \\a YamlObject\n+ * methods.\n+ *\n+ * \\note \\a YamlObject type itself shouldn't be used in type declarations to\n+ * avoid trouble if we decide to change the underlying data objects in future.\n+ *\n+ * \\return The whole configuration section\n+ */\n+GlobalConfiguration::Configuration GlobalConfiguration::configuration()\n+{\n+\treturn get()[\"configuration\"];\n+}\n+\n+} /* namespace libcamera */\ndiff --git a/src/libcamera/meson.build b/src/libcamera/meson.build\nindex aa9ab0291..520c6c8d3 100644\n--- a/src/libcamera/meson.build\n+++ b/src/libcamera/meson.build\n@@ -29,6 +29,7 @@ libcamera_internal_sources = files([\n     'device_enumerator_sysfs.cpp',\n     'dma_buf_allocator.cpp',\n     'formats.cpp',\n+    'global_configuration.cpp',\n     'ipa_controls.cpp',\n     'ipa_data_serializer.cpp',\n     'ipa_interface.cpp',\n",
    "prefixes": [
        "v4",
        "01/15"
    ]
}