1 /*
2 * Copyright 2019 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
18 #undef LOG_TAG
19 #define LOG_TAG "CompositionTest"
20
21 #include <compositionengine/Display.h>
22 #include <compositionengine/mock/DisplaySurface.h>
23 #include <gmock/gmock.h>
24 #include <gtest/gtest.h>
25 #include <gui/SurfaceComposerClient.h>
26 #include <log/log.h>
27 #include <utils/String8.h>
28
29 #include "TestableScheduler.h"
30 #include "TestableSurfaceFlinger.h"
31 #include "mock/MockEventThread.h"
32 #include "mock/MockMessageQueue.h"
33 #include "mock/MockVsyncController.h"
34
35 namespace android {
36
37 using testing::_;
38 using testing::Return;
39
40 using FakeHwcDisplayInjector = TestableSurfaceFlinger::FakeHwcDisplayInjector;
41
42 class TransactionApplicationTest : public testing::Test {
43 public:
TransactionApplicationTest()44 TransactionApplicationTest() {
45 const ::testing::TestInfo* const test_info =
46 ::testing::UnitTest::GetInstance()->current_test_info();
47 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
48
49 mFlinger.mutableEventQueue().reset(mMessageQueue);
50 setupScheduler();
51 }
52
~TransactionApplicationTest()53 ~TransactionApplicationTest() {
54 const ::testing::TestInfo* const test_info =
55 ::testing::UnitTest::GetInstance()->current_test_info();
56 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
57 }
58
setupScheduler()59 void setupScheduler() {
60 auto eventThread = std::make_unique<mock::EventThread>();
61 auto sfEventThread = std::make_unique<mock::EventThread>();
62
63 EXPECT_CALL(*eventThread, registerDisplayEventConnection(_));
64 EXPECT_CALL(*eventThread, createEventConnection(_, _))
65 .WillOnce(Return(new EventThreadConnection(eventThread.get(), /*callingUid=*/0,
66 ResyncCallback())));
67
68 EXPECT_CALL(*sfEventThread, registerDisplayEventConnection(_));
69 EXPECT_CALL(*sfEventThread, createEventConnection(_, _))
70 .WillOnce(Return(new EventThreadConnection(sfEventThread.get(), /*callingUid=*/0,
71 ResyncCallback())));
72
73 EXPECT_CALL(*mVSyncTracker, nextAnticipatedVSyncTimeFrom(_)).WillRepeatedly(Return(0));
74 EXPECT_CALL(*mVSyncTracker, currentPeriod())
75 .WillRepeatedly(Return(FakeHwcDisplayInjector::DEFAULT_VSYNC_PERIOD));
76
77 mFlinger.setupComposer(std::make_unique<Hwc2::mock::Composer>());
78 mFlinger.setupScheduler(std::unique_ptr<mock::VsyncController>(mVsyncController),
79 std::unique_ptr<mock::VSyncTracker>(mVSyncTracker),
80 std::move(eventThread), std::move(sfEventThread));
81 }
82
83 TestableScheduler* mScheduler;
84 TestableSurfaceFlinger mFlinger;
85
86 std::unique_ptr<mock::EventThread> mEventThread = std::make_unique<mock::EventThread>();
87
88 mock::MessageQueue* mMessageQueue = new mock::MessageQueue();
89 mock::VsyncController* mVsyncController = new mock::VsyncController();
90 mock::VSyncTracker* mVSyncTracker = new mock::VSyncTracker();
91
92 struct TransactionInfo {
93 Vector<ComposerState> states;
94 Vector<DisplayState> displays;
95 uint32_t flags = 0;
96 sp<IBinder> applyToken = IInterface::asBinder(TransactionCompletedListener::getIInstance());
97 InputWindowCommands inputWindowCommands;
98 int64_t desiredPresentTime = 0;
99 bool isAutoTimestamp = true;
100 FrameTimelineInfo frameTimelineInfo;
101 client_cache_t uncacheBuffer;
102 uint64_t id = static_cast<uint64_t>(-1);
103 static_assert(0xffffffffffffffff == static_cast<uint64_t>(-1));
104 };
105
checkEqual(TransactionInfo info,SurfaceFlinger::TransactionState state)106 void checkEqual(TransactionInfo info, SurfaceFlinger::TransactionState state) {
107 EXPECT_EQ(0u, info.states.size());
108 EXPECT_EQ(0u, state.states.size());
109
110 EXPECT_EQ(0u, info.displays.size());
111 EXPECT_EQ(0u, state.displays.size());
112 EXPECT_EQ(info.flags, state.flags);
113 EXPECT_EQ(info.desiredPresentTime, state.desiredPresentTime);
114 }
115
setupSingle(TransactionInfo & transaction,uint32_t flags,bool syncInputWindows,int64_t desiredPresentTime,bool isAutoTimestamp,const FrameTimelineInfo & frameTimelineInfo)116 void setupSingle(TransactionInfo& transaction, uint32_t flags, bool syncInputWindows,
117 int64_t desiredPresentTime, bool isAutoTimestamp,
118 const FrameTimelineInfo& frameTimelineInfo) {
119 mTransactionNumber++;
120 transaction.flags |= flags; // ISurfaceComposer::eSynchronous;
121 transaction.inputWindowCommands.syncInputWindows = syncInputWindows;
122 transaction.desiredPresentTime = desiredPresentTime;
123 transaction.isAutoTimestamp = isAutoTimestamp;
124 transaction.frameTimelineInfo = frameTimelineInfo;
125 }
126
NotPlacedOnTransactionQueue(uint32_t flags,bool syncInputWindows)127 void NotPlacedOnTransactionQueue(uint32_t flags, bool syncInputWindows) {
128 ASSERT_EQ(0u, mFlinger.getTransactionQueue().size());
129 // called in SurfaceFlinger::signalTransaction
130 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
131 TransactionInfo transaction;
132 setupSingle(transaction, flags, syncInputWindows,
133 /*desiredPresentTime*/ systemTime(), /*isAutoTimestamp*/ true,
134 FrameTimelineInfo{});
135 nsecs_t applicationTime = systemTime();
136 mFlinger.setTransactionState(transaction.frameTimelineInfo, transaction.states,
137 transaction.displays, transaction.flags,
138 transaction.applyToken, transaction.inputWindowCommands,
139 transaction.desiredPresentTime, transaction.isAutoTimestamp,
140 transaction.uncacheBuffer, mHasListenerCallbacks, mCallbacks,
141 transaction.id);
142
143 // If transaction is synchronous or syncs input windows, SF
144 // applyTransactionState should time out (5s) wating for SF to commit
145 // the transaction or to receive a signal that syncInputWindows has
146 // completed. If this is animation, it should not time out waiting.
147 nsecs_t returnedTime = systemTime();
148 if (flags & ISurfaceComposer::eSynchronous || syncInputWindows) {
149 EXPECT_GE(returnedTime, applicationTime + s2ns(5));
150 } else {
151 EXPECT_LE(returnedTime, applicationTime + s2ns(5));
152 }
153 // Each transaction should have been placed on the transaction queue
154 auto transactionQueue = mFlinger.getTransactionQueue();
155 EXPECT_EQ(1u, transactionQueue.size());
156 }
157
PlaceOnTransactionQueue(uint32_t flags,bool syncInputWindows)158 void PlaceOnTransactionQueue(uint32_t flags, bool syncInputWindows) {
159 ASSERT_EQ(0u, mFlinger.getTransactionQueue().size());
160 // called in SurfaceFlinger::signalTransaction
161 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
162
163 // first check will see desired present time has not passed,
164 // but afterwards it will look like the desired present time has passed
165 nsecs_t time = systemTime();
166 TransactionInfo transaction;
167 setupSingle(transaction, flags, syncInputWindows,
168 /*desiredPresentTime*/ time + s2ns(1), false, FrameTimelineInfo{});
169 nsecs_t applicationSentTime = systemTime();
170 mFlinger.setTransactionState(transaction.frameTimelineInfo, transaction.states,
171 transaction.displays, transaction.flags,
172 transaction.applyToken, transaction.inputWindowCommands,
173 transaction.desiredPresentTime, transaction.isAutoTimestamp,
174 transaction.uncacheBuffer, mHasListenerCallbacks, mCallbacks,
175 transaction.id);
176
177 nsecs_t returnedTime = systemTime();
178 if ((flags & ISurfaceComposer::eSynchronous) || syncInputWindows) {
179 EXPECT_GE(systemTime(), applicationSentTime + s2ns(5));
180 } else {
181 EXPECT_LE(returnedTime, applicationSentTime + s2ns(5));
182 }
183 // This transaction should have been placed on the transaction queue
184 auto transactionQueue = mFlinger.getTransactionQueue();
185 EXPECT_EQ(1u, transactionQueue.size());
186 }
187
BlockedByPriorTransaction(uint32_t flags,bool syncInputWindows)188 void BlockedByPriorTransaction(uint32_t flags, bool syncInputWindows) {
189 ASSERT_EQ(0u, mFlinger.getTransactionQueue().size());
190 // called in SurfaceFlinger::signalTransaction
191 nsecs_t time = systemTime();
192 if (!syncInputWindows) {
193 EXPECT_CALL(*mMessageQueue, invalidate()).Times(2);
194 } else {
195 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
196 }
197 // transaction that should go on the pending thread
198 TransactionInfo transactionA;
199 setupSingle(transactionA, /*flags*/ 0, /*syncInputWindows*/ false,
200 /*desiredPresentTime*/ time + s2ns(1), false, FrameTimelineInfo{});
201
202 // transaction that would not have gone on the pending thread if not
203 // blocked
204 TransactionInfo transactionB;
205 setupSingle(transactionB, flags, syncInputWindows,
206 /*desiredPresentTime*/ systemTime(), /*isAutoTimestamp*/ true,
207 FrameTimelineInfo{});
208
209 nsecs_t applicationSentTime = systemTime();
210 mFlinger.setTransactionState(transactionA.frameTimelineInfo, transactionA.states,
211 transactionA.displays, transactionA.flags,
212 transactionA.applyToken, transactionA.inputWindowCommands,
213 transactionA.desiredPresentTime, transactionA.isAutoTimestamp,
214 transactionA.uncacheBuffer, mHasListenerCallbacks, mCallbacks,
215 transactionA.id);
216
217 // This thread should not have been blocked by the above transaction
218 // (5s is the timeout period that applyTransactionState waits for SF to
219 // commit the transaction)
220 EXPECT_LE(systemTime(), applicationSentTime + s2ns(5));
221 // transaction that would goes to pending transaciton queue.
222 mFlinger.flushTransactionQueues();
223
224 applicationSentTime = systemTime();
225 mFlinger.setTransactionState(transactionB.frameTimelineInfo, transactionB.states,
226 transactionB.displays, transactionB.flags,
227 transactionB.applyToken, transactionB.inputWindowCommands,
228 transactionB.desiredPresentTime, transactionB.isAutoTimestamp,
229 transactionB.uncacheBuffer, mHasListenerCallbacks, mCallbacks,
230 transactionB.id);
231
232 // this thread should have been blocked by the above transaction
233 // if this is an animation, this thread should be blocked for 5s
234 // in setTransactionState waiting for transactionA to flush. Otherwise,
235 // the transaction should be placed on the pending queue
236 if (flags & (ISurfaceComposer::eAnimation | ISurfaceComposer::eSynchronous) ||
237 syncInputWindows) {
238 EXPECT_GE(systemTime(), applicationSentTime + s2ns(5));
239 } else {
240 EXPECT_LE(systemTime(), applicationSentTime + s2ns(5));
241 }
242
243 // transaction that would goes to pending transaciton queue.
244 mFlinger.flushTransactionQueues();
245
246 // check that the transaction was applied.
247 auto transactionQueue = mFlinger.getPendingTransactionQueue();
248 EXPECT_EQ(0u, transactionQueue.size());
249 }
250
251 bool mHasListenerCallbacks = false;
252 std::vector<ListenerCallbacks> mCallbacks;
253 int mTransactionNumber = 0;
254 };
255
TEST_F(TransactionApplicationTest,Flush_RemovesFromQueue)256 TEST_F(TransactionApplicationTest, Flush_RemovesFromQueue) {
257 ASSERT_EQ(0u, mFlinger.getTransactionQueue().size());
258 // called in SurfaceFlinger::signalTransaction
259 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
260
261 TransactionInfo transactionA; // transaction to go on pending queue
262 setupSingle(transactionA, /*flags*/ 0, /*syncInputWindows*/ false,
263 /*desiredPresentTime*/ s2ns(1), false, FrameTimelineInfo{});
264 mFlinger.setTransactionState(transactionA.frameTimelineInfo, transactionA.states,
265 transactionA.displays, transactionA.flags, transactionA.applyToken,
266 transactionA.inputWindowCommands, transactionA.desiredPresentTime,
267 transactionA.isAutoTimestamp, transactionA.uncacheBuffer,
268 mHasListenerCallbacks, mCallbacks, transactionA.id);
269
270 auto& transactionQueue = mFlinger.getTransactionQueue();
271 ASSERT_EQ(1u, transactionQueue.size());
272
273 auto& transactionState = transactionQueue.front();
274 checkEqual(transactionA, transactionState);
275
276 // because flushing uses the cached expected present time, we send an empty
277 // transaction here (sending a null applyToken to fake it as from a
278 // different process) to re-query and reset the cached expected present time
279 TransactionInfo empty;
280 empty.applyToken = sp<IBinder>();
281 mFlinger.setTransactionState(empty.frameTimelineInfo, empty.states, empty.displays, empty.flags,
282 empty.applyToken, empty.inputWindowCommands,
283 empty.desiredPresentTime, empty.isAutoTimestamp,
284 empty.uncacheBuffer, mHasListenerCallbacks, mCallbacks, empty.id);
285
286 // flush transaction queue should flush as desiredPresentTime has
287 // passed
288 mFlinger.flushTransactionQueues();
289
290 EXPECT_EQ(0u, transactionQueue.size());
291 }
292
TEST_F(TransactionApplicationTest,NotPlacedOnTransactionQueue_Synchronous)293 TEST_F(TransactionApplicationTest, NotPlacedOnTransactionQueue_Synchronous) {
294 NotPlacedOnTransactionQueue(ISurfaceComposer::eSynchronous, /*syncInputWindows*/ false);
295 }
296
TEST_F(TransactionApplicationTest,NotPlacedOnTransactionQueue_Animation)297 TEST_F(TransactionApplicationTest, NotPlacedOnTransactionQueue_Animation) {
298 NotPlacedOnTransactionQueue(ISurfaceComposer::eAnimation, /*syncInputWindows*/ false);
299 }
300
TEST_F(TransactionApplicationTest,NotPlacedOnTransactionQueue_SyncInputWindows)301 TEST_F(TransactionApplicationTest, NotPlacedOnTransactionQueue_SyncInputWindows) {
302 NotPlacedOnTransactionQueue(/*flags*/ 0, /*syncInputWindows*/ true);
303 }
304
TEST_F(TransactionApplicationTest,PlaceOnTransactionQueue_Synchronous)305 TEST_F(TransactionApplicationTest, PlaceOnTransactionQueue_Synchronous) {
306 PlaceOnTransactionQueue(ISurfaceComposer::eSynchronous, /*syncInputWindows*/ false);
307 }
308
TEST_F(TransactionApplicationTest,PlaceOnTransactionQueue_Animation)309 TEST_F(TransactionApplicationTest, PlaceOnTransactionQueue_Animation) {
310 PlaceOnTransactionQueue(ISurfaceComposer::eAnimation, /*syncInputWindows*/ false);
311 }
312
TEST_F(TransactionApplicationTest,PlaceOnTransactionQueue_SyncInputWindows)313 TEST_F(TransactionApplicationTest, PlaceOnTransactionQueue_SyncInputWindows) {
314 PlaceOnTransactionQueue(/*flags*/ 0, /*syncInputWindows*/ true);
315 }
316
TEST_F(TransactionApplicationTest,BlockWithPriorTransaction_Synchronous)317 TEST_F(TransactionApplicationTest, BlockWithPriorTransaction_Synchronous) {
318 BlockedByPriorTransaction(ISurfaceComposer::eSynchronous, /*syncInputWindows*/ false);
319 }
320
TEST_F(TransactionApplicationTest,BlockWithPriorTransaction_Animation)321 TEST_F(TransactionApplicationTest, BlockWithPriorTransaction_Animation) {
322 BlockedByPriorTransaction(ISurfaceComposer::eSynchronous, /*syncInputWindows*/ false);
323 }
324
TEST_F(TransactionApplicationTest,BlockWithPriorTransaction_SyncInputWindows)325 TEST_F(TransactionApplicationTest, BlockWithPriorTransaction_SyncInputWindows) {
326 BlockedByPriorTransaction(/*flags*/ 0, /*syncInputWindows*/ true);
327 }
328
TEST_F(TransactionApplicationTest,FromHandle)329 TEST_F(TransactionApplicationTest, FromHandle) {
330 sp<IBinder> badHandle;
331 auto ret = mFlinger.fromHandle(badHandle);
332 EXPECT_EQ(nullptr, ret.promote().get());
333 }
334 } // namespace android
335