• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <pybind11/pybind11.h>
2 #include <pybind11/stl.h>
3 #include <kms++/kms++.h>
4 
5 namespace py = pybind11;
6 
7 using namespace kms;
8 using namespace std;
9 
10 // Helper to convert vector<T*> to vector<unique_ptr<T, py::nodelete>>
11 template<typename T>
convert_vector(const vector<T * > & source)12 static vector<unique_ptr<T, py::nodelete>> convert_vector(const vector<T*>& source)
13 {
14 	vector<unique_ptr<T, py::nodelete>> v;
15 	for (T* p : source)
16 		v.push_back(unique_ptr<T, py::nodelete>(p));
17 	return v;
18 }
19 
init_pykmsbase(py::module & m)20 void init_pykmsbase(py::module& m)
21 {
22 	py::class_<Card>(m, "Card")
23 		.def(py::init<>())
24 		.def(py::init<const string&>())
25 		.def(py::init<const string&, uint32_t>())
26 		.def_property_readonly("fd", &Card::fd)
27 		.def_property_readonly("minor", &Card::dev_minor)
28 		.def_property_readonly("get_first_connected_connector", &Card::get_first_connected_connector)
29 
30 		// XXX pybind11 can't handle vector<T*> where T is non-copyable, and complains:
31 		// RuntimeError: return_value_policy = move, but the object is neither movable nor copyable!
32 		// So we do this manually.
33 		.def_property_readonly("connectors", [](Card* self) {
34 			return convert_vector(self->get_connectors());
35 		})
36 
37 		.def_property_readonly("crtcs", [](Card* self) {
38 			return convert_vector(self->get_crtcs());
39 		})
40 
41 		.def_property_readonly("encoders", [](Card* self) {
42 			return convert_vector(self->get_encoders());
43 		})
44 
45 		.def_property_readonly("planes", [](Card* self) {
46 			return convert_vector(self->get_planes());
47 		})
48 
49 		.def_property_readonly("properties", [](Card* self) {
50 			return convert_vector(self->get_properties());
51 		})
52 
53 		.def_property_readonly("has_atomic", &Card::has_atomic)
54 		.def("get_prop", (Property * (Card::*)(uint32_t) const) & Card::get_prop)
55 
56 		.def_property_readonly("version_name", &Card::version_name);
57 	;
58 
59 	py::class_<DrmObject, unique_ptr<DrmObject, py::nodelete>>(m, "DrmObject")
60 		.def_property_readonly("id", &DrmObject::id)
61 		.def_property_readonly("idx", &DrmObject::idx)
62 		.def_property_readonly("card", &DrmObject::card);
63 
64 	py::class_<DrmPropObject, DrmObject, unique_ptr<DrmPropObject, py::nodelete>>(m, "DrmPropObject")
65 		.def("refresh_props", &DrmPropObject::refresh_props)
66 		.def_property_readonly("prop_map", &DrmPropObject::get_prop_map)
67 		.def("get_prop_value", (uint64_t(DrmPropObject::*)(const string&) const) & DrmPropObject::get_prop_value)
68 		.def("set_prop_value", (int(DrmPropObject::*)(const string&, uint64_t)) & DrmPropObject::set_prop_value)
69 		.def("get_prop_value_as_blob", &DrmPropObject::get_prop_value_as_blob)
70 		.def("get_prop", &DrmPropObject::get_prop)
71 		.def("has_prop", &DrmPropObject::has_prop);
72 
73 	py::class_<Connector, DrmPropObject, unique_ptr<Connector, py::nodelete>>(m, "Connector")
74 		.def_property_readonly("fullname", &Connector::fullname)
75 		.def("get_default_mode", &Connector::get_default_mode)
76 		.def("get_current_crtc", &Connector::get_current_crtc)
77 		.def("get_possible_crtcs", [](Connector* self) {
78 			return convert_vector(self->get_possible_crtcs());
79 		})
80 		.def("get_modes", &Connector::get_modes)
81 		.def("get_mode", (Videomode(Connector::*)(const string& mode) const) & Connector::get_mode)
82 		.def("get_mode", (Videomode(Connector::*)(unsigned xres, unsigned yres, float refresh, bool ilace) const) & Connector::get_mode)
83 		.def("connected", &Connector::connected)
84 		.def("__repr__", [](const Connector& o) { return "<pykms.Connector " + to_string(o.id()) + ">"; })
85 		.def("refresh", &Connector::refresh);
86 
87 	py::class_<Crtc, DrmPropObject, unique_ptr<Crtc, py::nodelete>>(m, "Crtc")
88 		.def("set_mode", (int(Crtc::*)(Connector*, const Videomode&)) & Crtc::set_mode)
89 		.def("set_mode", (int(Crtc::*)(Connector*, Framebuffer&, const Videomode&)) & Crtc::set_mode)
90 		.def("disable_mode", &Crtc::disable_mode)
91 		.def(
92 			"page_flip",
93 			[](Crtc* self, Framebuffer& fb, uint32_t data) {
94 				self->page_flip(fb, (void*)(intptr_t)data);
95 			},
96 			py::arg("fb"), py::arg("data") = 0)
97 		.def("set_plane", &Crtc::set_plane)
98 		.def_property_readonly("possible_planes", &Crtc::get_possible_planes)
99 		.def_property_readonly("primary_plane", &Crtc::get_primary_plane)
100 		.def_property_readonly("mode", &Crtc::mode)
101 		.def_property_readonly("mode_valid", &Crtc::mode_valid)
102 		.def("__repr__", [](const Crtc& o) { return "<pykms.Crtc " + to_string(o.id()) + ">"; })
103 		.def("refresh", &Crtc::refresh)
104 		.def("legacy_gamma_size", &Crtc::legacy_gamma_size)
105 		.def("legacy_gamma_set", &Crtc::legacy_gamma_set);
106 
107 	py::class_<Encoder, DrmPropObject, unique_ptr<Encoder, py::nodelete>>(m, "Encoder")
108 		.def("refresh", &Encoder::refresh);
109 
110 	py::class_<Plane, DrmPropObject, unique_ptr<Plane, py::nodelete>>(m, "Plane")
111 		.def("supports_crtc", &Plane::supports_crtc)
112 		.def_property_readonly("formats", &Plane::get_formats)
113 		.def_property_readonly("plane_type", &Plane::plane_type)
114 		.def("__repr__", [](const Plane& o) { return "<pykms.Plane " + to_string(o.id()) + ">"; });
115 
116 	py::enum_<PlaneType>(m, "PlaneType")
117 		.value("Overlay", PlaneType::Overlay)
118 		.value("Primary", PlaneType::Primary)
119 		.value("Cursor", PlaneType::Cursor);
120 
121 	py::class_<Property, DrmObject, unique_ptr<Property, py::nodelete>>(m, "Property")
122 		.def_property_readonly("name", &Property::name)
123 		.def_property_readonly("type", &Property::type)
124 		.def_property_readonly("enums", &Property::get_enums)
125 		.def_property_readonly("values", &Property::get_values)
126 		.def("__repr__", [](const Property& o) { return "<pykms.Property " + to_string(o.id()) + " '" + o.name() + "'>"; });
127 
128 	py::enum_<PropertyType>(m, "PropertyType")
129 		.value("Range", PropertyType::Range)
130 		.value("Enum", PropertyType::Enum)
131 		.value("Blob", PropertyType::Blob)
132 		.value("Bitmask", PropertyType::Bitmask)
133 		.value("Object", PropertyType::Object)
134 		.value("SignedRange", PropertyType::SignedRange);
135 
136 	py::class_<Blob>(m, "Blob")
137 		.def(py::init([](Card& card, py::buffer buf) {
138 			     py::buffer_info info = buf.request();
139 			     if (info.ndim != 1)
140 				     throw std::runtime_error("Incompatible buffer dimension!");
141 
142 			     return new Blob(card, info.ptr, info.size * info.itemsize);
143 		     }),
144 		     py::keep_alive<1, 2>()) // Keep Card alive until this is destructed
145 
146 		.def_property_readonly("data", &Blob::data)
147 
148 		// XXX pybind11 doesn't support a base object (DrmObject) with custom holder-type,
149 		// and a subclass with standard holder-type.
150 		// So we just copy the DrmObject members here.
151 		// Note that this means that python thinks we don't derive from DrmObject
152 		.def_property_readonly("id", &DrmObject::id)
153 		.def_property_readonly("idx", &DrmObject::idx)
154 		.def_property_readonly("card", &DrmObject::card);
155 
156 	py::class_<Framebuffer>(m, "Framebuffer")
157 		.def_property_readonly("width", &Framebuffer::width)
158 		.def_property_readonly("height", &Framebuffer::height)
159 		.def_property_readonly("format", &Framebuffer::format)
160 		.def_property_readonly("num_planes", &Framebuffer::num_planes)
161 		.def("stride", &Framebuffer::stride)
162 		.def("size", &Framebuffer::size)
163 		.def("offset", &Framebuffer::offset)
164 		.def("fd", &Framebuffer::prime_fd)
165 
166 		.def("flush", (void(Framebuffer::*)(void)) & Framebuffer::flush)
167 		.def("flush", (void(Framebuffer::*)(uint32_t x, uint32_t y, uint32_t width, uint32_t height)) & Framebuffer::flush)
168 
169 		// XXX pybind11 doesn't support a base object (DrmObject) with custom holder-type,
170 		// and a subclass with standard holder-type.
171 		// So we just copy the DrmObject members here.
172 		// Note that this means that python thinks we don't derive from DrmObject
173 		.def_property_readonly("id", &DrmObject::id)
174 		.def_property_readonly("idx", &DrmObject::idx)
175 		.def_property_readonly("card", &DrmObject::card)
176 		.def("map", [](Framebuffer& self, uint32_t plane) {
177 			const auto& format_info = get_pixel_format_info(self.format());
178 
179 			if (plane >= format_info.num_planes)
180 				throw runtime_error("map: bad plane number");
181 
182 			array<uint32_t, 2> shape{ self.height(), self.width() * format_info.planes[plane].bitspp / 8 };
183 			array<uint32_t, 2> strides{ self.stride(plane), sizeof(uint8_t) };
184 
185 			return py::memoryview::from_buffer(self.map(plane), shape, strides);
186 		});
187 
188 	py::class_<DumbFramebuffer, Framebuffer>(m, "DumbFramebuffer")
189 		.def(py::init<Card&, uint32_t, uint32_t, const string&>(),
190 		     py::keep_alive<1, 2>()) // Keep Card alive until this is destructed
191 		.def(py::init<Card&, uint32_t, uint32_t, PixelFormat>(),
192 		     py::keep_alive<1, 2>()) // Keep Card alive until this is destructed
193 		.def("__repr__", [](const DumbFramebuffer& o) { return "<pykms.DumbFramebuffer " + to_string(o.id()) + ">"; });
194 
195 	py::class_<DmabufFramebuffer, Framebuffer>(m, "DmabufFramebuffer")
196 		.def(py::init<Card&, uint32_t, uint32_t, const string&, vector<int>, vector<uint32_t>, vector<uint32_t>>(),
197 		     py::keep_alive<1, 2>()) // Keep Card alive until this is destructed
198 		.def(py::init<Card&, uint32_t, uint32_t, PixelFormat, vector<int>, vector<uint32_t>, vector<uint32_t>>(),
199 		     py::keep_alive<1, 2>()) // Keep Card alive until this is destructed
200 		.def("__repr__", [](const DmabufFramebuffer& o) { return "<pykms.DmabufFramebuffer " + to_string(o.id()) + ">"; });
201 
202 	py::enum_<PixelFormat>(m, "PixelFormat")
203 		.value("Undefined", PixelFormat::Undefined)
204 
205 		.value("NV12", PixelFormat::NV12)
206 		.value("NV21", PixelFormat::NV21)
207 		.value("NV16", PixelFormat::NV16)
208 		.value("NV61", PixelFormat::NV61)
209 
210 		.value("YUV420", PixelFormat::YUV420)
211 		.value("YVU420", PixelFormat::YVU420)
212 		.value("YUV422", PixelFormat::YUV422)
213 		.value("YVU422", PixelFormat::YVU422)
214 		.value("YUV444", PixelFormat::YUV444)
215 		.value("YVU444", PixelFormat::YVU444)
216 
217 		.value("UYVY", PixelFormat::UYVY)
218 		.value("YUYV", PixelFormat::YUYV)
219 		.value("YVYU", PixelFormat::YVYU)
220 		.value("VYUY", PixelFormat::VYUY)
221 
222 		.value("XRGB8888", PixelFormat::XRGB8888)
223 		.value("XBGR8888", PixelFormat::XBGR8888)
224 		.value("RGBX8888", PixelFormat::RGBX8888)
225 		.value("BGRX8888", PixelFormat::BGRX8888)
226 
227 		.value("ARGB8888", PixelFormat::ARGB8888)
228 		.value("ABGR8888", PixelFormat::ABGR8888)
229 		.value("RGBA8888", PixelFormat::RGBA8888)
230 		.value("BGRA8888", PixelFormat::BGRA8888)
231 
232 		.value("RGB888", PixelFormat::RGB888)
233 		.value("BGR888", PixelFormat::BGR888)
234 
235 		.value("RGB332", PixelFormat::RGB332)
236 
237 		.value("RGB565", PixelFormat::RGB565)
238 		.value("BGR565", PixelFormat::BGR565)
239 
240 		.value("XRGB4444", PixelFormat::XRGB4444)
241 		.value("XRGB1555", PixelFormat::XRGB1555)
242 
243 		.value("ARGB4444", PixelFormat::ARGB4444)
244 		.value("ARGB1555", PixelFormat::ARGB1555)
245 
246 		.value("XRGB2101010", PixelFormat::XRGB2101010)
247 		.value("XBGR2101010", PixelFormat::XBGR2101010)
248 		.value("RGBX1010102", PixelFormat::RGBX1010102)
249 		.value("BGRX1010102", PixelFormat::BGRX1010102)
250 
251 		.value("ARGB2101010", PixelFormat::ARGB2101010)
252 		.value("ABGR2101010", PixelFormat::ABGR2101010)
253 		.value("RGBA1010102", PixelFormat::RGBA1010102)
254 		.value("BGRA1010102", PixelFormat::BGRA1010102);
255 
256 	m.def("fourcc_to_pixelformat", &FourCCToPixelFormat);
257 	m.def("pixelformat_to_fourcc", &PixelFormatToFourCC);
258 
259 	py::enum_<SyncPolarity>(m, "SyncPolarity")
260 		.value("Undefined", SyncPolarity::Undefined)
261 		.value("Positive", SyncPolarity::Positive)
262 		.value("Negative", SyncPolarity::Negative);
263 
264 	py::class_<Videomode>(m, "Videomode")
265 		.def(py::init<>())
266 
267 		.def_readwrite("name", &Videomode::name)
268 
269 		.def_readwrite("clock", &Videomode::clock)
270 
271 		.def_readwrite("hdisplay", &Videomode::hdisplay)
272 		.def_readwrite("hsync_start", &Videomode::hsync_start)
273 		.def_readwrite("hsync_end", &Videomode::hsync_end)
274 		.def_readwrite("htotal", &Videomode::htotal)
275 
276 		.def_readwrite("vdisplay", &Videomode::vdisplay)
277 		.def_readwrite("vsync_start", &Videomode::vsync_start)
278 		.def_readwrite("vsync_end", &Videomode::vsync_end)
279 		.def_readwrite("vtotal", &Videomode::vtotal)
280 
281 		.def_readwrite("vrefresh", &Videomode::vrefresh)
282 
283 		.def_readwrite("flags", &Videomode::flags)
284 		.def_readwrite("type", &Videomode::type)
285 
286 		.def("__repr__", [](const Videomode& vm) { return "<pykms.Videomode " + to_string(vm.hdisplay) + "x" + to_string(vm.vdisplay) + ">"; })
287 
288 		.def("to_blob", &Videomode::to_blob)
289 
290 		.def_property("hsync", &Videomode::hsync, &Videomode::set_hsync)
291 		.def_property("vsync", &Videomode::vsync, &Videomode::set_vsync)
292 
293 		.def("to_string_short", &Videomode::to_string_short)
294 		.def("to_string_long", &Videomode::to_string_long);
295 
296 	m.def("videomode_from_timings", &videomode_from_timings);
297 
298 	py::class_<AtomicReq>(m, "AtomicReq")
299 		.def(py::init<Card&>(),
300 		     py::keep_alive<1, 2>()) // Keep Card alive until this is destructed
301 		.def("add", (void(AtomicReq::*)(DrmPropObject*, const string&, uint64_t)) & AtomicReq::add)
302 		.def("add", (void(AtomicReq::*)(DrmPropObject*, Property*, uint64_t)) & AtomicReq::add)
303 		.def("add", (void(AtomicReq::*)(DrmPropObject*, const map<string, uint64_t>&)) & AtomicReq::add)
304 		.def("test", &AtomicReq::test, py::arg("allow_modeset") = false)
305 		.def(
306 			"commit",
307 			[](AtomicReq* self, uint32_t data, bool allow) {
308 				return self->commit((void*)(intptr_t)data, allow);
309 			},
310 			py::arg("data") = 0, py::arg("allow_modeset") = false)
311 		.def("commit_sync", &AtomicReq::commit_sync, py::arg("allow_modeset") = false);
312 
313 	py::class_<PixelFormatPlaneInfo>(m, "PixelFormatPlaneInfo")
314 		.def_readonly("bitspp", &PixelFormatPlaneInfo::bitspp)
315 		.def_readonly("xsub", &PixelFormatPlaneInfo::xsub)
316 		.def_readonly("ysub", &PixelFormatPlaneInfo::ysub);
317 
318 	py::class_<PixelFormatInfo>(m, "PixelFormatInfo")
319 		.def_readonly("num_planes", &PixelFormatInfo::num_planes)
320 		.def(
321 			"plane", [](const PixelFormatInfo& self, uint32_t idx) {
322 				if (idx >= self.num_planes)
323 					throw runtime_error("invalid plane number");
324 				return self.planes[idx];
325 			},
326 			py::return_value_policy::reference_internal);
327 
328 	m.def("get_pixel_format_info", &get_pixel_format_info,
329 	      py::return_value_policy::reference_internal);
330 }
331