Valerie Hau | d251afb | 2019-03-29 14:19:02 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | #undef LOG_TAG |
| 18 | #define LOG_TAG "CompositionTest" |
| 19 | |
| 20 | #include <compositionengine/Display.h> |
| 21 | #include <compositionengine/mock/DisplaySurface.h> |
| 22 | #include <gmock/gmock.h> |
| 23 | #include <gtest/gtest.h> |
| 24 | #include <gui/SurfaceComposerClient.h> |
| 25 | #include <log/log.h> |
| 26 | #include <utils/String8.h> |
| 27 | |
| 28 | #include "TestableScheduler.h" |
| 29 | #include "TestableSurfaceFlinger.h" |
| 30 | #include "mock/MockDispSync.h" |
| 31 | #include "mock/MockEventControlThread.h" |
| 32 | #include "mock/MockEventThread.h" |
| 33 | #include "mock/MockMessageQueue.h" |
| 34 | |
| 35 | namespace android { |
| 36 | |
| 37 | using testing::_; |
| 38 | using testing::Return; |
| 39 | |
| 40 | using FakeHwcDisplayInjector = TestableSurfaceFlinger::FakeHwcDisplayInjector; |
| 41 | |
| 42 | class TransactionApplicationTest : public testing::Test { |
| 43 | public: |
| 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(_, _)) |
| 65 | .WillOnce(Return( |
| 66 | new EventThreadConnection(eventThread.get(), ResyncCallback(), |
| 67 | ISurfaceComposer::eConfigChangedSuppress))); |
| 68 | |
| 69 | EXPECT_CALL(*sfEventThread, registerDisplayEventConnection(_)); |
| 70 | EXPECT_CALL(*sfEventThread, createEventConnection(_, _)) |
| 71 | .WillOnce(Return( |
| 72 | new EventThreadConnection(sfEventThread.get(), ResyncCallback(), |
| 73 | ISurfaceComposer::eConfigChangedSuppress))); |
| 74 | |
| 75 | EXPECT_CALL(*mPrimaryDispSync, computeNextRefresh(0)).WillRepeatedly(Return(0)); |
| 76 | EXPECT_CALL(*mPrimaryDispSync, getPeriod()) |
| 77 | .WillRepeatedly(Return(FakeHwcDisplayInjector::DEFAULT_REFRESH_RATE)); |
| 78 | |
| 79 | mFlinger.setupScheduler(std::unique_ptr<mock::DispSync>(mPrimaryDispSync), |
| 80 | std::make_unique<mock::EventControlThread>(), |
| 81 | std::move(eventThread), std::move(sfEventThread)); |
| 82 | } |
| 83 | |
| 84 | TestableScheduler* mScheduler; |
| 85 | TestableSurfaceFlinger mFlinger; |
| 86 | |
| 87 | std::unique_ptr<mock::EventThread> mEventThread = std::make_unique<mock::EventThread>(); |
| 88 | mock::EventControlThread* mEventControlThread = new mock::EventControlThread(); |
| 89 | |
| 90 | mock::MessageQueue* mMessageQueue = new mock::MessageQueue(); |
| 91 | mock::DispSync* mPrimaryDispSync = new mock::DispSync(); |
| 92 | |
| 93 | struct TransactionInfo { |
| 94 | Vector<ComposerState> states; |
| 95 | Vector<DisplayState> displays; |
| 96 | uint32_t flags = 0; |
| 97 | sp<IBinder> applyToken = IInterface::asBinder(TransactionCompletedListener::getIInstance()); |
| 98 | InputWindowCommands inputWindowCommands; |
| 99 | int64_t desiredPresentTime = -1; |
| 100 | client_cache_t uncacheBuffer; |
| 101 | }; |
| 102 | |
| 103 | void checkEqual(TransactionInfo info, SurfaceFlinger::TransactionState state) { |
| 104 | EXPECT_EQ(0, info.states.size()); |
| 105 | EXPECT_EQ(0, state.states.size()); |
| 106 | |
| 107 | EXPECT_EQ(0, info.displays.size()); |
| 108 | EXPECT_EQ(0, state.displays.size()); |
| 109 | EXPECT_EQ(info.flags, state.flags); |
| 110 | EXPECT_EQ(info.desiredPresentTime, state.desiredPresentTime); |
| 111 | } |
| 112 | |
| 113 | void setupSingle(TransactionInfo& transaction, uint32_t flags, bool syncInputWindows, |
| 114 | int64_t desiredPresentTime) { |
| 115 | mTransactionNumber++; |
| 116 | transaction.flags |= flags; // ISurfaceComposer::eSynchronous; |
| 117 | transaction.inputWindowCommands.syncInputWindows = syncInputWindows; |
| 118 | transaction.desiredPresentTime = desiredPresentTime; |
| 119 | } |
| 120 | |
| 121 | void NotPlacedOnTransactionQueue(uint32_t flags, bool syncInputWindows) { |
| 122 | ASSERT_EQ(0, mFlinger.getTransactionQueue().size()); |
| 123 | // called in SurfaceFlinger::signalTransaction |
| 124 | EXPECT_CALL(*mMessageQueue, invalidate()).Times(1); |
| 125 | EXPECT_CALL(*mPrimaryDispSync, expectedPresentTime()).WillOnce(Return(systemTime())); |
| 126 | TransactionInfo transaction; |
| 127 | setupSingle(transaction, flags, syncInputWindows, |
| 128 | /*desiredPresentTime*/ -1); |
| 129 | nsecs_t applicationTime = systemTime(); |
| 130 | mFlinger.setTransactionState(transaction.states, transaction.displays, transaction.flags, |
| 131 | transaction.applyToken, transaction.inputWindowCommands, |
| 132 | transaction.desiredPresentTime, transaction.uncacheBuffer, |
| 133 | mHasListenerCallbacks, mCallbacks); |
| 134 | |
| 135 | // This transaction should not have been placed on the transaction queue. |
| 136 | // If transaction is synchronous or syncs input windows, SF |
| 137 | // applyTransactionState should time out (5s) wating for SF to commit |
| 138 | // the transaction or to receive a signal that syncInputWindows has |
| 139 | // completed. If this is animation, it should not time out waiting. |
| 140 | nsecs_t returnedTime = systemTime(); |
| 141 | if (flags & ISurfaceComposer::eSynchronous || syncInputWindows) { |
| 142 | EXPECT_GE(returnedTime, applicationTime + s2ns(5)); |
| 143 | } else { |
| 144 | EXPECT_LE(returnedTime, applicationTime + s2ns(5)); |
| 145 | } |
| 146 | auto transactionQueue = mFlinger.getTransactionQueue(); |
| 147 | EXPECT_EQ(0, transactionQueue.size()); |
| 148 | } |
| 149 | |
| 150 | void PlaceOnTransactionQueue(uint32_t flags, bool syncInputWindows) { |
| 151 | ASSERT_EQ(0, mFlinger.getTransactionQueue().size()); |
| 152 | // called in SurfaceFlinger::signalTransaction |
| 153 | EXPECT_CALL(*mMessageQueue, invalidate()).Times(1); |
| 154 | |
| 155 | // first check will see desired present time has not passed, |
| 156 | // but afterwards it will look like the desired present time has passed |
| 157 | nsecs_t time = systemTime(); |
| 158 | EXPECT_CALL(*mPrimaryDispSync, expectedPresentTime()) |
| 159 | .WillOnce(Return(time + nsecs_t(5 * 1e8))); |
| 160 | TransactionInfo transaction; |
| 161 | setupSingle(transaction, flags, syncInputWindows, |
| 162 | /*desiredPresentTime*/ time + s2ns(1)); |
| 163 | nsecs_t applicationSentTime = systemTime(); |
| 164 | mFlinger.setTransactionState(transaction.states, transaction.displays, transaction.flags, |
| 165 | transaction.applyToken, transaction.inputWindowCommands, |
| 166 | transaction.desiredPresentTime, transaction.uncacheBuffer, |
| 167 | mHasListenerCallbacks, mCallbacks); |
| 168 | |
| 169 | nsecs_t returnedTime = systemTime(); |
| 170 | EXPECT_LE(returnedTime, applicationSentTime + s2ns(5)); |
| 171 | // This transaction should have been placed on the transaction queue |
| 172 | auto transactionQueue = mFlinger.getTransactionQueue(); |
| 173 | EXPECT_EQ(1, transactionQueue.size()); |
| 174 | } |
| 175 | |
| 176 | void BlockedByPriorTransaction(uint32_t flags, bool syncInputWindows) { |
| 177 | ASSERT_EQ(0, mFlinger.getTransactionQueue().size()); |
| 178 | // called in SurfaceFlinger::signalTransaction |
| 179 | nsecs_t time = systemTime(); |
| 180 | EXPECT_CALL(*mMessageQueue, invalidate()).Times(1); |
| 181 | EXPECT_CALL(*mPrimaryDispSync, expectedPresentTime()) |
| 182 | .WillOnce(Return(time + nsecs_t(5 * 1e8))); |
| 183 | // transaction that should go on the pending thread |
| 184 | TransactionInfo transactionA; |
| 185 | setupSingle(transactionA, /*flags*/ 0, /*syncInputWindows*/ false, |
| 186 | /*desiredPresentTime*/ time + s2ns(1)); |
| 187 | |
| 188 | // transaction that would not have gone on the pending thread if not |
| 189 | // blocked |
| 190 | TransactionInfo transactionB; |
| 191 | setupSingle(transactionB, flags, syncInputWindows, |
| 192 | /*desiredPresentTime*/ -1); |
| 193 | |
| 194 | nsecs_t applicationSentTime = systemTime(); |
| 195 | mFlinger.setTransactionState(transactionA.states, transactionA.displays, transactionA.flags, |
| 196 | transactionA.applyToken, transactionA.inputWindowCommands, |
| 197 | transactionA.desiredPresentTime, transactionA.uncacheBuffer, |
| 198 | mHasListenerCallbacks, mCallbacks); |
| 199 | |
| 200 | // This thread should not have been blocked by the above transaction |
| 201 | // (5s is the timeout period that applyTransactionState waits for SF to |
| 202 | // commit the transaction) |
| 203 | EXPECT_LE(systemTime(), applicationSentTime + s2ns(5)); |
| 204 | |
| 205 | applicationSentTime = systemTime(); |
| 206 | mFlinger.setTransactionState(transactionB.states, transactionB.displays, transactionB.flags, |
| 207 | transactionB.applyToken, transactionB.inputWindowCommands, |
| 208 | transactionB.desiredPresentTime, transactionB.uncacheBuffer, |
| 209 | mHasListenerCallbacks, mCallbacks); |
| 210 | |
| 211 | // this thread should have been blocked by the above transaction |
| 212 | // if this is an animation, this thread should be blocked for 5s |
| 213 | // in setTransactionState waiting for transactionA to flush. Otherwise, |
| 214 | // the transaction should be placed on the pending queue |
| 215 | if (flags & ISurfaceComposer::eAnimation) { |
| 216 | EXPECT_GE(systemTime(), applicationSentTime + s2ns(5)); |
| 217 | } else { |
| 218 | EXPECT_LE(systemTime(), applicationSentTime + s2ns(5)); |
| 219 | } |
| 220 | |
| 221 | // check that there is one binder on the pending queue. |
| 222 | auto transactionQueue = mFlinger.getTransactionQueue(); |
| 223 | EXPECT_EQ(1, transactionQueue.size()); |
| 224 | |
| 225 | auto& [applyToken, transactionStates] = *(transactionQueue.begin()); |
| 226 | EXPECT_EQ(2, transactionStates.size()); |
| 227 | |
| 228 | auto& transactionStateA = transactionStates.front(); |
| 229 | transactionStates.pop(); |
| 230 | checkEqual(transactionA, transactionStateA); |
| 231 | auto& transactionStateB = transactionStates.front(); |
| 232 | checkEqual(transactionB, transactionStateB); |
| 233 | } |
| 234 | |
| 235 | bool mHasListenerCallbacks = false; |
| 236 | std::vector<ListenerCallbacks> mCallbacks; |
| 237 | int mTransactionNumber = 0; |
| 238 | }; |
| 239 | |
| 240 | TEST_F(TransactionApplicationTest, Flush_RemovesFromQueue) { |
| 241 | ASSERT_EQ(0, mFlinger.getTransactionQueue().size()); |
| 242 | // called in SurfaceFlinger::signalTransaction |
| 243 | EXPECT_CALL(*mMessageQueue, invalidate()).Times(1); |
| 244 | |
| 245 | // nsecs_t time = systemTime(); |
| 246 | EXPECT_CALL(*mPrimaryDispSync, expectedPresentTime()) |
| 247 | .WillOnce(Return(nsecs_t(5 * 1e8))) |
| 248 | .WillOnce(Return(s2ns(2))); |
| 249 | TransactionInfo transactionA; // transaction to go on pending queue |
| 250 | setupSingle(transactionA, /*flags*/ 0, /*syncInputWindows*/ false, |
| 251 | /*desiredPresentTime*/ s2ns(1)); |
| 252 | mFlinger.setTransactionState(transactionA.states, transactionA.displays, transactionA.flags, |
| 253 | transactionA.applyToken, transactionA.inputWindowCommands, |
| 254 | transactionA.desiredPresentTime, transactionA.uncacheBuffer, |
| 255 | mHasListenerCallbacks, mCallbacks); |
| 256 | |
| 257 | auto& transactionQueue = mFlinger.getTransactionQueue(); |
| 258 | ASSERT_EQ(1, transactionQueue.size()); |
| 259 | |
| 260 | auto& [applyToken, transactionStates] = *(transactionQueue.begin()); |
| 261 | ASSERT_EQ(1, transactionStates.size()); |
| 262 | |
| 263 | auto& transactionState = transactionStates.front(); |
| 264 | checkEqual(transactionA, transactionState); |
| 265 | |
| 266 | // because flushing uses the cached expected present time, we send an empty |
| 267 | // transaction here (sending a null applyToken to fake it as from a |
| 268 | // different process) to re-query and reset the cached expected present time |
| 269 | TransactionInfo empty; |
| 270 | empty.applyToken = sp<IBinder>(); |
| 271 | mFlinger.setTransactionState(empty.states, empty.displays, empty.flags, empty.applyToken, |
| 272 | empty.inputWindowCommands, empty.desiredPresentTime, |
| 273 | empty.uncacheBuffer, mHasListenerCallbacks, mCallbacks); |
| 274 | |
| 275 | // flush transaction queue should flush as desiredPresentTime has |
| 276 | // passed |
| 277 | mFlinger.flushTransactionQueues(); |
| 278 | |
| 279 | EXPECT_EQ(0, transactionQueue.size()); |
| 280 | } |
| 281 | |
| 282 | TEST_F(TransactionApplicationTest, NotPlacedOnTransactionQueue_Synchronous) { |
| 283 | NotPlacedOnTransactionQueue(ISurfaceComposer::eSynchronous, /*syncInputWindows*/ false); |
| 284 | } |
| 285 | |
| 286 | TEST_F(TransactionApplicationTest, NotPlacedOnTransactionQueue_Animation) { |
| 287 | NotPlacedOnTransactionQueue(ISurfaceComposer::eAnimation, /*syncInputWindows*/ false); |
| 288 | } |
| 289 | |
| 290 | TEST_F(TransactionApplicationTest, NotPlacedOnTransactionQueue_SyncInputWindows) { |
| 291 | NotPlacedOnTransactionQueue(/*flags*/ 0, /*syncInputWindows*/ true); |
| 292 | } |
| 293 | |
| 294 | TEST_F(TransactionApplicationTest, PlaceOnTransactionQueue_Synchronous) { |
| 295 | PlaceOnTransactionQueue(ISurfaceComposer::eSynchronous, /*syncInputWindows*/ false); |
| 296 | } |
| 297 | |
| 298 | TEST_F(TransactionApplicationTest, PlaceOnTransactionQueue_Animation) { |
| 299 | PlaceOnTransactionQueue(ISurfaceComposer::eAnimation, /*syncInputWindows*/ false); |
| 300 | } |
| 301 | |
| 302 | TEST_F(TransactionApplicationTest, PlaceOnTransactionQueue_SyncInputWindows) { |
| 303 | PlaceOnTransactionQueue(/*flags*/ 0, /*syncInputWindows*/ true); |
| 304 | } |
| 305 | |
| 306 | TEST_F(TransactionApplicationTest, BlockWithPriorTransaction_Synchronous) { |
| 307 | BlockedByPriorTransaction(ISurfaceComposer::eSynchronous, /*syncInputWindows*/ false); |
| 308 | } |
| 309 | |
| 310 | TEST_F(TransactionApplicationTest, BlockWithPriorTransaction_Animation) { |
| 311 | BlockedByPriorTransaction(ISurfaceComposer::eSynchronous, /*syncInputWindows*/ false); |
| 312 | } |
| 313 | |
| 314 | TEST_F(TransactionApplicationTest, BlockWithPriorTransaction_SyncInputWindows) { |
| 315 | BlockedByPriorTransaction(/*flags*/ 0, /*syncInputWindows*/ true); |
| 316 | } |
| 317 | |
| 318 | } // namespace android |