xref: /aosp_15_r20/external/angle/src/libANGLE/Fence_unittest.cpp (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1 //
2 // Copyright 2014 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 
7 #include "gmock/gmock.h"
8 #include "gtest/gtest.h"
9 
10 #include "libANGLE/Fence.h"
11 #include "libANGLE/renderer/FenceNVImpl.h"
12 #include "libANGLE/renderer/SyncImpl.h"
13 #include "tests/angle_unittests_utils.h"
14 
15 using namespace testing;
16 
17 namespace
18 {
19 
20 //
21 // FenceNV tests
22 //
23 
24 class MockFenceNVImpl : public rx::FenceNVImpl
25 {
26   public:
~MockFenceNVImpl()27     virtual ~MockFenceNVImpl() { destroy(); }
28 
29     MOCK_METHOD1(onDestroy, void(const gl::Context *context));
30     MOCK_METHOD2(set, angle::Result(const gl::Context *, GLenum));
31     MOCK_METHOD2(test, angle::Result(const gl::Context *, GLboolean *));
32     MOCK_METHOD1(finish, angle::Result(const gl::Context *));
33 
34     MOCK_METHOD0(destroy, void());
35 };
36 
37 class FenceNVTest : public Test
38 {
39   protected:
SetUp()40     void SetUp() override
41     {
42         mImpl = new MockFenceNVImpl;
43         EXPECT_CALL(factory, createFenceNV()).WillOnce(Return(mImpl));
44 
45         EXPECT_CALL(*mImpl, destroy());
46         mFence = new gl::FenceNV(&factory);
47     }
48 
TearDown()49     void TearDown() override { delete mFence; }
50 
51     NiceMock<rx::MockGLFactory> factory;
52     MockFenceNVImpl *mImpl;
53     gl::FenceNV *mFence;
54 };
55 
TEST_F(FenceNVTest,SetAndTestBehavior)56 TEST_F(FenceNVTest, SetAndTestBehavior)
57 {
58     EXPECT_CALL(*mImpl, set(_, _)).WillOnce(Return(angle::Result::Continue)).RetiresOnSaturation();
59     EXPECT_FALSE(mFence->isSet());
60     EXPECT_EQ(angle::Result::Continue, mFence->set(nullptr, GL_ALL_COMPLETED_NV));
61     EXPECT_TRUE(mFence->isSet());
62     // Fake the behavior of testing the fence before and after it's passed.
63     EXPECT_CALL(*mImpl, test(_, _))
64         .WillOnce(DoAll(SetArgumentPointee<1>(static_cast<GLboolean>(GL_FALSE)),
65                         Return(angle::Result::Continue)))
66         .WillOnce(DoAll(SetArgumentPointee<1>(static_cast<GLboolean>(GL_TRUE)),
67                         Return(angle::Result::Continue)))
68         .RetiresOnSaturation();
69     GLboolean out;
70     EXPECT_EQ(angle::Result::Continue, mFence->test(nullptr, &out));
71     EXPECT_EQ(GL_FALSE, out);
72     EXPECT_EQ(angle::Result::Continue, mFence->test(nullptr, &out));
73     EXPECT_EQ(GL_TRUE, out);
74 }
75 
76 //
77 // Sync tests
78 //
79 
80 class MockSyncImpl : public rx::SyncImpl
81 {
82   public:
~MockSyncImpl()83     virtual ~MockSyncImpl() { destroy(); }
84 
85     MOCK_METHOD3(set, angle::Result(const gl::Context *, GLenum, GLbitfield));
86     MOCK_METHOD4(clientWait, angle::Result(const gl::Context *, GLbitfield, GLuint64, GLenum *));
87     MOCK_METHOD3(serverWait, angle::Result(const gl::Context *, GLbitfield, GLuint64));
88     MOCK_METHOD2(getStatus, angle::Result(const gl::Context *, GLint *));
89 
90     MOCK_METHOD0(destroy, void());
91 };
92 
93 class FenceSyncTest : public Test
94 {
95   protected:
SetUp()96     void SetUp() override
97     {
98         mImpl = new MockSyncImpl;
99         EXPECT_CALL(factory, createSync()).WillOnce(Return(mImpl));
100 
101         mFence = new gl::Sync(&factory, {1});
102         EXPECT_CALL(*mImpl, destroy());
103         mFence->addRef();
104     }
105 
TearDown()106     void TearDown() override { mFence->release(nullptr); }
107 
108     NiceMock<rx::MockGLFactory> factory;
109     MockSyncImpl *mImpl;
110     gl::Sync *mFence;
111 };
112 
TEST_F(FenceSyncTest,SetAndGetStatusBehavior)113 TEST_F(FenceSyncTest, SetAndGetStatusBehavior)
114 {
115     EXPECT_CALL(*mImpl, set(_, _, _))
116         .WillOnce(Return(angle::Result::Continue))
117         .RetiresOnSaturation();
118     EXPECT_EQ(angle::Result::Continue, mFence->set(nullptr, GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
119     EXPECT_EQ(static_cast<GLenum>(GL_SYNC_GPU_COMMANDS_COMPLETE), mFence->getCondition());
120     // Fake the behavior of testing the fence before and after it's passed.
121     EXPECT_CALL(*mImpl, getStatus(_, _))
122         .WillOnce(DoAll(SetArgumentPointee<1>(GL_UNSIGNALED), Return(angle::Result::Continue)))
123         .WillOnce(DoAll(SetArgumentPointee<1>(GL_SIGNALED), Return(angle::Result::Continue)))
124         .RetiresOnSaturation();
125     GLint out;
126     EXPECT_EQ(angle::Result::Continue, mFence->getStatus(nullptr, &out));
127     EXPECT_EQ(GL_UNSIGNALED, out);
128     EXPECT_EQ(angle::Result::Continue, mFence->getStatus(nullptr, &out));
129     EXPECT_EQ(GL_SIGNALED, out);
130 }
131 
132 }  // namespace
133