blob: 237b48d55fd32e37473273aeab07202a3e9bccd1 [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>
21#include <mutex>
22#include <queue>
23#include <thread>
24#include <vector>
25
26#include <LocklessQueue.h>
27#include <TransactionState.h>
28#include <android-base/thread_annotations.h>
29#include <ftl/small_map.h>
30#include <ftl/small_vector.h>
31
32namespace android {
33class TransactionHandler {
34public:
35 struct TransactionFlushState {
36 const TransactionState* transaction;
37 bool firstTransaction = true;
38 nsecs_t queueProcessTime = 0;
39 // Layer handles that have transactions with buffers that are ready to be applied.
40 ftl::SmallMap<IBinder* /* binder address */, uint64_t /* framenumber */, 15>
41 bufferLayersReadyToPresent = {};
42 ftl::SmallVector<IBinder* /* queueToken */, 15> queuesWithUnsignaledBuffers;
43 };
44 enum class TransactionReadiness {
45 NotReady,
46 NotReadyBarrier,
47 Ready,
48 ReadyUnsignaled,
49 ReadyUnsignaledSingle,
50 };
51 using TransactionFilter = std::function<TransactionReadiness(const TransactionFlushState&)>;
52
53 bool hasPendingTransactions();
54 std::vector<TransactionState> flushTransactions();
55 void addTransactionReadyFilter(TransactionFilter&&);
56 void queueTransaction(TransactionState&&);
57 void onTransactionQueueStalled(const TransactionState&, sp<ITransactionCompletedListener>&);
58 void removeFromStalledTransactions(uint64_t transactionId);
59
60private:
61 // For unit tests
62 friend class TestableSurfaceFlinger;
63
64 int flushPendingTransactionQueues(std::vector<TransactionState>&, TransactionFlushState&);
65 TransactionReadiness applyFilters(TransactionFlushState&);
66 std::unordered_map<sp<IBinder>, std::queue<TransactionState>, IListenerHash>
67 mPendingTransactionQueues;
68 LocklessQueue<TransactionState> mLocklessTransactionQueue;
69 std::atomic<size_t> mPendingTransactionCount = 0;
70 ftl::SmallVector<TransactionFilter, 2> mTransactionReadyFilters;
71 std::vector<uint64_t> mStalledTransactions;
72};
73
74} // namespace android