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