/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2019, Google Inc.
 *
 * libcamera V4L2 API tests
 */

#include <iostream>

#include <libcamera/buffer.h>
#include <libcamera/event_dispatcher.h>
#include <libcamera/timer.h>

#include "libcamera/internal/thread.h"

#include "v4l2_videodevice_test.h"

class CaptureAsyncTest : public V4L2VideoDeviceTest
{
public:
	CaptureAsyncTest()
		: V4L2VideoDeviceTest("vimc", "Raw Capture 0"), frames(0) {}

	void receiveBuffer(FrameBuffer *buffer)
	{
		std::cout << "Buffer received" << std::endl;
		frames++;

		/* Requeue the buffer for further use. */
		capture_->queueBuffer(buffer);
	}

protected:
	int run()
	{
		const unsigned int bufferCount = 8;

		EventDispatcher *dispatcher = Thread::current()->eventDispatcher();
		Timer timeout;
		int ret;

		ret = capture_->allocateBuffers(bufferCount, &buffers_);
		if (ret < 0) {
			std::cout << "Failed to allocate buffers" << std::endl;
			return TestFail;
		}

		capture_->bufferReady.connect(this, &CaptureAsyncTest::receiveBuffer);

		for (const std::unique_ptr<FrameBuffer> &buffer : buffers_) {
			if (capture_->queueBuffer(buffer.get())) {
				std::cout << "Failed to queue buffer" << std::endl;
				return TestFail;
			}
		}

		ret = capture_->streamOn();
		if (ret)
			return TestFail;

		timeout.start(10000);
		while (timeout.isRunning()) {
			dispatcher->processEvents();
			if (frames > 30)
				break;
		}

		if (frames < 1) {
			std::cout << "Failed to capture any frames within timeout." << std::endl;
			return TestFail;
		}

		if (frames < 30) {
			std::cout << "Failed to capture 30 frames within timeout." << std::endl;
			return TestFail;
		}

		std::cout << "Processed " << frames << " frames" << std::endl;

		ret = capture_->streamOff();
		if (ret)
			return TestFail;

		return TestPass;
	}

private:
	unsigned int frames;
};

TEST_REGISTER(CaptureAsyncTest)
class='right' method='get' action='/libcamera/libcamera.git/log/src/ipa/rpi/controller/ccm_status.h'>
<input type='hidden' name='h' value='v0.0.0'/><input type='hidden' name='id' value='fceaa7f3e2095a21170bcf5f113bac45cf89b91d'/><select name='qt'>
<option value='grep'>log msg</option>
<option value='author'>author</option>
<option value='committer'>committer</option>
<option value='range'>range</option>
</select>
<input class='txt' type='search' size='10' name='q' value=''/>
<input type='submit' value='search'/>
</form>
</td></tr></table>
<div class='path'>path: <a href='/libcamera/libcamera.git/tree/?h=v0.0.0&amp;id=fceaa7f3e2095a21170bcf5f113bac45cf89b91d'>root</a>/<a href='/libcamera/libcamera.git/tree/src?h=v0.0.0&amp;id=fceaa7f3e2095a21170bcf5f113bac45cf89b91d'>src</a>/<a href='/libcamera/libcamera.git/tree/src/ipa?h=v0.0.0&amp;id=fceaa7f3e2095a21170bcf5f113bac45cf89b91d'>ipa</a>/<a href='/libcamera/libcamera.git/tree/src/ipa/rpi?h=v0.0.0&amp;id=fceaa7f3e2095a21170bcf5f113bac45cf89b91d'>rpi</a>/<a href='/libcamera/libcamera.git/tree/src/ipa/rpi/controller?h=v0.0.0&amp;id=fceaa7f3e2095a21170bcf5f113bac45cf89b91d'>controller</a>/<a href='/libcamera/libcamera.git/tree/src/ipa/rpi/controller/ccm_status.h?h=v0.0.0&amp;id=fceaa7f3e2095a21170bcf5f113bac45cf89b91d'>ccm_status.h</a></div><div class='content'>blob: c81bcd42342d4ccd6ac466b17125941f631310a7 (<a href='/libcamera/libcamera.git/plain/src/ipa/rpi/controller/ccm_status.h?h=v0.0.0&amp;id=fceaa7f3e2095a21170bcf5f113bac45cf89b91d'>plain</a>)
<table summary='blob content' class='blob'>
<tr><td class='linenumbers'><pre><a id='n1' href='#n1'>1</a>
<a id='n2' href='#n2'>2</a>
<a id='n3' href='#n3'>3</a>
<a id='n4' href='#n4'>4</a>
<a id='n5' href='#n5'>5</a>
<a id='n6' href='#n6'>6</a>
<a id='n7' href='#n7'>7</a>
<a id='n8' href='#n8'>8</a>
<a id='n9' href='#n9'>9</a>
<a id='n10' href='#n10'>10</a>
<a id='n11' href='#n11'>11</a>
<a id='n12' href='#n12'>12</a>
<a id='n13' href='#n13'>13</a>
<a id='n14' href='#n14'>14</a>
</pre></td>
<td class='lines'><pre><code><span class="hl com">/* SPDX-License-Identifier: BSD-2-Clause */</span>
<span class="hl com">/*</span>
<span class="hl com"> * Copyright (C) 2019, Raspberry Pi Ltd</span>
<span class="hl com"> *</span>
<span class="hl com"> * CCM (colour correction matrix) control algorithm status</span>
<span class="hl com"> */</span>
<span class="hl ppc">#pragma once</span>

<span class="hl com">/* The &quot;ccm&quot; algorithm generates an appropriate colour matrix. */</span>

<span class="hl kwb">struct</span> CcmStatus <span class="hl opt">{</span>
	<span class="hl kwb">double</span> matrix<span class="hl opt">[</span><span class="hl num">9</span><span class="hl opt">];</span>
	<span class="hl kwb">double</span> saturation<span class="hl opt">;</span>
<span class="hl opt">};</span>