From 45e0f50463776c4e53911d11ccb6e445d11f7eab Mon Sep 17 00:00:00 2001 From: Aliaksandr Kalenik Date: Sat, 30 Nov 2024 16:52:09 +0100 Subject: [PATCH] LibWeb: Flip vertically PaintingSurface attached to WebGL context OpenGL's origin is at the bottom-left corner, while Skia's origin is at the top-left corner. This change adds a transformation to compensate for this difference when rendering PaintingSurface attached to WebGL context. --- Libraries/LibGfx/PaintingSurface.h | 4 ++++ Libraries/LibWeb/Painting/DisplayListPlayerSkia.cpp | 10 ++++++++++ Libraries/LibWeb/WebGL/OpenGLContext.cpp | 1 + 3 files changed, 15 insertions(+) diff --git a/Libraries/LibGfx/PaintingSurface.h b/Libraries/LibGfx/PaintingSurface.h index ae25b5fb9c2..ea5e84dc56e 100644 --- a/Libraries/LibGfx/PaintingSurface.h +++ b/Libraries/LibGfx/PaintingSurface.h @@ -45,6 +45,9 @@ public: void flush() const; + bool flip_vertically() const { return m_flip_vertically; } + void set_flip_vertically() { m_flip_vertically = true; } + ~PaintingSurface(); private: @@ -53,6 +56,7 @@ private: PaintingSurface(NonnullOwnPtr&&); NonnullOwnPtr m_impl; + bool m_flip_vertically { false }; }; } diff --git a/Libraries/LibWeb/Painting/DisplayListPlayerSkia.cpp b/Libraries/LibWeb/Painting/DisplayListPlayerSkia.cpp index 9460fdab76c..97cf904225c 100644 --- a/Libraries/LibWeb/Painting/DisplayListPlayerSkia.cpp +++ b/Libraries/LibWeb/Painting/DisplayListPlayerSkia.cpp @@ -329,7 +329,17 @@ void DisplayListPlayerSkia::draw_painting_surface(DrawPaintingSurface const& com auto& canvas = surface().canvas(); auto image = sk_surface.makeImageSnapshot(); SkPaint paint; + if (command.surface->flip_vertically()) { + canvas.save(); + SkMatrix matrix; + matrix.setIdentity(); + matrix.preScale(1, -1, dst_rect.centerX(), dst_rect.centerY()); + canvas.concat(matrix); + } canvas.drawImageRect(image, src_rect, dst_rect, to_skia_sampling_options(command.scaling_mode), &paint, SkCanvas::kStrict_SrcRectConstraint); + if (command.surface->flip_vertically()) { + canvas.restore(); + } } void DisplayListPlayerSkia::draw_scaled_immutable_bitmap(DrawScaledImmutableBitmap const& command) diff --git a/Libraries/LibWeb/WebGL/OpenGLContext.cpp b/Libraries/LibWeb/WebGL/OpenGLContext.cpp index 44bfa62c6ed..bf9a3d0684d 100644 --- a/Libraries/LibWeb/WebGL/OpenGLContext.cpp +++ b/Libraries/LibWeb/WebGL/OpenGLContext.cpp @@ -122,6 +122,7 @@ void OpenGLContext::allocate_painting_surface_if_needed() auto iosurface = Core::IOSurfaceHandle::create(m_size.width(), m_size.height()); m_painting_surface = Gfx::PaintingSurface::wrap_iosurface(iosurface, m_skia_backend_context); + m_painting_surface->set_flip_vertically(); auto width = m_size.width(); auto height = m_size.height();