Painter.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2023, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/HashMap.h>
  9. #include <AK/Noncopyable.h>
  10. #include <AK/Vector.h>
  11. #include <LibAccelGfx/Canvas.h>
  12. #include <LibAccelGfx/Context.h>
  13. #include <LibAccelGfx/Forward.h>
  14. #include <LibAccelGfx/GL.h>
  15. #include <LibAccelGfx/GlyphAtlas.h>
  16. #include <LibAccelGfx/Program.h>
  17. #include <LibGfx/AffineTransform.h>
  18. #include <LibGfx/Font/Font.h>
  19. #include <LibGfx/Forward.h>
  20. #include <LibGfx/Gradients.h>
  21. #include <LibGfx/TextLayout.h>
  22. namespace AccelGfx {
  23. class Painter {
  24. AK_MAKE_NONCOPYABLE(Painter);
  25. AK_MAKE_NONMOVABLE(Painter);
  26. public:
  27. static NonnullOwnPtr<Painter> create(Context&, NonnullRefPtr<Canvas>);
  28. Painter(Context&, NonnullRefPtr<Canvas>);
  29. ~Painter();
  30. Canvas const& canvas() { return *m_target_canvas; }
  31. void clear(Gfx::Color);
  32. void save();
  33. void restore();
  34. [[nodiscard]] Gfx::AffineTransform const& transform() const { return state().transform; }
  35. void set_transform(Gfx::AffineTransform const& transform) { state().transform = transform; }
  36. void translate(Gfx::FloatPoint translation) { state().transform.translate(translation); }
  37. Gfx::IntRect const& clip_rect() const { return state().clip_rect; }
  38. void fill_rect(Gfx::FloatRect, Gfx::Color);
  39. void fill_rect(Gfx::IntRect, Gfx::Color);
  40. enum class ScalingMode {
  41. NearestNeighbor,
  42. Bilinear,
  43. };
  44. enum class BlendingMode {
  45. AlphaAdd,
  46. AlphaOverride,
  47. AlphaPreserve,
  48. };
  49. void draw_line(Gfx::IntPoint a, Gfx::IntPoint b, float thickness, Gfx::Color color);
  50. void draw_line(Gfx::FloatPoint a, Gfx::FloatPoint b, float thickness, Gfx::Color color);
  51. void draw_scaled_bitmap(Gfx::FloatRect const& dst_rect, Gfx::Bitmap const&, Gfx::FloatRect const& src_rect, ScalingMode = ScalingMode::NearestNeighbor);
  52. void draw_scaled_bitmap(Gfx::IntRect const& dst_rect, Gfx::Bitmap const&, Gfx::IntRect const& src_rect, ScalingMode = ScalingMode::NearestNeighbor);
  53. void draw_scaled_immutable_bitmap(Gfx::IntRect const& dst_rect, Gfx::ImmutableBitmap const&, Gfx::IntRect const& src_rect, ScalingMode = ScalingMode::NearestNeighbor);
  54. void draw_scaled_immutable_bitmap(Gfx::FloatRect const& dst_rect, Gfx::ImmutableBitmap const&, Gfx::FloatRect const& src_rect, ScalingMode = ScalingMode::NearestNeighbor);
  55. void draw_glyph_run(Span<Gfx::DrawGlyphOrEmoji const> glyph_run, Color const& color);
  56. void set_clip_rect(Gfx::IntRect);
  57. void clear_clip_rect();
  58. void flush(Gfx::Bitmap&);
  59. void fill_rect_with_linear_gradient(Gfx::IntRect const&, ReadonlySpan<Gfx::ColorStop>, float angle, Optional<float> repeat_length = {});
  60. void fill_rect_with_linear_gradient(Gfx::FloatRect const&, ReadonlySpan<Gfx::ColorStop>, float angle, Optional<float> repeat_length = {});
  61. struct CornerRadius {
  62. float horizontal_radius;
  63. float vertical_radius;
  64. };
  65. void fill_rect_with_rounded_corners(Gfx::IntRect const& rect, Color const& color, CornerRadius const& top_left_radius, CornerRadius const& top_right_radius, CornerRadius const& bottom_left_radius, CornerRadius const& bottom_right_radius, BlendingMode = BlendingMode::AlphaAdd);
  66. void fill_rect_with_rounded_corners(Gfx::FloatRect const& rect, Color const& color, CornerRadius const& top_left_radius, CornerRadius const& top_right_radius, CornerRadius const& bottom_left_radius, CornerRadius const& bottom_right_radius, BlendingMode = BlendingMode::AlphaAdd);
  67. void blit_canvas(Gfx::IntRect const& dst_rect, Canvas const&, float opacity = 1.0f, Optional<Gfx::AffineTransform> affine_transform = {});
  68. void blit_canvas(Gfx::FloatRect const& dst_rect, Canvas const&, float opacity = 1.0f, Optional<Gfx::AffineTransform> affine_transform = {});
  69. void blit_canvas(Gfx::FloatRect const& dst_rect, Canvas const&, Gfx::FloatRect const& src_rect, float opacity = 1.0f, Optional<Gfx::AffineTransform> affine_transform = {}, BlendingMode = BlendingMode::AlphaAdd);
  70. enum class BlurDirection {
  71. Horizontal,
  72. Vertical,
  73. };
  74. void blit_blurred_canvas(Gfx::FloatRect const& dst_rect, Canvas const&, int radius, BlurDirection direction, ScalingMode = ScalingMode::NearestNeighbor);
  75. void update_immutable_bitmap_texture_cache(HashMap<u32, Gfx::ImmutableBitmap const*>&);
  76. private:
  77. Context& m_context;
  78. struct State {
  79. Gfx::AffineTransform transform;
  80. Gfx::IntRect clip_rect;
  81. };
  82. [[nodiscard]] State& state() { return m_state_stack.last(); }
  83. [[nodiscard]] State const& state() const { return m_state_stack.last(); }
  84. void blit_scaled_texture(Gfx::FloatRect const& dst_rect, GL::Texture const&, Gfx::FloatRect const& src_rect, ScalingMode, float opacity = 1.0f, Optional<Gfx::AffineTransform> affine_transform = {}, BlendingMode = BlendingMode::AlphaAdd);
  85. void blit_blurred_texture(Gfx::FloatRect const& dst_rect, GL::Texture const&, Gfx::FloatRect const& src_rect, int radius, BlurDirection direction, ScalingMode = ScalingMode::NearestNeighbor);
  86. void bind_target_canvas();
  87. [[nodiscard]] Gfx::FloatRect to_clip_space(Gfx::FloatRect const& screen_rect) const;
  88. Vector<State, 1> m_state_stack;
  89. NonnullRefPtr<Canvas> m_target_canvas;
  90. Program m_rectangle_program;
  91. Program m_rounded_rectangle_program;
  92. Program m_blit_program;
  93. Program m_linear_gradient_program;
  94. Program m_blur_program;
  95. };
  96. }