TransactionCompletedThread.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. #pragma once
  17. #include <condition_variable>
  18. #include <deque>
  19. #include <mutex>
  20. #include <thread>
  21. #include <unordered_map>
  22. #include <android-base/thread_annotations.h>
  23. #include <binder/IBinder.h>
  24. #include <gui/ITransactionCompletedListener.h>
  25. #include <ui/Fence.h>
  26. namespace android {
  27. struct CallbackIdsHash {
  28. // CallbackId vectors have several properties that let us get away with this simple hash.
  29. // 1) CallbackIds are never 0 so if something has gone wrong and our CallbackId vector is
  30. // empty we can still hash 0.
  31. // 2) CallbackId vectors for the same listener either are identical or contain none of the
  32. // same members. It is sufficient to just check the first CallbackId in the vectors. If
  33. // they match, they are the same. If they do not match, they are not the same.
  34. std::size_t operator()(const std::vector<CallbackId>& callbackIds) const {
  35. return std::hash<CallbackId>{}((callbackIds.empty()) ? 0 : callbackIds.front());
  36. }
  37. };
  38. class CallbackHandle : public RefBase {
  39. public:
  40. CallbackHandle(const sp<ITransactionCompletedListener>& transactionListener,
  41. const std::vector<CallbackId>& ids, const sp<IBinder>& sc);
  42. sp<ITransactionCompletedListener> listener;
  43. std::vector<CallbackId> callbackIds;
  44. wp<IBinder> surfaceControl;
  45. bool releasePreviousBuffer = false;
  46. sp<Fence> previousReleaseFence;
  47. nsecs_t acquireTime = -1;
  48. nsecs_t latchTime = -1;
  49. };
  50. class TransactionCompletedThread {
  51. public:
  52. ~TransactionCompletedThread();
  53. void run();
  54. // Adds listener and callbackIds in case there are no SurfaceControls that are supposed
  55. // to be included in the callback. This functions should be call before attempting to add any
  56. // callback handles.
  57. status_t addCallback(const sp<ITransactionCompletedListener>& transactionListener,
  58. const std::vector<CallbackId>& callbackIds);
  59. // Informs the TransactionCompletedThread that there is a Transaction with a CallbackHandle
  60. // that needs to be latched and presented this frame. This function should be called once the
  61. // layer has received the CallbackHandle so the TransactionCompletedThread knows not to send
  62. // a callback for that Listener/Transaction pair until that CallbackHandle has been latched and
  63. // presented.
  64. status_t registerPendingCallbackHandle(const sp<CallbackHandle>& handle);
  65. // Notifies the TransactionCompletedThread that a pending CallbackHandle has been presented.
  66. status_t addPresentedCallbackHandles(const std::deque<sp<CallbackHandle>>& handles);
  67. // Adds the Transaction CallbackHandle from a layer that does not need to be relatched and
  68. // presented this frame.
  69. status_t addUnpresentedCallbackHandle(const sp<CallbackHandle>& handle);
  70. void addPresentFence(const sp<Fence>& presentFence);
  71. void sendCallbacks();
  72. private:
  73. void threadMain();
  74. status_t findTransactionStats(const sp<ITransactionCompletedListener>& listener,
  75. const std::vector<CallbackId>& callbackIds,
  76. TransactionStats** outTransactionStats) REQUIRES(mMutex);
  77. status_t addCallbackHandle(const sp<CallbackHandle>& handle) REQUIRES(mMutex);
  78. class ThreadDeathRecipient : public IBinder::DeathRecipient {
  79. public:
  80. // This function is a no-op. isBinderAlive needs a linked DeathRecipient to work.
  81. // Death recipients needs a binderDied function.
  82. //
  83. // (isBinderAlive checks if BpBinder's mAlive is 0. mAlive is only set to 0 in sendObituary.
  84. // sendObituary is only called if linkToDeath was called with a DeathRecipient.)
  85. void binderDied(const wp<IBinder>& /*who*/) override {}
  86. };
  87. sp<ThreadDeathRecipient> mDeathRecipient;
  88. struct ITransactionCompletedListenerHash {
  89. std::size_t operator()(const sp<ITransactionCompletedListener>& listener) const {
  90. return std::hash<IBinder*>{}((listener) ? IInterface::asBinder(listener).get()
  91. : nullptr);
  92. }
  93. };
  94. // Protects the creation and destruction of mThread
  95. std::mutex mThreadMutex;
  96. std::thread mThread GUARDED_BY(mThreadMutex);
  97. std::mutex mMutex;
  98. std::condition_variable_any mConditionVariable;
  99. std::unordered_map<
  100. sp<ITransactionCompletedListener>,
  101. std::unordered_map<std::vector<CallbackId>, uint32_t /*count*/, CallbackIdsHash>,
  102. ITransactionCompletedListenerHash>
  103. mPendingTransactions GUARDED_BY(mMutex);
  104. std::unordered_map<sp<ITransactionCompletedListener>, std::deque<TransactionStats>,
  105. ITransactionCompletedListenerHash>
  106. mCompletedTransactions GUARDED_BY(mMutex);
  107. bool mRunning GUARDED_BY(mMutex) = false;
  108. bool mKeepRunning GUARDED_BY(mMutex) = true;
  109. sp<Fence> mPresentFence GUARDED_BY(mMutex);
  110. };
  111. } // namespace android