summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb
diff options
context:
space:
mode:
authorMacDue <macdue@dueutil.tech>2022-06-15 21:13:44 +0100
committerLinus Groh <mail@linusgroh.de>2022-06-16 10:28:07 +0100
commit4dfbbd59650dce4f9eafa7c664dda718a404cc2e (patch)
tree240a248091c2314e3003c29efa08881595ba137e /Userland/Libraries/LibWeb
parent81a3ec0692ebab6eb378e6eedae84d71d0d6c29b (diff)
downloadserenity-4dfbbd59650dce4f9eafa7c664dda718a404cc2e.zip
LibWeb: Move border corner bitmap getter/cache to function
This will allow the same bitmap to be shared between border, background, and various other corner clipping code.
Diffstat (limited to 'Userland/Libraries/LibWeb')
-rw-r--r--Userland/Libraries/LibWeb/Painting/BorderPainting.cpp51
-rw-r--r--Userland/Libraries/LibWeb/Painting/BorderPainting.h3
2 files changed, 32 insertions, 22 deletions
diff --git a/Userland/Libraries/LibWeb/Painting/BorderPainting.cpp b/Userland/Libraries/LibWeb/Painting/BorderPainting.cpp
index 491703b4c0..ea569a4dbf 100644
--- a/Userland/Libraries/LibWeb/Painting/BorderPainting.cpp
+++ b/Userland/Libraries/LibWeb/Painting/BorderPainting.cpp
@@ -204,6 +204,31 @@ void paint_border(PaintContext& context, BorderEdge edge, Gfx::IntRect const& re
}
}
+RefPtr<Gfx::Bitmap> get_cached_corner_bitmap(Gfx::IntSize const& corners_size)
+{
+ auto allocate_mask_bitmap = [&]() -> RefPtr<Gfx::Bitmap> {
+ auto bitmap = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, corners_size);
+ if (!bitmap.is_error())
+ return bitmap.release_value();
+ return nullptr;
+ };
+ // FIXME: Allocate per page?
+ static thread_local auto corner_bitmap = allocate_mask_bitmap();
+ // Only reallocate the corner bitmap is the existing one is too small.
+ // (should mean no more allocations after the first paint -- amortised zero allocations :^))
+ if (corner_bitmap && corner_bitmap->rect().size().contains(corners_size)) {
+ Gfx::Painter painter { *corner_bitmap };
+ painter.clear_rect({ { 0, 0 }, corners_size }, Gfx::Color());
+ } else {
+ corner_bitmap = allocate_mask_bitmap();
+ if (!corner_bitmap) {
+ dbgln("Failed to allocate corner bitmap with size {}", corners_size);
+ return nullptr;
+ }
+ }
+ return corner_bitmap;
+}
+
void paint_all_borders(PaintContext& context, Gfx::FloatRect const& bordered_rect, BorderRadiiData const& border_radii_data, BordersData const& borders_data)
{
if (borders_data.top.width <= 0 && borders_data.right.width <= 0 && borders_data.left.width <= 0 && borders_data.bottom.width <= 0)
@@ -289,29 +314,11 @@ void paint_all_borders(PaintContext& context, Gfx::FloatRect const& bordered_rec
top_left.vertical_radius + bottom_left.vertical_radius + expand_width,
top_right.vertical_radius + bottom_right.vertical_radius + expand_height)
};
- auto allocate_mask_bitmap = [&]() -> RefPtr<Gfx::Bitmap> {
- auto bitmap = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, corner_mask_rect.size());
- if (!bitmap.is_error())
- return bitmap.release_value();
- return nullptr;
- };
- static thread_local auto corner_bitmap = allocate_mask_bitmap();
- // Only reallocate the corner bitmap is the existing one is too small.
- // (should mean no more allocations after the first paint -- amortised zero allocations :^))
- Gfx::Painter painter = ({
- Optional<Gfx::Painter> painter;
- if (corner_bitmap && corner_bitmap->rect().contains(corner_mask_rect)) {
- painter = Gfx::Painter { *corner_bitmap };
- painter->clear_rect(corner_mask_rect, Gfx::Color());
- } else {
- corner_bitmap = allocate_mask_bitmap();
- if (!corner_bitmap)
- return dbgln("Failed to allocate border corner bitmap with size {}", corner_mask_rect.size());
- painter = Gfx::Painter { *corner_bitmap };
- }
- *painter;
- });
+ auto corner_bitmap = get_cached_corner_bitmap(corner_mask_rect.size());
+ if (!corner_bitmap)
+ return;
+ Gfx::Painter painter { *corner_bitmap };
Gfx::AntiAliasingPainter aa_painter { painter };
diff --git a/Userland/Libraries/LibWeb/Painting/BorderPainting.h b/Userland/Libraries/LibWeb/Painting/BorderPainting.h
index 08a8692520..0505870f0e 100644
--- a/Userland/Libraries/LibWeb/Painting/BorderPainting.h
+++ b/Userland/Libraries/LibWeb/Painting/BorderPainting.h
@@ -56,6 +56,9 @@ struct BordersData {
CSS::BorderData bottom;
CSS::BorderData left;
};
+
+RefPtr<Gfx::Bitmap> get_cached_corner_bitmap(Gfx::IntSize const& corners_size);
+
void paint_border(PaintContext& context, BorderEdge edge, Gfx::IntRect const& rect, BorderRadiiData const& border_radii_data, BordersData const& borders_data);
void paint_all_borders(PaintContext& context, Gfx::FloatRect const& bordered_rect, BorderRadiiData const& border_radii_data, BordersData const&);