WebGLRenderingContext.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
  3. * Copyright (c) 2023, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibJS/Runtime/ArrayBuffer.h>
  8. #include <LibJS/Runtime/TypedArray.h>
  9. #include <LibWeb/Bindings/Intrinsics.h>
  10. #include <LibWeb/Bindings/WebGLRenderingContextPrototype.h>
  11. #include <LibWeb/HTML/HTMLCanvasElement.h>
  12. #include <LibWeb/HTML/TraversableNavigable.h>
  13. #include <LibWeb/Painting/Paintable.h>
  14. #include <LibWeb/WebGL/EventNames.h>
  15. #include <LibWeb/WebGL/OpenGLContext.h>
  16. #include <LibWeb/WebGL/WebGLContextEvent.h>
  17. #include <LibWeb/WebGL/WebGLRenderingContext.h>
  18. #include <LibWeb/WebGL/WebGLShader.h>
  19. #include <LibWeb/WebIDL/Buffers.h>
  20. #include <GLES2/gl2.h>
  21. #include <GLES2/gl2ext.h>
  22. namespace Web::WebGL {
  23. GC_DEFINE_ALLOCATOR(WebGLRenderingContext);
  24. // https://www.khronos.org/registry/webgl/specs/latest/1.0/#fire-a-webgl-context-event
  25. static void fire_webgl_context_event(HTML::HTMLCanvasElement& canvas_element, FlyString const& type)
  26. {
  27. // 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.
  28. // FIXME: Consider setting a status message.
  29. auto event = WebGLContextEvent::create(canvas_element.realm(), type, WebGLContextEventInit {});
  30. event->set_is_trusted(true);
  31. event->set_cancelable(true);
  32. canvas_element.dispatch_event(*event);
  33. }
  34. // https://www.khronos.org/registry/webgl/specs/latest/1.0/#fire-a-webgl-context-creation-error
  35. static void fire_webgl_context_creation_error(HTML::HTMLCanvasElement& canvas_element)
  36. {
  37. // 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.
  38. fire_webgl_context_event(canvas_element, EventNames::webglcontextcreationerror);
  39. }
  40. JS::ThrowCompletionOr<GC::Ptr<WebGLRenderingContext>> WebGLRenderingContext::create(JS::Realm& realm, HTML::HTMLCanvasElement& canvas_element, JS::Value options)
  41. {
  42. // We should be coming here from getContext being called on a wrapped <canvas> element.
  43. auto context_attributes = TRY(convert_value_to_context_attributes_dictionary(canvas_element.vm(), options));
  44. auto skia_backend_context = canvas_element.navigable()->traversable_navigable()->skia_backend_context();
  45. auto context = OpenGLContext::create(*skia_backend_context);
  46. if (!context) {
  47. fire_webgl_context_creation_error(canvas_element);
  48. return GC::Ptr<WebGLRenderingContext> { nullptr };
  49. }
  50. context->set_size(canvas_element.bitmap_size_for_canvas(1, 1));
  51. return realm.create<WebGLRenderingContext>(realm, canvas_element, context.release_nonnull(), context_attributes, context_attributes);
  52. }
  53. WebGLRenderingContext::WebGLRenderingContext(JS::Realm& realm, HTML::HTMLCanvasElement& canvas_element, NonnullOwnPtr<OpenGLContext> context, WebGLContextAttributes context_creation_parameters, WebGLContextAttributes actual_context_parameters)
  54. : PlatformObject(realm)
  55. , WebGLRenderingContextImpl(realm, move(context))
  56. , m_canvas_element(canvas_element)
  57. , m_context_creation_parameters(context_creation_parameters)
  58. , m_actual_context_parameters(actual_context_parameters)
  59. {
  60. }
  61. WebGLRenderingContext::~WebGLRenderingContext() = default;
  62. void WebGLRenderingContext::initialize(JS::Realm& realm)
  63. {
  64. Base::initialize(realm);
  65. WEB_SET_PROTOTYPE_FOR_INTERFACE(WebGLRenderingContext);
  66. }
  67. void WebGLRenderingContext::visit_edges(Cell::Visitor& visitor)
  68. {
  69. Base::visit_edges(visitor);
  70. visitor.visit(m_canvas_element);
  71. }
  72. void WebGLRenderingContext::present()
  73. {
  74. if (!m_should_present)
  75. return;
  76. m_should_present = false;
  77. // "Before the drawing buffer is presented for compositing the implementation shall ensure that all rendering operations have been flushed to the drawing buffer."
  78. glFlush();
  79. // "By default, after compositing the contents of the drawing buffer shall be cleared to their default values, as shown in the table above.
  80. // This default behavior can be changed by setting the preserveDrawingBuffer attribute of the WebGLContextAttributes object.
  81. // If this flag is true, the contents of the drawing buffer shall be preserved until the author either clears or overwrites them."
  82. if (!m_context_creation_parameters.preserve_drawing_buffer) {
  83. context().clear_buffer_to_default_values();
  84. }
  85. }
  86. GC::Ref<HTML::HTMLCanvasElement> WebGLRenderingContext::canvas_for_binding() const
  87. {
  88. return *m_canvas_element;
  89. }
  90. void WebGLRenderingContext::needs_to_present()
  91. {
  92. m_should_present = true;
  93. if (!m_canvas_element->paintable())
  94. return;
  95. m_canvas_element->paintable()->set_needs_display();
  96. }
  97. void WebGLRenderingContext::set_error(GLenum error)
  98. {
  99. auto context_error = glGetError();
  100. if (context_error != GL_NO_ERROR)
  101. m_error = context_error;
  102. else
  103. m_error = error;
  104. }
  105. bool WebGLRenderingContext::is_context_lost() const
  106. {
  107. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContext::is_context_lost()");
  108. return m_context_lost;
  109. }
  110. void WebGLRenderingContext::set_size(Gfx::IntSize const& size)
  111. {
  112. context().set_size(size);
  113. }
  114. void WebGLRenderingContext::reset_to_default_state()
  115. {
  116. }
  117. RefPtr<Gfx::PaintingSurface> WebGLRenderingContext::surface()
  118. {
  119. return context().surface();
  120. }
  121. void WebGLRenderingContext::allocate_painting_surface_if_needed()
  122. {
  123. context().allocate_painting_surface_if_needed();
  124. }
  125. }