[libcamera-devel,v3,30/30] v4l2: Sum bytesused for all planes when dequeuing buffer
diff mbox series

Message ID 20210906225636.14683-30-laurent.pinchart@ideasonboard.com
State Accepted
Headers show
Series
  • libcamera: Handle fallout of FrameBuffer offset support
Related show

Commit Message

Laurent Pinchart Sept. 6, 2021, 10:56 p.m. UTC
The V4L2 compatibility layer supports the single-planar API only, and
thuss expose a single V4L2 buffer plane to applications, regardless of
the number of planes in the FrameBuffer. For multi-planar frame buffers,
the bytesused value isn't correct as it only takes the first plane into
account. Fix it by summing the bytesused values for all FrameBuffer
planes.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 src/v4l2/v4l2_camera_proxy.cpp | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

Comments

Kieran Bingham Sept. 7, 2021, 2:17 p.m. UTC | #1
On 06/09/2021 23:56, Laurent Pinchart wrote:
> The V4L2 compatibility layer supports the single-planar API only, and
> thuss expose a single V4L2 buffer plane to applications, regardless of

s/thuss/thus/
s/expose/exposes/

> the number of planes in the FrameBuffer. For multi-planar frame buffers,
> the bytesused value isn't correct as it only takes the first plane into
> account. Fix it by summing the bytesused values for all FrameBuffer
> planes.
> 
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> ---
>  src/v4l2/v4l2_camera_proxy.cpp | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
> 
> diff --git a/src/v4l2/v4l2_camera_proxy.cpp b/src/v4l2/v4l2_camera_proxy.cpp
> index 07d2250bb846..aeffef26c5bf 100644
> --- a/src/v4l2/v4l2_camera_proxy.cpp
> +++ b/src/v4l2/v4l2_camera_proxy.cpp
> @@ -11,6 +11,7 @@
>  #include <array>
>  #include <errno.h>
>  #include <linux/videodev2.h>
> +#include <numeric>
>  #include <set>
>  #include <string.h>
>  #include <sys/mman.h>
> @@ -211,7 +212,11 @@ void V4L2CameraProxy::updateBuffers()
>  
>  		switch (fmd.status) {
>  		case FrameMetadata::FrameSuccess:
> -			buf.bytesused = fmd.planes()[0].bytesused;
> +			buf.bytesused = std::accumulate(fmd.planes().begin(),
> +							fmd.planes().end(), 0,
> +							[](unsigned int total, const auto &plane) {

lambda's always look like black magic to me.
I assume total is either automatically initialised, or initialised by
the 0 after fmd.planes().end()

Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>

> +								return total + plane.bytesused;
> +							});
>  			buf.field = V4L2_FIELD_NONE;
>  			buf.timestamp.tv_sec = fmd.timestamp / 1000000000;
>  			buf.timestamp.tv_usec = fmd.timestamp % 1000000;
>
Jean-Michel Hautbois Sept. 7, 2021, 2:26 p.m. UTC | #2
Hi Laurent,

On 07/09/2021 16:17, Kieran Bingham wrote:
> On 06/09/2021 23:56, Laurent Pinchart wrote:
>> The V4L2 compatibility layer supports the single-planar API only, and
>> thuss expose a single V4L2 buffer plane to applications, regardless of
> 
> s/thuss/thus/
> s/expose/exposes/
> 
>> the number of planes in the FrameBuffer. For multi-planar frame buffers,
>> the bytesused value isn't correct as it only takes the first plane into
>> account. Fix it by summing the bytesused values for all FrameBuffer
>> planes.
>>
>> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
>> ---
>>  src/v4l2/v4l2_camera_proxy.cpp | 7 ++++++-
>>  1 file changed, 6 insertions(+), 1 deletion(-)
>>
>> diff --git a/src/v4l2/v4l2_camera_proxy.cpp b/src/v4l2/v4l2_camera_proxy.cpp
>> index 07d2250bb846..aeffef26c5bf 100644
>> --- a/src/v4l2/v4l2_camera_proxy.cpp
>> +++ b/src/v4l2/v4l2_camera_proxy.cpp
>> @@ -11,6 +11,7 @@
>>  #include <array>
>>  #include <errno.h>
>>  #include <linux/videodev2.h>
>> +#include <numeric>
>>  #include <set>
>>  #include <string.h>
>>  #include <sys/mman.h>
>> @@ -211,7 +212,11 @@ void V4L2CameraProxy::updateBuffers()
>>  
>>  		switch (fmd.status) {
>>  		case FrameMetadata::FrameSuccess:
>> -			buf.bytesused = fmd.planes()[0].bytesused;
>> +			buf.bytesused = std::accumulate(fmd.planes().begin(),
>> +							fmd.planes().end(), 0,
>> +							[](unsigned int total, const auto &plane) {
> 
> lambda's always look like black magic to me.
> I assume total is either automatically initialised, or initialised by
> the 0 after fmd.planes().end()

I read back cpp reference for that one :-), and yes, total takes the
value of init, which is 0.

> 
> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Reviewed-by: Jean-Michel Hautbois <jeanmichel.hautbois@ideasonboard.com>
> 
>> +								return total + plane.bytesused;
>> +							});
>>  			buf.field = V4L2_FIELD_NONE;
>>  			buf.timestamp.tv_sec = fmd.timestamp / 1000000000;
>>  			buf.timestamp.tv_usec = fmd.timestamp % 1000000;
>>
Laurent Pinchart Sept. 7, 2021, 2:55 p.m. UTC | #3
Hi Kieran,

On Tue, Sep 07, 2021 at 03:17:48PM +0100, Kieran Bingham wrote:
> On 06/09/2021 23:56, Laurent Pinchart wrote:
> > The V4L2 compatibility layer supports the single-planar API only, and
> > thuss expose a single V4L2 buffer plane to applications, regardless of
> 
> s/thuss/thus/
> s/expose/exposes/
> 
> > the number of planes in the FrameBuffer. For multi-planar frame buffers,
> > the bytesused value isn't correct as it only takes the first plane into
> > account. Fix it by summing the bytesused values for all FrameBuffer
> > planes.
> > 
> > Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> > ---
> >  src/v4l2/v4l2_camera_proxy.cpp | 7 ++++++-
> >  1 file changed, 6 insertions(+), 1 deletion(-)
> > 
> > diff --git a/src/v4l2/v4l2_camera_proxy.cpp b/src/v4l2/v4l2_camera_proxy.cpp
> > index 07d2250bb846..aeffef26c5bf 100644
> > --- a/src/v4l2/v4l2_camera_proxy.cpp
> > +++ b/src/v4l2/v4l2_camera_proxy.cpp
> > @@ -11,6 +11,7 @@
> >  #include <array>
> >  #include <errno.h>
> >  #include <linux/videodev2.h>
> > +#include <numeric>
> >  #include <set>
> >  #include <string.h>
> >  #include <sys/mman.h>
> > @@ -211,7 +212,11 @@ void V4L2CameraProxy::updateBuffers()
> >  
> >  		switch (fmd.status) {
> >  		case FrameMetadata::FrameSuccess:
> > -			buf.bytesused = fmd.planes()[0].bytesused;
> > +			buf.bytesused = std::accumulate(fmd.planes().begin(),
> > +							fmd.planes().end(), 0,
> > +							[](unsigned int total, const auto &plane) {
> 
> lambda's always look like black magic to me.
> I assume total is either automatically initialised, or initialised by
> the 0 after fmd.planes().end()

The latter (https://en.cppreference.com/w/cpp/algorithm/accumulate).

> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
> 
> > +								return total + plane.bytesused;
> > +							});
> >  			buf.field = V4L2_FIELD_NONE;
> >  			buf.timestamp.tv_sec = fmd.timestamp / 1000000000;
> >  			buf.timestamp.tv_usec = fmd.timestamp % 1000000;

Patch
diff mbox series

diff --git a/src/v4l2/v4l2_camera_proxy.cpp b/src/v4l2/v4l2_camera_proxy.cpp
index 07d2250bb846..aeffef26c5bf 100644
--- a/src/v4l2/v4l2_camera_proxy.cpp
+++ b/src/v4l2/v4l2_camera_proxy.cpp
@@ -11,6 +11,7 @@ 
 #include <array>
 #include <errno.h>
 #include <linux/videodev2.h>
+#include <numeric>
 #include <set>
 #include <string.h>
 #include <sys/mman.h>
@@ -211,7 +212,11 @@  void V4L2CameraProxy::updateBuffers()
 
 		switch (fmd.status) {
 		case FrameMetadata::FrameSuccess:
-			buf.bytesused = fmd.planes()[0].bytesused;
+			buf.bytesused = std::accumulate(fmd.planes().begin(),
+							fmd.planes().end(), 0,
+							[](unsigned int total, const auto &plane) {
+								return total + plane.bytesused;
+							});
 			buf.field = V4L2_FIELD_NONE;
 			buf.timestamp.tv_sec = fmd.timestamp / 1000000000;
 			buf.timestamp.tv_usec = fmd.timestamp % 1000000;