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 
33 class ServiceLog;
34 struct ProcessInfoInterface;
35 
36 struct ResourceInfo {
37     int64_t clientId;
38     sp<IResourceManagerClient> client;
39     Vector<MediaResource> resources;
40 };
41 
42 typedef Vector<ResourceInfo> ResourceInfos;
43 typedef KeyedVector<int, ResourceInfos> PidResourceInfosMap;
44 
45 class ResourceManagerService
46     : public BinderService<ResourceManagerService>,
47       public BnResourceManagerService
48 {
49 public:
getServiceName()50     static char const *getServiceName() { return "media.resource_manager"; }
51 
52     virtual status_t dump(int fd, const Vector<String16>& args);
53 
54     ResourceManagerService();
55     ResourceManagerService(sp<ProcessInfoInterface> processInfo);
56 
57     // IResourceManagerService interface
58     virtual void config(const Vector<MediaResourcePolicy> &policies);
59 
60     virtual void addResource(
61             int pid,
62             int64_t clientId,
63             const sp<IResourceManagerClient> client,
64             const Vector<MediaResource> &resources);
65 
66     virtual void removeResource(int pid, int64_t clientId);
67 
68     // Tries to reclaim resource from processes with lower priority than the calling process
69     // according to the requested resources.
70     // Returns true if any resource has been reclaimed, otherwise returns false.
71     virtual bool reclaimResource(int callingPid, const Vector<MediaResource> &resources);
72 
73 protected:
74     virtual ~ResourceManagerService();
75 
76 private:
77     friend class ResourceManagerServiceTest;
78 
79     // Gets the list of all the clients who own the specified resource type.
80     // Returns false if any client belongs to a process with higher priority than the
81     // calling process. The clients will remain unchanged if returns false.
82     bool getAllClients_l(int callingPid, MediaResource::Type type,
83             Vector<sp<IResourceManagerClient>> *clients);
84 
85     // Gets the client who owns specified resource type from lowest possible priority process.
86     // Returns false if the calling process priority is not higher than the lowest process
87     // priority. The client will remain unchanged if returns false.
88     bool getLowestPriorityBiggestClient_l(int callingPid, MediaResource::Type type,
89             sp<IResourceManagerClient> *client);
90 
91     // Gets lowest priority process that has the specified resource type.
92     // Returns false if failed. The output parameters will remain unchanged if failed.
93     bool getLowestPriorityPid_l(MediaResource::Type type, int *pid, int *priority);
94 
95     // Gets the client who owns biggest piece of specified resource type from pid.
96     // Returns false if failed. The client will remain unchanged if failed.
97     bool getBiggestClient_l(int pid, MediaResource::Type type, sp<IResourceManagerClient> *client);
98 
99     bool isCallingPriorityHigher_l(int callingPid, int pid);
100 
101     // A helper function basically calls getLowestPriorityBiggestClient_l and add the result client
102     // to the given Vector.
103     void getClientForResource_l(
104         int callingPid, const MediaResource *res, Vector<sp<IResourceManagerClient>> *clients);
105 
106     mutable Mutex mLock;
107     sp<ProcessInfoInterface> mProcessInfo;
108     sp<ServiceLog> mServiceLog;
109     PidResourceInfosMap mMap;
110     bool mSupportsMultipleSecureCodecs;
111     bool mSupportsSecureWithNonSecureCodec;
112 };
113 
114 // ----------------------------------------------------------------------------
115 
116 }; // namespace android
117 
118 #endif // ANDROID_RESOURCEMANAGERSERVICE_H
119