[{"id":1718,"web_url":"https://patchwork.libcamera.org/comment/1718/","msgid":"<20190528161911.GH14336@pendragon.ideasonboard.com>","date":"2019-05-28T16:19:11","subject":"Re: [libcamera-devel] [PATCH 5/8] libcamera: ipa_manager: implement\n\tclass for managing IPA modules","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Paul,\n\nThank you for the patch.\n\nOn Mon, May 27, 2019 at 06:35:37PM -0400, Paul Elder wrote:\n> IPAManager is a class that will search in given directories for IPA\n> modules, and will load them into a list. It also provides an interface\n> for pipeline handlers to aquire an IPA.\n> \n> Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>\n> ---\n>  src/libcamera/include/ipa_manager.h |  40 +++++++++\n>  src/libcamera/ipa_manager.cpp       | 129 ++++++++++++++++++++++++++++\n>  src/libcamera/meson.build           |   2 +\n>  3 files changed, 171 insertions(+)\n>  create mode 100644 src/libcamera/include/ipa_manager.h\n>  create mode 100644 src/libcamera/ipa_manager.cpp\n> \n> diff --git a/src/libcamera/include/ipa_manager.h b/src/libcamera/include/ipa_manager.h\n> new file mode 100644\n> index 0000000..fafafad\n> --- /dev/null\n> +++ b/src/libcamera/include/ipa_manager.h\n> @@ -0,0 +1,40 @@\n> +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> +/*\n> + * Copyright (C) 2019, Google Inc.\n> + *\n> + * ipa_manager.h - Image Processing Algorithm module manager\n> + */\n> +#ifndef __LIBCAMERA_IPA_MANAGER_H__\n> +#define __LIBCAMERA_IPA_MANAGER_H__\n> +\n> +#include <libcamera/ipa/ipa_interface.h>\n> +#include <libcamera/ipa/ipa_module_info.h>\n> +#include <list>\n> +#include <string>\n\nSame comment as for the previous patch.\n\n> +\n> +#include \"ipa_module.h\"\n> +#include \"pipeline_handler.h\"\n> +\n> +namespace libcamera {\n> +\n> +class IPAManager\n> +{\n> +public:\n> +\tstatic IPAManager *instance();\n> +\n> +\tint addDir(const std::string &libDir);\n\nI would pass a const char * here, as that's what the caller has, so\nconversion to std::string would only happen internally, as needed. I\nwould also name the method addDirectory.\n\nThis shouldn't be a public method, otherwise pipeline handlers would\nhave to call it, and thus all have a list of directories to consider. I\nsee two options:\n\n- Make this method private, and call it from the constructor, with a\n  hardcoded directory.\n\n- Call this method from the CameraManager at init time, with the\n  hardcoded directory specified in camera_manager.cpp. You could still\n  make the method private, and specify the CameraManager as a friend.\n\nThe hardcoded directory should come from meson, and set to\njoin_paths(get_option('libdir'), 'libcamera'). You will need to get\nmeson to generate a config.h, see commit 2caceca8e135.\n\n> +\n> +\tstd::unique_ptr<IPAInterface> createIPA(PipelineHandler *pipe) const;\n\nI wouldn't make this method const, as conceptually it modifies (by\ncalling load()) one of the IPA modules that the manager contains.\n\n> +\n> +private:\n> +\tstd::list<IPAModule *> modules_;\n\nAny reason to use list instead of vector ?\n\n> +\n> +\tIPAManager();\n> +\t~IPAManager();\n> +\n> +\tbool match(const IPAModule *ipam, PipelineHandler *pipe) const;\n> +};\n> +\n> +} /* namespace libcamera */\n> +\n> +#endif /* __LIBCAMERA_IPA_MANAGER_H__ */\n> diff --git a/src/libcamera/ipa_manager.cpp b/src/libcamera/ipa_manager.cpp\n> new file mode 100644\n> index 0000000..3b3c1a6\n> --- /dev/null\n> +++ b/src/libcamera/ipa_manager.cpp\n> @@ -0,0 +1,129 @@\n> +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> +/*\n> + * Copyright (C) 2019, Google Inc.\n> + *\n> + * ipa_manager.cpp - Image Processing Algorithm module manager\n> + */\n> +\n> +#include \"ipa_manager.h\"\n> +#include \"ipa_module.h\"\n> +#include \"pipeline_handler.h\"\n> +#include \"utils.h\"\n> +\n> +#include <dlfcn.h>\n> +#include <dirent.h>\n> +#include <string.h>\n> +#include <sys/types.h>\n> +\n> +#include \"log.h\"\n\nipa_manager.h first, followed by system headers, followed by libcamera\nheaders.\n\n> +\n> +/**\n> + * \\file ipa_manager.h\n> + * \\brief Image Processing Algorithm module manager\n> + */\n> +\n> +namespace libcamera {\n> +\n> +LOG_DEFINE_CATEGORY(IPAManager)\n> +\n> +/**\n> + * \\class IPAManager\n> + * \\brief Manager for IPA modules\n> + */\n> +\n> +IPAManager::IPAManager()\n> +{\n> +}\n> +\n> +IPAManager::~IPAManager()\n> +{\n> +\tfor (IPAModule *module : modules_)\n> +\t\tdelete module;\n> +}\n> +\n> +/**\n> + * \\brief Retrieve the IPA manager instance\n> + *\n> + * The IPAManager is a singleton and can't be constructed manually. This\n> + * function shall instead be used to retrieve the single global instance of the\n> + * manager.\n> + *\n> + * \\return The IPA manager instance\n> + */\n> +IPAManager *IPAManager::instance()\n> +{\n> +\tstatic IPAManager ipaManager;\n> +\treturn &ipaManager;\n> +}\n> +\n> +/**\n> + * \\brief Load IPA modules from a directory\n> + * \\param[in] libDir directory to search for IPA modules\n> + *\n> + * Goes through \\a libDir and tries to create an IPAModule instance for every\n> + * found shared object. Skips invalid IPA modules.\n\nPlease add subjects to your sentences :-)\n\n> + *\n> + * \\return total number of modules loaded (including modules loaded in\n> + * previous calls of this function), or negative error code\n\nThat's a bit of a weird return value. I would either return the number\nof modules loaded by this call, or just zero (on success).\n\n\", or a negative error code otherwise\"\n\n> + */\n> +int IPAManager::addDir(const std::string &libDir)\n> +{\n> +\tstruct dirent *ent;\n> +\tDIR *dir;\n> +\n> +\tdir = opendir(libDir.c_str());\n> +\tif (!dir) {\n> +\t\tint ret = -errno;\n> +\t\tLOG(IPAManager, Error) << \"Invalid path for IPA modules: \"\n> +\t\t\t\t       << strerror(ret);\n\nI would print the path as part of the message (and use the usual LOG()\nstyle as explained in a previous patch).\n\n> +\t\treturn ret;\n> +\t}\n> +\n> +\twhile ((ent = readdir(dir)) != nullptr) {\n> +\t\tint offset = strlen(ent->d_name) - 3;\n\nWhat if the file name is shorter than 3 characters ?\n\n> +\t\tif (strncmp(&ent->d_name[offset], \".so\", 3))\n\nI think you can use strcmp() as both strings are zero-terminated.\n\n> +\t\t\tcontinue;\n> +\n> +\t\tIPAModule *ipaModule = new IPAModule(libDir + \"/\" + ent->d_name);\n> +\t\tif (ipaModule->isValid())\n> +\t\t\tmodules_.push_back(ipaModule);\n\n\t\telse\n\t\t\tdelete ipaModule;\n\notherwise you will leak it. You could also write\n\n\t\tif (!ipaModule->isValid()) {\n\t\t\tdelete ipaModule;\n\t\t\tcontinue;\n\t\t}\n\n\t\tmodules_.push_back(ipaModule);\n\n> +\t}\n> +\n> +\tclosedir(dir);\n> +\treturn modules_.size();\n> +}\n> +\n> +/**\n> + * \\brief Create an IPA interface that matches a given pipeline handler\n> + * \\param[in] pipe the pipeline handler that wants a matching IPA interface\n\ns/the/The/\n\n> + *\n> + * \\return IPA interface, or nullptr if no matching IPA module is found\n> + */\n> +std::unique_ptr<IPAInterface> IPAManager::createIPA(PipelineHandler *pipe) const\n> +{\n> +\tIPAModule *m = nullptr;\n> +\n> +\tfor (IPAModule *module : modules_) {\n> +\t\tif (match(module, pipe)) {\n> +\t\t\tm = module;\n> +\t\t\tbreak;\n> +\t\t}\n> +\t}\n> +\n> +\tif (!m || !m->load())\n> +\t\treturn nullptr;\n> +\n> +\treturn m->createInstance();\n> +}\n> +\n> +bool IPAManager::match(const IPAModule *ipam, PipelineHandler *pipe) const\n\nOpen question, would it make sense to move this method to the IPAModule\nclass ?\n\n> +{\n> +\tconst struct IPAModuleInfo *info = &ipam->info();\n> +\n> +\treturn !strcmp(info->pipelineName, pipe->name()) &&\n> +\t       info->pipelineVersionMajor == pipe->majorVersion() &&\n> +\t       info->pipelineVersionMinor >= pipe->minorVersion() &&\n> +\t       info->moduleAPIVersion == IPA_MODULE_API_VERSION;\n\nYou should compare moduleAPIVersion first, as this conditions the layout\nof the rest of the structure (granted, right now we support a single\nversion, so it doesn't matter and we'll have to change this code when\ndifferent versions will be needed, but it doesn't hurt to handle the\nversion correctly right away, with a comment explaining why).\n\n> +}\n> +\n> +} /* namespace libcamera */\n> diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build\n> index 32f7da4..0889b0d 100644\n> --- a/src/libcamera/meson.build\n> +++ b/src/libcamera/meson.build\n> @@ -11,6 +11,7 @@ libcamera_sources = files([\n>      'formats.cpp',\n>      'geometry.cpp',\n>      'ipa_interface.cpp',\n> +    'ipa_manager.cpp',\n>      'ipa_module.cpp',\n>      'log.cpp',\n>      'media_device.cpp',\n> @@ -33,6 +34,7 @@ libcamera_headers = files([\n>      'include/device_enumerator_udev.h',\n>      'include/event_dispatcher_poll.h',\n>      'include/formats.h',\n> +    'include/ipa_manager.h',\n>      'include/ipa_module.h',\n>      'include/log.h',\n>      'include/media_device.h',","headers":{"Return-Path":"<laurent.pinchart@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 8F334600EA\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 28 May 2019 18:19:31 +0200 (CEST)","from pendragon.ideasonboard.com (85-76-96-53-nat.elisa-mobile.fi\n\t[85.76.96.53])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 2C3E6D85;\n\tTue, 28 May 2019 18:19:28 +0200 (CEST)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1559060371;\n\tbh=Ok9Rw09Yi0Af/Uray7LMUx91arFNC+smjMjqZ/TugNs=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=huhbU9ftRzEVHxMSRvpA6PBi6zcQbcfIe3iDI4aqQmDApNzc9XmgcZO5zFXp1Harg\n\tjRknmzDYpIOCsZDT8nhXJ+r+JDGUKjhztBsNGPYWL3Gu+KWEkmeMJ9+50CQK3oWGhg\n\teQXJg7YXcynHjliwsjEdN7JGR1cK0qYURhCQ5CuE=","Date":"Tue, 28 May 2019 19:19:11 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Paul Elder <paul.elder@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Message-ID":"<20190528161911.GH14336@pendragon.ideasonboard.com>","References":"<20190527223540.21855-1-paul.elder@ideasonboard.com>\n\t<20190527223540.21855-6-paul.elder@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20190527223540.21855-6-paul.elder@ideasonboard.com>","User-Agent":"Mutt/1.10.1 (2018-07-13)","Subject":"Re: [libcamera-devel] [PATCH 5/8] libcamera: ipa_manager: implement\n\tclass for managing IPA modules","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":"Tue, 28 May 2019 16:19:31 -0000"}}]