blob: 934f2e81358a6db3c5b1641fd6ef1cf918f1f447 [file] [log] [blame]
Arpit Singhd6d7df72025-03-07 11:14:03 +00001/*
2 * Copyright 2025 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
17#define LOG_TAG "DisplayTopologyValidator"
18
19#include <android-base/logging.h>
Arpit Singh6b512042025-03-11 17:05:07 +000020#include <android-base/stringprintf.h>
Arpit Singhd6d7df72025-03-07 11:14:03 +000021#include <ftl/enum.h>
22#include <input/DisplayTopologyGraph.h>
Arpit Singh6b512042025-03-11 17:05:07 +000023#include <input/PrintTools.h>
Arpit Singhd6d7df72025-03-07 11:14:03 +000024#include <ui/LogicalDisplayId.h>
25
26#include <algorithm>
27
Arpit Singh6b512042025-03-11 17:05:07 +000028#define INDENT " "
29
Arpit Singhd6d7df72025-03-07 11:14:03 +000030namespace android {
31
32namespace {
Arpit Singh6b512042025-03-11 17:05:07 +000033
Arpit Singhd6d7df72025-03-07 11:14:03 +000034DisplayTopologyPosition getOppositePosition(DisplayTopologyPosition position) {
35 switch (position) {
36 case DisplayTopologyPosition::LEFT:
37 return DisplayTopologyPosition::RIGHT;
38 case DisplayTopologyPosition::TOP:
39 return DisplayTopologyPosition::BOTTOM;
40 case DisplayTopologyPosition::RIGHT:
41 return DisplayTopologyPosition::LEFT;
42 case DisplayTopologyPosition::BOTTOM:
43 return DisplayTopologyPosition::TOP;
44 }
45}
46
47bool validatePrimaryDisplay(const android::DisplayTopologyGraph& displayTopologyGraph) {
48 return displayTopologyGraph.primaryDisplayId != ui::LogicalDisplayId::INVALID &&
49 displayTopologyGraph.graph.contains(displayTopologyGraph.primaryDisplayId);
50}
51
52bool validateTopologyGraph(const android::DisplayTopologyGraph& displayTopologyGraph) {
53 for (const auto& [sourceDisplay, adjacentDisplays] : displayTopologyGraph.graph) {
54 for (const DisplayTopologyAdjacentDisplay& adjacentDisplay : adjacentDisplays) {
55 const auto adjacentGraphIt = displayTopologyGraph.graph.find(adjacentDisplay.displayId);
56 if (adjacentGraphIt == displayTopologyGraph.graph.end()) {
57 LOG(ERROR) << "Missing adjacent display in topology graph: "
58 << adjacentDisplay.displayId << " for source " << sourceDisplay;
59 return false;
60 }
61 const auto reverseEdgeIt =
62 std::find_if(adjacentGraphIt->second.begin(), adjacentGraphIt->second.end(),
63 [sourceDisplay](const DisplayTopologyAdjacentDisplay&
64 reverseAdjacentDisplay) {
65 return sourceDisplay == reverseAdjacentDisplay.displayId;
66 });
67 if (reverseEdgeIt == adjacentGraphIt->second.end()) {
68 LOG(ERROR) << "Missing reverse edge in topology graph for: " << sourceDisplay
69 << " -> " << adjacentDisplay.displayId;
70 return false;
71 }
72 DisplayTopologyPosition expectedPosition =
73 getOppositePosition(adjacentDisplay.position);
74 if (reverseEdgeIt->position != expectedPosition) {
75 LOG(ERROR) << "Unexpected reverse edge for: " << sourceDisplay << " -> "
76 << adjacentDisplay.displayId
77 << " expected position: " << ftl::enum_string(expectedPosition)
78 << " actual " << ftl::enum_string(reverseEdgeIt->position);
79 return false;
80 }
81 if (reverseEdgeIt->offsetDp != -adjacentDisplay.offsetDp) {
82 LOG(ERROR) << "Unexpected reverse edge offset: " << sourceDisplay << " -> "
83 << adjacentDisplay.displayId
84 << " expected offset: " << -adjacentDisplay.offsetDp << " actual "
85 << reverseEdgeIt->offsetDp;
86 return false;
87 }
88 }
89 }
90 return true;
91}
92
93bool validateDensities(const android::DisplayTopologyGraph& displayTopologyGraph) {
94 for (const auto& [sourceDisplay, adjacentDisplays] : displayTopologyGraph.graph) {
95 if (!displayTopologyGraph.displaysDensity.contains(sourceDisplay)) {
96 LOG(ERROR) << "Missing density value in topology graph for display: " << sourceDisplay;
97 return false;
98 }
99 }
100 return true;
101}
102
Arpit Singh6b512042025-03-11 17:05:07 +0000103std::string logicalDisplayIdToString(const ui::LogicalDisplayId& displayId) {
104 return base::StringPrintf("displayId(%d)", displayId.val());
105}
106
107std::string adjacentDisplayToString(const DisplayTopologyAdjacentDisplay& adjacentDisplay) {
108 return adjacentDisplay.dump();
109}
110
111std::string adjacentDisplayVectorToString(
112 const std::vector<DisplayTopologyAdjacentDisplay>& adjacentDisplays) {
113 return dumpVector(adjacentDisplays, adjacentDisplayToString);
114}
115
Arpit Singhd6d7df72025-03-07 11:14:03 +0000116} // namespace
117
Arpit Singh6b512042025-03-11 17:05:07 +0000118std::string DisplayTopologyAdjacentDisplay::dump() const {
119 std::string dump;
120 dump += base::StringPrintf("DisplayTopologyAdjacentDisplay: {displayId: %d, position: %s, "
121 "offsetDp: %f}",
122 displayId.val(), ftl::enum_string(position).c_str(), offsetDp);
123 return dump;
124}
125
Arpit Singhd6d7df72025-03-07 11:14:03 +0000126bool DisplayTopologyGraph::isValid() const {
127 return validatePrimaryDisplay(*this) && validateTopologyGraph(*this) &&
128 validateDensities(*this);
129}
130
Arpit Singh6b512042025-03-11 17:05:07 +0000131std::string DisplayTopologyGraph::dump() const {
132 std::string dump;
133 dump += base::StringPrintf("PrimaryDisplayId: %d\n", primaryDisplayId.val());
134 dump += base::StringPrintf("TopologyGraph:\n");
135 dump += addLinePrefix(dumpMap(graph, logicalDisplayIdToString, adjacentDisplayVectorToString),
136 INDENT);
137 dump += "\n";
138 dump += base::StringPrintf("DisplaysDensity:\n");
139 dump += addLinePrefix(dumpMap(displaysDensity, logicalDisplayIdToString), INDENT);
140 dump += "\n";
141 return dump;
142}
143
Arpit Singhd6d7df72025-03-07 11:14:03 +0000144} // namespace android