WebGLRenderingContext.h 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. // FIXME: This is a hack required to visit context from WebGLObject.
  23. // It should be gone once WebGLRenderingContextBase inherits from PlatformObject.
  24. GC::Cell const* gc_cell() const override { return this; }
  25. void present() override;
  26. void needs_to_present() override;
  27. GC::Ref<HTML::HTMLCanvasElement> canvas_for_binding() const;
  28. bool is_context_lost() const;
  29. Optional<WebGLContextAttributes> get_context_attributes();
  30. RefPtr<Gfx::PaintingSurface> surface();
  31. void allocate_painting_surface_if_needed();
  32. void set_size(Gfx::IntSize const&);
  33. void reset_to_default_state();
  34. Optional<Vector<String>> get_supported_extensions();
  35. JS::Object* get_extension(String const& name);
  36. WebIDL::Long drawing_buffer_width() const;
  37. WebIDL::Long drawing_buffer_height() const;
  38. private:
  39. virtual void initialize(JS::Realm&) override;
  40. WebGLRenderingContext(JS::Realm&, HTML::HTMLCanvasElement&, NonnullOwnPtr<OpenGLContext> context, WebGLContextAttributes context_creation_parameters, WebGLContextAttributes actual_context_parameters);
  41. virtual void visit_edges(Cell::Visitor&) override;
  42. GC::Ref<HTML::HTMLCanvasElement> m_canvas_element;
  43. // https://www.khronos.org/registry/webgl/specs/latest/1.0/#context-creation-parameters
  44. // Each WebGLRenderingContext has context creation parameters, set upon creation, in a WebGLContextAttributes object.
  45. WebGLContextAttributes m_context_creation_parameters {};
  46. // https://www.khronos.org/registry/webgl/specs/latest/1.0/#actual-context-parameters
  47. // Each WebGLRenderingContext has actual context parameters, set each time the drawing buffer is created, in a WebGLContextAttributes object.
  48. WebGLContextAttributes m_actual_context_parameters {};
  49. // https://www.khronos.org/registry/webgl/specs/latest/1.0/#webgl-context-lost-flag
  50. // Each WebGLRenderingContext has a webgl context lost flag, which is initially unset.
  51. bool m_context_lost { false };
  52. // 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:
  53. // - Context creation
  54. // - Canvas resize
  55. // - clear, drawArrays, or drawElements has been called while the drawing buffer is the currently bound framebuffer
  56. bool m_should_present { true };
  57. GLenum m_error { 0 };
  58. virtual void set_error(GLenum error) override;
  59. };
  60. void fire_webgl_context_event(HTML::HTMLCanvasElement& canvas_element, FlyString const& type);
  61. void fire_webgl_context_creation_error(HTML::HTMLCanvasElement& canvas_element);
  62. }