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 <thread> 23 #include <unordered_map> 24 #include <unordered_set> 25 26 #include <android-base/thread_annotations.h> 27 28 #include <binder/IBinder.h> 29 #include <gui/ITransactionCompletedListener.h> 30 #include <ui/Fence.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 sp<Fence> previousReleaseFence; 45 nsecs_t acquireTime = -1; 46 nsecs_t latchTime = -1; 47 uint32_t transformHint = 0; 48 std::shared_ptr<FenceTime> gpuCompositionDoneFence{FenceTime::NO_FENCE}; 49 CompositorTiming compositorTiming; 50 nsecs_t refreshStartTime = 0; 51 nsecs_t dequeueReadyTime = 0; 52 uint64_t frameNumber = 0; 53 }; 54 55 class TransactionCompletedThread { 56 public: 57 ~TransactionCompletedThread(); 58 59 void run(); 60 61 // Adds listener and callbackIds in case there are no SurfaceControls that are supposed 62 // to be included in the callback. This functions should be call before attempting to register 63 // any callback handles. 64 status_t startRegistration(const ListenerCallbacks& listenerCallbacks); 65 // Ends the registration. After this is called, no more CallbackHandles will be registered. 66 // It is safe to send a callback if the Transaction doesn't have any Pending callback handles. 67 status_t endRegistration(const ListenerCallbacks& listenerCallbacks); 68 69 // Informs the TransactionCompletedThread that there is a Transaction with a CallbackHandle 70 // that needs to be latched and presented this frame. This function should be called once the 71 // layer has received the CallbackHandle so the TransactionCompletedThread knows not to send 72 // a callback for that Listener/Transaction pair until that CallbackHandle has been latched and 73 // presented. 74 status_t registerPendingCallbackHandle(const sp<CallbackHandle>& handle); 75 // Notifies the TransactionCompletedThread that a pending CallbackHandle has been presented. 76 status_t finalizePendingCallbackHandles(const std::deque<sp<CallbackHandle>>& handles); 77 78 // Adds the Transaction CallbackHandle from a layer that does not need to be relatched and 79 // presented this frame. 80 status_t registerUnpresentedCallbackHandle(const sp<CallbackHandle>& handle); 81 82 void addPresentFence(const sp<Fence>& presentFence); 83 84 void sendCallbacks(); 85 86 private: 87 void threadMain(); 88 89 bool isRegisteringTransaction(const sp<IBinder>& transactionListener, 90 const std::vector<CallbackId>& callbackIds) REQUIRES(mMutex); 91 92 status_t findTransactionStats(const sp<IBinder>& listener, 93 const std::vector<CallbackId>& callbackIds, 94 TransactionStats** outTransactionStats) REQUIRES(mMutex); 95 96 status_t addCallbackHandle(const sp<CallbackHandle>& handle) REQUIRES(mMutex); 97 98 class ThreadDeathRecipient : public IBinder::DeathRecipient { 99 public: 100 // This function is a no-op. isBinderAlive needs a linked DeathRecipient to work. 101 // Death recipients needs a binderDied function. 102 // 103 // (isBinderAlive checks if BpBinder's mAlive is 0. mAlive is only set to 0 in sendObituary. 104 // sendObituary is only called if linkToDeath was called with a DeathRecipient.) binderDied(const wp<IBinder> &)105 void binderDied(const wp<IBinder>& /*who*/) override {} 106 }; 107 sp<ThreadDeathRecipient> mDeathRecipient; 108 109 // Protects the creation and destruction of mThread 110 std::mutex mThreadMutex; 111 112 std::thread mThread GUARDED_BY(mThreadMutex); 113 114 std::mutex mMutex; 115 std::condition_variable_any mConditionVariable; 116 117 std::unordered_set<ListenerCallbacks, ListenerCallbacksHash> mRegisteringTransactions 118 GUARDED_BY(mMutex); 119 120 std::unordered_map< 121 sp<IBinder>, 122 std::unordered_map<std::vector<CallbackId>, uint32_t /*count*/, CallbackIdsHash>, 123 IListenerHash> 124 mPendingTransactions GUARDED_BY(mMutex); 125 126 std::unordered_map<sp<IBinder>, std::deque<TransactionStats>, IListenerHash> 127 mCompletedTransactions GUARDED_BY(mMutex); 128 129 bool mRunning GUARDED_BY(mMutex) = false; 130 bool mKeepRunning GUARDED_BY(mMutex) = true; 131 132 sp<Fence> mPresentFence GUARDED_BY(mMutex); 133 }; 134 135 } // namespace android 136