r/FlutterDev • • Jun 01 '23

Discussion Flutter Desktop apps on Linux should soon feel a lot faster and more CPU efficient!

Hey, I recently contributed a performance fix to the GDK X11 backend that is used internally in the Flutter engine. The fix eliminates a slow X11 native code path that severely crippled the performance of Flutter applications making use of even simple animations.

https://gitlab.gnome.org/GNOME/gtk/-/merge_requests/6042

From my own testing, I have seen total CPU usage drop by roughly 2-3 times in addition to the app feeling buttery smooth now.

Unfortunately because Flutter uses the version of GTK that is shipped with the system, the time it will take for this fix to make its way downstream will depend on the Linux distribution you are using, so I'm currently trying to figure out if flutter can implement its own GDK texture rendering function. Happy coding :)

215 Upvotes

27 comments sorted by

33

u/KoodiMetsa Jun 01 '23

Great work, thank you!

6

u/[deleted] Jun 01 '23

:)

4

u/uldall Jun 01 '23

Sounds great! Is this only relevant for the 3.x branch? I.e. if Flutter upgraded to use gtk 4.x then this would not be relevant? I just want to understand how I can apply this fix to our custom Linux distribution :)

12

u/[deleted] Jun 01 '23

This is only the 3.24.x branch yes. I don't think Flutter will start depending on GTK4 for quite some time but when it does, I would expect performance to be a little higher than what we're currently dealing with in GTK3. GTK4 completely removes the Cairo dependency and only relies on OpenGL. They also switched from GLX to EGL which is more modern and has better hardware acceleration support.

5

u/purplefox69 Jun 01 '23

But doesn't Flutter use vulkan for graphics? This looks like it is specific to opengl.

15

u/[deleted] Jun 01 '23

The only use of Vulkan I am familiar with is on android when using Impeller, and even that is a work in progress.

The linux embedder uses GDK under the hood which uses OpenGL/GLES to render textures.

2

u/purplefox69 Jun 01 '23

Really weird if they don't use vulkan, since skia can use it on linux: https://skia.org/docs/user/special/vulkan/

13

u/[deleted] Jun 01 '23

Flutter might or might not use Vulkan when it comes to the way it renders widgets. The fix that I am talking about above is part of the linux embedder which handles surface creation. Essentially you have the cross-platform engine that composites together all of the different widgets and then outputs a final texture that can be used by an embedder. The Linux embedder uses GDK to render that final texture and makes use of OpenGL/GLES.

I'm not really sure if one could use Vulkan for the rendering of the widgets and GLES for the texture...

3

u/anlumo Jun 01 '23

GTK doesn't support Vulkan. I was flabbergasted when I realized that (I tried to patch the Linux shell code to use Vulkan).

It's possible to show Vulkan in a GTK window by taking over its low level window reference and overwriting whatever GTK is doing there, but then GTK is just a glorified window creation library. The Linux shell uses it for more than that.

1

u/anlumo Jun 01 '23

The Fuchsia shell also uses Vulkan AFAIK.

2

u/anlumo Jun 01 '23

That doesn't apply when working under Wayland, right?

Flutter does have direct Wayland support (without XWayland, that is). It's just disabled by default when running from within Visual Studio Code, but I was able to force it on again (which I had to do because it breaks with mixed DPI screens).

3

u/[deleted] Jun 01 '23 edited Jun 01 '23

Flutter has direct wayland support via GTK. I haven't tested this but given that the patch deletes code that is also used by the wayland backend, I'd also expect it to also benefit a little. But yeah, this patch mainly fixes the crippling X11 lag.

1

u/NaiveInvestigator Jun 03 '23

how do i run flutter on direct wayland and not xwayland? I tried looking around but sadly couldnt find anything

2

u/anlumo Jun 03 '23

You have to set the GDK_BACKEND environment variable to wayland or unset it.

If you run the app with flutter run -d linux from the command line, it works fine, but if you run it from vscode that variable is set to x11 for some unkown reason. I've changed my launch configuration to

    {
        "name": "launch app",
        "request": "launch",
        "type": "dart",
        "env": {
            "GDK_BACKEND": "wayland"
        }
    }

to fix this.

1

u/ravenravener Sep 02 '24

sorry for replying on an old comment but I wanted to mention that 'unknown reason': it seems that launching an app from an xwayland app will launch it in x11 mode, VSCode still runs on Xwayland so those apps launching from it defaults to x11. Same is true with for example discord uses x11 so if I click a link and it opens my browser in x11 mode. This might be because xwayland apps are in an X11 environment so it ends up passing those environments to child processes making them also think they are in X11.

1

u/NaiveInvestigator Jun 04 '23

TYSM for the help! This worked for me!

18

u/DrFossil Jun 01 '23

This is pretty cool, thanks!

To be honest I generally run my mobile apps on the Linux desktop during development since it's so much easier than testing on a device, and I always found the performance acceptable, but more of always better.

14

u/psylomatika Jun 01 '23

Awesome. Happy to see posts like these.

1

u/Accomplished_Low2231 Jun 02 '23

will this fix the bugs in the ubuntu desktop (23.04) installer?

2

u/[deleted] Jun 02 '23

This fix is dependent on Distros shipping a version of GTK that has that patched in it. Given that it was just merged in the main channel of gtk-24, it'll take a while to find itself downstream.

I'll see if I can manually open up patches for Distros like Debian, Ubuntu and Fedora but this isn't an area I'm very familiar with.

If this works, I'll make another post about it :)

3

u/o_Zion_o Jun 01 '23

Awesome stuff! Thank you.

3

u/Gears6 Jun 02 '23

Your contribution is appreciated!

1

u/[deleted] Jun 02 '23

Brilliant work!

1

u/scalatronn Jun 02 '23

I'm still waiting for the day that official flutter embedder will be just Wayland client

1

u/MannyManMoin Jun 02 '23

would this fix slow loading times of eg. custompaint and pathdrawing aswell on Android ? I mean on iphone 11 is blazing fast (I have 14 pro also 11 to test older iphones), but on Android even newer a complex SVG with path's and custompaint its alot slower to redraw.

1

u/[deleted] Jun 04 '23

Thank you!!!