WebGLRenderingContext.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 <LibGC/Ptr.h>
  9. #include <LibWeb/Bindings/PlatformObject.h>
  10. #include <LibWeb/Forward.h>
  11. #include <LibWeb/WebGL/Types.h>
  12. #include <LibWeb/WebGL/WebGLContextAttributes.h>
  13. #include <LibWeb/WebGL/WebGLRenderingContextImpl.h>
  14. namespace Web::WebGL {
  15. class WebGLRenderingContext : public Bindings::PlatformObject
  16. , public WebGLRenderingContextImpl {
  17. WEB_PLATFORM_OBJECT(WebGLRenderingContext, Bindings::PlatformObject);
  18. GC_DECLARE_ALLOCATOR(WebGLRenderingContext);
  19. public:
  20. static JS::ThrowCompletionOr<GC::Ptr<WebGLRenderingContext>> create(JS::Realm&, HTML::HTMLCanvasElement& canvas_element, JS::Value options);
  21. virtual ~WebGLRenderingContext() override;
  22. void present() override;
  23. void needs_to_present() override;
  24. GC::Ref<HTML::HTMLCanvasElement> canvas_for_binding() const;
  25. bool is_context_lost() const;
  26. Optional<WebGLContextAttributes> get_context_attributes();
  27. RefPtr<Gfx::PaintingSurface> surface();
  28. void allocate_painting_surface_if_needed();
  29. void set_size(Gfx::IntSize const&);
  30. void reset_to_default_state();
  31. Optional<Vector<String>> get_supported_extensions();
  32. JS::Object* get_extension(String const& name);
  33. WebIDL::Long drawing_buffer_width() const;
  34. WebIDL::Long drawing_buffer_height() const;
  35. private:
  36. virtual void initialize(JS::Realm&) override;
  37. WebGLRenderingContext(JS::Realm&, HTML::HTMLCanvasElement&, NonnullOwnPtr<OpenGLContext> context, WebGLContextAttributes context_creation_parameters, WebGLContextAttributes actual_context_parameters);
  38. virtual void visit_edges(Cell::Visitor&) override;
  39. GC::Ref<HTML::HTMLCanvasElement> m_canvas_element;
  40. // https://www.khronos.org/registry/webgl/specs/latest/1.0/#context-creation-parameters
  41. // Each WebGLRenderingContext has context creation parameters, set upon creation, in a WebGLContextAttributes object.
  42. WebGLContextAttributes m_context_creation_parameters {};
  43. // https://www.khronos.org/registry/webgl/specs/latest/1.0/#actual-context-parameters
  44. // Each WebGLRenderingContext has actual context parameters, set each time the drawing buffer is created, in a WebGLContextAttributes object.
  45. WebGLContextAttributes m_actual_context_parameters {};
  46. // https://www.khronos.org/registry/webgl/specs/latest/1.0/#webgl-context-lost-flag
  47. // Each WebGLRenderingContext has a webgl context lost flag, which is initially unset.
  48. bool m_context_lost { false };
  49. // 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:
  50. // - Context creation
  51. // - Canvas resize
  52. // - clear, drawArrays, or drawElements has been called while the drawing buffer is the currently bound framebuffer
  53. bool m_should_present { true };
  54. GLenum m_error { 0 };
  55. virtual void set_error(GLenum error) override;
  56. };
  57. void fire_webgl_context_event(HTML::HTMLCanvasElement& canvas_element, FlyString const& type);
  58. void fire_webgl_context_creation_error(HTML::HTMLCanvasElement& canvas_element);
  59. }