mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-20 19:45:12 +00:00
LibWeb: Generate WebGLRenderingContext implementation
This commit is contained in:
parent
45e0f50463
commit
e2e54dccc3
Notes:
github-actions[bot]
2024-12-03 22:37:17 +00:00
Author: https://github.com/kalenikaliaksandr Commit: https://github.com/LadybirdBrowser/ladybird/commit/e2e54dccc32 Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2688 Reviewed-by: https://github.com/ADKaster ✅
10 changed files with 316 additions and 261 deletions
|
@ -853,6 +853,18 @@ invoke_generator(
|
|||
|
||||
generate_css_implementation()
|
||||
|
||||
invoke_generator(
|
||||
"WebGLRenderingContextImpl.cpp"
|
||||
Lagom::GenerateWebGLRenderingContext
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/WebGL/WebGLRenderingContextBase.idl"
|
||||
"WebGL/WebGLRenderingContextImpl.h"
|
||||
"WebGL/WebGLRenderingContextImpl.cpp"
|
||||
arguments -b "${CMAKE_CURRENT_SOURCE_DIR}" -b "${CMAKE_CURRENT_BINARY_DIR}" -i "${CMAKE_CURRENT_SOURCE_DIR}/WebGL/WebGLRenderingContext.idl"
|
||||
# NOTE: GeneratedCSSStyleProperties.idl is listed because it's transitively included by WebGLRenderingContextBase.idl
|
||||
# and we need to make sure it's generated before we generate the WebGLRenderingContext implementation.
|
||||
dependencies WebGL/WebGLRenderingContextBase.idl WebGL/WebGLRenderingContextOverloads.idl CSS/GeneratedCSSStyleProperties.idl
|
||||
)
|
||||
|
||||
set(GENERATED_SOURCES
|
||||
ARIA/AriaRoles.cpp
|
||||
CSS/DefaultStyleSheetSource.cpp
|
||||
|
@ -868,6 +880,7 @@ set(GENERATED_SOURCES
|
|||
CSS/TransformFunctions.cpp
|
||||
MathML/MathMLStyleSheetSource.cpp
|
||||
SVG/SVGStyleSheetSource.cpp
|
||||
WebGL/WebGLRenderingContextImpl.cpp
|
||||
Worker/WebWorkerClientEndpoint.h
|
||||
Worker/WebWorkerServerEndpoint.h
|
||||
)
|
||||
|
|
|
@ -831,9 +831,18 @@ struct OscillatorOptions;
|
|||
}
|
||||
|
||||
namespace Web::WebGL {
|
||||
class OpenGLContext;
|
||||
class WebGLActiveInfo;
|
||||
class WebGLBuffer;
|
||||
class WebGLContextEvent;
|
||||
class WebGLFramebuffer;
|
||||
class WebGLObject;
|
||||
class WebGLProgram;
|
||||
class WebGLRenderbuffer;
|
||||
class WebGLRenderingContext;
|
||||
class WebGLRenderingContextBase;
|
||||
class WebGLShader;
|
||||
class WebGLTexture;
|
||||
class WebGLUniformLocation;
|
||||
}
|
||||
|
||||
namespace Web::WebIDL {
|
||||
|
|
|
@ -10,7 +10,6 @@
|
|||
#include <LibGfx/Forward.h>
|
||||
#include <LibGfx/PaintingSurface.h>
|
||||
#include <LibWeb/HTML/HTMLElement.h>
|
||||
#include <LibWeb/WebGL/WebGLRenderingContext.h>
|
||||
#include <LibWeb/WebIDL/Types.h>
|
||||
|
||||
namespace Web::HTML {
|
||||
|
|
|
@ -13,9 +13,8 @@
|
|||
#include <LibWeb/HTML/TraversableNavigable.h>
|
||||
#include <LibWeb/Painting/Paintable.h>
|
||||
#include <LibWeb/WebGL/EventNames.h>
|
||||
#include <LibWeb/WebGL/WebGLBuffer.h>
|
||||
#include <LibWeb/WebGL/OpenGLContext.h>
|
||||
#include <LibWeb/WebGL/WebGLContextEvent.h>
|
||||
#include <LibWeb/WebGL/WebGLProgram.h>
|
||||
#include <LibWeb/WebGL/WebGLRenderingContext.h>
|
||||
#include <LibWeb/WebGL/WebGLShader.h>
|
||||
#include <LibWeb/WebIDL/Buffers.h>
|
||||
|
@ -27,13 +26,6 @@ namespace Web::WebGL {
|
|||
|
||||
GC_DEFINE_ALLOCATOR(WebGLRenderingContext);
|
||||
|
||||
#define RETURN_WITH_WEBGL_ERROR_IF(condition, error) \
|
||||
if (condition) { \
|
||||
dbgln_if(WEBGL_CONTEXT_DEBUG, "{}(): error {:#x}", __func__, error); \
|
||||
set_error(error); \
|
||||
return; \
|
||||
}
|
||||
|
||||
// https://www.khronos.org/registry/webgl/specs/latest/1.0/#fire-a-webgl-context-event
|
||||
static void fire_webgl_context_event(HTML::HTMLCanvasElement& canvas_element, FlyString const& type)
|
||||
{
|
||||
|
@ -59,7 +51,6 @@ JS::ThrowCompletionOr<GC::Ptr<WebGLRenderingContext>> WebGLRenderingContext::cre
|
|||
|
||||
auto skia_backend_context = canvas_element.navigable()->traversable_navigable()->skia_backend_context();
|
||||
auto context = OpenGLContext::create(*skia_backend_context);
|
||||
|
||||
if (!context) {
|
||||
fire_webgl_context_creation_error(canvas_element);
|
||||
return GC::Ptr<WebGLRenderingContext> { nullptr };
|
||||
|
@ -72,7 +63,7 @@ JS::ThrowCompletionOr<GC::Ptr<WebGLRenderingContext>> WebGLRenderingContext::cre
|
|||
|
||||
WebGLRenderingContext::WebGLRenderingContext(JS::Realm& realm, HTML::HTMLCanvasElement& canvas_element, NonnullOwnPtr<OpenGLContext> context, WebGLContextAttributes context_creation_parameters, WebGLContextAttributes actual_context_parameters)
|
||||
: PlatformObject(realm)
|
||||
, m_context(move(context))
|
||||
, WebGLRenderingContextImpl(realm, move(context))
|
||||
, m_canvas_element(canvas_element)
|
||||
, m_context_creation_parameters(context_creation_parameters)
|
||||
, m_actual_context_parameters(actual_context_parameters)
|
||||
|
@ -107,7 +98,7 @@ void WebGLRenderingContext::present()
|
|||
// This default behavior can be changed by setting the preserveDrawingBuffer attribute of the WebGLContextAttributes object.
|
||||
// If this flag is true, the contents of the drawing buffer shall be preserved until the author either clears or overwrites them."
|
||||
if (!m_context_creation_parameters.preserve_drawing_buffer) {
|
||||
m_context->clear_buffer_to_default_values();
|
||||
context().clear_buffer_to_default_values();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -142,7 +133,7 @@ bool WebGLRenderingContext::is_context_lost() const
|
|||
|
||||
void WebGLRenderingContext::set_size(Gfx::IntSize const& size)
|
||||
{
|
||||
m_context->set_size(size);
|
||||
context().set_size(size);
|
||||
}
|
||||
|
||||
void WebGLRenderingContext::reset_to_default_state()
|
||||
|
@ -151,213 +142,12 @@ void WebGLRenderingContext::reset_to_default_state()
|
|||
|
||||
RefPtr<Gfx::PaintingSurface> WebGLRenderingContext::surface()
|
||||
{
|
||||
return m_context->surface();
|
||||
return context().surface();
|
||||
}
|
||||
|
||||
void WebGLRenderingContext::allocate_painting_surface_if_needed()
|
||||
{
|
||||
m_context->allocate_painting_surface_if_needed();
|
||||
}
|
||||
|
||||
Optional<Vector<String>> WebGLRenderingContext::get_supported_extensions() const
|
||||
{
|
||||
if (m_context_lost)
|
||||
return Optional<Vector<String>> {};
|
||||
|
||||
dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContext::get_supported_extensions()");
|
||||
|
||||
// FIXME: We don't currently support any extensions.
|
||||
return Vector<String> {};
|
||||
}
|
||||
|
||||
JS::Object* WebGLRenderingContext::get_extension(String const& name) const
|
||||
{
|
||||
if (m_context_lost)
|
||||
return nullptr;
|
||||
|
||||
dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContext::get_extension(name='{}')", name);
|
||||
|
||||
// FIXME: We don't currently support any extensions.
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void WebGLRenderingContext::active_texture(GLenum texture)
|
||||
{
|
||||
if (m_context_lost)
|
||||
return;
|
||||
|
||||
dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContext::active_texture(texture={:#08x})", texture);
|
||||
}
|
||||
|
||||
void WebGLRenderingContext::clear(GLbitfield mask)
|
||||
{
|
||||
if (m_context_lost)
|
||||
return;
|
||||
|
||||
dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContext::clear(mask={:#08x})", mask);
|
||||
|
||||
// FIXME: This should only be done if this is targeting the front buffer.
|
||||
needs_to_present();
|
||||
}
|
||||
|
||||
void WebGLRenderingContext::clear_color(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)
|
||||
{
|
||||
if (m_context_lost)
|
||||
return;
|
||||
|
||||
dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContext::clear_color(red={}, green={}, blue={}, alpha={})", red, green, blue, alpha);
|
||||
}
|
||||
|
||||
void WebGLRenderingContext::clear_depth(GLclampf depth)
|
||||
{
|
||||
if (m_context_lost)
|
||||
return;
|
||||
|
||||
dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContext::clear_depth(depth={})", depth);
|
||||
}
|
||||
|
||||
void WebGLRenderingContext::clear_stencil(GLint s)
|
||||
{
|
||||
if (m_context_lost)
|
||||
return;
|
||||
|
||||
dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContext::clear_stencil(s={:#08x})", s);
|
||||
}
|
||||
|
||||
void WebGLRenderingContext::color_mask(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha)
|
||||
{
|
||||
if (m_context_lost)
|
||||
return;
|
||||
|
||||
dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContext::color_mask(red={}, green={}, blue={}, alpha={})", red, green, blue, alpha);
|
||||
}
|
||||
|
||||
void WebGLRenderingContext::cull_face(GLenum mode)
|
||||
{
|
||||
if (m_context_lost)
|
||||
return;
|
||||
|
||||
dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContext::cull_face(mode={:#08x})", mode);
|
||||
}
|
||||
|
||||
void WebGLRenderingContext::depth_func(GLenum func)
|
||||
{
|
||||
if (m_context_lost)
|
||||
return;
|
||||
|
||||
dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContext::depth_func(func={:#08x})", func);
|
||||
}
|
||||
|
||||
void WebGLRenderingContext::depth_mask(GLboolean mask)
|
||||
{
|
||||
if (m_context_lost)
|
||||
return;
|
||||
|
||||
dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContext::depth_mask(mask={})", mask);
|
||||
}
|
||||
|
||||
void WebGLRenderingContext::depth_range(GLclampf z_near, GLclampf z_far)
|
||||
{
|
||||
if (m_context_lost)
|
||||
return;
|
||||
|
||||
dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContext::depth_range(z_near={}, z_far={})", z_near, z_far);
|
||||
|
||||
// https://www.khronos.org/registry/webgl/specs/latest/1.0/#VIEWPORT_DEPTH_RANGE
|
||||
// "The WebGL API does not support depth ranges with where the near plane is mapped to a value greater than that of the far plane. A call to depthRange will generate an INVALID_OPERATION error if zNear is greater than zFar."
|
||||
RETURN_WITH_WEBGL_ERROR_IF(z_near > z_far, GL_INVALID_OPERATION);
|
||||
}
|
||||
|
||||
void WebGLRenderingContext::finish()
|
||||
{
|
||||
if (m_context_lost)
|
||||
return;
|
||||
|
||||
dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContext::finish()");
|
||||
}
|
||||
|
||||
void WebGLRenderingContext::flush()
|
||||
{
|
||||
if (m_context_lost)
|
||||
return;
|
||||
|
||||
dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContext::flush()");
|
||||
}
|
||||
|
||||
void WebGLRenderingContext::front_face(GLenum mode)
|
||||
{
|
||||
if (m_context_lost)
|
||||
return;
|
||||
|
||||
dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContext::front_face(mode={:#08x})", mode);
|
||||
}
|
||||
|
||||
GLenum WebGLRenderingContext::get_error()
|
||||
{
|
||||
dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContext::get_error()");
|
||||
|
||||
// "If the context's webgl context lost flag is set, returns CONTEXT_LOST_WEBGL the first time this method is called. Afterward, returns NO_ERROR until the context has been restored."
|
||||
// FIXME: The plan here is to make the context lost handler unconditionally set m_error to CONTEXT_LOST_WEBGL, which we currently do not have.
|
||||
// The idea for the unconditional set is that any potentially error generating functions will not execute when the context is lost.
|
||||
if (m_error != GL_NO_ERROR || m_context_lost) {
|
||||
auto last_error = m_error;
|
||||
m_error = GL_NO_ERROR;
|
||||
return last_error;
|
||||
}
|
||||
|
||||
return glGetError();
|
||||
}
|
||||
|
||||
void WebGLRenderingContext::line_width(GLfloat width)
|
||||
{
|
||||
if (m_context_lost)
|
||||
return;
|
||||
|
||||
dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContext::line_width(width={})", width);
|
||||
|
||||
// https://www.khronos.org/registry/webgl/specs/latest/1.0/#NAN_LINE_WIDTH
|
||||
// "In the WebGL API, if the width parameter passed to lineWidth is set to NaN, an INVALID_VALUE error is generated and the line width is not changed."
|
||||
RETURN_WITH_WEBGL_ERROR_IF(isnan(width), GL_INVALID_VALUE);
|
||||
}
|
||||
|
||||
void WebGLRenderingContext::polygon_offset(GLfloat factor, GLfloat units)
|
||||
{
|
||||
if (m_context_lost)
|
||||
return;
|
||||
|
||||
dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContext::polygon_offset(factor={}, units={})", factor, units);
|
||||
}
|
||||
|
||||
void WebGLRenderingContext::scissor(GLint x, GLint y, GLsizei width, GLsizei height)
|
||||
{
|
||||
if (m_context_lost)
|
||||
return;
|
||||
|
||||
dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContext::scissor(x={}, y={}, width={}, height={})", x, y, width, height);
|
||||
}
|
||||
|
||||
void WebGLRenderingContext::stencil_op(GLenum fail, GLenum zfail, GLenum zpass)
|
||||
{
|
||||
if (m_context_lost)
|
||||
return;
|
||||
|
||||
dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContext::stencil_op(fail={:#08x}, zfail={:#08x}, zpass={:#08x})", fail, zfail, zpass);
|
||||
}
|
||||
|
||||
void WebGLRenderingContext::stencil_op_separate(GLenum face, GLenum fail, GLenum zfail, GLenum zpass)
|
||||
{
|
||||
if (m_context_lost)
|
||||
return;
|
||||
|
||||
dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContext::stencil_op_separate(face={:#08x}, fail={:#08x}, zfail={:#08x}, zpass={:#08x})", face, fail, zfail, zpass);
|
||||
}
|
||||
|
||||
void WebGLRenderingContext::viewport(GLint x, GLint y, GLsizei width, GLsizei height)
|
||||
{
|
||||
if (m_context_lost)
|
||||
return;
|
||||
|
||||
dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContext::viewport(x={}, y={}, width={}, height={})", x, y, width, height);
|
||||
context().allocate_painting_surface_if_needed();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -10,13 +10,14 @@
|
|||
#include <LibGC/Ptr.h>
|
||||
#include <LibWeb/Bindings/PlatformObject.h>
|
||||
#include <LibWeb/Forward.h>
|
||||
#include <LibWeb/WebGL/OpenGLContext.h>
|
||||
#include <LibWeb/WebGL/Types.h>
|
||||
#include <LibWeb/WebGL/WebGLContextAttributes.h>
|
||||
#include <LibWeb/WebGL/WebGLRenderingContextImpl.h>
|
||||
|
||||
namespace Web::WebGL {
|
||||
|
||||
class WebGLRenderingContext : public Bindings::PlatformObject {
|
||||
class WebGLRenderingContext : public Bindings::PlatformObject
|
||||
, public WebGLRenderingContextImpl {
|
||||
WEB_PLATFORM_OBJECT(WebGLRenderingContext, Bindings::PlatformObject);
|
||||
GC_DECLARE_ALLOCATOR(WebGLRenderingContext);
|
||||
|
||||
|
@ -25,8 +26,8 @@ public:
|
|||
|
||||
virtual ~WebGLRenderingContext() override;
|
||||
|
||||
void present();
|
||||
void needs_to_present();
|
||||
void present() override;
|
||||
void needs_to_present() override;
|
||||
|
||||
GC::Ref<HTML::HTMLCanvasElement> canvas_for_binding() const;
|
||||
|
||||
|
@ -38,40 +39,6 @@ public:
|
|||
void set_size(Gfx::IntSize const&);
|
||||
void reset_to_default_state();
|
||||
|
||||
Optional<Vector<String>> get_supported_extensions() const;
|
||||
JS::Object* get_extension(String const& name) const;
|
||||
|
||||
void active_texture(GLenum texture);
|
||||
|
||||
void clear(GLbitfield mask);
|
||||
void clear_color(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha);
|
||||
void clear_depth(GLclampf depth);
|
||||
void clear_stencil(GLint s);
|
||||
void color_mask(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha);
|
||||
|
||||
void cull_face(GLenum mode);
|
||||
|
||||
void depth_func(GLenum func);
|
||||
void depth_mask(GLboolean mask);
|
||||
void depth_range(GLclampf z_near, GLclampf z_far);
|
||||
|
||||
void finish();
|
||||
void flush();
|
||||
|
||||
void front_face(GLenum mode);
|
||||
|
||||
GLenum get_error();
|
||||
|
||||
void line_width(GLfloat width);
|
||||
void polygon_offset(GLfloat factor, GLfloat units);
|
||||
|
||||
void scissor(GLint x, GLint y, GLsizei width, GLsizei height);
|
||||
|
||||
void stencil_op(GLenum fail, GLenum zfail, GLenum zpass);
|
||||
void stencil_op_separate(GLenum face, GLenum fail, GLenum zfail, GLenum zpass);
|
||||
|
||||
void viewport(GLint x, GLint y, GLsizei width, GLsizei height);
|
||||
|
||||
private:
|
||||
virtual void initialize(JS::Realm&) override;
|
||||
|
||||
|
@ -79,8 +46,6 @@ private:
|
|||
|
||||
virtual void visit_edges(Cell::Visitor&) override;
|
||||
|
||||
OwnPtr<OpenGLContext> m_context;
|
||||
|
||||
GC::Ref<HTML::HTMLCanvasElement> m_canvas_element;
|
||||
|
||||
// https://www.khronos.org/registry/webgl/specs/latest/1.0/#context-creation-parameters
|
||||
|
|
|
@ -30,10 +30,10 @@ interface mixin WebGLRenderingContextBase {
|
|||
[FIXME] attribute PredefinedColorSpace unpackColorSpace;
|
||||
|
||||
[FIXME] WebGLContextAttributes? getContextAttributes();
|
||||
boolean isContextLost();
|
||||
[FIXME] boolean isContextLost();
|
||||
|
||||
sequence<DOMString>? getSupportedExtensions();
|
||||
object? getExtension(DOMString name);
|
||||
[FIXME] sequence<DOMString>? getSupportedExtensions();
|
||||
[FIXME] object? getExtension(DOMString name);
|
||||
|
||||
undefined activeTexture(GLenum texture);
|
||||
[FIXME] undefined attachShader(WebGLProgram program, WebGLShader shader);
|
||||
|
|
|
@ -105,7 +105,16 @@ static bool is_platform_object(Type const& type)
|
|||
"VTTRegion"sv,
|
||||
"VideoTrack"sv,
|
||||
"VideoTrackList"sv,
|
||||
"WebGLActiveInfo"sv,
|
||||
"WebGLBuffer"sv,
|
||||
"WebGLFramebuffer"sv,
|
||||
"WebGLObject"sv,
|
||||
"WebGLProgram"sv,
|
||||
"WebGLRenderbuffer"sv,
|
||||
"WebGLRenderingContext"sv,
|
||||
"WebGLShader"sv,
|
||||
"WebGLTexture"sv,
|
||||
"WebGLUniformLocation"sv,
|
||||
"Window"sv,
|
||||
"WindowProxy"sv,
|
||||
"WritableStream"sv,
|
||||
|
|
|
@ -28,6 +28,8 @@ void generate_iterator_prototype_implementation(IDL::Interface const&, StringBui
|
|||
void generate_global_mixin_header(IDL::Interface const&, StringBuilder&);
|
||||
void generate_global_mixin_implementation(IDL::Interface const&, StringBuilder&);
|
||||
|
||||
CppType idl_type_name_to_cpp_type(Type const& type, Interface const& interface);
|
||||
|
||||
extern Vector<StringView> g_header_search_paths;
|
||||
|
||||
}
|
||||
|
|
|
@ -10,5 +10,6 @@ lagom_tool(GenerateCSSStyleProperties SOURCES GenerateCSSStyleProperties.c
|
|||
lagom_tool(GenerateCSSTransformFunctions SOURCES GenerateCSSTransformFunctions.cpp LIBS LibMain)
|
||||
lagom_tool(GenerateWindowOrWorkerInterfaces SOURCES GenerateWindowOrWorkerInterfaces.cpp LIBS LibMain LibIDL)
|
||||
lagom_tool(GenerateAriaRoles SOURCES GenerateAriaRoles.cpp LIBS LibMain)
|
||||
lagom_tool(GenerateWebGLRenderingContext SOURCES GenerateWebGLRenderingContext.cpp BindingsGenerator/IDLGenerators.cpp LIBS LibMain LibIDL)
|
||||
|
||||
add_subdirectory(BindingsGenerator)
|
||||
|
|
|
@ -0,0 +1,267 @@
|
|||
/*
|
||||
* Copyright (c) 2024, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include "BindingsGenerator/IDLGenerators.h"
|
||||
#include <AK/SourceGenerator.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <LibCore/ArgsParser.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <LibIDL/IDLParser.h>
|
||||
#include <LibMain/Main.h>
|
||||
|
||||
static bool is_webgl_object_type(StringView type_name)
|
||||
{
|
||||
return type_name == "WebGLShader"sv
|
||||
|| type_name == "WebGLBuffer"sv
|
||||
|| type_name == "WebGLFramebuffer"sv
|
||||
|| type_name == "WebGLProgram"sv
|
||||
|| type_name == "WebGLRenderbuffer"sv
|
||||
|| type_name == "WebGLTexture"sv
|
||||
|| type_name == "WebGLUniformLocation"sv;
|
||||
}
|
||||
|
||||
static bool gl_function_modifies_framebuffer(StringView function_name)
|
||||
{
|
||||
return function_name == "clearColor"sv || function_name == "drawArrays"sv || function_name == "drawElements"sv;
|
||||
}
|
||||
|
||||
static ByteString to_cpp_type(const IDL::Type& type, const IDL::Interface& interface)
|
||||
{
|
||||
if (type.name() == "undefined"sv)
|
||||
return "void"sv;
|
||||
if (type.name() == "object"sv) {
|
||||
if (type.is_nullable())
|
||||
return "JS::Object*"sv;
|
||||
return "JS::Object&"sv;
|
||||
}
|
||||
auto cpp_type = idl_type_name_to_cpp_type(type, interface);
|
||||
return cpp_type.name;
|
||||
}
|
||||
|
||||
static ByteString idl_to_gl_function_name(StringView function_name)
|
||||
{
|
||||
StringBuilder gl_function_name_builder;
|
||||
gl_function_name_builder.append("gl"sv);
|
||||
for (size_t i = 0; i < function_name.length(); ++i) {
|
||||
if (i == 0) {
|
||||
gl_function_name_builder.append(to_ascii_uppercase(function_name[i]));
|
||||
} else {
|
||||
gl_function_name_builder.append(function_name[i]);
|
||||
}
|
||||
}
|
||||
if (function_name == "clearDepth"sv || function_name == "depthRange"sv) {
|
||||
gl_function_name_builder.append("f"sv);
|
||||
}
|
||||
return gl_function_name_builder.to_byte_string();
|
||||
}
|
||||
|
||||
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||
{
|
||||
StringView generated_header_path;
|
||||
StringView generated_implementation_path;
|
||||
Vector<ByteString> base_paths;
|
||||
StringView webgl_context_idl_path;
|
||||
|
||||
Core::ArgsParser args_parser;
|
||||
args_parser.add_option(webgl_context_idl_path, "Path to the WebGLRenderingContext.idl file", "webgl-idl-path", 'i', "webgl-idl-path");
|
||||
args_parser.add_option(Core::ArgsParser::Option {
|
||||
.argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
|
||||
.help_string = "Path to root of IDL file tree(s)",
|
||||
.long_name = "base-path",
|
||||
.short_name = 'b',
|
||||
.value_name = "base-path",
|
||||
.accept_value = [&](StringView s) {
|
||||
base_paths.append(s);
|
||||
return true;
|
||||
},
|
||||
});
|
||||
args_parser.add_option(generated_header_path, "Path to the Enums header file to generate", "generated-header-path", 'h', "generated-header-path");
|
||||
args_parser.add_option(generated_implementation_path, "Path to the Enums implementation file to generate", "generated-implementation-path", 'c', "generated-implementation-path");
|
||||
args_parser.parse(arguments);
|
||||
|
||||
auto generated_header_file = TRY(Core::File::open(generated_header_path, Core::File::OpenMode::Write));
|
||||
auto generated_implementation_file = TRY(Core::File::open(generated_implementation_path, Core::File::OpenMode::Write));
|
||||
|
||||
auto idl_file = MUST(Core::File::open(webgl_context_idl_path, Core::File::OpenMode::Read));
|
||||
auto webgl_context_idl_file_content = MUST(idl_file->read_until_eof());
|
||||
|
||||
Vector<ByteString> import_base_paths;
|
||||
for (auto const& base_path : base_paths) {
|
||||
VERIFY(!base_path.is_empty());
|
||||
import_base_paths.append(base_path);
|
||||
}
|
||||
|
||||
IDL::Parser parser(webgl_context_idl_path, StringView(webgl_context_idl_file_content), import_base_paths);
|
||||
auto const& interface = parser.parse();
|
||||
|
||||
StringBuilder header_file_string_builder;
|
||||
SourceGenerator header_file_generator { header_file_string_builder };
|
||||
|
||||
StringBuilder implementation_file_string_builder;
|
||||
SourceGenerator implementation_file_generator { implementation_file_string_builder };
|
||||
|
||||
implementation_file_generator.append(R"~~~(
|
||||
#include <LibJS/Runtime/ArrayBuffer.h>
|
||||
#include <LibJS/Runtime/TypedArray.h>
|
||||
#include <LibWeb/WebGL/OpenGLContext.h>
|
||||
#include <LibWeb/WebGL/WebGLActiveInfo.h>
|
||||
#include <LibWeb/WebGL/WebGLBuffer.h>
|
||||
#include <LibWeb/WebGL/WebGLFramebuffer.h>
|
||||
#include <LibWeb/WebGL/WebGLProgram.h>
|
||||
#include <LibWeb/WebGL/WebGLRenderbuffer.h>
|
||||
#include <LibWeb/WebGL/WebGLRenderingContextImpl.h>
|
||||
#include <LibWeb/WebGL/WebGLShader.h>
|
||||
#include <LibWeb/WebGL/WebGLTexture.h>
|
||||
#include <LibWeb/WebGL/WebGLUniformLocation.h>
|
||||
#include <LibWeb/WebIDL/Buffers.h>
|
||||
|
||||
#include <GLES2/gl2.h>
|
||||
#include <GLES2/gl2ext.h>
|
||||
|
||||
namespace Web::WebGL {
|
||||
|
||||
WebGLRenderingContextImpl::WebGLRenderingContextImpl(JS::Realm& realm, NonnullOwnPtr<OpenGLContext> context)
|
||||
: m_realm(realm)
|
||||
, m_context(move(context))
|
||||
{
|
||||
}
|
||||
|
||||
)~~~");
|
||||
|
||||
header_file_generator.append(R"~~~(
|
||||
#pragma once
|
||||
|
||||
#include <AK/NonnullOwnPtr.h>
|
||||
#include <LibGC/Ptr.h>
|
||||
#include <LibGfx/Bitmap.h>
|
||||
#include <LibWeb/Bindings/PlatformObject.h>
|
||||
#include <LibWeb/Forward.h>
|
||||
#include <LibWeb/HTML/HTMLCanvasElement.h>
|
||||
#include <LibWeb/HTML/HTMLImageElement.h>
|
||||
#include <LibWeb/WebIDL/Types.h>
|
||||
|
||||
namespace Web::WebGL {
|
||||
|
||||
using namespace Web::HTML;
|
||||
|
||||
class WebGLRenderingContextImpl {
|
||||
public:
|
||||
WebGLRenderingContextImpl(JS::Realm&, NonnullOwnPtr<OpenGLContext>);
|
||||
|
||||
OpenGLContext& context() { return *m_context; }
|
||||
|
||||
virtual void present() = 0;
|
||||
virtual void needs_to_present() = 0;
|
||||
)~~~");
|
||||
|
||||
for (auto const& function : interface.functions) {
|
||||
if (function.extended_attributes.contains("FIXME")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
StringBuilder function_declaration;
|
||||
|
||||
StringBuilder function_parameters;
|
||||
for (size_t i = 0; i < function.parameters.size(); ++i) {
|
||||
auto const& parameter = function.parameters[i];
|
||||
function_parameters.append(to_cpp_type(*parameter.type, interface));
|
||||
function_parameters.append(" "sv);
|
||||
function_parameters.append(parameter.name);
|
||||
if (i != function.parameters.size() - 1) {
|
||||
function_parameters.append(", "sv);
|
||||
}
|
||||
}
|
||||
|
||||
auto function_name = function.name.to_snakecase();
|
||||
function_declaration.append(to_cpp_type(*function.return_type, interface));
|
||||
function_declaration.append(" "sv);
|
||||
function_declaration.append(function_name);
|
||||
function_declaration.append("("sv);
|
||||
|
||||
function_declaration.append(function_parameters.string_view());
|
||||
function_declaration.append(");"sv);
|
||||
|
||||
header_file_generator.append(" "sv);
|
||||
header_file_generator.append(function_declaration.string_view());
|
||||
header_file_generator.append("\n"sv);
|
||||
|
||||
StringBuilder function_impl;
|
||||
SourceGenerator function_impl_generator { function_impl };
|
||||
|
||||
ScopeGuard function_guard { [&] {
|
||||
function_impl_generator.append("}\n"sv);
|
||||
implementation_file_generator.append(function_impl_generator.as_string_view().bytes());
|
||||
} };
|
||||
|
||||
function_impl_generator.set("function_name", function_name);
|
||||
function_impl_generator.set("function_parameters", function_parameters.string_view());
|
||||
function_impl_generator.set("function_return_type", to_cpp_type(*function.return_type, interface));
|
||||
function_impl_generator.append(R"~~~(
|
||||
@function_return_type@ WebGLRenderingContextImpl::@function_name@(@function_parameters@)
|
||||
{
|
||||
m_context->make_current();
|
||||
)~~~");
|
||||
|
||||
Vector<ByteString> gl_call_arguments;
|
||||
for (size_t i = 0; i < function.parameters.size(); ++i) {
|
||||
auto const& parameter = function.parameters[i];
|
||||
if (parameter.type->is_numeric() || parameter.type->is_boolean()) {
|
||||
gl_call_arguments.append(parameter.name);
|
||||
continue;
|
||||
}
|
||||
if (parameter.type->is_string()) {
|
||||
gl_call_arguments.append(ByteString::formatted("{}", parameter.name));
|
||||
continue;
|
||||
}
|
||||
if (is_webgl_object_type(parameter.type->name())) {
|
||||
gl_call_arguments.append(ByteString::formatted("{} ? {}->handle() : 0", parameter.name, parameter.name));
|
||||
continue;
|
||||
}
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
StringBuilder gl_call_arguments_string_builder;
|
||||
gl_call_arguments_string_builder.join(", "sv, gl_call_arguments);
|
||||
|
||||
auto gl_call_string = ByteString::formatted("{}({})", idl_to_gl_function_name(function.name), gl_call_arguments_string_builder.string_view());
|
||||
function_impl_generator.set("call_string", gl_call_string);
|
||||
|
||||
if (gl_function_modifies_framebuffer(function.name)) {
|
||||
function_impl_generator.append(" needs_to_present();\n"sv);
|
||||
}
|
||||
|
||||
if (function.return_type->name() == "undefined"sv) {
|
||||
function_impl_generator.append(" @call_string@;"sv);
|
||||
} else if (function.return_type->is_integer() || function.return_type->is_boolean()) {
|
||||
function_impl_generator.append(" return @call_string@;"sv);
|
||||
} else if (is_webgl_object_type(function.return_type->name())) {
|
||||
function_impl_generator.set("return_type_name", function.return_type->name());
|
||||
function_impl_generator.append(" return @return_type_name@::create(m_realm, @call_string@);"sv);
|
||||
} else {
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
function_impl_generator.append("\n"sv);
|
||||
}
|
||||
|
||||
header_file_generator.append(R"~~~(
|
||||
private:
|
||||
GC::Ref<JS::Realm> m_realm;
|
||||
NonnullOwnPtr<OpenGLContext> m_context;
|
||||
};
|
||||
|
||||
}
|
||||
)~~~");
|
||||
|
||||
implementation_file_generator.append(R"~~~(
|
||||
}
|
||||
)~~~");
|
||||
|
||||
MUST(generated_header_file->write_until_depleted(header_file_generator.as_string_view().bytes()));
|
||||
MUST(generated_implementation_file->write_until_depleted(implementation_file_generator.as_string_view().bytes()));
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Reference in a new issue