summaryrefslogtreecommitdiff
path: root/test/controls/control_list.cpp
blob: 70cf61b85cbc04d696f87f32ba9bdbae74a90ed6 (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
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.contains(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.contains(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.contains(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.contains(controls::Brightness) ||
		    !list.contains(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.contains(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.contains(controls::Brightness) ||
		    !mergeList.contains(controls::Contrast) ||
		    !mergeList.contains(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)
* \param[in] sequence The sequence number to get controls for * * Read back what controls where in effect at a specific sequence number. The * history is a ring buffer of 16 entries where new and old values coexist. It's * the callers responsibility to not read too old sequence numbers that have been * pushed out of the history. * * Historic values are evicted by pushing new values onto the queue using * push(). The max history from the current sequence number that yields valid * values are thus 16 minus number of controls pushed. * * \return The controls at \a sequence number */ ControlList DelayedControls::get(uint32_t sequence) { uint32_t adjustedSeq = sequence - firstSequence_; unsigned int index = std::max<int>(0, adjustedSeq - maxDelay_); ControlList out(device_->controls()); for (const auto &ctrl : values_) { const ControlId *id = ctrl.first; const Info &info = ctrl.second[index]; out.set(id->id(), info); LOG(DelayedControls, Debug) << "Reading " << id->name() << " to " << info.toString() << " at index " << index; } return out; } /** * \brief Inform DelayedControls of the start of a new frame * \param[in] sequence Sequence number of the frame that started * * Inform the state machine that a new frame has started and of its sequence * number. Any user of these helpers is responsible to inform the helper about * the start of any frame. This can be connected with ease to the start of a * exposure (SOE) V4L2 event. */ void DelayedControls::applyControls(uint32_t sequence) { LOG(DelayedControls, Debug) << "frame " << sequence << " started"; if (!running_) { firstSequence_ = sequence; running_ = true; } /* * Create control list peeking ahead in the value queue to ensure * values are set in time to satisfy the sensor delay. */ ControlList out(device_->controls()); for (auto &ctrl : values_) { const ControlId *id = ctrl.first; unsigned int delayDiff = maxDelay_ - controlParams_[id].delay; unsigned int index = std::max<int>(0, writeCount_ - delayDiff); Info &info = ctrl.second[index]; if (info.updated) { if (controlParams_[id].priorityWrite) { /* * This control must be written now, it could * affect validity of the other controls. */ ControlList priority(device_->controls()); priority.set(id->id(), info); device_->setControls(&priority); } else { /* * Batch up the list of controls and write them * at the end of the function. */ out.set(id->id(), info); } LOG(DelayedControls, Debug) << "Setting " << id->name() << " to " << info.toString() << " at index " << index; /* Done with this update, so mark as completed. */ info.updated = false; } } writeCount_ = sequence - firstSequence_ + 1; while (writeCount_ > queueCount_) { LOG(DelayedControls, Debug) << "Queue is empty, auto queue no-op."; push({}); } device_->setControls(&out); } } /* namespace libcamera */