1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 08:37:35 +00:00

LibGfx+LibWeb: Add ImmutableBitmap for images bitmap caching in painter

Before this change, we used Gfx::Bitmap to represent both decoded
images that are not going to be mutated and bitmaps corresponding
to canvases that could be mutated.

This change introduces a wrapper for bitmaps that are not going to be
mutated, so the painter could do caching: texture caching in the case
of GPU painter and potentially scaled bitmap caching in the case of CPU
painter.
This commit is contained in:
Aliaksandr Kalenik 2023-11-24 14:45:45 +01:00 committed by Andreas Kling
parent abcf71a8ca
commit f4a5c136c3
24 changed files with 146 additions and 35 deletions

View file

@ -175,7 +175,7 @@ void ImagePaintable::paint(PaintContext& context, PaintPhase phase) const
(int)scaled_bitmap_height
};
context.painter().draw_scaled_bitmap(draw_rect.intersected(image_int_rect), *bitmap, bitmap_rect.intersected(bitmap_intersect), scaling_mode);
context.painter().draw_scaled_immutable_bitmap(draw_rect.intersected(image_int_rect), *bitmap, bitmap_rect.intersected(bitmap_intersect), scaling_mode);
}
}
}

View file

@ -64,6 +64,13 @@ CommandResult PaintingCommandExecutorCPU::draw_scaled_bitmap(Gfx::IntRect const&
return CommandResult::Continue;
}
CommandResult PaintingCommandExecutorCPU::draw_scaled_immutable_bitmap(Gfx::IntRect const& dst_rect, Gfx::ImmutableBitmap const& immutable_bitmap, Gfx::IntRect const& src_rect, Gfx::Painter::ScalingMode scaling_mode)
{
auto& painter = this->painter();
painter.draw_scaled_bitmap(dst_rect, immutable_bitmap.bitmap(), src_rect, 1, scaling_mode);
return CommandResult::Continue;
}
CommandResult PaintingCommandExecutorCPU::set_clip_rect(Gfx::IntRect const& rect)
{
auto& painter = this->painter();

View file

@ -17,6 +17,7 @@ public:
CommandResult draw_text(Gfx::IntRect const& rect, String const& raw_text, Gfx::TextAlignment alignment, Color const&, Gfx::TextElision, Gfx::TextWrapping, Optional<NonnullRefPtr<Gfx::Font>> const&) override;
CommandResult fill_rect(Gfx::IntRect const& rect, Color const&) override;
CommandResult draw_scaled_bitmap(Gfx::IntRect const& dst_rect, Gfx::Bitmap const& bitmap, Gfx::IntRect const& src_rect, Gfx::Painter::ScalingMode scaling_mode) override;
CommandResult draw_scaled_immutable_bitmap(Gfx::IntRect const& dst_rect, Gfx::ImmutableBitmap const&, Gfx::IntRect const& src_rect, Gfx::Painter::ScalingMode scaling_mode) override;
CommandResult set_clip_rect(Gfx::IntRect const& rect) override;
CommandResult clear_clip_rect() override;
CommandResult set_font(Gfx::Font const&) override;

View file

@ -44,25 +44,30 @@ CommandResult PaintingCommandExecutorGPU::fill_rect(Gfx::IntRect const& rect, Co
return CommandResult::Continue;
}
CommandResult PaintingCommandExecutorGPU::draw_scaled_bitmap(Gfx::IntRect const& dst_rect, Gfx::Bitmap const& bitmap, Gfx::IntRect const& src_rect, Gfx::Painter::ScalingMode scaling_mode)
static AccelGfx::Painter::ScalingMode to_accelgfx_scaling_mode(Gfx::Painter::ScalingMode scaling_mode)
{
// FIXME: We should avoid using Gfx::Painter specific enums in painting commands
AccelGfx::Painter::ScalingMode accel_scaling_mode;
switch (scaling_mode) {
case Gfx::Painter::ScalingMode::NearestNeighbor:
case Gfx::Painter::ScalingMode::BoxSampling:
case Gfx::Painter::ScalingMode::SmoothPixels:
case Gfx::Painter::ScalingMode::None:
accel_scaling_mode = AccelGfx::Painter::ScalingMode::NearestNeighbor;
break;
return AccelGfx::Painter::ScalingMode::NearestNeighbor;
case Gfx::Painter::ScalingMode::BilinearBlend:
accel_scaling_mode = AccelGfx::Painter::ScalingMode::Bilinear;
break;
return AccelGfx::Painter::ScalingMode::Bilinear;
default:
VERIFY_NOT_REACHED();
}
}
painter().draw_scaled_bitmap(dst_rect, bitmap, src_rect, accel_scaling_mode);
CommandResult PaintingCommandExecutorGPU::draw_scaled_bitmap(Gfx::IntRect const& dst_rect, Gfx::Bitmap const& bitmap, Gfx::IntRect const& src_rect, Gfx::Painter::ScalingMode scaling_mode)
{
painter().draw_scaled_bitmap(dst_rect, bitmap, src_rect, to_accelgfx_scaling_mode(scaling_mode));
return CommandResult::Continue;
}
CommandResult PaintingCommandExecutorGPU::draw_scaled_immutable_bitmap(Gfx::IntRect const& dst_rect, Gfx::ImmutableBitmap const& immutable_bitmap, Gfx::IntRect const& src_rect, Gfx::Painter::ScalingMode scaling_mode)
{
painter().draw_scaled_bitmap(dst_rect, immutable_bitmap.bitmap(), src_rect, to_accelgfx_scaling_mode(scaling_mode));
return CommandResult::Continue;
}

View file

@ -17,6 +17,7 @@ public:
CommandResult draw_text(Gfx::IntRect const& rect, String const& raw_text, Gfx::TextAlignment alignment, Color const&, Gfx::TextElision, Gfx::TextWrapping, Optional<NonnullRefPtr<Gfx::Font>> const&) override;
CommandResult fill_rect(Gfx::IntRect const& rect, Color const&) override;
CommandResult draw_scaled_bitmap(Gfx::IntRect const& dst_rect, Gfx::Bitmap const& bitmap, Gfx::IntRect const& src_rect, Gfx::Painter::ScalingMode scaling_mode) override;
CommandResult draw_scaled_immutable_bitmap(Gfx::IntRect const& dst_rect, Gfx::ImmutableBitmap const&, Gfx::IntRect const& src_rect, Gfx::Painter::ScalingMode scaling_mode) override;
CommandResult set_clip_rect(Gfx::IntRect const& rect) override;
CommandResult clear_clip_rect() override;
CommandResult set_font(Gfx::Font const&) override;

View file

@ -157,6 +157,16 @@ void RecordingPainter::draw_scaled_bitmap(Gfx::IntRect const& dst_rect, Gfx::Bit
});
}
void RecordingPainter::draw_scaled_immutable_bitmap(Gfx::IntRect const& dst_rect, Gfx::ImmutableBitmap const& bitmap, Gfx::IntRect const& src_rect, Gfx::Painter::ScalingMode scaling_mode)
{
push_command(DrawScaledImmutableBitmap {
.dst_rect = state().translation.map(dst_rect),
.bitmap = bitmap,
.src_rect = src_rect,
.scaling_mode = scaling_mode,
});
}
void RecordingPainter::draw_line(Gfx::IntPoint from, Gfx::IntPoint to, Color color, int thickness, Gfx::Painter::LineStyle style, Color alternate_color)
{
push_command(DrawLine {
@ -436,6 +446,9 @@ void RecordingPainter::execute(PaintingCommandExecutor& executor)
[&](DrawScaledBitmap const& command) {
return executor.draw_scaled_bitmap(command.dst_rect, command.bitmap, command.src_rect, command.scaling_mode);
},
[&](DrawScaledImmutableBitmap const& command) {
return executor.draw_scaled_immutable_bitmap(command.dst_rect, command.bitmap, command.src_rect, command.scaling_mode);
},
[&](SetClipRect const& command) {
return executor.set_clip_rect(command.rect);
},

View file

@ -15,6 +15,7 @@
#include <LibGfx/Forward.h>
#include <LibGfx/Gradients.h>
#include <LibGfx/GrayscaleBitmap.h>
#include <LibGfx/ImmutableBitmap.h>
#include <LibGfx/PaintStyle.h>
#include <LibGfx/Painter.h>
#include <LibGfx/Palette.h>
@ -76,6 +77,15 @@ struct DrawScaledBitmap {
[[nodiscard]] Gfx::IntRect bounding_rect() const { return dst_rect; }
};
struct DrawScaledImmutableBitmap {
Gfx::IntRect dst_rect;
NonnullRefPtr<Gfx::ImmutableBitmap> bitmap;
Gfx::IntRect src_rect;
Gfx::Painter::ScalingMode scaling_mode;
[[nodiscard]] Gfx::IntRect bounding_rect() const { return dst_rect; }
};
struct SetClipRect {
Gfx::IntRect rect;
};
@ -314,6 +324,7 @@ using PaintingCommand = Variant<
DrawText,
FillRect,
DrawScaledBitmap,
DrawScaledImmutableBitmap,
SetClipRect,
ClearClipRect,
SetFont,
@ -351,6 +362,7 @@ public:
virtual CommandResult draw_text(Gfx::IntRect const&, String const&, Gfx::TextAlignment alignment, Color const&, Gfx::TextElision, Gfx::TextWrapping, Optional<NonnullRefPtr<Gfx::Font>> const&) = 0;
virtual CommandResult fill_rect(Gfx::IntRect const&, Color const&) = 0;
virtual CommandResult draw_scaled_bitmap(Gfx::IntRect const& dst_rect, Gfx::Bitmap const& bitmap, Gfx::IntRect const& src_rect, Gfx::Painter::ScalingMode scaling_mode) = 0;
virtual CommandResult draw_scaled_immutable_bitmap(Gfx::IntRect const& dst_rect, Gfx::ImmutableBitmap const&, Gfx::IntRect const& src_rect, Gfx::Painter::ScalingMode scaling_mode) = 0;
virtual CommandResult set_clip_rect(Gfx::IntRect const& rect) = 0;
virtual CommandResult clear_clip_rect() = 0;
virtual CommandResult set_font(Gfx::Font const& font) = 0;
@ -435,6 +447,7 @@ public:
void draw_rect(Gfx::IntRect const& rect, Color color, bool rough = false);
void draw_scaled_bitmap(Gfx::IntRect const& dst_rect, Gfx::Bitmap const& bitmap, Gfx::IntRect const& src_rect, Gfx::Painter::ScalingMode scaling_mode = Gfx::Painter::ScalingMode::NearestNeighbor);
void draw_scaled_immutable_bitmap(Gfx::IntRect const& dst_rect, Gfx::ImmutableBitmap const& bitmap, Gfx::IntRect const& src_rect, Gfx::Painter::ScalingMode scaling_mode = Gfx::Painter::ScalingMode::NearestNeighbor);
void draw_line(Gfx::IntPoint from, Gfx::IntPoint to, Color color, int thickness = 1, Gfx::Painter::LineStyle style = Gfx::Painter::LineStyle::Solid, Color alternate_color = Color::Transparent);