blob: c6441ae80257671812dca61380e8e6c43b11cdaf [file]
/*
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "SessionLayerMap.h"
#include <algorithm>
namespace android::adpf {
void SessionLayerMap::unbindSessionFromLayer(const int32_t layerId, const int32_t sessionId) {
const auto layerIt = mLayerToSessions.find(layerId);
if (layerIt == mLayerToSessions.end()) {
return;
}
if (std::erase(layerIt->second, sessionId) > 0) {
if (std::find(mUpdatedLayers.begin(), mUpdatedLayers.end(), layerId) ==
mUpdatedLayers.end()) {
mUpdatedLayers.push_back(layerId);
}
}
if (layerIt->second.empty()) {
mLayerToSessions.erase(layerIt);
}
}
void SessionLayerMap::unbindLayerFromSession(const int32_t layerId, const int32_t sessionId) {
const auto sessionIt = mSessionToLayers.find(sessionId);
if (sessionIt == mSessionToLayers.end()) {
return;
}
std::erase(sessionIt->second, layerId);
if (sessionIt->second.empty()) {
mSessionToLayers.erase(sessionIt);
}
}
void SessionLayerMap::notifySessionsDied(const std::vector<int32_t>& sessions) {
for (const int32_t sessionId : sessions) {
if (const auto it = mSessionToLayers.find(sessionId); it != mSessionToLayers.end()) {
for (const int32_t layerId : it->second) {
unbindSessionFromLayer(layerId, sessionId);
}
mSessionToLayers.erase(it);
}
}
}
void SessionLayerMap::notifyLayersDied(const std::vector<int32_t>& layers) {
for (const int32_t layerId : layers) {
if (const auto it = mLayerToSessions.find(layerId); it != mLayerToSessions.end()) {
for (const int32_t sessionId : it->second) {
unbindLayerFromSession(layerId, sessionId);
}
mLayerToSessions.erase(it);
std::erase(mUpdatedLayers, layerId);
}
}
}
bool SessionLayerMap::bindSessionIDToLayers(const int32_t sessionId,
std::vector<int32_t> layerIds) {
if (layerIds.empty()) {
notifySessionsDied({sessionId});
return false;
}
std::sort(layerIds.begin(), layerIds.end());
layerIds.erase(std::unique(layerIds.begin(), layerIds.end()), layerIds.end());
auto& currentLayers = mSessionToLayers[sessionId];
if (currentLayers == layerIds) {
return true;
}
for (const int32_t oldLayer : currentLayers) {
if (std::find(layerIds.begin(), layerIds.end(), oldLayer) == layerIds.end()) {
unbindSessionFromLayer(oldLayer, sessionId);
}
}
for (const int32_t newLayer : layerIds) {
if (std::find(currentLayers.begin(), currentLayers.end(), newLayer) ==
currentLayers.end()) {
auto& sessions = mLayerToSessions[newLayer];
if (std::find(sessions.begin(), sessions.end(), sessionId) == sessions.end()) {
sessions.push_back(sessionId);
}
if (std::find(mUpdatedLayers.begin(), mUpdatedLayers.end(), newLayer) ==
mUpdatedLayers.end()) {
mUpdatedLayers.push_back(newLayer);
}
}
}
currentLayers = std::move(layerIds);
return true;
}
std::vector<int32_t> SessionLayerMap::getAssociatedSessions(const int32_t layerId) const {
if (const auto it = mLayerToSessions.find(layerId); it != mLayerToSessions.end()) {
return it->second;
}
return {};
}
bool SessionLayerMap::isLayerRelevant(const int32_t layerId) const {
return mLayerToSessions.contains(layerId);
}
std::vector<int32_t> SessionLayerMap::extractUpdatedLayers() {
std::vector<int32_t> updated = std::move(mUpdatedLayers);
mUpdatedLayers.clear();
return updated;
}
bool SessionLayerMap::isIdle() const {
return mLayerToSessions.empty() && mUpdatedLayers.empty() && mSessionToLayers.empty();
}
} // namespace android::adpf