[libcamera-devel,v4,4/6] libcamera: camera_sensor: Break out properties initialization

Message ID 20200326145927.324919-5-jacopo@jmondi.org
State Accepted
Delegated to: Jacopo Mondi
Headers show
Series
  • Camera properties and camera sensor factory
Related show

Commit Message

Jacopo Mondi March 26, 2020, 2:59 p.m. UTC
Refactor the CameraSensor properties initialization to a dedicated virtual
function that derived classes could override to register sensor-specific
property values.

While at it, move documentation of the properties() method to match the
declaration order in the class definition and make the properties_ and
subdev_ fields protected to allow subclasses access.

Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>
---
 src/libcamera/camera_sensor.cpp       | 115 +++++++++++++++-----------
 src/libcamera/include/camera_sensor.h |   7 +-
 2 files changed, 73 insertions(+), 49 deletions(-)

Comments

Niklas Söderlund April 7, 2020, 11:05 p.m. UTC | #1
Hi Jacopo,

Thanks for your work.

On 2020-03-26 15:59:25 +0100, Jacopo Mondi wrote:
> Refactor the CameraSensor properties initialization to a dedicated virtual
> function that derived classes could override to register sensor-specific
> property values.
> 
> While at it, move documentation of the properties() method to match the
> declaration order in the class definition and make the properties_ and
> subdev_ fields protected to allow subclasses access.
> 
> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
> Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>

Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>

> ---
>  src/libcamera/camera_sensor.cpp       | 115 +++++++++++++++-----------
>  src/libcamera/include/camera_sensor.h |   7 +-
>  2 files changed, 73 insertions(+), 49 deletions(-)
> 
> diff --git a/src/libcamera/camera_sensor.cpp b/src/libcamera/camera_sensor.cpp
> index c1a29e0b3cfc..09771fc40bbb 100644
> --- a/src/libcamera/camera_sensor.cpp
> +++ b/src/libcamera/camera_sensor.cpp
> @@ -51,7 +51,7 @@ LOG_DEFINE_CATEGORY(CameraSensor);
>   * Once constructed the instance must be initialized with init().
>   */
>  CameraSensor::CameraSensor(const MediaEntity *entity)
> -	: entity_(entity), properties_(properties::properties)
> +	: properties_(properties::properties), entity_(entity)
>  {
>  	subdev_ = new V4L2Subdevice(entity);
>  }
> @@ -93,45 +93,6 @@ int CameraSensor::init()
>  	if (ret < 0)
>  		return ret;
>  
> -	/* Retrieve and store the camera sensor properties. */
> -	const ControlInfoMap &controls = subdev_->controls();
> -	int32_t propertyValue;
> -
> -	/* Camera Location: default is front location. */
> -	const auto &locationControl = controls.find(V4L2_CID_CAMERA_SENSOR_LOCATION);
> -	if (locationControl != controls.end()) {
> -		int32_t v4l2Location =
> -			locationControl->second.def().get<int32_t>();
> -
> -		switch (v4l2Location) {
> -		default:
> -			LOG(CameraSensor, Warning)
> -				<< "Unsupported camera location "
> -				<< v4l2Location << ", setting to Front";
> -			/* Fall-through */
> -		case V4L2_LOCATION_FRONT:
> -			propertyValue = properties::CameraLocationFront;
> -			break;
> -		case V4L2_LOCATION_BACK:
> -			propertyValue = properties::CameraLocationBack;
> -			break;
> -		case V4L2_LOCATION_EXTERNAL:
> -			propertyValue = properties::CameraLocationExternal;
> -			break;
> -		}
> -	} else {
> -		propertyValue = properties::CameraLocationFront;
> -	}
> -	properties_.set(properties::Location, propertyValue);
> -
> -	/* Camera Rotation: default is 0 degrees. */
> -	const auto &rotationControl = controls.find(V4L2_CID_CAMERA_SENSOR_ROTATION);
> -	if (rotationControl != controls.end())
> -		propertyValue = rotationControl->second.def().get<int32_t>();
> -	else
> -		propertyValue = 0;
> -	properties_.set(properties::Rotation, propertyValue);
> -
>  	/* Enumerate and cache media bus codes and sizes. */
>  	const ImageFormats formats = subdev_->formats(0);
>  	if (formats.isEmpty()) {
> @@ -162,6 +123,58 @@ int CameraSensor::init()
>  	std::sort(mbusCodes_.begin(), mbusCodes_.end());
>  	std::sort(sizes_.begin(), sizes_.end());
>  
> +	return initProperties();
> +}
> +
> +/**
> + * \brief Initialize the camera sensor standard properties
> + *
> + * This method initializes the camera sensor standard properties, by inspecting
> + * the control information reported by the sensor subdevice.
> + *
> + * Derived classes may override this method to register sensor-specific
> + * properties if needed. If they do so, they shall call the base
> + * initProperties() method to register standard properties.
> + *
> + * \return 0 on success, a negative error code otherwise
> + */
> +int CameraSensor::initProperties()
> +{
> +	const ControlInfoMap &controlMap = subdev_->controls();
> +	int32_t propertyValue;
> +
> +	/* Camera Location: default is front location. */
> +	const auto &locationControl = controlMap.find(V4L2_CID_CAMERA_SENSOR_LOCATION);
> +	if (locationControl != controlMap.end()) {
> +		int32_t v4l2Location =
> +			locationControl->second.def().get<int32_t>();
> +		switch (v4l2Location) {
> +		case V4L2_LOCATION_EXTERNAL:
> +			propertyValue = properties::CameraLocationExternal;
> +			break;
> +		case V4L2_LOCATION_FRONT:
> +			propertyValue = properties::CameraLocationFront;
> +			break;
> +		case V4L2_LOCATION_BACK:
> +			propertyValue = properties::CameraLocationBack;
> +			break;
> +		default:
> +			LOG(CameraSensor, Error)
> +				<< "Unsupported camera location: " << v4l2Location;
> +			return -EINVAL;
> +		}
> +	} else {
> +		propertyValue = properties::CameraLocationFront;
> +	}
> +	properties_.set(properties::Location, propertyValue);
> +
> +	/* Camera Rotation: default is 0 degrees. */
> +	propertyValue = 0;
> +	const auto &rotationControl = controlMap.find(V4L2_CID_CAMERA_SENSOR_ROTATION);
> +	if (rotationControl != controlMap.end())
> +		propertyValue = rotationControl->second.def().get<int32_t>();
> +	properties_.set(properties::Rotation, propertyValue);
> +
>  	return 0;
>  }
>  
> @@ -327,12 +340,6 @@ int CameraSensor::getControls(ControlList *ctrls)
>  	return subdev_->getControls(ctrls);
>  }
>  
> -/**
> - * \fn CameraSensor::properties()
> - * \brief Retrieve the camera sensor properties
> - * \return The list of camera sensor properties
> - */
> -
>  /**
>   * \brief Write controls to the sensor
>   * \param[in] ctrls The list of controls to write
> @@ -363,11 +370,27 @@ int CameraSensor::setControls(ControlList *ctrls)
>  	return subdev_->setControls(ctrls);
>  }
>  
> +/**
> + * \fn CameraSensor::properties()
> + * \brief Retrieve the camera sensor properties
> + * \return The list of camera sensor properties
> + */
> +
>  std::string CameraSensor::logPrefix() const
>  {
>  	return "'" + subdev_->entity()->name() + "'";
>  }
>  
> +/**
> + * \var CameraSensor::properties_
> + * \brief The list of camera sensor properties
> + */
> +
> +/**
> + * \var CameraSensor::subdev_
> + * \brief The camera sensor V4L2 subdevice
> + */
> +
>  /**
>   * \class CameraSensorFactory
>   * \brief Factory of camera sensors
> diff --git a/src/libcamera/include/camera_sensor.h b/src/libcamera/include/camera_sensor.h
> index 8ffc787e740f..6e4d2b0118bc 100644
> --- a/src/libcamera/include/camera_sensor.h
> +++ b/src/libcamera/include/camera_sensor.h
> @@ -34,6 +34,7 @@ public:
>  	CameraSensor &operator=(const CameraSensor &) = delete;
>  
>  	int init();
> +	virtual int initProperties();
>  
>  	const MediaEntity *entity() const { return entity_; }
>  	const std::vector<unsigned int> &mbusCodes() const { return mbusCodes_; }
> @@ -53,14 +54,14 @@ public:
>  protected:
>  	std::string logPrefix() const;
>  
> +	ControlList properties_;
> +	V4L2Subdevice *subdev_;
> +
>  private:
>  	const MediaEntity *entity_;
> -	V4L2Subdevice *subdev_;
>  
>  	std::vector<unsigned int> mbusCodes_;
>  	std::vector<Size> sizes_;
> -
> -	ControlList properties_;
>  };
>  
>  class CameraSensorFactory
> -- 
> 2.25.1
> 
> _______________________________________________
> libcamera-devel mailing list
> libcamera-devel@lists.libcamera.org
> https://lists.libcamera.org/listinfo/libcamera-devel

Patch

diff --git a/src/libcamera/camera_sensor.cpp b/src/libcamera/camera_sensor.cpp
index c1a29e0b3cfc..09771fc40bbb 100644
--- a/src/libcamera/camera_sensor.cpp
+++ b/src/libcamera/camera_sensor.cpp
@@ -51,7 +51,7 @@  LOG_DEFINE_CATEGORY(CameraSensor);
  * Once constructed the instance must be initialized with init().
  */
 CameraSensor::CameraSensor(const MediaEntity *entity)
-	: entity_(entity), properties_(properties::properties)
+	: properties_(properties::properties), entity_(entity)
 {
 	subdev_ = new V4L2Subdevice(entity);
 }
@@ -93,45 +93,6 @@  int CameraSensor::init()
 	if (ret < 0)
 		return ret;
 
-	/* Retrieve and store the camera sensor properties. */
-	const ControlInfoMap &controls = subdev_->controls();
-	int32_t propertyValue;
-
-	/* Camera Location: default is front location. */
-	const auto &locationControl = controls.find(V4L2_CID_CAMERA_SENSOR_LOCATION);
-	if (locationControl != controls.end()) {
-		int32_t v4l2Location =
-			locationControl->second.def().get<int32_t>();
-
-		switch (v4l2Location) {
-		default:
-			LOG(CameraSensor, Warning)
-				<< "Unsupported camera location "
-				<< v4l2Location << ", setting to Front";
-			/* Fall-through */
-		case V4L2_LOCATION_FRONT:
-			propertyValue = properties::CameraLocationFront;
-			break;
-		case V4L2_LOCATION_BACK:
-			propertyValue = properties::CameraLocationBack;
-			break;
-		case V4L2_LOCATION_EXTERNAL:
-			propertyValue = properties::CameraLocationExternal;
-			break;
-		}
-	} else {
-		propertyValue = properties::CameraLocationFront;
-	}
-	properties_.set(properties::Location, propertyValue);
-
-	/* Camera Rotation: default is 0 degrees. */
-	const auto &rotationControl = controls.find(V4L2_CID_CAMERA_SENSOR_ROTATION);
-	if (rotationControl != controls.end())
-		propertyValue = rotationControl->second.def().get<int32_t>();
-	else
-		propertyValue = 0;
-	properties_.set(properties::Rotation, propertyValue);
-
 	/* Enumerate and cache media bus codes and sizes. */
 	const ImageFormats formats = subdev_->formats(0);
 	if (formats.isEmpty()) {
@@ -162,6 +123,58 @@  int CameraSensor::init()
 	std::sort(mbusCodes_.begin(), mbusCodes_.end());
 	std::sort(sizes_.begin(), sizes_.end());
 
+	return initProperties();
+}
+
+/**
+ * \brief Initialize the camera sensor standard properties
+ *
+ * This method initializes the camera sensor standard properties, by inspecting
+ * the control information reported by the sensor subdevice.
+ *
+ * Derived classes may override this method to register sensor-specific
+ * properties if needed. If they do so, they shall call the base
+ * initProperties() method to register standard properties.
+ *
+ * \return 0 on success, a negative error code otherwise
+ */
+int CameraSensor::initProperties()
+{
+	const ControlInfoMap &controlMap = subdev_->controls();
+	int32_t propertyValue;
+
+	/* Camera Location: default is front location. */
+	const auto &locationControl = controlMap.find(V4L2_CID_CAMERA_SENSOR_LOCATION);
+	if (locationControl != controlMap.end()) {
+		int32_t v4l2Location =
+			locationControl->second.def().get<int32_t>();
+		switch (v4l2Location) {
+		case V4L2_LOCATION_EXTERNAL:
+			propertyValue = properties::CameraLocationExternal;
+			break;
+		case V4L2_LOCATION_FRONT:
+			propertyValue = properties::CameraLocationFront;
+			break;
+		case V4L2_LOCATION_BACK:
+			propertyValue = properties::CameraLocationBack;
+			break;
+		default:
+			LOG(CameraSensor, Error)
+				<< "Unsupported camera location: " << v4l2Location;
+			return -EINVAL;
+		}
+	} else {
+		propertyValue = properties::CameraLocationFront;
+	}
+	properties_.set(properties::Location, propertyValue);
+
+	/* Camera Rotation: default is 0 degrees. */
+	propertyValue = 0;
+	const auto &rotationControl = controlMap.find(V4L2_CID_CAMERA_SENSOR_ROTATION);
+	if (rotationControl != controlMap.end())
+		propertyValue = rotationControl->second.def().get<int32_t>();
+	properties_.set(properties::Rotation, propertyValue);
+
 	return 0;
 }
 
@@ -327,12 +340,6 @@  int CameraSensor::getControls(ControlList *ctrls)
 	return subdev_->getControls(ctrls);
 }
 
-/**
- * \fn CameraSensor::properties()
- * \brief Retrieve the camera sensor properties
- * \return The list of camera sensor properties
- */
-
 /**
  * \brief Write controls to the sensor
  * \param[in] ctrls The list of controls to write
@@ -363,11 +370,27 @@  int CameraSensor::setControls(ControlList *ctrls)
 	return subdev_->setControls(ctrls);
 }
 
+/**
+ * \fn CameraSensor::properties()
+ * \brief Retrieve the camera sensor properties
+ * \return The list of camera sensor properties
+ */
+
 std::string CameraSensor::logPrefix() const
 {
 	return "'" + subdev_->entity()->name() + "'";
 }
 
+/**
+ * \var CameraSensor::properties_
+ * \brief The list of camera sensor properties
+ */
+
+/**
+ * \var CameraSensor::subdev_
+ * \brief The camera sensor V4L2 subdevice
+ */
+
 /**
  * \class CameraSensorFactory
  * \brief Factory of camera sensors
diff --git a/src/libcamera/include/camera_sensor.h b/src/libcamera/include/camera_sensor.h
index 8ffc787e740f..6e4d2b0118bc 100644
--- a/src/libcamera/include/camera_sensor.h
+++ b/src/libcamera/include/camera_sensor.h
@@ -34,6 +34,7 @@  public:
 	CameraSensor &operator=(const CameraSensor &) = delete;
 
 	int init();
+	virtual int initProperties();
 
 	const MediaEntity *entity() const { return entity_; }
 	const std::vector<unsigned int> &mbusCodes() const { return mbusCodes_; }
@@ -53,14 +54,14 @@  public:
 protected:
 	std::string logPrefix() const;
 
+	ControlList properties_;
+	V4L2Subdevice *subdev_;
+
 private:
 	const MediaEntity *entity_;
-	V4L2Subdevice *subdev_;
 
 	std::vector<unsigned int> mbusCodes_;
 	std::vector<Size> sizes_;
-
-	ControlList properties_;
 };
 
 class CameraSensorFactory