Vishnu Nair | 6b59115 | 2021-10-08 11:45:14 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2021 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 <gui/LayerState.h> |
| 20 | |
| 21 | namespace android { |
| 22 | class CountDownLatch; |
| 23 | |
| 24 | struct TransactionState { |
| 25 | TransactionState(const FrameTimelineInfo& frameTimelineInfo, |
| 26 | const Vector<ComposerState>& composerStates, |
| 27 | const Vector<DisplayState>& displayStates, uint32_t transactionFlags, |
| 28 | const sp<IBinder>& applyToken, const InputWindowCommands& inputWindowCommands, |
| 29 | int64_t desiredPresentTime, bool isAutoTimestamp, |
| 30 | const client_cache_t& uncacheBuffer, int64_t postTime, uint32_t permissions, |
| 31 | bool hasListenerCallbacks, std::vector<ListenerCallbacks> listenerCallbacks, |
| 32 | int originPid, int originUid, uint64_t transactionId) |
| 33 | : frameTimelineInfo(frameTimelineInfo), |
| 34 | states(composerStates), |
| 35 | displays(displayStates), |
| 36 | flags(transactionFlags), |
| 37 | applyToken(applyToken), |
| 38 | inputWindowCommands(inputWindowCommands), |
| 39 | desiredPresentTime(desiredPresentTime), |
| 40 | isAutoTimestamp(isAutoTimestamp), |
| 41 | buffer(uncacheBuffer), |
| 42 | postTime(postTime), |
| 43 | permissions(permissions), |
| 44 | hasListenerCallbacks(hasListenerCallbacks), |
| 45 | listenerCallbacks(listenerCallbacks), |
| 46 | originPid(originPid), |
| 47 | originUid(originUid), |
| 48 | id(transactionId) {} |
| 49 | |
| 50 | TransactionState() {} |
| 51 | |
| 52 | void traverseStatesWithBuffers(std::function<void(const layer_state_t&)> visitor); |
| 53 | |
| 54 | FrameTimelineInfo frameTimelineInfo; |
| 55 | Vector<ComposerState> states; |
| 56 | Vector<DisplayState> displays; |
| 57 | uint32_t flags; |
| 58 | sp<IBinder> applyToken; |
| 59 | InputWindowCommands inputWindowCommands; |
| 60 | int64_t desiredPresentTime; |
| 61 | bool isAutoTimestamp; |
| 62 | client_cache_t buffer; |
| 63 | int64_t postTime; |
| 64 | uint32_t permissions; |
| 65 | bool hasListenerCallbacks; |
| 66 | std::vector<ListenerCallbacks> listenerCallbacks; |
| 67 | int originPid; |
| 68 | int originUid; |
| 69 | uint64_t id; |
| 70 | std::shared_ptr<CountDownLatch> transactionCommittedSignal; |
| 71 | }; |
| 72 | |
| 73 | class CountDownLatch { |
| 74 | public: |
| 75 | enum { |
| 76 | eSyncTransaction = 1 << 0, |
| 77 | eSyncInputWindows = 1 << 1, |
| 78 | }; |
| 79 | explicit CountDownLatch(uint32_t flags) : mFlags(flags) {} |
| 80 | |
| 81 | // True if there is no waiting condition after count down. |
| 82 | bool countDown(uint32_t flag) { |
| 83 | std::unique_lock<std::mutex> lock(mMutex); |
| 84 | if (mFlags == 0) { |
| 85 | return true; |
| 86 | } |
| 87 | mFlags &= ~flag; |
| 88 | if (mFlags == 0) { |
| 89 | mCountDownComplete.notify_all(); |
| 90 | return true; |
| 91 | } |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | // Return true if triggered. |
| 96 | bool wait_until(const std::chrono::seconds& timeout) const { |
| 97 | std::unique_lock<std::mutex> lock(mMutex); |
| 98 | const auto untilTime = std::chrono::system_clock::now() + timeout; |
| 99 | while (mFlags != 0) { |
| 100 | // Conditional variables can be woken up sporadically, so we check count |
| 101 | // to verify the wakeup was triggered by |countDown|. |
| 102 | if (std::cv_status::timeout == mCountDownComplete.wait_until(lock, untilTime)) { |
| 103 | return false; |
| 104 | } |
| 105 | } |
| 106 | return true; |
| 107 | } |
| 108 | |
| 109 | private: |
| 110 | uint32_t mFlags; |
| 111 | mutable std::condition_variable mCountDownComplete; |
| 112 | mutable std::mutex mMutex; |
| 113 | }; |
| 114 | |
| 115 | } // namespace android |