From c083a7cc9015b52e12b0fb78e5a488ab1f5b68f6 Mon Sep 17 00:00:00 2001 From: Yan Date: Wed, 5 Apr 2023 16:04:03 +0200 Subject: [PATCH] Force OpenGL Core Profile context on macOS By default, SDL creates an OpenGL 2.1 context on macOS for an OpenGL renderer. As a consequence, mipmapping is not supported. Force to use a core profile context, to get a higher version. Before: INFO: Renderer: opengl INFO: OpenGL version: 2.1 NVIDIA-14.0.32 355.11.11.10.10.143 WARN: Trilinear filtering disabled (OpenGL 3.0+ or ES 2.0+ required) After: INFO: Renderer: opengl DEBUG: Creating OpenGL Core Profile context INFO: OpenGL version: 4.1 NVIDIA-14.0.32 355.11.11.10.10.143 INFO: Trilinear filtering enabled when running with: scrcpy --verbosity=debug --render-driver=opengl Note: Since SDL_CreateRenderer() causes a fallback to OpenGL 2.1, the profile and version attributes have to be set and the context created _after_. PR #3895 Signed-off-by: Romain Vimont --- app/src/display.c | 19 +++++++++++++++++++ app/src/display.h | 8 ++++++++ 2 files changed, 27 insertions(+) diff --git a/app/src/display.c b/app/src/display.c index 96ff9e06..4852952b 100644 --- a/app/src/display.c +++ b/app/src/display.c @@ -23,6 +23,22 @@ sc_display_init(struct sc_display *display, SDL_Window *window, bool mipmaps) { // starts with "opengl" bool use_opengl = renderer_name && !strncmp(renderer_name, "opengl", 6); if (use_opengl) { + +#ifdef SC_DISPLAY_FORCE_OPENGL_CORE_PROFILE + // Persuade macOS to give us something better than OpenGL 2.1. + // If we create a Core Profile context, we get the best OpenGL version. + SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, + SDL_GL_CONTEXT_PROFILE_CORE); + + LOGD("Creating OpenGL Core Profile context"); + display->gl_context = SDL_GL_CreateContext(window); + if (!display->gl_context) { + LOGE("Could not create OpenGL context: %s", SDL_GetError()); + SDL_DestroyRenderer(display->renderer); + return false; + } +#endif + struct sc_opengl *gl = &display->gl; sc_opengl_init(gl); @@ -51,6 +67,9 @@ sc_display_init(struct sc_display *display, SDL_Window *window, bool mipmaps) { void sc_display_destroy(struct sc_display *display) { +#ifdef SC_DISPLAY_FORCE_OPENGL_CORE_PROFILE + SDL_GL_DeleteContext(display->gl_context); +#endif if (display->texture) { SDL_DestroyTexture(display->texture); } diff --git a/app/src/display.h b/app/src/display.h index 8856afcd..e30b4822 100644 --- a/app/src/display.h +++ b/app/src/display.h @@ -10,11 +10,19 @@ #include "coords.h" #include "opengl.h" +#ifdef __APPLE__ +# define SC_DISPLAY_FORCE_OPENGL_CORE_PROFILE +#endif + struct sc_display { SDL_Renderer *renderer; SDL_Texture *texture; struct sc_opengl gl; +#ifdef SC_DISPLAY_FORCE_OPENGL_CORE_PROFILE + SDL_GLContext *gl_context; +#endif + bool mipmaps; };