Message ID | 20191126233620.1695316-23-niklas.soderlund@ragnatech.se |
---|---|
State | Superseded |
Headers | show |
Series |
|
Related | show |
Hi Niklas, On Wed, Nov 27, 2019 at 12:36:12AM +0100, Niklas Söderlund wrote: > In the FrameBuffer interface the streams and not the pipelines abstracts > the knowledge about how and if buffers needs to be allocated or prepared > before a capture session is started. > > The rational behind this model is the V4L2 API where if applications > needs to allocate memory for buffers it do that using a video node and applications -> "it do" ? > not using a external allocator. As the video node needs to be prepared > in different ways depending on if it have been used for memory s/on if/if it have/it has > allocation or not each streams facing applications needs not only be > available to be used to allocated buffers but track if they have been so > in order to be started correctly. very confusing As the video device node needs to be configured to work with memory buffers allocated internally or externally the Stream class needs to track the current memory usage in order to be properly started. I'm not even sure that's what you meant. > > This change implements the interface for cameras backed by V4L2 video > devices to function with the FrameBuffer interface. > > Signed-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se> > --- > src/libcamera/include/v4l2_videodevice.h | 24 +++++++ > src/libcamera/v4l2_videodevice.cpp | 90 ++++++++++++++++++++++++ > 2 files changed, 114 insertions(+) > > diff --git a/src/libcamera/include/v4l2_videodevice.h b/src/libcamera/include/v4l2_videodevice.h > index f4cbcfbd26ea540c..0e94c3f71d0b1208 100644 > --- a/src/libcamera/include/v4l2_videodevice.h > +++ b/src/libcamera/include/v4l2_videodevice.h > @@ -16,6 +16,7 @@ > #include <libcamera/geometry.h> > #include <libcamera/pixelformats.h> > #include <libcamera/signal.h> > +#include <libcamera/stream.h> > > #include "formats.h" > #include "log.h" > @@ -236,6 +237,29 @@ private: > V4L2VideoDevice *capture_; > }; > > +class V4L2Stream : public Stream > +{ > +public: > + V4L2Stream(V4L2VideoDevice *video); > + > +protected: > + int allocateBuffers(const StreamConfiguration &config, > + std::vector<FrameBuffer *> *buffers) override; > + void releaseBuffers() override; > + int start() override; > + void stop() override; > + > +private: > + enum BufferType { > + NoBuffers, > + AllocatedBuffers, > + ExternalBuffers, the duality between interanlly and externally allocated buffers is so clear that I'm not sure why Allocated/External has to be used here s/Allocated/Internal I would drop Buffers completely, as the enum is calle BufferType. > + }; > + > + V4L2VideoDevice *video_; > + BufferType bufferType_; > +}; > + > } /* namespace libcamera */ > > #endif /* __LIBCAMERA_V4L2_VIDEODEVICE_H__ */ > diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp > index 9fe66018ec502626..f5810956b2040ce6 100644 > --- a/src/libcamera/v4l2_videodevice.cpp > +++ b/src/libcamera/v4l2_videodevice.cpp > @@ -1711,4 +1711,94 @@ void V4L2M2MDevice::close() > output_->close(); > } > > +/** > + * \class V4L2Stream > + * \brief V4L2 Video stream for a camera A stream generated from a V4L2 device ? > + * > + * The V4l2Stream implemets the interfaces mandated by Stream to allow buffers s/implemets/implements/ s/mandated/defined/ by Stream/in the Stream class/ > + * to be allocoated from a video device and presented to the user as if they s/allocoated/allocated > + * where allocated externaly. I understand the "treat all streams as externally allocated" is the juice of the series, but I'm not sure this is the right place to specify so > + */ > + > +/** > + * \brief Create a stream around a V4L2 video device around ? Maybe "backed by" ? "from" ? > + * \param[in] video V4L2 video device to serve The V4L2 video device that produces data for the stream ? > + * > + * Create a stream which will manage the application visible buffers for a V4L2 > + * video device. > + */ > +V4L2Stream::V4L2Stream(V4L2VideoDevice *video) > + : Stream(), video_(video), bufferType_(NoBuffers) > +{ > +} > + > +int V4L2Stream::allocateBuffers(const StreamConfiguration &config, > + std::vector<FrameBuffer *> *buffers) > +{ > + if (bufferType_ != NoBuffers) > + return -EINVAL; > + > + if (!buffers) && !buffers.size() > + return -EINVAL; > + > + /* > + * \todo: Extend V4L2VideoDevice to support VIDIOC_CREATE_BUFS which > + * can take the whole configuration into account, until then use as > + * much as possible (number of buffers) with VIDIOC_QUERYBUF. > + * > + * For this reason check that the format of the video device is the > + * same as the one request or fail. This will allow for a applicaiton s/applicaiton/application > + * facing API that will work with VIDIOC_CREATE_BUFS. > + */ > + V4L2DeviceFormat format; > + int ret = video_->getFormat(&format); > + if (ret) > + return ret; > + > + if (config.size != format.size || > + config.pixelFormat != V4L2VideoDevice::toPixelFormat(format.fourcc)) > + return -EINVAL; > + > + ret = video_->allocateBuffers(config.bufferCount, buffers); > + if (ret) > + return ret; > + > + bufferType_ = AllocatedBuffers; > + > + return ret; > +} > + > +void V4L2Stream::releaseBuffers() > +{ > + if (bufferType_ == NoBuffers) > + return; > + > + video_->releaseBuffers(); > + bufferType_ = NoBuffers; > +} > + > +int V4L2Stream::start() > +{ > + /* If internal buffers is already allocated nothing to do. */ > + if (bufferType_ == AllocatedBuffers) > + return 0; > + > + int ret = video_->externalBuffers(configuration_.bufferCount); > + if (ret) > + return ret; > + > + bufferType_ = ExternalBuffers; > + > + return ret; > +} > + > +void V4L2Stream::stop() > +{ > + /* Only need to clean up if external buffers is used. */ > + if (bufferType_ != ExternalBuffers) Aren't internally allocated buffers that needs to be released? > + return; > + > + releaseBuffers(); > +} > + > } /* namespace libcamera */ > -- > 2.24.0 > > _______________________________________________ > libcamera-devel mailing list > libcamera-devel@lists.libcamera.org > https://lists.libcamera.org/listinfo/libcamera-devel
diff --git a/src/libcamera/include/v4l2_videodevice.h b/src/libcamera/include/v4l2_videodevice.h index f4cbcfbd26ea540c..0e94c3f71d0b1208 100644 --- a/src/libcamera/include/v4l2_videodevice.h +++ b/src/libcamera/include/v4l2_videodevice.h @@ -16,6 +16,7 @@ #include <libcamera/geometry.h> #include <libcamera/pixelformats.h> #include <libcamera/signal.h> +#include <libcamera/stream.h> #include "formats.h" #include "log.h" @@ -236,6 +237,29 @@ private: V4L2VideoDevice *capture_; }; +class V4L2Stream : public Stream +{ +public: + V4L2Stream(V4L2VideoDevice *video); + +protected: + int allocateBuffers(const StreamConfiguration &config, + std::vector<FrameBuffer *> *buffers) override; + void releaseBuffers() override; + int start() override; + void stop() override; + +private: + enum BufferType { + NoBuffers, + AllocatedBuffers, + ExternalBuffers, + }; + + V4L2VideoDevice *video_; + BufferType bufferType_; +}; + } /* namespace libcamera */ #endif /* __LIBCAMERA_V4L2_VIDEODEVICE_H__ */ diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp index 9fe66018ec502626..f5810956b2040ce6 100644 --- a/src/libcamera/v4l2_videodevice.cpp +++ b/src/libcamera/v4l2_videodevice.cpp @@ -1711,4 +1711,94 @@ void V4L2M2MDevice::close() output_->close(); } +/** + * \class V4L2Stream + * \brief V4L2 Video stream for a camera + * + * The V4l2Stream implemets the interfaces mandated by Stream to allow buffers + * to be allocoated from a video device and presented to the user as if they + * where allocated externaly. + */ + +/** + * \brief Create a stream around a V4L2 video device + * \param[in] video V4L2 video device to serve + * + * Create a stream which will manage the application visible buffers for a V4L2 + * video device. + */ +V4L2Stream::V4L2Stream(V4L2VideoDevice *video) + : Stream(), video_(video), bufferType_(NoBuffers) +{ +} + +int V4L2Stream::allocateBuffers(const StreamConfiguration &config, + std::vector<FrameBuffer *> *buffers) +{ + if (bufferType_ != NoBuffers) + return -EINVAL; + + if (!buffers) + return -EINVAL; + + /* + * \todo: Extend V4L2VideoDevice to support VIDIOC_CREATE_BUFS which + * can take the whole configuration into account, until then use as + * much as possible (number of buffers) with VIDIOC_QUERYBUF. + * + * For this reason check that the format of the video device is the + * same as the one request or fail. This will allow for a applicaiton + * facing API that will work with VIDIOC_CREATE_BUFS. + */ + V4L2DeviceFormat format; + int ret = video_->getFormat(&format); + if (ret) + return ret; + + if (config.size != format.size || + config.pixelFormat != V4L2VideoDevice::toPixelFormat(format.fourcc)) + return -EINVAL; + + ret = video_->allocateBuffers(config.bufferCount, buffers); + if (ret) + return ret; + + bufferType_ = AllocatedBuffers; + + return ret; +} + +void V4L2Stream::releaseBuffers() +{ + if (bufferType_ == NoBuffers) + return; + + video_->releaseBuffers(); + bufferType_ = NoBuffers; +} + +int V4L2Stream::start() +{ + /* If internal buffers is already allocated nothing to do. */ + if (bufferType_ == AllocatedBuffers) + return 0; + + int ret = video_->externalBuffers(configuration_.bufferCount); + if (ret) + return ret; + + bufferType_ = ExternalBuffers; + + return ret; +} + +void V4L2Stream::stop() +{ + /* Only need to clean up if external buffers is used. */ + if (bufferType_ != ExternalBuffers) + return; + + releaseBuffers(); +} + } /* namespace libcamera */
In the FrameBuffer interface the streams and not the pipelines abstracts the knowledge about how and if buffers needs to be allocated or prepared before a capture session is started. The rational behind this model is the V4L2 API where if applications needs to allocate memory for buffers it do that using a video node and not using a external allocator. As the video node needs to be prepared in different ways depending on if it have been used for memory allocation or not each streams facing applications needs not only be available to be used to allocated buffers but track if they have been so in order to be started correctly. This change implements the interface for cameras backed by V4L2 video devices to function with the FrameBuffer interface. Signed-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se> --- src/libcamera/include/v4l2_videodevice.h | 24 +++++++ src/libcamera/v4l2_videodevice.cpp | 90 ++++++++++++++++++++++++ 2 files changed, 114 insertions(+)