Allow disabling atomic modeset with LIBKMSXX_DISABLE_ATOMIC
[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         if (getenv("LIBKMSXX_DISABLE_ATOMIC") == 0) {
42                 r = drmSetClientCap(m_fd, DRM_CLIENT_CAP_ATOMIC, 1);
43                 m_has_atomic = r == 0;
44         } else {
45                 m_has_atomic = false;
46         }
48         uint64_t has_dumb;
49         r = drmGetCap(fd, DRM_CAP_DUMB_BUFFER, &has_dumb);
50         if (r || !has_dumb)
51                 throw invalid_argument("Dumb buffers not available");
53         auto res = drmModeGetResources(m_fd);
54         if (!res)
55                 throw invalid_argument("Can't get card resources");
57         for (int i = 0; i < res->count_connectors; ++i) {
58                 uint32_t id = res->connectors[i];
59                 m_obmap[id] = new Connector(*this, id, i);
60         }
62         for (int i = 0; i < res->count_crtcs; ++i) {
63                 uint32_t id = res->crtcs[i];
64                 m_obmap[id] = new Crtc(*this, id, i);
65         }
67         for (int i = 0; i < res->count_encoders; ++i) {
68                 uint32_t id = res->encoders[i];
69                 m_obmap[id] = new Encoder(*this, id);
70         }
72         drmModeFreeResources(res);
74         auto planeRes = drmModeGetPlaneResources(m_fd);
76         for (uint i = 0; i < planeRes->count_planes; ++i) {
77                 uint32_t id = planeRes->planes[i];
78                 m_obmap[id] = new Plane(*this, id);
79         }
81         drmModeFreePlaneResources(planeRes);
83         // collect all possible props
84         for (auto ob : get_objects()) {
85                 auto props = drmModeObjectGetProperties(m_fd, ob->id(), ob->object_type());
87                 if (props == nullptr)
88                         continue;
90                 for (unsigned i = 0; i < props->count_props; ++i) {
91                         uint32_t prop_id = props->props[i];
93                         if (m_obmap.find(prop_id) == m_obmap.end())
94                                 m_obmap[prop_id] = new Property(*this, prop_id);
95                 }
97                 drmModeFreeObjectProperties(props);
98         }
100         for (auto pair : m_obmap)
101                 pair.second->setup();
104 Card::~Card()
106         for (auto pair : m_obmap)
107                 delete pair.second;
109         close(m_fd);
112 template <class T> static void print_obs(const map<uint32_t, DrmObject*>& obmap)
114         for (auto pair : obmap) {
115                 auto ob = pair.second;
116                 if (dynamic_cast<T*>(ob)) {
117                         ob->print_short();
118                         //ob->print_props();
119                 }
120         }
123 void Card::print_short() const
125         print_obs<Connector>(m_obmap);
126         print_obs<Encoder>(m_obmap);
127         print_obs<Crtc>(m_obmap);
128         print_obs<Plane>(m_obmap);
131 Property* Card::get_prop(const char *name) const
133         for (auto pair : m_obmap) {
134                 auto prop = dynamic_cast<Property*>(pair.second);
135                 if (!prop)
136                         continue;
138                 if (strcmp(name, prop->name()) == 0)
139                         return prop;
140         }
142         throw invalid_argument(string("Card property ") + name + " not found");
145 Connector* Card::get_first_connected_connector() const
147         for(auto pair : m_obmap) {
148                 auto c = dynamic_cast<Connector*>(pair.second);
150                 if (c && c->connected())
151                         return c;
152         }
154         throw invalid_argument("no connected connectors");
157 DrmObject* Card::get_object(uint32_t id) const
159         return m_obmap.at(id);
162 vector<Connector*> Card::get_connectors() const
164         vector<Connector*> v;
165         for(auto pair : m_obmap) {
166                 auto p = dynamic_cast<Connector*>(pair.second);
167                 if (p)
168                         v.push_back(p);
169         }
170         return v;
173 vector<Plane*> Card::get_planes() const
175         vector<Plane*> v;
176         for(auto pair : m_obmap) {
177                 auto p = dynamic_cast<Plane*>(pair.second);
178                 if (p)
179                         v.push_back(p);
180         }
181         return v;
184 vector<DrmObject*> Card::get_objects() const
186         vector<DrmObject*> v;
187         for(auto pair : m_obmap)
188                 v.push_back(pair.second);
189         return v;
192 Crtc* Card::get_crtc_by_index(uint32_t idx) const
194         for(auto pair : m_obmap) {
195                 auto crtc = dynamic_cast<Crtc*>(pair.second);
196                 if (crtc && crtc->idx() == idx)
197                         return crtc;
198         }
199         throw invalid_argument(string("Crtc #") + to_string(idx) + "not found");
202 Crtc* Card::get_crtc(uint32_t id) const { return dynamic_cast<Crtc*>(get_object(id)); }
203 Encoder* Card::get_encoder(uint32_t id) const { return dynamic_cast<Encoder*>(get_object(id)); }
204 Property* Card::get_prop(uint32_t id) const { return dynamic_cast<Property*>(get_object(id)); }
206 std::vector<kms::Pipeline> Card::get_connected_pipelines()
208         vector<Pipeline> outputs;
210         for (auto conn : get_connectors())
211         {
212                 if (conn->connected() == false)
213                         continue;
215                 Crtc* crtc = conn->get_current_crtc();
217                 if (!crtc) {
218                         for (auto possible : conn->get_possible_crtcs()) {
219                                 if (find_if(outputs.begin(), outputs.end(), [possible](Pipeline out) { return out.crtc == possible; }) == outputs.end()) {
220                                         crtc = possible;
221                                         break;
222                                 }
223                         }
224                 }
226                 if (!crtc)
227                         throw invalid_argument(string("Connector #") +
228                                                to_string(conn->idx()) +
229                                                " has no possible crtcs");
231                 outputs.push_back(Pipeline { crtc, conn });
232         }
234         return outputs;