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

LibGfx: Give Bitmap a scale factor

Gfx::Bitmap can now store its scale factor. Normally it's 1, but
in high dpi mode it can be 2.

If a Bitmap with a scale factor of 2 is blitted to a Painter with
scale factor of 2, the pixels can be copied over without any resampling.
(When blitting a Bitmap with a scale factor of 1 to a Painter with scale
factor of 2, the Bitmap is painted at twice its width and height at
paint time. Blitting a Bitmap with a scale factor of 2 to a Painter with
scale factor 1 is not supported.)

A Bitmap with scale factor of 2 reports the same width() and height() as
one with scale factor 1. That's important because many places in the
codebase use a bitmap's width() and height() to layout Widgets, and all
widget coordinates are in logical coordinates as well, per
Documentation/HighDPI.md.

Bitmap grows physical_width() / physical_height() to access the actual
pixel size. Update a few callers that work with pixels to call this
instead.

Make Painter's constructor take its scale factor from the target bitmap
that's passed in, and update its various blit() methods to handle
blitting a 2x bitmap to a 2x painter. This allows removing some gnarly
code in Compositor. (In return, put some new gnarly code in
LibGfxScaleDemo to preserve behavior there.)

No intended behavior change.
This commit is contained in:
Nico Weber 2021-01-19 12:10:47 -05:00 committed by Andreas Kling
parent c6726f331e
commit 5f9c42c404
14 changed files with 192 additions and 174 deletions

View file

@ -50,6 +50,7 @@ private:
Canvas();
RefPtr<Gfx::Bitmap> m_bitmap_1x;
RefPtr<Gfx::Bitmap> m_bitmap_2x;
RefPtr<Gfx::Bitmap> m_bitmap_2x_as_1x;
void draw(Gfx::Painter& painter);
virtual void paint_event(GUI::PaintEvent&) override;
@ -57,13 +58,20 @@ private:
Canvas::Canvas()
{
m_bitmap_1x = Gfx::Bitmap::create(Gfx::BitmapFormat::RGB32, { WIDTH, HEIGHT });
m_bitmap_2x = Gfx::Bitmap::create(Gfx::BitmapFormat::RGB32, { WIDTH * 2, HEIGHT * 2 });
m_bitmap_1x = Gfx::Bitmap::create(Gfx::BitmapFormat::RGB32, { WIDTH, HEIGHT }, 1);
m_bitmap_2x = Gfx::Bitmap::create(Gfx::BitmapFormat::RGB32, { WIDTH, HEIGHT }, 2);
Gfx::Painter painter_1x(*m_bitmap_1x, 1);
// m_bitmap_1x and m_bitmap_2x have the same logical size, so LibGfx will try to draw them at the same physical size:
// When drawing on a 2x backing store it'd scale m_bitmap_1x up 2x and paint m_bitmap_2x at its physical size.
// When drawing on a 1x backing store it'd draw m_bitmap_1x at its physical size, and it would have to scale down m_bitmap_2x to 0.5x its size.
// But the system can't current scale down, and we want to draw the 2x bitmap at twice the size of the 1x bitmap in this particular application,
// so make a 1x alias of the 2x bitmap to make LibGfx paint it without any scaling at paint time, mapping once pixel to one pixel.
m_bitmap_2x_as_1x = Gfx::Bitmap::create_wrapper(Gfx::BitmapFormat::RGB32, m_bitmap_2x->physical_size(), 1, m_bitmap_2x->pitch(), m_bitmap_2x->scanline(0));
Gfx::Painter painter_1x(*m_bitmap_1x);
draw(painter_1x);
Gfx::Painter painter_2x(*m_bitmap_2x, 2);
Gfx::Painter painter_2x(*m_bitmap_2x);
draw(painter_2x);
update();
@ -79,7 +87,7 @@ void Canvas::paint_event(GUI::PaintEvent& event)
painter.add_clip_rect(event.rect());
painter.fill_rect(event.rect(), Color::Magenta);
painter.blit({ 0, 0 }, *m_bitmap_1x, m_bitmap_1x->rect());
painter.blit({ 0, HEIGHT }, *m_bitmap_2x, m_bitmap_2x->rect());
painter.blit({ 0, HEIGHT }, *m_bitmap_2x_as_1x, m_bitmap_2x_as_1x->rect());
}
void Canvas::draw(Gfx::Painter& painter)