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