liyujie
2025-08-28 d9927380ed7c8366f762049be9f3fee225860833
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
/*
 * Copyright (c) 2021 by Allwinnertech Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
#include "CameraMetadata.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
 
#include <map>
 
#include "format_metadata_factory.h"
#include "metadata/test_common.h"
#include "v4l2_wrapper_mock.h"
 
using testing::AtLeast;
using testing::Expectation;
using testing::Return;
using testing::SetArgPointee;
using testing::Test;
using testing::_;
 
namespace v4l2_camera_hal {
 
using ::android::hardware::camera::common::V1_0::helper::CameraMetadata;
 
class FormatMetadataFactoryTest : public Test {
 protected:
  virtual void SetUp() { mock_device_.reset(new V4L2WrapperMock()); }
 
  virtual void ExpectMetadataTagCount(const CameraMetadata& metadata,
                                      uint32_t tag,
                                      size_t count) {
    camera_metadata_ro_entry_t entry = metadata.find(tag);
    EXPECT_EQ(entry.count, count);
  }
 
  std::shared_ptr<V4L2WrapperMock> mock_device_;
};
 
TEST_F(FormatMetadataFactoryTest, GetFormatMetadata) {
  std::set<uint32_t> formats{V4L2_PIX_FMT_JPEG, V4L2_PIX_FMT_YUV420};
  std::map<uint32_t, std::set<std::array<int32_t, 2>>> sizes{
      {V4L2_PIX_FMT_JPEG, {{{10, 20}}, {{30, 60}}, {{120, 240}}}},
      {V4L2_PIX_FMT_YUV420, {{{1, 2}}, {{3, 6}}, {{12, 24}}}}};
  // These need to be on the correct order of magnitude,
  // as there is a check for min fps > 15.
  std::map<uint32_t, std::map<std::array<int32_t, 2>, std::array<int64_t, 2>>>
      durations{{V4L2_PIX_FMT_JPEG,
                 {{{{10, 20}}, {{100000000, 200000000}}},
                  {{{30, 60}}, {{1000000000, 2000000000}}},
                  {{{120, 240}}, {{700000000, 1200000000}}}}},
                {V4L2_PIX_FMT_YUV420,
                 {{{{1, 2}}, {{10000000000, 20000000000}}},
                  {{{3, 6}}, {{11000000000, 21000000000}}},
                  {{{12, 24}}, {{10500000000, 19000000000}}}}}};
 
  // Device must support IMPLEMENTATION_DEFINED (as well as JPEG & YUV).
  // Just duplicate the values from another format.
  uint32_t imp_defined_format = StreamFormat::HalToV4L2PixelFormat(
      HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED);
  formats.insert(imp_defined_format);
  sizes[imp_defined_format] = sizes[V4L2_PIX_FMT_YUV420];
  durations[imp_defined_format] = durations[V4L2_PIX_FMT_YUV420];
 
  EXPECT_CALL(*mock_device_, GetFormats(_))
      .WillOnce(DoAll(SetArgPointee<0>(formats), Return(0)));
 
  for (auto format : formats) {
    std::set<std::array<int32_t, 2>> format_sizes = sizes[format];
    EXPECT_CALL(*mock_device_, GetFormatFrameSizes(format, _))
        .Times(AtLeast(1))
        .WillRepeatedly(DoAll(SetArgPointee<1>(format_sizes), Return(0)));
    for (auto size : format_sizes) {
      EXPECT_CALL(*mock_device_, GetFormatFrameDurationRange(format, size, _))
          .Times(AtLeast(1))
          .WillRepeatedly(
              DoAll(SetArgPointee<2>(durations[format][size]), Return(0)));
    }
  }
 
  PartialMetadataSet components;
  ASSERT_EQ(AddFormatComponents(mock_device_,
                                std::inserter(components, components.end())),
            0);
 
  for (auto& component : components) {
    CameraMetadata metadata;
    component->PopulateStaticFields(&metadata);
    ASSERT_EQ(metadata.entryCount(), 1);
    int32_t tag = component->StaticTags()[0];
    switch (tag) {
      case ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS:  // Fall through.
      case ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS:    // Fall through.
      case ANDROID_SCALER_AVAILABLE_STALL_DURATIONS:        // Fall through.
        // 3 sizes per format, 4 elements per config.
        // # formats + 1 for IMPLEMENTATION_DEFINED.
        ExpectMetadataTagCount(metadata, tag, (formats.size() + 1) * 3 * 4);
        break;
      case ANDROID_SENSOR_INFO_MAX_FRAME_DURATION:
        // The lowest max duration from above.
        ExpectMetadataEq(metadata, tag, 200000000);
        break;
      case ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES:
        // 2 ranges ({min, max} and {max, max}), each with a min and max.
        ExpectMetadataTagCount(metadata, tag, 4);
        break;
      default:
        FAIL() << "Unexpected component created.";
        break;
    }
  }
}
 
TEST_F(FormatMetadataFactoryTest, GetFormatMetadataMissingJpeg) {
  uint32_t imp_defined_format = StreamFormat::HalToV4L2PixelFormat(
      HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED);
  std::set<uint32_t> formats{V4L2_PIX_FMT_YUV420, imp_defined_format};
  EXPECT_CALL(*mock_device_, GetFormats(_))
      .WillOnce(DoAll(SetArgPointee<0>(formats), Return(0)));
  PartialMetadataSet components;
  ASSERT_EQ(AddFormatComponents(mock_device_,
                                std::inserter(components, components.end())),
            -ENODEV);
}
 
TEST_F(FormatMetadataFactoryTest, GetFormatMetadataMissingYuv) {
  uint32_t imp_defined_format = StreamFormat::HalToV4L2PixelFormat(
      HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED);
  std::set<uint32_t> formats{V4L2_PIX_FMT_JPEG, imp_defined_format};
  EXPECT_CALL(*mock_device_, GetFormats(_))
      .WillOnce(DoAll(SetArgPointee<0>(formats), Return(0)));
  PartialMetadataSet components;
  ASSERT_EQ(AddFormatComponents(mock_device_,
                                std::inserter(components, components.end())),
            -ENODEV);
}
 
TEST_F(FormatMetadataFactoryTest,
       GetFormatMetadataMissingImplementationDefined) {
  std::set<uint32_t> formats{V4L2_PIX_FMT_JPEG, V4L2_PIX_FMT_YUV420};
  EXPECT_CALL(*mock_device_, GetFormats(_))
      .WillOnce(DoAll(SetArgPointee<0>(formats), Return(0)));
  PartialMetadataSet components;
  ASSERT_EQ(AddFormatComponents(mock_device_,
                                std::inserter(components, components.end())),
            -ENODEV);
}
 
}  // namespace v4l2_camera_hal