From patchwork Mon Jun 3 23:16:31 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Elder X-Patchwork-Id: 1341 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 07EAD618FD for ; Tue, 4 Jun 2019 01:16:50 +0200 (CEST) Received: from localhost.localdomain (unknown [96.44.9.117]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 61A4C530; Tue, 4 Jun 2019 01:16:49 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1559603809; bh=UOsv0TgC+jo2hmcdz8tvv/XPHbWwu29iW6WxYLw/Mgg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LjBS3FVAd/yjxKBQA5ct57WnN404ehtrSxu/cLcZGQjSOXsQfD9oYLIg0IdXf+LdL L5pSYeBmG/fvk+aD7/pDtyvnP6t1PMx8temRUhpssRS0TGipX2I3mVRYPquklnvXGa NSUF4OGldUURKJHgblu9e9gsg4rsZsJJxJ9L8kpQ= From: Paul Elder To: libcamera-devel@lists.libcamera.org Date: Mon, 3 Jun 2019 19:16:31 -0400 Message-Id: <20190603231637.28554-5-paul.elder@ideasonboard.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190603231637.28554-1-paul.elder@ideasonboard.com> References: <20190603231637.28554-1-paul.elder@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 04/10] libcamera: ipa_module: allow instantiation of IPAInterface X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 03 Jun 2019 23:16:50 -0000 Add functions for loading the IPAInterface factory function from an IPA module shared object, and for creating an instance of an IPAInterface. These functions will be used by IPAManager, from which a PipelineHandler will request an IPAInterface. Also update meson to add the "-ldl" linker argument, to allow loading of the factory function from a shared object. Signed-off-by: Paul Elder Reviewed-by: Laurent Pinchart --- Changes in v2: - mostly just documentation src/libcamera/include/ipa_module.h | 12 +++++ src/libcamera/ipa_module.cpp | 83 ++++++++++++++++++++++++++++-- src/libcamera/meson.build | 3 +- 3 files changed, 94 insertions(+), 4 deletions(-) diff --git a/src/libcamera/include/ipa_module.h b/src/libcamera/include/ipa_module.h index a4c6dbd..557435c 100644 --- a/src/libcamera/include/ipa_module.h +++ b/src/libcamera/include/ipa_module.h @@ -7,8 +7,10 @@ #ifndef __LIBCAMERA_IPA_MODULE_H__ #define __LIBCAMERA_IPA_MODULE_H__ +#include #include +#include #include namespace libcamera { @@ -17,16 +19,26 @@ class IPAModule { public: explicit IPAModule(const std::string &libPath); + ~IPAModule(); bool isValid() const; const struct IPAModuleInfo &info() const; + bool load(); + + std::unique_ptr createInstance(); + private: struct IPAModuleInfo info_; std::string libPath_; bool valid_; + bool loaded_; + + void *dlHandle_; + typedef IPAInterface *(*ipaIntfFactory)(void); + ipaIntfFactory ipaCreate_; int loadIPAModuleInfo(); }; diff --git a/src/libcamera/ipa_module.cpp b/src/libcamera/ipa_module.cpp index 067f868..91d97ae 100644 --- a/src/libcamera/ipa_module.cpp +++ b/src/libcamera/ipa_module.cpp @@ -7,6 +7,7 @@ #include "ipa_module.h" +#include #include #include #include @@ -257,13 +258,12 @@ int elfLoadSymbol(void *dst, size_t size, void *map, size_t soSize, * The IPA module shared object file must be of the same endianness and * bitness as libcamera. * - * \todo load funtions from the IPA to be used by pipelines - * * The caller shall call the isValid() method after constructing an * IPAModule instance to verify the validity of the IPAModule. */ IPAModule::IPAModule(const std::string &libPath) - : libPath_(libPath), valid_(false) + : libPath_(libPath), valid_(false), loaded_(false), + dlHandle_(nullptr), ipaCreate_(nullptr) { if (loadIPAModuleInfo() < 0) return; @@ -271,6 +271,12 @@ IPAModule::IPAModule(const std::string &libPath) valid_ = true; } +IPAModule::~IPAModule() +{ + if (dlHandle_) + dlclose(dlHandle_); +} + int IPAModule::loadIPAModuleInfo() { int fd = open(libPath_.c_str(), O_RDONLY); @@ -340,4 +346,75 @@ const struct IPAModuleInfo &IPAModule::info() const return info_; } +/** + * \brief Load the IPA implementation factory from the shared object + * + * The IPA module shared object implements an IPAInterface class to be used + * by pipeline handlers. This function loads the factory function from the + * shared object. Later, createInstance() can be called to instantiate the + * IPAInterface. + * + * This method only needs to be called successfully once, after which + * createInstance can be called as many times as IPAInterface instances are + * needed. + * + * Calling this function on an invalid module (as returned by isValid()) is + * an error. + * + * \return true if load was successful, or already loaded, and false otherwise + */ +bool IPAModule::load() +{ + if (!valid_) + return false; + + if (loaded_) + return true; + + dlHandle_ = dlopen(libPath_.c_str(), RTLD_LAZY); + if (!dlHandle_) { + LOG(IPAModule, Error) + << "Failed to open IPA module shared object " + << dlerror(); + return false; + } + + void *symbol = dlsym(dlHandle_, "ipaCreate"); + if (!symbol) { + LOG(IPAModule, Error) + << "Failed to load ipaCreate() from IPA module shared object " + << dlerror(); + dlclose(dlHandle_); + dlHandle_ = nullptr; + return false; + } + + ipaCreate_ = (ipaIntfFactory)symbol; + + loaded_ = true; + + return true; +} + +/** + * \brief Instantiate an IPAInterface + * + * After loading the IPA module with load(), this method creates an + * instance of the IPA module interface. + * + * Calling this function on a module that has not yet been loaded, or an + * invalid module (as returned by load() and isValid(), respectively) is + * an error. + * + * \return the IPA implementation as a new IPAInterface instance on success, + * or nullptr on error + */ +std::unique_ptr IPAModule::createInstance() +{ + if (!valid_ || !loaded_) + return nullptr; + + return std::unique_ptr(ipaCreate_()); +} + } /* namespace libcamera */ diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build index 07335e5..32f7da4 100644 --- a/src/libcamera/meson.build +++ b/src/libcamera/meson.build @@ -65,7 +65,8 @@ libcamera = shared_library('camera', libcamera_sources, install : true, include_directories : includes, - dependencies : libudev) + dependencies : libudev, + link_args : '-ldl') libcamera_dep = declare_dependency(sources : [libcamera_api, libcamera_h], include_directories : libcamera_includes,