From patchwork Fri May 21 13:30:50 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?TsOtY29sYXMgRi4gUi4gQS4gUHJhZG8=?= X-Patchwork-Id: 12346 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 8F81CC31FF for ; Fri, 21 May 2021 13:31:53 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 52A7B68920; Fri, 21 May 2021 15:31:53 +0200 (CEST) Received: from bhuna.collabora.co.uk (bhuna.collabora.co.uk [IPv6:2a00:1098:0:82:1000:25:2eeb:e3e3]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id BF89768911 for ; Fri, 21 May 2021 15:31:52 +0200 (CEST) Received: from localhost.localdomain (unknown [IPv6:2804:14c:1a9:2978:fc03:8702:eda1:a1f8]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: nfraprado) by bhuna.collabora.co.uk (Postfix) with ESMTPSA id ED3A91F44074; Fri, 21 May 2021 14:31:50 +0100 (BST) From: =?utf-8?b?TsOtY29sYXMgRi4gUi4gQS4gUHJhZG8=?= To: libcamera-devel@lists.libcamera.org Date: Fri, 21 May 2021 10:30:50 -0300 Message-Id: <20210521133054.274502-2-nfraprado@collabora.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210521133054.274502-1-nfraprado@collabora.com> References: <20210521133054.274502-1-nfraprado@collabora.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v4 1/5] libcamera: camera: Make stop() idempotent 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: , Cc: kernel@collabora.com, =?utf-8?q?Andr=C3=A9_Almeida?= Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Make Camera::stop() idempotent so that it can be called in any state and consecutive times. When called in any state other than CameraRunning, it is a no-op. This simplifies the cleanup path for applications. Signed-off-by: Nícolas F. R. A. Prado Reviewed-by: Laurent Pinchart --- Changes in v4: - Thanks to Jacopo: - Added \todo in Camera::stop() No changes in v3 Changes in v2: - Suggested by Kieran: - Add new isRunning() function instead of relying on isAccessAllowed() - Suggested by Laurent: - Make stop()'s description clearer src/libcamera/camera.cpp | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/libcamera/camera.cpp b/src/libcamera/camera.cpp index 1340c266cc5f..be0428ecbd46 100644 --- a/src/libcamera/camera.cpp +++ b/src/libcamera/camera.cpp @@ -347,6 +347,7 @@ public: const std::set &streams); ~Private(); + bool isRunning() const; int isAccessAllowed(State state, bool allowDisconnected = false, const char *from = __builtin_FUNCTION()) const; int isAccessAllowed(State low, State high, @@ -388,6 +389,15 @@ static const char *const camera_state_names[] = { "Running", }; +bool Camera::Private::isRunning() const +{ + State currentState = state_.load(std::memory_order_acquire); + if (currentState == CameraRunning) + return true; + + return false; +} + int Camera::Private::isAccessAllowed(State state, bool allowDisconnected, const char *from) const { @@ -1061,9 +1071,10 @@ int Camera::start(const ControlList *controls) * This method stops capturing and processing requests immediately. All pending * requests are cancelled and complete synchronously in an error state. * - * \context This function may only be called when the camera is in the Running - * state as defined in \ref camera_operation, and shall be synchronized by the - * caller with other functions that affect the camera state. + * \context This function may be called in any camera state as defined in \ref + * camera_operation, and shall be synchronized by the caller with other + * functions that affect the camera state. If called when the camera isn't + * running, it is a no-op. * * \return 0 on success or a negative error code otherwise * \retval -ENODEV The camera has been disconnected from the system @@ -1073,6 +1084,13 @@ int Camera::stop() { Private *const d = LIBCAMERA_D_PTR(); + /* + * \todo Make calling stop() when not in 'Running' part of the state + * machine rather than take this shortcut + */ + if (!d->isRunning()) + return 0; + int ret = d->isAccessAllowed(Private::CameraRunning); if (ret < 0) return ret; From patchwork Fri May 21 13:30:51 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?TsOtY29sYXMgRi4gUi4gQS4gUHJhZG8=?= X-Patchwork-Id: 12347 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id D03B8C31FF for ; Fri, 21 May 2021 13:31:56 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 9CC9D6891F; Fri, 21 May 2021 15:31:56 +0200 (CEST) Received: from bhuna.collabora.co.uk (bhuna.collabora.co.uk [IPv6:2a00:1098:0:82:1000:25:2eeb:e3e3]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id EB88D68911 for ; Fri, 21 May 2021 15:31:54 +0200 (CEST) Received: from localhost.localdomain (unknown [IPv6:2804:14c:1a9:2978:fc03:8702:eda1:a1f8]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: nfraprado) by bhuna.collabora.co.uk (Postfix) with ESMTPSA id 208D11F44077; Fri, 21 May 2021 14:31:52 +0100 (BST) From: =?utf-8?b?TsOtY29sYXMgRi4gUi4gQS4gUHJhZG8=?= To: libcamera-devel@lists.libcamera.org Date: Fri, 21 May 2021 10:30:51 -0300 Message-Id: <20210521133054.274502-3-nfraprado@collabora.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210521133054.274502-1-nfraprado@collabora.com> References: <20210521133054.274502-1-nfraprado@collabora.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v4 2/5] lc-compliance: Make SimpleCapture::stop() idempotent 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: , Cc: kernel@collabora.com, =?utf-8?q?Andr=C3=A9_Almeida?= Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Make SimpleCapture::stop() be able to be called multiple times and at any point so that it can be called from the destructor and an assert failure can return immediately. Signed-off-by: Nícolas F. R. A. Prado Reviewed-by: Laurent Pinchart --- No changes in v4 No changes in v3 Changes in v2: - Suggested by Laurent: - Fixed code style src/lc-compliance/simple_capture.cpp | 33 +++++++++++----------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/src/lc-compliance/simple_capture.cpp b/src/lc-compliance/simple_capture.cpp index 64e862a08e3a..f90fe6d0f9aa 100644 --- a/src/lc-compliance/simple_capture.cpp +++ b/src/lc-compliance/simple_capture.cpp @@ -17,6 +17,7 @@ SimpleCapture::SimpleCapture(std::shared_ptr camera) SimpleCapture::~SimpleCapture() { + stop(); } Results::Result SimpleCapture::configure(StreamRole role) @@ -55,12 +56,17 @@ Results::Result SimpleCapture::start() void SimpleCapture::stop() { - Stream *stream = config_->at(0).stream(); + if (!config_) + return; camera_->stop(); camera_->requestCompleted.disconnect(this, &SimpleCapture::requestComplete); + if (!allocator_->allocated()) + return; + + Stream *stream = config_->at(0).stream(); allocator_->free(stream); } @@ -84,7 +90,6 @@ Results::Result SimpleCaptureBalanced::capture(unsigned int numRequests) if (buffers.size() > numRequests) { /* Cache buffers.size() before we destroy it in stop() */ int buffers_size = buffers.size(); - stop(); return { Results::Skip, "Camera needs " + std::to_string(buffers_size) + " requests, can't test only " + std::to_string(numRequests) }; @@ -98,20 +103,14 @@ Results::Result SimpleCaptureBalanced::capture(unsigned int numRequests) std::vector> requests; for (const std::unique_ptr &buffer : buffers) { std::unique_ptr request = camera_->createRequest(); - if (!request) { - stop(); + if (!request) return { Results::Fail, "Can't create request" }; - } - if (request->addBuffer(stream, buffer.get())) { - stop(); + if (request->addBuffer(stream, buffer.get())) return { Results::Fail, "Can't set buffer for request" }; - } - if (queueRequest(request.get()) < 0) { - stop(); + if (queueRequest(request.get()) < 0) return { Results::Fail, "Failed to queue request" }; - } requests.push_back(std::move(request)); } @@ -175,20 +174,14 @@ Results::Result SimpleCaptureUnbalanced::capture(unsigned int numRequests) std::vector> requests; for (const std::unique_ptr &buffer : buffers) { std::unique_ptr request = camera_->createRequest(); - if (!request) { - stop(); + if (!request) return { Results::Fail, "Can't create request" }; - } - if (request->addBuffer(stream, buffer.get())) { - stop(); + if (request->addBuffer(stream, buffer.get())) return { Results::Fail, "Can't set buffer for request" }; - } - if (camera_->queueRequest(request.get()) < 0) { - stop(); + if (camera_->queueRequest(request.get()) < 0) return { Results::Fail, "Failed to queue request" }; - } requests.push_back(std::move(request)); } From patchwork Fri May 21 13:30:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?TsOtY29sYXMgRi4gUi4gQS4gUHJhZG8=?= X-Patchwork-Id: 12348 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 68E01C31FF for ; Fri, 21 May 2021 13:31:58 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 3419468921; Fri, 21 May 2021 15:31:58 +0200 (CEST) Received: from bhuna.collabora.co.uk (bhuna.collabora.co.uk [46.235.227.227]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id C2E1168911 for ; Fri, 21 May 2021 15:31:57 +0200 (CEST) Received: from localhost.localdomain (unknown [IPv6:2804:14c:1a9:2978:fc03:8702:eda1:a1f8]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: nfraprado) by bhuna.collabora.co.uk (Postfix) with ESMTPSA id 433AF1F4407F; Fri, 21 May 2021 14:31:55 +0100 (BST) From: =?utf-8?b?TsOtY29sYXMgRi4gUi4gQS4gUHJhZG8=?= To: libcamera-devel@lists.libcamera.org Date: Fri, 21 May 2021 10:30:52 -0300 Message-Id: <20210521133054.274502-4-nfraprado@collabora.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210521133054.274502-1-nfraprado@collabora.com> References: <20210521133054.274502-1-nfraprado@collabora.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v4 3/5] lc-compliance: Add Environment singleton 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: , Cc: kernel@collabora.com, =?utf-8?q?Andr=C3=A9_Almeida?= Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Add a singleton Environment class in order to make the camera available inside all tests. This is needed for the Googletest refactor, otherwise the tests, which are statically declared, won't be able to access the camera. Signed-off-by: Nícolas F. R. A. Prado --- Added in v4 src/lc-compliance/environment.cpp | 35 +++++++++++++++++++++++++++++++ src/lc-compliance/environment.h | 31 +++++++++++++++++++++++++++ src/lc-compliance/meson.build | 1 + 3 files changed, 67 insertions(+) create mode 100644 src/lc-compliance/environment.cpp create mode 100644 src/lc-compliance/environment.h diff --git a/src/lc-compliance/environment.cpp b/src/lc-compliance/environment.cpp new file mode 100644 index 000000000000..de7fa5bbadd1 --- /dev/null +++ b/src/lc-compliance/environment.cpp @@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2021, Collabora Ltd. + * + * environment.cpp - Common environment for tests + */ + +#include "environment.h" + +Environment *Environment::instance_ = nullptr; +std::shared_ptr Environment::camera_ = nullptr; + +bool Environment::create(std::shared_ptr camera) +{ + if (instance_) + return false; + + camera_ = camera; + instance_ = new Environment; + return true; +} + +void Environment::destroy() +{ + if (!camera_) + return; + + camera_->release(); + camera_.reset(); +} + +Environment *Environment::instance() +{ + return instance_; +} diff --git a/src/lc-compliance/environment.h b/src/lc-compliance/environment.h new file mode 100644 index 000000000000..f4832d371b6c --- /dev/null +++ b/src/lc-compliance/environment.h @@ -0,0 +1,31 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2021, Collabora Ltd. + * + * environment.h - Common environment for tests + */ +#ifndef __LC_COMPLIANCE_ENVIRONMENT_H__ +#define __LC_COMPLIANCE_ENVIRONMENT_H__ + +#include + +using namespace libcamera; + +class Environment +{ +public: + static bool create(std::shared_ptr camera); + void destroy(); + + static Environment *instance(); + + std::shared_ptr camera() const { return camera_; } + +private: + Environment() {}; + + static Environment *instance_; + static std::shared_ptr camera_; +}; + +#endif /* __LC_COMPLIANCE_ENVIRONMENT_H__ */ diff --git a/src/lc-compliance/meson.build b/src/lc-compliance/meson.build index a2bfcceb1259..c287017575bd 100644 --- a/src/lc-compliance/meson.build +++ b/src/lc-compliance/meson.build @@ -14,6 +14,7 @@ lc_compliance_sources = files([ '../cam/options.cpp', 'main.cpp', 'results.cpp', + 'environment.cpp', 'simple_capture.cpp', 'single_stream.cpp', ]) From patchwork Fri May 21 13:30:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?TsOtY29sYXMgRi4gUi4gQS4gUHJhZG8=?= X-Patchwork-Id: 12349 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 8EDD9C31FF for ; Fri, 21 May 2021 13:32:01 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 591876891E; Fri, 21 May 2021 15:32:01 +0200 (CEST) Received: from bhuna.collabora.co.uk (bhuna.collabora.co.uk [46.235.227.227]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 15D5368911 for ; Fri, 21 May 2021 15:32:00 +0200 (CEST) Received: from localhost.localdomain (unknown [IPv6:2804:14c:1a9:2978:fc03:8702:eda1:a1f8]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: nfraprado) by bhuna.collabora.co.uk (Postfix) with ESMTPSA id E3F6A1F44082; Fri, 21 May 2021 14:31:57 +0100 (BST) From: =?utf-8?b?TsOtY29sYXMgRi4gUi4gQS4gUHJhZG8=?= To: libcamera-devel@lists.libcamera.org Date: Fri, 21 May 2021 10:30:53 -0300 Message-Id: <20210521133054.274502-5-nfraprado@collabora.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210521133054.274502-1-nfraprado@collabora.com> References: <20210521133054.274502-1-nfraprado@collabora.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v4 4/5] lc-compliance: Refactor using Googletest 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: , Cc: kernel@collabora.com, =?utf-8?q?Andr=C3=A9_Almeida?= Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Refactor lc-compliance using Googletest as the test framework. Signed-off-by: Nícolas F. R. A. Prado Reviewed-by: Niklas Söderlund --- Changes in v4: - Removed old lc-compliance results classes - Thanks to Niklas: - Improved naming of tests Changes in v3: - Thanks to Niklas: - Went back to static test registration, and created a Singleton Environment class to store the camera global variable - Added a nameParameters() function to give meaningful names to the test parameters, removing the need to register each test suite for every role src/lc-compliance/main.cpp | 71 +++++++------ src/lc-compliance/meson.build | 4 +- src/lc-compliance/results.cpp | 75 -------------- src/lc-compliance/results.h | 47 --------- src/lc-compliance/simple_capture.cpp | 74 +++++-------- src/lc-compliance/simple_capture.h | 9 +- src/lc-compliance/single_stream.cpp | 150 ++++++++++++++------------- src/lc-compliance/tests.h | 16 --- 8 files changed, 152 insertions(+), 294 deletions(-) delete mode 100644 src/lc-compliance/results.cpp delete mode 100644 src/lc-compliance/results.h delete mode 100644 src/lc-compliance/tests.h diff --git a/src/lc-compliance/main.cpp b/src/lc-compliance/main.cpp index 54cee54aa978..37884ff70a69 100644 --- a/src/lc-compliance/main.cpp +++ b/src/lc-compliance/main.cpp @@ -9,10 +9,12 @@ #include #include +#include + #include +#include "environment.h" #include "../cam/options.h" -#include "tests.h" using namespace libcamera; @@ -21,21 +23,34 @@ enum { OptHelp = 'h', }; +/* + * Make asserts act like exceptions, otherwise they only fail (or skip) the + * current function. From gtest documentation: + * https://google.github.io/googletest/advanced.html#asserting-on-subroutines-with-an-exception + */ +class ThrowListener : public testing::EmptyTestEventListener { + void OnTestPartResult(const testing::TestPartResult& result) override { + if (result.type() == testing::TestPartResult::kFatalFailure + || result.type() == testing::TestPartResult::kSkip) { + throw testing::AssertionException(result); + } + } +}; + class Harness { public: Harness(const OptionsParser::Options &options); ~Harness(); - int exec(); + int init(); + int run(int argc, char **argv); private: - int init(); void listCameras(); OptionsParser::Options options_; std::unique_ptr cm_; - std::shared_ptr camera_; }; Harness::Harness(const OptionsParser::Options &options) @@ -46,35 +61,15 @@ Harness::Harness(const OptionsParser::Options &options) Harness::~Harness() { - if (camera_) { - camera_->release(); - camera_.reset(); - } + Environment::instance()->destroy(); cm_->stop(); } -int Harness::exec() -{ - int ret = init(); - if (ret) - return ret; - - std::vector results; - - results.push_back(testSingleStream(camera_)); - - for (const Results &result : results) { - ret = result.summary(); - if (ret) - return ret; - } - - return 0; -} - int Harness::init() { + std::shared_ptr camera; + int ret = cm_->start(); if (ret) { std::cout << "Failed to start camera manager: " @@ -89,23 +84,34 @@ int Harness::init() } const std::string &cameraId = options_[OptCamera]; - camera_ = cm_->get(cameraId); - if (!camera_) { + camera = cm_->get(cameraId); + if (!camera) { std::cout << "Camera " << cameraId << " not found, available cameras:" << std::endl; listCameras(); return -ENODEV; } - if (camera_->acquire()) { + if (camera->acquire()) { std::cout << "Failed to acquire camera" << std::endl; return -EINVAL; } + Environment::create(camera); + std::cout << "Using camera " << cameraId << std::endl; return 0; } +int Harness::run(int argc, char **argv) +{ + ::testing::InitGoogleTest(&argc, argv); + + testing::UnitTest::GetInstance()->listeners().Append(new ThrowListener); + + return RUN_ALL_TESTS(); +} + void Harness::listCameras() { for (const std::shared_ptr &cam : cm_->cameras()) @@ -143,6 +149,9 @@ int main(int argc, char **argv) return EXIT_FAILURE; Harness harness(options); + ret = harness.init(); + if (ret) + return ret; - return harness.exec() ? EXIT_FAILURE : EXIT_SUCCESS; + return harness.run(argc, argv); } diff --git a/src/lc-compliance/meson.build b/src/lc-compliance/meson.build index c287017575bd..ae68844ac9bd 100644 --- a/src/lc-compliance/meson.build +++ b/src/lc-compliance/meson.build @@ -13,16 +13,18 @@ lc_compliance_sources = files([ '../cam/event_loop.cpp', '../cam/options.cpp', 'main.cpp', - 'results.cpp', 'environment.cpp', 'simple_capture.cpp', 'single_stream.cpp', ]) +gtest_dep = dependency('gtest') + lc_compliance = executable('lc-compliance', lc_compliance_sources, dependencies : [ libatomic, libcamera_dep, libevent, + gtest_dep, ], install : true) diff --git a/src/lc-compliance/results.cpp b/src/lc-compliance/results.cpp deleted file mode 100644 index f149f7859e28..000000000000 --- a/src/lc-compliance/results.cpp +++ /dev/null @@ -1,75 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-or-later */ -/* - * Copyright (C) 2020, Google Inc. - * - * results.cpp - Test result aggregator - */ - -#include "results.h" - -#include - -void Results::add(const Result &result) -{ - if (result.first == Pass) - passed_++; - else if (result.first == Fail) - failed_++; - else if (result.first == Skip) - skipped_++; - - printResult(result); -} - -void Results::add(Status status, const std::string &message) -{ - add({ status, message }); -} - -void Results::fail(const std::string &message) -{ - add(Fail, message); -} - -void Results::pass(const std::string &message) -{ - add(Pass, message); -} - -void Results::skip(const std::string &message) -{ - add(Skip, message); -} - -int Results::summary() const -{ - if (failed_ + passed_ + skipped_ != planned_) { - std::cout << "Planned and executed number of tests differ " - << failed_ + passed_ + skipped_ << " executed " - << planned_ << " planned" << std::endl; - - return -EINVAL; - } - - std::cout << planned_ << " tests executed, " - << passed_ << " tests passed, " - << skipped_ << " tests skipped and " - << failed_ << " tests failed " << std::endl; - - return 0; -} - -void Results::printResult(const Result &result) -{ - std::string prefix; - - /* \todo Make parsable as TAP. */ - if (result.first == Pass) - prefix = "PASS"; - else if (result.first == Fail) - prefix = "FAIL"; - else if (result.first == Skip) - prefix = "SKIP"; - - std::cout << "- " << prefix << ": " << result.second << std::endl; -} diff --git a/src/lc-compliance/results.h b/src/lc-compliance/results.h deleted file mode 100644 index 2a3722b841a6..000000000000 --- a/src/lc-compliance/results.h +++ /dev/null @@ -1,47 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-or-later */ -/* - * Copyright (C) 2020, Google Inc. - * - * results.h - Test result aggregator - */ -#ifndef __LC_COMPLIANCE_RESULTS_H__ -#define __LC_COMPLIANCE_RESULTS_H__ - -#include -#include - -/* \todo Check if result aggregator can be shared with self tests in test/ */ -class Results -{ -public: - enum Status { - Fail, - Pass, - Skip, - }; - - using Result = std::pair; - - Results(unsigned int planned) - : planned_(planned), passed_(0), failed_(0), skipped_(0) - { - } - - void add(const Result &result); - void add(Status status, const std::string &message); - void fail(const std::string &message); - void pass(const std::string &message); - void skip(const std::string &message); - - int summary() const; - -private: - void printResult(const Result &result); - - unsigned int planned_; - unsigned int passed_; - unsigned int failed_; - unsigned int skipped_; -}; - -#endif /* __LC_COMPLIANCE_RESULTS_H__ */ diff --git a/src/lc-compliance/simple_capture.cpp b/src/lc-compliance/simple_capture.cpp index f90fe6d0f9aa..7731eb16f8c2 100644 --- a/src/lc-compliance/simple_capture.cpp +++ b/src/lc-compliance/simple_capture.cpp @@ -5,6 +5,8 @@ * simple_capture.cpp - Simple capture helper */ +#include + #include "simple_capture.h" using namespace libcamera; @@ -20,38 +22,34 @@ SimpleCapture::~SimpleCapture() stop(); } -Results::Result SimpleCapture::configure(StreamRole role) +void SimpleCapture::configure(StreamRole role) { config_ = camera_->generateConfiguration({ role }); - if (!config_) - return { Results::Skip, "Role not supported by camera" }; + if (!config_) { + std::cout << "Role not supported by camera" << std::endl; + GTEST_SKIP(); + } if (config_->validate() != CameraConfiguration::Valid) { config_.reset(); - return { Results::Fail, "Configuration not valid" }; + FAIL() << "Configuration not valid"; } if (camera_->configure(config_.get())) { config_.reset(); - return { Results::Fail, "Failed to configure camera" }; + FAIL() << "Failed to configure camera"; } - - return { Results::Pass, "Configure camera" }; } -Results::Result SimpleCapture::start() +void SimpleCapture::start() { Stream *stream = config_->at(0).stream(); - if (allocator_->allocate(stream) < 0) - return { Results::Fail, "Failed to allocate buffers" }; + ASSERT_GE(allocator_->allocate(stream), 0) << "Failed to allocate buffers"; - if (camera_->start()) - return { Results::Fail, "Failed to start camera" }; + ASSERT_TRUE(!camera_->start()) << "Failed to start camera"; camera_->requestCompleted.connect(this, &SimpleCapture::requestComplete); - - return { Results::Pass, "Started camera" }; } void SimpleCapture::stop() @@ -77,22 +75,19 @@ SimpleCaptureBalanced::SimpleCaptureBalanced(std::shared_ptr camera) { } -Results::Result SimpleCaptureBalanced::capture(unsigned int numRequests) +void SimpleCaptureBalanced::capture(unsigned int numRequests) { - Results::Result ret = start(); - if (ret.first != Results::Pass) - return ret; + start(); Stream *stream = config_->at(0).stream(); const std::vector> &buffers = allocator_->buffers(stream); /* No point in testing less requests then the camera depth. */ if (buffers.size() > numRequests) { - /* Cache buffers.size() before we destroy it in stop() */ - int buffers_size = buffers.size(); - - return { Results::Skip, "Camera needs " + std::to_string(buffers_size) - + " requests, can't test only " + std::to_string(numRequests) }; + std::cout << "Camera needs " + std::to_string(buffers.size()) + + " requests, can't test only " + + std::to_string(numRequests) << std::endl; + GTEST_SKIP(); } queueCount_ = 0; @@ -103,14 +98,11 @@ Results::Result SimpleCaptureBalanced::capture(unsigned int numRequests) std::vector> requests; for (const std::unique_ptr &buffer : buffers) { std::unique_ptr request = camera_->createRequest(); - if (!request) - return { Results::Fail, "Can't create request" }; + ASSERT_TRUE(request) << "Can't create request"; - if (request->addBuffer(stream, buffer.get())) - return { Results::Fail, "Can't set buffer for request" }; + ASSERT_FALSE(request->addBuffer(stream, buffer.get())) << "Can't set buffer for request"; - if (queueRequest(request.get()) < 0) - return { Results::Fail, "Failed to queue request" }; + ASSERT_GE(queueRequest(request.get()), 0) << "Failed to queue request"; requests.push_back(std::move(request)); } @@ -121,12 +113,7 @@ Results::Result SimpleCaptureBalanced::capture(unsigned int numRequests) stop(); delete loop_; - if (captureCount_ != captureLimit_) - return { Results::Fail, "Got " + std::to_string(captureCount_) + - " request, wanted " + std::to_string(captureLimit_) }; - - return { Results::Pass, "Balanced capture of " + - std::to_string(numRequests) + " requests" }; + ASSERT_EQ(captureCount_, captureLimit_); } int SimpleCaptureBalanced::queueRequest(Request *request) @@ -158,11 +145,9 @@ SimpleCaptureUnbalanced::SimpleCaptureUnbalanced(std::shared_ptr camera) { } -Results::Result SimpleCaptureUnbalanced::capture(unsigned int numRequests) +void SimpleCaptureUnbalanced::capture(unsigned int numRequests) { - Results::Result ret = start(); - if (ret.first != Results::Pass) - return ret; + start(); Stream *stream = config_->at(0).stream(); const std::vector> &buffers = allocator_->buffers(stream); @@ -174,14 +159,11 @@ Results::Result SimpleCaptureUnbalanced::capture(unsigned int numRequests) std::vector> requests; for (const std::unique_ptr &buffer : buffers) { std::unique_ptr request = camera_->createRequest(); - if (!request) - return { Results::Fail, "Can't create request" }; + ASSERT_TRUE(request) << "Can't create request"; - if (request->addBuffer(stream, buffer.get())) - return { Results::Fail, "Can't set buffer for request" }; + ASSERT_FALSE(request->addBuffer(stream, buffer.get())) << "Can't set buffer for request"; - if (camera_->queueRequest(request.get()) < 0) - return { Results::Fail, "Failed to queue request" }; + ASSERT_GE(camera_->queueRequest(request.get()), 0) << "Failed to queue request"; requests.push_back(std::move(request)); } @@ -192,7 +174,7 @@ Results::Result SimpleCaptureUnbalanced::capture(unsigned int numRequests) stop(); delete loop_; - return { status ? Results::Fail : Results::Pass, "Unbalanced capture of " + std::to_string(numRequests) + " requests" }; + ASSERT_FALSE(status); } void SimpleCaptureUnbalanced::requestComplete(Request *request) diff --git a/src/lc-compliance/simple_capture.h b/src/lc-compliance/simple_capture.h index d9de53fb63a3..62984243a1fa 100644 --- a/src/lc-compliance/simple_capture.h +++ b/src/lc-compliance/simple_capture.h @@ -12,18 +12,17 @@ #include #include "../cam/event_loop.h" -#include "results.h" class SimpleCapture { public: - Results::Result configure(libcamera::StreamRole role); + void configure(libcamera::StreamRole role); protected: SimpleCapture(std::shared_ptr camera); virtual ~SimpleCapture(); - Results::Result start(); + void start(); void stop(); virtual void requestComplete(libcamera::Request *request) = 0; @@ -40,7 +39,7 @@ class SimpleCaptureBalanced : public SimpleCapture public: SimpleCaptureBalanced(std::shared_ptr camera); - Results::Result capture(unsigned int numRequests); + void capture(unsigned int numRequests); private: int queueRequest(libcamera::Request *request); @@ -56,7 +55,7 @@ class SimpleCaptureUnbalanced : public SimpleCapture public: SimpleCaptureUnbalanced(std::shared_ptr camera); - Results::Result capture(unsigned int numRequests); + void capture(unsigned int numRequests); private: void requestComplete(libcamera::Request *request) override; diff --git a/src/lc-compliance/single_stream.cpp b/src/lc-compliance/single_stream.cpp index 8318b42f42d6..a8ae2f0e355b 100644 --- a/src/lc-compliance/single_stream.cpp +++ b/src/lc-compliance/single_stream.cpp @@ -7,91 +7,95 @@ #include +#include + +#include "environment.h" #include "simple_capture.h" -#include "tests.h" using namespace libcamera; -Results::Result testRequestBalance(std::shared_ptr camera, - StreamRole role, unsigned int startCycles, - unsigned int numRequests) +const std::vector NUMREQUESTS = {1, 2, 3, 5, 8, 13, 21, 34, 55, 89}; +const std::vector ROLES = {Raw, StillCapture, VideoRecording, Viewfinder}; + +class FixedRequestsTest : + public testing::TestWithParam> { +}; + +std::string nameParameters(const testing::TestParamInfo& info) { + std::map rolesMap = {{Raw, "Raw"}, + {StillCapture, "StillCapture"}, + {VideoRecording, "VideoRecording"}, + {Viewfinder, "Viewfinder"}}; + + std::string roleName = rolesMap[std::get<0>(info.param)]; + std::string numRequestsName = std::to_string(std::get<1>(info.param)); + + return roleName + "_" + numRequestsName; +} + +/* + * Test single capture cycles + * + * Makes sure the camera completes the exact number of requests queued. Example + * failure is a camera that needs N+M requests queued to complete N requests to + * the application. + */ +TEST_P(FixedRequestsTest, BalancedSingleCycle) +{ + auto [role, numRequests] = GetParam(); + std::shared_ptr camera = Environment::instance()->camera(); + SimpleCaptureBalanced capture(camera); - Results::Result ret = capture.configure(role); - if (ret.first != Results::Pass) - return ret; + capture.configure(role); - for (unsigned int starts = 0; starts < startCycles; starts++) { - ret = capture.capture(numRequests); - if (ret.first != Results::Pass) - return ret; - } + capture.capture(numRequests); +}; - return { Results::Pass, "Balanced capture of " + - std::to_string(numRequests) + " requests with " + - std::to_string(startCycles) + " start cycles" }; -} +/* + * Test multiple start/stop cycles + * + * Makes sure the camera supports multiple start/stop cycles. Example failure is + * a camera that does not clean up correctly in its error path but is only + * tested by single-capture applications. + */ +TEST_P(FixedRequestsTest, BalancedMultiCycle) +{ + auto [role, numRequests] = GetParam(); + std::shared_ptr camera = Environment::instance()->camera(); + unsigned int numRepeats = 3; + + SimpleCaptureBalanced capture(camera); -Results::Result testRequestUnbalance(std::shared_ptr camera, - StreamRole role, unsigned int numRequests) + capture.configure(role); + + for (unsigned int starts = 0; starts < numRepeats; starts++) + capture.capture(numRequests); +}; + +/* + * Test unbalanced stop + * + * Makes sure the camera supports a stop with requests queued. Example failure + * is a camera that does not handle cancelation of buffers coming back from the + * video device while stopping. + */ +TEST_P(FixedRequestsTest, Unbalanced) { + auto [role, numRequests] = GetParam(); + std::shared_ptr camera = Environment::instance()->camera(); + SimpleCaptureUnbalanced capture(camera); - Results::Result ret = capture.configure(role); - if (ret.first != Results::Pass) - return ret; + capture.configure(role); - return capture.capture(numRequests); -} + capture.capture(numRequests); +}; -Results testSingleStream(std::shared_ptr camera) -{ - static const std::vector> roles = { - { "raw", Raw }, - { "still", StillCapture }, - { "video", VideoRecording }, - { "viewfinder", Viewfinder }, - }; - static const std::vector numRequests = { 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 }; - - Results results(numRequests.size() * roles.size() * 3); - - for (const auto &role : roles) { - std::cout << "= Test role " << role.first << std::endl; - /* - * Test single capture cycles - * - * Makes sure the camera completes the exact number of requests queued. - * Example failure is a camera that needs N+M requests queued to - * complete N requests to the application. - */ - std::cout << "* Test single capture cycles" << std::endl; - for (unsigned int num : numRequests) - results.add(testRequestBalance(camera, role.second, 1, num)); - - /* - * Test multiple start/stop cycles - * - * Makes sure the camera supports multiple start/stop cycles. - * Example failure is a camera that does not clean up correctly in its - * error path but is only tested by single-capture applications. - */ - std::cout << "* Test multiple start/stop cycles" << std::endl; - for (unsigned int num : numRequests) - results.add(testRequestBalance(camera, role.second, 3, num)); - - /* - * Test unbalanced stop - * - * Makes sure the camera supports a stop with requests queued. - * Example failure is a camera that does not handle cancelation - * of buffers coming back from the video device while stopping. - */ - std::cout << "* Test unbalanced stop" << std::endl; - for (unsigned int num : numRequests) - results.add(testRequestUnbalance(camera, role.second, num)); - } - - return results; -} + +INSTANTIATE_TEST_SUITE_P(RolesAndRequests, + FixedRequestsTest, + testing::Combine(testing::ValuesIn(ROLES), + testing::ValuesIn(NUMREQUESTS)), + nameParameters); diff --git a/src/lc-compliance/tests.h b/src/lc-compliance/tests.h deleted file mode 100644 index 396605214e4b..000000000000 --- a/src/lc-compliance/tests.h +++ /dev/null @@ -1,16 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-or-later */ -/* - * Copyright (C) 2020, Google Inc. - * - * tests.h - Test modules - */ -#ifndef __LC_COMPLIANCE_TESTS_H__ -#define __LC_COMPLIANCE_TESTS_H__ - -#include - -#include "results.h" - -Results testSingleStream(std::shared_ptr camera); - -#endif /* __LC_COMPLIANCE_TESTS_H__ */ From patchwork Fri May 21 13:30:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?TsOtY29sYXMgRi4gUi4gQS4gUHJhZG8=?= X-Patchwork-Id: 12350 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id D536AC31FF for ; Fri, 21 May 2021 13:32:03 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 8CBA368923; Fri, 21 May 2021 15:32:03 +0200 (CEST) Received: from bhuna.collabora.co.uk (bhuna.collabora.co.uk [IPv6:2a00:1098:0:82:1000:25:2eeb:e3e3]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 9C67368911 for ; Fri, 21 May 2021 15:32:02 +0200 (CEST) Received: from localhost.localdomain (unknown [IPv6:2804:14c:1a9:2978:fc03:8702:eda1:a1f8]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: nfraprado) by bhuna.collabora.co.uk (Postfix) with ESMTPSA id 5E99C1F44089; Fri, 21 May 2021 14:32:00 +0100 (BST) From: =?utf-8?b?TsOtY29sYXMgRi4gUi4gQS4gUHJhZG8=?= To: libcamera-devel@lists.libcamera.org Date: Fri, 21 May 2021 10:30:54 -0300 Message-Id: <20210521133054.274502-6-nfraprado@collabora.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210521133054.274502-1-nfraprado@collabora.com> References: <20210521133054.274502-1-nfraprado@collabora.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v4 5/5] lc-compliance: Add list and filter parameters 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: , Cc: kernel@collabora.com, =?utf-8?q?Andr=C3=A9_Almeida?= Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Add a --list parameter that lists all current tests (by mapping to googletest's --gtest_list_tests). Add a --filter 'filterString' parameter that filters the tests to run (by mapping to googletest's --gtest_filter). Signed-off-by: Nícolas F. R. A. Prado --- No changes in v4 src/lc-compliance/main.cpp | 71 +++++++++++++++++++++++++++++++++++--- 1 file changed, 66 insertions(+), 5 deletions(-) diff --git a/src/lc-compliance/main.cpp b/src/lc-compliance/main.cpp index 37884ff70a69..92ef9c1fa082 100644 --- a/src/lc-compliance/main.cpp +++ b/src/lc-compliance/main.cpp @@ -20,6 +20,8 @@ using namespace libcamera; enum { OptCamera = 'c', + OptList = 'l', + OptFilter = 'f', OptHelp = 'h', }; @@ -44,17 +46,25 @@ public: ~Harness(); int init(); - int run(int argc, char **argv); + int run(); + int buildGtestParameters(char *arg0); private: void listCameras(); OptionsParser::Options options_; std::unique_ptr cm_; + + const std::map gtestFlag_ = {{"list", "--gtest_list_tests"}, + {"filter", "--gtest_filter"}}; + + int gtestArgc_; + char **gtestArgv_; + std::string gtestFilterParam_; }; Harness::Harness(const OptionsParser::Options &options) - : options_(options) + : options_(options), gtestArgv_(nullptr) { cm_ = std::make_unique(); } @@ -64,6 +74,8 @@ Harness::~Harness() Environment::instance()->destroy(); cm_->stop(); + + free(gtestArgv_); } int Harness::init() @@ -77,6 +89,9 @@ int Harness::init() return ret; } + if (options_.isSet(OptList)) + return 0; + if (!options_.isSet(OptCamera)) { std::cout << "No camera specified, available cameras:" << std::endl; listCameras(); @@ -103,9 +118,9 @@ int Harness::init() return 0; } -int Harness::run(int argc, char **argv) +int Harness::run() { - ::testing::InitGoogleTest(&argc, argv); + ::testing::InitGoogleTest(>estArgc_, gtestArgv_); testing::UnitTest::GetInstance()->listeners().Append(new ThrowListener); @@ -118,12 +133,54 @@ void Harness::listCameras() std::cout << "- " << cam.get()->id() << std::endl; } +int Harness::buildGtestParameters(char *arg0) +{ + int argc = 0; + + /* + * +2 to have space for both the 0th argument that is needed but not + * used and the null at the end. + */ + gtestArgv_ = (char**) malloc((gtestFlag_.size() + 2) * sizeof(char*)); + if (!gtestArgv_) + return -ENOMEM; + + gtestArgv_[argc] = arg0; + argc++; + + if (options_.isSet(OptList)) { + gtestArgv_[argc] = const_cast(gtestFlag_.at("list").c_str()); + argc++; + } + + if (options_.isSet(OptFilter)) { + /* + * The filter flag needs to be passed as a single parameter, in + * the format --gtest_filter=filterStr + */ + const std::string &filter = options_[OptFilter]; + gtestFilterParam_ = gtestFlag_.at("filter") + "=" + filter; + + gtestArgv_[argc] = const_cast(gtestFilterParam_.c_str()); + argc++; + } + + gtestArgv_[argc] = 0; + gtestArgc_ = argc; + + return 0; +} + static int parseOptions(int argc, char **argv, OptionsParser::Options *options) { OptionsParser parser; parser.addOption(OptCamera, OptionString, "Specify which camera to operate on, by id", "camera", ArgumentRequired, "camera"); + parser.addOption(OptList, OptionNone, "List all tests and exit", "list"); + parser.addOption(OptFilter, OptionString, + "Specify which tests to run", "filter", + ArgumentRequired, "filter"); parser.addOption(OptHelp, OptionNone, "Display this help message", "help"); @@ -153,5 +210,9 @@ int main(int argc, char **argv) if (ret) return ret; - return harness.run(argc, argv); + ret = harness.buildGtestParameters(argv[0]); + if (ret) + return ret; + + return harness.run(); }