rename libkms to kms++
[android/external-libkmsxx.git] / kms++ / 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 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 Property* Card::get_prop(const string& name) const
148         for (auto prop : m_properties) {
149                 if (name == prop->name())
150                         return prop;
151         }
153         throw invalid_argument(string("Card property ") + name + " not found");
156 Connector* Card::get_first_connected_connector() const
158         for(auto c : m_connectors) {
159                 if (c->connected())
160                         return c;
161         }
163         throw invalid_argument("no connected connectors");
166 DrmObject* Card::get_object(uint32_t id) const
168         auto iter = m_obmap.find(id);
169         if (iter != m_obmap.end())
170                 return iter->second;
171         return nullptr;
174 const vector<DrmObject*> Card::get_objects() const
176         vector<DrmObject*> v;
177         for(auto pair : m_obmap)
178                 v.push_back(pair.second);
179         return v;
182 Connector* Card::get_connector(uint32_t id) const { return dynamic_cast<Connector*>(get_object(id)); }
183 Crtc* Card::get_crtc(uint32_t id) const { return dynamic_cast<Crtc*>(get_object(id)); }
184 Encoder* Card::get_encoder(uint32_t id) const { return dynamic_cast<Encoder*>(get_object(id)); }
185 Property* Card::get_prop(uint32_t id) const { return dynamic_cast<Property*>(get_object(id)); }
186 Plane* Card::get_plane(uint32_t id) const { return dynamic_cast<Plane*>(get_object(id)); }
188 std::vector<kms::Pipeline> Card::get_connected_pipelines()
190         vector<Pipeline> outputs;
192         for (auto conn : get_connectors())
193         {
194                 if (conn->connected() == false)
195                         continue;
197                 Crtc* crtc = conn->get_current_crtc();
199                 if (!crtc) {
200                         for (auto possible : conn->get_possible_crtcs()) {
201                                 if (find_if(outputs.begin(), outputs.end(), [possible](Pipeline out) { return out.crtc == possible; }) == outputs.end()) {
202                                         crtc = possible;
203                                         break;
204                                 }
205                         }
206                 }
208                 if (!crtc)
209                         throw invalid_argument(string("Connector #") +
210                                                to_string(conn->idx()) +
211                                                " has no possible crtcs");
213                 outputs.push_back(Pipeline { crtc, conn });
214         }
216         return outputs;
219 static void page_flip_handler(int fd, unsigned int frame,
220                               unsigned int sec, unsigned int usec,
221                               void *data)
223         auto handler = (PageFlipHandlerBase*)data;
224         double time = sec + usec / 1000000.0;
225         handler->handle_page_flip(frame, time);
228 void Card::call_page_flip_handlers()
230         drmEventContext ev = {
231                 .version = DRM_EVENT_CONTEXT_VERSION,
232                 .vblank_handler = 0,
233                 .page_flip_handler = page_flip_handler,
234         };
236         drmHandleEvent(fd(), &ev);