blob: 741ef73c61c80b76f2fcc8e179cc00b610d3be67 [file] [log] [blame]
Ronghua Wu231c3d12015-03-11 15:10:32 -07001/*
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
31namespace android {
32
Ronghua Wua8ec8fc2015-05-07 13:58:22 -070033class ServiceLog;
Ronghua Wu231c3d12015-03-11 15:10:32 -070034struct ProcessInfoInterface;
35
36struct ResourceInfo {
37 int64_t clientId;
Chong Zhangee33d642019-08-08 14:26:43 -070038 uid_t uid;
Ronghua Wu231c3d12015-03-11 15:10:32 -070039 sp<IResourceManagerClient> client;
Wonsik Kim3e378962017-01-05 17:00:02 +090040 sp<IBinder::DeathRecipient> deathNotifier;
Ronghua Wu231c3d12015-03-11 15:10:32 -070041 Vector<MediaResource> resources;
Chong Zhang79d2b282018-04-17 14:14:31 -070042 bool cpuBoost;
Chong Zhangee33d642019-08-08 14:26:43 -070043 bool batteryNoted;
Ronghua Wu231c3d12015-03-11 15:10:32 -070044};
45
46typedef Vector<ResourceInfo> ResourceInfos;
47typedef KeyedVector<int, ResourceInfos> PidResourceInfosMap;
48
49class ResourceManagerService
50 : public BinderService<ResourceManagerService>,
51 public BnResourceManagerService
52{
53public:
54 static char const *getServiceName() { return "media.resource_manager"; }
55
Ronghua Wu8f9dd872015-04-23 15:24:25 -070056 virtual status_t dump(int fd, const Vector<String16>& args);
57
Ronghua Wu231c3d12015-03-11 15:10:32 -070058 ResourceManagerService();
Chih-Hung Hsieh51873d82016-08-09 14:18:51 -070059 explicit ResourceManagerService(sp<ProcessInfoInterface> processInfo);
Ronghua Wu231c3d12015-03-11 15:10:32 -070060
61 // IResourceManagerService interface
62 virtual void config(const Vector<MediaResourcePolicy> &policies);
63
64 virtual void addResource(
65 int pid,
Chong Zhangee33d642019-08-08 14:26:43 -070066 int uid,
Ronghua Wu231c3d12015-03-11 15:10:32 -070067 int64_t clientId,
68 const sp<IResourceManagerClient> client,
69 const Vector<MediaResource> &resources);
70
Ronghua Wu37c89242015-07-15 12:23:48 -070071 virtual void removeResource(int pid, int64_t clientId);
Ronghua Wu231c3d12015-03-11 15:10:32 -070072
Ronghua Wu05d89f12015-07-07 16:47:42 -070073 // Tries to reclaim resource from processes with lower priority than the calling process
74 // according to the requested resources.
75 // Returns true if any resource has been reclaimed, otherwise returns false.
Ronghua Wu231c3d12015-03-11 15:10:32 -070076 virtual bool reclaimResource(int callingPid, const Vector<MediaResource> &resources);
77
Wonsik Kim3e378962017-01-05 17:00:02 +090078 void removeResource(int pid, int64_t clientId, bool checkValid);
79
Ronghua Wu231c3d12015-03-11 15:10:32 -070080protected:
81 virtual ~ResourceManagerService();
82
83private:
84 friend class ResourceManagerServiceTest;
85
86 // Gets the list of all the clients who own the specified resource type.
87 // Returns false if any client belongs to a process with higher priority than the
88 // calling process. The clients will remain unchanged if returns false.
Ronghua Wuea15fd22016-03-03 13:35:05 -080089 bool getAllClients_l(int callingPid, MediaResource::Type type,
Ronghua Wu231c3d12015-03-11 15:10:32 -070090 Vector<sp<IResourceManagerClient>> *clients);
91
92 // Gets the client who owns specified resource type from lowest possible priority process.
93 // Returns false if the calling process priority is not higher than the lowest process
94 // priority. The client will remain unchanged if returns false.
Ronghua Wuea15fd22016-03-03 13:35:05 -080095 bool getLowestPriorityBiggestClient_l(int callingPid, MediaResource::Type type,
Ronghua Wu231c3d12015-03-11 15:10:32 -070096 sp<IResourceManagerClient> *client);
97
98 // Gets lowest priority process that has the specified resource type.
99 // Returns false if failed. The output parameters will remain unchanged if failed.
Ronghua Wuea15fd22016-03-03 13:35:05 -0800100 bool getLowestPriorityPid_l(MediaResource::Type type, int *pid, int *priority);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700101
102 // Gets the client who owns biggest piece of specified resource type from pid.
103 // Returns false if failed. The client will remain unchanged if failed.
Ronghua Wuea15fd22016-03-03 13:35:05 -0800104 bool getBiggestClient_l(int pid, MediaResource::Type type, sp<IResourceManagerClient> *client);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700105
106 bool isCallingPriorityHigher_l(int callingPid, int pid);
107
Ronghua Wu05d89f12015-07-07 16:47:42 -0700108 // A helper function basically calls getLowestPriorityBiggestClient_l and add the result client
109 // to the given Vector.
110 void getClientForResource_l(
111 int callingPid, const MediaResource *res, Vector<sp<IResourceManagerClient>> *clients);
112
Ronghua Wu231c3d12015-03-11 15:10:32 -0700113 mutable Mutex mLock;
114 sp<ProcessInfoInterface> mProcessInfo;
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700115 sp<ServiceLog> mServiceLog;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700116 PidResourceInfosMap mMap;
117 bool mSupportsMultipleSecureCodecs;
118 bool mSupportsSecureWithNonSecureCodec;
Chong Zhang79d2b282018-04-17 14:14:31 -0700119 int32_t mCpuBoostCount;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700120};
121
122// ----------------------------------------------------------------------------
123
124}; // namespace android
125
126#endif // ANDROID_RESOURCEMANAGERSERVICE_H