card: use object vectors
[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                 auto ob = new Connector(*this, id, i);
64                 m_obmap[id] = ob;
65                 m_connectors.push_back(ob);
66         }
68         for (int i = 0; i < res->count_crtcs; ++i) {
69                 uint32_t id = res->crtcs[i];
70                 auto ob = new Crtc(*this, id, i);
71                 m_obmap[id] = ob;
72                 m_crtcs.push_back(ob);
73         }
75         for (int i = 0; i < res->count_encoders; ++i) {
76                 uint32_t id = res->encoders[i];
77                 auto ob = new Encoder(*this, id);
78                 m_obmap[id] = ob;
79                 m_encoders.push_back(ob);
80         }
82         drmModeFreeResources(res);
84         auto planeRes = drmModeGetPlaneResources(m_fd);
86         for (uint i = 0; i < planeRes->count_planes; ++i) {
87                 uint32_t id = planeRes->planes[i];
88                 auto ob = new Plane(*this, id);
89                 m_obmap[id] = ob;
90                 m_planes.push_back(ob);
91         }
93         drmModeFreePlaneResources(planeRes);
95         // collect all possible props
96         for (auto ob : get_objects()) {
97                 auto props = drmModeObjectGetProperties(m_fd, ob->id(), ob->object_type());
99                 if (props == nullptr)
100                         continue;
102                 for (unsigned i = 0; i < props->count_props; ++i) {
103                         uint32_t prop_id = props->props[i];
105                         if (m_obmap.find(prop_id) == m_obmap.end()) {
106                                 auto ob = new Property(*this, prop_id);
107                                 m_obmap[prop_id] = ob;
108                                 m_properties.push_back(ob);
109                         }
110                 }
112                 drmModeFreeObjectProperties(props);
113         }
115         for (auto pair : m_obmap)
116                 pair.second->setup();
119 Card::~Card()
121         restore_modes();
123         for (auto pair : m_obmap)
124                 delete pair.second;
126         close(m_fd);
129 void Card::restore_modes()
131         for (auto conn : get_connectors())
132                 conn->restore_mode();
135 template <class T> static void print_obs(const map<uint32_t, DrmObject*>& obmap)
137         for (auto pair : obmap) {
138                 auto ob = pair.second;
139                 if (dynamic_cast<T*>(ob)) {
140                         ob->print_short();
141                         //ob->print_props();
142                 }
143         }
146 void Card::print_short() const
148         print_obs<Connector>(m_obmap);
149         print_obs<Encoder>(m_obmap);
150         print_obs<Crtc>(m_obmap);
151         print_obs<Plane>(m_obmap);
154 Property* Card::get_prop(const string& name) const
156         for (auto prop : m_properties) {
157                 if (name == prop->name())
158                         return prop;
159         }
161         throw invalid_argument(string("Card property ") + name + " not found");
164 Connector* Card::get_first_connected_connector() const
166         for(auto c : m_connectors) {
167                 if (c->connected())
168                         return c;
169         }
171         throw invalid_argument("no connected connectors");
174 DrmObject* Card::get_object(uint32_t id) const
176         return m_obmap.at(id);
179 const vector<DrmObject*> Card::get_objects() const
181         vector<DrmObject*> v;
182         for(auto pair : m_obmap)
183                 v.push_back(pair.second);
184         return v;
187 Crtc* Card::get_crtc_by_index(uint32_t idx) const
189         return m_crtcs[idx];
192 Connector* Card::get_connector(uint32_t id) const { return dynamic_cast<Connector*>(get_object(id)); }
193 Crtc* Card::get_crtc(uint32_t id) const { return dynamic_cast<Crtc*>(get_object(id)); }
194 Encoder* Card::get_encoder(uint32_t id) const { return dynamic_cast<Encoder*>(get_object(id)); }
195 Property* Card::get_prop(uint32_t id) const { return dynamic_cast<Property*>(get_object(id)); }
196 Plane* Card::get_plane(uint32_t id) const { return dynamic_cast<Plane*>(get_object(id)); }
198 std::vector<kms::Pipeline> Card::get_connected_pipelines()
200         vector<Pipeline> outputs;
202         for (auto conn : get_connectors())
203         {
204                 if (conn->connected() == false)
205                         continue;
207                 Crtc* crtc = conn->get_current_crtc();
209                 if (!crtc) {
210                         for (auto possible : conn->get_possible_crtcs()) {
211                                 if (find_if(outputs.begin(), outputs.end(), [possible](Pipeline out) { return out.crtc == possible; }) == outputs.end()) {
212                                         crtc = possible;
213                                         break;
214                                 }
215                         }
216                 }
218                 if (!crtc)
219                         throw invalid_argument(string("Connector #") +
220                                                to_string(conn->idx()) +
221                                                " has no possible crtcs");
223                 outputs.push_back(Pipeline { crtc, conn });
224         }
226         return outputs;
229 static void page_flip_handler(int fd, unsigned int frame,
230                               unsigned int sec, unsigned int usec,
231                               void *data)
233         auto handler = (PageFlipHandlerBase*)data;
234         double time = sec + usec / 1000000.0;
235         handler->handle_page_flip(frame, time);
238 void Card::call_page_flip_handlers()
240         drmEventContext ev = {
241                 .version = DRM_EVENT_CONTEXT_VERSION,
242                 .vblank_handler = 0,
243                 .page_flip_handler = page_flip_handler,
244         };
246         drmHandleEvent(fd(), &ev);