MatrixHal.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (C) 2017 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "MatrixHal.h"
  17. #include <algorithm>
  18. #include "MapValueIterator.h"
  19. namespace android {
  20. namespace vintf {
  21. bool MatrixHal::operator==(const MatrixHal &other) const {
  22. if (format != other.format)
  23. return false;
  24. if (name != other.name)
  25. return false;
  26. if (versionRanges != other.versionRanges)
  27. return false;
  28. if (interfaces != other.interfaces)
  29. return false;
  30. // do not compare optional
  31. return true;
  32. }
  33. bool MatrixHal::containsVersion(const Version& version) const {
  34. for (VersionRange vRange : versionRanges) {
  35. if (vRange.contains(version)) return true;
  36. }
  37. return false;
  38. }
  39. bool MatrixHal::forEachInstance(const std::function<bool(const MatrixInstance&)>& func) const {
  40. for (const auto& vr : versionRanges) {
  41. if (!forEachInstance(vr, func)) {
  42. return false;
  43. }
  44. }
  45. return true;
  46. }
  47. bool MatrixHal::forEachInstance(const VersionRange& vr,
  48. const std::function<bool(const MatrixInstance&)>& func) const {
  49. for (const auto& intf : iterateValues(interfaces)) {
  50. bool cont =
  51. intf.forEachInstance([&](const auto& interface, const auto& instance, bool isRegex) {
  52. // TODO(b/73556059): Store MatrixInstance as well to avoid creating temps
  53. FqInstance fqInstance;
  54. if (fqInstance.setTo(getName(), vr.majorVer, vr.minMinor, interface, instance)) {
  55. if (!func(MatrixInstance(std::move(fqInstance), VersionRange(vr), optional,
  56. isRegex))) {
  57. return false;
  58. }
  59. }
  60. return true;
  61. });
  62. if (!cont) {
  63. return false;
  64. }
  65. }
  66. return true;
  67. }
  68. bool MatrixHal::forEachInstance(
  69. const std::function<bool(const std::vector<VersionRange>&, const std::string&,
  70. const std::string&, bool isRegex)>& func) const {
  71. for (const auto& intf : iterateValues(interfaces)) {
  72. bool cont =
  73. intf.forEachInstance([&](const auto& interface, const auto& instance, bool isRegex) {
  74. return func(this->versionRanges, interface, instance, isRegex);
  75. });
  76. if (!cont) {
  77. return false;
  78. }
  79. }
  80. return true;
  81. }
  82. bool MatrixHal::isCompatible(const std::set<FqInstance>& providedInstances,
  83. const std::set<Version>& providedVersions) const {
  84. // <version>'s are related by OR.
  85. return std::any_of(versionRanges.begin(), versionRanges.end(), [&](const VersionRange& vr) {
  86. return isCompatible(vr, providedInstances, providedVersions);
  87. });
  88. }
  89. bool MatrixHal::isCompatible(const VersionRange& vr, const std::set<FqInstance>& providedInstances,
  90. const std::set<Version>& providedVersions) const {
  91. bool hasAnyInstance = false;
  92. bool versionUnsatisfied = false;
  93. // Look at each interface/instance, and ensure that they are in providedInstances.
  94. forEachInstance(vr, [&](const MatrixInstance& matrixInstance) {
  95. hasAnyInstance = true;
  96. versionUnsatisfied |=
  97. !std::any_of(providedInstances.begin(), providedInstances.end(),
  98. [&](const FqInstance& providedInstance) {
  99. return matrixInstance.isSatisfiedBy(providedInstance);
  100. });
  101. return !versionUnsatisfied; // if any interface/instance is unsatisfied, break
  102. });
  103. if (hasAnyInstance) {
  104. return !versionUnsatisfied;
  105. }
  106. // In some cases (e.g. tests and native HALs), compatibility matrix doesn't specify
  107. // any instances. Check versions only.
  108. return std::any_of(
  109. providedVersions.begin(), providedVersions.end(),
  110. [&](const auto& providedVersion) { return vr.supportedBy(providedVersion); });
  111. }
  112. void MatrixHal::setOptional(bool o) {
  113. this->optional = o;
  114. }
  115. void MatrixHal::insertVersionRanges(const std::vector<VersionRange>& other) {
  116. for (const VersionRange& otherVr : other) {
  117. auto existingVr = std::find_if(this->versionRanges.begin(), this->versionRanges.end(),
  118. [&](const auto& e) { return e.overlaps(otherVr); });
  119. if (existingVr == this->versionRanges.end()) {
  120. this->versionRanges.push_back(otherVr);
  121. } else {
  122. existingVr->minMinor = std::min(existingVr->minMinor, otherVr.minMinor);
  123. existingVr->maxMinor = std::max(existingVr->maxMinor, otherVr.maxMinor);
  124. }
  125. }
  126. }
  127. void MatrixHal::insertInstance(const std::string& interface, const std::string& instance,
  128. bool isRegex) {
  129. auto it = interfaces.find(interface);
  130. if (it == interfaces.end())
  131. it = interfaces.emplace(interface, HalInterface{interface, {}}).first;
  132. it->second.insertInstance(instance, isRegex);
  133. }
  134. size_t MatrixHal::instancesCount() const {
  135. size_t count = 0;
  136. forEachInstance([&](const MatrixInstance&) {
  137. ++count;
  138. return true; // continue;
  139. });
  140. return count;
  141. }
  142. bool MatrixHal::removeInstance(const std::string& interface, const std::string& instance,
  143. bool isRegex) {
  144. auto it = interfaces.find(interface);
  145. if (it == interfaces.end()) return false;
  146. bool removed = it->second.removeInstance(instance, isRegex);
  147. if (!it->second.hasAnyInstance()) interfaces.erase(it);
  148. return removed;
  149. }
  150. void MatrixHal::clearInstances() {
  151. this->interfaces.clear();
  152. }
  153. } // namespace vintf
  154. } // namespace android