123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- /*
- * Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
- * Copyright (c) 2023, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
- #include <LibJS/Runtime/ArrayBuffer.h>
- #include <LibJS/Runtime/TypedArray.h>
- #include <LibWeb/Bindings/Intrinsics.h>
- #include <LibWeb/Bindings/WebGLRenderingContextPrototype.h>
- #include <LibWeb/HTML/HTMLCanvasElement.h>
- #include <LibWeb/HTML/TraversableNavigable.h>
- #include <LibWeb/Painting/Paintable.h>
- #include <LibWeb/WebGL/EventNames.h>
- #include <LibWeb/WebGL/OpenGLContext.h>
- #include <LibWeb/WebGL/WebGLContextEvent.h>
- #include <LibWeb/WebGL/WebGLRenderingContext.h>
- #include <LibWeb/WebGL/WebGLShader.h>
- #include <LibWeb/WebIDL/Buffers.h>
- #include <GLES2/gl2.h>
- #include <GLES2/gl2ext.h>
- namespace Web::WebGL {
- GC_DEFINE_ALLOCATOR(WebGLRenderingContext);
- // https://www.khronos.org/registry/webgl/specs/latest/1.0/#fire-a-webgl-context-event
- static void fire_webgl_context_event(HTML::HTMLCanvasElement& canvas_element, FlyString const& type)
- {
- // To fire a WebGL context event named e means that an event using the WebGLContextEvent interface, with its type attribute [DOM4] initialized to e, its cancelable attribute initialized to true, and its isTrusted attribute [DOM4] initialized to true, is to be dispatched at the given object.
- // FIXME: Consider setting a status message.
- auto event = WebGLContextEvent::create(canvas_element.realm(), type, WebGLContextEventInit {});
- event->set_is_trusted(true);
- event->set_cancelable(true);
- canvas_element.dispatch_event(*event);
- }
- // https://www.khronos.org/registry/webgl/specs/latest/1.0/#fire-a-webgl-context-creation-error
- static void fire_webgl_context_creation_error(HTML::HTMLCanvasElement& canvas_element)
- {
- // 1. Fire a WebGL context event named "webglcontextcreationerror" at canvas, optionally with its statusMessage attribute set to a platform dependent string about the nature of the failure.
- fire_webgl_context_event(canvas_element, EventNames::webglcontextcreationerror);
- }
- JS::ThrowCompletionOr<GC::Ptr<WebGLRenderingContext>> WebGLRenderingContext::create(JS::Realm& realm, HTML::HTMLCanvasElement& canvas_element, JS::Value options)
- {
- // We should be coming here from getContext being called on a wrapped <canvas> element.
- auto context_attributes = TRY(convert_value_to_context_attributes_dictionary(canvas_element.vm(), options));
- auto skia_backend_context = canvas_element.navigable()->traversable_navigable()->skia_backend_context();
- auto context = OpenGLContext::create(*skia_backend_context);
- if (!context) {
- fire_webgl_context_creation_error(canvas_element);
- return GC::Ptr<WebGLRenderingContext> { nullptr };
- }
- context->set_size(canvas_element.bitmap_size_for_canvas(1, 1));
- return realm.create<WebGLRenderingContext>(realm, canvas_element, context.release_nonnull(), context_attributes, context_attributes);
- }
- WebGLRenderingContext::WebGLRenderingContext(JS::Realm& realm, HTML::HTMLCanvasElement& canvas_element, NonnullOwnPtr<OpenGLContext> context, WebGLContextAttributes context_creation_parameters, WebGLContextAttributes actual_context_parameters)
- : PlatformObject(realm)
- , WebGLRenderingContextImpl(realm, move(context))
- , m_canvas_element(canvas_element)
- , m_context_creation_parameters(context_creation_parameters)
- , m_actual_context_parameters(actual_context_parameters)
- {
- }
- WebGLRenderingContext::~WebGLRenderingContext() = default;
- void WebGLRenderingContext::initialize(JS::Realm& realm)
- {
- Base::initialize(realm);
- WEB_SET_PROTOTYPE_FOR_INTERFACE(WebGLRenderingContext);
- }
- void WebGLRenderingContext::visit_edges(Cell::Visitor& visitor)
- {
- Base::visit_edges(visitor);
- visitor.visit(m_canvas_element);
- }
- void WebGLRenderingContext::present()
- {
- if (!m_should_present)
- return;
- m_should_present = false;
- // "Before the drawing buffer is presented for compositing the implementation shall ensure that all rendering operations have been flushed to the drawing buffer."
- glFlush();
- // "By default, after compositing the contents of the drawing buffer shall be cleared to their default values, as shown in the table above.
- // This default behavior can be changed by setting the preserveDrawingBuffer attribute of the WebGLContextAttributes object.
- // If this flag is true, the contents of the drawing buffer shall be preserved until the author either clears or overwrites them."
- if (!m_context_creation_parameters.preserve_drawing_buffer) {
- context().clear_buffer_to_default_values();
- }
- }
- GC::Ref<HTML::HTMLCanvasElement> WebGLRenderingContext::canvas_for_binding() const
- {
- return *m_canvas_element;
- }
- void WebGLRenderingContext::needs_to_present()
- {
- m_should_present = true;
- if (!m_canvas_element->paintable())
- return;
- m_canvas_element->paintable()->set_needs_display();
- }
- void WebGLRenderingContext::set_error(GLenum error)
- {
- auto context_error = glGetError();
- if (context_error != GL_NO_ERROR)
- m_error = context_error;
- else
- m_error = error;
- }
- bool WebGLRenderingContext::is_context_lost() const
- {
- dbgln_if(WEBGL_CONTEXT_DEBUG, "WebGLRenderingContext::is_context_lost()");
- return m_context_lost;
- }
- void WebGLRenderingContext::set_size(Gfx::IntSize const& size)
- {
- context().set_size(size);
- }
- void WebGLRenderingContext::reset_to_default_state()
- {
- }
- RefPtr<Gfx::PaintingSurface> WebGLRenderingContext::surface()
- {
- return context().surface();
- }
- void WebGLRenderingContext::allocate_painting_surface_if_needed()
- {
- context().allocate_painting_surface_if_needed();
- }
- }
|