Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit cf9686c3 authored by Arthur Ishiguro's avatar Arthur Ishiguro Committed by Automerger Merge Worker
Browse files

Convert KeyedVector to std::ordered_map am: ad46c786

Change-Id: I97093270eb3f8a080e4c420088a50bc2dc10d2c5
parents 4ee59a28 ad46c786
Loading
Loading
Loading
Loading
+38 −33
Original line number Original line Diff line number Diff line
@@ -91,11 +91,11 @@ void SensorService::SensorEventConnection::dump(String8& result) {
    result.appendFormat("\t %s | WakeLockRefCount %d | uid %d | cache size %d | "
    result.appendFormat("\t %s | WakeLockRefCount %d | uid %d | cache size %d | "
            "max cache size %d\n", mPackageName.string(), mWakeLockRefCount, mUid, mCacheSize,
            "max cache size %d\n", mPackageName.string(), mWakeLockRefCount, mUid, mCacheSize,
            mMaxCacheSize);
            mMaxCacheSize);
    for (size_t i = 0; i < mSensorInfo.size(); ++i) {
    for (auto& it : mSensorInfo) {
        const FlushInfo& flushInfo = mSensorInfo.valueAt(i);
        const FlushInfo& flushInfo = it.second;
        result.appendFormat("\t %s 0x%08x | status: %s | pending flush events %d \n",
        result.appendFormat("\t %s 0x%08x | status: %s | pending flush events %d \n",
                            mService->getSensorName(mSensorInfo.keyAt(i)).string(),
                            mService->getSensorName(it.first).string(),
                            mSensorInfo.keyAt(i),
                            it.first,
                            flushInfo.mFirstFlushPending ? "First flush pending" :
                            flushInfo.mFirstFlushPending ? "First flush pending" :
                                                           "active",
                                                           "active",
                            flushInfo.mPendingFlushEventsToSend);
                            flushInfo.mPendingFlushEventsToSend);
@@ -135,12 +135,12 @@ void SensorService::SensorEventConnection::dump(util::ProtoOutputStream* proto)
    proto->write(UID, int32_t(mUid));
    proto->write(UID, int32_t(mUid));
    proto->write(CACHE_SIZE, int32_t(mCacheSize));
    proto->write(CACHE_SIZE, int32_t(mCacheSize));
    proto->write(MAX_CACHE_SIZE, int32_t(mMaxCacheSize));
    proto->write(MAX_CACHE_SIZE, int32_t(mMaxCacheSize));
    for (size_t i = 0; i < mSensorInfo.size(); ++i) {
    for (auto& it : mSensorInfo) {
        const FlushInfo& flushInfo = mSensorInfo.valueAt(i);
        const FlushInfo& flushInfo = it.second;
        const uint64_t token = proto->start(FLUSH_INFOS);
        const uint64_t token = proto->start(FLUSH_INFOS);
        proto->write(FlushInfoProto::SENSOR_NAME,
        proto->write(FlushInfoProto::SENSOR_NAME,
                std::string(mService->getSensorName(mSensorInfo.keyAt(i))));
                std::string(mService->getSensorName(it.first)));
        proto->write(FlushInfoProto::SENSOR_HANDLE, mSensorInfo.keyAt(i));
        proto->write(FlushInfoProto::SENSOR_HANDLE, it.first);
        proto->write(FlushInfoProto::FIRST_FLUSH_PENDING, flushInfo.mFirstFlushPending);
        proto->write(FlushInfoProto::FIRST_FLUSH_PENDING, flushInfo.mFirstFlushPending);
        proto->write(FlushInfoProto::PENDING_FLUSH_EVENTS_TO_SEND,
        proto->write(FlushInfoProto::PENDING_FLUSH_EVENTS_TO_SEND,
                flushInfo.mPendingFlushEventsToSend);
                flushInfo.mPendingFlushEventsToSend);
@@ -162,24 +162,33 @@ bool SensorService::SensorEventConnection::addSensor(int32_t handle) {
    sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(handle);
    sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(handle);
    if (si == nullptr ||
    if (si == nullptr ||
        !canAccessSensor(si->getSensor(), "Tried adding", mOpPackageName) ||
        !canAccessSensor(si->getSensor(), "Tried adding", mOpPackageName) ||
        mSensorInfo.indexOfKey(handle) >= 0) {
        mSensorInfo.count(handle) > 0) {
        return false;
        return false;
    }
    }
    mSensorInfo.add(handle, FlushInfo());
    mSensorInfo[handle] = FlushInfo();
    return true;
    return true;
}
}


bool SensorService::SensorEventConnection::removeSensor(int32_t handle) {
bool SensorService::SensorEventConnection::removeSensor(int32_t handle) {
    Mutex::Autolock _l(mConnectionLock);
    Mutex::Autolock _l(mConnectionLock);
    if (mSensorInfo.removeItem(handle) >= 0) {
    if (mSensorInfo.erase(handle) >= 0) {
        return true;
        return true;
    }
    }
    return false;
    return false;
}
}


std::vector<int32_t> SensorService::SensorEventConnection::getActiveSensorHandles() const {
    Mutex::Autolock _l(mConnectionLock);
    std::vector<int32_t> list;
    for (auto& it : mSensorInfo) {
        list.push_back(it.first);
    }
    return list;
}

bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const {
bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const {
    Mutex::Autolock _l(mConnectionLock);
    Mutex::Autolock _l(mConnectionLock);
    return mSensorInfo.indexOfKey(handle) >= 0;
    return mSensorInfo.count(handle) > 0;
}
}


bool SensorService::SensorEventConnection::hasAnySensor() const {
bool SensorService::SensorEventConnection::hasAnySensor() const {
@@ -189,8 +198,8 @@ bool SensorService::SensorEventConnection::hasAnySensor() const {


bool SensorService::SensorEventConnection::hasOneShotSensors() const {
bool SensorService::SensorEventConnection::hasOneShotSensors() const {
    Mutex::Autolock _l(mConnectionLock);
    Mutex::Autolock _l(mConnectionLock);
    for (size_t i = 0; i < mSensorInfo.size(); ++i) {
    for (auto &it : mSensorInfo) {
        const int handle = mSensorInfo.keyAt(i);
        const int handle = it.first;
        sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(handle);
        sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(handle);
        if (si != nullptr && si->getSensor().getReportingMode() == AREPORTING_MODE_ONE_SHOT) {
        if (si != nullptr && si->getSensor().getReportingMode() == AREPORTING_MODE_ONE_SHOT) {
            return true;
            return true;
@@ -206,9 +215,8 @@ String8 SensorService::SensorEventConnection::getPackageName() const {
void SensorService::SensorEventConnection::setFirstFlushPending(int32_t handle,
void SensorService::SensorEventConnection::setFirstFlushPending(int32_t handle,
                                bool value) {
                                bool value) {
    Mutex::Autolock _l(mConnectionLock);
    Mutex::Autolock _l(mConnectionLock);
    ssize_t index = mSensorInfo.indexOfKey(handle);
    if (mSensorInfo.count(handle) > 0) {
    if (index >= 0) {
        FlushInfo& flushInfo = mSensorInfo[handle];
        FlushInfo& flushInfo = mSensorInfo.editValueAt(index);
        flushInfo.mFirstFlushPending = value;
        flushInfo.mFirstFlushPending = value;
    }
    }
}
}
@@ -233,8 +241,8 @@ void SensorService::SensorEventConnection::updateLooperRegistrationLocked(
    int looper_flags = 0;
    int looper_flags = 0;
    if (mCacheSize > 0) looper_flags |= ALOOPER_EVENT_OUTPUT;
    if (mCacheSize > 0) looper_flags |= ALOOPER_EVENT_OUTPUT;
    if (mDataInjectionMode) looper_flags |= ALOOPER_EVENT_INPUT;
    if (mDataInjectionMode) looper_flags |= ALOOPER_EVENT_INPUT;
    for (size_t i = 0; i < mSensorInfo.size(); ++i) {
    for (auto& it : mSensorInfo) {
        const int handle = mSensorInfo.keyAt(i);
        const int handle = it.first;
        sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(handle);
        sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(handle);
        if (si != nullptr && si->getSensor().isWakeUpSensor()) {
        if (si != nullptr && si->getSensor().isWakeUpSensor()) {
            looper_flags |= ALOOPER_EVENT_INPUT;
            looper_flags |= ALOOPER_EVENT_INPUT;
@@ -266,9 +274,8 @@ void SensorService::SensorEventConnection::updateLooperRegistrationLocked(


void SensorService::SensorEventConnection::incrementPendingFlushCount(int32_t handle) {
void SensorService::SensorEventConnection::incrementPendingFlushCount(int32_t handle) {
    Mutex::Autolock _l(mConnectionLock);
    Mutex::Autolock _l(mConnectionLock);
    ssize_t index = mSensorInfo.indexOfKey(handle);
    if (mSensorInfo.count(handle) > 0) {
    if (index >= 0) {
        FlushInfo& flushInfo = mSensorInfo[handle];
        FlushInfo& flushInfo = mSensorInfo.editValueAt(index);
        flushInfo.mPendingFlushEventsToSend++;
        flushInfo.mPendingFlushEventsToSend++;
    }
    }
}
}
@@ -296,15 +303,14 @@ status_t SensorService::SensorEventConnection::sendEvents(
                sensor_handle = buffer[i].meta_data.sensor;
                sensor_handle = buffer[i].meta_data.sensor;
            }
            }


            ssize_t index = mSensorInfo.indexOfKey(sensor_handle);
            // Check if this connection has registered for this sensor. If not continue to the
            // Check if this connection has registered for this sensor. If not continue to the
            // next sensor_event.
            // next sensor_event.
            if (index < 0) {
            if (mSensorInfo.count(sensor_handle) == 0) {
                ++i;
                ++i;
                continue;
                continue;
            }
            }


            FlushInfo& flushInfo = mSensorInfo.editValueAt(index);
            FlushInfo& flushInfo = mSensorInfo[sensor_handle];
            // Check if there is a pending flush_complete event for this sensor on this connection.
            // Check if there is a pending flush_complete event for this sensor on this connection.
            if (buffer[i].type == SENSOR_TYPE_META_DATA && flushInfo.mFirstFlushPending == true &&
            if (buffer[i].type == SENSOR_TYPE_META_DATA && flushInfo.mFirstFlushPending == true &&
                    mapFlushEventsToConnections[i] == this) {
                    mapFlushEventsToConnections[i] == this) {
@@ -522,14 +528,14 @@ void SensorService::SensorEventConnection::sendPendingFlushEventsLocked() {
    flushCompleteEvent.type = SENSOR_TYPE_META_DATA;
    flushCompleteEvent.type = SENSOR_TYPE_META_DATA;
    // Loop through all the sensors for this connection and check if there are any pending
    // Loop through all the sensors for this connection and check if there are any pending
    // flush complete events to be sent.
    // flush complete events to be sent.
    for (size_t i = 0; i < mSensorInfo.size(); ++i) {
    for (auto& it : mSensorInfo) {
        const int handle = mSensorInfo.keyAt(i);
        const int handle = it.first;
        sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(handle);
        sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(handle);
        if (si == nullptr) {
        if (si == nullptr) {
            continue;
            continue;
        }
        }


        FlushInfo& flushInfo = mSensorInfo.editValueAt(i);
        FlushInfo& flushInfo = it.second;
        while (flushInfo.mPendingFlushEventsToSend > 0) {
        while (flushInfo.mPendingFlushEventsToSend > 0) {
            flushCompleteEvent.meta_data.sensor = handle;
            flushCompleteEvent.meta_data.sensor = handle;
            bool wakeUpSensor = si->getSensor().isWakeUpSensor();
            bool wakeUpSensor = si->getSensor().isWakeUpSensor();
@@ -615,14 +621,13 @@ void SensorService::SensorEventConnection::countFlushCompleteEventsLocked(
    // separately before the next batch of events.
    // separately before the next batch of events.
    for (int j = 0; j < numEventsDropped; ++j) {
    for (int j = 0; j < numEventsDropped; ++j) {
        if (scratch[j].type == SENSOR_TYPE_META_DATA) {
        if (scratch[j].type == SENSOR_TYPE_META_DATA) {
            ssize_t index = mSensorInfo.indexOfKey(scratch[j].meta_data.sensor);
            if (mSensorInfo.count(scratch[j].meta_data.sensor) == 0) {
            if (index < 0) {
                ALOGW("%s: sensor 0x%x is not found in connection",
                ALOGW("%s: sensor 0x%x is not found in connection",
                      __func__, scratch[j].meta_data.sensor);
                      __func__, scratch[j].meta_data.sensor);
                continue;
                continue;
            }
            }


            FlushInfo& flushInfo = mSensorInfo.editValueAt(index);
            FlushInfo& flushInfo = mSensorInfo[scratch[j].meta_data.sensor];
            flushInfo.mPendingFlushEventsToSend++;
            flushInfo.mPendingFlushEventsToSend++;
            ALOGD_IF(DEBUG_CONNECTIONS, "increment pendingFlushCount %d",
            ALOGD_IF(DEBUG_CONNECTIONS, "increment pendingFlushCount %d",
                     flushInfo.mPendingFlushEventsToSend);
                     flushInfo.mPendingFlushEventsToSend);
@@ -764,8 +769,8 @@ int SensorService::SensorEventConnection::handleEvent(int fd, int events, void*
int SensorService::SensorEventConnection::computeMaxCacheSizeLocked() const {
int SensorService::SensorEventConnection::computeMaxCacheSizeLocked() const {
    size_t fifoWakeUpSensors = 0;
    size_t fifoWakeUpSensors = 0;
    size_t fifoNonWakeUpSensors = 0;
    size_t fifoNonWakeUpSensors = 0;
    for (size_t i = 0; i < mSensorInfo.size(); ++i) {
    for (auto& it : mSensorInfo) {
        sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(mSensorInfo.keyAt(i));
        sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(it.first);
        if (si == nullptr) {
        if (si == nullptr) {
            continue;
            continue;
        }
        }
+3 −3
Original line number Original line Diff line number Diff line
@@ -23,7 +23,6 @@


#include <utils/Vector.h>
#include <utils/Vector.h>
#include <utils/SortedVector.h>
#include <utils/SortedVector.h>
#include <utils/KeyedVector.h>
#include <utils/threads.h>
#include <utils/threads.h>
#include <utils/AndroidThreads.h>
#include <utils/AndroidThreads.h>
#include <utils/RefBase.h>
#include <utils/RefBase.h>
@@ -60,6 +59,7 @@ public:
    bool hasOneShotSensors() const;
    bool hasOneShotSensors() const;
    bool addSensor(int32_t handle);
    bool addSensor(int32_t handle);
    bool removeSensor(int32_t handle);
    bool removeSensor(int32_t handle);
    std::vector<int32_t> getActiveSensorHandles() const;
    void setFirstFlushPending(int32_t handle, bool value);
    void setFirstFlushPending(int32_t handle, bool value);
    void dump(String8& result);
    void dump(String8& result);
    void dump(util::ProtoOutputStream* proto) const;
    void dump(util::ProtoOutputStream* proto) const;
@@ -169,8 +169,8 @@ private:


        FlushInfo() : mPendingFlushEventsToSend(0), mFirstFlushPending(false) {}
        FlushInfo() : mPendingFlushEventsToSend(0), mFirstFlushPending(false) {}
    };
    };
    // protected by SensorService::mLock. Key for this vector is the sensor handle.
    // protected by SensorService::mLock. Key for this map is the sensor handle.
    KeyedVector<int, FlushInfo> mSensorInfo;
    std::unordered_map<int32_t, FlushInfo> mSensorInfo;


    sensors_event_t *mEventCache;
    sensors_event_t *mEventCache;
    int mCacheSize, mMaxCacheSize;
    int mCacheSize, mMaxCacheSize;
+1 −2
Original line number Original line Diff line number Diff line
@@ -1740,8 +1740,7 @@ status_t SensorService::flushSensor(const sp<SensorEventConnection>& connection,
    status_t err(NO_ERROR);
    status_t err(NO_ERROR);
    Mutex::Autolock _l(mLock);
    Mutex::Autolock _l(mLock);
    // Loop through all sensors for this connection and call flush on each of them.
    // Loop through all sensors for this connection and call flush on each of them.
    for (size_t i = 0; i < connection->mSensorInfo.size(); ++i) {
    for (int handle : connection->getActiveSensorHandles()) {
        const int handle = connection->mSensorInfo.keyAt(i);
        sp<SensorInterface> sensor = getSensorInterfaceFromHandle(handle);
        sp<SensorInterface> sensor = getSensorInterfaceFromHandle(handle);
        if (sensor == nullptr) {
        if (sensor == nullptr) {
            continue;
            continue;