1#include <android/dvr/BnVrComposerCallback.h>
2#include <binder/IServiceManager.h>
3#include <gtest/gtest.h>
4#include <sys/eventfd.h>
5#include <vr_composer.h>
6
7namespace android {
8namespace dvr {
9namespace {
10
11const char kVrDisplayName[] = "VrDisplay_Test";
12
13class TestComposerView : public ComposerView {
14 public:
15  TestComposerView() {}
16  ~TestComposerView() override = default;
17
18  size_t display_refresh_count() const { return display_refresh_count_; }
19
20  void ForceDisplaysRefresh() override { display_refresh_count_++; }
21  void RegisterObserver(Observer* observer) override {}
22  void UnregisterObserver(Observer* observer) override {}
23
24  TestComposerView(const TestComposerView&) = delete;
25  void operator=(const TestComposerView&) = delete;
26
27 private:
28  size_t display_refresh_count_ = 0;
29};
30
31class TestComposerCallback : public BnVrComposerCallback {
32 public:
33  TestComposerCallback() {}
34  ~TestComposerCallback() override = default;
35
36  ComposerView::Frame last_frame() const { return last_frame_; }
37
38  binder::Status onNewFrame(
39      const ParcelableComposerFrame& frame,
40      ParcelableUniqueFd* /* fence */) override {
41    last_frame_ = frame.frame();
42    return binder::Status::ok();
43  }
44
45 private:
46  ComposerView::Frame last_frame_;
47
48  TestComposerCallback(const TestComposerCallback&) = delete;
49  void operator=(const TestComposerCallback&) = delete;
50};
51
52class TestComposerCallbackWithFence : public TestComposerCallback {
53 public:
54  ~TestComposerCallbackWithFence() override = default;
55
56  binder::Status onNewFrame(
57      const ParcelableComposerFrame& frame,
58      ParcelableUniqueFd* fence) override {
59    binder::Status status = TestComposerCallback::onNewFrame(frame, fence);
60
61    base::unique_fd fd(eventfd(0, 0));
62    EXPECT_LE(0, fd.get());
63    fence->set_fence(fd);
64
65    return status;
66  }
67};
68
69sp<GraphicBuffer> CreateBuffer() {
70  return new GraphicBuffer(600, 400, PIXEL_FORMAT_RGBA_8888,
71                           GraphicBuffer::USAGE_HW_TEXTURE);
72}
73
74}  // namespace
75
76class VrComposerTest : public testing::Test {
77 public:
78  VrComposerTest() : composer_(new VrComposer(&composer_view_)) {}
79  ~VrComposerTest() override = default;
80
81  sp<IVrComposer> GetComposerProxy() const {
82    sp<IServiceManager> sm(defaultServiceManager());
83    return interface_cast<IVrComposer>(sm->getService(String16(kVrDisplayName)));
84  }
85
86  void SetUp() override {
87    sp<IServiceManager> sm(defaultServiceManager());
88    EXPECT_EQ(OK,
89              sm->addService(String16(kVrDisplayName), composer_, false));
90  }
91
92 protected:
93  TestComposerView composer_view_;
94  sp<VrComposer> composer_;
95
96  VrComposerTest(const VrComposerTest&) = delete;
97  void operator=(const VrComposerTest&) = delete;
98};
99
100TEST_F(VrComposerTest, TestWithoutObserver) {
101  sp<IVrComposer> composer = GetComposerProxy();
102  ComposerView::Frame frame;
103
104  base::unique_fd fence = composer_->OnNewFrame(frame);
105  ASSERT_EQ(-1, fence.get());
106}
107
108TEST_F(VrComposerTest, TestWithObserver) {
109  sp<IVrComposer> composer = GetComposerProxy();
110  sp<TestComposerCallback> callback = new TestComposerCallback();
111  ASSERT_EQ(0, composer_view_.display_refresh_count());
112  ASSERT_TRUE(composer->registerObserver(callback).isOk());
113  ASSERT_EQ(1, composer_view_.display_refresh_count());
114
115  ComposerView::Frame frame;
116  base::unique_fd fence = composer_->OnNewFrame(frame);
117  ASSERT_EQ(-1, fence.get());
118}
119
120TEST_F(VrComposerTest, TestWithOneLayer) {
121  sp<IVrComposer> composer = GetComposerProxy();
122  sp<TestComposerCallback> callback = new TestComposerCallbackWithFence();
123  ASSERT_TRUE(composer->registerObserver(callback).isOk());
124
125  ComposerView::Frame frame;
126  frame.display_id = 1;
127  frame.removed = false;
128  frame.display_width = 600;
129  frame.display_height = 400;
130  frame.layers.push_back(ComposerView::ComposerLayer{
131    .id = 1,
132    .buffer = CreateBuffer(),
133    .fence = new Fence(eventfd(0, 0)),
134    .display_frame = {0, 0, 600, 400},
135    .crop = {0.0f, 0.0f, 600.0f, 400.0f},
136    .blend_mode = IComposerClient::BlendMode::NONE,
137    .alpha = 1.0f,
138    .type = 1,
139    .app_id = 1,
140  });
141  base::unique_fd fence = composer_->OnNewFrame(frame);
142  ASSERT_LE(0, fence.get());
143
144  ComposerView::Frame received_frame = callback->last_frame();
145  ASSERT_EQ(frame.display_id, received_frame.display_id);
146  ASSERT_EQ(frame.display_width, received_frame.display_width);
147  ASSERT_EQ(frame.display_height, received_frame.display_height);
148  ASSERT_EQ(frame.removed, received_frame.removed);
149  ASSERT_EQ(1u, received_frame.layers.size());
150  ASSERT_EQ(frame.layers[0].id, received_frame.layers[0].id);
151  ASSERT_NE(nullptr, received_frame.layers[0].buffer.get());
152  ASSERT_TRUE(received_frame.layers[0].fence->isValid());
153  ASSERT_EQ(frame.layers[0].display_frame.left,
154            received_frame.layers[0].display_frame.left);
155  ASSERT_EQ(frame.layers[0].display_frame.top,
156            received_frame.layers[0].display_frame.top);
157  ASSERT_EQ(frame.layers[0].display_frame.right,
158            received_frame.layers[0].display_frame.right);
159  ASSERT_EQ(frame.layers[0].display_frame.bottom,
160            received_frame.layers[0].display_frame.bottom);
161  ASSERT_EQ(frame.layers[0].crop.left, received_frame.layers[0].crop.left);
162  ASSERT_EQ(frame.layers[0].crop.top, received_frame.layers[0].crop.top);
163  ASSERT_EQ(frame.layers[0].crop.right, received_frame.layers[0].crop.right);
164  ASSERT_EQ(frame.layers[0].crop.bottom, received_frame.layers[0].crop.bottom);
165  ASSERT_EQ(frame.layers[0].blend_mode, received_frame.layers[0].blend_mode);
166  ASSERT_EQ(frame.layers[0].alpha, received_frame.layers[0].alpha);
167  ASSERT_EQ(frame.layers[0].type, received_frame.layers[0].type);
168  ASSERT_EQ(frame.layers[0].app_id, received_frame.layers[0].app_id);
169}
170
171}  // namespace dvr
172}  // namespace android
173