[libcamera-devel] [PATCH v4 3/6] lc-compliance: Factor common capture code into SimpleCapture

Nícolas F. R. A. Prado nfraprado at collabora.com
Thu May 6 20:02:46 CEST 2021


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 at collabora.com>
---
 src/lc-compliance/simple_capture.cpp | 142 ++++++++++++++++-----------
 src/lc-compliance/simple_capture.h   |  16 ++-
 2 files changed, 95 insertions(+), 63 deletions(-)

diff --git a/src/lc-compliance/simple_capture.cpp b/src/lc-compliance/simple_capture.cpp
index f961e71f1815..bb173ef1f71a 100644
--- a/src/lc-compliance/simple_capture.cpp
+++ b/src/lc-compliance/simple_capture.cpp
@@ -73,6 +73,53 @@ void SimpleCapture::stop()
 	allocator_->free(stream);
 }
 
+bool SimpleCapture::captureCompleted()
+{
+	captureCount_++;
+	if (captureCount_ >= captureLimit_) {
+		loop_->exit(0);
+		return true;
+	}
+	return false;
+}
+
+Results::Result SimpleCapture::queueRequests(Stream *stream,
+					     std::vector<std::unique_ptr<libcamera::Request>> &requests,
+					     const std::vector<std::unique_ptr<FrameBuffer>> &buffers)
+{
+	for (const std::unique_ptr<FrameBuffer> &buffer : buffers) {
+		std::unique_ptr<Request> request = camera_->createRequest();
+		if (!request) {
+			stop();
+			return { Results::Fail, "Can't create request" };
+		}
+
+		if (request->addBuffer(stream, buffer.get())) {
+			stop();
+			return { Results::Fail, "Can't set buffer for request" };
+		}
+
+		if (camera_->queueRequest(request.get()) < 0) {
+			stop();
+			return { Results::Fail, "Failed to queue request" };
+		}
+
+		requests.push_back(std::move(request));
+	}
+
+	return { Results::Pass, "Succesfully queued requests" };
+}
+
+int SimpleCapture::runCaptureSession()
+{
+	loop_ = new EventLoop();
+	int status = loop_->exec();
+	stop();
+	delete loop_;
+
+	return status;
+}
+
 /* SimpleCaptureBalanced */
 
 SimpleCaptureBalanced::SimpleCaptureBalanced(std::shared_ptr<Camera> camera)
@@ -80,6 +127,33 @@ SimpleCaptureBalanced::SimpleCaptureBalanced(std::shared_ptr<Camera> camera)
 {
 }
 
+Results::Result SimpleCaptureBalanced::queueRequests(Stream *stream,
+						     std::vector<std::unique_ptr<libcamera::Request>> &requests,
+						     const std::vector<std::unique_ptr<FrameBuffer>> &buffers)
+{
+	for (const std::unique_ptr<FrameBuffer> &buffer : buffers) {
+		std::unique_ptr<Request> request = camera_->createRequest();
+		if (!request) {
+			stop();
+			return { Results::Fail, "Can't create request" };
+		}
+
+		if (request->addBuffer(stream, buffer.get())) {
+			stop();
+			return { Results::Fail, "Can't set buffer for request" };
+		}
+
+		if (queueRequest(request.get()) < 0) {
+			stop();
+			return { Results::Fail, "Failed to queue request" };
+		}
+
+		requests.push_back(std::move(request));
+	}
+
+	return { Results::Pass, "Succesfully queued requests" };
+}
+
 Results::Result SimpleCaptureBalanced::capture(unsigned int numRequests)
 {
 	Results::Result ret = start();
@@ -103,33 +177,12 @@ Results::Result SimpleCaptureBalanced::capture(unsigned int numRequests)
 	captureCount_ = 0;
 	captureLimit_ = numRequests;
 
-	/* Queue the recommended number of reqeuests. */
 	std::vector<std::unique_ptr<libcamera::Request>> requests;
-	for (const std::unique_ptr<FrameBuffer> &buffer : buffers) {
-		std::unique_ptr<Request> request = camera_->createRequest();
-		if (!request) {
-			stop();
-			return { Results::Fail, "Can't create request" };
-		}
-
-		if (request->addBuffer(stream, buffer.get())) {
-			stop();
-			return { Results::Fail, "Can't set buffer for request" };
-		}
-
-		if (queueRequest(request.get()) < 0) {
-			stop();
-			return { Results::Fail, "Failed to queue request" };
-		}
+	ret = queueRequests(stream, requests, buffers);
+	if (ret.first != Results::Pass)
+		return ret;
 
-		requests.push_back(std::move(request));
-	}
-
-	/* Run capture session. */
-	loop_ = new EventLoop();
-	loop_->exec();
-	stop();
-	delete loop_;
+	runCaptureSession();
 
 	if (captureCount_ != captureLimit_)
 		return { Results::Fail, "Got " + std::to_string(captureCount_) +
@@ -150,11 +203,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))
@@ -180,44 +230,20 @@ Results::Result SimpleCaptureUnbalanced::capture(unsigned int numRequests)
 	captureCount_ = 0;
 	captureLimit_ = numRequests;
 
-	/* Queue the recommended number of reqeuests. */
 	std::vector<std::unique_ptr<libcamera::Request>> requests;
-	for (const std::unique_ptr<FrameBuffer> &buffer : buffers) {
-		std::unique_ptr<Request> request = camera_->createRequest();
-		if (!request) {
-			stop();
-			return { Results::Fail, "Can't create request" };
-		}
-
-		if (request->addBuffer(stream, buffer.get())) {
-			stop();
-			return { Results::Fail, "Can't set buffer for request" };
-		}
-
-		if (camera_->queueRequest(request.get()) < 0) {
-			stop();
-			return { Results::Fail, "Failed to queue request" };
-		}
-
-		requests.push_back(std::move(request));
-	}
+	ret = queueRequests(stream, requests, buffers);
+	if (ret.first != Results::Pass)
+		return ret;
 
-	/* Run capture session. */
-	loop_ = new EventLoop();
-	int status = loop_->exec();
-	stop();
-	delete loop_;
+	int status = runCaptureSession();
 
 	return { status ? Results::Fail : Results::Pass, "Unbalanced capture of " + std::to_string(numRequests) + " requests" };
 }
 
 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/lc-compliance/simple_capture.h b/src/lc-compliance/simple_capture.h
index 82e2c56a55f1..8d0f94f0c3e5 100644
--- a/src/lc-compliance/simple_capture.h
+++ b/src/lc-compliance/simple_capture.h
@@ -26,11 +26,19 @@ protected:
 
 	Results::Result start();
 	void stop();
+	Results::Result queueRequests(libcamera::Stream *stream,
+				      std::vector<std::unique_ptr<libcamera::Request>> &requests,
+				      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_;
@@ -46,10 +54,11 @@ public:
 private:
 	int queueRequest(libcamera::Request *request);
 	void requestComplete(libcamera::Request *request) override;
+	Results::Result queueRequests(libcamera::Stream *stream,
+				      std::vector<std::unique_ptr<libcamera::Request>> &requests,
+				      const std::vector<std::unique_ptr<libcamera::FrameBuffer>> &buffers);
 
 	unsigned int queueCount_;
-	unsigned int captureCount_;
-	unsigned int captureLimit_;
 };
 
 class SimpleCaptureUnbalanced : public SimpleCapture
@@ -61,9 +70,6 @@ public:
 
 private:
 	void requestComplete(libcamera::Request *request) override;
-
-	unsigned int captureCount_;
-	unsigned int captureLimit_;
 };
 
 #endif /* __LC_COMPLIANCE_SIMPLE_CAPTURE_H__ */
-- 
2.31.1



More information about the libcamera-devel mailing list