Fix property name lookup
[android/external-libkmsxx.git] / kms++ / src / 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++/kms++.h>
14 using namespace std;
16 namespace kms
17 {
19 Card::Card()
20         : Card("/dev/dri/card0")
21 {
22 }
25 Card::Card(const std::string& device)
26 {
27         int fd = open(device.c_str(), O_RDWR | O_CLOEXEC);
28         if (fd < 0)
29                 throw invalid_argument(string(strerror(errno)) + " opening " + device);
30         m_fd = fd;
32         int r;
34         r = drmSetMaster(fd);
35         m_master = r == 0;
37         if (getenv("KMSXX_DISABLE_UNIVERSAL_PLANES") == 0) {
38                 r = drmSetClientCap(m_fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
39                 m_has_universal_planes = r == 0;
40         } else {
41                 m_has_universal_planes = false;
42         }
44 #ifdef DRM_CLIENT_CAP_ATOMIC
45         if (getenv("KMSXX_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         }
51 #else
52         m_has_atomic = false;
53 #endif
55         uint64_t has_dumb;
56         r = drmGetCap(fd, DRM_CAP_DUMB_BUFFER, &has_dumb);
57         if (r || !has_dumb)
58                 throw invalid_argument("Dumb buffers not available");
60         auto res = drmModeGetResources(m_fd);
61         if (!res)
62                 throw invalid_argument("Can't get card resources");
64         for (int i = 0; i < res->count_connectors; ++i) {
65                 uint32_t id = res->connectors[i];
66                 auto ob = new Connector(*this, id, i);
67                 m_obmap[id] = ob;
68                 m_connectors.push_back(ob);
69         }
71         for (int i = 0; i < res->count_crtcs; ++i) {
72                 uint32_t id = res->crtcs[i];
73                 auto ob = new Crtc(*this, id, i);
74                 m_obmap[id] = ob;
75                 m_crtcs.push_back(ob);
76         }
78         for (int i = 0; i < res->count_encoders; ++i) {
79                 uint32_t id = res->encoders[i];
80                 auto ob = new Encoder(*this, id, i);
81                 m_obmap[id] = ob;
82                 m_encoders.push_back(ob);
83         }
85         drmModeFreeResources(res);
87         auto planeRes = drmModeGetPlaneResources(m_fd);
89         for (uint i = 0; i < planeRes->count_planes; ++i) {
90                 uint32_t id = planeRes->planes[i];
91                 auto ob = new Plane(*this, id, i);
92                 m_obmap[id] = ob;
93                 m_planes.push_back(ob);
94         }
96         drmModeFreePlaneResources(planeRes);
98         // collect all possible props
99         for (auto ob : get_objects()) {
100                 auto props = drmModeObjectGetProperties(m_fd, ob->id(), ob->object_type());
102                 if (props == nullptr)
103                         continue;
105                 for (unsigned i = 0; i < props->count_props; ++i) {
106                         uint32_t prop_id = props->props[i];
108                         if (m_obmap.find(prop_id) == m_obmap.end()) {
109                                 auto ob = new Property(*this, prop_id);
110                                 m_obmap[prop_id] = ob;
111                                 m_properties.push_back(ob);
112                         }
113                 }
115                 drmModeFreeObjectProperties(props);
116         }
118         for (auto pair : m_obmap)
119                 pair.second->setup();
122 Card::~Card()
124         restore_modes();
126         while (m_framebuffers.size() > 0)
127                 delete m_framebuffers.back();
129         for (auto pair : m_obmap)
130                 delete pair.second;
132         close(m_fd);
135 void Card::drop_master()
137         drmDropMaster(fd());
140 void Card::restore_modes()
142         for (auto conn : get_connectors())
143                 conn->restore_mode();
146 Connector* Card::get_first_connected_connector() const
148         for(auto c : m_connectors) {
149                 if (c->connected())
150                         return c;
151         }
153         throw invalid_argument("no connected connectors");
156 DrmObject* Card::get_object(uint32_t id) const
158         auto iter = m_obmap.find(id);
159         if (iter != m_obmap.end())
160                 return iter->second;
161         return nullptr;
164 const vector<DrmObject*> Card::get_objects() const
166         vector<DrmObject*> v;
167         for(auto pair : m_obmap)
168                 v.push_back(pair.second);
169         return v;
172 Connector* Card::get_connector(uint32_t id) const { return dynamic_cast<Connector*>(get_object(id)); }
173 Crtc* Card::get_crtc(uint32_t id) const { return dynamic_cast<Crtc*>(get_object(id)); }
174 Encoder* Card::get_encoder(uint32_t id) const { return dynamic_cast<Encoder*>(get_object(id)); }
175 Property* Card::get_prop(uint32_t id) const { return dynamic_cast<Property*>(get_object(id)); }
176 Plane* Card::get_plane(uint32_t id) const { return dynamic_cast<Plane*>(get_object(id)); }
178 std::vector<kms::Pipeline> Card::get_connected_pipelines()
180         vector<Pipeline> outputs;
182         for (auto conn : get_connectors())
183         {
184                 if (conn->connected() == false)
185                         continue;
187                 Crtc* crtc = conn->get_current_crtc();
189                 if (!crtc) {
190                         for (auto possible : conn->get_possible_crtcs()) {
191                                 if (find_if(outputs.begin(), outputs.end(), [possible](Pipeline out) { return out.crtc == possible; }) == outputs.end()) {
192                                         crtc = possible;
193                                         break;
194                                 }
195                         }
196                 }
198                 if (!crtc)
199                         throw invalid_argument(string("Connector #") +
200                                                to_string(conn->idx()) +
201                                                " has no possible crtcs");
203                 outputs.push_back(Pipeline { crtc, conn });
204         }
206         return outputs;
209 static void page_flip_handler(int fd, unsigned int frame,
210                               unsigned int sec, unsigned int usec,
211                               void *data)
213         auto handler = (PageFlipHandlerBase*)data;
214         double time = sec + usec / 1000000.0;
215         handler->handle_page_flip(frame, time);
218 void Card::call_page_flip_handlers()
220         drmEventContext ev = {
221                 .version = DRM_EVENT_CONTEXT_VERSION,
222                 .vblank_handler = 0,
223                 .page_flip_handler = page_flip_handler,
224         };
226         drmHandleEvent(fd(), &ev);