Skip to content

Commit 7cea62b

Browse files
committed
Replace SortedVector and maps with Vector in Transactions
Align the data in client so we can remove some duplicate parcelling and clean up the interface to sf. micro benchmark results (atest libgui_benchmarks) shows slight improvements when merging and applying transactions. 5-10% Note: we would ideally switch to std::vector but this would involve more changes. Once we clean up the interfaces to sf, changing the container would be easier. Flag: EXEMPT refactor Bug: 385156191 Test: presubmit Change-Id: I2f14236e5f571af192d371b7b58f0a0fa0c62f49
1 parent e715810 commit 7cea62b

2 files changed

Lines changed: 55 additions & 54 deletions

File tree

libs/gui/SurfaceComposerClient.cpp

Lines changed: 53 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <semaphore.h>
2020
#include <stdint.h>
2121
#include <sys/types.h>
22+
#include <algorithm>
2223

2324
#include <android/gui/BnWindowInfosReportedListener.h>
2425
#include <android/gui/DisplayState.h>
@@ -844,7 +845,7 @@ SurfaceComposerClient::Transaction::Transaction(const Transaction& other)
844845

845846
void SurfaceComposerClient::Transaction::sanitize(int pid, int uid) {
846847
uint32_t permissions = LayerStatePermissions::getTransactionPermissions(pid, uid);
847-
for (auto & [handle, composerState] : mComposerStates) {
848+
for (auto& composerState : mComposerStates) {
848849
composerState.state.sanitize(permissions);
849850
}
850851
if (!mInputWindowCommands.empty() &&
@@ -879,7 +880,7 @@ status_t SurfaceComposerClient::Transaction::readFromParcel(const Parcel* parcel
879880
if (count > parcel->dataSize()) {
880881
return BAD_VALUE;
881882
}
882-
SortedVector<DisplayState> displayStates;
883+
Vector<DisplayState> displayStates;
883884
displayStates.setCapacity(count);
884885
for (size_t i = 0; i < count; i++) {
885886
DisplayState displayState;
@@ -922,17 +923,14 @@ status_t SurfaceComposerClient::Transaction::readFromParcel(const Parcel* parcel
922923
if (count > parcel->dataSize()) {
923924
return BAD_VALUE;
924925
}
925-
std::unordered_map<sp<IBinder>, ComposerState, IBinderHash> composerStates;
926-
composerStates.reserve(count);
926+
Vector<ComposerState> composerStates;
927+
composerStates.setCapacity(count);
927928
for (size_t i = 0; i < count; i++) {
928-
sp<IBinder> surfaceControlHandle;
929-
SAFE_PARCEL(parcel->readStrongBinder, &surfaceControlHandle);
930-
931929
ComposerState composerState;
932930
if (composerState.read(*parcel) == BAD_VALUE) {
933931
return BAD_VALUE;
934932
}
935-
composerStates[surfaceControlHandle] = composerState;
933+
composerStates.add(composerState);
936934
}
937935

938936
InputWindowCommands inputWindowCommands;
@@ -965,9 +963,9 @@ status_t SurfaceComposerClient::Transaction::readFromParcel(const Parcel* parcel
965963
mDesiredPresentTime = desiredPresentTime;
966964
mIsAutoTimestamp = isAutoTimestamp;
967965
mFrameTimelineInfo = frameTimelineInfo;
968-
mDisplayStates = displayStates;
966+
mDisplayStates = std::move(displayStates);
969967
mListenerCallbacks = listenerCallbacks;
970-
mComposerStates = composerStates;
968+
mComposerStates = std::move(composerStates);
971969
mInputWindowCommands = inputWindowCommands;
972970
mApplyToken = applyToken;
973971
mUncacheBuffers = std::move(uncacheBuffers);
@@ -1015,8 +1013,7 @@ status_t SurfaceComposerClient::Transaction::writeToParcel(Parcel* parcel) const
10151013
}
10161014

10171015
parcel->writeUint32(static_cast<uint32_t>(mComposerStates.size()));
1018-
for (auto const& [handle, composerState] : mComposerStates) {
1019-
SAFE_PARCEL(parcel->writeStrongBinder, handle);
1016+
for (auto const& composerState : mComposerStates) {
10201017
composerState.write(*parcel);
10211018
}
10221019

@@ -1073,23 +1070,31 @@ SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::merge(Tr
10731070
}
10741071
mMergedTransactionIds.insert(mMergedTransactionIds.begin(), other.mId);
10751072

1076-
for (auto const& [handle, composerState] : other.mComposerStates) {
1077-
if (mComposerStates.count(handle) == 0) {
1078-
mComposerStates[handle] = composerState;
1079-
} else {
1080-
if (composerState.state.what & layer_state_t::eBufferChanged) {
1081-
releaseBufferIfOverwriting(mComposerStates[handle].state);
1073+
for (auto const& otherState : other.mComposerStates) {
1074+
if (auto it = std::find_if(mComposerStates.begin(), mComposerStates.end(),
1075+
[&otherState](const auto& composerState) {
1076+
return composerState.state.surface ==
1077+
otherState.state.surface;
1078+
});
1079+
it != mComposerStates.end()) {
1080+
if (otherState.state.what & layer_state_t::eBufferChanged) {
1081+
releaseBufferIfOverwriting(it->state);
10821082
}
1083-
mComposerStates[handle].state.merge(composerState.state);
1083+
it->state.merge(otherState.state);
1084+
} else {
1085+
mComposerStates.add(otherState);
10841086
}
10851087
}
10861088

10871089
for (auto const& state : other.mDisplayStates) {
1088-
ssize_t index = mDisplayStates.indexOf(state);
1089-
if (index < 0) {
1090-
mDisplayStates.add(state);
1090+
if (auto it = std::find_if(mDisplayStates.begin(), mDisplayStates.end(),
1091+
[&state](const auto& displayState) {
1092+
return displayState.token == state.token;
1093+
});
1094+
it != mDisplayStates.end()) {
1095+
it->merge(state);
10911096
} else {
1092-
mDisplayStates.editItemAt(static_cast<size_t>(index)).merge(state);
1097+
mDisplayStates.add(state);
10931098
}
10941099
}
10951100

@@ -1186,8 +1191,8 @@ void SurfaceComposerClient::Transaction::cacheBuffers() {
11861191
}
11871192

11881193
size_t count = 0;
1189-
for (auto& [handle, cs] : mComposerStates) {
1190-
layer_state_t* s = &(mComposerStates[handle].state);
1194+
for (auto& cs : mComposerStates) {
1195+
layer_state_t* s = &cs.state;
11911196
if (!(s->what & layer_state_t::eBufferChanged)) {
11921197
continue;
11931198
} else if (s->bufferData &&
@@ -1312,15 +1317,6 @@ status_t SurfaceComposerClient::Transaction::apply(bool synchronous, bool oneWay
13121317

13131318
cacheBuffers();
13141319

1315-
Vector<ComposerState> composerStates;
1316-
Vector<DisplayState> displayStates;
1317-
1318-
for (auto const& kv : mComposerStates) {
1319-
composerStates.add(kv.second);
1320-
}
1321-
1322-
displayStates = std::move(mDisplayStates);
1323-
13241320
if (oneWay) {
13251321
if (synchronous) {
13261322
ALOGE("Transaction attempted to set synchronous and one way at the same time"
@@ -1340,7 +1336,7 @@ status_t SurfaceComposerClient::Transaction::apply(bool synchronous, bool oneWay
13401336

13411337
sp<ISurfaceComposer> sf(ComposerService::getComposerService());
13421338
status_t binderStatus =
1343-
sf->setTransactionState(mFrameTimelineInfo, composerStates, displayStates, mFlags,
1339+
sf->setTransactionState(mFrameTimelineInfo, mComposerStates, mDisplayStates, mFlags,
13441340
applyToken, mInputWindowCommands, mDesiredPresentTime,
13451341
mIsAutoTimestamp, mUncacheBuffers, hasListenerCallbacks,
13461342
listenerCallbacks, mId, mMergedTransactionIds);
@@ -1456,18 +1452,21 @@ void SurfaceComposerClient::Transaction::setEarlyWakeupEnd() {
14561452

14571453
layer_state_t* SurfaceComposerClient::Transaction::getLayerState(const sp<SurfaceControl>& sc) {
14581454
auto handle = sc->getLayerStateHandle();
1459-
1460-
if (mComposerStates.count(handle) == 0) {
1461-
// we don't have it, add an initialized layer_state to our list
1462-
ComposerState s;
1463-
1464-
s.state.surface = handle;
1465-
s.state.layerId = sc->getLayerId();
1466-
1467-
mComposerStates[handle] = s;
1455+
if (auto it = std::find_if(mComposerStates.begin(), mComposerStates.end(),
1456+
[&handle](const auto& composerState) {
1457+
return composerState.state.surface == handle;
1458+
});
1459+
it != mComposerStates.end()) {
1460+
return &it->state;
14681461
}
14691462

1470-
return &(mComposerStates[handle].state);
1463+
// we don't have it, add an initialized layer_state to our list
1464+
ComposerState s;
1465+
s.state.surface = handle;
1466+
s.state.layerId = sc->getLayerId();
1467+
mComposerStates.add(s);
1468+
1469+
return &mComposerStates.editItemAt(mComposerStates.size() - 1).state;
14711470
}
14721471

14731472
void SurfaceComposerClient::Transaction::registerSurfaceControlForCallback(
@@ -2492,15 +2491,17 @@ SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setConte
24922491
// ---------------------------------------------------------------------------
24932492

24942493
DisplayState& SurfaceComposerClient::Transaction::getDisplayState(const sp<IBinder>& token) {
2494+
if (auto it = std::find_if(mDisplayStates.begin(), mDisplayStates.end(),
2495+
[token](const auto& display) { return display.token == token; });
2496+
it != mDisplayStates.end()) {
2497+
return *it;
2498+
}
2499+
2500+
// If display state doesn't exist, add a new one.
24952501
DisplayState s;
24962502
s.token = token;
2497-
ssize_t index = mDisplayStates.indexOf(s);
2498-
if (index < 0) {
2499-
// we don't have it, add an initialized layer_state to our list
2500-
s.what = 0;
2501-
index = mDisplayStates.add(s);
2502-
}
2503-
return mDisplayStates.editItemAt(static_cast<size_t>(index));
2503+
mDisplayStates.add(s);
2504+
return mDisplayStates.editItemAt(mDisplayStates.size() - 1);
25042505
}
25052506

25062507
status_t SurfaceComposerClient::Transaction::setDisplaySurface(const sp<IBinder>& token,

libs/gui/include/gui/SurfaceComposerClient.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,8 +455,8 @@ class SurfaceComposerClient : public RefBase
455455
bool mLogCallPoints = false;
456456

457457
protected:
458-
std::unordered_map<sp<IBinder>, ComposerState, IBinderHash> mComposerStates;
459-
SortedVector<DisplayState> mDisplayStates;
458+
Vector<ComposerState> mComposerStates;
459+
Vector<DisplayState> mDisplayStates;
460460
std::unordered_map<sp<ITransactionCompletedListener>, CallbackInfo, TCLHash>
461461
mListenerCallbacks;
462462
std::vector<client_cache_t> mUncacheBuffers;

0 commit comments

Comments
 (0)