Skip to content

Commit 1382bbf

Browse files
nicoawesomekling
authored andcommitted
LibGfx: Make Painter take the scale factor as constructor argument
I want to give Bitmap an intrinsic scale factor and this is a step in that direction. No behavior change.
1 parent 7a0bc2f commit 1382bbf

File tree

4 files changed

+12
-14
lines changed

4 files changed

+12
-14
lines changed

Userland/Demos/LibGfxScaleDemo/main.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,10 @@ Canvas::Canvas()
6060
m_bitmap_1x = Gfx::Bitmap::create(Gfx::BitmapFormat::RGB32, { WIDTH, HEIGHT });
6161
m_bitmap_2x = Gfx::Bitmap::create(Gfx::BitmapFormat::RGB32, { WIDTH * 2, HEIGHT * 2 });
6262

63-
Gfx::Painter painter_1x(*m_bitmap_1x);
63+
Gfx::Painter painter_1x(*m_bitmap_1x, 1);
6464
draw(painter_1x);
6565

66-
Gfx::Painter painter_2x(*m_bitmap_2x);
67-
painter_2x.scale(2);
66+
Gfx::Painter painter_2x(*m_bitmap_2x, 2);
6867
draw(painter_2x);
6968

7069
update();

Userland/Libraries/LibGfx/Painter.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,16 @@ ALWAYS_INLINE Color get_pixel(const Gfx::Bitmap& bitmap, int x, int y)
6868
return bitmap.get_pixel(x, y);
6969
}
7070

71-
Painter::Painter(Gfx::Bitmap& bitmap)
71+
Painter::Painter(Gfx::Bitmap& bitmap, int scale)
7272
: m_target(bitmap)
7373
{
7474
ASSERT(bitmap.format() == Gfx::BitmapFormat::RGB32 || bitmap.format() == Gfx::BitmapFormat::RGBA32);
75+
ASSERT(bitmap.width() % scale == 0);
76+
ASSERT(bitmap.height() % scale == 0);
7577
m_state_stack.append(State());
7678
state().font = &FontDatabase::default_font();
7779
state().clip_rect = { { 0, 0 }, bitmap.size() };
80+
state().scale = scale;
7881
m_clip_origin = state().clip_rect;
7982
}
8083

Userland/Libraries/LibGfx/Painter.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ namespace Gfx {
4141

4242
class Painter {
4343
public:
44-
explicit Painter(Gfx::Bitmap&);
44+
explicit Painter(Gfx::Bitmap&, int scale = 1);
4545
~Painter();
4646

4747
enum class LineStyle {
@@ -119,7 +119,6 @@ class Painter {
119119

120120
void translate(int dx, int dy) { translate({ dx, dy }); }
121121
void translate(const IntPoint& delta) { state().translation.move_by(delta * state().scale); }
122-
void scale(int s) { state().scale *= s; }
123122

124123
Gfx::Bitmap* target() { return m_target.ptr(); }
125124

Userland/Services/WindowServer/Compositor.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,16 @@ void Compositor::init_bitmaps()
9595
auto physical_size = screen.physical_size();
9696

9797
m_front_bitmap = Gfx::Bitmap::create_wrapper(Gfx::BitmapFormat::RGB32, physical_size, screen.pitch(), screen.scanline(0));
98-
m_front_painter = make<Gfx::Painter>(*m_front_bitmap);
99-
m_front_painter->scale(screen.scale_factor());
98+
m_front_painter = make<Gfx::Painter>(*m_front_bitmap, screen.scale_factor());
10099

101100
if (m_screen_can_set_buffer)
102101
m_back_bitmap = Gfx::Bitmap::create_wrapper(Gfx::BitmapFormat::RGB32, physical_size, screen.pitch(), screen.scanline(physical_size.height()));
103102
else
104103
m_back_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::RGB32, physical_size);
105-
m_back_painter = make<Gfx::Painter>(*m_back_bitmap);
106-
m_back_painter->scale(screen.scale_factor());
104+
m_back_painter = make<Gfx::Painter>(*m_back_bitmap, screen.scale_factor());
107105

108106
m_temp_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::RGB32, physical_size);
109-
m_temp_painter = make<Gfx::Painter>(*m_temp_bitmap);
110-
m_temp_painter->scale(screen.scale_factor());
107+
m_temp_painter = make<Gfx::Painter>(*m_temp_bitmap, screen.scale_factor());
111108

112109
m_buffers_are_flipped = false;
113110

@@ -456,7 +453,7 @@ void Compositor::compose()
456453
{
457454
// FIXME: Give Bitmap an intrinsic scale factor and make Painter::blit() do the right thing if both it and the passed bitmap have scale factors:
458455
// If a 2x scaled bitmap is blitted on a 2x scaled painter, it should be blitted without scale.
459-
Gfx::Painter unscaled_back_painter(*m_back_bitmap);
456+
Gfx::Painter unscaled_back_painter(*m_back_bitmap, 1);
460457
auto scale = Screen::the().scale_factor();
461458
for (auto& rect : flush_transparent_rects.rects())
462459
unscaled_back_painter.blit(rect.location() * scale, *m_temp_bitmap, rect * scale);
@@ -825,7 +822,7 @@ void Compositor::restore_cursor_back()
825822

826823
// FIXME: Give Bitmap an intrinsic scale factor and make Painter::blit() do the right thing if both it and the passed bitmap have scale factors:
827824
// If a 2x scaled bitmap is blitted on a 2x scaled painter, it should be blitted without scale.
828-
Gfx::Painter unscaled_back_painter(*m_back_bitmap);
825+
Gfx::Painter unscaled_back_painter(*m_back_bitmap, 1);
829826
auto last_physical_cursor_rect = (m_last_cursor_rect * Screen::the().scale_factor()).intersected(Screen::the().physical_rect());
830827
unscaled_back_painter.blit(last_physical_cursor_rect.location(), *m_cursor_back_bitmap, { { 0, 0 }, last_physical_cursor_rect.size() });
831828
}

0 commit comments

Comments
 (0)