2025-10-02 17:26:53 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2024-2025, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
|
|
|
|
|
* Copyright (c) 2024-2025, Luke Wilde <luke@ladybird.org>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#define GL_GLEXT_PROTOTYPES 1
|
|
|
|
|
#include <GLES2/gl2.h>
|
|
|
|
|
#include <GLES2/gl2ext.h>
|
|
|
|
|
extern "C" {
|
|
|
|
|
#include <GLES2/gl2ext_angle.h>
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-23 13:07:35 +01:00
|
|
|
#include <LibGfx/ImmutableBitmap.h>
|
2025-10-23 20:24:14 +01:00
|
|
|
#include <LibGfx/SkiaUtils.h>
|
2025-10-02 17:26:53 +02:00
|
|
|
#include <LibWeb/HTML/HTMLCanvasElement.h>
|
|
|
|
|
#include <LibWeb/HTML/HTMLImageElement.h>
|
|
|
|
|
#include <LibWeb/HTML/HTMLVideoElement.h>
|
|
|
|
|
#include <LibWeb/HTML/ImageBitmap.h>
|
|
|
|
|
#include <LibWeb/HTML/ImageData.h>
|
|
|
|
|
#include <LibWeb/WebGL/OpenGLContext.h>
|
|
|
|
|
#include <LibWeb/WebGL/WebGLRenderingContextBase.h>
|
|
|
|
|
|
2025-10-23 20:24:14 +01:00
|
|
|
#include <core/SkCanvas.h>
|
2025-10-02 17:26:53 +02:00
|
|
|
#include <core/SkColorSpace.h>
|
|
|
|
|
#include <core/SkColorType.h>
|
|
|
|
|
#include <core/SkImage.h>
|
|
|
|
|
#include <core/SkPixmap.h>
|
2025-10-23 20:24:14 +01:00
|
|
|
#include <core/SkSurface.h>
|
2025-10-02 17:26:53 +02:00
|
|
|
|
|
|
|
|
namespace Web::WebGL {
|
|
|
|
|
|
2025-11-23 13:07:40 +01:00
|
|
|
static constexpr Optional<Gfx::ExportFormat> determine_export_format(WebIDL::UnsignedLong format, WebIDL::UnsignedLong type)
|
2025-10-02 17:26:53 +02:00
|
|
|
{
|
|
|
|
|
switch (format) {
|
|
|
|
|
case GL_RGB:
|
|
|
|
|
switch (type) {
|
|
|
|
|
case GL_UNSIGNED_BYTE:
|
2025-11-23 13:07:40 +01:00
|
|
|
return Gfx::ExportFormat::RGB888;
|
2025-10-02 17:26:53 +02:00
|
|
|
case GL_UNSIGNED_SHORT_5_6_5:
|
2025-11-23 13:07:40 +01:00
|
|
|
return Gfx::ExportFormat::RGB565;
|
2025-10-02 17:26:53 +02:00
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case GL_RGBA:
|
|
|
|
|
switch (type) {
|
|
|
|
|
case GL_UNSIGNED_BYTE:
|
2025-11-23 13:07:40 +01:00
|
|
|
return Gfx::ExportFormat::RGBA8888;
|
2025-10-02 17:26:53 +02:00
|
|
|
case GL_UNSIGNED_SHORT_4_4_4_4:
|
|
|
|
|
// FIXME: This is not exactly the same as RGBA.
|
2025-11-23 13:07:40 +01:00
|
|
|
return Gfx::ExportFormat::RGBA4444;
|
2025-10-02 17:26:53 +02:00
|
|
|
case GL_UNSIGNED_SHORT_5_5_5_1:
|
2025-11-23 13:07:40 +01:00
|
|
|
return Gfx::ExportFormat::RGBA5551;
|
2025-10-02 17:26:53 +02:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case GL_ALPHA:
|
|
|
|
|
switch (type) {
|
|
|
|
|
case GL_UNSIGNED_BYTE:
|
2025-11-23 13:07:40 +01:00
|
|
|
return Gfx::ExportFormat::Alpha8;
|
2025-10-02 17:26:53 +02:00
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case GL_LUMINANCE:
|
|
|
|
|
switch (type) {
|
|
|
|
|
case GL_UNSIGNED_BYTE:
|
2025-11-23 13:07:40 +01:00
|
|
|
return Gfx::ExportFormat::Gray8;
|
2025-10-02 17:26:53 +02:00
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dbgln("WebGL: Unsupported format and type combination. format: 0x{:04x}, type: 0x{:04x}", format, type);
|
2025-11-23 13:07:40 +01:00
|
|
|
return {};
|
2025-10-02 17:26:53 +02:00
|
|
|
}
|
|
|
|
|
|
2025-11-23 13:07:40 +01:00
|
|
|
Optional<Gfx::BitmapExportResult> WebGLRenderingContextBase::read_and_pixel_convert_texture_image_source(TexImageSource const& source, WebIDL::UnsignedLong format, WebIDL::UnsignedLong type, Optional<int> destination_width, Optional<int> destination_height)
|
2025-10-02 17:26:53 +02:00
|
|
|
{
|
|
|
|
|
// FIXME: If this function is called with an ImageData whose data attribute has been neutered,
|
|
|
|
|
// an INVALID_VALUE error is generated.
|
|
|
|
|
// FIXME: If this function is called with an ImageBitmap that has been neutered, an INVALID_VALUE
|
|
|
|
|
// error is generated.
|
|
|
|
|
// FIXME: If this function is called with an HTMLImageElement or HTMLVideoElement whose origin
|
|
|
|
|
// differs from the origin of the containing Document, or with an HTMLCanvasElement,
|
|
|
|
|
// ImageBitmap or OffscreenCanvas whose bitmap's origin-clean flag is set to false,
|
|
|
|
|
// a SECURITY_ERR exception must be thrown. See Origin Restrictions.
|
|
|
|
|
// FIXME: If source is null then an INVALID_VALUE error is generated.
|
|
|
|
|
auto bitmap = source.visit(
|
|
|
|
|
[](GC::Root<HTML::HTMLImageElement> const& source) -> RefPtr<Gfx::ImmutableBitmap> {
|
|
|
|
|
return source->immutable_bitmap();
|
|
|
|
|
},
|
|
|
|
|
[](GC::Root<HTML::HTMLCanvasElement> const& source) -> RefPtr<Gfx::ImmutableBitmap> {
|
|
|
|
|
auto surface = source->surface();
|
|
|
|
|
if (!surface)
|
2025-10-24 15:59:17 +01:00
|
|
|
return Gfx::ImmutableBitmap::create(*source->get_bitmap_from_surface());
|
|
|
|
|
return Gfx::ImmutableBitmap::create_snapshot_from_painting_surface(*surface);
|
2025-10-02 17:26:53 +02:00
|
|
|
},
|
|
|
|
|
[](GC::Root<HTML::OffscreenCanvas> const& source) -> RefPtr<Gfx::ImmutableBitmap> {
|
|
|
|
|
return Gfx::ImmutableBitmap::create(*source->bitmap());
|
|
|
|
|
},
|
|
|
|
|
[](GC::Root<HTML::HTMLVideoElement> const& source) -> RefPtr<Gfx::ImmutableBitmap> {
|
|
|
|
|
return Gfx::ImmutableBitmap::create(*source->bitmap());
|
|
|
|
|
},
|
|
|
|
|
[](GC::Root<HTML::ImageBitmap> const& source) -> RefPtr<Gfx::ImmutableBitmap> {
|
|
|
|
|
return Gfx::ImmutableBitmap::create(*source->bitmap());
|
|
|
|
|
},
|
|
|
|
|
[](GC::Root<HTML::ImageData> const& source) -> RefPtr<Gfx::ImmutableBitmap> {
|
|
|
|
|
return Gfx::ImmutableBitmap::create(source->bitmap());
|
|
|
|
|
});
|
|
|
|
|
if (!bitmap)
|
|
|
|
|
return OptionalNone {};
|
|
|
|
|
|
2025-11-23 13:07:40 +01:00
|
|
|
auto export_format = determine_export_format(format, type);
|
|
|
|
|
if (!export_format.has_value())
|
2025-10-02 17:26:53 +02:00
|
|
|
return OptionalNone {};
|
|
|
|
|
|
2025-11-23 13:07:40 +01:00
|
|
|
// FIXME: Respect unpackColorSpace
|
|
|
|
|
auto export_flags = 0;
|
|
|
|
|
if (m_unpack_flip_y && !source.has<GC::Root<HTML::ImageBitmap>>())
|
2025-10-28 16:03:13 +00:00
|
|
|
// 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.
|
2025-11-23 13:07:40 +01:00
|
|
|
export_flags |= Gfx::ExportFlags::FlipY;
|
|
|
|
|
if (m_unpack_premultiply_alpha)
|
|
|
|
|
export_flags |= Gfx::ExportFlags::PremultiplyAlpha;
|
2025-10-23 20:24:14 +01:00
|
|
|
|
2025-11-23 13:07:40 +01:00
|
|
|
auto result = bitmap->export_to_byte_buffer(export_format.value(), export_flags, destination_width, destination_height);
|
|
|
|
|
if (result.is_error()) {
|
|
|
|
|
dbgln("Could not export bitmap: {}", result.release_error());
|
|
|
|
|
return OptionalNone {};
|
2025-10-28 16:03:13 +00:00
|
|
|
}
|
2025-10-23 20:24:14 +01:00
|
|
|
|
2025-11-23 13:07:40 +01:00
|
|
|
return result.release_value();
|
2025-10-02 17:26:53 +02:00
|
|
|
}
|
|
|
|
|
|
2025-11-05 20:25:48 +01:00
|
|
|
// TODO: The glGetError spec allows for queueing errors which is something we should probably do, for now
|
|
|
|
|
// this just keeps track of one error which is also fine by the spec
|
|
|
|
|
GLenum WebGLRenderingContextBase::get_error_value()
|
|
|
|
|
{
|
|
|
|
|
if (m_error == GL_NO_ERROR)
|
|
|
|
|
return glGetError();
|
|
|
|
|
|
|
|
|
|
auto error = m_error;
|
|
|
|
|
m_error = GL_NO_ERROR;
|
|
|
|
|
return error;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-05 20:15:09 +01:00
|
|
|
void WebGLRenderingContextBase::set_error(GLenum error)
|
|
|
|
|
{
|
2025-11-05 20:25:48 +01:00
|
|
|
if (m_error != GL_NO_ERROR)
|
|
|
|
|
return;
|
|
|
|
|
|
2025-11-05 20:15:09 +01:00
|
|
|
auto context_error = glGetError();
|
|
|
|
|
if (context_error != GL_NO_ERROR)
|
|
|
|
|
m_error = context_error;
|
|
|
|
|
else
|
|
|
|
|
m_error = error;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-02 17:26:53 +02:00
|
|
|
}
|