From 07ba3830a37466d86bcdbab8bce9c27c9ecd3506 Mon Sep 17 00:00:00 2001 From: kevin Date: Mon, 24 Jun 2019 09:18:22 -0400 Subject: [PATCH] Reduce CPU usage by more than half by using QImage to do ABGR -> RGB conversion. Now the main CPU hog is base64encoding the image data to send to Hyperion. Don't sent signal as exit code. --- CMakeLists.txt | 3 ++- README.md | 2 +- hgx11grab.cpp | 21 +++++---------------- hgx11grab.h | 1 + main.cpp | 4 ++-- 5 files changed, 11 insertions(+), 20 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c961aaf..4225df5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,9 +8,10 @@ set(CMAKE_BUILD_TYPE Desktop) find_package(Qt5Core REQUIRED) find_package(Qt5Network REQUIRED) +find_package(Qt5Widgets REQUIRED) find_package(X11 REQUIRED) set(CMAKE_CXX_STANDARD_LIBRARIES "${CMAKE_CXX_STANDARD_LIBRARIES} -lXrender -lXdamage") include_directories(${X11_INCLUDE_DIR}) add_executable(${PROJECT_NAME} "main.cpp" "hgx11.h" "hgx11.cpp" "hgx11net.h" "hgx11net.cpp" "hgx11damage.h" "hgx11damage.cpp" "hgx11grab.h" "hgx11grab.cpp") -target_link_libraries(${PROJECT_NAME} Qt5::Core Qt5::Network ${X11_LIBRARIES}) +target_link_libraries(${PROJECT_NAME} Qt5::Core Qt5::Network Qt5::Widgets ${X11_LIBRARIES}) diff --git a/README.md b/README.md index fcc568b..029e702 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ You should set this option so the image resolution sent to Hyperion is at least Ideally you want a perfect dividor, for example if your LED array is 70x39 and screen sis 3840x2160, then you get (3840/70) 54.86 for the width and (2160/39) 55.38 for the height, the nearest perfect dividor is 48 (3840/48 = 80 ; 2160/48 = 45), if you were to use 54, then there would be rounding error (3840/54 = 71.111, but is truncated to 71) and the image sent to hyperion will not be correctly scaled. -While watching a video with a 3840x2160 60hz display, frameskip at 1 (30fps), divisor at 24 (160x90 output resolution) CPU usage is about 2% of one cpu thread, memory usage is 17.3 KB according to HTOP. CPU usage is 0% when nothing is changing on the display. +While watching a video with a 3840x2160 60hz display, frameskip at 1 (30fps), divisor at 24 (160x90 output resolution) CPU usage is about 0.7% of one cpu thread, memory usage is 17.3 KB according to HTOP. CPU usage is 0% when nothing is changing on the display. ## Contributing diff --git a/hgx11grab.cpp b/hgx11grab.cpp index f3039f7..4e6f7a8 100644 --- a/hgx11grab.cpp +++ b/hgx11grab.cpp @@ -109,7 +109,7 @@ void hgx11grab::_grabFrame() _dstPicture_m = XRenderCreatePicture(_x11Display_p, _pixmap_m, _dstFormat_p, CPRepeat, &_pictAttr_m); XRenderSetPictureFilter(_x11Display_p, _srcPicture_m, FilterBilinear, nullptr, 0); - XRenderSetPictureTransform (_x11Display_p, _srcPicture_m, &_mTransform_m); + XRenderSetPictureTransform(_x11Display_p, _srcPicture_m, &_mTransform_m); XRenderComposite( _x11Display_p, // *dpy, @@ -129,29 +129,18 @@ void hgx11grab::_grabFrame() XSync(_x11Display_p, false); - XShmGetImage(_x11Display_p, _pixmap_m, _xImage_p, 0, 0, 0x00FFFFFF); + XShmGetImage(_x11Display_p, _pixmap_m, _xImage_p, 0, 0, 0xFFFFFFFF); if (_xImage_p == nullptr) { qWarning() << "Failed to get image from X11 server."; return; } + QImage qimg = *new QImage(reinterpret_cast(_xImage_p->data), _destWidth_m, _destHeight_m, _xImage_p->bytes_per_line, QImage::Format_RGBA8888); + qimg = qimg.convertToFormat(QImage::Format_RGB888); imgdata_m.clear(); + imgdata_m = QByteArray::fromRawData(reinterpret_cast(qimg.bits()), qimg.byteCount()); - if (_xImage_p->byte_order == 0) { // BGR - for (int i = 0; i < _imgSize_m; i += 4) { - imgdata_m.append(_xImage_p->data[i+2]); - imgdata_m.append(_xImage_p->data[i+1]); - imgdata_m.append(_xImage_p->data[i]); - } - } else { // RGB - for (int i = 0; i < _imgSize_m; i += 4) { - imgdata_m.append(_xImage_p->data[i]); - imgdata_m.append(_xImage_p->data[i+1]); - imgdata_m.append(_xImage_p->data[i+2]); - } - } - _freeResources(); imageCreated(); diff --git a/hgx11grab.h b/hgx11grab.h index 7b52a0b..9384207 100644 --- a/hgx11grab.h +++ b/hgx11grab.h @@ -3,6 +3,7 @@ #include #include +#include #include #include #include diff --git a/main.cpp b/main.cpp index 7dde676..e282431 100644 --- a/main.cpp +++ b/main.cpp @@ -6,12 +6,12 @@ static hgx11 *grab; static QCoreApplication *qapp; -static void quit(int sig) +static void quit(int) { if (grab != nullptr) { grab->~hgx11(); } - qapp->exit(sig); + qapp->exit(); } int main(int argc, char *argv[])