WebGLRenderingContextBase.cpp 11 KB

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