resourcemanager: move utility functions
As part of refactoring the resource manager and the resources,
this change moves the utility functions out of the main
source code and into a new compilation unit.
Since, there is no change in the functionality or addition
or modifications of ResourceManagerService binder APIs,
this change is unflagged.
Bug: 289097671
Test: atest android.media.misc.cts.ResourceManagerTest
atest android.media.misc.cts.ResourceManagerMultiTest
/data/nativetest64/ResourceManagerService_test/ResourceManagerService_test
/data/nativetest64/ResourceObserverService_test/ResourceObserverService_test
Merged-In: I49321e411e6671c547878ec947c553807829376a
Change-Id: I49321e411e6671c547878ec947c553807829376a
diff --git a/services/mediaresourcemanager/ResourceManagerServiceUtils.cpp b/services/mediaresourcemanager/ResourceManagerServiceUtils.cpp
new file mode 100644
index 0000000..892b1b3
--- /dev/null
+++ b/services/mediaresourcemanager/ResourceManagerServiceUtils.cpp
@@ -0,0 +1,98 @@
+/*
+**
+** Copyright 2023, The Android Open Source Project
+**
+** Licensed under the Apache License, Version 2.0 (the "License");
+** you may not use this file except in compliance with the License.
+** You may obtain a copy of the License at
+**
+** http://www.apache.org/licenses/LICENSE-2.0
+**
+** Unless required by applicable law or agreed to in writing, software
+** distributed under the License is distributed on an "AS IS" BASIS,
+** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+** See the License for the specific language governing permissions and
+** limitations under the License.
+*/
+
+//#define LOG_NDEBUG 0
+#define LOG_TAG "ResourceManagerServiceUtils"
+#include <utils/Log.h>
+
+#include "ResourceManagerService.h"
+#include "ResourceManagerServiceUtils.h"
+
+namespace android {
+
+bool hasResourceType(MediaResource::Type type, MediaResource::SubType subType,
+ const MediaResourceParcel& resource) {
+ if (type != resource.type) {
+ return false;
+ }
+ switch (type) {
+ // Codec subtypes (e.g. video vs. audio) are each considered separate resources, so
+ // compare the subtypes as well.
+ case MediaResource::Type::kSecureCodec:
+ case MediaResource::Type::kNonSecureCodec:
+ if (resource.subType == subType) {
+ return true;
+ }
+ break;
+ // Non-codec resources are not segregated by the subtype (e.g. video vs. audio).
+ default:
+ return true;
+ }
+ return false;
+}
+
+bool hasResourceType(MediaResource::Type type, MediaResource::SubType subType,
+ const ResourceList& resources) {
+ for (auto it = resources.begin(); it != resources.end(); it++) {
+ if (hasResourceType(type, subType, it->second)) {
+ return true;
+ }
+ }
+ return false;
+}
+
+bool hasResourceType(MediaResource::Type type, MediaResource::SubType subType,
+ const ResourceInfos& infos) {
+ for (const auto& [id, info] : infos) {
+ if (hasResourceType(type, subType, info.resources)) {
+ return true;
+ }
+ }
+ return false;
+}
+
+ResourceInfos& getResourceInfosForEdit(int pid, PidResourceInfosMap& map) {
+ PidResourceInfosMap::iterator found = map.find(pid);
+ if (found == map.end()) {
+ // new pid
+ ResourceInfos infosForPid;
+ auto [it, inserted] = map.emplace(pid, infosForPid);
+ found = it;
+ }
+
+ return found->second;
+}
+
+ResourceInfo& getResourceInfoForEdit(const ClientInfoParcel& clientInfo,
+ const std::shared_ptr<IResourceManagerClient>& client, ResourceInfos& infos) {
+ ResourceInfos::iterator found = infos.find(clientInfo.id);
+
+ if (found == infos.end()) {
+ ResourceInfo info{.uid = static_cast<uid_t>(clientInfo.uid),
+ .clientId = clientInfo.id,
+ .name = clientInfo.name.empty()? "<unknown client>" : clientInfo.name,
+ .client = client,
+ .deathNotifier = nullptr,
+ .pendingRemoval = false};
+ auto [it, inserted] = infos.emplace(clientInfo.id, info);
+ found = it;
+ }
+
+ return found->second;
+}
+
+} // namespace android