1 /*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "metadata_reader.h"
18
19 #include <camera/CameraMetadata.h>
20 #include <gtest/gtest.h>
21 #include <system/camera.h>
22
23 #include "array_vector.h"
24 #include "metadata_common.h"
25
26 using testing::Test;
27
28 namespace default_camera_hal {
29
30 class MetadataReaderTest : public Test {
31 protected:
SetUp()32 void SetUp() {
33 ResetMetadata();
34 // FillDUT should be called before using the device under test.
35 dut_.reset();
36 }
37
ResetMetadata()38 void ResetMetadata() {
39 metadata_ = std::make_unique<android::CameraMetadata>();
40 }
41
FillDUT()42 void FillDUT() {
43 dut_ = std::make_unique<MetadataReader>(std::move(metadata_));
44 ResetMetadata();
45 }
46
47 std::unique_ptr<MetadataReader> dut_;
48 std::unique_ptr<android::CameraMetadata> metadata_;
49
50 const int32_t facing_tag_ = ANDROID_LENS_FACING;
51 const int32_t orientation_tag_ = ANDROID_SENSOR_ORIENTATION;
52 const int32_t max_inputs_tag_ = ANDROID_REQUEST_MAX_NUM_INPUT_STREAMS;
53 const int32_t max_outputs_tag_ = ANDROID_REQUEST_MAX_NUM_OUTPUT_STREAMS;
54 const int32_t configs_tag_ = ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS;
55 const int32_t stalls_tag_ = ANDROID_SCALER_AVAILABLE_STALL_DURATIONS;
56 const int32_t reprocess_formats_tag_ =
57 ANDROID_SCALER_AVAILABLE_INPUT_OUTPUT_FORMATS_MAP;
58
59 const std::vector<int32_t> valid_orientations_ = {0, 90, 180, 270};
60 // TODO(b/31384253): check for required configs/reprocess formats.
61 };
62
TEST_F(MetadataReaderTest,FacingTranslations)63 TEST_F(MetadataReaderTest, FacingTranslations) {
64 // Check that the enums are converting properly.
65 std::map<uint8_t, int> translations{
66 {ANDROID_LENS_FACING_FRONT, CAMERA_FACING_FRONT},
67 {ANDROID_LENS_FACING_BACK, CAMERA_FACING_BACK},
68 {ANDROID_LENS_FACING_EXTERNAL, CAMERA_FACING_EXTERNAL}};
69 for (const auto& translation : translations) {
70 ASSERT_EQ(v4l2_camera_hal::UpdateMetadata(
71 metadata_.get(), facing_tag_, translation.first),
72 0);
73 FillDUT();
74
75 int expected = translation.second;
76 int actual = expected + 1;
77 EXPECT_EQ(dut_->Facing(&actual), 0);
78 EXPECT_EQ(actual, expected);
79 }
80 }
81
TEST_F(MetadataReaderTest,InvalidFacing)82 TEST_F(MetadataReaderTest, InvalidFacing) {
83 uint8_t invalid = 99;
84 ASSERT_EQ(
85 v4l2_camera_hal::UpdateMetadata(metadata_.get(), facing_tag_, invalid),
86 0);
87 FillDUT();
88 int actual = 0;
89 EXPECT_EQ(dut_->Facing(&actual), -EINVAL);
90 }
91
TEST_F(MetadataReaderTest,EmptyFacing)92 TEST_F(MetadataReaderTest, EmptyFacing) {
93 FillDUT();
94 int actual = 0;
95 EXPECT_EQ(dut_->Facing(&actual), -ENOENT);
96 }
97
TEST_F(MetadataReaderTest,ValidOrientations)98 TEST_F(MetadataReaderTest, ValidOrientations) {
99 for (int32_t orientation : valid_orientations_) {
100 ASSERT_EQ(v4l2_camera_hal::UpdateMetadata(
101 metadata_.get(), orientation_tag_, orientation),
102 0);
103 FillDUT();
104
105 int actual = orientation + 1;
106 EXPECT_EQ(dut_->Orientation(&actual), 0);
107 EXPECT_EQ(actual, orientation);
108 }
109 }
110
TEST_F(MetadataReaderTest,InvalidOrientations)111 TEST_F(MetadataReaderTest, InvalidOrientations) {
112 // High.
113 for (int32_t orientation : valid_orientations_) {
114 ASSERT_EQ(v4l2_camera_hal::UpdateMetadata(
115 metadata_.get(), orientation_tag_, orientation + 1),
116 0);
117 FillDUT();
118 int actual = 0;
119 EXPECT_EQ(dut_->Orientation(&actual), -EINVAL);
120 }
121 // Low.
122 for (int32_t orientation : valid_orientations_) {
123 ASSERT_EQ(v4l2_camera_hal::UpdateMetadata(
124 metadata_.get(), orientation_tag_, orientation - 1),
125 0);
126 FillDUT();
127 int actual = 0;
128 EXPECT_EQ(dut_->Orientation(&actual), -EINVAL);
129 }
130 }
131
TEST_F(MetadataReaderTest,EmptyOrientation)132 TEST_F(MetadataReaderTest, EmptyOrientation) {
133 FillDUT();
134 int actual = 0;
135 EXPECT_EQ(dut_->Orientation(&actual), -ENOENT);
136 }
137
TEST_F(MetadataReaderTest,MaxInputs)138 TEST_F(MetadataReaderTest, MaxInputs) {
139 int32_t expected = 12;
140 ASSERT_EQ(v4l2_camera_hal::UpdateMetadata(
141 metadata_.get(), max_inputs_tag_, expected),
142 0);
143 FillDUT();
144 int32_t actual = expected + 1;
145 ASSERT_EQ(dut_->MaxInputStreams(&actual), 0);
146 EXPECT_EQ(actual, expected);
147 }
148
TEST_F(MetadataReaderTest,EmptyMaxInputs)149 TEST_F(MetadataReaderTest, EmptyMaxInputs) {
150 FillDUT();
151 // Max inputs is an optional key; if not present the default is 0.
152 int32_t expected = 0;
153 int32_t actual = expected + 1;
154 ASSERT_EQ(dut_->MaxInputStreams(&actual), 0);
155 EXPECT_EQ(actual, expected);
156 }
157
TEST_F(MetadataReaderTest,MaxOutputs)158 TEST_F(MetadataReaderTest, MaxOutputs) {
159 std::array<int32_t, 3> expected = {{12, 34, 56}};
160 ASSERT_EQ(v4l2_camera_hal::UpdateMetadata(
161 metadata_.get(), max_outputs_tag_, expected),
162 0);
163 FillDUT();
164 std::array<int32_t, 3> actual;
165 ASSERT_EQ(dut_->MaxOutputStreams(&actual[0], &actual[1], &actual[2]), 0);
166 EXPECT_EQ(actual, expected);
167 }
168
TEST_F(MetadataReaderTest,InvalidMaxOutputs)169 TEST_F(MetadataReaderTest, InvalidMaxOutputs) {
170 // Must be a 3-tuple to be valid.
171 std::array<int32_t, 4> invalid = {{12, 34, 56, 78}};
172 ASSERT_EQ(v4l2_camera_hal::UpdateMetadata(
173 metadata_.get(), max_outputs_tag_, invalid),
174 0);
175 FillDUT();
176 int32_t actual;
177 // Don't mind the aliasing since we don't care about the value.
178 ASSERT_EQ(dut_->MaxOutputStreams(&actual, &actual, &actual), -EINVAL);
179 }
180
TEST_F(MetadataReaderTest,EmptyMaxOutputs)181 TEST_F(MetadataReaderTest, EmptyMaxOutputs) {
182 FillDUT();
183 int32_t actual;
184 // Don't mind the aliasing since we don't care about the value.
185 ASSERT_EQ(dut_->MaxOutputStreams(&actual, &actual, &actual), -ENOENT);
186 }
187
TEST_F(MetadataReaderTest,StreamConfigurations)188 TEST_F(MetadataReaderTest, StreamConfigurations) {
189 v4l2_camera_hal::ArrayVector<int32_t, 4> configs;
190 std::array<int32_t, 4> config1{
191 {1, 2, 3, ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT}};
192 std::array<int32_t, 4> config2{
193 {5, 6, 7, ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT}};
194 configs.push_back(config1);
195 configs.push_back(config2);
196 ASSERT_EQ(
197 v4l2_camera_hal::UpdateMetadata(metadata_.get(), configs_tag_, configs),
198 0);
199 FillDUT();
200 std::vector<StreamConfiguration> actual;
201 ASSERT_EQ(dut_->StreamConfigurations(&actual), 0);
202 ASSERT_EQ(actual.size(), configs.num_arrays());
203 EXPECT_EQ(actual[0].spec.format, config1[0]);
204 EXPECT_EQ(actual[0].spec.width, config1[1]);
205 EXPECT_EQ(actual[0].spec.height, config1[2]);
206 EXPECT_EQ(actual[0].direction, config1[3]);
207 EXPECT_EQ(actual[1].spec.format, config2[0]);
208 EXPECT_EQ(actual[1].spec.width, config2[1]);
209 EXPECT_EQ(actual[1].spec.height, config2[2]);
210 EXPECT_EQ(actual[1].direction, config2[3]);
211 }
212
TEST_F(MetadataReaderTest,InvalidStreamConfigurationDirection)213 TEST_F(MetadataReaderTest, InvalidStreamConfigurationDirection) {
214 // -1 is not a valid direction.
215 std::array<int32_t, 4> config{{1, 2, 3, -1}};
216 ASSERT_EQ(
217 v4l2_camera_hal::UpdateMetadata(metadata_.get(), configs_tag_, config),
218 0);
219 FillDUT();
220 std::vector<StreamConfiguration> actual;
221 ASSERT_EQ(dut_->StreamConfigurations(&actual), -EINVAL);
222 }
223
TEST_F(MetadataReaderTest,InvalidStreamConfigurationSize)224 TEST_F(MetadataReaderTest, InvalidStreamConfigurationSize) {
225 // Both size dimensions must be > 0.
226 std::array<int32_t, 4> config{
227 {1, 2, 0, ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT}};
228 ASSERT_EQ(
229 v4l2_camera_hal::UpdateMetadata(metadata_.get(), configs_tag_, config),
230 0);
231 FillDUT();
232 std::vector<StreamConfiguration> actual;
233 ASSERT_EQ(dut_->StreamConfigurations(&actual), -EINVAL);
234 }
235
TEST_F(MetadataReaderTest,InvalidStreamConfigurationNumElements)236 TEST_F(MetadataReaderTest, InvalidStreamConfigurationNumElements) {
237 // Should be a multiple of 4.
238 std::array<int32_t, 5> config{
239 {1, 2, 3, ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT, 5}};
240 ASSERT_EQ(
241 v4l2_camera_hal::UpdateMetadata(metadata_.get(), configs_tag_, config),
242 0);
243 FillDUT();
244 std::vector<StreamConfiguration> actual;
245 ASSERT_EQ(dut_->StreamConfigurations(&actual), -EINVAL);
246 }
247
248 // TODO(b/31384253): Test that failure occurs if
249 // required configurations are not present.
250
TEST_F(MetadataReaderTest,NoStreamConfigurations)251 TEST_F(MetadataReaderTest, NoStreamConfigurations) {
252 FillDUT();
253 std::vector<StreamConfiguration> actual;
254 ASSERT_EQ(dut_->StreamConfigurations(&actual), -ENOENT);
255 }
256
TEST_F(MetadataReaderTest,StreamStallDurations)257 TEST_F(MetadataReaderTest, StreamStallDurations) {
258 v4l2_camera_hal::ArrayVector<int64_t, 4> stalls;
259 std::array<int64_t, 4> stall1{{1, 2, 3, 4}};
260 std::array<int64_t, 4> stall2{{5, 6, 7, 8}};
261 stalls.push_back(stall1);
262 stalls.push_back(stall2);
263 ASSERT_EQ(
264 v4l2_camera_hal::UpdateMetadata(metadata_.get(), stalls_tag_, stalls), 0);
265 FillDUT();
266 std::vector<StreamStallDuration> actual;
267 ASSERT_EQ(dut_->StreamStallDurations(&actual), 0);
268 ASSERT_EQ(actual.size(), stalls.num_arrays());
269 EXPECT_EQ(actual[0].spec.format, stall1[0]);
270 EXPECT_EQ(actual[0].spec.width, stall1[1]);
271 EXPECT_EQ(actual[0].spec.height, stall1[2]);
272 EXPECT_EQ(actual[0].duration, stall1[3]);
273 EXPECT_EQ(actual[1].spec.format, stall2[0]);
274 EXPECT_EQ(actual[1].spec.width, stall2[1]);
275 EXPECT_EQ(actual[1].spec.height, stall2[2]);
276 EXPECT_EQ(actual[1].duration, stall2[3]);
277 }
278
TEST_F(MetadataReaderTest,InvalidStreamStallDurationDuration)279 TEST_F(MetadataReaderTest, InvalidStreamStallDurationDuration) {
280 // -1 is not a valid duration.
281 std::array<int64_t, 4> stall{{1, 2, 3, -1}};
282 ASSERT_EQ(
283 v4l2_camera_hal::UpdateMetadata(metadata_.get(), stalls_tag_, stall), 0);
284 FillDUT();
285 std::vector<StreamStallDuration> actual;
286 ASSERT_EQ(dut_->StreamStallDurations(&actual), -EINVAL);
287 }
288
TEST_F(MetadataReaderTest,InvalidStreamStallDurationSize)289 TEST_F(MetadataReaderTest, InvalidStreamStallDurationSize) {
290 // Both size dimensions must be > 0.
291 std::array<int64_t, 4> stall{{1, 2, 0, 3}};
292 ASSERT_EQ(
293 v4l2_camera_hal::UpdateMetadata(metadata_.get(), stalls_tag_, stall), 0);
294 FillDUT();
295 std::vector<StreamStallDuration> actual;
296 ASSERT_EQ(dut_->StreamStallDurations(&actual), -EINVAL);
297 }
298
TEST_F(MetadataReaderTest,InvalidStreamStallDurationNumElements)299 TEST_F(MetadataReaderTest, InvalidStreamStallDurationNumElements) {
300 // Should be a multiple of 4.
301 std::array<int64_t, 5> stall{{1, 2, 3, 4, 5}};
302 ASSERT_EQ(
303 v4l2_camera_hal::UpdateMetadata(metadata_.get(), stalls_tag_, stall), 0);
304 FillDUT();
305 std::vector<StreamStallDuration> actual;
306 ASSERT_EQ(dut_->StreamStallDurations(&actual), -EINVAL);
307 }
308
309 // TODO(b/31384253): Test that failure occurs if
310 // YUV_420_888, RAW10, RAW12, RAW_OPAQUE, or IMPLEMENTATION_DEFINED
311 // formats have stall durations > 0.
312
TEST_F(MetadataReaderTest,NoStreamStallDurations)313 TEST_F(MetadataReaderTest, NoStreamStallDurations) {
314 FillDUT();
315 std::vector<StreamStallDuration> actual;
316 ASSERT_EQ(dut_->StreamStallDurations(&actual), -ENOENT);
317 }
318
TEST_F(MetadataReaderTest,ReprocessFormats)319 TEST_F(MetadataReaderTest, ReprocessFormats) {
320 ReprocessFormatMap expected{{1, {4}}, {2, {5, 6}}, {3, {7, 8, 9}}};
321 std::vector<int32_t> raw;
322 for (const auto& input_outputs : expected) {
323 raw.push_back(input_outputs.first);
324 raw.push_back(input_outputs.second.size());
325 raw.insert(
326 raw.end(), input_outputs.second.begin(), input_outputs.second.end());
327 }
328 ASSERT_EQ(v4l2_camera_hal::UpdateMetadata(
329 metadata_.get(), reprocess_formats_tag_, raw),
330 0);
331 FillDUT();
332 ReprocessFormatMap actual;
333 ASSERT_EQ(dut_->ReprocessFormats(&actual), 0);
334 EXPECT_EQ(actual, expected);
335 }
336
TEST_F(MetadataReaderTest,ReprocessFormatsNoOutputs)337 TEST_F(MetadataReaderTest, ReprocessFormatsNoOutputs) {
338 // 0 indicates that there are 0 output formats for input format 1,
339 // which is not ok.
340 std::vector<int32_t> raw{1, 0};
341 ASSERT_EQ(v4l2_camera_hal::UpdateMetadata(
342 metadata_.get(), reprocess_formats_tag_, raw),
343 0);
344 FillDUT();
345 ReprocessFormatMap actual;
346 ASSERT_EQ(dut_->ReprocessFormats(&actual), -EINVAL);
347 }
348
TEST_F(MetadataReaderTest,ReprocessFormatsPastEnd)349 TEST_F(MetadataReaderTest, ReprocessFormatsPastEnd) {
350 // 3 indicates that there are 3 output formats for input format 1,
351 // which is not ok since there are only 2 here.
352 std::vector<int32_t> raw{1, 3, 0, 0};
353 ASSERT_EQ(v4l2_camera_hal::UpdateMetadata(
354 metadata_.get(), reprocess_formats_tag_, raw),
355 0);
356 FillDUT();
357 ReprocessFormatMap actual;
358 ASSERT_EQ(dut_->ReprocessFormats(&actual), -EINVAL);
359 }
360
TEST_F(MetadataReaderTest,EmptyReprocessFormats)361 TEST_F(MetadataReaderTest, EmptyReprocessFormats) {
362 FillDUT();
363 ReprocessFormatMap actual;
364 ASSERT_EQ(dut_->ReprocessFormats(&actual), -ENOENT);
365 }
366
367 } // namespace default_camera_hal
368