summaryrefslogtreecommitdiff
path: root/libkms++/card.cpp
blob: 8438629b96166c4af9c73256fbe45236edbb4fd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <utility>
#include <stdexcept>
#include <string.h>
#include <algorithm>

#include <xf86drm.h>
#include <xf86drmMode.h>

#include "kms++.h"

#ifndef DRM_CLIENT_CAP_ATOMIC
#define DRM_CLIENT_CAP_ATOMIC 3
#endif

using namespace std;

namespace kms
{

Card::Card()
{
	const char *card = "/dev/dri/card0";

	int fd = open(card, O_RDWR | O_CLOEXEC);
	if (fd < 0)
		throw invalid_argument(string(strerror(errno)) + " opening " +
				       card);
	m_fd = fd;

	int r;

	r = drmSetMaster(fd);
	m_master = r == 0;

	if (getenv("LIBKMSXX_DISABLE_UNIVERSAL_PLANES") == 0) {
		r = drmSetClientCap(m_fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
		m_has_universal_planes = r == 0;
	} else {
		m_has_universal_planes = false;
	}

	if (getenv("LIBKMSXX_DISABLE_ATOMIC") == 0) {
		r = drmSetClientCap(m_fd, DRM_CLIENT_CAP_ATOMIC, 1);
		m_has_atomic = r == 0;
	} else {
		m_has_atomic = false;
	}

	uint64_t has_dumb;
	r = drmGetCap(fd, DRM_CAP_DUMB_BUFFER, &has_dumb);
	if (r || !has_dumb)
		throw invalid_argument("Dumb buffers not available");

	auto res = drmModeGetResources(m_fd);
	if (!res)
		throw invalid_argument("Can't get card resources");

	for (int i = 0; i < res->count_connectors; ++i) {
		uint32_t id = res->connectors[i];
		m_obmap[id] = new Connector(*this, id, i);
	}

	for (int i = 0; i < res->count_crtcs; ++i) {
		uint32_t id = res->crtcs[i];
		m_obmap[id] = new Crtc(*this, id, i);
	}

	for (int i = 0; i < res->count_encoders; ++i) {
		uint32_t id = res->encoders[i];
		m_obmap[id] = new Encoder(*this, id);
	}

	drmModeFreeResources(res);

	auto planeRes = drmModeGetPlaneResources(m_fd);

	for (uint i = 0; i < planeRes->count_planes; ++i) {
		uint32_t id = planeRes->planes[i];
		m_obmap[id] = new Plane(*this, id);
	}

	drmModeFreePlaneResources(planeRes);

	// collect all possible props
	for (auto ob : get_objects()) {
		auto props = drmModeObjectGetProperties(m_fd, ob->id(), ob->object_type());

		if (props == nullptr)
			continue;

		for (unsigned i = 0; i < props->count_props; ++i) {
			uint32_t prop_id = props->props[i];

			if (m_obmap.find(prop_id) == m_obmap.end())
				m_obmap[prop_id] = new Property(*this, prop_id);
		}

		drmModeFreeObjectProperties(props);
	}

	for (auto pair : m_obmap)
		pair.second->setup();
}

Card::~Card()
{
	restore_modes();

	for (auto pair : m_obmap)
		delete pair.second;

	close(m_fd);
}

void Card::restore_modes()
{
	for (auto conn : get_connectors())
		conn->restore_mode();
}

template <class T> static void print_obs(const map<uint32_t, DrmObject*>& obmap)
{
	for (auto pair : obmap) {
		auto ob = pair.second;
		if (dynamic_cast<T*>(ob)) {
			ob->print_short();
			//ob->print_props();
		}
	}
}

void Card::print_short() const
{
	print_obs<Connector>(m_obmap);
	print_obs<Encoder>(m_obmap);
	print_obs<Crtc>(m_obmap);
	print_obs<Plane>(m_obmap);
}

Property* Card::get_prop(const string& name) const
{
	for (auto pair : m_obmap) {
		auto prop = dynamic_cast<Property*>(pair.second);
		if (!prop)
			continue;

		if (name == prop->name())
			return prop;
	}

	throw invalid_argument(string("Card property ") + name + " not found");
}

Connector* Card::get_first_connected_connector() const
{
	for(auto pair : m_obmap) {
		auto c = dynamic_cast<Connector*>(pair.second);

		if (c && c->connected())
			return c;
	}

	throw invalid_argument("no connected connectors");
}

DrmObject* Card::get_object(uint32_t id) const
{
	return m_obmap.at(id);
}

vector<Connector*> Card::get_connectors() const
{
	vector<Connector*> v;
	for(auto pair : m_obmap) {
		auto p = dynamic_cast<Connector*>(pair.second);
		if (p)
			v.push_back(p);
	}
	return v;
}

vector<Plane*> Card::get_planes() const
{
	vector<Plane*> v;
	for(auto pair : m_obmap) {
		auto p = dynamic_cast<Plane*>(pair.second);
		if (p)
			v.push_back(p);
	}
	return v;
}

vector<DrmObject*> Card::get_objects() const
{
	vector<DrmObject*> v;
	for(auto pair : m_obmap)
		v.push_back(pair.second);
	return v;
}

Crtc* Card::get_crtc_by_index(uint32_t idx) const
{
	for(auto pair : m_obmap) {
		auto crtc = dynamic_cast<Crtc*>(pair.second);
		if (crtc && crtc->idx() == idx)
			return crtc;
	}
	throw invalid_argument(string("Crtc #") + to_string(idx) + "not found");
}

Crtc* Card::get_crtc(uint32_t id) const { return dynamic_cast<Crtc*>(get_object(id)); }
Encoder* Card::get_encoder(uint32_t id) const { return dynamic_cast<Encoder*>(get_object(id)); }
Property* Card::get_prop(uint32_t id) const { return dynamic_cast<Property*>(get_object(id)); }

std::vector<kms::Pipeline> Card::get_connected_pipelines()
{
	vector<Pipeline> outputs;

	for (auto conn : get_connectors())
	{
		if (conn->connected() == false)
			continue;

		Crtc* crtc = conn->get_current_crtc();

		if (!crtc) {
			for (auto possible : conn->get_possible_crtcs()) {
				if (find_if(outputs.begin(), outputs.end(), [possible](Pipeline out) { return out.crtc == possible; }) == outputs.end()) {
					crtc = possible;
					break;
				}
			}
		}

		if (!crtc)
			throw invalid_argument(string("Connector #") +
					       to_string(conn->idx()) +
					       " has no possible crtcs");

		outputs.push_back(Pipeline { crtc, conn });
	}

	return outputs;
}

static void page_flip_handler(int fd, unsigned int frame,
			      unsigned int sec, unsigned int usec,
			      void *data)
{
	auto handler = (PageFlipHandlerBase*)data;
	double time = sec + usec / 1000000.0;
	handler->handle_page_flip(frame, time);
}

void Card::call_page_flip_handlers()
{
	drmEventContext ev = {
		.version = DRM_EVENT_CONTEXT_VERSION,
		.vblank_handler = 0,
		.page_flip_handler = page_flip_handler,
	};

	drmHandleEvent(fd(), &ev);
}

}