Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2015, 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_RESOURCEMANAGERSERVICE_H |
| 19 | #define ANDROID_RESOURCEMANAGERSERVICE_H |
| 20 | |
| 21 | #include <arpa/inet.h> |
| 22 | #include <binder/BinderService.h> |
| 23 | #include <utils/Errors.h> |
| 24 | #include <utils/KeyedVector.h> |
| 25 | #include <utils/String8.h> |
| 26 | #include <utils/threads.h> |
| 27 | #include <utils/Vector.h> |
| 28 | |
| 29 | #include <media/IResourceManagerService.h> |
| 30 | |
| 31 | namespace android { |
| 32 | |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 33 | class ServiceLog; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 34 | struct ProcessInfoInterface; |
| 35 | |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame^] | 36 | typedef std::map<std::tuple<MediaResource::Type, MediaResource::SubType, std::vector<uint8_t>>, MediaResource> ResourceList; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 37 | struct ResourceInfo { |
| 38 | int64_t clientId; |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 39 | uid_t uid; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 40 | sp<IResourceManagerClient> client; |
Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 41 | sp<IBinder::DeathRecipient> deathNotifier; |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 42 | ResourceList resources; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 43 | }; |
| 44 | |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 45 | // TODO: convert these to std::map |
| 46 | typedef KeyedVector<int64_t, ResourceInfo> ResourceInfos; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 47 | typedef KeyedVector<int, ResourceInfos> PidResourceInfosMap; |
| 48 | |
| 49 | class ResourceManagerService |
| 50 | : public BinderService<ResourceManagerService>, |
| 51 | public BnResourceManagerService |
| 52 | { |
| 53 | public: |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 54 | struct SystemCallbackInterface : public RefBase { |
| 55 | virtual void noteStartVideo(int uid) = 0; |
| 56 | virtual void noteStopVideo(int uid) = 0; |
| 57 | virtual void noteResetVideo() = 0; |
| 58 | virtual bool requestCpusetBoost( |
| 59 | bool enable, const sp<IInterface> &client) = 0; |
| 60 | }; |
| 61 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 62 | static char const *getServiceName() { return "media.resource_manager"; } |
| 63 | |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 64 | virtual status_t dump(int fd, const Vector<String16>& args); |
| 65 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 66 | ResourceManagerService(); |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 67 | explicit ResourceManagerService( |
| 68 | const sp<ProcessInfoInterface> &processInfo, |
| 69 | const sp<SystemCallbackInterface> &systemResource); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 70 | |
| 71 | // IResourceManagerService interface |
| 72 | virtual void config(const Vector<MediaResourcePolicy> &policies); |
| 73 | |
| 74 | virtual void addResource( |
| 75 | int pid, |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 76 | int uid, |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 77 | int64_t clientId, |
| 78 | const sp<IResourceManagerClient> client, |
| 79 | const Vector<MediaResource> &resources); |
| 80 | |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 81 | virtual void removeResource(int pid, int64_t clientId, |
| 82 | const Vector<MediaResource> &resources); |
| 83 | |
| 84 | virtual void removeClient(int pid, int64_t clientId); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 85 | |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 86 | // Tries to reclaim resource from processes with lower priority than the calling process |
| 87 | // according to the requested resources. |
| 88 | // Returns true if any resource has been reclaimed, otherwise returns false. |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 89 | virtual bool reclaimResource(int callingPid, const Vector<MediaResource> &resources); |
| 90 | |
Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 91 | void removeResource(int pid, int64_t clientId, bool checkValid); |
| 92 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 93 | protected: |
| 94 | virtual ~ResourceManagerService(); |
| 95 | |
| 96 | private: |
| 97 | friend class ResourceManagerServiceTest; |
| 98 | |
| 99 | // Gets the list of all the clients who own the specified resource type. |
| 100 | // Returns false if any client belongs to a process with higher priority than the |
| 101 | // calling process. The clients will remain unchanged if returns false. |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 102 | bool getAllClients_l(int callingPid, MediaResource::Type type, |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 103 | Vector<sp<IResourceManagerClient>> *clients); |
| 104 | |
| 105 | // Gets the client who owns specified resource type from lowest possible priority process. |
| 106 | // Returns false if the calling process priority is not higher than the lowest process |
| 107 | // priority. The client will remain unchanged if returns false. |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 108 | bool getLowestPriorityBiggestClient_l(int callingPid, MediaResource::Type type, |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 109 | sp<IResourceManagerClient> *client); |
| 110 | |
| 111 | // Gets lowest priority process that has the specified resource type. |
| 112 | // Returns false if failed. The output parameters will remain unchanged if failed. |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 113 | bool getLowestPriorityPid_l(MediaResource::Type type, int *pid, int *priority); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 114 | |
| 115 | // Gets the client who owns biggest piece of specified resource type from pid. |
| 116 | // Returns false if failed. The client will remain unchanged if failed. |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 117 | bool getBiggestClient_l(int pid, MediaResource::Type type, sp<IResourceManagerClient> *client); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 118 | |
| 119 | bool isCallingPriorityHigher_l(int callingPid, int pid); |
| 120 | |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 121 | // A helper function basically calls getLowestPriorityBiggestClient_l and add the result client |
| 122 | // to the given Vector. |
| 123 | void getClientForResource_l( |
| 124 | int callingPid, const MediaResource *res, Vector<sp<IResourceManagerClient>> *clients); |
| 125 | |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 126 | void onFirstAdded(const MediaResource& res, const ResourceInfo& clientInfo); |
| 127 | void onLastRemoved(const MediaResource& res, const ResourceInfo& clientInfo); |
| 128 | |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame^] | 129 | // Merge r2 into r1 |
| 130 | void mergeResources(MediaResource& r1, const MediaResource& r2); |
| 131 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 132 | mutable Mutex mLock; |
| 133 | sp<ProcessInfoInterface> mProcessInfo; |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 134 | sp<SystemCallbackInterface> mSystemCB; |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 135 | sp<ServiceLog> mServiceLog; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 136 | PidResourceInfosMap mMap; |
| 137 | bool mSupportsMultipleSecureCodecs; |
| 138 | bool mSupportsSecureWithNonSecureCodec; |
Chong Zhang | 79d2b28 | 2018-04-17 14:14:31 -0700 | [diff] [blame] | 139 | int32_t mCpuBoostCount; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | // ---------------------------------------------------------------------------- |
| 143 | |
| 144 | }; // namespace android |
| 145 | |
| 146 | #endif // ANDROID_RESOURCEMANAGERSERVICE_H |