blob: 184b15103e728318c2df6cb236dc291734a60086 [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;
Valerie Hau32cdc1f2019-10-21 14:45:54 -070047 uint32_t transformHint = 0;
Valerie Hau871d6352020-01-29 08:44:02 -080048 std::shared_ptr<FenceTime> gpuCompositionDoneFence{FenceTime::NO_FENCE};
49 CompositorTiming compositorTiming;
50 nsecs_t refreshStartTime = 0;
51 nsecs_t dequeueReadyTime = 0;
52 uint64_t frameNumber = 0;
Vishnu Nair1506b182021-02-22 14:35:15 -080053 uint64_t previousBufferId = 0;
Marissa Walle2ffb422018-10-12 11:33:52 -070054};
55
Robert Carr9a803c32021-01-14 16:57:58 -080056class TransactionCallbackInvoker {
Marissa Walle2ffb422018-10-12 11:33:52 -070057public:
Robert Carr9a803c32021-01-14 16:57:58 -080058 ~TransactionCallbackInvoker();
Marissa Walle2ffb422018-10-12 11:33:52 -070059
Marissa Walld600d572019-03-26 15:38:50 -070060 // Adds listener and callbackIds in case there are no SurfaceControls that are supposed
Marissa Wallefb71af2019-06-27 14:45:53 -070061 // to be included in the callback. This functions should be call before attempting to register
62 // any callback handles.
63 status_t startRegistration(const ListenerCallbacks& listenerCallbacks);
64 // Ends the registration. After this is called, no more CallbackHandles will be registered.
65 // It is safe to send a callback if the Transaction doesn't have any Pending callback handles.
66 status_t endRegistration(const ListenerCallbacks& listenerCallbacks);
Marissa Walld600d572019-03-26 15:38:50 -070067
Robert Carr9a803c32021-01-14 16:57:58 -080068 // Informs the TransactionCallbackInvoker that there is a Transaction with a CallbackHandle
Marissa Walle2ffb422018-10-12 11:33:52 -070069 // that needs to be latched and presented this frame. This function should be called once the
Robert Carr9a803c32021-01-14 16:57:58 -080070 // layer has received the CallbackHandle so the TransactionCallbackInvoker knows not to send
Marissa Walle2ffb422018-10-12 11:33:52 -070071 // a callback for that Listener/Transaction pair until that CallbackHandle has been latched and
72 // presented.
Marissa Walld600d572019-03-26 15:38:50 -070073 status_t registerPendingCallbackHandle(const sp<CallbackHandle>& handle);
Robert Carr9a803c32021-01-14 16:57:58 -080074 // Notifies the TransactionCallbackInvoker that a pending CallbackHandle has been presented.
Jorim Jaggi9c03b502020-11-24 23:51:31 +010075 status_t finalizePendingCallbackHandles(const std::deque<sp<CallbackHandle>>& handles,
76 const std::vector<JankData>& jankData);
Vishnu Nairfc46c1e2021-04-21 08:31:32 -070077 status_t finalizeOnCommitCallbackHandles(const std::deque<sp<CallbackHandle>>& handles,
78 std::deque<sp<CallbackHandle>>& outRemainingHandles);
Marissa Walle2ffb422018-10-12 11:33:52 -070079
80 // Adds the Transaction CallbackHandle from a layer that does not need to be relatched and
81 // presented this frame.
Marissa Wallefb71af2019-06-27 14:45:53 -070082 status_t registerUnpresentedCallbackHandle(const sp<CallbackHandle>& handle);
Marissa Wall3dad52d2019-03-22 14:03:19 -070083
Marissa Wallfda30bb2018-10-12 11:34:28 -070084 void addPresentFence(const sp<Fence>& presentFence);
85
Marissa Walle2ffb422018-10-12 11:33:52 -070086 void sendCallbacks();
87
88private:
Marissa Walle2ffb422018-10-12 11:33:52 -070089
Valerie Hau5de3ad22019-08-20 07:47:43 -070090 bool isRegisteringTransaction(const sp<IBinder>& transactionListener,
Marissa Wallefb71af2019-06-27 14:45:53 -070091 const std::vector<CallbackId>& callbackIds) REQUIRES(mMutex);
92
Valerie Hau5de3ad22019-08-20 07:47:43 -070093 status_t findTransactionStats(const sp<IBinder>& listener,
Marissa Walld600d572019-03-26 15:38:50 -070094 const std::vector<CallbackId>& callbackIds,
95 TransactionStats** outTransactionStats) REQUIRES(mMutex);
96
Jorim Jaggi9c03b502020-11-24 23:51:31 +010097 status_t addCallbackHandle(const sp<CallbackHandle>& handle,
98 const std::vector<JankData>& jankData) REQUIRES(mMutex);
Marissa Walle2ffb422018-10-12 11:33:52 -070099
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700100 status_t finalizeCallbackHandle(const sp<CallbackHandle>& handle,
101 const std::vector<JankData>& jankData) REQUIRES(mMutex);
102
Robert Carr9a803c32021-01-14 16:57:58 -0800103 class CallbackDeathRecipient : public IBinder::DeathRecipient {
Marissa Walle2ffb422018-10-12 11:33:52 -0700104 public:
105 // This function is a no-op. isBinderAlive needs a linked DeathRecipient to work.
106 // Death recipients needs a binderDied function.
107 //
108 // (isBinderAlive checks if BpBinder's mAlive is 0. mAlive is only set to 0 in sendObituary.
109 // sendObituary is only called if linkToDeath was called with a DeathRecipient.)
110 void binderDied(const wp<IBinder>& /*who*/) override {}
111 };
Robert Carr9a803c32021-01-14 16:57:58 -0800112 sp<CallbackDeathRecipient> mDeathRecipient =
113 new CallbackDeathRecipient();
Marissa Walle2ffb422018-10-12 11:33:52 -0700114
115 std::mutex mMutex;
116 std::condition_variable_any mConditionVariable;
117
Marissa Wallefb71af2019-06-27 14:45:53 -0700118 std::unordered_set<ListenerCallbacks, ListenerCallbacksHash> mRegisteringTransactions
119 GUARDED_BY(mMutex);
120
Marissa Walle2ffb422018-10-12 11:33:52 -0700121 std::unordered_map<
Valerie Hau5de3ad22019-08-20 07:47:43 -0700122 sp<IBinder>,
Marissa Walle2ffb422018-10-12 11:33:52 -0700123 std::unordered_map<std::vector<CallbackId>, uint32_t /*count*/, CallbackIdsHash>,
Valerie Hau5de3ad22019-08-20 07:47:43 -0700124 IListenerHash>
Marissa Walle2ffb422018-10-12 11:33:52 -0700125 mPendingTransactions GUARDED_BY(mMutex);
Marissa Wallefb71af2019-06-27 14:45:53 -0700126
Valerie Hau5de3ad22019-08-20 07:47:43 -0700127 std::unordered_map<sp<IBinder>, std::deque<TransactionStats>, IListenerHash>
Marissa Walld600d572019-03-26 15:38:50 -0700128 mCompletedTransactions GUARDED_BY(mMutex);
Marissa Walle2ffb422018-10-12 11:33:52 -0700129
Marissa Wallfda30bb2018-10-12 11:34:28 -0700130 sp<Fence> mPresentFence GUARDED_BY(mMutex);
Marissa Walle2ffb422018-10-12 11:33:52 -0700131};
132
133} // namespace android