• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <unordered_set>
25 
26 #include <android-base/thread_annotations.h>
27 
28 #include <binder/IBinder.h>
29 #include <gui/ITransactionCompletedListener.h>
30 #include <ui/Fence.h>
31 
32 namespace android {
33 
34 class CallbackHandle : public RefBase {
35 public:
36     CallbackHandle(const sp<IBinder>& transactionListener, const std::vector<CallbackId>& ids,
37                    const sp<IBinder>& sc);
38 
39     sp<IBinder> listener;
40     std::vector<CallbackId> callbackIds;
41     wp<IBinder> surfaceControl;
42 
43     bool releasePreviousBuffer = false;
44     sp<Fence> previousReleaseFence;
45     nsecs_t acquireTime = -1;
46     nsecs_t latchTime = -1;
47     uint32_t transformHint = 0;
48     uint32_t currentMaxAcquiredBufferCount = 0;
49     std::shared_ptr<FenceTime> gpuCompositionDoneFence{FenceTime::NO_FENCE};
50     CompositorTiming compositorTiming;
51     nsecs_t refreshStartTime = 0;
52     nsecs_t dequeueReadyTime = 0;
53     uint64_t frameNumber = 0;
54     ReleaseCallbackId previousReleaseCallbackId = ReleaseCallbackId::INVALID_ID;
55 };
56 
57 class TransactionCallbackInvoker {
58 public:
59     ~TransactionCallbackInvoker();
60 
61     // Adds listener and callbackIds in case there are no SurfaceControls that are supposed
62     // to be included in the callback. This functions should be call before attempting to register
63     // any callback handles.
64     status_t startRegistration(const ListenerCallbacks& listenerCallbacks);
65     // Ends the registration. After this is called, no more CallbackHandles will be registered.
66     // It is safe to send a callback if the Transaction doesn't have any Pending callback handles.
67     status_t endRegistration(const ListenerCallbacks& listenerCallbacks);
68 
69     // Informs the TransactionCallbackInvoker that there is a Transaction with a CallbackHandle
70     // that needs to be latched and presented this frame. This function should be called once the
71     // layer has received the CallbackHandle so the TransactionCallbackInvoker knows not to send
72     // a callback for that Listener/Transaction pair until that CallbackHandle has been latched and
73     // presented.
74     status_t registerPendingCallbackHandle(const sp<CallbackHandle>& handle);
75     // Notifies the TransactionCallbackInvoker that a pending CallbackHandle has been presented.
76     status_t finalizePendingCallbackHandles(const std::deque<sp<CallbackHandle>>& handles,
77                                             const std::vector<JankData>& jankData);
78     status_t finalizeOnCommitCallbackHandles(const std::deque<sp<CallbackHandle>>& handles,
79                                              std::deque<sp<CallbackHandle>>& outRemainingHandles);
80 
81     // Adds the Transaction CallbackHandle from a layer that does not need to be relatched and
82     // presented this frame.
83     status_t registerUnpresentedCallbackHandle(const sp<CallbackHandle>& handle);
84 
85     void addPresentFence(const sp<Fence>& presentFence);
86 
87     void sendCallbacks();
88 
89 private:
90 
91     bool isRegisteringTransaction(const sp<IBinder>& transactionListener,
92                                   const std::vector<CallbackId>& callbackIds) REQUIRES(mMutex);
93 
94     status_t findTransactionStats(const sp<IBinder>& listener,
95                                   const std::vector<CallbackId>& callbackIds,
96                                   TransactionStats** outTransactionStats) REQUIRES(mMutex);
97 
98     status_t addCallbackHandle(const sp<CallbackHandle>& handle,
99                                const std::vector<JankData>& jankData) REQUIRES(mMutex);
100 
101     status_t finalizeCallbackHandle(const sp<CallbackHandle>& handle,
102                                     const std::vector<JankData>& jankData) REQUIRES(mMutex);
103 
104     class CallbackDeathRecipient : public IBinder::DeathRecipient {
105     public:
106         // This function is a no-op. isBinderAlive needs a linked DeathRecipient to work.
107         // Death recipients needs a binderDied function.
108         //
109         // (isBinderAlive checks if BpBinder's mAlive is 0. mAlive is only set to 0 in sendObituary.
110         // sendObituary is only called if linkToDeath was called with a DeathRecipient.)
binderDied(const wp<IBinder> &)111         void binderDied(const wp<IBinder>& /*who*/) override {}
112     };
113     sp<CallbackDeathRecipient> mDeathRecipient =
114         new CallbackDeathRecipient();
115 
116     std::mutex mMutex;
117     std::condition_variable_any mConditionVariable;
118 
119     std::unordered_set<ListenerCallbacks, ListenerCallbacksHash> mRegisteringTransactions
120             GUARDED_BY(mMutex);
121 
122     std::unordered_map<
123             sp<IBinder>,
124             std::unordered_map<std::vector<CallbackId>, uint32_t /*count*/, CallbackIdsHash>,
125             IListenerHash>
126             mPendingTransactions GUARDED_BY(mMutex);
127 
128     std::unordered_map<sp<IBinder>, std::deque<TransactionStats>, IListenerHash>
129             mCompletedTransactions GUARDED_BY(mMutex);
130 
131     sp<Fence> mPresentFence GUARDED_BY(mMutex);
132 };
133 
134 } // namespace android
135