diff options
author | Andreas Kling <kling@serenityos.org> | 2022-09-02 15:53:02 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-09-06 00:27:09 +0200 |
commit | 4452b5ca098a10e31eab5c49837dfcb107fe1414 (patch) | |
tree | c0d13e7dde3db45280f408f268834c4ad43a9f6a /Userland/Libraries/LibWeb/WebGL | |
parent | b8d485e6f074ce906473765eb1cf761c9b9a4a91 (diff) | |
download | serenity-4452b5ca098a10e31eab5c49837dfcb107fe1414.zip |
LibWeb: Make 2D and 3D canvas rendering contexts GC-allocated
Diffstat (limited to 'Userland/Libraries/LibWeb/WebGL')
4 files changed, 40 insertions, 23 deletions
diff --git a/Userland/Libraries/LibWeb/WebGL/WebGLRenderingContext.cpp b/Userland/Libraries/LibWeb/WebGL/WebGLRenderingContext.cpp index 5848200053..3fea5c92ed 100644 --- a/Userland/Libraries/LibWeb/WebGL/WebGLRenderingContext.cpp +++ b/Userland/Libraries/LibWeb/WebGL/WebGLRenderingContext.cpp @@ -4,9 +4,11 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include <LibWeb/Bindings/WebGLRenderingContextPrototype.h> #include <LibWeb/Bindings/Wrapper.h> #include <LibWeb/DOM/Document.h> #include <LibWeb/HTML/HTMLCanvasElement.h> +#include <LibWeb/HTML/Window.h> #include <LibWeb/WebGL/WebGLContextEvent.h> #include <LibWeb/WebGL/WebGLRenderingContext.h> @@ -30,7 +32,7 @@ static void fire_webgl_context_creation_error(HTML::HTMLCanvasElement& canvas_el fire_webgl_context_event(canvas_element, "webglcontextcreationerror"sv); } -JS::ThrowCompletionOr<RefPtr<WebGLRenderingContext>> WebGLRenderingContext::create(HTML::HTMLCanvasElement& canvas_element, JS::Value options) +JS::ThrowCompletionOr<JS::GCPtr<WebGLRenderingContext>> WebGLRenderingContext::create(HTML::Window& window, HTML::HTMLCanvasElement& canvas_element, JS::Value options) { // We should be coming here from getContext being called on a wrapped <canvas> element. auto context_attributes = TRY(convert_value_to_context_attributes_dictionary(canvas_element.vm(), options)); @@ -38,20 +40,29 @@ JS::ThrowCompletionOr<RefPtr<WebGLRenderingContext>> WebGLRenderingContext::crea bool created_bitmap = canvas_element.create_bitmap(/* minimum_width= */ 1, /* minimum_height= */ 1); if (!created_bitmap) { fire_webgl_context_creation_error(canvas_element); - return RefPtr<WebGLRenderingContext> { nullptr }; + return JS::GCPtr<WebGLRenderingContext> { nullptr }; } #ifndef __serenity__ // FIXME: Make WebGL work on other platforms. + (void)window; (void)context_attributes; dbgln("FIXME: WebGL not supported on the current platform"); fire_webgl_context_creation_error(canvas_element); - return RefPtr<WebGLRenderingContext> { nullptr }; + return JS::GCPtr<WebGLRenderingContext> { nullptr }; #else // FIXME: LibGL currently doesn't propagate context creation errors. auto context = GL::create_context(*canvas_element.bitmap()); - return adopt_ref(*new WebGLRenderingContext(canvas_element, move(context), context_attributes, context_attributes)); + return window.heap().allocate<WebGLRenderingContext>(window.realm(), window, canvas_element, move(context), context_attributes, context_attributes); #endif } +WebGLRenderingContext::WebGLRenderingContext(HTML::Window& window, HTML::HTMLCanvasElement& canvas_element, NonnullOwnPtr<GL::GLContext> context, WebGLContextAttributes context_creation_parameters, WebGLContextAttributes actual_context_parameters) + : WebGLRenderingContextBase(window, canvas_element, move(context), move(context_creation_parameters), move(actual_context_parameters)) +{ + set_prototype(&window.ensure_web_prototype<Bindings::WebGLRenderingContextPrototype>("WebGLRenderingContext")); +} + +WebGLRenderingContext::~WebGLRenderingContext() = default; + } diff --git a/Userland/Libraries/LibWeb/WebGL/WebGLRenderingContext.h b/Userland/Libraries/LibWeb/WebGL/WebGLRenderingContext.h index 7798009798..d6ee5e5cbc 100644 --- a/Userland/Libraries/LibWeb/WebGL/WebGLRenderingContext.h +++ b/Userland/Libraries/LibWeb/WebGL/WebGLRenderingContext.h @@ -6,26 +6,22 @@ #pragma once -#include <LibWeb/Bindings/Wrappable.h> #include <LibWeb/WebGL/WebGLRenderingContextBase.h> namespace Web::WebGL { -class WebGLRenderingContext - : public WebGLRenderingContextBase - , public Bindings::Wrappable { -public: - using WrapperType = Bindings::WebGLRenderingContextWrapper; +class WebGLRenderingContext final : public WebGLRenderingContextBase { + WEB_PLATFORM_OBJECT(WebGLRenderingContext, WebGLRenderingContextBase); - static JS::ThrowCompletionOr<RefPtr<WebGLRenderingContext>> create(HTML::HTMLCanvasElement& canvas_element, JS::Value options); +public: + static JS::ThrowCompletionOr<JS::GCPtr<WebGLRenderingContext>> create(HTML::Window&, HTML::HTMLCanvasElement& canvas_element, JS::Value options); - virtual ~WebGLRenderingContext() override = default; + virtual ~WebGLRenderingContext() override; private: - WebGLRenderingContext(HTML::HTMLCanvasElement& canvas_element, NonnullOwnPtr<GL::GLContext> context, WebGLContextAttributes context_creation_parameters, WebGLContextAttributes actual_context_parameters) - : WebGLRenderingContextBase(canvas_element, move(context), move(context_creation_parameters), move(actual_context_parameters)) - { - } + WebGLRenderingContext(HTML::Window&, HTML::HTMLCanvasElement&, NonnullOwnPtr<GL::GLContext> context, WebGLContextAttributes context_creation_parameters, WebGLContextAttributes actual_context_parameters); }; } + +WRAPPER_HACK(WebGLRenderingContext, Web::WebGL) diff --git a/Userland/Libraries/LibWeb/WebGL/WebGLRenderingContextBase.cpp b/Userland/Libraries/LibWeb/WebGL/WebGLRenderingContextBase.cpp index 3ed7f77264..f6d4c37890 100644 --- a/Userland/Libraries/LibWeb/WebGL/WebGLRenderingContextBase.cpp +++ b/Userland/Libraries/LibWeb/WebGL/WebGLRenderingContextBase.cpp @@ -7,12 +7,14 @@ #include <AK/Debug.h> #include <LibGL/GLContext.h> #include <LibWeb/HTML/HTMLCanvasElement.h> +#include <LibWeb/HTML/Window.h> #include <LibWeb/WebGL/WebGLRenderingContextBase.h> namespace Web::WebGL { -WebGLRenderingContextBase::WebGLRenderingContextBase(HTML::HTMLCanvasElement& canvas_element, NonnullOwnPtr<GL::GLContext> context, WebGLContextAttributes context_creation_parameters, WebGLContextAttributes actual_context_parameters) - : m_canvas_element(canvas_element) +WebGLRenderingContextBase::WebGLRenderingContextBase(HTML::Window& window, HTML::HTMLCanvasElement& canvas_element, NonnullOwnPtr<GL::GLContext> context, WebGLContextAttributes context_creation_parameters, WebGLContextAttributes actual_context_parameters) + : PlatformObject(window.realm()) + , m_canvas_element(canvas_element) , m_context(move(context)) , m_context_creation_parameters(move(context_creation_parameters)) , m_actual_context_parameters(move(actual_context_parameters)) @@ -21,6 +23,12 @@ WebGLRenderingContextBase::WebGLRenderingContextBase(HTML::HTMLCanvasElement& ca WebGLRenderingContextBase::~WebGLRenderingContextBase() = default; +void WebGLRenderingContextBase::visit_edges(Cell::Visitor& visitor) +{ + Base::visit_edges(visitor); + visitor.visit(m_canvas_element.ptr()); +} + #define RETURN_WITH_WEBGL_ERROR_IF(condition, error) \ if (condition) { \ dbgln_if(WEBGL_CONTEXT_DEBUG, "{}(): error {:#x}", __func__, error); \ diff --git a/Userland/Libraries/LibWeb/WebGL/WebGLRenderingContextBase.h b/Userland/Libraries/LibWeb/WebGL/WebGLRenderingContextBase.h index 7a5b74c8ee..c29145c99e 100644 --- a/Userland/Libraries/LibWeb/WebGL/WebGLRenderingContextBase.h +++ b/Userland/Libraries/LibWeb/WebGL/WebGLRenderingContextBase.h @@ -15,9 +15,9 @@ namespace Web::WebGL { -class WebGLRenderingContextBase - : public RefCounted<WebGLRenderingContextBase> - , public Weakable<WebGLRenderingContextBase> { +class WebGLRenderingContextBase : public Bindings::PlatformObject { + WEB_PLATFORM_OBJECT(WebGLRenderingContextBase, Bindings::PlatformObject); + public: virtual ~WebGLRenderingContextBase(); @@ -62,10 +62,12 @@ public: void viewport(GLint x, GLint y, GLsizei width, GLsizei height); protected: - WebGLRenderingContextBase(HTML::HTMLCanvasElement& canvas_element, NonnullOwnPtr<GL::GLContext> context, WebGLContextAttributes context_creation_parameters, WebGLContextAttributes actual_context_parameters); + WebGLRenderingContextBase(HTML::Window&, HTML::HTMLCanvasElement& canvas_element, NonnullOwnPtr<GL::GLContext> context, WebGLContextAttributes context_creation_parameters, WebGLContextAttributes actual_context_parameters); private: - JS::Handle<HTML::HTMLCanvasElement> m_canvas_element; + virtual void visit_edges(Cell::Visitor&) override; + + JS::NonnullGCPtr<HTML::HTMLCanvasElement> m_canvas_element; NonnullOwnPtr<GL::GLContext> m_context; |