From patchwork Mon Oct 28 02:25:22 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: 2269 Return-Path: Received: from bin-mail-out-05.binero.net (bin-mail-out-05.binero.net [195.74.38.228]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id AFACB6017E for ; Mon, 28 Oct 2019 03:25:55 +0100 (CET) X-Halon-ID: 434a2b97-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 434a2b97-f92a-11e9-903a-005056917f90; Mon, 28 Oct 2019 03:25:52 +0100 (CET) From: =?utf-8?q?Niklas_S=C3=B6derlund?= To: libcamera-devel@lists.libcamera.org Date: Mon, 28 Oct 2019 03:25:22 +0100 Message-Id: <20191028022525.796995-10-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 09/12] libcamera: v4l2_videodevice: Add a buffer cache class 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:56 -0000 In preparation for the new buffer handling add a class which will deal with keeping the cache between dmafds and V4L2 video device buffer indexes. Previously this responsibility have been split between multiple classes. This initial implement ensures that no hot association is lost while its eviction strategy could be improved in the future. Signed-off-by: Niklas Söderlund --- src/libcamera/include/v4l2_videodevice.h | 19 ++++++++++ src/libcamera/v4l2_videodevice.cpp | 47 ++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/src/libcamera/include/v4l2_videodevice.h b/src/libcamera/include/v4l2_videodevice.h index 4b8cf9394eb9516f..5b178339d0ce7e2c 100644 --- a/src/libcamera/include/v4l2_videodevice.h +++ b/src/libcamera/include/v4l2_videodevice.h @@ -104,6 +104,25 @@ struct V4L2Capability final : v4l2_capability { } }; +class V4L2BufferCache +{ +public: + V4L2BufferCache(unsigned int size); + + void populate(unsigned int index, const std::array &fds); + + int pop(const std::array &fds); + void push(unsigned int index); + +private: + struct CacheInfo { + bool free; + std::array last; + }; + + std::map cache_; +}; + class V4L2DeviceFormat { public: diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp index a2a9eab2bcc0d7e8..8bc2e439e4faeb68 100644 --- a/src/libcamera/v4l2_videodevice.cpp +++ b/src/libcamera/v4l2_videodevice.cpp @@ -132,6 +132,53 @@ LOG_DECLARE_CATEGORY(V4L2) * \return True if the video device provides Streaming I/O IOCTLs */ +V4L2BufferCache::V4L2BufferCache(unsigned int size) +{ + for (unsigned int i = 0; i < size; i++) + cache_[i] = { .free = true, .last = { -1, -1, -1 } }; +} + +void V4L2BufferCache::populate(unsigned int index, const std::array &fds) +{ + ASSERT(index < cache_.size()); + cache_[index].last = fds; +} + +int V4L2BufferCache::pop(const std::array &fds) +{ + int use = -1; + + for (auto &it : cache_) { + int index = it.first; + CacheInfo &info = it.second; + + if (!info.free) + continue; + + if (use < 0) + use = index; + + if (info.last == fds) { + use = index; + break; + } + } + + if (use < 0) + return -ENOENT; + + cache_[use].free = false; + cache_[use].last = fds; + + return use; +} + +void V4L2BufferCache::push(unsigned int index) +{ + ASSERT(index < cache_.size()); + cache_[index].free = true; +} + /** * \class V4L2DeviceFormat * \brief The V4L2 video device image format and sizes