1231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu/*
2231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu**
3231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu** Copyright 2015, The Android Open Source Project
4231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu**
5231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu** Licensed under the Apache License, Version 2.0 (the "License");
6231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu** you may not use this file except in compliance with the License.
7231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu** You may obtain a copy of the License at
8231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu**
9231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu**     http://www.apache.org/licenses/LICENSE-2.0
10231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu**
11231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu** Unless required by applicable law or agreed to in writing, software
12231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu** distributed under the License is distributed on an "AS IS" BASIS,
13231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu** See the License for the specific language governing permissions and
15231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu** limitations under the License.
16231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu*/
17231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu
18231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu#ifndef ANDROID_RESOURCEMANAGERSERVICE_H
19231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu#define ANDROID_RESOURCEMANAGERSERVICE_H
20231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu
21231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu#include <arpa/inet.h>
22231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu#include <binder/BinderService.h>
23231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu#include <utils/Errors.h>
24231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu#include <utils/KeyedVector.h>
25231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu#include <utils/String8.h>
26231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu#include <utils/threads.h>
27231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu#include <utils/Vector.h>
28231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu
29231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu#include <media/IResourceManagerService.h>
30231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu
31231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wunamespace android {
32231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu
33a8ec8fcf6982ef158daf897faaab3e45285d0bf1Ronghua Wuclass ServiceLog;
34231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wustruct ProcessInfoInterface;
35231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu
36231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wustruct ResourceInfo {
37231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu    int64_t clientId;
38231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu    sp<IResourceManagerClient> client;
39231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu    Vector<MediaResource> resources;
40231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu};
41231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu
42231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wutypedef Vector<ResourceInfo> ResourceInfos;
43231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wutypedef KeyedVector<int, ResourceInfos> PidResourceInfosMap;
44231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu
45231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wuclass ResourceManagerService
46231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu    : public BinderService<ResourceManagerService>,
47231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu      public BnResourceManagerService
48231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu{
49231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wupublic:
50231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu    static char const *getServiceName() { return "media.resource_manager"; }
51231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu
528f9dd872366f54b6260506c75c3d0cc3f9f73f81Ronghua Wu    virtual status_t dump(int fd, const Vector<String16>& args);
538f9dd872366f54b6260506c75c3d0cc3f9f73f81Ronghua Wu
54231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu    ResourceManagerService();
55231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu    ResourceManagerService(sp<ProcessInfoInterface> processInfo);
56231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu
57231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu    // IResourceManagerService interface
58231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu    virtual void config(const Vector<MediaResourcePolicy> &policies);
59231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu
60231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu    virtual void addResource(
61231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu            int pid,
62231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu            int64_t clientId,
63231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu            const sp<IResourceManagerClient> client,
64231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu            const Vector<MediaResource> &resources);
65231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu
6637c8924c508a7c9b8bd3c8ce80fc005070531902Ronghua Wu    virtual void removeResource(int pid, int64_t clientId);
67231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu
6805d89f1c6452b2fce0b9f1315240f8cace69a88dRonghua Wu    // Tries to reclaim resource from processes with lower priority than the calling process
6905d89f1c6452b2fce0b9f1315240f8cace69a88dRonghua Wu    // according to the requested resources.
7005d89f1c6452b2fce0b9f1315240f8cace69a88dRonghua Wu    // Returns true if any resource has been reclaimed, otherwise returns false.
71231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu    virtual bool reclaimResource(int callingPid, const Vector<MediaResource> &resources);
72231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu
73231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wuprotected:
74231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu    virtual ~ResourceManagerService();
75231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu
76231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wuprivate:
77231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu    friend class ResourceManagerServiceTest;
78231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu
79231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu    // Gets the list of all the clients who own the specified resource type.
80231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu    // Returns false if any client belongs to a process with higher priority than the
81231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu    // calling process. The clients will remain unchanged if returns false.
82ea15fd29af81490311af9e12949b43524c39400eRonghua Wu    bool getAllClients_l(int callingPid, MediaResource::Type type,
83231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu            Vector<sp<IResourceManagerClient>> *clients);
84231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu
85231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu    // Gets the client who owns specified resource type from lowest possible priority process.
86231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu    // Returns false if the calling process priority is not higher than the lowest process
87231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu    // priority. The client will remain unchanged if returns false.
88ea15fd29af81490311af9e12949b43524c39400eRonghua Wu    bool getLowestPriorityBiggestClient_l(int callingPid, MediaResource::Type type,
89231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu            sp<IResourceManagerClient> *client);
90231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu
91231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu    // Gets lowest priority process that has the specified resource type.
92231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu    // Returns false if failed. The output parameters will remain unchanged if failed.
93ea15fd29af81490311af9e12949b43524c39400eRonghua Wu    bool getLowestPriorityPid_l(MediaResource::Type type, int *pid, int *priority);
94231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu
95231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu    // Gets the client who owns biggest piece of specified resource type from pid.
96231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu    // Returns false if failed. The client will remain unchanged if failed.
97ea15fd29af81490311af9e12949b43524c39400eRonghua Wu    bool getBiggestClient_l(int pid, MediaResource::Type type, sp<IResourceManagerClient> *client);
98231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu
99231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu    bool isCallingPriorityHigher_l(int callingPid, int pid);
100231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu
10105d89f1c6452b2fce0b9f1315240f8cace69a88dRonghua Wu    // A helper function basically calls getLowestPriorityBiggestClient_l and add the result client
10205d89f1c6452b2fce0b9f1315240f8cace69a88dRonghua Wu    // to the given Vector.
10305d89f1c6452b2fce0b9f1315240f8cace69a88dRonghua Wu    void getClientForResource_l(
10405d89f1c6452b2fce0b9f1315240f8cace69a88dRonghua Wu        int callingPid, const MediaResource *res, Vector<sp<IResourceManagerClient>> *clients);
10505d89f1c6452b2fce0b9f1315240f8cace69a88dRonghua Wu
106231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu    mutable Mutex mLock;
107231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu    sp<ProcessInfoInterface> mProcessInfo;
108a8ec8fcf6982ef158daf897faaab3e45285d0bf1Ronghua Wu    sp<ServiceLog> mServiceLog;
109231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu    PidResourceInfosMap mMap;
110231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu    bool mSupportsMultipleSecureCodecs;
111231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu    bool mSupportsSecureWithNonSecureCodec;
112231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu};
113231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu
114231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu// ----------------------------------------------------------------------------
115231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu
116231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu}; // namespace android
117231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu
118231c3d169ad029689f9d688b68179af7e96b8d78Ronghua Wu#endif // ANDROID_RESOURCEMANAGERSERVICE_H
119