From 6b63b57f17f299f36c67dc69552d825f80f8532f Mon Sep 17 00:00:00 2001 From: "T. van der Zwan" Date: Tue, 25 Feb 2014 22:08:54 +0100 Subject: [PATCH] Added performance test for x11 grabbing Former-commit-id: b8c60cf9984c6961675b41002bed40d251bff9fa --- test/CMakeLists.txt | 8 +++ test/TestX11Performance.cpp | 139 ++++++++++++++++++++++++++++++++++++ 2 files changed, 147 insertions(+) create mode 100644 test/TestX11Performance.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index bd46d059..8bf17456 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -53,6 +53,14 @@ add_executable(test_qtscreenshot TestQtScreenshot.cpp) target_link_libraries(test_qtscreenshot ${QT_LIBRARIES}) +if(ENABLE_X11) + # Find X11 + find_package(X11 REQUIRED) + + add_executable(test_x11performance TestX11Performance.cpp) + target_link_libraries(test_x11performance ${X11_LIBRARIES} ${QT_LIBRARIES}) +endif(ENABLE_X11) + add_executable(determineWs2811Timing DetermineWs2811Timing.cpp) add_executable(test_rs232highspeed diff --git a/test/TestX11Performance.cpp b/test/TestX11Performance.cpp new file mode 100644 index 00000000..9b7fb402 --- /dev/null +++ b/test/TestX11Performance.cpp @@ -0,0 +1,139 @@ + +// X11 includes +#include +#include + +#include + +#include +#include + +void foo_1(int pixelDecimation) +{ + int cropWidth = 0; + int cropHeight = 0; + + Image image(64, 64); + + /// Reference to the X11 display (nullptr if not opened) + Display * x11Display; + + const char * display_name = nullptr; + x11Display = XOpenDisplay(display_name); + + std::cout << "Opened display: " << x11Display << std::endl; + + XWindowAttributes window_attributes_return; + XGetWindowAttributes(x11Display, DefaultRootWindow(x11Display), &window_attributes_return); + + int screenWidth = window_attributes_return.width; + int screenHeight = window_attributes_return.height; + std::cout << "[" << screenWidth << "x" << screenHeight <<"]" << std::endl; + + // Update the size of the buffer used to transfer the screenshot + int width = (screenWidth - 2 * cropWidth + pixelDecimation/2) / pixelDecimation; + int height = (screenHeight - 2 * cropHeight + pixelDecimation/2) / pixelDecimation; + image.resize(width, height); + + const int croppedWidth = screenWidth - 2*cropWidth; + const int croppedHeight = screenHeight - 2*cropHeight; + + QElapsedTimer timer; + timer.start(); + + XImage * xImage = XGetImage(x11Display, DefaultRootWindow(x11Display), cropWidth, cropHeight, croppedWidth, croppedHeight, AllPlanes, ZPixmap); + + std::cout << "Captured image: " << xImage << std::endl; + + // Copy the capture XImage to the local image (and apply required decimation) + ColorRgb * outputPtr = image.memptr(); + for (int iY=(pixelDecimation/2); iYred = uint8_t((pixel >> 16) & 0xff); + outputPtr->green = uint8_t((pixel >> 8) & 0xff); + outputPtr->blue = uint8_t((pixel >> 0) & 0xff); + + // Move to the next output pixel + ++outputPtr; + } + } + + // Cleanup allocated resources of the X11 grab + XDestroyImage(xImage); + + std::cout << "Time required: " << timer.elapsed() << " us" << std::endl; + + XCloseDisplay(x11Display); +} + +void foo_2(int pixelDecimation) +{ + int cropWidth = 0; + int cropHeight = 0; + + Image image(64, 64); + + /// Reference to the X11 display (nullptr if not opened) + Display * x11Display; + + const char * display_name = nullptr; + x11Display = XOpenDisplay(display_name); + + XWindowAttributes window_attributes_return; + XGetWindowAttributes(x11Display, DefaultRootWindow(x11Display), &window_attributes_return); + + int screenWidth = window_attributes_return.width; + int screenHeight = window_attributes_return.height; + std::cout << "[" << screenWidth << "x" << screenHeight <<"]" << std::endl; + + // Update the size of the buffer used to transfer the screenshot + int width = (screenWidth - 2 * cropWidth + pixelDecimation/2) / pixelDecimation; + int height = (screenHeight - 2 * cropHeight + pixelDecimation/2) / pixelDecimation; + image.resize(width, height); + + const int croppedWidth = screenWidth - 2*cropWidth; + const int croppedHeight = screenHeight - 2*cropHeight; + + QElapsedTimer timer; + timer.start(); + + // Copy the capture XImage to the local image (and apply required decimation) + ColorRgb * outputPtr = image.memptr(); + for (int iY=(pixelDecimation/2); iYred = uint8_t((pixel >> 16) & 0xff); + outputPtr->green = uint8_t((pixel >> 8) & 0xff); + outputPtr->blue = uint8_t((pixel >> 0) & 0xff); + + // Move to the next output pixel + ++outputPtr; + + // Cleanup allocated resources of the X11 grab + XDestroyImage(xImage); + } + } + std::cout << "Time required: " << timer.elapsed() << " us" << std::endl; + + + XCloseDisplay(x11Display); +} + +int main() +{ + foo_1(10); + foo_2(10); + return 0; +}