• 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 #include <algorithm>
10 #include <glob.h>
11 
12 #include <xf86drm.h>
13 #include <xf86drmMode.h>
14 
15 #include <kms++/kms++.h>
16 
17 using namespace std;
18 
19 namespace kms
20 {
21 
glob(const string & pattern)22 static vector<string> glob(const string& pattern)
23 {
24 	glob_t glob_result;
25 	memset(&glob_result, 0, sizeof(glob_result));
26 
27 	int r = glob(pattern.c_str(), 0, NULL, &glob_result);
28 	if(r != 0) {
29 		globfree(&glob_result);
30 		throw runtime_error("failed to find DRM cards");
31 	}
32 
33 	vector<string> filenames;
34 	for(size_t i = 0; i < glob_result.gl_pathc; ++i)
35 		filenames.push_back(string(glob_result.gl_pathv[i]));
36 
37 	globfree(&glob_result);
38 
39 	return filenames;
40 }
41 
open_first_kms_device()42 static int open_first_kms_device()
43 {
44 	vector<string> paths = glob("/dev/dri/card*");
45 
46 	for (const string& path : paths) {
47 		int fd = open(path.c_str(), O_RDWR | O_CLOEXEC);
48 		if (fd < 0)
49 			throw invalid_argument(string(strerror(errno)) + " opening device " + path);
50 
51 		auto res = drmModeGetResources(fd);
52 		if (!res) {
53 			close(fd);
54 			continue;
55 		}
56 
57 		bool has_kms = res->count_crtcs > 0 && res->count_connectors > 0 && res->count_encoders > 0;
58 
59 		drmModeFreeResources(res);
60 
61 		if (has_kms)
62 			return fd;
63 
64 		close(fd);
65 	}
66 
67 	throw runtime_error("No modesetting DRM card found");
68 }
69 
open_device_by_path(string path)70 static int open_device_by_path(string path)
71 {
72 	int fd = open(path.c_str(), O_RDWR | O_CLOEXEC);
73 	if (fd < 0)
74 		throw invalid_argument(string(strerror(errno)) + " opening device " + path);
75 	return fd;
76 }
77 
78 // open Nth DRM card with the given driver name
open_device_by_driver(string name,uint32_t idx)79 static int open_device_by_driver(string name, uint32_t idx)
80 {
81 	transform(name.begin(), name.end(), name.begin(), ::tolower);
82 
83 	uint32_t num_matches = 0;
84 	vector<string> paths = glob("/dev/dri/card*");
85 
86 	for (const string& path : paths) {
87 		int fd = open_device_by_path(path);
88 
89 		drmVersionPtr ver = drmGetVersion(fd);
90 		string drv_name = string(ver->name, ver->name_len);
91 		drmFreeVersion(ver);
92 
93 		transform(drv_name.begin(), drv_name.end(), drv_name.begin(), ::tolower);
94 
95 		if (name == drv_name) {
96 			if (idx == num_matches)
97 				return fd;
98 			num_matches++;
99 		}
100 
101 		close(fd);
102 	}
103 
104 	throw invalid_argument("Failed to find a DRM device " + name + ":" + to_string(idx));
105 }
106 
Card(const std::string & dev_path)107 Card::Card(const std::string& dev_path)
108 {
109 	const char* drv_p = getenv("KMSXX_DRIVER");
110 	const char* dev_p = getenv("KMSXX_DEVICE");
111 
112 	if (!dev_path.empty()) {
113 		m_fd = open_device_by_path(dev_path);
114 	} else if (dev_p) {
115 		string dev(dev_p);
116 		m_fd = open_device_by_path(dev);
117 	} else if (drv_p) {
118 		string drv(drv_p);
119 
120 		auto isplit = find(drv.begin(), drv.end(), ':');
121 
122 		if (isplit == drv.begin())
123 			throw runtime_error("Invalid KMSXX_DRIVER");
124 
125 		string name;
126 		uint32_t num = 0;
127 
128 		if (isplit == drv.end()) {
129 			name = drv;
130 		} else {
131 			name = string(drv.begin(), isplit);
132 			string numstr = string(isplit + 1, drv.end());
133 			num = stoul(numstr);
134 		}
135 
136 		m_fd = open_device_by_driver(name, num);
137 	} else {
138 		m_fd = open_first_kms_device();
139 	}
140 
141 	setup();
142 }
143 
Card(const std::string & driver,uint32_t idx)144 Card::Card(const std::string& driver, uint32_t idx)
145 {
146 	m_fd = open_device_by_driver(driver, idx);
147 
148 	setup();
149 }
150 
setup()151 void Card::setup()
152 {
153 	drmVersionPtr ver = drmGetVersion(m_fd);
154 	m_version_major = ver->version_major;
155 	m_version_minor = ver->version_minor;
156 	m_version_patchlevel = ver->version_patchlevel;
157 	m_version_name = string(ver->name, ver->name_len);
158 	m_version_date = string(ver->date, ver->date_len);
159 	m_version_desc = string(ver->desc, ver->desc_len);
160 	drmFreeVersion(ver);
161 
162 	int r;
163 
164 	r = drmSetMaster(m_fd);
165 	m_is_master = r == 0;
166 
167 	if (getenv("KMSXX_DISABLE_UNIVERSAL_PLANES") == 0) {
168 		r = drmSetClientCap(m_fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
169 		m_has_universal_planes = r == 0;
170 	} else {
171 		m_has_universal_planes = false;
172 	}
173 
174 #ifdef DRM_CLIENT_CAP_ATOMIC
175 	if (getenv("KMSXX_DISABLE_ATOMIC") == 0) {
176 		r = drmSetClientCap(m_fd, DRM_CLIENT_CAP_ATOMIC, 1);
177 		m_has_atomic = r == 0;
178 	} else {
179 		m_has_atomic = false;
180 	}
181 #else
182 	m_has_atomic = false;
183 #endif
184 
185 	uint64_t has_dumb;
186 	r = drmGetCap(m_fd, DRM_CAP_DUMB_BUFFER, &has_dumb);
187 	m_has_dumb = r == 0 && has_dumb;
188 
189 	auto res = drmModeGetResources(m_fd);
190 	if (res) {
191 		for (int i = 0; i < res->count_connectors; ++i) {
192 			uint32_t id = res->connectors[i];
193 			auto ob = new Connector(*this, id, i);
194 			m_obmap[id] = ob;
195 			m_connectors.push_back(ob);
196 		}
197 
198 		for (int i = 0; i < res->count_crtcs; ++i) {
199 			uint32_t id = res->crtcs[i];
200 			auto ob = new Crtc(*this, id, i);
201 			m_obmap[id] = ob;
202 			m_crtcs.push_back(ob);
203 		}
204 
205 		for (int i = 0; i < res->count_encoders; ++i) {
206 			uint32_t id = res->encoders[i];
207 			auto ob = new Encoder(*this, id, i);
208 			m_obmap[id] = ob;
209 			m_encoders.push_back(ob);
210 		}
211 
212 		drmModeFreeResources(res);
213 
214 		auto planeRes = drmModeGetPlaneResources(m_fd);
215 		if (planeRes) {
216 			for (uint i = 0; i < planeRes->count_planes; ++i) {
217 				uint32_t id = planeRes->planes[i];
218 				auto ob = new Plane(*this, id, i);
219 				m_obmap[id] = ob;
220 				m_planes.push_back(ob);
221 			}
222 
223 			drmModeFreePlaneResources(planeRes);
224 		}
225 	}
226 
227 	// collect all possible props
228 	for (auto ob : get_objects()) {
229 		auto props = drmModeObjectGetProperties(m_fd, ob->id(), ob->object_type());
230 
231 		if (props == nullptr)
232 			continue;
233 
234 		for (unsigned i = 0; i < props->count_props; ++i) {
235 			uint32_t prop_id = props->props[i];
236 
237 			if (m_obmap.find(prop_id) == m_obmap.end()) {
238 				auto ob = new Property(*this, prop_id);
239 				m_obmap[prop_id] = ob;
240 				m_properties.push_back(ob);
241 			}
242 		}
243 
244 		drmModeFreeObjectProperties(props);
245 	}
246 
247 	for (auto pair : m_obmap)
248 		pair.second->setup();
249 }
250 
~Card()251 Card::~Card()
252 {
253 	restore_modes();
254 
255 	while (m_framebuffers.size() > 0)
256 		delete m_framebuffers.back();
257 
258 	for (auto pair : m_obmap)
259 		delete pair.second;
260 
261 	close(m_fd);
262 }
263 
drop_master()264 void Card::drop_master()
265 {
266 	drmDropMaster(fd());
267 	m_is_master = false;
268 }
269 
has_kms() const270 bool Card::has_kms() const
271 {
272 	return m_connectors.size() > 0 && m_encoders.size() > 0 && m_crtcs.size() > 0;
273 }
274 
restore_modes()275 void Card::restore_modes()
276 {
277 	for (auto conn : get_connectors())
278 		conn->restore_mode();
279 }
280 
get_first_connected_connector() const281 Connector* Card::get_first_connected_connector() const
282 {
283 	for(auto c : m_connectors) {
284 		if (c->connected())
285 			return c;
286 	}
287 
288 	throw invalid_argument("no connected connectors");
289 }
290 
get_object(uint32_t id) const291 DrmObject* Card::get_object(uint32_t id) const
292 {
293 	auto iter = m_obmap.find(id);
294 	if (iter != m_obmap.end())
295 		return iter->second;
296 	return nullptr;
297 }
298 
get_objects() const299 const vector<DrmObject*> Card::get_objects() const
300 {
301 	vector<DrmObject*> v;
302 	for(auto pair : m_obmap)
303 		v.push_back(pair.second);
304 	return v;
305 }
306 
get_connector(uint32_t id) const307 Connector* Card::get_connector(uint32_t id) const { return dynamic_cast<Connector*>(get_object(id)); }
get_crtc(uint32_t id) const308 Crtc* Card::get_crtc(uint32_t id) const { return dynamic_cast<Crtc*>(get_object(id)); }
get_encoder(uint32_t id) const309 Encoder* Card::get_encoder(uint32_t id) const { return dynamic_cast<Encoder*>(get_object(id)); }
get_prop(uint32_t id) const310 Property* Card::get_prop(uint32_t id) const { return dynamic_cast<Property*>(get_object(id)); }
get_plane(uint32_t id) const311 Plane* Card::get_plane(uint32_t id) const { return dynamic_cast<Plane*>(get_object(id)); }
312 
get_connected_pipelines()313 std::vector<kms::Pipeline> Card::get_connected_pipelines()
314 {
315 	vector<Pipeline> outputs;
316 
317 	for (auto conn : get_connectors())
318 	{
319 		if (conn->connected() == false)
320 			continue;
321 
322 		Crtc* crtc = conn->get_current_crtc();
323 
324 		if (!crtc) {
325 			for (auto possible : conn->get_possible_crtcs()) {
326 				if (find_if(outputs.begin(), outputs.end(), [possible](Pipeline out) { return out.crtc == possible; }) == outputs.end()) {
327 					crtc = possible;
328 					break;
329 				}
330 			}
331 		}
332 
333 		if (!crtc)
334 			throw invalid_argument(string("Connector #") +
335 					       to_string(conn->idx()) +
336 					       " has no possible crtcs");
337 
338 		outputs.push_back(Pipeline { crtc, conn });
339 	}
340 
341 	return outputs;
342 }
343 
page_flip_handler(int fd,unsigned int frame,unsigned int sec,unsigned int usec,void * data)344 static void page_flip_handler(int fd, unsigned int frame,
345 			      unsigned int sec, unsigned int usec,
346 			      void *data)
347 {
348 	auto handler = (PageFlipHandlerBase*)data;
349 	double time = sec + usec / 1000000.0;
350 	handler->handle_page_flip(frame, time);
351 }
352 
call_page_flip_handlers()353 void Card::call_page_flip_handlers()
354 {
355 	drmEventContext ev { };
356 	ev.version = DRM_EVENT_CONTEXT_VERSION;
357 	ev.page_flip_handler = page_flip_handler;
358 
359 	drmHandleEvent(fd(), &ev);
360 }
361 
disable_all()362 int Card::disable_all()
363 {
364 	AtomicReq req(*this);
365 
366 	for (Crtc* c : m_crtcs) {
367 		req.add(c, {
368 				{ "ACTIVE", 0 },
369 			});
370 	}
371 
372 	for (Plane* p : m_planes) {
373 		req.add(p, {
374 				{ "FB_ID", 0 },
375 				{ "CRTC_ID", 0 },
376 			});
377 	}
378 
379 	return req.commit_sync(true);
380 }
381 
382 }
383