blob: 04183bcdb8f44b3dd3dda43940dc010fdcd6e918 [file] [log] [blame]
Vishnu Nair59f6d2d2022-10-05 16:59:56 -07001/*
2 * Copyright 2022 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#pragma once
18
19#include <semaphore.h>
20#include <cstdint>
Vishnu Nair59f6d2d2022-10-05 16:59:56 -070021#include <vector>
22
23#include <LocklessQueue.h>
24#include <TransactionState.h>
25#include <android-base/thread_annotations.h>
26#include <ftl/small_map.h>
27#include <ftl/small_vector.h>
28
29namespace android {
Vishnu Nairaf6d2972022-11-18 06:26:38 +000030
31class TestableSurfaceFlinger;
32namespace surfaceflinger::frontend {
33
Vishnu Nair59f6d2d2022-10-05 16:59:56 -070034class TransactionHandler {
35public:
36 struct TransactionFlushState {
liulijuneb489f62022-10-17 22:02:14 +080037 TransactionState* transaction;
Vishnu Nair59f6d2d2022-10-05 16:59:56 -070038 bool firstTransaction = true;
39 nsecs_t queueProcessTime = 0;
40 // Layer handles that have transactions with buffers that are ready to be applied.
41 ftl::SmallMap<IBinder* /* binder address */, uint64_t /* framenumber */, 15>
42 bufferLayersReadyToPresent = {};
Vishnu Nairf01a6f12023-04-03 22:34:17 +000043 // Tracks the queue with an unsignaled buffer. This is used to handle
44 // LatchUnsignaledConfig::AutoSingleLayer to ensure we only apply an unsignaled buffer
45 // if it's the only transaction that is ready to be applied.
46 sp<IBinder> queueWithUnsignaledBuffer = nullptr;
Vishnu Nair59f6d2d2022-10-05 16:59:56 -070047 };
48 enum class TransactionReadiness {
Vishnu Nairf01a6f12023-04-03 22:34:17 +000049 // Transaction is ready to be applied
Vishnu Nair59f6d2d2022-10-05 16:59:56 -070050 Ready,
Vishnu Nairf01a6f12023-04-03 22:34:17 +000051 // Transaction has unmet conditions (fence, present time, etc) and cannot be applied.
52 NotReady,
53 // Transaction is waiting on a barrier (another buffer to be latched first)
54 NotReadyBarrier,
55 // Transaction has an unsignaled fence but can be applied if it's the only transaction
56 NotReadyUnsignaled,
Vishnu Nair59f6d2d2022-10-05 16:59:56 -070057 };
58 using TransactionFilter = std::function<TransactionReadiness(const TransactionFlushState&)>;
59
60 bool hasPendingTransactions();
Vishnu Nair4d9cef92023-06-24 22:34:41 +000061 // Moves transactions from the lockless queue.
62 void collectTransactions();
Vishnu Nair59f6d2d2022-10-05 16:59:56 -070063 std::vector<TransactionState> flushTransactions();
64 void addTransactionReadyFilter(TransactionFilter&&);
65 void queueTransaction(TransactionState&&);
Patrick Williamsf1e5df12022-10-17 21:37:42 +000066 void onTransactionQueueStalled(uint64_t transactionId, sp<ITransactionCompletedListener>&,
67 const std::string& reason);
Vishnu Nair59f6d2d2022-10-05 16:59:56 -070068 void removeFromStalledTransactions(uint64_t transactionId);
69
70private:
71 // For unit tests
Vishnu Nairaf6d2972022-11-18 06:26:38 +000072 friend class ::android::TestableSurfaceFlinger;
Vishnu Nair59f6d2d2022-10-05 16:59:56 -070073
74 int flushPendingTransactionQueues(std::vector<TransactionState>&, TransactionFlushState&);
Vishnu Nairf01a6f12023-04-03 22:34:17 +000075 void applyUnsignaledBufferTransaction(std::vector<TransactionState>&, TransactionFlushState&);
76 void popTransactionFromPending(std::vector<TransactionState>&, TransactionFlushState&,
77 std::queue<TransactionState>&);
Vishnu Nair59f6d2d2022-10-05 16:59:56 -070078 TransactionReadiness applyFilters(TransactionFlushState&);
79 std::unordered_map<sp<IBinder>, std::queue<TransactionState>, IListenerHash>
80 mPendingTransactionQueues;
81 LocklessQueue<TransactionState> mLocklessTransactionQueue;
82 std::atomic<size_t> mPendingTransactionCount = 0;
83 ftl::SmallVector<TransactionFilter, 2> mTransactionReadyFilters;
84 std::vector<uint64_t> mStalledTransactions;
85};
Vishnu Nairaf6d2972022-11-18 06:26:38 +000086} // namespace surfaceflinger::frontend
Vishnu Nair59f6d2d2022-10-05 16:59:56 -070087} // namespace android