• 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 <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 <renderengine/ExternalTexture.h>
29 #include <ui/Fence.h>
30 #include <ui/FenceResult.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     std::string name;
45     sp<Fence> previousReleaseFence;
46     std::vector<ftl::Future<FenceResult>> previousReleaseFences;
47     std::variant<nsecs_t, sp<Fence>> acquireTimeOrFence = -1;
48     nsecs_t latchTime = -1;
49     std::optional<uint32_t> transformHint = std::nullopt;
50     uint32_t currentMaxAcquiredBufferCount = 0;
51     std::shared_ptr<FenceTime> gpuCompositionDoneFence{FenceTime::NO_FENCE};
52     CompositorTiming compositorTiming;
53     nsecs_t refreshStartTime = 0;
54     nsecs_t dequeueReadyTime = 0;
55     uint64_t frameNumber = 0;
56     uint64_t previousFrameNumber = 0;
57     ReleaseCallbackId previousReleaseCallbackId = ReleaseCallbackId::INVALID_ID;
58     std::shared_ptr<gui::BufferReleaseChannel::ProducerEndpoint> bufferReleaseChannel;
59     std::weak_ptr<renderengine::ExternalTexture> previousBuffer;
60 };
61 
62 class TransactionCallbackInvoker {
63 public:
64     status_t addCallbackHandles(const std::deque<sp<CallbackHandle>>& handles);
65     status_t addOnCommitCallbackHandles(const std::deque<sp<CallbackHandle>>& handles,
66                                              std::deque<sp<CallbackHandle>>& outRemainingHandles);
67 
68     void addEmptyTransaction(const ListenerCallbacks& listenerCallbacks);
69 
70     void addPresentFence(sp<Fence>);
71 
72     void sendCallbacks(bool onCommitOnly);
clearCompletedTransactions()73     void clearCompletedTransactions() {
74         mCompletedTransactions.clear();
75     }
76 
77     status_t addCallbackHandle(const sp<CallbackHandle>& handle);
78 
79 private:
80     status_t findOrCreateTransactionStats(const sp<IBinder>& listener,
81                                           const std::vector<CallbackId>& callbackIds,
82                                           TransactionStats** outTransactionStats);
83 
84     std::unordered_map<sp<IBinder>, std::deque<TransactionStats>, IListenerHash>
85         mCompletedTransactions;
86 
87     struct BufferRelease {
88         std::string layerName;
89         std::shared_ptr<gui::BufferReleaseChannel::ProducerEndpoint> channel;
90         ReleaseCallbackId callbackId;
91         sp<Fence> fence;
92         uint32_t currentMaxAcquiredBufferCount;
93     };
94     std::vector<BufferRelease> mBufferReleases;
95 
96     sp<Fence> mPresentFence;
97 };
98 
99 } // namespace android
100