WebGL2RenderingContext.cpp 5.5 KB

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