WebGLRenderingContext.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. 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. 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. if (!skia_backend_context) {
  46. fire_webgl_context_creation_error(canvas_element);
  47. return GC::Ptr<WebGLRenderingContext> { nullptr };
  48. }
  49. auto context = OpenGLContext::create(*skia_backend_context);
  50. if (!context) {
  51. fire_webgl_context_creation_error(canvas_element);
  52. return GC::Ptr<WebGLRenderingContext> { nullptr };
  53. }
  54. context->set_size(canvas_element.bitmap_size_for_canvas(1, 1));
  55. return realm.create<WebGLRenderingContext>(realm, canvas_element, context.release_nonnull(), context_attributes, context_attributes);
  56. }
  57. WebGLRenderingContext::WebGLRenderingContext(JS::Realm& realm, HTML::HTMLCanvasElement& canvas_element, NonnullOwnPtr<OpenGLContext> context, WebGLContextAttributes context_creation_parameters, WebGLContextAttributes actual_context_parameters)
  58. : PlatformObject(realm)
  59. , WebGLRenderingContextImpl(realm, move(context))
  60. , m_canvas_element(canvas_element)
  61. , m_context_creation_parameters(context_creation_parameters)
  62. , m_actual_context_parameters(actual_context_parameters)
  63. {
  64. }
  65. WebGLRenderingContext::~WebGLRenderingContext() = default;
  66. void WebGLRenderingContext::initialize(JS::Realm& realm)
  67. {
  68. Base::initialize(realm);
  69. WEB_SET_PROTOTYPE_FOR_INTERFACE(WebGLRenderingContext);
  70. }
  71. void WebGLRenderingContext::visit_edges(Cell::Visitor& visitor)
  72. {
  73. Base::visit_edges(visitor);
  74. visitor.visit(m_canvas_element);
  75. }
  76. void WebGLRenderingContext::present()
  77. {
  78. if (!m_should_present)
  79. return;
  80. m_should_present = false;
  81. // "Before the drawing buffer is presented for compositing the implementation shall ensure that all rendering operations have been flushed to the drawing buffer."
  82. glFlush();
  83. // "By default, after compositing the contents of the drawing buffer shall be cleared to their default values, as shown in the table above.
  84. // This default behavior can be changed by setting the preserveDrawingBuffer attribute of the WebGLContextAttributes object.
  85. // If this flag is true, the contents of the drawing buffer shall be preserved until the author either clears or overwrites them."
  86. if (!m_context_creation_parameters.preserve_drawing_buffer) {
  87. context().clear_buffer_to_default_values();
  88. }
  89. }
  90. GC::Ref<HTML::HTMLCanvasElement> WebGLRenderingContext::canvas_for_binding() const
  91. {
  92. return *m_canvas_element;
  93. }
  94. void WebGLRenderingContext::needs_to_present()
  95. {
  96. m_should_present = true;
  97. if (!m_canvas_element->paintable())
  98. return;
  99. m_canvas_element->paintable()->set_needs_display();
  100. }
  101. void WebGLRenderingContext::set_error(GLenum error)
  102. {
  103. auto context_error = glGetError();
  104. if (context_error != GL_NO_ERROR)
  105. m_error = context_error;
  106. else
  107. m_error = error;
  108. }
  109. bool WebGLRenderingContext::is_context_lost() const
  110. {
  111. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContext::is_context_lost()");
  112. return m_context_lost;
  113. }
  114. Optional<WebGLContextAttributes> WebGLRenderingContext::get_context_attributes()
  115. {
  116. if (is_context_lost())
  117. return {};
  118. return m_actual_context_parameters;
  119. }
  120. void WebGLRenderingContext::set_size(Gfx::IntSize const& size)
  121. {
  122. context().set_size(size);
  123. }
  124. void WebGLRenderingContext::reset_to_default_state()
  125. {
  126. }
  127. RefPtr<Gfx::PaintingSurface> WebGLRenderingContext::surface()
  128. {
  129. return context().surface();
  130. }
  131. void WebGLRenderingContext::allocate_painting_surface_if_needed()
  132. {
  133. context().allocate_painting_surface_if_needed();
  134. }
  135. Optional<Vector<String>> WebGLRenderingContext::get_supported_extensions()
  136. {
  137. return context().get_supported_extensions();
  138. }
  139. JS::Object* WebGLRenderingContext::get_extension(String const&)
  140. {
  141. return nullptr;
  142. }
  143. WebIDL::Long WebGLRenderingContext::drawing_buffer_width() const
  144. {
  145. auto size = canvas_for_binding()->bitmap_size_for_canvas();
  146. return size.width();
  147. }
  148. WebIDL::Long WebGLRenderingContext::drawing_buffer_height() const
  149. {
  150. auto size = canvas_for_binding()->bitmap_size_for_canvas();
  151. return size.height();
  152. }
  153. }