WebGLRenderingContextBase.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/RefCounted.h>
  8. #include <AK/WeakPtr.h>
  9. #include <AK/Weakable.h>
  10. #include <LibGL/GLContext.h>
  11. #include <LibWeb/Forward.h>
  12. #include <LibWeb/WebGL/WebGLContextAttributes.h>
  13. namespace Web::WebGL {
  14. class WebGLRenderingContextBase
  15. : public RefCounted<WebGLRenderingContextBase>
  16. , public Weakable<WebGLRenderingContextBase> {
  17. public:
  18. virtual ~WebGLRenderingContextBase();
  19. void present();
  20. bool is_context_lost() const;
  21. Optional<Vector<String>> get_supported_extensions() const;
  22. JS::Object* get_extension(String const& name) const;
  23. void active_texture(GLenum texture);
  24. void clear(GLbitfield mask);
  25. void clear_color(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha);
  26. void clear_depth(GLclampf depth);
  27. void clear_stencil(GLint s);
  28. void color_mask(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha);
  29. void cull_face(GLenum mode);
  30. void depth_func(GLenum func);
  31. void depth_mask(GLboolean mask);
  32. void depth_range(GLclampf z_near, GLclampf z_far);
  33. void finish();
  34. void flush();
  35. void front_face(GLenum mode);
  36. GLenum get_error();
  37. void line_width(GLfloat width);
  38. void polygon_offset(GLfloat factor, GLfloat units);
  39. void scissor(GLint x, GLint y, GLsizei width, GLsizei height);
  40. void stencil_op(GLenum fail, GLenum zfail, GLenum zpass);
  41. void stencil_op_separate(GLenum face, GLenum fail, GLenum zfail, GLenum zpass);
  42. void viewport(GLint x, GLint y, GLsizei width, GLsizei height);
  43. protected:
  44. WebGLRenderingContextBase(HTML::HTMLCanvasElement& canvas_element, NonnullOwnPtr<GL::GLContext> context, WebGLContextAttributes context_creation_parameters, WebGLContextAttributes actual_context_parameters);
  45. private:
  46. WeakPtr<HTML::HTMLCanvasElement> m_canvas_element;
  47. NonnullOwnPtr<GL::GLContext> m_context;
  48. // https://www.khronos.org/registry/webgl/specs/latest/1.0/#context-creation-parameters
  49. // Each WebGLRenderingContext has context creation parameters, set upon creation, in a WebGLContextAttributes object.
  50. WebGLContextAttributes m_context_creation_parameters {};
  51. // https://www.khronos.org/registry/webgl/specs/latest/1.0/#actual-context-parameters
  52. // Each WebGLRenderingContext has actual context parameters, set each time the drawing buffer is created, in a WebGLContextAttributes object.
  53. WebGLContextAttributes m_actual_context_parameters {};
  54. // https://www.khronos.org/registry/webgl/specs/latest/1.0/#webgl-context-lost-flag
  55. // Each WebGLRenderingContext has a webgl context lost flag, which is initially unset.
  56. bool m_context_lost { false };
  57. // WebGL presents its drawing buffer to the HTML page compositor immediately before a compositing operation, but only if at least one of the following has occurred since the previous compositing operation:
  58. // - Context creation
  59. // - Canvas resize
  60. // - clear, drawArrays, or drawElements has been called while the drawing buffer is the currently bound framebuffer
  61. bool m_should_present { true };
  62. GLenum m_error { GL_NO_ERROR };
  63. void needs_to_present();
  64. void set_error(GLenum error);
  65. };
  66. }