| 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> | 
| Patrick Williams | cee446c | 2023-08-08 12:30:10 -0500 | [diff] [blame] | 21 | #include <optional> | 
| Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 22 | #include <vector> | 
|  | 23 |  | 
|  | 24 | #include <LocklessQueue.h> | 
|  | 25 | #include <TransactionState.h> | 
|  | 26 | #include <android-base/thread_annotations.h> | 
|  | 27 | #include <ftl/small_map.h> | 
|  | 28 | #include <ftl/small_vector.h> | 
|  | 29 |  | 
|  | 30 | namespace android { | 
| Vishnu Nair | af6d297 | 2022-11-18 06:26:38 +0000 | [diff] [blame] | 31 |  | 
|  | 32 | class TestableSurfaceFlinger; | 
|  | 33 | namespace surfaceflinger::frontend { | 
|  | 34 |  | 
| Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 35 | class TransactionHandler { | 
|  | 36 | public: | 
|  | 37 | struct TransactionFlushState { | 
| liulijun | eb489f6 | 2022-10-17 22:02:14 +0800 | [diff] [blame] | 38 | TransactionState* transaction; | 
| Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 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 = {}; | 
| Vishnu Nair | f01a6f1 | 2023-04-03 22:34:17 +0000 | [diff] [blame] | 44 | // Tracks the queue with an unsignaled buffer. This is used to handle | 
|  | 45 | // LatchUnsignaledConfig::AutoSingleLayer to ensure we only apply an unsignaled buffer | 
|  | 46 | // if it's the only transaction that is ready to be applied. | 
|  | 47 | sp<IBinder> queueWithUnsignaledBuffer = nullptr; | 
| Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 48 | }; | 
|  | 49 | enum class TransactionReadiness { | 
| Vishnu Nair | f01a6f1 | 2023-04-03 22:34:17 +0000 | [diff] [blame] | 50 | // Transaction is ready to be applied | 
| Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 51 | Ready, | 
| Vishnu Nair | f01a6f1 | 2023-04-03 22:34:17 +0000 | [diff] [blame] | 52 | // Transaction has unmet conditions (fence, present time, etc) and cannot be applied. | 
|  | 53 | NotReady, | 
|  | 54 | // Transaction is waiting on a barrier (another buffer to be latched first) | 
|  | 55 | NotReadyBarrier, | 
|  | 56 | // Transaction has an unsignaled fence but can be applied if it's the only transaction | 
|  | 57 | NotReadyUnsignaled, | 
| Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 58 | }; | 
|  | 59 | using TransactionFilter = std::function<TransactionReadiness(const TransactionFlushState&)>; | 
|  | 60 |  | 
|  | 61 | bool hasPendingTransactions(); | 
|  | 62 | std::vector<TransactionState> flushTransactions(); | 
|  | 63 | void addTransactionReadyFilter(TransactionFilter&&); | 
|  | 64 | void queueTransaction(TransactionState&&); | 
| Patrick Williams | cee446c | 2023-08-08 12:30:10 -0500 | [diff] [blame] | 65 |  | 
|  | 66 | struct StalledTransactionInfo { | 
|  | 67 | pid_t pid; | 
|  | 68 | uint32_t layerId; | 
|  | 69 | std::string layerName; | 
|  | 70 | uint64_t bufferId; | 
|  | 71 | uint64_t frameNumber; | 
|  | 72 | }; | 
|  | 73 | void onTransactionQueueStalled(uint64_t transactionId, StalledTransactionInfo); | 
| Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 74 | void removeFromStalledTransactions(uint64_t transactionId); | 
| Patrick Williams | cee446c | 2023-08-08 12:30:10 -0500 | [diff] [blame] | 75 | std::optional<StalledTransactionInfo> getStalledTransactionInfo(pid_t pid); | 
|  | 76 | void onLayerDestroyed(uint32_t layerId); | 
| Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 77 |  | 
|  | 78 | private: | 
|  | 79 | // For unit tests | 
| Vishnu Nair | af6d297 | 2022-11-18 06:26:38 +0000 | [diff] [blame] | 80 | friend class ::android::TestableSurfaceFlinger; | 
| Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 81 |  | 
|  | 82 | int flushPendingTransactionQueues(std::vector<TransactionState>&, TransactionFlushState&); | 
| Vishnu Nair | f01a6f1 | 2023-04-03 22:34:17 +0000 | [diff] [blame] | 83 | void applyUnsignaledBufferTransaction(std::vector<TransactionState>&, TransactionFlushState&); | 
|  | 84 | void popTransactionFromPending(std::vector<TransactionState>&, TransactionFlushState&, | 
|  | 85 | std::queue<TransactionState>&); | 
| Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 86 | TransactionReadiness applyFilters(TransactionFlushState&); | 
|  | 87 | std::unordered_map<sp<IBinder>, std::queue<TransactionState>, IListenerHash> | 
|  | 88 | mPendingTransactionQueues; | 
|  | 89 | LocklessQueue<TransactionState> mLocklessTransactionQueue; | 
|  | 90 | std::atomic<size_t> mPendingTransactionCount = 0; | 
|  | 91 | ftl::SmallVector<TransactionFilter, 2> mTransactionReadyFilters; | 
| Patrick Williams | cee446c | 2023-08-08 12:30:10 -0500 | [diff] [blame] | 92 |  | 
|  | 93 | std::mutex mStalledMutex; | 
|  | 94 | std::unordered_map<uint64_t /* transactionId */, StalledTransactionInfo> mStalledTransactions | 
|  | 95 | GUARDED_BY(mStalledMutex); | 
| Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 96 | }; | 
| Vishnu Nair | af6d297 | 2022-11-18 06:26:38 +0000 | [diff] [blame] | 97 | } // namespace surfaceflinger::frontend | 
| Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 98 | } // namespace android |