blob: a85ad1e63ce3f842ede22976a4c1be6897c4d3f3 [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
34class CallbackHandle : public RefBase {
35public:
Valerie Hau5de3ad22019-08-20 07:47:43 -070036 CallbackHandle(const sp<IBinder>& transactionListener, const std::vector<CallbackId>& ids,
37 const sp<IBinder>& sc);
Marissa Walle2ffb422018-10-12 11:33:52 -070038
Valerie Hau5de3ad22019-08-20 07:47:43 -070039 sp<IBinder> listener;
Marissa Walle2ffb422018-10-12 11:33:52 -070040 std::vector<CallbackId> callbackIds;
Marissa Wall0e24a832019-07-10 15:32:50 -070041 wp<IBinder> surfaceControl;
Marissa Wallfda30bb2018-10-12 11:34:28 -070042
43 bool releasePreviousBuffer = false;
Marissa Wall5a68a772018-12-22 17:43:42 -080044 sp<Fence> previousReleaseFence;
Marissa Wallfda30bb2018-10-12 11:34:28 -070045 nsecs_t acquireTime = -1;
Marissa Wall5a68a772018-12-22 17:43:42 -080046 nsecs_t latchTime = -1;
Marissa Walle2ffb422018-10-12 11:33:52 -070047};
48
49class TransactionCompletedThread {
50public:
51 ~TransactionCompletedThread();
52
53 void run();
54
Marissa Walld600d572019-03-26 15:38:50 -070055 // Adds listener and callbackIds in case there are no SurfaceControls that are supposed
Marissa Wallefb71af2019-06-27 14:45:53 -070056 // to be included in the callback. This functions should be call before attempting to register
57 // any callback handles.
58 status_t startRegistration(const ListenerCallbacks& listenerCallbacks);
59 // Ends the registration. After this is called, no more CallbackHandles will be registered.
60 // It is safe to send a callback if the Transaction doesn't have any Pending callback handles.
61 status_t endRegistration(const ListenerCallbacks& listenerCallbacks);
Marissa Walld600d572019-03-26 15:38:50 -070062
Marissa Walle2ffb422018-10-12 11:33:52 -070063 // Informs the TransactionCompletedThread that there is a Transaction with a CallbackHandle
64 // that needs to be latched and presented this frame. This function should be called once the
65 // layer has received the CallbackHandle so the TransactionCompletedThread knows not to send
66 // a callback for that Listener/Transaction pair until that CallbackHandle has been latched and
67 // presented.
Marissa Walld600d572019-03-26 15:38:50 -070068 status_t registerPendingCallbackHandle(const sp<CallbackHandle>& handle);
Marissa Wall5a68a772018-12-22 17:43:42 -080069 // Notifies the TransactionCompletedThread that a pending CallbackHandle has been presented.
Marissa Wallefb71af2019-06-27 14:45:53 -070070 status_t finalizePendingCallbackHandles(const std::deque<sp<CallbackHandle>>& handles);
Marissa Walle2ffb422018-10-12 11:33:52 -070071
72 // Adds the Transaction CallbackHandle from a layer that does not need to be relatched and
73 // presented this frame.
Marissa Wallefb71af2019-06-27 14:45:53 -070074 status_t registerUnpresentedCallbackHandle(const sp<CallbackHandle>& handle);
Marissa Wall3dad52d2019-03-22 14:03:19 -070075
Marissa Wallfda30bb2018-10-12 11:34:28 -070076 void addPresentFence(const sp<Fence>& presentFence);
77
Marissa Walle2ffb422018-10-12 11:33:52 -070078 void sendCallbacks();
79
80private:
81 void threadMain();
82
Valerie Hau5de3ad22019-08-20 07:47:43 -070083 bool isRegisteringTransaction(const sp<IBinder>& transactionListener,
Marissa Wallefb71af2019-06-27 14:45:53 -070084 const std::vector<CallbackId>& callbackIds) REQUIRES(mMutex);
85
Valerie Hau5de3ad22019-08-20 07:47:43 -070086 status_t findTransactionStats(const sp<IBinder>& listener,
Marissa Walld600d572019-03-26 15:38:50 -070087 const std::vector<CallbackId>& callbackIds,
88 TransactionStats** outTransactionStats) REQUIRES(mMutex);
89
Marissa Wall3dad52d2019-03-22 14:03:19 -070090 status_t addCallbackHandle(const sp<CallbackHandle>& handle) REQUIRES(mMutex);
Marissa Walle2ffb422018-10-12 11:33:52 -070091
92 class ThreadDeathRecipient : public IBinder::DeathRecipient {
93 public:
94 // This function is a no-op. isBinderAlive needs a linked DeathRecipient to work.
95 // Death recipients needs a binderDied function.
96 //
97 // (isBinderAlive checks if BpBinder's mAlive is 0. mAlive is only set to 0 in sendObituary.
98 // sendObituary is only called if linkToDeath was called with a DeathRecipient.)
99 void binderDied(const wp<IBinder>& /*who*/) override {}
100 };
101 sp<ThreadDeathRecipient> mDeathRecipient;
102
Marissa Wall99343ba2018-11-13 10:39:08 -0800103 // Protects the creation and destruction of mThread
104 std::mutex mThreadMutex;
105
106 std::thread mThread GUARDED_BY(mThreadMutex);
Marissa Walle2ffb422018-10-12 11:33:52 -0700107
108 std::mutex mMutex;
109 std::condition_variable_any mConditionVariable;
110
Marissa Wallefb71af2019-06-27 14:45:53 -0700111 std::unordered_set<ListenerCallbacks, ListenerCallbacksHash> mRegisteringTransactions
112 GUARDED_BY(mMutex);
113
Marissa Walle2ffb422018-10-12 11:33:52 -0700114 std::unordered_map<
Valerie Hau5de3ad22019-08-20 07:47:43 -0700115 sp<IBinder>,
Marissa Walle2ffb422018-10-12 11:33:52 -0700116 std::unordered_map<std::vector<CallbackId>, uint32_t /*count*/, CallbackIdsHash>,
Valerie Hau5de3ad22019-08-20 07:47:43 -0700117 IListenerHash>
Marissa Walle2ffb422018-10-12 11:33:52 -0700118 mPendingTransactions GUARDED_BY(mMutex);
Marissa Wallefb71af2019-06-27 14:45:53 -0700119
Valerie Hau5de3ad22019-08-20 07:47:43 -0700120 std::unordered_map<sp<IBinder>, std::deque<TransactionStats>, IListenerHash>
Marissa Walld600d572019-03-26 15:38:50 -0700121 mCompletedTransactions GUARDED_BY(mMutex);
Marissa Walle2ffb422018-10-12 11:33:52 -0700122
123 bool mRunning GUARDED_BY(mMutex) = false;
124 bool mKeepRunning GUARDED_BY(mMutex) = true;
Marissa Wallfda30bb2018-10-12 11:34:28 -0700125
126 sp<Fence> mPresentFence GUARDED_BY(mMutex);
Marissa Walle2ffb422018-10-12 11:33:52 -0700127};
128
129} // namespace android