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 | //#define LOG_NDEBUG 0 |
| 19 | #define LOG_TAG "ResourceManagerService" |
| 20 | #include <utils/Log.h> |
| 21 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 22 | #include <android/binder_manager.h> |
| 23 | #include <android/binder_process.h> |
Dongwon Kang | fe508d3 | 2015-12-15 14:22:05 +0900 | [diff] [blame] | 24 | #include <binder/IMediaResourceMonitor.h> |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 25 | #include <binder/IServiceManager.h> |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 26 | #include <cutils/sched_policy.h> |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 27 | #include <dirent.h> |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 28 | #include <media/MediaResourcePolicy.h> |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 29 | #include <media/stagefright/ProcessInfo.h> |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 30 | #include <mediautils/BatteryNotifier.h> |
| 31 | #include <mediautils/SchedulingPolicyService.h> |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 32 | #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 Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 39 | #include "ServiceLog.h" |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 40 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 41 | namespace android { |
| 42 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 43 | DeathNotifier::DeathNotifier(const std::shared_ptr<ResourceManagerService> &service, |
| 44 | int pid, int64_t clientId) |
| 45 | : mService(service), mPid(pid), mClientId(clientId) {} |
Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 46 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 47 | //static |
| 48 | void DeathNotifier::BinderDiedCallback(void* cookie) { |
| 49 | auto thiz = static_cast<DeathNotifier*>(cookie); |
| 50 | thiz->binderDied(); |
| 51 | } |
Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 52 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 53 | void 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 Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 59 | } |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 60 | service->removeResource(mPid, mClientId, false); |
| 61 | } |
Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 62 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 63 | template <typename T> |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 64 | static String8 getString(const std::vector<T> &items) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 65 | String8 itemsStr; |
| 66 | for (size_t i = 0; i < items.size(); ++i) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 67 | itemsStr.appendFormat("%s ", toString(items[i]).string()); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 68 | } |
| 69 | return itemsStr; |
| 70 | } |
| 71 | |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 72 | static bool hasResourceType(MediaResource::Type type, const ResourceList& resources) { |
| 73 | for (auto it = resources.begin(); it != resources.end(); it++) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 74 | if (it->second.type == type) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 75 | return true; |
| 76 | } |
| 77 | } |
| 78 | return false; |
| 79 | } |
| 80 | |
Chih-Hung Hsieh | 51873d8 | 2016-08-09 14:18:51 -0700 | [diff] [blame] | 81 | static bool hasResourceType(MediaResource::Type type, const ResourceInfos& infos) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 82 | for (size_t i = 0; i < infos.size(); ++i) { |
| 83 | if (hasResourceType(type, infos[i].resources)) { |
| 84 | return true; |
| 85 | } |
| 86 | } |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | static ResourceInfos& getResourceInfosForEdit( |
| 91 | int pid, |
| 92 | PidResourceInfosMap& map) { |
| 93 | ssize_t index = map.indexOfKey(pid); |
| 94 | if (index < 0) { |
| 95 | // new pid |
| 96 | ResourceInfos infosForPid; |
| 97 | map.add(pid, infosForPid); |
| 98 | } |
| 99 | |
| 100 | return map.editValueFor(pid); |
| 101 | } |
| 102 | |
| 103 | static ResourceInfo& getResourceInfoForEdit( |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 104 | uid_t uid, |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 105 | int64_t clientId, |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 106 | const std::shared_ptr<IResourceManagerClient>& client, |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 107 | ResourceInfos& infos) { |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 108 | ssize_t index = infos.indexOfKey(clientId); |
| 109 | |
| 110 | if (index < 0) { |
| 111 | ResourceInfo info; |
| 112 | info.uid = uid; |
| 113 | info.clientId = clientId; |
| 114 | info.client = client; |
| 115 | |
| 116 | index = infos.add(clientId, info); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 117 | } |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 118 | |
| 119 | return infos.editValueAt(index); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 122 | static void notifyResourceGranted(int pid, const std::vector<MediaResourceParcel> &resources) { |
Dongwon Kang | fe508d3 | 2015-12-15 14:22:05 +0900 | [diff] [blame] | 123 | static const char* const kServiceName = "media_resource_monitor"; |
Dongwon Kang | 2642c84 | 2016-03-23 18:07:29 -0700 | [diff] [blame] | 124 | sp<IBinder> binder = defaultServiceManager()->checkService(String16(kServiceName)); |
Dongwon Kang | fe508d3 | 2015-12-15 14:22:05 +0900 | [diff] [blame] | 125 | if (binder != NULL) { |
| 126 | sp<IMediaResourceMonitor> service = interface_cast<IMediaResourceMonitor>(binder); |
| 127 | for (size_t i = 0; i < resources.size(); ++i) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 128 | if (resources[i].subType == MediaResource::SubType::kAudioCodec) { |
Dongwon Kang | 69c23dd | 2016-03-22 15:22:45 -0700 | [diff] [blame] | 129 | service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_AUDIO_CODEC); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 130 | } else if (resources[i].subType == MediaResource::SubType::kVideoCodec) { |
Dongwon Kang | 69c23dd | 2016-03-22 15:22:45 -0700 | [diff] [blame] | 131 | service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_VIDEO_CODEC); |
| 132 | } |
Dongwon Kang | fe508d3 | 2015-12-15 14:22:05 +0900 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 137 | binder_status_t ResourceManagerService::dump( |
| 138 | int fd, const char** /*args*/, uint32_t /*numArgs*/) { |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 139 | String8 result; |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 140 | |
dcashman | 014e91e | 2015-09-11 09:33:01 -0700 | [diff] [blame] | 141 | if (checkCallingPermission(String16("android.permission.DUMP")) == false) { |
| 142 | result.format("Permission Denial: " |
| 143 | "can't dump ResourceManagerService from pid=%d, uid=%d\n", |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 144 | AIBinder_getCallingPid(), |
| 145 | AIBinder_getCallingUid()); |
dcashman | 014e91e | 2015-09-11 09:33:01 -0700 | [diff] [blame] | 146 | write(fd, result.string(), result.size()); |
| 147 | return PERMISSION_DENIED; |
| 148 | } |
| 149 | |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 150 | PidResourceInfosMap mapCopy; |
| 151 | bool supportsMultipleSecureCodecs; |
| 152 | bool supportsSecureWithNonSecureCodec; |
| 153 | String8 serviceLog; |
| 154 | { |
| 155 | Mutex::Autolock lock(mLock); |
| 156 | mapCopy = mMap; // Shadow copy, real copy will happen on write. |
| 157 | supportsMultipleSecureCodecs = mSupportsMultipleSecureCodecs; |
| 158 | supportsSecureWithNonSecureCodec = mSupportsSecureWithNonSecureCodec; |
| 159 | serviceLog = mServiceLog->toString(" " /* linePrefix */); |
| 160 | } |
| 161 | |
| 162 | const size_t SIZE = 256; |
| 163 | char buffer[SIZE]; |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 164 | snprintf(buffer, SIZE, "ResourceManagerService: %p\n", this); |
| 165 | result.append(buffer); |
| 166 | result.append(" Policies:\n"); |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 167 | snprintf(buffer, SIZE, " SupportsMultipleSecureCodecs: %d\n", supportsMultipleSecureCodecs); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 168 | result.append(buffer); |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 169 | snprintf(buffer, SIZE, " SupportsSecureWithNonSecureCodec: %d\n", |
| 170 | supportsSecureWithNonSecureCodec); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 171 | result.append(buffer); |
| 172 | |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 173 | result.append(" Processes:\n"); |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 174 | for (size_t i = 0; i < mapCopy.size(); ++i) { |
| 175 | snprintf(buffer, SIZE, " Pid: %d\n", mapCopy.keyAt(i)); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 176 | result.append(buffer); |
| 177 | |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 178 | const ResourceInfos &infos = mapCopy.valueAt(i); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 179 | for (size_t j = 0; j < infos.size(); ++j) { |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 180 | result.append(" Client:\n"); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 181 | snprintf(buffer, SIZE, " Id: %lld\n", (long long)infos[j].clientId); |
| 182 | result.append(buffer); |
| 183 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 184 | std::string clientName; |
| 185 | Status status = infos[j].client->getName(&clientName); |
| 186 | if (!status.isOk()) { |
| 187 | clientName = "<unknown client>"; |
| 188 | } |
| 189 | snprintf(buffer, SIZE, " Name: %s\n", clientName.c_str()); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 190 | result.append(buffer); |
| 191 | |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 192 | const ResourceList &resources = infos[j].resources; |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 193 | result.append(" Resources:\n"); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 194 | for (auto it = resources.begin(); it != resources.end(); it++) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 195 | snprintf(buffer, SIZE, " %s\n", toString(it->second).string()); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 196 | result.append(buffer); |
| 197 | } |
| 198 | } |
| 199 | } |
Ronghua Wu | 022ed72 | 2015-05-11 15:15:09 -0700 | [diff] [blame] | 200 | result.append(" Events logs (most recent at top):\n"); |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 201 | result.append(serviceLog); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 202 | |
| 203 | write(fd, result.string(), result.size()); |
| 204 | return OK; |
| 205 | } |
| 206 | |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 207 | struct SystemCallbackImpl : |
| 208 | public ResourceManagerService::SystemCallbackInterface { |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 209 | SystemCallbackImpl() : mClientToken(new BBinder()) {} |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 210 | |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 211 | virtual void noteStartVideo(int uid) override { |
| 212 | BatteryNotifier::getInstance().noteStartVideo(uid); |
| 213 | } |
| 214 | virtual void noteStopVideo(int uid) override { |
| 215 | BatteryNotifier::getInstance().noteStopVideo(uid); |
| 216 | } |
| 217 | virtual void noteResetVideo() override { |
| 218 | BatteryNotifier::getInstance().noteResetVideo(); |
| 219 | } |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 220 | virtual bool requestCpusetBoost(bool enable) override { |
| 221 | return android::requestCpusetBoost(enable, mClientToken); |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | protected: |
| 225 | virtual ~SystemCallbackImpl() {} |
| 226 | |
| 227 | private: |
| 228 | DISALLOW_EVIL_CONSTRUCTORS(SystemCallbackImpl); |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 229 | sp<IBinder> mClientToken; |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 230 | }; |
| 231 | |
| 232 | ResourceManagerService::ResourceManagerService() |
| 233 | : ResourceManagerService(new ProcessInfo(), new SystemCallbackImpl()) {} |
| 234 | |
| 235 | ResourceManagerService::ResourceManagerService( |
| 236 | const sp<ProcessInfoInterface> &processInfo, |
| 237 | const sp<SystemCallbackInterface> &systemResource) |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 238 | : mProcessInfo(processInfo), |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 239 | mSystemCB(systemResource), |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 240 | mServiceLog(new ServiceLog()), |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 241 | mSupportsMultipleSecureCodecs(true), |
Chong Zhang | 79d2b28 | 2018-04-17 14:14:31 -0700 | [diff] [blame] | 242 | mSupportsSecureWithNonSecureCodec(true), |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 243 | mCpuBoostCount(0), |
| 244 | mDeathRecipient(AIBinder_DeathRecipient_new(DeathNotifier::BinderDiedCallback)) { |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 245 | mSystemCB->noteResetVideo(); |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 246 | } |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 247 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 248 | //static |
| 249 | void ResourceManagerService::instantiate() { |
| 250 | std::shared_ptr<ResourceManagerService> service = |
| 251 | ::ndk::SharedRefBase::make<ResourceManagerService>(); |
| 252 | binder_status_t status = |
| 253 | AServiceManager_addService(service->asBinder().get(), getServiceName()); |
| 254 | if (status != STATUS_OK) { |
| 255 | return; |
| 256 | } |
| 257 | // TODO: mediaserver main() is already starting the thread pool, |
| 258 | // move this to mediaserver main() when other services in mediaserver |
| 259 | // are converted to ndk-platform aidl. |
| 260 | //ABinderProcess_startThreadPool(); |
| 261 | } |
| 262 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 263 | ResourceManagerService::~ResourceManagerService() {} |
| 264 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 265 | Status ResourceManagerService::config(const std::vector<MediaResourcePolicyParcel>& policies) { |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 266 | String8 log = String8::format("config(%s)", getString(policies).string()); |
| 267 | mServiceLog->add(log); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 268 | |
| 269 | Mutex::Autolock lock(mLock); |
| 270 | for (size_t i = 0; i < policies.size(); ++i) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 271 | const std::string &type = policies[i].type; |
| 272 | const std::string &value = policies[i].value; |
| 273 | if (type == MediaResourcePolicy::kPolicySupportsMultipleSecureCodecs()) { |
Ronghua Wu | 9ba21b9 | 2015-04-21 14:23:06 -0700 | [diff] [blame] | 274 | mSupportsMultipleSecureCodecs = (value == "true"); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 275 | } else if (type == MediaResourcePolicy::kPolicySupportsSecureWithNonSecureCodec()) { |
Ronghua Wu | 9ba21b9 | 2015-04-21 14:23:06 -0700 | [diff] [blame] | 276 | mSupportsSecureWithNonSecureCodec = (value == "true"); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 277 | } |
| 278 | } |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 279 | return Status::ok(); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 282 | void ResourceManagerService::onFirstAdded( |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 283 | const MediaResourceParcel& resource, const ResourceInfo& clientInfo) { |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 284 | // first time added |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 285 | if (resource.type == MediaResource::Type::kCpuBoost |
| 286 | && resource.subType == MediaResource::SubType::kUnspecifiedSubType) { |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 287 | // Request it on every new instance of kCpuBoost, as the media.codec |
| 288 | // could have died, if we only do it the first time subsequent instances |
| 289 | // never gets the boost. |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 290 | if (mSystemCB->requestCpusetBoost(true) != OK) { |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 291 | ALOGW("couldn't request cpuset boost"); |
| 292 | } |
| 293 | mCpuBoostCount++; |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 294 | } else if (resource.type == MediaResource::Type::kBattery |
| 295 | && resource.subType == MediaResource::SubType::kVideoCodec) { |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 296 | mSystemCB->noteStartVideo(clientInfo.uid); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
| 300 | void ResourceManagerService::onLastRemoved( |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 301 | const MediaResourceParcel& resource, const ResourceInfo& clientInfo) { |
| 302 | if (resource.type == MediaResource::Type::kCpuBoost |
| 303 | && resource.subType == MediaResource::SubType::kUnspecifiedSubType |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 304 | && mCpuBoostCount > 0) { |
| 305 | if (--mCpuBoostCount == 0) { |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 306 | mSystemCB->requestCpusetBoost(false); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 307 | } |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 308 | } else if (resource.type == MediaResource::Type::kBattery |
| 309 | && resource.subType == MediaResource::SubType::kVideoCodec) { |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 310 | mSystemCB->noteStopVideo(clientInfo.uid); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 314 | void ResourceManagerService::mergeResources( |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 315 | MediaResourceParcel& r1, const MediaResourceParcel& r2) { |
| 316 | // The resource entry on record is maintained to be in [0,INT64_MAX]. |
| 317 | // Clamp if merging in the new resource value causes it to go out of bound. |
| 318 | // Note that the new resource value could be negative, eg.DrmSession, the |
| 319 | // value goes lower when the session is used more often. During reclaim |
| 320 | // the session with the highest value (lowest usage) would be closed. |
| 321 | if (r2.value < INT64_MAX - r1.value) { |
| 322 | r1.value += r2.value; |
| 323 | if (r1.value < 0) { |
| 324 | r1.value = 0; |
| 325 | } |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 326 | } else { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 327 | r1.value = INT64_MAX; |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 331 | Status ResourceManagerService::addResource( |
| 332 | int32_t pid, |
| 333 | int32_t uid, |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 334 | int64_t clientId, |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 335 | const std::shared_ptr<IResourceManagerClient>& client, |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 336 | const std::vector<MediaResourceParcel>& resources) { |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 337 | String8 log = String8::format("addResource(pid %d, clientId %lld, resources %s)", |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 338 | pid, (long long) clientId, getString(resources).string()); |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 339 | mServiceLog->add(log); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 340 | |
| 341 | Mutex::Autolock lock(mLock); |
Ronghua Wu | d11c43a | 2016-01-27 16:26:12 -0800 | [diff] [blame] | 342 | if (!mProcessInfo->isValidPid(pid)) { |
| 343 | ALOGE("Rejected addResource call with invalid pid."); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 344 | return Status::fromServiceSpecificError(BAD_VALUE); |
Ronghua Wu | d11c43a | 2016-01-27 16:26:12 -0800 | [diff] [blame] | 345 | } |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 346 | ResourceInfos& infos = getResourceInfosForEdit(pid, mMap); |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 347 | ResourceInfo& info = getResourceInfoForEdit(uid, clientId, client, infos); |
Chong Zhang | 79d2b28 | 2018-04-17 14:14:31 -0700 | [diff] [blame] | 348 | |
| 349 | for (size_t i = 0; i < resources.size(); ++i) { |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 350 | const auto &res = resources[i]; |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 351 | const auto resType = std::tuple(res.type, res.subType, res.id); |
| 352 | |
| 353 | if (res.value < 0 && res.type != MediaResource::Type::kDrmSession) { |
| 354 | ALOGW("Ignoring request to remove negative value of non-drm resource"); |
| 355 | continue; |
| 356 | } |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 357 | if (info.resources.find(resType) == info.resources.end()) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 358 | if (res.value <= 0) { |
| 359 | // We can't init a new entry with negative value, although it's allowed |
| 360 | // to merge in negative values after the initial add. |
| 361 | ALOGW("Ignoring request to add new resource entry with value <= 0"); |
| 362 | continue; |
| 363 | } |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 364 | onFirstAdded(res, info); |
| 365 | info.resources[resType] = res; |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 366 | } else { |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 367 | mergeResources(info.resources[resType], res); |
Chong Zhang | 79d2b28 | 2018-04-17 14:14:31 -0700 | [diff] [blame] | 368 | } |
| 369 | } |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 370 | if (info.deathNotifier == nullptr && client != nullptr) { |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 371 | info.deathNotifier = new DeathNotifier(ref<ResourceManagerService>(), pid, clientId); |
| 372 | AIBinder_linkToDeath(client->asBinder().get(), |
| 373 | mDeathRecipient.get(), info.deathNotifier.get()); |
Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 374 | } |
Dongwon Kang | fe508d3 | 2015-12-15 14:22:05 +0900 | [diff] [blame] | 375 | notifyResourceGranted(pid, resources); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 376 | return Status::ok(); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 377 | } |
| 378 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 379 | Status ResourceManagerService::removeResource( |
| 380 | int32_t pid, int64_t clientId, |
| 381 | const std::vector<MediaResourceParcel>& resources) { |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 382 | String8 log = String8::format("removeResource(pid %d, clientId %lld, resources %s)", |
| 383 | pid, (long long) clientId, getString(resources).string()); |
| 384 | mServiceLog->add(log); |
| 385 | |
| 386 | Mutex::Autolock lock(mLock); |
| 387 | if (!mProcessInfo->isValidPid(pid)) { |
| 388 | ALOGE("Rejected removeResource call with invalid pid."); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 389 | return Status::fromServiceSpecificError(BAD_VALUE); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 390 | } |
| 391 | ssize_t index = mMap.indexOfKey(pid); |
| 392 | if (index < 0) { |
| 393 | ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 394 | return Status::ok(); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 395 | } |
| 396 | ResourceInfos &infos = mMap.editValueAt(index); |
| 397 | |
| 398 | index = infos.indexOfKey(clientId); |
| 399 | if (index < 0) { |
| 400 | ALOGV("removeResource: didn't find clientId %lld", (long long) clientId); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 401 | return Status::ok(); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | ResourceInfo &info = infos.editValueAt(index); |
| 405 | |
| 406 | for (size_t i = 0; i < resources.size(); ++i) { |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 407 | const auto &res = resources[i]; |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 408 | const auto resType = std::tuple(res.type, res.subType, res.id); |
| 409 | |
| 410 | if (res.value < 0) { |
| 411 | ALOGW("Ignoring request to remove negative value of resource"); |
| 412 | continue; |
| 413 | } |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 414 | // ignore if we don't have it |
| 415 | if (info.resources.find(resType) != info.resources.end()) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 416 | MediaResourceParcel &resource = info.resources[resType]; |
| 417 | if (resource.value > res.value) { |
| 418 | resource.value -= res.value; |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 419 | } else { |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 420 | onLastRemoved(res, info); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 421 | info.resources.erase(resType); |
| 422 | } |
| 423 | } |
| 424 | } |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 425 | return Status::ok(); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 428 | Status ResourceManagerService::removeClient(int32_t pid, int64_t clientId) { |
Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 429 | removeResource(pid, clientId, true); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 430 | return Status::ok(); |
Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 431 | } |
| 432 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 433 | Status ResourceManagerService::removeResource(int pid, int64_t clientId, bool checkValid) { |
Ronghua Wu | 37c8924 | 2015-07-15 12:23:48 -0700 | [diff] [blame] | 434 | String8 log = String8::format( |
| 435 | "removeResource(pid %d, clientId %lld)", |
| 436 | pid, (long long) clientId); |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 437 | mServiceLog->add(log); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 438 | |
| 439 | Mutex::Autolock lock(mLock); |
Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 440 | if (checkValid && !mProcessInfo->isValidPid(pid)) { |
Ronghua Wu | d11c43a | 2016-01-27 16:26:12 -0800 | [diff] [blame] | 441 | ALOGE("Rejected removeResource call with invalid pid."); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 442 | return Status::fromServiceSpecificError(BAD_VALUE); |
Ronghua Wu | d11c43a | 2016-01-27 16:26:12 -0800 | [diff] [blame] | 443 | } |
Ronghua Wu | 37c8924 | 2015-07-15 12:23:48 -0700 | [diff] [blame] | 444 | ssize_t index = mMap.indexOfKey(pid); |
| 445 | if (index < 0) { |
| 446 | ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 447 | return Status::ok(); |
Ronghua Wu | 37c8924 | 2015-07-15 12:23:48 -0700 | [diff] [blame] | 448 | } |
Ronghua Wu | 37c8924 | 2015-07-15 12:23:48 -0700 | [diff] [blame] | 449 | ResourceInfos &infos = mMap.editValueAt(index); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 450 | |
| 451 | index = infos.indexOfKey(clientId); |
| 452 | if (index < 0) { |
| 453 | ALOGV("removeResource: didn't find clientId %lld", (long long) clientId); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 454 | return Status::ok(); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 455 | } |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 456 | |
| 457 | const ResourceInfo &info = infos[index]; |
| 458 | for (auto it = info.resources.begin(); it != info.resources.end(); it++) { |
| 459 | onLastRemoved(it->second, info); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 460 | } |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 461 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 462 | AIBinder_unlinkToDeath(info.client->asBinder().get(), |
| 463 | mDeathRecipient.get(), info.deathNotifier.get()); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 464 | |
| 465 | infos.removeItemsAt(index); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 466 | return Status::ok(); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 467 | } |
| 468 | |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 469 | void ResourceManagerService::getClientForResource_l( |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 470 | int callingPid, const MediaResourceParcel *res, |
| 471 | Vector<std::shared_ptr<IResourceManagerClient>> *clients) { |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 472 | if (res == NULL) { |
| 473 | return; |
| 474 | } |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 475 | std::shared_ptr<IResourceManagerClient> client; |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 476 | if (getLowestPriorityBiggestClient_l(callingPid, res->type, &client)) { |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 477 | clients->push_back(client); |
| 478 | } |
| 479 | } |
| 480 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 481 | Status ResourceManagerService::reclaimResource( |
| 482 | int32_t callingPid, |
| 483 | const std::vector<MediaResourceParcel>& resources, |
| 484 | bool* _aidl_return) { |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 485 | String8 log = String8::format("reclaimResource(callingPid %d, resources %s)", |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 486 | callingPid, getString(resources).string()); |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 487 | mServiceLog->add(log); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 488 | *_aidl_return = false; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 489 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 490 | Vector<std::shared_ptr<IResourceManagerClient>> clients; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 491 | { |
| 492 | Mutex::Autolock lock(mLock); |
Ronghua Wu | d11c43a | 2016-01-27 16:26:12 -0800 | [diff] [blame] | 493 | if (!mProcessInfo->isValidPid(callingPid)) { |
| 494 | ALOGE("Rejected reclaimResource call with invalid callingPid."); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 495 | return Status::fromServiceSpecificError(BAD_VALUE); |
Ronghua Wu | d11c43a | 2016-01-27 16:26:12 -0800 | [diff] [blame] | 496 | } |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 497 | const MediaResourceParcel *secureCodec = NULL; |
| 498 | const MediaResourceParcel *nonSecureCodec = NULL; |
| 499 | const MediaResourceParcel *graphicMemory = NULL; |
| 500 | const MediaResourceParcel *drmSession = NULL; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 501 | for (size_t i = 0; i < resources.size(); ++i) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 502 | MediaResource::Type type = resources[i].type; |
| 503 | if (resources[i].type == MediaResource::Type::kSecureCodec) { |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 504 | secureCodec = &resources[i]; |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 505 | } else if (type == MediaResource::Type::kNonSecureCodec) { |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 506 | nonSecureCodec = &resources[i]; |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 507 | } else if (type == MediaResource::Type::kGraphicMemory) { |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 508 | graphicMemory = &resources[i]; |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 509 | } else if (type == MediaResource::Type::kDrmSession) { |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 510 | drmSession = &resources[i]; |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 511 | } |
| 512 | } |
| 513 | |
| 514 | // first pass to handle secure/non-secure codec conflict |
| 515 | if (secureCodec != NULL) { |
| 516 | if (!mSupportsMultipleSecureCodecs) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 517 | if (!getAllClients_l(callingPid, MediaResource::Type::kSecureCodec, &clients)) { |
| 518 | return Status::ok(); |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 519 | } |
| 520 | } |
| 521 | if (!mSupportsSecureWithNonSecureCodec) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 522 | if (!getAllClients_l(callingPid, MediaResource::Type::kNonSecureCodec, &clients)) { |
| 523 | return Status::ok(); |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | } |
| 527 | if (nonSecureCodec != NULL) { |
| 528 | if (!mSupportsSecureWithNonSecureCodec) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 529 | if (!getAllClients_l(callingPid, MediaResource::Type::kSecureCodec, &clients)) { |
| 530 | return Status::ok(); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 531 | } |
| 532 | } |
| 533 | } |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 534 | if (drmSession != NULL) { |
| 535 | getClientForResource_l(callingPid, drmSession, &clients); |
| 536 | if (clients.size() == 0) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 537 | return Status::ok(); |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 538 | } |
| 539 | } |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 540 | |
| 541 | if (clients.size() == 0) { |
| 542 | // if no secure/non-secure codec conflict, run second pass to handle other resources. |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 543 | getClientForResource_l(callingPid, graphicMemory, &clients); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 544 | } |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 545 | |
| 546 | if (clients.size() == 0) { |
| 547 | // if we are here, run the third pass to free one codec with the same type. |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 548 | getClientForResource_l(callingPid, secureCodec, &clients); |
| 549 | getClientForResource_l(callingPid, nonSecureCodec, &clients); |
| 550 | } |
| 551 | |
| 552 | if (clients.size() == 0) { |
| 553 | // if we are here, run the fourth pass to free one codec with the different type. |
| 554 | if (secureCodec != NULL) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 555 | MediaResource temp(MediaResource::Type::kNonSecureCodec, 1); |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 556 | getClientForResource_l(callingPid, &temp, &clients); |
| 557 | } |
| 558 | if (nonSecureCodec != NULL) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 559 | MediaResource temp(MediaResource::Type::kSecureCodec, 1); |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 560 | getClientForResource_l(callingPid, &temp, &clients); |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 561 | } |
| 562 | } |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | if (clients.size() == 0) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 566 | return Status::ok(); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 567 | } |
| 568 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 569 | std::shared_ptr<IResourceManagerClient> failedClient; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 570 | for (size_t i = 0; i < clients.size(); ++i) { |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 571 | log = String8::format("reclaimResource from client %p", clients[i].get()); |
| 572 | mServiceLog->add(log); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 573 | bool success; |
| 574 | Status status = clients[i]->reclaimResource(&success); |
| 575 | if (!status.isOk() || !success) { |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 576 | failedClient = clients[i]; |
| 577 | break; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 578 | } |
| 579 | } |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 580 | |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 581 | if (failedClient == NULL) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 582 | *_aidl_return = true; |
| 583 | return Status::ok(); |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 584 | } |
| 585 | |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 586 | { |
| 587 | Mutex::Autolock lock(mLock); |
| 588 | bool found = false; |
| 589 | for (size_t i = 0; i < mMap.size(); ++i) { |
| 590 | ResourceInfos &infos = mMap.editValueAt(i); |
| 591 | for (size_t j = 0; j < infos.size();) { |
| 592 | if (infos[j].client == failedClient) { |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 593 | j = infos.removeItemsAt(j); |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 594 | found = true; |
| 595 | } else { |
| 596 | ++j; |
| 597 | } |
| 598 | } |
| 599 | if (found) { |
| 600 | break; |
| 601 | } |
| 602 | } |
| 603 | if (!found) { |
| 604 | ALOGV("didn't find failed client"); |
| 605 | } |
| 606 | } |
| 607 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 608 | return Status::ok(); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | bool ResourceManagerService::getAllClients_l( |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 612 | int callingPid, MediaResource::Type type, |
| 613 | Vector<std::shared_ptr<IResourceManagerClient>> *clients) { |
| 614 | Vector<std::shared_ptr<IResourceManagerClient>> temp; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 615 | for (size_t i = 0; i < mMap.size(); ++i) { |
| 616 | ResourceInfos &infos = mMap.editValueAt(i); |
| 617 | for (size_t j = 0; j < infos.size(); ++j) { |
| 618 | if (hasResourceType(type, infos[j].resources)) { |
| 619 | if (!isCallingPriorityHigher_l(callingPid, mMap.keyAt(i))) { |
| 620 | // some higher/equal priority process owns the resource, |
| 621 | // this request can't be fulfilled. |
| 622 | ALOGE("getAllClients_l: can't reclaim resource %s from pid %d", |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 623 | asString(type), mMap.keyAt(i)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 624 | return false; |
| 625 | } |
| 626 | temp.push_back(infos[j].client); |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | if (temp.size() == 0) { |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 631 | ALOGV("getAllClients_l: didn't find any resource %s", asString(type)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 632 | return true; |
| 633 | } |
| 634 | clients->appendVector(temp); |
| 635 | return true; |
| 636 | } |
| 637 | |
| 638 | bool ResourceManagerService::getLowestPriorityBiggestClient_l( |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 639 | int callingPid, MediaResource::Type type, |
| 640 | std::shared_ptr<IResourceManagerClient> *client) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 641 | int lowestPriorityPid; |
| 642 | int lowestPriority; |
| 643 | int callingPriority; |
| 644 | if (!mProcessInfo->getPriority(callingPid, &callingPriority)) { |
| 645 | ALOGE("getLowestPriorityBiggestClient_l: can't get process priority for pid %d", |
| 646 | callingPid); |
| 647 | return false; |
| 648 | } |
| 649 | if (!getLowestPriorityPid_l(type, &lowestPriorityPid, &lowestPriority)) { |
| 650 | return false; |
| 651 | } |
| 652 | if (lowestPriority <= callingPriority) { |
| 653 | ALOGE("getLowestPriorityBiggestClient_l: lowest priority %d vs caller priority %d", |
| 654 | lowestPriority, callingPriority); |
| 655 | return false; |
| 656 | } |
| 657 | |
| 658 | if (!getBiggestClient_l(lowestPriorityPid, type, client)) { |
| 659 | return false; |
| 660 | } |
| 661 | return true; |
| 662 | } |
| 663 | |
| 664 | bool ResourceManagerService::getLowestPriorityPid_l( |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 665 | MediaResource::Type type, int *lowestPriorityPid, int *lowestPriority) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 666 | int pid = -1; |
| 667 | int priority = -1; |
| 668 | for (size_t i = 0; i < mMap.size(); ++i) { |
| 669 | if (mMap.valueAt(i).size() == 0) { |
| 670 | // no client on this process. |
| 671 | continue; |
| 672 | } |
| 673 | if (!hasResourceType(type, mMap.valueAt(i))) { |
| 674 | // doesn't have the requested resource type |
| 675 | continue; |
| 676 | } |
| 677 | int tempPid = mMap.keyAt(i); |
| 678 | int tempPriority; |
| 679 | if (!mProcessInfo->getPriority(tempPid, &tempPriority)) { |
| 680 | ALOGV("getLowestPriorityPid_l: can't get priority of pid %d, skipped", tempPid); |
| 681 | // TODO: remove this pid from mMap? |
| 682 | continue; |
| 683 | } |
| 684 | if (pid == -1 || tempPriority > priority) { |
| 685 | // initial the value |
| 686 | pid = tempPid; |
| 687 | priority = tempPriority; |
| 688 | } |
| 689 | } |
| 690 | if (pid != -1) { |
| 691 | *lowestPriorityPid = pid; |
| 692 | *lowestPriority = priority; |
| 693 | } |
| 694 | return (pid != -1); |
| 695 | } |
| 696 | |
| 697 | bool ResourceManagerService::isCallingPriorityHigher_l(int callingPid, int pid) { |
| 698 | int callingPidPriority; |
| 699 | if (!mProcessInfo->getPriority(callingPid, &callingPidPriority)) { |
| 700 | return false; |
| 701 | } |
| 702 | |
| 703 | int priority; |
| 704 | if (!mProcessInfo->getPriority(pid, &priority)) { |
| 705 | return false; |
| 706 | } |
| 707 | |
| 708 | return (callingPidPriority < priority); |
| 709 | } |
| 710 | |
| 711 | bool ResourceManagerService::getBiggestClient_l( |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 712 | int pid, MediaResource::Type type, std::shared_ptr<IResourceManagerClient> *client) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 713 | ssize_t index = mMap.indexOfKey(pid); |
| 714 | if (index < 0) { |
| 715 | ALOGE("getBiggestClient_l: can't find resource info for pid %d", pid); |
| 716 | return false; |
| 717 | } |
| 718 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 719 | std::shared_ptr<IResourceManagerClient> clientTemp; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 720 | uint64_t largestValue = 0; |
| 721 | const ResourceInfos &infos = mMap.valueAt(index); |
| 722 | for (size_t i = 0; i < infos.size(); ++i) { |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 723 | const ResourceList &resources = infos[i].resources; |
| 724 | for (auto it = resources.begin(); it != resources.end(); it++) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 725 | const MediaResourceParcel &resource = it->second; |
| 726 | if (resource.type == type) { |
| 727 | if (resource.value > largestValue) { |
| 728 | largestValue = resource.value; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 729 | clientTemp = infos[i].client; |
| 730 | } |
| 731 | } |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | if (clientTemp == NULL) { |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 736 | ALOGE("getBiggestClient_l: can't find resource type %s for pid %d", asString(type), pid); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 737 | return false; |
| 738 | } |
| 739 | |
| 740 | *client = clientTemp; |
| 741 | return true; |
| 742 | } |
| 743 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 744 | } // namespace android |