[libcamera-devel,v2,2/8] android: CameraHalManager: Hold CameraManager with std::unique_ptr
diff mbox series

Message ID 20210324070757.3530377-3-hiroh@chromium.org
State Accepted
Headers show
Series
  • Improve pointer types in android HAL adaptation layer
Related show

Commit Message

Hirokazu Honda March 24, 2021, 7:07 a.m. UTC
CameraManager is owned by CameraHalManager. The ownership of the
object is not shared with other classes. So CameraHalManager
should manage CameraManager with std::unique_ptr.

Signed-off-by: Hirokazu Honda <hiroh@chromium.org>
---
 src/android/camera_hal_manager.cpp | 15 ++++-----------
 src/android/camera_hal_manager.h   |  2 +-
 2 files changed, 5 insertions(+), 12 deletions(-)

--
2.31.0.291.g576ba9dcdaf-goog

Comments

Laurent Pinchart March 24, 2021, 8:46 p.m. UTC | #1
Hi Hiro,

Thank you for the patch.

On Wed, Mar 24, 2021 at 04:07:51PM +0900, Hirokazu Honda wrote:
> CameraManager is owned by CameraHalManager. The ownership of the
> object is not shared with other classes. So CameraHalManager
> should manage CameraManager with std::unique_ptr.
> 
> Signed-off-by: Hirokazu Honda <hiroh@chromium.org>

Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

> ---
>  src/android/camera_hal_manager.cpp | 15 ++++-----------
>  src/android/camera_hal_manager.h   |  2 +-
>  2 files changed, 5 insertions(+), 12 deletions(-)
> 
> diff --git a/src/android/camera_hal_manager.cpp b/src/android/camera_hal_manager.cpp
> index b3c85406..b501f8db 100644
> --- a/src/android/camera_hal_manager.cpp
> +++ b/src/android/camera_hal_manager.cpp
> @@ -34,18 +34,12 @@ CameraHalManager::CameraHalManager()
>  {
>  }
> 
> -CameraHalManager::~CameraHalManager()
> -{
> -	if (cameraManager_) {
> -		cameraManager_->stop();
> -		delete cameraManager_;
> -		cameraManager_ = nullptr;
> -	}
> -}
> +/* CameraManager calls stop() in the destructor. */
> +CameraHalManager::~CameraHalManager() = default;
> 
>  int CameraHalManager::init()
>  {
> -	cameraManager_ = new CameraManager();
> +	cameraManager_ = std::make_unique<CameraManager>();
> 
>  	/* Support camera hotplug. */
>  	cameraManager_->cameraAdded.connect(this, &CameraHalManager::cameraAdded);
> @@ -55,8 +49,7 @@ int CameraHalManager::init()
>  	if (ret) {
>  		LOG(HAL, Error) << "Failed to start camera manager: "
>  				<< strerror(-ret);
> -		delete cameraManager_;
> -		cameraManager_ = nullptr;
> +		cameraManager_.reset();
>  		return ret;
>  	}
> 
> diff --git a/src/android/camera_hal_manager.h b/src/android/camera_hal_manager.h
> index 65bb3554..dc4e37e5 100644
> --- a/src/android/camera_hal_manager.h
> +++ b/src/android/camera_hal_manager.h
> @@ -47,7 +47,7 @@ private:
> 
>  	CameraDevice *cameraDeviceFromHalId(unsigned int id);
> 
> -	libcamera::CameraManager *cameraManager_;
> +	std::unique_ptr<libcamera::CameraManager> cameraManager_;
> 
>  	const camera_module_callbacks_t *callbacks_;
>  	std::vector<std::unique_ptr<CameraDevice>> cameras_;
Jacopo Mondi March 25, 2021, 8:36 a.m. UTC | #2
Hi Hiro,

On Wed, Mar 24, 2021 at 04:07:51PM +0900, Hirokazu Honda wrote:
> CameraManager is owned by CameraHalManager. The ownership of the
> object is not shared with other classes. So CameraHalManager
> should manage CameraManager with std::unique_ptr.
>
> Signed-off-by: Hirokazu Honda <hiroh@chromium.org>

Thanks
Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>


> ---
>  src/android/camera_hal_manager.cpp | 15 ++++-----------
>  src/android/camera_hal_manager.h   |  2 +-
>  2 files changed, 5 insertions(+), 12 deletions(-)
>
> diff --git a/src/android/camera_hal_manager.cpp b/src/android/camera_hal_manager.cpp
> index b3c85406..b501f8db 100644
> --- a/src/android/camera_hal_manager.cpp
> +++ b/src/android/camera_hal_manager.cpp
> @@ -34,18 +34,12 @@ CameraHalManager::CameraHalManager()
>  {
>  }
>
> -CameraHalManager::~CameraHalManager()
> -{
> -	if (cameraManager_) {
> -		cameraManager_->stop();
> -		delete cameraManager_;
> -		cameraManager_ = nullptr;
> -	}
> -}
> +/* CameraManager calls stop() in the destructor. */
> +CameraHalManager::~CameraHalManager() = default;
>
>  int CameraHalManager::init()
>  {
> -	cameraManager_ = new CameraManager();
> +	cameraManager_ = std::make_unique<CameraManager>();
>
>  	/* Support camera hotplug. */
>  	cameraManager_->cameraAdded.connect(this, &CameraHalManager::cameraAdded);
> @@ -55,8 +49,7 @@ int CameraHalManager::init()
>  	if (ret) {
>  		LOG(HAL, Error) << "Failed to start camera manager: "
>  				<< strerror(-ret);
> -		delete cameraManager_;
> -		cameraManager_ = nullptr;
> +		cameraManager_.reset();
>  		return ret;
>  	}
>
> diff --git a/src/android/camera_hal_manager.h b/src/android/camera_hal_manager.h
> index 65bb3554..dc4e37e5 100644
> --- a/src/android/camera_hal_manager.h
> +++ b/src/android/camera_hal_manager.h
> @@ -47,7 +47,7 @@ private:
>
>  	CameraDevice *cameraDeviceFromHalId(unsigned int id);
>
> -	libcamera::CameraManager *cameraManager_;
> +	std::unique_ptr<libcamera::CameraManager> cameraManager_;
>
>  	const camera_module_callbacks_t *callbacks_;
>  	std::vector<std::unique_ptr<CameraDevice>> cameras_;
> --
> 2.31.0.291.g576ba9dcdaf-goog
> _______________________________________________
> libcamera-devel mailing list
> libcamera-devel@lists.libcamera.org
> https://lists.libcamera.org/listinfo/libcamera-devel

Patch
diff mbox series

diff --git a/src/android/camera_hal_manager.cpp b/src/android/camera_hal_manager.cpp
index b3c85406..b501f8db 100644
--- a/src/android/camera_hal_manager.cpp
+++ b/src/android/camera_hal_manager.cpp
@@ -34,18 +34,12 @@  CameraHalManager::CameraHalManager()
 {
 }

-CameraHalManager::~CameraHalManager()
-{
-	if (cameraManager_) {
-		cameraManager_->stop();
-		delete cameraManager_;
-		cameraManager_ = nullptr;
-	}
-}
+/* CameraManager calls stop() in the destructor. */
+CameraHalManager::~CameraHalManager() = default;

 int CameraHalManager::init()
 {
-	cameraManager_ = new CameraManager();
+	cameraManager_ = std::make_unique<CameraManager>();

 	/* Support camera hotplug. */
 	cameraManager_->cameraAdded.connect(this, &CameraHalManager::cameraAdded);
@@ -55,8 +49,7 @@  int CameraHalManager::init()
 	if (ret) {
 		LOG(HAL, Error) << "Failed to start camera manager: "
 				<< strerror(-ret);
-		delete cameraManager_;
-		cameraManager_ = nullptr;
+		cameraManager_.reset();
 		return ret;
 	}

diff --git a/src/android/camera_hal_manager.h b/src/android/camera_hal_manager.h
index 65bb3554..dc4e37e5 100644
--- a/src/android/camera_hal_manager.h
+++ b/src/android/camera_hal_manager.h
@@ -47,7 +47,7 @@  private:

 	CameraDevice *cameraDeviceFromHalId(unsigned int id);

-	libcamera::CameraManager *cameraManager_;
+	std::unique_ptr<libcamera::CameraManager> cameraManager_;

 	const camera_module_callbacks_t *callbacks_;
 	std::vector<std::unique_ptr<CameraDevice>> cameras_;