| 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 |  | 
 | 36 | struct ResourceInfo { | 
 | 37 |     int64_t clientId; | 
 | 38 |     sp<IResourceManagerClient> client; | 
| Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame^] | 39 |     sp<IBinder::DeathRecipient> deathNotifier; | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 40 |     Vector<MediaResource> resources; | 
 | 41 | }; | 
 | 42 |  | 
 | 43 | typedef Vector<ResourceInfo> ResourceInfos; | 
 | 44 | typedef KeyedVector<int, ResourceInfos> PidResourceInfosMap; | 
 | 45 |  | 
 | 46 | class ResourceManagerService | 
 | 47 |     : public BinderService<ResourceManagerService>, | 
 | 48 |       public BnResourceManagerService | 
 | 49 | { | 
 | 50 | public: | 
 | 51 |     static char const *getServiceName() { return "media.resource_manager"; } | 
 | 52 |  | 
| Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 53 |     virtual status_t dump(int fd, const Vector<String16>& args); | 
 | 54 |  | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 55 |     ResourceManagerService(); | 
| Chih-Hung Hsieh | 51873d8 | 2016-08-09 14:18:51 -0700 | [diff] [blame] | 56 |     explicit ResourceManagerService(sp<ProcessInfoInterface> processInfo); | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 57 |  | 
 | 58 |     // IResourceManagerService interface | 
 | 59 |     virtual void config(const Vector<MediaResourcePolicy> &policies); | 
 | 60 |  | 
 | 61 |     virtual void addResource( | 
 | 62 |             int pid, | 
 | 63 |             int64_t clientId, | 
 | 64 |             const sp<IResourceManagerClient> client, | 
 | 65 |             const Vector<MediaResource> &resources); | 
 | 66 |  | 
| Ronghua Wu | 37c8924 | 2015-07-15 12:23:48 -0700 | [diff] [blame] | 67 |     virtual void removeResource(int pid, int64_t clientId); | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 68 |  | 
| Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 69 |     // Tries to reclaim resource from processes with lower priority than the calling process | 
 | 70 |     // according to the requested resources. | 
 | 71 |     // Returns true if any resource has been reclaimed, otherwise returns false. | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 72 |     virtual bool reclaimResource(int callingPid, const Vector<MediaResource> &resources); | 
 | 73 |  | 
| Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame^] | 74 |     void removeResource(int pid, int64_t clientId, bool checkValid); | 
 | 75 |  | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 76 | protected: | 
 | 77 |     virtual ~ResourceManagerService(); | 
 | 78 |  | 
 | 79 | private: | 
 | 80 |     friend class ResourceManagerServiceTest; | 
 | 81 |  | 
 | 82 |     // Gets the list of all the clients who own the specified resource type. | 
 | 83 |     // Returns false if any client belongs to a process with higher priority than the | 
 | 84 |     // calling process. The clients will remain unchanged if returns false. | 
| Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 85 |     bool getAllClients_l(int callingPid, MediaResource::Type type, | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 86 |             Vector<sp<IResourceManagerClient>> *clients); | 
 | 87 |  | 
 | 88 |     // Gets the client who owns specified resource type from lowest possible priority process. | 
 | 89 |     // Returns false if the calling process priority is not higher than the lowest process | 
 | 90 |     // priority. The client will remain unchanged if returns false. | 
| Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 91 |     bool getLowestPriorityBiggestClient_l(int callingPid, MediaResource::Type type, | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 92 |             sp<IResourceManagerClient> *client); | 
 | 93 |  | 
 | 94 |     // Gets lowest priority process that has the specified resource type. | 
 | 95 |     // Returns false if failed. The output parameters will remain unchanged if failed. | 
| Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 96 |     bool getLowestPriorityPid_l(MediaResource::Type type, int *pid, int *priority); | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 97 |  | 
 | 98 |     // Gets the client who owns biggest piece of specified resource type from pid. | 
 | 99 |     // Returns false if failed. The client will remain unchanged if failed. | 
| Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 100 |     bool getBiggestClient_l(int pid, MediaResource::Type type, sp<IResourceManagerClient> *client); | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 101 |  | 
 | 102 |     bool isCallingPriorityHigher_l(int callingPid, int pid); | 
 | 103 |  | 
| Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 104 |     // A helper function basically calls getLowestPriorityBiggestClient_l and add the result client | 
 | 105 |     // to the given Vector. | 
 | 106 |     void getClientForResource_l( | 
 | 107 |         int callingPid, const MediaResource *res, Vector<sp<IResourceManagerClient>> *clients); | 
 | 108 |  | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 109 |     mutable Mutex mLock; | 
 | 110 |     sp<ProcessInfoInterface> mProcessInfo; | 
| Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 111 |     sp<ServiceLog> mServiceLog; | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 112 |     PidResourceInfosMap mMap; | 
 | 113 |     bool mSupportsMultipleSecureCodecs; | 
 | 114 |     bool mSupportsSecureWithNonSecureCodec; | 
 | 115 | }; | 
 | 116 |  | 
 | 117 | // ---------------------------------------------------------------------------- | 
 | 118 |  | 
 | 119 | }; // namespace android | 
 | 120 |  | 
 | 121 | #endif // ANDROID_RESOURCEMANAGERSERVICE_H |