blob: 39cf7e877c432b8d35b93f265c8e891aef591730 [file] [log] [blame]
Marissa Walle2ffb422018-10-12 11:33:52 -07001/*
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>
29
30namespace android {
31
32class CallbackHandle : public RefBase {
33public:
34 CallbackHandle(const sp<ITransactionCompletedListener>& transactionListener,
35 const std::vector<CallbackId>& ids, const sp<IBinder>& sc);
36
37 sp<ITransactionCompletedListener> listener;
38 std::vector<CallbackId> callbackIds;
39 sp<IBinder> surfaceControl;
40};
41
42class TransactionCompletedThread {
43public:
44 ~TransactionCompletedThread();
45
46 void run();
47
48 // Informs the TransactionCompletedThread that there is a Transaction with a CallbackHandle
49 // that needs to be latched and presented this frame. This function should be called once the
50 // layer has received the CallbackHandle so the TransactionCompletedThread knows not to send
51 // a callback for that Listener/Transaction pair until that CallbackHandle has been latched and
52 // presented.
53 void registerPendingLatchedCallbackHandle(const sp<CallbackHandle>& handle);
54 // Notifies the TransactionCompletedThread that a pending CallbackHandle has been latched.
55 void addLatchedCallbackHandles(const std::deque<sp<CallbackHandle>>& handles);
56
57 // Adds the Transaction CallbackHandle from a layer that does not need to be relatched and
58 // presented this frame.
59 void addUnlatchedCallbackHandle(const sp<CallbackHandle>& handle);
60
61 void sendCallbacks();
62
63private:
64 void threadMain();
65
66 void addCallbackHandle(const sp<CallbackHandle>& handle) REQUIRES(mMutex);
67
68 class ThreadDeathRecipient : public IBinder::DeathRecipient {
69 public:
70 // This function is a no-op. isBinderAlive needs a linked DeathRecipient to work.
71 // Death recipients needs a binderDied function.
72 //
73 // (isBinderAlive checks if BpBinder's mAlive is 0. mAlive is only set to 0 in sendObituary.
74 // sendObituary is only called if linkToDeath was called with a DeathRecipient.)
75 void binderDied(const wp<IBinder>& /*who*/) override {}
76 };
77 sp<ThreadDeathRecipient> mDeathRecipient;
78
79 struct IBinderHash {
80 std::size_t operator()(const sp<IBinder>& strongPointer) const {
81 return std::hash<IBinder*>{}(strongPointer.get());
82 }
83 };
84
85 std::thread mThread;
86
87 std::mutex mMutex;
88 std::condition_variable_any mConditionVariable;
89
90 std::unordered_map<
91 sp<IBinder /*listener*/>,
92 std::unordered_map<std::vector<CallbackId>, uint32_t /*count*/, CallbackIdsHash>,
93 IBinderHash>
94 mPendingTransactions GUARDED_BY(mMutex);
95 std::unordered_map<sp<IBinder /*listener*/>, ListenerStats, IBinderHash> mListenerStats
96 GUARDED_BY(mMutex);
97
98 bool mRunning GUARDED_BY(mMutex) = false;
99 bool mKeepRunning GUARDED_BY(mMutex) = true;
100};
101
102} // namespace android