1 /*
2 * Copyright 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #undef LOG_TAG
18 #define LOG_TAG "LibSurfaceFlingerUnittests"
19
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22
23 #include "FrameTimeline.h"
24 #include "Scheduler/MessageQueue.h"
25 #include "SurfaceFlinger.h"
26
27 namespace android {
28
29 using namespace std::chrono_literals;
30 using namespace testing;
31
32 using CallbackToken = scheduler::VSyncDispatch::CallbackToken;
33
34 struct NoOpCompositor final : ICompositor {
commitandroid::NoOpCompositor35 bool commit(nsecs_t, int64_t, nsecs_t) override { return false; }
compositeandroid::NoOpCompositor36 void composite(nsecs_t, int64_t) override {}
sampleandroid::NoOpCompositor37 void sample() override {}
38 } gNoOpCompositor;
39
40 class TestableMessageQueue : public impl::MessageQueue {
41 struct MockHandler : MessageQueue::Handler {
42 using MessageQueue::Handler::Handler;
43
44 MOCK_METHOD(void, dispatchFrame, (int64_t, nsecs_t), (override));
45 };
46
TestableMessageQueue(sp<MockHandler> handler)47 explicit TestableMessageQueue(sp<MockHandler> handler)
48 : impl::MessageQueue(gNoOpCompositor, handler), mHandler(std::move(handler)) {}
49
50 public:
TestableMessageQueue()51 TestableMessageQueue() : TestableMessageQueue(sp<MockHandler>::make(*this)) {}
52
53 using impl::MessageQueue::vsyncCallback;
54
55 const sp<MockHandler> mHandler;
56 };
57
58 struct MockVSyncDispatch : scheduler::VSyncDispatch {
59 MOCK_METHOD(CallbackToken, registerCallback, (Callback, std::string), (override));
60 MOCK_METHOD(void, unregisterCallback, (CallbackToken), (override));
61 MOCK_METHOD(scheduler::ScheduleResult, schedule, (CallbackToken, ScheduleTiming), (override));
62 MOCK_METHOD(scheduler::CancelResult, cancel, (CallbackToken token), (override));
63 MOCK_METHOD(void, dump, (std::string&), (const, override));
64 };
65
66 struct MockTokenManager : frametimeline::TokenManager {
67 MOCK_METHOD1(generateTokenForPredictions, int64_t(frametimeline::TimelineItem&& prediction));
68 MOCK_CONST_METHOD1(getPredictionsForToken, std::optional<frametimeline::TimelineItem>(int64_t));
69 };
70
71 struct MessageQueueTest : testing::Test {
SetUpandroid::MessageQueueTest72 void SetUp() override {
73 EXPECT_CALL(mVSyncDispatch, registerCallback(_, "sf")).WillOnce(Return(mCallbackToken));
74 EXPECT_NO_FATAL_FAILURE(mEventQueue.initVsync(mVSyncDispatch, mTokenManager, mDuration));
75 EXPECT_CALL(mVSyncDispatch, unregisterCallback(mCallbackToken)).Times(1);
76 }
77
78 MockVSyncDispatch mVSyncDispatch;
79 MockTokenManager mTokenManager;
80 TestableMessageQueue mEventQueue;
81
82 const CallbackToken mCallbackToken{5};
83 constexpr static auto mDuration = std::chrono::nanoseconds(100ms);
84 constexpr static auto mDifferentDuration = std::chrono::nanoseconds(250ms);
85 };
86
87 namespace {
88 /* ------------------------------------------------------------------------
89 * Test cases
90 */
TEST_F(MessageQueueTest,commit)91 TEST_F(MessageQueueTest, commit) {
92 const auto timing = scheduler::VSyncDispatch::ScheduleTiming{.workDuration = mDuration.count(),
93 .readyDuration = 0,
94 .earliestVsync = 0};
95 EXPECT_FALSE(mEventQueue.getScheduledFrameTime());
96
97 EXPECT_CALL(mVSyncDispatch, schedule(mCallbackToken, timing)).WillOnce(Return(1234));
98 EXPECT_NO_FATAL_FAILURE(mEventQueue.scheduleFrame());
99
100 ASSERT_TRUE(mEventQueue.getScheduledFrameTime());
101 EXPECT_EQ(1234, mEventQueue.getScheduledFrameTime()->time_since_epoch().count());
102 }
103
TEST_F(MessageQueueTest,commitTwice)104 TEST_F(MessageQueueTest, commitTwice) {
105 InSequence s;
106 const auto timing = scheduler::VSyncDispatch::ScheduleTiming{.workDuration = mDuration.count(),
107 .readyDuration = 0,
108 .earliestVsync = 0};
109
110 EXPECT_CALL(mVSyncDispatch, schedule(mCallbackToken, timing)).WillOnce(Return(1234));
111 EXPECT_NO_FATAL_FAILURE(mEventQueue.scheduleFrame());
112
113 ASSERT_TRUE(mEventQueue.getScheduledFrameTime());
114 EXPECT_EQ(1234, mEventQueue.getScheduledFrameTime()->time_since_epoch().count());
115
116 EXPECT_CALL(mVSyncDispatch, schedule(mCallbackToken, timing)).WillOnce(Return(4567));
117 EXPECT_NO_FATAL_FAILURE(mEventQueue.scheduleFrame());
118
119 ASSERT_TRUE(mEventQueue.getScheduledFrameTime());
120 EXPECT_EQ(4567, mEventQueue.getScheduledFrameTime()->time_since_epoch().count());
121 }
122
TEST_F(MessageQueueTest,commitTwiceWithCallback)123 TEST_F(MessageQueueTest, commitTwiceWithCallback) {
124 InSequence s;
125 const auto timing = scheduler::VSyncDispatch::ScheduleTiming{.workDuration = mDuration.count(),
126 .readyDuration = 0,
127 .earliestVsync = 0};
128
129 EXPECT_CALL(mVSyncDispatch, schedule(mCallbackToken, timing)).WillOnce(Return(1234));
130 EXPECT_NO_FATAL_FAILURE(mEventQueue.scheduleFrame());
131
132 ASSERT_TRUE(mEventQueue.getScheduledFrameTime());
133 EXPECT_EQ(1234, mEventQueue.getScheduledFrameTime()->time_since_epoch().count());
134
135 const auto startTime = 100;
136 const auto endTime = startTime + mDuration.count();
137 const auto presentTime = 500;
138 const auto vsyncId = 42;
139 EXPECT_CALL(mTokenManager,
140 generateTokenForPredictions(
141 frametimeline::TimelineItem(startTime, endTime, presentTime)))
142 .WillOnce(Return(vsyncId));
143 EXPECT_CALL(*mEventQueue.mHandler, dispatchFrame(vsyncId, presentTime)).Times(1);
144 EXPECT_NO_FATAL_FAILURE(mEventQueue.vsyncCallback(presentTime, startTime, endTime));
145
146 EXPECT_FALSE(mEventQueue.getScheduledFrameTime());
147
148 const auto timingAfterCallback =
149 scheduler::VSyncDispatch::ScheduleTiming{.workDuration = mDuration.count(),
150 .readyDuration = 0,
151 .earliestVsync = presentTime};
152
153 EXPECT_CALL(mVSyncDispatch, schedule(mCallbackToken, timingAfterCallback)).WillOnce(Return(0));
154 EXPECT_NO_FATAL_FAILURE(mEventQueue.scheduleFrame());
155 }
156
TEST_F(MessageQueueTest,commitWithDurationChange)157 TEST_F(MessageQueueTest, commitWithDurationChange) {
158 EXPECT_NO_FATAL_FAILURE(mEventQueue.setDuration(mDifferentDuration));
159
160 const auto timing =
161 scheduler::VSyncDispatch::ScheduleTiming{.workDuration = mDifferentDuration.count(),
162 .readyDuration = 0,
163 .earliestVsync = 0};
164
165 EXPECT_CALL(mVSyncDispatch, schedule(mCallbackToken, timing)).WillOnce(Return(0));
166 EXPECT_NO_FATAL_FAILURE(mEventQueue.scheduleFrame());
167 }
168
169 } // namespace
170 } // namespace android
171