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