From 161082e28254d64be95a86a415f1b236eec91c16 Mon Sep 17 00:00:00 2001 From: Aliaksandr Kalenik Date: Sat, 16 Dec 2023 16:22:11 +0100 Subject: [PATCH] LibAccelGfx+LibWeb: Explicitly pass OpenGL context to Painter Let's not assume there is one global OpenGL context because it might change once we will start creating >1 page inside single WebContent process or contexts for WebGL. --- Userland/Libraries/LibAccelGfx/Context.cpp | 8 -------- Userland/Libraries/LibAccelGfx/Context.h | 2 -- Userland/Libraries/LibAccelGfx/Painter.cpp | 3 +-- Userland/Libraries/LibAccelGfx/Painter.h | 2 +- .../LibWeb/Painting/PaintingCommandExecutorGPU.cpp | 11 ++++++----- .../LibWeb/Painting/PaintingCommandExecutorGPU.h | 3 ++- Userland/Services/WebContent/PageClient.cpp | 8 +++++++- Userland/Services/WebContent/PageClient.h | 8 ++++++++ 8 files changed, 25 insertions(+), 20 deletions(-) diff --git a/Userland/Libraries/LibAccelGfx/Context.cpp b/Userland/Libraries/LibAccelGfx/Context.cpp index da00e9f66f..f06e3f264f 100644 --- a/Userland/Libraries/LibAccelGfx/Context.cpp +++ b/Userland/Libraries/LibAccelGfx/Context.cpp @@ -16,14 +16,6 @@ namespace AccelGfx { -Context& Context::the() -{ - static OwnPtr s_the; - if (!s_the) - s_the = Context::create(); - return *s_the; -} - #ifdef AK_OS_MACOS static void make_context_cgl() { diff --git a/Userland/Libraries/LibAccelGfx/Context.h b/Userland/Libraries/LibAccelGfx/Context.h index 4fc89c3958..cdfff72da5 100644 --- a/Userland/Libraries/LibAccelGfx/Context.h +++ b/Userland/Libraries/LibAccelGfx/Context.h @@ -20,8 +20,6 @@ namespace AccelGfx { class Context { public: - static Context& the(); - static OwnPtr create(); Context() diff --git a/Userland/Libraries/LibAccelGfx/Painter.cpp b/Userland/Libraries/LibAccelGfx/Painter.cpp index 6b46a75a7d..21c3e23a8e 100644 --- a/Userland/Libraries/LibAccelGfx/Painter.cpp +++ b/Userland/Libraries/LibAccelGfx/Painter.cpp @@ -170,9 +170,8 @@ void main() { HashMap s_immutable_bitmap_texture_cache; -NonnullOwnPtr Painter::create() +NonnullOwnPtr Painter::create(Context& context) { - auto& context = Context::the(); return make(context); } diff --git a/Userland/Libraries/LibAccelGfx/Painter.h b/Userland/Libraries/LibAccelGfx/Painter.h index 53ff704e98..55d3f15630 100644 --- a/Userland/Libraries/LibAccelGfx/Painter.h +++ b/Userland/Libraries/LibAccelGfx/Painter.h @@ -29,7 +29,7 @@ class Painter { AK_MAKE_NONMOVABLE(Painter); public: - static NonnullOwnPtr create(); + static NonnullOwnPtr create(Context&); Painter(Context&); ~Painter(); diff --git a/Userland/Libraries/LibWeb/Painting/PaintingCommandExecutorGPU.cpp b/Userland/Libraries/LibWeb/Painting/PaintingCommandExecutorGPU.cpp index ab14c46e97..5088ec9990 100644 --- a/Userland/Libraries/LibWeb/Painting/PaintingCommandExecutorGPU.cpp +++ b/Userland/Libraries/LibWeb/Painting/PaintingCommandExecutorGPU.cpp @@ -9,10 +9,11 @@ namespace Web::Painting { -PaintingCommandExecutorGPU::PaintingCommandExecutorGPU(Gfx::Bitmap& bitmap) +PaintingCommandExecutorGPU::PaintingCommandExecutorGPU(AccelGfx::Context& context, Gfx::Bitmap& bitmap) : m_target_bitmap(bitmap) + , m_context(context) { - auto painter = AccelGfx::Painter::create(); + auto painter = AccelGfx::Painter::create(m_context); auto canvas = AccelGfx::Canvas::create(bitmap.size()); painter->set_target_canvas(canvas); m_stacking_contexts.append({ .canvas = canvas, @@ -114,7 +115,7 @@ CommandResult PaintingCommandExecutorGPU::push_stacking_context(float opacity, b final_transform.multiply(stacking_context_transform); final_transform.multiply(inverse_origin_translation); if (opacity < 1 || !stacking_context_transform.is_identity_or_translation()) { - auto painter = AccelGfx::Painter::create(); + auto painter = AccelGfx::Painter::create(m_context); auto canvas = AccelGfx::Canvas::create(source_paintable_rect.size()); painter->set_target_canvas(canvas); painter->translate(-source_paintable_rect.location().to_type()); @@ -168,7 +169,7 @@ CommandResult PaintingCommandExecutorGPU::paint_inner_box_shadow(PaintOuterBoxSh CommandResult PaintingCommandExecutorGPU::paint_text_shadow(int blur_radius, Gfx::IntRect const& shadow_bounding_rect, Gfx::IntRect const& text_rect, Span glyph_run, Color const& color, int fragment_baseline, Gfx::IntPoint const& draw_location) { auto text_shadow_canvas = AccelGfx::Canvas::create(shadow_bounding_rect.size()); - auto text_shadow_painter = AccelGfx::Painter::create(); + auto text_shadow_painter = AccelGfx::Painter::create(m_context); text_shadow_painter->set_target_canvas(text_shadow_canvas); text_shadow_painter->clear(color.with_alpha(0)); @@ -182,7 +183,7 @@ CommandResult PaintingCommandExecutorGPU::paint_text_shadow(int blur_radius, Gfx } auto horizontal_blur_canvas = AccelGfx::Canvas::create(shadow_bounding_rect.size()); - auto horizontal_blur_painter = AccelGfx::Painter::create(); + auto horizontal_blur_painter = AccelGfx::Painter::create(m_context); horizontal_blur_painter->set_target_canvas(horizontal_blur_canvas); horizontal_blur_painter->clear(color.with_alpha(0)); horizontal_blur_painter->blit_blurred_canvas(shadow_bounding_rect.to_type(), *text_shadow_canvas, blur_radius, AccelGfx::Painter::BlurDirection::Horizontal); diff --git a/Userland/Libraries/LibWeb/Painting/PaintingCommandExecutorGPU.h b/Userland/Libraries/LibWeb/Painting/PaintingCommandExecutorGPU.h index 5035613688..05c4926079 100644 --- a/Userland/Libraries/LibWeb/Painting/PaintingCommandExecutorGPU.h +++ b/Userland/Libraries/LibWeb/Painting/PaintingCommandExecutorGPU.h @@ -55,11 +55,12 @@ public: bool needs_update_immutable_bitmap_texture_cache() const override { return true; } void update_immutable_bitmap_texture_cache(HashMap&) override; - PaintingCommandExecutorGPU(Gfx::Bitmap& bitmap); + PaintingCommandExecutorGPU(AccelGfx::Context&, Gfx::Bitmap& bitmap); ~PaintingCommandExecutorGPU() override; private: Gfx::Bitmap& m_target_bitmap; + AccelGfx::Context& m_context; struct StackingContext { RefPtr canvas; diff --git a/Userland/Services/WebContent/PageClient.cpp b/Userland/Services/WebContent/PageClient.cpp index c5a15b04b1..825a8c652e 100644 --- a/Userland/Services/WebContent/PageClient.cpp +++ b/Userland/Services/WebContent/PageClient.cpp @@ -55,6 +55,12 @@ PageClient::PageClient(PageHost& owner, u64 id) client().async_did_invalidate_content_rect({ m_invalidation_rect.x().value(), m_invalidation_rect.y().value(), m_invalidation_rect.width().value(), m_invalidation_rect.height().value() }); m_invalidation_rect = {}; }); + +#ifdef HAS_ACCELERATED_GRAPHICS + if (s_use_gpu_painter) { + m_accelerated_graphics_context = AccelGfx::Context::create(); + } +#endif } void PageClient::visit_edges(JS::Cell::Visitor& visitor) @@ -169,7 +175,7 @@ void PageClient::paint(Web::DevicePixelRect const& content_rect, Gfx::Bitmap& ta if (s_use_gpu_painter) { #ifdef HAS_ACCELERATED_GRAPHICS - Web::Painting::PaintingCommandExecutorGPU painting_command_executor(target); + Web::Painting::PaintingCommandExecutorGPU painting_command_executor(*m_accelerated_graphics_context, target); recording_painter.execute(painting_command_executor); #else static bool has_warned_about_configuration = false; diff --git a/Userland/Services/WebContent/PageClient.h b/Userland/Services/WebContent/PageClient.h index 5e521e9173..ff7235caec 100644 --- a/Userland/Services/WebContent/PageClient.h +++ b/Userland/Services/WebContent/PageClient.h @@ -14,6 +14,10 @@ #include #include +#ifdef HAS_ACCELERATED_GRAPHICS +# include +#endif + namespace WebContent { class PageClient final : public Web::PageClient { @@ -151,6 +155,10 @@ private: Web::CSS::PreferredColorScheme m_preferred_color_scheme { Web::CSS::PreferredColorScheme::Auto }; RefPtr m_webdriver; + +#ifdef HAS_ACCELERATED_GRAPHICS + OwnPtr m_accelerated_graphics_context; +#endif }; }