WebGLRenderingContext.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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() const;
  32. JS::Object* get_extension(String const& name);
  33. private:
  34. virtual void initialize(JS::Realm&) override;
  35. WebGLRenderingContext(JS::Realm&, HTML::HTMLCanvasElement&, NonnullOwnPtr<OpenGLContext> context, WebGLContextAttributes context_creation_parameters, WebGLContextAttributes actual_context_parameters);
  36. virtual void visit_edges(Cell::Visitor&) override;
  37. GC::Ref<HTML::HTMLCanvasElement> m_canvas_element;
  38. // https://www.khronos.org/registry/webgl/specs/latest/1.0/#context-creation-parameters
  39. // Each WebGLRenderingContext has context creation parameters, set upon creation, in a WebGLContextAttributes object.
  40. WebGLContextAttributes m_context_creation_parameters {};
  41. // https://www.khronos.org/registry/webgl/specs/latest/1.0/#actual-context-parameters
  42. // Each WebGLRenderingContext has actual context parameters, set each time the drawing buffer is created, in a WebGLContextAttributes object.
  43. WebGLContextAttributes m_actual_context_parameters {};
  44. // https://www.khronos.org/registry/webgl/specs/latest/1.0/#webgl-context-lost-flag
  45. // Each WebGLRenderingContext has a webgl context lost flag, which is initially unset.
  46. bool m_context_lost { false };
  47. // 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:
  48. // - Context creation
  49. // - Canvas resize
  50. // - clear, drawArrays, or drawElements has been called while the drawing buffer is the currently bound framebuffer
  51. bool m_should_present { true };
  52. GLenum m_error { 0 };
  53. virtual void set_error(GLenum error) override;
  54. };
  55. void fire_webgl_context_event(HTML::HTMLCanvasElement& canvas_element, FlyString const& type);
  56. void fire_webgl_context_creation_error(HTML::HTMLCanvasElement& canvas_element);
  57. }