summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPaul Elder <paul.elder@ideasonboard.com>2020-07-04 17:49:33 +0900
committerPaul Elder <paul.elder@ideasonboard.com>2020-07-10 16:11:05 +0900
commit8addae2583d68aaa4e904d7607b1d94ddc371860 (patch)
tree358b6baa5f71da32048a01d2dca5b9a594756c5b /src
parentfad0314bc909cf74444ef97b74b2a933a194e986 (diff)
libcamera: StreamConfiguration: Add frameSize field
In addition to the stride field, we want the pipeline handler to be able to declare the frame size for the configuration. Add a frameSize field to StreamConfiguration for this purpose. Signed-off-by: Paul Elder <paul.elder@ideasonboard.com> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Diffstat (limited to 'src')
-rw-r--r--src/libcamera/stream.cpp17
1 files changed, 14 insertions, 3 deletions
diff --git a/src/libcamera/stream.cpp b/src/libcamera/stream.cpp
index 6df5882f..a3c015fe 100644
--- a/src/libcamera/stream.cpp
+++ b/src/libcamera/stream.cpp
@@ -279,7 +279,8 @@ SizeRange StreamFormats::range(const PixelFormat &pixelformat) const
* handlers provide StreamFormats.
*/
StreamConfiguration::StreamConfiguration()
- : pixelFormat(0), stride(0), bufferCount(0), stream_(nullptr)
+ : pixelFormat(0), stride(0), frameSize(0), bufferCount(0),
+ stream_(nullptr)
{
}
@@ -287,8 +288,8 @@ StreamConfiguration::StreamConfiguration()
* \brief Construct a configuration with stream formats
*/
StreamConfiguration::StreamConfiguration(const StreamFormats &formats)
- : pixelFormat(0), stride(0), bufferCount(0), stream_(nullptr),
- formats_(formats)
+ : pixelFormat(0), stride(0), frameSize(0), bufferCount(0),
+ stream_(nullptr), formats_(formats)
{
}
@@ -316,6 +317,16 @@ StreamConfiguration::StreamConfiguration(const StreamFormats &formats)
*/
/**
+ * \var StreamConfiguration::frameSize
+ * \brief Frame size for the stream, in bytes
+ *
+ * The frameSize value reports the number of bytes necessary to contain one
+ * frame of an image buffer for this stream. This total includes the bytes
+ * required for all image planes. The value is valid after successfully
+ * validating the configuration with a call to CameraConfiguration::validate().
+ */
+
+/**
* \var StreamConfiguration::bufferCount
* \brief Requested number of buffers to allocate for the stream
*/
='n188' href='#n188'>188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2021, Google Inc.
 *
 * Test:
 * - Multiple reconfigurations of the Camera without stopping the CameraManager
 * - Validate there are no file descriptor leaks when using IPC
 */

#include <dirent.h>
#include <fstream>
#include <iostream>

#include <libcamera/base/event_dispatcher.h>
#include <libcamera/base/file.h>
#include <libcamera/base/thread.h>
#include <libcamera/base/timer.h>

#include <libcamera/framebuffer_allocator.h>

#include "camera_test.h"
#include "test.h"

using namespace std;

namespace {

class CameraReconfigure : public CameraTest, public Test
{
public:
	/* Initialize CameraTest with isolated IPA */
	CameraReconfigure()
		: CameraTest(kCamId_, true)
	{
	}

private:
	static constexpr const char *kCamId_ = "platform/vimc.0 Sensor B";
	static constexpr const char *kIpaProxyName_ = "vimc_ipa_proxy";
	static constexpr unsigned int kNumOfReconfigures_ = 10;

	void requestComplete(Request *request)
	{
		if (request->status() != Request::RequestComplete)
			return;

		const Request::BufferMap &buffers = request->buffers();

		const Stream *stream = buffers.begin()->first;
		FrameBuffer *buffer = buffers.begin()->second;

		/* Reuse the request and re-queue it with the same buffers. */
		request->reuse();
		request->addBuffer(stream, buffer);
		camera_->queueRequest(request);
	}

	int startAndStop()
	{
		StreamConfiguration &cfg = config_->at(0);

		if (camera_->acquire()) {
			cerr << "Failed to acquire the camera" << endl;
			return TestFail;
		}

		if (camera_->configure(config_.get())) {
			cerr << "Failed to set default configuration" << endl;
			return TestFail;
		}

		Stream *stream = cfg.stream();

		/*
		 * The configuration is consistent so we can re-use the
		 * same buffer allocation for each run.
		 */
		if (!allocated_) {
			int ret = allocator_->allocate(stream);
			if (ret < 0) {
				cerr << "Failed to allocate buffers" << endl;
				return TestFail;
			}
			allocated_ = true;
		}

		for (const unique_ptr<FrameBuffer> &buffer : allocator_->buffers(stream)) {
			unique_ptr<Request> request = camera_->createRequest();
			if (!request) {
				cerr << "Failed to create request" << endl;
				return TestFail;
			}

			if (request->addBuffer(stream, buffer.get())) {
				cerr << "Failed to associate buffer with request" << endl;
				return TestFail;
			}

			requests_.push_back(move(request));
		}

		camera_->requestCompleted.connect(this, &CameraReconfigure::requestComplete);

		if (camera_->start()) {
			cerr << "Failed to start camera" << endl;
			return TestFail;
		}

		for (unique_ptr<Request> &request : requests_) {
			if (camera_->queueRequest(request.get())) {
				cerr << "Failed to queue request" << endl;
				return TestFail;
			}
		}

		EventDispatcher *dispatcher = Thread::current()->eventDispatcher();

		Timer timer;
		timer.start(100);
		while (timer.isRunning())
			dispatcher->processEvents();

		if (camera_->stop()) {
			cerr << "Failed to stop camera" << endl;
			return TestFail;
		}

		if (camera_->release()) {
			cerr << "Failed to release camera" << endl;
			return TestFail;
		}

		camera_->requestCompleted.disconnect(this, &CameraReconfigure::requestComplete);

		requests_.clear();

		return 0;
	}

	int fdsOpen(pid_t pid)
	{
		string proxyFdPath = "/proc/" + to_string(pid) + "/fd";
		DIR *dir;
		struct dirent *ptr;
		unsigned int openFds = 0;

		dir = opendir(proxyFdPath.c_str());
		if (dir == nullptr) {
			int err = errno;
			cerr << "Error opening " << proxyFdPath << ": "
			     << strerror(-err) << endl;
			return 0;
		}

		while ((ptr = readdir(dir)) != nullptr) {
			if ((strcmp(ptr->d_name, ".") == 0) ||
			    (strcmp(ptr->d_name, "..") == 0))
				continue;

			openFds++;
		}
		closedir(dir);

		return openFds;
	}

	pid_t findProxyPid()
	{
		string proxyPid;
		string proxyName(kIpaProxyName_);
		DIR *dir;
		struct dirent *ptr;

		dir = opendir("/proc");
		while ((ptr = readdir(dir)) != nullptr) {
			if (ptr->d_type != DT_DIR)
				continue;

			string pname("/proc/" + string(ptr->d_name) + "/comm");
			if (File::exists(pname.c_str())) {
				ifstream pfile(pname.c_str());
				string comm;
				getline(pfile, comm);
				pfile.close();

				proxyPid = comm == proxyName ? string(ptr->d_name) : "";
			}

			if (!proxyPid.empty())
				break;
		}
		closedir(dir);

		if (!proxyPid.empty())
			return atoi(proxyPid.c_str());

		return -1;
	}

	int init() override
	{
		if (status_ != TestPass)
			return status_;

		config_ = camera_->generateConfiguration({ StreamRole::StillCapture });
		if (!config_ || config_->size() != 1) {
			cerr << "Failed to generate default configuration" << endl;
			return TestFail;
		}

		allocator_ = make_unique<FrameBufferAllocator>(camera_);
		allocated_ = false;

		return TestPass;
	}

	int run() override
	{
		unsigned int openFdsAtStart = 0;
		unsigned int openFds = 0;

		pid_t proxyPid = findProxyPid();
		if (proxyPid < 0) {
			cerr << "Cannot find " << kIpaProxyName_
			     << " pid, exiting" << endl;
			return TestFail;
		}

		openFdsAtStart = fdsOpen(proxyPid);
		for (unsigned int i = 0; i < kNumOfReconfigures_; i++) {
			startAndStop();
			openFds = fdsOpen(proxyPid);
			if (openFds == 0) {
				cerr << "No open fds found whereas "
				     << "open fds at start: " << openFdsAtStart
				     << endl;
				return TestFail;
			}

			if (openFds != openFdsAtStart) {
				cerr << "Leaking fds for " << kIpaProxyName_
				     << " - Open fds: " << openFds << " vs "
				     << "Open fds at start: " << openFdsAtStart
				     << endl;
				return TestFail;
			}
		}

		return TestPass;
	}

	bool allocated_;

	vector<unique_ptr<Request>> requests_;

	unique_ptr<CameraConfiguration> config_;
	unique_ptr<FrameBufferAllocator> allocator_;
};

} /* namespace */

TEST_REGISTER(CameraReconfigure)