Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix iOS Dispatcher #13942

Merged
merged 4 commits into from
Dec 21, 2023
Merged

Fix iOS Dispatcher #13942

merged 4 commits into from
Dec 21, 2023

Conversation

maxkatz6
Copy link
Member

@maxkatz6 maxkatz6 commented Dec 14, 2023

What does the pull request do?

Two major changes:

  1. Replaces kCFRunLoopCommonModes with kCFRunLoopDefaultMode on loop observer and timer. Original code was copy-pasted from our macOS backend, where we used kCFRunLoopCommonModes. But apparently it's a no-op on iOS. This was an actual bug in our backend, as the timer wasn't ticking basically.
  2. Replaces delegates with function pointers where in dispatcher code. And ensuring that we don't have any runtime marshaling with dll imports. Both should result in less overhead.

Fixed issues

I have not tested all of these issues, but only animations+timer related. Let me know if any of these issues is still reproducible.

Fixes #13594
Fixes #13110
Fixes #13029
Fixes #11696
Fixes #13941

@maxkatz6 maxkatz6 changed the title Fix ios dispatcher Fix iOS Dispatcher Dec 14, 2023
@maxkatz6 maxkatz6 added bug os-ios backport-candidate-11.0.x Consider this PR for backporting to 11.0 branch labels Dec 14, 2023
@maxkatz6 maxkatz6 requested a review from a team December 14, 2023 01:45
@avaloniaui-bot
Copy link

You can test this PR using the following package version. 11.1.999-cibuild0042885-beta. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@avaloniaui-bot
Copy link

You can test this PR using the following package version. 11.1.999-cibuild0043034-beta. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@maxkatz6 maxkatz6 added this pull request to the merge queue Dec 21, 2023
Merged via the queue into master with commit 447b9b9 Dec 21, 2023
7 checks passed
@maxkatz6 maxkatz6 deleted the fix-ios-dispatcher branch December 21, 2023 22:04
@maxkatz6 maxkatz6 added backported-11.0.x and removed backport-candidate-11.0.x Consider this PR for backporting to 11.0 branch labels Jan 17, 2024
maxkatz6 added a commit that referenced this pull request Jan 17, 2024
* Replace kCFRunLoopCommonModes with kCFRunLoopDefaultMode

* Replace delegates with function pointers in iOS backend

* Update DispatcherImpl.cs
@cangunaydin
Copy link

don't know if this is directly related but #13941 is still not fixed.
InvalidateVisual is still triggering with very much latency, sometimes it is not even triggered.
Can be related with dispatcher but not sure.
i have created a minimal repro with the problem if you want to check.
https://github.com/cangunaydin/avalonia-customskia-ios-bug

@maxkatz6
Copy link
Member Author

maxkatz6 commented Jan 19, 2024

@cangunaydin please use a newer invalidation APIs: TopLevel.GetTopLevel(thisControll).RequestAnimationFrame(callback). You can use InvalidateVisual as a callback.

In short, Dispatcher shouldn't be used to sync with framerate. It was an old approach of 0.10 version. RequestAnimationFrame will actually sync with the framerate.

@cangunaydin
Copy link

Hello again,
I tried but it doesn't help. same behavior. check the image 6 or 7 ticks happening in 30secs.
Screenshot 2024-01-20 at 11 52 48

@maxkatz6
Copy link
Member Author

Can you test on a real device? .NET runtime is rather slow in the simulator.
If not, I can try on the next week.

@maxkatz6
Copy link
Member Author

Either way, it doesn’t look related to the old issue. Previously dispatcher wouldn’t raise any timer ticks at all. In your case there is something ticking

@cangunaydin
Copy link

sure i will try it on the real device. Yes you are right it is not related with the old issue. But don't think the behavior will change on real device. The weird part is like 2 days ago it seems it was working with dispatcher, i was playing with it and it is broken now :) maybe it can be related with whatever i did. But same sample works fine on android emulator
if it doesn't work with real device, should i open a new issue instead?

@cangunaydin
Copy link

I have tested it on iphone 12, it is much more better. But still it is around 28fps. It seems like sth else is slowing it down.
on android emulator it is around 60fps, on my old samsung galaxy tablet Galaxy Tab A (2016) it is only 2fps.

@maxkatz6
Copy link
Member Author

@cangunaydin so, I have 60 fps on iPhone 15. Though there shouldn't be such difference, unless you have limited FPS or there some another problem with configuration.

  1. How do you measure FPS? You should use TopLevel.GetTopLevel(this).RendererDiagnostics.DebugOverlays |= RendererDebugOverlays.Fps;.
  2. Simulator apps are known to be much slower. They also run JIT interpreter. It's still surprising that iphone 12 has such bad fps.
  3. You can try to enable Metal rendering in the latest master (was merged today):
            .With(new iOSPlatformOptions
            {
                RenderingMode = new [] { iOSRenderingMode.Metal }
            })
Screenshot 2024-01-21 at 1 36 52 AM

IMG_0517

@cangunaydin
Copy link

Hello @maxkatz6 ,
I have tried with

 .With(new iOSPlatformOptions
            {
                RenderingMode = new [] { iOSRenderingMode.Metal }
            })

and it increased to 60fps. weird though why it has so much difference with opengl.
I was using my own calculation for fps.
Screenshot 2024-01-22 at 02 27 26

But it has the same value with opLevel.GetTopLevel(this).RendererDiagnostics.DebugOverlays |= RendererDebugOverlays.Fps;
is there a way to set the fps overlay somewhere else than safe areas?

@maxkatz6
Copy link
Member Author

Good to know that Metal helps.

No, I don’t think it’s possible to move fps counter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment