blob: 7fc825eba3ad7323cf719de08477aee174bedc67 [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 = {};
43 ftl::SmallVector<IBinder* /* queueToken */, 15> queuesWithUnsignaledBuffers;
44 };
45 enum class TransactionReadiness {
46 NotReady,
47 NotReadyBarrier,
48 Ready,
49 ReadyUnsignaled,
50 ReadyUnsignaledSingle,
51 };
52 using TransactionFilter = std::function<TransactionReadiness(const TransactionFlushState&)>;
53
54 bool hasPendingTransactions();
55 std::vector<TransactionState> flushTransactions();
56 void addTransactionReadyFilter(TransactionFilter&&);
57 void queueTransaction(TransactionState&&);
Patrick Williamsf1e5df12022-10-17 21:37:42 +000058 void onTransactionQueueStalled(uint64_t transactionId, sp<ITransactionCompletedListener>&,
59 const std::string& reason);
Vishnu Nair59f6d2d2022-10-05 16:59:56 -070060 void removeFromStalledTransactions(uint64_t transactionId);
61
62private:
63 // For unit tests
Vishnu Nairaf6d2972022-11-18 06:26:38 +000064 friend class ::android::TestableSurfaceFlinger;
Vishnu Nair59f6d2d2022-10-05 16:59:56 -070065
66 int flushPendingTransactionQueues(std::vector<TransactionState>&, TransactionFlushState&);
67 TransactionReadiness applyFilters(TransactionFlushState&);
68 std::unordered_map<sp<IBinder>, std::queue<TransactionState>, IListenerHash>
69 mPendingTransactionQueues;
70 LocklessQueue<TransactionState> mLocklessTransactionQueue;
71 std::atomic<size_t> mPendingTransactionCount = 0;
72 ftl::SmallVector<TransactionFilter, 2> mTransactionReadyFilters;
73 std::vector<uint64_t> mStalledTransactions;
74};
Vishnu Nairaf6d2972022-11-18 06:26:38 +000075} // namespace surfaceflinger::frontend
Vishnu Nair59f6d2d2022-10-05 16:59:56 -070076} // namespace android