From 82dba26d7911a786016702843ffe77e810c7ba1f Mon Sep 17 00:00:00 2001 From: Aliaksandr Kalenik Date: Fri, 28 Mar 2025 11:23:54 +0000 Subject: [PATCH] LibWeb: Optimize painting if all background layers have normal blending a73cd88 resulted in a severe performance regression because now we emit for every paintable an expensive SaveLayer display list item, for which Skia internally allocates a new surface. With this change we emit a SaveLayer only if paintable has any layers with non-normal blending. --- .../LibWeb/Painting/BackgroundPainting.cpp | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/Libraries/LibWeb/Painting/BackgroundPainting.cpp b/Libraries/LibWeb/Painting/BackgroundPainting.cpp index fea6e574b6d..32905070e68 100644 --- a/Libraries/LibWeb/Painting/BackgroundPainting.cpp +++ b/Libraries/LibWeb/Painting/BackgroundPainting.cpp @@ -78,10 +78,16 @@ void paint_background(PaintContext& context, PaintableBox const& paintable_box, { auto& display_list_recorder = context.display_list_recorder(); - // https://drafts.fxtf.org/compositing/#background-blend-mode - // Background layers must not blend with the content that is behind the element, - // instead they must act as if they are rendered into an isolated group. - display_list_recorder.save_layer(); + auto has_any_layers_with_non_normal_blending = any_of(resolved_background.layers, [](auto& layer) { + return mix_blend_mode_to_compositing_and_blending_operator(layer.blend_mode) != Gfx::CompositingAndBlendingOperator::Normal; + }); + + if (has_any_layers_with_non_normal_blending) { + // https://drafts.fxtf.org/compositing/#background-blend-mode + // Background layers must not blend with the content that is behind the element, + // instead they must act as if they are rendered into an isolated group. + display_list_recorder.save_layer(); + } DisplayListRecorderStateSaver state { display_list_recorder }; if (resolved_background.needs_text_clip) { @@ -306,7 +312,9 @@ void paint_background(PaintContext& context, PaintableBox const& paintable_box, } } - display_list_recorder.restore(); + if (has_any_layers_with_non_normal_blending) { + display_list_recorder.restore(); + } } ResolvedBackground resolve_background_layers(Vector const& layers, PaintableBox const& paintable_box, Color background_color, CSSPixelRect const& border_rect, BorderRadiiData const& border_radii)