libcamera: MappedFrameBuffer: Use typed Flags<MapModes>

Remove the need for callers to reference PROT_READ/PROT_WRITE directly
from <sys/mman.h> by instead exposing the Read/Write mapping options as
flags from the MappedFrameBuffer class itself.

While here, introduce the <stdint.h> header which is required for the
uint8_t as part of the Plane.

Reviewed-by: Hirokazu Honda <hiroh@chromium.org>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
This commit is contained in:
Kieran Bingham 2021-08-06 13:18:01 +01:00
parent f3629363c4
commit fdf1851f0b
8 changed files with 51 additions and 17 deletions

View file

@ -7,10 +7,11 @@
#ifndef __LIBCAMERA_INTERNAL_MAPPED_FRAMEBUFFER_H__ #ifndef __LIBCAMERA_INTERNAL_MAPPED_FRAMEBUFFER_H__
#define __LIBCAMERA_INTERNAL_MAPPED_FRAMEBUFFER_H__ #define __LIBCAMERA_INTERNAL_MAPPED_FRAMEBUFFER_H__
#include <sys/mman.h> #include <stdint.h>
#include <vector> #include <vector>
#include <libcamera/base/class.h> #include <libcamera/base/class.h>
#include <libcamera/base/flags.h>
#include <libcamera/base/span.h> #include <libcamera/base/span.h>
#include <libcamera/framebuffer.h> #include <libcamera/framebuffer.h>
@ -44,9 +45,19 @@ private:
class MappedFrameBuffer : public MappedBuffer class MappedFrameBuffer : public MappedBuffer
{ {
public: public:
MappedFrameBuffer(const FrameBuffer *buffer, int flags); enum class MapFlag {
Read = 1 << 0,
Write = 1 << 1,
ReadWrite = Read | Write,
};
using MapFlags = Flags<MapFlag>;
MappedFrameBuffer(const FrameBuffer *buffer, MapFlags flags);
}; };
LIBCAMERA_FLAGS_ENABLE_OPERATORS(MappedFrameBuffer::MapFlag)
} /* namespace libcamera */ } /* namespace libcamera */
#endif /* __LIBCAMERA_INTERNAL_MAPPED_FRAMEBUFFER_H__ */ #endif /* __LIBCAMERA_INTERNAL_MAPPED_FRAMEBUFFER_H__ */

View file

@ -12,7 +12,6 @@
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <string.h> #include <string.h>
#include <sys/mman.h>
#include <unistd.h> #include <unistd.h>
#include <vector> #include <vector>
@ -182,7 +181,7 @@ void EncoderLibJpeg::compressNV(Span<const uint8_t> frame)
int EncoderLibJpeg::encode(const FrameBuffer &source, Span<uint8_t> dest, int EncoderLibJpeg::encode(const FrameBuffer &source, Span<uint8_t> dest,
Span<const uint8_t> exifData, unsigned int quality) Span<const uint8_t> exifData, unsigned int quality)
{ {
MappedFrameBuffer frame(&source, PROT_READ); MappedFrameBuffer frame(&source, MappedFrameBuffer::MapFlag::Read);
if (!frame.isValid()) { if (!frame.isValid()) {
LOG(JPEG, Error) << "Failed to map FrameBuffer : " LOG(JPEG, Error) << "Failed to map FrameBuffer : "
<< strerror(frame.error()); << strerror(frame.error());

View file

@ -41,7 +41,7 @@ void Thumbnailer::createThumbnail(const FrameBuffer &source,
const Size &targetSize, const Size &targetSize,
std::vector<unsigned char> *destination) std::vector<unsigned char> *destination)
{ {
MappedFrameBuffer frame(&source, PROT_READ); MappedFrameBuffer frame(&source, MappedFrameBuffer::MapFlag::Read);
if (!frame.isValid()) { if (!frame.isValid()) {
LOG(Thumbnailer, Error) LOG(Thumbnailer, Error)
<< "Failed to map FrameBuffer : " << "Failed to map FrameBuffer : "

View file

@ -57,7 +57,7 @@ int PostProcessorYuv::process(const FrameBuffer &source,
if (!isValidBuffers(source, *destination)) if (!isValidBuffers(source, *destination))
return -EINVAL; return -EINVAL;
const MappedFrameBuffer sourceMapped(&source, PROT_READ); const MappedFrameBuffer sourceMapped(&source, MappedFrameBuffer::MapFlag::Read);
if (!sourceMapped.isValid()) { if (!sourceMapped.isValid()) {
LOG(YUV, Error) << "Failed to mmap camera frame buffer"; LOG(YUV, Error) << "Failed to mmap camera frame buffer";
return -EINVAL; return -EINVAL;

View file

@ -6,7 +6,6 @@
*/ */
#include <stdint.h> #include <stdint.h>
#include <sys/mman.h>
#include <linux/intel-ipu3.h> #include <linux/intel-ipu3.h>
#include <linux/v4l2-controls.h> #include <linux/v4l2-controls.h>
@ -211,7 +210,7 @@ void IPAIPU3::mapBuffers(const std::vector<IPABuffer> &buffers)
for (const IPABuffer &buffer : buffers) { for (const IPABuffer &buffer : buffers) {
const FrameBuffer fb(buffer.planes); const FrameBuffer fb(buffer.planes);
buffers_.emplace(buffer.id, buffers_.emplace(buffer.id,
MappedFrameBuffer(&fb, PROT_READ | PROT_WRITE)); MappedFrameBuffer(&fb, MappedFrameBuffer::MapFlag::ReadWrite));
} }
} }

View file

@ -411,7 +411,8 @@ void IPARPi::mapBuffers(const std::vector<IPABuffer> &buffers)
{ {
for (const IPABuffer &buffer : buffers) { for (const IPABuffer &buffer : buffers) {
const FrameBuffer fb(buffer.planes); const FrameBuffer fb(buffer.planes);
buffers_.emplace(buffer.id, MappedFrameBuffer(&fb, PROT_READ | PROT_WRITE)); buffers_.emplace(buffer.id,
MappedFrameBuffer(&fb, MappedFrameBuffer::MapFlag::ReadWrite));
} }
} }

View file

@ -140,21 +140,45 @@ MappedBuffer::~MappedBuffer()
* \brief Map a FrameBuffer using the MappedBuffer interface * \brief Map a FrameBuffer using the MappedBuffer interface
*/ */
/**
* \enum MappedFrameBuffer::MapFlag
* \brief Specify the mapping mode for the FrameBuffer
* \var MappedFrameBuffer::Read
* \brief Create a read-only mapping
* \var MappedFrameBuffer::Write
* \brief Create a write-only mapping
* \var MappedFrameBuffer::ReadWrite
* \brief Create a mapping that can be both read and written
*/
/**
* \typedef MappedFrameBuffer::MapFlags
* \brief A bitwise combination of MappedFrameBuffer::MapFlag values
*/
/** /**
* \brief Map all planes of a FrameBuffer * \brief Map all planes of a FrameBuffer
* \param[in] buffer FrameBuffer to be mapped * \param[in] buffer FrameBuffer to be mapped
* \param[in] flags Protection flags to apply to map * \param[in] flags Protection flags to apply to map
* *
* Construct an object to map a frame buffer for CPU access. * Construct an object to map a frame buffer for CPU access. The mapping can be
* The flags are passed directly to mmap and should be either PROT_READ, * made as Read only, Write only or support Read and Write operations by setting
* PROT_WRITE, or a bitwise-or combination of both. * the MapFlag flags accordingly.
*/ */
MappedFrameBuffer::MappedFrameBuffer(const FrameBuffer *buffer, int flags) MappedFrameBuffer::MappedFrameBuffer(const FrameBuffer *buffer, MapFlags flags)
{ {
maps_.reserve(buffer->planes().size()); maps_.reserve(buffer->planes().size());
int mmapFlags = 0;
if (flags & MapFlag::Read)
mmapFlags |= PROT_READ;
if (flags & MapFlag::Write)
mmapFlags |= PROT_WRITE;
for (const FrameBuffer::Plane &plane : buffer->planes()) { for (const FrameBuffer::Plane &plane : buffer->planes()) {
void *address = mmap(nullptr, plane.length, flags, void *address = mmap(nullptr, plane.length, mmapFlags,
MAP_SHARED, plane.fd.fd(), 0); MAP_SHARED, plane.fd.fd(), 0);
if (address == MAP_FAILED) { if (address == MAP_FAILED) {
error_ = -errno; error_ = -errno;

View file

@ -71,7 +71,7 @@ protected:
const std::unique_ptr<FrameBuffer> &buffer = allocator_->buffers(stream_).front(); const std::unique_ptr<FrameBuffer> &buffer = allocator_->buffers(stream_).front();
std::vector<MappedBuffer> maps; std::vector<MappedBuffer> maps;
MappedFrameBuffer map(buffer.get(), PROT_READ); MappedFrameBuffer map(buffer.get(), MappedFrameBuffer::MapFlag::Read);
if (!map.isValid()) { if (!map.isValid()) {
cout << "Failed to successfully map buffer" << endl; cout << "Failed to successfully map buffer" << endl;
return TestFail; return TestFail;
@ -90,13 +90,13 @@ protected:
} }
/* Test for multiple successful maps on the same buffer. */ /* Test for multiple successful maps on the same buffer. */
MappedFrameBuffer write_map(buffer.get(), PROT_WRITE); MappedFrameBuffer write_map(buffer.get(), MappedFrameBuffer::MapFlag::Write);
if (!write_map.isValid()) { if (!write_map.isValid()) {
cout << "Failed to map write buffer" << endl; cout << "Failed to map write buffer" << endl;
return TestFail; return TestFail;
} }
MappedFrameBuffer rw_map(buffer.get(), PROT_READ | PROT_WRITE); MappedFrameBuffer rw_map(buffer.get(), MappedFrameBuffer::MapFlag::ReadWrite);
if (!rw_map.isValid()) { if (!rw_map.isValid()) {
cout << "Failed to map RW buffer" << endl; cout << "Failed to map RW buffer" << endl;
return TestFail; return TestFail;