blob: 7bad7151dd4d326d94ee42a2b80e13cfd11fef0d [file] [log] [blame]
Girish0ac5c212023-11-23 09:14:03 +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 "DefaultResourceModel"
20#include <utils/Log.h>
21
22#include "ResourceManagerServiceUtils.h"
23#include "DefaultResourceModel.h"
24#include "ResourceTracker.h"
25
26namespace android {
27
28DefaultResourceModel::DefaultResourceModel(
29 const std::shared_ptr<ResourceTracker>& resourceTracker,
30 bool supportsMultipleSecureCodecs,
31 bool supportsSecureWithNonSecureCodec)
32 : mSupportsMultipleSecureCodecs(supportsMultipleSecureCodecs),
33 mSupportsSecureWithNonSecureCodec(supportsSecureWithNonSecureCodec),
34 mResourceTracker(resourceTracker) {
35}
36
37DefaultResourceModel::~DefaultResourceModel() {
38}
39
40bool DefaultResourceModel::getAllClients(
41 const ReclaimRequestInfo& reclimRequestInfo,
42 std::vector<ClientInfo>& clients) {
43
44 clients.clear();
45 MediaResourceParcel mediaResource{.type = reclimRequestInfo.mResources[0].type,
46 .subType = reclimRequestInfo.mResources[0].subType};
47 ResourceRequestInfo resourceRequestInfo{reclimRequestInfo.mCallingPid, &mediaResource};
48
49 // Resolve the secure-unsecure codec conflicts if there is any.
50 switch (reclimRequestInfo.mResources[0].type) {
51 case MediaResource::Type::kSecureCodec:
52 // Looking to start a secure codec.
53 // #1. Make sure if multiple secure codecs can coexist
54 if (!mSupportsMultipleSecureCodecs) {
55 if (!mResourceTracker->getNonConflictingClients(resourceRequestInfo, clients)) {
56 // A higher priority process owns an instance of a secure codec.
57 // So this request can't be fulfilled.
58 return false;
59 }
60 }
61 // #2. Make sure a secure codec can coexist if there is an instance
62 // of non-secure codec running already.
63 if (!mSupportsSecureWithNonSecureCodec) {
64 mediaResource.type = MediaResource::Type::kNonSecureCodec;
65 if (!mResourceTracker->getNonConflictingClients(resourceRequestInfo, clients)) {
66 // A higher priority process owns an instance of a non-secure codec.
67 // So this request can't be fulfilled.
68 return false;
69 }
70 }
71 break;
72 case MediaResource::Type::kNonSecureCodec:
73 // Looking to start a non-secure codec.
74 // Make sure a non-secure codec can coexist if there is an instance
75 // of secure codec running already.
76 if (!mSupportsSecureWithNonSecureCodec) {
77 mediaResource.type = MediaResource::Type::kSecureCodec;
78 if (!mResourceTracker->getNonConflictingClients(resourceRequestInfo, clients)) {
79 // A higher priority process owns an instance of a secure codec.
80 // So this request can't be fulfilled.
81 return false;
82 }
83 }
84 break;
85 default:
86 break;
87 }
88
89 if (!clients.empty()) {
90 // There is secure/unsecure codec co-existence conflict
91 // and we have only found processes with lower priority holding the
92 // resources. So, all of these need to be reclaimed.
93 return false;
94 }
95
96 // No more resource conflicts.
97 switch (reclimRequestInfo.mResources[0].type) {
98 case MediaResource::Type::kSecureCodec:
99 case MediaResource::Type::kNonSecureCodec:
100 // Handling Codec resource reclaim
101 return getCodecClients(reclimRequestInfo, clients);
102 case MediaResource::Type::kGraphicMemory:
103 case MediaResource::Type::kDrmSession:
104 // Handling DRM and GraphicMemory resource reclaim
105 mediaResource.id = reclimRequestInfo.mResources[0].id;
106 mediaResource.value = reclimRequestInfo.mResources[0].value;
107 return mResourceTracker->getAllClients(resourceRequestInfo, clients);
108 default:
109 break;
110 }
111
112 return !clients.empty();
113}
114
115bool DefaultResourceModel::getCodecClients(
116 const ReclaimRequestInfo& reclimRequestInfo,
117 std::vector<ClientInfo>& clients) {
118 MediaResourceParcel mediaResource;
119 ResourceRequestInfo resourceRequestInfo{reclimRequestInfo.mCallingPid, &mediaResource};
120
121 // 1. Look to find the client(s) with the other resources, for the given
122 // primary type.
123 MediaResource::SubType primarySubType = reclimRequestInfo.mResources[0].subType;
124 for (size_t index = 1; index < reclimRequestInfo.mResources.size(); index++) {
125 mediaResource.type = reclimRequestInfo.mResources[index].type;
126 mediaResource.subType = reclimRequestInfo.mResources[index].subType;
127 mResourceTracker->getAllClients(resourceRequestInfo, clients, primarySubType);
128 }
129
130 // 2. Get all clients of the same type.
131 mediaResource.type = reclimRequestInfo.mResources[0].type;
132 mediaResource.subType = reclimRequestInfo.mResources[0].subType;
133 mResourceTracker->getAllClients(resourceRequestInfo, clients);
134
135 // 3. Get all cliends of the different type.
136 MediaResourceType otherType =
137 (reclimRequestInfo.mResources[0].type == MediaResource::Type::kSecureCodec) ?
138 MediaResource::Type::kNonSecureCodec : MediaResource::Type::kSecureCodec;
139 mediaResource.type = otherType;
140 mResourceTracker->getAllClients(resourceRequestInfo, clients);
141
142 return !clients.empty();
143}
144
145} // namespace android