[libcamera-devel,v2] libcamera: rkisp1: Use parametered constructor instead of default

Message ID 20200321183246.GA14651@kaaira-HP-Pavilion-Notebook
State Superseded
Headers show
Series
  • [libcamera-devel,v2] libcamera: rkisp1: Use parametered constructor instead of default
Related show

Commit Message

Kaaira Gupta March 21, 2020, 6:32 p.m. UTC
Use of default constructor StreamConfiguration() in deprecated, hence
make it private. Make Stream class a friend of StreamConfiguration as it
uses the default constructor.

Replace default constructor StreamConfiguration() by it's parametrized
counterpart by using StreamFormats in generateConfiguration() in rkisp1

Also, use erase(), instead of replace() in validate() to prevent it from
creating a new instance with empty constructor.

Signed-off-by: Kaaira Gupta <kgupta@es.iitr.ac.in>
---
Changes since v1:
	- replace resize() by erase() in validate() to prevent it from
creating a new instance with empty constructor.

WIP:
        - It fails to build as other pipelines still use default
          constructor.

 include/libcamera/stream.h               |  4 ++-
 src/libcamera/pipeline/rkisp1/rkisp1.cpp | 39 ++++++++++++++++--------
 src/libcamera/stream.cpp                 |  9 ------
 3 files changed, 29 insertions(+), 23 deletions(-)

Comments

Jacopo Mondi March 21, 2020, 7:23 p.m. UTC | #1
Hi Kaaira,

On Sun, Mar 22, 2020 at 12:02:46AM +0530, Kaaira Gupta wrote:
> Use of default constructor StreamConfiguration() in deprecated, hence
> make it private. Make Stream class a friend of StreamConfiguration as it
> uses the default constructor.
>
> Replace default constructor StreamConfiguration() by it's parametrized
> counterpart by using StreamFormats in generateConfiguration() in rkisp1
>
> Also, use erase(), instead of replace() in validate() to prevent it from
> creating a new instance with empty constructor.
>
> Signed-off-by: Kaaira Gupta <kgupta@es.iitr.ac.in>
> ---
> Changes since v1:
> 	- replace resize() by erase() in validate() to prevent it from
> creating a new instance with empty constructor.
>
> WIP:
>         - It fails to build as other pipelines still use default
>           constructor.
>
>  include/libcamera/stream.h               |  4 ++-
>  src/libcamera/pipeline/rkisp1/rkisp1.cpp | 39 ++++++++++++++++--------
>  src/libcamera/stream.cpp                 |  9 ------
>  3 files changed, 29 insertions(+), 23 deletions(-)
>
> diff --git a/include/libcamera/stream.h b/include/libcamera/stream.h
> index b1441f8..c19aed6 100644
> --- a/include/libcamera/stream.h
> +++ b/include/libcamera/stream.h
> @@ -37,7 +37,6 @@ private:
>  };
>
>  struct StreamConfiguration {
> -	StreamConfiguration();
>  	StreamConfiguration(const StreamFormats &formats);
>
>  	PixelFormat pixelFormat;
> @@ -52,8 +51,11 @@ struct StreamConfiguration {
>  	std::string toString() const;
>
>  private:
> +	StreamConfiguration();

Empty line between method and class data member.

>  	Stream *stream_;
>  	StreamFormats formats_;
> +
> +	friend class Stream;

In most cases (that's bad as the library style is not consistent)
friends comes after the visibility modifier or before the method it
needs to access. In this case the constructor is the first method
declared after private, so friend should come between the two.

private:
        friend class Stream;
        StreamConfiguration()

        Stream *stream_;
        StreamFormats formats_;

>  };
>
>  enum StreamRole {
> diff --git a/src/libcamera/pipeline/rkisp1/rkisp1.cpp b/src/libcamera/pipeline/rkisp1/rkisp1.cpp
> index 2f909ce..8e8414e 100644
> --- a/src/libcamera/pipeline/rkisp1/rkisp1.cpp
> +++ b/src/libcamera/pipeline/rkisp1/rkisp1.cpp
> @@ -218,6 +218,21 @@ private:
>  	Camera *activeCamera_;
>  };
>
> +namespace {
> +
> +static const std::array<PixelFormat, 8> formats{
> +	PixelFormat(DRM_FORMAT_YUYV),
> +	PixelFormat(DRM_FORMAT_YVYU),
> +	PixelFormat(DRM_FORMAT_VYUY),
> +	PixelFormat(DRM_FORMAT_NV16),
> +	PixelFormat(DRM_FORMAT_NV61),
> +	PixelFormat(DRM_FORMAT_NV21),
> +	PixelFormat(DRM_FORMAT_NV12),
> +	/* \todo Add support for 8-bit greyscale to DRM formats */
> +};
> +
> +} /* namespace */
> +
>  RkISP1Frames::RkISP1Frames(PipelineHandler *pipe)
>  	: pipe_(dynamic_cast<PipelineHandlerRkISP1 *>(pipe))
>  {
> @@ -430,17 +445,6 @@ RkISP1CameraConfiguration::RkISP1CameraConfiguration(Camera *camera,
>
>  CameraConfiguration::Status RkISP1CameraConfiguration::validate()
>  {
> -	static const std::array<PixelFormat, 8> formats{
> -		PixelFormat(DRM_FORMAT_YUYV),
> -		PixelFormat(DRM_FORMAT_YVYU),
> -		PixelFormat(DRM_FORMAT_VYUY),
> -		PixelFormat(DRM_FORMAT_NV16),
> -		PixelFormat(DRM_FORMAT_NV61),
> -		PixelFormat(DRM_FORMAT_NV21),
> -		PixelFormat(DRM_FORMAT_NV12),
> -		/* \todo Add support for 8-bit greyscale to DRM formats */
> -	};
> -
>  	const CameraSensor *sensor = data_->sensor_;
>  	Status status = Valid;
>
> @@ -449,7 +453,7 @@ CameraConfiguration::Status RkISP1CameraConfiguration::validate()
>
>  	/* Cap the number of entries to the available streams. */
>  	if (config_.size() > 1) {
> -		config_.resize(1);
> +		config_.erase(config_.begin() + 1, config_.end() - 1);

Just config.end(), I bet STL containers handles being() and end()
in the right way.

>  		status = Adjusted;
>  	}
>
> @@ -537,7 +541,16 @@ CameraConfiguration *PipelineHandlerRkISP1::generateConfiguration(Camera *camera
>  	if (roles.empty())
>  		return config;
>
> -	StreamConfiguration cfg{};
> +	std::map<PixelFormat, std::vector<SizeRange>> pixelformats;
> +
> +	for (PixelFormat pixelformat : formats) {
> +		std::vector<SizeRange> sizes{
> +			SizeRange{ { 32, 16 }, { 4416, 3312 } }
> +		};
> +		pixelformats[pixelformat] = sizes;
> +	}
> +	StreamFormats format(pixelformats);
> +	StreamConfiguration cfg(format);

This can be later optimized on top. I wonder, in example what going through
a StreamFormats gives us. Not on this patch, it was there already.

>  	cfg.pixelFormat = PixelFormat(DRM_FORMAT_NV12);
>  	cfg.size = data->sensor_->resolution();
>
> diff --git a/src/libcamera/stream.cpp b/src/libcamera/stream.cpp
> index ea3d214..96b3dc5 100644
> --- a/src/libcamera/stream.cpp
> +++ b/src/libcamera/stream.cpp
> @@ -274,15 +274,6 @@ SizeRange StreamFormats::range(const PixelFormat &pixelformat) const
>   * configured for a single video stream.
>   */
>
> -/**
> - * \todo This method is deprecated and should be removed once all pipeline
> - * handlers provied StreamFormats.
> - */
> -StreamConfiguration::StreamConfiguration()
> -	: pixelFormat(0), stream_(nullptr)
> -{
> -}
> -

Nope, as reported, removing the constructor implementation will give
you linkage errors. Even if private, it has to be defined somewhere.

To try if at least this pipeline handlers build right, you can remove
from src/libcamera/pipeline/meson.build the other pipeline handlers
(and disable tests as well, as you will have an error there too)

Thanks
  j

>  /**
>   * \brief Construct a configuration with stream formats
>   */
> --
> 2.17.1
>
Kaaira Gupta March 21, 2020, 9:35 p.m. UTC | #2
On Sat, Mar 21, 2020 at 08:23:16PM +0100, Jacopo Mondi wrote:
> Hi Kaaira,
> 
> On Sun, Mar 22, 2020 at 12:02:46AM +0530, Kaaira Gupta wrote:
> > Use of default constructor StreamConfiguration() in deprecated, hence
> > make it private. Make Stream class a friend of StreamConfiguration as it
> > uses the default constructor.
> >
> > Replace default constructor StreamConfiguration() by it's parametrized
> > counterpart by using StreamFormats in generateConfiguration() in rkisp1
> >
> > Also, use erase(), instead of replace() in validate() to prevent it from
> > creating a new instance with empty constructor.
> >
> > Signed-off-by: Kaaira Gupta <kgupta@es.iitr.ac.in>
> > ---
> > Changes since v1:
> > 	- replace resize() by erase() in validate() to prevent it from
> > creating a new instance with empty constructor.
> >
> > WIP:
> >         - It fails to build as other pipelines still use default
> >           constructor.
> >
> >  include/libcamera/stream.h               |  4 ++-
> >  src/libcamera/pipeline/rkisp1/rkisp1.cpp | 39 ++++++++++++++++--------
> >  src/libcamera/stream.cpp                 |  9 ------
> >  3 files changed, 29 insertions(+), 23 deletions(-)
> >
> > diff --git a/include/libcamera/stream.h b/include/libcamera/stream.h
> > index b1441f8..c19aed6 100644
> > --- a/include/libcamera/stream.h
> > +++ b/include/libcamera/stream.h
> > @@ -37,7 +37,6 @@ private:
> >  };
> >
> >  struct StreamConfiguration {
> > -	StreamConfiguration();
> >  	StreamConfiguration(const StreamFormats &formats);
> >
> >  	PixelFormat pixelFormat;
> > @@ -52,8 +51,11 @@ struct StreamConfiguration {
> >  	std::string toString() const;
> >
> >  private:
> > +	StreamConfiguration();
> 
> Empty line between method and class data member.
> 
> >  	Stream *stream_;
> >  	StreamFormats formats_;
> > +
> > +	friend class Stream;
> 
> In most cases (that's bad as the library style is not consistent)
> friends comes after the visibility modifier or before the method it
> needs to access. In this case the constructor is the first method
> declared after private, so friend should come between the two.

Thanks, I'll fix this up.

> 
> private:
>         friend class Stream;
>         StreamConfiguration()
> 
>         Stream *stream_;
>         StreamFormats formats_;
> 
> >  };
> >
> >  enum StreamRole {
> > diff --git a/src/libcamera/pipeline/rkisp1/rkisp1.cpp b/src/libcamera/pipeline/rkisp1/rkisp1.cpp
> > index 2f909ce..8e8414e 100644
> > --- a/src/libcamera/pipeline/rkisp1/rkisp1.cpp
> > +++ b/src/libcamera/pipeline/rkisp1/rkisp1.cpp
> > @@ -218,6 +218,21 @@ private:
> >  	Camera *activeCamera_;
> >  };
> >
> > +namespace {
> > +
> > +static const std::array<PixelFormat, 8> formats{
> > +	PixelFormat(DRM_FORMAT_YUYV),
> > +	PixelFormat(DRM_FORMAT_YVYU),
> > +	PixelFormat(DRM_FORMAT_VYUY),
> > +	PixelFormat(DRM_FORMAT_NV16),
> > +	PixelFormat(DRM_FORMAT_NV61),
> > +	PixelFormat(DRM_FORMAT_NV21),
> > +	PixelFormat(DRM_FORMAT_NV12),
> > +	/* \todo Add support for 8-bit greyscale to DRM formats */
> > +};
> > +
> > +} /* namespace */
> > +
> >  RkISP1Frames::RkISP1Frames(PipelineHandler *pipe)
> >  	: pipe_(dynamic_cast<PipelineHandlerRkISP1 *>(pipe))
> >  {
> > @@ -430,17 +445,6 @@ RkISP1CameraConfiguration::RkISP1CameraConfiguration(Camera *camera,
> >
> >  CameraConfiguration::Status RkISP1CameraConfiguration::validate()
> >  {
> > -	static const std::array<PixelFormat, 8> formats{
> > -		PixelFormat(DRM_FORMAT_YUYV),
> > -		PixelFormat(DRM_FORMAT_YVYU),
> > -		PixelFormat(DRM_FORMAT_VYUY),
> > -		PixelFormat(DRM_FORMAT_NV16),
> > -		PixelFormat(DRM_FORMAT_NV61),
> > -		PixelFormat(DRM_FORMAT_NV21),
> > -		PixelFormat(DRM_FORMAT_NV12),
> > -		/* \todo Add support for 8-bit greyscale to DRM formats */
> > -	};
> > -
> >  	const CameraSensor *sensor = data_->sensor_;
> >  	Status status = Valid;
> >
> > @@ -449,7 +453,7 @@ CameraConfiguration::Status RkISP1CameraConfiguration::validate()
> >
> >  	/* Cap the number of entries to the available streams. */
> >  	if (config_.size() > 1) {
> > -		config_.resize(1);
> > +		config_.erase(config_.begin() + 1, config_.end() - 1);
> 
> Just config.end(), I bet STL containers handles being() and end()
> in the right way.

Yes sorry, I read the documentation again. erase() erases [first,
last)..it's open bracket at last. I'll fix this. But begin() + 1 is
right, as we need the first element intact.
Thanks, I'll change this

> 
> >  		status = Adjusted;
> >  	}
> >
> > @@ -537,7 +541,16 @@ CameraConfiguration *PipelineHandlerRkISP1::generateConfiguration(Camera *camera
> >  	if (roles.empty())
> >  		return config;
> >
> > -	StreamConfiguration cfg{};
> > +	std::map<PixelFormat, std::vector<SizeRange>> pixelformats;
> > +
> > +	for (PixelFormat pixelformat : formats) {
> > +		std::vector<SizeRange> sizes{
> > +			SizeRange{ { 32, 16 }, { 4416, 3312 } }
> > +		};
> > +		pixelformats[pixelformat] = sizes;
> > +	}
> > +	StreamFormats format(pixelformats);
> > +	StreamConfiguration cfg(format);
> 
> This can be later optimized on top. I wonder, in example what going through
> a StreamFormats gives us. Not on this patch, it was there already.

Yes, I thought I'll optimise this after I get a thumbs-up on the
size-range that I have used.

I don't know exactly, but I think it's because since we already know the
streamformats (pixelsformats..the sizes) for these pipelines, we must not
use an empty constructor object and define things later, we should pass
them in the parametrized constructor directly?? 

> 
> >  	cfg.pixelFormat = PixelFormat(DRM_FORMAT_NV12);
> >  	cfg.size = data->sensor_->resolution();
> >
> > diff --git a/src/libcamera/stream.cpp b/src/libcamera/stream.cpp
> > index ea3d214..96b3dc5 100644
> > --- a/src/libcamera/stream.cpp
> > +++ b/src/libcamera/stream.cpp
> > @@ -274,15 +274,6 @@ SizeRange StreamFormats::range(const PixelFormat &pixelformat) const
> >   * configured for a single video stream.
> >   */
> >
> > -/**
> > - * \todo This method is deprecated and should be removed once all pipeline
> > - * handlers provied StreamFormats.
> > - */
> > -StreamConfiguration::StreamConfiguration()
> > -	: pixelFormat(0), stream_(nullptr)
> > -{
> > -}
> > -
> 
> Nope, as reported, removing the constructor implementation will give
> you linkage errors. Even if private, it has to be defined somewhere.
> 
> To try if at least this pipeline handlers build right, you can remove
> from src/libcamera/pipeline/meson.build the other pipeline handlers
> (and disable tests as well, as you will have an error there too)
> 

Okay, I'll do this and check.

Thanks for your time!

> Thanks
>   j
> 
> >  /**
> >   * \brief Construct a configuration with stream formats
> >   */
> > --
> > 2.17.1
> >

Patch

diff --git a/include/libcamera/stream.h b/include/libcamera/stream.h
index b1441f8..c19aed6 100644
--- a/include/libcamera/stream.h
+++ b/include/libcamera/stream.h
@@ -37,7 +37,6 @@  private:
 };
 
 struct StreamConfiguration {
-	StreamConfiguration();
 	StreamConfiguration(const StreamFormats &formats);
 
 	PixelFormat pixelFormat;
@@ -52,8 +51,11 @@  struct StreamConfiguration {
 	std::string toString() const;
 
 private:
+	StreamConfiguration();
 	Stream *stream_;
 	StreamFormats formats_;
+
+	friend class Stream;
 };
 
 enum StreamRole {
diff --git a/src/libcamera/pipeline/rkisp1/rkisp1.cpp b/src/libcamera/pipeline/rkisp1/rkisp1.cpp
index 2f909ce..8e8414e 100644
--- a/src/libcamera/pipeline/rkisp1/rkisp1.cpp
+++ b/src/libcamera/pipeline/rkisp1/rkisp1.cpp
@@ -218,6 +218,21 @@  private:
 	Camera *activeCamera_;
 };
 
+namespace {
+
+static const std::array<PixelFormat, 8> formats{
+	PixelFormat(DRM_FORMAT_YUYV),
+	PixelFormat(DRM_FORMAT_YVYU),
+	PixelFormat(DRM_FORMAT_VYUY),
+	PixelFormat(DRM_FORMAT_NV16),
+	PixelFormat(DRM_FORMAT_NV61),
+	PixelFormat(DRM_FORMAT_NV21),
+	PixelFormat(DRM_FORMAT_NV12),
+	/* \todo Add support for 8-bit greyscale to DRM formats */
+};
+
+} /* namespace */
+
 RkISP1Frames::RkISP1Frames(PipelineHandler *pipe)
 	: pipe_(dynamic_cast<PipelineHandlerRkISP1 *>(pipe))
 {
@@ -430,17 +445,6 @@  RkISP1CameraConfiguration::RkISP1CameraConfiguration(Camera *camera,
 
 CameraConfiguration::Status RkISP1CameraConfiguration::validate()
 {
-	static const std::array<PixelFormat, 8> formats{
-		PixelFormat(DRM_FORMAT_YUYV),
-		PixelFormat(DRM_FORMAT_YVYU),
-		PixelFormat(DRM_FORMAT_VYUY),
-		PixelFormat(DRM_FORMAT_NV16),
-		PixelFormat(DRM_FORMAT_NV61),
-		PixelFormat(DRM_FORMAT_NV21),
-		PixelFormat(DRM_FORMAT_NV12),
-		/* \todo Add support for 8-bit greyscale to DRM formats */
-	};
-
 	const CameraSensor *sensor = data_->sensor_;
 	Status status = Valid;
 
@@ -449,7 +453,7 @@  CameraConfiguration::Status RkISP1CameraConfiguration::validate()
 
 	/* Cap the number of entries to the available streams. */
 	if (config_.size() > 1) {
-		config_.resize(1);
+		config_.erase(config_.begin() + 1, config_.end() - 1);
 		status = Adjusted;
 	}
 
@@ -537,7 +541,16 @@  CameraConfiguration *PipelineHandlerRkISP1::generateConfiguration(Camera *camera
 	if (roles.empty())
 		return config;
 
-	StreamConfiguration cfg{};
+	std::map<PixelFormat, std::vector<SizeRange>> pixelformats;
+
+	for (PixelFormat pixelformat : formats) {
+		std::vector<SizeRange> sizes{
+			SizeRange{ { 32, 16 }, { 4416, 3312 } }
+		};
+		pixelformats[pixelformat] = sizes;
+	}
+	StreamFormats format(pixelformats);
+	StreamConfiguration cfg(format);
 	cfg.pixelFormat = PixelFormat(DRM_FORMAT_NV12);
 	cfg.size = data->sensor_->resolution();
 
diff --git a/src/libcamera/stream.cpp b/src/libcamera/stream.cpp
index ea3d214..96b3dc5 100644
--- a/src/libcamera/stream.cpp
+++ b/src/libcamera/stream.cpp
@@ -274,15 +274,6 @@  SizeRange StreamFormats::range(const PixelFormat &pixelformat) const
  * configured for a single video stream.
  */
 
-/**
- * \todo This method is deprecated and should be removed once all pipeline
- * handlers provied StreamFormats.
- */
-StreamConfiguration::StreamConfiguration()
-	: pixelFormat(0), stream_(nullptr)
-{
-}
-
 /**
  * \brief Construct a configuration with stream formats
  */