cdd3e40440a98b4bd2bbb5de9debb41d330e1661
1 #include <kms++util/resourcemanager.h>
2 #include <algorithm>
3 #include <kms++util/strhelpers.h>
5 using namespace kms;
6 using namespace std;
8 template<class C, class T>
9 auto contains(const C& v, const T& x)
10 -> decltype(end(v), true)
11 {
12 return end(v) != std::find(begin(v), end(v), x);
13 }
15 ResourceManager::ResourceManager(Card& card)
16 : m_card(card)
17 {
18 }
20 void ResourceManager::reset()
21 {
22 m_reserved_connectors.clear();
23 m_reserved_crtcs.clear();
24 m_reserved_planes.clear();
25 }
27 static Connector* find_connector(Card& card, const vector<Connector*> reserved)
28 {
29 for (Connector* conn : card.get_connectors()) {
30 if (!conn->connected())
31 continue;
33 if (contains(reserved, conn))
34 continue;
36 return conn;
37 }
39 return nullptr;
40 }
42 static Connector* resolve_connector(Card& card, const string& name, const vector<Connector*> reserved)
43 {
44 auto connectors = card.get_connectors();
46 if (name[0] == '@') {
47 char* endptr;
48 unsigned id = strtoul(name.c_str() + 1, &endptr, 10);
49 if (*endptr == 0) {
50 Connector* c = card.get_connector(id);
52 if (!c || contains(reserved, c))
53 return nullptr;
55 return c;
56 }
57 } else {
58 char* endptr;
59 unsigned idx = strtoul(name.c_str(), &endptr, 10);
60 if (*endptr == 0) {
61 if (idx >= connectors.size())
62 return nullptr;
64 Connector* c = connectors[idx];
66 if (contains(reserved, c))
67 return nullptr;
69 return c;
70 }
71 }
73 for (Connector* conn : connectors) {
74 if (to_lower(conn->fullname()).find(to_lower(name)) == string::npos)
75 continue;
77 if (contains(reserved, conn))
78 continue;
80 return conn;
81 }
83 return nullptr;
84 }
86 Connector* ResourceManager::reserve_connector(const string& name)
87 {
88 Connector* conn;
90 if (name.empty())
91 conn = find_connector(m_card, m_reserved_connectors);
92 else
93 conn = resolve_connector(m_card, name, m_reserved_connectors);
95 if (!conn)
96 return nullptr;
98 m_reserved_connectors.push_back(conn);
99 return conn;
100 }
102 Crtc* ResourceManager::reserve_crtc(Connector* conn)
103 {
104 if (Crtc* crtc = conn->get_current_crtc()) {
105 m_reserved_crtcs.push_back(crtc);
106 return crtc;
107 }
109 for (Crtc* crtc : conn->get_possible_crtcs()) {
110 if (contains(m_reserved_crtcs, crtc))
111 continue;
113 m_reserved_crtcs.push_back(crtc);
114 return crtc;
115 }
117 return nullptr;
118 }
120 Plane* ResourceManager::reserve_plane(Crtc* crtc, PlaneType type, PixelFormat format)
121 {
122 for (Plane* plane : crtc->get_possible_planes()) {
123 if (plane->plane_type() != type)
124 continue;
126 if (format != PixelFormat::Undefined && !plane->supports_format(format))
127 continue;
129 if (contains(m_reserved_planes, plane))
130 continue;
132 m_reserved_planes.push_back(plane);
133 return plane;
134 }
136 return nullptr;
137 }
139 Plane* ResourceManager::reserve_primary_plane(Crtc* crtc, PixelFormat format)
140 {
141 return reserve_plane(crtc, PlaneType::Primary, format);
142 }
144 Plane* ResourceManager::reserve_overlay_plane(Crtc* crtc, PixelFormat format)
145 {
146 return reserve_plane(crtc, PlaneType::Overlay, format);
147 }