From 61a2e59d8793b75c788b620a2102705a20123c46 Mon Sep 17 00:00:00 2001 From: Aliaksandr Kalenik Date: Sun, 12 Nov 2023 14:18:00 +0100 Subject: [PATCH] LibAccelGfx+WebContent: Add GPU painter support on macOS With these changes it is now possible to create OpenGL context on macOS and run GPU-painter. For now only QT client has a CLI param that turns it on though. --- Meta/CMake/accelerated_graphics.cmake | 5 ++ Userland/Libraries/LibAccelGfx/Context.cpp | 53 +++++++++++++++++++++- Userland/Libraries/LibAccelGfx/Context.h | 18 +++----- Userland/Libraries/LibAccelGfx/GL.h | 8 +++- Userland/Libraries/LibAccelGfx/Painter.cpp | 2 +- 5 files changed, 70 insertions(+), 16 deletions(-) diff --git a/Meta/CMake/accelerated_graphics.cmake b/Meta/CMake/accelerated_graphics.cmake index 8f7f842e5d..248a3fbde5 100644 --- a/Meta/CMake/accelerated_graphics.cmake +++ b/Meta/CMake/accelerated_graphics.cmake @@ -12,3 +12,8 @@ if (OPENGL_FOUND) else() set(HAS_ACCELERATED_GRAPHICS OFF CACHE BOOL "" FORCE) endif() + +if (APPLE) + set(HAS_ACCELERATED_GRAPHICS ON CACHE BOOL "" FORCE) + set(ACCEL_GFX_LIBS "-framework OpenGL" CACHE STRING "" FORCE) +endif() diff --git a/Userland/Libraries/LibAccelGfx/Context.cpp b/Userland/Libraries/LibAccelGfx/Context.cpp index fea12f67f4..da00e9f66f 100644 --- a/Userland/Libraries/LibAccelGfx/Context.cpp +++ b/Userland/Libraries/LibAccelGfx/Context.cpp @@ -6,6 +6,14 @@ #include +#ifdef AK_OS_MACOS +# define GL_SILENCE_DEPRECATION +# include +# include +# include +# include +#endif + namespace AccelGfx { Context& Context::the() @@ -16,7 +24,38 @@ Context& Context::the() return *s_the; } -OwnPtr Context::create() +#ifdef AK_OS_MACOS +static void make_context_cgl() +{ + CGLContextObj context = NULL; + CGLPixelFormatAttribute attributes[4] = { + kCGLPFAOpenGLProfile, + (CGLPixelFormatAttribute)kCGLOGLPVersion_3_2_Core, + kCGLPFAAccelerated, + (CGLPixelFormatAttribute)0 + }; + + CGLPixelFormatObj pixelFormat = NULL; + GLint numPixelFormats = 0; + CGLError error = CGLChoosePixelFormat(attributes, &pixelFormat, &numPixelFormats); + if (error) { + VERIFY_NOT_REACHED(); + } + + error = CGLCreateContext(pixelFormat, NULL, &context); + if (error) { + VERIFY_NOT_REACHED(); + } + + error = CGLSetCurrentContext(context); + if (error) { + VERIFY_NOT_REACHED(); + } + + VERIFY(glGetError() == GL_NO_ERROR); +} +#else +static void make_context_egl() { EGLDisplay egl_display = eglGetDisplay(EGL_DEFAULT_DISPLAY); @@ -60,8 +99,18 @@ OwnPtr Context::create() dbgln("eglMakeCurrent failed"); VERIFY_NOT_REACHED(); } +} +#endif - return make(egl_display, egl_context, egl_config); +OwnPtr Context::create() +{ +#ifdef AK_OS_MACOS + make_context_cgl(); +#else + make_context_egl(); +#endif + + return make(); } } diff --git a/Userland/Libraries/LibAccelGfx/Context.h b/Userland/Libraries/LibAccelGfx/Context.h index 6854d8fa3f..4fc89c3958 100644 --- a/Userland/Libraries/LibAccelGfx/Context.h +++ b/Userland/Libraries/LibAccelGfx/Context.h @@ -9,10 +9,12 @@ #include #include +#ifndef AK_OS_MACOS // Make sure egl.h doesn't give us definitions from X11 headers -#define EGL_NO_X11 -#include -#undef EGL_NO_X11 +# define EGL_NO_X11 +# include +# undef EGL_NO_X11 +#endif namespace AccelGfx { @@ -22,17 +24,9 @@ public: static OwnPtr create(); - Context(EGLDisplay egl_display, EGLContext egl_context, EGLConfig egl_config) - : m_egl_display(egl_display) - , m_egl_context(egl_context) - , m_egl_config(egl_config) + Context() { } - -private: - EGLDisplay m_egl_display { nullptr }; - EGLContext m_egl_context { nullptr }; - EGLConfig m_egl_config { nullptr }; }; } diff --git a/Userland/Libraries/LibAccelGfx/GL.h b/Userland/Libraries/LibAccelGfx/GL.h index ea6e628c2c..0d59c701d1 100644 --- a/Userland/Libraries/LibAccelGfx/GL.h +++ b/Userland/Libraries/LibAccelGfx/GL.h @@ -7,7 +7,13 @@ #pragma once #include -#include +#ifdef AK_OS_MACOS +# define GL_SILENCE_DEPRECATION +# include +# include +#else +# include +#endif #include namespace AccelGfx::GL { diff --git a/Userland/Libraries/LibAccelGfx/Painter.cpp b/Userland/Libraries/LibAccelGfx/Painter.cpp index 2144877951..0651fc7889 100644 --- a/Userland/Libraries/LibAccelGfx/Painter.cpp +++ b/Userland/Libraries/LibAccelGfx/Painter.cpp @@ -75,7 +75,7 @@ in vec2 vTextureCoord; uniform sampler2D uSampler; out vec4 fragColor; void main() { - fragColor = texture2D(uSampler, vTextureCoord) * uColor; + fragColor = texture(uSampler, vTextureCoord) * uColor; } )";