From patchwork Mon Oct 28 02:25:21 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Niklas_S=C3=B6derlund?= X-Patchwork-Id: 2268 Return-Path: Received: from vsp-unauthed02.binero.net (vsp-unauthed02.binero.net [195.74.38.227]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 298816017C for ; Mon, 28 Oct 2019 03:25:55 +0100 (CET) X-Halon-ID: 42bdd136-f92a-11e9-903a-005056917f90 Authorized-sender: niklas@soderlund.pp.se Received: from localhost.localdomain (unknown [93.2.121.143]) by bin-vsp-out-02.atm.binero.net (Halon) with ESMTPA id 42bdd136-f92a-11e9-903a-005056917f90; Mon, 28 Oct 2019 03:25:51 +0100 (CET) From: =?utf-8?q?Niklas_S=C3=B6derlund?= To: libcamera-devel@lists.libcamera.org Date: Mon, 28 Oct 2019 03:25:21 +0100 Message-Id: <20191028022525.796995-9-niklas.soderlund@ragnatech.se> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191028022525.796995-1-niklas.soderlund@ragnatech.se> References: <20191028022525.796995-1-niklas.soderlund@ragnatech.se> MIME-Version: 1.0 Subject: [libcamera-devel] [RFC 08/12] libcamera: buffer: Add a buffer allocator X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 28 Oct 2019 02:25:55 -0000 >From an applications point of view buffers shall not be allocated directly by a camera or stream. Instead they shall be allocated externally and used by a camera. To model this behavior add a buffer allocator that is exposed to applications. How the allocator creates buffers is pipeline specific and handled by sub-classing the stream class. Signed-off-by: Niklas Söderlund --- include/libcamera/buffer.h | 17 ++++++++++++++ include/libcamera/stream.h | 1 + src/libcamera/buffer.cpp | 48 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) diff --git a/include/libcamera/buffer.h b/include/libcamera/buffer.h index c626f669040b3c04..adb642ad5da072d2 100644 --- a/include/libcamera/buffer.h +++ b/include/libcamera/buffer.h @@ -8,11 +8,14 @@ #define __LIBCAMERA_BUFFER_H__ #include +#include +#include #include #include namespace libcamera { +class Camera; class Request; class Stream; @@ -139,6 +142,20 @@ private: std::vector planes_; }; +class BufferAllocator +{ +public: + BufferAllocator(std::shared_ptr camera); + ~BufferAllocator(); + + int allocate(Stream *stream); + std::vector get(Stream *stream); + +private: + std::shared_ptr camera_; + std::map> allocated_; +}; + } /* namespace libcamera */ #endif /* __LIBCAMERA_BUFFER_H__ */ diff --git a/include/libcamera/stream.h b/include/libcamera/stream.h index dac4831cfa1a9b1d..b051341511a7ab7c 100644 --- a/include/libcamera/stream.h +++ b/include/libcamera/stream.h @@ -84,6 +84,7 @@ public: MemoryType memoryType() const { return memoryType_; } protected: + friend class BufferAllocator; friend class Camera; virtual int allocateBuffers(std::vector *buffers) { return -EINVAL; } diff --git a/src/libcamera/buffer.cpp b/src/libcamera/buffer.cpp index 10b16a862393b536..fce1ce5e49cbbf42 100644 --- a/src/libcamera/buffer.cpp +++ b/src/libcamera/buffer.cpp @@ -12,6 +12,9 @@ #include #include +#include +#include + #include "log.h" /** @@ -393,4 +396,49 @@ void Buffer::cancel() * The intended callers are Request::prepare() and Request::completeBuffer(). */ +BufferAllocator::BufferAllocator(std::shared_ptr camera) + : camera_(camera) +{ +} + +BufferAllocator::~BufferAllocator() +{ + for (auto &it : allocated_) { + Stream *stream = it.first; + std::vector &buffers = it.second; + + for (Buffer *buffer : buffers) + delete buffer; + + buffers.clear(); + + stream->allocateBuffers(nullptr); + } +} + +int BufferAllocator::allocate(Stream *stream) +{ + bool found = false; + + for (const Stream *s : camera_->streams()) { + if (stream == s) { + found = true; + break; + } + } + + if (!found) + return -EINVAL; + + return stream->allocateBuffers(&allocated_[stream]); +} + +std::vector BufferAllocator::get(Stream *stream) +{ + if (allocated_.find(stream) == allocated_.end()) + return {}; + + return allocated_[stream]; +} + } /* namespace libcamera */