| 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 |  | 
| Dongwon Kang | fe508d3 | 2015-12-15 14:22:05 +0900 | [diff] [blame] | 22 | #include <binder/IMediaResourceMonitor.h> | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 23 | #include <binder/IServiceManager.h> | 
|  | 24 | #include <dirent.h> | 
|  | 25 | #include <media/stagefright/ProcessInfo.h> | 
|  | 26 | #include <string.h> | 
|  | 27 | #include <sys/types.h> | 
|  | 28 | #include <sys/stat.h> | 
|  | 29 | #include <sys/time.h> | 
|  | 30 | #include <unistd.h> | 
|  | 31 |  | 
|  | 32 | #include "ResourceManagerService.h" | 
| Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 33 | #include "ServiceLog.h" | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 34 |  | 
|  | 35 | namespace android { | 
|  | 36 |  | 
|  | 37 | template <typename T> | 
|  | 38 | static String8 getString(const Vector<T> &items) { | 
|  | 39 | String8 itemsStr; | 
|  | 40 | for (size_t i = 0; i < items.size(); ++i) { | 
|  | 41 | itemsStr.appendFormat("%s ", items[i].toString().string()); | 
|  | 42 | } | 
|  | 43 | return itemsStr; | 
|  | 44 | } | 
|  | 45 |  | 
| Chih-Hung Hsieh | 7ad2854 | 2016-08-10 10:18:32 -0700 | [diff] [blame] | 46 | static bool hasResourceType(MediaResource::Type type, const Vector<MediaResource>& resources) { | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 47 | for (size_t i = 0; i < resources.size(); ++i) { | 
|  | 48 | if (resources[i].mType == type) { | 
|  | 49 | return true; | 
|  | 50 | } | 
|  | 51 | } | 
|  | 52 | return false; | 
|  | 53 | } | 
|  | 54 |  | 
| Chih-Hung Hsieh | 7ad2854 | 2016-08-10 10:18:32 -0700 | [diff] [blame] | 55 | static bool hasResourceType(MediaResource::Type type, const ResourceInfos& infos) { | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 56 | for (size_t i = 0; i < infos.size(); ++i) { | 
|  | 57 | if (hasResourceType(type, infos[i].resources)) { | 
|  | 58 | return true; | 
|  | 59 | } | 
|  | 60 | } | 
|  | 61 | return false; | 
|  | 62 | } | 
|  | 63 |  | 
|  | 64 | static ResourceInfos& getResourceInfosForEdit( | 
|  | 65 | int pid, | 
|  | 66 | PidResourceInfosMap& map) { | 
|  | 67 | ssize_t index = map.indexOfKey(pid); | 
|  | 68 | if (index < 0) { | 
|  | 69 | // new pid | 
|  | 70 | ResourceInfos infosForPid; | 
|  | 71 | map.add(pid, infosForPid); | 
|  | 72 | } | 
|  | 73 |  | 
|  | 74 | return map.editValueFor(pid); | 
|  | 75 | } | 
|  | 76 |  | 
|  | 77 | static ResourceInfo& getResourceInfoForEdit( | 
|  | 78 | int64_t clientId, | 
| Chih-Hung Hsieh | 5a3b6ca | 2016-08-09 14:18:51 -0700 | [diff] [blame] | 79 | const sp<IResourceManagerClient>& client, | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 80 | ResourceInfos& infos) { | 
|  | 81 | for (size_t i = 0; i < infos.size(); ++i) { | 
|  | 82 | if (infos[i].clientId == clientId) { | 
|  | 83 | return infos.editItemAt(i); | 
|  | 84 | } | 
|  | 85 | } | 
|  | 86 | ResourceInfo info; | 
|  | 87 | info.clientId = clientId; | 
|  | 88 | info.client = client; | 
|  | 89 | infos.push_back(info); | 
|  | 90 | return infos.editItemAt(infos.size() - 1); | 
|  | 91 | } | 
|  | 92 |  | 
| Dongwon Kang | fe508d3 | 2015-12-15 14:22:05 +0900 | [diff] [blame] | 93 | static void notifyResourceGranted(int pid, const Vector<MediaResource> &resources) { | 
|  | 94 | static const char* const kServiceName = "media_resource_monitor"; | 
| Dongwon Kang | 2642c84 | 2016-03-23 18:07:29 -0700 | [diff] [blame] | 95 | sp<IBinder> binder = defaultServiceManager()->checkService(String16(kServiceName)); | 
| Dongwon Kang | fe508d3 | 2015-12-15 14:22:05 +0900 | [diff] [blame] | 96 | if (binder != NULL) { | 
|  | 97 | sp<IMediaResourceMonitor> service = interface_cast<IMediaResourceMonitor>(binder); | 
|  | 98 | for (size_t i = 0; i < resources.size(); ++i) { | 
| Dongwon Kang | 69c23dd | 2016-03-22 15:22:45 -0700 | [diff] [blame] | 99 | if (resources[i].mSubType == MediaResource::kAudioCodec) { | 
|  | 100 | service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_AUDIO_CODEC); | 
|  | 101 | } else if (resources[i].mSubType == MediaResource::kVideoCodec) { | 
|  | 102 | service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_VIDEO_CODEC); | 
|  | 103 | } | 
| Dongwon Kang | fe508d3 | 2015-12-15 14:22:05 +0900 | [diff] [blame] | 104 | } | 
|  | 105 | } | 
|  | 106 | } | 
|  | 107 |  | 
| Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 108 | status_t ResourceManagerService::dump(int fd, const Vector<String16>& /* args */) { | 
| Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 109 | String8 result; | 
| Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 110 |  | 
| dcashman | 014e91e | 2015-09-11 09:33:01 -0700 | [diff] [blame] | 111 | if (checkCallingPermission(String16("android.permission.DUMP")) == false) { | 
|  | 112 | result.format("Permission Denial: " | 
|  | 113 | "can't dump ResourceManagerService from pid=%d, uid=%d\n", | 
|  | 114 | IPCThreadState::self()->getCallingPid(), | 
|  | 115 | IPCThreadState::self()->getCallingUid()); | 
|  | 116 | write(fd, result.string(), result.size()); | 
|  | 117 | return PERMISSION_DENIED; | 
|  | 118 | } | 
|  | 119 |  | 
| Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 120 | PidResourceInfosMap mapCopy; | 
|  | 121 | bool supportsMultipleSecureCodecs; | 
|  | 122 | bool supportsSecureWithNonSecureCodec; | 
|  | 123 | String8 serviceLog; | 
|  | 124 | { | 
|  | 125 | Mutex::Autolock lock(mLock); | 
|  | 126 | mapCopy = mMap;  // Shadow copy, real copy will happen on write. | 
|  | 127 | supportsMultipleSecureCodecs = mSupportsMultipleSecureCodecs; | 
|  | 128 | supportsSecureWithNonSecureCodec = mSupportsSecureWithNonSecureCodec; | 
|  | 129 | serviceLog = mServiceLog->toString("    " /* linePrefix */); | 
|  | 130 | } | 
|  | 131 |  | 
|  | 132 | const size_t SIZE = 256; | 
|  | 133 | char buffer[SIZE]; | 
| Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 134 | snprintf(buffer, SIZE, "ResourceManagerService: %p\n", this); | 
|  | 135 | result.append(buffer); | 
|  | 136 | result.append("  Policies:\n"); | 
| Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 137 | snprintf(buffer, SIZE, "    SupportsMultipleSecureCodecs: %d\n", supportsMultipleSecureCodecs); | 
| Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 138 | result.append(buffer); | 
| Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 139 | snprintf(buffer, SIZE, "    SupportsSecureWithNonSecureCodec: %d\n", | 
|  | 140 | supportsSecureWithNonSecureCodec); | 
| Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 141 | result.append(buffer); | 
|  | 142 |  | 
| Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 143 | result.append("  Processes:\n"); | 
| Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 144 | for (size_t i = 0; i < mapCopy.size(); ++i) { | 
|  | 145 | snprintf(buffer, SIZE, "    Pid: %d\n", mapCopy.keyAt(i)); | 
| Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 146 | result.append(buffer); | 
|  | 147 |  | 
| Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 148 | const ResourceInfos &infos = mapCopy.valueAt(i); | 
| Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 149 | for (size_t j = 0; j < infos.size(); ++j) { | 
| Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 150 | result.append("      Client:\n"); | 
| Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 151 | snprintf(buffer, SIZE, "        Id: %lld\n", (long long)infos[j].clientId); | 
|  | 152 | result.append(buffer); | 
|  | 153 |  | 
|  | 154 | snprintf(buffer, SIZE, "        Name: %s\n", infos[j].client->getName().string()); | 
|  | 155 | result.append(buffer); | 
|  | 156 |  | 
|  | 157 | Vector<MediaResource> resources = infos[j].resources; | 
| Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 158 | result.append("        Resources:\n"); | 
| Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 159 | for (size_t k = 0; k < resources.size(); ++k) { | 
|  | 160 | snprintf(buffer, SIZE, "          %s\n", resources[k].toString().string()); | 
|  | 161 | result.append(buffer); | 
|  | 162 | } | 
|  | 163 | } | 
|  | 164 | } | 
| Ronghua Wu | 022ed72 | 2015-05-11 15:15:09 -0700 | [diff] [blame] | 165 | result.append("  Events logs (most recent at top):\n"); | 
| Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 166 | result.append(serviceLog); | 
| Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 167 |  | 
|  | 168 | write(fd, result.string(), result.size()); | 
|  | 169 | return OK; | 
|  | 170 | } | 
|  | 171 |  | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 172 | ResourceManagerService::ResourceManagerService() | 
| Dongwon Kang | 2642c84 | 2016-03-23 18:07:29 -0700 | [diff] [blame] | 173 | : ResourceManagerService(new ProcessInfo()) {} | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 174 |  | 
|  | 175 | ResourceManagerService::ResourceManagerService(sp<ProcessInfoInterface> processInfo) | 
|  | 176 | : mProcessInfo(processInfo), | 
| Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 177 | mServiceLog(new ServiceLog()), | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 178 | mSupportsMultipleSecureCodecs(true), | 
|  | 179 | mSupportsSecureWithNonSecureCodec(true) {} | 
|  | 180 |  | 
|  | 181 | ResourceManagerService::~ResourceManagerService() {} | 
|  | 182 |  | 
|  | 183 | void ResourceManagerService::config(const Vector<MediaResourcePolicy> &policies) { | 
| Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 184 | String8 log = String8::format("config(%s)", getString(policies).string()); | 
|  | 185 | mServiceLog->add(log); | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 186 |  | 
|  | 187 | Mutex::Autolock lock(mLock); | 
|  | 188 | for (size_t i = 0; i < policies.size(); ++i) { | 
|  | 189 | String8 type = policies[i].mType; | 
| Ronghua Wu | 9ba21b9 | 2015-04-21 14:23:06 -0700 | [diff] [blame] | 190 | String8 value = policies[i].mValue; | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 191 | if (type == kPolicySupportsMultipleSecureCodecs) { | 
| Ronghua Wu | 9ba21b9 | 2015-04-21 14:23:06 -0700 | [diff] [blame] | 192 | mSupportsMultipleSecureCodecs = (value == "true"); | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 193 | } else if (type == kPolicySupportsSecureWithNonSecureCodec) { | 
| Ronghua Wu | 9ba21b9 | 2015-04-21 14:23:06 -0700 | [diff] [blame] | 194 | mSupportsSecureWithNonSecureCodec = (value == "true"); | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 195 | } | 
|  | 196 | } | 
|  | 197 | } | 
|  | 198 |  | 
|  | 199 | void ResourceManagerService::addResource( | 
|  | 200 | int pid, | 
|  | 201 | int64_t clientId, | 
|  | 202 | const sp<IResourceManagerClient> client, | 
|  | 203 | const Vector<MediaResource> &resources) { | 
| Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 204 | String8 log = String8::format("addResource(pid %d, clientId %lld, resources %s)", | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 205 | pid, (long long) clientId, getString(resources).string()); | 
| Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 206 | mServiceLog->add(log); | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 207 |  | 
|  | 208 | Mutex::Autolock lock(mLock); | 
| Ronghua Wu | d11c43a | 2016-01-27 16:26:12 -0800 | [diff] [blame] | 209 | if (!mProcessInfo->isValidPid(pid)) { | 
|  | 210 | ALOGE("Rejected addResource call with invalid pid."); | 
|  | 211 | return; | 
|  | 212 | } | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 213 | ResourceInfos& infos = getResourceInfosForEdit(pid, mMap); | 
|  | 214 | ResourceInfo& info = getResourceInfoForEdit(clientId, client, infos); | 
| Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 215 | // TODO: do the merge instead of append. | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 216 | info.resources.appendVector(resources); | 
| Dongwon Kang | fe508d3 | 2015-12-15 14:22:05 +0900 | [diff] [blame] | 217 | notifyResourceGranted(pid, resources); | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 218 | } | 
|  | 219 |  | 
| Ronghua Wu | 37c8924 | 2015-07-15 12:23:48 -0700 | [diff] [blame] | 220 | void ResourceManagerService::removeResource(int pid, int64_t clientId) { | 
|  | 221 | String8 log = String8::format( | 
|  | 222 | "removeResource(pid %d, clientId %lld)", | 
|  | 223 | pid, (long long) clientId); | 
| Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 224 | mServiceLog->add(log); | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 225 |  | 
|  | 226 | Mutex::Autolock lock(mLock); | 
| Ronghua Wu | d11c43a | 2016-01-27 16:26:12 -0800 | [diff] [blame] | 227 | if (!mProcessInfo->isValidPid(pid)) { | 
|  | 228 | ALOGE("Rejected removeResource call with invalid pid."); | 
|  | 229 | return; | 
|  | 230 | } | 
| Ronghua Wu | 37c8924 | 2015-07-15 12:23:48 -0700 | [diff] [blame] | 231 | ssize_t index = mMap.indexOfKey(pid); | 
|  | 232 | if (index < 0) { | 
|  | 233 | ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId); | 
|  | 234 | return; | 
|  | 235 | } | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 236 | bool found = false; | 
| Ronghua Wu | 37c8924 | 2015-07-15 12:23:48 -0700 | [diff] [blame] | 237 | ResourceInfos &infos = mMap.editValueAt(index); | 
|  | 238 | for (size_t j = 0; j < infos.size(); ++j) { | 
|  | 239 | if (infos[j].clientId == clientId) { | 
|  | 240 | j = infos.removeAt(j); | 
|  | 241 | found = true; | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 242 | break; | 
|  | 243 | } | 
|  | 244 | } | 
|  | 245 | if (!found) { | 
|  | 246 | ALOGV("didn't find client"); | 
|  | 247 | } | 
|  | 248 | } | 
|  | 249 |  | 
| Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 250 | void ResourceManagerService::getClientForResource_l( | 
|  | 251 | int callingPid, const MediaResource *res, Vector<sp<IResourceManagerClient>> *clients) { | 
|  | 252 | if (res == NULL) { | 
|  | 253 | return; | 
|  | 254 | } | 
|  | 255 | sp<IResourceManagerClient> client; | 
|  | 256 | if (getLowestPriorityBiggestClient_l(callingPid, res->mType, &client)) { | 
|  | 257 | clients->push_back(client); | 
|  | 258 | } | 
|  | 259 | } | 
|  | 260 |  | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 261 | bool ResourceManagerService::reclaimResource( | 
|  | 262 | int callingPid, const Vector<MediaResource> &resources) { | 
| Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 263 | String8 log = String8::format("reclaimResource(callingPid %d, resources %s)", | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 264 | callingPid, getString(resources).string()); | 
| Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 265 | mServiceLog->add(log); | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 266 |  | 
|  | 267 | Vector<sp<IResourceManagerClient>> clients; | 
|  | 268 | { | 
|  | 269 | Mutex::Autolock lock(mLock); | 
| Ronghua Wu | d11c43a | 2016-01-27 16:26:12 -0800 | [diff] [blame] | 270 | if (!mProcessInfo->isValidPid(callingPid)) { | 
|  | 271 | ALOGE("Rejected reclaimResource call with invalid callingPid."); | 
|  | 272 | return false; | 
|  | 273 | } | 
| Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 274 | const MediaResource *secureCodec = NULL; | 
|  | 275 | const MediaResource *nonSecureCodec = NULL; | 
|  | 276 | const MediaResource *graphicMemory = NULL; | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 277 | for (size_t i = 0; i < resources.size(); ++i) { | 
| Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 278 | MediaResource::Type type = resources[i].mType; | 
|  | 279 | if (resources[i].mType == MediaResource::kSecureCodec) { | 
| Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 280 | secureCodec = &resources[i]; | 
| Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 281 | } else if (type == MediaResource::kNonSecureCodec) { | 
| Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 282 | nonSecureCodec = &resources[i]; | 
| Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 283 | } else if (type == MediaResource::kGraphicMemory) { | 
| Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 284 | graphicMemory = &resources[i]; | 
|  | 285 | } | 
|  | 286 | } | 
|  | 287 |  | 
|  | 288 | // first pass to handle secure/non-secure codec conflict | 
|  | 289 | if (secureCodec != NULL) { | 
|  | 290 | if (!mSupportsMultipleSecureCodecs) { | 
| Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 291 | if (!getAllClients_l(callingPid, MediaResource::kSecureCodec, &clients)) { | 
| Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 292 | return false; | 
|  | 293 | } | 
|  | 294 | } | 
|  | 295 | if (!mSupportsSecureWithNonSecureCodec) { | 
| Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 296 | if (!getAllClients_l(callingPid, MediaResource::kNonSecureCodec, &clients)) { | 
| Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 297 | return false; | 
|  | 298 | } | 
|  | 299 | } | 
|  | 300 | } | 
|  | 301 | if (nonSecureCodec != NULL) { | 
|  | 302 | if (!mSupportsSecureWithNonSecureCodec) { | 
| Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 303 | if (!getAllClients_l(callingPid, MediaResource::kSecureCodec, &clients)) { | 
| Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 304 | return false; | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 305 | } | 
|  | 306 | } | 
|  | 307 | } | 
|  | 308 |  | 
|  | 309 | if (clients.size() == 0) { | 
|  | 310 | // 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] | 311 | getClientForResource_l(callingPid, graphicMemory, &clients); | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 312 | } | 
| Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 313 |  | 
|  | 314 | if (clients.size() == 0) { | 
|  | 315 | // 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] | 316 | getClientForResource_l(callingPid, secureCodec, &clients); | 
|  | 317 | getClientForResource_l(callingPid, nonSecureCodec, &clients); | 
|  | 318 | } | 
|  | 319 |  | 
|  | 320 | if (clients.size() == 0) { | 
|  | 321 | // if we are here, run the fourth pass to free one codec with the different type. | 
|  | 322 | if (secureCodec != NULL) { | 
| Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 323 | MediaResource temp(MediaResource::kNonSecureCodec, 1); | 
| Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 324 | getClientForResource_l(callingPid, &temp, &clients); | 
|  | 325 | } | 
|  | 326 | if (nonSecureCodec != NULL) { | 
| Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 327 | MediaResource temp(MediaResource::kSecureCodec, 1); | 
| Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 328 | getClientForResource_l(callingPid, &temp, &clients); | 
| Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 329 | } | 
|  | 330 | } | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 331 | } | 
|  | 332 |  | 
|  | 333 | if (clients.size() == 0) { | 
|  | 334 | return false; | 
|  | 335 | } | 
|  | 336 |  | 
| Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 337 | sp<IResourceManagerClient> failedClient; | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 338 | for (size_t i = 0; i < clients.size(); ++i) { | 
| Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 339 | log = String8::format("reclaimResource from client %p", clients[i].get()); | 
|  | 340 | mServiceLog->add(log); | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 341 | if (!clients[i]->reclaimResource()) { | 
| Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 342 | failedClient = clients[i]; | 
|  | 343 | break; | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 344 | } | 
|  | 345 | } | 
| Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 346 |  | 
| Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 347 | if (failedClient == NULL) { | 
|  | 348 | return true; | 
|  | 349 | } | 
|  | 350 |  | 
| Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 351 | { | 
|  | 352 | Mutex::Autolock lock(mLock); | 
|  | 353 | bool found = false; | 
|  | 354 | for (size_t i = 0; i < mMap.size(); ++i) { | 
|  | 355 | ResourceInfos &infos = mMap.editValueAt(i); | 
|  | 356 | for (size_t j = 0; j < infos.size();) { | 
|  | 357 | if (infos[j].client == failedClient) { | 
|  | 358 | j = infos.removeAt(j); | 
|  | 359 | found = true; | 
|  | 360 | } else { | 
|  | 361 | ++j; | 
|  | 362 | } | 
|  | 363 | } | 
|  | 364 | if (found) { | 
|  | 365 | break; | 
|  | 366 | } | 
|  | 367 | } | 
|  | 368 | if (!found) { | 
|  | 369 | ALOGV("didn't find failed client"); | 
|  | 370 | } | 
|  | 371 | } | 
|  | 372 |  | 
| Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 373 | return false; | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 374 | } | 
|  | 375 |  | 
|  | 376 | bool ResourceManagerService::getAllClients_l( | 
| Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 377 | int callingPid, MediaResource::Type type, Vector<sp<IResourceManagerClient>> *clients) { | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 378 | Vector<sp<IResourceManagerClient>> temp; | 
|  | 379 | for (size_t i = 0; i < mMap.size(); ++i) { | 
|  | 380 | ResourceInfos &infos = mMap.editValueAt(i); | 
|  | 381 | for (size_t j = 0; j < infos.size(); ++j) { | 
|  | 382 | if (hasResourceType(type, infos[j].resources)) { | 
|  | 383 | if (!isCallingPriorityHigher_l(callingPid, mMap.keyAt(i))) { | 
|  | 384 | // some higher/equal priority process owns the resource, | 
|  | 385 | // this request can't be fulfilled. | 
|  | 386 | ALOGE("getAllClients_l: can't reclaim resource %s from pid %d", | 
| Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 387 | asString(type), mMap.keyAt(i)); | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 388 | return false; | 
|  | 389 | } | 
|  | 390 | temp.push_back(infos[j].client); | 
|  | 391 | } | 
|  | 392 | } | 
|  | 393 | } | 
|  | 394 | if (temp.size() == 0) { | 
| Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 395 | ALOGV("getAllClients_l: didn't find any resource %s", asString(type)); | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 396 | return true; | 
|  | 397 | } | 
|  | 398 | clients->appendVector(temp); | 
|  | 399 | return true; | 
|  | 400 | } | 
|  | 401 |  | 
|  | 402 | bool ResourceManagerService::getLowestPriorityBiggestClient_l( | 
| Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 403 | int callingPid, MediaResource::Type type, sp<IResourceManagerClient> *client) { | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 404 | int lowestPriorityPid; | 
|  | 405 | int lowestPriority; | 
|  | 406 | int callingPriority; | 
|  | 407 | if (!mProcessInfo->getPriority(callingPid, &callingPriority)) { | 
|  | 408 | ALOGE("getLowestPriorityBiggestClient_l: can't get process priority for pid %d", | 
|  | 409 | callingPid); | 
|  | 410 | return false; | 
|  | 411 | } | 
|  | 412 | if (!getLowestPriorityPid_l(type, &lowestPriorityPid, &lowestPriority)) { | 
|  | 413 | return false; | 
|  | 414 | } | 
|  | 415 | if (lowestPriority <= callingPriority) { | 
|  | 416 | ALOGE("getLowestPriorityBiggestClient_l: lowest priority %d vs caller priority %d", | 
|  | 417 | lowestPriority, callingPriority); | 
|  | 418 | return false; | 
|  | 419 | } | 
|  | 420 |  | 
|  | 421 | if (!getBiggestClient_l(lowestPriorityPid, type, client)) { | 
|  | 422 | return false; | 
|  | 423 | } | 
|  | 424 | return true; | 
|  | 425 | } | 
|  | 426 |  | 
|  | 427 | bool ResourceManagerService::getLowestPriorityPid_l( | 
| Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 428 | MediaResource::Type type, int *lowestPriorityPid, int *lowestPriority) { | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 429 | int pid = -1; | 
|  | 430 | int priority = -1; | 
|  | 431 | for (size_t i = 0; i < mMap.size(); ++i) { | 
|  | 432 | if (mMap.valueAt(i).size() == 0) { | 
|  | 433 | // no client on this process. | 
|  | 434 | continue; | 
|  | 435 | } | 
|  | 436 | if (!hasResourceType(type, mMap.valueAt(i))) { | 
|  | 437 | // doesn't have the requested resource type | 
|  | 438 | continue; | 
|  | 439 | } | 
|  | 440 | int tempPid = mMap.keyAt(i); | 
|  | 441 | int tempPriority; | 
|  | 442 | if (!mProcessInfo->getPriority(tempPid, &tempPriority)) { | 
|  | 443 | ALOGV("getLowestPriorityPid_l: can't get priority of pid %d, skipped", tempPid); | 
|  | 444 | // TODO: remove this pid from mMap? | 
|  | 445 | continue; | 
|  | 446 | } | 
|  | 447 | if (pid == -1 || tempPriority > priority) { | 
|  | 448 | // initial the value | 
|  | 449 | pid = tempPid; | 
|  | 450 | priority = tempPriority; | 
|  | 451 | } | 
|  | 452 | } | 
|  | 453 | if (pid != -1) { | 
|  | 454 | *lowestPriorityPid = pid; | 
|  | 455 | *lowestPriority = priority; | 
|  | 456 | } | 
|  | 457 | return (pid != -1); | 
|  | 458 | } | 
|  | 459 |  | 
|  | 460 | bool ResourceManagerService::isCallingPriorityHigher_l(int callingPid, int pid) { | 
|  | 461 | int callingPidPriority; | 
|  | 462 | if (!mProcessInfo->getPriority(callingPid, &callingPidPriority)) { | 
|  | 463 | return false; | 
|  | 464 | } | 
|  | 465 |  | 
|  | 466 | int priority; | 
|  | 467 | if (!mProcessInfo->getPriority(pid, &priority)) { | 
|  | 468 | return false; | 
|  | 469 | } | 
|  | 470 |  | 
|  | 471 | return (callingPidPriority < priority); | 
|  | 472 | } | 
|  | 473 |  | 
|  | 474 | bool ResourceManagerService::getBiggestClient_l( | 
| Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 475 | int pid, MediaResource::Type type, sp<IResourceManagerClient> *client) { | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 476 | ssize_t index = mMap.indexOfKey(pid); | 
|  | 477 | if (index < 0) { | 
|  | 478 | ALOGE("getBiggestClient_l: can't find resource info for pid %d", pid); | 
|  | 479 | return false; | 
|  | 480 | } | 
|  | 481 |  | 
|  | 482 | sp<IResourceManagerClient> clientTemp; | 
|  | 483 | uint64_t largestValue = 0; | 
|  | 484 | const ResourceInfos &infos = mMap.valueAt(index); | 
|  | 485 | for (size_t i = 0; i < infos.size(); ++i) { | 
|  | 486 | Vector<MediaResource> resources = infos[i].resources; | 
|  | 487 | for (size_t j = 0; j < resources.size(); ++j) { | 
|  | 488 | if (resources[j].mType == type) { | 
|  | 489 | if (resources[j].mValue > largestValue) { | 
|  | 490 | largestValue = resources[j].mValue; | 
|  | 491 | clientTemp = infos[i].client; | 
|  | 492 | } | 
|  | 493 | } | 
|  | 494 | } | 
|  | 495 | } | 
|  | 496 |  | 
|  | 497 | if (clientTemp == NULL) { | 
| Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 498 | 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] | 499 | return false; | 
|  | 500 | } | 
|  | 501 |  | 
|  | 502 | *client = clientTemp; | 
|  | 503 | return true; | 
|  | 504 | } | 
|  | 505 |  | 
|  | 506 | } // namespace android |