blob: 05551b47013d84ea3ae6819f33a0de8f136cc76d [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
Valerie Haud251afb2019-03-29 14:19:02 -070018#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"
Valerie Haud251afb2019-03-29 14:19:02 -070031#include "mock/MockEventThread.h"
32#include "mock/MockMessageQueue.h"
Ady Abraham8cb21882020-08-26 18:22:05 -070033#include "mock/MockVsyncController.h"
Valerie Haud251afb2019-03-29 14:19:02 -070034
35namespace android {
36
37using testing::_;
38using testing::Return;
39
40using FakeHwcDisplayInjector = TestableSurfaceFlinger::FakeHwcDisplayInjector;
41
42class TransactionApplicationTest : public testing::Test {
43public:
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
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
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(_, _))
Ady Abraham62f216c2020-10-13 19:07:23 -070065 .WillOnce(Return(new EventThreadConnection(eventThread.get(), /*callingUid=*/0,
66 ResyncCallback())));
Valerie Haud251afb2019-03-29 14:19:02 -070067
68 EXPECT_CALL(*sfEventThread, registerDisplayEventConnection(_));
69 EXPECT_CALL(*sfEventThread, createEventConnection(_, _))
Ady Abraham62f216c2020-10-13 19:07:23 -070070 .WillOnce(Return(new EventThreadConnection(sfEventThread.get(), /*callingUid=*/0,
71 ResyncCallback())));
Valerie Haud251afb2019-03-29 14:19:02 -070072
Ady Abraham8cb21882020-08-26 18:22:05 -070073 EXPECT_CALL(*mVSyncTracker, nextAnticipatedVSyncTimeFrom(_)).WillRepeatedly(Return(0));
74 EXPECT_CALL(*mVSyncTracker, currentPeriod())
Marin Shalamanov045b7002021-01-07 16:56:24 +010075 .WillRepeatedly(Return(FakeHwcDisplayInjector::DEFAULT_VSYNC_PERIOD));
Valerie Haud251afb2019-03-29 14:19:02 -070076
Ady Abraham3efa3942021-06-24 19:01:25 -070077 mFlinger.setupComposer(std::make_unique<Hwc2::mock::Composer>());
Ady Abraham8cb21882020-08-26 18:22:05 -070078 mFlinger.setupScheduler(std::unique_ptr<mock::VsyncController>(mVsyncController),
79 std::unique_ptr<mock::VSyncTracker>(mVSyncTracker),
Valerie Haud251afb2019-03-29 14:19:02 -070080 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>();
Valerie Haud251afb2019-03-29 14:19:02 -070087
88 mock::MessageQueue* mMessageQueue = new mock::MessageQueue();
Ady Abraham8cb21882020-08-26 18:22:05 -070089 mock::VsyncController* mVsyncController = new mock::VsyncController();
90 mock::VSyncTracker* mVSyncTracker = new mock::VSyncTracker();
Valerie Haud251afb2019-03-29 14:19:02 -070091
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;
Ady Abrahamf0c56492020-12-17 18:04:15 -080098 int64_t desiredPresentTime = 0;
99 bool isAutoTimestamp = true;
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000100 FrameTimelineInfo frameTimelineInfo;
Valerie Haud251afb2019-03-29 14:19:02 -0700101 client_cache_t uncacheBuffer;
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000102 uint64_t id = static_cast<uint64_t>(-1);
103 static_assert(0xffffffffffffffff == static_cast<uint64_t>(-1));
Valerie Haud251afb2019-03-29 14:19:02 -0700104 };
105
Vishnu Nair6b591152021-10-08 11:45:14 -0700106 void checkEqual(TransactionInfo info, TransactionState state) {
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000107 EXPECT_EQ(0u, info.states.size());
108 EXPECT_EQ(0u, state.states.size());
Valerie Haud251afb2019-03-29 14:19:02 -0700109
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000110 EXPECT_EQ(0u, info.displays.size());
111 EXPECT_EQ(0u, state.displays.size());
Valerie Haud251afb2019-03-29 14:19:02 -0700112 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,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800117 int64_t desiredPresentTime, bool isAutoTimestamp,
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000118 const FrameTimelineInfo& frameTimelineInfo) {
Valerie Haud251afb2019-03-29 14:19:02 -0700119 mTransactionNumber++;
120 transaction.flags |= flags; // ISurfaceComposer::eSynchronous;
121 transaction.inputWindowCommands.syncInputWindows = syncInputWindows;
122 transaction.desiredPresentTime = desiredPresentTime;
Ady Abrahamf0c56492020-12-17 18:04:15 -0800123 transaction.isAutoTimestamp = isAutoTimestamp;
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000124 transaction.frameTimelineInfo = frameTimelineInfo;
Valerie Haud251afb2019-03-29 14:19:02 -0700125 }
126
127 void NotPlacedOnTransactionQueue(uint32_t flags, bool syncInputWindows) {
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000128 ASSERT_EQ(0u, mFlinger.getTransactionQueue().size());
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700129 EXPECT_CALL(*mMessageQueue, scheduleCommit()).Times(1);
Valerie Haud251afb2019-03-29 14:19:02 -0700130 TransactionInfo transaction;
131 setupSingle(transaction, flags, syncInputWindows,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800132 /*desiredPresentTime*/ systemTime(), /*isAutoTimestamp*/ true,
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000133 FrameTimelineInfo{});
Valerie Haud251afb2019-03-29 14:19:02 -0700134 nsecs_t applicationTime = systemTime();
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000135 mFlinger.setTransactionState(transaction.frameTimelineInfo, transaction.states,
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700136 transaction.displays, transaction.flags,
Valerie Haud251afb2019-03-29 14:19:02 -0700137 transaction.applyToken, transaction.inputWindowCommands,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800138 transaction.desiredPresentTime, transaction.isAutoTimestamp,
139 transaction.uncacheBuffer, mHasListenerCallbacks, mCallbacks,
140 transaction.id);
Valerie Haud251afb2019-03-29 14:19:02 -0700141
Valerie Haud251afb2019-03-29 14:19:02 -0700142 // If transaction is synchronous or syncs input windows, SF
143 // applyTransactionState should time out (5s) wating for SF to commit
144 // the transaction or to receive a signal that syncInputWindows has
145 // completed. If this is animation, it should not time out waiting.
146 nsecs_t returnedTime = systemTime();
147 if (flags & ISurfaceComposer::eSynchronous || syncInputWindows) {
148 EXPECT_GE(returnedTime, applicationTime + s2ns(5));
149 } else {
150 EXPECT_LE(returnedTime, applicationTime + s2ns(5));
151 }
Arthur Hung58144272021-01-16 03:43:53 +0000152 // Each transaction should have been placed on the transaction queue
Zhuoyao Zhang3d3540d2021-01-14 05:14:54 +0000153 auto transactionQueue = mFlinger.getTransactionQueue();
Arthur Hung58144272021-01-16 03:43:53 +0000154 EXPECT_EQ(1u, transactionQueue.size());
Valerie Haud251afb2019-03-29 14:19:02 -0700155 }
156
157 void PlaceOnTransactionQueue(uint32_t flags, bool syncInputWindows) {
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000158 ASSERT_EQ(0u, mFlinger.getTransactionQueue().size());
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700159 EXPECT_CALL(*mMessageQueue, scheduleCommit()).Times(1);
Valerie Haud251afb2019-03-29 14:19:02 -0700160
161 // first check will see desired present time has not passed,
162 // but afterwards it will look like the desired present time has passed
163 nsecs_t time = systemTime();
Valerie Haud251afb2019-03-29 14:19:02 -0700164 TransactionInfo transaction;
165 setupSingle(transaction, flags, syncInputWindows,
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000166 /*desiredPresentTime*/ time + s2ns(1), false, FrameTimelineInfo{});
Valerie Haud251afb2019-03-29 14:19:02 -0700167 nsecs_t applicationSentTime = systemTime();
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000168 mFlinger.setTransactionState(transaction.frameTimelineInfo, transaction.states,
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700169 transaction.displays, transaction.flags,
Valerie Haud251afb2019-03-29 14:19:02 -0700170 transaction.applyToken, transaction.inputWindowCommands,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800171 transaction.desiredPresentTime, transaction.isAutoTimestamp,
172 transaction.uncacheBuffer, mHasListenerCallbacks, mCallbacks,
173 transaction.id);
Valerie Haud251afb2019-03-29 14:19:02 -0700174
175 nsecs_t returnedTime = systemTime();
Ady Abrahame46243a2021-02-23 19:33:49 -0800176 if ((flags & ISurfaceComposer::eSynchronous) || syncInputWindows) {
177 EXPECT_GE(systemTime(), applicationSentTime + s2ns(5));
178 } else {
179 EXPECT_LE(returnedTime, applicationSentTime + s2ns(5));
180 }
Valerie Haud251afb2019-03-29 14:19:02 -0700181 // This transaction should have been placed on the transaction queue
Zhuoyao Zhang3d3540d2021-01-14 05:14:54 +0000182 auto transactionQueue = mFlinger.getTransactionQueue();
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000183 EXPECT_EQ(1u, transactionQueue.size());
Valerie Haud251afb2019-03-29 14:19:02 -0700184 }
185
186 void BlockedByPriorTransaction(uint32_t flags, bool syncInputWindows) {
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000187 ASSERT_EQ(0u, mFlinger.getTransactionQueue().size());
Valerie Haud251afb2019-03-29 14:19:02 -0700188 nsecs_t time = systemTime();
Ady Abrahame46243a2021-02-23 19:33:49 -0800189 if (!syncInputWindows) {
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700190 EXPECT_CALL(*mMessageQueue, scheduleCommit()).Times(2);
Ady Abrahame46243a2021-02-23 19:33:49 -0800191 } else {
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700192 EXPECT_CALL(*mMessageQueue, scheduleCommit()).Times(1);
Ady Abrahame46243a2021-02-23 19:33:49 -0800193 }
Valerie Haud251afb2019-03-29 14:19:02 -0700194 // transaction that should go on the pending thread
195 TransactionInfo transactionA;
196 setupSingle(transactionA, /*flags*/ 0, /*syncInputWindows*/ false,
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000197 /*desiredPresentTime*/ time + s2ns(1), false, FrameTimelineInfo{});
Valerie Haud251afb2019-03-29 14:19:02 -0700198
199 // transaction that would not have gone on the pending thread if not
200 // blocked
201 TransactionInfo transactionB;
202 setupSingle(transactionB, flags, syncInputWindows,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800203 /*desiredPresentTime*/ systemTime(), /*isAutoTimestamp*/ true,
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000204 FrameTimelineInfo{});
Valerie Haud251afb2019-03-29 14:19:02 -0700205
206 nsecs_t applicationSentTime = systemTime();
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000207 mFlinger.setTransactionState(transactionA.frameTimelineInfo, transactionA.states,
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700208 transactionA.displays, transactionA.flags,
Valerie Haud251afb2019-03-29 14:19:02 -0700209 transactionA.applyToken, transactionA.inputWindowCommands,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800210 transactionA.desiredPresentTime, transactionA.isAutoTimestamp,
211 transactionA.uncacheBuffer, mHasListenerCallbacks, mCallbacks,
212 transactionA.id);
Valerie Haud251afb2019-03-29 14:19:02 -0700213
214 // This thread should not have been blocked by the above transaction
215 // (5s is the timeout period that applyTransactionState waits for SF to
216 // commit the transaction)
217 EXPECT_LE(systemTime(), applicationSentTime + s2ns(5));
Arthur Hung58144272021-01-16 03:43:53 +0000218 // transaction that would goes to pending transaciton queue.
219 mFlinger.flushTransactionQueues();
Valerie Haud251afb2019-03-29 14:19:02 -0700220
221 applicationSentTime = systemTime();
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000222 mFlinger.setTransactionState(transactionB.frameTimelineInfo, transactionB.states,
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700223 transactionB.displays, transactionB.flags,
Valerie Haud251afb2019-03-29 14:19:02 -0700224 transactionB.applyToken, transactionB.inputWindowCommands,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800225 transactionB.desiredPresentTime, transactionB.isAutoTimestamp,
226 transactionB.uncacheBuffer, mHasListenerCallbacks, mCallbacks,
227 transactionB.id);
Valerie Haud251afb2019-03-29 14:19:02 -0700228
229 // this thread should have been blocked by the above transaction
230 // if this is an animation, this thread should be blocked for 5s
231 // in setTransactionState waiting for transactionA to flush. Otherwise,
232 // the transaction should be placed on the pending queue
Ady Abrahame46243a2021-02-23 19:33:49 -0800233 if (flags & (ISurfaceComposer::eAnimation | ISurfaceComposer::eSynchronous) ||
234 syncInputWindows) {
Valerie Haud251afb2019-03-29 14:19:02 -0700235 EXPECT_GE(systemTime(), applicationSentTime + s2ns(5));
236 } else {
237 EXPECT_LE(systemTime(), applicationSentTime + s2ns(5));
238 }
239
Arthur Hung58144272021-01-16 03:43:53 +0000240 // transaction that would goes to pending transaciton queue.
241 mFlinger.flushTransactionQueues();
242
Ady Abrahame46243a2021-02-23 19:33:49 -0800243 // check that the transaction was applied.
Arthur Hung58144272021-01-16 03:43:53 +0000244 auto transactionQueue = mFlinger.getPendingTransactionQueue();
Ady Abrahame46243a2021-02-23 19:33:49 -0800245 EXPECT_EQ(0u, transactionQueue.size());
Valerie Haud251afb2019-03-29 14:19:02 -0700246 }
247
248 bool mHasListenerCallbacks = false;
249 std::vector<ListenerCallbacks> mCallbacks;
250 int mTransactionNumber = 0;
251};
252
253TEST_F(TransactionApplicationTest, Flush_RemovesFromQueue) {
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000254 ASSERT_EQ(0u, mFlinger.getTransactionQueue().size());
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700255 EXPECT_CALL(*mMessageQueue, scheduleCommit()).Times(1);
Valerie Haud251afb2019-03-29 14:19:02 -0700256
Valerie Haud251afb2019-03-29 14:19:02 -0700257 TransactionInfo transactionA; // transaction to go on pending queue
258 setupSingle(transactionA, /*flags*/ 0, /*syncInputWindows*/ false,
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000259 /*desiredPresentTime*/ s2ns(1), false, FrameTimelineInfo{});
260 mFlinger.setTransactionState(transactionA.frameTimelineInfo, transactionA.states,
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700261 transactionA.displays, transactionA.flags, transactionA.applyToken,
262 transactionA.inputWindowCommands, transactionA.desiredPresentTime,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800263 transactionA.isAutoTimestamp, transactionA.uncacheBuffer,
264 mHasListenerCallbacks, mCallbacks, transactionA.id);
Valerie Haud251afb2019-03-29 14:19:02 -0700265
Zhuoyao Zhang3d3540d2021-01-14 05:14:54 +0000266 auto& transactionQueue = mFlinger.getTransactionQueue();
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000267 ASSERT_EQ(1u, transactionQueue.size());
Valerie Haud251afb2019-03-29 14:19:02 -0700268
Arthur Hung58144272021-01-16 03:43:53 +0000269 auto& transactionState = transactionQueue.front();
Valerie Haud251afb2019-03-29 14:19:02 -0700270 checkEqual(transactionA, transactionState);
271
272 // because flushing uses the cached expected present time, we send an empty
273 // transaction here (sending a null applyToken to fake it as from a
274 // different process) to re-query and reset the cached expected present time
275 TransactionInfo empty;
276 empty.applyToken = sp<IBinder>();
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000277 mFlinger.setTransactionState(empty.frameTimelineInfo, empty.states, empty.displays, empty.flags,
278 empty.applyToken, empty.inputWindowCommands,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800279 empty.desiredPresentTime, empty.isAutoTimestamp,
280 empty.uncacheBuffer, mHasListenerCallbacks, mCallbacks, empty.id);
Valerie Haud251afb2019-03-29 14:19:02 -0700281
Zhuoyao Zhang3d3540d2021-01-14 05:14:54 +0000282 // flush transaction queue should flush as desiredPresentTime has
Valerie Haud251afb2019-03-29 14:19:02 -0700283 // passed
Zhuoyao Zhang3d3540d2021-01-14 05:14:54 +0000284 mFlinger.flushTransactionQueues();
Valerie Haud251afb2019-03-29 14:19:02 -0700285
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000286 EXPECT_EQ(0u, transactionQueue.size());
Valerie Haud251afb2019-03-29 14:19:02 -0700287}
288
289TEST_F(TransactionApplicationTest, NotPlacedOnTransactionQueue_Synchronous) {
290 NotPlacedOnTransactionQueue(ISurfaceComposer::eSynchronous, /*syncInputWindows*/ false);
291}
292
293TEST_F(TransactionApplicationTest, NotPlacedOnTransactionQueue_Animation) {
294 NotPlacedOnTransactionQueue(ISurfaceComposer::eAnimation, /*syncInputWindows*/ false);
295}
296
297TEST_F(TransactionApplicationTest, NotPlacedOnTransactionQueue_SyncInputWindows) {
298 NotPlacedOnTransactionQueue(/*flags*/ 0, /*syncInputWindows*/ true);
299}
300
301TEST_F(TransactionApplicationTest, PlaceOnTransactionQueue_Synchronous) {
302 PlaceOnTransactionQueue(ISurfaceComposer::eSynchronous, /*syncInputWindows*/ false);
303}
304
305TEST_F(TransactionApplicationTest, PlaceOnTransactionQueue_Animation) {
306 PlaceOnTransactionQueue(ISurfaceComposer::eAnimation, /*syncInputWindows*/ false);
307}
308
309TEST_F(TransactionApplicationTest, PlaceOnTransactionQueue_SyncInputWindows) {
310 PlaceOnTransactionQueue(/*flags*/ 0, /*syncInputWindows*/ true);
311}
312
313TEST_F(TransactionApplicationTest, BlockWithPriorTransaction_Synchronous) {
314 BlockedByPriorTransaction(ISurfaceComposer::eSynchronous, /*syncInputWindows*/ false);
315}
316
317TEST_F(TransactionApplicationTest, BlockWithPriorTransaction_Animation) {
318 BlockedByPriorTransaction(ISurfaceComposer::eSynchronous, /*syncInputWindows*/ false);
319}
320
321TEST_F(TransactionApplicationTest, BlockWithPriorTransaction_SyncInputWindows) {
322 BlockedByPriorTransaction(/*flags*/ 0, /*syncInputWindows*/ true);
323}
324
Valerie Hau09e60052019-12-15 14:51:15 -0800325TEST_F(TransactionApplicationTest, FromHandle) {
326 sp<IBinder> badHandle;
327 auto ret = mFlinger.fromHandle(badHandle);
Alec Mouri9a02eda2020-04-21 17:39:34 -0700328 EXPECT_EQ(nullptr, ret.promote().get());
Valerie Hau09e60052019-12-15 14:51:15 -0800329}
Valerie Haud251afb2019-03-29 14:19:02 -0700330} // namespace android