Skip to content
This repository was archived by the owner on May 6, 2021. It is now read-only.

Commit b8c60cf

Browse files
committed
Added performance test for x11 grabbing
1 parent 53dab1a commit b8c60cf

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-0
lines changed

test/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ add_executable(test_qtscreenshot TestQtScreenshot.cpp)
5353
target_link_libraries(test_qtscreenshot
5454
${QT_LIBRARIES})
5555

56+
if(ENABLE_X11)
57+
# Find X11
58+
find_package(X11 REQUIRED)
59+
60+
add_executable(test_x11performance TestX11Performance.cpp)
61+
target_link_libraries(test_x11performance ${X11_LIBRARIES} ${QT_LIBRARIES})
62+
endif(ENABLE_X11)
63+
5664
add_executable(determineWs2811Timing DetermineWs2811Timing.cpp)
5765

5866
add_executable(test_rs232highspeed

test/TestX11Performance.cpp

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
2+
// X11 includes
3+
#include <X11/Xlib.h>
4+
#include <X11/Xutil.h>
5+
6+
#include <QElapsedTimer>
7+
8+
#include <utils/Image.h>
9+
#include <utils/ColorRgb.h>
10+
11+
void foo_1(int pixelDecimation)
12+
{
13+
int cropWidth = 0;
14+
int cropHeight = 0;
15+
16+
Image<ColorRgb> image(64, 64);
17+
18+
/// Reference to the X11 display (nullptr if not opened)
19+
Display * x11Display;
20+
21+
const char * display_name = nullptr;
22+
x11Display = XOpenDisplay(display_name);
23+
24+
std::cout << "Opened display: " << x11Display << std::endl;
25+
26+
XWindowAttributes window_attributes_return;
27+
XGetWindowAttributes(x11Display, DefaultRootWindow(x11Display), &window_attributes_return);
28+
29+
int screenWidth = window_attributes_return.width;
30+
int screenHeight = window_attributes_return.height;
31+
std::cout << "[" << screenWidth << "x" << screenHeight <<"]" << std::endl;
32+
33+
// Update the size of the buffer used to transfer the screenshot
34+
int width = (screenWidth - 2 * cropWidth + pixelDecimation/2) / pixelDecimation;
35+
int height = (screenHeight - 2 * cropHeight + pixelDecimation/2) / pixelDecimation;
36+
image.resize(width, height);
37+
38+
const int croppedWidth = screenWidth - 2*cropWidth;
39+
const int croppedHeight = screenHeight - 2*cropHeight;
40+
41+
QElapsedTimer timer;
42+
timer.start();
43+
44+
XImage * xImage = XGetImage(x11Display, DefaultRootWindow(x11Display), cropWidth, cropHeight, croppedWidth, croppedHeight, AllPlanes, ZPixmap);
45+
46+
std::cout << "Captured image: " << xImage << std::endl;
47+
48+
// Copy the capture XImage to the local image (and apply required decimation)
49+
ColorRgb * outputPtr = image.memptr();
50+
for (int iY=(pixelDecimation/2); iY<croppedHeight; iY+=pixelDecimation)
51+
{
52+
for (int iX=(pixelDecimation/2); iX<croppedWidth; iX+=pixelDecimation)
53+
{
54+
// Extract the pixel from the X11-image
55+
const uint32_t pixel = uint32_t(XGetPixel(xImage, iX, iY));
56+
57+
// Assign the color value
58+
outputPtr->red = uint8_t((pixel >> 16) & 0xff);
59+
outputPtr->green = uint8_t((pixel >> 8) & 0xff);
60+
outputPtr->blue = uint8_t((pixel >> 0) & 0xff);
61+
62+
// Move to the next output pixel
63+
++outputPtr;
64+
}
65+
}
66+
67+
// Cleanup allocated resources of the X11 grab
68+
XDestroyImage(xImage);
69+
70+
std::cout << "Time required: " << timer.elapsed() << " us" << std::endl;
71+
72+
XCloseDisplay(x11Display);
73+
}
74+
75+
void foo_2(int pixelDecimation)
76+
{
77+
int cropWidth = 0;
78+
int cropHeight = 0;
79+
80+
Image<ColorRgb> image(64, 64);
81+
82+
/// Reference to the X11 display (nullptr if not opened)
83+
Display * x11Display;
84+
85+
const char * display_name = nullptr;
86+
x11Display = XOpenDisplay(display_name);
87+
88+
XWindowAttributes window_attributes_return;
89+
XGetWindowAttributes(x11Display, DefaultRootWindow(x11Display), &window_attributes_return);
90+
91+
int screenWidth = window_attributes_return.width;
92+
int screenHeight = window_attributes_return.height;
93+
std::cout << "[" << screenWidth << "x" << screenHeight <<"]" << std::endl;
94+
95+
// Update the size of the buffer used to transfer the screenshot
96+
int width = (screenWidth - 2 * cropWidth + pixelDecimation/2) / pixelDecimation;
97+
int height = (screenHeight - 2 * cropHeight + pixelDecimation/2) / pixelDecimation;
98+
image.resize(width, height);
99+
100+
const int croppedWidth = screenWidth - 2*cropWidth;
101+
const int croppedHeight = screenHeight - 2*cropHeight;
102+
103+
QElapsedTimer timer;
104+
timer.start();
105+
106+
// Copy the capture XImage to the local image (and apply required decimation)
107+
ColorRgb * outputPtr = image.memptr();
108+
for (int iY=(pixelDecimation/2); iY<croppedHeight; iY+=pixelDecimation)
109+
{
110+
for (int iX=(pixelDecimation/2); iX<croppedWidth; iX+=pixelDecimation)
111+
{
112+
XImage * xImage = XGetImage(x11Display, DefaultRootWindow(x11Display), iX, iY, 1, 1, AllPlanes, ZPixmap);
113+
// Extract the pixel from the X11-image
114+
const uint32_t pixel = uint32_t(XGetPixel(xImage, 0, 0));
115+
116+
// Assign the color value
117+
outputPtr->red = uint8_t((pixel >> 16) & 0xff);
118+
outputPtr->green = uint8_t((pixel >> 8) & 0xff);
119+
outputPtr->blue = uint8_t((pixel >> 0) & 0xff);
120+
121+
// Move to the next output pixel
122+
++outputPtr;
123+
124+
// Cleanup allocated resources of the X11 grab
125+
XDestroyImage(xImage);
126+
}
127+
}
128+
std::cout << "Time required: " << timer.elapsed() << " us" << std::endl;
129+
130+
131+
XCloseDisplay(x11Display);
132+
}
133+
134+
int main()
135+
{
136+
foo_1(10);
137+
foo_2(10);
138+
return 0;
139+
}

0 commit comments

Comments
 (0)