[libcamera-devel,v10,14/19] lc-compliance: Factor common capture code into SimpleCapture
diff mbox series

Message ID 20221228223003.2265712-15-paul.elder@ideasonboard.com
State New
Headers show
Series
  • lc-compliance: Add test to queue more requests than hardware depth
Related show

Commit Message

Paul Elder Dec. 28, 2022, 10:29 p.m. UTC
From: NĂ­colas F. R. A. Prado <nfraprado@collabora.com>

Factor common code from SimpleCapture* subclasses into the SimpleCapture
parent class in order to avoid duplicated code.

Signed-off-by: NĂ­colas F. R. A. Prado <nfraprado@collabora.com>
Reviewed-by: Paul Elder <paul.elder@ideasonboard.com>
Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>

---
Changes in v9:
- rebased

Changes in v8:
- Added requests_ member to SimpleCapture to hold ownership of queued
  requests during capture
- Made queueRequest() virtual and removed SimpleCaptureBalanced::queueRequests()

Changes in v6:
- Switched queueRequests()'s 'buffers' and 'requests' parameters order, since
  'requests' is an output variable
- Added comment to runCaptureSession()
- Added missing blank line before return in captureCompleted()
- Added blank line to runCaptureSession()
---
 src/apps/lc-compliance/simple_capture.cpp | 89 +++++++++++++----------
 src/apps/lc-compliance/simple_capture.h   | 15 ++--
 2 files changed, 58 insertions(+), 46 deletions(-)

Patch
diff mbox series

diff --git a/src/apps/lc-compliance/simple_capture.cpp b/src/apps/lc-compliance/simple_capture.cpp
index 1e3e816a..6dd5c7af 100644
--- a/src/apps/lc-compliance/simple_capture.cpp
+++ b/src/apps/lc-compliance/simple_capture.cpp
@@ -77,6 +77,49 @@  void SimpleCapture::stop()
 	allocator_->free(stream);
 }
 
+bool SimpleCapture::captureCompleted()
+{
+	captureCount_++;
+	if (captureCount_ >= captureLimit_) {
+		loop_->exit(0);
+		return true;
+	}
+
+	return false;
+}
+
+int SimpleCapture::queueRequest(Request *request)
+{
+	return camera_->queueRequest(request);
+}
+
+void SimpleCapture::queueRequests(Stream *stream,
+				  const std::vector<std::unique_ptr<FrameBuffer>> &buffers)
+{
+	for (const std::unique_ptr<FrameBuffer> &buffer : buffers) {
+		std::unique_ptr<Request> request = camera_->createRequest();
+		ASSERT_TRUE(request) << "Can't create request";
+
+		ASSERT_EQ(request->addBuffer(stream, buffer.get()), 0) << "Can't set buffer for request";
+
+		ASSERT_EQ(queueRequest(request.get()), 0) << "Failed to queue request";
+
+		requests_.push_back(std::move(request));
+	}
+}
+
+int SimpleCapture::runCaptureSession()
+{
+	loop_ = new EventLoop();
+	int status = loop_->exec();
+
+	/* After session ended */
+	stop();
+	delete loop_;
+
+	return status;
+}
+
 /* SimpleCaptureBalanced */
 
 SimpleCaptureBalanced::SimpleCaptureBalanced(std::shared_ptr<Camera> camera)
@@ -103,23 +146,9 @@  void SimpleCaptureBalanced::capture(unsigned int numRequests)
 	captureCount_ = 0;
 	captureLimit_ = numRequests;
 
-	/* Queue the recommended number of reqeuests. */
-	for (const std::unique_ptr<FrameBuffer> &buffer : buffers) {
-		std::unique_ptr<Request> request = camera_->createRequest();
-		ASSERT_TRUE(request) << "Can't create request";
-
-		ASSERT_EQ(request->addBuffer(stream, buffer.get()), 0) << "Can't set buffer for request";
+	queueRequests(stream, buffers);
 
-		ASSERT_EQ(queueRequest(request.get()), 0) << "Failed to queue request";
-
-		requests_.push_back(std::move(request));
-	}
-
-	/* Run capture session. */
-	loop_ = new EventLoop();
-	loop_->exec();
-	stop();
-	delete loop_;
+	runCaptureSession();
 
 	ASSERT_EQ(captureCount_, captureLimit_);
 }
@@ -135,11 +164,8 @@  int SimpleCaptureBalanced::queueRequest(Request *request)
 
 void SimpleCaptureBalanced::requestComplete(Request *request)
 {
-	captureCount_++;
-	if (captureCount_ >= captureLimit_) {
-		loop_->exit(0);
+	if (captureCompleted())
 		return;
-	}
 
 	request->reuse(Request::ReuseBuffers);
 	if (queueRequest(request))
@@ -163,34 +189,17 @@  void SimpleCaptureUnbalanced::capture(unsigned int numRequests)
 	captureCount_ = 0;
 	captureLimit_ = numRequests;
 
-	/* Queue the recommended number of reqeuests. */
-	for (const std::unique_ptr<FrameBuffer> &buffer : buffers) {
-		std::unique_ptr<Request> request = camera_->createRequest();
-		ASSERT_TRUE(request) << "Can't create request";
-
-		ASSERT_EQ(request->addBuffer(stream, buffer.get()), 0) << "Can't set buffer for request";
+	queueRequests(stream, buffers);
 
-		ASSERT_EQ(camera_->queueRequest(request.get()), 0) << "Failed to queue request";
-
-		requests_.push_back(std::move(request));
-	}
-
-	/* Run capture session. */
-	loop_ = new EventLoop();
-	int status = loop_->exec();
-	stop();
-	delete loop_;
+	int status = runCaptureSession();
 
 	ASSERT_EQ(status, 0);
 }
 
 void SimpleCaptureUnbalanced::requestComplete(Request *request)
 {
-	captureCount_++;
-	if (captureCount_ >= captureLimit_) {
-		loop_->exit(0);
+	if (captureCompleted())
 		return;
-	}
 
 	request->reuse(Request::ReuseBuffers);
 	if (camera_->queueRequest(request))
diff --git a/src/apps/lc-compliance/simple_capture.h b/src/apps/lc-compliance/simple_capture.h
index b3091547..89ecc71c 100644
--- a/src/apps/lc-compliance/simple_capture.h
+++ b/src/apps/lc-compliance/simple_capture.h
@@ -25,11 +25,19 @@  protected:
 
 	void start();
 	void stop();
+	virtual int queueRequest(libcamera::Request *request);
+	void queueRequests(libcamera::Stream *stream,
+			   const std::vector<std::unique_ptr<libcamera::FrameBuffer>> &buffers);
+	bool captureCompleted();
+	int runCaptureSession();
 
 	virtual void requestComplete(libcamera::Request *request) = 0;
 
 	EventLoop *loop_;
 
+	unsigned int captureCount_;
+	unsigned int captureLimit_;
+
 	std::shared_ptr<libcamera::Camera> camera_;
 	std::unique_ptr<libcamera::FrameBufferAllocator> allocator_;
 	std::unique_ptr<libcamera::CameraConfiguration> config_;
@@ -44,12 +52,10 @@  public:
 	void capture(unsigned int numRequests);
 
 private:
-	int queueRequest(libcamera::Request *request);
+	int queueRequest(libcamera::Request *request) override;
 	void requestComplete(libcamera::Request *request) override;
 
 	unsigned int queueCount_;
-	unsigned int captureCount_;
-	unsigned int captureLimit_;
 };
 
 class SimpleCaptureUnbalanced : public SimpleCapture
@@ -61,7 +67,4 @@  public:
 
 private:
 	void requestComplete(libcamera::Request *request) override;
-
-	unsigned int captureCount_;
-	unsigned int captureLimit_;
 };