blob: 865835f92d0e6dc16f935971b19bdafadd1206e4 [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();
61 std::vector<TransactionState> flushTransactions();
62 void addTransactionReadyFilter(TransactionFilter&&);
63 void queueTransaction(TransactionState&&);
Patrick Williamsf1e5df12022-10-17 21:37:42 +000064 void onTransactionQueueStalled(uint64_t transactionId, sp<ITransactionCompletedListener>&,
65 const std::string& reason);
Vishnu Nair59f6d2d2022-10-05 16:59:56 -070066 void removeFromStalledTransactions(uint64_t transactionId);
67
68private:
69 // For unit tests
Vishnu Nairaf6d2972022-11-18 06:26:38 +000070 friend class ::android::TestableSurfaceFlinger;
Vishnu Nair59f6d2d2022-10-05 16:59:56 -070071
72 int flushPendingTransactionQueues(std::vector<TransactionState>&, TransactionFlushState&);
Vishnu Nairf01a6f12023-04-03 22:34:17 +000073 void applyUnsignaledBufferTransaction(std::vector<TransactionState>&, TransactionFlushState&);
74 void popTransactionFromPending(std::vector<TransactionState>&, TransactionFlushState&,
75 std::queue<TransactionState>&);
Vishnu Nair59f6d2d2022-10-05 16:59:56 -070076 TransactionReadiness applyFilters(TransactionFlushState&);
77 std::unordered_map<sp<IBinder>, std::queue<TransactionState>, IListenerHash>
78 mPendingTransactionQueues;
79 LocklessQueue<TransactionState> mLocklessTransactionQueue;
80 std::atomic<size_t> mPendingTransactionCount = 0;
81 ftl::SmallVector<TransactionFilter, 2> mTransactionReadyFilters;
82 std::vector<uint64_t> mStalledTransactions;
83};
Vishnu Nairaf6d2972022-11-18 06:26:38 +000084} // namespace surfaceflinger::frontend
Vishnu Nair59f6d2d2022-10-05 16:59:56 -070085} // namespace android