WebGLRenderingContextBase.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 <LibJS/Heap/GCPtr.h>
  12. #include <LibWeb/Bindings/PlatformObject.h>
  13. #include <LibWeb/Forward.h>
  14. #include <LibWeb/WebGL/WebGLContextAttributes.h>
  15. namespace Web::WebGL {
  16. class WebGLRenderingContextBase : public Bindings::PlatformObject {
  17. WEB_PLATFORM_OBJECT(WebGLRenderingContextBase, Bindings::PlatformObject);
  18. public:
  19. virtual ~WebGLRenderingContextBase();
  20. void present();
  21. JS::NonnullGCPtr<HTML::HTMLCanvasElement> canvas_for_binding() const;
  22. bool is_context_lost() const;
  23. Optional<Vector<DeprecatedString>> get_supported_extensions() const;
  24. JS::Object* get_extension(DeprecatedString const& name) const;
  25. void active_texture(GLenum texture);
  26. void clear(GLbitfield mask);
  27. void clear_color(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha);
  28. void clear_depth(GLclampf depth);
  29. void clear_stencil(GLint s);
  30. void color_mask(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha);
  31. void cull_face(GLenum mode);
  32. void depth_func(GLenum func);
  33. void depth_mask(GLboolean mask);
  34. void depth_range(GLclampf z_near, GLclampf z_far);
  35. void finish();
  36. void flush();
  37. void front_face(GLenum mode);
  38. GLenum get_error();
  39. void line_width(GLfloat width);
  40. void polygon_offset(GLfloat factor, GLfloat units);
  41. void scissor(GLint x, GLint y, GLsizei width, GLsizei height);
  42. void stencil_op(GLenum fail, GLenum zfail, GLenum zpass);
  43. void stencil_op_separate(GLenum face, GLenum fail, GLenum zfail, GLenum zpass);
  44. void viewport(GLint x, GLint y, GLsizei width, GLsizei height);
  45. protected:
  46. WebGLRenderingContextBase(JS::Realm&, HTML::HTMLCanvasElement& canvas_element, NonnullOwnPtr<GL::GLContext> context, WebGLContextAttributes context_creation_parameters, WebGLContextAttributes actual_context_parameters);
  47. private:
  48. virtual void visit_edges(Cell::Visitor&) override;
  49. JS::NonnullGCPtr<HTML::HTMLCanvasElement> m_canvas_element;
  50. NonnullOwnPtr<GL::GLContext> m_context;
  51. // https://www.khronos.org/registry/webgl/specs/latest/1.0/#context-creation-parameters
  52. // Each WebGLRenderingContext has context creation parameters, set upon creation, in a WebGLContextAttributes object.
  53. WebGLContextAttributes m_context_creation_parameters {};
  54. // https://www.khronos.org/registry/webgl/specs/latest/1.0/#actual-context-parameters
  55. // Each WebGLRenderingContext has actual context parameters, set each time the drawing buffer is created, in a WebGLContextAttributes object.
  56. WebGLContextAttributes m_actual_context_parameters {};
  57. // https://www.khronos.org/registry/webgl/specs/latest/1.0/#webgl-context-lost-flag
  58. // Each WebGLRenderingContext has a webgl context lost flag, which is initially unset.
  59. bool m_context_lost { false };
  60. // 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:
  61. // - Context creation
  62. // - Canvas resize
  63. // - clear, drawArrays, or drawElements has been called while the drawing buffer is the currently bound framebuffer
  64. bool m_should_present { true };
  65. GLenum m_error { GL_NO_ERROR };
  66. HTML::HTMLCanvasElement& canvas_element();
  67. HTML::HTMLCanvasElement const& canvas_element() const;
  68. void needs_to_present();
  69. void set_error(GLenum error);
  70. };
  71. }