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

Rendering engine issues High GPU and CPU #7608

Open
Zilkode opened this issue Feb 15, 2022 · 13 comments
Open

Rendering engine issues High GPU and CPU #7608

Zilkode opened this issue Feb 15, 2022 · 13 comments
Assignees
Labels
Milestone

Comments

@Zilkode
Copy link

Zilkode commented Feb 15, 2022

Something is not working well with the rendering engine, i will explain.
There are two separate issues that both affect CPU and GPU usage:

  1. When the window is open the GPU goes up to 6%-7% and the CPU up to 2% without any activity or UI updates.
  2. When starting an application minimized the GPU and CPU usage are high without justification.

About (1) there is nothing to elaborate, those stats are too high.
regarding (2):
When starting the application normally and than minimizing GPU and CPU are at 0%, as expected,

Now take a look at the following screen shots.

Starting the application minimized (window is not visible):
image
CPU can go up to 2% and GPU to 1.5%

Window is visible:
image

Window minimized again:
image

@Zilkode Zilkode added the bug label Feb 15, 2022
@Zilkode Zilkode changed the title Rendering engine issues High GPU and CPU when app started minimized or idling Rendering engine issues High GPU and CPU Feb 15, 2022
@maxkatz6
Copy link
Member

without any activity or UI updates.

How is it known? Was "window. Renderer. DrawDirtyRects" used?

@Zilkode
Copy link
Author

Zilkode commented Feb 16, 2022

For the first part: I put breakpoint on every single method in my code (yes, it was very frustrating) to find out what is executing. nothing popped.

As for the second: Renderer and DrawDirtyRects wasn't used.
Window was used:

1.
MainWindow constructor:
nativeNotification.Handler.ShowMainWindowRequested += OnBackgroundRequestShowWindow;
  this.WindowState = WindowState.Minimized;
2.
protected override async void HandleWindowStateChanged(WindowState state)
        {
            base.HandleWindowStateChanged(state);

            await Task.Delay(100);
            if (state == WindowState.Minimized)
            {
                if (RuntimeConstants.MINIMIZE_TO_TRAY))
                {
                    this.ShowInTaskbar = false;
                    if (RuntimeConstants.PlatformHelper is INativeNotificationHelper nativeNotification)
                        nativeNotification.Handler.ShowRunningInBackgroundMessage(); //<- Windows.UI.Notifications  
                }
            }
            else
            {
                this.ShowInTaskbar = true;
            }
        }

private void OnBackgroundRequestShowWindow(object? sender, EventArgs e)
        {
            Dispatcher.UIThread.InvokeAsync(() =>
            {
                try
                {
                    if (this.WindowState == WindowState.Minimized)
                        this.WindowState = WindowState.Normal;
                    else
                    {
                        this.Topmost = true;
                        this.Topmost = false;
                    }
                }
                catch (Exception iex)
                {
                }
            });
        }

@Zilkode
Copy link
Author

Zilkode commented Feb 16, 2022

Just for clarification and better understanding..
The same symptoms occur when minimizing to Taskbar, not only tray (using Windows.UI.Notifications)
I did this check to eliminate to cause of the issue.

By the way its, Windows 11 (don't know about 10)

@maxkatz6
Copy link
Member

The same symptoms occur when minimizing to Taskbar, not only tray (using Windows.UI.Notifications)

It doesn't matter. It's just minimizing with additional taskbar icon.

As for the second: Renderer and DrawDirtyRects wasn't used.

Try to use it. Most likely there is UI activity. And it will show if anything rendered.
Also you can profile your code.

@Zilkode
Copy link
Author

Zilkode commented Feb 17, 2022

  1. Overriding render doesn't trigger anything except when intended.
  2. I couldn't find what is DrawDirtyRects and how to use it.
  3. As for the profiler, which led me to assume it's rendering issue at the first place, here's a screenshot:
    There is a lot happening with no reason, it keeps executing UpdateSubResource, Draw etc.
    image

After showing and minimizing the window the GPU is idle:
image

@maxkatz6
Copy link
Member

I couldn't find what is DrawDirtyRects and how to use it.

window.Renderer.DrawDirtyRects = true;

@Zilkode
Copy link
Author

Zilkode commented Feb 20, 2022

No good

@adirh3
Copy link
Contributor

adirh3 commented Nov 26, 2022

@maxkatz6 some of my users experienced the same issue as well.
I checked with empty hello world Avalonia app (11-preview) on Windows 11, and I see idle CPU usage of ~%1 (checked in taskmgr and process hacker) even if the window is closed (Shutdown mode = explicit).

I tried debugging it and it seems like the reason is that Avalonia's rendering loop runs even when a window is not visible or even closed.
This is the case in either WinUIComposition with RequestCommitAsync loop, or the normal DefaultRenderTimer when not using WinUIComposition.
In both cases Avalonia is processing messages continuously when idle (Timer every 1000/60 ms, and WinUI according to RequestCommitAsync).

Also checked with empty WPF app and it is consuming 1% when a window is focused, but when it's not it's completely 0.
Debugging it I see that the app waits in GetMessage when a window is not visible. Only when visible it will get a message and start rendering.

I'm not sure if WPF gets some kind of special treatment from Windows, but I think we should aim for 0 CPU/GPU usage when a window is not visible or if no window is opened.

@kekekeks
Copy link
Member

I tried debugging it and it seems like the reason is that Avalonia's rendering loop runs even when a window is not visible or even closed.

It's a known issue caused by our animation clock not unsubscribing from the loop when there are no active animations. It's not related to the renderer since DeferredRenderer does unsubscribe.

@adirh3
Copy link
Contributor

adirh3 commented Nov 27, 2022

I tried debugging it and it seems like the reason is that Avalonia's rendering loop runs even when a window is not visible or even closed.

It's a known issue caused by our animation clock not unsubscribing from the loop when there are no active animations. It's not related to the renderer since DeferredRenderer does unsubscribe.

I think I used the wrong term, I meant that the GetMessage loop is triggered continously in Avalonia even when no window was created. Is that caused by the animation clock? Does it subscribe even if th app starts with no window?

@kekekeks
Copy link
Member

Does it subscribe even if th app starts with no window?

Yes

@kekekeks kekekeks modified the milestones: 11.x, 11.0 Mar 4, 2023
@kekekeks kekekeks self-assigned this Mar 4, 2023
@maxkatz6 maxkatz6 modified the milestones: 11.0, 11.x Jul 5, 2023
@GabrielHalvonik
Copy link

ibing from the loop when there are no active animations. It's not related to the renderer since DeferredRenderer does unsubscribe

@kekekeks

Is there plan to fix it in nearly future? Cause having opened (even minimized) avalonia app on laptop for longer period of time is unberable.

@timunie
Copy link
Contributor

timunie commented Apr 5, 2024

@GabrielHalvonik you can add priority to issues if you have a support contract. otherwise it's done when it's done. thank you for your understanding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants