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