surfaceflinger: use std::vector for mDisplayEventConnections
Replace SortedVector by std::vector for mDisplayEventConnections.
There are usually a couple dozens of connections. They are created
and destroyed as app processes come and go, and are iterated
frequently.
Semantically, SortedVector is std::set rather than std::vector. But
registerDisplayEventConnection can be made private, and the sole
caller never adds the same connection twice, we choose to replace it
by std::vector. However, we still keep a check in
registerDisplayEventConnection to make sure it is indeed the case
temporarily.
Bug: 115738279
Test: boots
Change-Id: Ie2df1f346cceda272f58f7751b31534171d593ca
diff --git a/services/surfaceflinger/Scheduler/EventThread.cpp b/services/surfaceflinger/Scheduler/EventThread.cpp
index a75235c..343162c 100644
--- a/services/surfaceflinger/Scheduler/EventThread.cpp
+++ b/services/surfaceflinger/Scheduler/EventThread.cpp
@@ -111,14 +111,28 @@
status_t EventThread::registerDisplayEventConnection(
const sp<EventThread::Connection>& connection) {
std::lock_guard<std::mutex> lock(mMutex);
- mDisplayEventConnections.add(connection);
+
+ // this should never happen
+ auto it = std::find(mDisplayEventConnections.cbegin(),
+ mDisplayEventConnections.cend(), connection);
+ if (it != mDisplayEventConnections.cend()) {
+ ALOGW("DisplayEventConnection %p already exists", connection.get());
+ mCondition.notify_all();
+ return ALREADY_EXISTS;
+ }
+
+ mDisplayEventConnections.push_back(connection);
mCondition.notify_all();
return NO_ERROR;
}
void EventThread::removeDisplayEventConnectionLocked(
const wp<EventThread::Connection>& connection) {
- mDisplayEventConnections.remove(connection);
+ auto it = std::find(mDisplayEventConnections.cbegin(),
+ mDisplayEventConnections.cend(), connection);
+ if (it != mDisplayEventConnections.cend()) {
+ mDisplayEventConnections.erase(it);
+ }
}
void EventThread::setVsyncRate(uint32_t count, const sp<EventThread::Connection>& connection) {
@@ -253,9 +267,9 @@
}
// find out connections waiting for events
- size_t count = mDisplayEventConnections.size();
- for (size_t i = 0; i < count;) {
- sp<Connection> connection(mDisplayEventConnections[i].promote());
+ auto it = mDisplayEventConnections.begin();
+ while (it != mDisplayEventConnections.end()) {
+ sp<Connection> connection(it->promote());
if (connection != nullptr) {
bool added = false;
if (connection->count >= 0) {
@@ -285,12 +299,11 @@
// messages.
signalConnections.add(connection);
}
- ++i;
+ ++it;
} else {
// we couldn't promote this reference, the connection has
// died, so clean-up!
- mDisplayEventConnections.removeAt(i);
- --count;
+ it = mDisplayEventConnections.erase(it);
}
}
@@ -379,8 +392,8 @@
result.appendFormat(" soft-vsync: %s\n", mUseSoftwareVSync ? "enabled" : "disabled");
result.appendFormat(" numListeners=%zu,\n events-delivered: %u\n",
mDisplayEventConnections.size(), mVSyncEvent[0].vsync.count);
- for (size_t i = 0; i < mDisplayEventConnections.size(); i++) {
- sp<Connection> connection = mDisplayEventConnections.itemAt(i).promote();
+ for (const wp<Connection>& weak : mDisplayEventConnections) {
+ sp<Connection> connection = weak.promote();
result.appendFormat(" %p: count=%d\n", connection.get(),
connection != nullptr ? connection->count : 0);
}