Home | History | Annotate | Download | only in tests
      1 #include <android-base/properties.h>
      2 #include <base/logging.h>
      3 #include <cutils/properties.h>
      4 #include <gtest/gtest.h>
      5 #include <log/log.h>
      6 #include <poll.h>
      7 
      8 #include <android/hardware_buffer.h>
      9 
     10 #include <algorithm>
     11 #include <array>
     12 #include <set>
     13 #include <thread>
     14 #include <vector>
     15 
     16 #include <dvr/dvr_configuration_data.h>
     17 #include <dvr/dvr_deleter.h>
     18 #include <dvr/dvr_display_manager.h>
     19 #include <dvr/dvr_surface.h>
     20 
     21 #include <pdx/status.h>
     22 
     23 using android::pdx::ErrorStatus;
     24 using android::pdx::Status;
     25 
     26 namespace android {
     27 namespace dvr {
     28 
     29 namespace {
     30 
     31 using ::testing::Test;
     32 
     33 DvrSurfaceAttribute MakeAttribute(DvrSurfaceAttributeKey key, nullptr_t) {
     34   DvrSurfaceAttribute attribute;
     35   attribute.key = key;
     36   attribute.value.type = DVR_SURFACE_ATTRIBUTE_TYPE_NONE;
     37   return attribute;
     38 }
     39 
     40 DvrSurfaceAttribute MakeAttribute(DvrSurfaceAttributeKey key, int32_t value) {
     41   DvrSurfaceAttribute attribute;
     42   attribute.key = key;
     43   attribute.value.type = DVR_SURFACE_ATTRIBUTE_TYPE_INT32;
     44   attribute.value.int32_value = value;
     45   return attribute;
     46 }
     47 
     48 DvrSurfaceAttribute MakeAttribute(DvrSurfaceAttributeKey key, int64_t value) {
     49   DvrSurfaceAttribute attribute;
     50   attribute.key = key;
     51   attribute.value.type = DVR_SURFACE_ATTRIBUTE_TYPE_INT64;
     52   attribute.value.int64_value = value;
     53   return attribute;
     54 }
     55 
     56 DvrSurfaceAttribute MakeAttribute(DvrSurfaceAttributeKey key, bool value) {
     57   DvrSurfaceAttribute attribute;
     58   attribute.key = key;
     59   attribute.value.type = DVR_SURFACE_ATTRIBUTE_TYPE_BOOL;
     60   attribute.value.bool_value = value;
     61   return attribute;
     62 }
     63 
     64 DvrSurfaceAttribute MakeAttribute(DvrSurfaceAttributeKey key, float value) {
     65   DvrSurfaceAttribute attribute;
     66   attribute.key = key;
     67   attribute.value.type = DVR_SURFACE_ATTRIBUTE_TYPE_FLOAT;
     68   attribute.value.float_value = value;
     69   return attribute;
     70 }
     71 
     72 DvrSurfaceAttribute MakeAttribute(DvrSurfaceAttributeKey key,
     73                                   const std::array<float, 2>& value) {
     74   DvrSurfaceAttribute attribute;
     75   attribute.key = key;
     76   attribute.value.type = DVR_SURFACE_ATTRIBUTE_TYPE_FLOAT2;
     77   std::copy(value.begin(), value.end(), attribute.value.float2_value);
     78   return attribute;
     79 }
     80 
     81 DvrSurfaceAttribute MakeAttribute(DvrSurfaceAttributeKey key,
     82                                   const std::array<float, 3>& value) {
     83   DvrSurfaceAttribute attribute;
     84   attribute.key = key;
     85   attribute.value.type = DVR_SURFACE_ATTRIBUTE_TYPE_FLOAT3;
     86   std::copy(value.begin(), value.end(), attribute.value.float3_value);
     87   return attribute;
     88 }
     89 
     90 DvrSurfaceAttribute MakeAttribute(DvrSurfaceAttributeKey key,
     91                                   const std::array<float, 4>& value) {
     92   DvrSurfaceAttribute attribute;
     93   attribute.key = key;
     94   attribute.value.type = DVR_SURFACE_ATTRIBUTE_TYPE_FLOAT4;
     95   std::copy(value.begin(), value.end(), attribute.value.float4_value);
     96   return attribute;
     97 }
     98 
     99 DvrSurfaceAttribute MakeAttribute(DvrSurfaceAttributeKey key,
    100                                   const std::array<float, 8>& value) {
    101   DvrSurfaceAttribute attribute;
    102   attribute.key = key;
    103   attribute.value.type = DVR_SURFACE_ATTRIBUTE_TYPE_FLOAT8;
    104   std::copy(value.begin(), value.end(), attribute.value.float8_value);
    105   return attribute;
    106 }
    107 
    108 DvrSurfaceAttribute MakeAttribute(DvrSurfaceAttributeKey key,
    109                                   const std::array<float, 16>& value) {
    110   DvrSurfaceAttribute attribute;
    111   attribute.key = key;
    112   attribute.value.type = DVR_SURFACE_ATTRIBUTE_TYPE_FLOAT16;
    113   std::copy(value.begin(), value.end(), attribute.value.float16_value);
    114   return attribute;
    115 }
    116 
    117 Status<UniqueDvrSurface> CreateApplicationSurface(bool visible = false,
    118                                                   int32_t z_order = 0) {
    119   DvrSurface* surface = nullptr;
    120   DvrSurfaceAttribute attributes[] = {
    121       MakeAttribute(DVR_SURFACE_ATTRIBUTE_Z_ORDER, z_order),
    122       MakeAttribute(DVR_SURFACE_ATTRIBUTE_VISIBLE, visible)};
    123 
    124   const int ret = dvrSurfaceCreate(
    125       attributes, std::extent<decltype(attributes)>::value, &surface);
    126   if (ret < 0)
    127     return ErrorStatus(-ret);
    128   else
    129     return {UniqueDvrSurface(surface)};
    130 }
    131 
    132 Status<UniqueDvrWriteBufferQueue> CreateSurfaceQueue(
    133     const UniqueDvrSurface& surface, uint32_t width, uint32_t height,
    134     uint32_t format, uint32_t layer_count, uint64_t usage, size_t capacity,
    135     size_t metadata_size) {
    136   DvrWriteBufferQueue* queue;
    137   const int ret = dvrSurfaceCreateWriteBufferQueue(
    138       surface.get(), width, height, format, layer_count, usage, capacity,
    139       metadata_size, &queue);
    140   if (ret < 0)
    141     return ErrorStatus(-ret);
    142   else
    143     return {UniqueDvrWriteBufferQueue(queue)};
    144 }
    145 
    146 Status<std::vector<uint8_t>> GetConfigData(int config_type) {
    147   uint8_t* data = nullptr;
    148   size_t data_size = 0;
    149   int error = dvrConfigurationDataGet(config_type, &data, &data_size);
    150   if (error < 0) {
    151     return ErrorStatus(-error);
    152   }
    153 
    154   if (!data || data_size == 0) {
    155     return ErrorStatus(EINVAL);
    156   }
    157   std::vector<uint8_t> data_result(data, data + data_size);
    158   dvrConfigurationDataDestroy(data);
    159   std::string s(data, data + data_size);
    160   return {std::move(data_result)};
    161 }
    162 
    163 class TestDisplayManager {
    164  public:
    165   TestDisplayManager(UniqueDvrDisplayManager display_manager,
    166                      UniqueDvrSurfaceState surface_state)
    167       : display_manager_(std::move(display_manager)),
    168         surface_state_(std::move(surface_state)) {
    169     const int fd = dvrDisplayManagerGetEventFd(display_manager_.get());
    170     LOG_IF(INFO, fd < 0) << "Failed to get event fd: " << strerror(-fd);
    171     display_manager_event_fd_ = fd;
    172   }
    173 
    174   Status<UniqueDvrReadBufferQueue> GetReadBufferQueue(int surface_id,
    175                                                       int queue_id) {
    176     DvrReadBufferQueue* queue;
    177     const int ret = dvrDisplayManagerGetReadBufferQueue(
    178         display_manager_.get(), surface_id, queue_id, &queue);
    179     if (ret < 0)
    180       return ErrorStatus(-ret);
    181     else
    182       return {UniqueDvrReadBufferQueue(queue)};
    183   }
    184 
    185   Status<void> UpdateSurfaceState() {
    186     const int ret = dvrDisplayManagerGetSurfaceState(display_manager_.get(),
    187                                                      surface_state_.get());
    188     if (ret < 0)
    189       return ErrorStatus(-ret);
    190     else
    191       return {};
    192   }
    193 
    194   enum : int { kTimeoutMs = 10000 };  // 10s
    195 
    196   Status<void> WaitForUpdate(int timeout_ms = kTimeoutMs) {
    197     if (display_manager_event_fd_ < 0)
    198       return ErrorStatus(-display_manager_event_fd_);
    199 
    200     pollfd pfd = {display_manager_event_fd_, POLLIN, 0};
    201     const int count = poll(&pfd, 1, timeout_ms);
    202     if (count < 0)
    203       return ErrorStatus(errno);
    204     else if (count == 0)
    205       return ErrorStatus(ETIMEDOUT);
    206 
    207     int events;
    208     const int ret = dvrDisplayManagerTranslateEpollEventMask(
    209         display_manager_.get(), pfd.revents, &events);
    210     if (ret < 0)
    211       return ErrorStatus(-ret);
    212     else if (events & POLLIN)
    213       return UpdateSurfaceState();
    214     else
    215       return ErrorStatus(EPROTO);
    216   }
    217 
    218   Status<size_t> GetSurfaceCount() {
    219     size_t count = 0;
    220     const int ret =
    221         dvrSurfaceStateGetSurfaceCount(surface_state_.get(), &count);
    222     if (ret < 0)
    223       return ErrorStatus(-ret);
    224     else
    225       return {count};
    226   }
    227 
    228   Status<DvrSurfaceUpdateFlags> GetUpdateFlags(size_t surface_index) {
    229     DvrSurfaceUpdateFlags update_flags;
    230     const int ret = dvrSurfaceStateGetUpdateFlags(surface_state_.get(),
    231                                                   surface_index, &update_flags);
    232     if (ret < 0)
    233       return ErrorStatus(-ret);
    234     else
    235       return {update_flags};
    236   }
    237 
    238   Status<int> GetSurfaceId(size_t surface_index) {
    239     int surface_id;
    240     const int ret = dvrSurfaceStateGetSurfaceId(surface_state_.get(),
    241                                                 surface_index, &surface_id);
    242     if (ret < 0)
    243       return ErrorStatus(-ret);
    244     else
    245       return {surface_id};
    246   }
    247 
    248   Status<int> GetProcessId(size_t surface_index) {
    249     int process_id;
    250     const int ret = dvrSurfaceStateGetProcessId(surface_state_.get(),
    251                                                 surface_index, &process_id);
    252     if (ret < 0)
    253       return ErrorStatus(-ret);
    254     else
    255       return {process_id};
    256   }
    257 
    258   Status<std::vector<DvrSurfaceAttribute>> GetAttributes(size_t surface_index) {
    259     std::vector<DvrSurfaceAttribute> attributes;
    260     size_t count = 0;
    261     const int ret = dvrSurfaceStateGetAttributeCount(surface_state_.get(),
    262                                                      surface_index, &count);
    263     if (ret < 0)
    264       return ErrorStatus(-ret);
    265 
    266     attributes.resize(count);
    267     const ssize_t return_count = dvrSurfaceStateGetAttributes(
    268         surface_state_.get(), surface_index, attributes.data(), count);
    269     if (return_count < 0)
    270       return ErrorStatus(-return_count);
    271 
    272     attributes.resize(return_count);
    273     return {std::move(attributes)};
    274   }
    275 
    276   Status<std::vector<int>> GetQueueIds(size_t surface_index) {
    277     std::vector<int> queue_ids;
    278     size_t count = 0;
    279     const int ret = dvrSurfaceStateGetQueueCount(surface_state_.get(),
    280                                                  surface_index, &count);
    281     if (ret < 0)
    282       return ErrorStatus(-ret);
    283 
    284     if (count > 0) {
    285       queue_ids.resize(count);
    286       const ssize_t return_count = dvrSurfaceStateGetQueueIds(
    287           surface_state_.get(), surface_index, queue_ids.data(), count);
    288       if (return_count < 0)
    289         return ErrorStatus(-return_count);
    290 
    291       queue_ids.resize(return_count);
    292     }
    293 
    294     return {std::move(queue_ids)};
    295   }
    296 
    297  private:
    298   UniqueDvrDisplayManager display_manager_;
    299   UniqueDvrSurfaceState surface_state_;
    300 
    301   // Owned by object in display_manager_, do not explicitly close.
    302   int display_manager_event_fd_;
    303 
    304   TestDisplayManager(const TestDisplayManager&) = delete;
    305   void operator=(const TestDisplayManager&) = delete;
    306 };
    307 
    308 class DvrDisplayManagerTest : public Test {
    309  protected:
    310   void SetUp() override {
    311     // dvr display manager test doesn't apply to standalone vr devices because
    312     // tests cannot create display manager client on these devices.
    313     if (property_get_bool("ro.boot.vr", false)) {
    314       GTEST_SKIP()
    315           << "All tests in DvrDisplayManagerTest test case are skipped "
    316              "because the device boot to VR.";
    317     }
    318 
    319     int ret;
    320     DvrDisplayManager* display_manager;
    321     DvrSurfaceState* surface_state;
    322 
    323     ret = dvrDisplayManagerCreate(&display_manager);
    324     ASSERT_EQ(0, ret) << "Failed to create display manager client";
    325     ASSERT_NE(nullptr, display_manager);
    326 
    327     ret = dvrSurfaceStateCreate(&surface_state);
    328     ASSERT_EQ(0, ret) << "Failed to create surface state object";
    329     ASSERT_NE(nullptr, surface_state);
    330 
    331     manager_.reset(
    332         new TestDisplayManager(UniqueDvrDisplayManager(display_manager),
    333                                UniqueDvrSurfaceState(surface_state)));
    334   }
    335   void TearDown() override {}
    336 
    337   std::unique_ptr<TestDisplayManager> manager_;
    338 };
    339 
    340 // TODO(eieio): Consider moving these somewhere more central because they are
    341 // broadly useful.
    342 
    343 template <typename T>
    344 testing::AssertionResult StatusOk(const char* status_expression,
    345                                   const Status<T>& status) {
    346   if (!status.ok()) {
    347     return testing::AssertionFailure()
    348            << "(" << status_expression
    349            << ") expected to indicate success but actually contains error ("
    350            << status.error() << ")";
    351   } else {
    352     return testing::AssertionSuccess();
    353   }
    354 }
    355 
    356 template <typename T>
    357 testing::AssertionResult StatusError(const char* status_expression,
    358                                      const Status<T>& status) {
    359   if (status.ok()) {
    360     return testing::AssertionFailure()
    361            << "(" << status_expression
    362            << ") expected to indicate error but instead indicates success.";
    363   } else {
    364     return testing::AssertionSuccess();
    365   }
    366 }
    367 
    368 template <typename T>
    369 testing::AssertionResult StatusHasError(const char* status_expression,
    370                                         const char* /*error_code_expression*/,
    371                                         const Status<T>& status,
    372                                         int error_code) {
    373   if (status.ok()) {
    374     return StatusError(status_expression, status);
    375   } else if (status.error() != error_code) {
    376     return testing::AssertionFailure()
    377            << "(" << status_expression << ") expected to indicate error ("
    378            << error_code << ") but actually indicates error (" << status.error()
    379            << ")";
    380   } else {
    381     return testing::AssertionSuccess();
    382   }
    383 }
    384 
    385 template <typename T, typename U>
    386 testing::AssertionResult StatusHasValue(const char* status_expression,
    387                                         const char* /*value_expression*/,
    388                                         const Status<T>& status,
    389                                         const U& value) {
    390   if (!status.ok()) {
    391     return StatusOk(status_expression, status);
    392   } else if (status.get() != value) {
    393     return testing::AssertionFailure()
    394            << "(" << status_expression << ") expected to contain value ("
    395            << testing::PrintToString(value) << ") but actually contains value ("
    396            << testing::PrintToString(status.get()) << ")";
    397   } else {
    398     return testing::AssertionSuccess();
    399   }
    400 }
    401 
    402 template <typename T, typename Op>
    403 testing::AssertionResult StatusPred(const char* status_expression,
    404                                     const char* pred_expression,
    405                                     const Status<T>& status, Op pred) {
    406   if (!status.ok()) {
    407     return StatusOk(status_expression, status);
    408   } else if (!pred(status.get())) {
    409     return testing::AssertionFailure()
    410            << status_expression << " value ("
    411            << testing::PrintToString(status.get())
    412            << ") failed to pass predicate " << pred_expression;
    413   } else {
    414     return testing::AssertionSuccess();
    415   }
    416 }
    417 
    418 #define ASSERT_STATUS_OK(status) ASSERT_PRED_FORMAT1(StatusOk, status)
    419 #define ASSERT_STATUS_ERROR(status) ASSERT_PRED_FORMAT1(StatusError, status)
    420 
    421 #define ASSERT_STATUS_ERROR_VALUE(value, status) \
    422   ASSERT_PRED_FORMAT2(StatusHasError, status, value)
    423 
    424 #define ASSERT_STATUS_EQ(value, status) \
    425   ASSERT_PRED_FORMAT2(StatusHasValue, status, value)
    426 
    427 #define EXPECT_STATUS_OK(status) EXPECT_PRED_FORMAT1(StatusOk, status)
    428 #define EXPECT_STATUS_ERROR(status) EXPECT_PRED_FORMAT1(StatusError, status)
    429 
    430 #define EXPECT_STATUS_ERROR_VALUE(value, status) \
    431   EXPECT_PRED_FORMAT2(StatusHasError, status, value)
    432 
    433 #define EXPECT_STATUS_EQ(value, status) \
    434   EXPECT_PRED_FORMAT2(StatusHasValue, status, value)
    435 
    436 #define EXPECT_STATUS_PRED(pred, status) \
    437   EXPECT_PRED_FORMAT2(StatusPred, status, pred)
    438 
    439 #if 0
    440 // Verify utility predicate/macro functionality. This section is commented out
    441 // because it is designed to fail in some cases to validate the helpers.
    442 TEST_F(Test, ExpectVoid) {
    443   Status<void> status_error{ErrorStatus{EINVAL}};
    444   Status<void> status_ok{};
    445 
    446   EXPECT_STATUS_ERROR(status_error);
    447   EXPECT_STATUS_ERROR(status_ok);
    448   EXPECT_STATUS_OK(status_error);
    449   EXPECT_STATUS_OK(status_ok);
    450 
    451   EXPECT_STATUS_ERROR_VALUE(EINVAL, status_error);
    452   EXPECT_STATUS_ERROR_VALUE(ENOMEM, status_error);
    453   EXPECT_STATUS_ERROR_VALUE(EINVAL, status_ok);
    454   EXPECT_STATUS_ERROR_VALUE(ENOMEM, status_ok);
    455 }
    456 
    457 TEST_F(Test, ExpectInt) {
    458   Status<int> status_error{ErrorStatus{EINVAL}};
    459   Status<int> status_ok{10};
    460 
    461   EXPECT_STATUS_ERROR(status_error);
    462   EXPECT_STATUS_ERROR(status_ok);
    463   EXPECT_STATUS_OK(status_error);
    464   EXPECT_STATUS_OK(status_ok);
    465 
    466   EXPECT_STATUS_ERROR_VALUE(EINVAL, status_error);
    467   EXPECT_STATUS_ERROR_VALUE(ENOMEM, status_error);
    468   EXPECT_STATUS_ERROR_VALUE(EINVAL, status_ok);
    469   EXPECT_STATUS_ERROR_VALUE(ENOMEM, status_ok);
    470 
    471   EXPECT_STATUS_EQ(10, status_error);
    472   EXPECT_STATUS_EQ(20, status_error);
    473   EXPECT_STATUS_EQ(10, status_ok);
    474   EXPECT_STATUS_EQ(20, status_ok);
    475 
    476   auto pred1 = [](const auto& value) { return value < 15; };
    477   auto pred2 = [](const auto& value) { return value > 5; };
    478   auto pred3 = [](const auto& value) { return value > 15; };
    479   auto pred4 = [](const auto& value) { return value < 5; };
    480 
    481   EXPECT_STATUS_PRED(pred1, status_error);
    482   EXPECT_STATUS_PRED(pred2, status_error);
    483   EXPECT_STATUS_PRED(pred3, status_error);
    484   EXPECT_STATUS_PRED(pred4, status_error);
    485   EXPECT_STATUS_PRED(pred1, status_ok);
    486   EXPECT_STATUS_PRED(pred2, status_ok);
    487   EXPECT_STATUS_PRED(pred3, status_ok);
    488   EXPECT_STATUS_PRED(pred4, status_ok);
    489 }
    490 #endif
    491 
    492 TEST_F(DvrDisplayManagerTest, SurfaceCreateEvent) {
    493   // Get surface state and verify there are no surfaces.
    494   ASSERT_STATUS_OK(manager_->UpdateSurfaceState());
    495   ASSERT_STATUS_EQ(0u, manager_->GetSurfaceCount());
    496 
    497   // Get flags for invalid surface index.
    498   EXPECT_STATUS_ERROR_VALUE(EINVAL, manager_->GetUpdateFlags(0));
    499 
    500   // Create an application surface.
    501   auto surface_status = CreateApplicationSurface();
    502   ASSERT_STATUS_OK(surface_status);
    503   UniqueDvrSurface surface = surface_status.take();
    504   ASSERT_NE(nullptr, surface.get());
    505 
    506   const int surface_id = dvrSurfaceGetId(surface.get());
    507   ASSERT_GE(surface_id, 0);
    508 
    509   // Now there should be one new surface.
    510   ASSERT_STATUS_OK(manager_->WaitForUpdate());
    511   EXPECT_STATUS_EQ(1u, manager_->GetSurfaceCount());
    512 
    513   // Verify the new surface flag is set.
    514   auto check_flags = [](const auto& value) {
    515     return value & DVR_SURFACE_UPDATE_FLAGS_NEW_SURFACE;
    516   };
    517   EXPECT_STATUS_PRED(check_flags, manager_->GetUpdateFlags(0));
    518 
    519   // Verify the surface id matches.
    520   EXPECT_STATUS_EQ(surface_id, manager_->GetSurfaceId(0));
    521 
    522   // Verify the owning process of the surface.
    523   EXPECT_STATUS_EQ(getpid(), manager_->GetProcessId(0));
    524 
    525   surface.reset();
    526 
    527   ASSERT_STATUS_OK(manager_->WaitForUpdate());
    528   EXPECT_STATUS_EQ(0u, manager_->GetSurfaceCount());
    529 }
    530 
    531 TEST_F(DvrDisplayManagerTest, SurfaceAttributeEvent) {
    532   // Get surface state and verify there are no surfaces.
    533   ASSERT_STATUS_OK(manager_->UpdateSurfaceState());
    534   ASSERT_STATUS_EQ(0u, manager_->GetSurfaceCount());
    535 
    536   // Get attributes for an invalid surface index.
    537   EXPECT_STATUS_ERROR_VALUE(EINVAL, manager_->GetAttributes(0));
    538 
    539   const bool kInitialVisibility = true;
    540   const int32_t kInitialZOrder = 10;
    541   auto surface_status =
    542       CreateApplicationSurface(kInitialVisibility, kInitialZOrder);
    543   ASSERT_STATUS_OK(surface_status);
    544   auto surface = surface_status.take();
    545   ASSERT_NE(nullptr, surface.get());
    546 
    547   ASSERT_STATUS_OK(manager_->WaitForUpdate());
    548   ASSERT_STATUS_EQ(1u, manager_->GetSurfaceCount());
    549 
    550   // Check the initial attribute values.
    551   auto attribute_status = manager_->GetAttributes(0);
    552   ASSERT_STATUS_OK(attribute_status);
    553   auto attributes = attribute_status.take();
    554   EXPECT_GE(attributes.size(), 2u);
    555 
    556   std::set<int32_t> actual_keys;
    557   std::set<int32_t> expected_keys = {DVR_SURFACE_ATTRIBUTE_Z_ORDER,
    558                                      DVR_SURFACE_ATTRIBUTE_VISIBLE};
    559 
    560   // Collect all the keys in attributes that match the expected keys.
    561   auto compare_keys = [](const auto& attributes, const auto& expected_keys) {
    562     std::set<int32_t> keys;
    563     for (const auto& attribute : attributes) {
    564       if (expected_keys.find(attribute.key) != expected_keys.end())
    565         keys.emplace(attribute.key);
    566     }
    567     return keys;
    568   };
    569 
    570   // If the sets match then attributes contained at least the expected keys,
    571   // even if other keys were also present.
    572   actual_keys = compare_keys(attributes, expected_keys);
    573   EXPECT_EQ(expected_keys, actual_keys);
    574 
    575   std::vector<DvrSurfaceAttribute> attributes_to_set = {
    576       MakeAttribute(DVR_SURFACE_ATTRIBUTE_Z_ORDER, 0)};
    577 
    578   // Test invalid args.
    579   EXPECT_EQ(-EINVAL, dvrSurfaceSetAttributes(nullptr, attributes_to_set.data(),
    580                                              attributes_to_set.size()));
    581   EXPECT_EQ(-EINVAL, dvrSurfaceSetAttributes(surface.get(), nullptr,
    582                                              attributes_to_set.size()));
    583 
    584   // Test attribute change events.
    585   ASSERT_EQ(0, dvrSurfaceSetAttributes(surface.get(), attributes_to_set.data(),
    586                                        attributes_to_set.size()));
    587   ASSERT_STATUS_OK(manager_->WaitForUpdate());
    588 
    589   // Verify the attributes changed flag is set.
    590   auto check_flags = [](const auto& value) {
    591     return value & DVR_SURFACE_UPDATE_FLAGS_ATTRIBUTES_CHANGED;
    592   };
    593   EXPECT_STATUS_PRED(check_flags, manager_->GetUpdateFlags(0));
    594 
    595   attribute_status = manager_->GetAttributes(0);
    596   ASSERT_STATUS_OK(attribute_status);
    597   attributes = attribute_status.take();
    598   EXPECT_GE(attributes.size(), 2u);
    599 
    600   expected_keys = {DVR_SURFACE_ATTRIBUTE_Z_ORDER,
    601                    DVR_SURFACE_ATTRIBUTE_VISIBLE};
    602 
    603   actual_keys.clear();
    604   actual_keys = compare_keys(attributes, expected_keys);
    605   EXPECT_EQ(expected_keys, actual_keys);
    606 
    607   // Test setting and then deleting an attribute.
    608   const DvrSurfaceAttributeKey kUserKey = 1;
    609   attributes_to_set = {MakeAttribute(kUserKey, 1024)};
    610 
    611   ASSERT_EQ(0, dvrSurfaceSetAttributes(surface.get(), attributes_to_set.data(),
    612                                        attributes_to_set.size()));
    613   ASSERT_STATUS_OK(manager_->WaitForUpdate());
    614   EXPECT_STATUS_PRED(check_flags, manager_->GetUpdateFlags(0));
    615 
    616   attribute_status = manager_->GetAttributes(0);
    617   ASSERT_STATUS_OK(attribute_status);
    618   attributes = attribute_status.take();
    619   EXPECT_GE(attributes.size(), 2u);
    620 
    621   expected_keys = {DVR_SURFACE_ATTRIBUTE_Z_ORDER, DVR_SURFACE_ATTRIBUTE_VISIBLE,
    622                    kUserKey};
    623 
    624   actual_keys.clear();
    625   actual_keys = compare_keys(attributes, expected_keys);
    626   EXPECT_EQ(expected_keys, actual_keys);
    627 
    628   // Delete the attribute.
    629   attributes_to_set = {MakeAttribute(kUserKey, nullptr)};
    630 
    631   ASSERT_EQ(0, dvrSurfaceSetAttributes(surface.get(), attributes_to_set.data(),
    632                                        attributes_to_set.size()));
    633   ASSERT_STATUS_OK(manager_->WaitForUpdate());
    634   EXPECT_STATUS_PRED(check_flags, manager_->GetUpdateFlags(0));
    635 
    636   attribute_status = manager_->GetAttributes(0);
    637   ASSERT_STATUS_OK(attribute_status);
    638   attributes = attribute_status.take();
    639   EXPECT_GE(attributes.size(), 2u);
    640 
    641   expected_keys = {DVR_SURFACE_ATTRIBUTE_Z_ORDER, DVR_SURFACE_ATTRIBUTE_VISIBLE,
    642                    kUserKey};
    643 
    644   actual_keys.clear();
    645   actual_keys = compare_keys(attributes, expected_keys);
    646   EXPECT_NE(expected_keys, actual_keys);
    647 
    648   // Test deleting a reserved attribute.
    649   attributes_to_set = {MakeAttribute(DVR_SURFACE_ATTRIBUTE_VISIBLE, nullptr)};
    650 
    651   EXPECT_EQ(0, dvrSurfaceSetAttributes(surface.get(), attributes_to_set.data(),
    652                                        attributes_to_set.size()));
    653 
    654   // Failed attribute operations should not trigger update events.
    655   const int kTimeoutMs = 100;  // 0.1s
    656   EXPECT_STATUS_ERROR_VALUE(ETIMEDOUT, manager_->WaitForUpdate(kTimeoutMs));
    657 
    658   attribute_status = manager_->GetAttributes(0);
    659   ASSERT_STATUS_OK(attribute_status);
    660   attributes = attribute_status.take();
    661   EXPECT_GE(attributes.size(), 2u);
    662 
    663   expected_keys = {DVR_SURFACE_ATTRIBUTE_Z_ORDER,
    664                    DVR_SURFACE_ATTRIBUTE_VISIBLE};
    665 
    666   actual_keys.clear();
    667   actual_keys = compare_keys(attributes, expected_keys);
    668   EXPECT_EQ(expected_keys, actual_keys);
    669 }
    670 
    671 TEST_F(DvrDisplayManagerTest, SurfaceAttributeTypes) {
    672   // Create an application surface.
    673   auto surface_status = CreateApplicationSurface();
    674   ASSERT_STATUS_OK(surface_status);
    675   UniqueDvrSurface surface = surface_status.take();
    676   ASSERT_NE(nullptr, surface.get());
    677 
    678   enum : std::int32_t {
    679     kInt32Key = 1,
    680     kInt64Key,
    681     kBoolKey,
    682     kFloatKey,
    683     kFloat2Key,
    684     kFloat3Key,
    685     kFloat4Key,
    686     kFloat8Key,
    687     kFloat16Key,
    688   };
    689 
    690   const std::vector<DvrSurfaceAttribute> attributes_to_set = {
    691       MakeAttribute(kInt32Key, int32_t{0}),
    692       MakeAttribute(kInt64Key, int64_t{0}),
    693       MakeAttribute(kBoolKey, false),
    694       MakeAttribute(kFloatKey, 0.0f),
    695       MakeAttribute(kFloat2Key, std::array<float, 2>{{1.0f, 2.0f}}),
    696       MakeAttribute(kFloat3Key, std::array<float, 3>{{3.0f, 4.0f, 5.0f}}),
    697       MakeAttribute(kFloat4Key, std::array<float, 4>{{6.0f, 7.0f, 8.0f, 9.0f}}),
    698       MakeAttribute(kFloat8Key,
    699                     std::array<float, 8>{{10.0f, 11.0f, 12.0f, 13.0f, 14.0f,
    700                                           15.0f, 16.0f, 17.0f}}),
    701       MakeAttribute(kFloat16Key, std::array<float, 16>{
    702                                      {18.0f, 19.0f, 20.0f, 21.0f, 22.0f, 23.0f,
    703                                       24.0f, 25.0f, 26.0f, 27.0f, 28.0f, 29.0f,
    704                                       30.0f, 31.0f, 32.0f, 33.0f}})};
    705 
    706   EXPECT_EQ(0, dvrSurfaceSetAttributes(surface.get(), attributes_to_set.data(),
    707                                        attributes_to_set.size()));
    708 
    709   ASSERT_STATUS_OK(manager_->WaitForUpdate());
    710   auto attribute_status = manager_->GetAttributes(0);
    711   ASSERT_STATUS_OK(attribute_status);
    712   auto attributes = attribute_status.take();
    713   EXPECT_GE(attributes.size(), attributes_to_set.size());
    714 
    715   auto HasAttribute = [](const auto& attributes,
    716                          DvrSurfaceAttributeKey key) -> bool {
    717     for (const auto& attribute : attributes) {
    718       if (attribute.key == key)
    719         return true;
    720     }
    721     return false;
    722   };
    723   auto AttributeType =
    724       [](const auto& attributes,
    725          DvrSurfaceAttributeKey key) -> DvrSurfaceAttributeType {
    726     for (const auto& attribute : attributes) {
    727       if (attribute.key == key)
    728         return attribute.value.type;
    729     }
    730     return DVR_SURFACE_ATTRIBUTE_TYPE_NONE;
    731   };
    732 
    733   ASSERT_TRUE(HasAttribute(attributes, kInt32Key));
    734   EXPECT_EQ(DVR_SURFACE_ATTRIBUTE_TYPE_INT32,
    735             AttributeType(attributes, kInt32Key));
    736 
    737   ASSERT_TRUE(HasAttribute(attributes, kInt64Key));
    738   EXPECT_EQ(DVR_SURFACE_ATTRIBUTE_TYPE_INT64,
    739             AttributeType(attributes, kInt64Key));
    740 
    741   ASSERT_TRUE(HasAttribute(attributes, kBoolKey));
    742   EXPECT_EQ(DVR_SURFACE_ATTRIBUTE_TYPE_BOOL,
    743             AttributeType(attributes, kBoolKey));
    744 
    745   ASSERT_TRUE(HasAttribute(attributes, kFloatKey));
    746   EXPECT_EQ(DVR_SURFACE_ATTRIBUTE_TYPE_FLOAT,
    747             AttributeType(attributes, kFloatKey));
    748 
    749   ASSERT_TRUE(HasAttribute(attributes, kFloat2Key));
    750   EXPECT_EQ(DVR_SURFACE_ATTRIBUTE_TYPE_FLOAT2,
    751             AttributeType(attributes, kFloat2Key));
    752 
    753   ASSERT_TRUE(HasAttribute(attributes, kFloat3Key));
    754   EXPECT_EQ(DVR_SURFACE_ATTRIBUTE_TYPE_FLOAT3,
    755             AttributeType(attributes, kFloat3Key));
    756 
    757   ASSERT_TRUE(HasAttribute(attributes, kFloat4Key));
    758   EXPECT_EQ(DVR_SURFACE_ATTRIBUTE_TYPE_FLOAT4,
    759             AttributeType(attributes, kFloat4Key));
    760 
    761   ASSERT_TRUE(HasAttribute(attributes, kFloat8Key));
    762   EXPECT_EQ(DVR_SURFACE_ATTRIBUTE_TYPE_FLOAT8,
    763             AttributeType(attributes, kFloat8Key));
    764 
    765   ASSERT_TRUE(HasAttribute(attributes, kFloat16Key));
    766   EXPECT_EQ(DVR_SURFACE_ATTRIBUTE_TYPE_FLOAT16,
    767             AttributeType(attributes, kFloat16Key));
    768 }
    769 
    770 TEST_F(DvrDisplayManagerTest, SurfaceQueueEvent) {
    771   // Create an application surface.
    772   auto surface_status = CreateApplicationSurface();
    773   ASSERT_STATUS_OK(surface_status);
    774   UniqueDvrSurface surface = surface_status.take();
    775   ASSERT_NE(nullptr, surface.get());
    776 
    777   const int surface_id = dvrSurfaceGetId(surface.get());
    778   ASSERT_GE(surface_id, 0);
    779   // Get surface state and verify there is one surface.
    780   ASSERT_STATUS_OK(manager_->WaitForUpdate());
    781   ASSERT_STATUS_EQ(1u, manager_->GetSurfaceCount());
    782 
    783   // Verify there are no queues for the surface recorded in the state
    784   // snapshot.
    785   EXPECT_STATUS_EQ(std::vector<int>{}, manager_->GetQueueIds(0));
    786 
    787   // Create a new queue in the surface.
    788   auto write_queue_status = CreateSurfaceQueue(
    789       surface, 320, 240, AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM, 1,
    790       AHARDWAREBUFFER_USAGE_CPU_READ_RARELY, 1, 0);
    791   ASSERT_STATUS_OK(write_queue_status);
    792   UniqueDvrWriteBufferQueue write_queue = write_queue_status.take();
    793   ASSERT_NE(nullptr, write_queue.get());
    794 
    795   const int queue_id = dvrWriteBufferQueueGetId(write_queue.get());
    796   ASSERT_GE(queue_id, 0);
    797 
    798   // Update surface state.
    799   ASSERT_STATUS_OK(manager_->WaitForUpdate());
    800   ASSERT_STATUS_EQ(1u, manager_->GetSurfaceCount());
    801 
    802   // Verify the buffers changed flag is set.
    803   auto check_flags = [](const auto& value) {
    804     return value & DVR_SURFACE_UPDATE_FLAGS_BUFFERS_CHANGED;
    805   };
    806   EXPECT_STATUS_PRED(check_flags, manager_->GetUpdateFlags(0));
    807 
    808   auto queue_ids_status = manager_->GetQueueIds(0);
    809   ASSERT_STATUS_OK(queue_ids_status);
    810 
    811   auto queue_ids = queue_ids_status.take();
    812   ASSERT_EQ(1u, queue_ids.size());
    813   EXPECT_EQ(queue_id, queue_ids[0]);
    814 
    815   auto read_queue_status = manager_->GetReadBufferQueue(surface_id, queue_id);
    816   ASSERT_STATUS_OK(read_queue_status);
    817   UniqueDvrReadBufferQueue read_queue = read_queue_status.take();
    818   ASSERT_NE(nullptr, read_queue.get());
    819   EXPECT_EQ(queue_id, dvrReadBufferQueueGetId(read_queue.get()));
    820 
    821   write_queue.reset();
    822 
    823   // Verify that destroying the queue generates a surface update event.
    824   ASSERT_STATUS_OK(manager_->WaitForUpdate());
    825   ASSERT_STATUS_EQ(1u, manager_->GetSurfaceCount());
    826 
    827   // Verify that the buffers changed flag is set.
    828   EXPECT_STATUS_PRED(check_flags, manager_->GetUpdateFlags(0));
    829 
    830   // Verify that the queue ids reflect the change.
    831   queue_ids_status = manager_->GetQueueIds(0);
    832   ASSERT_STATUS_OK(queue_ids_status);
    833 
    834   queue_ids = queue_ids_status.take();
    835   ASSERT_EQ(0u, queue_ids.size());
    836 }
    837 
    838 TEST_F(DvrDisplayManagerTest, MultiLayerBufferQueue) {
    839   // Create an application surface.
    840   auto surface_status = CreateApplicationSurface();
    841   ASSERT_STATUS_OK(surface_status);
    842   UniqueDvrSurface surface = surface_status.take();
    843   ASSERT_NE(nullptr, surface.get());
    844 
    845   // Get surface state and verify there is one surface.
    846   ASSERT_STATUS_OK(manager_->WaitForUpdate());
    847   ASSERT_STATUS_EQ(1u, manager_->GetSurfaceCount());
    848 
    849   // Create a new queue in the surface.
    850   const uint32_t kLayerCount = 3;
    851   auto write_queue_status = CreateSurfaceQueue(
    852       surface, 320, 240, AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM, kLayerCount,
    853       AHARDWAREBUFFER_USAGE_CPU_READ_RARELY, 1, 0);
    854   ASSERT_STATUS_OK(write_queue_status);
    855   UniqueDvrWriteBufferQueue write_queue = write_queue_status.take();
    856   ASSERT_NE(nullptr, write_queue.get());
    857 
    858   DvrWriteBuffer* buffer = nullptr;
    859   DvrNativeBufferMetadata metadata;
    860   int fence_fd = -1;
    861   int error = dvrWriteBufferQueueGainBuffer(write_queue.get(), /*timeout=*/1000,
    862                                             &buffer, &metadata, &fence_fd);
    863   ASSERT_EQ(0, error);
    864 
    865   AHardwareBuffer* hardware_buffer = nullptr;
    866   error = dvrWriteBufferGetAHardwareBuffer(buffer, &hardware_buffer);
    867   ASSERT_EQ(0, error);
    868 
    869   AHardwareBuffer_Desc desc = {};
    870   AHardwareBuffer_describe(hardware_buffer, &desc);
    871   ASSERT_EQ(kLayerCount, desc.layers);
    872 
    873   AHardwareBuffer_release(hardware_buffer);
    874   dvrWriteBufferDestroy(buffer);
    875 }
    876 
    877 TEST_F(Test, ConfigurationData) {
    878   // TODO(hendrikw): Move this test and GetConfigData helper function out of the
    879   // display manager tests.
    880   auto data1 = GetConfigData(-1);
    881   ASSERT_STATUS_ERROR(data1);
    882 
    883   const char kDvrLensMetricsProperty[] = "ro.dvr.lens_metrics";
    884 
    885   // This should be run on devices with and without built in metrics.
    886   bool has_metric = !base::GetProperty(kDvrLensMetricsProperty, "").empty();
    887   auto data2 = GetConfigData(DVR_CONFIGURATION_DATA_LENS_METRICS);
    888   if (has_metric) {
    889     ASSERT_STATUS_OK(data2);
    890     ASSERT_NE(0u, data2.get().size());
    891   } else {
    892     ASSERT_STATUS_ERROR(data2);
    893   }
    894 }
    895 
    896 }  // namespace
    897 
    898 }  // namespace dvr
    899 }  // namespace android
    900