/* * Copyright (c) 2025, Ladybird contributors * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include namespace Web::HTML { // https://html.spec.whatwg.org/multipage/canvas.html#offscreenrenderingcontext // NOTE: This is the Variant created by the IDL wrapper generator, and needs to be updated accordingly. using OffscreenRenderingContext = Variant, GC::Root, GC::Root, Empty>; // https://html.spec.whatwg.org/multipage/canvas.html#imageencodeoptions struct ImageEncodeOptions { FlyString type { "image/png"_fly_string }; Optional quality; }; // https://html.spec.whatwg.org/multipage/canvas.html#offscreencanvas class OffscreenCanvas : public DOM::EventTarget , public Web::Bindings::Transferable { WEB_PLATFORM_OBJECT(OffscreenCanvas, DOM::EventTarget); GC_DECLARE_ALLOCATOR(OffscreenCanvas); public: static GC::Ref create(JS::Realm&, WebIDL::UnsignedLong width, WebIDL::UnsignedLong height); static WebIDL::ExceptionOr> construct_impl( JS::Realm&, WebIDL::UnsignedLong width, WebIDL::UnsignedLong height); virtual ~OffscreenCanvas() override; // ^Web::Bindings::Transferable virtual WebIDL::ExceptionOr transfer_steps(HTML::TransferDataHolder&) override; virtual WebIDL::ExceptionOr transfer_receiving_steps(HTML::TransferDataHolder&) override; virtual HTML::TransferType primary_interface() const override; WebIDL::UnsignedLong width() const; WebIDL::UnsignedLong height() const; RefPtr bitmap() const; WebIDL::ExceptionOr set_width(WebIDL::UnsignedLong); WebIDL::ExceptionOr set_height(WebIDL::UnsignedLong); Gfx::IntSize bitmap_size_for_canvas() const; JS::ThrowCompletionOr get_context(Bindings::OffscreenRenderingContextId contextId, JS::Value options); WebIDL::ExceptionOr> transfer_to_image_bitmap(); GC::Ref convert_to_blob(Optional options); void set_oncontextlost(GC::Ptr); GC::Ptr oncontextlost(); void set_oncontextrestored(GC::Ptr); GC::Ptr oncontextrestored(); private: OffscreenCanvas(JS::Realm&, RefPtr bitmap); virtual void initialize(JS::Realm&) override; virtual void visit_edges(Cell::Visitor&) override; enum class HasOrCreatedContext { No, Yes, }; JS::ThrowCompletionOr create_2d_context(JS::Value options); void reset_context_to_default_state(); void set_new_bitmap_size(Gfx::IntSize new_size); Variant, GC::Ref, GC::Ref, Empty> m_context; RefPtr m_bitmap; }; }