1 /*
2 * Copyright 2024 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "SessionLayerMap.h"
18 #include <android/binder_libbinder.h>
19
20 namespace android::adpf {
21
notifySessionsDied(std::vector<int32_t> & sessionIds)22 void SessionLayerMap::notifySessionsDied(std::vector<int32_t>& sessionIds) {
23 for (int id : sessionIds) {
24 auto&& iter = mSessions.find(id);
25 if (iter != mSessions.end()) {
26 mSessions.erase(iter);
27 }
28 }
29 }
30
notifyLayersDied(std::vector<int32_t> & layers)31 void SessionLayerMap::notifyLayersDied(std::vector<int32_t>& layers) {
32 for (auto&& layer : layers) {
33 auto&& iter = mLayers.find(layer);
34 if (iter != mLayers.end()) {
35 mLayers.erase(iter);
36 }
37 }
38 }
39
bindSessionIDToLayers(int sessionId,const std::vector<int32_t> & layerIds)40 bool SessionLayerMap::bindSessionIDToLayers(int sessionId, const std::vector<int32_t>& layerIds) {
41 // If there is no association, just drop from map
42 if (layerIds.empty()) {
43 mSessions.erase(sessionId);
44 return false;
45 }
46
47 // Ensure session exists
48 if (!mSessions.contains(sessionId)) {
49 mSessions.emplace(sessionId, MappedType(sessionId, mLayers));
50 }
51
52 MappedType& session = mSessions.at(sessionId);
53 std::set<int32_t> newLinks;
54
55 // For each incoming link
56 for (auto&& layerId : layerIds) {
57 auto&& iter = mLayers.find(layerId);
58
59 // If it's not in the map, add it
60 if (iter == mLayers.end()) {
61 mLayers.emplace(layerId, MappedType(layerId, mSessions));
62 }
63
64 // Make a ref to it in the session's new association map
65 newLinks.insert(layerId);
66 }
67
68 session.swapLinks(std::move(newLinks));
69 return true;
70 }
71
getAssociatedSessions(int32_t layerId,std::vector<int32_t> & sessionIdsOut)72 void SessionLayerMap::getAssociatedSessions(int32_t layerId, std::vector<int32_t>& sessionIdsOut) {
73 sessionIdsOut.clear();
74 auto&& iter = mLayers.find(layerId);
75
76 if (iter == mLayers.end()) {
77 return;
78 }
79
80 // Dump the internal association set into this vector
81 sessionIdsOut.insert(sessionIdsOut.begin(), iter->second.mLinks.begin(),
82 iter->second.mLinks.end());
83 }
84
getCurrentlyRelevantLayers(std::unordered_set<int32_t> & currentlyRelevantLayers)85 void SessionLayerMap::getCurrentlyRelevantLayers(
86 std::unordered_set<int32_t>& currentlyRelevantLayers) {
87 currentlyRelevantLayers.clear();
88 for (auto&& layer : mLayers) {
89 currentlyRelevantLayers.insert(layer.first);
90 }
91 }
92
93 } // namespace android::adpf