blob: fa6ff301af41e00dd1e5716b9fbd017cfeff29fd [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(_, _))
Ady Abraham62f216c2020-10-13 19:07:23 -070068 .WillOnce(Return(new EventThreadConnection(eventThread.get(), /*callingUid=*/0,
69 ResyncCallback())));
Valerie Haud251afb2019-03-29 14:19:02 -070070
71 EXPECT_CALL(*sfEventThread, registerDisplayEventConnection(_));
72 EXPECT_CALL(*sfEventThread, createEventConnection(_, _))
Ady Abraham62f216c2020-10-13 19:07:23 -070073 .WillOnce(Return(new EventThreadConnection(sfEventThread.get(), /*callingUid=*/0,
74 ResyncCallback())));
Valerie Haud251afb2019-03-29 14:19:02 -070075
Ady Abraham8cb21882020-08-26 18:22:05 -070076 EXPECT_CALL(*mVSyncTracker, nextAnticipatedVSyncTimeFrom(_)).WillRepeatedly(Return(0));
77 EXPECT_CALL(*mVSyncTracker, currentPeriod())
Valerie Haud251afb2019-03-29 14:19:02 -070078 .WillRepeatedly(Return(FakeHwcDisplayInjector::DEFAULT_REFRESH_RATE));
79
Ady Abraham8cb21882020-08-26 18:22:05 -070080 mFlinger.setupScheduler(std::unique_ptr<mock::VsyncController>(mVsyncController),
81 std::unique_ptr<mock::VSyncTracker>(mVSyncTracker),
Valerie Haud251afb2019-03-29 14:19:02 -070082 std::move(eventThread), std::move(sfEventThread));
83 }
84
85 TestableScheduler* mScheduler;
86 TestableSurfaceFlinger mFlinger;
87
88 std::unique_ptr<mock::EventThread> mEventThread = std::make_unique<mock::EventThread>();
Valerie Haud251afb2019-03-29 14:19:02 -070089
90 mock::MessageQueue* mMessageQueue = new mock::MessageQueue();
Ady Abraham8cb21882020-08-26 18:22:05 -070091 mock::VsyncController* mVsyncController = new mock::VsyncController();
92 mock::VSyncTracker* mVSyncTracker = new mock::VSyncTracker();
Valerie Haud251afb2019-03-29 14:19:02 -070093
94 struct TransactionInfo {
95 Vector<ComposerState> states;
96 Vector<DisplayState> displays;
97 uint32_t flags = 0;
98 sp<IBinder> applyToken = IInterface::asBinder(TransactionCompletedListener::getIInstance());
99 InputWindowCommands inputWindowCommands;
Ady Abrahamf0c56492020-12-17 18:04:15 -0800100 int64_t desiredPresentTime = 0;
101 bool isAutoTimestamp = true;
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700102 int64_t frameTimelineVsyncId = ISurfaceComposer::INVALID_VSYNC_ID;
Valerie Haud251afb2019-03-29 14:19:02 -0700103 client_cache_t uncacheBuffer;
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000104 int64_t id = -1;
Valerie Haud251afb2019-03-29 14:19:02 -0700105 };
106
107 void checkEqual(TransactionInfo info, SurfaceFlinger::TransactionState state) {
108 EXPECT_EQ(0, info.states.size());
109 EXPECT_EQ(0, state.states.size());
110
111 EXPECT_EQ(0, info.displays.size());
112 EXPECT_EQ(0, state.displays.size());
113 EXPECT_EQ(info.flags, state.flags);
114 EXPECT_EQ(info.desiredPresentTime, state.desiredPresentTime);
115 }
116
117 void setupSingle(TransactionInfo& transaction, uint32_t flags, bool syncInputWindows,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800118 int64_t desiredPresentTime, bool isAutoTimestamp,
119 int64_t frameTimelineVsyncId) {
Valerie Haud251afb2019-03-29 14:19:02 -0700120 mTransactionNumber++;
121 transaction.flags |= flags; // ISurfaceComposer::eSynchronous;
122 transaction.inputWindowCommands.syncInputWindows = syncInputWindows;
123 transaction.desiredPresentTime = desiredPresentTime;
Ady Abrahamf0c56492020-12-17 18:04:15 -0800124 transaction.isAutoTimestamp = isAutoTimestamp;
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700125 transaction.frameTimelineVsyncId = frameTimelineVsyncId;
Valerie Haud251afb2019-03-29 14:19:02 -0700126 }
127
128 void NotPlacedOnTransactionQueue(uint32_t flags, bool syncInputWindows) {
Arthur Hung1bedccb2020-09-24 10:09:25 +0000129 ASSERT_EQ(0, mFlinger.getTransactionQueue().size());
Valerie Haud251afb2019-03-29 14:19:02 -0700130 // called in SurfaceFlinger::signalTransaction
131 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
Ady Abraham8cb21882020-08-26 18:22:05 -0700132 EXPECT_CALL(*mVSyncTracker, nextAnticipatedVSyncTimeFrom(_)).WillOnce(Return(systemTime()));
Valerie Haud251afb2019-03-29 14:19:02 -0700133 TransactionInfo transaction;
134 setupSingle(transaction, flags, syncInputWindows,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800135 /*desiredPresentTime*/ systemTime(), /*isAutoTimestamp*/ true,
136 ISurfaceComposer::INVALID_VSYNC_ID);
Valerie Haud251afb2019-03-29 14:19:02 -0700137 nsecs_t applicationTime = systemTime();
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700138 mFlinger.setTransactionState(transaction.frameTimelineVsyncId, transaction.states,
139 transaction.displays, transaction.flags,
Valerie Haud251afb2019-03-29 14:19:02 -0700140 transaction.applyToken, transaction.inputWindowCommands,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800141 transaction.desiredPresentTime, transaction.isAutoTimestamp,
142 transaction.uncacheBuffer, mHasListenerCallbacks, mCallbacks,
143 transaction.id);
Valerie Haud251afb2019-03-29 14:19:02 -0700144
145 // This transaction should not have been placed on the transaction queue.
146 // If transaction is synchronous or syncs input windows, SF
147 // applyTransactionState should time out (5s) wating for SF to commit
148 // the transaction or to receive a signal that syncInputWindows has
149 // completed. If this is animation, it should not time out waiting.
150 nsecs_t returnedTime = systemTime();
151 if (flags & ISurfaceComposer::eSynchronous || syncInputWindows) {
152 EXPECT_GE(returnedTime, applicationTime + s2ns(5));
153 } else {
154 EXPECT_LE(returnedTime, applicationTime + s2ns(5));
155 }
Arthur Hung1bedccb2020-09-24 10:09:25 +0000156 auto transactionQueue = mFlinger.getTransactionQueue();
Valerie Haud251afb2019-03-29 14:19:02 -0700157 EXPECT_EQ(0, transactionQueue.size());
158 }
159
160 void PlaceOnTransactionQueue(uint32_t flags, bool syncInputWindows) {
Arthur Hung1bedccb2020-09-24 10:09:25 +0000161 ASSERT_EQ(0, mFlinger.getTransactionQueue().size());
Valerie Haud251afb2019-03-29 14:19:02 -0700162 // called in SurfaceFlinger::signalTransaction
163 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
164
165 // first check will see desired present time has not passed,
166 // but afterwards it will look like the desired present time has passed
167 nsecs_t time = systemTime();
Ady Abraham8cb21882020-08-26 18:22:05 -0700168 EXPECT_CALL(*mVSyncTracker, nextAnticipatedVSyncTimeFrom(_))
Valerie Haud251afb2019-03-29 14:19:02 -0700169 .WillOnce(Return(time + nsecs_t(5 * 1e8)));
170 TransactionInfo transaction;
171 setupSingle(transaction, flags, syncInputWindows,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800172 /*desiredPresentTime*/ time + s2ns(1), false,
173 ISurfaceComposer::INVALID_VSYNC_ID);
Valerie Haud251afb2019-03-29 14:19:02 -0700174 nsecs_t applicationSentTime = systemTime();
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700175 mFlinger.setTransactionState(transaction.frameTimelineVsyncId, transaction.states,
176 transaction.displays, transaction.flags,
Valerie Haud251afb2019-03-29 14:19:02 -0700177 transaction.applyToken, transaction.inputWindowCommands,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800178 transaction.desiredPresentTime, transaction.isAutoTimestamp,
179 transaction.uncacheBuffer, mHasListenerCallbacks, mCallbacks,
180 transaction.id);
Valerie Haud251afb2019-03-29 14:19:02 -0700181
182 nsecs_t returnedTime = systemTime();
183 EXPECT_LE(returnedTime, applicationSentTime + s2ns(5));
184 // This transaction should have been placed on the transaction queue
Arthur Hung1bedccb2020-09-24 10:09:25 +0000185 auto transactionQueue = mFlinger.getTransactionQueue();
Valerie Haud251afb2019-03-29 14:19:02 -0700186 EXPECT_EQ(1, transactionQueue.size());
187 }
188
189 void BlockedByPriorTransaction(uint32_t flags, bool syncInputWindows) {
Arthur Hung1bedccb2020-09-24 10:09:25 +0000190 ASSERT_EQ(0, mFlinger.getTransactionQueue().size());
Valerie Haud251afb2019-03-29 14:19:02 -0700191 // called in SurfaceFlinger::signalTransaction
192 nsecs_t time = systemTime();
193 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
Ady Abraham8cb21882020-08-26 18:22:05 -0700194 EXPECT_CALL(*mVSyncTracker, nextAnticipatedVSyncTimeFrom(_))
Valerie Haud251afb2019-03-29 14:19:02 -0700195 .WillOnce(Return(time + nsecs_t(5 * 1e8)));
196 // transaction that should go on the pending thread
197 TransactionInfo transactionA;
198 setupSingle(transactionA, /*flags*/ 0, /*syncInputWindows*/ false,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800199 /*desiredPresentTime*/ time + s2ns(1), false,
200 ISurfaceComposer::INVALID_VSYNC_ID);
Valerie Haud251afb2019-03-29 14:19:02 -0700201
202 // transaction that would not have gone on the pending thread if not
203 // blocked
204 TransactionInfo transactionB;
205 setupSingle(transactionB, flags, syncInputWindows,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800206 /*desiredPresentTime*/ systemTime(), /*isAutoTimestamp*/ true,
207 ISurfaceComposer::INVALID_VSYNC_ID);
Valerie Haud251afb2019-03-29 14:19:02 -0700208
209 nsecs_t applicationSentTime = systemTime();
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700210 mFlinger.setTransactionState(transactionA.frameTimelineVsyncId, transactionA.states,
211 transactionA.displays, transactionA.flags,
Valerie Haud251afb2019-03-29 14:19:02 -0700212 transactionA.applyToken, transactionA.inputWindowCommands,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800213 transactionA.desiredPresentTime, transactionA.isAutoTimestamp,
214 transactionA.uncacheBuffer, mHasListenerCallbacks, mCallbacks,
215 transactionA.id);
Valerie Haud251afb2019-03-29 14:19:02 -0700216
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
222 applicationSentTime = systemTime();
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700223 mFlinger.setTransactionState(transactionB.frameTimelineVsyncId, transactionB.states,
224 transactionB.displays, transactionB.flags,
Valerie Haud251afb2019-03-29 14:19:02 -0700225 transactionB.applyToken, transactionB.inputWindowCommands,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800226 transactionB.desiredPresentTime, transactionB.isAutoTimestamp,
227 transactionB.uncacheBuffer, mHasListenerCallbacks, mCallbacks,
228 transactionB.id);
Valerie Haud251afb2019-03-29 14:19:02 -0700229
230 // this thread should have been blocked by the above transaction
231 // if this is an animation, this thread should be blocked for 5s
232 // in setTransactionState waiting for transactionA to flush. Otherwise,
233 // the transaction should be placed on the pending queue
234 if (flags & ISurfaceComposer::eAnimation) {
235 EXPECT_GE(systemTime(), applicationSentTime + s2ns(5));
236 } else {
237 EXPECT_LE(systemTime(), applicationSentTime + s2ns(5));
238 }
239
240 // check that there is one binder on the pending queue.
Arthur Hung1bedccb2020-09-24 10:09:25 +0000241 auto transactionQueue = mFlinger.getTransactionQueue();
Valerie Haud251afb2019-03-29 14:19:02 -0700242 EXPECT_EQ(1, transactionQueue.size());
243
244 auto& [applyToken, transactionStates] = *(transactionQueue.begin());
245 EXPECT_EQ(2, transactionStates.size());
246
247 auto& transactionStateA = transactionStates.front();
248 transactionStates.pop();
249 checkEqual(transactionA, transactionStateA);
250 auto& transactionStateB = transactionStates.front();
251 checkEqual(transactionB, transactionStateB);
252 }
253
254 bool mHasListenerCallbacks = false;
255 std::vector<ListenerCallbacks> mCallbacks;
256 int mTransactionNumber = 0;
257};
258
259TEST_F(TransactionApplicationTest, Flush_RemovesFromQueue) {
Arthur Hung1bedccb2020-09-24 10:09:25 +0000260 ASSERT_EQ(0, mFlinger.getTransactionQueue().size());
Valerie Haud251afb2019-03-29 14:19:02 -0700261 // called in SurfaceFlinger::signalTransaction
262 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
263
264 // nsecs_t time = systemTime();
Ady Abraham8cb21882020-08-26 18:22:05 -0700265 EXPECT_CALL(*mVSyncTracker, nextAnticipatedVSyncTimeFrom(_))
Valerie Haud251afb2019-03-29 14:19:02 -0700266 .WillOnce(Return(nsecs_t(5 * 1e8)))
267 .WillOnce(Return(s2ns(2)));
268 TransactionInfo transactionA; // transaction to go on pending queue
269 setupSingle(transactionA, /*flags*/ 0, /*syncInputWindows*/ false,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800270 /*desiredPresentTime*/ s2ns(1), false, ISurfaceComposer::INVALID_VSYNC_ID);
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700271 mFlinger.setTransactionState(transactionA.frameTimelineVsyncId, transactionA.states,
272 transactionA.displays, transactionA.flags, transactionA.applyToken,
273 transactionA.inputWindowCommands, transactionA.desiredPresentTime,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800274 transactionA.isAutoTimestamp, transactionA.uncacheBuffer,
275 mHasListenerCallbacks, mCallbacks, transactionA.id);
Valerie Haud251afb2019-03-29 14:19:02 -0700276
Arthur Hung1bedccb2020-09-24 10:09:25 +0000277 auto& transactionQueue = mFlinger.getTransactionQueue();
Valerie Haud251afb2019-03-29 14:19:02 -0700278 ASSERT_EQ(1, transactionQueue.size());
279
280 auto& [applyToken, transactionStates] = *(transactionQueue.begin());
281 ASSERT_EQ(1, transactionStates.size());
282
283 auto& transactionState = transactionStates.front();
284 checkEqual(transactionA, transactionState);
285
286 // because flushing uses the cached expected present time, we send an empty
287 // transaction here (sending a null applyToken to fake it as from a
288 // different process) to re-query and reset the cached expected present time
289 TransactionInfo empty;
290 empty.applyToken = sp<IBinder>();
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700291 mFlinger.setTransactionState(empty.frameTimelineVsyncId, empty.states, empty.displays,
292 empty.flags, empty.applyToken, empty.inputWindowCommands,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800293 empty.desiredPresentTime, empty.isAutoTimestamp,
294 empty.uncacheBuffer, mHasListenerCallbacks, mCallbacks, empty.id);
Valerie Haud251afb2019-03-29 14:19:02 -0700295
Arthur Hung1bedccb2020-09-24 10:09:25 +0000296 // flush transaction queue should flush as desiredPresentTime has
Valerie Haud251afb2019-03-29 14:19:02 -0700297 // passed
Arthur Hung1bedccb2020-09-24 10:09:25 +0000298 mFlinger.flushTransactionQueues();
Valerie Haud251afb2019-03-29 14:19:02 -0700299
300 EXPECT_EQ(0, transactionQueue.size());
301}
302
303TEST_F(TransactionApplicationTest, NotPlacedOnTransactionQueue_Synchronous) {
304 NotPlacedOnTransactionQueue(ISurfaceComposer::eSynchronous, /*syncInputWindows*/ false);
305}
306
307TEST_F(TransactionApplicationTest, NotPlacedOnTransactionQueue_Animation) {
308 NotPlacedOnTransactionQueue(ISurfaceComposer::eAnimation, /*syncInputWindows*/ false);
309}
310
311TEST_F(TransactionApplicationTest, NotPlacedOnTransactionQueue_SyncInputWindows) {
312 NotPlacedOnTransactionQueue(/*flags*/ 0, /*syncInputWindows*/ true);
313}
314
315TEST_F(TransactionApplicationTest, PlaceOnTransactionQueue_Synchronous) {
316 PlaceOnTransactionQueue(ISurfaceComposer::eSynchronous, /*syncInputWindows*/ false);
317}
318
319TEST_F(TransactionApplicationTest, PlaceOnTransactionQueue_Animation) {
320 PlaceOnTransactionQueue(ISurfaceComposer::eAnimation, /*syncInputWindows*/ false);
321}
322
323TEST_F(TransactionApplicationTest, PlaceOnTransactionQueue_SyncInputWindows) {
324 PlaceOnTransactionQueue(/*flags*/ 0, /*syncInputWindows*/ true);
325}
326
327TEST_F(TransactionApplicationTest, BlockWithPriorTransaction_Synchronous) {
328 BlockedByPriorTransaction(ISurfaceComposer::eSynchronous, /*syncInputWindows*/ false);
329}
330
331TEST_F(TransactionApplicationTest, BlockWithPriorTransaction_Animation) {
332 BlockedByPriorTransaction(ISurfaceComposer::eSynchronous, /*syncInputWindows*/ false);
333}
334
335TEST_F(TransactionApplicationTest, BlockWithPriorTransaction_SyncInputWindows) {
336 BlockedByPriorTransaction(/*flags*/ 0, /*syncInputWindows*/ true);
337}
338
Valerie Hau09e60052019-12-15 14:51:15 -0800339TEST_F(TransactionApplicationTest, FromHandle) {
340 sp<IBinder> badHandle;
341 auto ret = mFlinger.fromHandle(badHandle);
Alec Mouri9a02eda2020-04-21 17:39:34 -0700342 EXPECT_EQ(nullptr, ret.promote().get());
Valerie Hau09e60052019-12-15 14:51:15 -0800343}
Valerie Haud251afb2019-03-29 14:19:02 -0700344} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800345
346// TODO(b/129481165): remove the #pragma below and fix conversion issues
347#pragma clang diagnostic pop // ignored "-Wconversion"