Allow disabling universal planes with LIBKMSXX_DISABLE_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         if (getenv("LIBKMSXX_DISABLE_UNIVERSAL_PLANES") == 0) {
39                 r = drmSetClientCap(m_fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
40                 m_has_universal_planes = r == 0;
41         } else {
42                 m_has_universal_planes = false;
43         }
45         if (getenv("LIBKMSXX_DISABLE_ATOMIC") == 0) {
46                 r = drmSetClientCap(m_fd, DRM_CLIENT_CAP_ATOMIC, 1);
47                 m_has_atomic = r == 0;
48         } else {
49                 m_has_atomic = false;
50         }
52         uint64_t has_dumb;
53         r = drmGetCap(fd, DRM_CAP_DUMB_BUFFER, &has_dumb);
54         if (r || !has_dumb)
55                 throw invalid_argument("Dumb buffers not available");
57         auto res = drmModeGetResources(m_fd);
58         if (!res)
59                 throw invalid_argument("Can't get card resources");
61         for (int i = 0; i < res->count_connectors; ++i) {
62                 uint32_t id = res->connectors[i];
63                 m_obmap[id] = new Connector(*this, id, i);
64         }
66         for (int i = 0; i < res->count_crtcs; ++i) {
67                 uint32_t id = res->crtcs[i];
68                 m_obmap[id] = new Crtc(*this, id, i);
69         }
71         for (int i = 0; i < res->count_encoders; ++i) {
72                 uint32_t id = res->encoders[i];
73                 m_obmap[id] = new Encoder(*this, id);
74         }
76         drmModeFreeResources(res);
78         auto planeRes = drmModeGetPlaneResources(m_fd);
80         for (uint i = 0; i < planeRes->count_planes; ++i) {
81                 uint32_t id = planeRes->planes[i];
82                 m_obmap[id] = new Plane(*this, id);
83         }
85         drmModeFreePlaneResources(planeRes);
87         // collect all possible props
88         for (auto ob : get_objects()) {
89                 auto props = drmModeObjectGetProperties(m_fd, ob->id(), ob->object_type());
91                 if (props == nullptr)
92                         continue;
94                 for (unsigned i = 0; i < props->count_props; ++i) {
95                         uint32_t prop_id = props->props[i];
97                         if (m_obmap.find(prop_id) == m_obmap.end())
98                                 m_obmap[prop_id] = new Property(*this, prop_id);
99                 }
101                 drmModeFreeObjectProperties(props);
102         }
104         for (auto pair : m_obmap)
105                 pair.second->setup();
108 Card::~Card()
110         for (auto pair : m_obmap)
111                 delete pair.second;
113         close(m_fd);
116 template <class T> static void print_obs(const map<uint32_t, DrmObject*>& obmap)
118         for (auto pair : obmap) {
119                 auto ob = pair.second;
120                 if (dynamic_cast<T*>(ob)) {
121                         ob->print_short();
122                         //ob->print_props();
123                 }
124         }
127 void Card::print_short() const
129         print_obs<Connector>(m_obmap);
130         print_obs<Encoder>(m_obmap);
131         print_obs<Crtc>(m_obmap);
132         print_obs<Plane>(m_obmap);
135 Property* Card::get_prop(const char *name) const
137         for (auto pair : m_obmap) {
138                 auto prop = dynamic_cast<Property*>(pair.second);
139                 if (!prop)
140                         continue;
142                 if (strcmp(name, prop->name()) == 0)
143                         return prop;
144         }
146         throw invalid_argument(string("Card property ") + name + " not found");
149 Connector* Card::get_first_connected_connector() const
151         for(auto pair : m_obmap) {
152                 auto c = dynamic_cast<Connector*>(pair.second);
154                 if (c && c->connected())
155                         return c;
156         }
158         throw invalid_argument("no connected connectors");
161 DrmObject* Card::get_object(uint32_t id) const
163         return m_obmap.at(id);
166 vector<Connector*> Card::get_connectors() const
168         vector<Connector*> v;
169         for(auto pair : m_obmap) {
170                 auto p = dynamic_cast<Connector*>(pair.second);
171                 if (p)
172                         v.push_back(p);
173         }
174         return v;
177 vector<Plane*> Card::get_planes() const
179         vector<Plane*> v;
180         for(auto pair : m_obmap) {
181                 auto p = dynamic_cast<Plane*>(pair.second);
182                 if (p)
183                         v.push_back(p);
184         }
185         return v;
188 vector<DrmObject*> Card::get_objects() const
190         vector<DrmObject*> v;
191         for(auto pair : m_obmap)
192                 v.push_back(pair.second);
193         return v;
196 Crtc* Card::get_crtc_by_index(uint32_t idx) const
198         for(auto pair : m_obmap) {
199                 auto crtc = dynamic_cast<Crtc*>(pair.second);
200                 if (crtc && crtc->idx() == idx)
201                         return crtc;
202         }
203         throw invalid_argument(string("Crtc #") + to_string(idx) + "not found");
206 Crtc* Card::get_crtc(uint32_t id) const { return dynamic_cast<Crtc*>(get_object(id)); }
207 Encoder* Card::get_encoder(uint32_t id) const { return dynamic_cast<Encoder*>(get_object(id)); }
208 Property* Card::get_prop(uint32_t id) const { return dynamic_cast<Property*>(get_object(id)); }
210 std::vector<kms::Pipeline> Card::get_connected_pipelines()
212         vector<Pipeline> outputs;
214         for (auto conn : get_connectors())
215         {
216                 if (conn->connected() == false)
217                         continue;
219                 Crtc* crtc = conn->get_current_crtc();
221                 if (!crtc) {
222                         for (auto possible : conn->get_possible_crtcs()) {
223                                 if (find_if(outputs.begin(), outputs.end(), [possible](Pipeline out) { return out.crtc == possible; }) == outputs.end()) {
224                                         crtc = possible;
225                                         break;
226                                 }
227                         }
228                 }
230                 if (!crtc)
231                         throw invalid_argument(string("Connector #") +
232                                                to_string(conn->idx()) +
233                                                " has no possible crtcs");
235                 outputs.push_back(Pipeline { crtc, conn });
236         }
238         return outputs;