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 "format_metadata_factory.h"
18
19 #include <camera/CameraMetadata.h>
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22
23 #include "metadata/test_common.h"
24 #include "v4l2_wrapper_mock.h"
25
26 using testing::AtLeast;
27 using testing::Expectation;
28 using testing::Return;
29 using testing::SetArgPointee;
30 using testing::Test;
31 using testing::_;
32
33 namespace v4l2_camera_hal {
34
35 class FormatMetadataFactoryTest : public Test {
36 protected:
SetUp()37 virtual void SetUp() { mock_device_.reset(new V4L2WrapperMock()); }
38
ExpectMetadataTagCount(const android::CameraMetadata & metadata,uint32_t tag,size_t count)39 virtual void ExpectMetadataTagCount(const android::CameraMetadata& metadata,
40 uint32_t tag,
41 size_t count) {
42 camera_metadata_ro_entry_t entry = metadata.find(tag);
43 EXPECT_EQ(entry.count, count);
44 }
45
46 std::shared_ptr<V4L2WrapperMock> mock_device_;
47 };
48
TEST_F(FormatMetadataFactoryTest,GetFormatMetadata)49 TEST_F(FormatMetadataFactoryTest, GetFormatMetadata) {
50 std::set<uint32_t> formats{V4L2_PIX_FMT_JPEG, V4L2_PIX_FMT_YUV420,
51 V4L2_PIX_FMT_YUYV};
52 std::map<uint32_t, std::set<std::array<int32_t, 2>>> sizes{
53 {V4L2_PIX_FMT_JPEG, {{{10, 20}}, {{30, 60}}, {{120, 240}}}},
54 {V4L2_PIX_FMT_YUV420, {{{1, 2}}, {{3, 6}}, {{12, 24}}}},
55 {V4L2_PIX_FMT_YUYV, {{{20, 40}}, {{80, 160}}, {{320, 640}}}}};
56 // These need to be on the correct order of magnitude,
57 // as there is a check for min fps > 15.
58 std::map<uint32_t, std::map<std::array<int32_t, 2>, std::array<int64_t, 2>>>
59 durations{{V4L2_PIX_FMT_JPEG,
60 {{{{10, 20}}, {{100000000, 200000000}}},
61 {{{30, 60}}, {{1000000000, 2000000000}}},
62 {{{120, 240}}, {{700000000, 1200000000}}}}},
63 {V4L2_PIX_FMT_YUV420,
64 {{{{1, 2}}, {{10000000000, 20000000000}}},
65 {{{3, 6}}, {{11000000000, 21000000000}}},
66 {{{12, 24}}, {{10500000000, 19000000000}}}}},
67 {V4L2_PIX_FMT_YUYV,
68 {{{{20, 40}}, {{11000000000, 22000000000}}},
69 {{{80, 160}}, {{13000000000, 25000000000}}},
70 {{{320, 640}}, {{10100000000, 19000000000}}}}}};
71 // The camera must report at least one qualified format.
72 std::vector<uint32_t> qualified_formats = {V4L2_PIX_FMT_YUYV};
73
74 // Device must support IMPLEMENTATION_DEFINED (as well as JPEG & YUV).
75 // For USB cameras, we assume that this format will not be present, and it
76 // will default to a qualified format or one of the other required formats.
77
78 EXPECT_CALL(*mock_device_, GetFormats(_))
79 .WillOnce(DoAll(SetArgPointee<0>(formats), Return(0)));
80
81 EXPECT_CALL(*mock_device_, GetQualifiedFormats(_))
82 .WillOnce(DoAll(SetArgPointee<0>(qualified_formats), Return(0)));
83
84 for (auto format : formats) {
85 std::set<std::array<int32_t, 2>> format_sizes = sizes[format];
86 EXPECT_CALL(*mock_device_, GetFormatFrameSizes(format, _))
87 .Times(AtLeast(1))
88 .WillRepeatedly(DoAll(SetArgPointee<1>(format_sizes), Return(0)));
89 for (auto size : format_sizes) {
90 EXPECT_CALL(*mock_device_, GetFormatFrameDurationRange(format, size, _))
91 .Times(AtLeast(1))
92 .WillRepeatedly(
93 DoAll(SetArgPointee<2>(durations[format][size]), Return(0)));
94 }
95 }
96
97 PartialMetadataSet components;
98 ASSERT_EQ(AddFormatComponents(mock_device_,
99 std::inserter(components, components.end())),
100 0);
101
102 for (auto& component : components) {
103 android::CameraMetadata metadata;
104 component->PopulateStaticFields(&metadata);
105 ASSERT_EQ(metadata.entryCount(), 1u);
106 int32_t tag = component->StaticTags()[0];
107 switch (tag) {
108 case ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS: // Fall through.
109 case ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS: // Fall through.
110 case ANDROID_SCALER_AVAILABLE_STALL_DURATIONS: // Fall through.
111 // 3 sizes per format, 4 elements per config.
112 // # formats + 1 for IMPLEMENTATION_DEFINED.
113 ExpectMetadataTagCount(metadata, tag, (formats.size() + 1) * 3 * 4);
114 break;
115 case ANDROID_SENSOR_INFO_MAX_FRAME_DURATION:
116 // The lowest max duration from above.
117 ExpectMetadataEq(metadata, tag, 200000000);
118 break;
119 case ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES:
120 // 2 ranges ({min, max} and {max, max}), each with a min and max.
121 ExpectMetadataTagCount(metadata, tag, 4);
122 break;
123 default:
124 FAIL() << "Unexpected component created.";
125 break;
126 }
127 }
128 }
129
TEST_F(FormatMetadataFactoryTest,GetFormatMetadataMissingRequired)130 TEST_F(FormatMetadataFactoryTest, GetFormatMetadataMissingRequired) {
131 std::set<uint32_t> formats{V4L2_PIX_FMT_YUYV};
132 std::map<uint32_t, std::set<std::array<int32_t, 2>>> sizes{
133 {V4L2_PIX_FMT_YUYV, {{{640, 480}}, {{320, 240}}}}};
134 std::map<uint32_t, std::map<std::array<int32_t, 2>, std::array<int64_t, 2>>>
135 durations{{V4L2_PIX_FMT_YUYV,
136 {{{{640, 480}}, {{100000000, 200000000}}},
137 {{{320, 240}}, {{100000000, 200000000}}}}}};
138
139 EXPECT_CALL(*mock_device_, GetFormats(_))
140 .WillOnce(DoAll(SetArgPointee<0>(formats), Return(0)));
141 // If a qualified format is present, we expect that required fields are
142 // populated as if they are supported.
143 std::vector<uint32_t> qualified_formats = {V4L2_PIX_FMT_YUYV};
144
145 EXPECT_CALL(*mock_device_, GetQualifiedFormats(_))
146 .WillOnce(DoAll(SetArgPointee<0>(qualified_formats), Return(0)));
147
148 for (auto format : formats) {
149 std::set<std::array<int32_t, 2>> format_sizes = sizes[format];
150 EXPECT_CALL(*mock_device_, GetFormatFrameSizes(format, _))
151 .Times(AtLeast(1))
152 .WillRepeatedly(DoAll(SetArgPointee<1>(format_sizes), Return(0)));
153 for (auto size : format_sizes) {
154 EXPECT_CALL(*mock_device_, GetFormatFrameDurationRange(format, size, _))
155 .Times(AtLeast(1))
156 .WillRepeatedly(
157 DoAll(SetArgPointee<2>(durations[format][size]), Return(0)));
158 }
159 }
160
161 // Check that all required formats are present.
162 PartialMetadataSet components;
163 ASSERT_EQ(AddFormatComponents(mock_device_,
164 std::inserter(components, components.end())),
165 0);
166
167 std::vector<std::array<int32_t, 2>> target_fps_ranges{{{5, 10}}, {{10, 10}}};
168 for (auto& component : components) {
169 android::CameraMetadata metadata;
170 component->PopulateStaticFields(&metadata);
171 ASSERT_EQ(metadata.entryCount(), 1u);
172 int32_t tag = component->StaticTags()[0];
173 switch (tag) {
174 case ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS: // Fall through.
175 case ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS: // Fall through.
176 case ANDROID_SCALER_AVAILABLE_STALL_DURATIONS: // Fall through.
177 // Two sizes per format, four elements per config.
178 // # formats + 3 for YUV420, JPEG, IMPLEMENTATION_DEFINED.
179 ExpectMetadataTagCount(metadata, tag, (formats.size() + 3) * 2 * 4);
180 break;
181 case ANDROID_SENSOR_INFO_MAX_FRAME_DURATION:
182 // The lowest max duration from above.
183 ExpectMetadataEq(metadata, tag, 200000000);
184 break;
185 case ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES:
186 // 2 ranges ({min, max} and {max, max}), each with a min and max.
187 ExpectMetadataTagCount(metadata, tag, 4);
188 ExpectMetadataEq(metadata, tag, target_fps_ranges);
189 break;
190 default:
191 FAIL() << "Unexpected component created.";
192 break;
193 }
194 }
195 }
196
197 } // namespace v4l2_camera_hal
198