blob: 28415bcb12e25eb40168614f7f84f0d980933434 [file] [log] [blame]
Valerie Haud251afb2019-03-29 14:19:02 -07001/*
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
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
Valerie Haud251afb2019-03-29 14:19:02 -070021#undef LOG_TAG
22#define LOG_TAG "CompositionTest"
23
24#include <compositionengine/Display.h>
25#include <compositionengine/mock/DisplaySurface.h>
26#include <gmock/gmock.h>
27#include <gtest/gtest.h>
28#include <gui/SurfaceComposerClient.h>
29#include <log/log.h>
30#include <utils/String8.h>
31
32#include "TestableScheduler.h"
33#include "TestableSurfaceFlinger.h"
Valerie Haud251afb2019-03-29 14:19:02 -070034#include "mock/MockEventThread.h"
35#include "mock/MockMessageQueue.h"
Ady Abraham8cb21882020-08-26 18:22:05 -070036#include "mock/MockVsyncController.h"
Valerie Haud251afb2019-03-29 14:19:02 -070037
38namespace android {
39
40using testing::_;
41using testing::Return;
42
43using FakeHwcDisplayInjector = TestableSurfaceFlinger::FakeHwcDisplayInjector;
44
45class TransactionApplicationTest : public testing::Test {
46public:
47 TransactionApplicationTest() {
48 const ::testing::TestInfo* const test_info =
49 ::testing::UnitTest::GetInstance()->current_test_info();
50 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
51
52 mFlinger.mutableEventQueue().reset(mMessageQueue);
53 setupScheduler();
54 }
55
56 ~TransactionApplicationTest() {
57 const ::testing::TestInfo* const test_info =
58 ::testing::UnitTest::GetInstance()->current_test_info();
59 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
60 }
61
62 void setupScheduler() {
63 auto eventThread = std::make_unique<mock::EventThread>();
64 auto sfEventThread = std::make_unique<mock::EventThread>();
65
66 EXPECT_CALL(*eventThread, registerDisplayEventConnection(_));
67 EXPECT_CALL(*eventThread, createEventConnection(_, _))
68 .WillOnce(Return(
69 new EventThreadConnection(eventThread.get(), ResyncCallback(),
70 ISurfaceComposer::eConfigChangedSuppress)));
71
72 EXPECT_CALL(*sfEventThread, registerDisplayEventConnection(_));
73 EXPECT_CALL(*sfEventThread, createEventConnection(_, _))
74 .WillOnce(Return(
75 new EventThreadConnection(sfEventThread.get(), ResyncCallback(),
76 ISurfaceComposer::eConfigChangedSuppress)));
77
Ady Abraham8cb21882020-08-26 18:22:05 -070078 EXPECT_CALL(*mVSyncTracker, nextAnticipatedVSyncTimeFrom(_)).WillRepeatedly(Return(0));
79 EXPECT_CALL(*mVSyncTracker, currentPeriod())
Valerie Haud251afb2019-03-29 14:19:02 -070080 .WillRepeatedly(Return(FakeHwcDisplayInjector::DEFAULT_REFRESH_RATE));
81
Ady Abraham8cb21882020-08-26 18:22:05 -070082 mFlinger.setupScheduler(std::unique_ptr<mock::VsyncController>(mVsyncController),
83 std::unique_ptr<mock::VSyncTracker>(mVSyncTracker),
Valerie Haud251afb2019-03-29 14:19:02 -070084 std::move(eventThread), std::move(sfEventThread));
85 }
86
87 TestableScheduler* mScheduler;
88 TestableSurfaceFlinger mFlinger;
89
90 std::unique_ptr<mock::EventThread> mEventThread = std::make_unique<mock::EventThread>();
Valerie Haud251afb2019-03-29 14:19:02 -070091
92 mock::MessageQueue* mMessageQueue = new mock::MessageQueue();
Ady Abraham8cb21882020-08-26 18:22:05 -070093 mock::VsyncController* mVsyncController = new mock::VsyncController();
94 mock::VSyncTracker* mVSyncTracker = new mock::VSyncTracker();
Valerie Haud251afb2019-03-29 14:19:02 -070095
96 struct TransactionInfo {
97 Vector<ComposerState> states;
98 Vector<DisplayState> displays;
99 uint32_t flags = 0;
100 sp<IBinder> applyToken = IInterface::asBinder(TransactionCompletedListener::getIInstance());
101 InputWindowCommands inputWindowCommands;
102 int64_t desiredPresentTime = -1;
103 client_cache_t uncacheBuffer;
104 };
105
106 void checkEqual(TransactionInfo info, SurfaceFlinger::TransactionState state) {
107 EXPECT_EQ(0, info.states.size());
108 EXPECT_EQ(0, state.states.size());
109
110 EXPECT_EQ(0, info.displays.size());
111 EXPECT_EQ(0, state.displays.size());
112 EXPECT_EQ(info.flags, state.flags);
113 EXPECT_EQ(info.desiredPresentTime, state.desiredPresentTime);
114 }
115
116 void setupSingle(TransactionInfo& transaction, uint32_t flags, bool syncInputWindows,
117 int64_t desiredPresentTime) {
118 mTransactionNumber++;
119 transaction.flags |= flags; // ISurfaceComposer::eSynchronous;
120 transaction.inputWindowCommands.syncInputWindows = syncInputWindows;
121 transaction.desiredPresentTime = desiredPresentTime;
122 }
123
124 void NotPlacedOnTransactionQueue(uint32_t flags, bool syncInputWindows) {
Arthur Hung1bedccb2020-09-24 10:09:25 +0000125 ASSERT_EQ(0, mFlinger.getTransactionQueue().size());
Valerie Haud251afb2019-03-29 14:19:02 -0700126 // called in SurfaceFlinger::signalTransaction
127 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
Ady Abraham8cb21882020-08-26 18:22:05 -0700128 EXPECT_CALL(*mVSyncTracker, nextAnticipatedVSyncTimeFrom(_)).WillOnce(Return(systemTime()));
Valerie Haud251afb2019-03-29 14:19:02 -0700129 TransactionInfo transaction;
130 setupSingle(transaction, flags, syncInputWindows,
131 /*desiredPresentTime*/ -1);
132 nsecs_t applicationTime = systemTime();
133 mFlinger.setTransactionState(transaction.states, transaction.displays, transaction.flags,
134 transaction.applyToken, transaction.inputWindowCommands,
135 transaction.desiredPresentTime, transaction.uncacheBuffer,
136 mHasListenerCallbacks, mCallbacks);
137
138 // This transaction should not have been placed on the transaction queue.
139 // If transaction is synchronous or syncs input windows, SF
140 // applyTransactionState should time out (5s) wating for SF to commit
141 // the transaction or to receive a signal that syncInputWindows has
142 // completed. If this is animation, it should not time out waiting.
143 nsecs_t returnedTime = systemTime();
144 if (flags & ISurfaceComposer::eSynchronous || syncInputWindows) {
145 EXPECT_GE(returnedTime, applicationTime + s2ns(5));
146 } else {
147 EXPECT_LE(returnedTime, applicationTime + s2ns(5));
148 }
Arthur Hung1bedccb2020-09-24 10:09:25 +0000149 auto transactionQueue = mFlinger.getTransactionQueue();
Valerie Haud251afb2019-03-29 14:19:02 -0700150 EXPECT_EQ(0, transactionQueue.size());
151 }
152
153 void PlaceOnTransactionQueue(uint32_t flags, bool syncInputWindows) {
Arthur Hung1bedccb2020-09-24 10:09:25 +0000154 ASSERT_EQ(0, mFlinger.getTransactionQueue().size());
Valerie Haud251afb2019-03-29 14:19:02 -0700155 // called in SurfaceFlinger::signalTransaction
156 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
157
158 // first check will see desired present time has not passed,
159 // but afterwards it will look like the desired present time has passed
160 nsecs_t time = systemTime();
Ady Abraham8cb21882020-08-26 18:22:05 -0700161 EXPECT_CALL(*mVSyncTracker, nextAnticipatedVSyncTimeFrom(_))
Valerie Haud251afb2019-03-29 14:19:02 -0700162 .WillOnce(Return(time + nsecs_t(5 * 1e8)));
163 TransactionInfo transaction;
164 setupSingle(transaction, flags, syncInputWindows,
165 /*desiredPresentTime*/ time + s2ns(1));
166 nsecs_t applicationSentTime = systemTime();
167 mFlinger.setTransactionState(transaction.states, transaction.displays, transaction.flags,
168 transaction.applyToken, transaction.inputWindowCommands,
169 transaction.desiredPresentTime, transaction.uncacheBuffer,
170 mHasListenerCallbacks, mCallbacks);
171
172 nsecs_t returnedTime = systemTime();
173 EXPECT_LE(returnedTime, applicationSentTime + s2ns(5));
174 // This transaction should have been placed on the transaction queue
Arthur Hung1bedccb2020-09-24 10:09:25 +0000175 auto transactionQueue = mFlinger.getTransactionQueue();
Valerie Haud251afb2019-03-29 14:19:02 -0700176 EXPECT_EQ(1, transactionQueue.size());
177 }
178
179 void BlockedByPriorTransaction(uint32_t flags, bool syncInputWindows) {
Arthur Hung1bedccb2020-09-24 10:09:25 +0000180 ASSERT_EQ(0, mFlinger.getTransactionQueue().size());
Valerie Haud251afb2019-03-29 14:19:02 -0700181 // called in SurfaceFlinger::signalTransaction
182 nsecs_t time = systemTime();
183 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
Ady Abraham8cb21882020-08-26 18:22:05 -0700184 EXPECT_CALL(*mVSyncTracker, nextAnticipatedVSyncTimeFrom(_))
Valerie Haud251afb2019-03-29 14:19:02 -0700185 .WillOnce(Return(time + nsecs_t(5 * 1e8)));
186 // transaction that should go on the pending thread
187 TransactionInfo transactionA;
188 setupSingle(transactionA, /*flags*/ 0, /*syncInputWindows*/ false,
189 /*desiredPresentTime*/ time + s2ns(1));
190
191 // transaction that would not have gone on the pending thread if not
192 // blocked
193 TransactionInfo transactionB;
194 setupSingle(transactionB, flags, syncInputWindows,
195 /*desiredPresentTime*/ -1);
196
197 nsecs_t applicationSentTime = systemTime();
198 mFlinger.setTransactionState(transactionA.states, transactionA.displays, transactionA.flags,
199 transactionA.applyToken, transactionA.inputWindowCommands,
200 transactionA.desiredPresentTime, transactionA.uncacheBuffer,
201 mHasListenerCallbacks, mCallbacks);
202
203 // This thread should not have been blocked by the above transaction
204 // (5s is the timeout period that applyTransactionState waits for SF to
205 // commit the transaction)
206 EXPECT_LE(systemTime(), applicationSentTime + s2ns(5));
207
208 applicationSentTime = systemTime();
209 mFlinger.setTransactionState(transactionB.states, transactionB.displays, transactionB.flags,
210 transactionB.applyToken, transactionB.inputWindowCommands,
211 transactionB.desiredPresentTime, transactionB.uncacheBuffer,
212 mHasListenerCallbacks, mCallbacks);
213
214 // this thread should have been blocked by the above transaction
215 // if this is an animation, this thread should be blocked for 5s
216 // in setTransactionState waiting for transactionA to flush. Otherwise,
217 // the transaction should be placed on the pending queue
218 if (flags & ISurfaceComposer::eAnimation) {
219 EXPECT_GE(systemTime(), applicationSentTime + s2ns(5));
220 } else {
221 EXPECT_LE(systemTime(), applicationSentTime + s2ns(5));
222 }
223
224 // check that there is one binder on the pending queue.
Arthur Hung1bedccb2020-09-24 10:09:25 +0000225 auto transactionQueue = mFlinger.getTransactionQueue();
Valerie Haud251afb2019-03-29 14:19:02 -0700226 EXPECT_EQ(1, transactionQueue.size());
227
228 auto& [applyToken, transactionStates] = *(transactionQueue.begin());
229 EXPECT_EQ(2, transactionStates.size());
230
231 auto& transactionStateA = transactionStates.front();
232 transactionStates.pop();
233 checkEqual(transactionA, transactionStateA);
234 auto& transactionStateB = transactionStates.front();
235 checkEqual(transactionB, transactionStateB);
236 }
237
238 bool mHasListenerCallbacks = false;
239 std::vector<ListenerCallbacks> mCallbacks;
240 int mTransactionNumber = 0;
241};
242
243TEST_F(TransactionApplicationTest, Flush_RemovesFromQueue) {
Arthur Hung1bedccb2020-09-24 10:09:25 +0000244 ASSERT_EQ(0, mFlinger.getTransactionQueue().size());
Valerie Haud251afb2019-03-29 14:19:02 -0700245 // called in SurfaceFlinger::signalTransaction
246 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
247
248 // nsecs_t time = systemTime();
Ady Abraham8cb21882020-08-26 18:22:05 -0700249 EXPECT_CALL(*mVSyncTracker, nextAnticipatedVSyncTimeFrom(_))
Valerie Haud251afb2019-03-29 14:19:02 -0700250 .WillOnce(Return(nsecs_t(5 * 1e8)))
251 .WillOnce(Return(s2ns(2)));
252 TransactionInfo transactionA; // transaction to go on pending queue
253 setupSingle(transactionA, /*flags*/ 0, /*syncInputWindows*/ false,
254 /*desiredPresentTime*/ s2ns(1));
255 mFlinger.setTransactionState(transactionA.states, transactionA.displays, transactionA.flags,
256 transactionA.applyToken, transactionA.inputWindowCommands,
257 transactionA.desiredPresentTime, transactionA.uncacheBuffer,
258 mHasListenerCallbacks, mCallbacks);
259
Arthur Hung1bedccb2020-09-24 10:09:25 +0000260 auto& transactionQueue = mFlinger.getTransactionQueue();
Valerie Haud251afb2019-03-29 14:19:02 -0700261 ASSERT_EQ(1, transactionQueue.size());
262
263 auto& [applyToken, transactionStates] = *(transactionQueue.begin());
264 ASSERT_EQ(1, transactionStates.size());
265
266 auto& transactionState = transactionStates.front();
267 checkEqual(transactionA, transactionState);
268
269 // because flushing uses the cached expected present time, we send an empty
270 // transaction here (sending a null applyToken to fake it as from a
271 // different process) to re-query and reset the cached expected present time
272 TransactionInfo empty;
273 empty.applyToken = sp<IBinder>();
274 mFlinger.setTransactionState(empty.states, empty.displays, empty.flags, empty.applyToken,
275 empty.inputWindowCommands, empty.desiredPresentTime,
276 empty.uncacheBuffer, mHasListenerCallbacks, mCallbacks);
277
Arthur Hung1bedccb2020-09-24 10:09:25 +0000278 // flush transaction queue should flush as desiredPresentTime has
Valerie Haud251afb2019-03-29 14:19:02 -0700279 // passed
Arthur Hung1bedccb2020-09-24 10:09:25 +0000280 mFlinger.flushTransactionQueues();
Valerie Haud251afb2019-03-29 14:19:02 -0700281
282 EXPECT_EQ(0, transactionQueue.size());
283}
284
285TEST_F(TransactionApplicationTest, NotPlacedOnTransactionQueue_Synchronous) {
286 NotPlacedOnTransactionQueue(ISurfaceComposer::eSynchronous, /*syncInputWindows*/ false);
287}
288
289TEST_F(TransactionApplicationTest, NotPlacedOnTransactionQueue_Animation) {
290 NotPlacedOnTransactionQueue(ISurfaceComposer::eAnimation, /*syncInputWindows*/ false);
291}
292
293TEST_F(TransactionApplicationTest, NotPlacedOnTransactionQueue_SyncInputWindows) {
294 NotPlacedOnTransactionQueue(/*flags*/ 0, /*syncInputWindows*/ true);
295}
296
297TEST_F(TransactionApplicationTest, PlaceOnTransactionQueue_Synchronous) {
298 PlaceOnTransactionQueue(ISurfaceComposer::eSynchronous, /*syncInputWindows*/ false);
299}
300
301TEST_F(TransactionApplicationTest, PlaceOnTransactionQueue_Animation) {
302 PlaceOnTransactionQueue(ISurfaceComposer::eAnimation, /*syncInputWindows*/ false);
303}
304
305TEST_F(TransactionApplicationTest, PlaceOnTransactionQueue_SyncInputWindows) {
306 PlaceOnTransactionQueue(/*flags*/ 0, /*syncInputWindows*/ true);
307}
308
309TEST_F(TransactionApplicationTest, BlockWithPriorTransaction_Synchronous) {
310 BlockedByPriorTransaction(ISurfaceComposer::eSynchronous, /*syncInputWindows*/ false);
311}
312
313TEST_F(TransactionApplicationTest, BlockWithPriorTransaction_Animation) {
314 BlockedByPriorTransaction(ISurfaceComposer::eSynchronous, /*syncInputWindows*/ false);
315}
316
317TEST_F(TransactionApplicationTest, BlockWithPriorTransaction_SyncInputWindows) {
318 BlockedByPriorTransaction(/*flags*/ 0, /*syncInputWindows*/ true);
319}
320
Valerie Hau09e60052019-12-15 14:51:15 -0800321TEST_F(TransactionApplicationTest, FromHandle) {
322 sp<IBinder> badHandle;
323 auto ret = mFlinger.fromHandle(badHandle);
Alec Mouri9a02eda2020-04-21 17:39:34 -0700324 EXPECT_EQ(nullptr, ret.promote().get());
Valerie Hau09e60052019-12-15 14:51:15 -0800325}
Valerie Haud251afb2019-03-29 14:19:02 -0700326} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800327
328// TODO(b/129481165): remove the #pragma below and fix conversion issues
329#pragma clang diagnostic pop // ignored "-Wconversion"