1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 13:18:13 +00:00

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.
This commit is contained in:
Aliaksandr Kalenik 2023-12-16 16:22:11 +01:00 committed by Andreas Kling
parent ed1ade0534
commit 161082e282
8 changed files with 25 additions and 20 deletions

View file

@ -16,14 +16,6 @@
namespace AccelGfx { namespace AccelGfx {
Context& Context::the()
{
static OwnPtr<Context> s_the;
if (!s_the)
s_the = Context::create();
return *s_the;
}
#ifdef AK_OS_MACOS #ifdef AK_OS_MACOS
static void make_context_cgl() static void make_context_cgl()
{ {

View file

@ -20,8 +20,6 @@ namespace AccelGfx {
class Context { class Context {
public: public:
static Context& the();
static OwnPtr<Context> create(); static OwnPtr<Context> create();
Context() Context()

View file

@ -170,9 +170,8 @@ void main() {
HashMap<u32, GL::Texture> s_immutable_bitmap_texture_cache; HashMap<u32, GL::Texture> s_immutable_bitmap_texture_cache;
NonnullOwnPtr<Painter> Painter::create() NonnullOwnPtr<Painter> Painter::create(Context& context)
{ {
auto& context = Context::the();
return make<Painter>(context); return make<Painter>(context);
} }

View file

@ -29,7 +29,7 @@ class Painter {
AK_MAKE_NONMOVABLE(Painter); AK_MAKE_NONMOVABLE(Painter);
public: public:
static NonnullOwnPtr<Painter> create(); static NonnullOwnPtr<Painter> create(Context&);
Painter(Context&); Painter(Context&);
~Painter(); ~Painter();

View file

@ -9,10 +9,11 @@
namespace Web::Painting { namespace Web::Painting {
PaintingCommandExecutorGPU::PaintingCommandExecutorGPU(Gfx::Bitmap& bitmap) PaintingCommandExecutorGPU::PaintingCommandExecutorGPU(AccelGfx::Context& context, Gfx::Bitmap& bitmap)
: m_target_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()); auto canvas = AccelGfx::Canvas::create(bitmap.size());
painter->set_target_canvas(canvas); painter->set_target_canvas(canvas);
m_stacking_contexts.append({ .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(stacking_context_transform);
final_transform.multiply(inverse_origin_translation); final_transform.multiply(inverse_origin_translation);
if (opacity < 1 || !stacking_context_transform.is_identity_or_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()); auto canvas = AccelGfx::Canvas::create(source_paintable_rect.size());
painter->set_target_canvas(canvas); painter->set_target_canvas(canvas);
painter->translate(-source_paintable_rect.location().to_type<float>()); painter->translate(-source_paintable_rect.location().to_type<float>());
@ -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<Gfx::DrawGlyphOrEmoji const> glyph_run, Color const& color, int fragment_baseline, Gfx::IntPoint const& draw_location) CommandResult PaintingCommandExecutorGPU::paint_text_shadow(int blur_radius, Gfx::IntRect const& shadow_bounding_rect, Gfx::IntRect const& text_rect, Span<Gfx::DrawGlyphOrEmoji const> 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_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->set_target_canvas(text_shadow_canvas);
text_shadow_painter->clear(color.with_alpha(0)); 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_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->set_target_canvas(horizontal_blur_canvas);
horizontal_blur_painter->clear(color.with_alpha(0)); horizontal_blur_painter->clear(color.with_alpha(0));
horizontal_blur_painter->blit_blurred_canvas(shadow_bounding_rect.to_type<float>(), *text_shadow_canvas, blur_radius, AccelGfx::Painter::BlurDirection::Horizontal); horizontal_blur_painter->blit_blurred_canvas(shadow_bounding_rect.to_type<float>(), *text_shadow_canvas, blur_radius, AccelGfx::Painter::BlurDirection::Horizontal);

View file

@ -55,11 +55,12 @@ public:
bool needs_update_immutable_bitmap_texture_cache() const override { return true; } bool needs_update_immutable_bitmap_texture_cache() const override { return true; }
void update_immutable_bitmap_texture_cache(HashMap<u32, Gfx::ImmutableBitmap const*>&) override; void update_immutable_bitmap_texture_cache(HashMap<u32, Gfx::ImmutableBitmap const*>&) override;
PaintingCommandExecutorGPU(Gfx::Bitmap& bitmap); PaintingCommandExecutorGPU(AccelGfx::Context&, Gfx::Bitmap& bitmap);
~PaintingCommandExecutorGPU() override; ~PaintingCommandExecutorGPU() override;
private: private:
Gfx::Bitmap& m_target_bitmap; Gfx::Bitmap& m_target_bitmap;
AccelGfx::Context& m_context;
struct StackingContext { struct StackingContext {
RefPtr<AccelGfx::Canvas> canvas; RefPtr<AccelGfx::Canvas> canvas;

View file

@ -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() }); 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 = {}; 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) 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) { if (s_use_gpu_painter) {
#ifdef HAS_ACCELERATED_GRAPHICS #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); recording_painter.execute(painting_command_executor);
#else #else
static bool has_warned_about_configuration = false; static bool has_warned_about_configuration = false;

View file

@ -14,6 +14,10 @@
#include <LibWeb/PixelUnits.h> #include <LibWeb/PixelUnits.h>
#include <WebContent/Forward.h> #include <WebContent/Forward.h>
#ifdef HAS_ACCELERATED_GRAPHICS
# include <LibAccelGfx/Context.h>
#endif
namespace WebContent { namespace WebContent {
class PageClient final : public Web::PageClient { class PageClient final : public Web::PageClient {
@ -151,6 +155,10 @@ private:
Web::CSS::PreferredColorScheme m_preferred_color_scheme { Web::CSS::PreferredColorScheme::Auto }; Web::CSS::PreferredColorScheme m_preferred_color_scheme { Web::CSS::PreferredColorScheme::Auto };
RefPtr<WebDriverConnection> m_webdriver; RefPtr<WebDriverConnection> m_webdriver;
#ifdef HAS_ACCELERATED_GRAPHICS
OwnPtr<AccelGfx::Context> m_accelerated_graphics_context;
#endif
}; };
} }