/* * Copyright (c) 2022, Luke Wilde * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include #include namespace Web::WebGL { // https://www.khronos.org/registry/webgl/specs/latest/1.0/#fire-a-webgl-context-event static void fire_webgl_context_event(HTML::HTMLCanvasElement& canvas_element, FlyString const& type) { // To fire a WebGL context event named e means that an event using the WebGLContextEvent interface, with its type attribute [DOM4] initialized to e, its cancelable attribute initialized to true, and its isTrusted attribute [DOM4] initialized to true, is to be dispatched at the given object. // FIXME: Consider setting a status message. auto event = WebGLContextEvent::create(canvas_element.document().window(), type, WebGLContextEventInit {}); event->set_is_trusted(true); event->set_cancelable(true); canvas_element.dispatch_event(*event); } // https://www.khronos.org/registry/webgl/specs/latest/1.0/#fire-a-webgl-context-creation-error static void fire_webgl_context_creation_error(HTML::HTMLCanvasElement& canvas_element) { // 1. Fire a WebGL context event named "webglcontextcreationerror" at canvas, optionally with its statusMessage attribute set to a platform dependent string about the nature of the failure. fire_webgl_context_event(canvas_element, "webglcontextcreationerror"sv); } 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)); bool created_bitmap = canvas_element.create_bitmap(/* minimum_width= */ 1, /* minimum_height= */ 1); if (!created_bitmap) { fire_webgl_context_creation_error(canvas_element); 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 JS::GCPtr { nullptr }; #else // FIXME: LibGL currently doesn't propagate context creation errors. auto context = GL::create_context(*canvas_element.bitmap()); 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.cached_web_prototype("WebGLRenderingContext")); } WebGLRenderingContext::~WebGLRenderingContext() = default; }