Use pybind11 instead of SWIG
authorTomi Valkeinen <tomi.valkeinen@ti.com>
Fri, 20 May 2016 19:09:35 +0000 (22:09 +0300)
committerTomi Valkeinen <tomi.valkeinen@ti.com>
Mon, 23 May 2016 06:43:27 +0000 (09:43 +0300)
.gitmodules [new file with mode: 0644]
ext/pybind11 [new submodule]
py/CMakeLists.txt
py/alpha-test.py
py/db.py
py/helpers.py
py/iact.py
py/pykms.cpp [new file with mode: 0644]
py/pykms.i [deleted file]
py/run.sh [new file with mode: 0755]
py/trans-test.py

diff --git a/.gitmodules b/.gitmodules
new file mode 100644 (file)
index 0000000..c6d1083
--- /dev/null
@@ -0,0 +1,3 @@
+[submodule "ext/pybind11"]
+       path = ext/pybind11
+       url = https://github.com/pybind/pybind11.git
diff --git a/ext/pybind11 b/ext/pybind11
new file mode 160000 (submodule)
index 0000000..e70b2ab
--- /dev/null
@@ -0,0 +1 @@
+Subproject commit e70b2abb6dee27a2889b01f245a2a28e6fcd4b01
index c6341d81444a8f7be11205a24c9377f59becbddf..ff689393a476e054a866eac3592615415b8f20b5 100644 (file)
@@ -1,27 +1,20 @@
-set(SWIG_EXECUTABLE "swig3.0")
-find_package(SWIG 3.0 REQUIRED)
-include(${SWIG_USE_FILE})
-
 include_directories(${LIBDRM_INCLUDE_DIRS})
 link_directories(${LIBDRM_LIBRARY_DIRS})
 
 pkg_check_modules(PYTHON python3 REQUIRED)
 include_directories(${PYTHON_INCLUDE_DIRS})
 
-# XXX how to add these inc dirs in a proper way?
-include_directories(../libkms++ ../libkmstest)
-
-#set(CMAKE_SWIG_FLAGS "-I../../libkms")
-set(CMAKE_SWIG_FLAGS "-builtin")
+if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
+    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden")
+endif()
 
-set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-missing-field-initializers")
+include_directories(${PROJECT_SOURCE_DIR}/ext/pybind11/include)
 
-set_source_files_properties(pykms.i PROPERTIES CPLUSPLUS ON)
-swig_add_module(pykms python pykms.i)
-swig_link_libraries(pykms kms++ kmstest ${LIBDRM_LIBRARIES} ${PYTHON_LIBRARIES})
+add_library(pykms SHARED pykms.cpp)
+target_link_libraries(pykms kms++ kmstest ${LIBDRM_LIBRARIES})
 
-# We get some "maybe-uninitialized" warnings from the generated code. I hope they are harmless.
-set_source_files_properties(${swig_generated_file_fullname} PROPERTIES COMPILE_FLAGS "-Wno-maybe-uninitialized")
+# Don't add a 'lib' prefix to the shared library
+set_target_properties(pykms PROPERTIES PREFIX "")
 
 file(GLOB PY_SRCS "*.py")
 add_custom_target(pyextras SOURCES ${PY_SRCS})
index 695cd1baef5d1add4b3eb9740ef2a46e5e3569cd..99c84fbfd5d09f34acec760faf52c3e0c9e5ed08 100755 (executable)
@@ -15,7 +15,7 @@ mode = conn.get_default_mode()
 crtc = conn.get_current_crtc()
 
 planes = []
-for p in card.get_planes():
+for p in card.planes:
     if p.supports_crtc(crtc) == False:
         continue
     planes.append(p)
index ce1a54e1529b415417e43fc320a70c4e1067be8c..c51ca700f9c02a063532e886b2e7c81c9e45d772 100755 (executable)
--- a/py/db.py
+++ b/py/db.py
@@ -24,16 +24,16 @@ class FlipHandler(pykms.PageFlipHandlerBase):
         self.front_buf = self.front_buf ^ 1
 
         current_xpos = self.bar_xpos;
-        old_xpos = (current_xpos + (fb.width() - bar_width - bar_speed)) % (fb.width() - bar_width);
-        new_xpos = (current_xpos + bar_speed) % (fb.width() - bar_width);
+        old_xpos = (current_xpos + (fb.width - bar_width - bar_speed)) % (fb.width - bar_width);
+        new_xpos = (current_xpos + bar_speed) % (fb.width - bar_width);
 
         self.bar_xpos = new_xpos
 
         pykms.draw_color_bar(fb, old_xpos, new_xpos, bar_width)
 
-        if card.has_atomic():
+        if card.has_atomic:
             ctx = pykms.AtomicReq(card)
-            ctx.add(crtc, "FB_ID", fb.id())
+            ctx.add(crtc, "FB_ID", fb.id)
             ctx.commit(self)
         else:
             crtc.page_flip(fb, self)
@@ -61,7 +61,7 @@ def readkey(conn, mask):
     exit(0)
 
 sel = selectors.DefaultSelector()
-sel.register(card.fd(), selectors.EVENT_READ, readdrm)
+sel.register(card.fd, selectors.EVENT_READ, readdrm)
 sel.register(sys.stdin, selectors.EVENT_READ, readkey)
 
 while True:
index 15f6cfa7868db45c23e2cf5b91abfbc83e787ce7..acb90989f6f488309ba1e1bf60c747d537d25f35 100644 (file)
@@ -4,13 +4,12 @@ def add_props(areq, ob, map):
     for key, value in map.items():
         areq.add(ob, key, value)
 
-
 def props(o):
     o.refresh_props()
-    map = o.get_prop_map()
-    for propid in map:
-        prop = o.card().get_prop(propid)
-        print("%-15s %d (%#x)" % (prop.name(), map[propid], map[propid]))
+    map = o.prop_map
+    for propid,propval in map.items():
+        prop = o.card.get_prop(propid)
+        print("%-15s %d (%#x)" % (prop.name, propval, propval))
 
 def set_props(ob, map):
     areq = pykms.AtomicReq(ob.card())
@@ -32,7 +31,7 @@ cyan = pykms.RGB(0, 255, 255)
 def disable_planes(card):
     areq = pykms.AtomicReq(card)
 
-    for p in card.get_planes():
+    for p in card.planes:
         areq.add(p, "FB_ID", 0)
         areq.add(p, "CRTC_ID", 0)
 
index 073e4ca1b277876b99e68c040c5055c64f5b15a3..82511c3f0971392474510e64afc4bfd48df1db2a 100755 (executable)
@@ -6,6 +6,7 @@ import pykms
 from time import sleep
 from math import sin
 from math import cos
+from helpers import *
 
 card = pykms.Card()
 
@@ -21,29 +22,22 @@ crtc = conn.get_current_crtc()
 #crtc.set_mode(conn, fb, mode)
 
 i = 0
-for p in card.get_planes():
+for p in card.planes:
     globals()["plane"+str(i)] = p
     i=i+1
 
 i = 0
-for c in card.get_crtcs():
+for c in card.crtcs:
     globals()["crtc"+str(i)] = c
     i=i+1
 
-for p in crtc.get_possible_planes():
-    if p.plane_type() == 0:
+for p in crtc.possible_planes:
+    if p.plane_type == pykms.PlaneType.Overlay:
         plane = p
         break
 
 def set_plane(x, y):
-    crtc.set_plane(plane, fb, x, y, fb.width(), fb.height(), 0, 0, fb.width(), fb.height())
-
-def props(o):
-    o.refresh_props()
-    map = o.get_prop_map()
-    for propid in map:
-        prop = card.get_prop(propid)
-        print("%-15s %d (%#x)" % (prop.name(), map[propid], map[propid]))
+    crtc.set_plane(plane, fb, x, y, fb.width, fb.height, 0, 0, fb.width, fb.height)
 
 set_plane(0, 0)
 
diff --git a/py/pykms.cpp b/py/pykms.cpp
new file mode 100644 (file)
index 0000000..7e42e4f
--- /dev/null
@@ -0,0 +1,155 @@
+#include <pybind11/pybind11.h>
+#include <pybind11/stl.h>
+#include <kms++.h>
+#include "kmstest.h"
+
+namespace py = pybind11;
+
+using namespace kms;
+using namespace std;
+
+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");
+
+       py::class_<Card>(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)
+                       .def("get_prop", (Property* (Card::*)(const string&) const)&Card::get_prop)
+                       ;
+
+       py::class_<DrmObject, DrmObject*>(m, "DrmObject")
+                       .def_property_readonly("id", &DrmObject::id)
+                       .def("refresh_props", &DrmObject::refresh_props)
+                       .def_property_readonly("prop_map", &DrmObject::get_prop_map)
+                       .def_property_readonly("card", &DrmObject::card)
+                       ;
+
+       py::class_<Connector, Connector*>(m, "Connector",  py::base<DrmObject>())
+                       .def_property_readonly("fullname", &Connector::fullname)
+                       .def("get_default_mode", &Connector::get_default_mode)
+                       .def("get_current_crtc", &Connector::get_current_crtc)
+                       .def("get_modes", &Connector::get_modes)
+                       .def("__repr__", [](const Connector& o) { return "<pykms.Connector " + to_string(o.id()) + ">"; })
+                       ;
+
+       py::class_<Crtc, Crtc*>(m, "Crtc",  py::base<DrmObject>())
+                       .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("__repr__", [](const Crtc& o) { return "<pykms.Crtc " + to_string(o.id()) + ">"; })
+                       ;
+
+       py::class_<Encoder, Encoder*>(m, "Encoder",  py::base<DrmObject>())
+                       ;
+
+       py::class_<Plane, Plane*>(m, "Plane",  py::base<DrmObject>())
+                       .def("supports_crtc", &Plane::supports_crtc)
+                       .def_property_readonly("plane_type", &Plane::plane_type)
+                       .def("__repr__", [](const Plane& o) { return "<pykms.Plane " + to_string(o.id()) + ">"; })
+                       ;
+
+       py::enum_<PlaneType>(m, "PlaneType")
+                       .value("Overlay", PlaneType::Overlay)
+                       .value("Primary", PlaneType::Primary)
+                       .value("Cursor", PlaneType::Cursor)
+                       ;
+
+       py::class_<Property, Property*>(m, "Property",  py::base<DrmObject>())
+                       .def_property_readonly("name", &Property::name)
+                       ;
+
+       py::class_<Framebuffer>(m, "Framebuffer",  py::base<DrmObject>())
+                       ;
+
+       py::class_<DumbFramebuffer>(m, "DumbFramebuffer",  py::base<Framebuffer>())
+                       .def(py::init<Card&, uint32_t, uint32_t, const string&>(),
+                            py::keep_alive<1, 2>())    // Keep Card alive until this is destructed
+                       .def_property_readonly("width", &DumbFramebuffer::width)
+                       .def_property_readonly("height", &DumbFramebuffer::height)
+                       ;
+
+       py::class_<Videomode>(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_<PyPageFlipHandlerBase>(m, "PageFlipHandlerBase")
+                       .alias<PageFlipHandlerBase>()
+                       .def(py::init<>())
+                       .def("handle_page_flip", &PageFlipHandlerBase::handle_page_flip)
+                       ;
+
+       py::class_<AtomicReq>(m, "AtomicReq")
+                       .def(py::init<Card&>(),
+                            py::keep_alive<1, 2>())    // Keep Card alive until this is destructed
+                       .def("add", (void (AtomicReq::*)(DrmObject*, const string&, uint64_t)) &AtomicReq::add)
+                       .def("test", &AtomicReq::test)
+                       .def("commit", (int (AtomicReq::*)()) &AtomicReq::commit)
+                       .def("commit", (int (AtomicReq::*)(void*)) &AtomicReq::commit)
+                       .def("commit_sync", &AtomicReq::commit_sync)
+                       ;
+
+
+
+       /* libkmstest */
+
+       py::class_<RGB>(m, "RGB")
+                       .def(py::init<>())
+                       .def(py::init<uint8_t, uint8_t, uint8_t&>())
+                       .def(py::init<uint8_t, uint8_t, uint8_t, uint8_t&>())
+                       ;
+
+       // Use lambdas to handle IMappedFramebuffer
+       m.def("draw_test_pattern", [](DumbFramebuffer& fb) { draw_test_pattern(fb); } );
+       m.def("draw_color_bar", [](DumbFramebuffer& fb, int old_xpos, int xpos, int width) {
+               draw_color_bar(fb, old_xpos, xpos, width);
+       } );
+       m.def("draw_rect", [](DumbFramebuffer& fb, uint32_t x, uint32_t y, uint32_t w, uint32_t h, RGB color) {
+               draw_rect(fb, x, y, w, h, color);
+       } );
+
+       return m.ptr();
+}
diff --git a/py/pykms.i b/py/pykms.i
deleted file mode 100644 (file)
index cefdce7..0000000
+++ /dev/null
@@ -1,41 +0,0 @@
-%module(directors="1") pykms
-%{
-#include "kms++.h"
-
-#include "kmstest.h"
-
-using namespace kms;
-%}
-
-%include "std_string.i"
-%include "stdint.i"
-%include "std_vector.i"
-%include "std_map.i"
-
-%feature("director") PageFlipHandlerBase;
-
-%include "decls.h"
-%include "drmobject.h"
-%include "atomicreq.h"
-%include "crtc.h"
-%include "card.h"
-%include "property.h"
-%include "framebuffer.h"
-%include "dumbframebuffer.h"
-%include "plane.h"
-%include "connector.h"
-%include "encoder.h"
-%include "pagefliphandler.h"
-%include "videomode.h"
-
-%include "color.h"
-%include "kmstest.h"
-
-%template(ConnectorVector) std::vector<kms::Connector*>;
-%template(CrtcVector) std::vector<kms::Crtc*>;
-%template(EncoderVector) std::vector<kms::Encoder*>;
-%template(PlaneVector) std::vector<kms::Plane*>;
-%template(VideoModeVector) std::vector<kms::Videomode>;
-/* for some reason uint64_t doesn't compile on 64 bit pc */
-/* %template(map_u32_u64) std::map<uint32_t, uint64_t>; */
-%template(map_u32_u64) std::map<uint32_t, unsigned long long>;
diff --git a/py/run.sh b/py/run.sh
new file mode 100755 (executable)
index 0000000..f0ead78
--- /dev/null
+++ b/py/run.sh
@@ -0,0 +1,3 @@
+#!/bin/sh
+PYTHONPATH=build/py/ python3 $*
+
index d2e6050af7da0fe1a2ff1768f9ff9fe02e26b124..57855ff14df22a7a0a37bc270695aefe1f5dcec6 100755 (executable)
@@ -15,7 +15,7 @@ mode = conn.get_default_mode()
 crtc = conn.get_current_crtc()
 
 planes = []
-for p in card.get_planes():
+for p in card.planes:
     if p.supports_crtc(crtc) == False:
         continue
     planes.append(p)