blob: 44b3dc0ea16e8f0fe7e9670d18f7c489748b9f6b [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"
34#include "mock/MockDispSync.h"
Valerie Haud251afb2019-03-29 14:19:02 -070035#include "mock/MockEventThread.h"
36#include "mock/MockMessageQueue.h"
37
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 Abraham0ed31c92020-04-16 11:48:45 -070078 EXPECT_CALL(*mPrimaryDispSync, computeNextRefresh(0, _)).WillRepeatedly(Return(0));
Valerie Haud251afb2019-03-29 14:19:02 -070079 EXPECT_CALL(*mPrimaryDispSync, getPeriod())
80 .WillRepeatedly(Return(FakeHwcDisplayInjector::DEFAULT_REFRESH_RATE));
81
82 mFlinger.setupScheduler(std::unique_ptr<mock::DispSync>(mPrimaryDispSync),
Valerie Haud251afb2019-03-29 14:19:02 -070083 std::move(eventThread), std::move(sfEventThread));
84 }
85
86 TestableScheduler* mScheduler;
87 TestableSurfaceFlinger mFlinger;
88
89 std::unique_ptr<mock::EventThread> mEventThread = std::make_unique<mock::EventThread>();
Valerie Haud251afb2019-03-29 14:19:02 -070090
91 mock::MessageQueue* mMessageQueue = new mock::MessageQueue();
92 mock::DispSync* mPrimaryDispSync = new mock::DispSync();
93
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;
101 client_cache_t uncacheBuffer;
102 };
103
104 void checkEqual(TransactionInfo info, SurfaceFlinger::TransactionState state) {
105 EXPECT_EQ(0, info.states.size());
106 EXPECT_EQ(0, state.states.size());
107
108 EXPECT_EQ(0, info.displays.size());
109 EXPECT_EQ(0, state.displays.size());
110 EXPECT_EQ(info.flags, state.flags);
111 EXPECT_EQ(info.desiredPresentTime, state.desiredPresentTime);
112 }
113
114 void setupSingle(TransactionInfo& transaction, uint32_t flags, bool syncInputWindows,
115 int64_t desiredPresentTime) {
116 mTransactionNumber++;
117 transaction.flags |= flags; // ISurfaceComposer::eSynchronous;
118 transaction.inputWindowCommands.syncInputWindows = syncInputWindows;
119 transaction.desiredPresentTime = desiredPresentTime;
120 }
121
122 void NotPlacedOnTransactionQueue(uint32_t flags, bool syncInputWindows) {
123 ASSERT_EQ(0, mFlinger.getTransactionQueue().size());
124 // called in SurfaceFlinger::signalTransaction
125 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
Ady Abraham0ed31c92020-04-16 11:48:45 -0700126 EXPECT_CALL(*mPrimaryDispSync, expectedPresentTime(_)).WillOnce(Return(systemTime()));
Valerie Haud251afb2019-03-29 14:19:02 -0700127 TransactionInfo transaction;
128 setupSingle(transaction, flags, syncInputWindows,
129 /*desiredPresentTime*/ -1);
130 nsecs_t applicationTime = systemTime();
131 mFlinger.setTransactionState(transaction.states, transaction.displays, transaction.flags,
132 transaction.applyToken, transaction.inputWindowCommands,
133 transaction.desiredPresentTime, transaction.uncacheBuffer,
134 mHasListenerCallbacks, mCallbacks);
135
136 // This transaction should not have been placed on the transaction queue.
137 // If transaction is synchronous or syncs input windows, SF
138 // applyTransactionState should time out (5s) wating for SF to commit
139 // the transaction or to receive a signal that syncInputWindows has
140 // completed. If this is animation, it should not time out waiting.
141 nsecs_t returnedTime = systemTime();
142 if (flags & ISurfaceComposer::eSynchronous || syncInputWindows) {
143 EXPECT_GE(returnedTime, applicationTime + s2ns(5));
144 } else {
145 EXPECT_LE(returnedTime, applicationTime + s2ns(5));
146 }
147 auto transactionQueue = mFlinger.getTransactionQueue();
148 EXPECT_EQ(0, transactionQueue.size());
149 }
150
151 void PlaceOnTransactionQueue(uint32_t flags, bool syncInputWindows) {
152 ASSERT_EQ(0, mFlinger.getTransactionQueue().size());
153 // called in SurfaceFlinger::signalTransaction
154 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
155
156 // first check will see desired present time has not passed,
157 // but afterwards it will look like the desired present time has passed
158 nsecs_t time = systemTime();
Ady Abraham0ed31c92020-04-16 11:48:45 -0700159 EXPECT_CALL(*mPrimaryDispSync, expectedPresentTime(_))
Valerie Haud251afb2019-03-29 14:19:02 -0700160 .WillOnce(Return(time + nsecs_t(5 * 1e8)));
161 TransactionInfo transaction;
162 setupSingle(transaction, flags, syncInputWindows,
163 /*desiredPresentTime*/ time + s2ns(1));
164 nsecs_t applicationSentTime = systemTime();
165 mFlinger.setTransactionState(transaction.states, transaction.displays, transaction.flags,
166 transaction.applyToken, transaction.inputWindowCommands,
167 transaction.desiredPresentTime, transaction.uncacheBuffer,
168 mHasListenerCallbacks, mCallbacks);
169
170 nsecs_t returnedTime = systemTime();
171 EXPECT_LE(returnedTime, applicationSentTime + s2ns(5));
172 // This transaction should have been placed on the transaction queue
173 auto transactionQueue = mFlinger.getTransactionQueue();
174 EXPECT_EQ(1, transactionQueue.size());
175 }
176
177 void BlockedByPriorTransaction(uint32_t flags, bool syncInputWindows) {
178 ASSERT_EQ(0, mFlinger.getTransactionQueue().size());
179 // called in SurfaceFlinger::signalTransaction
180 nsecs_t time = systemTime();
181 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
Ady Abraham0ed31c92020-04-16 11:48:45 -0700182 EXPECT_CALL(*mPrimaryDispSync, expectedPresentTime(_))
Valerie Haud251afb2019-03-29 14:19:02 -0700183 .WillOnce(Return(time + nsecs_t(5 * 1e8)));
184 // transaction that should go on the pending thread
185 TransactionInfo transactionA;
186 setupSingle(transactionA, /*flags*/ 0, /*syncInputWindows*/ false,
187 /*desiredPresentTime*/ time + s2ns(1));
188
189 // transaction that would not have gone on the pending thread if not
190 // blocked
191 TransactionInfo transactionB;
192 setupSingle(transactionB, flags, syncInputWindows,
193 /*desiredPresentTime*/ -1);
194
195 nsecs_t applicationSentTime = systemTime();
196 mFlinger.setTransactionState(transactionA.states, transactionA.displays, transactionA.flags,
197 transactionA.applyToken, transactionA.inputWindowCommands,
198 transactionA.desiredPresentTime, transactionA.uncacheBuffer,
199 mHasListenerCallbacks, mCallbacks);
200
201 // This thread should not have been blocked by the above transaction
202 // (5s is the timeout period that applyTransactionState waits for SF to
203 // commit the transaction)
204 EXPECT_LE(systemTime(), applicationSentTime + s2ns(5));
205
206 applicationSentTime = systemTime();
207 mFlinger.setTransactionState(transactionB.states, transactionB.displays, transactionB.flags,
208 transactionB.applyToken, transactionB.inputWindowCommands,
209 transactionB.desiredPresentTime, transactionB.uncacheBuffer,
210 mHasListenerCallbacks, mCallbacks);
211
212 // this thread should have been blocked by the above transaction
213 // if this is an animation, this thread should be blocked for 5s
214 // in setTransactionState waiting for transactionA to flush. Otherwise,
215 // the transaction should be placed on the pending queue
216 if (flags & ISurfaceComposer::eAnimation) {
217 EXPECT_GE(systemTime(), applicationSentTime + s2ns(5));
218 } else {
219 EXPECT_LE(systemTime(), applicationSentTime + s2ns(5));
220 }
221
222 // check that there is one binder on the pending queue.
223 auto transactionQueue = mFlinger.getTransactionQueue();
224 EXPECT_EQ(1, transactionQueue.size());
225
226 auto& [applyToken, transactionStates] = *(transactionQueue.begin());
227 EXPECT_EQ(2, transactionStates.size());
228
229 auto& transactionStateA = transactionStates.front();
230 transactionStates.pop();
231 checkEqual(transactionA, transactionStateA);
232 auto& transactionStateB = transactionStates.front();
233 checkEqual(transactionB, transactionStateB);
234 }
235
236 bool mHasListenerCallbacks = false;
237 std::vector<ListenerCallbacks> mCallbacks;
238 int mTransactionNumber = 0;
239};
240
241TEST_F(TransactionApplicationTest, Flush_RemovesFromQueue) {
242 ASSERT_EQ(0, mFlinger.getTransactionQueue().size());
243 // called in SurfaceFlinger::signalTransaction
244 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
245
246 // nsecs_t time = systemTime();
Ady Abraham0ed31c92020-04-16 11:48:45 -0700247 EXPECT_CALL(*mPrimaryDispSync, expectedPresentTime(_))
Valerie Haud251afb2019-03-29 14:19:02 -0700248 .WillOnce(Return(nsecs_t(5 * 1e8)))
249 .WillOnce(Return(s2ns(2)));
250 TransactionInfo transactionA; // transaction to go on pending queue
251 setupSingle(transactionA, /*flags*/ 0, /*syncInputWindows*/ false,
252 /*desiredPresentTime*/ s2ns(1));
253 mFlinger.setTransactionState(transactionA.states, transactionA.displays, transactionA.flags,
254 transactionA.applyToken, transactionA.inputWindowCommands,
255 transactionA.desiredPresentTime, transactionA.uncacheBuffer,
256 mHasListenerCallbacks, mCallbacks);
257
258 auto& transactionQueue = mFlinger.getTransactionQueue();
259 ASSERT_EQ(1, transactionQueue.size());
260
261 auto& [applyToken, transactionStates] = *(transactionQueue.begin());
262 ASSERT_EQ(1, transactionStates.size());
263
264 auto& transactionState = transactionStates.front();
265 checkEqual(transactionA, transactionState);
266
267 // because flushing uses the cached expected present time, we send an empty
268 // transaction here (sending a null applyToken to fake it as from a
269 // different process) to re-query and reset the cached expected present time
270 TransactionInfo empty;
271 empty.applyToken = sp<IBinder>();
272 mFlinger.setTransactionState(empty.states, empty.displays, empty.flags, empty.applyToken,
273 empty.inputWindowCommands, empty.desiredPresentTime,
274 empty.uncacheBuffer, mHasListenerCallbacks, mCallbacks);
275
276 // flush transaction queue should flush as desiredPresentTime has
277 // passed
278 mFlinger.flushTransactionQueues();
279
280 EXPECT_EQ(0, transactionQueue.size());
281}
282
283TEST_F(TransactionApplicationTest, NotPlacedOnTransactionQueue_Synchronous) {
284 NotPlacedOnTransactionQueue(ISurfaceComposer::eSynchronous, /*syncInputWindows*/ false);
285}
286
287TEST_F(TransactionApplicationTest, NotPlacedOnTransactionQueue_Animation) {
288 NotPlacedOnTransactionQueue(ISurfaceComposer::eAnimation, /*syncInputWindows*/ false);
289}
290
291TEST_F(TransactionApplicationTest, NotPlacedOnTransactionQueue_SyncInputWindows) {
292 NotPlacedOnTransactionQueue(/*flags*/ 0, /*syncInputWindows*/ true);
293}
294
295TEST_F(TransactionApplicationTest, PlaceOnTransactionQueue_Synchronous) {
296 PlaceOnTransactionQueue(ISurfaceComposer::eSynchronous, /*syncInputWindows*/ false);
297}
298
299TEST_F(TransactionApplicationTest, PlaceOnTransactionQueue_Animation) {
300 PlaceOnTransactionQueue(ISurfaceComposer::eAnimation, /*syncInputWindows*/ false);
301}
302
303TEST_F(TransactionApplicationTest, PlaceOnTransactionQueue_SyncInputWindows) {
304 PlaceOnTransactionQueue(/*flags*/ 0, /*syncInputWindows*/ true);
305}
306
307TEST_F(TransactionApplicationTest, BlockWithPriorTransaction_Synchronous) {
308 BlockedByPriorTransaction(ISurfaceComposer::eSynchronous, /*syncInputWindows*/ false);
309}
310
311TEST_F(TransactionApplicationTest, BlockWithPriorTransaction_Animation) {
312 BlockedByPriorTransaction(ISurfaceComposer::eSynchronous, /*syncInputWindows*/ false);
313}
314
315TEST_F(TransactionApplicationTest, BlockWithPriorTransaction_SyncInputWindows) {
316 BlockedByPriorTransaction(/*flags*/ 0, /*syncInputWindows*/ true);
317}
318
Valerie Hau09e60052019-12-15 14:51:15 -0800319TEST_F(TransactionApplicationTest, FromHandle) {
320 sp<IBinder> badHandle;
321 auto ret = mFlinger.fromHandle(badHandle);
Alec Mouri9a02eda2020-04-21 17:39:34 -0700322 EXPECT_EQ(nullptr, ret.promote().get());
Valerie Hau09e60052019-12-15 14:51:15 -0800323}
Valerie Haud251afb2019-03-29 14:19:02 -0700324} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800325
326// TODO(b/129481165): remove the #pragma below and fix conversion issues
327#pragma clang diagnostic pop // ignored "-Wconversion"