| Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 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> | 
| Marissa Wall | efb71af | 2019-06-27 14:45:53 -0700 | [diff] [blame] | 24 | #include <unordered_set> | 
| Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 25 |  | 
|  | 26 | #include <android-base/thread_annotations.h> | 
|  | 27 |  | 
|  | 28 | #include <binder/IBinder.h> | 
|  | 29 | #include <gui/ITransactionCompletedListener.h> | 
| Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 30 | #include <ui/Fence.h> | 
| Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 31 |  | 
|  | 32 | namespace android { | 
|  | 33 |  | 
|  | 34 | class CallbackHandle : public RefBase { | 
|  | 35 | public: | 
| Valerie Hau | 5de3ad2 | 2019-08-20 07:47:43 -0700 | [diff] [blame] | 36 | CallbackHandle(const sp<IBinder>& transactionListener, const std::vector<CallbackId>& ids, | 
|  | 37 | const sp<IBinder>& sc); | 
| Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 38 |  | 
| Valerie Hau | 5de3ad2 | 2019-08-20 07:47:43 -0700 | [diff] [blame] | 39 | sp<IBinder> listener; | 
| Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 40 | std::vector<CallbackId> callbackIds; | 
| Marissa Wall | 0e24a83 | 2019-07-10 15:32:50 -0700 | [diff] [blame] | 41 | wp<IBinder> surfaceControl; | 
| Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 42 |  | 
|  | 43 | bool releasePreviousBuffer = false; | 
| Marissa Wall | 5a68a77 | 2018-12-22 17:43:42 -0800 | [diff] [blame] | 44 | sp<Fence> previousReleaseFence; | 
| Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 45 | nsecs_t acquireTime = -1; | 
| Marissa Wall | 5a68a77 | 2018-12-22 17:43:42 -0800 | [diff] [blame] | 46 | nsecs_t latchTime = -1; | 
| Valerie Hau | 32cdc1f | 2019-10-21 14:45:54 -0700 | [diff] [blame] | 47 | uint32_t transformHint = 0; | 
| Valerie Hau | 871d635 | 2020-01-29 08:44:02 -0800 | [diff] [blame] | 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; | 
| Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 53 | }; | 
|  | 54 |  | 
|  | 55 | class TransactionCompletedThread { | 
|  | 56 | public: | 
|  | 57 | ~TransactionCompletedThread(); | 
|  | 58 |  | 
|  | 59 | void run(); | 
|  | 60 |  | 
| Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 61 | // Adds listener and callbackIds in case there are no SurfaceControls that are supposed | 
| Marissa Wall | efb71af | 2019-06-27 14:45:53 -0700 | [diff] [blame] | 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); | 
| Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 68 |  | 
| Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 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. | 
| Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 74 | status_t registerPendingCallbackHandle(const sp<CallbackHandle>& handle); | 
| Marissa Wall | 5a68a77 | 2018-12-22 17:43:42 -0800 | [diff] [blame] | 75 | // Notifies the TransactionCompletedThread that a pending CallbackHandle has been presented. | 
| Marissa Wall | efb71af | 2019-06-27 14:45:53 -0700 | [diff] [blame] | 76 | status_t finalizePendingCallbackHandles(const std::deque<sp<CallbackHandle>>& handles); | 
| Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 77 |  | 
|  | 78 | // Adds the Transaction CallbackHandle from a layer that does not need to be relatched and | 
|  | 79 | // presented this frame. | 
| Marissa Wall | efb71af | 2019-06-27 14:45:53 -0700 | [diff] [blame] | 80 | status_t registerUnpresentedCallbackHandle(const sp<CallbackHandle>& handle); | 
| Marissa Wall | 3dad52d | 2019-03-22 14:03:19 -0700 | [diff] [blame] | 81 |  | 
| Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 82 | void addPresentFence(const sp<Fence>& presentFence); | 
|  | 83 |  | 
| Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 84 | void sendCallbacks(); | 
|  | 85 |  | 
|  | 86 | private: | 
|  | 87 | void threadMain(); | 
|  | 88 |  | 
| Valerie Hau | 5de3ad2 | 2019-08-20 07:47:43 -0700 | [diff] [blame] | 89 | bool isRegisteringTransaction(const sp<IBinder>& transactionListener, | 
| Marissa Wall | efb71af | 2019-06-27 14:45:53 -0700 | [diff] [blame] | 90 | const std::vector<CallbackId>& callbackIds) REQUIRES(mMutex); | 
|  | 91 |  | 
| Valerie Hau | 5de3ad2 | 2019-08-20 07:47:43 -0700 | [diff] [blame] | 92 | status_t findTransactionStats(const sp<IBinder>& listener, | 
| Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 93 | const std::vector<CallbackId>& callbackIds, | 
|  | 94 | TransactionStats** outTransactionStats) REQUIRES(mMutex); | 
|  | 95 |  | 
| Marissa Wall | 3dad52d | 2019-03-22 14:03:19 -0700 | [diff] [blame] | 96 | status_t addCallbackHandle(const sp<CallbackHandle>& handle) REQUIRES(mMutex); | 
| Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 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.) | 
|  | 105 | void binderDied(const wp<IBinder>& /*who*/) override {} | 
|  | 106 | }; | 
|  | 107 | sp<ThreadDeathRecipient> mDeathRecipient; | 
|  | 108 |  | 
| Marissa Wall | 99343ba | 2018-11-13 10:39:08 -0800 | [diff] [blame] | 109 | // Protects the creation and destruction of mThread | 
|  | 110 | std::mutex mThreadMutex; | 
|  | 111 |  | 
|  | 112 | std::thread mThread GUARDED_BY(mThreadMutex); | 
| Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 113 |  | 
|  | 114 | std::mutex mMutex; | 
|  | 115 | std::condition_variable_any mConditionVariable; | 
|  | 116 |  | 
| Marissa Wall | efb71af | 2019-06-27 14:45:53 -0700 | [diff] [blame] | 117 | std::unordered_set<ListenerCallbacks, ListenerCallbacksHash> mRegisteringTransactions | 
|  | 118 | GUARDED_BY(mMutex); | 
|  | 119 |  | 
| Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 120 | std::unordered_map< | 
| Valerie Hau | 5de3ad2 | 2019-08-20 07:47:43 -0700 | [diff] [blame] | 121 | sp<IBinder>, | 
| Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 122 | std::unordered_map<std::vector<CallbackId>, uint32_t /*count*/, CallbackIdsHash>, | 
| Valerie Hau | 5de3ad2 | 2019-08-20 07:47:43 -0700 | [diff] [blame] | 123 | IListenerHash> | 
| Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 124 | mPendingTransactions GUARDED_BY(mMutex); | 
| Marissa Wall | efb71af | 2019-06-27 14:45:53 -0700 | [diff] [blame] | 125 |  | 
| Valerie Hau | 5de3ad2 | 2019-08-20 07:47:43 -0700 | [diff] [blame] | 126 | std::unordered_map<sp<IBinder>, std::deque<TransactionStats>, IListenerHash> | 
| Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 127 | mCompletedTransactions GUARDED_BY(mMutex); | 
| Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 128 |  | 
|  | 129 | bool mRunning GUARDED_BY(mMutex) = false; | 
|  | 130 | bool mKeepRunning GUARDED_BY(mMutex) = true; | 
| Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 131 |  | 
|  | 132 | sp<Fence> mPresentFence GUARDED_BY(mMutex); | 
| Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 133 | }; | 
|  | 134 |  | 
|  | 135 | } // namespace android |