xref: /aosp_15_r20/frameworks/native/services/surfaceflinger/TransactionCallbackInvoker.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright 2018 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker #pragma once
18*38e8c45fSAndroid Build Coastguard Worker 
19*38e8c45fSAndroid Build Coastguard Worker #include <deque>
20*38e8c45fSAndroid Build Coastguard Worker #include <optional>
21*38e8c45fSAndroid Build Coastguard Worker #include <unordered_map>
22*38e8c45fSAndroid Build Coastguard Worker 
23*38e8c45fSAndroid Build Coastguard Worker #include <android-base/thread_annotations.h>
24*38e8c45fSAndroid Build Coastguard Worker #include <binder/IBinder.h>
25*38e8c45fSAndroid Build Coastguard Worker #include <ftl/future.h>
26*38e8c45fSAndroid Build Coastguard Worker #include <gui/BufferReleaseChannel.h>
27*38e8c45fSAndroid Build Coastguard Worker #include <gui/ITransactionCompletedListener.h>
28*38e8c45fSAndroid Build Coastguard Worker #include <ui/Fence.h>
29*38e8c45fSAndroid Build Coastguard Worker #include <ui/FenceResult.h>
30*38e8c45fSAndroid Build Coastguard Worker 
31*38e8c45fSAndroid Build Coastguard Worker namespace android {
32*38e8c45fSAndroid Build Coastguard Worker 
33*38e8c45fSAndroid Build Coastguard Worker class CallbackHandle : public RefBase {
34*38e8c45fSAndroid Build Coastguard Worker public:
35*38e8c45fSAndroid Build Coastguard Worker     CallbackHandle(const sp<IBinder>& transactionListener, const std::vector<CallbackId>& ids,
36*38e8c45fSAndroid Build Coastguard Worker                    const sp<IBinder>& sc);
37*38e8c45fSAndroid Build Coastguard Worker 
38*38e8c45fSAndroid Build Coastguard Worker     sp<IBinder> listener;
39*38e8c45fSAndroid Build Coastguard Worker     std::vector<CallbackId> callbackIds;
40*38e8c45fSAndroid Build Coastguard Worker     wp<IBinder> surfaceControl;
41*38e8c45fSAndroid Build Coastguard Worker 
42*38e8c45fSAndroid Build Coastguard Worker     bool releasePreviousBuffer = false;
43*38e8c45fSAndroid Build Coastguard Worker     std::string name;
44*38e8c45fSAndroid Build Coastguard Worker     sp<Fence> previousReleaseFence;
45*38e8c45fSAndroid Build Coastguard Worker     std::vector<ftl::Future<FenceResult>> previousReleaseFences;
46*38e8c45fSAndroid Build Coastguard Worker     std::variant<nsecs_t, sp<Fence>> acquireTimeOrFence = -1;
47*38e8c45fSAndroid Build Coastguard Worker     nsecs_t latchTime = -1;
48*38e8c45fSAndroid Build Coastguard Worker     std::optional<uint32_t> transformHint = std::nullopt;
49*38e8c45fSAndroid Build Coastguard Worker     uint32_t currentMaxAcquiredBufferCount = 0;
50*38e8c45fSAndroid Build Coastguard Worker     std::shared_ptr<FenceTime> gpuCompositionDoneFence{FenceTime::NO_FENCE};
51*38e8c45fSAndroid Build Coastguard Worker     CompositorTiming compositorTiming;
52*38e8c45fSAndroid Build Coastguard Worker     nsecs_t refreshStartTime = 0;
53*38e8c45fSAndroid Build Coastguard Worker     nsecs_t dequeueReadyTime = 0;
54*38e8c45fSAndroid Build Coastguard Worker     uint64_t frameNumber = 0;
55*38e8c45fSAndroid Build Coastguard Worker     uint64_t previousFrameNumber = 0;
56*38e8c45fSAndroid Build Coastguard Worker     ReleaseCallbackId previousReleaseCallbackId = ReleaseCallbackId::INVALID_ID;
57*38e8c45fSAndroid Build Coastguard Worker     std::shared_ptr<gui::BufferReleaseChannel::ProducerEndpoint> bufferReleaseChannel;
58*38e8c45fSAndroid Build Coastguard Worker };
59*38e8c45fSAndroid Build Coastguard Worker 
60*38e8c45fSAndroid Build Coastguard Worker class TransactionCallbackInvoker {
61*38e8c45fSAndroid Build Coastguard Worker public:
62*38e8c45fSAndroid Build Coastguard Worker     status_t addCallbackHandles(const std::deque<sp<CallbackHandle>>& handles);
63*38e8c45fSAndroid Build Coastguard Worker     status_t addOnCommitCallbackHandles(const std::deque<sp<CallbackHandle>>& handles,
64*38e8c45fSAndroid Build Coastguard Worker                                              std::deque<sp<CallbackHandle>>& outRemainingHandles);
65*38e8c45fSAndroid Build Coastguard Worker 
66*38e8c45fSAndroid Build Coastguard Worker     void addEmptyTransaction(const ListenerCallbacks& listenerCallbacks);
67*38e8c45fSAndroid Build Coastguard Worker 
68*38e8c45fSAndroid Build Coastguard Worker     void addPresentFence(sp<Fence>);
69*38e8c45fSAndroid Build Coastguard Worker 
70*38e8c45fSAndroid Build Coastguard Worker     void sendCallbacks(bool onCommitOnly);
clearCompletedTransactions()71*38e8c45fSAndroid Build Coastguard Worker     void clearCompletedTransactions() {
72*38e8c45fSAndroid Build Coastguard Worker         mCompletedTransactions.clear();
73*38e8c45fSAndroid Build Coastguard Worker     }
74*38e8c45fSAndroid Build Coastguard Worker 
75*38e8c45fSAndroid Build Coastguard Worker     status_t addCallbackHandle(const sp<CallbackHandle>& handle);
76*38e8c45fSAndroid Build Coastguard Worker 
77*38e8c45fSAndroid Build Coastguard Worker private:
78*38e8c45fSAndroid Build Coastguard Worker     status_t findOrCreateTransactionStats(const sp<IBinder>& listener,
79*38e8c45fSAndroid Build Coastguard Worker                                           const std::vector<CallbackId>& callbackIds,
80*38e8c45fSAndroid Build Coastguard Worker                                           TransactionStats** outTransactionStats);
81*38e8c45fSAndroid Build Coastguard Worker 
82*38e8c45fSAndroid Build Coastguard Worker     std::unordered_map<sp<IBinder>, std::deque<TransactionStats>, IListenerHash>
83*38e8c45fSAndroid Build Coastguard Worker         mCompletedTransactions;
84*38e8c45fSAndroid Build Coastguard Worker 
85*38e8c45fSAndroid Build Coastguard Worker     struct BufferRelease {
86*38e8c45fSAndroid Build Coastguard Worker         std::string layerName;
87*38e8c45fSAndroid Build Coastguard Worker         std::shared_ptr<gui::BufferReleaseChannel::ProducerEndpoint> channel;
88*38e8c45fSAndroid Build Coastguard Worker         ReleaseCallbackId callbackId;
89*38e8c45fSAndroid Build Coastguard Worker         sp<Fence> fence;
90*38e8c45fSAndroid Build Coastguard Worker         uint32_t currentMaxAcquiredBufferCount;
91*38e8c45fSAndroid Build Coastguard Worker     };
92*38e8c45fSAndroid Build Coastguard Worker     std::vector<BufferRelease> mBufferReleases;
93*38e8c45fSAndroid Build Coastguard Worker 
94*38e8c45fSAndroid Build Coastguard Worker     sp<Fence> mPresentFence;
95*38e8c45fSAndroid Build Coastguard Worker };
96*38e8c45fSAndroid Build Coastguard Worker 
97*38e8c45fSAndroid Build Coastguard Worker } // namespace android
98