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; |
| 43 | nsecs_t acquireTime = -1; |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | class TransactionCompletedThread { |
| 47 | public: |
| 48 | ~TransactionCompletedThread(); |
| 49 | |
| 50 | void run(); |
| 51 | |
| 52 | // Informs the TransactionCompletedThread that there is a Transaction with a CallbackHandle |
| 53 | // that needs to be latched and presented this frame. This function should be called once the |
| 54 | // layer has received the CallbackHandle so the TransactionCompletedThread knows not to send |
| 55 | // a callback for that Listener/Transaction pair until that CallbackHandle has been latched and |
| 56 | // presented. |
| 57 | void registerPendingLatchedCallbackHandle(const sp<CallbackHandle>& handle); |
| 58 | // Notifies the TransactionCompletedThread that a pending CallbackHandle has been latched. |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 59 | void addLatchedCallbackHandles(const std::deque<sp<CallbackHandle>>& handles, nsecs_t latchTime, |
| 60 | const sp<Fence>& previousReleaseFence); |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 61 | |
| 62 | // Adds the Transaction CallbackHandle from a layer that does not need to be relatched and |
| 63 | // presented this frame. |
| 64 | void addUnlatchedCallbackHandle(const sp<CallbackHandle>& handle); |
| 65 | |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 66 | void addPresentFence(const sp<Fence>& presentFence); |
| 67 | |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 68 | void sendCallbacks(); |
| 69 | |
| 70 | private: |
| 71 | void threadMain(); |
| 72 | |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 73 | void addCallbackHandle(const sp<CallbackHandle>& handle, nsecs_t latchTime = -1) |
| 74 | REQUIRES(mMutex); |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 75 | |
| 76 | class ThreadDeathRecipient : public IBinder::DeathRecipient { |
| 77 | public: |
| 78 | // This function is a no-op. isBinderAlive needs a linked DeathRecipient to work. |
| 79 | // Death recipients needs a binderDied function. |
| 80 | // |
| 81 | // (isBinderAlive checks if BpBinder's mAlive is 0. mAlive is only set to 0 in sendObituary. |
| 82 | // sendObituary is only called if linkToDeath was called with a DeathRecipient.) |
| 83 | void binderDied(const wp<IBinder>& /*who*/) override {} |
| 84 | }; |
| 85 | sp<ThreadDeathRecipient> mDeathRecipient; |
| 86 | |
| 87 | struct IBinderHash { |
| 88 | std::size_t operator()(const sp<IBinder>& strongPointer) const { |
| 89 | return std::hash<IBinder*>{}(strongPointer.get()); |
| 90 | } |
| 91 | }; |
| 92 | |
Marissa Wall | 99343ba | 2018-11-13 10:39:08 -0800 | [diff] [blame^] | 93 | // Protects the creation and destruction of mThread |
| 94 | std::mutex mThreadMutex; |
| 95 | |
| 96 | std::thread mThread GUARDED_BY(mThreadMutex); |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 97 | |
| 98 | std::mutex mMutex; |
| 99 | std::condition_variable_any mConditionVariable; |
| 100 | |
| 101 | std::unordered_map< |
| 102 | sp<IBinder /*listener*/>, |
| 103 | std::unordered_map<std::vector<CallbackId>, uint32_t /*count*/, CallbackIdsHash>, |
| 104 | IBinderHash> |
| 105 | mPendingTransactions GUARDED_BY(mMutex); |
| 106 | std::unordered_map<sp<IBinder /*listener*/>, ListenerStats, IBinderHash> mListenerStats |
| 107 | GUARDED_BY(mMutex); |
| 108 | |
| 109 | bool mRunning GUARDED_BY(mMutex) = false; |
| 110 | bool mKeepRunning GUARDED_BY(mMutex) = true; |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 111 | |
| 112 | sp<Fence> mPresentFence GUARDED_BY(mMutex); |
| 113 | std::vector<sp<Fence>> mPreviousReleaseFences GUARDED_BY(mMutex); |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | } // namespace android |