mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 22:57:44 +00:00
LibWeb: Add the ability to retrieve a WebGL context from getContext
This commit is contained in:
parent
b0c2aee2e4
commit
58f882200c
19 changed files with 903 additions and 21 deletions
50
Userland/Libraries/LibWeb/WebGL/WebGLRenderingContext.cpp
Normal file
50
Userland/Libraries/LibWeb/WebGL/WebGLRenderingContext.cpp
Normal file
|
@ -0,0 +1,50 @@
|
|||
/*
|
||||
* Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibWeb/Bindings/Wrapper.h>
|
||||
#include <LibWeb/HTML/HTMLCanvasElement.h>
|
||||
#include <LibWeb/WebGL/WebGLContextEvent.h>
|
||||
#include <LibWeb/WebGL/WebGLRenderingContext.h>
|
||||
|
||||
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(type, WebGLContextEventInit {});
|
||||
event->set_is_trusted(true);
|
||||
event->set_cancelable(true);
|
||||
canvas_element.dispatch_event(move(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<RefPtr<WebGLRenderingContext>> WebGLRenderingContext::create(HTML::HTMLCanvasElement& canvas_element, JS::Value options)
|
||||
{
|
||||
// We should be coming here from getContext being called on a wrapped <canvas> element.
|
||||
VERIFY(canvas_element.wrapper());
|
||||
auto context_attributes = TRY(convert_value_to_context_attributes_dictionary(canvas_element.wrapper()->global_object(), 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 RefPtr<WebGLRenderingContext> { nullptr };
|
||||
}
|
||||
|
||||
// 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));
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue