Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 1 | /* |
| 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 Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 21 | #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 | |
| 29 | namespace android { |
Vishnu Nair | af6d297 | 2022-11-18 06:26:38 +0000 | [diff] [blame^] | 30 | |
| 31 | class TestableSurfaceFlinger; |
| 32 | namespace surfaceflinger::frontend { |
| 33 | |
Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 34 | class TransactionHandler { |
| 35 | public: |
| 36 | struct TransactionFlushState { |
| 37 | const TransactionState* transaction; |
| 38 | 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 Williams | f1e5df1 | 2022-10-17 21:37:42 +0000 | [diff] [blame] | 58 | void onTransactionQueueStalled(uint64_t transactionId, sp<ITransactionCompletedListener>&, |
| 59 | const std::string& reason); |
Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 60 | void removeFromStalledTransactions(uint64_t transactionId); |
| 61 | |
| 62 | private: |
| 63 | // For unit tests |
Vishnu Nair | af6d297 | 2022-11-18 06:26:38 +0000 | [diff] [blame^] | 64 | friend class ::android::TestableSurfaceFlinger; |
Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 65 | |
| 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 Nair | af6d297 | 2022-11-18 06:26:38 +0000 | [diff] [blame^] | 75 | } // namespace surfaceflinger::frontend |
Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 76 | } // namespace android |