LibWeb: Move set_needs_display() from layout node to paintable

For this method, there is no need to go through the layout node when we
can directly reach the paintable.
This commit is contained in:
Aliaksandr Kalenik 2024-01-14 13:46:52 +01:00 committed by Andreas Kling
parent 814bed33b4
commit 7c2713c14f
Notes: sideshowbarker 2024-07-17 00:53:02 +09:00
21 changed files with 97 additions and 87 deletions

View File

@ -1177,14 +1177,14 @@ void Document::set_inspected_node(Node* node, Optional<CSS::Selector::PseudoElem
if (m_inspected_node.ptr() == node && m_inspected_pseudo_element == pseudo_element)
return;
if (auto layout_node = inspected_layout_node())
layout_node->set_needs_display();
if (auto layout_node = inspected_layout_node(); layout_node && layout_node->paintable())
layout_node->paintable()->set_needs_display();
m_inspected_node = node;
m_inspected_pseudo_element = pseudo_element;
if (auto layout_node = inspected_layout_node())
layout_node->set_needs_display();
if (auto layout_node = inspected_layout_node(); layout_node && layout_node->paintable())
layout_node->paintable()->set_needs_display();
}
Layout::Node* Document::inspected_layout_node()
@ -1736,8 +1736,8 @@ void Document::set_focused_element(Element* element)
m_focused_element->set_needs_style_update(true);
}
if (m_layout_root)
m_layout_root->set_needs_display();
if (paintable())
paintable()->set_needs_display();
// Scroll the viewport if necessary to make the newly focused element visible.
if (m_focused_element) {
@ -1755,8 +1755,8 @@ void Document::set_active_element(Element* element)
m_active_element = element;
if (m_layout_root)
m_layout_root->set_needs_display();
if (paintable())
paintable()->set_needs_display();
}
void Document::set_target_element(Element* element)
@ -1772,8 +1772,8 @@ void Document::set_target_element(Element* element)
if (m_target_element)
m_target_element->set_needs_style_update(true);
if (m_layout_root)
m_layout_root->set_needs_display();
if (paintable())
paintable()->set_needs_display();
}
// https://html.spec.whatwg.org/multipage/browsing-the-web.html#the-indicated-part-of-the-document

View File

@ -602,8 +602,8 @@ Element::RequiredInvalidationAfterStyleChange Element::recompute_style()
if (!invalidation.rebuild_layout_tree && layout_node()) {
// If we're keeping the layout tree, we can just apply the new style to the existing layout tree.
layout_node()->apply_style(*m_computed_css_values);
if (invalidation.repaint)
layout_node()->set_needs_display();
if (invalidation.repaint && paintable())
paintable()->set_needs_display();
}
return invalidation;

View File

@ -22,6 +22,7 @@
#include <LibWeb/HTML/Window.h>
#include <LibWeb/Layout/Viewport.h>
#include <LibWeb/Namespace.h>
#include <LibWeb/Painting/Paintable.h>
namespace Web::DOM {
@ -94,9 +95,9 @@ void Range::update_associated_selection()
{
if (!m_associated_selection)
return;
if (auto* layout_root = m_associated_selection->document()->layout_node()) {
if (auto* layout_root = m_associated_selection->document()->layout_node(); layout_root && layout_root->paintable()) {
layout_root->recompute_selection_states();
layout_root->set_needs_display();
layout_root->paintable()->set_needs_display();
}
}

View File

@ -16,6 +16,7 @@
#include <LibWeb/HTML/EventNames.h>
#include <LibWeb/HTML/HTMLMediaElement.h>
#include <LibWeb/Layout/Node.h>
#include <LibWeb/Painting/Paintable.h>
#include <LibWeb/Platform/AudioCodecPlugin.h>
namespace Web::HTML {
@ -30,8 +31,8 @@ AudioTrack::AudioTrack(JS::Realm& realm, JS::NonnullGCPtr<HTMLMediaElement> medi
, m_audio_plugin(Platform::AudioCodecPlugin::create(move(loader)).release_value_but_fixme_should_propagate_errors())
{
m_audio_plugin->on_playback_position_updated = [this](auto position) {
if (auto* layout_node = m_media_element->layout_node())
layout_node->set_needs_display();
if (auto const* paintable = m_media_element->paintable())
paintable->set_needs_display();
auto playback_position = static_cast<double>(position.to_milliseconds()) / 1000.0;
m_media_element->set_current_playback_position(playback_position);

View File

@ -273,9 +273,9 @@ BrowsingContext::BrowsingContext(JS::NonnullGCPtr<Page> page, HTML::NavigableCon
m_cursor_blink_timer = Core::Timer::create_repeating(500, [this] {
if (!is_focused_context())
return;
if (m_cursor_position && m_cursor_position->node()->layout_node()) {
if (m_cursor_position && m_cursor_position->node()->layout_node() && m_cursor_position->node()->layout_node()->paintable()) {
m_cursor_blink_state = !m_cursor_blink_state;
m_cursor_position->node()->layout_node()->set_needs_display();
m_cursor_position->node()->paintable()->set_needs_display();
}
}).release_value_but_fixme_should_propagate_errors();
}
@ -327,8 +327,8 @@ void BrowsingContext::reset_cursor_blink_cycle()
{
m_cursor_blink_state = true;
m_cursor_blink_timer->restart();
if (m_cursor_position && m_cursor_position->node()->layout_node())
m_cursor_position->node()->layout_node()->set_needs_display();
if (m_cursor_position && m_cursor_position->node()->paintable())
m_cursor_position->node()->paintable()->set_needs_display();
}
// https://html.spec.whatwg.org/multipage/browsers.html#top-level-browsing-context
@ -404,13 +404,13 @@ void BrowsingContext::set_cursor_position(JS::NonnullGCPtr<DOM::Position> positi
if (m_cursor_position && m_cursor_position->equals(position))
return;
if (m_cursor_position && m_cursor_position->node()->layout_node())
m_cursor_position->node()->layout_node()->set_needs_display();
if (m_cursor_position && m_cursor_position->node()->paintable())
m_cursor_position->node()->paintable()->set_needs_display();
m_cursor_position = position;
if (m_cursor_position && m_cursor_position->node()->layout_node())
m_cursor_position->node()->layout_node()->set_needs_display();
if (m_cursor_position && m_cursor_position->node()->paintable())
m_cursor_position->node()->paintable()->set_needs_display();
reset_cursor_blink_cycle();
}

View File

@ -21,6 +21,7 @@
#include <LibWeb/HTML/TextMetrics.h>
#include <LibWeb/Infra/CharacterTypes.h>
#include <LibWeb/Layout/TextNode.h>
#include <LibWeb/Painting/Paintable.h>
#include <LibWeb/Platform/FontPlugin.h>
#include <LibWeb/WebIDL/ExceptionOr.h>
@ -174,9 +175,9 @@ WebIDL::ExceptionOr<void> CanvasRenderingContext2D::draw_image_internal(CanvasIm
void CanvasRenderingContext2D::did_draw(Gfx::FloatRect const&)
{
// FIXME: Make use of the rect to reduce the invalidated area when possible.
if (!canvas_element().layout_node())
if (!canvas_element().paintable())
return;
canvas_element().layout_node()->set_needs_display();
canvas_element().paintable()->set_needs_display();
}
Gfx::Painter* CanvasRenderingContext2D::painter()

View File

@ -12,6 +12,7 @@
#include <LibWeb/HTML/Parser/HTMLParser.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/Layout/Node.h>
#include <LibWeb/Painting/Paintable.h>
namespace Web::HTML {
@ -78,8 +79,8 @@ void HTMLBodyElement::attribute_changed(FlyString const& name, Optional<String>
} else if (name.equals_ignoring_ascii_case("background"sv)) {
m_background_style_value = CSS::ImageStyleValue::create(document().parse_url(value.value_or(String {})));
m_background_style_value->on_animate = [this] {
if (layout_node()) {
layout_node()->set_needs_display();
if (paintable()) {
paintable()->set_needs_display();
}
};
}

View File

@ -1005,8 +1005,8 @@ void HTMLImageElement::animate()
}
}
if (layout_node())
layout_node()->set_needs_display();
if (paintable())
paintable()->set_needs_display();
}
}

View File

@ -35,6 +35,7 @@
#include <LibWeb/HTML/VideoTrackList.h>
#include <LibWeb/Layout/Node.h>
#include <LibWeb/MimeSniff/MimeType.h>
#include <LibWeb/Painting/Paintable.h>
#include <LibWeb/WebIDL/Promise.h>
namespace Web::HTML {
@ -325,8 +326,8 @@ void HTMLMediaElement::set_duration(double duration)
m_duration = duration;
if (auto* layout_node = this->layout_node())
layout_node->set_needs_display();
if (auto* paintable = this->paintable())
paintable->set_needs_display();
}
WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> HTMLMediaElement::play()
@ -424,8 +425,8 @@ void HTMLMediaElement::volume_or_muted_attribute_changed()
// FIXME: Then, if the media element is not allowed to play, the user agent must run the internal pause steps for the media element.
if (auto* layout_node = this->layout_node())
layout_node->set_needs_display();
if (auto* paintable = this->paintable())
paintable->set_needs_display();
on_volume_change();
}
@ -1586,8 +1587,8 @@ void HTMLMediaElement::set_show_poster(bool show_poster)
m_show_poster = show_poster;
if (auto* layout_node = this->layout_node())
layout_node->set_needs_display();
if (auto* paintable = this->paintable())
paintable->set_needs_display();
}
void HTMLMediaElement::set_paused(bool paused)
@ -1600,8 +1601,8 @@ void HTMLMediaElement::set_paused(bool paused)
if (m_paused)
on_paused();
if (auto* layout_node = this->layout_node())
layout_node->set_needs_display();
if (auto* paintable = this->paintable())
paintable->set_needs_display();
set_needs_style_update(true);
}
@ -1928,8 +1929,8 @@ void HTMLMediaElement::set_layout_display_time(Badge<Painting::MediaPaintable>,
m_display_time = move(display_time);
if (auto* layout_node = this->layout_node())
layout_node->set_needs_display();
if (auto* paintable = this->paintable())
paintable->set_needs_display();
}
double HTMLMediaElement::layout_display_time(Badge<Painting::MediaPaintable>) const

View File

@ -16,6 +16,7 @@
#include <LibWeb/HTML/HTMLVideoElement.h>
#include <LibWeb/HTML/VideoTrack.h>
#include <LibWeb/Layout/VideoBox.h>
#include <LibWeb/Painting/Paintable.h>
#include <LibWeb/Platform/ImageCodecPlugin.h>
namespace Web::HTML {
@ -102,8 +103,8 @@ void HTMLVideoElement::set_video_track(JS::GCPtr<HTML::VideoTrack> video_track)
void HTMLVideoElement::set_current_frame(Badge<VideoTrack>, RefPtr<Gfx::Bitmap> frame, double position)
{
m_current_frame = { move(frame), position };
if (layout_node())
layout_node()->set_needs_display();
if (paintable())
paintable()->set_needs_display();
}
void HTMLVideoElement::on_playing()

View File

@ -1990,8 +1990,8 @@ void Navigable::set_needs_display(CSSPixelRect const& rect)
return;
}
if (container() && container()->layout_node())
container()->layout_node()->set_needs_display();
if (container() && container()->paintable())
container()->paintable()->set_needs_display();
}
// https://html.spec.whatwg.org/#rendering-opportunity

View File

@ -60,15 +60,6 @@ bool Box::is_user_scrollable() const
return computed_values().overflow_y() == CSS::Overflow::Scroll || computed_values().overflow_y() == CSS::Overflow::Auto;
}
void Box::set_needs_display()
{
if (!navigable())
return;
if (paintable_box())
navigable()->set_needs_display(paintable_box()->absolute_rect());
}
bool Box::is_body() const
{
return dom_node() && dom_node() == document().body();

View File

@ -25,8 +25,6 @@ public:
Painting::PaintableBox const* paintable_box() const;
Painting::PaintableBox* paintable_box();
virtual void set_needs_display() override;
bool is_body() const;
// https://www.w3.org/TR/css-images-3/#natural-dimensions

View File

@ -226,31 +226,6 @@ Viewport& Node::root()
return *document().layout_node();
}
void Node::set_needs_display()
{
auto* containing_block = this->containing_block();
if (!containing_block)
return;
if (!containing_block->paintable_box())
return;
auto navigable = this->navigable();
if (!navigable)
return;
if (this->paintable() && is<Painting::InlinePaintable>(this->paintable())) {
auto const& fragments = static_cast<Painting::InlinePaintable*>(this->paintable())->fragments();
for (auto const& fragment : fragments)
navigable->set_needs_display(fragment.absolute_rect());
}
if (!is<Painting::PaintableWithLines>(*containing_block->paintable_box()))
return;
static_cast<Painting::PaintableWithLines const&>(*containing_block->paintable_box()).for_each_fragment([&](auto& fragment) {
navigable->set_needs_display(fragment.absolute_rect());
return IterationDecision::Continue;
});
}
bool Node::is_floating() const
{
if (!has_style())

View File

@ -155,8 +155,6 @@ public:
void removed_from(Node&) { }
void children_changed() { }
virtual void set_needs_display();
bool children_are_inline() const { return m_children_are_inline; }
void set_children_are_inline(bool value) { m_children_are_inline = value; }

View File

@ -73,6 +73,11 @@ HTML::BrowsingContext& Paintable::browsing_context()
return m_browsing_context;
}
JS::GCPtr<HTML::Navigable> Paintable::navigable() const
{
return document().navigable();
}
Paintable::DispatchEventOfSameName Paintable::handle_mousedown(Badge<EventHandler>, CSSPixelPoint, unsigned, unsigned)
{
return DispatchEventOfSameName::Yes;
@ -118,6 +123,31 @@ void Paintable::invalidate_stacking_context()
m_stacking_context = nullptr;
}
void Paintable::set_needs_display() const
{
auto* containing_block = this->containing_block();
if (!containing_block)
return;
if (!containing_block->paintable_box())
return;
auto navigable = this->navigable();
if (!navigable)
return;
if (is<Painting::InlinePaintable>(*this)) {
auto const& fragments = static_cast<Painting::InlinePaintable const*>(this)->fragments();
for (auto const& fragment : fragments)
navigable->set_needs_display(fragment.absolute_rect());
}
if (!is<Painting::PaintableWithLines>(*containing_block->paintable_box()))
return;
static_cast<Painting::PaintableWithLines const&>(*containing_block->paintable_box()).for_each_fragment([&](auto& fragment) {
navigable->set_needs_display(fragment.absolute_rect());
return IterationDecision::Continue;
});
}
PaintableBox const* Paintable::nearest_scrollable_ancestor_within_stacking_context() const
{
auto* ancestor = parent();

View File

@ -165,7 +165,9 @@ public:
[[nodiscard]] HTML::BrowsingContext const& browsing_context() const;
[[nodiscard]] HTML::BrowsingContext& browsing_context();
void set_needs_display() const { const_cast<Layout::Node&>(*m_layout_node).set_needs_display(); }
JS::GCPtr<HTML::Navigable> navigable() const;
virtual void set_needs_display() const;
Layout::Box const* containing_block() const
{

View File

@ -80,7 +80,7 @@ void PaintableBox::set_scroll_offset(CSSPixelPoint offset)
return;
}
node.set_needs_display();
set_needs_display();
}
void PaintableBox::scroll_by(int delta_x, int delta_y)
@ -790,4 +790,10 @@ Optional<HitTestResult> PaintableWithLines::hit_test(CSSPixelPoint position, Hit
return {};
}
void PaintableBox::set_needs_display() const
{
if (auto navigable = this->navigable())
navigable->set_needs_display(absolute_rect());
}
}

View File

@ -124,6 +124,8 @@ public:
DOM::Node const* dom_node() const { return layout_box().dom_node(); }
DOM::Node* dom_node() { return layout_box().dom_node(); }
virtual void set_needs_display() const override;
virtual void apply_scroll_offset(PaintContext&, PaintPhase) const override;
virtual void reset_scroll_offset(PaintContext&, PaintPhase) const override;

View File

@ -24,7 +24,8 @@ PaintableFragment::PaintableFragment(Layout::LineBoxFragment const& fragment)
CSSPixelRect const PaintableFragment::absolute_rect() const
{
CSSPixelRect rect { {}, size() };
rect.set_location(m_layout_node->containing_block()->paintable_box()->absolute_position());
if (m_layout_node->containing_block() && m_layout_node->containing_block()->paintable_box())
rect.set_location(m_layout_node->containing_block()->paintable_box()->absolute_position());
rect.translate_by(offset());
return rect;
}

View File

@ -8,6 +8,7 @@
#include <LibGL/GLContext.h>
#include <LibWeb/HTML/HTMLCanvasElement.h>
#include <LibWeb/Layout/Node.h>
#include <LibWeb/Painting/Paintable.h>
#include <LibWeb/WebGL/WebGLRenderingContextBase.h>
namespace Web::WebGL {
@ -99,9 +100,9 @@ void WebGLRenderingContextBase::needs_to_present()
{
m_should_present = true;
if (!canvas_element().layout_node())
if (!canvas_element().paintable())
return;
canvas_element().layout_node()->set_needs_display();
canvas_element().paintable()->set_needs_display();
}
void WebGLRenderingContextBase::set_error(GLenum error)