Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 <condition_variable> |
| 20 | #include <deque> |
| 21 | #include <mutex> |
| 22 | #include <thread> |
| 23 | #include <unordered_map> |
| 24 | |
| 25 | #include <android-base/thread_annotations.h> |
| 26 | |
| 27 | #include <binder/IBinder.h> |
| 28 | #include <gui/ITransactionCompletedListener.h> |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 29 | #include <ui/Fence.h> |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | |
| 33 | class CallbackHandle : public RefBase { |
| 34 | public: |
| 35 | CallbackHandle(const sp<ITransactionCompletedListener>& transactionListener, |
| 36 | const std::vector<CallbackId>& ids, const sp<IBinder>& sc); |
| 37 | |
| 38 | sp<ITransactionCompletedListener> listener; |
| 39 | std::vector<CallbackId> callbackIds; |
| 40 | sp<IBinder> surfaceControl; |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 41 | |
| 42 | bool releasePreviousBuffer = false; |
Marissa Wall | 5a68a77 | 2018-12-22 17:43:42 -0800 | [diff] [blame] | 43 | sp<Fence> previousReleaseFence; |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 44 | nsecs_t acquireTime = -1; |
Marissa Wall | 5a68a77 | 2018-12-22 17:43:42 -0800 | [diff] [blame] | 45 | nsecs_t latchTime = -1; |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | class TransactionCompletedThread { |
| 49 | public: |
| 50 | ~TransactionCompletedThread(); |
| 51 | |
| 52 | void run(); |
| 53 | |
| 54 | // Informs the TransactionCompletedThread that there is a Transaction with a CallbackHandle |
| 55 | // that needs to be latched and presented this frame. This function should be called once the |
| 56 | // layer has received the CallbackHandle so the TransactionCompletedThread knows not to send |
| 57 | // a callback for that Listener/Transaction pair until that CallbackHandle has been latched and |
| 58 | // presented. |
Marissa Wall | 5a68a77 | 2018-12-22 17:43:42 -0800 | [diff] [blame] | 59 | void registerPendingCallbackHandle(const sp<CallbackHandle>& handle); |
| 60 | // Notifies the TransactionCompletedThread that a pending CallbackHandle has been presented. |
| 61 | void addPresentedCallbackHandles(const std::deque<sp<CallbackHandle>>& handles); |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 62 | |
| 63 | // Adds the Transaction CallbackHandle from a layer that does not need to be relatched and |
| 64 | // presented this frame. |
Marissa Wall | 5a68a77 | 2018-12-22 17:43:42 -0800 | [diff] [blame] | 65 | void addUnpresentedCallbackHandle(const sp<CallbackHandle>& handle); |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 66 | |
Marissa Wall | 3dad52d | 2019-03-22 14:03:19 -0700 | [diff] [blame] | 67 | // Adds listener and callbackIds in case there are no SurfaceControls that are supposed |
| 68 | // to be included in the callback. |
| 69 | void addCallback(const sp<ITransactionCompletedListener>& transactionListener, |
| 70 | const std::vector<CallbackId>& callbackIds); |
| 71 | |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 72 | void addPresentFence(const sp<Fence>& presentFence); |
| 73 | |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 74 | void sendCallbacks(); |
| 75 | |
| 76 | private: |
| 77 | void threadMain(); |
| 78 | |
Marissa Wall | 3dad52d | 2019-03-22 14:03:19 -0700 | [diff] [blame] | 79 | status_t addCallbackHandle(const sp<CallbackHandle>& handle) REQUIRES(mMutex); |
| 80 | status_t addCallbackLocked(const sp<ITransactionCompletedListener>& transactionListener, |
| 81 | const std::vector<CallbackId>& callbackIds) REQUIRES(mMutex); |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 82 | |
| 83 | class ThreadDeathRecipient : public IBinder::DeathRecipient { |
| 84 | public: |
| 85 | // This function is a no-op. isBinderAlive needs a linked DeathRecipient to work. |
| 86 | // Death recipients needs a binderDied function. |
| 87 | // |
| 88 | // (isBinderAlive checks if BpBinder's mAlive is 0. mAlive is only set to 0 in sendObituary. |
| 89 | // sendObituary is only called if linkToDeath was called with a DeathRecipient.) |
| 90 | void binderDied(const wp<IBinder>& /*who*/) override {} |
| 91 | }; |
| 92 | sp<ThreadDeathRecipient> mDeathRecipient; |
| 93 | |
| 94 | struct IBinderHash { |
| 95 | std::size_t operator()(const sp<IBinder>& strongPointer) const { |
| 96 | return std::hash<IBinder*>{}(strongPointer.get()); |
| 97 | } |
| 98 | }; |
| 99 | |
Marissa Wall | 99343ba | 2018-11-13 10:39:08 -0800 | [diff] [blame] | 100 | // Protects the creation and destruction of mThread |
| 101 | std::mutex mThreadMutex; |
| 102 | |
| 103 | std::thread mThread GUARDED_BY(mThreadMutex); |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 104 | |
| 105 | std::mutex mMutex; |
| 106 | std::condition_variable_any mConditionVariable; |
| 107 | |
| 108 | std::unordered_map< |
| 109 | sp<IBinder /*listener*/>, |
| 110 | std::unordered_map<std::vector<CallbackId>, uint32_t /*count*/, CallbackIdsHash>, |
| 111 | IBinderHash> |
| 112 | mPendingTransactions GUARDED_BY(mMutex); |
| 113 | std::unordered_map<sp<IBinder /*listener*/>, ListenerStats, IBinderHash> mListenerStats |
| 114 | GUARDED_BY(mMutex); |
| 115 | |
| 116 | bool mRunning GUARDED_BY(mMutex) = false; |
| 117 | bool mKeepRunning GUARDED_BY(mMutex) = true; |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 118 | |
| 119 | sp<Fence> mPresentFence GUARDED_BY(mMutex); |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | } // namespace android |