WebGLRenderingContextBase.cpp 11 KB

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