blob: a28d1cd415239edc890b5c2847a473a341315f5d [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
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>
Vishnu Nair59f6d2d2022-10-05 16:59:56 -070024#include <gui/LayerState.h>
Valerie Haud251afb2019-03-29 14:19:02 -070025#include <gui/SurfaceComposerClient.h>
Vishnu Nair59f6d2d2022-10-05 16:59:56 -070026#include <gui/fake/BufferData.h>
Valerie Haud251afb2019-03-29 14:19:02 -070027#include <log/log.h>
ramindani4d48f902021-09-20 21:07:45 +000028#include <ui/MockFence.h>
Valerie Haud251afb2019-03-29 14:19:02 -070029#include <utils/String8.h>
Vishnu Nair59f6d2d2022-10-05 16:59:56 -070030#include <vector>
31#include <binder/Binder.h>
Dominik Laskowski068173d2021-08-11 17:22:59 -070032
Vishnu Naira61e4fb2022-10-18 18:29:37 +000033#include "FrontEnd/TransactionHandler.h"
Valerie Haud251afb2019-03-29 14:19:02 -070034#include "TestableSurfaceFlinger.h"
Vishnu Nair40fff5c2022-11-04 02:46:28 +000035#include "TransactionState.h"
Valerie Haud251afb2019-03-29 14:19:02 -070036#include "mock/MockEventThread.h"
Ady Abraham8cb21882020-08-26 18:22:05 -070037#include "mock/MockVsyncController.h"
Valerie Haud251afb2019-03-29 14:19:02 -070038
39namespace android {
40
41using testing::_;
42using testing::Return;
43
44using FakeHwcDisplayInjector = TestableSurfaceFlinger::FakeHwcDisplayInjector;
Vishnu Nairaf6d2972022-11-18 06:26:38 +000045using frontend::TransactionHandler;
46
Vishnu Nair1523dad2022-09-29 16:05:18 -070047constexpr nsecs_t TRANSACTION_TIMEOUT = s2ns(5);
Valerie Haud251afb2019-03-29 14:19:02 -070048class TransactionApplicationTest : public testing::Test {
49public:
50 TransactionApplicationTest() {
51 const ::testing::TestInfo* const test_info =
52 ::testing::UnitTest::GetInstance()->current_test_info();
53 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
54
Valerie Haud251afb2019-03-29 14:19:02 -070055 setupScheduler();
56 }
57
58 ~TransactionApplicationTest() {
59 const ::testing::TestInfo* const test_info =
60 ::testing::UnitTest::GetInstance()->current_test_info();
61 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
62 }
63
64 void setupScheduler() {
65 auto eventThread = std::make_unique<mock::EventThread>();
66 auto sfEventThread = std::make_unique<mock::EventThread>();
67
68 EXPECT_CALL(*eventThread, registerDisplayEventConnection(_));
69 EXPECT_CALL(*eventThread, createEventConnection(_, _))
Ady Abrahamd11bade2022-08-01 16:18:03 -070070 .WillOnce(Return(sp<EventThreadConnection>::make(eventThread.get(),
71 mock::EventThread::kCallingUid,
72 ResyncCallback())));
Valerie Haud251afb2019-03-29 14:19:02 -070073
74 EXPECT_CALL(*sfEventThread, registerDisplayEventConnection(_));
75 EXPECT_CALL(*sfEventThread, createEventConnection(_, _))
Ady Abrahamd11bade2022-08-01 16:18:03 -070076 .WillOnce(Return(sp<EventThreadConnection>::make(sfEventThread.get(),
77 mock::EventThread::kCallingUid,
78 ResyncCallback())));
Valerie Haud251afb2019-03-29 14:19:02 -070079
Ady Abraham8cb21882020-08-26 18:22:05 -070080 EXPECT_CALL(*mVSyncTracker, nextAnticipatedVSyncTimeFrom(_)).WillRepeatedly(Return(0));
81 EXPECT_CALL(*mVSyncTracker, currentPeriod())
Marin Shalamanov045b7002021-01-07 16:56:24 +010082 .WillRepeatedly(Return(FakeHwcDisplayInjector::DEFAULT_VSYNC_PERIOD));
Valerie Haud251afb2019-03-29 14:19:02 -070083
Ady Abraham3efa3942021-06-24 19:01:25 -070084 mFlinger.setupComposer(std::make_unique<Hwc2::mock::Composer>());
Ady Abraham8cb21882020-08-26 18:22:05 -070085 mFlinger.setupScheduler(std::unique_ptr<mock::VsyncController>(mVsyncController),
86 std::unique_ptr<mock::VSyncTracker>(mVSyncTracker),
Valerie Haud251afb2019-03-29 14:19:02 -070087 std::move(eventThread), std::move(sfEventThread));
Vishnu Nair59f6d2d2022-10-05 16:59:56 -070088 mFlinger.flinger()->addTransactionReadyFilters();
Valerie Haud251afb2019-03-29 14:19:02 -070089 }
90
Valerie Haud251afb2019-03-29 14:19:02 -070091 TestableSurfaceFlinger mFlinger;
92
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;
Ady Abrahamf0c56492020-12-17 18:04:15 -0800102 int64_t desiredPresentTime = 0;
103 bool isAutoTimestamp = true;
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000104 FrameTimelineInfo frameTimelineInfo;
Patrick Williams75ce1ea2023-01-19 15:02:30 -0600105 std::vector<client_cache_t> uncacheBuffers;
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000106 uint64_t id = static_cast<uint64_t>(-1);
107 static_assert(0xffffffffffffffff == static_cast<uint64_t>(-1));
Valerie Haud251afb2019-03-29 14:19:02 -0700108 };
109
Vishnu Nair6b591152021-10-08 11:45:14 -0700110 void checkEqual(TransactionInfo info, TransactionState state) {
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000111 EXPECT_EQ(0u, info.states.size());
112 EXPECT_EQ(0u, state.states.size());
Valerie Haud251afb2019-03-29 14:19:02 -0700113
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000114 EXPECT_EQ(0u, info.displays.size());
115 EXPECT_EQ(0u, state.displays.size());
Valerie Haud251afb2019-03-29 14:19:02 -0700116 EXPECT_EQ(info.flags, state.flags);
117 EXPECT_EQ(info.desiredPresentTime, state.desiredPresentTime);
118 }
119
Patrick Williams641f7f22022-06-22 19:25:35 +0000120 void setupSingle(TransactionInfo& transaction, uint32_t flags, int64_t desiredPresentTime,
121 bool isAutoTimestamp, const FrameTimelineInfo& frameTimelineInfo) {
Valerie Haud251afb2019-03-29 14:19:02 -0700122 mTransactionNumber++;
Vishnu Nair1523dad2022-09-29 16:05:18 -0700123 transaction.flags |= flags;
Valerie Haud251afb2019-03-29 14:19:02 -0700124 transaction.desiredPresentTime = desiredPresentTime;
Ady Abrahamf0c56492020-12-17 18:04:15 -0800125 transaction.isAutoTimestamp = isAutoTimestamp;
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000126 transaction.frameTimelineInfo = frameTimelineInfo;
Valerie Haud251afb2019-03-29 14:19:02 -0700127 }
128
Patrick Williams641f7f22022-06-22 19:25:35 +0000129 void NotPlacedOnTransactionQueue(uint32_t flags) {
Vishnu Nair60d902e2022-07-20 02:55:37 +0000130 ASSERT_TRUE(mFlinger.getTransactionQueue().isEmpty());
Dominik Laskowski46f3e3b2021-08-10 11:44:24 -0700131 EXPECT_CALL(*mFlinger.scheduler(), scheduleFrame()).Times(1);
Valerie Haud251afb2019-03-29 14:19:02 -0700132 TransactionInfo transaction;
Patrick Williams641f7f22022-06-22 19:25:35 +0000133 setupSingle(transaction, flags,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800134 /*desiredPresentTime*/ systemTime(), /*isAutoTimestamp*/ true,
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000135 FrameTimelineInfo{});
Valerie Haud251afb2019-03-29 14:19:02 -0700136 nsecs_t applicationTime = systemTime();
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000137 mFlinger.setTransactionState(transaction.frameTimelineInfo, transaction.states,
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700138 transaction.displays, transaction.flags,
Valerie Haud251afb2019-03-29 14:19:02 -0700139 transaction.applyToken, transaction.inputWindowCommands,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800140 transaction.desiredPresentTime, transaction.isAutoTimestamp,
Patrick Williams75ce1ea2023-01-19 15:02:30 -0600141 transaction.uncacheBuffers, mHasListenerCallbacks, mCallbacks,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800142 transaction.id);
Valerie Haud251afb2019-03-29 14:19:02 -0700143
Patrick Williams641f7f22022-06-22 19:25:35 +0000144 // If transaction is synchronous, SF applyTransactionState should time out (5s) wating for
145 // SF to commit the transaction. If this is animation, it should not time out waiting.
Valerie Haud251afb2019-03-29 14:19:02 -0700146 nsecs_t returnedTime = systemTime();
Vishnu Nair1523dad2022-09-29 16:05:18 -0700147 EXPECT_LE(returnedTime, applicationTime + TRANSACTION_TIMEOUT);
Arthur Hung58144272021-01-16 03:43:53 +0000148 // Each transaction should have been placed on the transaction queue
Vishnu Nair60d902e2022-07-20 02:55:37 +0000149 auto& transactionQueue = mFlinger.getTransactionQueue();
150 EXPECT_FALSE(transactionQueue.isEmpty());
Valerie Haud251afb2019-03-29 14:19:02 -0700151 }
152
Patrick Williams641f7f22022-06-22 19:25:35 +0000153 void PlaceOnTransactionQueue(uint32_t flags) {
Vishnu Nair60d902e2022-07-20 02:55:37 +0000154 ASSERT_TRUE(mFlinger.getTransactionQueue().isEmpty());
Dominik Laskowski46f3e3b2021-08-10 11:44:24 -0700155 EXPECT_CALL(*mFlinger.scheduler(), scheduleFrame()).Times(1);
Valerie Haud251afb2019-03-29 14:19:02 -0700156
157 // first check will see desired present time has not passed,
158 // but afterwards it will look like the desired present time has passed
159 nsecs_t time = systemTime();
Valerie Haud251afb2019-03-29 14:19:02 -0700160 TransactionInfo transaction;
Patrick Williams641f7f22022-06-22 19:25:35 +0000161 setupSingle(transaction, flags, /*desiredPresentTime*/ time + s2ns(1), false,
162 FrameTimelineInfo{});
Valerie Haud251afb2019-03-29 14:19:02 -0700163 nsecs_t applicationSentTime = systemTime();
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000164 mFlinger.setTransactionState(transaction.frameTimelineInfo, transaction.states,
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700165 transaction.displays, transaction.flags,
Valerie Haud251afb2019-03-29 14:19:02 -0700166 transaction.applyToken, transaction.inputWindowCommands,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800167 transaction.desiredPresentTime, transaction.isAutoTimestamp,
Patrick Williams75ce1ea2023-01-19 15:02:30 -0600168 transaction.uncacheBuffers, mHasListenerCallbacks, mCallbacks,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800169 transaction.id);
Valerie Haud251afb2019-03-29 14:19:02 -0700170
171 nsecs_t returnedTime = systemTime();
Vishnu Nair1523dad2022-09-29 16:05:18 -0700172 EXPECT_LE(returnedTime, applicationSentTime + TRANSACTION_TIMEOUT);
Valerie Haud251afb2019-03-29 14:19:02 -0700173 // This transaction should have been placed on the transaction queue
Vishnu Nair60d902e2022-07-20 02:55:37 +0000174 auto& transactionQueue = mFlinger.getTransactionQueue();
175 EXPECT_FALSE(transactionQueue.isEmpty());
Valerie Haud251afb2019-03-29 14:19:02 -0700176 }
177
Patrick Williams641f7f22022-06-22 19:25:35 +0000178 void BlockedByPriorTransaction(uint32_t flags) {
Vishnu Nair60d902e2022-07-20 02:55:37 +0000179 ASSERT_TRUE(mFlinger.getTransactionQueue().isEmpty());
Valerie Haud251afb2019-03-29 14:19:02 -0700180 nsecs_t time = systemTime();
Patrick Williams641f7f22022-06-22 19:25:35 +0000181 EXPECT_CALL(*mFlinger.scheduler(), scheduleFrame()).Times(2);
182
Valerie Haud251afb2019-03-29 14:19:02 -0700183 // transaction that should go on the pending thread
184 TransactionInfo transactionA;
Patrick Williams641f7f22022-06-22 19:25:35 +0000185 setupSingle(transactionA, /*flags*/ 0, /*desiredPresentTime*/ time + s2ns(1), false,
186 FrameTimelineInfo{});
Valerie Haud251afb2019-03-29 14:19:02 -0700187
188 // transaction that would not have gone on the pending thread if not
189 // blocked
190 TransactionInfo transactionB;
Patrick Williams641f7f22022-06-22 19:25:35 +0000191 setupSingle(transactionB, flags, /*desiredPresentTime*/ systemTime(),
192 /*isAutoTimestamp*/ true, FrameTimelineInfo{});
Valerie Haud251afb2019-03-29 14:19:02 -0700193
194 nsecs_t applicationSentTime = systemTime();
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000195 mFlinger.setTransactionState(transactionA.frameTimelineInfo, transactionA.states,
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700196 transactionA.displays, transactionA.flags,
Valerie Haud251afb2019-03-29 14:19:02 -0700197 transactionA.applyToken, transactionA.inputWindowCommands,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800198 transactionA.desiredPresentTime, transactionA.isAutoTimestamp,
Patrick Williams75ce1ea2023-01-19 15:02:30 -0600199 transactionA.uncacheBuffers, mHasListenerCallbacks, mCallbacks,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800200 transactionA.id);
Valerie Haud251afb2019-03-29 14:19:02 -0700201
202 // This thread should not have been blocked by the above transaction
203 // (5s is the timeout period that applyTransactionState waits for SF to
204 // commit the transaction)
Vishnu Nair1523dad2022-09-29 16:05:18 -0700205 EXPECT_LE(systemTime(), applicationSentTime + TRANSACTION_TIMEOUT);
Arthur Hung58144272021-01-16 03:43:53 +0000206 // transaction that would goes to pending transaciton queue.
207 mFlinger.flushTransactionQueues();
Valerie Haud251afb2019-03-29 14:19:02 -0700208
209 applicationSentTime = systemTime();
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000210 mFlinger.setTransactionState(transactionB.frameTimelineInfo, transactionB.states,
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700211 transactionB.displays, transactionB.flags,
Valerie Haud251afb2019-03-29 14:19:02 -0700212 transactionB.applyToken, transactionB.inputWindowCommands,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800213 transactionB.desiredPresentTime, transactionB.isAutoTimestamp,
Patrick Williams75ce1ea2023-01-19 15:02:30 -0600214 transactionB.uncacheBuffers, mHasListenerCallbacks, mCallbacks,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800215 transactionB.id);
Valerie Haud251afb2019-03-29 14:19:02 -0700216
217 // this thread should have been blocked by the above transaction
218 // if this is an animation, this thread should be blocked for 5s
219 // in setTransactionState waiting for transactionA to flush. Otherwise,
220 // the transaction should be placed on the pending queue
Vishnu Nair1523dad2022-09-29 16:05:18 -0700221 EXPECT_LE(systemTime(), applicationSentTime + TRANSACTION_TIMEOUT);
Valerie Haud251afb2019-03-29 14:19:02 -0700222
Arthur Hung58144272021-01-16 03:43:53 +0000223 // transaction that would goes to pending transaciton queue.
224 mFlinger.flushTransactionQueues();
225
Ady Abrahame46243a2021-02-23 19:33:49 -0800226 // check that the transaction was applied.
Arthur Hung58144272021-01-16 03:43:53 +0000227 auto transactionQueue = mFlinger.getPendingTransactionQueue();
Ady Abrahame46243a2021-02-23 19:33:49 -0800228 EXPECT_EQ(0u, transactionQueue.size());
Valerie Haud251afb2019-03-29 14:19:02 -0700229 }
230
231 bool mHasListenerCallbacks = false;
232 std::vector<ListenerCallbacks> mCallbacks;
233 int mTransactionNumber = 0;
234};
235
Vishnu Nair60d902e2022-07-20 02:55:37 +0000236TEST_F(TransactionApplicationTest, AddToPendingQueue) {
237 ASSERT_TRUE(mFlinger.getTransactionQueue().isEmpty());
Dominik Laskowski46f3e3b2021-08-10 11:44:24 -0700238 EXPECT_CALL(*mFlinger.scheduler(), scheduleFrame()).Times(1);
Valerie Haud251afb2019-03-29 14:19:02 -0700239
Valerie Haud251afb2019-03-29 14:19:02 -0700240 TransactionInfo transactionA; // transaction to go on pending queue
Patrick Williams641f7f22022-06-22 19:25:35 +0000241 setupSingle(transactionA, /*flags*/ 0, /*desiredPresentTime*/ s2ns(1), false,
242 FrameTimelineInfo{});
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000243 mFlinger.setTransactionState(transactionA.frameTimelineInfo, transactionA.states,
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700244 transactionA.displays, transactionA.flags, transactionA.applyToken,
245 transactionA.inputWindowCommands, transactionA.desiredPresentTime,
Patrick Williams75ce1ea2023-01-19 15:02:30 -0600246 transactionA.isAutoTimestamp, transactionA.uncacheBuffers,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800247 mHasListenerCallbacks, mCallbacks, transactionA.id);
Valerie Haud251afb2019-03-29 14:19:02 -0700248
Zhuoyao Zhang3d3540d2021-01-14 05:14:54 +0000249 auto& transactionQueue = mFlinger.getTransactionQueue();
Vishnu Nair60d902e2022-07-20 02:55:37 +0000250 ASSERT_FALSE(transactionQueue.isEmpty());
Valerie Haud251afb2019-03-29 14:19:02 -0700251
Vishnu Nair60d902e2022-07-20 02:55:37 +0000252 auto transactionState = transactionQueue.pop().value();
Valerie Haud251afb2019-03-29 14:19:02 -0700253 checkEqual(transactionA, transactionState);
Vishnu Nair60d902e2022-07-20 02:55:37 +0000254}
255
256TEST_F(TransactionApplicationTest, Flush_RemovesFromQueue) {
257 ASSERT_TRUE(mFlinger.getTransactionQueue().isEmpty());
258 EXPECT_CALL(*mFlinger.scheduler(), scheduleFrame()).Times(1);
259
260 TransactionInfo transactionA; // transaction to go on pending queue
261 setupSingle(transactionA, /*flags*/ 0, /*desiredPresentTime*/ s2ns(1), false,
262 FrameTimelineInfo{});
263 mFlinger.setTransactionState(transactionA.frameTimelineInfo, transactionA.states,
264 transactionA.displays, transactionA.flags, transactionA.applyToken,
265 transactionA.inputWindowCommands, transactionA.desiredPresentTime,
Patrick Williams75ce1ea2023-01-19 15:02:30 -0600266 transactionA.isAutoTimestamp, transactionA.uncacheBuffers,
Vishnu Nair60d902e2022-07-20 02:55:37 +0000267 mHasListenerCallbacks, mCallbacks, transactionA.id);
268
269 auto& transactionQueue = mFlinger.getTransactionQueue();
270 ASSERT_FALSE(transactionQueue.isEmpty());
Valerie Haud251afb2019-03-29 14:19:02 -0700271
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,
Patrick Williams75ce1ea2023-01-19 15:02:30 -0600280 empty.uncacheBuffers, 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
Vishnu Nair60d902e2022-07-20 02:55:37 +0000286 EXPECT_TRUE(mFlinger.getTransactionQueue().isEmpty());
Valerie Haud251afb2019-03-29 14:19:02 -0700287}
288
Valerie Haud251afb2019-03-29 14:19:02 -0700289TEST_F(TransactionApplicationTest, NotPlacedOnTransactionQueue_SyncInputWindows) {
Patrick Williams641f7f22022-06-22 19:25:35 +0000290 NotPlacedOnTransactionQueue(/*flags*/ 0);
Valerie Haud251afb2019-03-29 14:19:02 -0700291}
292
Valerie Haud251afb2019-03-29 14:19:02 -0700293TEST_F(TransactionApplicationTest, PlaceOnTransactionQueue_SyncInputWindows) {
Patrick Williams641f7f22022-06-22 19:25:35 +0000294 PlaceOnTransactionQueue(/*flags*/ 0);
Valerie Haud251afb2019-03-29 14:19:02 -0700295}
296
Valerie Hau09e60052019-12-15 14:51:15 -0800297TEST_F(TransactionApplicationTest, FromHandle) {
298 sp<IBinder> badHandle;
299 auto ret = mFlinger.fromHandle(badHandle);
Vishnu Nair07e2a482022-10-18 19:18:16 +0000300 EXPECT_EQ(nullptr, ret.get());
Valerie Hau09e60052019-12-15 14:51:15 -0800301}
ramindani4d48f902021-09-20 21:07:45 +0000302
Ady Abraham9dada822022-02-03 10:26:59 -0800303class LatchUnsignaledTest : public TransactionApplicationTest {
304public:
305 void TearDown() override {
306 // Clear all transaction queues to release all transactions we sent
307 // in the tests. Otherwise, gmock complains about memory leaks.
Vishnu Nair60d902e2022-07-20 02:55:37 +0000308 while (!mFlinger.getTransactionQueue().isEmpty()) {
309 mFlinger.getTransactionQueue().pop();
310 }
Ady Abraham9dada822022-02-03 10:26:59 -0800311 mFlinger.getPendingTransactionQueue().clear();
Ady Abraham9dada822022-02-03 10:26:59 -0800312 mFlinger.commitTransactionsLocked(eTransactionMask);
313 mFlinger.mutableCurrentState().layersSortedByZ.clear();
314 mFlinger.mutableDrawingState().layersSortedByZ.clear();
315 }
316
317 static sp<Fence> fence(Fence::Status status) {
318 const auto fence = sp<mock::MockFence>::make();
319 EXPECT_CALL(*fence, getStatus()).WillRepeatedly(Return(status));
320 return fence;
321 }
322
323 ComposerState createComposerState(int layerId, sp<Fence> fence, uint64_t what) {
324 ComposerState state;
Vishnu Nair59f6d2d2022-10-05 16:59:56 -0700325 state.state.bufferData =
326 std::make_shared<fake::BufferData>(/* bufferId */ 123L, /* width */ 1,
327 /* height */ 2, /* pixelFormat */ 0,
328 /* outUsage */ 0);
Ady Abraham9dada822022-02-03 10:26:59 -0800329 state.state.bufferData->acquireFence = std::move(fence);
330 state.state.layerId = layerId;
331 state.state.surface =
Patrick Williams83f36b22022-09-14 17:57:35 +0000332 sp<Layer>::make(LayerCreationArgs(mFlinger.flinger(), nullptr, "TestLayer", 0, {}))
Ady Abraham9dada822022-02-03 10:26:59 -0800333 ->getHandle();
334 state.state.bufferData->flags = BufferData::BufferDataChange::fenceChanged;
335
336 state.state.what = what;
337 if (what & layer_state_t::eCropChanged) {
338 state.state.crop = Rect(1, 2, 3, 4);
339 }
340 return state;
341 }
342
343 TransactionInfo createTransactionInfo(const sp<IBinder>& applyToken,
344 const std::vector<ComposerState>& states) {
345 TransactionInfo transaction;
Vishnu Nair1523dad2022-09-29 16:05:18 -0700346 const uint32_t kFlags = 0;
Ady Abraham9dada822022-02-03 10:26:59 -0800347 const nsecs_t kDesiredPresentTime = systemTime();
348 const bool kIsAutoTimestamp = true;
349 const auto kFrameTimelineInfo = FrameTimelineInfo{};
350
Patrick Williams641f7f22022-06-22 19:25:35 +0000351 setupSingle(transaction, kFlags, kDesiredPresentTime, kIsAutoTimestamp, kFrameTimelineInfo);
Ady Abraham9dada822022-02-03 10:26:59 -0800352 transaction.applyToken = applyToken;
353 for (const auto& state : states) {
354 transaction.states.push_back(state);
355 }
356
357 return transaction;
358 }
359
360 void setTransactionStates(const std::vector<TransactionInfo>& transactions,
Ady Abraham9dada822022-02-03 10:26:59 -0800361 size_t expectedTransactionsPending) {
Vishnu Nair60d902e2022-07-20 02:55:37 +0000362 EXPECT_TRUE(mFlinger.getTransactionQueue().isEmpty());
Ady Abraham9dada822022-02-03 10:26:59 -0800363 EXPECT_EQ(0u, mFlinger.getPendingTransactionQueue().size());
364
Vishnu Nair40fff5c2022-11-04 02:46:28 +0000365 for (auto transaction : transactions) {
366 std::vector<ResolvedComposerState> resolvedStates;
367 resolvedStates.reserve(transaction.states.size());
368 for (auto& state : transaction.states) {
369 resolvedStates.emplace_back(std::move(state));
370 }
371
372 TransactionState transactionState(transaction.frameTimelineInfo, resolvedStates,
373 transaction.displays, transaction.flags,
374 transaction.applyToken,
375 transaction.inputWindowCommands,
376 transaction.desiredPresentTime,
Patrick Williams75ce1ea2023-01-19 15:02:30 -0600377 transaction.isAutoTimestamp, {}, systemTime(), 0,
Vishnu Nair40fff5c2022-11-04 02:46:28 +0000378 mHasListenerCallbacks, mCallbacks, getpid(),
379 static_cast<int>(getuid()), transaction.id);
380 mFlinger.setTransactionStateInternal(transactionState);
Ady Abraham9dada822022-02-03 10:26:59 -0800381 }
382 mFlinger.flushTransactionQueues();
Vishnu Nair60d902e2022-07-20 02:55:37 +0000383 EXPECT_TRUE(mFlinger.getTransactionQueue().isEmpty());
Vishnu Nair59f6d2d2022-10-05 16:59:56 -0700384 EXPECT_EQ(expectedTransactionsPending, mFlinger.getPendingTransactionCount());
Ady Abraham9dada822022-02-03 10:26:59 -0800385 }
386};
387
388class LatchUnsignaledAutoSingleLayerTest : public LatchUnsignaledTest {
389public:
390 void SetUp() override {
391 LatchUnsignaledTest::SetUp();
392 SurfaceFlinger::enableLatchUnsignaledConfig = LatchUnsignaledConfig::AutoSingleLayer;
393 }
394};
395
396TEST_F(LatchUnsignaledAutoSingleLayerTest, Flush_RemovesSingleSignaledFromTheQueue) {
397 const sp<IBinder> kApplyToken =
398 IInterface::asBinder(TransactionCompletedListener::getIInstance());
399 const auto kLayerId = 1;
Ady Abraham9dada822022-02-03 10:26:59 -0800400 const auto kExpectedTransactionsPending = 0u;
401
402 const auto signaledTransaction =
403 createTransactionInfo(kApplyToken,
404 {createComposerState(kLayerId, fence(Fence::Status::Signaled),
405 layer_state_t::eBufferChanged)});
Vishnu Nair1523dad2022-09-29 16:05:18 -0700406 setTransactionStates({signaledTransaction}, kExpectedTransactionsPending);
ramindani4d48f902021-09-20 21:07:45 +0000407}
408
Ady Abraham9dada822022-02-03 10:26:59 -0800409TEST_F(LatchUnsignaledAutoSingleLayerTest, Flush_RemovesSingleUnSignaledFromTheQueue) {
410 const sp<IBinder> kApplyToken =
411 IInterface::asBinder(TransactionCompletedListener::getIInstance());
412 const auto kLayerId = 1;
Ady Abraham9dada822022-02-03 10:26:59 -0800413 const auto kExpectedTransactionsPending = 0u;
414
415 const auto unsignaledTransaction =
416 createTransactionInfo(kApplyToken,
417 {
418 createComposerState(kLayerId,
419 fence(Fence::Status::Unsignaled),
420 layer_state_t::eBufferChanged),
421 });
Vishnu Nair1523dad2022-09-29 16:05:18 -0700422 setTransactionStates({unsignaledTransaction}, kExpectedTransactionsPending);
ramindani4d48f902021-09-20 21:07:45 +0000423}
424
Ady Abraham9dada822022-02-03 10:26:59 -0800425TEST_F(LatchUnsignaledAutoSingleLayerTest, Flush_KeepsUnSignaledInTheQueue_NonBufferCropChange) {
426 const sp<IBinder> kApplyToken =
427 IInterface::asBinder(TransactionCompletedListener::getIInstance());
428 const auto kLayerId = 1;
Ady Abraham9dada822022-02-03 10:26:59 -0800429 const auto kExpectedTransactionsPending = 1u;
430
431 const auto unsignaledTransaction =
432 createTransactionInfo(kApplyToken,
433 {
434 createComposerState(kLayerId,
435 fence(Fence::Status::Unsignaled),
Vishnu Nair59f6d2d2022-10-05 16:59:56 -0700436 layer_state_t::eCropChanged |
437 layer_state_t::
438 eBufferChanged),
Ady Abraham9dada822022-02-03 10:26:59 -0800439 });
Vishnu Nair1523dad2022-09-29 16:05:18 -0700440 setTransactionStates({unsignaledTransaction}, kExpectedTransactionsPending);
ramindani4d48f902021-09-20 21:07:45 +0000441}
442
Ady Abraham9dada822022-02-03 10:26:59 -0800443TEST_F(LatchUnsignaledAutoSingleLayerTest, Flush_KeepsUnSignaledInTheQueue_NonBufferChangeClubed) {
444 const sp<IBinder> kApplyToken =
445 IInterface::asBinder(TransactionCompletedListener::getIInstance());
446 const auto kLayerId = 1;
Ady Abraham9dada822022-02-03 10:26:59 -0800447 const auto kExpectedTransactionsPending = 1u;
448
449 const auto unsignaledTransaction =
450 createTransactionInfo(kApplyToken,
451 {
452 createComposerState(kLayerId,
453 fence(Fence::Status::Unsignaled),
454 layer_state_t::eCropChanged |
455 layer_state_t::
456 eBufferChanged),
457 });
Vishnu Nair1523dad2022-09-29 16:05:18 -0700458 setTransactionStates({unsignaledTransaction}, kExpectedTransactionsPending);
ramindani4d48f902021-09-20 21:07:45 +0000459}
460
Ady Abraham9dada822022-02-03 10:26:59 -0800461TEST_F(LatchUnsignaledAutoSingleLayerTest, Flush_KeepsInTheQueueSameApplyTokenMultiState) {
462 const sp<IBinder> kApplyToken =
463 IInterface::asBinder(TransactionCompletedListener::getIInstance());
464 const auto kLayerId = 1;
Ady Abraham9dada822022-02-03 10:26:59 -0800465 const auto kExpectedTransactionsPending = 1u;
466
467 const auto mixedTransaction =
468 createTransactionInfo(kApplyToken,
469 {
470 createComposerState(kLayerId,
471 fence(Fence::Status::Unsignaled),
472 layer_state_t::eBufferChanged),
473 createComposerState(kLayerId,
474 fence(Fence::Status::Signaled),
475 layer_state_t::eBufferChanged),
476 });
Vishnu Nair1523dad2022-09-29 16:05:18 -0700477 setTransactionStates({mixedTransaction}, kExpectedTransactionsPending);
ramindani4d48f902021-09-20 21:07:45 +0000478}
479
Ady Abraham9dada822022-02-03 10:26:59 -0800480TEST_F(LatchUnsignaledAutoSingleLayerTest, Flush_KeepsInTheQueue_MultipleStateTransaction) {
481 const sp<IBinder> kApplyToken =
482 IInterface::asBinder(TransactionCompletedListener::getIInstance());
483 const auto kLayerId1 = 1;
484 const auto kLayerId2 = 2;
Ady Abraham9dada822022-02-03 10:26:59 -0800485 const auto kExpectedTransactionsPending = 1u;
486
487 const auto mixedTransaction =
488 createTransactionInfo(kApplyToken,
489 {
490 createComposerState(kLayerId1,
491 fence(Fence::Status::Unsignaled),
492 layer_state_t::eBufferChanged),
493 createComposerState(kLayerId2,
494 fence(Fence::Status::Signaled),
495 layer_state_t::eBufferChanged),
496 });
Vishnu Nair1523dad2022-09-29 16:05:18 -0700497 setTransactionStates({mixedTransaction}, kExpectedTransactionsPending);
ramindani4d48f902021-09-20 21:07:45 +0000498}
499
Ady Abraham9dada822022-02-03 10:26:59 -0800500TEST_F(LatchUnsignaledAutoSingleLayerTest, Flush_RemovesSignaledFromTheQueue) {
501 const sp<IBinder> kApplyToken =
502 IInterface::asBinder(TransactionCompletedListener::getIInstance());
503 const auto kLayerId1 = 1;
504 const auto kLayerId2 = 2;
Ady Abraham9dada822022-02-03 10:26:59 -0800505 const auto kExpectedTransactionsPending = 0u;
506
507 const auto signaledTransaction =
508 createTransactionInfo(kApplyToken,
509 {
510 createComposerState(kLayerId1,
511 fence(Fence::Status::Signaled),
512 layer_state_t::eBufferChanged),
513 });
514 const auto signaledTransaction2 =
515 createTransactionInfo(kApplyToken,
516 {
517 createComposerState(kLayerId2,
518 fence(Fence::Status::Signaled),
519 layer_state_t::eBufferChanged),
520 });
Vishnu Nair1523dad2022-09-29 16:05:18 -0700521 setTransactionStates({signaledTransaction, signaledTransaction2}, kExpectedTransactionsPending);
ramindani4d48f902021-09-20 21:07:45 +0000522}
523
Ady Abrahame1bfaac2022-02-22 21:32:08 -0800524TEST_F(LatchUnsignaledAutoSingleLayerTest,
525 UnsignaledNotAppliedWhenThereAreSignaled_UnsignaledFirst) {
Ady Abraham9dada822022-02-03 10:26:59 -0800526 const sp<IBinder> kApplyToken1 =
527 IInterface::asBinder(TransactionCompletedListener::getIInstance());
528 const sp<IBinder> kApplyToken2 = sp<BBinder>::make();
Ady Abrahame1bfaac2022-02-22 21:32:08 -0800529 const sp<IBinder> kApplyToken3 = sp<BBinder>::make();
Ady Abraham9dada822022-02-03 10:26:59 -0800530 const auto kLayerId1 = 1;
531 const auto kLayerId2 = 2;
Ady Abrahame1bfaac2022-02-22 21:32:08 -0800532 const auto kExpectedTransactionsPending = 1u;
533
534 const auto unsignaledTransaction =
535 createTransactionInfo(kApplyToken1,
536 {
537 createComposerState(kLayerId1,
538 fence(Fence::Status::Unsignaled),
539 layer_state_t::eBufferChanged),
540 });
541
542 const auto signaledTransaction =
543 createTransactionInfo(kApplyToken2,
544 {
545 createComposerState(kLayerId2,
546 fence(Fence::Status::Signaled),
547 layer_state_t::eBufferChanged),
548 });
549 const auto signaledTransaction2 =
550 createTransactionInfo(kApplyToken3,
551 {
552 createComposerState(kLayerId2,
553 fence(Fence::Status::Signaled),
554 layer_state_t::eBufferChanged),
555 });
556
557 setTransactionStates({unsignaledTransaction, signaledTransaction, signaledTransaction2},
Vishnu Nair1523dad2022-09-29 16:05:18 -0700558 kExpectedTransactionsPending);
Ady Abrahame1bfaac2022-02-22 21:32:08 -0800559}
560
Ady Abraham9dada822022-02-03 10:26:59 -0800561TEST_F(LatchUnsignaledAutoSingleLayerTest, Flush_KeepsTransactionInTheQueueSameApplyToken) {
562 const sp<IBinder> kApplyToken =
563 IInterface::asBinder(TransactionCompletedListener::getIInstance());
564 const auto kLayerId1 = 1;
565 const auto kLayerId2 = 2;
Ady Abraham9dada822022-02-03 10:26:59 -0800566 const auto kExpectedTransactionsPending = 1u;
567
568 const auto unsignaledTransaction =
569 createTransactionInfo(kApplyToken,
570 {
571 createComposerState(kLayerId1,
572 fence(Fence::Status::Unsignaled),
573 layer_state_t::eBufferChanged),
574 });
575 const auto signaledTransaction =
576 createTransactionInfo(kApplyToken,
577 {
578 createComposerState(kLayerId2,
579 fence(Fence::Status::Signaled),
580 layer_state_t::eBufferChanged),
581 });
Vishnu Nair1523dad2022-09-29 16:05:18 -0700582 setTransactionStates({unsignaledTransaction, signaledTransaction},
Ady Abraham9dada822022-02-03 10:26:59 -0800583 kExpectedTransactionsPending);
ramindani4d48f902021-09-20 21:07:45 +0000584}
585
Ady Abraham9dada822022-02-03 10:26:59 -0800586TEST_F(LatchUnsignaledAutoSingleLayerTest, Flush_KeepsTransactionInTheQueue) {
587 const sp<IBinder> kApplyToken1 =
588 IInterface::asBinder(TransactionCompletedListener::getIInstance());
589 const sp<IBinder> kApplyToken2 = sp<BBinder>::make();
590 const auto kLayerId1 = 1;
591 const auto kLayerId2 = 2;
Ady Abraham9dada822022-02-03 10:26:59 -0800592 const auto kExpectedTransactionsPending = 1u;
593
594 const auto unsignaledTransaction =
595 createTransactionInfo(kApplyToken1,
596 {
597 createComposerState(kLayerId1,
598 fence(Fence::Status::Unsignaled),
599 layer_state_t::eBufferChanged),
600 });
601 const auto unsignaledTransaction2 =
602 createTransactionInfo(kApplyToken2,
603 {
604 createComposerState(kLayerId2,
605 fence(Fence::Status::Unsignaled),
606 layer_state_t::eBufferChanged),
607 });
608 setTransactionStates({unsignaledTransaction, unsignaledTransaction2},
Vishnu Nair1523dad2022-09-29 16:05:18 -0700609 kExpectedTransactionsPending);
ramindani4d48f902021-09-20 21:07:45 +0000610}
611
Ady Abraham2739e832022-02-14 17:42:00 -0800612TEST_F(LatchUnsignaledAutoSingleLayerTest, DontLatchUnsignaledWhenEarlyOffset) {
613 const sp<IBinder> kApplyToken =
614 IInterface::asBinder(TransactionCompletedListener::getIInstance());
615 const auto kLayerId = 1;
Ady Abraham2739e832022-02-14 17:42:00 -0800616 const auto kExpectedTransactionsPending = 1u;
617
618 const auto unsignaledTransaction =
619 createTransactionInfo(kApplyToken,
620 {
621 createComposerState(kLayerId,
622 fence(Fence::Status::Unsignaled),
623 layer_state_t::eBufferChanged),
624 });
625
626 // Get VsyncModulator out of the default config
627 static_cast<void>(mFlinger.mutableVsyncModulator()->onRefreshRateChangeInitiated());
628
Vishnu Nair1523dad2022-09-29 16:05:18 -0700629 setTransactionStates({unsignaledTransaction}, kExpectedTransactionsPending);
Ady Abraham2739e832022-02-14 17:42:00 -0800630}
631
Ady Abraham9dada822022-02-03 10:26:59 -0800632class LatchUnsignaledDisabledTest : public LatchUnsignaledTest {
633public:
634 void SetUp() override {
635 LatchUnsignaledTest::SetUp();
636 SurfaceFlinger::enableLatchUnsignaledConfig = LatchUnsignaledConfig::Disabled;
637 }
638};
639
640TEST_F(LatchUnsignaledDisabledTest, Flush_RemovesSignaledFromTheQueue) {
641 const sp<IBinder> kApplyToken =
642 IInterface::asBinder(TransactionCompletedListener::getIInstance());
643 const auto kLayerId = 1;
Ady Abraham9dada822022-02-03 10:26:59 -0800644 const auto kExpectedTransactionsPending = 0u;
645
646 const auto signaledTransaction =
647 createTransactionInfo(kApplyToken,
648 {createComposerState(kLayerId, fence(Fence::Status::Signaled),
649 layer_state_t::eBufferChanged)});
Vishnu Nair1523dad2022-09-29 16:05:18 -0700650 setTransactionStates({signaledTransaction}, kExpectedTransactionsPending);
ramindani4d48f902021-09-20 21:07:45 +0000651}
652
Ady Abraham9dada822022-02-03 10:26:59 -0800653TEST_F(LatchUnsignaledDisabledTest, Flush_KeepsInTheQueue) {
654 const sp<IBinder> kApplyToken =
655 IInterface::asBinder(TransactionCompletedListener::getIInstance());
656 const auto kLayerId = 1;
Ady Abraham9dada822022-02-03 10:26:59 -0800657 const auto kExpectedTransactionsPending = 1u;
658
659 const auto unsignaledTransaction =
660 createTransactionInfo(kApplyToken,
661 {
662 createComposerState(kLayerId,
663 fence(Fence::Status::Unsignaled),
664 layer_state_t::eBufferChanged),
665 });
Vishnu Nair1523dad2022-09-29 16:05:18 -0700666 setTransactionStates({unsignaledTransaction}, kExpectedTransactionsPending);
ramindani4d48f902021-09-20 21:07:45 +0000667}
668
Ady Abraham9dada822022-02-03 10:26:59 -0800669TEST_F(LatchUnsignaledDisabledTest, Flush_KeepsInTheQueueSameLayerId) {
670 const sp<IBinder> kApplyToken =
671 IInterface::asBinder(TransactionCompletedListener::getIInstance());
672 const auto kLayerId = 1;
Ady Abraham9dada822022-02-03 10:26:59 -0800673 const auto kExpectedTransactionsPending = 1u;
674
675 const auto unsignaledTransaction =
676 createTransactionInfo(kApplyToken,
677 {
678 createComposerState(kLayerId,
679 fence(Fence::Status::Unsignaled),
680 layer_state_t::eBufferChanged),
681 createComposerState(kLayerId,
682 fence(Fence::Status::Unsignaled),
683 layer_state_t::eBufferChanged),
684 });
Vishnu Nair1523dad2022-09-29 16:05:18 -0700685 setTransactionStates({unsignaledTransaction}, kExpectedTransactionsPending);
ramindani4d48f902021-09-20 21:07:45 +0000686}
687
Ady Abraham9dada822022-02-03 10:26:59 -0800688TEST_F(LatchUnsignaledDisabledTest, Flush_KeepsInTheQueueDifferentLayerId) {
689 const sp<IBinder> kApplyToken =
690 IInterface::asBinder(TransactionCompletedListener::getIInstance());
691 const auto kLayerId1 = 1;
692 const auto kLayerId2 = 2;
Ady Abraham9dada822022-02-03 10:26:59 -0800693 const auto kExpectedTransactionsPending = 1u;
694
695 const auto unsignaledTransaction =
696 createTransactionInfo(kApplyToken,
697 {
698 createComposerState(kLayerId1,
699 fence(Fence::Status::Unsignaled),
700 layer_state_t::eBufferChanged),
701 createComposerState(kLayerId2,
702 fence(Fence::Status::Unsignaled),
703 layer_state_t::eBufferChanged),
704 });
Vishnu Nair1523dad2022-09-29 16:05:18 -0700705 setTransactionStates({unsignaledTransaction}, kExpectedTransactionsPending);
ramindani4d48f902021-09-20 21:07:45 +0000706}
707
Ady Abraham9dada822022-02-03 10:26:59 -0800708TEST_F(LatchUnsignaledDisabledTest, Flush_RemovesSignaledFromTheQueue_MultipleLayers) {
709 const sp<IBinder> kApplyToken =
710 IInterface::asBinder(TransactionCompletedListener::getIInstance());
711 const auto kLayerId1 = 1;
712 const auto kLayerId2 = 2;
Ady Abraham9dada822022-02-03 10:26:59 -0800713 const auto kExpectedTransactionsPending = 0u;
714
715 const auto signaledTransaction =
716 createTransactionInfo(kApplyToken,
717 {
718 createComposerState(kLayerId1,
719 fence(Fence::Status::Signaled),
720 layer_state_t::eBufferChanged),
721 });
722 const auto signaledTransaction2 =
723 createTransactionInfo(kApplyToken,
724 {
725 createComposerState(kLayerId2,
726 fence(Fence::Status::Signaled),
727 layer_state_t::eBufferChanged),
728 });
Vishnu Nair1523dad2022-09-29 16:05:18 -0700729 setTransactionStates({signaledTransaction, signaledTransaction2}, kExpectedTransactionsPending);
ramindani4d48f902021-09-20 21:07:45 +0000730}
731
Ady Abraham9dada822022-02-03 10:26:59 -0800732TEST_F(LatchUnsignaledDisabledTest, Flush_KeepInTheQueueDifferentApplyToken) {
733 const sp<IBinder> kApplyToken1 =
734 IInterface::asBinder(TransactionCompletedListener::getIInstance());
735 const sp<IBinder> kApplyToken2 = sp<BBinder>::make();
736 const auto kLayerId1 = 1;
737 const auto kLayerId2 = 2;
Ady Abraham9dada822022-02-03 10:26:59 -0800738 const auto kExpectedTransactionsPending = 1u;
739
740 const auto unsignaledTransaction =
741 createTransactionInfo(kApplyToken1,
742 {
743 createComposerState(kLayerId1,
744 fence(Fence::Status::Unsignaled),
745 layer_state_t::eBufferChanged),
746 });
747 const auto signaledTransaction =
748 createTransactionInfo(kApplyToken2,
749 {
750 createComposerState(kLayerId2,
751 fence(Fence::Status::Signaled),
752 layer_state_t::eBufferChanged),
753 });
Vishnu Nair1523dad2022-09-29 16:05:18 -0700754 setTransactionStates({unsignaledTransaction, signaledTransaction},
Ady Abraham9dada822022-02-03 10:26:59 -0800755 kExpectedTransactionsPending);
ramindani4d48f902021-09-20 21:07:45 +0000756}
757
Ady Abraham9dada822022-02-03 10:26:59 -0800758TEST_F(LatchUnsignaledDisabledTest, Flush_KeepInTheQueueSameApplyToken) {
759 const sp<IBinder> kApplyToken =
760 IInterface::asBinder(TransactionCompletedListener::getIInstance());
761 const auto kLayerId1 = 1;
762 const auto kLayerId2 = 2;
Ady Abraham9dada822022-02-03 10:26:59 -0800763 const auto kExpectedTransactionsPending = 1u;
764
765 const auto signaledTransaction =
766 createTransactionInfo(kApplyToken,
767 {
768 createComposerState(kLayerId1,
769 fence(Fence::Status::Signaled),
770 layer_state_t::eBufferChanged),
771 });
772 const auto unsignaledTransaction =
773 createTransactionInfo(kApplyToken,
774 {
775 createComposerState(kLayerId2,
776 fence(Fence::Status::Unsignaled),
777 layer_state_t::eBufferChanged),
778 });
Vishnu Nair1523dad2022-09-29 16:05:18 -0700779 setTransactionStates({signaledTransaction, unsignaledTransaction},
Ady Abraham9dada822022-02-03 10:26:59 -0800780 kExpectedTransactionsPending);
ramindani4d48f902021-09-20 21:07:45 +0000781}
782
Ady Abraham9dada822022-02-03 10:26:59 -0800783TEST_F(LatchUnsignaledDisabledTest, Flush_KeepInTheUnsignaledTheQueue) {
784 const sp<IBinder> kApplyToken =
785 IInterface::asBinder(TransactionCompletedListener::getIInstance());
786 const auto kLayerId1 = 1;
787 const auto kLayerId2 = 2;
Vishnu Nair59f6d2d2022-10-05 16:59:56 -0700788 const auto kExpectedTransactionsPending = 2u;
Ady Abraham9dada822022-02-03 10:26:59 -0800789
790 const auto unsignaledTransaction =
791 createTransactionInfo(kApplyToken,
792 {
793 createComposerState(kLayerId1,
794 fence(Fence::Status::Unsignaled),
795 layer_state_t::eBufferChanged),
796 });
797 const auto unsignaledTransaction2 =
798 createTransactionInfo(kApplyToken,
799 {
800 createComposerState(kLayerId2,
801 fence(Fence::Status::Unsignaled),
802 layer_state_t::eBufferChanged),
803 });
804 setTransactionStates({unsignaledTransaction, unsignaledTransaction2},
Vishnu Nair1523dad2022-09-29 16:05:18 -0700805 kExpectedTransactionsPending);
ramindani4d48f902021-09-20 21:07:45 +0000806}
807
Ady Abraham9dada822022-02-03 10:26:59 -0800808class LatchUnsignaledAlwaysTest : public LatchUnsignaledTest {
809public:
810 void SetUp() override {
811 LatchUnsignaledTest::SetUp();
812 SurfaceFlinger::enableLatchUnsignaledConfig = LatchUnsignaledConfig::Always;
813 }
814};
815
816TEST_F(LatchUnsignaledAlwaysTest, Flush_RemovesSignaledFromTheQueue) {
817 const sp<IBinder> kApplyToken =
818 IInterface::asBinder(TransactionCompletedListener::getIInstance());
819 const auto kLayerId = 1;
Ady Abraham9dada822022-02-03 10:26:59 -0800820 const auto kExpectedTransactionsPending = 0u;
821
822 const auto signaledTransaction =
823 createTransactionInfo(kApplyToken,
824 {createComposerState(kLayerId, fence(Fence::Status::Signaled),
825 layer_state_t::eBufferChanged)});
Vishnu Nair1523dad2022-09-29 16:05:18 -0700826 setTransactionStates({signaledTransaction}, kExpectedTransactionsPending);
ramindani4d48f902021-09-20 21:07:45 +0000827}
828
Ady Abraham9dada822022-02-03 10:26:59 -0800829TEST_F(LatchUnsignaledAlwaysTest, Flush_RemovesFromTheQueue) {
830 const sp<IBinder> kApplyToken =
831 IInterface::asBinder(TransactionCompletedListener::getIInstance());
832 const auto kLayerId = 1;
Ady Abraham9dada822022-02-03 10:26:59 -0800833 const auto kExpectedTransactionsPending = 0u;
834
835 const auto unsignaledTransaction =
836 createTransactionInfo(kApplyToken,
837 {createComposerState(kLayerId, fence(Fence::Status::Unsignaled),
838 layer_state_t::eBufferChanged)});
Vishnu Nair1523dad2022-09-29 16:05:18 -0700839 setTransactionStates({unsignaledTransaction}, kExpectedTransactionsPending);
ramindani4d48f902021-09-20 21:07:45 +0000840}
841
Ady Abraham9dada822022-02-03 10:26:59 -0800842TEST_F(LatchUnsignaledAlwaysTest, Flush_RemovesFromTheQueueSameLayerId) {
843 const sp<IBinder> kApplyToken =
844 IInterface::asBinder(TransactionCompletedListener::getIInstance());
845 const auto kLayerId = 1;
Ady Abraham9dada822022-02-03 10:26:59 -0800846 const auto kExpectedTransactionsPending = 0u;
847
848 const auto mixedTransaction =
849 createTransactionInfo(kApplyToken,
850 {createComposerState(kLayerId, fence(Fence::Status::Unsignaled),
851 layer_state_t::eBufferChanged),
852 createComposerState(kLayerId, fence(Fence::Status::Signaled),
853 layer_state_t::eBufferChanged)});
Vishnu Nair1523dad2022-09-29 16:05:18 -0700854 setTransactionStates({mixedTransaction}, kExpectedTransactionsPending);
ramindani4d48f902021-09-20 21:07:45 +0000855}
856
Ady Abraham9dada822022-02-03 10:26:59 -0800857TEST_F(LatchUnsignaledAlwaysTest, Flush_RemovesFromTheQueueDifferentLayerId) {
858 const sp<IBinder> kApplyToken =
859 IInterface::asBinder(TransactionCompletedListener::getIInstance());
860 const auto kLayerId1 = 1;
861 const auto kLayerId2 = 2;
Ady Abraham9dada822022-02-03 10:26:59 -0800862 const auto kExpectedTransactionsPending = 0u;
863
864 const auto mixedTransaction =
865 createTransactionInfo(kApplyToken,
866 {createComposerState(kLayerId1, fence(Fence::Status::Unsignaled),
867 layer_state_t::eBufferChanged),
868 createComposerState(kLayerId2, fence(Fence::Status::Signaled),
869 layer_state_t::eBufferChanged)});
Vishnu Nair1523dad2022-09-29 16:05:18 -0700870 setTransactionStates({mixedTransaction}, kExpectedTransactionsPending);
ramindani4d48f902021-09-20 21:07:45 +0000871}
872
Ady Abraham9dada822022-02-03 10:26:59 -0800873TEST_F(LatchUnsignaledAlwaysTest, Flush_RemovesSignaledFromTheQueue_MultipleLayers) {
874 const sp<IBinder> kApplyToken =
875 IInterface::asBinder(TransactionCompletedListener::getIInstance());
876 const auto kLayerId1 = 1;
877 const auto kLayerId2 = 2;
Ady Abraham9dada822022-02-03 10:26:59 -0800878 const auto kExpectedTransactionsPending = 0u;
879
880 const auto signaledTransaction =
881 createTransactionInfo(kApplyToken,
882 {
883 createComposerState(kLayerId1,
884 fence(Fence::Status::Signaled),
885 layer_state_t::eBufferChanged),
886 });
887 const auto signaledTransaction2 =
888 createTransactionInfo(kApplyToken,
889 {
890 createComposerState(kLayerId2,
891 fence(Fence::Status::Signaled),
892 layer_state_t::eBufferChanged),
893 });
Vishnu Nair1523dad2022-09-29 16:05:18 -0700894 setTransactionStates({signaledTransaction, signaledTransaction2}, kExpectedTransactionsPending);
ramindani4d48f902021-09-20 21:07:45 +0000895}
896
Ady Abraham9dada822022-02-03 10:26:59 -0800897TEST_F(LatchUnsignaledAlwaysTest, Flush_RemovesFromTheQueueDifferentApplyToken) {
898 const sp<IBinder> kApplyToken1 =
899 IInterface::asBinder(TransactionCompletedListener::getIInstance());
900 const sp<IBinder> kApplyToken2 = sp<BBinder>::make();
901 const auto kLayerId1 = 1;
902 const auto kLayerId2 = 2;
Ady Abraham9dada822022-02-03 10:26:59 -0800903 const auto kExpectedTransactionsPending = 0u;
904
905 const auto signaledTransaction =
906 createTransactionInfo(kApplyToken1,
907 {
908 createComposerState(kLayerId1,
909 fence(Fence::Status::Signaled),
910 layer_state_t::eBufferChanged),
911 });
912 const auto unsignaledTransaction =
913 createTransactionInfo(kApplyToken2,
914 {
915 createComposerState(kLayerId2,
916 fence(Fence::Status::Unsignaled),
917 layer_state_t::eBufferChanged),
918 });
Vishnu Nair1523dad2022-09-29 16:05:18 -0700919 setTransactionStates({signaledTransaction, unsignaledTransaction},
Ady Abraham9dada822022-02-03 10:26:59 -0800920 kExpectedTransactionsPending);
ramindani4d48f902021-09-20 21:07:45 +0000921}
922
Ady Abraham9dada822022-02-03 10:26:59 -0800923TEST_F(LatchUnsignaledAlwaysTest, Flush_RemovesUnsignaledFromTheQueueSameApplyToken) {
924 const sp<IBinder> kApplyToken =
925 IInterface::asBinder(TransactionCompletedListener::getIInstance());
926 const auto kLayerId1 = 1;
927 const auto kLayerId2 = 2;
Ady Abraham9dada822022-02-03 10:26:59 -0800928 const auto kExpectedTransactionsPending = 0u;
929
930 const auto unsignaledTransaction =
931 createTransactionInfo(kApplyToken,
932 {
933 createComposerState(kLayerId1,
934 fence(Fence::Status::Unsignaled),
935 layer_state_t::eBufferChanged),
936 });
937 const auto signaledTransaction =
938 createTransactionInfo(kApplyToken,
939 {
940 createComposerState(kLayerId2,
941 fence(Fence::Status::Signaled),
942 layer_state_t::eBufferChanged),
943 });
Vishnu Nair1523dad2022-09-29 16:05:18 -0700944 setTransactionStates({unsignaledTransaction, signaledTransaction},
Ady Abraham9dada822022-02-03 10:26:59 -0800945 kExpectedTransactionsPending);
ramindani4d48f902021-09-20 21:07:45 +0000946}
947
Ady Abraham9dada822022-02-03 10:26:59 -0800948TEST_F(LatchUnsignaledAlwaysTest, Flush_RemovesUnsignaledFromTheQueue) {
949 const sp<IBinder> kApplyToken1 =
950 IInterface::asBinder(TransactionCompletedListener::getIInstance());
951 const sp<IBinder> kApplyToken2 = sp<BBinder>::make();
952 const auto kLayerId1 = 1;
953 const auto kLayerId2 = 2;
Ady Abraham9dada822022-02-03 10:26:59 -0800954 const auto kExpectedTransactionsPending = 0u;
955
956 const auto unsignaledTransaction =
957 createTransactionInfo(kApplyToken1,
958 {
959 createComposerState(kLayerId1,
960 fence(Fence::Status::Unsignaled),
961 layer_state_t::eBufferChanged),
962 });
963 const auto unsignaledTransaction2 =
964 createTransactionInfo(kApplyToken2,
965 {
966 createComposerState(kLayerId2,
967 fence(Fence::Status::Unsignaled),
968 layer_state_t::eBufferChanged),
969 });
970 setTransactionStates({unsignaledTransaction, unsignaledTransaction2},
Vishnu Nair1523dad2022-09-29 16:05:18 -0700971 kExpectedTransactionsPending);
ramindani4d48f902021-09-20 21:07:45 +0000972}
973
Ady Abraham2739e832022-02-14 17:42:00 -0800974TEST_F(LatchUnsignaledAlwaysTest, LatchUnsignaledWhenEarlyOffset) {
975 const sp<IBinder> kApplyToken =
976 IInterface::asBinder(TransactionCompletedListener::getIInstance());
977 const auto kLayerId = 1;
Ady Abraham2739e832022-02-14 17:42:00 -0800978 const auto kExpectedTransactionsPending = 0u;
979
980 const auto unsignaledTransaction =
981 createTransactionInfo(kApplyToken,
982 {
983 createComposerState(kLayerId,
984 fence(Fence::Status::Unsignaled),
985 layer_state_t::eBufferChanged),
986 });
987
988 // Get VsyncModulator out of the default config
989 static_cast<void>(mFlinger.mutableVsyncModulator()->onRefreshRateChangeInitiated());
990
Vishnu Nair1523dad2022-09-29 16:05:18 -0700991 setTransactionStates({unsignaledTransaction}, kExpectedTransactionsPending);
Ady Abraham2739e832022-02-14 17:42:00 -0800992}
993
Vishnu Nair59f6d2d2022-10-05 16:59:56 -0700994TEST(TransactionHandlerTest, QueueTransaction) {
995 TransactionHandler handler;
996 TransactionState transaction;
997 transaction.applyToken = sp<BBinder>::make();
998 transaction.id = 42;
999 handler.queueTransaction(std::move(transaction));
1000 std::vector<TransactionState> transactionsReadyToBeApplied = handler.flushTransactions();
1001
1002 EXPECT_EQ(transactionsReadyToBeApplied.size(), 1u);
1003 EXPECT_EQ(transactionsReadyToBeApplied.front().id, 42u);
1004}
1005
Valerie Haud251afb2019-03-29 14:19:02 -07001006} // namespace android