blob: ff45c878614dfe3712024ffd7f4ea57664a2595a [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//#define LOG_NDEBUG 0
19#define LOG_TAG "ResourceManagerService"
20#include <utils/Log.h>
21
Chong Zhangfdd512a2019-11-22 11:03:14 -080022#include <android/binder_manager.h>
23#include <android/binder_process.h>
Dongwon Kangfe508d32015-12-15 14:22:05 +090024#include <binder/IMediaResourceMonitor.h>
Ronghua Wu231c3d12015-03-11 15:10:32 -070025#include <binder/IServiceManager.h>
Chong Zhangee33d642019-08-08 14:26:43 -070026#include <cutils/sched_policy.h>
Ronghua Wu231c3d12015-03-11 15:10:32 -070027#include <dirent.h>
Chong Zhang181e6952019-10-09 13:23:39 -070028#include <media/MediaResourcePolicy.h>
Ronghua Wu231c3d12015-03-11 15:10:32 -070029#include <media/stagefright/ProcessInfo.h>
Chong Zhangee33d642019-08-08 14:26:43 -070030#include <mediautils/BatteryNotifier.h>
31#include <mediautils/SchedulingPolicyService.h>
Ronghua Wu231c3d12015-03-11 15:10:32 -070032#include <string.h>
33#include <sys/types.h>
34#include <sys/stat.h>
35#include <sys/time.h>
36#include <unistd.h>
37
38#include "ResourceManagerService.h"
Ronghua Wua8ec8fc2015-05-07 13:58:22 -070039#include "ServiceLog.h"
Chong Zhangee33d642019-08-08 14:26:43 -070040
Ronghua Wu231c3d12015-03-11 15:10:32 -070041namespace android {
42
Chong Zhangfdd512a2019-11-22 11:03:14 -080043DeathNotifier::DeathNotifier(const std::shared_ptr<ResourceManagerService> &service,
44 int pid, int64_t clientId)
45 : mService(service), mPid(pid), mClientId(clientId) {}
Wonsik Kim3e378962017-01-05 17:00:02 +090046
Chong Zhangfdd512a2019-11-22 11:03:14 -080047//static
48void DeathNotifier::BinderDiedCallback(void* cookie) {
49 auto thiz = static_cast<DeathNotifier*>(cookie);
50 thiz->binderDied();
51}
Wonsik Kim3e378962017-01-05 17:00:02 +090052
Chong Zhangfdd512a2019-11-22 11:03:14 -080053void DeathNotifier::binderDied() {
54 // Don't check for pid validity since we know it's already dead.
55 std::shared_ptr<ResourceManagerService> service = mService.lock();
56 if (service == nullptr) {
57 ALOGW("ResourceManagerService is dead as well.");
58 return;
Wonsik Kim3e378962017-01-05 17:00:02 +090059 }
Chong Zhangfdd512a2019-11-22 11:03:14 -080060 service->removeResource(mPid, mClientId, false);
Henry Fang32762922020-01-28 18:40:39 -080061
62 service->overridePid(mPid, -1);
Chong Zhangfdd512a2019-11-22 11:03:14 -080063}
Wonsik Kim3e378962017-01-05 17:00:02 +090064
Ronghua Wu231c3d12015-03-11 15:10:32 -070065template <typename T>
Chong Zhang181e6952019-10-09 13:23:39 -070066static String8 getString(const std::vector<T> &items) {
Ronghua Wu231c3d12015-03-11 15:10:32 -070067 String8 itemsStr;
68 for (size_t i = 0; i < items.size(); ++i) {
Chong Zhang181e6952019-10-09 13:23:39 -070069 itemsStr.appendFormat("%s ", toString(items[i]).string());
Ronghua Wu231c3d12015-03-11 15:10:32 -070070 }
71 return itemsStr;
72}
73
Chong Zhangfb092d32019-08-12 09:45:44 -070074static bool hasResourceType(MediaResource::Type type, const ResourceList& resources) {
75 for (auto it = resources.begin(); it != resources.end(); it++) {
Chong Zhang181e6952019-10-09 13:23:39 -070076 if (it->second.type == type) {
Ronghua Wu231c3d12015-03-11 15:10:32 -070077 return true;
78 }
79 }
80 return false;
81}
82
Chih-Hung Hsieh51873d82016-08-09 14:18:51 -070083static bool hasResourceType(MediaResource::Type type, const ResourceInfos& infos) {
Ronghua Wu231c3d12015-03-11 15:10:32 -070084 for (size_t i = 0; i < infos.size(); ++i) {
85 if (hasResourceType(type, infos[i].resources)) {
86 return true;
87 }
88 }
89 return false;
90}
91
92static ResourceInfos& getResourceInfosForEdit(
93 int pid,
94 PidResourceInfosMap& map) {
95 ssize_t index = map.indexOfKey(pid);
96 if (index < 0) {
97 // new pid
98 ResourceInfos infosForPid;
99 map.add(pid, infosForPid);
100 }
101
102 return map.editValueFor(pid);
103}
104
105static ResourceInfo& getResourceInfoForEdit(
Chong Zhangee33d642019-08-08 14:26:43 -0700106 uid_t uid,
Ronghua Wu231c3d12015-03-11 15:10:32 -0700107 int64_t clientId,
Chong Zhangfdd512a2019-11-22 11:03:14 -0800108 const std::shared_ptr<IResourceManagerClient>& client,
Ronghua Wu231c3d12015-03-11 15:10:32 -0700109 ResourceInfos& infos) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700110 ssize_t index = infos.indexOfKey(clientId);
111
112 if (index < 0) {
113 ResourceInfo info;
114 info.uid = uid;
115 info.clientId = clientId;
116 info.client = client;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700117 info.pendingRemoval = false;
Chong Zhangfb092d32019-08-12 09:45:44 -0700118
119 index = infos.add(clientId, info);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700120 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700121
122 return infos.editValueAt(index);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700123}
124
Chong Zhang181e6952019-10-09 13:23:39 -0700125static void notifyResourceGranted(int pid, const std::vector<MediaResourceParcel> &resources) {
Dongwon Kangfe508d32015-12-15 14:22:05 +0900126 static const char* const kServiceName = "media_resource_monitor";
Dongwon Kang2642c842016-03-23 18:07:29 -0700127 sp<IBinder> binder = defaultServiceManager()->checkService(String16(kServiceName));
Dongwon Kangfe508d32015-12-15 14:22:05 +0900128 if (binder != NULL) {
129 sp<IMediaResourceMonitor> service = interface_cast<IMediaResourceMonitor>(binder);
130 for (size_t i = 0; i < resources.size(); ++i) {
Chong Zhang181e6952019-10-09 13:23:39 -0700131 if (resources[i].subType == MediaResource::SubType::kAudioCodec) {
Dongwon Kang69c23dd2016-03-22 15:22:45 -0700132 service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_AUDIO_CODEC);
Chong Zhang181e6952019-10-09 13:23:39 -0700133 } else if (resources[i].subType == MediaResource::SubType::kVideoCodec) {
Dongwon Kang69c23dd2016-03-22 15:22:45 -0700134 service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_VIDEO_CODEC);
135 }
Dongwon Kangfe508d32015-12-15 14:22:05 +0900136 }
137 }
138}
139
Chong Zhangfdd512a2019-11-22 11:03:14 -0800140binder_status_t ResourceManagerService::dump(
141 int fd, const char** /*args*/, uint32_t /*numArgs*/) {
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700142 String8 result;
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700143
dcashman014e91e2015-09-11 09:33:01 -0700144 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
145 result.format("Permission Denial: "
146 "can't dump ResourceManagerService from pid=%d, uid=%d\n",
Chong Zhangfdd512a2019-11-22 11:03:14 -0800147 AIBinder_getCallingPid(),
148 AIBinder_getCallingUid());
dcashman014e91e2015-09-11 09:33:01 -0700149 write(fd, result.string(), result.size());
150 return PERMISSION_DENIED;
151 }
152
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700153 PidResourceInfosMap mapCopy;
154 bool supportsMultipleSecureCodecs;
155 bool supportsSecureWithNonSecureCodec;
Henry Fang32762922020-01-28 18:40:39 -0800156 std::map<int, int> overridePidMapCopy;
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700157 String8 serviceLog;
158 {
159 Mutex::Autolock lock(mLock);
160 mapCopy = mMap; // Shadow copy, real copy will happen on write.
161 supportsMultipleSecureCodecs = mSupportsMultipleSecureCodecs;
162 supportsSecureWithNonSecureCodec = mSupportsSecureWithNonSecureCodec;
163 serviceLog = mServiceLog->toString(" " /* linePrefix */);
Henry Fang32762922020-01-28 18:40:39 -0800164 overridePidMapCopy = mOverridePidMap;
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700165 }
166
167 const size_t SIZE = 256;
168 char buffer[SIZE];
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700169 snprintf(buffer, SIZE, "ResourceManagerService: %p\n", this);
170 result.append(buffer);
171 result.append(" Policies:\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700172 snprintf(buffer, SIZE, " SupportsMultipleSecureCodecs: %d\n", supportsMultipleSecureCodecs);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700173 result.append(buffer);
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700174 snprintf(buffer, SIZE, " SupportsSecureWithNonSecureCodec: %d\n",
175 supportsSecureWithNonSecureCodec);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700176 result.append(buffer);
177
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700178 result.append(" Processes:\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700179 for (size_t i = 0; i < mapCopy.size(); ++i) {
180 snprintf(buffer, SIZE, " Pid: %d\n", mapCopy.keyAt(i));
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700181 result.append(buffer);
182
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700183 const ResourceInfos &infos = mapCopy.valueAt(i);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700184 for (size_t j = 0; j < infos.size(); ++j) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700185 result.append(" Client:\n");
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700186 snprintf(buffer, SIZE, " Id: %lld\n", (long long)infos[j].clientId);
187 result.append(buffer);
188
Chong Zhang181e6952019-10-09 13:23:39 -0700189 std::string clientName;
190 Status status = infos[j].client->getName(&clientName);
191 if (!status.isOk()) {
192 clientName = "<unknown client>";
193 }
194 snprintf(buffer, SIZE, " Name: %s\n", clientName.c_str());
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700195 result.append(buffer);
196
Chong Zhangfb092d32019-08-12 09:45:44 -0700197 const ResourceList &resources = infos[j].resources;
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700198 result.append(" Resources:\n");
Chong Zhangfb092d32019-08-12 09:45:44 -0700199 for (auto it = resources.begin(); it != resources.end(); it++) {
Chong Zhang181e6952019-10-09 13:23:39 -0700200 snprintf(buffer, SIZE, " %s\n", toString(it->second).string());
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700201 result.append(buffer);
202 }
203 }
204 }
Henry Fang32762922020-01-28 18:40:39 -0800205 result.append(" Process Pid override:\n");
206 for (auto it = overridePidMapCopy.begin(); it != overridePidMapCopy.end(); ++it) {
207 snprintf(buffer, SIZE, " Original Pid: %d, Override Pid: %d\n",
208 it->first, it->second);
209 result.append(buffer);
210 }
Ronghua Wu022ed722015-05-11 15:15:09 -0700211 result.append(" Events logs (most recent at top):\n");
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700212 result.append(serviceLog);
Ronghua Wu8f9dd872015-04-23 15:24:25 -0700213
214 write(fd, result.string(), result.size());
215 return OK;
216}
217
Chong Zhangdd726802019-08-21 17:24:13 -0700218struct SystemCallbackImpl :
219 public ResourceManagerService::SystemCallbackInterface {
Chong Zhangfdd512a2019-11-22 11:03:14 -0800220 SystemCallbackImpl() : mClientToken(new BBinder()) {}
Ronghua Wu231c3d12015-03-11 15:10:32 -0700221
Chong Zhangdd726802019-08-21 17:24:13 -0700222 virtual void noteStartVideo(int uid) override {
223 BatteryNotifier::getInstance().noteStartVideo(uid);
224 }
225 virtual void noteStopVideo(int uid) override {
226 BatteryNotifier::getInstance().noteStopVideo(uid);
227 }
228 virtual void noteResetVideo() override {
229 BatteryNotifier::getInstance().noteResetVideo();
230 }
Chong Zhangfdd512a2019-11-22 11:03:14 -0800231 virtual bool requestCpusetBoost(bool enable) override {
232 return android::requestCpusetBoost(enable, mClientToken);
Chong Zhangdd726802019-08-21 17:24:13 -0700233 }
234
235protected:
236 virtual ~SystemCallbackImpl() {}
237
238private:
239 DISALLOW_EVIL_CONSTRUCTORS(SystemCallbackImpl);
Chong Zhangfdd512a2019-11-22 11:03:14 -0800240 sp<IBinder> mClientToken;
Chong Zhangdd726802019-08-21 17:24:13 -0700241};
242
243ResourceManagerService::ResourceManagerService()
244 : ResourceManagerService(new ProcessInfo(), new SystemCallbackImpl()) {}
245
246ResourceManagerService::ResourceManagerService(
247 const sp<ProcessInfoInterface> &processInfo,
248 const sp<SystemCallbackInterface> &systemResource)
Ronghua Wu231c3d12015-03-11 15:10:32 -0700249 : mProcessInfo(processInfo),
Chong Zhangdd726802019-08-21 17:24:13 -0700250 mSystemCB(systemResource),
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700251 mServiceLog(new ServiceLog()),
Ronghua Wu231c3d12015-03-11 15:10:32 -0700252 mSupportsMultipleSecureCodecs(true),
Chong Zhang79d2b282018-04-17 14:14:31 -0700253 mSupportsSecureWithNonSecureCodec(true),
Chong Zhangfdd512a2019-11-22 11:03:14 -0800254 mCpuBoostCount(0),
255 mDeathRecipient(AIBinder_DeathRecipient_new(DeathNotifier::BinderDiedCallback)) {
Chong Zhangdd726802019-08-21 17:24:13 -0700256 mSystemCB->noteResetVideo();
Chong Zhangee33d642019-08-08 14:26:43 -0700257}
Ronghua Wu231c3d12015-03-11 15:10:32 -0700258
Chong Zhangfdd512a2019-11-22 11:03:14 -0800259//static
260void ResourceManagerService::instantiate() {
261 std::shared_ptr<ResourceManagerService> service =
262 ::ndk::SharedRefBase::make<ResourceManagerService>();
263 binder_status_t status =
264 AServiceManager_addService(service->asBinder().get(), getServiceName());
265 if (status != STATUS_OK) {
266 return;
267 }
268 // TODO: mediaserver main() is already starting the thread pool,
269 // move this to mediaserver main() when other services in mediaserver
270 // are converted to ndk-platform aidl.
271 //ABinderProcess_startThreadPool();
272}
273
Ronghua Wu231c3d12015-03-11 15:10:32 -0700274ResourceManagerService::~ResourceManagerService() {}
275
Chong Zhang181e6952019-10-09 13:23:39 -0700276Status ResourceManagerService::config(const std::vector<MediaResourcePolicyParcel>& policies) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700277 String8 log = String8::format("config(%s)", getString(policies).string());
278 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700279
280 Mutex::Autolock lock(mLock);
281 for (size_t i = 0; i < policies.size(); ++i) {
Chong Zhang181e6952019-10-09 13:23:39 -0700282 const std::string &type = policies[i].type;
283 const std::string &value = policies[i].value;
284 if (type == MediaResourcePolicy::kPolicySupportsMultipleSecureCodecs()) {
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700285 mSupportsMultipleSecureCodecs = (value == "true");
Chong Zhang181e6952019-10-09 13:23:39 -0700286 } else if (type == MediaResourcePolicy::kPolicySupportsSecureWithNonSecureCodec()) {
Ronghua Wu9ba21b92015-04-21 14:23:06 -0700287 mSupportsSecureWithNonSecureCodec = (value == "true");
Ronghua Wu231c3d12015-03-11 15:10:32 -0700288 }
289 }
Chong Zhang181e6952019-10-09 13:23:39 -0700290 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700291}
292
Chong Zhangfb092d32019-08-12 09:45:44 -0700293void ResourceManagerService::onFirstAdded(
Chong Zhang181e6952019-10-09 13:23:39 -0700294 const MediaResourceParcel& resource, const ResourceInfo& clientInfo) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700295 // first time added
Chong Zhang181e6952019-10-09 13:23:39 -0700296 if (resource.type == MediaResource::Type::kCpuBoost
297 && resource.subType == MediaResource::SubType::kUnspecifiedSubType) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700298 // Request it on every new instance of kCpuBoost, as the media.codec
299 // could have died, if we only do it the first time subsequent instances
300 // never gets the boost.
Chong Zhangfdd512a2019-11-22 11:03:14 -0800301 if (mSystemCB->requestCpusetBoost(true) != OK) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700302 ALOGW("couldn't request cpuset boost");
303 }
304 mCpuBoostCount++;
Chong Zhang181e6952019-10-09 13:23:39 -0700305 } else if (resource.type == MediaResource::Type::kBattery
306 && resource.subType == MediaResource::SubType::kVideoCodec) {
Chong Zhangdd726802019-08-21 17:24:13 -0700307 mSystemCB->noteStartVideo(clientInfo.uid);
Chong Zhangfb092d32019-08-12 09:45:44 -0700308 }
309}
310
311void ResourceManagerService::onLastRemoved(
Chong Zhang181e6952019-10-09 13:23:39 -0700312 const MediaResourceParcel& resource, const ResourceInfo& clientInfo) {
313 if (resource.type == MediaResource::Type::kCpuBoost
314 && resource.subType == MediaResource::SubType::kUnspecifiedSubType
Chong Zhangfb092d32019-08-12 09:45:44 -0700315 && mCpuBoostCount > 0) {
316 if (--mCpuBoostCount == 0) {
Chong Zhangfdd512a2019-11-22 11:03:14 -0800317 mSystemCB->requestCpusetBoost(false);
Chong Zhangfb092d32019-08-12 09:45:44 -0700318 }
Chong Zhang181e6952019-10-09 13:23:39 -0700319 } else if (resource.type == MediaResource::Type::kBattery
320 && resource.subType == MediaResource::SubType::kVideoCodec) {
Chong Zhangdd726802019-08-21 17:24:13 -0700321 mSystemCB->noteStopVideo(clientInfo.uid);
Chong Zhangfb092d32019-08-12 09:45:44 -0700322 }
323}
324
Robert Shihc3af31b2019-09-20 21:45:01 -0700325void ResourceManagerService::mergeResources(
Chong Zhang181e6952019-10-09 13:23:39 -0700326 MediaResourceParcel& r1, const MediaResourceParcel& r2) {
327 // The resource entry on record is maintained to be in [0,INT64_MAX].
328 // Clamp if merging in the new resource value causes it to go out of bound.
329 // Note that the new resource value could be negative, eg.DrmSession, the
330 // value goes lower when the session is used more often. During reclaim
331 // the session with the highest value (lowest usage) would be closed.
332 if (r2.value < INT64_MAX - r1.value) {
333 r1.value += r2.value;
334 if (r1.value < 0) {
335 r1.value = 0;
336 }
Robert Shihc3af31b2019-09-20 21:45:01 -0700337 } else {
Chong Zhang181e6952019-10-09 13:23:39 -0700338 r1.value = INT64_MAX;
Robert Shihc3af31b2019-09-20 21:45:01 -0700339 }
340}
341
Chong Zhang181e6952019-10-09 13:23:39 -0700342Status ResourceManagerService::addResource(
343 int32_t pid,
344 int32_t uid,
Ronghua Wu231c3d12015-03-11 15:10:32 -0700345 int64_t clientId,
Chong Zhangfdd512a2019-11-22 11:03:14 -0800346 const std::shared_ptr<IResourceManagerClient>& client,
Chong Zhang181e6952019-10-09 13:23:39 -0700347 const std::vector<MediaResourceParcel>& resources) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700348 String8 log = String8::format("addResource(pid %d, clientId %lld, resources %s)",
Ronghua Wu231c3d12015-03-11 15:10:32 -0700349 pid, (long long) clientId, getString(resources).string());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700350 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700351
352 Mutex::Autolock lock(mLock);
Ronghua Wud11c43a2016-01-27 16:26:12 -0800353 if (!mProcessInfo->isValidPid(pid)) {
354 ALOGE("Rejected addResource call with invalid pid.");
Chong Zhang181e6952019-10-09 13:23:39 -0700355 return Status::fromServiceSpecificError(BAD_VALUE);
Ronghua Wud11c43a2016-01-27 16:26:12 -0800356 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700357 ResourceInfos& infos = getResourceInfosForEdit(pid, mMap);
Chong Zhangee33d642019-08-08 14:26:43 -0700358 ResourceInfo& info = getResourceInfoForEdit(uid, clientId, client, infos);
Chong Zhang79d2b282018-04-17 14:14:31 -0700359
360 for (size_t i = 0; i < resources.size(); ++i) {
Robert Shihc3af31b2019-09-20 21:45:01 -0700361 const auto &res = resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700362 const auto resType = std::tuple(res.type, res.subType, res.id);
363
364 if (res.value < 0 && res.type != MediaResource::Type::kDrmSession) {
365 ALOGW("Ignoring request to remove negative value of non-drm resource");
366 continue;
367 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700368 if (info.resources.find(resType) == info.resources.end()) {
Chong Zhang181e6952019-10-09 13:23:39 -0700369 if (res.value <= 0) {
370 // We can't init a new entry with negative value, although it's allowed
371 // to merge in negative values after the initial add.
372 ALOGW("Ignoring request to add new resource entry with value <= 0");
373 continue;
374 }
Robert Shihc3af31b2019-09-20 21:45:01 -0700375 onFirstAdded(res, info);
376 info.resources[resType] = res;
Chong Zhangfb092d32019-08-12 09:45:44 -0700377 } else {
Robert Shihc3af31b2019-09-20 21:45:01 -0700378 mergeResources(info.resources[resType], res);
Chong Zhang79d2b282018-04-17 14:14:31 -0700379 }
380 }
Robert Shihc3af31b2019-09-20 21:45:01 -0700381 if (info.deathNotifier == nullptr && client != nullptr) {
Chong Zhangfdd512a2019-11-22 11:03:14 -0800382 info.deathNotifier = new DeathNotifier(ref<ResourceManagerService>(), pid, clientId);
383 AIBinder_linkToDeath(client->asBinder().get(),
384 mDeathRecipient.get(), info.deathNotifier.get());
Wonsik Kim3e378962017-01-05 17:00:02 +0900385 }
Dongwon Kangfe508d32015-12-15 14:22:05 +0900386 notifyResourceGranted(pid, resources);
Chong Zhang181e6952019-10-09 13:23:39 -0700387 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700388}
389
Chong Zhang181e6952019-10-09 13:23:39 -0700390Status ResourceManagerService::removeResource(
391 int32_t pid, int64_t clientId,
392 const std::vector<MediaResourceParcel>& resources) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700393 String8 log = String8::format("removeResource(pid %d, clientId %lld, resources %s)",
394 pid, (long long) clientId, getString(resources).string());
395 mServiceLog->add(log);
396
397 Mutex::Autolock lock(mLock);
398 if (!mProcessInfo->isValidPid(pid)) {
399 ALOGE("Rejected removeResource call with invalid pid.");
Chong Zhang181e6952019-10-09 13:23:39 -0700400 return Status::fromServiceSpecificError(BAD_VALUE);
Chong Zhangfb092d32019-08-12 09:45:44 -0700401 }
402 ssize_t index = mMap.indexOfKey(pid);
403 if (index < 0) {
404 ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700405 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700406 }
407 ResourceInfos &infos = mMap.editValueAt(index);
408
409 index = infos.indexOfKey(clientId);
410 if (index < 0) {
411 ALOGV("removeResource: didn't find clientId %lld", (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700412 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700413 }
414
415 ResourceInfo &info = infos.editValueAt(index);
416
417 for (size_t i = 0; i < resources.size(); ++i) {
Robert Shihc3af31b2019-09-20 21:45:01 -0700418 const auto &res = resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700419 const auto resType = std::tuple(res.type, res.subType, res.id);
420
421 if (res.value < 0) {
422 ALOGW("Ignoring request to remove negative value of resource");
423 continue;
424 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700425 // ignore if we don't have it
426 if (info.resources.find(resType) != info.resources.end()) {
Chong Zhang181e6952019-10-09 13:23:39 -0700427 MediaResourceParcel &resource = info.resources[resType];
428 if (resource.value > res.value) {
429 resource.value -= res.value;
Chong Zhangfb092d32019-08-12 09:45:44 -0700430 } else {
Robert Shihc3af31b2019-09-20 21:45:01 -0700431 onLastRemoved(res, info);
Chong Zhangfb092d32019-08-12 09:45:44 -0700432 info.resources.erase(resType);
433 }
434 }
435 }
Chong Zhang181e6952019-10-09 13:23:39 -0700436 return Status::ok();
Chong Zhangfb092d32019-08-12 09:45:44 -0700437}
438
Chong Zhang181e6952019-10-09 13:23:39 -0700439Status ResourceManagerService::removeClient(int32_t pid, int64_t clientId) {
Wonsik Kim3e378962017-01-05 17:00:02 +0900440 removeResource(pid, clientId, true);
Chong Zhang181e6952019-10-09 13:23:39 -0700441 return Status::ok();
Wonsik Kim3e378962017-01-05 17:00:02 +0900442}
443
Chong Zhang181e6952019-10-09 13:23:39 -0700444Status ResourceManagerService::removeResource(int pid, int64_t clientId, bool checkValid) {
Ronghua Wu37c89242015-07-15 12:23:48 -0700445 String8 log = String8::format(
446 "removeResource(pid %d, clientId %lld)",
447 pid, (long long) clientId);
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700448 mServiceLog->add(log);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700449
450 Mutex::Autolock lock(mLock);
Wonsik Kim3e378962017-01-05 17:00:02 +0900451 if (checkValid && !mProcessInfo->isValidPid(pid)) {
Ronghua Wud11c43a2016-01-27 16:26:12 -0800452 ALOGE("Rejected removeResource call with invalid pid.");
Chong Zhang181e6952019-10-09 13:23:39 -0700453 return Status::fromServiceSpecificError(BAD_VALUE);
Ronghua Wud11c43a2016-01-27 16:26:12 -0800454 }
Ronghua Wu37c89242015-07-15 12:23:48 -0700455 ssize_t index = mMap.indexOfKey(pid);
456 if (index < 0) {
457 ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700458 return Status::ok();
Ronghua Wu37c89242015-07-15 12:23:48 -0700459 }
Ronghua Wu37c89242015-07-15 12:23:48 -0700460 ResourceInfos &infos = mMap.editValueAt(index);
Chong Zhangfb092d32019-08-12 09:45:44 -0700461
462 index = infos.indexOfKey(clientId);
463 if (index < 0) {
464 ALOGV("removeResource: didn't find clientId %lld", (long long) clientId);
Chong Zhang181e6952019-10-09 13:23:39 -0700465 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700466 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700467
468 const ResourceInfo &info = infos[index];
469 for (auto it = info.resources.begin(); it != info.resources.end(); it++) {
470 onLastRemoved(it->second, info);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700471 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700472
Chong Zhangfdd512a2019-11-22 11:03:14 -0800473 AIBinder_unlinkToDeath(info.client->asBinder().get(),
474 mDeathRecipient.get(), info.deathNotifier.get());
Chong Zhangfb092d32019-08-12 09:45:44 -0700475
476 infos.removeItemsAt(index);
Chong Zhang181e6952019-10-09 13:23:39 -0700477 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700478}
479
Ronghua Wu05d89f12015-07-07 16:47:42 -0700480void ResourceManagerService::getClientForResource_l(
Chong Zhangfdd512a2019-11-22 11:03:14 -0800481 int callingPid, const MediaResourceParcel *res,
482 Vector<std::shared_ptr<IResourceManagerClient>> *clients) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700483 if (res == NULL) {
484 return;
485 }
Chong Zhangfdd512a2019-11-22 11:03:14 -0800486 std::shared_ptr<IResourceManagerClient> client;
Chong Zhang181e6952019-10-09 13:23:39 -0700487 if (getLowestPriorityBiggestClient_l(callingPid, res->type, &client)) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700488 clients->push_back(client);
489 }
490}
491
Chong Zhang181e6952019-10-09 13:23:39 -0700492Status ResourceManagerService::reclaimResource(
493 int32_t callingPid,
494 const std::vector<MediaResourceParcel>& resources,
495 bool* _aidl_return) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700496 String8 log = String8::format("reclaimResource(callingPid %d, resources %s)",
Ronghua Wu231c3d12015-03-11 15:10:32 -0700497 callingPid, getString(resources).string());
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700498 mServiceLog->add(log);
Chong Zhang181e6952019-10-09 13:23:39 -0700499 *_aidl_return = false;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700500
Chong Zhangfdd512a2019-11-22 11:03:14 -0800501 Vector<std::shared_ptr<IResourceManagerClient>> clients;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700502 {
503 Mutex::Autolock lock(mLock);
Ronghua Wud11c43a2016-01-27 16:26:12 -0800504 if (!mProcessInfo->isValidPid(callingPid)) {
505 ALOGE("Rejected reclaimResource call with invalid callingPid.");
Chong Zhang181e6952019-10-09 13:23:39 -0700506 return Status::fromServiceSpecificError(BAD_VALUE);
Ronghua Wud11c43a2016-01-27 16:26:12 -0800507 }
Chong Zhang181e6952019-10-09 13:23:39 -0700508 const MediaResourceParcel *secureCodec = NULL;
509 const MediaResourceParcel *nonSecureCodec = NULL;
510 const MediaResourceParcel *graphicMemory = NULL;
511 const MediaResourceParcel *drmSession = NULL;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700512 for (size_t i = 0; i < resources.size(); ++i) {
Chong Zhang181e6952019-10-09 13:23:39 -0700513 MediaResource::Type type = resources[i].type;
514 if (resources[i].type == MediaResource::Type::kSecureCodec) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700515 secureCodec = &resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700516 } else if (type == MediaResource::Type::kNonSecureCodec) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700517 nonSecureCodec = &resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700518 } else if (type == MediaResource::Type::kGraphicMemory) {
Ronghua Wu05d89f12015-07-07 16:47:42 -0700519 graphicMemory = &resources[i];
Chong Zhang181e6952019-10-09 13:23:39 -0700520 } else if (type == MediaResource::Type::kDrmSession) {
Robert Shihc3af31b2019-09-20 21:45:01 -0700521 drmSession = &resources[i];
Ronghua Wu05d89f12015-07-07 16:47:42 -0700522 }
523 }
524
525 // first pass to handle secure/non-secure codec conflict
526 if (secureCodec != NULL) {
527 if (!mSupportsMultipleSecureCodecs) {
Chong Zhang181e6952019-10-09 13:23:39 -0700528 if (!getAllClients_l(callingPid, MediaResource::Type::kSecureCodec, &clients)) {
529 return Status::ok();
Ronghua Wu05d89f12015-07-07 16:47:42 -0700530 }
531 }
532 if (!mSupportsSecureWithNonSecureCodec) {
Chong Zhang181e6952019-10-09 13:23:39 -0700533 if (!getAllClients_l(callingPid, MediaResource::Type::kNonSecureCodec, &clients)) {
534 return Status::ok();
Ronghua Wu05d89f12015-07-07 16:47:42 -0700535 }
536 }
537 }
538 if (nonSecureCodec != NULL) {
539 if (!mSupportsSecureWithNonSecureCodec) {
Chong Zhang181e6952019-10-09 13:23:39 -0700540 if (!getAllClients_l(callingPid, MediaResource::Type::kSecureCodec, &clients)) {
541 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700542 }
543 }
544 }
Robert Shihc3af31b2019-09-20 21:45:01 -0700545 if (drmSession != NULL) {
546 getClientForResource_l(callingPid, drmSession, &clients);
547 if (clients.size() == 0) {
Chong Zhang181e6952019-10-09 13:23:39 -0700548 return Status::ok();
Robert Shihc3af31b2019-09-20 21:45:01 -0700549 }
550 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700551
552 if (clients.size() == 0) {
553 // if no secure/non-secure codec conflict, run second pass to handle other resources.
Ronghua Wu05d89f12015-07-07 16:47:42 -0700554 getClientForResource_l(callingPid, graphicMemory, &clients);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700555 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700556
557 if (clients.size() == 0) {
558 // if we are here, run the third pass to free one codec with the same type.
Ronghua Wu05d89f12015-07-07 16:47:42 -0700559 getClientForResource_l(callingPid, secureCodec, &clients);
560 getClientForResource_l(callingPid, nonSecureCodec, &clients);
561 }
562
563 if (clients.size() == 0) {
564 // if we are here, run the fourth pass to free one codec with the different type.
565 if (secureCodec != NULL) {
Chong Zhang181e6952019-10-09 13:23:39 -0700566 MediaResource temp(MediaResource::Type::kNonSecureCodec, 1);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700567 getClientForResource_l(callingPid, &temp, &clients);
568 }
569 if (nonSecureCodec != NULL) {
Chong Zhang181e6952019-10-09 13:23:39 -0700570 MediaResource temp(MediaResource::Type::kSecureCodec, 1);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700571 getClientForResource_l(callingPid, &temp, &clients);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700572 }
573 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700574 }
575
576 if (clients.size() == 0) {
Chong Zhang181e6952019-10-09 13:23:39 -0700577 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700578 }
579
Chong Zhangfdd512a2019-11-22 11:03:14 -0800580 std::shared_ptr<IResourceManagerClient> failedClient;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700581 for (size_t i = 0; i < clients.size(); ++i) {
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700582 log = String8::format("reclaimResource from client %p", clients[i].get());
583 mServiceLog->add(log);
Chong Zhang181e6952019-10-09 13:23:39 -0700584 bool success;
585 Status status = clients[i]->reclaimResource(&success);
586 if (!status.isOk() || !success) {
Ronghua Wu67e7f542015-03-13 10:47:08 -0700587 failedClient = clients[i];
588 break;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700589 }
590 }
Ronghua Wu67e7f542015-03-13 10:47:08 -0700591
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700592 if (failedClient == NULL) {
Chong Zhang181e6952019-10-09 13:23:39 -0700593 *_aidl_return = true;
594 return Status::ok();
Ronghua Wu76d4c7f2015-10-23 15:01:53 -0700595 }
596
Ronghua Wu67e7f542015-03-13 10:47:08 -0700597 {
598 Mutex::Autolock lock(mLock);
599 bool found = false;
600 for (size_t i = 0; i < mMap.size(); ++i) {
601 ResourceInfos &infos = mMap.editValueAt(i);
602 for (size_t j = 0; j < infos.size();) {
603 if (infos[j].client == failedClient) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700604 j = infos.removeItemsAt(j);
Ronghua Wu67e7f542015-03-13 10:47:08 -0700605 found = true;
606 } else {
607 ++j;
608 }
609 }
610 if (found) {
611 break;
612 }
613 }
614 if (!found) {
615 ALOGV("didn't find failed client");
616 }
617 }
618
Chong Zhang181e6952019-10-09 13:23:39 -0700619 return Status::ok();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700620}
621
Henry Fang32762922020-01-28 18:40:39 -0800622Status ResourceManagerService::overridePid(
623 int originalPid,
624 int newPid) {
625 String8 log = String8::format("overridePid(originalPid %d, newPid %d)",
626 originalPid, newPid);
627 mServiceLog->add(log);
628
629 // allow if this is called from the same process or the process has
630 // permission.
631 if ((AIBinder_getCallingPid() != getpid()) &&
632 (checkCallingPermission(String16(
633 "android.permission.MEDIA_RESOURCE_OVERRIDE_PID")) == false)) {
634 ALOGE(
635 "Permission Denial: can't access overridePid method from pid=%d, "
636 "self pid=%d\n",
637 AIBinder_getCallingPid(), getpid());
638 return Status::fromServiceSpecificError(PERMISSION_DENIED);
639 }
640
641 {
642 Mutex::Autolock lock(mLock);
643 mOverridePidMap.erase(originalPid);
644 if (newPid != -1) {
645 mOverridePidMap.emplace(originalPid, newPid);
646 }
647 }
648
649 return Status::ok();
650}
651
Wonsik Kimd20e9362020-04-28 10:42:57 -0700652Status ResourceManagerService::markClientForPendingRemoval(int32_t pid, int64_t clientId) {
653 String8 log = String8::format(
654 "markClientForPendingRemoval(pid %d, clientId %lld)",
655 pid, (long long) clientId);
656 mServiceLog->add(log);
657
658 Mutex::Autolock lock(mLock);
659 if (!mProcessInfo->isValidPid(pid)) {
660 ALOGE("Rejected markClientForPendingRemoval call with invalid pid.");
661 return Status::fromServiceSpecificError(BAD_VALUE);
662 }
663 ssize_t index = mMap.indexOfKey(pid);
664 if (index < 0) {
665 ALOGV("markClientForPendingRemoval: didn't find pid %d for clientId %lld",
666 pid, (long long)clientId);
667 return Status::ok();
668 }
669 ResourceInfos &infos = mMap.editValueAt(index);
670
671 index = infos.indexOfKey(clientId);
672 if (index < 0) {
673 ALOGV("markClientForPendingRemoval: didn't find clientId %lld", (long long) clientId);
674 return Status::ok();
675 }
676
677 ResourceInfo &info = infos.editValueAt(index);
678 info.pendingRemoval = true;
679 return Status::ok();
680}
681
Henry Fang32762922020-01-28 18:40:39 -0800682bool ResourceManagerService::getPriority_l(int pid, int* priority) {
683 int newPid = pid;
684
685 if (mOverridePidMap.find(pid) != mOverridePidMap.end()) {
686 newPid = mOverridePidMap[pid];
687 ALOGD("getPriority_l: use override pid %d instead original pid %d",
688 newPid, pid);
689 }
690
691 return mProcessInfo->getPriority(newPid, priority);
692}
693
Ronghua Wu231c3d12015-03-11 15:10:32 -0700694bool ResourceManagerService::getAllClients_l(
Chong Zhangfdd512a2019-11-22 11:03:14 -0800695 int callingPid, MediaResource::Type type,
696 Vector<std::shared_ptr<IResourceManagerClient>> *clients) {
697 Vector<std::shared_ptr<IResourceManagerClient>> temp;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700698 for (size_t i = 0; i < mMap.size(); ++i) {
699 ResourceInfos &infos = mMap.editValueAt(i);
700 for (size_t j = 0; j < infos.size(); ++j) {
701 if (hasResourceType(type, infos[j].resources)) {
702 if (!isCallingPriorityHigher_l(callingPid, mMap.keyAt(i))) {
703 // some higher/equal priority process owns the resource,
704 // this request can't be fulfilled.
705 ALOGE("getAllClients_l: can't reclaim resource %s from pid %d",
Ronghua Wuea15fd22016-03-03 13:35:05 -0800706 asString(type), mMap.keyAt(i));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700707 return false;
708 }
709 temp.push_back(infos[j].client);
710 }
711 }
712 }
713 if (temp.size() == 0) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800714 ALOGV("getAllClients_l: didn't find any resource %s", asString(type));
Ronghua Wu231c3d12015-03-11 15:10:32 -0700715 return true;
716 }
717 clients->appendVector(temp);
718 return true;
719}
720
721bool ResourceManagerService::getLowestPriorityBiggestClient_l(
Chong Zhangfdd512a2019-11-22 11:03:14 -0800722 int callingPid, MediaResource::Type type,
723 std::shared_ptr<IResourceManagerClient> *client) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700724 int lowestPriorityPid;
725 int lowestPriority;
726 int callingPriority;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700727
728 // Before looking into other processes, check if we have clients marked for
729 // pending removal in the same process.
730 if (getBiggestClient_l(callingPid, type, client, true /* pendingRemovalOnly */)) {
731 return true;
732 }
Henry Fang32762922020-01-28 18:40:39 -0800733 if (!getPriority_l(callingPid, &callingPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700734 ALOGE("getLowestPriorityBiggestClient_l: can't get process priority for pid %d",
735 callingPid);
736 return false;
737 }
738 if (!getLowestPriorityPid_l(type, &lowestPriorityPid, &lowestPriority)) {
739 return false;
740 }
741 if (lowestPriority <= callingPriority) {
742 ALOGE("getLowestPriorityBiggestClient_l: lowest priority %d vs caller priority %d",
743 lowestPriority, callingPriority);
744 return false;
745 }
746
747 if (!getBiggestClient_l(lowestPriorityPid, type, client)) {
748 return false;
749 }
750 return true;
751}
752
753bool ResourceManagerService::getLowestPriorityPid_l(
Ronghua Wuea15fd22016-03-03 13:35:05 -0800754 MediaResource::Type type, int *lowestPriorityPid, int *lowestPriority) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700755 int pid = -1;
756 int priority = -1;
757 for (size_t i = 0; i < mMap.size(); ++i) {
758 if (mMap.valueAt(i).size() == 0) {
759 // no client on this process.
760 continue;
761 }
762 if (!hasResourceType(type, mMap.valueAt(i))) {
763 // doesn't have the requested resource type
764 continue;
765 }
766 int tempPid = mMap.keyAt(i);
767 int tempPriority;
Henry Fang32762922020-01-28 18:40:39 -0800768 if (!getPriority_l(tempPid, &tempPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700769 ALOGV("getLowestPriorityPid_l: can't get priority of pid %d, skipped", tempPid);
770 // TODO: remove this pid from mMap?
771 continue;
772 }
773 if (pid == -1 || tempPriority > priority) {
774 // initial the value
775 pid = tempPid;
776 priority = tempPriority;
777 }
778 }
779 if (pid != -1) {
780 *lowestPriorityPid = pid;
781 *lowestPriority = priority;
782 }
783 return (pid != -1);
784}
785
786bool ResourceManagerService::isCallingPriorityHigher_l(int callingPid, int pid) {
787 int callingPidPriority;
Henry Fang32762922020-01-28 18:40:39 -0800788 if (!getPriority_l(callingPid, &callingPidPriority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700789 return false;
790 }
791
792 int priority;
Henry Fang32762922020-01-28 18:40:39 -0800793 if (!getPriority_l(pid, &priority)) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700794 return false;
795 }
796
797 return (callingPidPriority < priority);
798}
799
800bool ResourceManagerService::getBiggestClient_l(
Wonsik Kimd20e9362020-04-28 10:42:57 -0700801 int pid, MediaResource::Type type, std::shared_ptr<IResourceManagerClient> *client,
802 bool pendingRemovalOnly) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700803 ssize_t index = mMap.indexOfKey(pid);
804 if (index < 0) {
805 ALOGE("getBiggestClient_l: can't find resource info for pid %d", pid);
806 return false;
807 }
808
Chong Zhangfdd512a2019-11-22 11:03:14 -0800809 std::shared_ptr<IResourceManagerClient> clientTemp;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700810 uint64_t largestValue = 0;
811 const ResourceInfos &infos = mMap.valueAt(index);
812 for (size_t i = 0; i < infos.size(); ++i) {
Chong Zhangfb092d32019-08-12 09:45:44 -0700813 const ResourceList &resources = infos[i].resources;
Wonsik Kimd20e9362020-04-28 10:42:57 -0700814 if (pendingRemovalOnly && !infos[i].pendingRemoval) {
815 continue;
816 }
Chong Zhangfb092d32019-08-12 09:45:44 -0700817 for (auto it = resources.begin(); it != resources.end(); it++) {
Chong Zhang181e6952019-10-09 13:23:39 -0700818 const MediaResourceParcel &resource = it->second;
819 if (resource.type == type) {
820 if (resource.value > largestValue) {
821 largestValue = resource.value;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700822 clientTemp = infos[i].client;
823 }
824 }
825 }
826 }
827
828 if (clientTemp == NULL) {
Ronghua Wuea15fd22016-03-03 13:35:05 -0800829 ALOGE("getBiggestClient_l: can't find resource type %s for pid %d", asString(type), pid);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700830 return false;
831 }
832
833 *client = clientTemp;
834 return true;
835}
836
Ronghua Wu231c3d12015-03-11 15:10:32 -0700837} // namespace android