WebGLRenderingContextBase.h 3.6 KB

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