From 8ed5ed3ec07f77b8be8c286c43fafb60dbc6c495 Mon Sep 17 00:00:00 2001 From: Andrew Kaster Date: Fri, 16 Sep 2022 05:58:30 -0600 Subject: [PATCH] LibGL: Make GL::create_context fallible Propagate errors in places that are already set up to handle them, like WebGLRenderingContext and the Tubes demo, and convert other callers to using MUST. --- Tests/LibGL/TestAPI.cpp | 2 +- Tests/LibGL/TestRender.cpp | 2 +- Userland/Applications/3DFileViewer/main.cpp | 2 +- Userland/Demos/Tubes/Tubes.cpp | 2 +- Userland/Libraries/LibGL/GLContext.cpp | 6 +++--- Userland/Libraries/LibGL/GLContext.h | 2 +- .../LibWeb/WebGL/WebGLRenderingContext.cpp | 18 ++++++------------ 7 files changed, 14 insertions(+), 20 deletions(-) diff --git a/Tests/LibGL/TestAPI.cpp b/Tests/LibGL/TestAPI.cpp index 7f717db686b..b539c5db883 100644 --- a/Tests/LibGL/TestAPI.cpp +++ b/Tests/LibGL/TestAPI.cpp @@ -11,7 +11,7 @@ static NonnullOwnPtr create_testing_context() { auto bitmap = MUST(Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRx8888, { 1, 1 })); - auto context = GL::create_context(*bitmap); + auto context = MUST(GL::create_context(*bitmap)); GL::make_context_current(context); return context; } diff --git a/Tests/LibGL/TestRender.cpp b/Tests/LibGL/TestRender.cpp index f55bbf53710..9b01dfac989 100644 --- a/Tests/LibGL/TestRender.cpp +++ b/Tests/LibGL/TestRender.cpp @@ -24,7 +24,7 @@ static NonnullOwnPtr create_testing_context(int width, int height) { auto bitmap = MUST(Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRx8888, { width, height })); - auto context = GL::create_context(*bitmap); + auto context = MUST(GL::create_context(*bitmap)); GL::make_context_current(context); // Assume some defaults for our testing contexts diff --git a/Userland/Applications/3DFileViewer/main.cpp b/Userland/Applications/3DFileViewer/main.cpp index 431894dc970..ad71c307efb 100644 --- a/Userland/Applications/3DFileViewer/main.cpp +++ b/Userland/Applications/3DFileViewer/main.cpp @@ -59,7 +59,7 @@ private: constexpr u16 RENDER_WIDTH = 640; constexpr u16 RENDER_HEIGHT = 480; m_bitmap = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRx8888, { RENDER_WIDTH, RENDER_HEIGHT }).release_value_but_fixme_should_propagate_errors(); - m_context = GL::create_context(*m_bitmap); + m_context = MUST(GL::create_context(*m_bitmap)); start_timer(20); diff --git a/Userland/Demos/Tubes/Tubes.cpp b/Userland/Demos/Tubes/Tubes.cpp index 17713e4b7a3..cab53975c55 100644 --- a/Userland/Demos/Tubes/Tubes.cpp +++ b/Userland/Demos/Tubes/Tubes.cpp @@ -125,7 +125,7 @@ void Tubes::choose_new_direction_for_tube(Tube& tube) ErrorOr Tubes::create_buffer(Gfx::IntSize size) { m_bitmap = TRY(Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRx8888, size)); - m_gl_context = GL::create_context(*m_bitmap); + m_gl_context = TRY(GL::create_context(*m_bitmap)); return {}; } diff --git a/Userland/Libraries/LibGL/GLContext.cpp b/Userland/Libraries/LibGL/GLContext.cpp index 316f79714c7..f71c51a50b5 100644 --- a/Userland/Libraries/LibGL/GLContext.cpp +++ b/Userland/Libraries/LibGL/GLContext.cpp @@ -921,11 +921,11 @@ void GLContext::build_extension_string() m_extensions = String::join(' ', extensions); } -NonnullOwnPtr create_context(Gfx::Bitmap& bitmap) +ErrorOr> create_context(Gfx::Bitmap& bitmap) { // FIXME: Make driver selectable. This is currently hardcoded to LibSoftGPU - auto driver = MUST(GPU::Driver::try_create("softgpu"sv)); - auto device = MUST(driver->try_create_device(bitmap.size())); + auto driver = TRY(GPU::Driver::try_create("softgpu"sv)); + auto device = TRY(driver->try_create_device(bitmap.size())); auto context = make(driver, move(device), bitmap); dbgln_if(GL_DEBUG, "GL::create_context({}) -> {:p}", bitmap.size(), context.ptr()); diff --git a/Userland/Libraries/LibGL/GLContext.h b/Userland/Libraries/LibGL/GLContext.h index a2f6ef7dbfa..c8b5bf20f3f 100644 --- a/Userland/Libraries/LibGL/GLContext.h +++ b/Userland/Libraries/LibGL/GLContext.h @@ -532,7 +532,7 @@ private: String m_extensions; }; -NonnullOwnPtr create_context(Gfx::Bitmap&); +ErrorOr> create_context(Gfx::Bitmap&); void make_context_current(GLContext*); void present_context(GLContext*); diff --git a/Userland/Libraries/LibWeb/WebGL/WebGLRenderingContext.cpp b/Userland/Libraries/LibWeb/WebGL/WebGLRenderingContext.cpp index 27b0f03bed7..824425b6682 100644 --- a/Userland/Libraries/LibWeb/WebGL/WebGLRenderingContext.cpp +++ b/Userland/Libraries/LibWeb/WebGL/WebGLRenderingContext.cpp @@ -41,18 +41,12 @@ JS::ThrowCompletionOr> WebGLRenderingContext::c return JS::GCPtr { nullptr }; } -#ifndef __serenity__ - // FIXME: Make WebGL work on other platforms. - (void)window; - (void)context_attributes; - dbgln("FIXME: WebGL not supported on the current platform"); - fire_webgl_context_creation_error(canvas_element); - return JS::GCPtr { nullptr }; -#else - // FIXME: LibGL currently doesn't propagate context creation errors. - auto context = GL::create_context(*canvas_element.bitmap()); - return window.heap().allocate(window.realm(), window, canvas_element, move(context), context_attributes, context_attributes); -#endif + auto context_or_error = GL::create_context(*canvas_element.bitmap()); + if (context_or_error.is_error()) { + fire_webgl_context_creation_error(canvas_element); + return JS::GCPtr { nullptr }; + } + return window.heap().allocate(window.realm(), window, canvas_element, context_or_error.release_value(), context_attributes, context_attributes); } WebGLRenderingContext::WebGLRenderingContext(HTML::Window& window, HTML::HTMLCanvasElement& canvas_element, NonnullOwnPtr context, WebGLContextAttributes context_creation_parameters, WebGLContextAttributes actual_context_parameters)