WebGLRenderingContextBase.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. bool WebGLRenderingContextBase::is_context_lost() const
  58. {
  59. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::is_context_lost()");
  60. return m_context_lost;
  61. }
  62. Optional<Vector<String>> WebGLRenderingContextBase::get_supported_extensions() const
  63. {
  64. if (m_context_lost)
  65. return Optional<Vector<String>> {};
  66. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::get_supported_extensions()");
  67. // FIXME: We don't currently support any extensions.
  68. return Vector<String> {};
  69. }
  70. JS::Object* WebGLRenderingContextBase::get_extension(String const& name) const
  71. {
  72. if (m_context_lost)
  73. return nullptr;
  74. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::get_extension(name='{}')", name);
  75. // FIXME: We don't currently support any extensions.
  76. return nullptr;
  77. }
  78. void WebGLRenderingContextBase::active_texture(GLenum texture)
  79. {
  80. if (m_context_lost)
  81. return;
  82. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::active_texture(texture=0x{:08x})", texture);
  83. m_context->gl_active_texture(texture);
  84. }
  85. void WebGLRenderingContextBase::clear(GLbitfield mask)
  86. {
  87. if (m_context_lost)
  88. return;
  89. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::clear(mask=0x{:08x})", mask);
  90. m_context->gl_clear(mask);
  91. // FIXME: This should only be done if this is targeting the front buffer.
  92. needs_to_present();
  93. }
  94. void WebGLRenderingContextBase::clear_color(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)
  95. {
  96. if (m_context_lost)
  97. return;
  98. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::clear_color(red={}, green={}, blue={}, alpha={})", red, green, blue, alpha);
  99. m_context->gl_clear_color(red, green, blue, alpha);
  100. }
  101. void WebGLRenderingContextBase::clear_depth(GLclampf depth)
  102. {
  103. if (m_context_lost)
  104. return;
  105. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::clear_depth(depth={})", depth);
  106. m_context->gl_clear_depth(depth);
  107. }
  108. void WebGLRenderingContextBase::clear_stencil(GLint s)
  109. {
  110. if (m_context_lost)
  111. return;
  112. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::clear_stencil(s=0x{:08x})", s);
  113. m_context->gl_clear_stencil(s);
  114. }
  115. void WebGLRenderingContextBase::color_mask(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha)
  116. {
  117. if (m_context_lost)
  118. return;
  119. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::color_mask(red={}, green={}, blue={}, alpha={})", red, green, blue, alpha);
  120. m_context->gl_color_mask(red, green, blue, alpha);
  121. }
  122. void WebGLRenderingContextBase::cull_face(GLenum mode)
  123. {
  124. if (m_context_lost)
  125. return;
  126. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::cull_face(mode=0x{:08x})", mode);
  127. m_context->gl_cull_face(mode);
  128. }
  129. void WebGLRenderingContextBase::depth_func(GLenum func)
  130. {
  131. if (m_context_lost)
  132. return;
  133. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::depth_func(func=0x{:08x})", func);
  134. m_context->gl_depth_func(func);
  135. }
  136. void WebGLRenderingContextBase::depth_mask(GLboolean mask)
  137. {
  138. if (m_context_lost)
  139. return;
  140. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::depth_mask(mask={})", mask);
  141. m_context->gl_depth_mask(mask);
  142. }
  143. void WebGLRenderingContextBase::finish()
  144. {
  145. if (m_context_lost)
  146. return;
  147. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::finish()");
  148. m_context->gl_finish();
  149. }
  150. void WebGLRenderingContextBase::flush()
  151. {
  152. if (m_context_lost)
  153. return;
  154. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::flush()");
  155. m_context->gl_flush();
  156. }
  157. void WebGLRenderingContextBase::front_face(GLenum mode)
  158. {
  159. if (m_context_lost)
  160. return;
  161. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::front_face(mode=0x{:08x})", mode);
  162. m_context->gl_front_face(mode);
  163. }
  164. void WebGLRenderingContextBase::polygon_offset(GLfloat factor, GLfloat units)
  165. {
  166. if (m_context_lost)
  167. return;
  168. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::polygon_offset(factor={}, units={})", factor, units);
  169. m_context->gl_polygon_offset(factor, units);
  170. }
  171. void WebGLRenderingContextBase::scissor(GLint x, GLint y, GLsizei width, GLsizei height)
  172. {
  173. if (m_context_lost)
  174. return;
  175. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::scissor(x={}, y={}, width={}, height={})", x, y, width, height);
  176. m_context->gl_scissor(x, y, width, height);
  177. }
  178. void WebGLRenderingContextBase::stencil_op(GLenum fail, GLenum zfail, GLenum zpass)
  179. {
  180. if (m_context_lost)
  181. return;
  182. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::stencil_op(fail=0x{:08x}, zfail=0x{:08x}, zpass=0x{:08x})", fail, zfail, zpass);
  183. m_context->gl_stencil_op_separate(GL_FRONT_AND_BACK, fail, zfail, zpass);
  184. }
  185. void WebGLRenderingContextBase::stencil_op_separate(GLenum face, GLenum fail, GLenum zfail, GLenum zpass)
  186. {
  187. if (m_context_lost)
  188. return;
  189. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::stencil_op_separate(face=0x{:08x}, fail=0x{:08x}, zfail=0x{:08x}, zpass=0x{:08x})", face, fail, zfail, zpass);
  190. m_context->gl_stencil_op_separate(face, fail, zfail, zpass);
  191. }
  192. void WebGLRenderingContextBase::viewport(GLint x, GLint y, GLsizei width, GLsizei height)
  193. {
  194. if (m_context_lost)
  195. return;
  196. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::viewport(x={}, y={}, width={}, height={})", x, y, width, height);
  197. m_context->gl_viewport(x, y, width, height);
  198. }
  199. }