From b11baff09f78a4a383f817ec35208ae8966ab832 Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Mon, 2 Jan 2017 16:42:08 +0200 Subject: py: Reorganize source directory Separate the Python bindings sources from the test scripts. While at it, remove the unneeded run.sh script. Signed-off-by: Laurent Pinchart --- py/pykms/CMakeLists.txt | 26 ++++++++ py/pykms/pykms.cpp | 53 ++++++++++++++++ py/pykms/pykmsbase.cpp | 157 ++++++++++++++++++++++++++++++++++++++++++++++++ py/pykms/pykmsomap.cpp | 21 +++++++ py/pykms/pykmsutil.cpp | 49 +++++++++++++++ py/pykms/pyvid.cpp | 38 ++++++++++++ 6 files changed, 344 insertions(+) create mode 100644 py/pykms/CMakeLists.txt create mode 100644 py/pykms/pykms.cpp create mode 100644 py/pykms/pykmsbase.cpp create mode 100644 py/pykms/pykmsomap.cpp create mode 100644 py/pykms/pykmsutil.cpp create mode 100644 py/pykms/pyvid.cpp (limited to 'py/pykms') diff --git a/py/pykms/CMakeLists.txt b/py/pykms/CMakeLists.txt new file mode 100644 index 0000000..3e6e0e1 --- /dev/null +++ b/py/pykms/CMakeLists.txt @@ -0,0 +1,26 @@ +include_directories(${LIBDRM_INCLUDE_DIRS}) +link_directories(${LIBDRM_LIBRARY_DIRS}) + +pkg_check_modules(PYTHON python3 REQUIRED) +include_directories(${PYTHON_INCLUDE_DIRS}) + +if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden") +endif() + +include_directories(${PROJECT_SOURCE_DIR}/ext/pybind11/include) + +set(SRCS pykms.cpp pykmsbase.cpp pykmsutil.cpp pyvid.cpp) + +if(LIBDRM_OMAP_FOUND) + set(SRCS ${SRCS} pykmsomap.cpp) +endif() + +add_library(pykms SHARED ${SRCS}) +target_link_libraries(pykms kms++ kms++util ${LIBDRM_LIBRARIES}) + +# Don't add a 'lib' prefix to the shared library +set_target_properties(pykms PROPERTIES PREFIX "") + +# XXX Where should pykms.so be installed? +#install(TARGETS pykms DESTINATION lib) diff --git a/py/pykms/pykms.cpp b/py/pykms/pykms.cpp new file mode 100644 index 0000000..2199039 --- /dev/null +++ b/py/pykms/pykms.cpp @@ -0,0 +1,53 @@ +#include +#include +#include + +namespace py = pybind11; + +using namespace kms; +using namespace std; + +void init_pykmstest(py::module &m); +void init_pykmsbase(py::module &m); +void init_pyvid(py::module &m); + +#if HAS_LIBDRM_OMAP +void init_pykmsomap(py::module &m); +#endif + +class PyPageFlipHandlerBase : PageFlipHandlerBase +{ +public: + using PageFlipHandlerBase::PageFlipHandlerBase; + + virtual void handle_page_flip(uint32_t frame, double time) + { + PYBIND11_OVERLOAD_PURE( + void, /* Return type */ + PageFlipHandlerBase, /* Parent class */ + handle_page_flip, /* Name of function */ + frame, time + ); + } +}; + +PYBIND11_PLUGIN(pykms) { + py::module m("pykms", "kms bindings"); + + init_pykmsbase(m); + + py::class_(m, "PageFlipHandlerBase") + .alias() + .def(py::init<>()) + .def("handle_page_flip", &PageFlipHandlerBase::handle_page_flip) + ; + + init_pykmstest(m); + + init_pyvid(m); + +#if HAS_LIBDRM_OMAP + init_pykmsomap(m); +#endif + return m.ptr(); +} diff --git a/py/pykms/pykmsbase.cpp b/py/pykms/pykmsbase.cpp new file mode 100644 index 0000000..5247158 --- /dev/null +++ b/py/pykms/pykmsbase.cpp @@ -0,0 +1,157 @@ +#include +#include +#include + +namespace py = pybind11; + +using namespace kms; +using namespace std; + +void init_pykmsbase(py::module &m) +{ + py::class_(m, "Card") + .def(py::init<>()) + .def_property_readonly("fd", &Card::fd) + .def("get_first_connected_connector", &Card::get_first_connected_connector) + .def_property_readonly("connectors", &Card::get_connectors) + .def_property_readonly("crtcs", &Card::get_crtcs) + .def_property_readonly("encoders", &Card::get_encoders) + .def_property_readonly("planes", &Card::get_planes) + .def_property_readonly("has_atomic", &Card::has_atomic) + .def("call_page_flip_handlers", &Card::call_page_flip_handlers) + .def("get_prop", (Property* (Card::*)(uint32_t) const)&Card::get_prop) + ; + + py::class_(m, "DrmObject") + .def_property_readonly("id", &DrmObject::id) + .def_property_readonly("card", &DrmObject::card) + ; + + py::class_(m, "DrmPropObject", py::base()) + .def("refresh_props", &DrmPropObject::refresh_props) + .def_property_readonly("prop_map", &DrmPropObject::get_prop_map) + .def("get_prop_value", (uint64_t (DrmPropObject::*)(const string&) const)&DrmPropObject::get_prop_value) + .def("set_prop_value",(int (DrmPropObject::*)(const string&, uint64_t)) &DrmPropObject::set_prop_value) + .def("get_prop_value_as_blob", &DrmPropObject::get_prop_value_as_blob) + ; + + py::class_(m, "Connector", py::base()) + .def_property_readonly("fullname", &Connector::fullname) + .def("get_default_mode", &Connector::get_default_mode) + .def("get_current_crtc", &Connector::get_current_crtc) + .def("get_possible_crtcs", &Connector::get_possible_crtcs) + .def("get_modes", &Connector::get_modes) + .def("get_mode", (Videomode (Connector::*)(const string& mode) const)&Connector::get_mode) + .def("get_mode", (Videomode (Connector::*)(unsigned xres, unsigned yres, float refresh, bool ilace) const)&Connector::get_mode) + .def("__repr__", [](const Connector& o) { return ""; }) + ; + + py::class_(m, "Crtc", py::base()) + .def("set_mode", &Crtc::set_mode) + .def("page_flip", &Crtc::page_flip) + .def("set_plane", &Crtc::set_plane) + .def_property_readonly("possible_planes", &Crtc::get_possible_planes) + .def_property_readonly("primary_plane", &Crtc::get_primary_plane) + .def_property_readonly("mode", &Crtc::mode) + .def_property_readonly("mode_valid", &Crtc::mode_valid) + .def("__repr__", [](const Crtc& o) { return ""; }) + ; + + py::class_(m, "Encoder", py::base()) + ; + + py::class_(m, "Plane", py::base()) + .def("supports_crtc", &Plane::supports_crtc) + .def_property_readonly("plane_type", &Plane::plane_type) + .def("__repr__", [](const Plane& o) { return ""; }) + ; + + py::enum_(m, "PlaneType") + .value("Overlay", PlaneType::Overlay) + .value("Primary", PlaneType::Primary) + .value("Cursor", PlaneType::Cursor) + ; + + py::class_(m, "Property", py::base()) + .def_property_readonly("name", &Property::name) + ; + + py::class_(m, "Blob", py::base()) + .def("__init__", [](Blob& instance, Card& card, py::buffer buf) { + py::buffer_info info = buf.request(); + if (info.ndim != 1) + throw std::runtime_error("Incompatible buffer dimension!"); + + new (&instance) Blob(card, info.ptr, info.size * info.itemsize); + }) + .def_property_readonly("data", &Blob::data) + ; + + py::class_(m, "Framebuffer", py::base()) + ; + + py::class_(m, "MappedFramebuffer", py::base()) + .def_property_readonly("width", &MappedFramebuffer::width) + .def_property_readonly("height", &MappedFramebuffer::height) + ; + + py::class_(m, "DumbFramebuffer", py::base()) + .def(py::init(), + py::keep_alive<1, 2>()) // Keep Card alive until this is destructed + .def(py::init(), + py::keep_alive<1, 2>()) // Keep Card alive until this is destructed + ; + + py::enum_(m, "PixelFormat") + .value("Undefined", PixelFormat::Undefined) + + .value("NV12", PixelFormat::NV12) + .value("NV21", PixelFormat::NV21) + + .value("UYVY", PixelFormat::UYVY) + .value("YUYV", PixelFormat::YUYV) + .value("YVYU", PixelFormat::YVYU) + .value("VYUY", PixelFormat::VYUY) + + .value("XRGB8888", PixelFormat::XRGB8888) + .value("XBGR8888", PixelFormat::XBGR8888) + .value("ARGB8888", PixelFormat::ARGB8888) + .value("ABGR8888", PixelFormat::ABGR8888) + + .value("RGB888", PixelFormat::RGB888) + + .value("RGB565", PixelFormat::RGB565) + ; + + py::class_(m, "Videomode") + .def(py::init<>()) + + .def_readwrite("name", &Videomode::name) + + .def_readwrite("clock", &Videomode::clock) + + .def_readwrite("hdisplay", &Videomode::hdisplay) + .def_readwrite("hsync_start", &Videomode::hsync_start) + .def_readwrite("hsync_end", &Videomode::hsync_end) + .def_readwrite("htotal", &Videomode::htotal) + + .def_readwrite("vdisplay", &Videomode::vdisplay) + .def_readwrite("vsync_start", &Videomode::vsync_start) + .def_readwrite("vsync_end", &Videomode::vsync_end) + .def_readwrite("vtotal", &Videomode::vtotal) + + .def_readwrite("vrefresh", &Videomode::vrefresh) + + .def_readwrite("flags", &Videomode::flags) + .def_readwrite("type", &Videomode::type) + ; + + py::class_(m, "AtomicReq") + .def(py::init(), + py::keep_alive<1, 2>()) // Keep Card alive until this is destructed + .def("add", (void (AtomicReq::*)(DrmPropObject*, const string&, uint64_t)) &AtomicReq::add) + .def("test", &AtomicReq::test, py::arg("allow_modeset") = false) + .def("commit", &AtomicReq::commit, py::arg("data"), py::arg("allow_modeset") = false) + .def("commit_sync", &AtomicReq::commit_sync, py::arg("allow_modeset") = false) + ; +} diff --git a/py/pykms/pykmsomap.cpp b/py/pykms/pykmsomap.cpp new file mode 100644 index 0000000..525834b --- /dev/null +++ b/py/pykms/pykmsomap.cpp @@ -0,0 +1,21 @@ +#include +#include +#include +#include + +namespace py = pybind11; + +using namespace kms; +using namespace std; + +void init_pykmsomap(py::module &m) +{ + py::class_(m, "OmapCard", py::base()) + .def(py::init<>()) + ; + + py::class_(m, "OmapFramebuffer", py::base()) + .def(py::init(), + py::keep_alive<1, 2>()) // Keep OmapCard alive until this is destructed + ; +} diff --git a/py/pykms/pykmsutil.cpp b/py/pykms/pykmsutil.cpp new file mode 100644 index 0000000..b3b7594 --- /dev/null +++ b/py/pykms/pykmsutil.cpp @@ -0,0 +1,49 @@ +#include +#include +#include +#include + +namespace py = pybind11; + +using namespace kms; +using namespace std; + +void init_pykmstest(py::module &m) +{ + py::class_(m, "RGB") + .def(py::init<>()) + .def(py::init()) + .def(py::init()) + .def_property_readonly("rgb888", &RGB::rgb888) + .def_property_readonly("argb8888", &RGB::argb8888) + .def_property_readonly("abgr8888", &RGB::abgr8888) + .def_property_readonly("rgb565", &RGB::rgb565) + ; + + py::class_(m, "ResourceManager") + .def(py::init()) + .def("reset", &ResourceManager::reset) + .def("reserve_connector", &ResourceManager::reserve_connector, + py::arg("name") = string()) + .def("reserve_crtc", &ResourceManager::reserve_crtc) + .def("reserve_plane", &ResourceManager::reserve_plane, + py::arg("crtc"), + py::arg("type"), + py::arg("format") = PixelFormat::Undefined) + .def("reserve_primary_plane", &ResourceManager::reserve_primary_plane, + py::arg("crtc"), + py::arg("format") = PixelFormat::Undefined) + .def("reserve_overlay_plane", &ResourceManager::reserve_overlay_plane, + py::arg("crtc"), + py::arg("format") = PixelFormat::Undefined) + ; + + // Use lambdas to handle IMappedFramebuffer + m.def("draw_test_pattern", [](MappedFramebuffer& fb) { draw_test_pattern(fb); } ); + m.def("draw_color_bar", [](MappedFramebuffer& fb, int old_xpos, int xpos, int width) { + draw_color_bar(fb, old_xpos, xpos, width); + } ); + m.def("draw_rect", [](MappedFramebuffer& fb, uint32_t x, uint32_t y, uint32_t w, uint32_t h, RGB color) { + draw_rect(fb, x, y, w, h, color); + } ); +} diff --git a/py/pykms/pyvid.cpp b/py/pykms/pyvid.cpp new file mode 100644 index 0000000..01177d5 --- /dev/null +++ b/py/pykms/pyvid.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +#include +#include + +namespace py = pybind11; + +using namespace kms; +using namespace std; + +void init_pyvid(py::module &m) +{ + py::class_(m, "VideoDevice") + .def(py::init()) + .def_property_readonly("fd", &VideoDevice::fd) + .def_property_readonly("has_capture", &VideoDevice::has_capture) + .def_property_readonly("has_output", &VideoDevice::has_output) + .def_property_readonly("has_m2m", &VideoDevice::has_m2m) + .def_property_readonly("capture_streamer", &VideoDevice::get_capture_streamer) + .def_property_readonly("output_streamer", &VideoDevice::get_output_streamer) + .def_property_readonly("discrete_frame_sizes", &VideoDevice::get_discrete_frame_sizes) + .def_property_readonly("frame_sizes", &VideoDevice::get_frame_sizes) + .def("get_capture_devices", &VideoDevice::get_capture_devices) + ; + + py::class_(m, "VideoStreamer") + .def_property_readonly("fd", &VideoStreamer::fd) + .def_property_readonly("ports", &VideoStreamer::get_ports) + .def("set_port", &VideoStreamer::set_port) + .def_property_readonly("formats", &VideoStreamer::get_formats) + .def("set_format", &VideoStreamer::set_format) + .def("set_queue_size", &VideoStreamer::set_queue_size) + .def("queue", &VideoStreamer::queue) + .def("dequeue", &VideoStreamer::dequeue) + .def("stream_on", &VideoStreamer::stream_on) + ; +} -- cgit v1.2.3