{"id":1591,"url":"https://patchwork.libcamera.org/api/1.1/patches/1591/?format=json","web_url":"https://patchwork.libcamera.org/patch/1591/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20190703080007.21376-4-paul.elder@ideasonboard.com>","date":"2019-07-03T08:00:03","name":"[libcamera-devel,RFC,v2,3/7] libcamera: add IPA proxy","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"bea9a8759e585138162956aa6e03ba2302dfaf9b","submitter":{"id":17,"url":"https://patchwork.libcamera.org/api/1.1/people/17/?format=json","name":"Paul Elder","email":"paul.elder@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/1591/mbox/","series":[{"id":392,"url":"https://patchwork.libcamera.org/api/1.1/series/392/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=392","date":"2019-07-03T08:00:00","name":"Add IPA process isolation","version":2,"mbox":"https://patchwork.libcamera.org/series/392/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/1591/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/1591/checks/","tags":{},"headers":{"Return-Path":"<paul.elder@ideasonboard.com>","Received":["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 CBFE961570\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed,  3 Jul 2019 10:00:29 +0200 (CEST)","from neptunite.flets-east.jp\n\t(p1871204-ipngn14001hodogaya.kanagawa.ocn.ne.jp [153.220.127.204])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 19B3524B;\n\tWed,  3 Jul 2019 10:00:27 +0200 (CEST)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1562140829;\n\tbh=ZisJE4OplUNqbdzGv34PdSGuuMQJGsu7yatzszB5vfQ=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=nqAKTokYHrJwfDHn0yiK1ef6AB4Sj/TYJVOSzz9MJM+/SufY292uJ6/y6LyvoeGwf\n\tHxAYMPJ2tTJza/DMTdA4hA8Y+D82pHFogycMTDyERtcPsegUT6wqYG3rLXwxMuKU7h\n\tGAEnG9yGsXwvM91We6dwoACe+ZRMS+vSwryHQVQY=","From":"Paul Elder <paul.elder@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Wed,  3 Jul 2019 17:00:03 +0900","Message-Id":"<20190703080007.21376-4-paul.elder@ideasonboard.com>","X-Mailer":"git-send-email 2.20.1","In-Reply-To":"<20190703080007.21376-1-paul.elder@ideasonboard.com>","References":"<20190703080007.21376-1-paul.elder@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [RFC PATCH v2 3/7] libcamera: add IPA proxy","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.23","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>","X-List-Received-Date":"Wed, 03 Jul 2019 08:00:30 -0000"},"content":"Add a Proxy class whose implementations will act as a proxy between a\npipeline handler and an isolated IPA interface. Also add a ProxyFactory\nthat will construct the Proxy implementations as necessary.\n\nUpdate Doxygen to ignore the directory where Proxy implementations will\nreside.\n\nSigned-off-by: Paul Elder <paul.elder@ideasonboard.com>\n---\nNew in v2\n- replaces shims from v1\n- build into libcamera, hence the register macro (similar to what\n  PipelineHandler uses)\n- no longer builds separate .so\n\n Documentation/Doxyfile.in         |   3 +-\n src/libcamera/include/ipa_proxy.h |  68 ++++++++++\n src/libcamera/ipa_proxy.cpp       | 219 ++++++++++++++++++++++++++++++\n src/libcamera/meson.build         |   6 +\n test/libtest/test.cpp             |   4 +\n 5 files changed, 299 insertions(+), 1 deletion(-)\n create mode 100644 src/libcamera/include/ipa_proxy.h\n create mode 100644 src/libcamera/ipa_proxy.cpp","diff":"diff --git a/Documentation/Doxyfile.in b/Documentation/Doxyfile.in\nindex 9ca3224..8c24f94 100644\n--- a/Documentation/Doxyfile.in\n+++ b/Documentation/Doxyfile.in\n@@ -837,7 +837,8 @@ EXCLUDE                = @TOP_SRCDIR@/src/libcamera/device_enumerator_sysfs.cpp\n \t\t\t @TOP_SRCDIR@/src/libcamera/device_enumerator_udev.cpp \\\n \t\t\t @TOP_SRCDIR@/src/libcamera/include/device_enumerator_sysfs.h \\\n \t\t\t @TOP_SRCDIR@/src/libcamera/include/device_enumerator_udev.h \\\n-\t\t\t @TOP_SRCDIR@/src/libcamera/pipeline/\n+\t\t\t @TOP_SRCDIR@/src/libcamera/pipeline/ \\\n+\t\t\t @TOP_SRCDIR@/src/libcamera/proxy/\n \n # The EXCLUDE_SYMLINKS tag can be used to select whether or not files or\n # directories that are symbolic links (a Unix file system feature) are excluded\ndiff --git a/src/libcamera/include/ipa_proxy.h b/src/libcamera/include/ipa_proxy.h\nnew file mode 100644\nindex 0000000..b493c1a\n--- /dev/null\n+++ b/src/libcamera/include/ipa_proxy.h\n@@ -0,0 +1,68 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2019, Google Inc.\n+ *\n+ * ipa_proxy.h - Image Processing Algorithm proxy\n+ */\n+#ifndef __LIBCAMERA_IPA_PROXY_H__\n+#define __LIBCAMERA_IPA_PROXY_H__\n+\n+#include <vector>\n+\n+#include <libcamera/ipa/ipa_interface.h>\n+\n+#include \"ipa_module.h\"\n+#include \"utils.h\"\n+\n+namespace libcamera {\n+\n+class Proxy : public IPAInterface\n+{\n+public:\n+\tProxy(IPAModule *ipam);\n+\tvirtual ~Proxy();\n+\n+\tvirtual int init() = 0;\n+\n+\tbool isValid() const;\n+\n+protected:\n+\tconst std::string resolvePath(const std::string &file) const;\n+\n+\tstd::unique_ptr<IPAInterface> ipa_;\n+\n+\tbool valid_;\n+};\n+\n+class ProxyFactory\n+{\n+public:\n+\tProxyFactory(const char *name);\n+\tvirtual ~ProxyFactory() { };\n+\n+\tvirtual std::unique_ptr<Proxy> create(IPAModule *ipam) = 0;\n+\n+\tconst std::string &name() const { return name_; }\n+\n+\tstatic void registerType(ProxyFactory *factory);\n+\tstatic std::vector<ProxyFactory *> &factories();\n+\n+private:\n+\tstd::string name_;\n+};\n+\n+#define REGISTER_IPA_PROXY(proxy)\t\t\t\\\n+class proxy##Factory final : public ProxyFactory\t\\\n+{\t\t\t\t\t\t\t\\\n+public:\t\t\t\t\t\t\t\\\n+\tproxy##Factory() : ProxyFactory(#proxy) {}\t\\\n+\tstd::unique_ptr<Proxy> create(IPAModule *ipam)\t\\\n+\t{\t\t\t\t\t\t\\\n+\t\treturn utils::make_unique<proxy>(ipam);\t\\\n+\t}\t\t\t\t\t\t\\\n+};\t\t\t\t\t\t\t\\\n+static proxy##Factory global_##proxy##Factory;\n+\n+} /* namespace libcamera */\n+\n+#endif /* __LIBCAMERA_IPA_PROXY_H__ */\ndiff --git a/src/libcamera/ipa_proxy.cpp b/src/libcamera/ipa_proxy.cpp\nnew file mode 100644\nindex 0000000..8bb3622\n--- /dev/null\n+++ b/src/libcamera/ipa_proxy.cpp\n@@ -0,0 +1,219 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2019, Google Inc.\n+ *\n+ * ipa_proxy.cpp - Image Processing Algorithm proxy\n+ */\n+\n+#include \"ipa_proxy.h\"\n+\n+#include <string.h>\n+#include <unistd.h>\n+\n+#include \"log.h\"\n+#include \"utils.h\"\n+\n+#include <iostream>\n+\n+/**\n+ * \\file ipa_proxy.h\n+ * \\brief IPA Proxy\n+ *\n+ * TODO write this\n+ *\n+ * Isolate IPA into separate process.\n+ *\n+ * Every subclass of proxy shall be registered with libcamera using\n+ * the REGISTER_IPA_PROXY() macro.\n+ */\n+\n+namespace libcamera {\n+\n+LOG_DEFINE_CATEGORY(Proxy)\n+\n+/**\n+ * \\class Proxy\n+ * \\brief IPA Proxy\n+ *\n+ * TODO write this\n+ *\n+ * Isolate IPA into separate process.\n+ */\n+\n+/**\n+ * \\brief Construct a Proxy instance\n+ * \\param[in] ipam The IPA module to wrap around\n+ *\n+ * Proxy instances shall be constructed through the ProxyFactory::create()\n+ * method implemented by the respective factories.\n+ */\n+Proxy::Proxy(IPAModule *ipam)\n+\t: valid_(false)\n+{\n+}\n+\n+Proxy::~Proxy()\n+{\n+}\n+\n+/**\n+ * \\brief Check if the Proxy instance is valid\n+ *\n+ * A Proxy instance is valid if the IPA interface is successfully created in\n+ * isolation, and IPC is successfully set up.\n+ *\n+ * \\return true if the the Proxy is valid, false otherwise\n+ */\n+bool Proxy::isValid() const\n+{\n+\treturn valid_;\n+}\n+\n+/**\n+ * \\brief Find a valid full path for a proxy worker for a given executable name\n+ * \\param[in] file File name of proxy worker executable\n+ *\n+ * A proxy worker's executable could be found in either the global installation\n+ * directory, or in the paths specified by the environment variable\n+ * LIBCAMERA_IPA_PROXY_PATH. This method checks the global install directory\n+ * first, then LIBCAMERA_IPA_PROXY_PATH in order, and returns the full path to\n+ * the proxy worker executable that is specified by file. The proxy worker\n+ * executable must be have exec permission.\n+ *\n+ * \\return full path to proxy worker executable, or empty string if no valid\n+ * executable path\n+ */\n+const std::string Proxy::resolvePath(const std::string &file) const\n+{\n+\t/* Try finding the exec target from the install directory first */\n+\tstd::string proxyFile = \"/\" + file;\n+\tstd::string proxyPath = std::string(IPA_PROXY_DIR) + proxyFile;\n+\tif (!access(proxyPath.c_str(), X_OK))\n+\t\treturn proxyPath;\n+\n+\t/* No exec target in install directory; check env variable*/\n+\tconst char *execPaths = utils::secure_getenv(\"LIBCAMERA_IPA_PROXY_PATH\");\n+\twhile (execPaths) {\n+\t\tconst char *delim = strchrnul(execPaths, ':');\n+\t\tsize_t count = delim - execPaths;\n+\n+\t\tif (count) {\n+\t\t\tstd::string proxyPath(execPaths, count);\n+\t\t\tproxyPath += proxyFile;\n+\t\t\tif (!access(proxyPath.c_str(), X_OK))\n+\t\t\t\treturn proxyPath;\n+\t\t}\n+\n+\t\tif (*delim == '\\0')\n+\t\t\tbreak;\n+\n+\t\texecPaths += count + 1;\n+\t}\n+\n+\treturn std::string();\n+}\n+\n+/**\n+ * \\var Proxy::ipa_\n+ * \\brief The IPA interface that the Proxy is isolating\n+ */\n+\n+/**\n+ * \\var Proxy::valid_\n+ * \\brief Flag to indicate if the Proxy instance is valid\n+ *\n+ * A Proxy instance is valid if the IPA interface is successfully created in\n+ * isolation, and IPC is successfully set up.\n+ *\n+ * This flag can be read via Proxy::isValid()\n+ *\n+ * Implementations of the Proxy class should set this flag upon successful\n+ * construction.\n+ */\n+\n+/**\n+ * \\class ProxyFactory\n+ * \\brief Registration of Proxy classes and creation of instances\n+ *\n+ * To facilitate discovery and instantiation of Proxy classes, the\n+ * ProxyFactory class maintains a registry of Proxy classes. Each\n+ * Proxy subclass shall register itself using the REGISTER_IPA_PROXY()\n+ * macro, which will create a corresponding instance of a ProxyFactory\n+ * subclass and register it with the static list of factories.\n+ */\n+\n+/**\n+ * \\brief Construct a Proxy factory\n+ * \\param[in] name Name of the Proxy class\n+ *\n+ * Creating an instance of the factory registers is with the global list of\n+ * factories, accessible through the factories() function.\n+ *\n+ * The factory \\a name is used for debugging and Proxy matching purposes\n+ * and shall be unique.\n+ */\n+ProxyFactory::ProxyFactory(const char *name)\n+\t: name_(name)\n+{\n+\tregisterType(this);\n+}\n+\n+/**\n+ * \\fn ProxyFactory::create()\n+ * \\brief Create an instance of the Proxy corresponding to the factory\n+ * \\param[in] ipam The IPA module\n+ *\n+ * This virtual function is implemented by the REGISTER_IPA_PROXY() macro.\n+ * It creates a Proxy instance that isolates an IPA interface designated\n+ * by the IPA module \\a ipam.\n+ *\n+ * \\return a pointer to a newly constructed instance of the Proxy subclass\n+ * corresponding to the factory\n+ */\n+\n+/**\n+ * \\fn ProxyFactory::name()\n+ * \\brief Retrieve the factory name\n+ * \\return The factory name\n+ */\n+\n+/**\n+ * \\brief Add a Proxy class to the registry\n+ * \\param[in] factory Factory to use to construct the Proxy\n+ *\n+ * The caller is responsible to guarantee the uniqueness of the Proxy name.\n+ */\n+void ProxyFactory::registerType(ProxyFactory *factory)\n+{\n+\tstd::vector<ProxyFactory *> &factories = ProxyFactory::factories();\n+\n+\tfactories.push_back(factory);\n+\n+\tLOG(Proxy, Debug)\n+\t\t<< \"Registered proxy \\\"\" << factory->name() << \"\\\"\";\n+}\n+\n+/**\n+ * \\brief Retrieve the list of all Proxy factories\n+ *\n+ * The static factories map is defined inside the function to ensures it gets\n+ * initialized on first use, without any dependency on link order.\n+ *\n+ * \\return the list of pipeline handler factories\n+ */\n+std::vector<ProxyFactory *> &ProxyFactory::factories()\n+{\n+\tstatic std::vector<ProxyFactory *> factories;\n+\treturn factories;\n+}\n+\n+/**\n+ * \\def REGISTER_IPA_PROXY\n+ * \\brief Register a Proxy with the Proxy factory\n+ * \\param[in] proxy Class name of Proxy derived class to register\n+ *\n+ * Register a proxy subclass with the factory and make it available to\n+ * isolate IPA modules.\n+ */\n+\n+} /* namespace libcamera */\ndiff --git a/src/libcamera/meson.build b/src/libcamera/meson.build\nindex 087b578..412564f 100644\n--- a/src/libcamera/meson.build\n+++ b/src/libcamera/meson.build\n@@ -14,6 +14,7 @@ libcamera_sources = files([\n     'ipa_interface.cpp',\n     'ipa_manager.cpp',\n     'ipa_module.cpp',\n+    'ipa_proxy.cpp',\n     'ipc_unixsocket.cpp',\n     'log.cpp',\n     'media_device.cpp',\n@@ -42,6 +43,7 @@ libcamera_headers = files([\n     'include/formats.h',\n     'include/ipa_manager.h',\n     'include/ipa_module.h',\n+    'include/ipa_proxy.h',\n     'include/ipc_unixsocket.h',\n     'include/log.h',\n     'include/media_device.h',\n@@ -64,6 +66,10 @@ includes = [\n \n subdir('pipeline')\n \n+proxy_install_dir = join_paths(get_option('libdir'), 'libcamera', 'proxy')\n+config_h.set('IPA_PROXY_DIR',\n+             '\"' + join_paths(get_option('prefix'), proxy_install_dir) + '\"')\n+\n libudev = dependency('libudev', required : false)\n \n if libudev.found()\ndiff --git a/test/libtest/test.cpp b/test/libtest/test.cpp\nindex b119cf1..35aa021 100644\n--- a/test/libtest/test.cpp\n+++ b/test/libtest/test.cpp\n@@ -25,6 +25,10 @@ int Test::execute()\n \tif (ret)\n \t\treturn errno;\n \n+\tret = setenv(\"LIBCAMERA_IPA_PROXY_PATH\", \"src/libcamera/proxy_worker\", 1);\n+\tif (ret)\n+\t\treturn errno;\n+\n \tret = init();\n \tif (ret)\n \t\treturn ret;\n","prefixes":["libcamera-devel","RFC","v2","3/7"]}