blob: 475ff1b1dc430a9e0a15c8349f8cb82d0100ae91 [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;
Huihong Luoc50b4982021-11-24 12:34:52 -080032using gui::IListenerHash;
Vishnu Nairaf6d2972022-11-18 06:26:38 +000033namespace surfaceflinger::frontend {
34
Vishnu Nair59f6d2d2022-10-05 16:59:56 -070035class TransactionHandler {
36public:
37 struct TransactionFlushState {
38 const TransactionState* transaction;
39 bool firstTransaction = true;
40 nsecs_t queueProcessTime = 0;
41 // Layer handles that have transactions with buffers that are ready to be applied.
42 ftl::SmallMap<IBinder* /* binder address */, uint64_t /* framenumber */, 15>
43 bufferLayersReadyToPresent = {};
44 ftl::SmallVector<IBinder* /* queueToken */, 15> queuesWithUnsignaledBuffers;
45 };
46 enum class TransactionReadiness {
47 NotReady,
48 NotReadyBarrier,
49 Ready,
50 ReadyUnsignaled,
51 ReadyUnsignaledSingle,
52 };
53 using TransactionFilter = std::function<TransactionReadiness(const TransactionFlushState&)>;
54
55 bool hasPendingTransactions();
56 std::vector<TransactionState> flushTransactions();
57 void addTransactionReadyFilter(TransactionFilter&&);
58 void queueTransaction(TransactionState&&);
Patrick Williamsf1e5df12022-10-17 21:37:42 +000059 void onTransactionQueueStalled(uint64_t transactionId, sp<ITransactionCompletedListener>&,
60 const std::string& reason);
Vishnu Nair59f6d2d2022-10-05 16:59:56 -070061 void removeFromStalledTransactions(uint64_t transactionId);
62
63private:
64 // For unit tests
Vishnu Nairaf6d2972022-11-18 06:26:38 +000065 friend class ::android::TestableSurfaceFlinger;
Vishnu Nair59f6d2d2022-10-05 16:59:56 -070066
67 int flushPendingTransactionQueues(std::vector<TransactionState>&, TransactionFlushState&);
68 TransactionReadiness applyFilters(TransactionFlushState&);
69 std::unordered_map<sp<IBinder>, std::queue<TransactionState>, IListenerHash>
70 mPendingTransactionQueues;
71 LocklessQueue<TransactionState> mLocklessTransactionQueue;
72 std::atomic<size_t> mPendingTransactionCount = 0;
73 ftl::SmallVector<TransactionFilter, 2> mTransactionReadyFilters;
74 std::vector<uint64_t> mStalledTransactions;
75};
Vishnu Nairaf6d2972022-11-18 06:26:38 +000076} // namespace surfaceflinger::frontend
Vishnu Nair59f6d2d2022-10-05 16:59:56 -070077} // namespace android