From 962298b04005f59e34d93b6db088e98efc8a3ba1 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 3 Oct 2021 17:05:58 +0200 Subject: [PATCH] LibWeb: Don't update rendering in BrowsingContexts without opportunity This patch adds the "has a rendering opportunity" concept from the spec to BrowsingContext and uses it to filter out contexts that are unable to render right now when doing the event loop's rendering updates. Note that we actually consider all contexts to have a rendering opportunity at all times right now. Coming up with reasons to avoid rendering is left as a FIXME. :^) --- Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp | 6 +++++- Userland/Libraries/LibWeb/Page/BrowsingContext.cpp | 10 ++++++++++ Userland/Libraries/LibWeb/Page/BrowsingContext.h | 2 ++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp b/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp index 7d5110fd98..41a52e5a53 100644 --- a/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp +++ b/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp @@ -12,6 +12,7 @@ #include #include #include +#include namespace Web::HTML { @@ -139,7 +140,10 @@ void EventLoop::process() } }; - // FIXME: 2. Rendering opportunities: Remove from docs all Document objects whose browsing context do not have a rendering opportunity. + // 2. Rendering opportunities: Remove from docs all Document objects whose browsing context do not have a rendering opportunity. + docs.remove_all_matching([&](auto& document) { + return document->browsing_context() && !document->browsing_context()->has_a_rendering_opportunity(); + }); // 3. If docs is not empty, then set hasARenderingOpportunity to true. if (!docs.is_empty()) diff --git a/Userland/Libraries/LibWeb/Page/BrowsingContext.cpp b/Userland/Libraries/LibWeb/Page/BrowsingContext.cpp index 219d9c04ed..653f2cc1fd 100644 --- a/Userland/Libraries/LibWeb/Page/BrowsingContext.cpp +++ b/Userland/Libraries/LibWeb/Page/BrowsingContext.cpp @@ -348,4 +348,14 @@ DOM::Document const* BrowsingContext::container_document() const return nullptr; } +// https://html.spec.whatwg.org/#rendering-opportunity +bool BrowsingContext::has_a_rendering_opportunity() const +{ + // A browsing context has a rendering opportunity if the user agent is currently able to present the contents of the browsing context to the user, + // accounting for hardware refresh rate constraints and user agent throttling for performance reasons, but considering content presentable even if it's outside the viewport. + + // FIXME: We should at the very least say `false` here if we're an inactive browser tab. + return true; +} + } diff --git a/Userland/Libraries/LibWeb/Page/BrowsingContext.h b/Userland/Libraries/LibWeb/Page/BrowsingContext.h index f0bb307ece..5243de125c 100644 --- a/Userland/Libraries/LibWeb/Page/BrowsingContext.h +++ b/Userland/Libraries/LibWeb/Page/BrowsingContext.h @@ -102,6 +102,8 @@ public: DOM::Document* container_document(); DOM::Document const* container_document() const; + bool has_a_rendering_opportunity() const; + private: explicit BrowsingContext(Page&, HTML::BrowsingContextContainer*);