summaryrefslogtreecommitdiff
path: root/test/ipa/ipa_interface_test.cpp
blob: b8178366497789b6e0f64560beddf4562510ce48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2019, Google Inc.
 *
 * Test the IPA interface
 */

#include <fcntl.h>
#include <iostream>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include <libcamera/ipa/vimc_ipa_proxy.h>

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

#include "libcamera/internal/camera_manager.h"
#include "libcamera/internal/device_enumerator.h"
#include "libcamera/internal/ipa_manager.h"
#include "libcamera/internal/ipa_module.h"
#include "libcamera/internal/pipeline_handler.h"

#include "test.h"

using namespace libcamera;
using namespace std;
using namespace std::chrono_literals;

class IPAInterfaceTest : public Test, public Object
{
public:
	IPAInterfaceTest()
		: trace_(ipa::vimc::IPAOperationNone), notifier_(nullptr), fd_(-1)
	{
	}

	~IPAInterfaceTest()
	{
		delete notifier_;
		ipa_.reset();
		cameraManager_.reset();
	}

protected:
	int init() override
	{
		cameraManager_ = make_unique<CameraManager>();

		/* Create a pipeline handler for vimc. */
		const std::vector<PipelineHandlerFactoryBase *> &factories =
			PipelineHandlerFactoryBase::factories();
		for (const PipelineHandlerFactoryBase *factory : factories) {
			if (factory->name() == "vimc") {
				pipe_ = factory->create(cameraManager_.get());
				break;
			}
		}

		if (!pipe_) {
			cerr << "Vimc pipeline not found" << endl;
			return TestPass;
		}

		/* Create and open the communication FIFO. */
		int ret = mkfifo(ipa::vimc::VimcIPAFIFOPath.c_str(), S_IRUSR | S_IWUSR);
		if (ret) {
			ret = errno;
			cerr << "Failed to create IPA test FIFO at '"
			     << ipa::vimc::VimcIPAFIFOPath << "': " << strerror(ret)
			     << endl;
			return TestFail;
		}

		ret = open(ipa::vimc::VimcIPAFIFOPath.c_str(), O_RDONLY | O_NONBLOCK);
		if (ret < 0) {
			ret = errno;
			cerr << "Failed to open IPA test FIFO at '"
			     << ipa::vimc::VimcIPAFIFOPath << "': " << strerror(ret)
			     << endl;
			unlink(ipa::vimc::VimcIPAFIFOPath.c_str());
			return TestFail;
		}
		fd_ = ret;

		notifier_ = new EventNotifier(fd_, EventNotifier::Read, this);
		notifier_->activated.connect(this, &IPAInterfaceTest::readTrace);

		return TestPass;
	}

	int run() override
	{
		EventDispatcher *dispatcher = thread()->eventDispatcher();
		Timer timer;

		ipa_ = IPAManager::createIPA<ipa::vimc::IPAProxyVimc>(pipe_.get(), 0, 0);
		if (!ipa_) {
			cerr << "Failed to create VIMC IPA interface" << endl;
			return TestFail;
		}

		/* Test initialization of IPA module. */
		std::string conf = ipa_->configurationFile("vimc.conf");
		Flags<ipa::vimc::TestFlag> inFlags;
		Flags<ipa::vimc::TestFlag> outFlags;
		int ret = ipa_->init(IPASettings{ conf, "vimc" },
				     ipa::vimc::IPAOperationInit,
				     inFlags, &outFlags);
		if (ret < 0) {
			cerr << "IPA interface init() failed" << endl;
			return TestFail;
		}

		timer.start(1000ms);
		while (timer.isRunning() && trace_ != ipa::vimc::IPAOperationInit)
			dispatcher->processEvents();

		if (trace_ != ipa::vimc::IPAOperationInit) {
			cerr << "Failed to test IPA initialization sequence"
			     << endl;
			return TestFail;
		}

		/* Test start of IPA module. */
		ipa_->start();
		timer.start(1000ms);
		while (timer.isRunning() && trace_ != ipa::vimc::IPAOperationStart)
			dispatcher->processEvents();

		if (trace_ != ipa::vimc::IPAOperationStart) {
			cerr << "Failed to test IPA start sequence" << endl;
			return TestFail;
		}

		/* Test stop of IPA module. */
		ipa_->stop();
		timer.start(1000ms);
		while (timer.isRunning() && trace_ != ipa::vimc::IPAOperationStop)
			dispatcher->processEvents();

		if (trace_ != ipa::vimc::IPAOperationStop) {
			cerr << "Failed to test IPA stop sequence" << endl;
			return TestFail;
		}

		return TestPass;
	}

	void cleanup() override
	{
		close(fd_);
		unlink(ipa::vimc::VimcIPAFIFOPath.c_str());
	}

private:
	void readTrace()
	{
		ssize_t s = read(notifier_->fd(), &trace_, sizeof(trace_));
		if (s < 0) {
			int ret = errno;
			cerr << "Failed to read from IPA test FIFO at '"
			     << ipa::vimc::VimcIPAFIFOPath << "': " << strerror(ret)
			     << endl;
			trace_ = ipa::vimc::IPAOperationNone;
		}
	}

	std::shared_ptr<PipelineHandler> pipe_;
	std::unique_ptr<ipa::vimc::IPAProxyVimc> ipa_;
	std::unique_ptr<CameraManager> cameraManager_;
	enum ipa::vimc::IPAOperationCode trace_;
	EventNotifier *notifier_;
	int fd_;
};

TEST_REGISTER(IPAInterfaceTest)
">= RKISP1_CIF_ISP_GAMMA_OUT_MAX_SAMPLES_V10; hwHistogramWeightGridsSize_ = RKISP1_CIF_ISP_HISTOGRAM_WEIGHT_GRIDS_SIZE_V10; break; case RKISP1_V12: hwHistBinNMax_ = RKISP1_CIF_ISP_HIST_BIN_N_MAX_V12; hwGammaOutMaxSamples_ = RKISP1_CIF_ISP_GAMMA_OUT_MAX_SAMPLES_V12; hwHistogramWeightGridsSize_ = RKISP1_CIF_ISP_HISTOGRAM_WEIGHT_GRIDS_SIZE_V12; break; default: LOG(IPARkISP1, Error) << "Hardware revision " << hwRevision << " is currently not supported"; return -ENODEV; } LOG(IPARkISP1, Debug) << "Hardware revision is " << hwRevision; /* Cache the value to set it in configure. */ hwRevision_ = static_cast<rkisp1_cif_isp_version>(hwRevision); camHelper_ = CameraSensorHelperFactoryBase::create(settings.sensorModel); if (!camHelper_) { LOG(IPARkISP1, Error) << "Failed to create camera sensor helper for " << settings.sensorModel; return -ENODEV; } /* Load the tuning data file. */ File file(settings.configurationFile); if (!file.open(File::OpenModeFlag::ReadOnly)) { int ret = file.error(); LOG(IPARkISP1, Error) << "Failed to open configuration file " << settings.configurationFile << ": " << strerror(-ret); return ret; } std::unique_ptr<libcamera::YamlObject> data = YamlParser::parse(file); if (!data) return -EINVAL; unsigned int version = (*data)["version"].get<uint32_t>(0); if (version != 1) { LOG(IPARkISP1, Error) << "Invalid tuning file version " << version; return -EINVAL; } if (!data->contains("algorithms")) { LOG(IPARkISP1, Error) << "Tuning file doesn't contain any algorithm"; return -EINVAL; } int ret = createAlgorithms(context_, (*data)["algorithms"]); if (ret) return ret; /* Return the controls handled by the IPA. */ ControlInfoMap::Map ctrlMap = rkisp1Controls; *ipaControls = ControlInfoMap(std::move(ctrlMap), controls::controls); return 0; } int IPARkISP1::start() { setControls(0); return 0; } void IPARkISP1::stop() { context_.frameContexts.clear(); } /** * \todo The RkISP1 pipeline currently provides an empty IPACameraSensorInfo * if the connected sensor does not provide enough information to properly * assemble one. Make sure the reported sensor information are relevant * before accessing them. */ int IPARkISP1::configure([[maybe_unused]] const IPACameraSensorInfo &info, [[maybe_unused]] const std::map<uint32_t, IPAStream> &streamConfig, const std::map<uint32_t, ControlInfoMap> &entityControls) { if (entityControls.empty()) return -EINVAL; ctrls_ = entityControls.at(0); const auto itExp = ctrls_.find(V4L2_CID_EXPOSURE); if (itExp == ctrls_.end()) { LOG(IPARkISP1, Error) << "Can't find exposure control"; return -EINVAL; } const auto itGain = ctrls_.find(V4L2_CID_ANALOGUE_GAIN); if (itGain == ctrls_.end()) { LOG(IPARkISP1, Error) << "Can't find gain control"; return -EINVAL; } int32_t minExposure = itExp->second.min().get<int32_t>(); int32_t maxExposure = itExp->second.max().get<int32_t>(); int32_t minGain = itGain->second.min().get<int32_t>(); int32_t maxGain = itGain->second.max().get<int32_t>(); LOG(IPARkISP1, Debug) << "Exposure: [" << minExposure << ", " << maxExposure << "], gain: [" << minGain << ", " << maxGain << "]"; /* Clear the IPA context before the streaming session. */ context_.configuration = {}; context_.activeState = {}; context_.frameContexts.clear(); /* Set the hardware revision for the algorithms. */ context_.configuration.hw.revision = hwRevision_; context_.configuration.sensor.size = info.outputSize; context_.configuration.sensor.lineDuration = info.minLineLength * 1.0s / info.pixelRate; /* * When the AGC computes the new exposure values for a frame, it needs * to know the limits for shutter speed and analogue gain. * As it depends on the sensor, update it with the controls. * * \todo take VBLANK into account for maximum shutter speed */ context_.configuration.agc.minShutterSpeed = minExposure * context_.configuration.sensor.lineDuration; context_.configuration.agc.maxShutterSpeed = maxExposure * context_.configuration.sensor.lineDuration; context_.configuration.agc.minAnalogueGain = camHelper_->gain(minGain); context_.configuration.agc.maxAnalogueGain = camHelper_->gain(maxGain); for (auto const &algo : algorithms()) { int ret = algo->configure(context_, info); if (ret) return ret; } return 0; } void IPARkISP1::mapBuffers(const std::vector<IPABuffer> &buffers) { for (const IPABuffer &buffer : buffers) { auto elem = buffers_.emplace(std::piecewise_construct, std::forward_as_tuple(buffer.id), std::forward_as_tuple(buffer.planes)); const FrameBuffer &fb = elem.first->second; MappedFrameBuffer mappedBuffer(&fb, MappedFrameBuffer::MapFlag::ReadWrite); if (!mappedBuffer.isValid()) { LOG(IPARkISP1, Fatal) << "Failed to mmap buffer: " << strerror(mappedBuffer.error()); } mappedBuffers_.emplace(buffer.id, std::move(mappedBuffer)); } } void IPARkISP1::unmapBuffers(const std::vector<unsigned int> &ids) { for (unsigned int id : ids) { const auto fb = buffers_.find(id); if (fb == buffers_.end()) continue; mappedBuffers_.erase(id); buffers_.erase(id); } } void IPARkISP1::queueRequest(const uint32_t frame, const ControlList &controls) { IPAFrameContext &frameContext = context_.frameContexts.alloc(frame); for (auto const &algo : algorithms()) algo->queueRequest(context_, frame, frameContext, controls); } void IPARkISP1::fillParamsBuffer(const uint32_t frame, const uint32_t bufferId) { IPAFrameContext &frameContext = context_.frameContexts.get(frame); rkisp1_params_cfg *params = reinterpret_cast<rkisp1_params_cfg *>( mappedBuffers_.at(bufferId).planes()[0].data()); /* Prepare parameters buffer. */ memset(params, 0, sizeof(*params)); for (auto const &algo : algorithms()) algo->prepare(context_, frame, frameContext, params); paramsBufferReady.emit(frame); } void IPARkISP1::processStatsBuffer(const uint32_t frame, const uint32_t bufferId, const ControlList &sensorControls) { IPAFrameContext &frameContext = context_.frameContexts.get(frame); const rkisp1_stat_buffer *stats = reinterpret_cast<rkisp1_stat_buffer *>( mappedBuffers_.at(bufferId).planes()[0].data()); frameContext.sensor.exposure = sensorControls.get(V4L2_CID_EXPOSURE).get<int32_t>(); frameContext.sensor.gain = camHelper_->gain(sensorControls.get(V4L2_CID_ANALOGUE_GAIN).get<int32_t>()); ControlList metadata(controls::controls); for (auto const &algo : algorithms()) algo->process(context_, frame, frameContext, stats, metadata); setControls(frame); metadataReady.emit(frame, metadata); } void IPARkISP1::setControls(unsigned int frame) { /* * \todo The frame number is most likely wrong here, we need to take * internal sensor delays and other timing parameters into account. */ IPAFrameContext &frameContext = context_.frameContexts.get(frame); uint32_t exposure = frameContext.agc.exposure; uint32_t gain = camHelper_->gainCode(frameContext.agc.gain); ControlList ctrls(ctrls_); ctrls.set(V4L2_CID_EXPOSURE, static_cast<int32_t>(exposure)); ctrls.set(V4L2_CID_ANALOGUE_GAIN, static_cast<int32_t>(gain)); setSensorControls.emit(frame, ctrls); } } /* namespace ipa::rkisp1 */ /* * External IPA module interface */ extern "C" { const struct IPAModuleInfo ipaModuleInfo = { IPA_MODULE_API_VERSION, 1, "PipelineHandlerRkISP1", "rkisp1", }; IPAInterface *ipaCreate() { return new ipa::rkisp1::IPARkISP1(); } } } /* namespace libcamera */