blob: 892b1b32db2cd407e72e36e76c19edc07ed4291c [file] [log] [blame]
Girish27365ed2023-10-11 20:20:55 +00001/*
2**
3** Copyright 2023, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18//#define LOG_NDEBUG 0
19#define LOG_TAG "ResourceManagerServiceUtils"
20#include <utils/Log.h>
21
22#include "ResourceManagerService.h"
23#include "ResourceManagerServiceUtils.h"
24
25namespace android {
26
27bool hasResourceType(MediaResource::Type type, MediaResource::SubType subType,
28 const MediaResourceParcel& resource) {
29 if (type != resource.type) {
30 return false;
31 }
32 switch (type) {
33 // Codec subtypes (e.g. video vs. audio) are each considered separate resources, so
34 // compare the subtypes as well.
35 case MediaResource::Type::kSecureCodec:
36 case MediaResource::Type::kNonSecureCodec:
37 if (resource.subType == subType) {
38 return true;
39 }
40 break;
41 // Non-codec resources are not segregated by the subtype (e.g. video vs. audio).
42 default:
43 return true;
44 }
45 return false;
46}
47
48bool hasResourceType(MediaResource::Type type, MediaResource::SubType subType,
49 const ResourceList& resources) {
50 for (auto it = resources.begin(); it != resources.end(); it++) {
51 if (hasResourceType(type, subType, it->second)) {
52 return true;
53 }
54 }
55 return false;
56}
57
58bool hasResourceType(MediaResource::Type type, MediaResource::SubType subType,
59 const ResourceInfos& infos) {
60 for (const auto& [id, info] : infos) {
61 if (hasResourceType(type, subType, info.resources)) {
62 return true;
63 }
64 }
65 return false;
66}
67
68ResourceInfos& getResourceInfosForEdit(int pid, PidResourceInfosMap& map) {
69 PidResourceInfosMap::iterator found = map.find(pid);
70 if (found == map.end()) {
71 // new pid
72 ResourceInfos infosForPid;
73 auto [it, inserted] = map.emplace(pid, infosForPid);
74 found = it;
75 }
76
77 return found->second;
78}
79
80ResourceInfo& getResourceInfoForEdit(const ClientInfoParcel& clientInfo,
81 const std::shared_ptr<IResourceManagerClient>& client, ResourceInfos& infos) {
82 ResourceInfos::iterator found = infos.find(clientInfo.id);
83
84 if (found == infos.end()) {
85 ResourceInfo info{.uid = static_cast<uid_t>(clientInfo.uid),
86 .clientId = clientInfo.id,
87 .name = clientInfo.name.empty()? "<unknown client>" : clientInfo.name,
88 .client = client,
89 .deathNotifier = nullptr,
90 .pendingRemoval = false};
91 auto [it, inserted] = infos.emplace(clientInfo.id, info);
92 found = it;
93 }
94
95 return found->second;
96}
97
98} // namespace android