• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
MakeAttribute(DvrSurfaceAttributeKey key,nullptr_t)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 
MakeAttribute(DvrSurfaceAttributeKey key,int32_t value)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 
MakeAttribute(DvrSurfaceAttributeKey key,int64_t value)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 
MakeAttribute(DvrSurfaceAttributeKey key,bool value)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 
MakeAttribute(DvrSurfaceAttributeKey key,float value)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 
MakeAttribute(DvrSurfaceAttributeKey key,const std::array<float,2> & value)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 
MakeAttribute(DvrSurfaceAttributeKey key,const std::array<float,3> & value)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 
MakeAttribute(DvrSurfaceAttributeKey key,const std::array<float,4> & value)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 
MakeAttribute(DvrSurfaceAttributeKey key,const std::array<float,8> & value)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 
MakeAttribute(DvrSurfaceAttributeKey key,const std::array<float,16> & value)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 
CreateApplicationSurface(bool visible=false,int32_t z_order=0)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 
CreateSurfaceQueue(const UniqueDvrSurface & surface,uint32_t width,uint32_t height,uint32_t format,uint32_t layer_count,uint64_t usage,size_t capacity,size_t metadata_size)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 
GetConfigData(int config_type)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:
TestDisplayManager(UniqueDvrDisplayManager display_manager,UniqueDvrSurfaceState surface_state)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 
GetReadBufferQueue(int surface_id,int queue_id)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 
UpdateSurfaceState()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 
WaitForUpdate(int timeout_ms=kTimeoutMs)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 
GetSurfaceCount()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 
GetUpdateFlags(size_t surface_index)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 
GetSurfaceId(size_t surface_index)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 
GetProcessId(size_t surface_index)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 
GetAttributes(size_t surface_index)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 
GetQueueIds(size_t surface_index)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:
SetUp()310   void SetUp() override {
311     int ret;
312     DvrDisplayManager* display_manager;
313     DvrSurfaceState* surface_state;
314 
315     ret = dvrDisplayManagerCreate(&display_manager);
316     ASSERT_EQ(0, ret) << "Failed to create display manager client";
317     ASSERT_NE(nullptr, display_manager);
318 
319     ret = dvrSurfaceStateCreate(&surface_state);
320     ASSERT_EQ(0, ret) << "Failed to create surface state object";
321     ASSERT_NE(nullptr, surface_state);
322 
323     manager_.reset(
324         new TestDisplayManager(UniqueDvrDisplayManager(display_manager),
325                                UniqueDvrSurfaceState(surface_state)));
326   }
TearDown()327   void TearDown() override {}
328 
329   std::unique_ptr<TestDisplayManager> manager_;
330 };
331 
332 // TODO(eieio): Consider moving these somewhere more central because they are
333 // broadly useful.
334 
335 template <typename T>
StatusOk(const char * status_expression,const Status<T> & status)336 testing::AssertionResult StatusOk(const char* status_expression,
337                                   const Status<T>& status) {
338   if (!status.ok()) {
339     return testing::AssertionFailure()
340            << "(" << status_expression
341            << ") expected to indicate success but actually contains error ("
342            << status.error() << ")";
343   } else {
344     return testing::AssertionSuccess();
345   }
346 }
347 
348 template <typename T>
StatusError(const char * status_expression,const Status<T> & status)349 testing::AssertionResult StatusError(const char* status_expression,
350                                      const Status<T>& status) {
351   if (status.ok()) {
352     return testing::AssertionFailure()
353            << "(" << status_expression
354            << ") expected to indicate error but instead indicates success.";
355   } else {
356     return testing::AssertionSuccess();
357   }
358 }
359 
360 template <typename T>
StatusHasError(const char * status_expression,const char *,const Status<T> & status,int error_code)361 testing::AssertionResult StatusHasError(const char* status_expression,
362                                         const char* /*error_code_expression*/,
363                                         const Status<T>& status,
364                                         int error_code) {
365   if (status.ok()) {
366     return StatusError(status_expression, status);
367   } else if (status.error() != error_code) {
368     return testing::AssertionFailure()
369            << "(" << status_expression << ") expected to indicate error ("
370            << error_code << ") but actually indicates error (" << status.error()
371            << ")";
372   } else {
373     return testing::AssertionSuccess();
374   }
375 }
376 
377 template <typename T, typename U>
StatusHasValue(const char * status_expression,const char *,const Status<T> & status,const U & value)378 testing::AssertionResult StatusHasValue(const char* status_expression,
379                                         const char* /*value_expression*/,
380                                         const Status<T>& status,
381                                         const U& value) {
382   if (!status.ok()) {
383     return StatusOk(status_expression, status);
384   } else if (status.get() != value) {
385     return testing::AssertionFailure()
386            << "(" << status_expression << ") expected to contain value ("
387            << testing::PrintToString(value) << ") but actually contains value ("
388            << testing::PrintToString(status.get()) << ")";
389   } else {
390     return testing::AssertionSuccess();
391   }
392 }
393 
394 template <typename T, typename Op>
StatusPred(const char * status_expression,const char * pred_expression,const Status<T> & status,Op pred)395 testing::AssertionResult StatusPred(const char* status_expression,
396                                     const char* pred_expression,
397                                     const Status<T>& status, Op pred) {
398   if (!status.ok()) {
399     return StatusOk(status_expression, status);
400   } else if (!pred(status.get())) {
401     return testing::AssertionFailure()
402            << status_expression << " value ("
403            << testing::PrintToString(status.get())
404            << ") failed to pass predicate " << pred_expression;
405   } else {
406     return testing::AssertionSuccess();
407   }
408 }
409 
410 #define ASSERT_STATUS_OK(status) ASSERT_PRED_FORMAT1(StatusOk, status)
411 #define ASSERT_STATUS_ERROR(status) ASSERT_PRED_FORMAT1(StatusError, status)
412 
413 #define ASSERT_STATUS_ERROR_VALUE(value, status) \
414   ASSERT_PRED_FORMAT2(StatusHasError, status, value)
415 
416 #define ASSERT_STATUS_EQ(value, status) \
417   ASSERT_PRED_FORMAT2(StatusHasValue, status, value)
418 
419 #define EXPECT_STATUS_OK(status) EXPECT_PRED_FORMAT1(StatusOk, status)
420 #define EXPECT_STATUS_ERROR(status) EXPECT_PRED_FORMAT1(StatusError, status)
421 
422 #define EXPECT_STATUS_ERROR_VALUE(value, status) \
423   EXPECT_PRED_FORMAT2(StatusHasError, status, value)
424 
425 #define EXPECT_STATUS_EQ(value, status) \
426   EXPECT_PRED_FORMAT2(StatusHasValue, status, value)
427 
428 #define EXPECT_STATUS_PRED(pred, status) \
429   EXPECT_PRED_FORMAT2(StatusPred, status, pred)
430 
431 #if 0
432 // Verify utility predicate/macro functionality. This section is commented out
433 // because it is designed to fail in some cases to validate the helpers.
434 TEST_F(Test, ExpectVoid) {
435   Status<void> status_error{ErrorStatus{EINVAL}};
436   Status<void> status_ok{};
437 
438   EXPECT_STATUS_ERROR(status_error);
439   EXPECT_STATUS_ERROR(status_ok);
440   EXPECT_STATUS_OK(status_error);
441   EXPECT_STATUS_OK(status_ok);
442 
443   EXPECT_STATUS_ERROR_VALUE(EINVAL, status_error);
444   EXPECT_STATUS_ERROR_VALUE(ENOMEM, status_error);
445   EXPECT_STATUS_ERROR_VALUE(EINVAL, status_ok);
446   EXPECT_STATUS_ERROR_VALUE(ENOMEM, status_ok);
447 }
448 
449 TEST_F(Test, ExpectInt) {
450   Status<int> status_error{ErrorStatus{EINVAL}};
451   Status<int> status_ok{10};
452 
453   EXPECT_STATUS_ERROR(status_error);
454   EXPECT_STATUS_ERROR(status_ok);
455   EXPECT_STATUS_OK(status_error);
456   EXPECT_STATUS_OK(status_ok);
457 
458   EXPECT_STATUS_ERROR_VALUE(EINVAL, status_error);
459   EXPECT_STATUS_ERROR_VALUE(ENOMEM, status_error);
460   EXPECT_STATUS_ERROR_VALUE(EINVAL, status_ok);
461   EXPECT_STATUS_ERROR_VALUE(ENOMEM, status_ok);
462 
463   EXPECT_STATUS_EQ(10, status_error);
464   EXPECT_STATUS_EQ(20, status_error);
465   EXPECT_STATUS_EQ(10, status_ok);
466   EXPECT_STATUS_EQ(20, status_ok);
467 
468   auto pred1 = [](const auto& value) { return value < 15; };
469   auto pred2 = [](const auto& value) { return value > 5; };
470   auto pred3 = [](const auto& value) { return value > 15; };
471   auto pred4 = [](const auto& value) { return value < 5; };
472 
473   EXPECT_STATUS_PRED(pred1, status_error);
474   EXPECT_STATUS_PRED(pred2, status_error);
475   EXPECT_STATUS_PRED(pred3, status_error);
476   EXPECT_STATUS_PRED(pred4, status_error);
477   EXPECT_STATUS_PRED(pred1, status_ok);
478   EXPECT_STATUS_PRED(pred2, status_ok);
479   EXPECT_STATUS_PRED(pred3, status_ok);
480   EXPECT_STATUS_PRED(pred4, status_ok);
481 }
482 #endif
483 
TEST_F(DvrDisplayManagerTest,SurfaceCreateEvent)484 TEST_F(DvrDisplayManagerTest, SurfaceCreateEvent) {
485   // Get surface state and verify there are no surfaces.
486   ASSERT_STATUS_OK(manager_->UpdateSurfaceState());
487   ASSERT_STATUS_EQ(0u, manager_->GetSurfaceCount());
488 
489   // Get flags for invalid surface index.
490   EXPECT_STATUS_ERROR_VALUE(EINVAL, manager_->GetUpdateFlags(0));
491 
492   // Create an application surface.
493   auto surface_status = CreateApplicationSurface();
494   ASSERT_STATUS_OK(surface_status);
495   UniqueDvrSurface surface = surface_status.take();
496   ASSERT_NE(nullptr, surface.get());
497 
498   const int surface_id = dvrSurfaceGetId(surface.get());
499   ASSERT_GE(surface_id, 0);
500 
501   // Now there should be one new surface.
502   ASSERT_STATUS_OK(manager_->WaitForUpdate());
503   EXPECT_STATUS_EQ(1u, manager_->GetSurfaceCount());
504 
505   // Verify the new surface flag is set.
506   auto check_flags = [](const auto& value) {
507     return value & DVR_SURFACE_UPDATE_FLAGS_NEW_SURFACE;
508   };
509   EXPECT_STATUS_PRED(check_flags, manager_->GetUpdateFlags(0));
510 
511   // Verify the surface id matches.
512   EXPECT_STATUS_EQ(surface_id, manager_->GetSurfaceId(0));
513 
514   // Verify the owning process of the surface.
515   EXPECT_STATUS_EQ(getpid(), manager_->GetProcessId(0));
516 
517   surface.reset();
518 
519   ASSERT_STATUS_OK(manager_->WaitForUpdate());
520   EXPECT_STATUS_EQ(0u, manager_->GetSurfaceCount());
521 }
522 
TEST_F(DvrDisplayManagerTest,SurfaceAttributeEvent)523 TEST_F(DvrDisplayManagerTest, SurfaceAttributeEvent) {
524   // Get surface state and verify there are no surfaces.
525   ASSERT_STATUS_OK(manager_->UpdateSurfaceState());
526   ASSERT_STATUS_EQ(0u, manager_->GetSurfaceCount());
527 
528   // Get attributes for an invalid surface index.
529   EXPECT_STATUS_ERROR_VALUE(EINVAL, manager_->GetAttributes(0));
530 
531   const bool kInitialVisibility = true;
532   const int32_t kInitialZOrder = 10;
533   auto surface_status =
534       CreateApplicationSurface(kInitialVisibility, kInitialZOrder);
535   ASSERT_STATUS_OK(surface_status);
536   auto surface = surface_status.take();
537   ASSERT_NE(nullptr, surface.get());
538 
539   ASSERT_STATUS_OK(manager_->WaitForUpdate());
540   ASSERT_STATUS_EQ(1u, manager_->GetSurfaceCount());
541 
542   // Check the initial attribute values.
543   auto attribute_status = manager_->GetAttributes(0);
544   ASSERT_STATUS_OK(attribute_status);
545   auto attributes = attribute_status.take();
546   EXPECT_GE(attributes.size(), 2u);
547 
548   std::set<int32_t> actual_keys;
549   std::set<int32_t> expected_keys = {DVR_SURFACE_ATTRIBUTE_Z_ORDER,
550                                      DVR_SURFACE_ATTRIBUTE_VISIBLE};
551 
552   // Collect all the keys in attributes that match the expected keys.
553   auto compare_keys = [](const auto& attributes, const auto& expected_keys) {
554     std::set<int32_t> keys;
555     for (const auto& attribute : attributes) {
556       if (expected_keys.find(attribute.key) != expected_keys.end())
557         keys.emplace(attribute.key);
558     }
559     return keys;
560   };
561 
562   // If the sets match then attributes contained at least the expected keys,
563   // even if other keys were also present.
564   actual_keys = compare_keys(attributes, expected_keys);
565   EXPECT_EQ(expected_keys, actual_keys);
566 
567   std::vector<DvrSurfaceAttribute> attributes_to_set = {
568       MakeAttribute(DVR_SURFACE_ATTRIBUTE_Z_ORDER, 0)};
569 
570   // Test invalid args.
571   EXPECT_EQ(-EINVAL, dvrSurfaceSetAttributes(nullptr, attributes_to_set.data(),
572                                              attributes_to_set.size()));
573   EXPECT_EQ(-EINVAL, dvrSurfaceSetAttributes(surface.get(), nullptr,
574                                              attributes_to_set.size()));
575 
576   // Test attribute change events.
577   ASSERT_EQ(0, dvrSurfaceSetAttributes(surface.get(), attributes_to_set.data(),
578                                        attributes_to_set.size()));
579   ASSERT_STATUS_OK(manager_->WaitForUpdate());
580 
581   // Verify the attributes changed flag is set.
582   auto check_flags = [](const auto& value) {
583     return value & DVR_SURFACE_UPDATE_FLAGS_ATTRIBUTES_CHANGED;
584   };
585   EXPECT_STATUS_PRED(check_flags, manager_->GetUpdateFlags(0));
586 
587   attribute_status = manager_->GetAttributes(0);
588   ASSERT_STATUS_OK(attribute_status);
589   attributes = attribute_status.take();
590   EXPECT_GE(attributes.size(), 2u);
591 
592   expected_keys = {DVR_SURFACE_ATTRIBUTE_Z_ORDER,
593                    DVR_SURFACE_ATTRIBUTE_VISIBLE};
594 
595   actual_keys.clear();
596   actual_keys = compare_keys(attributes, expected_keys);
597   EXPECT_EQ(expected_keys, actual_keys);
598 
599   // Test setting and then deleting an attribute.
600   const DvrSurfaceAttributeKey kUserKey = 1;
601   attributes_to_set = {MakeAttribute(kUserKey, 1024)};
602 
603   ASSERT_EQ(0, dvrSurfaceSetAttributes(surface.get(), attributes_to_set.data(),
604                                        attributes_to_set.size()));
605   ASSERT_STATUS_OK(manager_->WaitForUpdate());
606   EXPECT_STATUS_PRED(check_flags, manager_->GetUpdateFlags(0));
607 
608   attribute_status = manager_->GetAttributes(0);
609   ASSERT_STATUS_OK(attribute_status);
610   attributes = attribute_status.take();
611   EXPECT_GE(attributes.size(), 2u);
612 
613   expected_keys = {DVR_SURFACE_ATTRIBUTE_Z_ORDER, DVR_SURFACE_ATTRIBUTE_VISIBLE,
614                    kUserKey};
615 
616   actual_keys.clear();
617   actual_keys = compare_keys(attributes, expected_keys);
618   EXPECT_EQ(expected_keys, actual_keys);
619 
620   // Delete the attribute.
621   attributes_to_set = {MakeAttribute(kUserKey, nullptr)};
622 
623   ASSERT_EQ(0, dvrSurfaceSetAttributes(surface.get(), attributes_to_set.data(),
624                                        attributes_to_set.size()));
625   ASSERT_STATUS_OK(manager_->WaitForUpdate());
626   EXPECT_STATUS_PRED(check_flags, manager_->GetUpdateFlags(0));
627 
628   attribute_status = manager_->GetAttributes(0);
629   ASSERT_STATUS_OK(attribute_status);
630   attributes = attribute_status.take();
631   EXPECT_GE(attributes.size(), 2u);
632 
633   expected_keys = {DVR_SURFACE_ATTRIBUTE_Z_ORDER, DVR_SURFACE_ATTRIBUTE_VISIBLE,
634                    kUserKey};
635 
636   actual_keys.clear();
637   actual_keys = compare_keys(attributes, expected_keys);
638   EXPECT_NE(expected_keys, actual_keys);
639 
640   // Test deleting a reserved attribute.
641   attributes_to_set = {MakeAttribute(DVR_SURFACE_ATTRIBUTE_VISIBLE, nullptr)};
642 
643   EXPECT_EQ(0, dvrSurfaceSetAttributes(surface.get(), attributes_to_set.data(),
644                                        attributes_to_set.size()));
645 
646   // Failed attribute operations should not trigger update events.
647   const int kTimeoutMs = 100;  // 0.1s
648   EXPECT_STATUS_ERROR_VALUE(ETIMEDOUT, manager_->WaitForUpdate(kTimeoutMs));
649 
650   attribute_status = manager_->GetAttributes(0);
651   ASSERT_STATUS_OK(attribute_status);
652   attributes = attribute_status.take();
653   EXPECT_GE(attributes.size(), 2u);
654 
655   expected_keys = {DVR_SURFACE_ATTRIBUTE_Z_ORDER,
656                    DVR_SURFACE_ATTRIBUTE_VISIBLE};
657 
658   actual_keys.clear();
659   actual_keys = compare_keys(attributes, expected_keys);
660   EXPECT_EQ(expected_keys, actual_keys);
661 }
662 
TEST_F(DvrDisplayManagerTest,SurfaceAttributeTypes)663 TEST_F(DvrDisplayManagerTest, SurfaceAttributeTypes) {
664   // Create an application surface.
665   auto surface_status = CreateApplicationSurface();
666   ASSERT_STATUS_OK(surface_status);
667   UniqueDvrSurface surface = surface_status.take();
668   ASSERT_NE(nullptr, surface.get());
669 
670   enum : std::int32_t {
671     kInt32Key = 1,
672     kInt64Key,
673     kBoolKey,
674     kFloatKey,
675     kFloat2Key,
676     kFloat3Key,
677     kFloat4Key,
678     kFloat8Key,
679     kFloat16Key,
680   };
681 
682   const std::vector<DvrSurfaceAttribute> attributes_to_set = {
683       MakeAttribute(kInt32Key, int32_t{0}),
684       MakeAttribute(kInt64Key, int64_t{0}),
685       MakeAttribute(kBoolKey, false),
686       MakeAttribute(kFloatKey, 0.0f),
687       MakeAttribute(kFloat2Key, std::array<float, 2>{{1.0f, 2.0f}}),
688       MakeAttribute(kFloat3Key, std::array<float, 3>{{3.0f, 4.0f, 5.0f}}),
689       MakeAttribute(kFloat4Key, std::array<float, 4>{{6.0f, 7.0f, 8.0f, 9.0f}}),
690       MakeAttribute(kFloat8Key,
691                     std::array<float, 8>{{10.0f, 11.0f, 12.0f, 13.0f, 14.0f,
692                                           15.0f, 16.0f, 17.0f}}),
693       MakeAttribute(kFloat16Key, std::array<float, 16>{
694                                      {18.0f, 19.0f, 20.0f, 21.0f, 22.0f, 23.0f,
695                                       24.0f, 25.0f, 26.0f, 27.0f, 28.0f, 29.0f,
696                                       30.0f, 31.0f, 32.0f, 33.0f}})};
697 
698   EXPECT_EQ(0, dvrSurfaceSetAttributes(surface.get(), attributes_to_set.data(),
699                                        attributes_to_set.size()));
700 
701   ASSERT_STATUS_OK(manager_->WaitForUpdate());
702   auto attribute_status = manager_->GetAttributes(0);
703   ASSERT_STATUS_OK(attribute_status);
704   auto attributes = attribute_status.take();
705   EXPECT_GE(attributes.size(), attributes_to_set.size());
706 
707   auto HasAttribute = [](const auto& attributes,
708                          DvrSurfaceAttributeKey key) -> bool {
709     for (const auto& attribute : attributes) {
710       if (attribute.key == key)
711         return true;
712     }
713     return false;
714   };
715   auto AttributeType =
716       [](const auto& attributes,
717          DvrSurfaceAttributeKey key) -> DvrSurfaceAttributeType {
718     for (const auto& attribute : attributes) {
719       if (attribute.key == key)
720         return attribute.value.type;
721     }
722     return DVR_SURFACE_ATTRIBUTE_TYPE_NONE;
723   };
724 
725   ASSERT_TRUE(HasAttribute(attributes, kInt32Key));
726   EXPECT_EQ(DVR_SURFACE_ATTRIBUTE_TYPE_INT32,
727             AttributeType(attributes, kInt32Key));
728 
729   ASSERT_TRUE(HasAttribute(attributes, kInt64Key));
730   EXPECT_EQ(DVR_SURFACE_ATTRIBUTE_TYPE_INT64,
731             AttributeType(attributes, kInt64Key));
732 
733   ASSERT_TRUE(HasAttribute(attributes, kBoolKey));
734   EXPECT_EQ(DVR_SURFACE_ATTRIBUTE_TYPE_BOOL,
735             AttributeType(attributes, kBoolKey));
736 
737   ASSERT_TRUE(HasAttribute(attributes, kFloatKey));
738   EXPECT_EQ(DVR_SURFACE_ATTRIBUTE_TYPE_FLOAT,
739             AttributeType(attributes, kFloatKey));
740 
741   ASSERT_TRUE(HasAttribute(attributes, kFloat2Key));
742   EXPECT_EQ(DVR_SURFACE_ATTRIBUTE_TYPE_FLOAT2,
743             AttributeType(attributes, kFloat2Key));
744 
745   ASSERT_TRUE(HasAttribute(attributes, kFloat3Key));
746   EXPECT_EQ(DVR_SURFACE_ATTRIBUTE_TYPE_FLOAT3,
747             AttributeType(attributes, kFloat3Key));
748 
749   ASSERT_TRUE(HasAttribute(attributes, kFloat4Key));
750   EXPECT_EQ(DVR_SURFACE_ATTRIBUTE_TYPE_FLOAT4,
751             AttributeType(attributes, kFloat4Key));
752 
753   ASSERT_TRUE(HasAttribute(attributes, kFloat8Key));
754   EXPECT_EQ(DVR_SURFACE_ATTRIBUTE_TYPE_FLOAT8,
755             AttributeType(attributes, kFloat8Key));
756 
757   ASSERT_TRUE(HasAttribute(attributes, kFloat16Key));
758   EXPECT_EQ(DVR_SURFACE_ATTRIBUTE_TYPE_FLOAT16,
759             AttributeType(attributes, kFloat16Key));
760 }
761 
TEST_F(DvrDisplayManagerTest,SurfaceQueueEvent)762 TEST_F(DvrDisplayManagerTest, SurfaceQueueEvent) {
763   // Create an application surface.
764   auto surface_status = CreateApplicationSurface();
765   ASSERT_STATUS_OK(surface_status);
766   UniqueDvrSurface surface = surface_status.take();
767   ASSERT_NE(nullptr, surface.get());
768 
769   const int surface_id = dvrSurfaceGetId(surface.get());
770   ASSERT_GE(surface_id, 0);
771   // Get surface state and verify there is one surface.
772   ASSERT_STATUS_OK(manager_->WaitForUpdate());
773   ASSERT_STATUS_EQ(1u, manager_->GetSurfaceCount());
774 
775   // Verify there are no queues for the surface recorded in the state
776   // snapshot.
777   EXPECT_STATUS_EQ(std::vector<int>{}, manager_->GetQueueIds(0));
778 
779   // Create a new queue in the surface.
780   auto write_queue_status = CreateSurfaceQueue(
781       surface, 320, 240, AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM, 1,
782       AHARDWAREBUFFER_USAGE_CPU_READ_RARELY, 1, 0);
783   ASSERT_STATUS_OK(write_queue_status);
784   UniqueDvrWriteBufferQueue write_queue = write_queue_status.take();
785   ASSERT_NE(nullptr, write_queue.get());
786 
787   const int queue_id = dvrWriteBufferQueueGetId(write_queue.get());
788   ASSERT_GE(queue_id, 0);
789 
790   // Update surface state.
791   ASSERT_STATUS_OK(manager_->WaitForUpdate());
792   ASSERT_STATUS_EQ(1u, manager_->GetSurfaceCount());
793 
794   // Verify the buffers changed flag is set.
795   auto check_flags = [](const auto& value) {
796     return value & DVR_SURFACE_UPDATE_FLAGS_BUFFERS_CHANGED;
797   };
798   EXPECT_STATUS_PRED(check_flags, manager_->GetUpdateFlags(0));
799 
800   auto queue_ids_status = manager_->GetQueueIds(0);
801   ASSERT_STATUS_OK(queue_ids_status);
802 
803   auto queue_ids = queue_ids_status.take();
804   ASSERT_EQ(1u, queue_ids.size());
805   EXPECT_EQ(queue_id, queue_ids[0]);
806 
807   auto read_queue_status = manager_->GetReadBufferQueue(surface_id, queue_id);
808   ASSERT_STATUS_OK(read_queue_status);
809   UniqueDvrReadBufferQueue read_queue = read_queue_status.take();
810   ASSERT_NE(nullptr, read_queue.get());
811   EXPECT_EQ(queue_id, dvrReadBufferQueueGetId(read_queue.get()));
812 
813   write_queue.reset();
814 
815   // Verify that destroying the queue generates a surface update event.
816   ASSERT_STATUS_OK(manager_->WaitForUpdate());
817   ASSERT_STATUS_EQ(1u, manager_->GetSurfaceCount());
818 
819   // Verify that the buffers changed flag is set.
820   EXPECT_STATUS_PRED(check_flags, manager_->GetUpdateFlags(0));
821 
822   // Verify that the queue ids reflect the change.
823   queue_ids_status = manager_->GetQueueIds(0);
824   ASSERT_STATUS_OK(queue_ids_status);
825 
826   queue_ids = queue_ids_status.take();
827   ASSERT_EQ(0u, queue_ids.size());
828 }
829 
TEST_F(DvrDisplayManagerTest,MultiLayerBufferQueue)830 TEST_F(DvrDisplayManagerTest, MultiLayerBufferQueue) {
831   // Create an application surface.
832   auto surface_status = CreateApplicationSurface();
833   ASSERT_STATUS_OK(surface_status);
834   UniqueDvrSurface surface = surface_status.take();
835   ASSERT_NE(nullptr, surface.get());
836 
837   // Get surface state and verify there is one surface.
838   ASSERT_STATUS_OK(manager_->WaitForUpdate());
839   ASSERT_STATUS_EQ(1u, manager_->GetSurfaceCount());
840 
841   // Create a new queue in the surface.
842   const uint32_t kLayerCount = 3;
843   auto write_queue_status = CreateSurfaceQueue(
844       surface, 320, 240, AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM, kLayerCount,
845       AHARDWAREBUFFER_USAGE_CPU_READ_RARELY, 1, 0);
846   ASSERT_STATUS_OK(write_queue_status);
847   UniqueDvrWriteBufferQueue write_queue = write_queue_status.take();
848   ASSERT_NE(nullptr, write_queue.get());
849 
850   DvrWriteBuffer* buffer = nullptr;
851   DvrNativeBufferMetadata metadata;
852   int fence_fd = -1;
853   int error = dvrWriteBufferQueueGainBuffer(write_queue.get(), /*timeout=*/1000,
854                                             &buffer, &metadata, &fence_fd);
855   ASSERT_EQ(0, error);
856 
857   AHardwareBuffer* hardware_buffer = nullptr;
858   error = dvrWriteBufferGetAHardwareBuffer(buffer, &hardware_buffer);
859   ASSERT_EQ(0, error);
860 
861   AHardwareBuffer_Desc desc = {};
862   AHardwareBuffer_describe(hardware_buffer, &desc);
863   ASSERT_EQ(kLayerCount, desc.layers);
864 
865   AHardwareBuffer_release(hardware_buffer);
866   dvrWriteBufferDestroy(buffer);
867 }
868 
TEST_F(Test,ConfigurationData)869 TEST_F(Test, ConfigurationData) {
870   // TODO(hendrikw): Move this test and GetConfigData helper function out of the
871   // display manager tests.
872   auto data1 = GetConfigData(-1);
873   ASSERT_STATUS_ERROR(data1);
874 
875   const char kDvrLensMetricsProperty[] = "ro.dvr.lens_metrics";
876 
877   // This should be run on devices with and without built in metrics.
878   bool has_metric = !base::GetProperty(kDvrLensMetricsProperty, "").empty();
879   auto data2 = GetConfigData(DVR_CONFIGURATION_DATA_LENS_METRICS);
880   if (has_metric) {
881     ASSERT_STATUS_OK(data2);
882     ASSERT_NE(0u, data2.get().size());
883   } else {
884     ASSERT_STATUS_ERROR(data2);
885   }
886 }
887 
888 }  // namespace
889 
890 }  // namespace dvr
891 }  // namespace android
892