mirror of
https://github.com/RGBCube/serenity
synced 2025-05-14 08:34:57 +00:00
LibAccelGfx+LibWeb+WebContent: Handle OpenGL Context init errors
Now, if OpenGL context fails, an error will be returned and a message printed, instead of crashing.
This commit is contained in:
parent
315c95a1ce
commit
980b61470c
4 changed files with 65 additions and 15 deletions
|
@ -4,6 +4,7 @@
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <AK/Error.h>
|
||||||
#include <LibAccelGfx/Context.h>
|
#include <LibAccelGfx/Context.h>
|
||||||
|
|
||||||
#ifdef AK_OS_MACOS
|
#ifdef AK_OS_MACOS
|
||||||
|
@ -61,7 +62,7 @@ private:
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef AK_OS_MACOS
|
#ifdef AK_OS_MACOS
|
||||||
static NonnullOwnPtr<CGLContextWrapper> make_context_cgl()
|
static ErrorOr<NonnullOwnPtr<CGLContextWrapper>> make_context_cgl()
|
||||||
{
|
{
|
||||||
CGLContextObj context = NULL;
|
CGLContextObj context = NULL;
|
||||||
CGLPixelFormatAttribute attributes[4] = {
|
CGLPixelFormatAttribute attributes[4] = {
|
||||||
|
@ -75,17 +76,20 @@ static NonnullOwnPtr<CGLContextWrapper> make_context_cgl()
|
||||||
GLint numPixelFormats = 0;
|
GLint numPixelFormats = 0;
|
||||||
CGLError error = CGLChoosePixelFormat(attributes, &pixelFormat, &numPixelFormats);
|
CGLError error = CGLChoosePixelFormat(attributes, &pixelFormat, &numPixelFormats);
|
||||||
if (error) {
|
if (error) {
|
||||||
VERIFY_NOT_REACHED();
|
auto const* cgl_error_string = CGLErrorString(error);
|
||||||
|
return Error::from_string_view(StringView { cgl_error_string, strlen(cgl_error_string) });
|
||||||
}
|
}
|
||||||
|
|
||||||
error = CGLCreateContext(pixelFormat, NULL, &context);
|
error = CGLCreateContext(pixelFormat, NULL, &context);
|
||||||
if (error) {
|
if (error) {
|
||||||
VERIFY_NOT_REACHED();
|
auto const* cgl_error_string = CGLErrorString(error);
|
||||||
|
return Error::from_string_view(StringView { cgl_error_string, strlen(cgl_error_string) });
|
||||||
}
|
}
|
||||||
|
|
||||||
error = CGLSetCurrentContext(context);
|
error = CGLSetCurrentContext(context);
|
||||||
if (error) {
|
if (error) {
|
||||||
VERIFY_NOT_REACHED();
|
auto const* cgl_error_string = CGLErrorString(error);
|
||||||
|
return Error::from_string_view(StringView { cgl_error_string, strlen(cgl_error_string) });
|
||||||
}
|
}
|
||||||
|
|
||||||
VERIFY(glGetError() == GL_NO_ERROR);
|
VERIFY(glGetError() == GL_NO_ERROR);
|
||||||
|
@ -93,7 +97,46 @@ static NonnullOwnPtr<CGLContextWrapper> make_context_cgl()
|
||||||
return make<CGLContextWrapper>(context);
|
return make<CGLContextWrapper>(context);
|
||||||
}
|
}
|
||||||
#elif !defined(AK_OS_SERENITY)
|
#elif !defined(AK_OS_SERENITY)
|
||||||
static NonnullOwnPtr<EGLContextWrapper> make_context_egl()
|
|
||||||
|
static StringView format_egl_error(EGLint error)
|
||||||
|
{
|
||||||
|
switch (error) {
|
||||||
|
case EGL_SUCCESS:
|
||||||
|
return "EGL_SUCCESS"sv;
|
||||||
|
case EGL_NOT_INITIALIZED:
|
||||||
|
return "EGL_NOT_INITIALIZED"sv;
|
||||||
|
case EGL_BAD_ACCESS:
|
||||||
|
return "EGL_BAD_ACCESS"sv;
|
||||||
|
case EGL_BAD_ALLOC:
|
||||||
|
return "EGL_BAD_ALLOC"sv;
|
||||||
|
case EGL_BAD_ATTRIBUTE:
|
||||||
|
return "EGL_BAD_ATTRIBUTE"sv;
|
||||||
|
case EGL_BAD_CONTEXT:
|
||||||
|
return "EGL_BAD_CONTEXT"sv;
|
||||||
|
case EGL_BAD_CONFIG:
|
||||||
|
return "EGL_BAD_CONFIG"sv;
|
||||||
|
case EGL_BAD_CURRENT_SURFACE:
|
||||||
|
return "EGL_BAD_CURRENT_SURFACE"sv;
|
||||||
|
case EGL_BAD_DISPLAY:
|
||||||
|
return "EGL_BAD_DISPLAY"sv;
|
||||||
|
case EGL_BAD_SURFACE:
|
||||||
|
return "EGL_BAD_SURFACE"sv;
|
||||||
|
case EGL_BAD_MATCH:
|
||||||
|
return "EGL_BAD_MATCH"sv;
|
||||||
|
case EGL_BAD_PARAMETER:
|
||||||
|
return "EGL_BAD_PARAMETER"sv;
|
||||||
|
case EGL_BAD_NATIVE_PIXMAP:
|
||||||
|
return "EGL_BAD_NATIVE_PIXMAP"sv;
|
||||||
|
case EGL_BAD_NATIVE_WINDOW:
|
||||||
|
return "EGL_BAD_NATIVE_WINDOW"sv;
|
||||||
|
case EGL_CONTEXT_LOST:
|
||||||
|
return "EGL_CONTEXT_LOST"sv;
|
||||||
|
default:
|
||||||
|
return "Unknown error"sv;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static ErrorOr<NonnullOwnPtr<EGLContextWrapper>> make_context_egl()
|
||||||
{
|
{
|
||||||
EGLDisplay egl_display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
|
EGLDisplay egl_display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
|
||||||
|
|
||||||
|
@ -128,21 +171,19 @@ static NonnullOwnPtr<EGLContextWrapper> make_context_egl()
|
||||||
};
|
};
|
||||||
EGLContext egl_context = eglCreateContext(egl_display, egl_config, EGL_NO_CONTEXT, context_attributes);
|
EGLContext egl_context = eglCreateContext(egl_display, egl_config, EGL_NO_CONTEXT, context_attributes);
|
||||||
if (egl_context == EGL_FALSE) {
|
if (egl_context == EGL_FALSE) {
|
||||||
dbgln("eglCreateContext failed");
|
return Error::from_string_view(format_egl_error(eglGetError()));
|
||||||
VERIFY_NOT_REACHED();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
EGLBoolean result = eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, egl_context);
|
EGLBoolean result = eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, egl_context);
|
||||||
if (result == EGL_FALSE) {
|
if (result == EGL_FALSE) {
|
||||||
dbgln("eglMakeCurrent failed");
|
return Error::from_string_view(format_egl_error(eglGetError()));
|
||||||
VERIFY_NOT_REACHED();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return make<EGLContextWrapper>(egl_context);
|
return make<EGLContextWrapper>(egl_context);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
OwnPtr<Context> Context::create()
|
ErrorOr<NonnullOwnPtr<Context>> Context::create()
|
||||||
{
|
{
|
||||||
#ifdef AK_OS_MACOS
|
#ifdef AK_OS_MACOS
|
||||||
return make_context_cgl();
|
return make_context_cgl();
|
||||||
|
|
|
@ -20,7 +20,7 @@ namespace AccelGfx {
|
||||||
|
|
||||||
class Context {
|
class Context {
|
||||||
public:
|
public:
|
||||||
static OwnPtr<Context> create();
|
static ErrorOr<NonnullOwnPtr<Context>> create();
|
||||||
|
|
||||||
Context()
|
Context()
|
||||||
{
|
{
|
||||||
|
|
|
@ -157,7 +157,7 @@ public:
|
||||||
TODO();
|
TODO();
|
||||||
}
|
}
|
||||||
|
|
||||||
AccelGfxContext(OwnPtr<AccelGfx::Context> context, NonnullRefPtr<AccelGfx::Canvas> canvas)
|
AccelGfxContext(NonnullOwnPtr<AccelGfx::Context> context, NonnullRefPtr<AccelGfx::Canvas> canvas)
|
||||||
: m_context(move(context))
|
: m_context(move(context))
|
||||||
, m_canvas(move(canvas))
|
, m_canvas(move(canvas))
|
||||||
{
|
{
|
||||||
|
@ -169,7 +169,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
OwnPtr<AccelGfx::Context> m_context;
|
NonnullOwnPtr<AccelGfx::Context> m_context;
|
||||||
NonnullRefPtr<AccelGfx::Canvas> m_canvas;
|
NonnullRefPtr<AccelGfx::Canvas> m_canvas;
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
@ -301,9 +301,13 @@ private:
|
||||||
static OwnPtr<AccelGfxContext> make_accelgfx_context(Gfx::Bitmap& bitmap)
|
static OwnPtr<AccelGfxContext> make_accelgfx_context(Gfx::Bitmap& bitmap)
|
||||||
{
|
{
|
||||||
auto context = AccelGfx::Context::create();
|
auto context = AccelGfx::Context::create();
|
||||||
|
if (context.is_error()) {
|
||||||
|
dbgln("Failed to create AccelGfx context: {}", context.error().string_literal());
|
||||||
|
return {};
|
||||||
|
}
|
||||||
auto canvas = AccelGfx::Canvas::create(bitmap.size());
|
auto canvas = AccelGfx::Canvas::create(bitmap.size());
|
||||||
canvas->bind();
|
canvas->bind();
|
||||||
return make<AccelGfxContext>(move(context), move(canvas));
|
return make<AccelGfxContext>(context.release_value(), move(canvas));
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -72,7 +72,12 @@ PageClient::PageClient(PageHost& owner, u64 id)
|
||||||
|
|
||||||
#ifdef HAS_ACCELERATED_GRAPHICS
|
#ifdef HAS_ACCELERATED_GRAPHICS
|
||||||
if (s_use_gpu_painter) {
|
if (s_use_gpu_painter) {
|
||||||
m_accelerated_graphics_context = AccelGfx::Context::create();
|
auto context = AccelGfx::Context::create();
|
||||||
|
if (context.is_error()) {
|
||||||
|
dbgln("Failed to create AccelGfx context: {}", context.error());
|
||||||
|
VERIFY_NOT_REACHED();
|
||||||
|
}
|
||||||
|
m_accelerated_graphics_context = AccelGfx::Context::create().release_value();
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue