/* SPDX-License-Identifier: LGPL-2.1-or-later */ /* * Copyright (C) 2019, Google Inc. * * v4l2_videodevice.h - V4L2 Video Device */ #ifndef __LIBCAMERA_V4L2_VIDEODEVICE_H__ #define __LIBCAMERA_V4L2_VIDEODEVICE_H__ #include #include #include #include #include #include #include #include #include "formats.h" #include "log.h" #include "v4l2_device.h" namespace libcamera { class EventNotifier; class FileDescriptor; class MediaDevice; class MediaEntity; struct V4L2Capability final : v4l2_capability { const char *driver() const { return reinterpret_cast(v4l2_capability::driver); } const char *card() const { return reinterpret_cast(v4l2_capability::card); } const char *bus_info() const { return reinterpret_cast(v4l2_capability::bus_info); } unsigned int device_caps() const { return capabilities & V4L2_CAP_DEVICE_CAPS ? v4l2_capability::device_caps : v4l2_capability::capabilities; } bool isMultiplanar() const { return device_caps() & (V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_VIDEO_OUTPUT_MPLANE | V4L2_CAP_VIDEO_M2M_MPLANE); } bool isCapture() const { return device_caps() & (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_META_CAPTURE); } bool isOutput() const { return device_caps() & (V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OUTPUT_MPLANE | V4L2_CAP_META_OUTPUT); } bool isVideo() const { return device_caps() & (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OUTPUT_MPLANE); } bool isM2M() const { return device_caps() & (V4L2_CAP_VIDEO_M2M | V4L2_CAP_VIDEO_M2M_MPLANE); } bool isMeta() const { return device_caps() & (V4L2_CAP_META_CAPTURE | V4L2_CAP_META_OUTPUT); } bool isVideoCapture() const { return isVideo() && isCapture(); } bool isVideoOutput() const { return isVideo() && isOutput(); } bool isMetaCapture() const { return isMeta() && isCapture(); } bool isMetaOutput() const { return isMeta() && isOutput(); } bool hasStreaming() const { return device_caps() & V4L2_CAP_STREAMING; } }; class V4L2BufferCache { public: V4L2BufferCache(unsigned int numEntries); V4L2BufferCache(const std::vector> &buffers); ~V4L2BufferCache(); int get(const FrameBuffer &buffer); void put(unsigned int index); private: class Entry { public: Entry(); Entry(bool free, const FrameBuffer &buffer); bool operator==(const FrameBuffer &buffer); bool free; private: struct Plane { Plane(const FrameBuffer::Plane &plane) : fd(plane.fd.fd()), length(plane.length) { } int fd; unsigned int length; }; std::vector planes_; }; std::vector cache_; /* \todo Expose the miss counter through an instrumentation API. */ unsigned int missCounter_; }; class V4L2DeviceFormat { public: uint32_t fourcc; Size size; struct { uint32_t size; uint32_t bpl; } planes[3]; unsigned int planesCount; const std::string toString() const; }; class V4L2VideoDevice : public V4L2Device { public: explicit V4L2VideoDevice(const std::string &deviceNode); explicit V4L2VideoDevice(const MediaEntity *entity); V4L2VideoDevice(const V4L2VideoDevice &) = delete; ~V4L2VideoDevice(); V4L2VideoDevice &operator=(const V4L2VideoDevice &) = delete; int open(); int open(int handle, enum v4l2_buf_type type); void close(); const char *driverName() const { return caps_.driver(); } const char *deviceName() const { return caps_.card(); } const char *busName() const { return caps_.bus_info(); } int getFormat(V4L2DeviceFormat *format); int setFormat(V4L2DeviceFormat *format); ImageFormats formats(); int setCrop(Rectangle *rect); int setCompose(Rectangle *rect); int exportBuffers(unsigned int count, std::vector> *buffers); int importBuffers(unsigned int count); int releaseBuffers(); int queueBuffer(FrameBuffer *buffer); Signal bufferReady; int streamOn(); int streamOff(); static V4L2VideoDevice *fromEntityName(const MediaDevice *media, const std::string &entity); static PixelFormat toPixelFormat(uint32_t v4l2Fourcc); uint32_t toV4L2Fourcc(PixelFormat pixelFormat); static uint32_t toV4L2Fourcc(PixelFormat pixelFormat, bool multiplanar); protected: std::string logPrefix() const; private: int getFormatMeta(V4L2DeviceFormat *format); int setFormatMeta(V4L2DeviceFormat *format); int getFormatMultiplane(V4L2DeviceFormat *format); int setFormatMultiplane(V4L2DeviceFormat *format); int getFormatSingleplane(V4L2DeviceFormat *format); int setFormatSingleplane(V4L2DeviceFormat *format); std::vector enumPixelformats(); std::vector enumSizes(unsigned int pixelFormat); int setSelection(unsigned int target, Rectangle *rect); int requestBuffers(unsigned int count); std::unique_ptr createBuffer(const struct v4l2_buffer &buf); FileDescriptor exportDmabufFd(unsigned int index, unsigned int plane); void bufferAvailable(EventNotifier *notifier); FrameBuffer *dequeueBuffer(); V4L2Capability caps_; enum v4l2_buf_type bufferType_; enum v4l2_memory memoryType_; V4L2BufferCache *cache_; std::map queuedBuffers_; EventNotifier *fdEvent_; }; class V4L2M2MDevice { public: V4L2M2MDevice(const std::string &deviceNode); ~V4L2M2MDevice(); int open(); void close(); V4L2VideoDevice *output() { return output_; } V4L2VideoDevice *capture() { return capture_; } private: std::string deviceNode_; V4L2VideoDevice *output_; V4L2VideoDevice *capture_; }; } /* namespace libcamera */ #endif /* __LIBCAMERA_V4L2_VIDEODEVICE_H__ */ 2'>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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2019, Google Inc.
 *
 * control_list.cpp - ControlList tests
 */

#include <iostream>

#include <libcamera/camera.h>
#include <libcamera/camera_manager.h>
#include <libcamera/control_ids.h>
#include <libcamera/controls.h>

#include "libcamera/internal/camera_controls.h"

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

using namespace std;
using namespace libcamera;

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

protected:
	int init() override
	{
		return status_;
	}

	int run() override
	{
		CameraControlValidator validator(camera_.get());
		ControlList list(controls::controls, &validator);

		/* Test that the list is initially empty. */
		if (!list.empty()) {
			cout << "List should to be empty" << endl;
			return TestFail;
		}

		if (list.size() != 0) {
			cout << "List should contain zero items" << endl;
			return TestFail;
		}

		if (list.get(controls::Brightness)) {
			cout << "List should not contain Brightness control" << endl;
			return TestFail;
		}

		unsigned int count = 0;
		for (auto iter = list.begin(); iter != list.end(); ++iter)
			count++;

		if (count != 0) {
			cout << "List iteration should not produce any item" << endl;
			return TestFail;
		}

		/*
		 * Set a control, and verify that the list now contains it, and
		 * nothing else.
		 */
		list.set(controls::Brightness, -0.5f);

		if (list.empty()) {
			cout << "List should not be empty" << endl;
			return TestFail;
		}

		if (list.size() != 1) {
			cout << "List should contain one item" << endl;
			return TestFail;
		}

		if (!list.get(controls::Brightness)) {
			cout << "List should contain Brightness control" << endl;
			return TestFail;
		}

		count = 0;
		for (auto iter = list.begin(); iter != list.end(); ++iter)
			count++;

		if (count != 1) {
			cout << "List iteration should produce one item" << endl;
			return TestFail;
		}

		if (list.get(controls::Brightness) != -0.5f) {
			cout << "Incorrest Brightness control value" << endl;
			return TestFail;
		}

		if (list.get(controls::Contrast)) {
			cout << "List should not contain Contract control" << endl;
			return TestFail;
		}

		/* Update the first control and set a second one. */
		list.set(controls::Brightness, 0.0f);
		list.set(controls::Contrast, 1.5f);

		if (!list.get(controls::Brightness) ||
		    !list.get(controls::Contrast)) {
			cout << "List should contain Brightness and Contrast controls"
			     << endl;
			return TestFail;
		}

		if (list.get(controls::Brightness) != 0.0f ||
		    list.get(controls::Contrast) != 1.5f) {
			cout << "Failed to retrieve control value" << endl;
			return TestFail;
		}

		/*
		 * Update both controls and verify that the container doesn't
		 * grow.
		 */
		list.set(controls::Brightness, 0.5f);
		list.set(controls::Contrast, 1.1f);

		if (list.get(controls::Brightness) != 0.5f ||
		    list.get(controls::Contrast) != 1.1f) {
			cout << "Failed to update control value" << endl;
			return TestFail;
		}

		if (list.size() != 2) {
			cout << "List should contain two elements" << endl;
			return TestFail;
		}

		/*
		 * Attempt to set an invalid control and verify that the
		 * operation failed.
		 */
		list.set(controls::AwbEnable, true);

		if (list.get(controls::AwbEnable)) {
			cout << "List shouldn't contain AwbEnable control" << endl;
			return TestFail;
		}

		/*
		 * Create a new list with a new control and merge it with the
		 * existing one, verifying that the existing controls
		 * values don't get overwritten.
		 */
		ControlList mergeList(controls::controls, &validator);
		mergeList.set(controls::Brightness, 0.7f);
		mergeList.set(controls::Saturation, 0.4f);

		mergeList.merge(list);
		if (mergeList.size() != 3) {
			cout << "Merged list should contain three elements" << endl;
			return TestFail;
		}

		if (list.size() != 2) {
			cout << "The list to merge should contain two elements"
			     << endl;
			return TestFail;
		}

		if (!mergeList.get(controls::Brightness) ||
		    !mergeList.get(controls::Contrast) ||
		    !mergeList.get(controls::Saturation)) {
			cout << "Merged list does not contain all controls" << endl;
			return TestFail;
		}

		if (mergeList.get(controls::Brightness) != 0.7f) {
			cout << "Brightness control value changed after merging lists"
			     << endl;
			return TestFail;
		}

		if (mergeList.get(controls::Contrast) != 1.1f) {
			cout << "Contrast control value changed after merging lists"
			     << endl;
			return TestFail;
		}

		if (mergeList.get(controls::Saturation) != 0.4f) {
			cout << "Saturation control value changed after merging lists"
			     << endl;
			return TestFail;
		}

		return TestPass;
	}
};

TEST_REGISTER(ControlListTest)