From 1045522af99c9f26e0f583aa2310174f7ff42178 Mon Sep 17 00:00:00 2001
From: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Date: Sat, 30 Sep 2023 16:08:56 +0300
Subject: libcamera: ipa_manager: Remove singleton requirement

The IPAManager class implements a singleton pattern due to the need of
accessing the instance in a static member function. The function now
takes a pointer to a PipelineHandler, which we can use to access the
CameraManager, and from there, the IPAManager.

Add accessors to the internal API to expose the CameraManager from the
PipelineHandler, and the IPAManager from the CameraManager. This
requires allocating the IPAManager dynamically to avoid a loop in
includes. Use those accessors to replace the IPAManager singleton.

Update the IPA interface unit test to instantiate a CameraManager
instead of an IPAManager and ProcessManager, to reflect the new way that
the IPAManager is accessed.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
---
 src/libcamera/camera_manager.cpp   |  9 +++++++++
 src/libcamera/ipa_manager.cpp      | 10 ----------
 src/libcamera/pipeline_handler.cpp |  7 +++++++
 3 files changed, 16 insertions(+), 10 deletions(-)

(limited to 'src')

diff --git a/src/libcamera/camera_manager.cpp b/src/libcamera/camera_manager.cpp
index fa44e33a..c5a7a93a 100644
--- a/src/libcamera/camera_manager.cpp
+++ b/src/libcamera/camera_manager.cpp
@@ -15,6 +15,7 @@
 
 #include "libcamera/internal/camera.h"
 #include "libcamera/internal/device_enumerator.h"
+#include "libcamera/internal/ipa_manager.h"
 #include "libcamera/internal/pipeline_handler.h"
 
 /**
@@ -39,6 +40,7 @@ LOG_DEFINE_CATEGORY(Camera)
 CameraManager::Private::Private()
 	: initialized_(false)
 {
+	ipaManager_ = std::make_unique<IPAManager>();
 }
 
 int CameraManager::Private::start()
@@ -252,6 +254,13 @@ void CameraManager::Private::removeCamera(std::shared_ptr<Camera> camera)
 }
 #endif /* __DOXYGEN_PUBLIC__ */
 
+/**
+ * \fn CameraManager::Private::ipaManager() const
+ * \brief Retrieve the IPAManager
+ * \context This function is \threadsafe.
+ * \return The IPAManager for this CameraManager
+ */
+
 /**
  * \class CameraManager
  * \brief Provide access and manage all cameras in the system
diff --git a/src/libcamera/ipa_manager.cpp b/src/libcamera/ipa_manager.cpp
index f4e0b633..cfc24d38 100644
--- a/src/libcamera/ipa_manager.cpp
+++ b/src/libcamera/ipa_manager.cpp
@@ -95,8 +95,6 @@ LOG_DEFINE_CATEGORY(IPAManager)
  * IPC.
  */
 
-IPAManager *IPAManager::self_ = nullptr;
-
 /**
  * \brief Construct an IPAManager instance
  *
@@ -105,10 +103,6 @@ IPAManager *IPAManager::self_ = nullptr;
  */
 IPAManager::IPAManager()
 {
-	if (self_)
-		LOG(IPAManager, Fatal)
-			<< "Multiple IPAManager objects are not allowed";
-
 #if HAVE_IPA_PUBKEY
 	if (!pubKey_.isValid())
 		LOG(IPAManager, Warning) << "Public key not valid";
@@ -153,16 +147,12 @@ IPAManager::IPAManager()
 	if (!ipaCount)
 		LOG(IPAManager, Warning)
 			<< "No IPA found in '" IPA_MODULE_DIR "'";
-
-	self_ = this;
 }
 
 IPAManager::~IPAManager()
 {
 	for (IPAModule *module : modules_)
 		delete module;
-
-	self_ = nullptr;
 }
 
 /**
diff --git a/src/libcamera/pipeline_handler.cpp b/src/libcamera/pipeline_handler.cpp
index 5ea2ca78..5a6de685 100644
--- a/src/libcamera/pipeline_handler.cpp
+++ b/src/libcamera/pipeline_handler.cpp
@@ -719,6 +719,13 @@ void PipelineHandler::disconnect()
  * \return The pipeline handler name
  */
 
+/**
+ * \fn PipelineHandler::cameraManager() const
+ * \brief Retrieve the CameraManager that this pipeline handler belongs to
+ * \context This function is \threadsafe.
+ * \return The CameraManager for this pipeline handler
+ */
+
 /**
  * \class PipelineHandlerFactoryBase
  * \brief Base class for pipeline handler factories
-- 
cgit v1.2.1