WebGLRenderingContextBase.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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/Layout/Node.h>
  10. #include <LibWeb/WebGL/WebGLRenderingContextBase.h>
  11. namespace Web::WebGL {
  12. WebGLRenderingContextBase::WebGLRenderingContextBase(JS::Realm& realm, HTML::HTMLCanvasElement& canvas_element, NonnullOwnPtr<GL::GLContext> context, WebGLContextAttributes context_creation_parameters, WebGLContextAttributes actual_context_parameters)
  13. : PlatformObject(realm)
  14. , m_canvas_element(canvas_element)
  15. , m_context(move(context))
  16. , m_context_creation_parameters(move(context_creation_parameters))
  17. , m_actual_context_parameters(move(actual_context_parameters))
  18. {
  19. }
  20. WebGLRenderingContextBase::~WebGLRenderingContextBase() = default;
  21. void WebGLRenderingContextBase::visit_edges(Cell::Visitor& visitor)
  22. {
  23. Base::visit_edges(visitor);
  24. visitor.visit(m_canvas_element.ptr());
  25. }
  26. #define RETURN_WITH_WEBGL_ERROR_IF(condition, error) \
  27. if (condition) { \
  28. dbgln_if(WEBGL_CONTEXT_DEBUG, "{}(): error {:#x}", __func__, error); \
  29. set_error(error); \
  30. return; \
  31. }
  32. void WebGLRenderingContextBase::present()
  33. {
  34. if (!m_should_present)
  35. return;
  36. m_should_present = false;
  37. // "Before the drawing buffer is presented for compositing the implementation shall ensure that all rendering operations have been flushed to the drawing buffer."
  38. // FIXME: Is this the operation it means?
  39. m_context->gl_flush();
  40. m_context->present();
  41. // "By default, after compositing the contents of the drawing buffer shall be cleared to their default values, as shown in the table above.
  42. // This default behavior can be changed by setting the preserveDrawingBuffer attribute of the WebGLContextAttributes object.
  43. // If this flag is true, the contents of the drawing buffer shall be preserved until the author either clears or overwrites them."
  44. if (!m_context_creation_parameters.preserve_drawing_buffer) {
  45. Array<GLdouble, 4> current_clear_color;
  46. m_context->gl_get_doublev(GL_COLOR_CLEAR_VALUE, current_clear_color.data());
  47. GLdouble current_clear_depth;
  48. m_context->gl_get_doublev(GL_DEPTH_CLEAR_VALUE, &current_clear_depth);
  49. GLint current_clear_stencil;
  50. m_context->gl_get_integerv(GL_STENCIL_CLEAR_VALUE, &current_clear_stencil);
  51. // The implicit clear value for the color buffer is (0, 0, 0, 0)
  52. m_context->gl_clear_color(0, 0, 0, 0);
  53. // The implicit clear value for the depth buffer is 1.0.
  54. m_context->gl_clear_depth(1.0);
  55. // The implicit clear value for the stencil buffer is 0.
  56. m_context->gl_clear_stencil(0);
  57. m_context->gl_clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  58. // Restore the clear values.
  59. m_context->gl_clear_color(current_clear_color[0], current_clear_color[1], current_clear_color[2], current_clear_color[3]);
  60. m_context->gl_clear_depth(current_clear_depth);
  61. m_context->gl_clear_stencil(current_clear_stencil);
  62. }
  63. }
  64. HTML::HTMLCanvasElement& WebGLRenderingContextBase::canvas_element()
  65. {
  66. return *m_canvas_element;
  67. }
  68. HTML::HTMLCanvasElement const& WebGLRenderingContextBase::canvas_element() const
  69. {
  70. return *m_canvas_element;
  71. }
  72. JS::NonnullGCPtr<HTML::HTMLCanvasElement> WebGLRenderingContextBase::canvas_for_binding() const
  73. {
  74. return *m_canvas_element;
  75. }
  76. void WebGLRenderingContextBase::needs_to_present()
  77. {
  78. m_should_present = true;
  79. if (!canvas_element().layout_node())
  80. return;
  81. canvas_element().layout_node()->set_needs_display();
  82. }
  83. void WebGLRenderingContextBase::set_error(GLenum error)
  84. {
  85. auto context_error = m_context->gl_get_error();
  86. if (context_error != GL_NO_ERROR)
  87. m_error = context_error;
  88. else
  89. m_error = error;
  90. }
  91. bool WebGLRenderingContextBase::is_context_lost() const
  92. {
  93. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::is_context_lost()");
  94. return m_context_lost;
  95. }
  96. Optional<Vector<String>> WebGLRenderingContextBase::get_supported_extensions() const
  97. {
  98. if (m_context_lost)
  99. return Optional<Vector<String>> {};
  100. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::get_supported_extensions()");
  101. // FIXME: We don't currently support any extensions.
  102. return Vector<String> {};
  103. }
  104. JS::Object* WebGLRenderingContextBase::get_extension(String const& name) const
  105. {
  106. if (m_context_lost)
  107. return nullptr;
  108. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::get_extension(name='{}')", name);
  109. // FIXME: We don't currently support any extensions.
  110. return nullptr;
  111. }
  112. void WebGLRenderingContextBase::active_texture(GLenum texture)
  113. {
  114. if (m_context_lost)
  115. return;
  116. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::active_texture(texture=0x{:08x})", texture);
  117. m_context->gl_active_texture(texture);
  118. }
  119. void WebGLRenderingContextBase::clear(GLbitfield mask)
  120. {
  121. if (m_context_lost)
  122. return;
  123. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::clear(mask=0x{:08x})", mask);
  124. m_context->gl_clear(mask);
  125. // FIXME: This should only be done if this is targeting the front buffer.
  126. needs_to_present();
  127. }
  128. void WebGLRenderingContextBase::clear_color(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)
  129. {
  130. if (m_context_lost)
  131. return;
  132. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::clear_color(red={}, green={}, blue={}, alpha={})", red, green, blue, alpha);
  133. m_context->gl_clear_color(red, green, blue, alpha);
  134. }
  135. void WebGLRenderingContextBase::clear_depth(GLclampf depth)
  136. {
  137. if (m_context_lost)
  138. return;
  139. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::clear_depth(depth={})", depth);
  140. m_context->gl_clear_depth(depth);
  141. }
  142. void WebGLRenderingContextBase::clear_stencil(GLint s)
  143. {
  144. if (m_context_lost)
  145. return;
  146. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::clear_stencil(s=0x{:08x})", s);
  147. m_context->gl_clear_stencil(s);
  148. }
  149. void WebGLRenderingContextBase::color_mask(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha)
  150. {
  151. if (m_context_lost)
  152. return;
  153. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::color_mask(red={}, green={}, blue={}, alpha={})", red, green, blue, alpha);
  154. m_context->gl_color_mask(red, green, blue, alpha);
  155. }
  156. void WebGLRenderingContextBase::cull_face(GLenum mode)
  157. {
  158. if (m_context_lost)
  159. return;
  160. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::cull_face(mode=0x{:08x})", mode);
  161. m_context->gl_cull_face(mode);
  162. }
  163. void WebGLRenderingContextBase::depth_func(GLenum func)
  164. {
  165. if (m_context_lost)
  166. return;
  167. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::depth_func(func=0x{:08x})", func);
  168. m_context->gl_depth_func(func);
  169. }
  170. void WebGLRenderingContextBase::depth_mask(GLboolean mask)
  171. {
  172. if (m_context_lost)
  173. return;
  174. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::depth_mask(mask={})", mask);
  175. m_context->gl_depth_mask(mask);
  176. }
  177. void WebGLRenderingContextBase::depth_range(GLclampf z_near, GLclampf z_far)
  178. {
  179. if (m_context_lost)
  180. return;
  181. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::depth_range(z_near={}, z_far={})", z_near, z_far);
  182. // https://www.khronos.org/registry/webgl/specs/latest/1.0/#VIEWPORT_DEPTH_RANGE
  183. // "The WebGL API does not support depth ranges with where the near plane is mapped to a value greater than that of the far plane. A call to depthRange will generate an INVALID_OPERATION error if zNear is greater than zFar."
  184. RETURN_WITH_WEBGL_ERROR_IF(z_near > z_far, GL_INVALID_OPERATION);
  185. m_context->gl_depth_range(z_near, z_far);
  186. }
  187. void WebGLRenderingContextBase::finish()
  188. {
  189. if (m_context_lost)
  190. return;
  191. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::finish()");
  192. m_context->gl_finish();
  193. }
  194. void WebGLRenderingContextBase::flush()
  195. {
  196. if (m_context_lost)
  197. return;
  198. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::flush()");
  199. m_context->gl_flush();
  200. }
  201. void WebGLRenderingContextBase::front_face(GLenum mode)
  202. {
  203. if (m_context_lost)
  204. return;
  205. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::front_face(mode=0x{:08x})", mode);
  206. m_context->gl_front_face(mode);
  207. }
  208. GLenum WebGLRenderingContextBase::get_error()
  209. {
  210. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::get_error()");
  211. // "If the context's webgl context lost flag is set, returns CONTEXT_LOST_WEBGL the first time this method is called. Afterward, returns NO_ERROR until the context has been restored."
  212. // FIXME: The plan here is to make the context lost handler unconditionally set m_error to CONTEXT_LOST_WEBGL, which we currently do not have.
  213. // The idea for the unconditional set is that any potentially error generating functions will not execute when the context is lost.
  214. if (m_error != GL_NO_ERROR || m_context_lost) {
  215. auto last_error = m_error;
  216. m_error = GL_NO_ERROR;
  217. return last_error;
  218. }
  219. return m_context->gl_get_error();
  220. }
  221. void WebGLRenderingContextBase::line_width(GLfloat width)
  222. {
  223. if (m_context_lost)
  224. return;
  225. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::line_width(width={})", width);
  226. // https://www.khronos.org/registry/webgl/specs/latest/1.0/#NAN_LINE_WIDTH
  227. // "In the WebGL API, if the width parameter passed to lineWidth is set to NaN, an INVALID_VALUE error is generated and the line width is not changed."
  228. RETURN_WITH_WEBGL_ERROR_IF(isnan(width), GL_INVALID_VALUE);
  229. m_context->gl_line_width(width);
  230. }
  231. void WebGLRenderingContextBase::polygon_offset(GLfloat factor, GLfloat units)
  232. {
  233. if (m_context_lost)
  234. return;
  235. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::polygon_offset(factor={}, units={})", factor, units);
  236. m_context->gl_polygon_offset(factor, units);
  237. }
  238. void WebGLRenderingContextBase::scissor(GLint x, GLint y, GLsizei width, GLsizei height)
  239. {
  240. if (m_context_lost)
  241. return;
  242. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::scissor(x={}, y={}, width={}, height={})", x, y, width, height);
  243. m_context->gl_scissor(x, y, width, height);
  244. }
  245. void WebGLRenderingContextBase::stencil_op(GLenum fail, GLenum zfail, GLenum zpass)
  246. {
  247. if (m_context_lost)
  248. return;
  249. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::stencil_op(fail=0x{:08x}, zfail=0x{:08x}, zpass=0x{:08x})", fail, zfail, zpass);
  250. m_context->gl_stencil_op_separate(GL_FRONT_AND_BACK, fail, zfail, zpass);
  251. }
  252. void WebGLRenderingContextBase::stencil_op_separate(GLenum face, GLenum fail, GLenum zfail, GLenum zpass)
  253. {
  254. if (m_context_lost)
  255. return;
  256. 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);
  257. m_context->gl_stencil_op_separate(face, fail, zfail, zpass);
  258. }
  259. void WebGLRenderingContextBase::viewport(GLint x, GLint y, GLsizei width, GLsizei height)
  260. {
  261. if (m_context_lost)
  262. return;
  263. dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContextBase::viewport(x={}, y={}, width={}, height={})", x, y, width, height);
  264. m_context->gl_viewport(x, y, width, height);
  265. }
  266. }