WebGLRenderingContext.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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/ANGLEInstancedArrays.h>
  15. #include <LibWeb/WebGL/EventNames.h>
  16. #include <LibWeb/WebGL/OpenGLContext.h>
  17. #include <LibWeb/WebGL/WebGLContextEvent.h>
  18. #include <LibWeb/WebGL/WebGLRenderingContext.h>
  19. #include <LibWeb/WebGL/WebGLShader.h>
  20. #include <LibWeb/WebIDL/Buffers.h>
  21. #include <GLES2/gl2.h>
  22. #include <GLES2/gl2ext.h>
  23. namespace Web::WebGL {
  24. GC_DEFINE_ALLOCATOR(WebGLRenderingContext);
  25. // https://www.khronos.org/registry/webgl/specs/latest/1.0/#fire-a-webgl-context-event
  26. void fire_webgl_context_event(HTML::HTMLCanvasElement& canvas_element, FlyString const& type)
  27. {
  28. // 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.
  29. // FIXME: Consider setting a status message.
  30. auto event = WebGLContextEvent::create(canvas_element.realm(), type, WebGLContextEventInit {});
  31. event->set_is_trusted(true);
  32. event->set_cancelable(true);
  33. canvas_element.dispatch_event(*event);
  34. }
  35. // https://www.khronos.org/registry/webgl/specs/latest/1.0/#fire-a-webgl-context-creation-error
  36. void fire_webgl_context_creation_error(HTML::HTMLCanvasElement& canvas_element)
  37. {
  38. // 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.
  39. fire_webgl_context_event(canvas_element, EventNames::webglcontextcreationerror);
  40. }
  41. JS::ThrowCompletionOr<GC::Ptr<WebGLRenderingContext>> WebGLRenderingContext::create(JS::Realm& realm, HTML::HTMLCanvasElement& canvas_element, JS::Value options)
  42. {
  43. // We should be coming here from getContext being called on a wrapped <canvas> element.
  44. auto context_attributes = TRY(convert_value_to_context_attributes_dictionary(canvas_element.vm(), options));
  45. auto skia_backend_context = canvas_element.navigable()->traversable_navigable()->skia_backend_context();
  46. if (!skia_backend_context) {
  47. fire_webgl_context_creation_error(canvas_element);
  48. return GC::Ptr<WebGLRenderingContext> { nullptr };
  49. }
  50. auto context = OpenGLContext::create(*skia_backend_context);
  51. if (!context) {
  52. fire_webgl_context_creation_error(canvas_element);
  53. return GC::Ptr<WebGLRenderingContext> { nullptr };
  54. }
  55. context->set_size(canvas_element.bitmap_size_for_canvas(1, 1));
  56. return realm.create<WebGLRenderingContext>(realm, canvas_element, context.release_nonnull(), context_attributes, context_attributes);
  57. }
  58. WebGLRenderingContext::WebGLRenderingContext(JS::Realm& realm, HTML::HTMLCanvasElement& canvas_element, NonnullOwnPtr<OpenGLContext> context, WebGLContextAttributes context_creation_parameters, WebGLContextAttributes actual_context_parameters)
  59. : PlatformObject(realm)
  60. , WebGLRenderingContextImpl(realm, move(context))
  61. , m_canvas_element(canvas_element)
  62. , m_context_creation_parameters(context_creation_parameters)
  63. , m_actual_context_parameters(actual_context_parameters)
  64. {
  65. }
  66. WebGLRenderingContext::~WebGLRenderingContext() = default;
  67. void WebGLRenderingContext::initialize(JS::Realm& realm)
  68. {
  69. Base::initialize(realm);
  70. WEB_SET_PROTOTYPE_FOR_INTERFACE(WebGLRenderingContext);
  71. }
  72. void WebGLRenderingContext::visit_edges(Cell::Visitor& visitor)
  73. {
  74. Base::visit_edges(visitor);
  75. WebGLRenderingContextImpl::visit_edges(visitor);
  76. visitor.visit(m_canvas_element);
  77. }
  78. void WebGLRenderingContext::present()
  79. {
  80. if (!m_should_present)
  81. return;
  82. m_should_present = false;
  83. // "Before the drawing buffer is presented for compositing the implementation shall ensure that all rendering operations have been flushed to the drawing buffer."
  84. glFlush();
  85. // "By default, after compositing the contents of the drawing buffer shall be cleared to their default values, as shown in the table above.
  86. // This default behavior can be changed by setting the preserveDrawingBuffer attribute of the WebGLContextAttributes object.
  87. // If this flag is true, the contents of the drawing buffer shall be preserved until the author either clears or overwrites them."
  88. if (!m_context_creation_parameters.preserve_drawing_buffer) {
  89. context().clear_buffer_to_default_values();
  90. }
  91. }
  92. GC::Ref<HTML::HTMLCanvasElement> WebGLRenderingContext::canvas_for_binding() const
  93. {
  94. return *m_canvas_element;
  95. }
  96. void WebGLRenderingContext::needs_to_present()
  97. {
  98. m_should_present = true;
  99. if (!m_canvas_element->paintable())
  100. return;
  101. m_canvas_element->paintable()->set_needs_display();
  102. }
  103. void WebGLRenderingContext::set_error(GLenum error)
  104. {
  105. auto context_error = glGetError();
  106. if (context_error != GL_NO_ERROR)
  107. m_error = context_error;
  108. else
  109. m_error = error;
  110. }
  111. bool WebGLRenderingContext::is_context_lost() const
  112. {
  113. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContext::is_context_lost()");
  114. return m_context_lost;
  115. }
  116. Optional<WebGLContextAttributes> WebGLRenderingContext::get_context_attributes()
  117. {
  118. if (is_context_lost())
  119. return {};
  120. return m_actual_context_parameters;
  121. }
  122. void WebGLRenderingContext::set_size(Gfx::IntSize const& size)
  123. {
  124. Gfx::IntSize final_size;
  125. final_size.set_width(max(size.width(), 1));
  126. final_size.set_height(max(size.height(), 1));
  127. context().set_size(final_size);
  128. }
  129. void WebGLRenderingContext::reset_to_default_state()
  130. {
  131. }
  132. RefPtr<Gfx::PaintingSurface> WebGLRenderingContext::surface()
  133. {
  134. return context().surface();
  135. }
  136. void WebGLRenderingContext::allocate_painting_surface_if_needed()
  137. {
  138. context().allocate_painting_surface_if_needed();
  139. }
  140. Optional<Vector<String>> WebGLRenderingContext::get_supported_extensions()
  141. {
  142. return context().get_supported_extensions();
  143. }
  144. JS::Object* WebGLRenderingContext::get_extension(String const& name)
  145. {
  146. if (name == "ANGLE_instanced_arrays"sv) {
  147. return MUST(ANGLEInstancedArrays::create(realm()));
  148. }
  149. return nullptr;
  150. }
  151. WebIDL::Long WebGLRenderingContext::drawing_buffer_width() const
  152. {
  153. auto size = canvas_for_binding()->bitmap_size_for_canvas();
  154. return size.width();
  155. }
  156. WebIDL::Long WebGLRenderingContext::drawing_buffer_height() const
  157. {
  158. auto size = canvas_for_binding()->bitmap_size_for_canvas();
  159. return size.height();
  160. }
  161. }