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