diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/WrapperGenerator/IDLGenerators.cpp b/Meta/Lagom/Tools/CodeGenerators/LibWeb/WrapperGenerator/IDLGenerators.cpp index e9ec6b244c7..08c4c5b1c03 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/WrapperGenerator/IDLGenerators.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/WrapperGenerator/IDLGenerators.cpp @@ -151,6 +151,10 @@ static bool impl_is_wrapper(Type const& type) return true; if (type.name == "MutationRecord"sv) return true; + if (type.name == "CanvasRenderingContext2D"sv) + return true; + if (type.name == "WebGLRenderingContext"sv) + return true; return false; } diff --git a/Userland/Libraries/LibWeb/Forward.h b/Userland/Libraries/LibWeb/Forward.h index 967e0575954..439c8335c48 100644 --- a/Userland/Libraries/LibWeb/Forward.h +++ b/Userland/Libraries/LibWeb/Forward.h @@ -450,7 +450,6 @@ class URLSearchParamsIterator; namespace Web::Bindings { class BlobWrapper; class CanvasGradientWrapper; -class CanvasRenderingContext2DWrapper; class CryptoWrapper; class DOMExceptionWrapper; class DOMPointWrapper; @@ -478,7 +477,6 @@ class TextMetricsWrapper; class URLSearchParamsIteratorWrapper; class URLSearchParamsWrapper; class URLWrapper; -class WebGLRenderingContextWrapper; class WindowProxy; class WorkerLocationWrapper; class WorkerNavigatorWrapper; diff --git a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp index fea84804e5f..5c923a6fe9e 100644 --- a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp +++ b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp @@ -10,7 +10,7 @@ #include #include #include -#include +#include #include #include #include @@ -23,13 +23,26 @@ namespace Web::HTML { -CanvasRenderingContext2D::CanvasRenderingContext2D(HTMLCanvasElement& element) - : m_element(JS::make_handle(element)) +JS::NonnullGCPtr CanvasRenderingContext2D::create(HTML::Window& window, HTMLCanvasElement& element) { + return *window.heap().allocate(window.realm(), window, element); +} + +CanvasRenderingContext2D::CanvasRenderingContext2D(HTML::Window& window, HTMLCanvasElement& element) + : PlatformObject(window.realm()) + , m_element(element) +{ + set_prototype(&window.ensure_web_prototype("CanvasRenderingContext2D")); } CanvasRenderingContext2D::~CanvasRenderingContext2D() = default; +void CanvasRenderingContext2D::visit_edges(Cell::Visitor& visitor) +{ + Base::visit_edges(visitor); + visitor.visit(m_element.ptr()); +} + HTMLCanvasElement& CanvasRenderingContext2D::canvas_element() { return *m_element; @@ -306,11 +319,7 @@ void CanvasRenderingContext2D::fill(Path2D& path, String const& fill_rule) RefPtr CanvasRenderingContext2D::create_image_data(int width, int height) const { - if (!wrapper()) { - dbgln("Hmm! Attempted to create ImageData for wrapper-less CRC2D."); - return {}; - } - return ImageData::create_with_size(wrapper()->vm(), width, height); + return ImageData::create_with_size(vm(), width, height); } // https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-getimagedata @@ -326,7 +335,7 @@ DOM::ExceptionOr> CanvasRenderingContext2D::get_image_data(int // 3. Let imageData be a new ImageData object. // 4. Initialize imageData given sw, sh, settings set to settings, and defaultColorSpace set to this's color space. - auto image_data = ImageData::create_with_size(wrapper()->vm(), width, height); + auto image_data = ImageData::create_with_size(vm(), width, height); // NOTE: We don't attempt to create the underlying bitmap here; if it doesn't exist, it's like copying only transparent black pixels (which is a no-op). if (!canvas_element().bitmap()) diff --git a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h index 639e6dba4ed..8cb2e56a1d7 100644 --- a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h +++ b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h @@ -7,14 +7,13 @@ #pragma once -#include #include #include #include #include #include #include -#include +#include #include #include #include @@ -37,8 +36,7 @@ namespace Web::HTML { using CanvasImageSource = Variant, JS::Handle>; class CanvasRenderingContext2D - : public RefCounted - , public Bindings::Wrappable + : public Bindings::PlatformObject , public CanvasPath , public CanvasState , public CanvasTransform @@ -50,14 +48,11 @@ class CanvasRenderingContext2D , public CanvasImageData , public CanvasPathDrawingStyles { - AK_MAKE_NONCOPYABLE(CanvasRenderingContext2D); - AK_MAKE_NONMOVABLE(CanvasRenderingContext2D); + WEB_PLATFORM_OBJECT(CanvasRenderingContext2D, Bindings::PlatformObject); public: - using WrapperType = Bindings::CanvasRenderingContext2DWrapper; - - static NonnullRefPtr create(HTMLCanvasElement& element) { return adopt_ref(*new CanvasRenderingContext2D(element)); } - ~CanvasRenderingContext2D(); + static JS::NonnullGCPtr create(HTML::Window&, HTMLCanvasElement&); + virtual ~CanvasRenderingContext2D() override; virtual void fill_rect(float x, float y, float width, float height) override; virtual void stroke_rect(float x, float y, float width, float height) override; @@ -88,7 +83,9 @@ public: virtual void clip() override; private: - explicit CanvasRenderingContext2D(HTMLCanvasElement&); + explicit CanvasRenderingContext2D(HTML::Window&, HTMLCanvasElement&); + + virtual void visit_edges(Cell::Visitor&) override; struct PreparedTextGlyph { unsigned int c; @@ -112,7 +109,7 @@ private: void stroke_internal(Gfx::Path const&); void fill_internal(Gfx::Path&, String const& fill_rule); - JS::Handle m_element; + JS::NonnullGCPtr m_element; // https://html.spec.whatwg.org/multipage/canvas.html#concept-canvas-origin-clean bool m_origin_clean { true }; @@ -127,3 +124,5 @@ DOM::ExceptionOr check_usability_of_image(CanvasImag bool image_is_not_origin_clean(CanvasImageSource const&); } + +WRAPPER_HACK(CanvasRenderingContext2D, Web::HTML) diff --git a/Userland/Libraries/LibWeb/HTML/HTMLCanvasElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLCanvasElement.cpp index 852dffe6b07..21a2f0b820a 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLCanvasElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLCanvasElement.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -21,10 +22,25 @@ static constexpr auto max_canvas_area = 16384 * 16384; HTMLCanvasElement::HTMLCanvasElement(DOM::Document& document, DOM::QualifiedName qualified_name) : HTMLElement(document, move(qualified_name)) { + set_prototype(&document.window().ensure_web_prototype("HTMLCanvasElement")); } HTMLCanvasElement::~HTMLCanvasElement() = default; +void HTMLCanvasElement::visit_edges(Cell::Visitor& visitor) +{ + Base::visit_edges(visitor); + m_context.visit( + [&](JS::NonnullGCPtr& context) { + visitor.visit(context.ptr()); + }, + [&](JS::NonnullGCPtr& context) { + visitor.visit(context.ptr()); + }, + [](Empty) { + }); +} + unsigned HTMLCanvasElement::width() const { return attribute(HTML::AttributeNames::width).to_uint().value_or(300); @@ -38,10 +54,10 @@ unsigned HTMLCanvasElement::height() const void HTMLCanvasElement::reset_context_to_default_state() { m_context.visit( - [](NonnullRefPtr& context) { + [](JS::NonnullGCPtr& context) { context->reset_to_default_state(); }, - [](NonnullRefPtr&) { + [](JS::NonnullGCPtr&) { TODO(); }, [](Empty) { @@ -71,22 +87,22 @@ RefPtr HTMLCanvasElement::create_layout_node(NonnullRefPtr()) - return m_context.has>() ? HasOrCreatedContext::Yes : HasOrCreatedContext::No; + return m_context.has>() ? HasOrCreatedContext::Yes : HasOrCreatedContext::No; - m_context = CanvasRenderingContext2D::create(*this); + m_context = CanvasRenderingContext2D::create(window(), *this); return HasOrCreatedContext::Yes; } JS::ThrowCompletionOr HTMLCanvasElement::create_webgl_context(JS::Value options) { if (!m_context.has()) - return m_context.has>() ? HasOrCreatedContext::Yes : HasOrCreatedContext::No; + return m_context.has>() ? HasOrCreatedContext::Yes : HasOrCreatedContext::No; - auto maybe_context = TRY(WebGL::WebGLRenderingContext::create(*this, options)); + auto maybe_context = TRY(WebGL::WebGLRenderingContext::create(window(), *this, options)); if (!maybe_context) return HasOrCreatedContext::No; - m_context = maybe_context.release_nonnull(); + m_context = JS::NonnullGCPtr(*maybe_context); return HasOrCreatedContext::Yes; } @@ -104,7 +120,7 @@ JS::ThrowCompletionOr HTMLCanvasElement::ge // NOTE: See the spec for the full table. if (type == "2d"sv) { if (create_2d_context() == HasOrCreatedContext::Yes) - return m_context; + return JS::make_handle(*m_context.get>()); return Empty {}; } @@ -112,7 +128,7 @@ JS::ThrowCompletionOr HTMLCanvasElement::ge // NOTE: The WebGL spec says "experimental-webgl" is also acceptable and must be equivalent to "webgl". Other engines accept this, so we do too. if (type.is_one_of("webgl"sv, "experimental-webgl"sv)) { if (TRY(create_webgl_context(options)) == HasOrCreatedContext::Yes) - return m_context; + return JS::make_handle(*m_context.get>()); return Empty {}; } @@ -168,10 +184,10 @@ String HTMLCanvasElement::to_data_url(String const& type, [[maybe_unused]] Optio void HTMLCanvasElement::present() { m_context.visit( - [](NonnullRefPtr&) { + [](JS::NonnullGCPtr&) { // Do nothing, CRC2D writes directly to the canvas bitmap. }, - [](NonnullRefPtr& context) { + [](JS::NonnullGCPtr& context) { context->present(); }, [](Empty) { diff --git a/Userland/Libraries/LibWeb/HTML/HTMLCanvasElement.h b/Userland/Libraries/LibWeb/HTML/HTMLCanvasElement.h index 79b9924935b..e7b988153eb 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLCanvasElement.h +++ b/Userland/Libraries/LibWeb/HTML/HTMLCanvasElement.h @@ -17,7 +17,7 @@ class HTMLCanvasElement final : public HTMLElement { WEB_PLATFORM_OBJECT(HTMLCanvasElement, HTMLElement); public: - using RenderingContext = Variant, NonnullRefPtr, Empty>; + using RenderingContext = Variant, JS::Handle, Empty>; virtual ~HTMLCanvasElement() override; @@ -40,6 +40,8 @@ public: private: HTMLCanvasElement(DOM::Document&, DOM::QualifiedName); + virtual void visit_edges(Cell::Visitor&) override; + virtual RefPtr create_layout_node(NonnullRefPtr) override; enum class HasOrCreatedContext { @@ -52,7 +54,8 @@ private: void reset_context_to_default_state(); RefPtr m_bitmap; - RenderingContext m_context; + + Variant, JS::NonnullGCPtr, Empty> m_context; }; } diff --git a/Userland/Libraries/LibWeb/WebGL/WebGLRenderingContext.cpp b/Userland/Libraries/LibWeb/WebGL/WebGLRenderingContext.cpp index 58482000530..3fea5c92ed9 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 #include #include #include +#include #include #include @@ -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> WebGLRenderingContext::create(HTML::HTMLCanvasElement& canvas_element, JS::Value options) +JS::ThrowCompletionOr> WebGLRenderingContext::create(HTML::Window& window, HTML::HTMLCanvasElement& canvas_element, JS::Value options) { // We should be coming here from getContext being called on a wrapped element. auto context_attributes = TRY(convert_value_to_context_attributes_dictionary(canvas_element.vm(), options)); @@ -38,20 +40,29 @@ JS::ThrowCompletionOr> 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 { nullptr }; + return JS::GCPtr { 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 { nullptr }; + return JS::GCPtr { 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(window.realm(), window, canvas_element, move(context), context_attributes, context_attributes); #endif } +WebGLRenderingContext::WebGLRenderingContext(HTML::Window& window, HTML::HTMLCanvasElement& canvas_element, NonnullOwnPtr 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("WebGLRenderingContext")); +} + +WebGLRenderingContext::~WebGLRenderingContext() = default; + } diff --git a/Userland/Libraries/LibWeb/WebGL/WebGLRenderingContext.h b/Userland/Libraries/LibWeb/WebGL/WebGLRenderingContext.h index 77980097980..d6ee5e5cbc7 100644 --- a/Userland/Libraries/LibWeb/WebGL/WebGLRenderingContext.h +++ b/Userland/Libraries/LibWeb/WebGL/WebGLRenderingContext.h @@ -6,26 +6,22 @@ #pragma once -#include #include namespace Web::WebGL { -class WebGLRenderingContext - : public WebGLRenderingContextBase - , public Bindings::Wrappable { +class WebGLRenderingContext final : public WebGLRenderingContextBase { + WEB_PLATFORM_OBJECT(WebGLRenderingContext, WebGLRenderingContextBase); + public: - using WrapperType = Bindings::WebGLRenderingContextWrapper; + static JS::ThrowCompletionOr> create(HTML::Window&, HTML::HTMLCanvasElement& canvas_element, JS::Value options); - static JS::ThrowCompletionOr> create(HTML::HTMLCanvasElement& canvas_element, JS::Value options); - - virtual ~WebGLRenderingContext() override = default; + virtual ~WebGLRenderingContext() override; private: - WebGLRenderingContext(HTML::HTMLCanvasElement& canvas_element, NonnullOwnPtr 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 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 3ed7f772644..f6d4c378900 100644 --- a/Userland/Libraries/LibWeb/WebGL/WebGLRenderingContextBase.cpp +++ b/Userland/Libraries/LibWeb/WebGL/WebGLRenderingContextBase.cpp @@ -7,12 +7,14 @@ #include #include #include +#include #include namespace Web::WebGL { -WebGLRenderingContextBase::WebGLRenderingContextBase(HTML::HTMLCanvasElement& canvas_element, NonnullOwnPtr context, WebGLContextAttributes context_creation_parameters, WebGLContextAttributes actual_context_parameters) - : m_canvas_element(canvas_element) +WebGLRenderingContextBase::WebGLRenderingContextBase(HTML::Window& window, HTML::HTMLCanvasElement& canvas_element, NonnullOwnPtr 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 7a5b74c8ee3..c29145c99ef 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 - , public Weakable { +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 context, WebGLContextAttributes context_creation_parameters, WebGLContextAttributes actual_context_parameters); + WebGLRenderingContextBase(HTML::Window&, HTML::HTMLCanvasElement& canvas_element, NonnullOwnPtr context, WebGLContextAttributes context_creation_parameters, WebGLContextAttributes actual_context_parameters); private: - JS::Handle m_canvas_element; + virtual void visit_edges(Cell::Visitor&) override; + + JS::NonnullGCPtr m_canvas_element; NonnullOwnPtr m_context; diff --git a/Userland/Libraries/LibWeb/idl_files.cmake b/Userland/Libraries/LibWeb/idl_files.cmake index cc407d018bf..71c487e08a6 100644 --- a/Userland/Libraries/LibWeb/idl_files.cmake +++ b/Userland/Libraries/LibWeb/idl_files.cmake @@ -62,7 +62,7 @@ libweb_js_wrapper(Geometry/DOMRect) libweb_js_wrapper(Geometry/DOMRectList) libweb_js_wrapper(Geometry/DOMRectReadOnly) libweb_js_wrapper(HTML/CanvasGradient) -libweb_js_wrapper(HTML/CanvasRenderingContext2D) +libweb_js_wrapper(HTML/CanvasRenderingContext2D NO_INSTANCE) libweb_js_wrapper(HTML/CloseEvent NO_INSTANCE) libweb_js_wrapper(HTML/DOMParser NO_INSTANCE) libweb_js_wrapper(HTML/DOMStringMap NO_INSTANCE) @@ -183,7 +183,7 @@ libweb_js_wrapper(UIEvents/UIEvent NO_INSTANCE) libweb_js_wrapper(URL/URL) libweb_js_wrapper(URL/URLSearchParams ITERABLE) libweb_js_wrapper(WebGL/WebGLContextEvent NO_INSTANCE) -libweb_js_wrapper(WebGL/WebGLRenderingContext) +libweb_js_wrapper(WebGL/WebGLRenderingContext NO_INSTANCE) libweb_js_wrapper(WebSockets/WebSocket NO_INSTANCE) libweb_js_wrapper(XHR/ProgressEvent NO_INSTANCE) libweb_js_wrapper(XHR/XMLHttpRequest NO_INSTANCE)