pykms: keep Card alive until Blob is gone
[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::get_possible_crtcs)
58                         .def("get_modes", &Connector::get_modes)
59                         .def("get_mode", (Videomode (Connector::*)(const string& mode) const)&Connector::get_mode)
60                         .def("get_mode", (Videomode (Connector::*)(unsigned xres, unsigned yres, float refresh, bool ilace) const)&Connector::get_mode)
61                         .def("connected", &Connector::connected)
62                         .def("__repr__", [](const Connector& o) { return "<pykms.Connector " + to_string(o.id()) + ">"; })
63                         .def("refresh", &Connector::refresh)
64                         ;
66         py::class_<Crtc, DrmPropObject, unique_ptr<Crtc, py::nodelete>>(m, "Crtc")
67                         .def("set_mode", (int (Crtc::*)(Connector*, const Videomode&))&Crtc::set_mode)
68                         .def("set_mode", (int (Crtc::*)(Connector*, Framebuffer&, const Videomode&))&Crtc::set_mode)
69                         .def("disable_mode", &Crtc::disable_mode)
70                         .def("page_flip",
71                              [](Crtc* self, Framebuffer& fb, uint32_t data)
72                                 {
73                                         self->page_flip(fb, (void*)(intptr_t)data);
74                                 }, py::arg("fb"), py::arg("data") = 0)
75                         .def("set_plane", &Crtc::set_plane)
76                         .def_property_readonly("possible_planes", &Crtc::get_possible_planes)
77                         .def_property_readonly("primary_plane", &Crtc::get_primary_plane)
78                         .def_property_readonly("mode", &Crtc::mode)
79                         .def_property_readonly("mode_valid", &Crtc::mode_valid)
80                         .def("__repr__", [](const Crtc& o) { return "<pykms.Crtc " + to_string(o.id()) + ">"; })
81                         .def("refresh", &Crtc::refresh)
82                         ;
84         py::class_<Encoder, DrmPropObject, unique_ptr<Encoder, py::nodelete>>(m, "Encoder")
85                         .def("refresh", &Encoder::refresh)
86                         ;
88         py::class_<Plane, DrmPropObject, unique_ptr<Plane, py::nodelete>>(m, "Plane")
89                         .def("supports_crtc", &Plane::supports_crtc)
90                         .def_property_readonly("formats", &Plane::get_formats)
91                         .def_property_readonly("plane_type", &Plane::plane_type)
92                         .def("__repr__", [](const Plane& o) { return "<pykms.Plane " + to_string(o.id()) + ">"; })
93                         ;
95         py::enum_<PlaneType>(m, "PlaneType")
96                         .value("Overlay", PlaneType::Overlay)
97                         .value("Primary", PlaneType::Primary)
98                         .value("Cursor", PlaneType::Cursor)
99                         ;
101         py::class_<Property, DrmObject, unique_ptr<Property, py::nodelete>>(m, "Property")
102                         .def_property_readonly("name", &Property::name)
103                         .def_property_readonly("enums", &Property::get_enums)
104                         ;
106         py::class_<Blob>(m, "Blob")
107                         .def("__init__", [](Blob& instance, Card& card, py::buffer buf) {
108                                         py::buffer_info info = buf.request();
109                                         if (info.ndim != 1)
110                                                 throw std::runtime_error("Incompatible buffer dimension!");
112                                         new (&instance) Blob(card, info.ptr, info.size * info.itemsize);
113                                 },
114                                 py::keep_alive<1, 3>()) // Keep Card alive until this is destructed
116                         .def_property_readonly("data", &Blob::data)
118                         // XXX pybind11 doesn't support a base object (DrmObject) with custom holder-type,
119                         // and a subclass with standard holder-type.
120                         // So we just copy the DrmObject members here.
121                         // Note that this means that python thinks we don't derive from DrmObject
122                         .def_property_readonly("id", &DrmObject::id)
123                         .def_property_readonly("idx", &DrmObject::idx)
124                         .def_property_readonly("card", &DrmObject::card)
125                         ;
127         py::class_<Framebuffer>(m, "Framebuffer")
128                         .def_property_readonly("width", &Framebuffer::width)
129                         .def_property_readonly("height", &Framebuffer::height)
131                         // XXX pybind11 doesn't support a base object (DrmObject) with custom holder-type,
132                         // and a subclass with standard holder-type.
133                         // So we just copy the DrmObject members here.
134                         // Note that this means that python thinks we don't derive from DrmObject
135                         .def_property_readonly("id", &DrmObject::id)
136                         .def_property_readonly("idx", &DrmObject::idx)
137                         .def_property_readonly("card", &DrmObject::card)
138                         ;
140         py::class_<DumbFramebuffer, Framebuffer>(m, "DumbFramebuffer")
141                         .def(py::init<Card&, uint32_t, uint32_t, const string&>(),
142                              py::keep_alive<1, 2>())    // Keep Card alive until this is destructed
143                         .def(py::init<Card&, uint32_t, uint32_t, PixelFormat>(),
144                              py::keep_alive<1, 2>())    // Keep Card alive until this is destructed
145                         .def_property_readonly("format", &DumbFramebuffer::format)
146                         .def_property_readonly("num_planes", &DumbFramebuffer::num_planes)
147                         .def("fd", &DumbFramebuffer::prime_fd)
148                         .def("stride", &DumbFramebuffer::stride)
149                         .def("offset", &DumbFramebuffer::offset)
150                         ;
152         py::class_<ExtFramebuffer, Framebuffer>(m, "ExtFramebuffer")
153                         .def(py::init<Card&, uint32_t, uint32_t, PixelFormat, vector<int>, vector<uint32_t>, vector<uint32_t>>(),
154                              py::keep_alive<1, 2>())    // Keep Card alive until this is destructed
155                         ;
157         py::enum_<PixelFormat>(m, "PixelFormat")
158                         .value("Undefined", PixelFormat::Undefined)
160                         .value("NV12", PixelFormat::NV12)
161                         .value("NV21", PixelFormat::NV21)
163                         .value("UYVY", PixelFormat::UYVY)
164                         .value("YUYV", PixelFormat::YUYV)
165                         .value("YVYU", PixelFormat::YVYU)
166                         .value("VYUY", PixelFormat::VYUY)
168                         .value("XRGB8888", PixelFormat::XRGB8888)
169                         .value("XBGR8888", PixelFormat::XBGR8888)
170                         .value("ARGB8888", PixelFormat::ARGB8888)
171                         .value("ABGR8888", PixelFormat::ABGR8888)
173                         .value("RGB888", PixelFormat::RGB888)
174                         .value("BGR888", PixelFormat::BGR888)
176                         .value("RGB565", PixelFormat::RGB565)
177                         .value("BGR565", PixelFormat::BGR565)
178                         ;
180         py::enum_<SyncPolarity>(m, "SyncPolarity")
181                         .value("Undefined", SyncPolarity::Undefined)
182                         .value("Positive", SyncPolarity::Positive)
183                         .value("Negative", SyncPolarity::Negative)
184                         ;
186         py::class_<Videomode>(m, "Videomode")
187                         .def(py::init<>())
189                         .def_readwrite("name", &Videomode::name)
191                         .def_readwrite("clock", &Videomode::clock)
193                         .def_readwrite("hdisplay", &Videomode::hdisplay)
194                         .def_readwrite("hsync_start", &Videomode::hsync_start)
195                         .def_readwrite("hsync_end", &Videomode::hsync_end)
196                         .def_readwrite("htotal", &Videomode::htotal)
198                         .def_readwrite("vdisplay", &Videomode::vdisplay)
199                         .def_readwrite("vsync_start", &Videomode::vsync_start)
200                         .def_readwrite("vsync_end", &Videomode::vsync_end)
201                         .def_readwrite("vtotal", &Videomode::vtotal)
203                         .def_readwrite("vrefresh", &Videomode::vrefresh)
205                         .def_readwrite("flags", &Videomode::flags)
206                         .def_readwrite("type", &Videomode::type)
208                         .def("__repr__", [](const Videomode& vm) { return "<pykms.Videomode " + to_string(vm.hdisplay) + "x" + to_string(vm.vdisplay) + ">"; })
210                         .def("to_blob", &Videomode::to_blob)
212                         .def_property("hsync", &Videomode::hsync, &Videomode::set_hsync)
213                         .def_property("vsync", &Videomode::vsync, &Videomode::set_vsync)
214                         ;
217         m.def("videomode_from_timings", &videomode_from_timings);
219         py::class_<AtomicReq>(m, "AtomicReq")
220                         .def(py::init<Card&>(),
221                              py::keep_alive<1, 2>())    // Keep Card alive until this is destructed
222                         .def("add", (void (AtomicReq::*)(DrmPropObject*, const string&, uint64_t)) &AtomicReq::add)
223                         .def("add", (void (AtomicReq::*)(DrmPropObject*, const map<string, uint64_t>&)) &AtomicReq::add)
224                         .def("test", &AtomicReq::test, py::arg("allow_modeset") = false)
225                         .def("commit",
226                              [](AtomicReq* self, uint32_t data, bool allow)
227                                 {
228                                         return self->commit((void*)(intptr_t)data, allow);
229                                 }, py::arg("data") = 0, py::arg("allow_modeset") = false)
230                         .def("commit_sync", &AtomicReq::commit_sync, py::arg("allow_modeset") = false)
231                         ;