LibWeb: Invalidate the display list when calling set_needs_display()

Calls to `Document::set_needs_display()` and
`Paintable::set_needs_display()` now invalidate the display list by
default. This behavior can be changed by passing
`InvalidateDisplayList::No` to the function where invalidating the
display list is not necessary.
This commit is contained in:
Tim Ledbetter 2024-09-02 16:47:32 +01:00 committed by Alexander Kalenik
commit 5800b7e884
Notes: github-actions[bot] 2024-09-02 18:13:03 +00:00
12 changed files with 53 additions and 28 deletions

View file

@ -384,7 +384,6 @@ Document::Document(JS::Realm& realm, const URL::URL& url, TemporaryDocumentForFr
if (!navigable || !navigable->is_focused()) if (!navigable || !navigable->is_focused())
return; return;
node->document().invalidate_display_list();
node->document().update_layout(); node->document().update_layout();
if (node->paintable()) { if (node->paintable()) {
@ -5383,18 +5382,22 @@ void Document::set_cached_navigable(JS::GCPtr<HTML::Navigable> navigable)
m_cached_navigable = navigable.ptr(); m_cached_navigable = navigable.ptr();
} }
void Document::set_needs_display() void Document::set_needs_display(InvalidateDisplayList should_invalidate_display_list)
{ {
set_needs_display(viewport_rect()); set_needs_display(viewport_rect(), should_invalidate_display_list);
} }
void Document::set_needs_display(CSSPixelRect const&) void Document::set_needs_display(CSSPixelRect const&, InvalidateDisplayList should_invalidate_display_list)
{ {
// FIXME: Ignore updates outside the visible viewport rect. // FIXME: Ignore updates outside the visible viewport rect.
// This requires accounting for fixed-position elements in the input rect, which we don't do yet. // This requires accounting for fixed-position elements in the input rect, which we don't do yet.
m_needs_repaint = true; m_needs_repaint = true;
if (should_invalidate_display_list == InvalidateDisplayList::Yes) {
invalidate_display_list();
}
auto navigable = this->navigable(); auto navigable = this->navigable();
if (!navigable) if (!navigable)
return; return;
@ -5404,8 +5407,8 @@ void Document::set_needs_display(CSSPixelRect const&)
return; return;
} }
if (navigable->container()) { if (auto container = navigable->container()) {
navigable->container()->document().set_needs_display(); container->document().set_needs_display(should_invalidate_display_list);
} }
} }
@ -5417,8 +5420,8 @@ void Document::invalidate_display_list()
if (!navigable) if (!navigable)
return; return;
if (navigable->container()) { if (auto container = navigable->container()) {
navigable->container()->document().invalidate_display_list(); container->document().invalidate_display_list();
} }
} }

View file

@ -35,6 +35,7 @@
#include <LibWeb/HTML/SandboxingFlagSet.h> #include <LibWeb/HTML/SandboxingFlagSet.h>
#include <LibWeb/HTML/Scripting/Environments.h> #include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/HTML/VisibilityState.h> #include <LibWeb/HTML/VisibilityState.h>
#include <LibWeb/InvalidateDisplayList.h>
#include <LibWeb/WebIDL/ExceptionOr.h> #include <LibWeb/WebIDL/ExceptionOr.h>
#include <LibWeb/WebIDL/ObservableArray.h> #include <LibWeb/WebIDL/ObservableArray.h>
@ -700,8 +701,8 @@ public:
void set_cached_navigable(JS::GCPtr<HTML::Navigable>); void set_cached_navigable(JS::GCPtr<HTML::Navigable>);
[[nodiscard]] bool needs_repaint() const { return m_needs_repaint; } [[nodiscard]] bool needs_repaint() const { return m_needs_repaint; }
void set_needs_display(); void set_needs_display(InvalidateDisplayList = InvalidateDisplayList::Yes);
void set_needs_display(CSSPixelRect const&); void set_needs_display(CSSPixelRect const&, InvalidateDisplayList = InvalidateDisplayList::Yes);
struct PaintConfig { struct PaintConfig {
bool paint_overlay { false }; bool paint_overlay { false };

View file

@ -98,7 +98,6 @@ void Range::update_associated_selection()
{ {
if (auto* viewport = m_start_container->document().paintable()) { if (auto* viewport = m_start_container->document().paintable()) {
viewport->recompute_selection_states(); viewport->recompute_selection_states();
m_start_container->document().invalidate_display_list();
viewport->set_needs_display(); viewport->set_needs_display();
} }

View file

@ -14,6 +14,7 @@ namespace Web {
class DragAndDropEventHandler; class DragAndDropEventHandler;
class EditEventHandler; class EditEventHandler;
class EventHandler; class EventHandler;
enum class InvalidateDisplayList;
class LoadRequest; class LoadRequest;
class Page; class Page;
class PageClient; class PageClient;

View file

@ -174,7 +174,7 @@ void CanvasRenderingContext2D::did_draw(Gfx::FloatRect const&)
// FIXME: Make use of the rect to reduce the invalidated area when possible. // FIXME: Make use of the rect to reduce the invalidated area when possible.
if (!canvas_element().paintable()) if (!canvas_element().paintable())
return; return;
canvas_element().paintable()->set_needs_display(); canvas_element().paintable()->set_needs_display(InvalidateDisplayList::No);
} }
Gfx::Painter* CanvasRenderingContext2D::painter() Gfx::Painter* CanvasRenderingContext2D::painter()

View file

@ -2012,7 +2012,7 @@ void Navigable::set_viewport_size(CSSPixelSize size)
} }
if (auto document = active_document()) { if (auto document = active_document()) {
document->set_needs_display(); document->set_needs_display(InvalidateDisplayList::No);
document->inform_all_viewport_clients_about_the_current_viewport_rect(); document->inform_all_viewport_clients_about_the_current_viewport_rect();
@ -2028,7 +2028,7 @@ void Navigable::perform_scroll_of_viewport(CSSPixelPoint new_position)
scroll_offset_did_change(); scroll_offset_did_change();
if (auto document = active_document()) { if (auto document = active_document()) {
document->set_needs_display(); document->set_needs_display(InvalidateDisplayList::No);
document->set_needs_to_refresh_scroll_state(true); document->set_needs_to_refresh_scroll_state(true);
document->inform_all_viewport_clients_about_the_current_viewport_rect(); document->inform_all_viewport_clients_about_the_current_viewport_rect();
} }
@ -2038,10 +2038,10 @@ void Navigable::perform_scroll_of_viewport(CSSPixelPoint new_position)
HTML::main_thread_event_loop().schedule(); HTML::main_thread_event_loop().schedule();
} }
void Navigable::set_needs_display() void Navigable::set_needs_display(InvalidateDisplayList should_invalidate_display_list)
{ {
if (auto document = active_document(); document) { if (auto document = active_document(); document) {
document->set_needs_display(); document->set_needs_display(should_invalidate_display_list);
} }
} }

View file

@ -21,6 +21,7 @@
#include <LibWeb/HTML/SourceSnapshotParams.h> #include <LibWeb/HTML/SourceSnapshotParams.h>
#include <LibWeb/HTML/StructuredSerialize.h> #include <LibWeb/HTML/StructuredSerialize.h>
#include <LibWeb/HTML/TokenizedFeatures.h> #include <LibWeb/HTML/TokenizedFeatures.h>
#include <LibWeb/InvalidateDisplayList.h>
#include <LibWeb/Page/EventHandler.h> #include <LibWeb/Page/EventHandler.h>
#include <LibWeb/Painting/DisplayList.h> #include <LibWeb/Painting/DisplayList.h>
#include <LibWeb/PixelUnits.h> #include <LibWeb/PixelUnits.h>
@ -170,7 +171,7 @@ public:
void set_viewport_size(CSSPixelSize); void set_viewport_size(CSSPixelSize);
void perform_scroll_of_viewport(CSSPixelPoint position); void perform_scroll_of_viewport(CSSPixelPoint position);
void set_needs_display(); void set_needs_display(InvalidateDisplayList = InvalidateDisplayList::Yes);
void set_is_popup(TokenizedFeature::Popup is_popup) { m_is_popup = is_popup; } void set_is_popup(TokenizedFeature::Popup is_popup) { m_is_popup = is_popup; }

View file

@ -0,0 +1,16 @@
/*
* Copyright (c) 2024, Tim Ledbetter <tim.ledbetter@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
namespace Web {
enum class InvalidateDisplayList {
Yes,
No,
};
}

View file

@ -128,24 +128,27 @@ void Paintable::invalidate_stacking_context()
m_stacking_context = nullptr; m_stacking_context = nullptr;
} }
void Paintable::set_needs_display() void Paintable::set_needs_display(InvalidateDisplayList should_invalidate_display_list)
{ {
auto& document = const_cast<DOM::Document&>(this->document());
if (should_invalidate_display_list == InvalidateDisplayList::Yes)
document.invalidate_display_list();
auto* containing_block = this->containing_block(); auto* containing_block = this->containing_block();
if (!containing_block) if (!containing_block)
return; return;
auto& document = const_cast<DOM::Document&>(this->document());
if (is<Painting::InlinePaintable>(*this)) { if (is<Painting::InlinePaintable>(*this)) {
auto const& fragments = static_cast<Painting::InlinePaintable const*>(this)->fragments(); auto const& fragments = static_cast<Painting::InlinePaintable const*>(this)->fragments();
for (auto const& fragment : fragments) for (auto const& fragment : fragments) {
document.set_needs_display(fragment.absolute_rect()); document.set_needs_display(fragment.absolute_rect(), InvalidateDisplayList::No);
}
} }
if (!is<Painting::PaintableWithLines>(*containing_block)) if (!is<Painting::PaintableWithLines>(*containing_block))
return; return;
static_cast<Painting::PaintableWithLines const&>(*containing_block).for_each_fragment([&](auto& fragment) { static_cast<Painting::PaintableWithLines const&>(*containing_block).for_each_fragment([&](auto& fragment) {
document.set_needs_display(fragment.absolute_rect()); document.set_needs_display(fragment.absolute_rect(), InvalidateDisplayList::No);
return IterationDecision::Continue; return IterationDecision::Continue;
}); });
} }

View file

@ -7,6 +7,7 @@
#pragma once #pragma once
#include <AK/NonnullOwnPtr.h> #include <AK/NonnullOwnPtr.h>
#include <LibWeb/InvalidateDisplayList.h>
#include <LibWeb/Layout/Box.h> #include <LibWeb/Layout/Box.h>
#include <LibWeb/Layout/LineBox.h> #include <LibWeb/Layout/LineBox.h>
#include <LibWeb/Layout/TextNode.h> #include <LibWeb/Layout/TextNode.h>
@ -199,7 +200,7 @@ public:
JS::GCPtr<HTML::Navigable> navigable() const; JS::GCPtr<HTML::Navigable> navigable() const;
virtual void set_needs_display(); virtual void set_needs_display(InvalidateDisplayList = InvalidateDisplayList::Yes);
PaintableBox* containing_block() const PaintableBox* containing_block() const
{ {

View file

@ -125,7 +125,7 @@ void PaintableBox::set_scroll_offset(CSSPixelPoint offset)
// 4. Append the element to docs pending scroll event targets. // 4. Append the element to docs pending scroll event targets.
document.pending_scroll_event_targets().append(*layout_box().dom_node()); document.pending_scroll_event_targets().append(*layout_box().dom_node());
set_needs_display(); set_needs_display(InvalidateDisplayList::No);
} }
void PaintableBox::scroll_by(int delta_x, int delta_y) void PaintableBox::scroll_by(int delta_x, int delta_y)
@ -915,9 +915,9 @@ TraversalDecision PaintableWithLines::hit_test(CSSPixelPoint position, HitTestTy
return TraversalDecision::Continue; return TraversalDecision::Continue;
} }
void PaintableBox::set_needs_display() void PaintableBox::set_needs_display(InvalidateDisplayList should_invalidate_display_list)
{ {
document().set_needs_display(absolute_rect()); document().set_needs_display(absolute_rect(), should_invalidate_display_list);
} }
Optional<CSSPixelRect> PaintableBox::get_masking_area() const Optional<CSSPixelRect> PaintableBox::get_masking_area() const

View file

@ -128,7 +128,7 @@ public:
DOM::Node const* dom_node() const { return layout_box().dom_node(); } DOM::Node const* dom_node() const { return layout_box().dom_node(); }
DOM::Node* dom_node() { return layout_box().dom_node(); } DOM::Node* dom_node() { return layout_box().dom_node(); }
virtual void set_needs_display() override; virtual void set_needs_display(InvalidateDisplayList = InvalidateDisplayList::Yes) override;
virtual void apply_scroll_offset(PaintContext&, PaintPhase) const override; virtual void apply_scroll_offset(PaintContext&, PaintPhase) const override;
virtual void reset_scroll_offset(PaintContext&, PaintPhase) const override; virtual void reset_scroll_offset(PaintContext&, PaintPhase) const override;