blob: c36d9947b7c3f12523d3185085155ae0b144ddcf [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;
100 int64_t desiredPresentTime = -1;
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700101 int64_t frameTimelineVsyncId = ISurfaceComposer::INVALID_VSYNC_ID;
Valerie Haud251afb2019-03-29 14:19:02 -0700102 client_cache_t uncacheBuffer;
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000103 int64_t id = -1;
Valerie Haud251afb2019-03-29 14:19:02 -0700104 };
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,
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700117 int64_t desiredPresentTime, int64_t frameTimelineVsyncId) {
Valerie Haud251afb2019-03-29 14:19:02 -0700118 mTransactionNumber++;
119 transaction.flags |= flags; // ISurfaceComposer::eSynchronous;
120 transaction.inputWindowCommands.syncInputWindows = syncInputWindows;
121 transaction.desiredPresentTime = desiredPresentTime;
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700122 transaction.frameTimelineVsyncId = frameTimelineVsyncId;
Valerie Haud251afb2019-03-29 14:19:02 -0700123 }
124
125 void NotPlacedOnTransactionQueue(uint32_t flags, bool syncInputWindows) {
Arthur Hung1bedccb2020-09-24 10:09:25 +0000126 ASSERT_EQ(0, mFlinger.getTransactionQueue().size());
Valerie Haud251afb2019-03-29 14:19:02 -0700127 // called in SurfaceFlinger::signalTransaction
128 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
Ady Abraham8cb21882020-08-26 18:22:05 -0700129 EXPECT_CALL(*mVSyncTracker, nextAnticipatedVSyncTimeFrom(_)).WillOnce(Return(systemTime()));
Valerie Haud251afb2019-03-29 14:19:02 -0700130 TransactionInfo transaction;
131 setupSingle(transaction, flags, syncInputWindows,
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700132 /*desiredPresentTime*/ -1, ISurfaceComposer::INVALID_VSYNC_ID);
Valerie Haud251afb2019-03-29 14:19:02 -0700133 nsecs_t applicationTime = systemTime();
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700134 mFlinger.setTransactionState(transaction.frameTimelineVsyncId, transaction.states,
135 transaction.displays, transaction.flags,
Valerie Haud251afb2019-03-29 14:19:02 -0700136 transaction.applyToken, transaction.inputWindowCommands,
137 transaction.desiredPresentTime, transaction.uncacheBuffer,
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000138 mHasListenerCallbacks, mCallbacks, transaction.id);
Valerie Haud251afb2019-03-29 14:19:02 -0700139
140 // This transaction should not have been placed on the transaction queue.
141 // If transaction is synchronous or syncs input windows, SF
142 // applyTransactionState should time out (5s) wating for SF to commit
143 // the transaction or to receive a signal that syncInputWindows has
144 // completed. If this is animation, it should not time out waiting.
145 nsecs_t returnedTime = systemTime();
146 if (flags & ISurfaceComposer::eSynchronous || syncInputWindows) {
147 EXPECT_GE(returnedTime, applicationTime + s2ns(5));
148 } else {
149 EXPECT_LE(returnedTime, applicationTime + s2ns(5));
150 }
Arthur Hung1bedccb2020-09-24 10:09:25 +0000151 auto transactionQueue = mFlinger.getTransactionQueue();
Valerie Haud251afb2019-03-29 14:19:02 -0700152 EXPECT_EQ(0, transactionQueue.size());
153 }
154
155 void PlaceOnTransactionQueue(uint32_t flags, bool syncInputWindows) {
Arthur Hung1bedccb2020-09-24 10:09:25 +0000156 ASSERT_EQ(0, mFlinger.getTransactionQueue().size());
Valerie Haud251afb2019-03-29 14:19:02 -0700157 // called in SurfaceFlinger::signalTransaction
158 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
159
160 // first check will see desired present time has not passed,
161 // but afterwards it will look like the desired present time has passed
162 nsecs_t time = systemTime();
Ady Abraham8cb21882020-08-26 18:22:05 -0700163 EXPECT_CALL(*mVSyncTracker, nextAnticipatedVSyncTimeFrom(_))
Valerie Haud251afb2019-03-29 14:19:02 -0700164 .WillOnce(Return(time + nsecs_t(5 * 1e8)));
165 TransactionInfo transaction;
166 setupSingle(transaction, flags, syncInputWindows,
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700167 /*desiredPresentTime*/ time + s2ns(1), ISurfaceComposer::INVALID_VSYNC_ID);
Valerie Haud251afb2019-03-29 14:19:02 -0700168 nsecs_t applicationSentTime = systemTime();
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700169 mFlinger.setTransactionState(transaction.frameTimelineVsyncId, transaction.states,
170 transaction.displays, transaction.flags,
Valerie Haud251afb2019-03-29 14:19:02 -0700171 transaction.applyToken, transaction.inputWindowCommands,
172 transaction.desiredPresentTime, transaction.uncacheBuffer,
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000173 mHasListenerCallbacks, mCallbacks, transaction.id);
Valerie Haud251afb2019-03-29 14:19:02 -0700174
175 nsecs_t returnedTime = systemTime();
176 EXPECT_LE(returnedTime, applicationSentTime + s2ns(5));
177 // This transaction should have been placed on the transaction queue
Arthur Hung1bedccb2020-09-24 10:09:25 +0000178 auto transactionQueue = mFlinger.getTransactionQueue();
Valerie Haud251afb2019-03-29 14:19:02 -0700179 EXPECT_EQ(1, transactionQueue.size());
180 }
181
182 void BlockedByPriorTransaction(uint32_t flags, bool syncInputWindows) {
Arthur Hung1bedccb2020-09-24 10:09:25 +0000183 ASSERT_EQ(0, mFlinger.getTransactionQueue().size());
Valerie Haud251afb2019-03-29 14:19:02 -0700184 // called in SurfaceFlinger::signalTransaction
185 nsecs_t time = systemTime();
186 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
Ady Abraham8cb21882020-08-26 18:22:05 -0700187 EXPECT_CALL(*mVSyncTracker, nextAnticipatedVSyncTimeFrom(_))
Valerie Haud251afb2019-03-29 14:19:02 -0700188 .WillOnce(Return(time + nsecs_t(5 * 1e8)));
189 // transaction that should go on the pending thread
190 TransactionInfo transactionA;
191 setupSingle(transactionA, /*flags*/ 0, /*syncInputWindows*/ false,
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700192 /*desiredPresentTime*/ time + s2ns(1), ISurfaceComposer::INVALID_VSYNC_ID);
Valerie Haud251afb2019-03-29 14:19:02 -0700193
194 // transaction that would not have gone on the pending thread if not
195 // blocked
196 TransactionInfo transactionB;
197 setupSingle(transactionB, flags, syncInputWindows,
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700198 /*desiredPresentTime*/ -1, ISurfaceComposer::INVALID_VSYNC_ID);
Valerie Haud251afb2019-03-29 14:19:02 -0700199
200 nsecs_t applicationSentTime = systemTime();
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700201 mFlinger.setTransactionState(transactionA.frameTimelineVsyncId, transactionA.states,
202 transactionA.displays, transactionA.flags,
Valerie Haud251afb2019-03-29 14:19:02 -0700203 transactionA.applyToken, transactionA.inputWindowCommands,
204 transactionA.desiredPresentTime, transactionA.uncacheBuffer,
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000205 mHasListenerCallbacks, mCallbacks, transactionA.id);
Valerie Haud251afb2019-03-29 14:19:02 -0700206
207 // This thread should not have been blocked by the above transaction
208 // (5s is the timeout period that applyTransactionState waits for SF to
209 // commit the transaction)
210 EXPECT_LE(systemTime(), applicationSentTime + s2ns(5));
211
212 applicationSentTime = systemTime();
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700213 mFlinger.setTransactionState(transactionB.frameTimelineVsyncId, transactionB.states,
214 transactionB.displays, transactionB.flags,
Valerie Haud251afb2019-03-29 14:19:02 -0700215 transactionB.applyToken, transactionB.inputWindowCommands,
216 transactionB.desiredPresentTime, transactionB.uncacheBuffer,
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000217 mHasListenerCallbacks, mCallbacks, transactionB.id);
Valerie Haud251afb2019-03-29 14:19:02 -0700218
219 // this thread should have been blocked by the above transaction
220 // if this is an animation, this thread should be blocked for 5s
221 // in setTransactionState waiting for transactionA to flush. Otherwise,
222 // the transaction should be placed on the pending queue
223 if (flags & ISurfaceComposer::eAnimation) {
224 EXPECT_GE(systemTime(), applicationSentTime + s2ns(5));
225 } else {
226 EXPECT_LE(systemTime(), applicationSentTime + s2ns(5));
227 }
228
229 // check that there is one binder on the pending queue.
Arthur Hung1bedccb2020-09-24 10:09:25 +0000230 auto transactionQueue = mFlinger.getTransactionQueue();
Valerie Haud251afb2019-03-29 14:19:02 -0700231 EXPECT_EQ(1, transactionQueue.size());
232
233 auto& [applyToken, transactionStates] = *(transactionQueue.begin());
234 EXPECT_EQ(2, transactionStates.size());
235
236 auto& transactionStateA = transactionStates.front();
237 transactionStates.pop();
238 checkEqual(transactionA, transactionStateA);
239 auto& transactionStateB = transactionStates.front();
240 checkEqual(transactionB, transactionStateB);
241 }
242
243 bool mHasListenerCallbacks = false;
244 std::vector<ListenerCallbacks> mCallbacks;
245 int mTransactionNumber = 0;
246};
247
248TEST_F(TransactionApplicationTest, Flush_RemovesFromQueue) {
Arthur Hung1bedccb2020-09-24 10:09:25 +0000249 ASSERT_EQ(0, mFlinger.getTransactionQueue().size());
Valerie Haud251afb2019-03-29 14:19:02 -0700250 // called in SurfaceFlinger::signalTransaction
251 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
252
253 // nsecs_t time = systemTime();
Ady Abraham8cb21882020-08-26 18:22:05 -0700254 EXPECT_CALL(*mVSyncTracker, nextAnticipatedVSyncTimeFrom(_))
Valerie Haud251afb2019-03-29 14:19:02 -0700255 .WillOnce(Return(nsecs_t(5 * 1e8)))
256 .WillOnce(Return(s2ns(2)));
257 TransactionInfo transactionA; // transaction to go on pending queue
258 setupSingle(transactionA, /*flags*/ 0, /*syncInputWindows*/ false,
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700259 /*desiredPresentTime*/ s2ns(1), ISurfaceComposer::INVALID_VSYNC_ID);
260 mFlinger.setTransactionState(transactionA.frameTimelineVsyncId, transactionA.states,
261 transactionA.displays, transactionA.flags, transactionA.applyToken,
262 transactionA.inputWindowCommands, transactionA.desiredPresentTime,
263 transactionA.uncacheBuffer, mHasListenerCallbacks, mCallbacks,
264 transactionA.id);
Valerie Haud251afb2019-03-29 14:19:02 -0700265
Arthur Hung1bedccb2020-09-24 10:09:25 +0000266 auto& transactionQueue = mFlinger.getTransactionQueue();
Valerie Haud251afb2019-03-29 14:19:02 -0700267 ASSERT_EQ(1, transactionQueue.size());
268
269 auto& [applyToken, transactionStates] = *(transactionQueue.begin());
270 ASSERT_EQ(1, transactionStates.size());
271
272 auto& transactionState = transactionStates.front();
273 checkEqual(transactionA, transactionState);
274
275 // because flushing uses the cached expected present time, we send an empty
276 // transaction here (sending a null applyToken to fake it as from a
277 // different process) to re-query and reset the cached expected present time
278 TransactionInfo empty;
279 empty.applyToken = sp<IBinder>();
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700280 mFlinger.setTransactionState(empty.frameTimelineVsyncId, empty.states, empty.displays,
281 empty.flags, empty.applyToken, empty.inputWindowCommands,
282 empty.desiredPresentTime, empty.uncacheBuffer,
283 mHasListenerCallbacks, mCallbacks, empty.id);
Valerie Haud251afb2019-03-29 14:19:02 -0700284
Arthur Hung1bedccb2020-09-24 10:09:25 +0000285 // flush transaction queue should flush as desiredPresentTime has
Valerie Haud251afb2019-03-29 14:19:02 -0700286 // passed
Arthur Hung1bedccb2020-09-24 10:09:25 +0000287 mFlinger.flushTransactionQueues();
Valerie Haud251afb2019-03-29 14:19:02 -0700288
289 EXPECT_EQ(0, transactionQueue.size());
290}
291
292TEST_F(TransactionApplicationTest, NotPlacedOnTransactionQueue_Synchronous) {
293 NotPlacedOnTransactionQueue(ISurfaceComposer::eSynchronous, /*syncInputWindows*/ false);
294}
295
296TEST_F(TransactionApplicationTest, NotPlacedOnTransactionQueue_Animation) {
297 NotPlacedOnTransactionQueue(ISurfaceComposer::eAnimation, /*syncInputWindows*/ false);
298}
299
300TEST_F(TransactionApplicationTest, NotPlacedOnTransactionQueue_SyncInputWindows) {
301 NotPlacedOnTransactionQueue(/*flags*/ 0, /*syncInputWindows*/ true);
302}
303
304TEST_F(TransactionApplicationTest, PlaceOnTransactionQueue_Synchronous) {
305 PlaceOnTransactionQueue(ISurfaceComposer::eSynchronous, /*syncInputWindows*/ false);
306}
307
308TEST_F(TransactionApplicationTest, PlaceOnTransactionQueue_Animation) {
309 PlaceOnTransactionQueue(ISurfaceComposer::eAnimation, /*syncInputWindows*/ false);
310}
311
312TEST_F(TransactionApplicationTest, PlaceOnTransactionQueue_SyncInputWindows) {
313 PlaceOnTransactionQueue(/*flags*/ 0, /*syncInputWindows*/ true);
314}
315
316TEST_F(TransactionApplicationTest, BlockWithPriorTransaction_Synchronous) {
317 BlockedByPriorTransaction(ISurfaceComposer::eSynchronous, /*syncInputWindows*/ false);
318}
319
320TEST_F(TransactionApplicationTest, BlockWithPriorTransaction_Animation) {
321 BlockedByPriorTransaction(ISurfaceComposer::eSynchronous, /*syncInputWindows*/ false);
322}
323
324TEST_F(TransactionApplicationTest, BlockWithPriorTransaction_SyncInputWindows) {
325 BlockedByPriorTransaction(/*flags*/ 0, /*syncInputWindows*/ true);
326}
327
Valerie Hau09e60052019-12-15 14:51:15 -0800328TEST_F(TransactionApplicationTest, FromHandle) {
329 sp<IBinder> badHandle;
330 auto ret = mFlinger.fromHandle(badHandle);
Alec Mouri9a02eda2020-04-21 17:39:34 -0700331 EXPECT_EQ(nullptr, ret.promote().get());
Valerie Hau09e60052019-12-15 14:51:15 -0800332}
Valerie Haud251afb2019-03-29 14:19:02 -0700333} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800334
335// TODO(b/129481165): remove the #pragma below and fix conversion issues
336#pragma clang diagnostic pop // ignored "-Wconversion"