fix: disable background throttling also in the viz::DisplayScheduler (#38924)

* fix: disable background throttling also in the `viz::DisplayScheduler`

`viz::DisplayScheduler` is responsible for drawing and swapping frames
in the `DisplayScheduler::DrawAndSwap` which is called from the
`DisplayScheduler::AttemptDrawAndSwap` if the `DisplayScheduler::ShouldDraw`
returns true. `ShouldDraw` depends on the `DisplayScheduler` visibility
and when it is not visible then it returns false.

In order to keep producing frames, disabling `backgroundThrottling`
should also prevent changing `DisplayScheduler` visibility to false.

`DisplayScheduler` lives in the `ui::Compositor` where every
`electron::NativewWindow` has its own `Compositor`. `electron::NativewWindow`
may be host of the multiple `electron::api::WebContents` instances which may
have different `WebPreferences` settings. Therefore if at least one
of the `WebContents` requires disabling throttling then all other `WebContents`
using the same window will have it disabled in the `ui::Compositor`.

BREAKING CHANGE:
`backgroundThrottling` set to false will disable frames throttling
in the `BrowserWindow` for all `WebContents` displayed by it.

Close: [#31016](https://github.com/electron/electron/issues/31016)

* fixup! fix: disable background throttling also in the `viz::DisplayScheduler`

* fixup! fix: disable background throttling also in the `viz::DisplayScheduler`

* fixup! fix: disable background throttling also in the `viz::DisplayScheduler`

---------

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>
This commit is contained in:
michal-pichlinski-openfin 2023-09-26 22:00:46 +02:00 committed by GitHub
parent fa215f1009
commit 2190793fe6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 176 additions and 3 deletions

View file

@ -14,6 +14,7 @@
#include "base/values.h"
#include "content/public/browser/web_contents_user_data.h"
#include "include/core/SkColor.h"
#include "shell/browser/background_throttling_source.h"
#include "shell/browser/browser.h"
#include "shell/browser/native_window_features.h"
#include "shell/browser/ui/drag_util.h"
@ -24,6 +25,7 @@
#include "shell/common/options_switches.h"
#include "third_party/skia/include/core/SkRegion.h"
#include "ui/base/hit_test.h"
#include "ui/compositor/compositor.h"
#include "ui/views/widget/widget.h"
#if !BUILDFLAG(IS_MAC)
@ -767,6 +769,37 @@ void NativeWindow::RemoveDraggableRegionProvider(
[&provider](DraggableRegionProvider* p) { return p == provider; });
}
void NativeWindow::AddBackgroundThrottlingSource(
BackgroundThrottlingSource* source) {
auto result = background_throttling_sources_.insert(source);
DCHECK(result.second) << "Added already stored BackgroundThrottlingSource.";
UpdateBackgroundThrottlingState();
}
void NativeWindow::RemoveBackgroundThrottlingSource(
BackgroundThrottlingSource* source) {
auto result = background_throttling_sources_.erase(source);
DCHECK(result == 1)
<< "Tried to remove non existing BackgroundThrottlingSource.";
UpdateBackgroundThrottlingState();
}
void NativeWindow::UpdateBackgroundThrottlingState() {
if (!GetWidget() || !GetWidget()->GetCompositor()) {
return;
}
bool enable_background_throttling = true;
for (const auto* background_throttling_source :
background_throttling_sources_) {
if (!background_throttling_source->GetBackgroundThrottling()) {
enable_background_throttling = false;
break;
}
}
GetWidget()->GetCompositor()->SetBackgroundThrottling(
enable_background_throttling);
}
views::Widget* NativeWindow::GetWidget() {
return widget();
}