From 40d96062a37ca924a66a926a234b35644928d4a7 Mon Sep 17 00:00:00 2001 From: Tomi Valkeinen Date: Thu, 23 Aug 2018 13:11:41 +0300 Subject: Revert "testpat: remove threaded drawing" This reverts commit 33246d9b5fb0347aabd62caac1da03440f9e1634. Add threaded drawing back, but have it behind a CMAKE variable so that it can easily be turned off. Signed-off-by: Tomi Valkeinen --- CMakeLists.txt | 2 ++ kms++util/CMakeLists.txt | 5 +++++ kms++util/src/testpat.cpp | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 57088bc..f2f24fe 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,6 +25,8 @@ set(KMSXX_PYTHON_VERSION "python3;python2" CACHE STRING "Python pkgconfig packag set(KMSXX_ENABLE_KMSCUBE OFF CACHE BOOL "Enable kmscube") +set(KMSXX_ENABLE_THREADING ON CACHE BOOL "Enable threading for parallelized drawing") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11 -Wall") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wextra -Wno-unused-parameter") diff --git a/kms++util/CMakeLists.txt b/kms++util/CMakeLists.txt index abee194..70f3b17 100644 --- a/kms++util/CMakeLists.txt +++ b/kms++util/CMakeLists.txt @@ -9,6 +9,11 @@ target_include_directories(kms++util PUBLIC target_link_libraries(kms++util kms++) +if (KMSXX_ENABLE_THREADING) + target_link_libraries(kms++util pthread) + add_definitions(-DHAS_PTHREAD) +endif() + set_target_properties(kms++util PROPERTIES PUBLIC_HEADER "${PUB_HDRS}") diff --git a/kms++util/src/testpat.cpp b/kms++util/src/testpat.cpp index fee8d8c..8795cc6 100644 --- a/kms++util/src/testpat.cpp +++ b/kms++util/src/testpat.cpp @@ -4,6 +4,10 @@ #include #include +#ifdef HAS_PTHREAD +#include +#endif + #include #include @@ -152,7 +156,36 @@ static void draw_test_pattern_part(IFramebuffer& fb, unsigned start_y, unsigned static void draw_test_pattern_impl(IFramebuffer& fb, YUVType yuvt) { +#ifdef HAS_PTHREAD + if (fb.height() < 20) { + draw_test_pattern_part(fb, 0, fb.height(), yuvt); + return; + } + + // Create the mmaps before starting the threads + for (unsigned i = 0; i < fb.num_planes(); ++i) + fb.map(0); + + unsigned num_threads = thread::hardware_concurrency(); + vector workers; + + unsigned part = (fb.height() / num_threads) & ~1; + + for (unsigned n = 0; n < num_threads; ++n) { + unsigned start = n * part; + unsigned end = start + part; + + if (n == num_threads - 1) + end = fb.height(); + + workers.push_back(thread([&fb, start, end, yuvt]() { draw_test_pattern_part(fb, start, end, yuvt); })); + } + + for (thread& t : workers) + t.join(); +#else draw_test_pattern_part(fb, 0, fb.height(), yuvt); +#endif } void draw_test_pattern(IFramebuffer &fb, YUVType yuvt) -- cgit v1.2.3