WebGLRenderingContext.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/Intrinsics.h>
  7. #include <LibWeb/DOM/Document.h>
  8. #include <LibWeb/HTML/HTMLCanvasElement.h>
  9. #include <LibWeb/WebGL/EventNames.h>
  10. #include <LibWeb/WebGL/WebGLContextEvent.h>
  11. #include <LibWeb/WebGL/WebGLRenderingContext.h>
  12. namespace Web::WebGL {
  13. // https://www.khronos.org/registry/webgl/specs/latest/1.0/#fire-a-webgl-context-event
  14. static void fire_webgl_context_event(HTML::HTMLCanvasElement& canvas_element, FlyString const& type)
  15. {
  16. // 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.
  17. // FIXME: Consider setting a status message.
  18. auto event = WebGLContextEvent::create(canvas_element.realm(), type, WebGLContextEventInit {}).release_value_but_fixme_should_propagate_errors();
  19. event->set_is_trusted(true);
  20. event->set_cancelable(true);
  21. canvas_element.dispatch_event(*event);
  22. }
  23. // https://www.khronos.org/registry/webgl/specs/latest/1.0/#fire-a-webgl-context-creation-error
  24. static void fire_webgl_context_creation_error(HTML::HTMLCanvasElement& canvas_element)
  25. {
  26. // 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.
  27. fire_webgl_context_event(canvas_element, EventNames::webglcontextcreationerror);
  28. }
  29. JS::ThrowCompletionOr<JS::GCPtr<WebGLRenderingContext>> WebGLRenderingContext::create(JS::Realm& realm, HTML::HTMLCanvasElement& canvas_element, JS::Value options)
  30. {
  31. // We should be coming here from getContext being called on a wrapped <canvas> element.
  32. auto context_attributes = TRY(convert_value_to_context_attributes_dictionary(canvas_element.vm(), options));
  33. bool created_bitmap = canvas_element.create_bitmap(/* minimum_width= */ 1, /* minimum_height= */ 1);
  34. if (!created_bitmap) {
  35. fire_webgl_context_creation_error(canvas_element);
  36. return JS::GCPtr<WebGLRenderingContext> { nullptr };
  37. }
  38. auto context_or_error = GL::create_context(*canvas_element.bitmap());
  39. if (context_or_error.is_error()) {
  40. fire_webgl_context_creation_error(canvas_element);
  41. return JS::GCPtr<WebGLRenderingContext> { nullptr };
  42. }
  43. return MUST_OR_THROW_OOM(realm.heap().allocate<WebGLRenderingContext>(realm, realm, canvas_element, context_or_error.release_value(), context_attributes, context_attributes));
  44. }
  45. WebGLRenderingContext::WebGLRenderingContext(JS::Realm& realm, HTML::HTMLCanvasElement& canvas_element, NonnullOwnPtr<GL::GLContext> context, WebGLContextAttributes context_creation_parameters, WebGLContextAttributes actual_context_parameters)
  46. : WebGLRenderingContextBase(realm, canvas_element, move(context), move(context_creation_parameters), move(actual_context_parameters))
  47. {
  48. }
  49. WebGLRenderingContext::~WebGLRenderingContext() = default;
  50. JS::ThrowCompletionOr<void> WebGLRenderingContext::initialize(JS::Realm& realm)
  51. {
  52. MUST_OR_THROW_OOM(Base::initialize(realm));
  53. set_prototype(&Bindings::ensure_web_prototype<Bindings::WebGLRenderingContextPrototype>(realm, "WebGLRenderingContext"));
  54. return {};
  55. }
  56. }