Add has_has_universal_planes().
[android/external-libkmsxx.git] / libkms++ / card.cpp
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <fcntl.h>
4 #include <utility>
5 #include <stdexcept>
6 #include <string.h>
7 #include <algorithm>
9 #include <xf86drm.h>
10 #include <xf86drmMode.h>
12 #include "kms++.h"
14 #ifndef DRM_CLIENT_CAP_ATOMIC
15 #define DRM_CLIENT_CAP_ATOMIC 3
16 #endif
18 using namespace std;
20 namespace kms
21 {
23 Card::Card()
24 {
25         const char *card = "/dev/dri/card0";
27         int fd = open(card, O_RDWR | O_CLOEXEC);
28         if (fd < 0)
29                 throw invalid_argument(string(strerror(errno)) + " opening " +
30                                        card);
31         m_fd = fd;
33         int r;
35         r = drmSetMaster(fd);
36         m_master = r == 0;
38         r = drmSetClientCap(m_fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
39         m_has_universal_planes = r == 0;
41         r = drmSetClientCap(m_fd, DRM_CLIENT_CAP_ATOMIC, 1);
42         m_has_atomic = r == 0;
44         uint64_t has_dumb;
45         r = drmGetCap(fd, DRM_CAP_DUMB_BUFFER, &has_dumb);
46         if (r || !has_dumb)
47                 throw invalid_argument("Dumb buffers not available");
49         auto res = drmModeGetResources(m_fd);
50         if (!res)
51                 throw invalid_argument("Can't get card resources");
53         for (int i = 0; i < res->count_connectors; ++i) {
54                 uint32_t id = res->connectors[i];
55                 m_obmap[id] = new Connector(*this, id, i);
56         }
58         for (int i = 0; i < res->count_crtcs; ++i) {
59                 uint32_t id = res->crtcs[i];
60                 m_obmap[id] = new Crtc(*this, id, i);
61         }
63         for (int i = 0; i < res->count_encoders; ++i) {
64                 uint32_t id = res->encoders[i];
65                 m_obmap[id] = new Encoder(*this, id);
66         }
68         drmModeFreeResources(res);
70         auto planeRes = drmModeGetPlaneResources(m_fd);
72         for (uint i = 0; i < planeRes->count_planes; ++i) {
73                 uint32_t id = planeRes->planes[i];
74                 m_obmap[id] = new Plane(*this, id);
75         }
77         drmModeFreePlaneResources(planeRes);
79         // collect all possible props
80         for (auto ob : get_objects()) {
81                 auto props = drmModeObjectGetProperties(m_fd, ob->id(), ob->object_type());
83                 if (props == nullptr)
84                         continue;
86                 for (unsigned i = 0; i < props->count_props; ++i) {
87                         uint32_t prop_id = props->props[i];
89                         if (m_obmap.find(prop_id) == m_obmap.end())
90                                 m_obmap[prop_id] = new Property(*this, prop_id);
91                 }
93                 drmModeFreeObjectProperties(props);
94         }
96         for (auto pair : m_obmap)
97                 pair.second->setup();
98 }
100 Card::~Card()
102         for (auto pair : m_obmap)
103                 delete pair.second;
105         close(m_fd);
108 template <class T> static void print_obs(const map<uint32_t, DrmObject*>& obmap)
110         for (auto pair : obmap) {
111                 auto ob = pair.second;
112                 if (dynamic_cast<T*>(ob)) {
113                         ob->print_short();
114                         //ob->print_props();
115                 }
116         }
119 void Card::print_short() const
121         print_obs<Connector>(m_obmap);
122         print_obs<Encoder>(m_obmap);
123         print_obs<Crtc>(m_obmap);
124         print_obs<Plane>(m_obmap);
127 Property* Card::get_prop(const char *name) const
129         for (auto pair : m_obmap) {
130                 auto prop = dynamic_cast<Property*>(pair.second);
131                 if (!prop)
132                         continue;
134                 if (strcmp(name, prop->name()) == 0)
135                         return prop;
136         }
138         throw invalid_argument(string("Card property ") + name + " not found");
141 Connector* Card::get_first_connected_connector() const
143         for(auto pair : m_obmap) {
144                 auto c = dynamic_cast<Connector*>(pair.second);
146                 if (c && c->connected())
147                         return c;
148         }
150         throw invalid_argument("no connected connectors");
153 DrmObject* Card::get_object(uint32_t id) const
155         return m_obmap.at(id);
158 vector<Connector*> Card::get_connectors() const
160         vector<Connector*> v;
161         for(auto pair : m_obmap) {
162                 auto p = dynamic_cast<Connector*>(pair.second);
163                 if (p)
164                         v.push_back(p);
165         }
166         return v;
169 vector<Plane*> Card::get_planes() const
171         vector<Plane*> v;
172         for(auto pair : m_obmap) {
173                 auto p = dynamic_cast<Plane*>(pair.second);
174                 if (p)
175                         v.push_back(p);
176         }
177         return v;
180 vector<DrmObject*> Card::get_objects() const
182         vector<DrmObject*> v;
183         for(auto pair : m_obmap)
184                 v.push_back(pair.second);
185         return v;
188 Crtc* Card::get_crtc_by_index(uint32_t idx) const
190         for(auto pair : m_obmap) {
191                 auto crtc = dynamic_cast<Crtc*>(pair.second);
192                 if (crtc && crtc->idx() == idx)
193                         return crtc;
194         }
195         throw invalid_argument(string("Crtc #") + to_string(idx) + "not found");
198 Crtc* Card::get_crtc(uint32_t id) const { return dynamic_cast<Crtc*>(get_object(id)); }
199 Encoder* Card::get_encoder(uint32_t id) const { return dynamic_cast<Encoder*>(get_object(id)); }
200 Property* Card::get_prop(uint32_t id) const { return dynamic_cast<Property*>(get_object(id)); }
202 std::vector<kms::Pipeline> Card::get_connected_pipelines()
204         vector<Pipeline> outputs;
206         for (auto conn : get_connectors())
207         {
208                 if (conn->connected() == false)
209                         continue;
211                 Crtc* crtc = conn->get_current_crtc();
213                 if (!crtc) {
214                         for (auto possible : conn->get_possible_crtcs()) {
215                                 if (find_if(outputs.begin(), outputs.end(), [possible](Pipeline out) { return out.crtc == possible; }) == outputs.end()) {
216                                         crtc = possible;
217                                         break;
218                                 }
219                         }
220                 }
222                 if (!crtc)
223                         throw invalid_argument(string("Connector #") +
224                                                to_string(conn->idx()) +
225                                                " has no possible crtcs");
227                 outputs.push_back(Pipeline { crtc, conn });
228         }
230         return outputs;