• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "PowerAdvisor/SessionManager.h"
18 #include <android/binder_libbinder.h>
19 #include <android/binder_status.h>
20 #include <binder/IPCThreadState.h>
21 #include "FrontEnd/LayerHandle.h"
22 #include "Layer.h"
23 #include "SurfaceFlinger.h"
24 
25 namespace android::adpf {
26 
SessionManager(uid_t uid)27 SessionManager::SessionManager(uid_t uid) : mUid(uid) {}
28 
associateSessionToLayers(int32_t sessionId,int32_t ownerUid,const std::vector<::ndk::SpAIBinder> & layerTokens)29 ndk::ScopedAStatus SessionManager::associateSessionToLayers(
30         int32_t sessionId, int32_t ownerUid, const std::vector<::ndk::SpAIBinder>& layerTokens) {
31     std::scoped_lock lock{mSessionManagerMutex};
32 
33     std::vector<int32_t> layerIds;
34 
35     for (auto&& token : layerTokens) {
36         auto platformToken = AIBinder_toPlatformBinder(token.get());
37 
38         // Get the layer id for it
39         int32_t layerId =
40                 static_cast<int32_t>(surfaceflinger::LayerHandle::getLayerId(platformToken));
41         auto&& iter = mTrackedLayerData.find(layerId);
42 
43         // Ensure it is being tracked
44         if (iter == mTrackedLayerData.end()) {
45             mTrackedLayerData.emplace(layerId, LayerData{.layerId = layerId});
46         }
47         layerIds.push_back(layerId);
48     }
49 
50     // Register the session then track it
51     if (mMap.bindSessionIDToLayers(sessionId, layerIds) &&
52         !mTrackedSessionData.contains(sessionId)) {
53         mTrackedSessionData.emplace(sessionId,
54                                     SessionData{.sessionId = sessionId, .uid = ownerUid});
55     }
56     return ndk::ScopedAStatus::ok();
57 }
58 
trackedSessionsDied(const std::vector<int32_t> & sessionIds)59 ndk::ScopedAStatus SessionManager::trackedSessionsDied(const std::vector<int32_t>& sessionIds) {
60     std::scoped_lock lock{mSessionManagerMutex};
61     for (int sessionId : sessionIds) {
62         mDeadSessions.push_back(sessionId);
63         mTrackedSessionData.erase(sessionId);
64     }
65 
66     return ndk::ScopedAStatus::ok();
67 }
68 
updateTrackingState(const std::vector<std::pair<uint32_t,std::string>> & handles)69 void SessionManager::updateTrackingState(
70         const std::vector<std::pair<uint32_t, std::string>>& handles) {
71     std::scoped_lock lock{mSessionManagerMutex};
72     std::vector<int32_t> deadLayers;
73     for (auto&& handle : handles) {
74         int32_t handleId = static_cast<int32_t>(handle.first);
75         auto it = mTrackedLayerData.find(handleId);
76         if (it != mTrackedLayerData.end()) {
77             // Track any dead layers to remove from the mapping
78             mTrackedLayerData.erase(it);
79             deadLayers.push_back(it->first);
80         }
81     }
82     mMap.notifyLayersDied(deadLayers);
83     mMap.notifySessionsDied(mDeadSessions);
84 
85     mDeadSessions.clear();
86     mMap.getCurrentlyRelevantLayers(mCurrentlyRelevantLayers);
87 }
88 
isLayerRelevant(int32_t layerId)89 bool SessionManager::isLayerRelevant(int32_t layerId) {
90     return mCurrentlyRelevantLayers.contains(layerId);
91 }
92 
93 } // namespace android::adpf