WebGL2RenderingContext.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
  3. * Copyright (c) 2024, Andrew Kaster <andrew@ladybird.org>
  4. * Copyright (c) 2024, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #pragma once
  9. #include <LibGC/Ptr.h>
  10. #include <LibWeb/Bindings/PlatformObject.h>
  11. #include <LibWeb/Forward.h>
  12. #include <LibWeb/WebGL/Types.h>
  13. #include <LibWeb/WebGL/WebGL2RenderingContextImpl.h>
  14. #include <LibWeb/WebGL/WebGLContextAttributes.h>
  15. namespace Web::WebGL {
  16. class WebGL2RenderingContext : public Bindings::PlatformObject
  17. , public WebGL2RenderingContextImpl {
  18. WEB_PLATFORM_OBJECT(WebGL2RenderingContext, Bindings::PlatformObject);
  19. GC_DECLARE_ALLOCATOR(WebGL2RenderingContext);
  20. public:
  21. static JS::ThrowCompletionOr<GC::Ptr<WebGL2RenderingContext>> create(JS::Realm&, HTML::HTMLCanvasElement& canvas_element, JS::Value options);
  22. virtual ~WebGL2RenderingContext() override;
  23. // FIXME: This is a hack required to visit context from WebGLObject.
  24. // It should be gone once WebGLRenderingContextBase inherits from PlatformObject.
  25. GC::Cell const* gc_cell() const override { return this; }
  26. void present() override;
  27. void needs_to_present() override;
  28. GC::Ref<HTML::HTMLCanvasElement> canvas_for_binding() const;
  29. bool is_context_lost() const;
  30. Optional<WebGLContextAttributes> get_context_attributes();
  31. RefPtr<Gfx::PaintingSurface> surface();
  32. void allocate_painting_surface_if_needed();
  33. void set_size(Gfx::IntSize const&);
  34. void reset_to_default_state();
  35. Optional<Vector<String>> get_supported_extensions();
  36. JS::Object* get_extension(String const& name);
  37. WebIDL::Long drawing_buffer_width() const;
  38. WebIDL::Long drawing_buffer_height() const;
  39. private:
  40. virtual void initialize(JS::Realm&) override;
  41. WebGL2RenderingContext(JS::Realm&, HTML::HTMLCanvasElement&, NonnullOwnPtr<OpenGLContext> context, WebGLContextAttributes context_creation_parameters, WebGLContextAttributes actual_context_parameters);
  42. virtual void visit_edges(Cell::Visitor&) override;
  43. GC::Ref<HTML::HTMLCanvasElement> m_canvas_element;
  44. // https://www.khronos.org/registry/webgl/specs/latest/1.0/#context-creation-parameters
  45. // Each WebGLRenderingContext has context creation parameters, set upon creation, in a WebGLContextAttributes object.
  46. WebGLContextAttributes m_context_creation_parameters {};
  47. // https://www.khronos.org/registry/webgl/specs/latest/1.0/#actual-context-parameters
  48. // Each WebGLRenderingContext has actual context parameters, set each time the drawing buffer is created, in a WebGLContextAttributes object.
  49. WebGLContextAttributes m_actual_context_parameters {};
  50. // https://www.khronos.org/registry/webgl/specs/latest/1.0/#webgl-context-lost-flag
  51. // Each WebGLRenderingContext has a webgl context lost flag, which is initially unset.
  52. bool m_context_lost { false };
  53. // 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:
  54. // - Context creation
  55. // - Canvas resize
  56. // - clear, drawArrays, or drawElements has been called while the drawing buffer is the currently bound framebuffer
  57. bool m_should_present { true };
  58. GLenum m_error { 0 };
  59. virtual void set_error(GLenum error) override;
  60. };
  61. }