blob: 760bf65feec6d302f87a98d6e8484fcb34401361 [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(
69 new EventThreadConnection(eventThread.get(), ResyncCallback(),
70 ISurfaceComposer::eConfigChangedSuppress)));
71
72 EXPECT_CALL(*sfEventThread, registerDisplayEventConnection(_));
73 EXPECT_CALL(*sfEventThread, createEventConnection(_, _))
74 .WillOnce(Return(
75 new EventThreadConnection(sfEventThread.get(), ResyncCallback(),
76 ISurfaceComposer::eConfigChangedSuppress)));
77
Ady Abraham8cb21882020-08-26 18:22:05 -070078 EXPECT_CALL(*mVSyncTracker, nextAnticipatedVSyncTimeFrom(_)).WillRepeatedly(Return(0));
79 EXPECT_CALL(*mVSyncTracker, currentPeriod())
Valerie Haud251afb2019-03-29 14:19:02 -070080 .WillRepeatedly(Return(FakeHwcDisplayInjector::DEFAULT_REFRESH_RATE));
81
Ady Abraham8cb21882020-08-26 18:22:05 -070082 mFlinger.setupScheduler(std::unique_ptr<mock::VsyncController>(mVsyncController),
83 std::unique_ptr<mock::VSyncTracker>(mVSyncTracker),
Valerie Haud251afb2019-03-29 14:19:02 -070084 std::move(eventThread), std::move(sfEventThread));
85 }
86
87 TestableScheduler* mScheduler;
88 TestableSurfaceFlinger mFlinger;
89
90 std::unique_ptr<mock::EventThread> mEventThread = std::make_unique<mock::EventThread>();
Valerie Haud251afb2019-03-29 14:19:02 -070091
92 mock::MessageQueue* mMessageQueue = new mock::MessageQueue();
Ady Abraham8cb21882020-08-26 18:22:05 -070093 mock::VsyncController* mVsyncController = new mock::VsyncController();
94 mock::VSyncTracker* mVSyncTracker = new mock::VSyncTracker();
Valerie Haud251afb2019-03-29 14:19:02 -070095
96 struct TransactionInfo {
97 Vector<ComposerState> states;
98 Vector<DisplayState> displays;
99 uint32_t flags = 0;
100 sp<IBinder> applyToken = IInterface::asBinder(TransactionCompletedListener::getIInstance());
101 InputWindowCommands inputWindowCommands;
102 int64_t desiredPresentTime = -1;
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 Abraham22c7b5c2020-09-22 19:33:40 -0700119 int64_t desiredPresentTime, 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 Abraham22c7b5c2020-09-22 19:33:40 -0700124 transaction.frameTimelineVsyncId = frameTimelineVsyncId;
Valerie Haud251afb2019-03-29 14:19:02 -0700125 }
126
127 void NotPlacedOnTransactionQueue(uint32_t flags, bool syncInputWindows) {
Arthur Hung1bedccb2020-09-24 10:09:25 +0000128 ASSERT_EQ(0, mFlinger.getTransactionQueue().size());
Valerie Haud251afb2019-03-29 14:19:02 -0700129 // called in SurfaceFlinger::signalTransaction
130 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
Ady Abraham8cb21882020-08-26 18:22:05 -0700131 EXPECT_CALL(*mVSyncTracker, nextAnticipatedVSyncTimeFrom(_)).WillOnce(Return(systemTime()));
Valerie Haud251afb2019-03-29 14:19:02 -0700132 TransactionInfo transaction;
133 setupSingle(transaction, flags, syncInputWindows,
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700134 /*desiredPresentTime*/ -1, ISurfaceComposer::INVALID_VSYNC_ID);
Valerie Haud251afb2019-03-29 14:19:02 -0700135 nsecs_t applicationTime = systemTime();
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700136 mFlinger.setTransactionState(transaction.frameTimelineVsyncId, transaction.states,
137 transaction.displays, transaction.flags,
Valerie Haud251afb2019-03-29 14:19:02 -0700138 transaction.applyToken, transaction.inputWindowCommands,
139 transaction.desiredPresentTime, transaction.uncacheBuffer,
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000140 mHasListenerCallbacks, mCallbacks, transaction.id);
Valerie Haud251afb2019-03-29 14:19:02 -0700141
142 // This transaction should not have been placed on the transaction queue.
143 // If transaction is synchronous or syncs input windows, SF
144 // applyTransactionState should time out (5s) wating for SF to commit
145 // the transaction or to receive a signal that syncInputWindows has
146 // completed. If this is animation, it should not time out waiting.
147 nsecs_t returnedTime = systemTime();
148 if (flags & ISurfaceComposer::eSynchronous || syncInputWindows) {
149 EXPECT_GE(returnedTime, applicationTime + s2ns(5));
150 } else {
151 EXPECT_LE(returnedTime, applicationTime + s2ns(5));
152 }
Arthur Hung1bedccb2020-09-24 10:09:25 +0000153 auto transactionQueue = mFlinger.getTransactionQueue();
Valerie Haud251afb2019-03-29 14:19:02 -0700154 EXPECT_EQ(0, transactionQueue.size());
155 }
156
157 void PlaceOnTransactionQueue(uint32_t flags, bool syncInputWindows) {
Arthur Hung1bedccb2020-09-24 10:09:25 +0000158 ASSERT_EQ(0, mFlinger.getTransactionQueue().size());
Valerie Haud251afb2019-03-29 14:19:02 -0700159 // called in SurfaceFlinger::signalTransaction
160 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
161
162 // first check will see desired present time has not passed,
163 // but afterwards it will look like the desired present time has passed
164 nsecs_t time = systemTime();
Ady Abraham8cb21882020-08-26 18:22:05 -0700165 EXPECT_CALL(*mVSyncTracker, nextAnticipatedVSyncTimeFrom(_))
Valerie Haud251afb2019-03-29 14:19:02 -0700166 .WillOnce(Return(time + nsecs_t(5 * 1e8)));
167 TransactionInfo transaction;
168 setupSingle(transaction, flags, syncInputWindows,
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700169 /*desiredPresentTime*/ time + s2ns(1), ISurfaceComposer::INVALID_VSYNC_ID);
Valerie Haud251afb2019-03-29 14:19:02 -0700170 nsecs_t applicationSentTime = systemTime();
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700171 mFlinger.setTransactionState(transaction.frameTimelineVsyncId, transaction.states,
172 transaction.displays, transaction.flags,
Valerie Haud251afb2019-03-29 14:19:02 -0700173 transaction.applyToken, transaction.inputWindowCommands,
174 transaction.desiredPresentTime, transaction.uncacheBuffer,
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000175 mHasListenerCallbacks, mCallbacks, transaction.id);
Valerie Haud251afb2019-03-29 14:19:02 -0700176
177 nsecs_t returnedTime = systemTime();
178 EXPECT_LE(returnedTime, applicationSentTime + s2ns(5));
179 // This transaction should have been placed on the transaction queue
Arthur Hung1bedccb2020-09-24 10:09:25 +0000180 auto transactionQueue = mFlinger.getTransactionQueue();
Valerie Haud251afb2019-03-29 14:19:02 -0700181 EXPECT_EQ(1, transactionQueue.size());
182 }
183
184 void BlockedByPriorTransaction(uint32_t flags, bool syncInputWindows) {
Arthur Hung1bedccb2020-09-24 10:09:25 +0000185 ASSERT_EQ(0, mFlinger.getTransactionQueue().size());
Valerie Haud251afb2019-03-29 14:19:02 -0700186 // called in SurfaceFlinger::signalTransaction
187 nsecs_t time = systemTime();
188 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
Ady Abraham8cb21882020-08-26 18:22:05 -0700189 EXPECT_CALL(*mVSyncTracker, nextAnticipatedVSyncTimeFrom(_))
Valerie Haud251afb2019-03-29 14:19:02 -0700190 .WillOnce(Return(time + nsecs_t(5 * 1e8)));
191 // transaction that should go on the pending thread
192 TransactionInfo transactionA;
193 setupSingle(transactionA, /*flags*/ 0, /*syncInputWindows*/ false,
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700194 /*desiredPresentTime*/ time + s2ns(1), ISurfaceComposer::INVALID_VSYNC_ID);
Valerie Haud251afb2019-03-29 14:19:02 -0700195
196 // transaction that would not have gone on the pending thread if not
197 // blocked
198 TransactionInfo transactionB;
199 setupSingle(transactionB, flags, syncInputWindows,
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700200 /*desiredPresentTime*/ -1, ISurfaceComposer::INVALID_VSYNC_ID);
Valerie Haud251afb2019-03-29 14:19:02 -0700201
202 nsecs_t applicationSentTime = systemTime();
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700203 mFlinger.setTransactionState(transactionA.frameTimelineVsyncId, transactionA.states,
204 transactionA.displays, transactionA.flags,
Valerie Haud251afb2019-03-29 14:19:02 -0700205 transactionA.applyToken, transactionA.inputWindowCommands,
206 transactionA.desiredPresentTime, transactionA.uncacheBuffer,
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000207 mHasListenerCallbacks, mCallbacks, transactionA.id);
Valerie Haud251afb2019-03-29 14:19:02 -0700208
209 // This thread should not have been blocked by the above transaction
210 // (5s is the timeout period that applyTransactionState waits for SF to
211 // commit the transaction)
212 EXPECT_LE(systemTime(), applicationSentTime + s2ns(5));
213
214 applicationSentTime = systemTime();
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700215 mFlinger.setTransactionState(transactionB.frameTimelineVsyncId, transactionB.states,
216 transactionB.displays, transactionB.flags,
Valerie Haud251afb2019-03-29 14:19:02 -0700217 transactionB.applyToken, transactionB.inputWindowCommands,
218 transactionB.desiredPresentTime, transactionB.uncacheBuffer,
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000219 mHasListenerCallbacks, mCallbacks, transactionB.id);
Valerie Haud251afb2019-03-29 14:19:02 -0700220
221 // this thread should have been blocked by the above transaction
222 // if this is an animation, this thread should be blocked for 5s
223 // in setTransactionState waiting for transactionA to flush. Otherwise,
224 // the transaction should be placed on the pending queue
225 if (flags & ISurfaceComposer::eAnimation) {
226 EXPECT_GE(systemTime(), applicationSentTime + s2ns(5));
227 } else {
228 EXPECT_LE(systemTime(), applicationSentTime + s2ns(5));
229 }
230
231 // check that there is one binder on the pending queue.
Arthur Hung1bedccb2020-09-24 10:09:25 +0000232 auto transactionQueue = mFlinger.getTransactionQueue();
Valerie Haud251afb2019-03-29 14:19:02 -0700233 EXPECT_EQ(1, transactionQueue.size());
234
235 auto& [applyToken, transactionStates] = *(transactionQueue.begin());
236 EXPECT_EQ(2, transactionStates.size());
237
238 auto& transactionStateA = transactionStates.front();
239 transactionStates.pop();
240 checkEqual(transactionA, transactionStateA);
241 auto& transactionStateB = transactionStates.front();
242 checkEqual(transactionB, transactionStateB);
243 }
244
245 bool mHasListenerCallbacks = false;
246 std::vector<ListenerCallbacks> mCallbacks;
247 int mTransactionNumber = 0;
248};
249
250TEST_F(TransactionApplicationTest, Flush_RemovesFromQueue) {
Arthur Hung1bedccb2020-09-24 10:09:25 +0000251 ASSERT_EQ(0, mFlinger.getTransactionQueue().size());
Valerie Haud251afb2019-03-29 14:19:02 -0700252 // called in SurfaceFlinger::signalTransaction
253 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
254
255 // nsecs_t time = systemTime();
Ady Abraham8cb21882020-08-26 18:22:05 -0700256 EXPECT_CALL(*mVSyncTracker, nextAnticipatedVSyncTimeFrom(_))
Valerie Haud251afb2019-03-29 14:19:02 -0700257 .WillOnce(Return(nsecs_t(5 * 1e8)))
258 .WillOnce(Return(s2ns(2)));
259 TransactionInfo transactionA; // transaction to go on pending queue
260 setupSingle(transactionA, /*flags*/ 0, /*syncInputWindows*/ false,
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700261 /*desiredPresentTime*/ s2ns(1), ISurfaceComposer::INVALID_VSYNC_ID);
262 mFlinger.setTransactionState(transactionA.frameTimelineVsyncId, transactionA.states,
263 transactionA.displays, transactionA.flags, transactionA.applyToken,
264 transactionA.inputWindowCommands, transactionA.desiredPresentTime,
265 transactionA.uncacheBuffer, mHasListenerCallbacks, mCallbacks,
266 transactionA.id);
Valerie Haud251afb2019-03-29 14:19:02 -0700267
Arthur Hung1bedccb2020-09-24 10:09:25 +0000268 auto& transactionQueue = mFlinger.getTransactionQueue();
Valerie Haud251afb2019-03-29 14:19:02 -0700269 ASSERT_EQ(1, transactionQueue.size());
270
271 auto& [applyToken, transactionStates] = *(transactionQueue.begin());
272 ASSERT_EQ(1, transactionStates.size());
273
274 auto& transactionState = transactionStates.front();
275 checkEqual(transactionA, transactionState);
276
277 // because flushing uses the cached expected present time, we send an empty
278 // transaction here (sending a null applyToken to fake it as from a
279 // different process) to re-query and reset the cached expected present time
280 TransactionInfo empty;
281 empty.applyToken = sp<IBinder>();
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700282 mFlinger.setTransactionState(empty.frameTimelineVsyncId, empty.states, empty.displays,
283 empty.flags, empty.applyToken, empty.inputWindowCommands,
284 empty.desiredPresentTime, empty.uncacheBuffer,
285 mHasListenerCallbacks, mCallbacks, empty.id);
Valerie Haud251afb2019-03-29 14:19:02 -0700286
Arthur Hung1bedccb2020-09-24 10:09:25 +0000287 // flush transaction queue should flush as desiredPresentTime has
Valerie Haud251afb2019-03-29 14:19:02 -0700288 // passed
Arthur Hung1bedccb2020-09-24 10:09:25 +0000289 mFlinger.flushTransactionQueues();
Valerie Haud251afb2019-03-29 14:19:02 -0700290
291 EXPECT_EQ(0, transactionQueue.size());
292}
293
294TEST_F(TransactionApplicationTest, NotPlacedOnTransactionQueue_Synchronous) {
295 NotPlacedOnTransactionQueue(ISurfaceComposer::eSynchronous, /*syncInputWindows*/ false);
296}
297
298TEST_F(TransactionApplicationTest, NotPlacedOnTransactionQueue_Animation) {
299 NotPlacedOnTransactionQueue(ISurfaceComposer::eAnimation, /*syncInputWindows*/ false);
300}
301
302TEST_F(TransactionApplicationTest, NotPlacedOnTransactionQueue_SyncInputWindows) {
303 NotPlacedOnTransactionQueue(/*flags*/ 0, /*syncInputWindows*/ true);
304}
305
306TEST_F(TransactionApplicationTest, PlaceOnTransactionQueue_Synchronous) {
307 PlaceOnTransactionQueue(ISurfaceComposer::eSynchronous, /*syncInputWindows*/ false);
308}
309
310TEST_F(TransactionApplicationTest, PlaceOnTransactionQueue_Animation) {
311 PlaceOnTransactionQueue(ISurfaceComposer::eAnimation, /*syncInputWindows*/ false);
312}
313
314TEST_F(TransactionApplicationTest, PlaceOnTransactionQueue_SyncInputWindows) {
315 PlaceOnTransactionQueue(/*flags*/ 0, /*syncInputWindows*/ true);
316}
317
318TEST_F(TransactionApplicationTest, BlockWithPriorTransaction_Synchronous) {
319 BlockedByPriorTransaction(ISurfaceComposer::eSynchronous, /*syncInputWindows*/ false);
320}
321
322TEST_F(TransactionApplicationTest, BlockWithPriorTransaction_Animation) {
323 BlockedByPriorTransaction(ISurfaceComposer::eSynchronous, /*syncInputWindows*/ false);
324}
325
326TEST_F(TransactionApplicationTest, BlockWithPriorTransaction_SyncInputWindows) {
327 BlockedByPriorTransaction(/*flags*/ 0, /*syncInputWindows*/ true);
328}
329
Valerie Hau09e60052019-12-15 14:51:15 -0800330TEST_F(TransactionApplicationTest, FromHandle) {
331 sp<IBinder> badHandle;
332 auto ret = mFlinger.fromHandle(badHandle);
Alec Mouri9a02eda2020-04-21 17:39:34 -0700333 EXPECT_EQ(nullptr, ret.promote().get());
Valerie Hau09e60052019-12-15 14:51:15 -0800334}
Valerie Haud251afb2019-03-29 14:19:02 -0700335} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800336
337// TODO(b/129481165): remove the #pragma below and fix conversion issues
338#pragma clang diagnostic pop // ignored "-Wconversion"