Use modern GTK4 snapshot API when available - #32251
Conversation
|
Regarding my slowdown comment from yesterday, here's a line profile: And the native calls via Xcode Instruments: On my machine, we are spending 15% in the Agg draw and 85% of the time in If I change it to: buf = GLib.Bytes.new(bytes(view.cast('B')))the resize seems much more responsive, although I'm not sure if there's a better way. This may be related to Python bytearray to GBytes conversion is very slow. |
GTK 4.14 adds enough path API that we can fully avoid Cairo, so it makes more sense to move to the new API now. This means we can drop Cairo, and also not do any processing ourselves to change formats. The texture is likely cached on the GPU, so we cache the object on the canvas until re-drawing occurs. On older versions, we fall back to a version using Cairo, but this is only required for the zoom rectangle. Also fix some deprecations in the GTK4 examples.
On fractional HiDPI screens, just placing the texture directly will use default scaling, which will result in blurry output. By rounding the position and using nearest scaling, we get direct pixel output and no blurriness.
For some reason, the Gtk.Image loads the icon at 16 pixels, even though the toolbar and the button are sized to 24. With Gtk.Picture, it displays at the "natural" size, which matches the 24 pixels we expect, and also resizes correctly if a theme uses a different size toolbar.
|
Thanks for investigating so deeply. For me, it is only 82.5% draw vs 16.5% conversion to |
011e19d to
8ba6a92
Compare
It looks like that PR was released in 3.57.1/3.58.0, but for me, it actually regresses the ratio to 72.1% draw vs 27% conversion. So I guess we should stay with |
iccir
left a comment
There was a problem hiding this comment.
I tested the latest on macOS 27 with gtk4 and everything seems good.

PR summary
On modern GTK 4, the Snapshot API provides enough of a Path API that we can use it for drawing the zoom overlay. This means that we can drop the use of Cairo entirely. The Snapshot API can then draw the overlay on the GPU, and we keep the texture for the figure around so it also remains on the GPU.
Additionally, on even newer GTK 4, we can specify the scaling of the texture, and so we can round out the location and disable the linear scaling filter so that the canvas is no longer blurry on fractional HiDPI screens.
Also, fix the size of the icons in the toolbar.
AI Disclosure
None
PR quality check