blob: a4a44085f3f53e7e36fafadf92e8efbb89dd8e91 [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"
Marin Shalamanovbed7fd32020-12-21 20:02:20 +010020#pragma clang diagnostic ignored "-Wextra"
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080021
Valerie Haud251afb2019-03-29 14:19:02 -070022#undef LOG_TAG
23#define LOG_TAG "CompositionTest"
24
25#include <compositionengine/Display.h>
26#include <compositionengine/mock/DisplaySurface.h>
27#include <gmock/gmock.h>
28#include <gtest/gtest.h>
29#include <gui/SurfaceComposerClient.h>
30#include <log/log.h>
31#include <utils/String8.h>
32
33#include "TestableScheduler.h"
34#include "TestableSurfaceFlinger.h"
Valerie Haud251afb2019-03-29 14:19:02 -070035#include "mock/MockEventThread.h"
36#include "mock/MockMessageQueue.h"
Ady Abraham8cb21882020-08-26 18:22:05 -070037#include "mock/MockVsyncController.h"
Valerie Haud251afb2019-03-29 14:19:02 -070038
39namespace android {
40
41using testing::_;
42using testing::Return;
43
44using FakeHwcDisplayInjector = TestableSurfaceFlinger::FakeHwcDisplayInjector;
45
46class TransactionApplicationTest : public testing::Test {
47public:
48 TransactionApplicationTest() {
49 const ::testing::TestInfo* const test_info =
50 ::testing::UnitTest::GetInstance()->current_test_info();
51 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
52
53 mFlinger.mutableEventQueue().reset(mMessageQueue);
54 setupScheduler();
55 }
56
57 ~TransactionApplicationTest() {
58 const ::testing::TestInfo* const test_info =
59 ::testing::UnitTest::GetInstance()->current_test_info();
60 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
61 }
62
63 void setupScheduler() {
64 auto eventThread = std::make_unique<mock::EventThread>();
65 auto sfEventThread = std::make_unique<mock::EventThread>();
66
67 EXPECT_CALL(*eventThread, registerDisplayEventConnection(_));
68 EXPECT_CALL(*eventThread, createEventConnection(_, _))
Ady Abraham62f216c2020-10-13 19:07:23 -070069 .WillOnce(Return(new EventThreadConnection(eventThread.get(), /*callingUid=*/0,
70 ResyncCallback())));
Valerie Haud251afb2019-03-29 14:19:02 -070071
72 EXPECT_CALL(*sfEventThread, registerDisplayEventConnection(_));
73 EXPECT_CALL(*sfEventThread, createEventConnection(_, _))
Ady Abraham62f216c2020-10-13 19:07:23 -070074 .WillOnce(Return(new EventThreadConnection(sfEventThread.get(), /*callingUid=*/0,
75 ResyncCallback())));
Valerie Haud251afb2019-03-29 14:19:02 -070076
Ady Abraham8cb21882020-08-26 18:22:05 -070077 EXPECT_CALL(*mVSyncTracker, nextAnticipatedVSyncTimeFrom(_)).WillRepeatedly(Return(0));
78 EXPECT_CALL(*mVSyncTracker, currentPeriod())
Valerie Haud251afb2019-03-29 14:19:02 -070079 .WillRepeatedly(Return(FakeHwcDisplayInjector::DEFAULT_REFRESH_RATE));
80
Ady Abraham8cb21882020-08-26 18:22:05 -070081 mFlinger.setupScheduler(std::unique_ptr<mock::VsyncController>(mVsyncController),
82 std::unique_ptr<mock::VSyncTracker>(mVSyncTracker),
Valerie Haud251afb2019-03-29 14:19:02 -070083 std::move(eventThread), std::move(sfEventThread));
84 }
85
86 TestableScheduler* mScheduler;
87 TestableSurfaceFlinger mFlinger;
88
89 std::unique_ptr<mock::EventThread> mEventThread = std::make_unique<mock::EventThread>();
Valerie Haud251afb2019-03-29 14:19:02 -070090
91 mock::MessageQueue* mMessageQueue = new mock::MessageQueue();
Ady Abraham8cb21882020-08-26 18:22:05 -070092 mock::VsyncController* mVsyncController = new mock::VsyncController();
93 mock::VSyncTracker* mVSyncTracker = new mock::VSyncTracker();
Valerie Haud251afb2019-03-29 14:19:02 -070094
95 struct TransactionInfo {
96 Vector<ComposerState> states;
97 Vector<DisplayState> displays;
98 uint32_t flags = 0;
99 sp<IBinder> applyToken = IInterface::asBinder(TransactionCompletedListener::getIInstance());
100 InputWindowCommands inputWindowCommands;
Ady Abrahamf0c56492020-12-17 18:04:15 -0800101 int64_t desiredPresentTime = 0;
102 bool isAutoTimestamp = true;
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700103 int64_t frameTimelineVsyncId = ISurfaceComposer::INVALID_VSYNC_ID;
Valerie Haud251afb2019-03-29 14:19:02 -0700104 client_cache_t uncacheBuffer;
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000105 int64_t id = -1;
Valerie Haud251afb2019-03-29 14:19:02 -0700106 };
107
108 void checkEqual(TransactionInfo info, SurfaceFlinger::TransactionState state) {
109 EXPECT_EQ(0, info.states.size());
110 EXPECT_EQ(0, state.states.size());
111
112 EXPECT_EQ(0, info.displays.size());
113 EXPECT_EQ(0, state.displays.size());
114 EXPECT_EQ(info.flags, state.flags);
115 EXPECT_EQ(info.desiredPresentTime, state.desiredPresentTime);
116 }
117
118 void setupSingle(TransactionInfo& transaction, uint32_t flags, bool syncInputWindows,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800119 int64_t desiredPresentTime, bool isAutoTimestamp,
120 int64_t frameTimelineVsyncId) {
Valerie Haud251afb2019-03-29 14:19:02 -0700121 mTransactionNumber++;
122 transaction.flags |= flags; // ISurfaceComposer::eSynchronous;
123 transaction.inputWindowCommands.syncInputWindows = syncInputWindows;
124 transaction.desiredPresentTime = desiredPresentTime;
Ady Abrahamf0c56492020-12-17 18:04:15 -0800125 transaction.isAutoTimestamp = isAutoTimestamp;
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700126 transaction.frameTimelineVsyncId = frameTimelineVsyncId;
Valerie Haud251afb2019-03-29 14:19:02 -0700127 }
128
129 void NotPlacedOnTransactionQueue(uint32_t flags, bool syncInputWindows) {
arthurhungdca14282020-08-20 22:48:42 +0800130 ASSERT_EQ(0, mFlinger.getPendingTransactionQueue().size());
Valerie Haud251afb2019-03-29 14:19:02 -0700131 // called in SurfaceFlinger::signalTransaction
132 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
Ady Abraham8cb21882020-08-26 18:22:05 -0700133 EXPECT_CALL(*mVSyncTracker, nextAnticipatedVSyncTimeFrom(_)).WillOnce(Return(systemTime()));
Valerie Haud251afb2019-03-29 14:19:02 -0700134 TransactionInfo transaction;
135 setupSingle(transaction, flags, syncInputWindows,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800136 /*desiredPresentTime*/ systemTime(), /*isAutoTimestamp*/ true,
137 ISurfaceComposer::INVALID_VSYNC_ID);
Valerie Haud251afb2019-03-29 14:19:02 -0700138 nsecs_t applicationTime = systemTime();
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700139 mFlinger.setTransactionState(transaction.frameTimelineVsyncId, transaction.states,
140 transaction.displays, transaction.flags,
Valerie Haud251afb2019-03-29 14:19:02 -0700141 transaction.applyToken, transaction.inputWindowCommands,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800142 transaction.desiredPresentTime, transaction.isAutoTimestamp,
143 transaction.uncacheBuffer, mHasListenerCallbacks, mCallbacks,
144 transaction.id);
Valerie Haud251afb2019-03-29 14:19:02 -0700145
146 // This transaction should not have been placed on the transaction queue.
147 // If transaction is synchronous or syncs input windows, SF
148 // applyTransactionState should time out (5s) wating for SF to commit
149 // the transaction or to receive a signal that syncInputWindows has
150 // completed. If this is animation, it should not time out waiting.
151 nsecs_t returnedTime = systemTime();
152 if (flags & ISurfaceComposer::eSynchronous || syncInputWindows) {
153 EXPECT_GE(returnedTime, applicationTime + s2ns(5));
154 } else {
155 EXPECT_LE(returnedTime, applicationTime + s2ns(5));
156 }
arthurhungdca14282020-08-20 22:48:42 +0800157 auto transactionQueue = mFlinger.getPendingTransactionQueue();
Valerie Haud251afb2019-03-29 14:19:02 -0700158 EXPECT_EQ(0, transactionQueue.size());
159 }
160
161 void PlaceOnTransactionQueue(uint32_t flags, bool syncInputWindows) {
arthurhungdca14282020-08-20 22:48:42 +0800162 ASSERT_EQ(0, mFlinger.getPendingTransactionQueue().size());
Valerie Haud251afb2019-03-29 14:19:02 -0700163 // called in SurfaceFlinger::signalTransaction
164 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
165
166 // first check will see desired present time has not passed,
167 // but afterwards it will look like the desired present time has passed
168 nsecs_t time = systemTime();
Ady Abraham8cb21882020-08-26 18:22:05 -0700169 EXPECT_CALL(*mVSyncTracker, nextAnticipatedVSyncTimeFrom(_))
Valerie Haud251afb2019-03-29 14:19:02 -0700170 .WillOnce(Return(time + nsecs_t(5 * 1e8)));
171 TransactionInfo transaction;
172 setupSingle(transaction, flags, syncInputWindows,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800173 /*desiredPresentTime*/ time + s2ns(1), false,
174 ISurfaceComposer::INVALID_VSYNC_ID);
Valerie Haud251afb2019-03-29 14:19:02 -0700175 nsecs_t applicationSentTime = systemTime();
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700176 mFlinger.setTransactionState(transaction.frameTimelineVsyncId, transaction.states,
177 transaction.displays, transaction.flags,
Valerie Haud251afb2019-03-29 14:19:02 -0700178 transaction.applyToken, transaction.inputWindowCommands,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800179 transaction.desiredPresentTime, transaction.isAutoTimestamp,
180 transaction.uncacheBuffer, mHasListenerCallbacks, mCallbacks,
181 transaction.id);
Valerie Haud251afb2019-03-29 14:19:02 -0700182
183 nsecs_t returnedTime = systemTime();
184 EXPECT_LE(returnedTime, applicationSentTime + s2ns(5));
185 // This transaction should have been placed on the transaction queue
arthurhungdca14282020-08-20 22:48:42 +0800186 auto transactionQueue = mFlinger.getPendingTransactionQueue();
Valerie Haud251afb2019-03-29 14:19:02 -0700187 EXPECT_EQ(1, transactionQueue.size());
188 }
189
190 void BlockedByPriorTransaction(uint32_t flags, bool syncInputWindows) {
arthurhungdca14282020-08-20 22:48:42 +0800191 ASSERT_EQ(0, mFlinger.getPendingTransactionQueue().size());
Valerie Haud251afb2019-03-29 14:19:02 -0700192 // called in SurfaceFlinger::signalTransaction
193 nsecs_t time = systemTime();
194 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
Ady Abraham8cb21882020-08-26 18:22:05 -0700195 EXPECT_CALL(*mVSyncTracker, nextAnticipatedVSyncTimeFrom(_))
Valerie Haud251afb2019-03-29 14:19:02 -0700196 .WillOnce(Return(time + nsecs_t(5 * 1e8)));
197 // transaction that should go on the pending thread
198 TransactionInfo transactionA;
199 setupSingle(transactionA, /*flags*/ 0, /*syncInputWindows*/ false,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800200 /*desiredPresentTime*/ time + s2ns(1), false,
201 ISurfaceComposer::INVALID_VSYNC_ID);
Valerie Haud251afb2019-03-29 14:19:02 -0700202
203 // transaction that would not have gone on the pending thread if not
204 // blocked
205 TransactionInfo transactionB;
206 setupSingle(transactionB, flags, syncInputWindows,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800207 /*desiredPresentTime*/ systemTime(), /*isAutoTimestamp*/ true,
208 ISurfaceComposer::INVALID_VSYNC_ID);
Valerie Haud251afb2019-03-29 14:19:02 -0700209
210 nsecs_t applicationSentTime = systemTime();
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700211 mFlinger.setTransactionState(transactionA.frameTimelineVsyncId, transactionA.states,
212 transactionA.displays, transactionA.flags,
Valerie Haud251afb2019-03-29 14:19:02 -0700213 transactionA.applyToken, transactionA.inputWindowCommands,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800214 transactionA.desiredPresentTime, transactionA.isAutoTimestamp,
215 transactionA.uncacheBuffer, mHasListenerCallbacks, mCallbacks,
216 transactionA.id);
Valerie Haud251afb2019-03-29 14:19:02 -0700217
218 // This thread should not have been blocked by the above transaction
219 // (5s is the timeout period that applyTransactionState waits for SF to
220 // commit the transaction)
221 EXPECT_LE(systemTime(), applicationSentTime + s2ns(5));
222
223 applicationSentTime = systemTime();
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700224 mFlinger.setTransactionState(transactionB.frameTimelineVsyncId, transactionB.states,
225 transactionB.displays, transactionB.flags,
Valerie Haud251afb2019-03-29 14:19:02 -0700226 transactionB.applyToken, transactionB.inputWindowCommands,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800227 transactionB.desiredPresentTime, transactionB.isAutoTimestamp,
228 transactionB.uncacheBuffer, mHasListenerCallbacks, mCallbacks,
229 transactionB.id);
Valerie Haud251afb2019-03-29 14:19:02 -0700230
231 // this thread should have been blocked by the above transaction
232 // if this is an animation, this thread should be blocked for 5s
233 // in setTransactionState waiting for transactionA to flush. Otherwise,
234 // the transaction should be placed on the pending queue
235 if (flags & ISurfaceComposer::eAnimation) {
236 EXPECT_GE(systemTime(), applicationSentTime + s2ns(5));
237 } else {
238 EXPECT_LE(systemTime(), applicationSentTime + s2ns(5));
239 }
240
241 // check that there is one binder on the pending queue.
arthurhungdca14282020-08-20 22:48:42 +0800242 auto transactionQueue = mFlinger.getPendingTransactionQueue();
Valerie Haud251afb2019-03-29 14:19:02 -0700243 EXPECT_EQ(1, transactionQueue.size());
244
245 auto& [applyToken, transactionStates] = *(transactionQueue.begin());
246 EXPECT_EQ(2, transactionStates.size());
247
248 auto& transactionStateA = transactionStates.front();
249 transactionStates.pop();
250 checkEqual(transactionA, transactionStateA);
251 auto& transactionStateB = transactionStates.front();
252 checkEqual(transactionB, transactionStateB);
253 }
254
255 bool mHasListenerCallbacks = false;
256 std::vector<ListenerCallbacks> mCallbacks;
257 int mTransactionNumber = 0;
258};
259
260TEST_F(TransactionApplicationTest, Flush_RemovesFromQueue) {
arthurhungdca14282020-08-20 22:48:42 +0800261 ASSERT_EQ(0, mFlinger.getPendingTransactionQueue().size());
Valerie Haud251afb2019-03-29 14:19:02 -0700262 // called in SurfaceFlinger::signalTransaction
263 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
264
265 // nsecs_t time = systemTime();
Ady Abraham8cb21882020-08-26 18:22:05 -0700266 EXPECT_CALL(*mVSyncTracker, nextAnticipatedVSyncTimeFrom(_))
Valerie Haud251afb2019-03-29 14:19:02 -0700267 .WillOnce(Return(nsecs_t(5 * 1e8)))
268 .WillOnce(Return(s2ns(2)));
269 TransactionInfo transactionA; // transaction to go on pending queue
270 setupSingle(transactionA, /*flags*/ 0, /*syncInputWindows*/ false,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800271 /*desiredPresentTime*/ s2ns(1), false, ISurfaceComposer::INVALID_VSYNC_ID);
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700272 mFlinger.setTransactionState(transactionA.frameTimelineVsyncId, transactionA.states,
273 transactionA.displays, transactionA.flags, transactionA.applyToken,
274 transactionA.inputWindowCommands, transactionA.desiredPresentTime,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800275 transactionA.isAutoTimestamp, transactionA.uncacheBuffer,
276 mHasListenerCallbacks, mCallbacks, transactionA.id);
Valerie Haud251afb2019-03-29 14:19:02 -0700277
arthurhungdca14282020-08-20 22:48:42 +0800278 auto& transactionQueue = mFlinger.getPendingTransactionQueue();
Valerie Haud251afb2019-03-29 14:19:02 -0700279 ASSERT_EQ(1, transactionQueue.size());
280
281 auto& [applyToken, transactionStates] = *(transactionQueue.begin());
282 ASSERT_EQ(1, transactionStates.size());
283
284 auto& transactionState = transactionStates.front();
285 checkEqual(transactionA, transactionState);
286
287 // because flushing uses the cached expected present time, we send an empty
288 // transaction here (sending a null applyToken to fake it as from a
289 // different process) to re-query and reset the cached expected present time
290 TransactionInfo empty;
291 empty.applyToken = sp<IBinder>();
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700292 mFlinger.setTransactionState(empty.frameTimelineVsyncId, empty.states, empty.displays,
293 empty.flags, empty.applyToken, empty.inputWindowCommands,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800294 empty.desiredPresentTime, empty.isAutoTimestamp,
295 empty.uncacheBuffer, mHasListenerCallbacks, mCallbacks, empty.id);
Valerie Haud251afb2019-03-29 14:19:02 -0700296
arthurhungdca14282020-08-20 22:48:42 +0800297 // flush pending transaction queue should flush as desiredPresentTime has
Valerie Haud251afb2019-03-29 14:19:02 -0700298 // passed
arthurhungdca14282020-08-20 22:48:42 +0800299 mFlinger.flushPendingTransactionQueues();
Valerie Haud251afb2019-03-29 14:19:02 -0700300
301 EXPECT_EQ(0, transactionQueue.size());
302}
303
304TEST_F(TransactionApplicationTest, NotPlacedOnTransactionQueue_Synchronous) {
305 NotPlacedOnTransactionQueue(ISurfaceComposer::eSynchronous, /*syncInputWindows*/ false);
306}
307
308TEST_F(TransactionApplicationTest, NotPlacedOnTransactionQueue_Animation) {
309 NotPlacedOnTransactionQueue(ISurfaceComposer::eAnimation, /*syncInputWindows*/ false);
310}
311
312TEST_F(TransactionApplicationTest, NotPlacedOnTransactionQueue_SyncInputWindows) {
313 NotPlacedOnTransactionQueue(/*flags*/ 0, /*syncInputWindows*/ true);
314}
315
316TEST_F(TransactionApplicationTest, PlaceOnTransactionQueue_Synchronous) {
317 PlaceOnTransactionQueue(ISurfaceComposer::eSynchronous, /*syncInputWindows*/ false);
318}
319
320TEST_F(TransactionApplicationTest, PlaceOnTransactionQueue_Animation) {
321 PlaceOnTransactionQueue(ISurfaceComposer::eAnimation, /*syncInputWindows*/ false);
322}
323
324TEST_F(TransactionApplicationTest, PlaceOnTransactionQueue_SyncInputWindows) {
325 PlaceOnTransactionQueue(/*flags*/ 0, /*syncInputWindows*/ true);
326}
327
328TEST_F(TransactionApplicationTest, BlockWithPriorTransaction_Synchronous) {
329 BlockedByPriorTransaction(ISurfaceComposer::eSynchronous, /*syncInputWindows*/ false);
330}
331
332TEST_F(TransactionApplicationTest, BlockWithPriorTransaction_Animation) {
333 BlockedByPriorTransaction(ISurfaceComposer::eSynchronous, /*syncInputWindows*/ false);
334}
335
336TEST_F(TransactionApplicationTest, BlockWithPriorTransaction_SyncInputWindows) {
337 BlockedByPriorTransaction(/*flags*/ 0, /*syncInputWindows*/ true);
338}
339
Valerie Hau09e60052019-12-15 14:51:15 -0800340TEST_F(TransactionApplicationTest, FromHandle) {
341 sp<IBinder> badHandle;
342 auto ret = mFlinger.fromHandle(badHandle);
Alec Mouri9a02eda2020-04-21 17:39:34 -0700343 EXPECT_EQ(nullptr, ret.promote().get());
Valerie Hau09e60052019-12-15 14:51:15 -0800344}
Valerie Haud251afb2019-03-29 14:19:02 -0700345} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800346
347// TODO(b/129481165): remove the #pragma below and fix conversion issues
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100348#pragma clang diagnostic pop // ignored "-Wconversion -Wextra"