diff options
author | 2025-03-21 15:59:43 -0700 | |
---|---|---|
committer | 2025-03-21 15:59:43 -0700 | |
commit | 520d8fcb8aa54289f2272369d3040635c2fbd4bd (patch) | |
tree | 127a5fbd6bcbdf2c0c82207671a2b38c26c4991d | |
parent | b21851c436274c69dcf87199c90dd0a7d459e1fd (diff) | |
parent | d7b71ac5337c06dd3c368dc796d8ab17597818e7 (diff) |
Merge "Revert^2 "Use TransactionState in SurfaceFlinger."" into main
18 files changed, 196 insertions, 649 deletions
diff --git a/libs/gui/BLASTBufferQueue.cpp b/libs/gui/BLASTBufferQueue.cpp index 1aae13c1f4..5b0f21de91 100644 --- a/libs/gui/BLASTBufferQueue.cpp +++ b/libs/gui/BLASTBufferQueue.cpp @@ -1032,7 +1032,7 @@ void BLASTBufferQueue::mergeWithNextTransaction(SurfaceComposerClient::Transacti // Apply the transaction since we have already acquired the desired frame. t->setApplyToken(mApplyToken).apply(); } else { - mPendingTransactions.emplace_back(frameNumber, *t); + mPendingTransactions.emplace_back(frameNumber, std::move(*t)); // Clear the transaction so it can't be applied elsewhere. t->clear(); } @@ -1050,8 +1050,8 @@ void BLASTBufferQueue::applyPendingTransactions(uint64_t frameNumber) { void BLASTBufferQueue::mergePendingTransactions(SurfaceComposerClient::Transaction* t, uint64_t frameNumber) { auto mergeTransaction = - [&t, currentFrameNumber = frameNumber]( - std::tuple<uint64_t, SurfaceComposerClient::Transaction> pendingTransaction) { + [t, currentFrameNumber = frameNumber]( + std::pair<uint64_t, SurfaceComposerClient::Transaction>& pendingTransaction) { auto& [targetFrameNumber, transaction] = pendingTransaction; if (currentFrameNumber < targetFrameNumber) { return false; diff --git a/libs/gui/ISurfaceComposer.cpp b/libs/gui/ISurfaceComposer.cpp index 269936858a..ae4b74e03b 100644 --- a/libs/gui/ISurfaceComposer.cpp +++ b/libs/gui/ISurfaceComposer.cpp @@ -26,6 +26,7 @@ #include <gui/ISurfaceComposer.h> #include <gui/LayerState.h> #include <gui/SchedulingPolicy.h> +#include <gui/TransactionState.h> #include <private/gui/ParcelUtils.h> #include <stdint.h> #include <sys/types.h> @@ -60,54 +61,12 @@ public: virtual ~BpSurfaceComposer(); - status_t setTransactionState( - const FrameTimelineInfo& frameTimelineInfo, Vector<ComposerState>& state, - Vector<DisplayState>& displays, uint32_t flags, const sp<IBinder>& applyToken, - InputWindowCommands commands, int64_t desiredPresentTime, bool isAutoTimestamp, - const std::vector<client_cache_t>& uncacheBuffers, bool hasListenerCallbacks, - const std::vector<ListenerCallbacks>& listenerCallbacks, uint64_t transactionId, - const std::vector<uint64_t>& mergedTransactionIds) override { + status_t setTransactionState(TransactionState&& state) override { Parcel data, reply; data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor()); + SAFE_PARCEL(state.writeToParcel, &data); - frameTimelineInfo.writeToParcel(&data); - - SAFE_PARCEL(data.writeUint32, static_cast<uint32_t>(state.size())); - for (const auto& s : state) { - SAFE_PARCEL(s.write, data); - } - - SAFE_PARCEL(data.writeUint32, static_cast<uint32_t>(displays.size())); - for (const auto& d : displays) { - SAFE_PARCEL(d.write, data); - } - - SAFE_PARCEL(data.writeUint32, flags); - SAFE_PARCEL(data.writeStrongBinder, applyToken); - SAFE_PARCEL(commands.write, data); - SAFE_PARCEL(data.writeInt64, desiredPresentTime); - SAFE_PARCEL(data.writeBool, isAutoTimestamp); - SAFE_PARCEL(data.writeUint32, static_cast<uint32_t>(uncacheBuffers.size())); - for (const client_cache_t& uncacheBuffer : uncacheBuffers) { - SAFE_PARCEL(data.writeStrongBinder, uncacheBuffer.token.promote()); - SAFE_PARCEL(data.writeUint64, uncacheBuffer.id); - } - SAFE_PARCEL(data.writeBool, hasListenerCallbacks); - - SAFE_PARCEL(data.writeVectorSize, listenerCallbacks); - for (const auto& [listener, callbackIds] : listenerCallbacks) { - SAFE_PARCEL(data.writeStrongBinder, listener); - SAFE_PARCEL(data.writeParcelableVector, callbackIds); - } - - SAFE_PARCEL(data.writeUint64, transactionId); - - SAFE_PARCEL(data.writeUint32, static_cast<uint32_t>(mergedTransactionIds.size())); - for (auto mergedTransactionId : mergedTransactionIds) { - SAFE_PARCEL(data.writeUint64, mergedTransactionId); - } - - if (flags & ISurfaceComposer::eOneWay) { + if (state.mFlags & ISurfaceComposer::eOneWay) { return remote()->transact(BnSurfaceComposer::SET_TRANSACTION_STATE, data, &reply, IBinder::FLAG_ONEWAY); } else { @@ -132,75 +91,9 @@ status_t BnSurfaceComposer::onTransact( case SET_TRANSACTION_STATE: { CHECK_INTERFACE(ISurfaceComposer, data, reply); - FrameTimelineInfo frameTimelineInfo; - frameTimelineInfo.readFromParcel(&data); - - uint32_t count = 0; - SAFE_PARCEL_READ_SIZE(data.readUint32, &count, data.dataSize()); - Vector<ComposerState> state; - state.setCapacity(count); - for (size_t i = 0; i < count; i++) { - ComposerState s; - SAFE_PARCEL(s.read, data); - state.add(s); - } - - SAFE_PARCEL_READ_SIZE(data.readUint32, &count, data.dataSize()); - DisplayState d; - Vector<DisplayState> displays; - displays.setCapacity(count); - for (size_t i = 0; i < count; i++) { - SAFE_PARCEL(d.read, data); - displays.add(d); - } - - uint32_t stateFlags = 0; - SAFE_PARCEL(data.readUint32, &stateFlags); - sp<IBinder> applyToken; - SAFE_PARCEL(data.readStrongBinder, &applyToken); - InputWindowCommands inputWindowCommands; - SAFE_PARCEL(inputWindowCommands.read, data); - - int64_t desiredPresentTime = 0; - bool isAutoTimestamp = true; - SAFE_PARCEL(data.readInt64, &desiredPresentTime); - SAFE_PARCEL(data.readBool, &isAutoTimestamp); - - SAFE_PARCEL_READ_SIZE(data.readUint32, &count, data.dataSize()); - std::vector<client_cache_t> uncacheBuffers(count); - sp<IBinder> tmpBinder; - for (size_t i = 0; i < count; i++) { - SAFE_PARCEL(data.readNullableStrongBinder, &tmpBinder); - uncacheBuffers[i].token = tmpBinder; - SAFE_PARCEL(data.readUint64, &uncacheBuffers[i].id); - } - - bool hasListenerCallbacks = false; - SAFE_PARCEL(data.readBool, &hasListenerCallbacks); - - std::vector<ListenerCallbacks> listenerCallbacks; - int32_t listenersSize = 0; - SAFE_PARCEL_READ_SIZE(data.readInt32, &listenersSize, data.dataSize()); - for (int32_t i = 0; i < listenersSize; i++) { - SAFE_PARCEL(data.readStrongBinder, &tmpBinder); - std::vector<CallbackId> callbackIds; - SAFE_PARCEL(data.readParcelableVector, &callbackIds); - listenerCallbacks.emplace_back(tmpBinder, callbackIds); - } - - uint64_t transactionId = -1; - SAFE_PARCEL(data.readUint64, &transactionId); - - SAFE_PARCEL_READ_SIZE(data.readUint32, &count, data.dataSize()); - std::vector<uint64_t> mergedTransactions(count); - for (size_t i = 0; i < count; i++) { - SAFE_PARCEL(data.readUint64, &mergedTransactions[i]); - } - - return setTransactionState(frameTimelineInfo, state, displays, stateFlags, applyToken, - std::move(inputWindowCommands), desiredPresentTime, - isAutoTimestamp, uncacheBuffers, hasListenerCallbacks, - listenerCallbacks, transactionId, mergedTransactions); + TransactionState state; + SAFE_PARCEL(state.readFromParcel, &data); + return setTransactionState(std::move(state)); } case GET_SCHEDULING_POLICY: { gui::SchedulingPolicy policy; diff --git a/libs/gui/SurfaceComposerClient.cpp b/libs/gui/SurfaceComposerClient.cpp index 9854274cb1..69ba1d731d 100644 --- a/libs/gui/SurfaceComposerClient.cpp +++ b/libs/gui/SurfaceComposerClient.cpp @@ -824,34 +824,24 @@ void removeDeadBufferCallback(void* /*context*/, uint64_t graphicBufferId) { // --------------------------------------------------------------------------- SurfaceComposerClient::Transaction::Transaction() { - mId = generateId(); + mState.mId = generateId(); mTransactionCompletedListener = TransactionCompletedListener::getInstance(); } -SurfaceComposerClient::Transaction::Transaction(const Transaction& other) - : mId(other.mId), - mFlags(other.mFlags), - mMayContainBuffer(other.mMayContainBuffer), - mDesiredPresentTime(other.mDesiredPresentTime), - mIsAutoTimestamp(other.mIsAutoTimestamp), - mFrameTimelineInfo(other.mFrameTimelineInfo), - mApplyToken(other.mApplyToken) { - mDisplayStates = other.mDisplayStates; - mComposerStates = other.mComposerStates; - mInputWindowCommands = other.mInputWindowCommands; - mListenerCallbacks = other.mListenerCallbacks; - mTransactionCompletedListener = TransactionCompletedListener::getInstance(); -} +SurfaceComposerClient::Transaction::Transaction(Transaction&& other) + : mTransactionCompletedListener(TransactionCompletedListener::getInstance()), + mState(std::move(other.mState)), + mListenerCallbacks(std::move(other.mListenerCallbacks)) {} void SurfaceComposerClient::Transaction::sanitize(int pid, int uid) { uint32_t permissions = LayerStatePermissions::getTransactionPermissions(pid, uid); - for (auto& composerState : mComposerStates) { + for (auto& composerState : mState.mComposerStates) { composerState.state.sanitize(permissions); } - if (!mInputWindowCommands.empty() && + if (!mState.mInputWindowCommands.empty() && (permissions & layer_state_t::Permission::ACCESS_SURFACE_FLINGER) == 0) { ALOGE("Only privileged callers are allowed to send input commands."); - mInputWindowCommands.clear(); + mState.mInputWindowCommands.clear(); } } @@ -866,31 +856,10 @@ SurfaceComposerClient::Transaction::createFromParcel(const Parcel* parcel) { status_t SurfaceComposerClient::Transaction::readFromParcel(const Parcel* parcel) { - const uint64_t transactionId = parcel->readUint64(); - const uint32_t flags = parcel->readUint32(); - const int64_t desiredPresentTime = parcel->readInt64(); - const bool isAutoTimestamp = parcel->readBool(); - const bool logCallPoints = parcel->readBool(); - FrameTimelineInfo frameTimelineInfo; - frameTimelineInfo.readFromParcel(parcel); - - sp<IBinder> applyToken; - parcel->readNullableStrongBinder(&applyToken); - size_t count = static_cast<size_t>(parcel->readUint32()); - if (count > parcel->dataSize()) { - return BAD_VALUE; - } - Vector<DisplayState> displayStates; - displayStates.setCapacity(count); - for (size_t i = 0; i < count; i++) { - DisplayState displayState; - if (displayState.read(*parcel) == BAD_VALUE) { - return BAD_VALUE; - } - displayStates.add(displayState); - } + TransactionState tmpState; + SAFE_PARCEL(tmpState.readFromParcel, parcel); - count = static_cast<size_t>(parcel->readUint32()); + size_t count = static_cast<size_t>(parcel->readUint32()); if (count > parcel->dataSize()) { return BAD_VALUE; } @@ -919,57 +888,8 @@ status_t SurfaceComposerClient::Transaction::readFromParcel(const Parcel* parcel } } - count = static_cast<size_t>(parcel->readUint32()); - if (count > parcel->dataSize()) { - return BAD_VALUE; - } - Vector<ComposerState> composerStates; - composerStates.setCapacity(count); - for (size_t i = 0; i < count; i++) { - ComposerState composerState; - if (composerState.read(*parcel) == BAD_VALUE) { - return BAD_VALUE; - } - composerStates.add(composerState); - } - - InputWindowCommands inputWindowCommands; - inputWindowCommands.read(*parcel); - - count = static_cast<size_t>(parcel->readUint32()); - if (count > parcel->dataSize()) { - return BAD_VALUE; - } - std::vector<client_cache_t> uncacheBuffers(count); - for (size_t i = 0; i < count; i++) { - sp<IBinder> tmpBinder; - SAFE_PARCEL(parcel->readStrongBinder, &tmpBinder); - uncacheBuffers[i].token = tmpBinder; - SAFE_PARCEL(parcel->readUint64, &uncacheBuffers[i].id); - } - - count = static_cast<size_t>(parcel->readUint32()); - if (count > parcel->dataSize()) { - return BAD_VALUE; - } - std::vector<uint64_t> mergedTransactionIds(count); - for (size_t i = 0; i < count; i++) { - SAFE_PARCEL(parcel->readUint64, &mergedTransactionIds[i]); - } - - // Parsing was successful. Update the object. - mId = transactionId; - mFlags = flags; - mDesiredPresentTime = desiredPresentTime; - mIsAutoTimestamp = isAutoTimestamp; - mFrameTimelineInfo = frameTimelineInfo; - mDisplayStates = std::move(displayStates); - mListenerCallbacks = listenerCallbacks; - mComposerStates = std::move(composerStates); - mInputWindowCommands = inputWindowCommands; - mApplyToken = applyToken; - mUncacheBuffers = std::move(uncacheBuffers); - mMergedTransactionIds = std::move(mergedTransactionIds); + mState = std::move(tmpState); + mListenerCallbacks = std::move(listenerCallbacks); return NO_ERROR; } @@ -987,17 +907,7 @@ status_t SurfaceComposerClient::Transaction::writeToParcel(Parcel* parcel) const const_cast<SurfaceComposerClient::Transaction*>(this)->cacheBuffers(); - parcel->writeUint64(mId); - parcel->writeUint32(mFlags); - parcel->writeInt64(mDesiredPresentTime); - parcel->writeBool(mIsAutoTimestamp); - parcel->writeBool(mLogCallPoints); - mFrameTimelineInfo.writeToParcel(parcel); - parcel->writeStrongBinder(mApplyToken); - parcel->writeUint32(static_cast<uint32_t>(mDisplayStates.size())); - for (auto const& displayState : mDisplayStates) { - displayState.write(*parcel); - } + SAFE_PARCEL(mState.writeToParcel, parcel); parcel->writeUint32(static_cast<uint32_t>(mListenerCallbacks.size())); for (auto const& [listener, callbackInfo] : mListenerCallbacks) { @@ -1012,24 +922,6 @@ status_t SurfaceComposerClient::Transaction::writeToParcel(Parcel* parcel) const } } - parcel->writeUint32(static_cast<uint32_t>(mComposerStates.size())); - for (auto const& composerState : mComposerStates) { - composerState.write(*parcel); - } - - mInputWindowCommands.write(*parcel); - - SAFE_PARCEL(parcel->writeUint32, static_cast<uint32_t>(mUncacheBuffers.size())); - for (const client_cache_t& uncacheBuffer : mUncacheBuffers) { - SAFE_PARCEL(parcel->writeStrongBinder, uncacheBuffer.token.promote()); - SAFE_PARCEL(parcel->writeUint64, uncacheBuffer.id); - } - - SAFE_PARCEL(parcel->writeUint32, static_cast<uint32_t>(mMergedTransactionIds.size())); - for (auto mergedTransactionId : mMergedTransactionIds) { - SAFE_PARCEL(parcel->writeUint64, mergedTransactionId); - } - return NO_ERROR; } @@ -1054,50 +946,8 @@ void SurfaceComposerClient::Transaction::releaseBufferIfOverwriting(const layer_ } SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::merge(Transaction&& other) { - while (mMergedTransactionIds.size() + other.mMergedTransactionIds.size() > - MAX_MERGE_HISTORY_LENGTH - 1 && - mMergedTransactionIds.size() > 0) { - mMergedTransactionIds.pop_back(); - } - if (other.mMergedTransactionIds.size() == MAX_MERGE_HISTORY_LENGTH) { - mMergedTransactionIds.insert(mMergedTransactionIds.begin(), - other.mMergedTransactionIds.begin(), - other.mMergedTransactionIds.end() - 1); - } else if (other.mMergedTransactionIds.size() > 0u) { - mMergedTransactionIds.insert(mMergedTransactionIds.begin(), - other.mMergedTransactionIds.begin(), - other.mMergedTransactionIds.end()); - } - mMergedTransactionIds.insert(mMergedTransactionIds.begin(), other.mId); - - for (auto const& otherState : other.mComposerStates) { - if (auto it = std::find_if(mComposerStates.begin(), mComposerStates.end(), - [&otherState](const auto& composerState) { - return composerState.state.surface == - otherState.state.surface; - }); - it != mComposerStates.end()) { - if (otherState.state.what & layer_state_t::eBufferChanged) { - releaseBufferIfOverwriting(it->state); - } - it->state.merge(otherState.state); - } else { - mComposerStates.add(otherState); - } - } - - for (auto const& state : other.mDisplayStates) { - if (auto it = std::find_if(mDisplayStates.begin(), mDisplayStates.end(), - [&state](const auto& displayState) { - return displayState.token == state.token; - }); - it != mDisplayStates.end()) { - it->merge(state); - } else { - mDisplayStates.add(state); - } - } - + mState.merge(std::move(other.mState), + std::bind(&Transaction::releaseBufferIfOverwriting, this, std::placeholders::_1)); for (const auto& [listener, callbackInfo] : other.mListenerCallbacks) { auto& [callbackIds, surfaceControls] = callbackInfo; mListenerCallbacks[listener].callbackIds.insert(std::make_move_iterator( @@ -1121,50 +971,21 @@ SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::merge(Tr } } - for (const auto& cacheId : other.mUncacheBuffers) { - mUncacheBuffers.push_back(cacheId); - } - - mInputWindowCommands.merge(other.mInputWindowCommands); - - mMayContainBuffer |= other.mMayContainBuffer; - mFlags |= other.mFlags; - mApplyToken = other.mApplyToken; - - mergeFrameTimelineInfo(mFrameTimelineInfo, other.mFrameTimelineInfo); - - mLogCallPoints |= other.mLogCallPoints; - if (mLogCallPoints) { - ALOG(LOG_DEBUG, LOG_SURFACE_CONTROL_REGISTRY, - "Transaction %" PRIu64 " merged with transaction %" PRIu64, other.getId(), mId); - } - other.clear(); return *this; } void SurfaceComposerClient::Transaction::clear() { - mComposerStates.clear(); - mDisplayStates.clear(); + mState.clear(); mListenerCallbacks.clear(); - mInputWindowCommands.clear(); - mUncacheBuffers.clear(); - mMayContainBuffer = false; - mDesiredPresentTime = 0; - mIsAutoTimestamp = true; - mFrameTimelineInfo = {}; - mApplyToken = nullptr; - mMergedTransactionIds.clear(); - mLogCallPoints = false; - mFlags = 0; } -uint64_t SurfaceComposerClient::Transaction::getId() { - return mId; +uint64_t SurfaceComposerClient::Transaction::getId() const { + return mState.mId; } std::vector<uint64_t> SurfaceComposerClient::Transaction::getMergedTransactionIds() { - return mMergedTransactionIds; + return mState.mMergedTransactionIds; } void SurfaceComposerClient::doUncacheBufferTransaction(uint64_t cacheId) { @@ -1173,12 +994,13 @@ void SurfaceComposerClient::doUncacheBufferTransaction(uint64_t cacheId) { client_cache_t uncacheBuffer; uncacheBuffer.token = BufferCache::getInstance().getToken(); uncacheBuffer.id = cacheId; - Vector<ComposerState> composerStates; - Vector<DisplayState> displayStates; - status_t status = sf->setTransactionState(FrameTimelineInfo{}, composerStates, displayStates, - ISurfaceComposer::eOneWay, - Transaction::getDefaultApplyToken(), {}, systemTime(), - true, {uncacheBuffer}, false, {}, generateId(), {}); + TransactionState state; + state.mId = generateId(); + state.mApplyToken = Transaction::getDefaultApplyToken(); + state.mUncacheBuffers.emplace_back(std::move(uncacheBuffer)); + state.mFlags = ISurfaceComposer::eOneWay; + state.mDesiredPresentTime = systemTime(); + status_t status = sf->setTransactionState(std::move(state)); if (status != NO_ERROR) { ALOGE_AND_TRACE("SurfaceComposerClient::doUncacheBufferTransaction - %s", strerror(-status)); @@ -1186,12 +1008,12 @@ void SurfaceComposerClient::doUncacheBufferTransaction(uint64_t cacheId) { } void SurfaceComposerClient::Transaction::cacheBuffers() { - if (!mMayContainBuffer) { + if (!mState.mMayContainBuffer) { return; } size_t count = 0; - for (auto& cs : mComposerStates) { + for (auto& cs : mState.mComposerStates) { layer_state_t* s = &cs.state; if (!(s->what & layer_state_t::eBufferChanged)) { continue; @@ -1219,7 +1041,7 @@ void SurfaceComposerClient::Transaction::cacheBuffers() { std::optional<client_cache_t> uncacheBuffer; cacheId = BufferCache::getInstance().cache(s->bufferData->buffer, uncacheBuffer); if (uncacheBuffer) { - mUncacheBuffers.push_back(*uncacheBuffer); + mState.mUncacheBuffers.emplace_back(*uncacheBuffer); } } s->bufferData->flags |= BufferData::BufferDataChange::cachedBufferChanged; @@ -1288,8 +1110,7 @@ status_t SurfaceComposerClient::Transaction::apply(bool synchronous, bool oneWay /*callbackContext=*/nullptr); } - bool hasListenerCallbacks = !mListenerCallbacks.empty(); - std::vector<ListenerCallbacks> listenerCallbacks; + mState.mHasListenerCallbacks = !mListenerCallbacks.empty(); // For every listener with registered callbacks for (const auto& [listener, callbackInfo] : mListenerCallbacks) { auto& [callbackIds, surfaceControls] = callbackInfo; @@ -1298,7 +1119,8 @@ status_t SurfaceComposerClient::Transaction::apply(bool synchronous, bool oneWay } if (surfaceControls.empty()) { - listenerCallbacks.emplace_back(IInterface::asBinder(listener), std::move(callbackIds)); + mState.mListenerCallbacks.emplace_back(IInterface::asBinder(listener), + std::move(callbackIds)); } else { // If the listener has any SurfaceControls set on this Transaction update the surface // state @@ -1310,7 +1132,7 @@ status_t SurfaceComposerClient::Transaction::apply(bool synchronous, bool oneWay } std::vector<CallbackId> callbacks(callbackIds.begin(), callbackIds.end()); s->what |= layer_state_t::eHasListenerCallbacksChanged; - s->listeners.emplace_back(IInterface::asBinder(listener), callbacks); + s->listeners.emplace_back(IInterface::asBinder(listener), std::move(callbacks)); } } } @@ -1322,25 +1144,21 @@ status_t SurfaceComposerClient::Transaction::apply(bool synchronous, bool oneWay ALOGE("Transaction attempted to set synchronous and one way at the same time" " this is an invalid request. Synchronous will win for safety"); } else { - mFlags |= ISurfaceComposer::eOneWay; + mState.mFlags |= ISurfaceComposer::eOneWay; } } // If both ISurfaceComposer::eEarlyWakeupStart and ISurfaceComposer::eEarlyWakeupEnd are set // it is equivalent for none uint32_t wakeupFlags = ISurfaceComposer::eEarlyWakeupStart | ISurfaceComposer::eEarlyWakeupEnd; - if ((mFlags & wakeupFlags) == wakeupFlags) { - mFlags &= ~(wakeupFlags); + if ((mState.mFlags & wakeupFlags) == wakeupFlags) { + mState.mFlags &= ~(wakeupFlags); } - sp<IBinder> applyToken = mApplyToken ? mApplyToken : getDefaultApplyToken(); + if (!mState.mApplyToken) mState.mApplyToken = getDefaultApplyToken(); sp<ISurfaceComposer> sf(ComposerService::getComposerService()); - status_t binderStatus = - sf->setTransactionState(mFrameTimelineInfo, mComposerStates, mDisplayStates, mFlags, - applyToken, mInputWindowCommands, mDesiredPresentTime, - mIsAutoTimestamp, mUncacheBuffers, hasListenerCallbacks, - listenerCallbacks, mId, mMergedTransactionIds); - mId = generateId(); + status_t binderStatus = sf->setTransactionState(std::move(mState)); + mState.mId = generateId(); // Clear the current states and flags clear(); @@ -1349,8 +1167,8 @@ status_t SurfaceComposerClient::Transaction::apply(bool synchronous, bool oneWay syncCallback->wait(); } - if (mLogCallPoints) { - ALOG(LOG_DEBUG, LOG_SURFACE_CONTROL_REGISTRY, "Transaction %" PRIu64 " applied", mId); + if (mState.mLogCallPoints) { + ALOG(LOG_DEBUG, LOG_SURFACE_CONTROL_REGISTRY, "Transaction %" PRIu64 " applied", getId()); } mStatus = NO_ERROR; @@ -1385,7 +1203,7 @@ status_t SurfaceComposerClient::Transaction::sendSurfaceFlushJankDataTransaction } void SurfaceComposerClient::Transaction::enableDebugLogCallPoints() { - mLogCallPoints = true; + mState.mLogCallPoints = true; } // --------------------------------------------------------------------------- @@ -1443,34 +1261,19 @@ std::optional<gui::StalledTransactionInfo> SurfaceComposerClient::getStalledTran } void SurfaceComposerClient::Transaction::setAnimationTransaction() { - mFlags |= ISurfaceComposer::eAnimation; + mState.mFlags |= ISurfaceComposer::eAnimation; } void SurfaceComposerClient::Transaction::setEarlyWakeupStart() { - mFlags |= ISurfaceComposer::eEarlyWakeupStart; + mState.mFlags |= ISurfaceComposer::eEarlyWakeupStart; } void SurfaceComposerClient::Transaction::setEarlyWakeupEnd() { - mFlags |= ISurfaceComposer::eEarlyWakeupEnd; + mState.mFlags |= ISurfaceComposer::eEarlyWakeupEnd; } layer_state_t* SurfaceComposerClient::Transaction::getLayerState(const sp<SurfaceControl>& sc) { - auto handle = sc->getLayerStateHandle(); - if (auto it = std::find_if(mComposerStates.begin(), mComposerStates.end(), - [&handle](const auto& composerState) { - return composerState.state.surface == handle; - }); - it != mComposerStates.end()) { - return &it->state; - } - - // we don't have it, add an initialized layer_state to our list - ComposerState s; - s.state.surface = handle; - s.state.layerId = sc->getLayerId(); - mComposerStates.add(s); - - return &mComposerStates.editItemAt(mComposerStates.size() - 1).state; + return mState.getLayerState(sc); } void SurfaceComposerClient::Transaction::registerSurfaceControlForCallback( @@ -1846,8 +1649,8 @@ SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBuffe setReleaseBufferCallback(bufferData.get(), callback); } - if (mIsAutoTimestamp) { - mDesiredPresentTime = systemTime(); + if (mState.mIsAutoTimestamp) { + mState.mDesiredPresentTime = systemTime(); } s->what |= layer_state_t::eBufferChanged; s->bufferData = std::move(bufferData); @@ -1865,7 +1668,7 @@ SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBuffe const std::vector<SurfaceControlStats>&) {}, nullptr); - mMayContainBuffer = true; + mState.mMayContainBuffer = true; return *this; } @@ -2041,8 +1844,8 @@ SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setSideb SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setDesiredPresentTime( nsecs_t desiredPresentTime) { - mDesiredPresentTime = desiredPresentTime; - mIsAutoTimestamp = false; + mState.mDesiredPresentTime = desiredPresentTime; + mState.mIsAutoTimestamp = false; return *this; } @@ -2131,14 +1934,14 @@ SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setInput SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFocusedWindow( const FocusRequest& request) { - mInputWindowCommands.addFocusRequest(request); + mState.mInputWindowCommands.addFocusRequest(request); return *this; } SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::addWindowInfosReportedListener( sp<gui::IWindowInfosReportedListener> windowInfosReportedListener) { - mInputWindowCommands.addWindowInfosReportedListener(windowInfosReportedListener); + mState.mInputWindowCommands.addWindowInfosReportedListener(windowInfosReportedListener); return *this; } @@ -2302,7 +2105,7 @@ SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFixed SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFrameTimelineInfo( const FrameTimelineInfo& frameTimelineInfo) { - mergeFrameTimelineInfo(mFrameTimelineInfo, frameTimelineInfo); + mState.mergeFrameTimelineInfo(frameTimelineInfo); return *this; } @@ -2341,7 +2144,7 @@ SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setTrust SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setApplyToken( const sp<IBinder>& applyToken) { - mApplyToken = applyToken; + mState.mApplyToken = applyToken; return *this; } @@ -2469,17 +2272,7 @@ SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setConte // --------------------------------------------------------------------------- DisplayState& SurfaceComposerClient::Transaction::getDisplayState(const sp<IBinder>& token) { - if (auto it = std::find_if(mDisplayStates.begin(), mDisplayStates.end(), - [token](const auto& display) { return display.token == token; }); - it != mDisplayStates.end()) { - return *it; - } - - // If display state doesn't exist, add a new one. - DisplayState s; - s.token = token; - mDisplayStates.add(s); - return mDisplayStates.editItemAt(mDisplayStates.size() - 1); + return mState.getDisplayState(token); } status_t SurfaceComposerClient::Transaction::setDisplaySurface(const sp<IBinder>& token, @@ -2532,20 +2325,6 @@ void SurfaceComposerClient::Transaction::setDisplaySize(const sp<IBinder>& token s.what |= DisplayState::eDisplaySizeChanged; } -// copied from FrameTimelineInfo::merge() -void SurfaceComposerClient::Transaction::mergeFrameTimelineInfo(FrameTimelineInfo& t, - const FrameTimelineInfo& other) { - // When merging vsync Ids we take the oldest valid one - if (t.vsyncId != FrameTimelineInfo::INVALID_VSYNC_ID && - other.vsyncId != FrameTimelineInfo::INVALID_VSYNC_ID) { - if (other.vsyncId > t.vsyncId) { - t = other; - } - } else if (t.vsyncId == FrameTimelineInfo::INVALID_VSYNC_ID) { - t = other; - } -} - SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setTrustedPresentationCallback( const sp<SurfaceControl>& sc, TrustedPresentationCallback cb, diff --git a/libs/gui/include/gui/BLASTBufferQueue.h b/libs/gui/include/gui/BLASTBufferQueue.h index db1b9fb8eb..c69b0a79ad 100644 --- a/libs/gui/include/gui/BLASTBufferQueue.h +++ b/libs/gui/include/gui/BLASTBufferQueue.h @@ -284,7 +284,7 @@ private: std::function<void(SurfaceComposerClient::Transaction*)> mTransactionReadyCallback GUARDED_BY(mMutex); SurfaceComposerClient::Transaction* mSyncTransaction GUARDED_BY(mMutex); - std::vector<std::tuple<uint64_t /* framenumber */, SurfaceComposerClient::Transaction>> + std::vector<std::pair<uint64_t /* framenumber */, SurfaceComposerClient::Transaction>> mPendingTransactions GUARDED_BY(mMutex); std::queue<std::pair<uint64_t, FrameTimelineInfo>> mPendingFrameTimelines GUARDED_BY(mMutex); diff --git a/libs/gui/include/gui/ISurfaceComposer.h b/libs/gui/include/gui/ISurfaceComposer.h index 9a422fd808..de553aef72 100644 --- a/libs/gui/include/gui/ISurfaceComposer.h +++ b/libs/gui/include/gui/ISurfaceComposer.h @@ -65,6 +65,7 @@ struct DisplayState; struct InputWindowCommands; class HdrCapabilities; class Rect; +class TransactionState; using gui::FrameTimelineInfo; using gui::IDisplayEventConnection; @@ -105,13 +106,7 @@ public: }; /* open/close transactions. requires ACCESS_SURFACE_FLINGER permission */ - virtual status_t setTransactionState( - const FrameTimelineInfo& frameTimelineInfo, Vector<ComposerState>& state, - Vector<DisplayState>& displays, uint32_t flags, const sp<IBinder>& applyToken, - InputWindowCommands inputWindowCommands, int64_t desiredPresentTime, - bool isAutoTimestamp, const std::vector<client_cache_t>& uncacheBuffer, - bool hasListenerCallbacks, const std::vector<ListenerCallbacks>& listenerCallbacks, - uint64_t transactionId, const std::vector<uint64_t>& mergedTransactionIds) = 0; + virtual status_t setTransactionState(TransactionState&& state) = 0; }; // ---------------------------------------------------------------------------- diff --git a/libs/gui/include/gui/SurfaceComposerClient.h b/libs/gui/include/gui/SurfaceComposerClient.h index 4fda8deb9c..668bd6fbb8 100644 --- a/libs/gui/include/gui/SurfaceComposerClient.h +++ b/libs/gui/include/gui/SurfaceComposerClient.h @@ -52,6 +52,7 @@ #include <gui/ITransactionCompletedListener.h> #include <gui/LayerState.h> #include <gui/SurfaceControl.h> +#include <gui/TransactionState.h> #include <gui/WindowInfosListenerReporter.h> #include <math/vec3.h> @@ -442,61 +443,16 @@ public: virtual ~PresentationCallbackRAII(); }; - class Transaction : public Parcelable { + class Transaction { private: static sp<IBinder> sApplyToken; static std::mutex sApplyTokenMutex; void releaseBufferIfOverwriting(const layer_state_t& state); - static void mergeFrameTimelineInfo(FrameTimelineInfo& t, const FrameTimelineInfo& other); // Tracks registered callbacks sp<TransactionCompletedListener> mTransactionCompletedListener = nullptr; - // Prints debug logs when enabled. - bool mLogCallPoints = false; - protected: - Vector<ComposerState> mComposerStates; - Vector<DisplayState> mDisplayStates; - std::unordered_map<sp<ITransactionCompletedListener>, CallbackInfo, TCLHash> - mListenerCallbacks; - std::vector<client_cache_t> mUncacheBuffers; - - // We keep track of the last MAX_MERGE_HISTORY_LENGTH merged transaction ids. - // Ordered most recently merged to least recently merged. - static const size_t MAX_MERGE_HISTORY_LENGTH = 10u; - std::vector<uint64_t> mMergedTransactionIds; - - uint64_t mId; - uint32_t mFlags = 0; - - // Indicates that the Transaction may contain buffers that should be cached. The reason this - // is only a guess is that buffers can be removed before cache is called. This is only a - // hint that at some point a buffer was added to this transaction before apply was called. - bool mMayContainBuffer = false; - - // mDesiredPresentTime is the time in nanoseconds that the client would like the transaction - // to be presented. When it is not possible to present at exactly that time, it will be - // presented after the time has passed. - // - // If the client didn't pass a desired presentation time, mDesiredPresentTime will be - // populated to the time setBuffer was called, and mIsAutoTimestamp will be set to true. - // - // Desired present times that are more than 1 second in the future may be ignored. - // When a desired present time has already passed, the transaction will be presented as soon - // as possible. - // - // Transactions from the same process are presented in the same order that they are applied. - // The desired present time does not affect this ordering. - int64_t mDesiredPresentTime = 0; - bool mIsAutoTimestamp = true; - - // The vsync id provided by Choreographer.getVsyncId and the input event id - FrameTimelineInfo mFrameTimelineInfo; - - // If not null, transactions will be queued up using this token otherwise a common token - // per process will be used. - sp<IBinder> mApplyToken = nullptr; + TransactionState mState; - InputWindowCommands mInputWindowCommands; int mStatus = NO_ERROR; layer_state_t* getLayerState(const sp<SurfaceControl>& sc); @@ -506,23 +462,29 @@ public: void registerSurfaceControlForCallback(const sp<SurfaceControl>& sc); void setReleaseBufferCallback(BufferData*, ReleaseBufferCallback); + protected: + // Accessed in tests. + explicit Transaction(Transaction const& other) = default; + std::unordered_map<sp<ITransactionCompletedListener>, CallbackInfo, TCLHash> + mListenerCallbacks; + public: Transaction(); - virtual ~Transaction() = default; - Transaction(Transaction const& other); + Transaction(Transaction&& other); + Transaction& operator=(Transaction&& other) = default; // Factory method that creates a new Transaction instance from the parcel. static std::unique_ptr<Transaction> createFromParcel(const Parcel* parcel); - status_t writeToParcel(Parcel* parcel) const override; - status_t readFromParcel(const Parcel* parcel) override; + status_t writeToParcel(Parcel* parcel) const; + status_t readFromParcel(const Parcel* parcel); // Clears the contents of the transaction without applying it. void clear(); // Returns the current id of the transaction. // The id is updated every time the transaction is applied. - uint64_t getId(); + uint64_t getId() const; std::vector<uint64_t> getMergedTransactionIds(); diff --git a/libs/gui/include/gui/TransactionState.h b/libs/gui/include/gui/TransactionState.h index 4358227dae..79124f3ed7 100644 --- a/libs/gui/include/gui/TransactionState.h +++ b/libs/gui/include/gui/TransactionState.h @@ -26,7 +26,8 @@ namespace android { class TransactionState { public: explicit TransactionState() = default; - TransactionState(TransactionState const& other) = default; + TransactionState(TransactionState&& other) = default; + TransactionState& operator=(TransactionState&& other) = default; status_t writeToParcel(Parcel* parcel) const; status_t readFromParcel(const Parcel* parcel); layer_state_t* getLayerState(const sp<SurfaceControl>& sc); @@ -86,6 +87,9 @@ public: std::vector<ListenerCallbacks> mListenerCallbacks; private: + explicit TransactionState(TransactionState const& other) = default; + friend class TransactionApplicationTest; + friend class SurfaceComposerClient; // We keep track of the last MAX_MERGE_HISTORY_LENGTH merged transaction ids. // Ordered most recently merged to least recently merged. static constexpr size_t MAX_MERGE_HISTORY_LENGTH = 10u; diff --git a/libs/gui/tests/Surface_test.cpp b/libs/gui/tests/Surface_test.cpp index 45cde7f10c..4fee11c2cb 100644 --- a/libs/gui/tests/Surface_test.cpp +++ b/libs/gui/tests/Surface_test.cpp @@ -648,16 +648,7 @@ public: mSupportsPresent = supportsPresent; } - status_t setTransactionState( - const FrameTimelineInfo& /*frameTimelineInfo*/, Vector<ComposerState>& /*state*/, - Vector<DisplayState>& /*displays*/, uint32_t /*flags*/, - const sp<IBinder>& /*applyToken*/, InputWindowCommands /*inputWindowCommands*/, - int64_t /*desiredPresentTime*/, bool /*isAutoTimestamp*/, - const std::vector<client_cache_t>& /*cachedBuffer*/, bool /*hasListenerCallbacks*/, - const std::vector<ListenerCallbacks>& /*listenerCallbacks*/, uint64_t /*transactionId*/, - const std::vector<uint64_t>& /*mergedTransactionIds*/) override { - return NO_ERROR; - } + status_t setTransactionState(TransactionState&&) override { return NO_ERROR; } protected: IBinder* onAsBinder() override { return nullptr; } diff --git a/services/surfaceflinger/QueuedTransactionState.h b/services/surfaceflinger/QueuedTransactionState.h index 86683da26c..6a17a0d0cb 100644 --- a/services/surfaceflinger/QueuedTransactionState.h +++ b/services/surfaceflinger/QueuedTransactionState.h @@ -25,6 +25,7 @@ #include <common/FlagManager.h> #include <ftl/flags.h> #include <gui/LayerState.h> +#include <gui/TransactionState.h> #include <system/window.h> namespace android { @@ -50,33 +51,26 @@ public: struct QueuedTransactionState { QueuedTransactionState() = default; - QueuedTransactionState(const FrameTimelineInfo& frameTimelineInfo, - std::vector<ResolvedComposerState>& composerStates, - const Vector<DisplayState>& displayStates, uint32_t transactionFlags, - const sp<IBinder>& applyToken, - const InputWindowCommands& inputWindowCommands, - int64_t desiredPresentTime, bool isAutoTimestamp, - std::vector<uint64_t> uncacheBufferIds, int64_t postTime, - bool hasListenerCallbacks, - std::vector<ListenerCallbacks> listenerCallbacks, int originPid, - int originUid, uint64_t transactionId, - std::vector<uint64_t> mergedTransactionIds) - : frameTimelineInfo(frameTimelineInfo), - states(std::move(composerStates)), - displays(displayStates), - flags(transactionFlags), - applyToken(applyToken), - inputWindowCommands(inputWindowCommands), - desiredPresentTime(desiredPresentTime), - isAutoTimestamp(isAutoTimestamp), + QueuedTransactionState(TransactionState&& transactionState, + std::vector<ResolvedComposerState>&& composerStates, + std::vector<uint64_t>&& uncacheBufferIds, int64_t postTime, + int originPid, int originUid) + : frameTimelineInfo(std::move(transactionState.mFrameTimelineInfo)), + states(composerStates), + displays(std::move(transactionState.mDisplayStates)), + flags(transactionState.mFlags), + applyToken(transactionState.mApplyToken), + inputWindowCommands(std::move(transactionState.mInputWindowCommands)), + desiredPresentTime(transactionState.mDesiredPresentTime), + isAutoTimestamp(transactionState.mIsAutoTimestamp), uncacheBufferIds(std::move(uncacheBufferIds)), postTime(postTime), - hasListenerCallbacks(hasListenerCallbacks), - listenerCallbacks(listenerCallbacks), + hasListenerCallbacks(transactionState.mHasListenerCallbacks), + listenerCallbacks(std::move(transactionState.mListenerCallbacks)), originPid(originPid), originUid(originUid), - id(transactionId), - mergedTransactionIds(std::move(mergedTransactionIds)) {} + id(transactionState.getId()), + mergedTransactionIds(std::move(transactionState.mMergedTransactionIds)) {} // Invokes `void(const layer_state_t&)` visitor for matching layers. template <typename Visitor> @@ -135,7 +129,7 @@ struct QueuedTransactionState { FrameTimelineInfo frameTimelineInfo; std::vector<ResolvedComposerState> states; - Vector<DisplayState> displays; + std::vector<DisplayState> displays; uint32_t flags; sp<IBinder> applyToken; InputWindowCommands inputWindowCommands; diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp index 2fb3500798..03ee3006ae 100644 --- a/services/surfaceflinger/SurfaceFlinger.cpp +++ b/services/surfaceflinger/SurfaceFlinger.cpp @@ -4990,13 +4990,7 @@ bool SurfaceFlinger::shouldLatchUnsignaled(const layer_state_t& state, size_t nu return true; } -status_t SurfaceFlinger::setTransactionState( - const FrameTimelineInfo& frameTimelineInfo, Vector<ComposerState>& states, - Vector<DisplayState>& displays, uint32_t flags, const sp<IBinder>& applyToken, - InputWindowCommands inputWindowCommands, int64_t desiredPresentTime, bool isAutoTimestamp, - const std::vector<client_cache_t>& uncacheBuffers, bool hasListenerCallbacks, - const std::vector<ListenerCallbacks>& listenerCallbacks, uint64_t transactionId, - const std::vector<uint64_t>& mergedTransactionIds) { +status_t SurfaceFlinger::setTransactionState(TransactionState&& transactionState) { SFTRACE_CALL(); IPCThreadState* ipc = IPCThreadState::self(); @@ -5004,7 +4998,7 @@ status_t SurfaceFlinger::setTransactionState( const int originUid = ipc->getCallingUid(); uint32_t permissions = LayerStatePermissions::getTransactionPermissions(originPid, originUid); ftl::Flags<adpf::Workload> queuedWorkload; - for (auto& composerState : states) { + for (auto& composerState : transactionState.mComposerStates) { composerState.state.sanitize(permissions); if (composerState.state.what & layer_state_t::COMPOSITION_EFFECTS) { queuedWorkload |= adpf::Workload::EFFECTS; @@ -5014,27 +5008,27 @@ status_t SurfaceFlinger::setTransactionState( } } - for (DisplayState& display : displays) { + for (DisplayState& display : transactionState.mDisplayStates) { display.sanitize(permissions); } - if (!inputWindowCommands.empty() && + if (!transactionState.mInputWindowCommands.empty() && (permissions & layer_state_t::Permission::ACCESS_SURFACE_FLINGER) == 0) { ALOGE("Only privileged callers are allowed to send input commands."); - inputWindowCommands.clear(); + transactionState.mInputWindowCommands.clear(); } - if (flags & (eEarlyWakeupStart | eEarlyWakeupEnd)) { + if (transactionState.mFlags & (eEarlyWakeupStart | eEarlyWakeupEnd)) { const bool hasPermission = (permissions & layer_state_t::Permission::ACCESS_SURFACE_FLINGER) || callingThreadHasPermission(sWakeupSurfaceFlinger); if (!hasPermission) { ALOGE("Caller needs permission android.permission.WAKEUP_SURFACE_FLINGER to use " "eEarlyWakeup[Start|End] flags"); - flags &= ~(eEarlyWakeupStart | eEarlyWakeupEnd); + transactionState.mFlags &= ~(eEarlyWakeupStart | eEarlyWakeupEnd); } } - if (flags & eEarlyWakeupStart) { + if (transactionState.mFlags & eEarlyWakeupStart) { queuedWorkload |= adpf::Workload::WAKEUP; } mPowerAdvisor->setQueuedWorkload(queuedWorkload); @@ -5042,8 +5036,8 @@ status_t SurfaceFlinger::setTransactionState( const int64_t postTime = systemTime(); std::vector<uint64_t> uncacheBufferIds; - uncacheBufferIds.reserve(uncacheBuffers.size()); - for (const auto& uncacheBuffer : uncacheBuffers) { + uncacheBufferIds.reserve(transactionState.mUncacheBuffers.size()); + for (const auto& uncacheBuffer : transactionState.mUncacheBuffers) { sp<GraphicBuffer> buffer = ClientCache::getInstance().erase(uncacheBuffer); if (buffer != nullptr) { uncacheBufferIds.push_back(buffer->getId()); @@ -5051,8 +5045,8 @@ status_t SurfaceFlinger::setTransactionState( } std::vector<ResolvedComposerState> resolvedStates; - resolvedStates.reserve(states.size()); - for (auto& state : states) { + resolvedStates.reserve(transactionState.mComposerStates.size()); + for (auto& state : transactionState.mComposerStates) { resolvedStates.emplace_back(std::move(state)); auto& resolvedState = resolvedStates.back(); resolvedState.layerId = LayerHandle::getLayerId(resolvedState.state.surface); @@ -5063,7 +5057,7 @@ status_t SurfaceFlinger::setTransactionState( (layer) ? layer->getDebugName() : std::to_string(resolvedState.state.layerId); resolvedState.externalTexture = getExternalTextureFromBufferData(*resolvedState.state.bufferData, - layerName.c_str(), transactionId); + layerName.c_str(), transactionState.getId()); if (resolvedState.externalTexture) { resolvedState.state.bufferData->buffer = resolvedState.externalTexture->getBuffer(); if (FlagManager::getInstance().monitor_buffer_fences()) { @@ -5091,22 +5085,12 @@ status_t SurfaceFlinger::setTransactionState( } } - QueuedTransactionState state{frameTimelineInfo, - resolvedStates, - displays, - flags, - applyToken, - std::move(inputWindowCommands), - desiredPresentTime, - isAutoTimestamp, + QueuedTransactionState state{std::move(transactionState), + std::move(resolvedStates), std::move(uncacheBufferIds), postTime, - hasListenerCallbacks, - listenerCallbacks, originPid, - originUid, - transactionId, - mergedTransactionIds}; + originUid}; state.workloadHint = queuedWorkload; if (mTransactionTracing) { @@ -5120,6 +5104,9 @@ status_t SurfaceFlinger::setTransactionState( }(state.flags); const auto frameHint = state.isFrameActive() ? FrameHint::kActive : FrameHint::kNone; + // Copy fields of |state| needed after it is moved into queueTransaction + VsyncId vsyncId{state.frameTimelineInfo.vsyncId}; + auto applyToken = state.applyToken; { // Transactions are added via a lockless queue and does not need to be added from the main // thread. @@ -5129,7 +5116,7 @@ status_t SurfaceFlinger::setTransactionState( for (const auto& [displayId, data] : mNotifyExpectedPresentMap) { if (data.hintStatus.load() == NotifyExpectedPresentHintStatus::ScheduleOnTx) { - scheduleNotifyExpectedPresentHint(displayId, VsyncId{frameTimelineInfo.vsyncId}); + scheduleNotifyExpectedPresentHint(displayId, vsyncId); } } setTransactionFlags(eTransactionFlushNeeded, schedule, applyToken, frameHint); @@ -5138,7 +5125,7 @@ status_t SurfaceFlinger::setTransactionState( bool SurfaceFlinger::applyTransactionState( const FrameTimelineInfo& frameTimelineInfo, std::vector<ResolvedComposerState>& states, - Vector<DisplayState>& displays, uint32_t flags, + std::span<DisplayState> displays, uint32_t flags, const InputWindowCommands& inputWindowCommands, const int64_t desiredPresentTime, bool isAutoTimestamp, const std::vector<uint64_t>& uncacheBufferIds, const int64_t postTime, bool hasListenerCallbacks, const std::vector<ListenerCallbacks>& listenerCallbacks, @@ -5628,7 +5615,8 @@ void SurfaceFlinger::initializeDisplays() { auto layerStack = ui::DEFAULT_LAYER_STACK.id; for (const auto& [id, display] : FTL_FAKE_GUARD(mStateLock, mPhysicalDisplays)) { - state.displays.push(DisplayState(display.token(), ui::LayerStack::fromValue(layerStack++))); + state.displays.emplace_back( + DisplayState(display.token(), ui::LayerStack::fromValue(layerStack++))); } std::vector<QueuedTransactionState> transactions; diff --git a/services/surfaceflinger/SurfaceFlinger.h b/services/surfaceflinger/SurfaceFlinger.h index 1a09269545..fe8998403e 100644 --- a/services/surfaceflinger/SurfaceFlinger.h +++ b/services/surfaceflinger/SurfaceFlinger.h @@ -545,13 +545,7 @@ private: } sp<IBinder> getPhysicalDisplayToken(PhysicalDisplayId displayId) const; - status_t setTransactionState( - const FrameTimelineInfo& frameTimelineInfo, Vector<ComposerState>& state, - Vector<DisplayState>& displays, uint32_t flags, const sp<IBinder>& applyToken, - InputWindowCommands inputWindowCommands, int64_t desiredPresentTime, - bool isAutoTimestamp, const std::vector<client_cache_t>& uncacheBuffers, - bool hasListenerCallbacks, const std::vector<ListenerCallbacks>& listenerCallbacks, - uint64_t transactionId, const std::vector<uint64_t>& mergedTransactionIds) override; + status_t setTransactionState(TransactionState&&) override; void bootFinished(); status_t getSupportedFrameTimestamps(std::vector<FrameEvent>* outSupported) const; sp<IDisplayEventConnection> createDisplayEventConnection( @@ -798,7 +792,7 @@ private: */ bool applyTransactionState(const FrameTimelineInfo& info, std::vector<ResolvedComposerState>& state, - Vector<DisplayState>& displays, uint32_t flags, + std::span<DisplayState> displays, uint32_t flags, const InputWindowCommands& inputWindowCommands, const int64_t desiredPresentTime, bool isAutoTimestamp, const std::vector<uint64_t>& uncacheBufferIds, diff --git a/services/surfaceflinger/Tracing/TransactionProtoParser.cpp b/services/surfaceflinger/Tracing/TransactionProtoParser.cpp index 3297c16113..6bbc04cf6f 100644 --- a/services/surfaceflinger/Tracing/TransactionProtoParser.cpp +++ b/services/surfaceflinger/Tracing/TransactionProtoParser.cpp @@ -321,7 +321,7 @@ QueuedTransactionState TransactionProtoParser::fromProto( int32_t displayCount = proto.display_changes_size(); t.displays.reserve(static_cast<size_t>(displayCount)); for (int i = 0; i < displayCount; i++) { - t.displays.add(fromProto(proto.display_changes(i))); + t.displays.emplace_back(fromProto(proto.display_changes(i))); } return t; } diff --git a/services/surfaceflinger/tests/DereferenceSurfaceControl_test.cpp b/services/surfaceflinger/tests/DereferenceSurfaceControl_test.cpp index 46b98f9193..192602d7d2 100644 --- a/services/surfaceflinger/tests/DereferenceSurfaceControl_test.cpp +++ b/services/surfaceflinger/tests/DereferenceSurfaceControl_test.cpp @@ -52,6 +52,8 @@ protected: }; TEST_F(DereferenceSurfaceControlTest, LayerNotInTransaction) { + // Last strong pointer is removed, the layer is destroyed and is removed + // from compostion. fgLayer = nullptr; { SCOPED_TRACE("after setting null"); @@ -61,7 +63,9 @@ TEST_F(DereferenceSurfaceControlTest, LayerNotInTransaction) { } TEST_F(DereferenceSurfaceControlTest, LayerInTransaction) { - auto transaction = Transaction().show(fgLayer); + Transaction transaction; + transaction.show(fgLayer); + // |transaction| retains a strong pointer, so layer is retained. fgLayer = nullptr; { SCOPED_TRACE("after setting null"); diff --git a/services/surfaceflinger/tests/IPC_test.cpp b/services/surfaceflinger/tests/IPC_test.cpp index 18bd3b92d2..94cb878311 100644 --- a/services/surfaceflinger/tests/IPC_test.cpp +++ b/services/surfaceflinger/tests/IPC_test.cpp @@ -42,14 +42,22 @@ using CallbackInfo = SurfaceComposerClient::CallbackInfo; using TCLHash = SurfaceComposerClient::TCLHash; using android::hardware::graphics::common::V1_1::BufferUsage; -class TransactionHelper : public Transaction { +class TransactionHelper : public Transaction, public Parcelable { public: + TransactionHelper() : Transaction() {} size_t getNumListeners() { return mListenerCallbacks.size(); } std::unordered_map<sp<ITransactionCompletedListener>, CallbackInfo, TCLHash> getListenerCallbacks() { return mListenerCallbacks; } + status_t writeToParcel(Parcel* parcel) const override { + return Transaction::writeToParcel(parcel); + } + + status_t readFromParcel(const Parcel* parcel) override { + return Transaction::readFromParcel(parcel); + } }; class IPCTestUtils { diff --git a/services/surfaceflinger/tests/unittests/SurfaceFlinger_NotifyExpectedPresentTest.cpp b/services/surfaceflinger/tests/unittests/SurfaceFlinger_NotifyExpectedPresentTest.cpp index 6cc6322bfc..9c143fdd41 100644 --- a/services/surfaceflinger/tests/unittests/SurfaceFlinger_NotifyExpectedPresentTest.cpp +++ b/services/surfaceflinger/tests/unittests/SurfaceFlinger_NotifyExpectedPresentTest.cpp @@ -45,28 +45,15 @@ protected: void setTransactionState() { ASSERT_TRUE(mFlinger.getTransactionQueue().isEmpty()); TransactionInfo transaction; - mFlinger.setTransactionState(FrameTimelineInfo{}, transaction.states, transaction.displays, - transaction.flags, transaction.applyToken, - transaction.inputWindowCommands, - TimePoint::now().ns() + s2ns(1), transaction.isAutoTimestamp, - transaction.unCachedBuffers, - /*HasListenerCallbacks=*/false, transaction.callbacks, - transaction.id, transaction.mergedTransactionIds); + mFlinger.setTransactionState(std::move(transaction)); } - struct TransactionInfo { - Vector<ComposerState> states; - Vector<DisplayState> displays; - uint32_t flags = 0; - sp<IBinder> applyToken = IInterface::asBinder(TransactionCompletedListener::getIInstance()); - InputWindowCommands inputWindowCommands; - int64_t desiredPresentTime = 0; - bool isAutoTimestamp = false; - FrameTimelineInfo frameTimelineInfo{}; - std::vector<client_cache_t> unCachedBuffers; - uint64_t id = static_cast<uint64_t>(-1); - std::vector<uint64_t> mergedTransactionIds; - std::vector<ListenerCallbacks> callbacks; + struct TransactionInfo : public TransactionState { + TransactionInfo() { + mApplyToken = IInterface::asBinder(TransactionCompletedListener::getIInstance()); + mIsAutoTimestamp = false; + mId = static_cast<uint64_t>(-1); + } }; struct Compositor final : ICompositor { @@ -383,4 +370,4 @@ TEST_F(NotifyExpectedPresentTest, notifyExpectedPresentRenderRateChanged) { } } } -} // namespace android
\ No newline at end of file +} // namespace android diff --git a/services/surfaceflinger/tests/unittests/TestableSurfaceFlinger.h b/services/surfaceflinger/tests/unittests/TestableSurfaceFlinger.h index c5973db109..13c32bdf08 100644 --- a/services/surfaceflinger/tests/unittests/TestableSurfaceFlinger.h +++ b/services/surfaceflinger/tests/unittests/TestableSurfaceFlinger.h @@ -519,18 +519,8 @@ public: return mFlinger->mTransactionHandler.mPendingTransactionCount.load(); } - auto setTransactionState( - const FrameTimelineInfo& frameTimelineInfo, Vector<ComposerState>& states, - Vector<DisplayState>& displays, uint32_t flags, const sp<IBinder>& applyToken, - const InputWindowCommands& inputWindowCommands, int64_t desiredPresentTime, - bool isAutoTimestamp, const std::vector<client_cache_t>& uncacheBuffers, - bool hasListenerCallbacks, std::vector<ListenerCallbacks>& listenerCallbacks, - uint64_t transactionId, const std::vector<uint64_t>& mergedTransactionIds) { - return mFlinger->setTransactionState(frameTimelineInfo, states, displays, flags, applyToken, - inputWindowCommands, desiredPresentTime, - isAutoTimestamp, uncacheBuffers, hasListenerCallbacks, - listenerCallbacks, transactionId, - mergedTransactionIds); + auto setTransactionState(TransactionState&& state) { + return mFlinger->setTransactionState(std::move(state)); } auto setTransactionStateInternal(QueuedTransactionState& transaction) { diff --git a/services/surfaceflinger/tests/unittests/TransactionApplicationTest.cpp b/services/surfaceflinger/tests/unittests/TransactionApplicationTest.cpp index 69dfcc4a8f..1395fb6af3 100644 --- a/services/surfaceflinger/tests/unittests/TransactionApplicationTest.cpp +++ b/services/surfaceflinger/tests/unittests/TransactionApplicationTest.cpp @@ -17,6 +17,8 @@ #undef LOG_TAG #define LOG_TAG "TransactionApplicationTest" +#include <cstdint> + #include <binder/Binder.h> #include <common/test/FlagUtils.h> #include <compositionengine/Display.h> @@ -69,38 +71,32 @@ public: TestableSurfaceFlinger mFlinger; renderengine::mock::RenderEngine* mRenderEngine = new renderengine::mock::RenderEngine(); - struct TransactionInfo { - Vector<ComposerState> states; - Vector<DisplayState> displays; - uint32_t flags = 0; - sp<IBinder> applyToken = IInterface::asBinder(TransactionCompletedListener::getIInstance()); - InputWindowCommands inputWindowCommands; - int64_t desiredPresentTime = 0; - bool isAutoTimestamp = true; - FrameTimelineInfo frameTimelineInfo; - std::vector<client_cache_t> uncacheBuffers; - uint64_t id = static_cast<uint64_t>(-1); - std::vector<uint64_t> mergedTransactionIds; - static_assert(0xffffffffffffffff == static_cast<uint64_t>(-1)); + struct TransactionInfo : public TransactionState { + TransactionInfo() { + mApplyToken = IInterface::asBinder(TransactionCompletedListener::getIInstance()); + mId = static_cast<uint64_t>(-1); + } }; - void checkEqual(TransactionInfo info, QueuedTransactionState state) { - EXPECT_EQ(0u, info.states.size()); + void checkEqual(const TransactionInfo& info, const QueuedTransactionState& state) { + EXPECT_EQ(0u, info.mComposerStates.size()); EXPECT_EQ(0u, state.states.size()); - EXPECT_EQ(0u, info.displays.size()); + EXPECT_EQ(0u, info.mDisplayStates.size()); EXPECT_EQ(0u, state.displays.size()); - EXPECT_EQ(info.flags, state.flags); - EXPECT_EQ(info.desiredPresentTime, state.desiredPresentTime); + EXPECT_EQ(info.mFlags, state.flags); + EXPECT_EQ(info.mDesiredPresentTime, state.desiredPresentTime); } void setupSingle(TransactionInfo& transaction, uint32_t flags, int64_t desiredPresentTime, bool isAutoTimestamp, const FrameTimelineInfo& frameTimelineInfo) { mTransactionNumber++; - transaction.flags |= flags; - transaction.desiredPresentTime = desiredPresentTime; - transaction.isAutoTimestamp = isAutoTimestamp; - transaction.frameTimelineInfo = frameTimelineInfo; + transaction.mFlags |= flags; + transaction.mDesiredPresentTime = desiredPresentTime; + transaction.mIsAutoTimestamp = isAutoTimestamp; + transaction.mFrameTimelineInfo = frameTimelineInfo; + transaction.mHasListenerCallbacks = mHasListenerCallbacks; + transaction.mListenerCallbacks = mCallbacks; } void NotPlacedOnTransactionQueue(uint32_t flags) { @@ -111,12 +107,7 @@ public: /*desiredPresentTime*/ systemTime(), /*isAutoTimestamp*/ true, FrameTimelineInfo{}); nsecs_t applicationTime = systemTime(); - mFlinger.setTransactionState(transaction.frameTimelineInfo, transaction.states, - transaction.displays, transaction.flags, - transaction.applyToken, transaction.inputWindowCommands, - transaction.desiredPresentTime, transaction.isAutoTimestamp, - transaction.uncacheBuffers, mHasListenerCallbacks, mCallbacks, - transaction.id, transaction.mergedTransactionIds); + mFlinger.setTransactionState(std::move(transaction)); // If transaction is synchronous, SF applyTransactionState should time out (5s) wating for // SF to commit the transaction. If this is animation, it should not time out waiting. @@ -138,12 +129,7 @@ public: setupSingle(transaction, flags, /*desiredPresentTime*/ time + s2ns(1), false, FrameTimelineInfo{}); nsecs_t applicationSentTime = systemTime(); - mFlinger.setTransactionState(transaction.frameTimelineInfo, transaction.states, - transaction.displays, transaction.flags, - transaction.applyToken, transaction.inputWindowCommands, - transaction.desiredPresentTime, transaction.isAutoTimestamp, - transaction.uncacheBuffers, mHasListenerCallbacks, mCallbacks, - transaction.id, transaction.mergedTransactionIds); + mFlinger.setTransactionState(std::move(transaction)); nsecs_t returnedTime = systemTime(); EXPECT_LE(returnedTime, applicationSentTime + TRANSACTION_TIMEOUT); @@ -169,12 +155,7 @@ public: /*isAutoTimestamp*/ true, FrameTimelineInfo{}); nsecs_t applicationSentTime = systemTime(); - mFlinger.setTransactionState(transactionA.frameTimelineInfo, transactionA.states, - transactionA.displays, transactionA.flags, - transactionA.applyToken, transactionA.inputWindowCommands, - transactionA.desiredPresentTime, transactionA.isAutoTimestamp, - transactionA.uncacheBuffers, mHasListenerCallbacks, mCallbacks, - transactionA.id, transactionA.mergedTransactionIds); + mFlinger.setTransactionState(std::move(transactionA)); // This thread should not have been blocked by the above transaction // (5s is the timeout period that applyTransactionState waits for SF to @@ -184,12 +165,7 @@ public: mFlinger.flushTransactionQueues(); applicationSentTime = systemTime(); - mFlinger.setTransactionState(transactionB.frameTimelineInfo, transactionB.states, - transactionB.displays, transactionB.flags, - transactionB.applyToken, transactionB.inputWindowCommands, - transactionB.desiredPresentTime, transactionB.isAutoTimestamp, - transactionB.uncacheBuffers, mHasListenerCallbacks, mCallbacks, - transactionB.id, transactionB.mergedTransactionIds); + mFlinger.setTransactionState(std::move(transactionB)); // this thread should have been blocked by the above transaction // if this is an animation, this thread should be blocked for 5s @@ -222,12 +198,7 @@ TEST_F(TransactionApplicationTest, AddToPendingQueue) { TransactionInfo transactionA; // transaction to go on pending queue setupSingle(transactionA, /*flags*/ 0, /*desiredPresentTime*/ s2ns(1), false, FrameTimelineInfo{}); - mFlinger.setTransactionState(transactionA.frameTimelineInfo, transactionA.states, - transactionA.displays, transactionA.flags, transactionA.applyToken, - transactionA.inputWindowCommands, transactionA.desiredPresentTime, - transactionA.isAutoTimestamp, transactionA.uncacheBuffers, - mHasListenerCallbacks, mCallbacks, transactionA.id, - transactionA.mergedTransactionIds); + mFlinger.setTransactionState(std::move(transactionA)); auto& transactionQueue = mFlinger.getTransactionQueue(); ASSERT_FALSE(transactionQueue.isEmpty()); @@ -243,12 +214,7 @@ TEST_F(TransactionApplicationTest, Flush_RemovesFromQueue) { TransactionInfo transactionA; // transaction to go on pending queue setupSingle(transactionA, /*flags*/ 0, /*desiredPresentTime*/ s2ns(1), false, FrameTimelineInfo{}); - mFlinger.setTransactionState(transactionA.frameTimelineInfo, transactionA.states, - transactionA.displays, transactionA.flags, transactionA.applyToken, - transactionA.inputWindowCommands, transactionA.desiredPresentTime, - transactionA.isAutoTimestamp, transactionA.uncacheBuffers, - mHasListenerCallbacks, mCallbacks, transactionA.id, - transactionA.mergedTransactionIds); + mFlinger.setTransactionState(std::move(transactionA)); auto& transactionQueue = mFlinger.getTransactionQueue(); ASSERT_FALSE(transactionQueue.isEmpty()); @@ -257,12 +223,10 @@ TEST_F(TransactionApplicationTest, Flush_RemovesFromQueue) { // transaction here (sending a null applyToken to fake it as from a // different process) to re-query and reset the cached expected present time TransactionInfo empty; - empty.applyToken = sp<IBinder>(); - mFlinger.setTransactionState(empty.frameTimelineInfo, empty.states, empty.displays, empty.flags, - empty.applyToken, empty.inputWindowCommands, - empty.desiredPresentTime, empty.isAutoTimestamp, - empty.uncacheBuffers, mHasListenerCallbacks, mCallbacks, empty.id, - empty.mergedTransactionIds); + empty.mApplyToken = sp<IBinder>(); + empty.mHasListenerCallbacks = mHasListenerCallbacks; + empty.mListenerCallbacks = mCallbacks; + mFlinger.setTransactionState(std::move(empty)); // flush transaction queue should flush as desiredPresentTime has // passed @@ -406,9 +370,9 @@ public: const auto kFrameTimelineInfo = FrameTimelineInfo{}; setupSingle(transaction, kFlags, kDesiredPresentTime, kIsAutoTimestamp, kFrameTimelineInfo); - transaction.applyToken = applyToken; + transaction.mApplyToken = applyToken; for (const auto& state : states) { - transaction.states.push_back(state); + transaction.mComposerStates.push_back(state); } return transaction; @@ -419,8 +383,8 @@ public: EXPECT_TRUE(mFlinger.getTransactionQueue().isEmpty()); EXPECT_EQ(0u, mFlinger.getPendingTransactionQueue().size()); std::unordered_set<uint32_t> createdLayers; - for (auto transaction : transactions) { - for (auto& state : transaction.states) { + for (auto& transaction : transactions) { + for (auto& state : transaction.mComposerStates) { auto layerId = static_cast<uint32_t>(state.state.layerId); if (createdLayers.find(layerId) == createdLayers.end()) { mFlinger.addLayer(layerId); @@ -434,8 +398,8 @@ public: for (auto transaction : transactions) { std::vector<ResolvedComposerState> resolvedStates; - resolvedStates.reserve(transaction.states.size()); - for (auto& state : transaction.states) { + resolvedStates.reserve(transaction.mComposerStates.size()); + for (auto& state : transaction.mComposerStates) { ResolvedComposerState resolvedState; resolvedState.state = std::move(state.state); resolvedState.externalTexture = @@ -446,15 +410,9 @@ public: resolvedStates.emplace_back(resolvedState); } - QueuedTransactionState transactionState(transaction.frameTimelineInfo, resolvedStates, - transaction.displays, transaction.flags, - transaction.applyToken, - transaction.inputWindowCommands, - transaction.desiredPresentTime, - transaction.isAutoTimestamp, {}, systemTime(), - mHasListenerCallbacks, mCallbacks, getpid(), - static_cast<int>(getuid()), transaction.id, - transaction.mergedTransactionIds); + QueuedTransactionState transactionState(std::move(transaction), + std::move(resolvedStates), {}, systemTime(), + getpid(), static_cast<int>(getuid())); mFlinger.setTransactionStateInternal(transactionState); } mFlinger.flushTransactionQueues(); diff --git a/services/surfaceflinger/tests/unittests/TransactionProtoParserTest.cpp b/services/surfaceflinger/tests/unittests/TransactionProtoParserTest.cpp index d3eec5c6f3..b36ad213c8 100644 --- a/services/surfaceflinger/tests/unittests/TransactionProtoParserTest.cpp +++ b/services/surfaceflinger/tests/unittests/TransactionProtoParserTest.cpp @@ -66,7 +66,7 @@ TEST(TransactionProtoParserTest, parse) { display.token = nullptr; } display.width = 85; - t1.displays.add(display); + t1.displays.push_back(display); } class TestMapper : public TransactionProtoParser::FlingerDataMapper { |