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