WebGLRenderingContextBase.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Debug.h>
  7. #include <LibGL/GLContext.h>
  8. #include <LibWeb/HTML/HTMLCanvasElement.h>
  9. #include <LibWeb/WebGL/WebGLRenderingContextBase.h>
  10. namespace Web::WebGL {
  11. WebGLRenderingContextBase::WebGLRenderingContextBase(HTML::HTMLCanvasElement& canvas_element, NonnullOwnPtr<GL::GLContext> context, WebGLContextAttributes context_creation_parameters, WebGLContextAttributes actual_context_parameters)
  12. : m_canvas_element(canvas_element)
  13. , m_context(move(context))
  14. , m_context_creation_parameters(move(context_creation_parameters))
  15. , m_actual_context_parameters(move(actual_context_parameters))
  16. {
  17. }
  18. WebGLRenderingContextBase::~WebGLRenderingContextBase() = default;
  19. void WebGLRenderingContextBase::present()
  20. {
  21. if (!m_should_present)
  22. return;
  23. m_should_present = false;
  24. // "Before the drawing buffer is presented for compositing the implementation shall ensure that all rendering operations have been flushed to the drawing buffer."
  25. // FIXME: Is this the operation it means?
  26. m_context->gl_flush();
  27. m_context->present();
  28. // "By default, after compositing the contents of the drawing buffer shall be cleared to their default values, as shown in the table above.
  29. // This default behavior can be changed by setting the preserveDrawingBuffer attribute of the WebGLContextAttributes object.
  30. // If this flag is true, the contents of the drawing buffer shall be preserved until the author either clears or overwrites them."
  31. if (!m_context_creation_parameters.preserve_drawing_buffer) {
  32. auto current_clear_color = m_context->current_clear_color();
  33. auto current_clear_depth = m_context->current_clear_depth();
  34. auto current_clear_stencil = m_context->current_clear_stencil();
  35. // The implicit clear value for the color buffer is (0, 0, 0, 0)
  36. m_context->gl_clear_color(0, 0, 0, 0);
  37. // The implicit clear value for the depth buffer is 1.0.
  38. m_context->gl_clear_depth(1.0);
  39. // The implicit clear value for the stencil buffer is 0.
  40. m_context->gl_clear_stencil(0);
  41. m_context->gl_clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  42. // Restore the clear values.
  43. m_context->gl_clear_color(current_clear_color[0], current_clear_color[1], current_clear_color[2], current_clear_color[3]);
  44. m_context->gl_clear_depth(current_clear_depth);
  45. m_context->gl_clear_stencil(current_clear_stencil);
  46. }
  47. }
  48. void WebGLRenderingContextBase::needs_to_present()
  49. {
  50. m_should_present = true;
  51. if (!m_canvas_element)
  52. return;
  53. if (!m_canvas_element->layout_node())
  54. return;
  55. m_canvas_element->layout_node()->set_needs_display();
  56. }
  57. Optional<Vector<String>> WebGLRenderingContextBase::get_supported_extensions() const
  58. {
  59. if (m_context_lost)
  60. return Optional<Vector<String>> {};
  61. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::get_supported_extensions()");
  62. // FIXME: We don't currently support any extensions.
  63. return Vector<String> {};
  64. }
  65. JS::Object* WebGLRenderingContextBase::get_extension(String const& name) const
  66. {
  67. if (m_context_lost)
  68. return nullptr;
  69. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::get_extension(name='{}')", name);
  70. // FIXME: We don't currently support any extensions.
  71. return nullptr;
  72. }
  73. void WebGLRenderingContextBase::clear(GLbitfield mask)
  74. {
  75. if (m_context_lost)
  76. return;
  77. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::clear(mask=0x{:08x})", mask);
  78. m_context->gl_clear(mask);
  79. // FIXME: This should only be done if this is targeting the front buffer.
  80. needs_to_present();
  81. }
  82. void WebGLRenderingContextBase::clear_color(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)
  83. {
  84. if (m_context_lost)
  85. return;
  86. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::clear_color(red={}, green={}, blue={}, alpha={})", red, green, blue, alpha);
  87. m_context->gl_clear_color(red, green, blue, alpha);
  88. }
  89. }