From 9c03b50a30c163ad4cee13455311812ef563db40 Mon Sep 17 00:00:00 2001 From: Jorim Jaggi Date: Tue, 24 Nov 2020 23:51:31 +0100 Subject: Add Shared timeline jank classification listener (1/2) Adds the ability to register a listener that gets informed about SF' jank classifications via the TransactionCompleted interface Bug: 17475548 Test: FrameTimelineTest Test: Register listener, ensure data flows back Change-Id: Ie42c508da605c03569eadab6ab18b7315b35d247 --- services/surfaceflinger/TransactionCompletedThread.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'services/surfaceflinger/TransactionCompletedThread.cpp') diff --git a/services/surfaceflinger/TransactionCompletedThread.cpp b/services/surfaceflinger/TransactionCompletedThread.cpp index ca244934e4..1797af4cf6 100644 --- a/services/surfaceflinger/TransactionCompletedThread.cpp +++ b/services/surfaceflinger/TransactionCompletedThread.cpp @@ -153,7 +153,7 @@ status_t TransactionCompletedThread::registerPendingCallbackHandle( } status_t TransactionCompletedThread::finalizePendingCallbackHandles( - const std::deque>& handles) { + const std::deque>& handles, const std::vector& jankData) { if (handles.empty()) { return NO_ERROR; } @@ -186,7 +186,7 @@ status_t TransactionCompletedThread::finalizePendingCallbackHandles( ALOGW("cannot find listener in mPendingTransactions"); } - status_t err = addCallbackHandle(handle); + status_t err = addCallbackHandle(handle, jankData); if (err != NO_ERROR) { ALOGE("could not add callback handle"); return err; @@ -204,7 +204,7 @@ status_t TransactionCompletedThread::registerUnpresentedCallbackHandle( return BAD_VALUE; } - return addCallbackHandle(handle); + return addCallbackHandle(handle, std::vector()); } status_t TransactionCompletedThread::findTransactionStats( @@ -225,7 +225,8 @@ status_t TransactionCompletedThread::findTransactionStats( return BAD_VALUE; } -status_t TransactionCompletedThread::addCallbackHandle(const sp& handle) { +status_t TransactionCompletedThread::addCallbackHandle(const sp& handle, + const std::vector& jankData) { // If we can't find the transaction stats something has gone wrong. The client should call // startRegistration before trying to add a callback handle. TransactionStats* transactionStats; @@ -246,7 +247,7 @@ status_t TransactionCompletedThread::addCallbackHandle(const sp& handle->dequeueReadyTime); transactionStats->surfaceStats.emplace_back(surfaceControl, handle->acquireTime, handle->previousReleaseFence, - handle->transformHint, eventStats); + handle->transformHint, eventStats, jankData); } return NO_ERROR; } -- cgit v1.2.3-59-g8ed1b From 9a803c3e842f0bd56b461cc7db912c2f4c09e39f Mon Sep 17 00:00:00 2001 From: Robert Carr Date: Thu, 14 Jan 2021 16:57:58 -0800 Subject: SurfaceFlinger: Emit TransactionCompleted callbacks directly. Transaction complete callbacks (BLAST Buffer release callbacks), are emitted from a TransactionCompletedThread. There's no clear reason for this behavior, as the actual runtime is exceedingly minimal. This opens us to scheduling delays when we call TransactionCompletedThread.sendCallbacks. In this CL we both remove the threaded nature of TransactionCompletedThread, and also take advantage of it running on the main thread to reduce some unnecessary copying and further reduce its runtime. Bug: 176691195 Test: Existing tests pass Change-Id: Ib1661df24c4a2ee39fc28c29a395158ce0ee7b7f --- services/surfaceflinger/Android.bp | 2 +- services/surfaceflinger/BufferStateLayer.cpp | 6 +- services/surfaceflinger/Layer.h | 2 +- services/surfaceflinger/SurfaceFlinger.cpp | 16 +- services/surfaceflinger/SurfaceFlinger.h | 8 +- .../surfaceflinger/TransactionCallbackInvoker.cpp | 298 +++++++++++++++++ .../surfaceflinger/TransactionCallbackInvoker.h | 127 +++++++ .../surfaceflinger/TransactionCompletedThread.cpp | 372 --------------------- .../surfaceflinger/TransactionCompletedThread.h | 137 -------- 9 files changed, 442 insertions(+), 526 deletions(-) create mode 100644 services/surfaceflinger/TransactionCallbackInvoker.cpp create mode 100644 services/surfaceflinger/TransactionCallbackInvoker.h delete mode 100644 services/surfaceflinger/TransactionCompletedThread.cpp delete mode 100644 services/surfaceflinger/TransactionCompletedThread.h (limited to 'services/surfaceflinger/TransactionCompletedThread.cpp') diff --git a/services/surfaceflinger/Android.bp b/services/surfaceflinger/Android.bp index a7cd2582b7..71a6329821 100644 --- a/services/surfaceflinger/Android.bp +++ b/services/surfaceflinger/Android.bp @@ -173,7 +173,7 @@ filegroup { "SurfaceFlingerDefaultFactory.cpp", "SurfaceInterceptor.cpp", "SurfaceTracing.cpp", - "TransactionCompletedThread.cpp", + "TransactionCallbackInvoker.cpp", ], } diff --git a/services/surfaceflinger/BufferStateLayer.cpp b/services/surfaceflinger/BufferStateLayer.cpp index 3dc62e3091..5b831a7018 100644 --- a/services/surfaceflinger/BufferStateLayer.cpp +++ b/services/surfaceflinger/BufferStateLayer.cpp @@ -188,7 +188,7 @@ void BufferStateLayer::releasePendingBuffer(nsecs_t dequeueReadyTime) { JankData(surfaceFrame->getToken(), surfaceFrame->getJankType().value())); } - mFlinger->getTransactionCompletedThread().finalizePendingCallbackHandles( + mFlinger->getTransactionCallbackInvoker().finalizePendingCallbackHandles( mDrawingState.callbackHandles, jankData); mDrawingState.callbackHandles = {}; @@ -443,14 +443,14 @@ bool BufferStateLayer::setTransactionCompletedListeners( // Notify the transaction completed thread that there is a pending latched callback // handle - mFlinger->getTransactionCompletedThread().registerPendingCallbackHandle(handle); + mFlinger->getTransactionCallbackInvoker().registerPendingCallbackHandle(handle); // Store so latched time and release fence can be set mCurrentState.callbackHandles.push_back(handle); } else { // If this layer will NOT need to be relatched and presented this frame // Notify the transaction completed thread this handle is done - mFlinger->getTransactionCompletedThread().registerUnpresentedCallbackHandle(handle); + mFlinger->getTransactionCallbackInvoker().registerUnpresentedCallbackHandle(handle); } } diff --git a/services/surfaceflinger/Layer.h b/services/surfaceflinger/Layer.h index 4b40c8ed83..15da2037a0 100644 --- a/services/surfaceflinger/Layer.h +++ b/services/surfaceflinger/Layer.h @@ -54,7 +54,7 @@ #include "Scheduler/Seamlessness.h" #include "SurfaceFlinger.h" #include "SurfaceTracing.h" -#include "TransactionCompletedThread.h" +#include "TransactionCallbackInvoker.h" using namespace android::surfaceflinger; diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp index e9b587510a..a131118392 100644 --- a/services/surfaceflinger/SurfaceFlinger.cpp +++ b/services/surfaceflinger/SurfaceFlinger.cpp @@ -2195,8 +2195,8 @@ void SurfaceFlinger::postComposition() { } }); - mTransactionCompletedThread.addPresentFence(mPreviousPresentFences[0]); - mTransactionCompletedThread.sendCallbacks(); + mTransactionCallbackInvoker.addPresentFence(mPreviousPresentFences[0]); + mTransactionCallbackInvoker.sendCallbacks(); if (display && display->isPrimary() && display->getPowerMode() == hal::PowerMode::ON && presentFenceTime->isValid()) { @@ -3547,8 +3547,8 @@ void SurfaceFlinger::applyTransactionState(const FrameTimelineInfo& frameTimelin // that listeners with SurfaceControls will start registration during setClientStateLocked // below. for (const auto& listener : listenerCallbacks) { - mTransactionCompletedThread.startRegistration(listener); - mTransactionCompletedThread.endRegistration(listener); + mTransactionCallbackInvoker.startRegistration(listener); + mTransactionCallbackInvoker.endRegistration(listener); } std::unordered_set listenerCallbacksWithSurfaces; @@ -3567,12 +3567,12 @@ void SurfaceFlinger::applyTransactionState(const FrameTimelineInfo& frameTimelin } for (const auto& listenerCallback : listenerCallbacksWithSurfaces) { - mTransactionCompletedThread.endRegistration(listenerCallback); + mTransactionCallbackInvoker.endRegistration(listenerCallback); } // If the state doesn't require a traversal and there are callbacks, send them now if (!(clientStateFlags & eTraversalNeeded) && hasListenerCallbacks) { - mTransactionCompletedThread.sendCallbacks(); + mTransactionCallbackInvoker.sendCallbacks(); } transactionFlags |= clientStateFlags; @@ -3687,7 +3687,7 @@ uint32_t SurfaceFlinger::setClientStateLocked( for (auto& listener : s.listeners) { // note that startRegistration will not re-register if the listener has // already be registered for a prior surface control - mTransactionCompletedThread.startRegistration(listener); + mTransactionCallbackInvoker.startRegistration(listener); listenerCallbacks.insert(listener); } @@ -3700,7 +3700,7 @@ uint32_t SurfaceFlinger::setClientStateLocked( } if (layer == nullptr) { for (auto& [listener, callbackIds] : s.listeners) { - mTransactionCompletedThread.registerUnpresentedCallbackHandle( + mTransactionCallbackInvoker.registerUnpresentedCallbackHandle( new CallbackHandle(listener, callbackIds, s.surface)); } return 0; diff --git a/services/surfaceflinger/SurfaceFlinger.h b/services/surfaceflinger/SurfaceFlinger.h index 66fc4f0d18..b3bb9a6bc4 100644 --- a/services/surfaceflinger/SurfaceFlinger.h +++ b/services/surfaceflinger/SurfaceFlinger.h @@ -64,7 +64,7 @@ #include "SurfaceFlingerFactory.h" #include "SurfaceTracing.h" #include "TracedOrdinal.h" -#include "TransactionCompletedThread.h" +#include "TransactionCallbackInvoker.h" #include #include @@ -319,8 +319,8 @@ public: void removeFromOffscreenLayers(Layer* layer); - TransactionCompletedThread& getTransactionCompletedThread() { - return mTransactionCompletedThread; + TransactionCallbackInvoker& getTransactionCallbackInvoker() { + return mTransactionCallbackInvoker; } // Converts from a binder handle to a Layer @@ -1159,7 +1159,7 @@ private: std::atomic mHwcFrameMissedCount = 0; std::atomic mGpuFrameMissedCount = 0; - TransactionCompletedThread mTransactionCompletedThread; + TransactionCallbackInvoker mTransactionCallbackInvoker; // Restrict layers to use two buffers in their bufferqueues. bool mLayerTripleBufferingDisabled = false; diff --git a/services/surfaceflinger/TransactionCallbackInvoker.cpp b/services/surfaceflinger/TransactionCallbackInvoker.cpp new file mode 100644 index 0000000000..a78510e902 --- /dev/null +++ b/services/surfaceflinger/TransactionCallbackInvoker.cpp @@ -0,0 +1,298 @@ +/* + * Copyright 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// TODO(b/129481165): remove the #pragma below and fix conversion issues +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wconversion" + +//#define LOG_NDEBUG 0 +#undef LOG_TAG +#define LOG_TAG "TransactionCallbackInvoker" +#define ATRACE_TAG ATRACE_TAG_GRAPHICS + +#include "TransactionCallbackInvoker.h" + +#include + +#include +#include + +namespace android { + +// Returns 0 if they are equal +// <0 if the first id that doesn't match is lower in c2 or all ids match but c2 is shorter +// >0 if the first id that doesn't match is greater in c2 or all ids match but c2 is longer +// +// See CallbackIdsHash for a explaniation of why this works +static int compareCallbackIds(const std::vector& c1, + const std::vector& c2) { + if (c1.empty()) { + return !c2.empty(); + } + return c1.front() - c2.front(); +} + +TransactionCallbackInvoker::~TransactionCallbackInvoker() { + { + std::lock_guard lock(mMutex); + for (const auto& [listener, transactionStats] : mCompletedTransactions) { + listener->unlinkToDeath(mDeathRecipient); + } + } +} + +status_t TransactionCallbackInvoker::startRegistration(const ListenerCallbacks& listenerCallbacks) { + std::lock_guard lock(mMutex); + + auto [itr, inserted] = mRegisteringTransactions.insert(listenerCallbacks); + auto& [listener, callbackIds] = listenerCallbacks; + + if (inserted) { + if (mCompletedTransactions.count(listener) == 0) { + status_t err = listener->linkToDeath(mDeathRecipient); + if (err != NO_ERROR) { + ALOGE("cannot add callback because linkToDeath failed, err: %d", err); + return err; + } + } + auto& transactionStatsDeque = mCompletedTransactions[listener]; + transactionStatsDeque.emplace_back(callbackIds); + } + + return NO_ERROR; +} + +status_t TransactionCallbackInvoker::endRegistration(const ListenerCallbacks& listenerCallbacks) { + std::lock_guard lock(mMutex); + + auto itr = mRegisteringTransactions.find(listenerCallbacks); + if (itr == mRegisteringTransactions.end()) { + ALOGE("cannot end a registration that does not exist"); + return BAD_VALUE; + } + + mRegisteringTransactions.erase(itr); + + return NO_ERROR; +} + +bool TransactionCallbackInvoker::isRegisteringTransaction( + const sp& transactionListener, const std::vector& callbackIds) { + ListenerCallbacks listenerCallbacks(transactionListener, callbackIds); + + auto itr = mRegisteringTransactions.find(listenerCallbacks); + return itr != mRegisteringTransactions.end(); +} + +status_t TransactionCallbackInvoker::registerPendingCallbackHandle( + const sp& handle) { + std::lock_guard lock(mMutex); + + // If we can't find the transaction stats something has gone wrong. The client should call + // startRegistration before trying to register a pending callback handle. + TransactionStats* transactionStats; + status_t err = findTransactionStats(handle->listener, handle->callbackIds, &transactionStats); + if (err != NO_ERROR) { + ALOGE("cannot find transaction stats"); + return err; + } + + mPendingTransactions[handle->listener][handle->callbackIds]++; + return NO_ERROR; +} + +status_t TransactionCallbackInvoker::finalizePendingCallbackHandles( + const std::deque>& handles, const std::vector& jankData) { + if (handles.empty()) { + return NO_ERROR; + } + std::lock_guard lock(mMutex); + + for (const auto& handle : handles) { + auto listener = mPendingTransactions.find(handle->listener); + if (listener != mPendingTransactions.end()) { + auto& pendingCallbacks = listener->second; + auto pendingCallback = pendingCallbacks.find(handle->callbackIds); + + if (pendingCallback != pendingCallbacks.end()) { + auto& pendingCount = pendingCallback->second; + + // Decrease the pending count for this listener + if (--pendingCount == 0) { + pendingCallbacks.erase(pendingCallback); + } + } else { + ALOGW("there are more latched callbacks than there were registered callbacks"); + } + if (listener->second.size() == 0) { + mPendingTransactions.erase(listener); + } + } else { + ALOGW("cannot find listener in mPendingTransactions"); + } + + status_t err = addCallbackHandle(handle, jankData); + if (err != NO_ERROR) { + ALOGE("could not add callback handle"); + return err; + } + } + + return NO_ERROR; +} + +status_t TransactionCallbackInvoker::registerUnpresentedCallbackHandle( + const sp& handle) { + std::lock_guard lock(mMutex); + + return addCallbackHandle(handle, std::vector()); +} + +status_t TransactionCallbackInvoker::findTransactionStats( + const sp& listener, const std::vector& callbackIds, + TransactionStats** outTransactionStats) { + auto& transactionStatsDeque = mCompletedTransactions[listener]; + + // Search back to front because the most recent transactions are at the back of the deque + auto itr = transactionStatsDeque.rbegin(); + for (; itr != transactionStatsDeque.rend(); itr++) { + if (compareCallbackIds(itr->callbackIds, callbackIds) == 0) { + *outTransactionStats = &(*itr); + return NO_ERROR; + } + } + + ALOGE("could not find transaction stats"); + return BAD_VALUE; +} + +status_t TransactionCallbackInvoker::addCallbackHandle(const sp& handle, + const std::vector& jankData) { + // If we can't find the transaction stats something has gone wrong. The client should call + // startRegistration before trying to add a callback handle. + TransactionStats* transactionStats; + status_t err = findTransactionStats(handle->listener, handle->callbackIds, &transactionStats); + if (err != NO_ERROR) { + return err; + } + + transactionStats->latchTime = handle->latchTime; + // If the layer has already been destroyed, don't add the SurfaceControl to the callback. + // The client side keeps a sp<> to the SurfaceControl so if the SurfaceControl has been + // destroyed the client side is dead and there won't be anyone to send the callback to. + sp surfaceControl = handle->surfaceControl.promote(); + if (surfaceControl) { + FrameEventHistoryStats eventStats(handle->frameNumber, + handle->gpuCompositionDoneFence->getSnapshot().fence, + handle->compositorTiming, handle->refreshStartTime, + handle->dequeueReadyTime); + transactionStats->surfaceStats.emplace_back(surfaceControl, handle->acquireTime, + handle->previousReleaseFence, + handle->transformHint, eventStats, jankData); + } + return NO_ERROR; +} + +void TransactionCallbackInvoker::addPresentFence(const sp& presentFence) { + std::lock_guard lock(mMutex); + mPresentFence = presentFence; +} + +void TransactionCallbackInvoker::sendCallbacks() { + std::lock_guard lock(mMutex); + + // For each listener + auto completedTransactionsItr = mCompletedTransactions.begin(); + while (completedTransactionsItr != mCompletedTransactions.end()) { + auto& [listener, transactionStatsDeque] = *completedTransactionsItr; + ListenerStats listenerStats; + listenerStats.listener = listener; + + // For each transaction + auto transactionStatsItr = transactionStatsDeque.begin(); + while (transactionStatsItr != transactionStatsDeque.end()) { + auto& transactionStats = *transactionStatsItr; + + // If this transaction is still registering, it is not safe to send a callback + // because there could be surface controls that haven't been added to + // transaction stats or mPendingTransactions. + if (isRegisteringTransaction(listener, transactionStats.callbackIds)) { + break; + } + + // If we are still waiting on the callback handles for this transaction, stop + // here because all transaction callbacks for the same listener must come in order + auto pendingTransactions = mPendingTransactions.find(listener); + if (pendingTransactions != mPendingTransactions.end() && + pendingTransactions->second.count(transactionStats.callbackIds) != 0) { + break; + } + + // If the transaction has been latched + if (transactionStats.latchTime >= 0) { + if (!mPresentFence) { + break; + } + transactionStats.presentFence = mPresentFence; + } + + // Remove the transaction from completed to the callback + listenerStats.transactionStats.push_back(std::move(transactionStats)); + transactionStatsItr = transactionStatsDeque.erase(transactionStatsItr); + } + // If the listener has completed transactions + if (!listenerStats.transactionStats.empty()) { + // If the listener is still alive + if (listener->isBinderAlive()) { + // Send callback. The listener stored in listenerStats + // comes from the cross-process setTransactionState call to + // SF. This MUST be an ITransactionCompletedListener. We + // keep it as an IBinder due to consistency reasons: if we + // interface_cast at the IPC boundary when reading a Parcel, + // we get pointers that compare unequal in the SF process. + interface_cast(listenerStats.listener) + ->onTransactionCompleted(listenerStats); + if (transactionStatsDeque.empty()) { + listener->unlinkToDeath(mDeathRecipient); + completedTransactionsItr = + mCompletedTransactions.erase(completedTransactionsItr); + } else { + completedTransactionsItr++; + } + } else { + completedTransactionsItr = + mCompletedTransactions.erase(completedTransactionsItr); + } + } else { + completedTransactionsItr++; + } + } + + if (mPresentFence) { + mPresentFence.clear(); + } +} + +// ----------------------------------------------------------------------- + +CallbackHandle::CallbackHandle(const sp& transactionListener, + const std::vector& ids, const sp& sc) + : listener(transactionListener), callbackIds(ids), surfaceControl(sc) {} + +} // namespace android + +// TODO(b/129481165): remove the #pragma below and fix conversion issues +#pragma clang diagnostic pop // ignored "-Wconversion" diff --git a/services/surfaceflinger/TransactionCallbackInvoker.h b/services/surfaceflinger/TransactionCallbackInvoker.h new file mode 100644 index 0000000000..a240c824a1 --- /dev/null +++ b/services/surfaceflinger/TransactionCallbackInvoker.h @@ -0,0 +1,127 @@ +/* + * Copyright 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include + +namespace android { + +class CallbackHandle : public RefBase { +public: + CallbackHandle(const sp& transactionListener, const std::vector& ids, + const sp& sc); + + sp listener; + std::vector callbackIds; + wp surfaceControl; + + bool releasePreviousBuffer = false; + sp previousReleaseFence; + nsecs_t acquireTime = -1; + nsecs_t latchTime = -1; + uint32_t transformHint = 0; + std::shared_ptr gpuCompositionDoneFence{FenceTime::NO_FENCE}; + CompositorTiming compositorTiming; + nsecs_t refreshStartTime = 0; + nsecs_t dequeueReadyTime = 0; + uint64_t frameNumber = 0; +}; + +class TransactionCallbackInvoker { +public: + ~TransactionCallbackInvoker(); + + // Adds listener and callbackIds in case there are no SurfaceControls that are supposed + // to be included in the callback. This functions should be call before attempting to register + // any callback handles. + status_t startRegistration(const ListenerCallbacks& listenerCallbacks); + // Ends the registration. After this is called, no more CallbackHandles will be registered. + // It is safe to send a callback if the Transaction doesn't have any Pending callback handles. + status_t endRegistration(const ListenerCallbacks& listenerCallbacks); + + // Informs the TransactionCallbackInvoker that there is a Transaction with a CallbackHandle + // that needs to be latched and presented this frame. This function should be called once the + // layer has received the CallbackHandle so the TransactionCallbackInvoker knows not to send + // a callback for that Listener/Transaction pair until that CallbackHandle has been latched and + // presented. + status_t registerPendingCallbackHandle(const sp& handle); + // Notifies the TransactionCallbackInvoker that a pending CallbackHandle has been presented. + status_t finalizePendingCallbackHandles(const std::deque>& handles, + const std::vector& jankData); + + // Adds the Transaction CallbackHandle from a layer that does not need to be relatched and + // presented this frame. + status_t registerUnpresentedCallbackHandle(const sp& handle); + + void addPresentFence(const sp& presentFence); + + void sendCallbacks(); + +private: + + bool isRegisteringTransaction(const sp& transactionListener, + const std::vector& callbackIds) REQUIRES(mMutex); + + status_t findTransactionStats(const sp& listener, + const std::vector& callbackIds, + TransactionStats** outTransactionStats) REQUIRES(mMutex); + + status_t addCallbackHandle(const sp& handle, + const std::vector& jankData) REQUIRES(mMutex); + + class CallbackDeathRecipient : public IBinder::DeathRecipient { + public: + // This function is a no-op. isBinderAlive needs a linked DeathRecipient to work. + // Death recipients needs a binderDied function. + // + // (isBinderAlive checks if BpBinder's mAlive is 0. mAlive is only set to 0 in sendObituary. + // sendObituary is only called if linkToDeath was called with a DeathRecipient.) + void binderDied(const wp& /*who*/) override {} + }; + sp mDeathRecipient = + new CallbackDeathRecipient(); + + std::mutex mMutex; + std::condition_variable_any mConditionVariable; + + std::unordered_set mRegisteringTransactions + GUARDED_BY(mMutex); + + std::unordered_map< + sp, + std::unordered_map, uint32_t /*count*/, CallbackIdsHash>, + IListenerHash> + mPendingTransactions GUARDED_BY(mMutex); + + std::unordered_map, std::deque, IListenerHash> + mCompletedTransactions GUARDED_BY(mMutex); + + sp mPresentFence GUARDED_BY(mMutex); +}; + +} // namespace android diff --git a/services/surfaceflinger/TransactionCompletedThread.cpp b/services/surfaceflinger/TransactionCompletedThread.cpp deleted file mode 100644 index 1797af4cf6..0000000000 --- a/services/surfaceflinger/TransactionCompletedThread.cpp +++ /dev/null @@ -1,372 +0,0 @@ -/* - * Copyright 2018 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -// TODO(b/129481165): remove the #pragma below and fix conversion issues -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wconversion" - -//#define LOG_NDEBUG 0 -#undef LOG_TAG -#define LOG_TAG "TransactionCompletedThread" -#define ATRACE_TAG ATRACE_TAG_GRAPHICS - -#include "TransactionCompletedThread.h" - -#include - -#include -#include - -namespace android { - -// Returns 0 if they are equal -// <0 if the first id that doesn't match is lower in c2 or all ids match but c2 is shorter -// >0 if the first id that doesn't match is greater in c2 or all ids match but c2 is longer -// -// See CallbackIdsHash for a explaniation of why this works -static int compareCallbackIds(const std::vector& c1, - const std::vector& c2) { - if (c1.empty()) { - return !c2.empty(); - } - return c1.front() - c2.front(); -} - -TransactionCompletedThread::~TransactionCompletedThread() { - std::lock_guard lockThread(mThreadMutex); - - { - std::lock_guard lock(mMutex); - mKeepRunning = false; - mConditionVariable.notify_all(); - } - - if (mThread.joinable()) { - mThread.join(); - } - - { - std::lock_guard lock(mMutex); - for (const auto& [listener, transactionStats] : mCompletedTransactions) { - listener->unlinkToDeath(mDeathRecipient); - } - } -} - -void TransactionCompletedThread::run() { - std::lock_guard lock(mMutex); - if (mRunning || !mKeepRunning) { - return; - } - mDeathRecipient = new ThreadDeathRecipient(); - mRunning = true; - - std::lock_guard lockThread(mThreadMutex); - mThread = std::thread(&TransactionCompletedThread::threadMain, this); -} - -status_t TransactionCompletedThread::startRegistration(const ListenerCallbacks& listenerCallbacks) { - // begin running if not already running - run(); - std::lock_guard lock(mMutex); - if (!mRunning) { - ALOGE("cannot add callback because the callback thread isn't running"); - return BAD_VALUE; - } - - auto [itr, inserted] = mRegisteringTransactions.insert(listenerCallbacks); - auto& [listener, callbackIds] = listenerCallbacks; - - if (inserted) { - if (mCompletedTransactions.count(listener) == 0) { - status_t err = listener->linkToDeath(mDeathRecipient); - if (err != NO_ERROR) { - ALOGE("cannot add callback because linkToDeath failed, err: %d", err); - return err; - } - } - auto& transactionStatsDeque = mCompletedTransactions[listener]; - transactionStatsDeque.emplace_back(callbackIds); - } - - return NO_ERROR; -} - -status_t TransactionCompletedThread::endRegistration(const ListenerCallbacks& listenerCallbacks) { - std::lock_guard lock(mMutex); - if (!mRunning) { - ALOGE("cannot add callback because the callback thread isn't running"); - return BAD_VALUE; - } - - auto itr = mRegisteringTransactions.find(listenerCallbacks); - if (itr == mRegisteringTransactions.end()) { - ALOGE("cannot end a registration that does not exist"); - return BAD_VALUE; - } - - mRegisteringTransactions.erase(itr); - - return NO_ERROR; -} - -bool TransactionCompletedThread::isRegisteringTransaction( - const sp& transactionListener, const std::vector& callbackIds) { - ListenerCallbacks listenerCallbacks(transactionListener, callbackIds); - - auto itr = mRegisteringTransactions.find(listenerCallbacks); - return itr != mRegisteringTransactions.end(); -} - -status_t TransactionCompletedThread::registerPendingCallbackHandle( - const sp& handle) { - std::lock_guard lock(mMutex); - if (!mRunning) { - ALOGE("cannot register callback handle because the callback thread isn't running"); - return BAD_VALUE; - } - - // If we can't find the transaction stats something has gone wrong. The client should call - // startRegistration before trying to register a pending callback handle. - TransactionStats* transactionStats; - status_t err = findTransactionStats(handle->listener, handle->callbackIds, &transactionStats); - if (err != NO_ERROR) { - ALOGE("cannot find transaction stats"); - return err; - } - - mPendingTransactions[handle->listener][handle->callbackIds]++; - return NO_ERROR; -} - -status_t TransactionCompletedThread::finalizePendingCallbackHandles( - const std::deque>& handles, const std::vector& jankData) { - if (handles.empty()) { - return NO_ERROR; - } - std::lock_guard lock(mMutex); - if (!mRunning) { - ALOGE("cannot add presented callback handle because the callback thread isn't running"); - return BAD_VALUE; - } - - for (const auto& handle : handles) { - auto listener = mPendingTransactions.find(handle->listener); - if (listener != mPendingTransactions.end()) { - auto& pendingCallbacks = listener->second; - auto pendingCallback = pendingCallbacks.find(handle->callbackIds); - - if (pendingCallback != pendingCallbacks.end()) { - auto& pendingCount = pendingCallback->second; - - // Decrease the pending count for this listener - if (--pendingCount == 0) { - pendingCallbacks.erase(pendingCallback); - } - } else { - ALOGW("there are more latched callbacks than there were registered callbacks"); - } - if (listener->second.size() == 0) { - mPendingTransactions.erase(listener); - } - } else { - ALOGW("cannot find listener in mPendingTransactions"); - } - - status_t err = addCallbackHandle(handle, jankData); - if (err != NO_ERROR) { - ALOGE("could not add callback handle"); - return err; - } - } - - return NO_ERROR; -} - -status_t TransactionCompletedThread::registerUnpresentedCallbackHandle( - const sp& handle) { - std::lock_guard lock(mMutex); - if (!mRunning) { - ALOGE("cannot add unpresented callback handle because the callback thread isn't running"); - return BAD_VALUE; - } - - return addCallbackHandle(handle, std::vector()); -} - -status_t TransactionCompletedThread::findTransactionStats( - const sp& listener, const std::vector& callbackIds, - TransactionStats** outTransactionStats) { - auto& transactionStatsDeque = mCompletedTransactions[listener]; - - // Search back to front because the most recent transactions are at the back of the deque - auto itr = transactionStatsDeque.rbegin(); - for (; itr != transactionStatsDeque.rend(); itr++) { - if (compareCallbackIds(itr->callbackIds, callbackIds) == 0) { - *outTransactionStats = &(*itr); - return NO_ERROR; - } - } - - ALOGE("could not find transaction stats"); - return BAD_VALUE; -} - -status_t TransactionCompletedThread::addCallbackHandle(const sp& handle, - const std::vector& jankData) { - // If we can't find the transaction stats something has gone wrong. The client should call - // startRegistration before trying to add a callback handle. - TransactionStats* transactionStats; - status_t err = findTransactionStats(handle->listener, handle->callbackIds, &transactionStats); - if (err != NO_ERROR) { - return err; - } - - transactionStats->latchTime = handle->latchTime; - // If the layer has already been destroyed, don't add the SurfaceControl to the callback. - // The client side keeps a sp<> to the SurfaceControl so if the SurfaceControl has been - // destroyed the client side is dead and there won't be anyone to send the callback to. - sp surfaceControl = handle->surfaceControl.promote(); - if (surfaceControl) { - FrameEventHistoryStats eventStats(handle->frameNumber, - handle->gpuCompositionDoneFence->getSnapshot().fence, - handle->compositorTiming, handle->refreshStartTime, - handle->dequeueReadyTime); - transactionStats->surfaceStats.emplace_back(surfaceControl, handle->acquireTime, - handle->previousReleaseFence, - handle->transformHint, eventStats, jankData); - } - return NO_ERROR; -} - -void TransactionCompletedThread::addPresentFence(const sp& presentFence) { - std::lock_guard lock(mMutex); - mPresentFence = presentFence; -} - -void TransactionCompletedThread::sendCallbacks() { - std::lock_guard lock(mMutex); - if (mRunning) { - mConditionVariable.notify_all(); - } -} - -void TransactionCompletedThread::threadMain() { - std::lock_guard lock(mMutex); - - while (mKeepRunning) { - mConditionVariable.wait(mMutex); - std::vector completedListenerStats; - - // For each listener - auto completedTransactionsItr = mCompletedTransactions.begin(); - while (completedTransactionsItr != mCompletedTransactions.end()) { - auto& [listener, transactionStatsDeque] = *completedTransactionsItr; - ListenerStats listenerStats; - listenerStats.listener = listener; - - // For each transaction - auto transactionStatsItr = transactionStatsDeque.begin(); - while (transactionStatsItr != transactionStatsDeque.end()) { - auto& transactionStats = *transactionStatsItr; - - // If this transaction is still registering, it is not safe to send a callback - // because there could be surface controls that haven't been added to - // transaction stats or mPendingTransactions. - if (isRegisteringTransaction(listener, transactionStats.callbackIds)) { - break; - } - - // If we are still waiting on the callback handles for this transaction, stop - // here because all transaction callbacks for the same listener must come in order - auto pendingTransactions = mPendingTransactions.find(listener); - if (pendingTransactions != mPendingTransactions.end() && - pendingTransactions->second.count(transactionStats.callbackIds) != 0) { - break; - } - - // If the transaction has been latched - if (transactionStats.latchTime >= 0) { - if (!mPresentFence) { - break; - } - transactionStats.presentFence = mPresentFence; - } - - // Remove the transaction from completed to the callback - listenerStats.transactionStats.push_back(std::move(transactionStats)); - transactionStatsItr = transactionStatsDeque.erase(transactionStatsItr); - } - // If the listener has completed transactions - if (!listenerStats.transactionStats.empty()) { - // If the listener is still alive - if (listener->isBinderAlive()) { - // Send callback. The listener stored in listenerStats - // comes from the cross-process setTransactionState call to - // SF. This MUST be an ITransactionCompletedListener. We - // keep it as an IBinder due to consistency reasons: if we - // interface_cast at the IPC boundary when reading a Parcel, - // we get pointers that compare unequal in the SF process. - interface_cast(listenerStats.listener) - ->onTransactionCompleted(listenerStats); - if (transactionStatsDeque.empty()) { - listener->unlinkToDeath(mDeathRecipient); - completedTransactionsItr = - mCompletedTransactions.erase(completedTransactionsItr); - } else { - completedTransactionsItr++; - } - } else { - completedTransactionsItr = - mCompletedTransactions.erase(completedTransactionsItr); - } - } else { - completedTransactionsItr++; - } - - completedListenerStats.push_back(std::move(listenerStats)); - } - - if (mPresentFence) { - mPresentFence.clear(); - } - - // If everyone else has dropped their reference to a layer and its listener is dead, - // we are about to cause the layer to be deleted. If this happens at the wrong time and - // we are holding mMutex, we will cause a deadlock. - // - // The deadlock happens because this thread is holding on to mMutex and when we delete - // the layer, it grabs SF's mStateLock. A different SF binder thread grabs mStateLock, - // then call's TransactionCompletedThread::run() which tries to grab mMutex. - // - // To avoid this deadlock, we need to unlock mMutex when dropping our last reference to - // to the layer. - mMutex.unlock(); - completedListenerStats.clear(); - mMutex.lock(); - } -} - -// ----------------------------------------------------------------------- - -CallbackHandle::CallbackHandle(const sp& transactionListener, - const std::vector& ids, const sp& sc) - : listener(transactionListener), callbackIds(ids), surfaceControl(sc) {} - -} // namespace android - -// TODO(b/129481165): remove the #pragma below and fix conversion issues -#pragma clang diagnostic pop // ignored "-Wconversion" diff --git a/services/surfaceflinger/TransactionCompletedThread.h b/services/surfaceflinger/TransactionCompletedThread.h deleted file mode 100644 index c4ba7e49ce..0000000000 --- a/services/surfaceflinger/TransactionCompletedThread.h +++ /dev/null @@ -1,137 +0,0 @@ -/* - * Copyright 2018 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#pragma once - -#include -#include -#include -#include -#include -#include - -#include - -#include -#include -#include - -namespace android { - -class CallbackHandle : public RefBase { -public: - CallbackHandle(const sp& transactionListener, const std::vector& ids, - const sp& sc); - - sp listener; - std::vector callbackIds; - wp surfaceControl; - - bool releasePreviousBuffer = false; - sp previousReleaseFence; - nsecs_t acquireTime = -1; - nsecs_t latchTime = -1; - uint32_t transformHint = 0; - std::shared_ptr gpuCompositionDoneFence{FenceTime::NO_FENCE}; - CompositorTiming compositorTiming; - nsecs_t refreshStartTime = 0; - nsecs_t dequeueReadyTime = 0; - uint64_t frameNumber = 0; -}; - -class TransactionCompletedThread { -public: - ~TransactionCompletedThread(); - - void run(); - - // Adds listener and callbackIds in case there are no SurfaceControls that are supposed - // to be included in the callback. This functions should be call before attempting to register - // any callback handles. - status_t startRegistration(const ListenerCallbacks& listenerCallbacks); - // Ends the registration. After this is called, no more CallbackHandles will be registered. - // It is safe to send a callback if the Transaction doesn't have any Pending callback handles. - status_t endRegistration(const ListenerCallbacks& listenerCallbacks); - - // Informs the TransactionCompletedThread that there is a Transaction with a CallbackHandle - // that needs to be latched and presented this frame. This function should be called once the - // layer has received the CallbackHandle so the TransactionCompletedThread knows not to send - // a callback for that Listener/Transaction pair until that CallbackHandle has been latched and - // presented. - status_t registerPendingCallbackHandle(const sp& handle); - // Notifies the TransactionCompletedThread that a pending CallbackHandle has been presented. - status_t finalizePendingCallbackHandles(const std::deque>& handles, - const std::vector& jankData); - - // Adds the Transaction CallbackHandle from a layer that does not need to be relatched and - // presented this frame. - status_t registerUnpresentedCallbackHandle(const sp& handle); - - void addPresentFence(const sp& presentFence); - - void sendCallbacks(); - -private: - void threadMain(); - - bool isRegisteringTransaction(const sp& transactionListener, - const std::vector& callbackIds) REQUIRES(mMutex); - - status_t findTransactionStats(const sp& listener, - const std::vector& callbackIds, - TransactionStats** outTransactionStats) REQUIRES(mMutex); - - status_t addCallbackHandle(const sp& handle, - const std::vector& jankData) REQUIRES(mMutex); - - class ThreadDeathRecipient : public IBinder::DeathRecipient { - public: - // This function is a no-op. isBinderAlive needs a linked DeathRecipient to work. - // Death recipients needs a binderDied function. - // - // (isBinderAlive checks if BpBinder's mAlive is 0. mAlive is only set to 0 in sendObituary. - // sendObituary is only called if linkToDeath was called with a DeathRecipient.) - void binderDied(const wp& /*who*/) override {} - }; - sp mDeathRecipient; - - // Protects the creation and destruction of mThread - std::mutex mThreadMutex; - - std::thread mThread GUARDED_BY(mThreadMutex); - - std::mutex mMutex; - std::condition_variable_any mConditionVariable; - - std::unordered_set mRegisteringTransactions - GUARDED_BY(mMutex); - - std::unordered_map< - sp, - std::unordered_map, uint32_t /*count*/, CallbackIdsHash>, - IListenerHash> - mPendingTransactions GUARDED_BY(mMutex); - - std::unordered_map, std::deque, IListenerHash> - mCompletedTransactions GUARDED_BY(mMutex); - - bool mRunning GUARDED_BY(mMutex) = false; - bool mKeepRunning GUARDED_BY(mMutex) = true; - - sp mPresentFence GUARDED_BY(mMutex); -}; - -} // namespace android -- cgit v1.2.3-59-g8ed1b