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