• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>
8 #include <cerrno>
9 
10 #include <xf86drm.h>
11 #include <xf86drmMode.h>
12 
13 #include <kms++/kms++.h>
14 
15 using namespace std;
16 
17 namespace kms
18 {
19 
Card()20 Card::Card()
21 	: Card("/dev/dri/card0")
22 {
23 }
24 
25 
Card(const std::string & device)26 Card::Card(const std::string& device)
27 {
28 	int fd = open(device.c_str(), O_RDWR | O_CLOEXEC);
29 	if (fd < 0)
30 		throw invalid_argument(string(strerror(errno)) + " opening " + device);
31 	m_fd = fd;
32 
33 	int r;
34 
35 	r = drmSetMaster(fd);
36 	m_master = r == 0;
37 
38 	if (getenv("KMSXX_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 	}
44 
45 #ifdef DRM_CLIENT_CAP_ATOMIC
46 	if (getenv("KMSXX_DISABLE_ATOMIC") == 0) {
47 		r = drmSetClientCap(m_fd, DRM_CLIENT_CAP_ATOMIC, 1);
48 		m_has_atomic = r == 0;
49 	} else {
50 		m_has_atomic = false;
51 	}
52 #else
53 	m_has_atomic = false;
54 #endif
55 
56 	uint64_t has_dumb;
57 	r = drmGetCap(fd, DRM_CAP_DUMB_BUFFER, &has_dumb);
58 	if (r || !has_dumb)
59 		throw invalid_argument("Dumb buffers not available");
60 
61 	auto res = drmModeGetResources(m_fd);
62 	if (!res)
63 		throw invalid_argument("Can't get card resources");
64 
65 	for (int i = 0; i < res->count_connectors; ++i) {
66 		uint32_t id = res->connectors[i];
67 		auto ob = new Connector(*this, id, i);
68 		m_obmap[id] = ob;
69 		m_connectors.push_back(ob);
70 	}
71 
72 	for (int i = 0; i < res->count_crtcs; ++i) {
73 		uint32_t id = res->crtcs[i];
74 		auto ob = new Crtc(*this, id, i);
75 		m_obmap[id] = ob;
76 		m_crtcs.push_back(ob);
77 	}
78 
79 	for (int i = 0; i < res->count_encoders; ++i) {
80 		uint32_t id = res->encoders[i];
81 		auto ob = new Encoder(*this, id, i);
82 		m_obmap[id] = ob;
83 		m_encoders.push_back(ob);
84 	}
85 
86 	drmModeFreeResources(res);
87 
88 	auto planeRes = drmModeGetPlaneResources(m_fd);
89 
90 	for (uint i = 0; i < planeRes->count_planes; ++i) {
91 		uint32_t id = planeRes->planes[i];
92 		auto ob = new Plane(*this, id, i);
93 		m_obmap[id] = ob;
94 		m_planes.push_back(ob);
95 	}
96 
97 	drmModeFreePlaneResources(planeRes);
98 
99 	// collect all possible props
100 	for (auto ob : get_objects()) {
101 		auto props = drmModeObjectGetProperties(m_fd, ob->id(), ob->object_type());
102 
103 		if (props == nullptr)
104 			continue;
105 
106 		for (unsigned i = 0; i < props->count_props; ++i) {
107 			uint32_t prop_id = props->props[i];
108 
109 			if (m_obmap.find(prop_id) == m_obmap.end()) {
110 				auto ob = new Property(*this, prop_id);
111 				m_obmap[prop_id] = ob;
112 				m_properties.push_back(ob);
113 			}
114 		}
115 
116 		drmModeFreeObjectProperties(props);
117 	}
118 
119 	for (auto pair : m_obmap)
120 		pair.second->setup();
121 }
122 
~Card()123 Card::~Card()
124 {
125 	restore_modes();
126 
127 	while (m_framebuffers.size() > 0)
128 		delete m_framebuffers.back();
129 
130 	for (auto pair : m_obmap)
131 		delete pair.second;
132 
133 	close(m_fd);
134 }
135 
drop_master()136 void Card::drop_master()
137 {
138 	drmDropMaster(fd());
139 }
140 
restore_modes()141 void Card::restore_modes()
142 {
143 	for (auto conn : get_connectors())
144 		conn->restore_mode();
145 }
146 
get_first_connected_connector() const147 Connector* Card::get_first_connected_connector() const
148 {
149 	for(auto c : m_connectors) {
150 		if (c->connected())
151 			return c;
152 	}
153 
154 	throw invalid_argument("no connected connectors");
155 }
156 
get_object(uint32_t id) const157 DrmObject* Card::get_object(uint32_t id) const
158 {
159 	auto iter = m_obmap.find(id);
160 	if (iter != m_obmap.end())
161 		return iter->second;
162 	return nullptr;
163 }
164 
get_objects() const165 const vector<DrmObject*> Card::get_objects() const
166 {
167 	vector<DrmObject*> v;
168 	for(auto pair : m_obmap)
169 		v.push_back(pair.second);
170 	return v;
171 }
172 
get_connector(uint32_t id) const173 Connector* Card::get_connector(uint32_t id) const { return dynamic_cast<Connector*>(get_object(id)); }
get_crtc(uint32_t id) const174 Crtc* Card::get_crtc(uint32_t id) const { return dynamic_cast<Crtc*>(get_object(id)); }
get_encoder(uint32_t id) const175 Encoder* Card::get_encoder(uint32_t id) const { return dynamic_cast<Encoder*>(get_object(id)); }
get_prop(uint32_t id) const176 Property* Card::get_prop(uint32_t id) const { return dynamic_cast<Property*>(get_object(id)); }
get_plane(uint32_t id) const177 Plane* Card::get_plane(uint32_t id) const { return dynamic_cast<Plane*>(get_object(id)); }
178 
get_connected_pipelines()179 std::vector<kms::Pipeline> Card::get_connected_pipelines()
180 {
181 	vector<Pipeline> outputs;
182 
183 	for (auto conn : get_connectors())
184 	{
185 		if (conn->connected() == false)
186 			continue;
187 
188 		Crtc* crtc = conn->get_current_crtc();
189 
190 		if (!crtc) {
191 			for (auto possible : conn->get_possible_crtcs()) {
192 				if (find_if(outputs.begin(), outputs.end(), [possible](Pipeline out) { return out.crtc == possible; }) == outputs.end()) {
193 					crtc = possible;
194 					break;
195 				}
196 			}
197 		}
198 
199 		if (!crtc)
200 			throw invalid_argument(string("Connector #") +
201 					       to_string(conn->idx()) +
202 					       " has no possible crtcs");
203 
204 		outputs.push_back(Pipeline { crtc, conn });
205 	}
206 
207 	return outputs;
208 }
209 
page_flip_handler(int fd,unsigned int frame,unsigned int sec,unsigned int usec,void * data)210 static void page_flip_handler(int fd, unsigned int frame,
211 			      unsigned int sec, unsigned int usec,
212 			      void *data)
213 {
214 	auto handler = (PageFlipHandlerBase*)data;
215 	double time = sec + usec / 1000000.0;
216 	handler->handle_page_flip(frame, time);
217 }
218 
call_page_flip_handlers()219 void Card::call_page_flip_handlers()
220 {
221 	drmEventContext ev { };
222 	ev.version = DRM_EVENT_CONTEXT_VERSION;
223 	ev.page_flip_handler = page_flip_handler;
224 
225 	drmHandleEvent(fd(), &ev);
226 }
227 
disable_all()228 int Card::disable_all()
229 {
230 	AtomicReq req(*this);
231 
232 	for (Crtc* c : m_crtcs) {
233 		req.add(c, {
234 				{ "ACTIVE", 0 },
235 			});
236 	}
237 
238 	for (Plane* p : m_planes) {
239 		req.add(p, {
240 				{ "FB_ID", 0 },
241 				{ "CRTC_ID", 0 },
242 			});
243 	}
244 
245 	return req.commit_sync(true);
246 }
247 
248 }
249