diff --git a/Userland/Libraries/LibWeb/Painting/ShadowPainting.cpp b/Userland/Libraries/LibWeb/Painting/ShadowPainting.cpp index 76de6318082..4b1818d3521 100644 --- a/Userland/Libraries/LibWeb/Painting/ShadowPainting.cpp +++ b/Userland/Libraries/LibWeb/Painting/ShadowPainting.cpp @@ -22,57 +22,6 @@ namespace Web::Painting { -void paint_inner_box_shadow(Gfx::Painter& painter, PaintBoxShadowParams params) -{ - auto device_content_rect = params.device_content_rect; - - int offset_x = params.offset_x; - int offset_y = params.offset_y; - int blur_radius = params.blur_radius; - int spread_distance = params.spread_distance; - auto shadows_bitmap_rect = device_content_rect.inflated( - blur_radius + offset_y, - blur_radius + abs(offset_x), - blur_radius + abs(offset_y), - blur_radius + offset_x); - auto shadows_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, shadows_bitmap_rect.size()); - if (shadows_bitmap.is_error()) { - dbgln("Unable to allocate temporary bitmap {} for box-shadow rendering: {}", device_content_rect, shadows_bitmap.error()); - return; - } - auto shadow_bitmap = shadows_bitmap.release_value(); - Gfx::Painter shadow_painter { *shadow_bitmap }; - Gfx::AntiAliasingPainter shadow_aa_painter { shadow_painter }; - auto device_content_rect_int = device_content_rect; - auto origin_device_content_rect = device_content_rect_int.translated(-device_content_rect_int.x(), -device_content_rect_int.y()); - auto outer_shadow_rect = origin_device_content_rect.translated({ offset_x + blur_radius, offset_y + blur_radius }); - auto spread_distance_value = spread_distance; - auto inner_shadow_rect = outer_shadow_rect.inflated(-spread_distance_value, -spread_distance_value, -spread_distance_value, -spread_distance_value); - outer_shadow_rect.inflate( - blur_radius + offset_y, - blur_radius + abs(offset_x), - blur_radius + abs(offset_y), - blur_radius + offset_x); - auto top_left_corner = params.corner_radii.top_left; - auto top_right_corner = params.corner_radii.top_right; - auto bottom_right_corner = params.corner_radii.bottom_right; - auto bottom_left_corner = params.corner_radii.bottom_left; - shadow_painter.fill_rect(outer_shadow_rect, params.color.with_alpha(0xff)); - if (params.corner_radii.has_any_radius()) { - shadow_aa_painter.fill_rect_with_rounded_corners(inner_shadow_rect, params.color.with_alpha(0xff), - top_left_corner, top_right_corner, bottom_right_corner, bottom_left_corner, - Gfx::AntiAliasingPainter::BlendMode::AlphaSubtract); - } else { - shadow_painter.clear_rect(inner_shadow_rect, Color::Transparent); - } - Gfx::StackBlurFilter filter(*shadow_bitmap); - filter.process_rgba(blur_radius, params.color); - Gfx::PainterStateSaver save { painter }; - painter.add_clip_rect(device_content_rect_int); - painter.blit({ device_content_rect_int.left() - blur_radius, device_content_rect_int.top() - blur_radius }, - *shadow_bitmap, shadow_bitmap->rect(), params.color.alpha() / 255.); -} - struct OuterBoxShadowMetrics { Gfx::IntRect shadow_bitmap_rect; Gfx::IntRect non_blurred_shadow_rect; @@ -305,230 +254,6 @@ Gfx::IntRect get_outer_box_shadow_bounding_rect(PaintBoxShadowParams params) }; } -void paint_outer_box_shadow(Gfx::Painter& painter, PaintBoxShadowParams params) -{ - auto const& device_content_rect = params.device_content_rect; - - auto const& top_left_corner = params.corner_radii.top_left; - auto const& top_right_corner = params.corner_radii.top_right; - auto const& bottom_right_corner = params.corner_radii.bottom_right; - auto const& bottom_left_corner = params.corner_radii.bottom_left; - - auto offset_x = params.offset_x; - auto offset_y = params.offset_y; - - auto shadow_config = get_outer_box_shadow_configuration(params); - - auto const& shadow_bitmap_rect = shadow_config.shadow_bitmap_rect; - auto const& non_blurred_shadow_rect = shadow_config.non_blurred_shadow_rect; - auto const& inner_bounding_rect = shadow_config.inner_bounding_rect; - auto const& blurred_edge_thickness = shadow_config.blurred_edge_thickness; - auto const& double_radius = shadow_config.double_radius; - auto const& blur_radius = shadow_config.blur_radius; - - auto const& top_left_corner_rect = shadow_config.top_left_corner_rect; - auto const& top_right_corner_rect = shadow_config.top_right_corner_rect; - auto const& bottom_right_corner_rect = shadow_config.bottom_right_corner_rect; - auto const& bottom_left_corner_rect = shadow_config.bottom_left_corner_rect; - - auto const& top_left_corner_blit_pos = shadow_config.top_left_corner_blit_pos; - auto const& top_right_corner_blit_pos = shadow_config.top_right_corner_blit_pos; - auto const& bottom_right_corner_blit_pos = shadow_config.bottom_right_corner_blit_pos; - auto const& bottom_left_corner_blit_pos = shadow_config.bottom_left_corner_blit_pos; - - auto const& top_left_corner_size = shadow_config.top_left_corner_size; - auto const& top_right_corner_size = shadow_config.top_right_corner_size; - auto const& bottom_right_corner_size = shadow_config.bottom_right_corner_size; - auto const& bottom_left_corner_size = shadow_config.bottom_left_corner_size; - - auto const& left_start = shadow_config.left_start; - auto const& top_start = shadow_config.top_start; - auto const& right_start = shadow_config.right_start; - auto const& bottom_start = shadow_config.bottom_start; - - auto const& left_edge_rect = shadow_config.left_edge_rect; - auto const& right_edge_rect = shadow_config.right_edge_rect; - auto const& top_edge_rect = shadow_config.top_edge_rect; - auto const& bottom_edge_rect = shadow_config.bottom_edge_rect; - - auto const& top_left_shadow_corner = shadow_config.top_left_shadow_corner; - auto const& top_right_shadow_corner = shadow_config.top_right_shadow_corner; - auto const& bottom_right_shadow_corner = shadow_config.bottom_right_shadow_corner; - auto const& bottom_left_shadow_corner = shadow_config.bottom_left_shadow_corner; - - auto fill_rect_masked = [](auto& painter, auto fill_rect, auto mask_rect, auto color) { - Gfx::DisjointRectSet rect_set; - rect_set.add(fill_rect); - auto shattered = rect_set.shatter(mask_rect); - for (auto& rect : shattered.rects()) - painter.fill_rect(rect, color); - }; - - // If there's no blurring, nor rounded corners, we can save a lot of effort. - if (blur_radius == 0 && !params.corner_radii.has_any_radius()) { - fill_rect_masked(painter, non_blurred_shadow_rect.translated(offset_x, offset_y), device_content_rect, params.color); - return; - } - - auto paint_shadow_infill = [&] { - if (!params.corner_radii.has_any_radius()) - return painter.fill_rect(inner_bounding_rect, params.color); - - auto top_left_inner_width = top_left_corner_rect.width() - blurred_edge_thickness; - auto top_left_inner_height = top_left_corner_rect.height() - blurred_edge_thickness; - auto top_right_inner_width = top_right_corner_rect.width() - blurred_edge_thickness; - auto top_right_inner_height = top_right_corner_rect.height() - blurred_edge_thickness; - auto bottom_right_inner_width = bottom_right_corner_rect.width() - blurred_edge_thickness; - auto bottom_right_inner_height = bottom_right_corner_rect.height() - blurred_edge_thickness; - auto bottom_left_inner_width = bottom_left_corner_rect.width() - blurred_edge_thickness; - auto bottom_left_inner_height = bottom_left_corner_rect.height() - blurred_edge_thickness; - - Gfx::IntRect top_rect { - inner_bounding_rect.x() + top_left_inner_width, - inner_bounding_rect.y(), - inner_bounding_rect.width() - top_left_inner_width - top_right_inner_width, - top_left_inner_height - }; - Gfx::IntRect right_rect { - inner_bounding_rect.x() + inner_bounding_rect.width() - top_right_inner_width, - inner_bounding_rect.y() + top_right_inner_height, - top_right_inner_width, - inner_bounding_rect.height() - top_right_inner_height - bottom_right_inner_height - }; - Gfx::IntRect bottom_rect { - inner_bounding_rect.x() + bottom_left_inner_width, - inner_bounding_rect.y() + inner_bounding_rect.height() - bottom_right_inner_height, - inner_bounding_rect.width() - bottom_left_inner_width - bottom_right_inner_width, - bottom_right_inner_height - }; - Gfx::IntRect left_rect { - inner_bounding_rect.x(), - inner_bounding_rect.y() + top_left_inner_height, - bottom_left_inner_width, - inner_bounding_rect.height() - top_left_inner_height - bottom_left_inner_height - }; - Gfx::IntRect inner = { - left_rect.x() + left_rect.width(), - left_rect.y(), - inner_bounding_rect.width() - left_rect.width() - right_rect.width(), - inner_bounding_rect.height() - top_rect.height() - bottom_rect.height() - }; - - painter.fill_rect(top_rect, params.color); - painter.fill_rect(right_rect, params.color); - painter.fill_rect(bottom_rect, params.color); - painter.fill_rect(left_rect, params.color); - painter.fill_rect(inner, params.color); - }; - - auto shadows_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, shadow_bitmap_rect.size()); - if (shadows_bitmap.is_error()) { - dbgln("Unable to allocate temporary bitmap {} for box-shadow rendering: {}", shadow_bitmap_rect, shadows_bitmap.error()); - return; - } - auto shadow_bitmap = shadows_bitmap.release_value(); - Gfx::Painter corner_painter { *shadow_bitmap }; - Gfx::AntiAliasingPainter aa_corner_painter { corner_painter }; - - aa_corner_painter.fill_rect_with_rounded_corners( - shadow_bitmap_rect.shrunken(double_radius, double_radius, double_radius, double_radius), - params.color, top_left_shadow_corner, top_right_shadow_corner, bottom_right_shadow_corner, bottom_left_shadow_corner); - Gfx::StackBlurFilter filter(*shadow_bitmap); - filter.process_rgba(blur_radius, params.color); - - auto paint_shadow = [&](Gfx::IntRect clip_rect) { - Gfx::PainterStateSaver save { painter }; - painter.add_clip_rect(clip_rect); - - paint_shadow_infill(); - - // Corners - painter.blit(top_left_corner_blit_pos, shadow_bitmap, top_left_corner_rect); - painter.blit(top_right_corner_blit_pos, shadow_bitmap, top_right_corner_rect); - painter.blit(bottom_left_corner_blit_pos, shadow_bitmap, bottom_left_corner_rect); - painter.blit(bottom_right_corner_blit_pos, shadow_bitmap, bottom_right_corner_rect); - - // Horizontal edges - for (auto x = inner_bounding_rect.left() + (bottom_left_corner_size.width() - double_radius); x < inner_bounding_rect.right() - (bottom_right_corner_size.width() - double_radius); ++x) - painter.blit({ x, bottom_start }, shadow_bitmap, bottom_edge_rect); - for (auto x = inner_bounding_rect.left() + (top_left_corner_size.width() - double_radius); x < inner_bounding_rect.right() - (top_right_corner_size.width() - double_radius); ++x) - painter.blit({ x, top_start }, shadow_bitmap, top_edge_rect); - - // Vertical edges - for (auto y = inner_bounding_rect.top() + (top_right_corner_size.height() - double_radius); y < inner_bounding_rect.bottom() - (bottom_right_corner_size.height() - double_radius); ++y) - painter.blit({ right_start, y }, shadow_bitmap, right_edge_rect); - for (auto y = inner_bounding_rect.top() + (top_left_corner_size.height() - double_radius); y < inner_bounding_rect.bottom() - (bottom_left_corner_size.height() - double_radius); ++y) - painter.blit({ left_start, y }, shadow_bitmap, left_edge_rect); - }; - - // FIXME: Painter only lets us define a clip-rect which discards drawing outside of it, whereas here we want - // a rect which discards drawing inside it. So, we run the draw operations 4 to 8 times with clip-rects - // covering each side of the content_rect exactly once. - - // If we were painting a shadow without a border radius we'd want to clip everything inside the box below. - // If painting a shadow with rounded corners (but still rectangular) we want to clip everything inside - // the box except the corners. This gives us an upper bound of 8 shadow paints now :^(. - // (However, this does not seem to be the costly part in profiling). - // - // ┌───┬────────┬───┐ - // │ │xxxxxxxx│ │ - // ├───┼────────┼───┤ - // │xxx│xxxxxxxx│xxx│ - // │xxx│xxxxxxxx│xxx│ - // │xxx│xxxxxxxx│xxx│ - // │xxx│xxxxxxxx│xxx│ - // │xxx│xxxxxxxx│xxx│ - // ├───┼────────┼───┤ - // │ │ xxxxxx │ │ - // └───┴────────┴───┘ - - // How many times would you like to paint the shadow sir? - // Yes. - - // FIXME: Could reduce the shadow paints from 8 to 4 for shadows with all corner radii 50%. - - // FIXME: We use this since we want the clip rect to include everything after a certain x or y. - // Note: Using painter.target().width() or height() does not work, when the painter is a small - // translated bitmap rather than full screen, as the clip rect may not intersect. - constexpr auto really_large_number = NumericLimits::max() / 2; - - // Everything above content_rect, including sides - paint_shadow({ 0, 0, really_large_number, device_content_rect.top() }); - - // Everything below content_rect, including sides - paint_shadow({ 0, device_content_rect.bottom(), really_large_number, really_large_number }); - - // Everything directly to the left of content_rect - paint_shadow({ 0, device_content_rect.top(), device_content_rect.left(), device_content_rect.height() }); - - // Everything directly to the right of content_rect - paint_shadow({ device_content_rect.right(), device_content_rect.top(), really_large_number, device_content_rect.height() }); - - if (top_left_corner) { - // Inside the top left corner (the part outside the border radius) - auto top_left = top_left_corner.as_rect().translated(device_content_rect.top_left()); - paint_shadow(top_left); - } - - if (top_right_corner) { - // Inside the top right corner (the part outside the border radius) - auto top_right = top_right_corner.as_rect().translated(device_content_rect.top_right().translated(-top_right_corner.horizontal_radius, 0)); - paint_shadow(top_right); - } - - if (bottom_right_corner) { - // Inside the bottom right corner (the part outside the border radius) - auto bottom_right = bottom_right_corner.as_rect().translated(device_content_rect.bottom_right().translated(-bottom_right_corner.horizontal_radius, -bottom_right_corner.vertical_radius)); - paint_shadow(bottom_right); - } - - if (bottom_left_corner) { - // Inside the bottom left corner (the part outside the border radius) - auto bottom_left = bottom_left_corner.as_rect().translated(device_content_rect.bottom_left().translated(0, -bottom_left_corner.vertical_radius)); - paint_shadow(bottom_left); - } -} - void paint_box_shadow(PaintContext& context, CSSPixelRect const& bordered_content_rect, CSSPixelRect const& borderless_content_rect, diff --git a/Userland/Libraries/LibWeb/Painting/ShadowPainting.h b/Userland/Libraries/LibWeb/Painting/ShadowPainting.h index 61f1a331dc1..a8ba4680cce 100644 --- a/Userland/Libraries/LibWeb/Painting/ShadowPainting.h +++ b/Userland/Libraries/LibWeb/Painting/ShadowPainting.h @@ -15,10 +15,7 @@ namespace Web::Painting { -void paint_inner_box_shadow(Gfx::Painter&, PaintBoxShadowParams params); - Gfx::IntRect get_outer_box_shadow_bounding_rect(PaintBoxShadowParams params); -void paint_outer_box_shadow(Gfx::Painter& painter, PaintBoxShadowParams params); void paint_box_shadow( PaintContext&,