[libcamera-devel,v3,4/8] android: camera_device: Replace running_ with CameraState
diff mbox series

Message ID 20210521154227.60186-5-jacopo@jmondi.org
State Superseded
Headers show
Series
  • Implement flush() camera operation
Related show

Commit Message

Jacopo Mondi May 21, 2021, 3:42 p.m. UTC
The CameraDevice class maintains the camera state in the 'running_'
boolean flag to check if the camera has to be started at the first
received process_capture_request() call which happens after the camera
had been stopped.

So far this was correct, as the operations that change the camera
could only start or stop the camera, so a simple boolean flag
was enough.

To prepare to handle the flush() operation that will introduce a new
'flushing' state, replace the simple plain boolean flag with an
enumeration of values that define the CameraState.

Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>
Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
Reviewed-by: Hirokazu Honda <hiroh@chromium.org>
---
 src/android/camera_device.cpp | 10 +++++-----
 src/android/camera_device.h   |  8 +++++++-
 2 files changed, 12 insertions(+), 6 deletions(-)

Comments

Laurent Pinchart May 23, 2021, 6:02 p.m. UTC | #1
Hi Jacopo,

Thank you for the patch.

On Fri, May 21, 2021 at 05:42:23PM +0200, Jacopo Mondi wrote:
> The CameraDevice class maintains the camera state in the 'running_'
> boolean flag to check if the camera has to be started at the first
> received process_capture_request() call which happens after the camera
> had been stopped.
> 
> So far this was correct, as the operations that change the camera
> could only start or stop the camera, so a simple boolean flag
> was enough.
> 
> To prepare to handle the flush() operation that will introduce a new
> 'flushing' state, replace the simple plain boolean flag with an
> enumeration of values that define the CameraState.
> 
> Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>
> Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
> Reviewed-by: Hirokazu Honda <hiroh@chromium.org>
> ---
>  src/android/camera_device.cpp | 10 +++++-----
>  src/android/camera_device.h   |  8 +++++++-
>  2 files changed, 12 insertions(+), 6 deletions(-)
> 
> diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp
> index bd96b355ea92..cf0e5e459213 100644
> --- a/src/android/camera_device.cpp
> +++ b/src/android/camera_device.cpp
> @@ -402,7 +402,7 @@ CameraDevice::Camera3RequestDescriptor::Camera3RequestDescriptor(
>   */
>  
>  CameraDevice::CameraDevice(unsigned int id, std::shared_ptr<Camera> camera)
> -	: id_(id), running_(false), camera_(std::move(camera)),
> +	: id_(id), state_(CameraStopped), camera_(std::move(camera)),
>  	  facing_(CAMERA_FACING_FRONT), orientation_(0)
>  {
>  	camera_->requestCompleted.connect(this, &CameraDevice::requestComplete);
> @@ -758,14 +758,14 @@ void CameraDevice::close()
>  
>  void CameraDevice::stop()
>  {
> -	if (!running_)
> +	if (state_ == CameraStopped)
>  		return;
>  
>  	worker_.stop();
>  	camera_->stop();
>  
>  	descriptors_.clear();
> -	running_ = false;
> +	state_ = CameraStopped;
>  }
>  
>  void CameraDevice::setCallbacks(const camera3_callback_ops_t *callbacks)
> @@ -1844,7 +1844,7 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques
>  		return -EINVAL;
>  
>  	/* Start the camera if that's the first request we handle. */
> -	if (!running_) {
> +	if (state_ == CameraStopped) {
>  		worker_.start();
>  
>  		int ret = camera_->start();
> @@ -1853,7 +1853,7 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques
>  			return ret;
>  		}
>  
> -		running_ = true;
> +		state_ = CameraRunning;
>  	}
>  
>  	/*
> diff --git a/src/android/camera_device.h b/src/android/camera_device.h
> index a34e8a2cd900..995b423c6deb 100644
> --- a/src/android/camera_device.h
> +++ b/src/android/camera_device.h
> @@ -88,6 +88,11 @@ private:
>  		int androidFormat;
>  	};
>  
> +	enum State {
> +		CameraStopped,
> +		CameraRunning,
> +	};

This is a topic that will require revisiting enums globally, but I think
the code would be clearer with

	enum class State {
		Stopped,
		Running,
	};

You would then need to use State::Stopped and State::Running explicitly
above. It states explicitly the value is a camera state (compared to
CameraRunning and CameraStopped that do so implicitly only), and avoids
using incorrect values for state_ by mistake as the compiler would catch
that with enum class. What do you think ?

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

> +
>  	void stop();
>  
>  	int initializeStreamConfigurations();
> @@ -114,7 +119,8 @@ private:
>  
>  	CameraWorker worker_;
>  
> -	bool running_;
> +	State state_;
> +
>  	std::shared_ptr<libcamera::Camera> camera_;
>  	std::unique_ptr<libcamera::CameraConfiguration> config_;
>

Patch
diff mbox series

diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp
index bd96b355ea92..cf0e5e459213 100644
--- a/src/android/camera_device.cpp
+++ b/src/android/camera_device.cpp
@@ -402,7 +402,7 @@  CameraDevice::Camera3RequestDescriptor::Camera3RequestDescriptor(
  */
 
 CameraDevice::CameraDevice(unsigned int id, std::shared_ptr<Camera> camera)
-	: id_(id), running_(false), camera_(std::move(camera)),
+	: id_(id), state_(CameraStopped), camera_(std::move(camera)),
 	  facing_(CAMERA_FACING_FRONT), orientation_(0)
 {
 	camera_->requestCompleted.connect(this, &CameraDevice::requestComplete);
@@ -758,14 +758,14 @@  void CameraDevice::close()
 
 void CameraDevice::stop()
 {
-	if (!running_)
+	if (state_ == CameraStopped)
 		return;
 
 	worker_.stop();
 	camera_->stop();
 
 	descriptors_.clear();
-	running_ = false;
+	state_ = CameraStopped;
 }
 
 void CameraDevice::setCallbacks(const camera3_callback_ops_t *callbacks)
@@ -1844,7 +1844,7 @@  int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques
 		return -EINVAL;
 
 	/* Start the camera if that's the first request we handle. */
-	if (!running_) {
+	if (state_ == CameraStopped) {
 		worker_.start();
 
 		int ret = camera_->start();
@@ -1853,7 +1853,7 @@  int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques
 			return ret;
 		}
 
-		running_ = true;
+		state_ = CameraRunning;
 	}
 
 	/*
diff --git a/src/android/camera_device.h b/src/android/camera_device.h
index a34e8a2cd900..995b423c6deb 100644
--- a/src/android/camera_device.h
+++ b/src/android/camera_device.h
@@ -88,6 +88,11 @@  private:
 		int androidFormat;
 	};
 
+	enum State {
+		CameraStopped,
+		CameraRunning,
+	};
+
 	void stop();
 
 	int initializeStreamConfigurations();
@@ -114,7 +119,8 @@  private:
 
 	CameraWorker worker_;
 
-	bool running_;
+	State state_;
+
 	std::shared_ptr<libcamera::Camera> camera_;
 	std::unique_ptr<libcamera::CameraConfiguration> config_;