From 412935a47b762c33e54a464243f2d789b065bbb6 Mon Sep 17 00:00:00 2001 From: LI Qingwu Date: Tue, 7 Nov 2023 11:21:53 +0800 Subject: [PATCH] py: Framebuffer: Add map wrapping Enables direct Python access to the framebuffer's buffer, facilitating rapid image drawing capabilities. Signed-off-by: LI Qingwu Signed-off-by: Tomi Valkeinen --- py/pykms/pykmsbase.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/py/pykms/pykmsbase.cpp b/py/pykms/pykmsbase.cpp index 74847703..254dce6e 100644 --- a/py/pykms/pykmsbase.cpp +++ b/py/pykms/pykmsbase.cpp @@ -172,7 +172,18 @@ void init_pykmsbase(py::module& m) // Note that this means that python thinks we don't derive from DrmObject .def_property_readonly("id", &DrmObject::id) .def_property_readonly("idx", &DrmObject::idx) - .def_property_readonly("card", &DrmObject::card); + .def_property_readonly("card", &DrmObject::card) + .def("map", [](Framebuffer& self, uint32_t plane) { + const auto& format_info = get_pixel_format_info(self.format()); + + if (plane >= format_info.num_planes) + throw runtime_error("map: bad plane number"); + + array shape{ self.height(), self.width() * format_info.planes[plane].bitspp / 8 }; + array strides{ self.stride(plane), sizeof(uint8_t) }; + + return py::memoryview::from_buffer(self.map(plane), shape, strides); + }); py::class_(m, "DumbFramebuffer") .def(py::init(),