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::SharedFuture<FenceResult>> previousReleaseFences; 50 std::variant<nsecs_t, sp<Fence>> acquireTimeOrFence = -1; 51 nsecs_t latchTime = -1; 52 std::optional<uint32_t> transformHint = std::nullopt; 53 uint32_t currentMaxAcquiredBufferCount = 0; 54 std::shared_ptr<FenceTime> gpuCompositionDoneFence{FenceTime::NO_FENCE}; 55 CompositorTiming compositorTiming; 56 nsecs_t refreshStartTime = 0; 57 nsecs_t dequeueReadyTime = 0; 58 uint64_t frameNumber = 0; 59 ReleaseCallbackId previousReleaseCallbackId = ReleaseCallbackId::INVALID_ID; 60 }; 61 62 class TransactionCallbackInvoker { 63 public: 64 status_t addCallbackHandles(const std::deque<sp<CallbackHandle>>& handles, 65 const std::vector<JankData>& jankData); 66 status_t addOnCommitCallbackHandles(const std::deque<sp<CallbackHandle>>& handles, 67 std::deque<sp<CallbackHandle>>& outRemainingHandles); 68 69 void addEmptyTransaction(const ListenerCallbacks& listenerCallbacks); 70 71 void addPresentFence(sp<Fence>); 72 73 void sendCallbacks(bool onCommitOnly); clearCompletedTransactions()74 void clearCompletedTransactions() { 75 mCompletedTransactions.clear(); 76 } 77 78 status_t addCallbackHandle(const sp<CallbackHandle>& handle, 79 const std::vector<JankData>& jankData); 80 81 82 private: 83 status_t findOrCreateTransactionStats(const sp<IBinder>& listener, 84 const std::vector<CallbackId>& callbackIds, 85 TransactionStats** outTransactionStats); 86 87 std::unordered_map<sp<IBinder>, std::deque<TransactionStats>, IListenerHash> 88 mCompletedTransactions; 89 90 sp<Fence> mPresentFence; 91 }; 92 93 } // namespace android 94