Girish | 27365ed | 2023-10-11 20:20:55 +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 | #ifndef ANDROID_MEDIA_RESOURCEMANAGERSERVICEUTILS_H_ |
| 19 | #define ANDROID_MEDIA_RESOURCEMANAGERSERVICEUTILS_H_ |
| 20 | |
Girish | 0ac5c21 | 2023-11-23 09:14:03 +0000 | [diff] [blame] | 21 | #include <map> |
| 22 | #include <memory> |
Girish | 27365ed | 2023-10-11 20:20:55 +0000 | [diff] [blame] | 23 | #include <vector> |
Girish | ab17b0f | 2023-11-20 06:00:44 +0000 | [diff] [blame] | 24 | |
| 25 | #include <aidl/android/media/BnResourceManagerService.h> |
| 26 | #include <media/MediaResource.h> |
Girish | 27365ed | 2023-10-11 20:20:55 +0000 | [diff] [blame] | 27 | #include <utils/String8.h> |
| 28 | |
| 29 | namespace android { |
| 30 | |
Girish | ab17b0f | 2023-11-20 06:00:44 +0000 | [diff] [blame] | 31 | class ResourceManagerService; |
| 32 | |
| 33 | /* |
| 34 | * Death Notifier to track IResourceManagerClient's death. |
| 35 | */ |
| 36 | class DeathNotifier : public std::enable_shared_from_this<DeathNotifier> { |
| 37 | |
| 38 | // BinderDiedContext defines the cookie that is passed as DeathRecipient. |
| 39 | // Since this can maintain more context than a raw pointer, we can |
| 40 | // validate the scope of DeathNotifier, before deferencing it upon the binder death. |
| 41 | struct BinderDiedContext { |
| 42 | std::weak_ptr<DeathNotifier> mDeathNotifier; |
| 43 | }; |
| 44 | public: |
| 45 | static std::shared_ptr<DeathNotifier> Create( |
| 46 | const std::shared_ptr<::aidl::android::media::IResourceManagerClient>& client, |
| 47 | const std::weak_ptr<ResourceManagerService>& service, |
| 48 | const ::aidl::android::media::ClientInfoParcel& clientInfo, |
| 49 | bool overrideProcessInfo = false); |
| 50 | |
| 51 | DeathNotifier(const std::shared_ptr<::aidl::android::media::IResourceManagerClient>& client, |
| 52 | const std::weak_ptr<ResourceManagerService>& service, |
| 53 | const ::aidl::android::media::ClientInfoParcel& clientInfo); |
| 54 | |
| 55 | virtual ~DeathNotifier() { |
| 56 | unlink(); |
| 57 | } |
| 58 | |
| 59 | // Implement death recipient |
| 60 | static void BinderDiedCallback(void* cookie); |
| 61 | static void BinderUnlinkedCallback(void* cookie); |
| 62 | virtual void binderDied(); |
| 63 | |
| 64 | private: |
| 65 | void link() { |
| 66 | // Create the context that is passed as cookie to the binder death notification. |
| 67 | // The context gets deleted at BinderUnlinkedCallback. |
| 68 | mCookie = new BinderDiedContext{.mDeathNotifier = weak_from_this()}; |
| 69 | // Register for the callbacks by linking to death notification. |
| 70 | AIBinder_linkToDeath(mClient->asBinder().get(), mDeathRecipient.get(), mCookie); |
| 71 | } |
| 72 | |
| 73 | void unlink() { |
| 74 | if (mClient != nullptr) { |
| 75 | // Unlink from the death notification. |
| 76 | AIBinder_unlinkToDeath(mClient->asBinder().get(), mDeathRecipient.get(), mCookie); |
| 77 | mClient = nullptr; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | protected: |
| 82 | std::shared_ptr<::aidl::android::media::IResourceManagerClient> mClient; |
| 83 | std::weak_ptr<ResourceManagerService> mService; |
| 84 | const ::aidl::android::media::ClientInfoParcel mClientInfo; |
| 85 | BinderDiedContext* mCookie; |
| 86 | ::ndk::ScopedAIBinder_DeathRecipient mDeathRecipient; |
| 87 | }; |
| 88 | |
| 89 | class OverrideProcessInfoDeathNotifier : public DeathNotifier { |
| 90 | public: |
| 91 | OverrideProcessInfoDeathNotifier( |
| 92 | const std::shared_ptr<::aidl::android::media::IResourceManagerClient>& client, |
| 93 | const std::weak_ptr<ResourceManagerService>& service, |
| 94 | const ::aidl::android::media::ClientInfoParcel& clientInfo) |
| 95 | : DeathNotifier(client, service, clientInfo) {} |
| 96 | |
| 97 | virtual ~OverrideProcessInfoDeathNotifier() {} |
| 98 | |
| 99 | virtual void binderDied(); |
| 100 | }; |
| 101 | |
| 102 | // A map of tuple(type, sub-type, id) and the resource parcel. |
| 103 | typedef std::map<std::tuple< |
| 104 | MediaResource::Type, MediaResource::SubType, std::vector<uint8_t>>, |
| 105 | ::aidl::android::media::MediaResourceParcel> ResourceList; |
| 106 | |
| 107 | // Encapsulation for Resource Info, that contains |
| 108 | // - pid of the app |
| 109 | // - uid of the app |
| 110 | // - client id |
| 111 | // - name of the client (specifically for the codec) |
| 112 | // - the client associted with it |
| 113 | // - death notifier for the (above) client |
| 114 | // - list of resources associated with it |
| 115 | // - A flag that marks whether this resource is pending to be removed. |
| 116 | struct ResourceInfo { |
| 117 | pid_t pid; |
| 118 | uid_t uid; |
| 119 | int64_t clientId; |
| 120 | std::string name; |
| 121 | std::shared_ptr<::aidl::android::media::IResourceManagerClient> client; |
| 122 | std::shared_ptr<DeathNotifier> deathNotifier = nullptr; |
| 123 | ResourceList resources; |
| 124 | bool pendingRemoval{false}; |
| 125 | }; |
| 126 | |
| 127 | /* |
Girish | 0ac5c21 | 2023-11-23 09:14:03 +0000 | [diff] [blame] | 128 | * Resource Reclaim request info that encapsulates |
| 129 | * - the calling/requesting process pid. |
| 130 | * - the list of resources requesting (to be reclaimed from others) |
| 131 | */ |
| 132 | struct ReclaimRequestInfo { |
| 133 | int mCallingPid = -1; |
| 134 | const std::vector<::aidl::android::media::MediaResourceParcel>& mResources; |
| 135 | }; |
| 136 | |
| 137 | /* |
Girish | ab17b0f | 2023-11-20 06:00:44 +0000 | [diff] [blame] | 138 | * Resource request info that encapsulates |
| 139 | * - the calling/requesting process pid. |
| 140 | * - the resource requesting (to be reclaimed from others) |
| 141 | */ |
| 142 | struct ResourceRequestInfo { |
| 143 | // pid of the calling/requesting process. |
| 144 | int mCallingPid = -1; |
| 145 | // resources requested. |
| 146 | const ::aidl::android::media::MediaResourceParcel* mResource; |
| 147 | }; |
| 148 | |
| 149 | /* |
| 150 | * Structure that defines the Client - a possible target to relcaim from. |
| 151 | * This encapsulates pid, uid of the process and the client id |
| 152 | * based on the reclaim policy. |
| 153 | */ |
| 154 | struct ClientInfo { |
| 155 | // pid of the process. |
| 156 | pid_t mPid = -1; |
| 157 | // uid of the process. |
| 158 | uid_t mUid = -1; |
| 159 | // Client Id. |
| 160 | int64_t mClientId = -1; |
| 161 | ClientInfo(pid_t pid = -1, uid_t uid = -1, const int64_t& clientId = -1) |
| 162 | : mPid(pid), mUid(uid), mClientId(clientId) {} |
| 163 | }; |
| 164 | |
| 165 | // Map of Resource information index through the client id. |
| 166 | typedef std::map<int64_t, ResourceInfo> ResourceInfos; |
| 167 | |
| 168 | // Map of Resource information indexed through the process id. |
| 169 | typedef std::map<int, ResourceInfos> PidResourceInfosMap; |
| 170 | |
Girish | 27365ed | 2023-10-11 20:20:55 +0000 | [diff] [blame] | 171 | // templated function to stringify the given vector of items. |
| 172 | template <typename T> |
| 173 | String8 getString(const std::vector<T>& items) { |
| 174 | String8 itemsStr; |
| 175 | for (size_t i = 0; i < items.size(); ++i) { |
| 176 | itemsStr.appendFormat("%s ", toString(items[i]).c_str()); |
| 177 | } |
| 178 | return itemsStr; |
| 179 | } |
| 180 | |
| 181 | // Bunch of utility functions that looks for a specific Resource. |
| 182 | |
| 183 | //Check whether a given resource (of type and subtype) is found in given resource parcel. |
| 184 | bool hasResourceType(MediaResource::Type type, MediaResource::SubType subType, |
Girish | 6a6044d | 2023-11-22 21:23:14 +0000 | [diff] [blame] | 185 | const ::aidl::android::media::MediaResourceParcel& resource); |
Girish | 27365ed | 2023-10-11 20:20:55 +0000 | [diff] [blame] | 186 | |
| 187 | //Check whether a given resource (of type and subtype) is found in given resource list. |
| 188 | bool hasResourceType(MediaResource::Type type, MediaResource::SubType subType, |
Girish | ab17b0f | 2023-11-20 06:00:44 +0000 | [diff] [blame] | 189 | const ResourceList& resources); |
Girish | 27365ed | 2023-10-11 20:20:55 +0000 | [diff] [blame] | 190 | |
| 191 | //Check whether a given resource (of type and subtype) is found in given resource info list. |
| 192 | bool hasResourceType(MediaResource::Type type, MediaResource::SubType subType, |
Girish | ab17b0f | 2023-11-20 06:00:44 +0000 | [diff] [blame] | 193 | const ResourceInfos& infos); |
Girish | 27365ed | 2023-10-11 20:20:55 +0000 | [diff] [blame] | 194 | |
| 195 | // Return modifiable list of ResourceInfo for a given process (look up by pid) |
| 196 | // from the map of ResourceInfos. |
| 197 | ResourceInfos& getResourceInfosForEdit(int pid, PidResourceInfosMap& map); |
| 198 | |
| 199 | // Return modifiable ResourceInfo for a given process (look up by pid) |
| 200 | // from the map of ResourceInfos. |
| 201 | // If the item is not in the map, create one and add it to the map. |
Girish | ab17b0f | 2023-11-20 06:00:44 +0000 | [diff] [blame] | 202 | ResourceInfo& getResourceInfoForEdit( |
| 203 | const aidl::android::media::ClientInfoParcel& clientInfo, |
| 204 | const std::shared_ptr<aidl::android::media::IResourceManagerClient>& client, |
| 205 | ResourceInfos& infos); |
| 206 | |
| 207 | // Merge resources from r2 into r1. |
Girish | 6a6044d | 2023-11-22 21:23:14 +0000 | [diff] [blame] | 208 | void mergeResources(::aidl::android::media::MediaResourceParcel& r1, |
| 209 | const ::aidl::android::media::MediaResourceParcel& r2); |
| 210 | |
| 211 | // To notify the media_resource_monitor about the resource being granted. |
| 212 | void notifyResourceGranted( |
| 213 | int pid, |
| 214 | const std::vector<::aidl::android::media::MediaResourceParcel>& resources); |
Girish | 27365ed | 2023-10-11 20:20:55 +0000 | [diff] [blame] | 215 | |
| 216 | } // namespace android |
| 217 | |
| 218 | #endif //ANDROID_MEDIA_RESOURCEMANAGERSERVICEUTILS_H_ |