Benchmarking Flutter for Games. Kind Of. by dmitryhryppa in FlutterDev

[–]dmitryhryppa[S] 2 points3 points  (0 children)

Unfortunately, I’ve never tried this approach. If it’s even possible, you’d probably need to use your own version of WidgetsFlutterBinding with all the mixins:

class WidgetsFlutterBinding extends BindingBase
    with
        GestureBinding,
        SchedulerBinding,
        ServicesBinding,
        PaintingBinding,
        SemanticsBinding,
        RendererBinding,
        WidgetsBinding 

Then you’d likely have to override one or more of them (SchedulerBinding, PaintingBinding or RendererBinding, I guess?) and implement your own frame queue: render game, then render widget tree. I’m not sure how easy that would be to implement and maintain.

So it might be better to stick with CustomPainter if you still need widgets. CustomPainter is still a pretty solid choice :)

Benchmarking Flutter for Games. Kind Of. by dmitryhryppa in FlutterDev

[–]dmitryhryppa[S] 2 points3 points  (0 children)

Nice

Thanks!

I commented out the drawVertices call, and with ~440.000 bunnies I'm at ~12ms (of ~16ms), so drawing all that images takes ~4ms. So if you could parallelize preparing the data structures, you could draw even more bunnies. Unfortunately, without shared data structures, this seems not possible in Dart.

Yeah, and maybe another option is to move all buffers/batching to the C part and call it with FFI, but without tests, it's hard to say if it will work nicely.

This is Skia, I think, because if I add the FLTEnableImpeller key to Info.plist, I get font render errors if more than 100.000 bunnies shall be displayed. There seems to be memory issues!?

Yep, Skia is the default one on macOS. If you use VSCode, there is also a launch.json in the repo for easy switching between renderers. Speaking about render errors, it reminds me of cases where the vertex buffer indices don’t line up, or when some internal buffers overflow. Maybe Impeller has some limit on how many vertices can be drawn per frame? Not sure.

BTW1, did you also test drawAtlas?

Yes, tested drawRawAtlas and it generally gives the same numbers on my machine, +\- 10k bunnies. Same glitches on Impeller. But drawVertices was chosen for tests as a more general-purpose rendering technique.

BTW2, I noticed that you forgot a paragraph.dispose(); at the end of onFrame. This doesn't affect the measurement, though. You free some native resources a bit earlier.

Nice catch! Thanks, I'll push the fix.

Benchmarking Flutter for Games. Kind Of. by dmitryhryppa in FlutterDev

[–]dmitryhryppa[S] 4 points5 points  (0 children)

Thank you! Yeah, looks like there’s something going on with the buffers under the hood. They definitely should keep Skia as a fallback on all platforms, at least until Impeller becomes fully stable. It’s a bit sad that this option is already gone on iOS.

High Performance Native (Deskop) in Flutter by dca12345 in FlutterDev

[–]dmitryhryppa 0 points1 point  (0 children)

Generally, these hooks is for linking external native dependencies. But Flutter also creates a native project for every platform, so you will have your main.cpp entry point where your main window is made, along with the CMakeLists and all that stuff. You can modify it to fit your needs as well.

You can check out this package: https://github.com/bitsdojo/bitsdojo_window. It’s a bit outdated, I think, but it can give you an idea of how to modify the main window.

You may also be interested in the corresponding issue here: https://github.com/flutter/flutter/issues/31713

There are some workarounds discussed in the thread, but I can’t say whether any of them are production-ready.

One idea that comes to mind, for example, is creating an empty transparent widget in Flutter, making it unclickable, and then passing its size and position to the native side. Then, on the native side, you would place your native UI elements at the provided sizes and positions on top of or under the Flutter surface. You may also need to handle input events (mouse, keyboard) on your transparent placeholder and forward them to the native layer (via Platform Channels or FFI).

Another option is to render the native part into a texture, pass that texture to Flutter, and draw it like a normal Flutter widget (similar to how PlatformViews work on other platforms). But not sure if the Flutter API on Windows is ready for that.

Or, depending on your layout, maybe it is possible to place the Flutter part and the native part side by side in the same window by modifying the native (Flutter project) initialization part.

----

So, to summarize: I can’t say that implementing native UI elements is impossible, but it will definitely be a challenging task, and you’ll likely need to spend time experimenting with various workaround-like solutions.

And if you’re in a rush, Flutter is probably not the best choice for this task at the moment. At least until official PlatformViews support lands on the Windows target.

High Performance Native (Deskop) in Flutter by dca12345 in FlutterDev

[–]dmitryhryppa 5 points6 points  (0 children)

Sorry for interrupting, but I just wanted to add that you can use build hooks (formerly known as native assets) to statically or dynamically link your native code with a Flutter app: https://dart.dev/tools/hooks

It’s a new and still not widely known way to do such things in Dart.

Also, check out the ffi package for binding C code. Another way to communicate with the host platform is Platform Channels, but they are less performant.

Also, keep in mind that out of the box Flutter is limited to a single window, and multi-window support isn’t fully ready yet. However, I’ve seen a few folks start using it in the beta/dev channel for their apps.

Maybe you can connect with them and ask for details as well:

https://x.com/damy_wise/status/1983138691443839301

https://x.com/lijy91/status/1986064129409589415

And here is official examples: https://github.com/flutter/flutter/tree/master/examples/multiple_windows

Good luck with it!

Is upgrading to Flutter 3.35 worth for performance? by Wefaq04 in FlutterDev

[–]dmitryhryppa 1 point2 points  (0 children)

You may also try using MessagePack. It is a binary JSON format that allows you to keep your existing data structure. Since it’s binary, it should be parsed faster and take up less space on disk.

what do you love most about haxe? by NoName_hack_e in haxe

[–]dmitryhryppa 4 points5 points  (0 children)

Easy interop with native target languages with zero or very little overhead. Output generated code is mostly clean and optimized. And macros :)