From 0ec8a19a34e6943d9dca04e944dcc5ce6cd59747 Mon Sep 17 00:00:00 2001 From: Luke Wilde Date: Sun, 19 Jun 2022 19:45:33 +0100 Subject: [PATCH] LibWeb/WebGL: Forward the render context ref count to HTMLCanvasElement This allows HTMLCanvasElement and WebGL rendering contexts to share their lifetime, as JS allows them to arbitrarily access them at any time and WebGLRCB.canvas expects a non-null return value. --- .../LibWeb/WebGL/WebGLRenderingContextBase.cpp | 18 +++++++++++++----- .../LibWeb/WebGL/WebGLRenderingContextBase.h | 9 +++++---- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/Userland/Libraries/LibWeb/WebGL/WebGLRenderingContextBase.cpp b/Userland/Libraries/LibWeb/WebGL/WebGLRenderingContextBase.cpp index a486ded3b2..da0d132571 100644 --- a/Userland/Libraries/LibWeb/WebGL/WebGLRenderingContextBase.cpp +++ b/Userland/Libraries/LibWeb/WebGL/WebGLRenderingContextBase.cpp @@ -12,7 +12,7 @@ namespace Web::WebGL { WebGLRenderingContextBase::WebGLRenderingContextBase(HTML::HTMLCanvasElement& canvas_element, NonnullOwnPtr context, WebGLContextAttributes context_creation_parameters, WebGLContextAttributes actual_context_parameters) - : m_canvas_element(canvas_element) + : RefCountForwarder(canvas_element) , m_context(move(context)) , m_context_creation_parameters(move(context_creation_parameters)) , m_actual_context_parameters(move(actual_context_parameters)) @@ -67,15 +67,23 @@ void WebGLRenderingContextBase::present() } } +HTML::HTMLCanvasElement& WebGLRenderingContextBase::canvas_element() +{ + return ref_count_target(); +} + +HTML::HTMLCanvasElement const& WebGLRenderingContextBase::canvas_element() const +{ + return ref_count_target(); +} + void WebGLRenderingContextBase::needs_to_present() { m_should_present = true; - if (!m_canvas_element) + if (!canvas_element().layout_node()) return; - if (!m_canvas_element->layout_node()) - return; - m_canvas_element->layout_node()->set_needs_display(); + canvas_element().layout_node()->set_needs_display(); } void WebGLRenderingContextBase::set_error(GLenum error) diff --git a/Userland/Libraries/LibWeb/WebGL/WebGLRenderingContextBase.h b/Userland/Libraries/LibWeb/WebGL/WebGLRenderingContextBase.h index 4bf1e26a64..5a5914b7cc 100644 --- a/Userland/Libraries/LibWeb/WebGL/WebGLRenderingContextBase.h +++ b/Userland/Libraries/LibWeb/WebGL/WebGLRenderingContextBase.h @@ -6,7 +6,7 @@ #pragma once -#include +#include #include #include #include @@ -16,7 +16,7 @@ namespace Web::WebGL { class WebGLRenderingContextBase - : public RefCounted + : public RefCountForwarder , public Weakable { public: virtual ~WebGLRenderingContextBase(); @@ -63,8 +63,6 @@ protected: WebGLRenderingContextBase(HTML::HTMLCanvasElement& canvas_element, NonnullOwnPtr context, WebGLContextAttributes context_creation_parameters, WebGLContextAttributes actual_context_parameters); private: - WeakPtr m_canvas_element; - NonnullOwnPtr m_context; // https://www.khronos.org/registry/webgl/specs/latest/1.0/#context-creation-parameters @@ -87,6 +85,9 @@ private: GLenum m_error { GL_NO_ERROR }; + HTML::HTMLCanvasElement& canvas_element(); + HTML::HTMLCanvasElement const& canvas_element() const; + void needs_to_present(); void set_error(GLenum error); };