/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2020, Google Inc.
 *
 * libcamera internal MappedBuffer tests
 */

#include <iostream>

#include <libcamera/framebuffer_allocator.h>

#include "libcamera/internal/mapped_framebuffer.h"

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

using namespace libcamera;
using namespace std;

namespace {

class MappedBufferTest : public CameraTest, public Test
{
public:
	MappedBufferTest()
		: CameraTest("platform/vimc.0 Sensor B")
	{
	}

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

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

		allocator_ = new FrameBufferAllocator(camera_);

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

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

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

		stream_ = cfg.stream();

		int ret = allocator_->allocate(stream_);
		if (ret < 0)
			return TestFail;

		return TestPass;
	}

	void cleanup() override
	{
		delete allocator_;
	}

	int run() override
	{
		const std::unique_ptr<FrameBuffer> &buffer = allocator_->buffers(stream_).front();
		std::vector<MappedBuffer> maps;

		MappedFrameBuffer map(buffer.get(), MappedFrameBuffer::MapFlag::Read);
		if (!map.isValid()) {
			cout << "Failed to successfully map buffer" << endl;
			return TestFail;
		}

		/* Make sure we can move it. */
		maps.emplace_back(std::move(map));

		/* But copying is prevented, it would cause double-unmap. */
		// MappedFrameBuffer map_copy = map;

		/* Local map should be invalid (after move). */
		if (map.isValid()) {
			cout << "Post-move map should not be valid" << endl;
			return TestFail;
		}

		/* Test for multiple successful maps on the same buffer. */
		MappedFrameBuffer write_map(buffer.get(), MappedFrameBuffer::MapFlag::Write);
		if (!write_map.isValid()) {
			cout << "Failed to map write buffer" << endl;
			return TestFail;
		}

		MappedFrameBuffer rw_map(buffer.get(), MappedFrameBuffer::MapFlag::ReadWrite);
		if (!rw_map.isValid()) {
			cout << "Failed to map RW buffer" << endl;
			return TestFail;
		}

		return TestPass;
	}

private:
	std::unique_ptr<CameraConfiguration> config_;
	FrameBufferAllocator *allocator_;
	Stream *stream_;
};

} /* namespace */

TEST_REGISTER(MappedBufferTest)
ter</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/pinchartl/libcamera.git/tree/?id=206fada99d8774fb7a9b4f1924f6caeccafdb9a1'>root</a>/<a href='/libcamera/pinchartl/libcamera.git/tree/src?id=206fada99d8774fb7a9b4f1924f6caeccafdb9a1'>src</a>/<a href='/libcamera/pinchartl/libcamera.git/tree/src/qcam?id=206fada99d8774fb7a9b4f1924f6caeccafdb9a1'>qcam</a>/<a href='/libcamera/pinchartl/libcamera.git/tree/src/qcam/assets?id=206fada99d8774fb7a9b4f1924f6caeccafdb9a1'>assets</a>/<a href='/libcamera/pinchartl/libcamera.git/tree/src/qcam/assets/feathericons?id=206fada99d8774fb7a9b4f1924f6caeccafdb9a1'>feathericons</a>/<a href='/libcamera/pinchartl/libcamera.git/tree/src/qcam/assets/feathericons/bell.svg?id=206fada99d8774fb7a9b4f1924f6caeccafdb9a1'>bell.svg</a></div><div class='content'>blob: bba561c19b2f3dbbbb8182e2c6a6760dbe067cf0 (<a href='/libcamera/pinchartl/libcamera.git/plain/src/qcam/assets/feathericons/bell.svg?id=206fada99d8774fb7a9b4f1924f6caeccafdb9a1'>plain</a>)
<table summary='blob content' class='blob'>
<tr><td class='linenumbers'><pre><a id='n1' href='#n1'>1</a>
</pre></td>
<td class='lines'><pre><code><span class="hl kwa">&lt;svg</span> <span class="hl kwb">xmlns</span>=<span class="hl str">&quot;http://www.w3.org/2000/svg&quot;</span> <span class="hl kwb">width</span>=<span class="hl str">&quot;24&quot;</span> <span class="hl kwb">height</span>=<span class="hl str">&quot;24&quot;</span> <span class="hl kwb">viewBox</span>=<span class="hl str">&quot;0 0 24 24&quot;</span> <span class="hl kwb">fill</span>=<span class="hl str">&quot;none&quot;</span> <span class="hl kwb">stroke</span>=<span class="hl str">&quot;currentColor&quot;</span> <span class="hl kwb">stroke-width</span>=<span class="hl str">&quot;2&quot;</span> <span class="hl kwb">stroke-linecap</span>=<span class="hl str">&quot;round&quot;</span> <span class="hl kwb">stroke-linejoin</span>=<span class="hl str">&quot;round&quot;</span> <span class="hl kwb">class</span>=<span class="hl str">&quot;feather feather-bell&quot;</span><span class="hl kwa">&gt;&lt;path</span> <span class="hl kwb">d</span>=<span class="hl str">&quot;M18 8A6 6 0 0 0 6 8c0 7-3 9-3 9h18s-3-2-3-9&quot;</span><span class="hl kwa">&gt;&lt;/path&gt;&lt;path</span> <span class="hl kwb">d</span>=<span class="hl str">&quot;M13.73 21a2 2 0 0 1-3.46 0&quot;</span><span class="hl kwa">&gt;&lt;/path&gt;&lt;/svg&gt;</span>
</code></pre></td></tr></table>
</div> <!-- class=content -->
<div class='footer'>generated by <a href='https://git.zx2c4.com/cgit/about/'>cgit v1.2.1</a> (<a href='https://git-scm.com/'>git 2.18.0</a>) at 2025-03-12 07:17:52 +0000</div>
</div> <!-- id=cgit -->
</body>
</html>