diff options
author | Luke Wilde <lukew@serenityos.org> | 2022-06-19 19:42:27 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-06-27 22:54:41 +0100 |
commit | adf8341c4ef7c54d7c2684ad8dd95bb6e764c3b0 (patch) | |
tree | 50ff55234b933425264cade5d2a86c251cea4277 /Userland/Libraries/LibWeb | |
parent | cd9864bbf1105dbc20e9ea293198ccab258388c5 (diff) | |
download | serenity-adf8341c4ef7c54d7c2684ad8dd95bb6e764c3b0.zip |
LibWeb: Forward CRC2D's ref count to HTMLCanvasElement
This allows HTMLCanvasElement and CRC2D to share their lifetime, as JS
allows them to arbitrarily access them at any time and CRC2D.canvas
expects a non-null return value.
Diffstat (limited to 'Userland/Libraries/LibWeb')
3 files changed, 29 insertions, 18 deletions
diff --git a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp index d088043be2..ae196db494 100644 --- a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp +++ b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp @@ -25,12 +25,27 @@ namespace Web::HTML { CanvasRenderingContext2D::CanvasRenderingContext2D(HTMLCanvasElement& element) - : m_element(element) + : RefCountForwarder(element) { } CanvasRenderingContext2D::~CanvasRenderingContext2D() = default; +HTMLCanvasElement& CanvasRenderingContext2D::canvas_element() +{ + return ref_count_target(); +} + +HTMLCanvasElement const& CanvasRenderingContext2D::canvas_element() const +{ + return ref_count_target(); +} + +NonnullRefPtr<HTMLCanvasElement> CanvasRenderingContext2D::canvas_for_binding() const +{ + return canvas_element(); +} + void CanvasRenderingContext2D::set_fill_style(String style) { // FIXME: 2. If the given value is a CanvasPattern object that is marked as not origin-clean, then set this's origin-clean flag to false. @@ -263,24 +278,19 @@ void CanvasRenderingContext2D::rotate(float radians) void CanvasRenderingContext2D::did_draw(Gfx::FloatRect const&) { // FIXME: Make use of the rect to reduce the invalidated area when possible. - if (!m_element) + if (!canvas_element().layout_node()) return; - if (!m_element->layout_node()) - return; - m_element->layout_node()->set_needs_display(); + canvas_element().layout_node()->set_needs_display(); } OwnPtr<Gfx::Painter> CanvasRenderingContext2D::painter() { - if (!m_element) - return {}; - - if (!m_element->bitmap()) { - if (!m_element->create_bitmap()) + if (!canvas_element().bitmap()) { + if (!canvas_element().create_bitmap()) return {}; } - return make<Gfx::Painter>(*m_element->bitmap()); + return make<Gfx::Painter>(*canvas_element().bitmap()); } void CanvasRenderingContext2D::fill_text(String const& text, float x, float y, Optional<double> max_width) @@ -468,9 +478,9 @@ DOM::ExceptionOr<RefPtr<ImageData>> CanvasRenderingContext2D::get_image_data(int auto image_data = ImageData::create_with_size(wrapper()->global_object(), 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 (!m_element || !m_element->bitmap()) + if (!canvas_element().bitmap()) return image_data; - auto const& bitmap = *m_element->bitmap(); + auto const& bitmap = *canvas_element().bitmap(); // 5. Let the source rectangle be the rectangle whose corners are the four points (sx, sy), (sx+sw, sy), (sx+sw, sy+sh), (sx, sy+sh). auto source_rect = Gfx::Rect { x, y, width, height }; diff --git a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h index efdbc55fff..b54dc410de 100644 --- a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h +++ b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h @@ -7,7 +7,7 @@ #pragma once -#include <AK/RefCounted.h> +#include <AK/RefCountForwarder.h> #include <AK/Variant.h> #include <LibGfx/AffineTransform.h> #include <LibGfx/Color.h> @@ -27,7 +27,7 @@ namespace Web::HTML { using CanvasImageSource = Variant<NonnullRefPtr<HTMLImageElement>, NonnullRefPtr<HTMLCanvasElement>>; class CanvasRenderingContext2D - : public RefCounted<CanvasRenderingContext2D> + : public RefCountForwarder<HTMLCanvasElement> , public Bindings::Wrappable { AK_MAKE_NONCOPYABLE(CanvasRenderingContext2D); @@ -90,7 +90,7 @@ public: void reset_to_default_state(); - HTMLCanvasElement* canvas() { return m_element; } + NonnullRefPtr<HTMLCanvasElement> canvas_for_binding() const; RefPtr<TextMetrics> measure_text(String const& text); @@ -122,7 +122,8 @@ private: OwnPtr<Gfx::Painter> painter(); - WeakPtr<HTMLCanvasElement> m_element; + HTMLCanvasElement& canvas_element(); + HTMLCanvasElement const& canvas_element() const; // https://html.spec.whatwg.org/multipage/canvas.html#drawing-state struct DrawingState { diff --git a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.idl b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.idl index f70159c8f3..20ae8750e8 100644 --- a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.idl +++ b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.idl @@ -47,7 +47,7 @@ interface CanvasRenderingContext2D { undefined reset(); boolean isContextLost(); - readonly attribute HTMLCanvasElement canvas; + [ImplementedAs=canvas_for_binding] readonly attribute HTMLCanvasElement canvas; TextMetrics measureText(DOMString text); |