WebGLRenderingContextBase.h 3.7 KB

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