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 <optional>
23 #include <queue>
24 #include <thread>
25 #include <unordered_map>
26 #include <unordered_set>
27 
28 #include <android-base/thread_annotations.h>
29 #include <binder/IBinder.h>
30 #include <ftl/future.h>
31 #include <gui/ITransactionCompletedListener.h>
32 #include <ui/Fence.h>
33 #include <ui/FenceResult.h>
34 
35 namespace android {
36 
37 class CallbackHandle : public RefBase {
38 public:
39     CallbackHandle(const sp<IBinder>& transactionListener, const std::vector<CallbackId>& ids,
40                    const sp<IBinder>& sc);
41 
42     sp<IBinder> listener;
43     std::vector<CallbackId> callbackIds;
44     wp<IBinder> surfaceControl;
45 
46     bool releasePreviousBuffer = false;
47     std::string name;
48     sp<Fence> previousReleaseFence;
49     std::vector<ftl::Future<FenceResult>> previousReleaseFences;
50     std::vector<ftl::SharedFuture<FenceResult>> previousSharedReleaseFences;
51     std::variant<nsecs_t, sp<Fence>> acquireTimeOrFence = -1;
52     nsecs_t latchTime = -1;
53     std::optional<uint32_t> transformHint = std::nullopt;
54     uint32_t currentMaxAcquiredBufferCount = 0;
55     std::shared_ptr<FenceTime> gpuCompositionDoneFence{FenceTime::NO_FENCE};
56     CompositorTiming compositorTiming;
57     nsecs_t refreshStartTime = 0;
58     nsecs_t dequeueReadyTime = 0;
59     uint64_t frameNumber = 0;
60     uint64_t previousFrameNumber = 0;
61     ReleaseCallbackId previousReleaseCallbackId = ReleaseCallbackId::INVALID_ID;
62 };
63 
64 class TransactionCallbackInvoker {
65 public:
66     status_t addCallbackHandles(const std::deque<sp<CallbackHandle>>& handles,
67                                 const std::vector<JankData>& jankData);
68     status_t addOnCommitCallbackHandles(const std::deque<sp<CallbackHandle>>& handles,
69                                              std::deque<sp<CallbackHandle>>& outRemainingHandles);
70 
71     void addEmptyTransaction(const ListenerCallbacks& listenerCallbacks);
72 
73     void addPresentFence(sp<Fence>);
74 
75     void sendCallbacks(bool onCommitOnly);
clearCompletedTransactions()76     void clearCompletedTransactions() {
77         mCompletedTransactions.clear();
78     }
79 
80     status_t addCallbackHandle(const sp<CallbackHandle>& handle,
81                                const std::vector<JankData>& jankData);
82 
83 
84 private:
85     status_t findOrCreateTransactionStats(const sp<IBinder>& listener,
86                                           const std::vector<CallbackId>& callbackIds,
87                                           TransactionStats** outTransactionStats);
88 
89     std::unordered_map<sp<IBinder>, std::deque<TransactionStats>, IListenerHash>
90         mCompletedTransactions;
91 
92     sp<Fence> mPresentFence;
93 };
94 
95 } // namespace android
96