From patchwork Mon Feb 24 19:35:58 2020 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: 2875 Return-Path: Received: from bin-mail-out-06.binero.net (bin-mail-out-06.binero.net [195.74.38.229]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id C21D360438 for ; Mon, 24 Feb 2020 20:36:24 +0100 (CET) X-Halon-ID: ee190fbf-573c-11ea-9f40-0050569116f7 Authorized-sender: niklas@soderlund.pp.se Received: from bismarck.berto.se (p4fca2392.dip0.t-ipconnect.de [79.202.35.146]) by bin-vsp-out-03.atm.binero.net (Halon) with ESMTPA id ee190fbf-573c-11ea-9f40-0050569116f7; Mon, 24 Feb 2020 20:36:19 +0100 (CET) From: =?utf-8?q?Niklas_S=C3=B6derlund?= To: libcamera-devel@lists.libcamera.org Date: Mon, 24 Feb 2020 20:35:58 +0100 Message-Id: <20200224193601.1040770-2-niklas.soderlund@ragnatech.se> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200224193601.1040770-1-niklas.soderlund@ragnatech.se> References: <20200224193601.1040770-1-niklas.soderlund@ragnatech.se> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 1/4] test: camera: buffer_import: Fix error messages 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, 24 Feb 2020 19:36:25 -0000 The error message for when a device name can't be resolved to an video device is wrong and applies to the next operation bellow it. Move it to it's correct location and add a new error message to highlight the resolution failure. Signed-off-by: Niklas Söderlund Reviewed-by: Laurent Pinchart --- test/camera/buffer_import.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/test/camera/buffer_import.cpp b/test/camera/buffer_import.cpp index ab6e74bd1671e6f5..6997ea78c9f608c9 100644 --- a/test/camera/buffer_import.cpp +++ b/test/camera/buffer_import.cpp @@ -74,13 +74,16 @@ public: video_ = V4L2VideoDevice::fromEntityName(media_.get(), videoDeviceName); if (!video_) { + std::cout << "Failed to get video device from entity " + << videoDeviceName << std::endl; + return TestFail; + } + + if (video_->open()) { std::cout << "Unable to open " << videoDeviceName << std::endl; return TestFail; } - if (video_->open()) - return TestFail; - /* Configure the format. */ V4L2DeviceFormat format; if (video_->getFormat(&format)) { From patchwork Mon Feb 24 19:35:59 2020 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: 2876 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 A59216265B for ; Mon, 24 Feb 2020 20:36:25 +0100 (CET) X-Halon-ID: ee83490e-573c-11ea-9f40-0050569116f7 Authorized-sender: niklas@soderlund.pp.se Received: from bismarck.berto.se (p4fca2392.dip0.t-ipconnect.de [79.202.35.146]) by bin-vsp-out-03.atm.binero.net (Halon) with ESMTPA id ee83490e-573c-11ea-9f40-0050569116f7; Mon, 24 Feb 2020 20:36:20 +0100 (CET) From: =?utf-8?q?Niklas_S=C3=B6derlund?= To: libcamera-devel@lists.libcamera.org Date: Mon, 24 Feb 2020 20:35:59 +0100 Message-Id: <20200224193601.1040770-3-niklas.soderlund@ragnatech.se> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200224193601.1040770-1-niklas.soderlund@ragnatech.se> References: <20200224193601.1040770-1-niklas.soderlund@ragnatech.se> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 2/4] test: Extract BufferSource class out of camera tests to libtest 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, 24 Feb 2020 19:36:27 -0000 The BufferSource class can be used by other tests other then the camera buffer importer test, move it to libtest. The only changes to BufferSource is for it to be allowed to be split in a header and source file. This change makes it necessary for libtest to have access to internal libcamera headers. As the internal headers already are accessible to all test cases this do not increase the exposure of libcamera internals to the test cases. Signed-off-by: Niklas Söderlund Reviewed-by: Laurent Pinchart --- test/camera/buffer_import.cpp | 95 +------------------------------- test/libtest/buffer_source.cpp | 98 ++++++++++++++++++++++++++++++++++ test/libtest/buffer_source.h | 32 +++++++++++ test/libtest/meson.build | 11 ++-- 4 files changed, 138 insertions(+), 98 deletions(-) create mode 100644 test/libtest/buffer_source.cpp create mode 100644 test/libtest/buffer_source.h diff --git a/test/camera/buffer_import.cpp b/test/camera/buffer_import.cpp index 6997ea78c9f608c9..3f392cdc0732941f 100644 --- a/test/camera/buffer_import.cpp +++ b/test/camera/buffer_import.cpp @@ -16,6 +16,7 @@ #include "media_device.h" #include "v4l2_videodevice.h" +#include "buffer_source.h" #include "camera_test.h" #include "test.h" @@ -23,100 +24,6 @@ using namespace libcamera; namespace { -/* A provider of external buffers, suitable for import by a Camera. */ -class BufferSource -{ -public: - BufferSource() - : video_(nullptr) - { - } - - ~BufferSource() - { - if (video_) { - video_->releaseBuffers(); - video_->close(); - } - - delete video_; - video_ = nullptr; - - if (media_) - media_->release(); - } - - int allocate(const StreamConfiguration &config) - { - /* Locate and open the video device. */ - std::string videoDeviceName = "vivid-000-vid-out"; - - std::unique_ptr enumerator = - DeviceEnumerator::create(); - if (!enumerator) { - std::cout << "Failed to create device enumerator" << std::endl; - return TestFail; - } - - if (enumerator->enumerate()) { - std::cout << "Failed to enumerate media devices" << std::endl; - return TestFail; - } - - DeviceMatch dm("vivid"); - dm.add(videoDeviceName); - - media_ = enumerator->search(dm); - if (!media_) { - std::cout << "No vivid output device available" << std::endl; - return TestSkip; - } - - video_ = V4L2VideoDevice::fromEntityName(media_.get(), videoDeviceName); - if (!video_) { - std::cout << "Failed to get video device from entity " - << videoDeviceName << std::endl; - return TestFail; - } - - if (video_->open()) { - std::cout << "Unable to open " << videoDeviceName << std::endl; - return TestFail; - } - - /* Configure the format. */ - V4L2DeviceFormat format; - if (video_->getFormat(&format)) { - std::cout << "Failed to get format on output device" << std::endl; - return TestFail; - } - - format.size = config.size; - format.fourcc = V4L2VideoDevice::toV4L2Fourcc(config.pixelFormat, false); - if (video_->setFormat(&format)) { - std::cout << "Failed to set format on output device" << std::endl; - return TestFail; - } - - if (video_->exportBuffers(config.bufferCount, &buffers_) < 0) { - std::cout << "Failed to export buffers" << std::endl; - return TestFail; - } - - return TestPass; - } - - const std::vector> &buffers() - { - return buffers_; - } - -private: - std::shared_ptr media_; - V4L2VideoDevice *video_; - std::vector> buffers_; -}; - class BufferImportTest : public CameraTest, public Test { public: diff --git a/test/libtest/buffer_source.cpp b/test/libtest/buffer_source.cpp new file mode 100644 index 0000000000000000..066049d342a491f0 --- /dev/null +++ b/test/libtest/buffer_source.cpp @@ -0,0 +1,98 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2020, Google Inc. + * + * A provider of external buffers, suitable for use in tests. + */ + +#include "buffer_source.h" + +#include + +#include "device_enumerator.h" + +#include "test.h" + +BufferSource::BufferSource() + : video_(nullptr) +{ +} + +BufferSource::~BufferSource() +{ + if (video_) { + video_->releaseBuffers(); + video_->close(); + } + + delete video_; + video_ = nullptr; + + if (media_) + media_->release(); +} + +int BufferSource::allocate(const StreamConfiguration &config) +{ + /* Locate and open the video device. */ + std::string videoDeviceName = "vivid-000-vid-out"; + + std::unique_ptr enumerator = + DeviceEnumerator::create(); + if (!enumerator) { + std::cout << "Failed to create device enumerator" << std::endl; + return TestFail; + } + + if (enumerator->enumerate()) { + std::cout << "Failed to enumerate media devices" << std::endl; + return TestFail; + } + + DeviceMatch dm("vivid"); + dm.add(videoDeviceName); + + media_ = enumerator->search(dm); + if (!media_) { + std::cout << "No vivid output device available" << std::endl; + return TestSkip; + } + + video_ = V4L2VideoDevice::fromEntityName(media_.get(), videoDeviceName); + if (!video_) { + std::cout << "Failed to get video device from entity " + << videoDeviceName << std::endl; + return TestFail; + } + + if (video_->open()) { + std::cout << "Unable to open " << videoDeviceName << std::endl; + return TestFail; + } + + /* Configure the format. */ + V4L2DeviceFormat format; + if (video_->getFormat(&format)) { + std::cout << "Failed to get format on output device" << std::endl; + return TestFail; + } + + format.size = config.size; + format.fourcc = V4L2VideoDevice::toV4L2Fourcc(config.pixelFormat, false); + if (video_->setFormat(&format)) { + std::cout << "Failed to set format on output device" << std::endl; + return TestFail; + } + + if (video_->exportBuffers(config.bufferCount, &buffers_) < 0) { + std::cout << "Failed to export buffers" << std::endl; + return TestFail; + } + + return TestPass; +} + +const std::vector> &BufferSource::buffers() +{ + return buffers_; +} diff --git a/test/libtest/buffer_source.h b/test/libtest/buffer_source.h new file mode 100644 index 0000000000000000..2d8fc5acf6d78771 --- /dev/null +++ b/test/libtest/buffer_source.h @@ -0,0 +1,32 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2020, Google Inc. + * + * buffer_source.h - libcamera camera test helper to create FrameBuffers + */ +#ifndef __LIBCAMERA_BUFFER_SOURCE_TEST_H__ +#define __LIBCAMERA_BUFFER_SOURCE_TEST_H__ + +#include + +#include "media_device.h" +#include "v4l2_videodevice.h" + +using namespace libcamera; + +class BufferSource +{ +public: + BufferSource(); + ~BufferSource(); + + int allocate(const StreamConfiguration &config); + const std::vector> &buffers(); + +private: + std::shared_ptr media_; + V4L2VideoDevice *video_; + std::vector> buffers_; +}; + +#endif /* __LIBCAMERA_BUFFER_SOURCE_TEST_H__ */ diff --git a/test/libtest/meson.build b/test/libtest/meson.build index 3e798ef3810e9b0d..33565e0eb3b66d6a 100644 --- a/test/libtest/meson.build +++ b/test/libtest/meson.build @@ -1,14 +1,11 @@ libtest_sources = files([ + 'buffer_source.cpp', 'camera_test.cpp', 'test.cpp', ]) -libtest = static_library('libtest', libtest_sources, - dependencies : libcamera_dep) - libtest_includes = include_directories('.') -test_libraries = [libtest] test_includes_public = [ libtest_includes, @@ -18,3 +15,9 @@ test_includes_internal = [ test_includes_public, libcamera_internal_includes, ] + +libtest = static_library('libtest', libtest_sources, + dependencies : libcamera_dep, + include_directories : test_includes_internal) + +test_libraries = [libtest] From patchwork Mon Feb 24 19:36:00 2020 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: 2877 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 6779C6042F for ; Mon, 24 Feb 2020 20:36:26 +0100 (CET) X-Halon-ID: ef150f55-573c-11ea-9f40-0050569116f7 Authorized-sender: niklas@soderlund.pp.se Received: from bismarck.berto.se (p4fca2392.dip0.t-ipconnect.de [79.202.35.146]) by bin-vsp-out-03.atm.binero.net (Halon) with ESMTPA id ef150f55-573c-11ea-9f40-0050569116f7; Mon, 24 Feb 2020 20:36:21 +0100 (CET) From: =?utf-8?q?Niklas_S=C3=B6derlund?= To: libcamera-devel@lists.libcamera.org Date: Mon, 24 Feb 2020 20:36:00 +0100 Message-Id: <20200224193601.1040770-4-niklas.soderlund@ragnatech.se> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200224193601.1040770-1-niklas.soderlund@ragnatech.se> References: <20200224193601.1040770-1-niklas.soderlund@ragnatech.se> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 3/4] test: v4l2_videodevice: Add test for V4L2BufferCache 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, 24 Feb 2020 19:36:27 -0000 Add test to test the different modes and situations the V4L2BufferCache can be put in. The tests verify that a FrameBuffer used with the cache results in a V4L2 video device index, and that the cache implementation is capable of keeping buffers in a hot state. Signed-off-by: Niklas Söderlund Reviewed-by: Laurent Pinchart --- * Changes since v1 - Update comments in code. - Use std::mt19937 PRNG instead of rand() - Print randomize and print std::mt19937 initial seed to be able to reproduce a test run with the same random sequences. - Add const std::vector> &buffers "alias" for source.buffers() to make code more readable. - Make use of libtest common implementation of BufferSource. --- test/v4l2_videodevice/buffer_cache.cpp | 215 +++++++++++++++++++++++++ test/v4l2_videodevice/meson.build | 1 + 2 files changed, 216 insertions(+) create mode 100644 test/v4l2_videodevice/buffer_cache.cpp diff --git a/test/v4l2_videodevice/buffer_cache.cpp b/test/v4l2_videodevice/buffer_cache.cpp new file mode 100644 index 0000000000000000..0a8cb0d28ca9b204 --- /dev/null +++ b/test/v4l2_videodevice/buffer_cache.cpp @@ -0,0 +1,215 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2020, Google Inc. + * + * Test the buffer cache different operation modes + */ + +#include +#include +#include + +#include + +#include "buffer_source.h" + +#include "test.h" + +using namespace libcamera; + +namespace { + +class BufferCacheTest : public Test +{ +public: + /* + * Test that a cache with the same size as there are buffers results in + * a sequential run over; 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, ... + * + * The test is only valid when the cache size is as least as big as the + * number of buffers. + */ + int testSequential(V4L2BufferCache *cache, + const std::vector> &buffers) + { + for (unsigned int i = 0; i < buffers.size() * 100; i++) { + int nBuffer = i % buffers.size(); + int index = cache->get(*buffers[nBuffer].get()); + + if (index != nBuffer) { + std::cout << "Expected index " << nBuffer + << " got " << index << std::endl; + return TestFail; + } + + cache->put(index); + } + + return TestPass; + } + + /* + * Test that randomly putting buffers to the cache always results in a + * valid index. + */ + int testRandom(V4L2BufferCache *cache, + const std::vector> &buffers) + { + std::uniform_int_distribution<> dist(0, buffers.size() - 1); + + for (unsigned int i = 0; i < buffers.size() * 100; i++) { + int nBuffer = dist(generator_); + int index = cache->get(*buffers[nBuffer].get()); + + if (index < 0) { + std::cout << "Failed lookup from cache" + << std::endl; + return TestFail; + } + + cache->put(index); + } + + return TestPass; + } + + /* + * Test that using a buffer more frequently keeps it hot in the cache at + * all times. + */ + int testHot(V4L2BufferCache *cache, + const std::vector> &buffers, + unsigned int hotFrequency) + { + /* Run the random test on the cache to make it messy. */ + if (testRandom(cache, buffers) != TestPass) + return TestFail; + + std::uniform_int_distribution<> dist(0, buffers.size() - 1); + + /* Pick a hot buffer at random and store its index. */ + int hotBuffer = dist(generator_); + int hotIndex = cache->get(*buffers[hotBuffer].get()); + cache->put(hotIndex); + + /* + * Queue hot buffer at the requested frequency and make sure + * it stays hot. + */ + for (unsigned int i = 0; i < buffers.size() * 100; i++) { + int nBuffer, index; + bool hotQueue = i % hotFrequency == 0; + + if (hotQueue) + nBuffer = hotBuffer; + else + nBuffer = dist(generator_); + + index = cache->get(*buffers[nBuffer].get()); + + if (index < 0) { + std::cout << "Failed lookup from cache" + << std::endl; + return TestFail; + } + + if (hotQueue && index != hotIndex) { + std::cout << "Hot buffer got cold" + << std::endl; + return TestFail; + } + + cache->put(index); + } + + return TestPass; + } + + int init() override + { + std::random_device rd; + unsigned int seed = rd(); + + std::cout << "Random seed is " << seed << std::endl; + + generator_.seed(seed); + + return TestPass; + } + + int run() override + { + const unsigned int numBuffers = 8; + + StreamConfiguration cfg; + cfg.pixelFormat = V4L2_PIX_FMT_YUYV; + cfg.size = Size(600, 800); + cfg.bufferCount = numBuffers; + + BufferSource source; + int ret = source.allocate(cfg); + if (ret != TestPass) + return ret; + + const std::vector> &buffers = + source.buffers(); + + if (buffers.size() != numBuffers) { + std::cout << "Got " << buffers.size() + << " buffers, expected " << numBuffers + << std::endl; + return TestFail; + } + + /* + * Test cache of same size as there are buffers, the cache is + * created from a list of buffers and will be pre-populated. + */ + V4L2BufferCache cacheFromBuffers(buffers); + + if (testSequential(&cacheFromBuffers, buffers) != TestPass) + return TestFail; + + if (testRandom(&cacheFromBuffers, buffers) != TestPass) + return TestFail; + + if (testHot(&cacheFromBuffers, buffers, numBuffers) != TestPass) + return TestFail; + + /* + * Test cache of same size as there are buffers, the cache is + * not pre-populated. + */ + V4L2BufferCache cacheFromNumbers(numBuffers); + + if (testSequential(&cacheFromNumbers, buffers) != TestPass) + return TestFail; + + if (testRandom(&cacheFromNumbers, buffers) != TestPass) + return TestFail; + + if (testHot(&cacheFromNumbers, buffers, numBuffers) != TestPass) + return TestFail; + + /* + * Test cache half the size of number of buffers used, the cache + * is not pre-populated. + */ + V4L2BufferCache cacheHalf(numBuffers / 2); + + if (testRandom(&cacheHalf, buffers) != TestPass) + return TestFail; + + if (testHot(&cacheHalf, buffers, numBuffers / 2) != TestPass) + return TestFail; + + return TestPass; + } + +private: + std::mt19937 generator_; +}; + +} /* namespace */ + +TEST_REGISTER(BufferCacheTest); diff --git a/test/v4l2_videodevice/meson.build b/test/v4l2_videodevice/meson.build index 5c52da7219c21cc3..685fcf6d16d72182 100644 --- a/test/v4l2_videodevice/meson.build +++ b/test/v4l2_videodevice/meson.build @@ -5,6 +5,7 @@ v4l2_videodevice_tests = [ [ 'controls', 'controls.cpp' ], [ 'formats', 'formats.cpp' ], [ 'request_buffers', 'request_buffers.cpp' ], + [ 'buffer_cache', 'buffer_cache.cpp' ], [ 'stream_on_off', 'stream_on_off.cpp' ], [ 'capture_async', 'capture_async.cpp' ], [ 'buffer_sharing', 'buffer_sharing.cpp' ], From patchwork Mon Feb 24 19:36:01 2020 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: 2878 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 15CB26265B for ; Mon, 24 Feb 2020 20:36:28 +0100 (CET) X-Halon-ID: ef92b543-573c-11ea-9f40-0050569116f7 Authorized-sender: niklas@soderlund.pp.se Received: from bismarck.berto.se (p4fca2392.dip0.t-ipconnect.de [79.202.35.146]) by bin-vsp-out-03.atm.binero.net (Halon) with ESMTPA id ef92b543-573c-11ea-9f40-0050569116f7; Mon, 24 Feb 2020 20:36:22 +0100 (CET) From: =?utf-8?q?Niklas_S=C3=B6derlund?= To: libcamera-devel@lists.libcamera.org Date: Mon, 24 Feb 2020 20:36:01 +0100 Message-Id: <20200224193601.1040770-5-niklas.soderlund@ragnatech.se> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200224193601.1040770-1-niklas.soderlund@ragnatech.se> References: <20200224193601.1040770-1-niklas.soderlund@ragnatech.se> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 4/4] libcamera: V4L2BufferCache: Improve cache eviction strategy 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, 24 Feb 2020 19:36:28 -0000 The strategy used to find a free cache entry in the first implementation was not the smartest, it picked the first free entry. This lead to unwanted performance issues as they cache was not used as good as it could for imported buffers. Improve this by adding a last usage timestamp to the cache entries and change the eviction strategy to use the oldest free entry instead of the first one it finds. Signed-off-by: Niklas Söderlund Reviewed-by: Naushir Patuck Reviewed-by: Jacopo Mondi --- src/libcamera/include/v4l2_videodevice.h | 1 + src/libcamera/v4l2_videodevice.cpp | 9 ++++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/libcamera/include/v4l2_videodevice.h b/src/libcamera/include/v4l2_videodevice.h index fcf072641420dacf..f04feed87b24f28f 100644 --- a/src/libcamera/include/v4l2_videodevice.h +++ b/src/libcamera/include/v4l2_videodevice.h @@ -125,6 +125,7 @@ private: bool operator==(const FrameBuffer &buffer); bool free; + utils::time_point lastUsed; private: struct Plane { diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp index 99470ce11421c77c..9a4d2479b20f5e27 100644 --- a/src/libcamera/v4l2_videodevice.cpp +++ b/src/libcamera/v4l2_videodevice.cpp @@ -205,6 +205,7 @@ int V4L2BufferCache::get(const FrameBuffer &buffer) { bool hit = false; int use = -1; + utils::time_point oldest = utils::clock::now(); for (unsigned int index = 0; index < cache_.size(); index++) { const Entry &entry = cache_[index]; @@ -212,8 +213,10 @@ int V4L2BufferCache::get(const FrameBuffer &buffer) if (!entry.free) continue; - if (use < 0) + if (cache_[index].lastUsed < oldest) { use = index; + oldest = cache_[index].lastUsed; + } /* Try to find a cache hit by comparing the planes. */ if (cache_[index] == buffer) { @@ -245,12 +248,12 @@ void V4L2BufferCache::put(unsigned int index) } V4L2BufferCache::Entry::Entry() - : free(true) + : free(true), lastUsed(utils::clock::now()) { } V4L2BufferCache::Entry::Entry(bool free, const FrameBuffer &buffer) - : free(free) + : free(free), lastUsed(utils::clock::now()) { for (const FrameBuffer::Plane &plane : buffer.planes()) planes_.emplace_back(plane);