Commit 8575606
authored
linux: add an XCB-based backend (#426)
* Working implementation of XCB-based screenshots
This version works fine, but needs improvements, especially to the
testing.
What has happened:
* A ctypes-based module to use XCB has been added.
* The mss.linux.MSS constructor is now a factory, which takes an
optional `backend` keyword parameter.
* The previous mss.linux implementation has now been designated as the
"xlib" backend. Per #425, this will have "legacy" status, and new
functionality like XShmGetImage won't be implemented there.
* A new XCB-based backend, named "getimage", has been added. This is
functionally the same as the existing "xlib" backend, in almost all
respects, but has a different implementation.
* The GNU/Linux-specific tests that could easily be made to work with
the new backend have been adjusted.
Notable improvement: I previously was seeing periodic thread failures
in `test_thread_safety`. In one scenario (Docker container, Python
3.9, Xvfb), I tested 84% failures in `test_thread_safety`. With the
XCB-based backend, I saw no failures. (See also #251 and the test
failure I mentioned in #421.)
The performance is, in my brief testing, the same as the existing xlib
implementation. This PR is about making clearer code and improving
thread safety, to pave the way for future improvements.
What's left to do (some before merging, some possibly later):
* The way the libraries are initialized needs to be rethought; I'll
want to talk with @BoboTiG about that.
* Many tests in `test_gnu_linux` used monkey-patches that are specific
to the Xlib implementation. I've arranged for those to only use
the Xlib implementation; I need to add corresponding tests for the
XCB implementation.
* The tests outside of `test_gnu_linux` need to test both backends, as
well as the different backends we may add to other OSs. I need to
add the right Pytest code to parameterize these.
* This code introduces new automatic memory management aspects that
need tests assigned to them.
* The bulk of the `xcb.py` code is just hand-translations of the [XCB
protocol specification XML
files](https://gitlab.freedesktop.org/xorg/proto/xcbproto).
Hand-translation can be error-prone (see #418). Since the XCB specs
are in XML, then the bulk of this code can be auto-generated, like
xcffib does. This can be done manually as needed, before shipping
the distribution, so end users won't have to do this. (See #425 for
a discussion of why we're not using xcffib.)
* Even if we don't auto-generate the code, some of the repetitive
parts can still be hoisted into higher-order functional programming,
to reduce the risk of making errors in copying.
* There are some parts that I worsened, to satisfy ruff. I want to
revisit those.
* The proof-of-concept code in `xcb.py:main` includes some additional
functionality should be integrated into the MSS class, either in
this PR or in separate branches. We also should delete that code
before merging.
* Additional testing on the X11 visual, to make sure it's really in
the format we expect. (This is probably the only part we'll merge
in with this PR.)
* Support for capturing an individual window, without needing its
location.
* Identifying whether the alpha channel is meaningful or not. (This
is only relevant with individual-window capture; there aren't many
screens that can turn transparent!)
Fixes: #425
* Improve test coverage
Some tests now take a "backend" parameter. A fixture will iterate
that through the backends implemented for that OS.
Many tests called mss(display=os.getenv("DISPLAY")). Rather than add
"backend" parameters to each of them, and maybe need to make other
changes in the future, these now take an "mss_impl" fixture. This is
a callable that will return the appropriate MSS object.
One reason for this is so that we might add a pyvirtualdisplay or
similar mechanism to these functions, when it's available. This may
enable some tests to run in conditions they might not otherwise. It
also may help standardize some of the testing.
* Ruff cleanups
* Small bug fixes to get the tests back to doing meaningful testing
* XCB: Verify visual, and improve error messages
The MSS object, when it's created, will now ensure that the X11
visual's format is exactly what we think it is: 32bpp, as a 24-bit
BGRx or 32-bit BGRA image, in that order, with no extra scanline
padding, color indexing, etc. This is true of all modern X servers in
real-world situations, but it's not guaranteed. For instance,
VNC-based servers can be run in depth 16 with RGB565 formats.
The error messages for protocol-level errors (such as bad coordinates)
are only available if the xcb-util-errors library is installed.
(That's libxcb-errors.so.0, in the libxcb-errors0 package on
Debian/Ubuntu.) This isn't widely installed, and is mostly useful to
developers, so it's optional. The connection-level errors (like the
hint to check $DISPLAY) are available everywhere.
* Move the library globals to a smart container class
* Improve testing
Test the cases of missing libraries when using XCB.
Add fixtures to reset the XCB library loader after each testcase.
Add fixtures to check for Xlib errors after each testcase.
* Use generated code to interface with XCB instead of hand-written code
* Fix bug in multithreaded initialization guard
* Improve testing and comments
* Add src/xcbproto/README.md
* Fix distribution list to include README.md1 parent 2dfc726 commit 8575606
File tree
29 files changed
+11323
-152
lines changed- src
- mss
- linux
- tests
- third_party
- xcbproto
29 files changed
+11323
-152
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
72 | 72 | | |
73 | 73 | | |
74 | 74 | | |
| 75 | + | |
75 | 76 | | |
76 | 77 | | |
77 | 78 | | |
| |||
134 | 135 | | |
135 | 136 | | |
136 | 137 | | |
| 138 | + | |
137 | 139 | | |
138 | 140 | | |
139 | 141 | | |
| |||
187 | 189 | | |
188 | 190 | | |
189 | 191 | | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
| 43 | + | |
43 | 44 | | |
44 | 45 | | |
45 | 46 | | |
| |||
51 | 52 | | |
52 | 53 | | |
53 | 54 | | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
54 | 60 | | |
55 | 61 | | |
56 | 62 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
0 commit comments