79c502ccc8aebfcb381ff8742237c16d1f33df3a
[android/external-libkmsxx.git] / py / pykms / pykmsbase.cpp
1 #include <pybind11/pybind11.h>
2 #include <pybind11/stl.h>
3 #include <kms++/kms++.h>
5 namespace py = pybind11;
7 using namespace kms;
8 using namespace std;
10 void init_pykmsbase(py::module &m)
11 {
12         py::class_<Card>(m, "Card")
13                         .def(py::init<>())
14                         .def_property_readonly("fd", &Card::fd)
15                         .def_property_readonly("get_first_connected_connector", &Card::get_first_connected_connector)
17                         // XXX pybind11 can't handle vector<T*> where T is non-copyable, and complains:
18                         // RuntimeError: return_value_policy = move, but the object is neither movable nor copyable!
19                         // So we do this manually.
20                         .def_property_readonly("connectors", [](Card* self) {
21                                 vector<unique_ptr<Connector, py::nodelete>> v;
22                                 for (Connector* p : self->get_connectors())
23                                         v.push_back(unique_ptr<Connector, py::nodelete>(p));
24                                 return v;
25                         })
27                         .def_property_readonly("planes", [](Card* self) {
28                                 vector<unique_ptr<Plane, py::nodelete>> v;
29                                 for (Plane* p : self->get_planes())
30                                         v.push_back(unique_ptr<Plane, py::nodelete>(p));
31                                 return v;
32                         })
34                         .def_property_readonly("has_atomic", &Card::has_atomic)
35                         .def("get_prop", (Property* (Card::*)(uint32_t) const)&Card::get_prop)
36                         ;
38         py::class_<DrmObject, unique_ptr<DrmObject, py::nodelete>>(m, "DrmObject")
39                         .def_property_readonly("id", &DrmObject::id)
40                         .def_property_readonly("idx", &DrmObject::idx)
41                         .def_property_readonly("card", &DrmObject::card)
42                         ;
44         py::class_<DrmPropObject, DrmObject, unique_ptr<DrmPropObject, py::nodelete>>(m, "DrmPropObject")
45                         .def("refresh_props", &DrmPropObject::refresh_props)
46                         .def_property_readonly("prop_map", &DrmPropObject::get_prop_map)
47                         .def("get_prop_value", (uint64_t (DrmPropObject::*)(const string&) const)&DrmPropObject::get_prop_value)
48                         .def("set_prop_value",(int (DrmPropObject::*)(const string&, uint64_t)) &DrmPropObject::set_prop_value)
49                         .def("get_prop_value_as_blob", &DrmPropObject::get_prop_value_as_blob)
50                         .def("get_prop", &DrmPropObject::get_prop)
51                         ;
53         py::class_<Connector, DrmPropObject, unique_ptr<Connector, py::nodelete>>(m, "Connector")
54                         .def_property_readonly("fullname", &Connector::fullname)
55                         .def("get_default_mode", &Connector::get_default_mode)
56                         .def("get_current_crtc", &Connector::get_current_crtc)
57                         .def("get_possible_crtcs", [](Connector* self) {
58                                 vector<unique_ptr<Crtc, py::nodelete>> v;
59                                 for (Crtc* p : self->get_possible_crtcs())
60                                         v.push_back(unique_ptr<Crtc, py::nodelete>(p));
61                                 return v;
62                         })
63                         .def("get_modes", &Connector::get_modes)
64                         .def("get_mode", (Videomode (Connector::*)(const string& mode) const)&Connector::get_mode)
65                         .def("get_mode", (Videomode (Connector::*)(unsigned xres, unsigned yres, float refresh, bool ilace) const)&Connector::get_mode)
66                         .def("connected", &Connector::connected)
67                         .def("__repr__", [](const Connector& o) { return "<pykms.Connector " + to_string(o.id()) + ">"; })
68                         .def("refresh", &Connector::refresh)
69                         ;
71         py::class_<Crtc, DrmPropObject, unique_ptr<Crtc, py::nodelete>>(m, "Crtc")
72                         .def("set_mode", (int (Crtc::*)(Connector*, const Videomode&))&Crtc::set_mode)
73                         .def("set_mode", (int (Crtc::*)(Connector*, Framebuffer&, const Videomode&))&Crtc::set_mode)
74                         .def("disable_mode", &Crtc::disable_mode)
75                         .def("page_flip",
76                              [](Crtc* self, Framebuffer& fb, uint32_t data)
77                                 {
78                                         self->page_flip(fb, (void*)(intptr_t)data);
79                                 }, py::arg("fb"), py::arg("data") = 0)
80                         .def("set_plane", &Crtc::set_plane)
81                         .def_property_readonly("possible_planes", &Crtc::get_possible_planes)
82                         .def_property_readonly("primary_plane", &Crtc::get_primary_plane)
83                         .def_property_readonly("mode", &Crtc::mode)
84                         .def_property_readonly("mode_valid", &Crtc::mode_valid)
85                         .def("__repr__", [](const Crtc& o) { return "<pykms.Crtc " + to_string(o.id()) + ">"; })
86                         .def("refresh", &Crtc::refresh)
87                         ;
89         py::class_<Encoder, DrmPropObject, unique_ptr<Encoder, py::nodelete>>(m, "Encoder")
90                         .def("refresh", &Encoder::refresh)
91                         ;
93         py::class_<Plane, DrmPropObject, unique_ptr<Plane, py::nodelete>>(m, "Plane")
94                         .def("supports_crtc", &Plane::supports_crtc)
95                         .def_property_readonly("formats", &Plane::get_formats)
96                         .def_property_readonly("plane_type", &Plane::plane_type)
97                         .def("__repr__", [](const Plane& o) { return "<pykms.Plane " + to_string(o.id()) + ">"; })
98                         ;
100         py::enum_<PlaneType>(m, "PlaneType")
101                         .value("Overlay", PlaneType::Overlay)
102                         .value("Primary", PlaneType::Primary)
103                         .value("Cursor", PlaneType::Cursor)
104                         ;
106         py::class_<Property, DrmObject, unique_ptr<Property, py::nodelete>>(m, "Property")
107                         .def_property_readonly("name", &Property::name)
108                         .def_property_readonly("enums", &Property::get_enums)
109                         ;
111         py::class_<Blob>(m, "Blob")
112                         .def("__init__", [](Blob& instance, Card& card, py::buffer buf) {
113                                         py::buffer_info info = buf.request();
114                                         if (info.ndim != 1)
115                                                 throw std::runtime_error("Incompatible buffer dimension!");
117                                         new (&instance) Blob(card, info.ptr, info.size * info.itemsize);
118                                 },
119                                 py::keep_alive<1, 3>()) // Keep Card alive until this is destructed
121                         .def_property_readonly("data", &Blob::data)
123                         // XXX pybind11 doesn't support a base object (DrmObject) with custom holder-type,
124                         // and a subclass with standard holder-type.
125                         // So we just copy the DrmObject members here.
126                         // Note that this means that python thinks we don't derive from DrmObject
127                         .def_property_readonly("id", &DrmObject::id)
128                         .def_property_readonly("idx", &DrmObject::idx)
129                         .def_property_readonly("card", &DrmObject::card)
130                         ;
132         py::class_<Framebuffer>(m, "Framebuffer")
133                         .def_property_readonly("width", &Framebuffer::width)
134                         .def_property_readonly("height", &Framebuffer::height)
136                         // XXX pybind11 doesn't support a base object (DrmObject) with custom holder-type,
137                         // and a subclass with standard holder-type.
138                         // So we just copy the DrmObject members here.
139                         // Note that this means that python thinks we don't derive from DrmObject
140                         .def_property_readonly("id", &DrmObject::id)
141                         .def_property_readonly("idx", &DrmObject::idx)
142                         .def_property_readonly("card", &DrmObject::card)
143                         ;
145         py::class_<DumbFramebuffer, Framebuffer>(m, "DumbFramebuffer")
146                         .def(py::init<Card&, uint32_t, uint32_t, const string&>(),
147                              py::keep_alive<1, 2>())    // Keep Card alive until this is destructed
148                         .def(py::init<Card&, uint32_t, uint32_t, PixelFormat>(),
149                              py::keep_alive<1, 2>())    // Keep Card alive until this is destructed
150                         .def_property_readonly("format", &DumbFramebuffer::format)
151                         .def_property_readonly("num_planes", &DumbFramebuffer::num_planes)
152                         .def("fd", &DumbFramebuffer::prime_fd)
153                         .def("stride", &DumbFramebuffer::stride)
154                         .def("offset", &DumbFramebuffer::offset)
155                         ;
157         py::class_<ExtFramebuffer, Framebuffer>(m, "ExtFramebuffer")
158                         .def(py::init<Card&, uint32_t, uint32_t, PixelFormat, vector<int>, vector<uint32_t>, vector<uint32_t>>(),
159                              py::keep_alive<1, 2>())    // Keep Card alive until this is destructed
160                         ;
162         py::enum_<PixelFormat>(m, "PixelFormat")
163                         .value("Undefined", PixelFormat::Undefined)
165                         .value("NV12", PixelFormat::NV12)
166                         .value("NV21", PixelFormat::NV21)
168                         .value("UYVY", PixelFormat::UYVY)
169                         .value("YUYV", PixelFormat::YUYV)
170                         .value("YVYU", PixelFormat::YVYU)
171                         .value("VYUY", PixelFormat::VYUY)
173                         .value("XRGB8888", PixelFormat::XRGB8888)
174                         .value("XBGR8888", PixelFormat::XBGR8888)
175                         .value("ARGB8888", PixelFormat::ARGB8888)
176                         .value("ABGR8888", PixelFormat::ABGR8888)
178                         .value("RGB888", PixelFormat::RGB888)
179                         .value("BGR888", PixelFormat::BGR888)
181                         .value("RGB565", PixelFormat::RGB565)
182                         .value("BGR565", PixelFormat::BGR565)
183                         ;
185         py::enum_<SyncPolarity>(m, "SyncPolarity")
186                         .value("Undefined", SyncPolarity::Undefined)
187                         .value("Positive", SyncPolarity::Positive)
188                         .value("Negative", SyncPolarity::Negative)
189                         ;
191         py::class_<Videomode>(m, "Videomode")
192                         .def(py::init<>())
194                         .def_readwrite("name", &Videomode::name)
196                         .def_readwrite("clock", &Videomode::clock)
198                         .def_readwrite("hdisplay", &Videomode::hdisplay)
199                         .def_readwrite("hsync_start", &Videomode::hsync_start)
200                         .def_readwrite("hsync_end", &Videomode::hsync_end)
201                         .def_readwrite("htotal", &Videomode::htotal)
203                         .def_readwrite("vdisplay", &Videomode::vdisplay)
204                         .def_readwrite("vsync_start", &Videomode::vsync_start)
205                         .def_readwrite("vsync_end", &Videomode::vsync_end)
206                         .def_readwrite("vtotal", &Videomode::vtotal)
208                         .def_readwrite("vrefresh", &Videomode::vrefresh)
210                         .def_readwrite("flags", &Videomode::flags)
211                         .def_readwrite("type", &Videomode::type)
213                         .def("__repr__", [](const Videomode& vm) { return "<pykms.Videomode " + to_string(vm.hdisplay) + "x" + to_string(vm.vdisplay) + ">"; })
215                         .def("to_blob", &Videomode::to_blob)
217                         .def_property("hsync", &Videomode::hsync, &Videomode::set_hsync)
218                         .def_property("vsync", &Videomode::vsync, &Videomode::set_vsync)
219                         ;
222         m.def("videomode_from_timings", &videomode_from_timings);
224         py::class_<AtomicReq>(m, "AtomicReq")
225                         .def(py::init<Card&>(),
226                              py::keep_alive<1, 2>())    // Keep Card alive until this is destructed
227                         .def("add", (void (AtomicReq::*)(DrmPropObject*, const string&, uint64_t)) &AtomicReq::add)
228                         .def("add", (void (AtomicReq::*)(DrmPropObject*, const map<string, uint64_t>&)) &AtomicReq::add)
229                         .def("test", &AtomicReq::test, py::arg("allow_modeset") = false)
230                         .def("commit",
231                              [](AtomicReq* self, uint32_t data, bool allow)
232                                 {
233                                         return self->commit((void*)(intptr_t)data, allow);
234                                 }, py::arg("data") = 0, py::arg("allow_modeset") = false)
235                         .def("commit_sync", &AtomicReq::commit_sync, py::arg("allow_modeset") = false)
236                         ;