blob: e255e5090eb0e05c7859b5f2176eff4963c87432 [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>
Marissa Wallefb71af2019-06-27 14:45:53 -070024#include <unordered_set>
Marissa Walle2ffb422018-10-12 11:33:52 -070025
26#include <android-base/thread_annotations.h>
27
28#include <binder/IBinder.h>
29#include <gui/ITransactionCompletedListener.h>
Marissa Wallfda30bb2018-10-12 11:34:28 -070030#include <ui/Fence.h>
Marissa Walle2ffb422018-10-12 11:33:52 -070031
32namespace android {
33
Marissa Wallefb71af2019-06-27 14:45:53 -070034struct ITransactionCompletedListenerHash {
35 std::size_t operator()(const sp<ITransactionCompletedListener>& listener) const {
36 return std::hash<IBinder*>{}((listener) ? IInterface::asBinder(listener).get() : nullptr);
37 }
38};
39
Marissa Walld600d572019-03-26 15:38:50 -070040struct CallbackIdsHash {
41 // CallbackId vectors have several properties that let us get away with this simple hash.
42 // 1) CallbackIds are never 0 so if something has gone wrong and our CallbackId vector is
43 // empty we can still hash 0.
44 // 2) CallbackId vectors for the same listener either are identical or contain none of the
45 // same members. It is sufficient to just check the first CallbackId in the vectors. If
46 // they match, they are the same. If they do not match, they are not the same.
Greg Kaisera9e843a2019-04-01 06:23:09 -070047 std::size_t operator()(const std::vector<CallbackId>& callbackIds) const {
Marissa Walld600d572019-03-26 15:38:50 -070048 return std::hash<CallbackId>{}((callbackIds.empty()) ? 0 : callbackIds.front());
49 }
50};
51
Marissa Wallefb71af2019-06-27 14:45:53 -070052struct ListenerCallbacksHash {
53 std::size_t HashCombine(size_t value1, size_t value2) const {
54 return value1 ^ (value2 + 0x9e3779b9 + (value1 << 6) + (value1 >> 2));
55 }
56
57 std::size_t operator()(const ListenerCallbacks& listenerCallbacks) const {
58 struct ITransactionCompletedListenerHash listenerHasher;
59 struct CallbackIdsHash callbackIdsHasher;
60
61 std::size_t listenerHash = listenerHasher(listenerCallbacks.transactionCompletedListener);
62 std::size_t callbackIdsHash = callbackIdsHasher(listenerCallbacks.callbackIds);
63
64 return HashCombine(listenerHash, callbackIdsHash);
65 }
66};
67
Marissa Walle2ffb422018-10-12 11:33:52 -070068class CallbackHandle : public RefBase {
69public:
70 CallbackHandle(const sp<ITransactionCompletedListener>& transactionListener,
71 const std::vector<CallbackId>& ids, const sp<IBinder>& sc);
72
73 sp<ITransactionCompletedListener> listener;
74 std::vector<CallbackId> callbackIds;
Marissa Wall0e24a832019-07-10 15:32:50 -070075 wp<IBinder> surfaceControl;
Marissa Wallfda30bb2018-10-12 11:34:28 -070076
77 bool releasePreviousBuffer = false;
Marissa Wall5a68a772018-12-22 17:43:42 -080078 sp<Fence> previousReleaseFence;
Marissa Wallfda30bb2018-10-12 11:34:28 -070079 nsecs_t acquireTime = -1;
Marissa Wall5a68a772018-12-22 17:43:42 -080080 nsecs_t latchTime = -1;
Marissa Walle2ffb422018-10-12 11:33:52 -070081};
82
83class TransactionCompletedThread {
84public:
85 ~TransactionCompletedThread();
86
87 void run();
88
Marissa Walld600d572019-03-26 15:38:50 -070089 // Adds listener and callbackIds in case there are no SurfaceControls that are supposed
Marissa Wallefb71af2019-06-27 14:45:53 -070090 // to be included in the callback. This functions should be call before attempting to register
91 // any callback handles.
92 status_t startRegistration(const ListenerCallbacks& listenerCallbacks);
93 // Ends the registration. After this is called, no more CallbackHandles will be registered.
94 // It is safe to send a callback if the Transaction doesn't have any Pending callback handles.
95 status_t endRegistration(const ListenerCallbacks& listenerCallbacks);
Marissa Walld600d572019-03-26 15:38:50 -070096
Marissa Walle2ffb422018-10-12 11:33:52 -070097 // Informs the TransactionCompletedThread that there is a Transaction with a CallbackHandle
98 // that needs to be latched and presented this frame. This function should be called once the
99 // layer has received the CallbackHandle so the TransactionCompletedThread knows not to send
100 // a callback for that Listener/Transaction pair until that CallbackHandle has been latched and
101 // presented.
Marissa Walld600d572019-03-26 15:38:50 -0700102 status_t registerPendingCallbackHandle(const sp<CallbackHandle>& handle);
Marissa Wall5a68a772018-12-22 17:43:42 -0800103 // Notifies the TransactionCompletedThread that a pending CallbackHandle has been presented.
Marissa Wallefb71af2019-06-27 14:45:53 -0700104 status_t finalizePendingCallbackHandles(const std::deque<sp<CallbackHandle>>& handles);
Marissa Walle2ffb422018-10-12 11:33:52 -0700105
106 // Adds the Transaction CallbackHandle from a layer that does not need to be relatched and
107 // presented this frame.
Marissa Wallefb71af2019-06-27 14:45:53 -0700108 status_t registerUnpresentedCallbackHandle(const sp<CallbackHandle>& handle);
Marissa Wall3dad52d2019-03-22 14:03:19 -0700109
Marissa Wallfda30bb2018-10-12 11:34:28 -0700110 void addPresentFence(const sp<Fence>& presentFence);
111
Marissa Walle2ffb422018-10-12 11:33:52 -0700112 void sendCallbacks();
113
114private:
115 void threadMain();
116
Marissa Wallefb71af2019-06-27 14:45:53 -0700117 bool isRegisteringTransaction(const sp<ITransactionCompletedListener>& transactionListener,
118 const std::vector<CallbackId>& callbackIds) REQUIRES(mMutex);
119
Marissa Walld600d572019-03-26 15:38:50 -0700120 status_t findTransactionStats(const sp<ITransactionCompletedListener>& listener,
121 const std::vector<CallbackId>& callbackIds,
122 TransactionStats** outTransactionStats) REQUIRES(mMutex);
123
Marissa Wall3dad52d2019-03-22 14:03:19 -0700124 status_t addCallbackHandle(const sp<CallbackHandle>& handle) REQUIRES(mMutex);
Marissa Walle2ffb422018-10-12 11:33:52 -0700125
126 class ThreadDeathRecipient : public IBinder::DeathRecipient {
127 public:
128 // This function is a no-op. isBinderAlive needs a linked DeathRecipient to work.
129 // Death recipients needs a binderDied function.
130 //
131 // (isBinderAlive checks if BpBinder's mAlive is 0. mAlive is only set to 0 in sendObituary.
132 // sendObituary is only called if linkToDeath was called with a DeathRecipient.)
133 void binderDied(const wp<IBinder>& /*who*/) override {}
134 };
135 sp<ThreadDeathRecipient> mDeathRecipient;
136
Marissa Wall99343ba2018-11-13 10:39:08 -0800137 // Protects the creation and destruction of mThread
138 std::mutex mThreadMutex;
139
140 std::thread mThread GUARDED_BY(mThreadMutex);
Marissa Walle2ffb422018-10-12 11:33:52 -0700141
142 std::mutex mMutex;
143 std::condition_variable_any mConditionVariable;
144
Marissa Wallefb71af2019-06-27 14:45:53 -0700145 std::unordered_set<ListenerCallbacks, ListenerCallbacksHash> mRegisteringTransactions
146 GUARDED_BY(mMutex);
147
Marissa Walle2ffb422018-10-12 11:33:52 -0700148 std::unordered_map<
Marissa Walld600d572019-03-26 15:38:50 -0700149 sp<ITransactionCompletedListener>,
Marissa Walle2ffb422018-10-12 11:33:52 -0700150 std::unordered_map<std::vector<CallbackId>, uint32_t /*count*/, CallbackIdsHash>,
Marissa Walld600d572019-03-26 15:38:50 -0700151 ITransactionCompletedListenerHash>
Marissa Walle2ffb422018-10-12 11:33:52 -0700152 mPendingTransactions GUARDED_BY(mMutex);
Marissa Wallefb71af2019-06-27 14:45:53 -0700153
Marissa Walld600d572019-03-26 15:38:50 -0700154 std::unordered_map<sp<ITransactionCompletedListener>, std::deque<TransactionStats>,
155 ITransactionCompletedListenerHash>
156 mCompletedTransactions GUARDED_BY(mMutex);
Marissa Walle2ffb422018-10-12 11:33:52 -0700157
158 bool mRunning GUARDED_BY(mMutex) = false;
159 bool mKeepRunning GUARDED_BY(mMutex) = true;
Marissa Wallfda30bb2018-10-12 11:34:28 -0700160
161 sp<Fence> mPresentFence GUARDED_BY(mMutex);
Marissa Walle2ffb422018-10-12 11:33:52 -0700162};
163
164} // namespace android