LibWeb/WebGL: Respect UNPACK_FLIP_Y_WEBGL pixel storage parameter

When this is true, we have to vertically flip TexImageSource provided
images before uploading them.

Fixes several graphical glitches on Google Maps.
Fixes globe being upside down on Shopify's homepage.
Likely fixes more websites.
This commit is contained in:
Luke Wilde 2025-10-23 20:24:14 +01:00 committed by Andreas Kling
parent 008699c129
commit 3e7061da40
Notes: github-actions[bot] 2025-10-25 10:57:35 +00:00
4 changed files with 45 additions and 3 deletions

View file

@ -12,6 +12,7 @@ extern "C" {
#include <GLES2/gl2ext_angle.h>
}
#include <LibGfx/SkiaUtils.h>
#include <LibWeb/HTML/HTMLCanvasElement.h>
#include <LibWeb/HTML/HTMLImageElement.h>
#include <LibWeb/HTML/HTMLVideoElement.h>
@ -20,10 +21,12 @@ extern "C" {
#include <LibWeb/WebGL/OpenGLContext.h>
#include <LibWeb/WebGL/WebGLRenderingContextBase.h>
#include <core/SkCanvas.h>
#include <core/SkColorSpace.h>
#include <core/SkColorType.h>
#include <core/SkImage.h>
#include <core/SkPixmap.h>
#include <core/SkSurface.h>
namespace Web::WebGL {
@ -178,8 +181,20 @@ Optional<WebGLRenderingContextBase::ConvertedTexture> WebGLRenderingContextBase:
// FIXME: Respect unpackColorSpace
auto color_space = SkColorSpace::MakeSRGB();
auto image_info = SkImageInfo::Make(width, height, skia_format, SkAlphaType::kPremul_SkAlphaType, color_space);
SkPixmap const pixmap(image_info, buffer.data(), buffer_pitch.value());
bitmap->sk_image()->readPixels(pixmap, 0, 0);
auto surface = SkSurfaces::WrapPixels(image_info, buffer.data(), buffer_pitch.value());
auto surface_canvas = surface->getCanvas();
auto dst_rect = Gfx::to_skia_rect(Gfx::Rect { 0, 0, width, height });
// The first pixel transferred from the source to the WebGL implementation corresponds to the upper left corner of
// the source. This behavior is modified by the UNPACK_FLIP_Y_WEBGL pixel storage parameter, except for ImageBitmap
// arguments, as described in the abovementioned section.
if (m_unpack_flip_y && !source.has<GC::Root<HTML::ImageBitmap>>()) {
surface_canvas->translate(0, dst_rect.height());
surface_canvas->scale(1, -1);
}
surface_canvas->drawImageRect(bitmap->sk_image(), dst_rect, Gfx::to_skia_sampling_options(Gfx::ScalingMode::NearestNeighbor));
return ConvertedTexture {
.buffer = move(buffer),
.width = width,