blob: 087f9ca842b4dab2b95a48d5610ddfe244d42f6c [file]
/*
* Copyright 2025 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.
*/
#include "Display/DebugDisplayModeConstraints.h"
#include <android-base/parseint.h>
#include <android-base/strings.h>
#include <log/log.h>
#include <map>
#include <set>
namespace android::display {
namespace {
uint64_t parsePixelCount(std::string s) {
if (s.empty()) return 0;
uint64_t multiplier = 1;
if (s.back() == 'K') {
multiplier = 1000;
s.pop_back();
} else if (s.back() == 'M') {
multiplier = 1000000;
s.pop_back();
} else if (s.back() == 'B') {
multiplier = 1000000000;
s.pop_back();
}
uint64_t val = 0;
if (android::base::ParseUint(s, &val)) {
return val * multiplier;
}
return 0;
}
} // namespace
bool DebugModeConstraintVerifier::shouldAccept(
const std::vector<std::pair<PhysicalDisplayId, DisplayModePtr>>& currentModes,
const std::vector<std::pair<PhysicalDisplayId, DisplayModePtr>>& proposedModes) const {
// EffectiveModes contains the final mode for each display, taking into account that
// proposedModes override currentModes.
struct DisplayIdLess {
bool operator()(PhysicalDisplayId lhs, PhysicalDisplayId rhs) const {
return lhs.value < rhs.value;
}
};
std::map<PhysicalDisplayId, DisplayModePtr, DisplayIdLess> effectiveModes;
for (const auto& [id, mode] : currentModes) {
effectiveModes[id] = mode;
}
for (const auto& [id, mode] : proposedModes) {
effectiveModes[id] = mode;
}
for (const auto& constraint : mConstraints) {
uint64_t totalPixelCount = 0;
bool constraintApplies = false;
for (const auto& [id, mode] : effectiveModes) {
if (!mode) continue;
bool matches = false;
switch (constraint.selector.type) {
case DebugDisplaySelector::Type::All:
matches = true;
break;
case DebugDisplaySelector::Type::External:
matches = mIsExternalDisplay(id);
break;
case DebugDisplaySelector::Type::Id:
for (auto targetId : constraint.selector.ids) {
if (id.value == targetId) {
matches = true;
break;
}
}
break;
}
if (matches) {
constraintApplies = true;
const auto& resolution = mode->getResolution();
totalPixelCount += static_cast<uint64_t>(resolution.width) *
static_cast<uint64_t>(resolution.height);
}
}
// If constraint is violated, do NOT accept
if (constraintApplies && constraint.maxPixelCount > 0 &&
totalPixelCount > constraint.maxPixelCount) {
return false;
}
}
return true;
}
std::vector<DebugDisplayModeConstraint> DebugModeRejectionParser::parse(
const std::string& debugString) {
if (debugString.empty()) return {};
std::vector<DebugDisplayModeConstraint> constraints;
auto ruleStrings = android::base::Split(debugString, ";");
for (const auto& ruleString : ruleStrings) {
DebugDisplayModeConstraint constraint;
bool hasLimit = false;
auto parts = android::base::Split(ruleString, ",");
for (const auto& part : parts) {
auto pos = part.find('=');
if (pos == std::string::npos) continue;
auto key = part.substr(0, pos);
auto value = part.substr(pos + 1);
if (key == "displays") {
if (value == "external") {
constraint.selector.type = DebugDisplaySelector::Type::External;
} else if (android::base::StartsWith(value, "id")) {
constraint.selector.type = DebugDisplaySelector::Type::Id;
auto ids = android::base::Split(value, "+");
for (const auto& idStr : ids) {
if (android::base::StartsWith(idStr, "id")) {
uint64_t id;
if (android::base::ParseUint(idStr.substr(2), &id)) {
constraint.selector.ids.push_back(id);
}
}
}
}
} else if (key == "max_pixel_count") {
constraint.maxPixelCount = parsePixelCount(value);
hasLimit = true;
}
}
if (hasLimit) {
constraints.push_back(constraint);
}
}
return constraints;
}
} // namespace android::display