WebGLRenderingContextBase.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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/RefCountForwarder.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 RefCountForwarder<HTML::HTMLCanvasElement>
  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. NonnullOwnPtr<GL::GLContext> m_context;
  47. // https://www.khronos.org/registry/webgl/specs/latest/1.0/#context-creation-parameters
  48. // Each WebGLRenderingContext has context creation parameters, set upon creation, in a WebGLContextAttributes object.
  49. WebGLContextAttributes m_context_creation_parameters {};
  50. // https://www.khronos.org/registry/webgl/specs/latest/1.0/#actual-context-parameters
  51. // Each WebGLRenderingContext has actual context parameters, set each time the drawing buffer is created, in a WebGLContextAttributes object.
  52. WebGLContextAttributes m_actual_context_parameters {};
  53. // https://www.khronos.org/registry/webgl/specs/latest/1.0/#webgl-context-lost-flag
  54. // Each WebGLRenderingContext has a webgl context lost flag, which is initially unset.
  55. bool m_context_lost { false };
  56. // 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:
  57. // - Context creation
  58. // - Canvas resize
  59. // - clear, drawArrays, or drawElements has been called while the drawing buffer is the currently bound framebuffer
  60. bool m_should_present { true };
  61. GLenum m_error { GL_NO_ERROR };
  62. HTML::HTMLCanvasElement& canvas_element();
  63. HTML::HTMLCanvasElement const& canvas_element() const;
  64. void needs_to_present();
  65. void set_error(GLenum error);
  66. };
  67. }