blob: 68cf330b9caaa9cf7d058c78535c3fdd499d684f [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(
Ady Abraham0bb6a472020-10-12 10:22:13 -070069 new EventThreadConnection(eventThread.get(), /*callingUid=*/0,
70 ResyncCallback(),
Valerie Haud251afb2019-03-29 14:19:02 -070071 ISurfaceComposer::eConfigChangedSuppress)));
72
73 EXPECT_CALL(*sfEventThread, registerDisplayEventConnection(_));
74 EXPECT_CALL(*sfEventThread, createEventConnection(_, _))
75 .WillOnce(Return(
Ady Abraham0bb6a472020-10-12 10:22:13 -070076 new EventThreadConnection(sfEventThread.get(), /*callingUid=*/0,
77 ResyncCallback(),
Valerie Haud251afb2019-03-29 14:19:02 -070078 ISurfaceComposer::eConfigChangedSuppress)));
79
Ady Abraham8cb21882020-08-26 18:22:05 -070080 EXPECT_CALL(*mVSyncTracker, nextAnticipatedVSyncTimeFrom(_)).WillRepeatedly(Return(0));
81 EXPECT_CALL(*mVSyncTracker, currentPeriod())
Valerie Haud251afb2019-03-29 14:19:02 -070082 .WillRepeatedly(Return(FakeHwcDisplayInjector::DEFAULT_REFRESH_RATE));
83
Ady Abraham8cb21882020-08-26 18:22:05 -070084 mFlinger.setupScheduler(std::unique_ptr<mock::VsyncController>(mVsyncController),
85 std::unique_ptr<mock::VSyncTracker>(mVSyncTracker),
Valerie Haud251afb2019-03-29 14:19:02 -070086 std::move(eventThread), std::move(sfEventThread));
87 }
88
89 TestableScheduler* mScheduler;
90 TestableSurfaceFlinger mFlinger;
91
92 std::unique_ptr<mock::EventThread> mEventThread = std::make_unique<mock::EventThread>();
Valerie Haud251afb2019-03-29 14:19:02 -070093
94 mock::MessageQueue* mMessageQueue = new mock::MessageQueue();
Ady Abraham8cb21882020-08-26 18:22:05 -070095 mock::VsyncController* mVsyncController = new mock::VsyncController();
96 mock::VSyncTracker* mVSyncTracker = new mock::VSyncTracker();
Valerie Haud251afb2019-03-29 14:19:02 -070097
98 struct TransactionInfo {
99 Vector<ComposerState> states;
100 Vector<DisplayState> displays;
101 uint32_t flags = 0;
102 sp<IBinder> applyToken = IInterface::asBinder(TransactionCompletedListener::getIInstance());
103 InputWindowCommands inputWindowCommands;
104 int64_t desiredPresentTime = -1;
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700105 int64_t frameTimelineVsyncId = ISurfaceComposer::INVALID_VSYNC_ID;
Valerie Haud251afb2019-03-29 14:19:02 -0700106 client_cache_t uncacheBuffer;
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000107 int64_t id = -1;
Valerie Haud251afb2019-03-29 14:19:02 -0700108 };
109
110 void checkEqual(TransactionInfo info, SurfaceFlinger::TransactionState state) {
111 EXPECT_EQ(0, info.states.size());
112 EXPECT_EQ(0, state.states.size());
113
114 EXPECT_EQ(0, info.displays.size());
115 EXPECT_EQ(0, state.displays.size());
116 EXPECT_EQ(info.flags, state.flags);
117 EXPECT_EQ(info.desiredPresentTime, state.desiredPresentTime);
118 }
119
120 void setupSingle(TransactionInfo& transaction, uint32_t flags, bool syncInputWindows,
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700121 int64_t desiredPresentTime, int64_t frameTimelineVsyncId) {
Valerie Haud251afb2019-03-29 14:19:02 -0700122 mTransactionNumber++;
123 transaction.flags |= flags; // ISurfaceComposer::eSynchronous;
124 transaction.inputWindowCommands.syncInputWindows = syncInputWindows;
125 transaction.desiredPresentTime = desiredPresentTime;
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) {
Arthur Hung1bedccb2020-09-24 10:09:25 +0000130 ASSERT_EQ(0, mFlinger.getTransactionQueue().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 Abraham22c7b5c2020-09-22 19:33:40 -0700136 /*desiredPresentTime*/ -1, 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,
141 transaction.desiredPresentTime, transaction.uncacheBuffer,
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000142 mHasListenerCallbacks, mCallbacks, transaction.id);
Valerie Haud251afb2019-03-29 14:19:02 -0700143
144 // This transaction should not have been placed on the transaction queue.
145 // If transaction is synchronous or syncs input windows, SF
146 // applyTransactionState should time out (5s) wating for SF to commit
147 // the transaction or to receive a signal that syncInputWindows has
148 // completed. If this is animation, it should not time out waiting.
149 nsecs_t returnedTime = systemTime();
150 if (flags & ISurfaceComposer::eSynchronous || syncInputWindows) {
151 EXPECT_GE(returnedTime, applicationTime + s2ns(5));
152 } else {
153 EXPECT_LE(returnedTime, applicationTime + s2ns(5));
154 }
Arthur Hung1bedccb2020-09-24 10:09:25 +0000155 auto transactionQueue = mFlinger.getTransactionQueue();
Valerie Haud251afb2019-03-29 14:19:02 -0700156 EXPECT_EQ(0, transactionQueue.size());
157 }
158
159 void PlaceOnTransactionQueue(uint32_t flags, bool syncInputWindows) {
Arthur Hung1bedccb2020-09-24 10:09:25 +0000160 ASSERT_EQ(0, mFlinger.getTransactionQueue().size());
Valerie Haud251afb2019-03-29 14:19:02 -0700161 // called in SurfaceFlinger::signalTransaction
162 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
163
164 // first check will see desired present time has not passed,
165 // but afterwards it will look like the desired present time has passed
166 nsecs_t time = systemTime();
Ady Abraham8cb21882020-08-26 18:22:05 -0700167 EXPECT_CALL(*mVSyncTracker, nextAnticipatedVSyncTimeFrom(_))
Valerie Haud251afb2019-03-29 14:19:02 -0700168 .WillOnce(Return(time + nsecs_t(5 * 1e8)));
169 TransactionInfo transaction;
170 setupSingle(transaction, flags, syncInputWindows,
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700171 /*desiredPresentTime*/ time + s2ns(1), ISurfaceComposer::INVALID_VSYNC_ID);
Valerie Haud251afb2019-03-29 14:19:02 -0700172 nsecs_t applicationSentTime = systemTime();
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700173 mFlinger.setTransactionState(transaction.frameTimelineVsyncId, transaction.states,
174 transaction.displays, transaction.flags,
Valerie Haud251afb2019-03-29 14:19:02 -0700175 transaction.applyToken, transaction.inputWindowCommands,
176 transaction.desiredPresentTime, transaction.uncacheBuffer,
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000177 mHasListenerCallbacks, mCallbacks, transaction.id);
Valerie Haud251afb2019-03-29 14:19:02 -0700178
179 nsecs_t returnedTime = systemTime();
180 EXPECT_LE(returnedTime, applicationSentTime + s2ns(5));
181 // This transaction should have been placed on the transaction queue
Arthur Hung1bedccb2020-09-24 10:09:25 +0000182 auto transactionQueue = mFlinger.getTransactionQueue();
Valerie Haud251afb2019-03-29 14:19:02 -0700183 EXPECT_EQ(1, transactionQueue.size());
184 }
185
186 void BlockedByPriorTransaction(uint32_t flags, bool syncInputWindows) {
Arthur Hung1bedccb2020-09-24 10:09:25 +0000187 ASSERT_EQ(0, mFlinger.getTransactionQueue().size());
Valerie Haud251afb2019-03-29 14:19:02 -0700188 // called in SurfaceFlinger::signalTransaction
189 nsecs_t time = systemTime();
190 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
Ady Abraham8cb21882020-08-26 18:22:05 -0700191 EXPECT_CALL(*mVSyncTracker, nextAnticipatedVSyncTimeFrom(_))
Valerie Haud251afb2019-03-29 14:19:02 -0700192 .WillOnce(Return(time + nsecs_t(5 * 1e8)));
193 // transaction that should go on the pending thread
194 TransactionInfo transactionA;
195 setupSingle(transactionA, /*flags*/ 0, /*syncInputWindows*/ false,
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700196 /*desiredPresentTime*/ time + s2ns(1), ISurfaceComposer::INVALID_VSYNC_ID);
Valerie Haud251afb2019-03-29 14:19:02 -0700197
198 // transaction that would not have gone on the pending thread if not
199 // blocked
200 TransactionInfo transactionB;
201 setupSingle(transactionB, flags, syncInputWindows,
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700202 /*desiredPresentTime*/ -1, ISurfaceComposer::INVALID_VSYNC_ID);
Valerie Haud251afb2019-03-29 14:19:02 -0700203
204 nsecs_t applicationSentTime = systemTime();
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700205 mFlinger.setTransactionState(transactionA.frameTimelineVsyncId, transactionA.states,
206 transactionA.displays, transactionA.flags,
Valerie Haud251afb2019-03-29 14:19:02 -0700207 transactionA.applyToken, transactionA.inputWindowCommands,
208 transactionA.desiredPresentTime, transactionA.uncacheBuffer,
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000209 mHasListenerCallbacks, mCallbacks, transactionA.id);
Valerie Haud251afb2019-03-29 14:19:02 -0700210
211 // This thread should not have been blocked by the above transaction
212 // (5s is the timeout period that applyTransactionState waits for SF to
213 // commit the transaction)
214 EXPECT_LE(systemTime(), applicationSentTime + s2ns(5));
215
216 applicationSentTime = systemTime();
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700217 mFlinger.setTransactionState(transactionB.frameTimelineVsyncId, transactionB.states,
218 transactionB.displays, transactionB.flags,
Valerie Haud251afb2019-03-29 14:19:02 -0700219 transactionB.applyToken, transactionB.inputWindowCommands,
220 transactionB.desiredPresentTime, transactionB.uncacheBuffer,
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000221 mHasListenerCallbacks, mCallbacks, transactionB.id);
Valerie Haud251afb2019-03-29 14:19:02 -0700222
223 // this thread should have been blocked by the above transaction
224 // if this is an animation, this thread should be blocked for 5s
225 // in setTransactionState waiting for transactionA to flush. Otherwise,
226 // the transaction should be placed on the pending queue
227 if (flags & ISurfaceComposer::eAnimation) {
228 EXPECT_GE(systemTime(), applicationSentTime + s2ns(5));
229 } else {
230 EXPECT_LE(systemTime(), applicationSentTime + s2ns(5));
231 }
232
233 // check that there is one binder on the pending queue.
Arthur Hung1bedccb2020-09-24 10:09:25 +0000234 auto transactionQueue = mFlinger.getTransactionQueue();
Valerie Haud251afb2019-03-29 14:19:02 -0700235 EXPECT_EQ(1, transactionQueue.size());
236
237 auto& [applyToken, transactionStates] = *(transactionQueue.begin());
238 EXPECT_EQ(2, transactionStates.size());
239
240 auto& transactionStateA = transactionStates.front();
241 transactionStates.pop();
242 checkEqual(transactionA, transactionStateA);
243 auto& transactionStateB = transactionStates.front();
244 checkEqual(transactionB, transactionStateB);
245 }
246
247 bool mHasListenerCallbacks = false;
248 std::vector<ListenerCallbacks> mCallbacks;
249 int mTransactionNumber = 0;
250};
251
252TEST_F(TransactionApplicationTest, Flush_RemovesFromQueue) {
Arthur Hung1bedccb2020-09-24 10:09:25 +0000253 ASSERT_EQ(0, mFlinger.getTransactionQueue().size());
Valerie Haud251afb2019-03-29 14:19:02 -0700254 // called in SurfaceFlinger::signalTransaction
255 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
256
257 // nsecs_t time = systemTime();
Ady Abraham8cb21882020-08-26 18:22:05 -0700258 EXPECT_CALL(*mVSyncTracker, nextAnticipatedVSyncTimeFrom(_))
Valerie Haud251afb2019-03-29 14:19:02 -0700259 .WillOnce(Return(nsecs_t(5 * 1e8)))
260 .WillOnce(Return(s2ns(2)));
261 TransactionInfo transactionA; // transaction to go on pending queue
262 setupSingle(transactionA, /*flags*/ 0, /*syncInputWindows*/ false,
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700263 /*desiredPresentTime*/ s2ns(1), ISurfaceComposer::INVALID_VSYNC_ID);
264 mFlinger.setTransactionState(transactionA.frameTimelineVsyncId, transactionA.states,
265 transactionA.displays, transactionA.flags, transactionA.applyToken,
266 transactionA.inputWindowCommands, transactionA.desiredPresentTime,
267 transactionA.uncacheBuffer, mHasListenerCallbacks, mCallbacks,
268 transactionA.id);
Valerie Haud251afb2019-03-29 14:19:02 -0700269
Arthur Hung1bedccb2020-09-24 10:09:25 +0000270 auto& transactionQueue = mFlinger.getTransactionQueue();
Valerie Haud251afb2019-03-29 14:19:02 -0700271 ASSERT_EQ(1, transactionQueue.size());
272
273 auto& [applyToken, transactionStates] = *(transactionQueue.begin());
274 ASSERT_EQ(1, transactionStates.size());
275
276 auto& transactionState = transactionStates.front();
277 checkEqual(transactionA, transactionState);
278
279 // because flushing uses the cached expected present time, we send an empty
280 // transaction here (sending a null applyToken to fake it as from a
281 // different process) to re-query and reset the cached expected present time
282 TransactionInfo empty;
283 empty.applyToken = sp<IBinder>();
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700284 mFlinger.setTransactionState(empty.frameTimelineVsyncId, empty.states, empty.displays,
285 empty.flags, empty.applyToken, empty.inputWindowCommands,
286 empty.desiredPresentTime, empty.uncacheBuffer,
287 mHasListenerCallbacks, mCallbacks, empty.id);
Valerie Haud251afb2019-03-29 14:19:02 -0700288
Arthur Hung1bedccb2020-09-24 10:09:25 +0000289 // flush transaction queue should flush as desiredPresentTime has
Valerie Haud251afb2019-03-29 14:19:02 -0700290 // passed
Arthur Hung1bedccb2020-09-24 10:09:25 +0000291 mFlinger.flushTransactionQueues();
Valerie Haud251afb2019-03-29 14:19:02 -0700292
293 EXPECT_EQ(0, transactionQueue.size());
294}
295
296TEST_F(TransactionApplicationTest, NotPlacedOnTransactionQueue_Synchronous) {
297 NotPlacedOnTransactionQueue(ISurfaceComposer::eSynchronous, /*syncInputWindows*/ false);
298}
299
300TEST_F(TransactionApplicationTest, NotPlacedOnTransactionQueue_Animation) {
301 NotPlacedOnTransactionQueue(ISurfaceComposer::eAnimation, /*syncInputWindows*/ false);
302}
303
304TEST_F(TransactionApplicationTest, NotPlacedOnTransactionQueue_SyncInputWindows) {
305 NotPlacedOnTransactionQueue(/*flags*/ 0, /*syncInputWindows*/ true);
306}
307
308TEST_F(TransactionApplicationTest, PlaceOnTransactionQueue_Synchronous) {
309 PlaceOnTransactionQueue(ISurfaceComposer::eSynchronous, /*syncInputWindows*/ false);
310}
311
312TEST_F(TransactionApplicationTest, PlaceOnTransactionQueue_Animation) {
313 PlaceOnTransactionQueue(ISurfaceComposer::eAnimation, /*syncInputWindows*/ false);
314}
315
316TEST_F(TransactionApplicationTest, PlaceOnTransactionQueue_SyncInputWindows) {
317 PlaceOnTransactionQueue(/*flags*/ 0, /*syncInputWindows*/ true);
318}
319
320TEST_F(TransactionApplicationTest, BlockWithPriorTransaction_Synchronous) {
321 BlockedByPriorTransaction(ISurfaceComposer::eSynchronous, /*syncInputWindows*/ false);
322}
323
324TEST_F(TransactionApplicationTest, BlockWithPriorTransaction_Animation) {
325 BlockedByPriorTransaction(ISurfaceComposer::eSynchronous, /*syncInputWindows*/ false);
326}
327
328TEST_F(TransactionApplicationTest, BlockWithPriorTransaction_SyncInputWindows) {
329 BlockedByPriorTransaction(/*flags*/ 0, /*syncInputWindows*/ true);
330}
331
Valerie Hau09e60052019-12-15 14:51:15 -0800332TEST_F(TransactionApplicationTest, FromHandle) {
333 sp<IBinder> badHandle;
334 auto ret = mFlinger.fromHandle(badHandle);
Alec Mouri9a02eda2020-04-21 17:39:34 -0700335 EXPECT_EQ(nullptr, ret.promote().get());
Valerie Hau09e60052019-12-15 14:51:15 -0800336}
Valerie Haud251afb2019-03-29 14:19:02 -0700337} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800338
339// TODO(b/129481165): remove the #pragma below and fix conversion issues
340#pragma clang diagnostic pop // ignored "-Wconversion"